|
OpenTTD Source 20260311-master-g511d3794ce
|
Runtime information about a script like a pointer to the squirrel vm and the current state. More...
#include <script_instance.hpp>
Public Types | |
| typedef std::variant< SQInteger, std::string, SQBool, SQSaveLoadType > | ScriptDataVariant |
| typedef std::list< ScriptDataVariant > | ScriptData |
Public Member Functions | |
| ScriptInstance (std::string_view api_name) | |
| Create a new script. | |
| virtual | ~ScriptInstance () |
| Release our hold on the engine and reset it in the right scope. | |
| void | Initialize (const std::string &main_script, const std::string &instance_name, CompanyID company) |
| Initialize the script and prepare it for its first run. | |
| virtual int | GetSetting (const std::string &name)=0 |
| Get the value of a setting of the current instance. | |
| virtual class ScriptInfo * | FindLibrary (const std::string &library, int version)=0 |
| Find a library. | |
| void | Continue () |
| A script in multiplayer waits for the server to handle its DoCommand. | |
| void | GameLoop () |
| Run the GameLoop of a script. | |
| void | CollectGarbage () |
| Let the VM collect any garbage. | |
| class ScriptStorage & | GetStorage () |
| Get the storage of this script. | |
| ScriptLogTypes::LogData & | GetLogData () |
| Get the log pointer of this script. | |
| class ScriptController & | GetController () |
| Get the controller attached to the instance. | |
| bool | IsDead () const |
| Return the "this script died" value. | |
| bool | IsAlive () const |
| Return whether the script is alive. | |
| void | Save () |
| Call the script Save function and save all data in the savegame. | |
| void | LoadOnStack (ScriptData *data) |
| Store loaded data on the stack. | |
| void | Pause () |
| Suspends the script for the current tick and then pause the execution of script. | |
| bool | IsPaused () |
| Checks if the script is paused. | |
| void | Unpause () |
| Resume execution of the script. | |
| SQInteger | GetOpsTillSuspend () |
| Get the number of operations the script can execute before being suspended. | |
| bool | DoCommandCallback (const CommandCost &result, const CommandDataBuffer &data, CommandDataBuffer result_data, Commands cmd) |
| DoCommand callback function for all commands executed by scripts. | |
| void | InsertEvent (class ScriptEvent *event) |
| Insert an event for this script. | |
| bool | IsSleeping () |
| Check if the instance is sleeping, which either happened because the script executed a DoCommand, executed this.Sleep() or it has been paused. | |
| size_t | GetAllocatedMemory () const |
| bool | InShutdown () const |
| Indicate whether this instance is currently being destroyed. | |
| void | ReleaseSQObject (HSQOBJECT *obj) |
| Decrease the ref count of a squirrel object. | |
Static Public Member Functions | |
| static void | DoCommandReturn (ScriptInstance &instance) |
| Return a true/false reply for a DoCommand. | |
| static void | DoCommandReturnVehicleID (ScriptInstance &instance) |
| Return a VehicleID reply for a DoCommand. | |
| static void | DoCommandReturnSignID (ScriptInstance &instance) |
| Return a SignID reply for a DoCommand. | |
| static void | DoCommandReturnGroupID (ScriptInstance &instance) |
| Return a GroupID reply for a DoCommand. | |
| static void | DoCommandReturnGoalID (ScriptInstance &instance) |
| Return a GoalID reply for a DoCommand. | |
| static void | DoCommandReturnStoryPageID (ScriptInstance &instance) |
| Return a StoryPageID reply for a DoCommand. | |
| static void | DoCommandReturnStoryPageElementID (ScriptInstance &instance) |
| Return a StoryPageElementID reply for a DoCommand. | |
| static void | DoCommandReturnLeagueTableID (ScriptInstance &instance) |
| Return a LeagueTableID reply for a DoCommand. | |
| static void | DoCommandReturnLeagueTableElementID (ScriptInstance &instance) |
| Return a LeagueTableElementID reply for a DoCommand. | |
| static void | SaveEmpty () |
| Don't save any data in the savegame. | |
| static ScriptData * | Load (int version) |
| Load data from a savegame. | |
| static void | LoadEmpty () |
| Load and discard data from a savegame. | |
Protected Member Functions | |
| virtual void | RegisterAPI () |
| Register all API functions to the VM. | |
| bool | LoadCompatibilityScripts (Subdirectory dir, std::span< const std::string_view > api_versions) |
| Load squirrel scripts to emulate an older API. | |
| virtual void | Died () |
| Tell the script it died. | |
| virtual CommandCallbackData * | GetDoCommandCallback ()=0 |
| Get the callback handling DoCommands in case of networking. | |
| virtual void | LoadDummyScript ()=0 |
| Load the dummy script. | |
Protected Attributes | |
| std::unique_ptr< class Squirrel > | engine |
| A wrapper around the squirrel vm. | |
| std::string | api_version {} |
| Current API used by this script. | |
Private Types | |
| enum | SQSaveLoadType : uint8_t { SQSL_INT = 0x00 , SQSL_STRING = 0x01 , SQSL_ARRAY = 0x02 , SQSL_TABLE = 0x03 , SQSL_BOOL = 0x04 , SQSL_NULL = 0x05 , SQSL_INSTANCE = 0x06 , SQSL_ARRAY_TABLE_END = 0xFF } |
| The type of the data that follows in the savegame. More... | |
Private Member Functions | |
| bool | CallLoad () |
| Call the script Load function if it exists and data was loaded from a savegame. | |
| bool | LoadCompatibilityScript (std::string_view api_version, Subdirectory dir) |
| Load squirrel script for a specific version to emulate an older API. | |
Static Private Member Functions | |
| static bool | SaveObject (HSQUIRRELVM vm, SQInteger index, int max_depth, bool test) |
| Save one object (int / string / array / table) to the savegame. | |
| static bool | LoadObjects (ScriptData *data) |
| Load all objects from a savegame. | |
| static bool | LoadObjects (HSQUIRRELVM vm, ScriptData *data) |
Private Attributes | |
| std::unique_ptr< class ScriptStorage > | storage |
| Some global information for each running script. | |
| std::unique_ptr< class ScriptController > | controller |
| The script main class. | |
| std::unique_ptr< SQObject > | instance |
| Squirrel-pointer to the script main class. | |
| bool | is_started = false |
| Is the scripts constructor executed? | |
| bool | is_dead = false |
| True if the script has been stopped. | |
| bool | is_save_data_on_stack = false |
| Is the save data still on the squirrel stack? | |
| int | suspend = 0 |
| The amount of ticks to suspend this script before it's allowed to continue. | |
| bool | is_paused = false |
| Is the script paused? (a paused script will not be executed until unpaused). | |
| bool | in_shutdown = false |
| Is this instance currently being destructed? | |
| Script_SuspendCallbackProc * | callback = nullptr |
| Callback that should be called in the next tick the script runs. | |
| size_t | last_allocated_memory = 0 |
| Last known allocated memory value (for display for crashed scripts). | |
Friends | |
| class | ScriptObject |
| class | ScriptController |
Runtime information about a script like a pointer to the squirrel vm and the current state.
Definition at line 25 of file script_instance.hpp.
| typedef std::list<ScriptDataVariant> ScriptInstance::ScriptData |
Definition at line 44 of file script_instance.hpp.
| typedef std::variant<SQInteger, std::string, SQBool, SQSaveLoadType> ScriptInstance::ScriptDataVariant |
Definition at line 43 of file script_instance.hpp.
|
private |
The type of the data that follows in the savegame.
Definition at line 28 of file script_instance.hpp.
| ScriptInstance::ScriptInstance | ( | std::string_view | api_name | ) |
Create a new script.
| api_name | The name of the API (AI/GS). |
Definition at line 51 of file script_instance.cpp.
References engine, PrintFunc(), and storage.
Referenced by DoCommandReturn(), DoCommandReturnGoalID(), DoCommandReturnGroupID(), DoCommandReturnLeagueTableElementID(), DoCommandReturnLeagueTableID(), DoCommandReturnSignID(), DoCommandReturnStoryPageElementID(), DoCommandReturnStoryPageID(), DoCommandReturnVehicleID(), and FindLibrary().
|
virtual |
Release our hold on the engine and reset it in the right scope.
Definition at line 156 of file script_instance.cpp.
References engine, in_shutdown, and instance.
|
private |
Call the script Load function if it exists and data was loaded from a savegame.
true iff the load succeeded. Definition at line 772 of file script_instance.cpp.
References Squirrel::GetVM(), instance, is_save_data_on_stack, MAX_SL_OPS, and Squirrel::MethodExists().
Referenced by GameLoop().
| void ScriptInstance::CollectGarbage | ( | ) |
Let the VM collect any garbage.
Definition at line 276 of file script_instance.cpp.
References engine, is_started, and IsDead().
| void ScriptInstance::Continue | ( | ) |
A script in multiplayer waits for the server to handle its DoCommand.
It keeps waiting for this until this function is called.
Definition at line 167 of file script_instance.cpp.
References suspend.
Referenced by CcGame().
|
protectedvirtual |
Tell the script it died.
Reimplemented in AIInstance, and GameInstance.
Definition at line 173 of file script_instance.cpp.
References Debug, engine, in_shutdown, instance, is_dead, and last_allocated_memory.
Referenced by AIInstance::Died(), GameInstance::Died(), GameLoop(), and Initialize().
| bool ScriptInstance::DoCommandCallback | ( | const CommandCost & | result, |
| const CommandDataBuffer & | data, | ||
| CommandDataBuffer | result_data, | ||
| Commands | cmd ) |
DoCommand callback function for all commands executed by scripts.
| result | The result of the command. |
| data | Command data as given to DoCommandPInternal. |
| result_data | Extra data return from the command. |
| cmd | cmd as given to DoCommandPInternal. |
Definition at line 814 of file script_instance.cpp.
References Debug, End, CommandCost::Failed(), CommandCost::GetCost(), CommandCost::GetErrorMessage(), and CommandCost::Succeeded().
|
static |
Return a true/false reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 284 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a GoalID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 304 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a GroupID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 299 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a LeagueTableElementID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 319 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a LeagueTableID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 324 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a SignID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 294 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a StoryPageElementID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 314 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a StoryPageID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 309 of file script_instance.cpp.
References instance, and ScriptInstance().
|
static |
Return a VehicleID reply for a DoCommand.
| instance | The instance to return the reply to. |
Definition at line 289 of file script_instance.cpp.
References instance, and ScriptInstance().
|
pure virtual |
Find a library.
| library | The library name to find. |
| version | The version the library should have. |
Implemented in AIInstance, and GameInstance.
References instance, and ScriptInstance().
| void ScriptInstance::GameLoop | ( | ) |
Run the GameLoop of a script.
Definition at line 186 of file script_instance.cpp.
References _current_company, _settings_game, callback, CallLoad(), controller, Died(), engine, Script_Suspend::GetSuspendCallback(), Script_Suspend::GetSuspendTime(), is_dead, is_paused, is_save_data_on_stack, is_started, IsDead(), MAX_CONSTRUCTOR_OPS, and suspend.
| size_t ScriptInstance::GetAllocatedMemory | ( | ) | const |
Definition at line 845 of file script_instance.cpp.
|
inline |
Get the controller attached to the instance.
Definition at line 162 of file script_instance.hpp.
References controller.
|
protectedpure virtual |
Get the callback handling DoCommands in case of networking.
Implemented in AIInstance, and GameInstance.
| ScriptLogTypes::LogData & ScriptInstance::GetLogData | ( | ) |
Get the log pointer of this script.
Definition at line 336 of file script_instance.cpp.
| SQInteger ScriptInstance::GetOpsTillSuspend | ( | ) |
Get the number of operations the script can execute before being suspended.
This function is safe to call from within a function called by the script.
Definition at line 809 of file script_instance.cpp.
References Squirrel::GetOpsTillSuspend().
Referenced by Save().
|
pure virtual |
Get the value of a setting of the current instance.
| name | The name of the setting. |
Implemented in AIInstance, and GameInstance.
| ScriptStorage & ScriptInstance::GetStorage | ( | ) |
Get the storage of this script.
Definition at line 330 of file script_instance.cpp.
References storage.
| void ScriptInstance::Initialize | ( | const std::string & | main_script, |
| const std::string & | instance_name, | ||
| CompanyID | company ) |
Initialize the script and prepare it for its first run.
| main_script | The full path of the script to load. |
| instance_name | The name of the instance out of the script to load. |
| company | Which company this script is serving. |
Definition at line 58 of file script_instance.cpp.
References controller, Died(), engine, Script_FatalError::GetErrorMessage(), instance, is_dead, IsDead(), LoadDummyScript(), and RegisterAPI().
Referenced by AIInstance::Initialize(), and GameInstance::Initialize().
| void ScriptInstance::InsertEvent | ( | class ScriptEvent * | event | ) |
Insert an event for this script.
| event | The event to insert. |
Definition at line 838 of file script_instance.cpp.
|
inline |
Indicate whether this instance is currently being destroyed.
true iff being shut down. Definition at line 266 of file script_instance.hpp.
References in_shutdown.
|
inline |
Return whether the script is alive.
true iff the script not dead and not dying (being shut down). Definition at line 178 of file script_instance.hpp.
References in_shutdown, and IsDead().
Referenced by GameInstance::RegisterAPI().
|
inline |
Return the "this script died" value.
true iff the script is dead. Definition at line 172 of file script_instance.hpp.
References is_dead.
Referenced by CollectGarbage(), GameLoop(), Initialize(), IsAlive(), ScriptDebugWindow::IsDead(), LoadOnStack(), ShowScriptDebugWindowIfScriptError(), and ScriptDebugWindow::UpdateGSButtonState().
| bool ScriptInstance::IsPaused | ( | ) |
Checks if the script is paused.
Definition at line 596 of file script_instance.cpp.
References is_paused.
Referenced by ScriptDebugWindow::UpdateGSButtonState().
|
inline |
Check if the instance is sleeping, which either happened because the script executed a DoCommand, executed this.Sleep() or it has been paused.
true iff the script is sleeping or paused. Definition at line 258 of file script_instance.hpp.
References suspend.
|
static |
Load data from a savegame.
| version | The version of the script when saving, or -1 if this was not the original script saving the game. |
Definition at line 733 of file script_instance.cpp.
References _script_byte, _script_sl_byte, LoadEmpty(), LoadObjects(), and SlObject().
Referenced by AIPLChunkHandler::Load(), and GSDTChunkHandler::Load().
|
private |
Load squirrel script for a specific version to emulate an older API.
| api_version | API version to load scripts for. |
| dir | Subdirectory to find the scripts in. |
Definition at line 105 of file script_instance.cpp.
References api_version, Debug, engine, and FileExists().
Referenced by LoadCompatibilityScripts().
|
protected |
Load squirrel scripts to emulate an older API.
| dir | Subdirectory to find the scripts in. |
| api_versions | List of available versions of the script type. |
Definition at line 125 of file script_instance.cpp.
References api_version, engine, and LoadCompatibilityScript().
Referenced by AIInstance::RegisterAPI(), and GameInstance::RegisterAPI().
|
protectedpure virtual |
|
static |
Load and discard data from a savegame.
Definition at line 724 of file script_instance.cpp.
References _script_byte, _script_sl_byte, LoadObjects(), and SlObject().
Referenced by AIPLChunkHandler::Load(), GSDTChunkHandler::Load(), and Load().
|
staticprivate |
Definition at line 652 of file script_instance.cpp.
|
staticprivate |
Load all objects from a savegame.
| data | The data from the savegame. |
Definition at line 601 of file script_instance.cpp.
References _script_byte, _script_sl_byte, IsSavegameVersionBefore(), LoadObjects(), SlCopy(), SlErrorCorrupt(), SlObject(), SLV_SCRIPT_INT64, SQSL_ARRAY, SQSL_ARRAY_TABLE_END, SQSL_BOOL, SQSL_INSTANCE, SQSL_INT, SQSL_NULL, SQSL_STRING, SQSL_TABLE, and StrMakeValid().
Referenced by Load(), LoadEmpty(), LoadObjects(), and LoadOnStack().
| void ScriptInstance::LoadOnStack | ( | ScriptData * | data | ) |
Store loaded data on the stack.
| data | The loaded data to store on the stack. |
Definition at line 750 of file script_instance.cpp.
References Script_FatalError::GetErrorMessage(), Squirrel::GetVM(), is_save_data_on_stack, IsDead(), and LoadObjects().
| void ScriptInstance::Pause | ( | ) |
Suspends the script for the current tick and then pause the execution of script.
The script will not be resumed from its suspended state until the script has been unpaused.
Definition at line 582 of file script_instance.cpp.
References _settings_game, Squirrel::DecreaseOps(), engine, and is_paused.
|
protectedvirtual |
Register all API functions to the VM.
Reimplemented in AIInstance, and GameInstance.
Definition at line 100 of file script_instance.cpp.
References engine, and squirrel_register_std().
Referenced by Initialize(), AIInstance::RegisterAPI(), and GameInstance::RegisterAPI().
| void ScriptInstance::ReleaseSQObject | ( | HSQOBJECT * | obj | ) |
Decrease the ref count of a squirrel object.
| obj | The object to release. |
Definition at line 851 of file script_instance.cpp.
References in_shutdown, and Squirrel::ReleaseObject().
| void ScriptInstance::Save | ( | ) |
Call the script Save function and save all data in the savegame.
Definition at line 514 of file script_instance.cpp.
References _script_byte, _script_sl_byte, engine, Script_FatalError::GetErrorMessage(), GetOpsTillSuspend(), is_dead, is_save_data_on_stack, is_started, MAX_SL_OPS, SaveEmpty(), SaveObject(), SlObject(), and SQUIRREL_MAX_DEPTH.
|
static |
Don't save any data in the savegame.
Definition at line 508 of file script_instance.cpp.
References _script_byte, _script_sl_byte, and SlObject().
Referenced by AI::Save(), Game::Save(), and Save().
|
staticprivate |
Save one object (int / string / array / table) to the savegame.
| vm | The virtual machine to get all the data from. |
| index | The index on the squirrel stack of the element to save. |
| max_depth | The maximum depth recursive arrays / tables will be stored with before an error is returned. |
| test | If true, don't really store the data but only check if it is valid. |
Definition at line 371 of file script_instance.cpp.
References _script_byte, _script_sl_byte, Squirrel::GetRealInstance(), SaveObject(), SlCopy(), SlObject(), SQSL_ARRAY, SQSL_ARRAY_TABLE_END, SQSL_BOOL, SQSL_INSTANCE, SQSL_INT, SQSL_NULL, SQSL_STRING, and SQSL_TABLE.
Referenced by Save(), and SaveObject().
| void ScriptInstance::Unpause | ( | ) |
Resume execution of the script.
This function will not actually execute the script, but set a flag so that the script is executed my the usual mechanism that executes the script.
Definition at line 591 of file script_instance.cpp.
References is_paused.
|
friend |
Definition at line 41 of file script_instance.hpp.
|
friend |
Definition at line 40 of file script_instance.hpp.
|
protected |
Current API used by this script.
Definition at line 276 of file script_instance.hpp.
Referenced by AIInstance::Initialize(), GameInstance::Initialize(), LoadCompatibilityScript(), and LoadCompatibilityScripts().
|
private |
Callback that should be called in the next tick the script runs.
Definition at line 318 of file script_instance.hpp.
Referenced by GameLoop().
|
private |
The script main class.
Definition at line 309 of file script_instance.hpp.
Referenced by GameLoop(), GetController(), and Initialize().
|
protected |
A wrapper around the squirrel vm.
Definition at line 275 of file script_instance.hpp.
Referenced by CollectGarbage(), Died(), GameLoop(), AIInstance::Initialize(), GameInstance::Initialize(), Initialize(), LoadCompatibilityScript(), LoadCompatibilityScripts(), AIInstance::LoadDummyScript(), Pause(), AIInstance::RegisterAPI(), GameInstance::RegisterAPI(), RegisterAPI(), Save(), ScriptInstance(), and ~ScriptInstance().
|
private |
Is this instance currently being destructed?
Definition at line 317 of file script_instance.hpp.
Referenced by Died(), InShutdown(), IsAlive(), ReleaseSQObject(), and ~ScriptInstance().
|
private |
Squirrel-pointer to the script main class.
Definition at line 310 of file script_instance.hpp.
Referenced by CallLoad(), Died(), DoCommandReturn(), DoCommandReturnGoalID(), DoCommandReturnGroupID(), DoCommandReturnLeagueTableElementID(), DoCommandReturnLeagueTableID(), DoCommandReturnSignID(), DoCommandReturnStoryPageElementID(), DoCommandReturnStoryPageID(), DoCommandReturnVehicleID(), FindLibrary(), Initialize(), and ~ScriptInstance().
|
private |
True if the script has been stopped.
Definition at line 313 of file script_instance.hpp.
Referenced by Died(), GameLoop(), Initialize(), IsDead(), and Save().
|
private |
Is the script paused? (a paused script will not be executed until unpaused).
Definition at line 316 of file script_instance.hpp.
Referenced by GameLoop(), IsPaused(), Pause(), and Unpause().
|
private |
Is the save data still on the squirrel stack?
Definition at line 314 of file script_instance.hpp.
Referenced by CallLoad(), GameLoop(), LoadOnStack(), and Save().
|
private |
Is the scripts constructor executed?
Definition at line 312 of file script_instance.hpp.
Referenced by CollectGarbage(), GameLoop(), and Save().
|
private |
Last known allocated memory value (for display for crashed scripts).
Definition at line 319 of file script_instance.hpp.
Referenced by Died().
|
private |
Some global information for each running script.
Definition at line 308 of file script_instance.hpp.
Referenced by GetStorage(), and ScriptInstance().
|
private |
The amount of ticks to suspend this script before it's allowed to continue.
Definition at line 315 of file script_instance.hpp.
Referenced by Continue(), GameLoop(), and IsSleeping().