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 AIController;
00050 friend class AIScanner;
00051 friend class AIInstance;
00052 friend class AIFileInfo;
00053
00054 Squirrel();
00055 ~Squirrel();
00056
00062 bool LoadScript(const char *script);
00063 static bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00064
00068 static SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00069
00074 void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00075
00080 void AddConst(const char *var_name, int value);
00081
00086 void AddClassBegin(const char *class_name);
00087
00092 void AddClassBegin(const char *class_name, const char *parent_class);
00093
00098 void AddClassEnd();
00099
00103 bool Resume(int suspend = -1);
00104
00108 void CollectGarbage();
00109
00110 void InsertResult(bool result);
00111 void InsertResult(int result);
00112
00117 bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend = -1);
00118 bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend = -1) { return this->CallMethod(instance, method_name, NULL, suspend); }
00119 bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend = -1);
00120 bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend = -1);
00121
00125 bool MethodExists(HSQOBJECT instance, const char *method_name);
00126
00135 static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
00136
00140 bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00141
00147 static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00148
00154 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; }
00155
00159 static const char *ObjectToString(HSQOBJECT *ptr) { return FS2OTTD(sq_objtostring(ptr)); }
00160
00164 static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00165
00170 void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00171
00175 static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00176
00180 void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00181
00185 void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2FS(error)); }
00186
00190 void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00191
00195 static void DecreaseOps(HSQUIRRELVM vm, int amount);
00196
00201 bool IsSuspended();
00202
00206 bool HasScriptCrashed();
00207
00211 void ResetCrashed();
00212 };
00213
00214 #endif