squirrel.hpp

Go to the documentation of this file.
00001 /* $Id: squirrel.hpp 23604 2011-12-19 20:50:54Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef SQUIRREL_HPP
00013 #define SQUIRREL_HPP
00014 
00015 #include <squirrel.h>
00016 
00018 enum ScriptType {
00019   ST_AI, 
00020   ST_GS, 
00021 };
00022 
00023 class Squirrel {
00024 private:
00025   typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
00026 
00027   HSQUIRRELVM vm;          
00028   void *global_pointer;    
00029   SQPrintFunc *print_func; 
00030   bool crashed;            
00031   int overdrawn_ops;       
00032   const char *APIName;     
00033 
00037   static SQInteger _RunError(HSQUIRRELVM vm);
00038 
00042   const char *GetAPIName() { return this->APIName; }
00043 
00044 protected:
00048   static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
00049 
00053   static void RunError(HSQUIRRELVM vm, const SQChar *error);
00054 
00058   static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00059 
00063   static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00064 
00065 public:
00066   Squirrel(const char *APIName);
00067   ~Squirrel();
00068 
00072   HSQUIRRELVM GetVM() { return this->vm; }
00073 
00079   bool LoadScript(const char *script);
00080   bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00081 
00085   SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00086 
00091   void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00092 
00097   void AddConst(const char *var_name, int value);
00098 
00103   void AddConst(const char *var_name, uint value) { this->AddConst(var_name, (int)value); }
00104 
00109   void AddConst(const char *var_name, bool value);
00110 
00115   void AddClassBegin(const char *class_name);
00116 
00121   void AddClassBegin(const char *class_name, const char *parent_class);
00122 
00127   void AddClassEnd();
00128 
00132   bool Resume(int suspend = -1);
00133 
00137   void ResumeError();
00138 
00142   void CollectGarbage();
00143 
00144   void InsertResult(bool result);
00145   void InsertResult(int result);
00146   void InsertResult(uint result) { this->InsertResult((int)result); }
00147 
00152   bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend);
00153   bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, NULL, suspend); }
00154   bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend);
00155   bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
00156   bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
00157 
00161   bool MethodExists(HSQOBJECT instance, const char *method_name);
00162 
00173   static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name = false);
00174 
00178   bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00179 
00185   static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00186 
00192   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; }
00193 
00197   static const char *ObjectToString(HSQOBJECT *ptr) { return SQ2OTTD(sq_objtostring(ptr)); }
00198 
00202   static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00203 
00207   static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
00208 
00213   void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00214 
00218   static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00219 
00223   void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00224 
00228   void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2SQ(error)); }
00229 
00233   void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00234 
00238   static void DecreaseOps(HSQUIRRELVM vm, int amount);
00239 
00244   bool IsSuspended();
00245 
00249   bool HasScriptCrashed();
00250 
00254   void ResetCrashed();
00255 
00259   void CrashOccurred();
00260 
00264   bool CanSuspend();
00265 
00269   SQInteger GetOpsTillSuspend();
00270 };
00271 
00272 #endif /* SQUIRREL_HPP */