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

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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 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::*)()>(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 sq_throwerror(vm, fmt::format("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(std::unique_ptr<GameInfo>{info});
82 return 0;
83}
84
85GameInfo::GameInfo() :
86 min_loadable_version(0),
87 is_developer_only(false)
88{
89}
90
91 bool 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");
102 engine.AddClassEnd();
103 engine.AddMethod("RegisterLibrary", &GameLibrary::Constructor, "tx");
104}
105
106/* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
107{
108 /* Create a new library */
109 auto library = std::make_unique<GameLibrary>();
110
111 SQInteger res = ScriptInfo::Constructor(vm, *library);
112 if (res != 0) {
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 return SQ_ERROR;
119 }
120
121 /* Register the Library to the base system */
122 ScriptScanner *scanner = library->GetScanner();
123 scanner->RegisterScript(std::move(library));
124
125 return 0;
126}
CheckAPIVersion
static bool CheckAPIVersion(const std::string &api_version)
Check if the API version provided by the AI is supported.
Definition ai_info.cpp:25
BaseBitSet::base
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
Definition base_bitset_type.hpp:219
DefSQClass
The template to define classes in Squirrel.
Definition squirrel_class.hpp:20
DefSQClass::DefSQAdvancedMethod
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!...
Definition squirrel_class.hpp:46
EnumBitSet< ScriptConfigFlag, uint8_t >
GameInfo
All static information from an Game like name, version, etc.
Definition game_info.hpp:16
GameInfo::api_version
std::string api_version
API version used by this Game.
Definition game_info.hpp:48
GameInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an Game, using this GameInfo as start-template.
Definition game_info.cpp:48
GameInfo::CanLoadFromVersion
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition game_info.cpp:91
GameInfo::RegisterAPI
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition game_info.cpp:30
GameInfo::is_developer_only
bool is_developer_only
Is the script selectable by non-developers?
Definition game_info.hpp:47
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:46
GameLibrary::RegisterAPI
static void RegisterAPI(Squirrel &engine)
Register the functions of this class.
Definition game_info.cpp:98
GameLibrary::Constructor
static SQInteger Constructor(HSQUIRRELVM vm)
Create an GSLibrary, using this GSInfo as start-template.
Definition game_info.cpp:106
ScriptInfo::CheckMethod
bool CheckMethod(std::string_view name) const
Check if a given method exists.
Definition script_info.cpp:22
ScriptInfo::version
int version
Version of the script.
Definition script_info.hpp:146
ScriptInfo::scanner
class ScriptScanner * scanner
ScriptScanner object that was used to scan this script info.
Definition script_info.hpp:149
ScriptInfo::GetName
const std::string & GetName() const
Get the Name of the script.
Definition script_info.hpp:40
ScriptInfo::engine
class Squirrel * engine
Engine used to register for Squirrel.
Definition script_info.hpp:133
ScriptInfo::Constructor
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo &info)
Process the creation of a FileInfo object.
Definition script_info.cpp:31
ScriptInfo::AddSetting
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
Definition script_info.cpp:98
ScriptInfo::AddLabels
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
Definition script_info.cpp:200
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition script_info.hpp:55
ScriptInfo::SQ_instance
HSQOBJECT SQ_instance
The Squirrel instance created for this info.
Definition script_info.hpp:134
ScriptInfo::GetScanner
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
Definition script_info.hpp:95
ScriptScanner
Scanner to help finding scripts.
Definition script_scanner.hpp:19
ScriptScanner::RegisterScript
void RegisterScript(std::unique_ptr< class ScriptInfo > &&info)
Register a ScriptInfo to the scanner.
Definition script_scanner.cpp:86
Squirrel
Definition squirrel.hpp:24
Squirrel::AddClassEnd
void AddClassEnd()
Finishes adding a class to the global scope.
Definition squirrel.cpp:315
Squirrel::AddClassBegin
void AddClassBegin(std::string_view class_name)
Adds a class to the global scope.
Definition squirrel.cpp:291
Squirrel::AddMethod
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
Squirrel::MethodExists
bool MethodExists(HSQOBJECT instance, std::string_view method_name)
Check if a method exists in an instance.
Definition squirrel.cpp:323
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
game_info.hpp
GameInfo keeps track of all information of an Game, like Author, Description, ...
game_scanner.hpp
declarations of the class for Game scanner
ScriptConfigFlag::Boolean
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
ScriptConfigFlag::Developer
@ Developer
This setting will only be visible when the Script development tools are active.
ScriptConfigFlag::InGame
@ InGame
This setting can be changed while the Script is running.
MAX_GET_OPS
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Definition script_info.hpp:25