OpenTTD Source 20250524-master-gc366e6a48e
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"); 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, "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 if (info->min_loadable_version < 0) return SQ_ERROR;
61 } else {
62 info->min_loadable_version = info->GetVersion();
63 }
64 /* When there is an IsSelectable function, call it. */
65 if (info->engine->MethodExists(info->SQ_instance, "IsDeveloperOnly")) {
66 if (!info->engine->CallBoolMethod(info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
67 } else {
68 info->is_developer_only = false;
69 }
70 /* Try to get the API version the AI is written for. */
71 if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
72 if (!info->engine->CallStringMethod(info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
73 if (!CheckAPIVersion(info->api_version)) {
74 Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
75 return SQ_ERROR;
76 }
77
78 /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
79 sq_setinstanceup(vm, 2, nullptr);
80 /* Register the Game to the base system */
81 info->GetScanner()->RegisterScript(info);
82 return 0;
83}
84
85GameInfo::GameInfo() :
86 min_loadable_version(0),
87 is_developer_only(false)
88{
89}
90
91bool GameInfo::CanLoadFromVersion(int version) const
92{
93 if (version == -1) return true;
94 return version >= this->min_loadable_version && version <= this->GetVersion();
95}
96
97
98/* static */ void GameLibrary::RegisterAPI(Squirrel &engine)
99{
100 /* Create the GameLibrary class, and add the RegisterLibrary function */
101 engine.AddClassBegin("GSLibrary");
103 engine.AddMethod("RegisterLibrary", &GameLibrary::Constructor, "tx");
104}
105
106/* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
107{
108 /* Create a new library */
109 GameLibrary *library = new GameLibrary();
110
111 SQInteger res = ScriptInfo::Constructor(vm, *library);
112 if (res != 0) {
113 delete library;
114 return res;
115 }
116
117 /* Cache the category */
118 if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
119 delete library;
120 return SQ_ERROR;
121 }
122
123 /* Register the Library to the base system */
124 library->GetScanner()->RegisterScript(library);
125
126 return 0;
127}
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, std::string_view 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
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition game_info.cpp:91
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition game_info.cpp:30
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:98
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.
bool CheckMethod(std::string_view name) const
Check if a given method exists.
int version
Version of the script.
const std::string & GetName() const
Get the Name of the script.
class Squirrel * engine
Engine used to register for Squirrel.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo &info)
Process the creation of a FileInfo object.
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:315
void AddClassBegin(std::string_view class_name)
Adds a class to the global scope.
Definition squirrel.cpp:291
void AddMethod(std::string_view method_name, SQFUNCTION proc, std::string_view params={}, void *userdata=nullptr, int size=0)
Adds a function to the stack.
Definition squirrel.cpp:256
bool MethodExists(HSQOBJECT instance, std::string_view method_name)
Check if a method exists in an instance.
Definition squirrel.cpp:323
#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.