00001
00002
00005 #ifndef SQUIRREL_HPP
00006 #define SQUIRREL_HPP
00007
00008 class Squirrel {
00009 private:
00010 typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
00011
00012 HSQUIRRELVM vm;
00013 void *global_pointer;
00014 SQPrintFunc *print_func;
00015 bool crashed;
00016
00020 static SQInteger _RunError(HSQUIRRELVM vm);
00021
00025 HSQUIRRELVM GetVM() { return this->vm; }
00026
00027 protected:
00031 static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
00032
00036 static void RunError(HSQUIRRELVM vm, const SQChar *error);
00037
00041 static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00042
00046 static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00047
00048 public:
00049 friend class AIScanner;
00050 friend class AIInstance;
00051
00052 Squirrel();
00053 ~Squirrel();
00054
00060 bool LoadScript(const char *script);
00061 static bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00062
00066 static SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00067
00072 void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00073
00078 void AddConst(const char *var_name, int value);
00079
00084 void AddConst(const char *var_name, bool value);
00085
00090 void AddClassBegin(const char *class_name);
00091
00096 void AddClassBegin(const char *class_name, const char *parent_class);
00097
00102 void AddClassEnd();
00103
00107 bool Resume(int suspend = -1);
00108
00112 void ResumeError();
00113
00117 void CollectGarbage();
00118
00119 void InsertResult(bool result);
00120 void InsertResult(int result);
00121
00126 bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend = -1);
00127 bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend = -1) { return this->CallMethod(instance, method_name, NULL, suspend); }
00128 bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend = -1);
00129 bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend = -1);
00130 bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend = -1);
00131
00135 bool MethodExists(HSQOBJECT instance, const char *method_name);
00136
00145 static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
00146
00150 bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00151
00157 static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00158
00164 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; }
00165
00169 static const char *ObjectToString(HSQOBJECT *ptr) { return FS2OTTD(sq_objtostring(ptr)); }
00170
00174 static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00175
00179 static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
00180
00185 void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00186
00190 static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00191
00195 void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00196
00200 void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2FS(error)); }
00201
00205 void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00206
00210 static void DecreaseOps(HSQUIRRELVM vm, int amount);
00211
00216 bool IsSuspended();
00217
00221 bool HasScriptCrashed();
00222
00226 void ResetCrashed();
00227
00231 void CrashOccurred();
00232
00236 bool CanSuspend();
00237 };
00238
00239 #endif