Add: Overlay cargo icon in vehicle/depot list when holding shift+ctrl. (#12938)
[openttd-github.git] / src / game / game_info.cpp
blobbdc94d7399eaa3a059f87a2c9a3d5fa69a20ca68
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"
17 #include "../safeguards.h"
19 /**
20 * Check if the API version provided by the Game is supported.
21 * @param api_version The API version as provided by the Game.
23 static bool CheckAPIVersion(const std::string &api_version)
25 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", "12", "13", "14", "15" };
26 return versions.find(api_version) != versions.end();
29 #if defined(_WIN32)
30 #undef GetClassName
31 #endif /* _WIN32 */
32 template <> const char *GetClassName<GameInfo, ScriptType::GS>() { return "GSInfo"; }
34 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
36 /* Create the GSInfo class, and add the RegisterGS function */
37 DefSQClass<GameInfo, ScriptType::GS> SQGSInfo("GSInfo");
38 SQGSInfo.PreRegister(engine);
39 SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
40 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
41 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
42 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
43 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
44 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
45 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
46 SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
48 SQGSInfo.PostRegister(engine);
49 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
52 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
54 /* Get the GameInfo */
55 SQUserPointer instance = nullptr;
56 if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
57 GameInfo *info = (GameInfo *)instance;
59 SQInteger res = ScriptInfo::Constructor(vm, info);
60 if (res != 0) return res;
62 if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
63 if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
64 } else {
65 info->min_loadable_version = info->GetVersion();
67 /* When there is an IsSelectable function, call it. */
68 if (info->engine->MethodExists(info->SQ_instance, "IsDeveloperOnly")) {
69 if (!info->engine->CallBoolMethod(info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
70 } else {
71 info->is_developer_only = false;
73 /* Try to get the API version the AI is written for. */
74 if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
75 if (!info->engine->CallStringMethod(info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
76 if (!CheckAPIVersion(info->api_version)) {
77 Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
78 return SQ_ERROR;
81 /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
82 sq_setinstanceup(vm, 2, nullptr);
83 /* Register the Game to the base system */
84 info->GetScanner()->RegisterScript(info);
85 return 0;
88 GameInfo::GameInfo() :
89 min_loadable_version(0),
90 is_developer_only(false)
94 bool GameInfo::CanLoadFromVersion(int version) const
96 if (version == -1) return true;
97 return version >= this->min_loadable_version && version <= this->GetVersion();
101 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
103 /* Create the GameLibrary class, and add the RegisterLibrary function */
104 engine->AddClassBegin("GSLibrary");
105 engine->AddClassEnd();
106 engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
109 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
111 /* Create a new library */
112 GameLibrary *library = new GameLibrary();
114 SQInteger res = ScriptInfo::Constructor(vm, library);
115 if (res != 0) {
116 delete library;
117 return res;
120 /* Cache the category */
121 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
122 delete library;
123 return SQ_ERROR;
126 /* Register the Library to the base system */
127 library->GetScanner()->RegisterScript(library);
129 return 0;