OpenTTD Source 20250312-master-gcdcc6b491d
|
Public Member Functions | |
Squirrel (const char *APIName) | |
HSQUIRRELVM | GetVM () |
Get the squirrel VM. | |
bool | LoadScript (const std::string &script) |
Load a script. | |
bool | LoadScript (HSQUIRRELVM vm, const std::string &script, bool in_root=true) |
SQRESULT | LoadFile (HSQUIRRELVM vm, const std::string &filename, SQBool printerror) |
Load a file to a given VM. | |
void | AddMethod (const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=nullptr, void *userdata=nullptr, int size=0) |
Adds a function to the stack. | |
void | AddConst (const char *var_name, int value) |
Adds a const to the stack. | |
void | AddConst (const char *var_name, uint value) |
Adds a const to the stack. | |
void | AddConst (const char *var_name, const ConvertibleThroughBase auto &value) |
void | AddConst (const char *var_name, bool value) |
Adds a const to the stack. | |
void | AddClassBegin (const char *class_name) |
Adds a class to the global scope. | |
void | AddClassBegin (const char *class_name, const char *parent_class) |
Adds a class to the global scope, extending 'parent_class'. | |
void | AddClassEnd () |
Finishes adding a class to the global scope. | |
bool | Resume (int suspend=-1) |
Resume a VM when it was suspended via a throw. | |
void | ResumeError () |
Resume the VM with an error so it prints a stack trace. | |
void | CollectGarbage () |
Tell the VM to do a garbage collection run. | |
void | InsertResult (bool result) |
void | InsertResult (int result) |
void | InsertResult (uint result) |
void | InsertResult (ConvertibleThroughBase auto result) |
bool | CallMethod (HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend) |
Call a method of an instance, in various flavors. | |
bool | CallMethod (HSQOBJECT instance, const char *method_name, int suspend) |
bool | CallStringMethod (HSQOBJECT instance, const char *method_name, std::string *res, int suspend) |
bool | CallIntegerMethod (HSQOBJECT instance, const char *method_name, int *res, int suspend) |
bool | CallBoolMethod (HSQOBJECT instance, const char *method_name, bool *res, int suspend) |
bool | MethodExists (HSQOBJECT instance, const char *method_name) |
Check if a method exists in an instance. | |
bool | CreateClassInstance (const std::string &class_name, void *real_instance, HSQOBJECT *instance) |
Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel. | |
void | SetGlobalPointer (void *ptr) |
Sets a pointer in the VM that is reachable from where ever you are in SQ. | |
void | SetPrintFunction (SQPrintFunc *func) |
Set a custom print function, so you can handle outputs from SQ yourself. | |
void | ThrowError (const std::string_view error) |
Throw a Squirrel error that will be nicely displayed to the user. | |
void | ReleaseObject (HSQOBJECT *ptr) |
Release a SQ object. | |
bool | IsSuspended () |
Did the squirrel code suspend or return normally. | |
bool | HasScriptCrashed () |
Find out if the squirrel script made an error before. | |
void | CrashOccurred () |
Set the script status to crashed. | |
bool | CanSuspend () |
Are we allowed to suspend the squirrel script at this moment? | |
SQInteger | GetOpsTillSuspend () |
How many operations can we execute till suspension? | |
void | Reset () |
Completely reset the engine; start from scratch. | |
size_t | GetAllocatedMemory () const noexcept |
Get number of bytes allocated by this VM. | |
Static Public Member Functions | |
static bool | CreateClassInstanceVM (HSQUIRRELVM vm, const std::string &class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name=false) |
Creates a class instance. | |
static SQUserPointer | GetRealInstance (HSQUIRRELVM vm, int index, const char *tag) |
Get the real-instance pointer. | |
static bool | GetInstance (HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1) |
Get the Squirrel-instance pointer. | |
static const char * | ObjectToString (HSQOBJECT *ptr) |
Convert a Squirrel-object to a string. | |
static int | ObjectToInteger (HSQOBJECT *ptr) |
Convert a Squirrel-object to an integer. | |
static bool | ObjectToBool (HSQOBJECT *ptr) |
Convert a Squirrel-object to a bool. | |
static void * | GetGlobalPointer (HSQUIRRELVM vm) |
Get the pointer as set by SetGlobalPointer. | |
static void | DecreaseOps (HSQUIRRELVM vm, int amount) |
Tell the VM to remove amount ops from the number of ops till suspend. | |
Static Protected Member Functions | |
static void | CompileError (HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column) |
The CompileError handler. | |
static void | RunError (HSQUIRRELVM vm, const SQChar *error) |
The RunError handler. | |
static void | PrintFunc (HSQUIRRELVM vm, const std::string &s) |
If a user runs 'print' inside a script, this function gets the params. | |
static void | ErrorPrintFunc (HSQUIRRELVM vm, const std::string &s) |
If an error has to be print, this function is called. | |
Private Types | |
typedef void() | SQPrintFunc(bool error_msg, const std::string &message) |
Private Member Functions | |
const char * | GetAPIName () |
Get the API name. | |
void | Initialize () |
Perform all initialization steps to create the engine. | |
void | Uninitialize () |
Perform all the cleanups for the engine. | |
Static Private Member Functions | |
static SQInteger | _RunError (HSQUIRRELVM vm) |
The internal RunError handler. | |
Private Attributes | |
HSQUIRRELVM | vm |
The VirtualMachine instance for squirrel. | |
void * | global_pointer |
Can be set by who ever initializes Squirrel. | |
SQPrintFunc * | print_func |
Points to either nullptr, or a custom print handler. | |
bool | crashed |
True if the squirrel script made an error. | |
int | overdrawn_ops |
The amount of operations we have overdrawn. | |
const char * | APIName |
Name of the API used for this squirrel. | |
std::unique_ptr< ScriptAllocator > | allocator |
Allocator object used by this script. | |
Friends | |
class | ScriptAllocatorScope |
class | ScriptInstance |
Definition at line 24 of file squirrel.hpp.
|
private |
Definition at line 29 of file squirrel.hpp.
Squirrel::Squirrel | ( | const char * | APIName | ) |
Definition at line 506 of file squirrel.cpp.
Squirrel::~Squirrel | ( | ) |
Definition at line 721 of file squirrel.cpp.
|
staticprivate |
The internal RunError handler.
It looks up the real error and calls RunError with it.
Definition at line 228 of file squirrel.cpp.
References RunError(), and vm.
Referenced by Initialize().
void Squirrel::AddClassBegin | ( | const char * | class_name | ) |
Adds a class to the global scope.
Make sure to call AddClassEnd when you are done adding methods.
Definition at line 289 of file squirrel.cpp.
References vm.
Referenced by AILibrary::RegisterAPI(), and GameLibrary::RegisterAPI().
void Squirrel::AddClassBegin | ( | const char * | class_name, |
const char * | parent_class | ||
) |
Adds a class to the global scope, extending 'parent_class'.
Make sure to call AddClassEnd when you are done adding methods.
Definition at line 298 of file squirrel.cpp.
void Squirrel::AddClassEnd | ( | ) |
Finishes adding a class to the global scope.
If this isn't called, no class is really created.
Definition at line 313 of file squirrel.cpp.
References vm.
Referenced by AILibrary::RegisterAPI(), and GameLibrary::RegisterAPI().
void Squirrel::AddConst | ( | const char * | var_name, |
bool | value | ||
) |
Adds a const to the stack.
Depending on the current state this means either a const to a class or to the global space.
Definition at line 280 of file squirrel.cpp.
References vm.
|
inline |
Definition at line 115 of file squirrel.hpp.
void Squirrel::AddConst | ( | const char * | var_name, |
int | value | ||
) |
Adds a const to the stack.
Depending on the current state this means either a const to a class or to the global space.
Definition at line 271 of file squirrel.cpp.
References vm.
|
inline |
Adds a const to the stack.
Depending on the current state this means either a const to a class or to the global space.
Definition at line 113 of file squirrel.hpp.
References AddConst().
Referenced by AddConst().
void Squirrel::AddMethod | ( | const char * | method_name, |
SQFUNCTION | proc, | ||
uint | nparam = 0 , |
||
const char * | params = nullptr , |
||
void * | userdata = nullptr , |
||
int | size = 0 |
||
) |
Adds a function to the stack.
Depending on the current state this means either a method or a global function.
Definition at line 254 of file squirrel.cpp.
References vm.
Referenced by DefSQClass< CL, ST >::DefSQAdvancedMethod(), DefSQClass< CL, ST >::DefSQAdvancedStaticMethod(), DefSQClass< CL, ST >::DefSQMethod(), DefSQClass< CL, ST >::DefSQMethod(), DefSQClass< CL, ST >::DefSQStaticMethod(), DefSQClass< CL, ST >::DefSQStaticMethod(), AIInfo::RegisterAPI(), AILibrary::RegisterAPI(), GameInfo::RegisterAPI(), GameLibrary::RegisterAPI(), squirrel_register_global_std(), and squirrel_register_std().
bool Squirrel::CallBoolMethod | ( | HSQOBJECT | instance, |
const char * | method_name, | ||
bool * | res, | ||
int | suspend | ||
) |
Definition at line 426 of file squirrel.cpp.
bool Squirrel::CallIntegerMethod | ( | HSQOBJECT | instance, |
const char * | method_name, | ||
int * | res, | ||
int | suspend | ||
) |
Definition at line 417 of file squirrel.cpp.
bool Squirrel::CallMethod | ( | HSQOBJECT | instance, |
const char * | method_name, | ||
HSQOBJECT * | ret, | ||
int | suspend | ||
) |
Call a method of an instance, in various flavors.
Definition at line 374 of file squirrel.cpp.
References crashed, Debug, IsSuspended(), and vm.
Referenced by ScriptInstance::GameLoop(), ScriptInfo::GetSettings(), and ScriptInstance::Save().
|
inline |
Definition at line 166 of file squirrel.hpp.
bool Squirrel::CallStringMethod | ( | HSQOBJECT | instance, |
const char * | method_name, | ||
std::string * | res, | ||
int | suspend | ||
) |
Definition at line 408 of file squirrel.cpp.
bool Squirrel::CanSuspend | ( | ) |
Are we allowed to suspend the squirrel script at this moment?
Definition at line 792 of file squirrel.cpp.
void Squirrel::CollectGarbage | ( | ) |
Tell the VM to do a garbage collection run.
Definition at line 368 of file squirrel.cpp.
References vm.
Referenced by ScriptInstance::CollectGarbage().
|
staticprotected |
The CompileError handler.
Definition at line 180 of file squirrel.cpp.
References crashed, Debug, print_func, and vm.
Referenced by Initialize().
void Squirrel::CrashOccurred | ( | ) |
Set the script status to crashed.
Definition at line 787 of file squirrel.cpp.
References crashed.
Referenced by ScriptInstance::Save().
bool Squirrel::CreateClassInstance | ( | const std::string & | class_name, |
void * | real_instance, | ||
HSQOBJECT * | instance | ||
) |
Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
Definition at line 483 of file squirrel.cpp.
References CreateClassInstanceVM(), and vm.
Referenced by ScriptInstance::Initialize().
|
static |
Creates a class instance.
vm | The VM to create the class instance for |
class_name | The name of the class of which we create an instance. |
real_instance | The instance to the real class, if it represents a real class. |
instance | Returning value with the pointer to the instance. |
release_hook | Optional param to give a release hook. |
prepend_API_name | Optional parameter; if true, the class_name is prefixed with the current API name. |
Definition at line 435 of file squirrel.cpp.
References Debug, GetAPIName(), and vm.
Referenced by CreateClassInstance().
|
static |
Tell the VM to remove amount
ops from the number of ops till suspend.
Definition at line 772 of file squirrel.cpp.
References vm.
Referenced by ScriptInstance::Pause().
|
staticprotected |
If an error has to be print, this function is called.
Definition at line 195 of file squirrel.cpp.
References print_func, and vm.
Referenced by RunError().
|
noexcept |
Get number of bytes allocated by this VM.
Definition at line 173 of file squirrel.cpp.
|
inlineprivate |
Get the API name.
Definition at line 47 of file squirrel.hpp.
References APIName.
Referenced by CreateClassInstanceVM(), GetRealInstance(), and LoadFile().
|
inlinestatic |
Get the pointer as set by SetGlobalPointer.
Definition at line 231 of file squirrel.hpp.
References vm.
Referenced by ScriptInfo::Constructor().
|
inlinestatic |
Get the Squirrel-instance pointer.
Definition at line 205 of file squirrel.hpp.
References vm.
Referenced by ScriptInfo::Constructor(), SQConvert::DefSQAdvancedNonStaticCallback(), and SQConvert::DefSQNonStaticCallback().
SQInteger Squirrel::GetOpsTillSuspend | ( | ) |
How many operations can we execute till suspension?
Definition at line 798 of file squirrel.cpp.
Referenced by ScriptInstance::GetOpsTillSuspend().
|
static |
Get the real-instance pointer.
Definition at line 489 of file squirrel.cpp.
References GetAPIName(), and vm.
Referenced by ScriptInstance::SaveObject().
|
inline |
Get the squirrel VM.
Try to avoid using this.
Definition at line 82 of file squirrel.hpp.
References vm.
Referenced by ScriptInstance::CallLoad(), ScriptInstance::GameLoop(), AIInstance::LoadDummyScript(), ScriptInstance::LoadOnStack(), ScriptInstance::Pause(), RegisterGameTranslation(), ScriptInstance::Save(), and squirrel_register_std().
bool Squirrel::HasScriptCrashed | ( | ) |
Find out if the squirrel script made an error before.
Definition at line 782 of file squirrel.cpp.
References crashed.
Referenced by ScriptInstance::GameLoop(), and ScriptInstance::Save().
|
private |
Perform all initialization steps to create the engine.
Definition at line 512 of file squirrel.cpp.
References _RunError(), CompileError(), crashed, global_pointer, overdrawn_ops, print_func, PrintFunc(), squirrel_register_global_std(), and vm.
Referenced by Reset().
void Squirrel::InsertResult | ( | bool | result | ) |
Definition at line 750 of file squirrel.cpp.
|
inline |
Definition at line 159 of file squirrel.hpp.
void Squirrel::InsertResult | ( | int | result | ) |
Definition at line 761 of file squirrel.cpp.
|
inline |
Definition at line 158 of file squirrel.hpp.
bool Squirrel::IsSuspended | ( | ) |
Did the squirrel code suspend or return normally.
Definition at line 777 of file squirrel.cpp.
Referenced by CallMethod(), ScriptInstance::GameLoop(), ScriptInstance::Initialize(), and ScriptInstance::Save().
SQRESULT Squirrel::LoadFile | ( | HSQUIRRELVM | vm, |
const std::string & | filename, | ||
SQBool | printerror | ||
) |
Load a file to a given VM.
Definition at line 614 of file squirrel.cpp.
References AI_DIR, AI_LIBRARY_DIR, FioFOpenFile(), GAME_DIR, GAME_LIBRARY_DIR, GetAPIName(), and vm.
bool Squirrel::LoadScript | ( | const std::string & | script | ) |
Load a script.
script | The full script-name to load. |
Definition at line 716 of file squirrel.cpp.
References LoadScript().
Referenced by ScriptScanner::AddFile(), ScriptInstance::Initialize(), ScriptInstance::LoadCompatibilityScript(), LoadScript(), and SquirrelStd::require().
bool Squirrel::LoadScript | ( | HSQUIRRELVM | vm, |
const std::string & | script, | ||
bool | in_root = true |
||
) |
Definition at line 692 of file squirrel.cpp.
bool Squirrel::MethodExists | ( | HSQOBJECT | instance, |
const char * | method_name | ||
) |
Check if a method exists in an instance.
Definition at line 321 of file squirrel.cpp.
Referenced by ScriptInstance::CallLoad(), ScriptInfo::CheckMethod(), AIInfo::Constructor(), GameInfo::Constructor(), ScriptInfo::Constructor(), ScriptInstance::GameLoop(), and ScriptInstance::Save().
|
inlinestatic |
Convert a Squirrel-object to a bool.
Definition at line 220 of file squirrel.hpp.
|
inlinestatic |
Convert a Squirrel-object to an integer.
Definition at line 215 of file squirrel.hpp.
|
inlinestatic |
Convert a Squirrel-object to a string.
Definition at line 210 of file squirrel.hpp.
|
staticprotected |
If a user runs 'print' inside a script, this function gets the params.
Definition at line 243 of file squirrel.cpp.
References print_func, and vm.
Referenced by Initialize().
|
inline |
Release a SQ object.
Definition at line 246 of file squirrel.hpp.
Referenced by ScriptInstance::Died(), and ScriptInstance::ReleaseSQObject().
void Squirrel::Reset | ( | ) |
Completely reset the engine; start from scratch.
Definition at line 744 of file squirrel.cpp.
References Initialize(), and Uninitialize().
Referenced by ScriptScanner::ResetEngine().
bool Squirrel::Resume | ( | int | suspend = -1 | ) |
Resume a VM when it was suspended via a throw.
Definition at line 339 of file squirrel.cpp.
References crashed, overdrawn_ops, and vm.
Referenced by ScriptInstance::GameLoop().
void Squirrel::ResumeError | ( | ) |
Resume the VM with an error so it prints a stack trace.
Definition at line 361 of file squirrel.cpp.
Referenced by ScriptInstance::GameLoop(), ScriptInstance::Initialize(), and ScriptInstance::Save().
|
staticprotected |
The RunError handler.
Definition at line 206 of file squirrel.cpp.
References ErrorPrintFunc(), print_func, and vm.
Referenced by _RunError().
|
inline |
Sets a pointer in the VM that is reachable from where ever you are in SQ.
Useful to keep track of the main instance.
Definition at line 226 of file squirrel.hpp.
Referenced by ScriptInstance::Initialize(), and ScriptScanner::ResetEngine().
|
inline |
Set a custom print function, so you can handle outputs from SQ yourself.
Definition at line 236 of file squirrel.hpp.
Referenced by ScriptInstance::ScriptInstance().
|
inline |
Throw a Squirrel error that will be nicely displayed to the user.
Definition at line 241 of file squirrel.hpp.
Referenced by ScriptInfo::AddLabels(), ScriptInfo::AddSetting(), ScriptInfo::CheckMethod(), ScriptInstance::GameLoop(), ScriptInstance::Initialize(), and ScriptInstance::Save().
|
private |
Perform all the cleanups for the engine.
Definition at line 726 of file squirrel.cpp.
Referenced by Reset().
|
friend |
Definition at line 25 of file squirrel.hpp.
|
friend |
Definition at line 26 of file squirrel.hpp.
|
private |
Allocator object used by this script.
Definition at line 37 of file squirrel.hpp.
|
private |
Name of the API used for this squirrel.
Definition at line 36 of file squirrel.hpp.
Referenced by GetAPIName().
|
private |
True if the squirrel script made an error.
Definition at line 34 of file squirrel.hpp.
Referenced by CallMethod(), CompileError(), CrashOccurred(), HasScriptCrashed(), Initialize(), MethodExists(), Resume(), and ResumeError().
|
private |
Can be set by who ever initializes Squirrel.
Definition at line 32 of file squirrel.hpp.
Referenced by Initialize().
|
private |
The amount of operations we have overdrawn.
Definition at line 35 of file squirrel.hpp.
Referenced by Initialize(), and Resume().
|
private |
Points to either nullptr, or a custom print handler.
Definition at line 33 of file squirrel.hpp.
Referenced by CompileError(), ErrorPrintFunc(), Initialize(), PrintFunc(), and RunError().
|
private |
The VirtualMachine instance for squirrel.
Definition at line 31 of file squirrel.hpp.
Referenced by _RunError(), AddClassBegin(), AddClassBegin(), AddClassEnd(), AddConst(), AddConst(), AddMethod(), CallMethod(), CollectGarbage(), CompileError(), CreateClassInstance(), CreateClassInstanceVM(), DecreaseOps(), ErrorPrintFunc(), GetGlobalPointer(), GetInstance(), GetRealInstance(), GetVM(), Initialize(), LoadFile(), MethodExists(), PrintFunc(), Resume(), ResumeError(), and RunError().