(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / game / game_info.cpp
blob39088b8462fcd2c0e5264f7a0bba6609783a3800
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;
31 #if defined(WIN32)
32 #undef GetClassName
33 #endif /* WIN32 */
34 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
36 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
38 /* Create the GSInfo class, and add the RegisterGS function */
39 DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
40 SQGSInfo.PreRegister(engine);
41 SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
42 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
43 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
44 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
45 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
46 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
47 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
48 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
50 SQGSInfo.PostRegister(engine);
51 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
54 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
56 /* Get the GameInfo */
57 SQUserPointer instance = NULL;
58 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");
59 GameInfo *info = (GameInfo *)instance;
61 SQInteger res = ScriptInfo::Constructor(vm, info);
62 if (res != 0) return res;
64 if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
65 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
66 } else {
67 info->min_loadable_version = info->GetVersion();
69 /* When there is an IsSelectable function, call it. */
70 if (info->engine->MethodExists(*info->SQ_instance, "IsDeveloperOnly")) {
71 if (!info->engine->CallBoolMethod(*info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
72 } else {
73 info->is_developer_only = false;
75 /* Try to get the API version the AI is written for. */
76 if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
77 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
78 if (!CheckAPIVersion(info->api_version)) {
79 DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
80 return SQ_ERROR;
83 /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
84 sq_setinstanceup(vm, 2, NULL);
85 /* Register the Game to the base system */
86 info->GetScanner()->RegisterScript(info);
87 return 0;
90 GameInfo::GameInfo() :
91 min_loadable_version(0),
92 is_developer_only(false),
93 api_version(NULL)
97 GameInfo::~GameInfo()
99 free(this->api_version);
102 bool GameInfo::CanLoadFromVersion(int version) const
104 if (version == -1) return true;
105 return version >= this->min_loadable_version && version <= this->GetVersion();
109 GameLibrary::~GameLibrary()
111 free(this->category);
114 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
116 /* Create the GameLibrary class, and add the RegisterLibrary function */
117 engine->AddClassBegin("GSLibrary");
118 engine->AddClassEnd();
119 engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
122 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
124 /* Create a new library */
125 GameLibrary *library = new GameLibrary();
127 SQInteger res = ScriptInfo::Constructor(vm, library);
128 if (res != 0) {
129 delete library;
130 return res;
133 /* Cache the category */
134 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
135 delete library;
136 return SQ_ERROR;
139 /* Register the Library to the base system */
140 library->GetScanner()->RegisterScript(library);
142 return 0;