(svn r28004) -Update from Eints:
[openttd.git] / src / game / game_info.cpp
blob976153296573e2beee71a6119cf3b4108e6d34d9
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file game_info.cpp Implementation of GameInfo */
12 #include "../stdafx.h"
14 #include "../script/squirrel_class.hpp"
15 #include "game_info.hpp"
16 #include "game_scanner.hpp"
17 #include "../debug.h"
19 #include "../safeguards.h"
21 /**
22 * Check if the API version provided by the Game is supported.
23 * @param api_version The API version as provided by the Game.
25 static bool CheckAPIVersion(const char *api_version)
27 return strcmp(api_version, "1.2") == 0 || strcmp(api_version, "1.3") == 0 || strcmp(api_version, "1.4") == 0 ||
28 strcmp(api_version, "1.5") == 0 || strcmp(api_version, "1.6") == 0 || strcmp(api_version, "1.7") == 0 ||
29 strcmp(api_version, "1.8") == 0 || strcmp(api_version, "1.9") == 0;
32 #if defined(WIN32)
33 #undef GetClassName
34 #endif /* WIN32 */
35 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
37 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
39 /* Create the GSInfo class, and add the RegisterGS function */
40 DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
41 SQGSInfo.PreRegister(engine);
42 SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
43 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
44 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
45 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
46 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
47 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
48 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
49 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
51 SQGSInfo.PostRegister(engine);
52 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
55 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
57 /* Get the GameInfo */
58 SQUserPointer instance = NULL;
59 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
60 GameInfo *info = (GameInfo *)instance;
62 SQInteger res = ScriptInfo::Constructor(vm, info);
63 if (res != 0) return res;
65 if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
66 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
67 } else {
68 info->min_loadable_version = info->GetVersion();
70 /* When there is an IsSelectable function, call it. */
71 if (info->engine->MethodExists(*info->SQ_instance, "IsDeveloperOnly")) {
72 if (!info->engine->CallBoolMethod(*info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
73 } else {
74 info->is_developer_only = false;
76 /* Try to get the API version the AI is written for. */
77 if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
78 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
79 if (!CheckAPIVersion(info->api_version)) {
80 DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
81 return SQ_ERROR;
84 /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
85 sq_setinstanceup(vm, 2, NULL);
86 /* Register the Game to the base system */
87 info->GetScanner()->RegisterScript(info);
88 return 0;
91 GameInfo::GameInfo() :
92 min_loadable_version(0),
93 is_developer_only(false),
94 api_version(NULL)
98 GameInfo::~GameInfo()
100 free(this->api_version);
103 bool GameInfo::CanLoadFromVersion(int version) const
105 if (version == -1) return true;
106 return version >= this->min_loadable_version && version <= this->GetVersion();
110 GameLibrary::~GameLibrary()
112 free(this->category);
115 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
117 /* Create the GameLibrary class, and add the RegisterLibrary function */
118 engine->AddClassBegin("GSLibrary");
119 engine->AddClassEnd();
120 engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
123 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
125 /* Create a new library */
126 GameLibrary *library = new GameLibrary();
128 SQInteger res = ScriptInfo::Constructor(vm, library);
129 if (res != 0) {
130 delete library;
131 return res;
134 /* Cache the category */
135 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
136 delete library;
137 return SQ_ERROR;
140 /* Register the Library to the base system */
141 library->GetScanner()->RegisterScript(library);
143 return 0;