00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef SQUIRREL_HPP
00013 #define SQUIRREL_HPP
00014
00015 class Squirrel {
00016 private:
00017 typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
00018
00019 HSQUIRRELVM vm;
00020 void *global_pointer;
00021 SQPrintFunc *print_func;
00022 bool crashed;
00023
00027 static SQInteger _RunError(HSQUIRRELVM vm);
00028
00032 HSQUIRRELVM GetVM() { return this->vm; }
00033
00034 protected:
00038 static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
00039
00043 static void RunError(HSQUIRRELVM vm, const SQChar *error);
00044
00048 static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00049
00053 static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00054
00055 public:
00056 friend class AIScanner;
00057 friend class AIInstance;
00058 friend void squirrel_register_std(Squirrel *engine);
00059
00060 Squirrel();
00061 ~Squirrel();
00062
00068 bool LoadScript(const char *script);
00069 static bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00070
00074 static SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00075
00080 void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00081
00086 void AddConst(const char *var_name, int value);
00087
00092 void AddConst(const char *var_name, bool value);
00093
00098 void AddClassBegin(const char *class_name);
00099
00104 void AddClassBegin(const char *class_name, const char *parent_class);
00105
00110 void AddClassEnd();
00111
00115 bool Resume(int suspend = -1);
00116
00120 void ResumeError();
00121
00125 void CollectGarbage();
00126
00127 void InsertResult(bool result);
00128 void InsertResult(int result);
00129
00134 bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend = -1);
00135 bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend = -1) { return this->CallMethod(instance, method_name, NULL, suspend); }
00136 bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend = -1);
00137 bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend = -1);
00138 bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend = -1);
00139
00143 bool MethodExists(HSQOBJECT instance, const char *method_name);
00144
00154 static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
00155
00159 bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00160
00166 static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00167
00173 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; }
00174
00178 static const char *ObjectToString(HSQOBJECT *ptr) { return FS2OTTD(sq_objtostring(ptr)); }
00179
00183 static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00184
00188 static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
00189
00194 void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00195
00199 static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00200
00204 void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00205
00209 void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2FS(error)); }
00210
00214 void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00215
00219 static void DecreaseOps(HSQUIRRELVM vm, int amount);
00220
00225 bool IsSuspended();
00226
00230 bool HasScriptCrashed();
00231
00235 void ResetCrashed();
00236
00240 void CrashOccurred();
00241
00245 bool CanSuspend();
00246 };
00247
00248 #endif