OpenTTD Source  20241108-master-g80f628063a
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", "15" };
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 }
The template to define classes in Squirrel.
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!...
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
std::string api_version
API version used by this Game.
Definition: game_info.hpp:45
static SQInteger Constructor(HSQUIRRELVM vm)
Create an Game, using this GameInfo as start-template.
Definition: game_info.cpp:52
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:34
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition: game_info.cpp:94
bool is_developer_only
Is the script selectable by non-developers?
Definition: game_info.hpp:44
int min_loadable_version
The Game can load savegame data if the version is equal or greater than this.
Definition: game_info.hpp:43
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:49
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:101
std::string category
The category this library is in.
Definition: game_info.hpp:69
static SQInteger Constructor(HSQUIRRELVM vm)
Create an GSLibrary, using this GSInfo as start-template.
Definition: game_info.cpp:109
int version
Version of the script.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
Definition: script_info.cpp:30
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
bool CheckMethod(const char *name) const
Check if a given method exists.
Definition: script_info.cpp:21
class Squirrel * engine
Engine used to register for Squirrel.
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
Definition: script_info.cpp:85
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:61
const std::string & GetName() const
Get the Name of the script.
Definition: script_info.hpp:46
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:337
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
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:313
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:345
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
static bool CheckAPIVersion(const std::string &api_version)
Check if the API version provided by the Game is supported.
Definition: game_info.cpp:23
GameInfo keeps track of all information of an Game, like Author, Description, ...
declarations of the class for Game scanner
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
@ SCRIPTCONFIG_NONE
No flags set.
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Definition: script_info.hpp:25