OpenTTD Source: src/script/script_instance.hpp Source File

OpenTTD Source 15.0-RC2

script_instance.hpp
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#ifndef SCRIPT_INSTANCE_HPP
11#define SCRIPT_INSTANCE_HPP
12
13#include <variant>
14#include <squirrel.h>
15#include "script_suspend.hpp"
16#include "script_log_types.hpp"
17
18#include "../command_type.h"
19#include "../company_type.h"
20#include "../fileio_type.h"
21
22 static const uint SQUIRREL_MAX_DEPTH = 25;
23
25 class ScriptInstance {
26private:
28 enum SQSaveLoadType : uint8_t {
29 SQSL_INT = 0x00,
30 SQSL_STRING = 0x01,
31 SQSL_ARRAY = 0x02,
32 SQSL_TABLE = 0x03,
33 SQSL_BOOL = 0x04,
34 SQSL_NULL = 0x05,
35 SQSL_INSTANCE = 0x06,
36 SQSL_ARRAY_TABLE_END = 0xFF,
37 };
38
39public:
40 friend class ScriptObject;
41 friend class ScriptController;
42
43 typedef std::variant<SQInteger, std::string, SQBool, SQSaveLoadType> ScriptDataVariant;
44 typedef std::list<ScriptDataVariant> ScriptData;
45
49 ScriptInstance(std::string_view api_name);
50 virtual ~ScriptInstance();
51
58 void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company);
59
65 virtual int GetSetting(const std::string &name) = 0;
66
73 virtual class ScriptInfo *FindLibrary(const std::string &library, int version) = 0;
74
79 void Continue();
80
84 void GameLoop();
85
89 void CollectGarbage();
90
94 class ScriptStorage &GetStorage();
95
99 ScriptLogTypes::LogData &GetLogData();
100
104 static void DoCommandReturn(ScriptInstance &instance);
105
109 static void DoCommandReturnVehicleID(ScriptInstance &instance);
110
114 static void DoCommandReturnSignID(ScriptInstance &instance);
115
119 static void DoCommandReturnGroupID(ScriptInstance &instance);
120
124 static void DoCommandReturnGoalID(ScriptInstance &instance);
125
129 static void DoCommandReturnStoryPageID(ScriptInstance &instance);
130
134 static void DoCommandReturnStoryPageElementID(ScriptInstance &instance);
135
139 static void DoCommandReturnLeagueTableID(ScriptInstance &instance);
140
144 static void DoCommandReturnLeagueTableElementID(ScriptInstance &instance);
145
149 class ScriptController &GetController()
150 {
151 assert(this->controller != nullptr);
152 return *this->controller;
153 }
154
158 inline bool IsDead() const { return this->is_dead; }
159
163 inline bool IsAlive() const { return !this->IsDead() && !this->in_shutdown; }
164
168 void Save();
169
173 static void SaveEmpty();
174
181 static ScriptData *Load(int version);
182
187 void LoadOnStack(ScriptData *data);
188
192 static void LoadEmpty();
193
199 void Pause();
200
205 bool IsPaused();
206
212 void Unpause();
213
219 SQInteger GetOpsTillSuspend();
220
230 bool DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd);
231
236 void InsertEvent(class ScriptEvent *event);
237
243 bool IsSleeping() { return this->suspend != 0; }
244
245 size_t GetAllocatedMemory() const;
246
250 inline bool InShutdown() const { return this->in_shutdown; }
251
256 void ReleaseSQObject(HSQOBJECT *obj);
257
258protected:
259 std::unique_ptr<class Squirrel> engine;
260 std::string api_version{};
261
265 virtual void RegisterAPI();
266
273 bool LoadCompatibilityScripts(Subdirectory dir, std::span<const std::string_view> api_versions);
274
278 virtual void Died();
279
283 virtual CommandCallbackData *GetDoCommandCallback() = 0;
284
288 virtual void LoadDummyScript() = 0;
289
290private:
291 std::unique_ptr<class ScriptStorage> storage;
292 std::unique_ptr<class ScriptController> controller;
293 std::unique_ptr<SQObject> instance;
294
295 bool is_started = false;
296 bool is_dead = false;
297 bool is_save_data_on_stack = false;
298 int suspend = 0;
299 bool is_paused = false;
300 bool in_shutdown = false;
301 Script_SuspendCallbackProc *callback = nullptr;
302 size_t last_allocated_memory = 0;
303
308 bool CallLoad();
309
316 bool LoadCompatibilityScript(std::string_view api_version, Subdirectory dir);
317
328 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
329
334 static bool LoadObjects(ScriptData *data);
335
336 static bool LoadObjects(HSQUIRRELVM vm, ScriptData *data);
337};
338
339#endif /* SCRIPT_INSTANCE_HPP */
CommandCost
Common return value for all commands.
Definition command_type.h:24
ScriptInfo
All static information from an Script like name, version, etc.
Definition script_info.hpp:30
ScriptInstance
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition script_instance.hpp:25
ScriptInstance::FindLibrary
virtual class ScriptInfo * FindLibrary(const std::string &library, int version)=0
Find a library.
ScriptInstance::LoadCompatibilityScripts
bool LoadCompatibilityScripts(Subdirectory dir, std::span< const std::string_view > api_versions)
Load squirrel scripts to emulate an older API.
Definition script_instance.cpp:125
ScriptInstance::Load
static ScriptData * Load(int version)
Load data from a savegame.
Definition script_instance.cpp:732
ScriptInstance::RegisterAPI
virtual void RegisterAPI()
Register all API functions to the VM.
Definition script_instance.cpp:100
ScriptInstance::InsertEvent
void InsertEvent(class ScriptEvent *event)
Insert an event for this script.
Definition script_instance.cpp:837
ScriptInstance::Unpause
void Unpause()
Resume execution of the script.
Definition script_instance.cpp:590
ScriptInstance::GetDoCommandCallback
virtual CommandCallbackData * GetDoCommandCallback()=0
Get the callback handling DoCommands in case of networking.
ScriptInstance::DoCommandReturnGroupID
static void DoCommandReturnGroupID(ScriptInstance &instance)
Return a GroupID reply for a DoCommand.
Definition script_instance.cpp:298
ScriptInstance::IsPaused
bool IsPaused()
Checks if the script is paused.
Definition script_instance.cpp:595
ScriptInstance::Died
virtual void Died()
Tell the script it died.
Definition script_instance.cpp:172
ScriptInstance::LoadObjects
static bool LoadObjects(ScriptData *data)
Load all objects from a savegame.
Definition script_instance.cpp:600
ScriptInstance::ReleaseSQObject
void ReleaseSQObject(HSQOBJECT *obj)
Decrease the ref count of a squirrel object.
Definition script_instance.cpp:850
ScriptInstance::DoCommandReturnVehicleID
static void DoCommandReturnVehicleID(ScriptInstance &instance)
Return a VehicleID reply for a DoCommand.
Definition script_instance.cpp:288
ScriptInstance::DoCommandReturnSignID
static void DoCommandReturnSignID(ScriptInstance &instance)
Return a SignID reply for a DoCommand.
Definition script_instance.cpp:293
ScriptInstance::engine
std::unique_ptr< class Squirrel > engine
A wrapper around the squirrel vm.
Definition script_instance.hpp:259
ScriptInstance::CollectGarbage
void CollectGarbage()
Let the VM collect any garbage.
Definition script_instance.cpp:275
ScriptInstance::DoCommandReturn
static void DoCommandReturn(ScriptInstance &instance)
Return a true/false reply for a DoCommand.
Definition script_instance.cpp:283
ScriptInstance::SQSaveLoadType
SQSaveLoadType
The type of the data that follows in the savegame.
Definition script_instance.hpp:28
ScriptInstance::SQSL_BOOL
@ SQSL_BOOL
The following data is a boolean.
Definition script_instance.hpp:33
ScriptInstance::SQSL_STRING
@ SQSL_STRING
The following data is an string.
Definition script_instance.hpp:30
ScriptInstance::SQSL_TABLE
@ SQSL_TABLE
The following data is an table.
Definition script_instance.hpp:32
ScriptInstance::SQSL_ARRAY_TABLE_END
@ SQSL_ARRAY_TABLE_END
Marks the end of an array or table, no data follows.
Definition script_instance.hpp:36
ScriptInstance::SQSL_ARRAY
@ SQSL_ARRAY
The following data is an array.
Definition script_instance.hpp:31
ScriptInstance::SQSL_NULL
@ SQSL_NULL
A null variable.
Definition script_instance.hpp:34
ScriptInstance::SQSL_INT
@ SQSL_INT
The following data is an integer.
Definition script_instance.hpp:29
ScriptInstance::SQSL_INSTANCE
@ SQSL_INSTANCE
The following data is an instance.
Definition script_instance.hpp:35
ScriptInstance::DoCommandReturnGoalID
static void DoCommandReturnGoalID(ScriptInstance &instance)
Return a GoalID reply for a DoCommand.
Definition script_instance.cpp:303
ScriptInstance::SaveEmpty
static void SaveEmpty()
Don't save any data in the savegame.
Definition script_instance.cpp:507
ScriptInstance::DoCommandReturnLeagueTableElementID
static void DoCommandReturnLeagueTableElementID(ScriptInstance &instance)
Return a LeagueTableElementID reply for a DoCommand.
Definition script_instance.cpp:318
ScriptInstance::LoadOnStack
void LoadOnStack(ScriptData *data)
Store loaded data on the stack.
Definition script_instance.cpp:749
ScriptInstance::Save
void Save()
Call the script Save function and save all data in the savegame.
Definition script_instance.cpp:513
ScriptInstance::is_save_data_on_stack
bool is_save_data_on_stack
Is the save data still on the squirrel stack?
Definition script_instance.hpp:297
ScriptInstance::in_shutdown
bool in_shutdown
Is this instance currently being destructed?
Definition script_instance.hpp:300
ScriptInstance::LoadDummyScript
virtual void LoadDummyScript()=0
Load the dummy script.
ScriptInstance::api_version
std::string api_version
Current API used by this script.
Definition script_instance.hpp:260
ScriptInstance::GetController
class ScriptController & GetController()
Get the controller attached to the instance.
Definition script_instance.hpp:149
ScriptInstance::IsSleeping
bool IsSleeping()
Check if the instance is sleeping, which either happened because the script executed a DoCommand,...
Definition script_instance.hpp:243
ScriptInstance::DoCommandCallback
bool DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
DoCommand callback function for all commands executed by scripts.
Definition script_instance.cpp:813
ScriptInstance::GetLogData
ScriptLogTypes::LogData & GetLogData()
Get the log pointer of this script.
Definition script_instance.cpp:335
ScriptInstance::Pause
void Pause()
Suspends the script for the current tick and then pause the execution of script.
Definition script_instance.cpp:581
ScriptInstance::callback
Script_SuspendCallbackProc * callback
Callback that should be called in the next tick the script runs.
Definition script_instance.hpp:301
ScriptInstance::Initialize
void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
Definition script_instance.cpp:58
ScriptInstance::SaveObject
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test)
Save one object (int / string / array / table) to the savegame.
Definition script_instance.cpp:370
ScriptInstance::CallLoad
bool CallLoad()
Call the script Load function if it exists and data was loaded from a savegame.
Definition script_instance.cpp:771
ScriptInstance::is_dead
bool is_dead
True if the script has been stopped.
Definition script_instance.hpp:296
ScriptInstance::controller
std::unique_ptr< class ScriptController > controller
The script main class.
Definition script_instance.hpp:292
ScriptInstance::InShutdown
bool InShutdown() const
Indicate whether this instance is currently being destroyed.
Definition script_instance.hpp:250
ScriptInstance::IsDead
bool IsDead() const
Return the "this script died" value.
Definition script_instance.hpp:158
ScriptInstance::last_allocated_memory
size_t last_allocated_memory
Last known allocated memory value (for display for crashed scripts)
Definition script_instance.hpp:302
ScriptInstance::Continue
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
Definition script_instance.cpp:166
ScriptInstance::GetSetting
virtual int GetSetting(const std::string &name)=0
Get the value of a setting of the current instance.
ScriptInstance::LoadEmpty
static void LoadEmpty()
Load and discard data from a savegame.
Definition script_instance.cpp:723
ScriptInstance::GameLoop
void GameLoop()
Run the GameLoop of a script.
Definition script_instance.cpp:185
ScriptInstance::DoCommandReturnStoryPageElementID
static void DoCommandReturnStoryPageElementID(ScriptInstance &instance)
Return a StoryPageElementID reply for a DoCommand.
Definition script_instance.cpp:313
ScriptInstance::suspend
int suspend
The amount of ticks to suspend this script before it's allowed to continue.
Definition script_instance.hpp:298
ScriptInstance::IsAlive
bool IsAlive() const
Return whether the script is alive.
Definition script_instance.hpp:163
ScriptInstance::GetOpsTillSuspend
SQInteger GetOpsTillSuspend()
Get the number of operations the script can execute before being suspended.
Definition script_instance.cpp:808
ScriptInstance::storage
std::unique_ptr< class ScriptStorage > storage
Some global information for each running script.
Definition script_instance.hpp:291
ScriptInstance::GetStorage
class ScriptStorage & GetStorage()
Get the storage of this script.
Definition script_instance.cpp:329
ScriptInstance::is_started
bool is_started
Is the scripts constructor executed?
Definition script_instance.hpp:295
ScriptInstance::DoCommandReturnLeagueTableID
static void DoCommandReturnLeagueTableID(ScriptInstance &instance)
Return a LeagueTableID reply for a DoCommand.
Definition script_instance.cpp:323
ScriptInstance::DoCommandReturnStoryPageID
static void DoCommandReturnStoryPageID(ScriptInstance &instance)
Return a StoryPageID reply for a DoCommand.
Definition script_instance.cpp:308
ScriptInstance::LoadCompatibilityScript
bool LoadCompatibilityScript(std::string_view api_version, Subdirectory dir)
Load squirrel script for a specific version to emulate an older API.
Definition script_instance.cpp:105
ScriptInstance::is_paused
bool is_paused
Is the script paused? (a paused script will not be executed until unpaused)
Definition script_instance.hpp:299
ScriptInstance::instance
std::unique_ptr< SQObject > instance
Squirrel-pointer to the script main class.
Definition script_instance.hpp:293
ScriptStorage
The storage for each script.
Definition script_storage.hpp:43
CommandCallbackData
void CommandCallbackData(Commands cmd, const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data)
Define a callback function for the client, after the command is finished.
Definition command_type.h:511
CommandDataBuffer
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Definition command_type.h:481
Commands
Commands
List of commands.
Definition command_type.h:196
Subdirectory
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
SQUIRREL_MAX_DEPTH
static const uint SQUIRREL_MAX_DEPTH
The maximum recursive depth for items stored in the savegame.
Definition script_instance.hpp:22
script_suspend.hpp
The Script_Suspend tracks the suspension of a script.
Script_SuspendCallbackProc
void() Script_SuspendCallbackProc(class ScriptInstance &instance)
The callback function when a script suspends.
Definition script_suspend.hpp:16
PoolID< uint8_t, struct CompanyIDTag, 0xF, 0xFF >