OpenTTD Source 20260311-master-g511d3794ce
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
9
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
22static const uint SQUIRREL_MAX_DEPTH = 25;
23
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,
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
50 ScriptInstance(std::string_view api_name);
51 virtual ~ScriptInstance();
52
59 void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company);
60
66 virtual int GetSetting(const std::string &name) = 0;
67
74 virtual class ScriptInfo *FindLibrary(const std::string &library, int version) = 0;
75
80 void Continue();
81
85 void GameLoop();
86
90 void CollectGarbage();
91
97
102 ScriptLogTypes::LogData &GetLogData();
103
109
115
121
127
133
139
145
151
157
162 class ScriptController &GetController()
163 {
164 assert(this->controller != nullptr);
165 return *this->controller;
166 }
167
172 inline bool IsDead() const { return this->is_dead; }
173
178 inline bool IsAlive() const { return !this->IsDead() && !this->in_shutdown; }
179
183 void Save();
184
188 static void SaveEmpty();
189
196 static ScriptData *Load(int version);
197
202 void LoadOnStack(ScriptData *data);
203
207 static void LoadEmpty();
208
214 void Pause();
215
220 bool IsPaused();
221
227 void Unpause();
228
234 SQInteger GetOpsTillSuspend();
235
244 bool DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd);
245
250 void InsertEvent(class ScriptEvent *event);
251
258 bool IsSleeping() { return this->suspend != 0; }
259
260 size_t GetAllocatedMemory() const;
261
266 inline bool InShutdown() const { return this->in_shutdown; }
267
272 void ReleaseSQObject(HSQOBJECT *obj);
273
274protected:
275 std::unique_ptr<class Squirrel> engine;
276 std::string api_version{};
277
281 virtual void RegisterAPI();
282
289 bool LoadCompatibilityScripts(Subdirectory dir, std::span<const std::string_view> api_versions);
290
294 virtual void Died();
295
301
305 virtual void LoadDummyScript() = 0;
306
307private:
308 std::unique_ptr<class ScriptStorage> storage;
309 std::unique_ptr<class ScriptController> controller;
310 std::unique_ptr<SQObject> instance;
311
312 bool is_started = false;
313 bool is_dead = false;
315 int suspend = 0;
316 bool is_paused = false;
317 bool in_shutdown = false;
320
326 bool CallLoad();
327
334 bool LoadCompatibilityScript(std::string_view api_version, Subdirectory dir);
335
346 static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test);
347
353 static bool LoadObjects(ScriptData *data);
354
355 static bool LoadObjects(HSQUIRRELVM vm, ScriptData *data);
356};
357
358#endif /* SCRIPT_INSTANCE_HPP */
Common return value for all commands.
All static information from an Script like name, version, etc.
virtual class ScriptInfo * FindLibrary(const std::string &library, int version)=0
Find a library.
bool LoadCompatibilityScripts(Subdirectory dir, std::span< const std::string_view > api_versions)
Load squirrel scripts to emulate an older API.
static ScriptData * Load(int version)
Load data from a savegame.
virtual void RegisterAPI()
Register all API functions to the VM.
void InsertEvent(class ScriptEvent *event)
Insert an event for this script.
void Unpause()
Resume execution of the script.
virtual CommandCallbackData * GetDoCommandCallback()=0
Get the callback handling DoCommands in case of networking.
static void DoCommandReturnGroupID(ScriptInstance &instance)
Return a GroupID reply for a DoCommand.
bool IsPaused()
Checks if the script is paused.
virtual void Died()
Tell the script it died.
static bool LoadObjects(ScriptData *data)
Load all objects from a savegame.
void ReleaseSQObject(HSQOBJECT *obj)
Decrease the ref count of a squirrel object.
static void DoCommandReturnVehicleID(ScriptInstance &instance)
Return a VehicleID reply for a DoCommand.
static void DoCommandReturnSignID(ScriptInstance &instance)
Return a SignID reply for a DoCommand.
std::unique_ptr< class Squirrel > engine
A wrapper around the squirrel vm.
void CollectGarbage()
Let the VM collect any garbage.
static void DoCommandReturn(ScriptInstance &instance)
Return a true/false reply for a DoCommand.
SQSaveLoadType
The type of the data that follows in the savegame.
@ SQSL_BOOL
The following data is a boolean.
@ SQSL_STRING
The following data is an string.
@ SQSL_TABLE
The following data is an table.
@ SQSL_ARRAY_TABLE_END
Marks the end of an array or table, no data follows.
@ SQSL_ARRAY
The following data is an array.
@ SQSL_NULL
A null variable.
@ SQSL_INT
The following data is an integer.
@ SQSL_INSTANCE
The following data is an instance.
static void DoCommandReturnGoalID(ScriptInstance &instance)
Return a GoalID reply for a DoCommand.
static void SaveEmpty()
Don't save any data in the savegame.
static void DoCommandReturnLeagueTableElementID(ScriptInstance &instance)
Return a LeagueTableElementID reply for a DoCommand.
void LoadOnStack(ScriptData *data)
Store loaded data on the stack.
void Save()
Call the script Save function and save all data in the savegame.
bool is_save_data_on_stack
Is the save data still on the squirrel stack?
bool in_shutdown
Is this instance currently being destructed?
virtual void LoadDummyScript()=0
Load the dummy script.
std::string api_version
Current API used by this script.
class ScriptController & GetController()
Get the controller attached to the instance.
virtual ~ScriptInstance()
Release our hold on the engine and reset it in the right scope.
bool IsSleeping()
Check if the instance is sleeping, which either happened because the script executed a DoCommand,...
bool DoCommandCallback(const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd)
DoCommand callback function for all commands executed by scripts.
ScriptLogTypes::LogData & GetLogData()
Get the log pointer of this script.
void Pause()
Suspends the script for the current tick and then pause the execution of script.
ScriptInstance(std::string_view api_name)
Create a new script.
Script_SuspendCallbackProc * callback
Callback that should be called in the next tick the script runs.
void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company)
Initialize the script and prepare it for its first run.
static bool SaveObject(HSQUIRRELVM vm, SQInteger index, int max_depth, bool test)
Save one object (int / string / array / table) to the savegame.
bool CallLoad()
Call the script Load function if it exists and data was loaded from a savegame.
bool is_dead
True if the script has been stopped.
std::unique_ptr< class ScriptController > controller
The script main class.
bool InShutdown() const
Indicate whether this instance is currently being destroyed.
bool IsDead() const
Return the "this script died" value.
size_t last_allocated_memory
Last known allocated memory value (for display for crashed scripts).
void Continue()
A script in multiplayer waits for the server to handle its DoCommand.
virtual int GetSetting(const std::string &name)=0
Get the value of a setting of the current instance.
static void LoadEmpty()
Load and discard data from a savegame.
void GameLoop()
Run the GameLoop of a script.
static void DoCommandReturnStoryPageElementID(ScriptInstance &instance)
Return a StoryPageElementID reply for a DoCommand.
int suspend
The amount of ticks to suspend this script before it's allowed to continue.
bool IsAlive() const
Return whether the script is alive.
SQInteger GetOpsTillSuspend()
Get the number of operations the script can execute before being suspended.
std::unique_ptr< class ScriptStorage > storage
Some global information for each running script.
class ScriptStorage & GetStorage()
Get the storage of this script.
bool is_started
Is the scripts constructor executed?
static void DoCommandReturnLeagueTableID(ScriptInstance &instance)
Return a LeagueTableID reply for a DoCommand.
static void DoCommandReturnStoryPageID(ScriptInstance &instance)
Return a StoryPageID reply for a DoCommand.
bool LoadCompatibilityScript(std::string_view api_version, Subdirectory dir)
Load squirrel script for a specific version to emulate an older API.
bool is_paused
Is the script paused? (a paused script will not be executed until unpaused).
std::unique_ptr< SQObject > instance
Squirrel-pointer to the script main class.
The storage for each script.
Types related to commands.
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.
std::vector< uint8_t > CommandDataBuffer
Storage buffer for serialized command data.
Commands
List of commands.
Types related to companies.
Types for standard in/out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:88
static const uint SQUIRREL_MAX_DEPTH
The maximum recursive depth for items stored in the savegame.
The Script_Suspend tracks the suspension of a script.
void Script_SuspendCallbackProc(class ScriptInstance &instance)
The callback function when a script suspends.