OpenTTD: src/script/squirrel.hpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 */