summaryrefslogtreecommitdiff
path: root/src/game
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2011-12-19 20:56:59 +0000
committertruebrain <truebrain@openttd.org>2011-12-19 20:56:59 +0000
commit963802e9a7cb67f51cb7e6ffe9d33a02cfe93821 (patch)
treed8b2d5c1c2d90c7cb1a02403cea942ab52c2e501 /src/game
parent83f2785f54de1c31267c5586464c97fb39f95b5a (diff)
downloadopenttd-963802e9a7cb67f51cb7e6ffe9d33a02cfe93821.tar.xz
(svn r23612) -Add: allow importing libraries in the same way as AI does, only with GS prefix (and in game/library)
Diffstat (limited to 'src/game')
-rw-r--r--src/game/game.hpp19
-rw-r--r--src/game/game_core.cpp58
-rw-r--r--src/game/game_info.cpp37
-rw-r--r--src/game/game_info.hpp25
-rw-r--r--src/game/game_instance.cpp3
-rw-r--r--src/game/game_scanner.cpp36
-rw-r--r--src/game/game_scanner.hpp23
7 files changed, 177 insertions, 24 deletions
diff --git a/src/game/game.hpp b/src/game/game.hpp
index b3a87b1f1..ac85fe133 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -68,23 +68,34 @@ public:
/** Wrapper function for GameScanner::GetConsoleList */
static char *GetConsoleList(char *p, const char *last, bool newest_only = false);
+ /** Wrapper function for GameScanner::GetConsoleLibraryList */
+ static char *GetConsoleLibraryList(char *p, const char *last);
/** Wrapper function for GameScanner::GetInfoList */
static const ScriptInfoList *GetInfoList();
/** Wrapper function for GameScanner::GetUniqueInfoList */
static const ScriptInfoList *GetUniqueInfoList();
/** Wrapper function for GameScannerInfo::FindInfo */
static class GameInfo *FindInfo(const char *name, int version, bool force_exact_match);
+ /** Wrapper function for GameScanner::FindLibrary */
+ static class GameLibrary *FindLibrary(const char *library, int version);
/**
* Get the current active instance.
*/
static class GameInstance *GetInstance() { return Game::instance; }
+#if defined(ENABLE_NETWORK)
+ /** Wrapper function for GameScanner::HasGame */
+ static bool HasGame(const struct ContentInfo *ci, bool md5sum);
+ static bool HasGameLibrary(const ContentInfo *ci, bool md5sum);
+#endif
+
private:
- static uint frame_counter; ///< Tick counter for the Game code.
- static class GameInstance *instance; ///< Instance to the current active Game.
- static class GameScannerInfo *scanner; ///< Scanner for Game scripts.
- static class GameInfo *info; ///< Current selected GameInfo.
+ static uint frame_counter; ///< Tick counter for the Game code.
+ static class GameInstance *instance; ///< Instance to the current active Game.
+ static class GameScannerInfo *scanner_info; ///< Scanner for Game scripts.
+ static class GameScannerLibrary *scanner_library; ///< Scanner for GS Libraries.
+ static class GameInfo *info; ///< Current selected GameInfo.
};
#endif /* GAME_HPP */
diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp
index fa63c42e9..13f9cb47d 100644
--- a/src/game/game_core.cpp
+++ b/src/game/game_core.cpp
@@ -26,7 +26,8 @@
/* static */ uint Game::frame_counter = 0;
/* static */ GameInfo *Game::info = NULL;
/* static */ GameInstance *Game::instance = NULL;
-/* static */ GameScannerInfo *Game::scanner = NULL;
+/* static */ GameScannerInfo *Game::scanner_info = NULL;
+/* static */ GameScannerLibrary *Game::scanner_library = NULL;
/* static */ void Game::GameLoop()
{
@@ -52,10 +53,12 @@
Game::frame_counter = 0;
- if (Game::scanner == NULL) {
+ if (Game::scanner_info == NULL) {
TarScanner::DoScan(TarScanner::GAME);
- Game::scanner = new GameScannerInfo();
- Game::scanner->Initialize();
+ Game::scanner_info = new GameScannerInfo();
+ Game::scanner_info->Initialize();
+ Game::scanner_library = new GameScannerLibrary();
+ Game::scanner_library->Initialize();
}
}
@@ -90,8 +93,10 @@
if (keepConfig) {
Rescan();
} else {
- delete Game::scanner;
- Game::scanner = NULL;
+ delete Game::scanner_info;
+ delete Game::scanner_library;
+ Game::scanner_info = NULL;
+ Game::scanner_library = NULL;
if (_settings_game.game_config != NULL) {
delete _settings_game.game_config;
@@ -132,7 +137,8 @@
{
TarScanner::DoScan(TarScanner::GAME);
- Game::scanner->RescanDir();
+ Game::scanner_info->RescanDir();
+ Game::scanner_library->RescanDir();
ResetConfig();
InvalidateWindowData(WC_AI_LIST, 0, 1);
@@ -166,20 +172,50 @@
/* static */ char *Game::GetConsoleList(char *p, const char *last, bool newest_only)
{
- return Game::scanner->GetConsoleList(p, last, newest_only);
+ return Game::scanner_info->GetConsoleList(p, last, newest_only);
+}
+
+/* static */ char *Game::GetConsoleLibraryList(char *p, const char *last)
+{
+ return Game::scanner_library->GetConsoleList(p, last, true);
}
/* static */ const ScriptInfoList *Game::GetInfoList()
{
- return Game::scanner->GetInfoList();
+ return Game::scanner_info->GetInfoList();
}
/* static */ const ScriptInfoList *Game::GetUniqueInfoList()
{
- return Game::scanner->GetUniqueInfoList();
+ return Game::scanner_info->GetUniqueInfoList();
}
/* static */ GameInfo *Game::FindInfo(const char *name, int version, bool force_exact_match)
{
- return Game::scanner->FindInfo(name, version, force_exact_match);
+ return Game::scanner_info->FindInfo(name, version, force_exact_match);
+}
+
+/* static */ GameLibrary *Game::FindLibrary(const char *library, int version)
+{
+ return Game::scanner_library->FindLibrary(library, version);
}
+
+#if defined(ENABLE_NETWORK)
+
+/**
+ * Check whether we have an Game (library) with the exact characteristics as ci.
+ * @param ci the characteristics to search on (shortname and md5sum)
+ * @param md5sum whether to check the MD5 checksum
+ * @return true iff we have an Game (library) matching.
+ */
+/* static */ bool Game::HasGame(const ContentInfo *ci, bool md5sum)
+{
+ return Game::scanner_info->HasScript(ci, md5sum);
+}
+
+/* static */ bool Game::HasGameLibrary(const ContentInfo *ci, bool md5sum)
+{
+ return Game::scanner_library->HasScript(ci, md5sum);
+}
+
+#endif /* defined(ENABLE_NETWORK) */
diff --git a/src/game/game_info.cpp b/src/game/game_info.cpp
index 722a9fc91..2d304bc94 100644
--- a/src/game/game_info.cpp
+++ b/src/game/game_info.cpp
@@ -99,3 +99,40 @@ bool GameInfo::CanLoadFromVersion(int version) const
if (version == -1) return true;
return version >= this->min_loadable_version && version <= this->GetVersion();
}
+
+
+GameLibrary::~GameLibrary()
+{
+ free(this->category);
+}
+
+/* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
+{
+ /* Create the GameLibrary class, and add the RegisterLibrary function */
+ engine->AddClassBegin("GSLibrary");
+ engine->AddClassEnd();
+ engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
+}
+
+/* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
+{
+ /* Create a new library */
+ GameLibrary *library = new GameLibrary();
+
+ SQInteger res = ScriptInfo::Constructor(vm, library);
+ if (res != 0) {
+ delete library;
+ return res;
+ }
+
+ /* Cache the category */
+ if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
+ delete library;
+ return SQ_ERROR;
+ }
+
+ /* Register the Library to the base system */
+ library->GetScanner()->RegisterScript(library);
+
+ return 0;
+}
diff --git a/src/game/game_info.hpp b/src/game/game_info.hpp
index e476e8e9c..8fe588dd0 100644
--- a/src/game/game_info.hpp
+++ b/src/game/game_info.hpp
@@ -46,4 +46,29 @@ private:
const char *api_version; ///< API version used by this Game.
};
+/** All static information from an Game library like name, version, etc. */
+class GameLibrary : public ScriptInfo {
+public:
+ GameLibrary() : ScriptInfo(), category(NULL) {};
+ ~GameLibrary();
+
+ /**
+ * Register the functions of this class.
+ */
+ static void RegisterAPI(Squirrel *engine);
+
+ /**
+ * Create an GSLibrary, using this GSInfo as start-template.
+ */
+ static SQInteger Constructor(HSQUIRRELVM vm);
+
+ /**
+ * Get the category this library is in.
+ */
+ const char *GetCategory() const { return this->category; }
+
+private:
+ const char *category; ///< The category this library is in.
+};
+
#endif /* GAME_INFO_HPP */
diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp
index 4668f5436..be7bb20e5 100644
--- a/src/game/game_instance.cpp
+++ b/src/game/game_instance.cpp
@@ -68,8 +68,7 @@ int GameInstance::GetSetting(const char *name)
ScriptInfo *GameInstance::FindLibrary(const char *library, int version)
{
- /* 'import' is not supported with GameScripts */
- return NULL;
+ return (ScriptInfo *)Game::FindLibrary(library, version);
}
/**
diff --git a/src/game/game_scanner.cpp b/src/game/game_scanner.cpp
index 54b684c3e..056c1378a 100644
--- a/src/game/game_scanner.cpp
+++ b/src/game/game_scanner.cpp
@@ -21,11 +21,6 @@
#include "../script/api/script_controller.hpp"
-GameScannerInfo::GameScannerInfo() :
- ScriptScanner()
-{
-}
-
void GameScannerInfo::Initialize()
{
ScriptScanner::Initialize("GSScanner");
@@ -87,3 +82,34 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo
return info;
}
+
+
+void GameScannerLibrary::Initialize()
+{
+ ScriptScanner::Initialize("GSScanner");
+}
+
+void GameScannerLibrary::GetScriptName(ScriptInfo *info, char *name, int len)
+{
+ GameLibrary *library = static_cast<GameLibrary *>(info);
+ snprintf(name, len, "%s.%s", library->GetCategory(), library->GetInstanceName());
+}
+
+void GameScannerLibrary::RegisterAPI(class Squirrel *engine)
+{
+ GameLibrary::RegisterAPI(engine);
+}
+
+GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
+{
+ /* Internally we store libraries as 'library.version' */
+ char library_name[1024];
+ snprintf(library_name, sizeof(library_name), "%s.%d", library, version);
+ strtolower(library_name);
+
+ /* Check if the library + version exists */
+ ScriptInfoList::iterator iter = this->info_list.find(library_name);
+ if (iter == this->info_list.end()) return NULL;
+
+ return static_cast<GameLibrary *>((*iter).second);
+}
diff --git a/src/game/game_scanner.hpp b/src/game/game_scanner.hpp
index bf6701221..8f43d145c 100644
--- a/src/game/game_scanner.hpp
+++ b/src/game/game_scanner.hpp
@@ -16,8 +16,6 @@
class GameScannerInfo : public ScriptScanner {
public:
- GameScannerInfo();
-
/* virtual */ void Initialize();
/**
@@ -37,4 +35,25 @@ protected:
/* virtual */ void RegisterAPI(class Squirrel *engine);
};
+
+class GameScannerLibrary : public ScriptScanner {
+public:
+ /* virtual */ void Initialize();
+
+ /**
+ * Find a library in the pool.
+ * @param library The library name to find.
+ * @param version The version the library should have.
+ * @return The library if found, NULL otherwise.
+ */
+ class GameLibrary *FindLibrary(const char *library, int version);
+
+protected:
+ /* virtual */ void GetScriptName(ScriptInfo *info, char *name, int len);
+ /* virtual */ const char *GetFileName() const { return PATHSEP "library.nut"; }
+ /* virtual */ Subdirectory GetDirectory() const { return GAME_LIBRARY_DIR; }
+ /* virtual */ const char *GetScannerName() const { return "GS Libraries"; }
+ /* virtual */ void RegisterAPI(class Squirrel *engine);
+};
+
#endif /* GAME_SCANNER_HPP */