OpenTTD

squirrel.hpp
Go to the documentation of this file.
1 /* $Id: squirrel.hpp 26771 2014-09-06 17:30:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 <http://www.gnu.org/licenses/>.
8 */
9
12 #ifndef SQUIRREL_HPP
13 #define SQUIRREL_HPP
14
15 #include <squirrel.h>
16
18 enum ScriptType {
19 ST_AI,
20 ST_GS,
21 };
22
23 class Squirrel {
24 private:
25 typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
26
27 HSQUIRRELVM vm;
28 void *global_pointer;
29 SQPrintFunc *print_func;
30 bool crashed;
31 int overdrawn_ops;
32 const char *APIName;
33
37 static SQInteger _RunError(HSQUIRRELVM vm);
38
42 const char *GetAPIName() { return this->APIName; }
43
45 void Initialize();
47 void Uninitialize();
48
49 protected:
53 static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
54
58 static void RunError(HSQUIRRELVM vm, const SQChar *error);
59
63 static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
64
68 static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
69
70 public:
71 Squirrel(const char *APIName);
72 ~Squirrel();
73
77 HSQUIRRELVM GetVM() { return this->vm; }
78
84 bool LoadScript(const char *script);
85 bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
86
90 SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
91
96 void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
97
102 void AddConst(const char *var_name, int value);
103
108 void AddConst(const char *var_name, uint value) { this->AddConst(var_name, (int)value); }
109
114 void AddConst(const char *var_name, bool value);
115
120 void AddClassBegin(const char *class_name);
121
126 void AddClassBegin(const char *class_name, const char *parent_class);
127
132 void AddClassEnd();
133
137 bool Resume(int suspend = -1);
138
142 void ResumeError();
143
147 void CollectGarbage();
148
149 void InsertResult(bool result);
150 void InsertResult(int result);
151 void InsertResult(uint result) { this->InsertResult((int)result); }
152
157 bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend);
158 bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, NULL, suspend); }
159 bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend);
160 bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
161 bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
162
166 bool MethodExists(HSQOBJECT instance, const char *method_name);
167
178 static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name = false);
179
183 bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
184
190 static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
191
197 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; }
198
202 static const char *ObjectToString(HSQOBJECT *ptr) { return sq_objtostring(ptr); }
203
207 static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
208
212 static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
213
218 void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
219
223 static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
224
228 void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
229
233 void ThrowError(const char *error) { sq_throwerror(this->vm, error); }
234
238 void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
239
243 static void DecreaseOps(HSQUIRRELVM vm, int amount);
244
249 bool IsSuspended();
250
254 bool HasScriptCrashed();
255
259 void CrashOccurred();
260
264 bool CanSuspend();
265
269 SQInteger GetOpsTillSuspend();
270
274 void Reset();
275 };
276
277 #endif /* SQUIRREL_HPP */
Squirrel::GetRealInstance
static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr)
Get the real-instance pointer.
Definition: squirrel.hpp:190
Squirrel::ObjectToString
static const char * ObjectToString(HSQOBJECT *ptr)
Convert a Squirrel-object to a string.
Definition: squirrel.hpp:202
Squirrel::PrintFunc
static void PrintFunc(HSQUIRRELVM vm, const SQChar *s,...)
If a user runs &#39;print&#39; inside a script, this function gets the params.
Definition: squirrel.cpp:97
Squirrel::HasScriptCrashed
bool HasScriptCrashed()
Find out if the squirrel script made an error before.
Definition: squirrel.cpp:582
Squirrel::AddMethod
void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=NULL, void *userdata=NULL, int size=0)
Adds a function to the stack.
Definition: squirrel.cpp:116
Squirrel::global_pointer
void * global_pointer
Can be set by who ever initializes Squirrel.
Definition: squirrel.hpp:28
Squirrel::ThrowError
void ThrowError(const char *error)
Throw a Squirrel error that will be nicely displayed to the user.
Definition: squirrel.hpp:233
Squirrel::CallMethod
bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
Definition: squirrel.cpp:217
Squirrel::CompileError
static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
The CompileError handler.
Definition: squirrel.cpp:24
Squirrel::CollectGarbage
void CollectGarbage()
Tell the VM to do a garbage collection run.
Definition: squirrel.cpp:212
Squirrel::GetOpsTillSuspend
SQInteger GetOpsTillSuspend()
How many operations can we execute till suspension?
Definition: squirrel.cpp:597
Squirrel::GetInstance
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
Definition: squirrel.hpp:197
Squirrel::_RunError
static SQInteger _RunError(HSQUIRRELVM vm)
The internal RunError handler.
Definition: squirrel.cpp:82
Squirrel::CrashOccurred
void CrashOccurred()
Set the script status to crashed.
Definition: squirrel.cpp:587
Squirrel::DecreaseOps
static void DecreaseOps(HSQUIRRELVM vm, int amount)
Tell the VM to remove amount ops from the number of ops till suspend.
Definition: squirrel.cpp:572
Squirrel::CreateClassInstance
bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance)
Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
Definition: squirrel.cpp:326
ST_AI
The script is for AI scripts.
Definition: squirrel.hpp:19
Squirrel::GetGlobalPointer
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
Definition: squirrel.hpp:223
Squirrel::crashed
bool crashed
True if the squirrel script made an error.
Definition: squirrel.hpp:30
Squirrel::GetAPIName
const char * GetAPIName()
Get the API name.
Definition: squirrel.hpp:42
ScriptType
ScriptType
The type of script we&#39;re working with, i.e.
Definition: squirrel.hpp:18
Squirrel::ObjectToBool
static bool ObjectToBool(HSQOBJECT *ptr)
Convert a Squirrel-object to a bool.
Definition: squirrel.hpp:212
ST_GS
The script is for Game scripts.
Definition: squirrel.hpp:20
Squirrel::ResumeError
void ResumeError()
Resume the VM with an error so it prints a stack trace.
Definition: squirrel.cpp:206
Squirrel::overdrawn_ops
int overdrawn_ops
The amount of operations we have overdrawn.
Definition: squirrel.hpp:31
Squirrel::CreateClassInstanceVM
static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name=false)
Creates a class instance.
Definition: squirrel.cpp:276
Squirrel::IsSuspended
bool IsSuspended()
Did the squirrel code suspend or return normally.
Definition: squirrel.cpp:577
Squirrel::GetVM
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:77
Squirrel::ObjectToInteger
static int ObjectToInteger(HSQOBJECT *ptr)
Convert a Squirrel-object to an integer.
Definition: squirrel.hpp:207
Squirrel::Reset
void Reset()
Completely reset the engine; start from scratch.
Definition: squirrel.cpp:548
Squirrel::CanSuspend
bool CanSuspend()
Are we allowed to suspend the squirrel script at this moment?
Definition: squirrel.cpp:592
Squirrel::SetGlobalPointer
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
Definition: squirrel.hpp:218
Squirrel::AddConst
void AddConst(const char *var_name, int value)
Adds a const to the stack.
Definition: squirrel.cpp:131
Squirrel::MethodExists
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:171
Squirrel::LoadFile
SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror)
Load a file to a given VM.
Definition: squirrel.cpp:433
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
Squirrel::AddClassBegin
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:145
Squirrel::AddConst
void AddConst(const char *var_name, uint value)
Adds a const to the stack.
Definition: squirrel.hpp:108
Squirrel::vm
HSQUIRRELVM vm
The VirtualMachine instance for squirrel.
Definition: squirrel.hpp:27
Squirrel::RunError
static void RunError(HSQUIRRELVM vm, const SQChar *error)
The RunError handler.
Definition: squirrel.cpp:59
Squirrel::APIName
const char * APIName
Name of the API used for this squirrel.
Definition: squirrel.hpp:32
Squirrel::LoadScript
bool LoadScript(const char *script)
Load a script.
Definition: squirrel.cpp:531
Squirrel::Uninitialize
void Uninitialize()
Perform all the cleanups for the engine.
Definition: squirrel.cpp:541
Squirrel
Definition: squirrel.hpp:23
Squirrel::ReleaseObject
void ReleaseObject(HSQOBJECT *ptr)
Release a SQ object.
Definition: squirrel.hpp:238
Squirrel::AddClassEnd
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:165
Squirrel::print_func
SQPrintFunc * print_func
Points to either NULL, or a custom print handler.
Definition: squirrel.hpp:29
Squirrel::ErrorPrintFunc
static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s,...)
If an error has to be print, this function is called.
Definition: squirrel.cpp:41
Squirrel::Initialize
void Initialize()
Perform all initialization steps to create the engine.
Definition: squirrel.cpp:337
Squirrel::Resume
bool Resume(int suspend=-1)
Resume a VM when it was suspended via a throw.
Definition: squirrel.cpp:187
Squirrel::SetPrintFunction
void SetPrintFunction(SQPrintFunc *func)
Set a custom print function, so you can handle outputs from SQ yourself.
Definition: squirrel.hpp:228