summaryrefslogtreecommitdiff
path: root/src/game/game_info.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game/game_info.cpp')
-rw-r--r--src/game/game_info.cpp101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/game/game_info.cpp b/src/game/game_info.cpp
new file mode 100644
index 000000000..722a9fc91
--- /dev/null
+++ b/src/game/game_info.cpp
@@ -0,0 +1,101 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
+ * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** @file game_info.cpp Implementation of GameInfo */
+
+#include "../stdafx.h"
+
+#include "../script/squirrel_helper.hpp"
+#include "../script/squirrel_class.hpp"
+#include "game_info.hpp"
+#include "game_scanner.hpp"
+#include "../settings_type.h"
+#include "../debug.h"
+#include "../rev.h"
+#include "game.hpp"
+
+/**
+ * Check if the API version provided by the Game is supported.
+ * @param api_version The API version as provided by the Game.
+ */
+static bool CheckAPIVersion(const char *api_version)
+{
+ return strcmp(api_version, "1.2") == 0;
+}
+
+#if defined(WIN32)
+#undef GetClassName
+#endif /* WIN32 */
+template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
+
+/* static */ void GameInfo::RegisterAPI(Squirrel *engine)
+{
+ /* Create the GSInfo class, and add the RegisterGS function */
+ DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
+ SQGSInfo.PreRegister(engine);
+ SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
+ SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
+ SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
+ SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
+ SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
+ SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
+ SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
+ SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
+
+ SQGSInfo.PostRegister(engine);
+ engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
+}
+
+/* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
+{
+ /* Get the GameInfo */
+ SQUserPointer instance = NULL;
+ if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, _SC("Pass an instance of a child class of GameInfo to RegisterGame"));
+ GameInfo *info = (GameInfo *)instance;
+
+ SQInteger res = ScriptInfo::Constructor(vm, info);
+ if (res != 0) return res;
+
+ if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
+ if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
+ } else {
+ info->min_loadable_version = info->GetVersion();
+ }
+
+ /* Try to get the API version the AI is written for. */
+ if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
+ if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
+ if (!CheckAPIVersion(info->api_version)) {
+ DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
+ return SQ_ERROR;
+ }
+
+ /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
+ sq_setinstanceup(vm, 2, NULL);
+ /* Register the Game to the base system */
+ info->GetScanner()->RegisterScript(info);
+ return 0;
+}
+
+GameInfo::GameInfo() :
+ min_loadable_version(0),
+ api_version(NULL)
+{
+}
+
+GameInfo::~GameInfo()
+{
+ free(this->api_version);
+}
+
+bool GameInfo::CanLoadFromVersion(int version) const
+{
+ if (version == -1) return true;
+ return version >= this->min_loadable_version && version <= this->GetVersion();
+}