OpenTTD Source: src/game/game_info.cpp Source File

OpenTTD Source 14.1

game_info.cpp
Go to the documentation of this file.
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 */
7
10 #include "../stdafx.h"
11
12 #include "../script/squirrel_class.hpp"
13 #include "game_info.hpp"
14 #include "game_scanner.hpp"
15 #include "../debug.h"
16
17 #include "../safeguards.h"
18
23 static bool CheckAPIVersion(const std::string &api_version)
24 {
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" };
26 return versions.find(api_version) != versions.end();
27 }
28
29 #if defined(_WIN32)
30 #undef GetClassName
31 #endif /* _WIN32 */
32 template <> const char *GetClassName<GameInfo, ScriptType::GS>() { return "GSInfo"; }
33
34 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
35 {
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");
47
48 SQGSInfo.PostRegister(engine);
49 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
50 }
51
52 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
53 {
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;
58
59 SQInteger res = ScriptInfo::Constructor(vm, info);
60 if (res != 0) return res;
61
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();
66 }
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;
72 }
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;
79 }
80
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;
86 }
87
88 GameInfo::GameInfo() :
89 min_loadable_version(0),
90 is_developer_only(false)
91 {
92 }
93
94 bool GameInfo::CanLoadFromVersion(int version) const
95 {
96 if (version == -1) return true;
97 return version >= this->min_loadable_version && version <= this->GetVersion();
98 }
99
100
101 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
102 {
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");
107 }
108
109 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
110 {
111 /* Create a new library */
112 GameLibrary *library = new GameLibrary();
113
114 SQInteger res = ScriptInfo::Constructor(vm, library);
115 if (res != 0) {
116 delete library;
117 return res;
118 }
119
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;
124 }
125
126 /* Register the Library to the base system */
127 library->GetScanner()->RegisterScript(library);
128
129 return 0;
130 }
ScriptInfo::AddSetting
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
Definition: script_info.cpp:85
GameInfo::api_version
std::string api_version
API version used by this Game.
Definition: game_info.hpp:45
ScriptInfo::engine
class Squirrel * engine
Engine used to register for Squirrel.
Definition: script_info.hpp:139
game_info.hpp
ScriptInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
Definition: script_info.cpp:30
CheckAPIVersion
static bool CheckAPIVersion(const std::string &api_version)
Check if the API version provided by the Game is supported.
Definition: game_info.cpp:23
GameLibrary::category
std::string category
The category this library is in.
Definition: game_info.hpp:69
Squirrel::AddClassBegin
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:313
SCRIPTCONFIG_BOOLEAN
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
Definition: script_config.hpp:24
GameLibrary::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an GSLibrary, using this GSInfo as start-template.
Definition: game_info.cpp:109
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:152
Squirrel
Definition: squirrel.hpp:23
Squirrel::AddClassEnd
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:337
GameInfo::CanLoadFromVersion
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition: game_info.cpp:94
MAX_GET_OPS
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Definition: script_info.hpp:25
DefSQClass::DefSQAdvancedMethod
void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!...
Definition: squirrel_class.hpp:43
SCRIPTCONFIG_NONE
@ SCRIPTCONFIG_NONE
No flags set.
Definition: script_config.hpp:22
GameInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an Game, using this GameInfo as start-template.
Definition: game_info.cpp:52
ScriptInfo::CheckMethod
bool CheckMethod(const char *name) const
Check if a given method exists.
Definition: script_info.cpp:21
SCRIPTCONFIG_INGAME
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
Definition: script_config.hpp:25
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:26
ScriptInfo::GetName
const std::string & GetName() const
Get the Name of the script.
Definition: script_info.hpp:46
GameLibrary::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:101
game_scanner.hpp
GameInfo::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:34
GameInfo::is_developer_only
bool is_developer_only
Is the script selectable by non-developers?
Definition: game_info.hpp:44
ScriptScanner::RegisterScript
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
Definition: script_scanner.cpp:94
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:61
Squirrel::MethodExists
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:345
ScriptInfo::AddLabels
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
Definition: script_info.cpp:190
Squirrel::AddMethod
void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=nullptr, void *userdata=nullptr, int size=0)
Adds a function to the stack.
Definition: squirrel.cpp:278
GameInfo
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
GameLibrary
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:49
DefSQClass
The template to define classes in Squirrel.
Definition: squirrel_class.hpp:20
GameInfo::min_loadable_version
int min_loadable_version
The Game can load savegame data if the version is equal or greater than this.
Definition: game_info.hpp:43
ScriptInfo::SQ_instance
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
Definition: script_info.hpp:140
ScriptInfo::GetScanner
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
Definition: script_info.hpp:101