OpenTTD Source
20240919-master-gdf0233f4c2
|
Go to the documentation of this file.
27 typedef void (SQPrintFunc)(
bool error_msg,
const std::string &message);
56 static void CompileError(HSQUIRRELVM
vm,
const SQChar *desc,
const SQChar *source, SQInteger line, SQInteger column);
61 static void RunError(HSQUIRRELVM
vm,
const SQChar *error);
66 static void PrintFunc(HSQUIRRELVM
vm,
const std::string &s);
88 bool LoadScript(HSQUIRRELVM
vm,
const std::string &script,
bool in_root =
true);
93 SQRESULT
LoadFile(HSQUIRRELVM
vm,
const std::string &filename, SQBool printerror);
99 void AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam = 0,
const char *params =
nullptr,
void *userdata =
nullptr,
int size = 0);
105 void AddConst(
const char *var_name,
int value);
117 void AddConst(
const char *var_name,
bool value);
129 void AddClassBegin(
const char *class_name,
const char *parent_class);
140 bool Resume(
int suspend = -1);
152 void InsertResult(
bool result);
153 void InsertResult(
int result);
154 void InsertResult(uint result) { this->InsertResult((
int)result); }
160 bool CallMethod(HSQOBJECT instance,
const char *method_name, HSQOBJECT *ret,
int suspend);
161 bool CallMethod(HSQOBJECT instance,
const char *method_name,
int suspend) {
return this->
CallMethod(instance, method_name,
nullptr, suspend); }
162 bool CallStringMethod(HSQOBJECT instance,
const char *method_name, std::string *res,
int suspend);
163 bool CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend);
164 bool CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend);
169 bool MethodExists(HSQOBJECT instance,
const char *method_name);
181 static bool CreateClassInstanceVM(HSQUIRRELVM
vm,
const std::string &class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name =
false);
186 bool CreateClassInstance(
const std::string &class_name,
void *real_instance, HSQOBJECT *instance);
193 static bool GetRealInstance(HSQUIRRELVM
vm, SQUserPointer *ptr) {
return SQ_SUCCEEDED(sq_getinstanceup(
vm, 1, ptr,
nullptr)); }
200 static bool GetInstance(HSQUIRRELVM
vm, HSQOBJECT *ptr,
int pos = 1) { sq_getclass(
vm, pos); sq_getstackobj(
vm, pos, ptr); sq_pop(
vm, 1);
return true; }
205 static const char *
ObjectToString(HSQOBJECT *ptr) {
return sq_objtostring(ptr); }
215 static bool ObjectToBool(HSQOBJECT *ptr) {
return sq_objtobool(ptr) == 1; }
236 void ThrowError(
const std::string_view error) { sq_throwerror(this->vm, error); }
void SetPrintFunction(SQPrintFunc *func)
Set a custom print function, so you can handle outputs from SQ yourself.
static const char * ObjectToString(HSQOBJECT *ptr)
Convert a Squirrel-object to a string.
const char * GetAPIName()
Get the API name.
bool CanSuspend()
Are we allowed to suspend the squirrel script at this moment?
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.
ScriptType
The type of script we're working with, i.e.
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
In the memory allocator for Squirrel we want to directly use malloc/realloc, so when the OS does not ...
@ GS
The script is for Game scripts.
HSQUIRRELVM GetVM()
Get the squirrel VM.
void Reset()
Completely reset the engine; start from scratch.
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
HSQUIRRELVM vm
The VirtualMachine instance for squirrel.
SQInteger GetOpsTillSuspend()
How many operations can we execute till suspension?
static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr)
Get the real-instance pointer.
void Initialize()
Perform all initialization steps to create the engine.
void * global_pointer
Can be set by who ever initializes Squirrel.
static int ObjectToInteger(HSQOBJECT *ptr)
Convert a Squirrel-object to an integer.
bool HasScriptCrashed()
Find out if the squirrel script made an error before.
void AddClassEnd()
Finishes adding a class to the global scope.
int overdrawn_ops
The amount of operations we have overdrawn.
void Uninitialize()
Perform all the cleanups for the engine.
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
size_t GetAllocatedMemory() const noexcept
Get number of bytes allocated by this VM.
static void PrintFunc(HSQUIRRELVM vm, const std::string &s)
If a user runs 'print' inside a script, this function gets the params.
void ThrowError(const std::string_view error)
Throw a Squirrel error that will be nicely displayed to the user.
bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
static bool ObjectToBool(HSQOBJECT *ptr)
Convert a Squirrel-object to a bool.
void AddConst(const char *var_name, int value)
Adds a const to the stack.
static void ErrorPrintFunc(HSQUIRRELVM vm, const std::string &s)
If an error has to be print, this function is called.
std::unique_ptr< ScriptAllocator > allocator
Allocator object used by this script.
void ResumeError()
Resume the VM with an error so it prints a stack trace.
bool LoadScript(const std::string &script)
Load a script.
void CollectGarbage()
Tell the VM to do a garbage collection run.
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
@ AI
The script is for AI scripts.
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 CrashOccurred()
Set the script status to crashed.
bool crashed
True if the squirrel script made an error.
void AddConst(const char *var_name, uint value)
Adds a const to the stack.
static void RunError(HSQUIRRELVM vm, const SQChar *error)
The RunError handler.
ScriptAllocator * _squirrel_allocator
In the memory allocator for Squirrel we want to directly use malloc/realloc, so when the OS does not ...
bool Resume(int suspend=-1)
Resume a VM when it was suspended via a throw.
const char * APIName
Name of the API used for this squirrel.
void ReleaseObject(HSQOBJECT *ptr)
Release a SQ object.
bool IsSuspended()
Did the squirrel code suspend or return normally.
SQPrintFunc * print_func
Points to either nullptr, or a custom print handler.
SQRESULT LoadFile(HSQUIRRELVM vm, const std::string &filename, SQBool printerror)
Load a file to a given VM.
static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
The CompileError handler.
static void DecreaseOps(HSQUIRRELVM vm, int amount)
Tell the VM to remove amount ops from the number of ops till suspend.
bool CreateClassInstance(const std::string &class_name, void *real_instance, HSQOBJECT *instance)
Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
static SQInteger _RunError(HSQUIRRELVM vm)
The internal RunError handler.