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

OpenTTD Source 13.2.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 #include <set>
17
18 #include "../safeguards.h"
19
24 static bool CheckAPIVersion(const char *api_version)
25 {
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", "12", "13" };
27 return versions.find(api_version) != versions.end();
28 }
29
30 #if defined(_WIN32)
31 #undef GetClassName
32 #endif /* _WIN32 */
33 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
34
35 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
36 {
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");
48
49 SQGSInfo.PostRegister(engine);
50 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
51 }
52
53 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
54 {
55 /* Get the GameInfo */
56 SQUserPointer instance = nullptr;
57 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");
58 GameInfo *info = (GameInfo *)instance;
59
60 SQInteger res = ScriptInfo::Constructor(vm, info);
61 if (res != 0) return res;
62
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();
67 }
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;
73 }
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 ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
79 return SQ_ERROR;
80 }
81
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;
87 }
88
89 GameInfo::GameInfo() :
90 min_loadable_version(0),
91 is_developer_only(false),
92 api_version(nullptr)
93 {
94 }
95
96 GameInfo::~GameInfo()
97 {
98 free(this->api_version);
99 }
100
101 bool GameInfo::CanLoadFromVersion(int version) const
102 {
103 if (version == -1) return true;
104 return version >= this->min_loadable_version && version <= this->GetVersion();
105 }
106
107
108 GameLibrary::~GameLibrary()
109 {
110 free(this->category);
111 }
112
113 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
114 {
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");
119 }
120
121 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
122 {
123 /* Create a new library */
124 GameLibrary *library = new GameLibrary();
125
126 SQInteger res = ScriptInfo::Constructor(vm, library);
127 if (res != 0) {
128 delete library;
129 return res;
130 }
131
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;
136 }
137
138 /* Register the Library to the base system */
139 library->GetScanner()->RegisterScript(library);
140
141 return 0;
142 }
ScriptInfo::AddSetting
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
Definition: script_info.cpp:112
ScriptInfo::engine
class Squirrel * engine
Engine used to register for Squirrel.
Definition: script_info.hpp:148
game_info.hpp
ScriptInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
Definition: script_info.cpp:56
Squirrel::AddClassBegin
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:336
SCRIPTCONFIG_BOOLEAN
@ SCRIPTCONFIG_BOOLEAN
This value is a boolean (either 0 (false) or 1 (true) ).
Definition: script_config.hpp:28
GameLibrary::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an GSLibrary, using this GSInfo as start-template.
Definition: game_info.cpp:121
ScriptInfo::version
int version
Version of the script.
Definition: script_info.hpp:161
Squirrel
Definition: squirrel.hpp:23
Squirrel::AddClassEnd
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:360
GameInfo::CanLoadFromVersion
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition: game_info.cpp:101
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:26
GameInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an Game, using this GameInfo as start-template.
Definition: game_info.cpp:53
ScriptInfo::CheckMethod
bool CheckMethod(const char *name) const
Check if a given method exists.
Definition: script_info.cpp:45
SCRIPTCONFIG_INGAME
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
Definition: script_config.hpp:29
SCRIPTCONFIG_DEVELOPER
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
Definition: script_config.hpp:30
GameLibrary::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:113
GameLibrary::category
const char * category
The category this library is in.
Definition: game_info.hpp:71
game_scanner.hpp
GameInfo::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:35
GameInfo::is_developer_only
bool is_developer_only
Is the script selectable by non-developers?
Definition: game_info.hpp:45
ScriptScanner::RegisterScript
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
Definition: script_scanner.cpp:98
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:70
Squirrel::MethodExists
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:368
ScriptInfo::AddLabels
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
Definition: script_info.cpp:225
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:301
GameInfo
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SCRIPTCONFIG_RANDOM
@ SCRIPTCONFIG_RANDOM
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
Definition: script_config.hpp:27
ScriptInfo::SQ_instance
HSQOBJECT * SQ_instance
The Squirrel instance created for this info.
Definition: script_info.hpp:149
GameLibrary
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:50
DefSQClass
The template to define classes in Squirrel.
Definition: squirrel_class.hpp:20
CheckAPIVersion
static bool CheckAPIVersion(const char *api_version)
Check if the API version provided by the Game is supported.
Definition: game_info.cpp:24
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:44
ScriptInfo::GetName
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:55
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:470
ScriptInfo::GetScanner
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
Definition: script_info.hpp:110
GameInfo::api_version
const char * api_version
API version used by this Game.
Definition: game_info.hpp:46