Add: INR currency (#8136)
[openttd-github.git] / src / game / game_info.cpp
blob77725c5e7bc85c7fb4b2986070f0a67f74540793
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file game_info.cpp Implementation of GameInfo */
10 #include "../stdafx.h"
12 #include "../script/squirrel_class.hpp"
13 #include "game_info.hpp"
14 #include "game_scanner.hpp"
15 #include "../debug.h"
16 #include <set>
18 #include "../safeguards.h"
20 /**
21 * Check if the API version provided by the Game is supported.
22 * @param api_version The API version as provided by the Game.
24 static bool CheckAPIVersion(const char *api_version)
26 static const std::set<std::string> versions = {"1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "1.10", "1.11"};
27 return versions.find(api_version) != versions.end();
30 #if defined(_WIN32)
31 #undef GetClassName
32 #endif /* _WIN32 */
33 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
35 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
37 /* Create the GSInfo class, and add the RegisterGS function */
38 DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
39 SQGSInfo.PreRegister(engine);
40 SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
41 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
42 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
43 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
44 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
45 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
46 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
47 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
49 SQGSInfo.PostRegister(engine);
50 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
53 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
55 /* Get the GameInfo */
56 SQUserPointer instance = nullptr;
57 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
58 GameInfo *info = (GameInfo *)instance;
60 SQInteger res = ScriptInfo::Constructor(vm, info);
61 if (res != 0) return res;
63 if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
64 if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
65 } else {
66 info->min_loadable_version = info->GetVersion();
68 /* When there is an IsSelectable function, call it. */
69 if (info->engine->MethodExists(*info->SQ_instance, "IsDeveloperOnly")) {
70 if (!info->engine->CallBoolMethod(*info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
71 } else {
72 info->is_developer_only = false;
74 /* Try to get the API version the AI is written for. */
75 if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
76 if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
77 if (!CheckAPIVersion(info->api_version)) {
78 DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
79 return SQ_ERROR;
82 /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
83 sq_setinstanceup(vm, 2, nullptr);
84 /* Register the Game to the base system */
85 info->GetScanner()->RegisterScript(info);
86 return 0;
89 GameInfo::GameInfo() :
90 min_loadable_version(0),
91 is_developer_only(false),
92 api_version(nullptr)
96 GameInfo::~GameInfo()
98 free(this->api_version);
101 bool GameInfo::CanLoadFromVersion(int version) const
103 if (version == -1) return true;
104 return version >= this->min_loadable_version && version <= this->GetVersion();
108 GameLibrary::~GameLibrary()
110 free(this->category);
113 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
115 /* Create the GameLibrary class, and add the RegisterLibrary function */
116 engine->AddClassBegin("GSLibrary");
117 engine->AddClassEnd();
118 engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
121 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
123 /* Create a new library */
124 GameLibrary *library = new GameLibrary();
126 SQInteger res = ScriptInfo::Constructor(vm, library);
127 if (res != 0) {
128 delete library;
129 return res;
132 /* Cache the category */
133 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
134 delete library;
135 return SQ_ERROR;
138 /* Register the Library to the base system */
139 library->GetScanner()->RegisterScript(library);
141 return 0;