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 {
21 };
22 
23 class Squirrel {
24 private:
25  typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
26 
27  HSQUIRRELVM vm;
29  SQPrintFunc *print_func;
30  bool crashed;
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 */
static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr)
Get the real-instance pointer.
Definition: squirrel.hpp:190
static const char * ObjectToString(HSQOBJECT *ptr)
Convert a Squirrel-object to a string.
Definition: squirrel.hpp:202
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
bool HasScriptCrashed()
Find out if the squirrel script made an error before.
Definition: squirrel.cpp:582
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
void * global_pointer
Can be set by who ever initializes Squirrel.
Definition: squirrel.hpp:28
void ThrowError(const char *error)
Throw a Squirrel error that will be nicely displayed to the user.
Definition: squirrel.hpp:233
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
static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
The CompileError handler.
Definition: squirrel.cpp:24
void CollectGarbage()
Tell the VM to do a garbage collection run.
Definition: squirrel.cpp:212
SQInteger GetOpsTillSuspend()
How many operations can we execute till suspension?
Definition: squirrel.cpp:597
static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr, int pos=1)
Get the Squirrel-instance pointer.
Definition: squirrel.hpp:197
static SQInteger _RunError(HSQUIRRELVM vm)
The internal RunError handler.
Definition: squirrel.cpp:82
void CrashOccurred()
Set the script status to crashed.
Definition: squirrel.cpp:587
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
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
The script is for AI scripts.
Definition: squirrel.hpp:19
static void * GetGlobalPointer(HSQUIRRELVM vm)
Get the pointer as set by SetGlobalPointer.
Definition: squirrel.hpp:223
bool crashed
True if the squirrel script made an error.
Definition: squirrel.hpp:30
const char * GetAPIName()
Get the API name.
Definition: squirrel.hpp:42
ScriptType
The type of script we&#39;re working with, i.e.
Definition: squirrel.hpp:18
static bool ObjectToBool(HSQOBJECT *ptr)
Convert a Squirrel-object to a bool.
Definition: squirrel.hpp:212
The script is for Game scripts.
Definition: squirrel.hpp:20
void ResumeError()
Resume the VM with an error so it prints a stack trace.
Definition: squirrel.cpp:206
int overdrawn_ops
The amount of operations we have overdrawn.
Definition: squirrel.hpp:31
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
bool IsSuspended()
Did the squirrel code suspend or return normally.
Definition: squirrel.cpp:577
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:77
static int ObjectToInteger(HSQOBJECT *ptr)
Convert a Squirrel-object to an integer.
Definition: squirrel.hpp:207
void Reset()
Completely reset the engine; start from scratch.
Definition: squirrel.cpp:548
bool CanSuspend()
Are we allowed to suspend the squirrel script at this moment?
Definition: squirrel.cpp:592
void SetGlobalPointer(void *ptr)
Sets a pointer in the VM that is reachable from where ever you are in SQ.
Definition: squirrel.hpp:218
void AddConst(const char *var_name, int value)
Adds a const to the stack.
Definition: squirrel.cpp:131
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:171
SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror)
Load a file to a given VM.
Definition: squirrel.cpp:433
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:110
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:145
void AddConst(const char *var_name, uint value)
Adds a const to the stack.
Definition: squirrel.hpp:108
HSQUIRRELVM vm
The VirtualMachine instance for squirrel.
Definition: squirrel.hpp:27
static void RunError(HSQUIRRELVM vm, const SQChar *error)
The RunError handler.
Definition: squirrel.cpp:59
const char * APIName
Name of the API used for this squirrel.
Definition: squirrel.hpp:32
bool LoadScript(const char *script)
Load a script.
Definition: squirrel.cpp:531
void Uninitialize()
Perform all the cleanups for the engine.
Definition: squirrel.cpp:541
void ReleaseObject(HSQOBJECT *ptr)
Release a SQ object.
Definition: squirrel.hpp:238
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:165
SQPrintFunc * print_func
Points to either NULL, or a custom print handler.
Definition: squirrel.hpp:29
static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s,...)
If an error has to be print, this function is called.
Definition: squirrel.cpp:41
void Initialize()
Perform all initialization steps to create the engine.
Definition: squirrel.cpp:337
bool Resume(int suspend=-1)
Resume a VM when it was suspended via a throw.
Definition: squirrel.cpp:187
void SetPrintFunction(SQPrintFunc *func)
Set a custom print function, so you can handle outputs from SQ yourself.
Definition: squirrel.hpp:228