OpenTTD Source 20250312-master-gcdcc6b491d
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
23static bool CheckAPIVersion(const std::string &api_version)
24{
25 return std::ranges::find(GameInfo::ApiVersions, api_version) != std::end(GameInfo::ApiVersions);
26}
27
28template <> SQInteger PushClassName<GameInfo, ScriptType::GS>(HSQUIRRELVM vm) { sq_pushstring(vm, "GSInfo", -1); return 1; }
29
30/* static */ void GameInfo::RegisterAPI(Squirrel *engine)
31{
32 /* Create the GSInfo class, and add the RegisterGS function */
33 DefSQClass<GameInfo, ScriptType::GS> SQGSInfo("GSInfo");
34 SQGSInfo.PreRegister(engine);
35 SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
36 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
37 SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
38 SQGSInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_NONE");
39 SQGSInfo.DefSQConst(engine, ScriptConfigFlags{}.base(), "CONFIG_RANDOM"); // Deprecated, mapped to NONE.
40 SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Boolean}.base(), "CONFIG_BOOLEAN");
41 SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "CONFIG_INGAME");
42 SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER");
43
44 SQGSInfo.PostRegister(engine);
45 engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
46}
47
48/* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
49{
50 /* Get the GameInfo */
51 SQUserPointer instance = nullptr;
52 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");
53 GameInfo *info = (GameInfo *)instance;
54
55 SQInteger res = ScriptInfo::Constructor(vm, info);
56 if (res != 0) return res;
57
58 if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
59 if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
60 } else {
61 info->min_loadable_version = info->GetVersion();
62 }
63 /* When there is an IsSelectable function, call it. */
64 if (info->engine->MethodExists(info->SQ_instance, "IsDeveloperOnly")) {
65 if (!info->engine->CallBoolMethod(info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
66 } else {
67 info->is_developer_only = false;
68 }
69 /* Try to get the API version the AI is written for. */
70 if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
71 if (!info->engine->CallStringMethod(info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
72 if (!CheckAPIVersion(info->api_version)) {
73 Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
74 return SQ_ERROR;
75 }
76
77 /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
78 sq_setinstanceup(vm, 2, nullptr);
79 /* Register the Game to the base system */
80 info->GetScanner()->RegisterScript(info);
81 return 0;
82}
83
84GameInfo::GameInfo() :
85 min_loadable_version(0),
86 is_developer_only(false)
87{
88}
89
90bool GameInfo::CanLoadFromVersion(int version) const
91{
92 if (version == -1) return true;
93 return version >= this->min_loadable_version && version <= this->GetVersion();
94}
95
96
97/* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
98{
99 /* Create the GameLibrary class, and add the RegisterLibrary function */
100 engine->AddClassBegin("GSLibrary");
102 engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
103}
104
105/* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
106{
107 /* Create a new library */
108 GameLibrary *library = new GameLibrary();
109
110 SQInteger res = ScriptInfo::Constructor(vm, library);
111 if (res != 0) {
112 delete library;
113 return res;
114 }
115
116 /* Cache the category */
117 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
118 delete library;
119 return SQ_ERROR;
120 }
121
122 /* Register the Library to the base system */
123 library->GetScanner()->RegisterScript(library);
124
125 return 0;
126}
static bool CheckAPIVersion(const std::string &api_version)
Check if the API version provided by the AI is supported.
Definition ai_info.cpp:25
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
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:48
static SQInteger Constructor(HSQUIRRELVM vm)
Create an Game, using this GameInfo as start-template.
Definition game_info.cpp:48
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition game_info.cpp:30
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition game_info.cpp:90
bool is_developer_only
Is the script selectable by non-developers?
Definition game_info.hpp:47
int min_loadable_version
The Game can load savegame data if the version is equal or greater than this.
Definition game_info.hpp:46
All static information from an Game library like name, version, etc.
Definition game_info.hpp:52
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition game_info.cpp:97
std::string category
The category this library is in.
Definition game_info.hpp:72
static SQInteger Constructor(HSQUIRRELVM vm)
Create an GSLibrary, using this GSInfo as start-template.
int version
Version of the script.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
const std::string & GetName() const
Get the Name of the script.
bool CheckMethod(const char *name) const
Check if a given method exists.
class Squirrel * engine
Engine used to register for Squirrel.
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
int GetVersion() const
Get the version of the script.
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
void AddClassEnd()
Finishes adding a class to the global scope.
Definition squirrel.cpp:313
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:254
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition squirrel.cpp:289
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition squirrel.cpp:321
#define Debug(category, level, format_string,...)
Output 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
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
@ Developer
This setting will only be visible when the Script development tools are active.
@ InGame
This setting can be changed while the Script is running.
static const int MAX_GET_OPS
Number of operations to get the author and similar information.