squirrel.hpp

Go to the documentation of this file.
00001 /* $Id$ */
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 
00017 class Squirrel {
00018 private:
00019   typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
00020 
00021   HSQUIRRELVM vm;          
00022   void *global_pointer;    
00023   SQPrintFunc *print_func; 
00024   bool crashed;            
00025   int overdrawn_ops;       
00026 
00030   static SQInteger _RunError(HSQUIRRELVM vm);
00031 
00035   HSQUIRRELVM GetVM() { return this->vm; }
00036 
00037 protected:
00041   static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
00042 
00046   static void RunError(HSQUIRRELVM vm, const SQChar *error);
00047 
00051   static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00052 
00056   static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00057 
00058 public:
00059   friend class AIScanner;
00060   friend class AIInstance;
00061   friend void squirrel_register_std(Squirrel *engine);
00062 
00063   Squirrel();
00064   ~Squirrel();
00065 
00071   bool LoadScript(const char *script);
00072   static bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00073 
00077   static SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00078 
00083   void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00084 
00089   void AddConst(const char *var_name, int value);
00090 
00095   void AddConst(const char *var_name, uint value) { this->AddConst(var_name, (int)value); }
00096 
00101   void AddConst(const char *var_name, bool value);
00102 
00107   void AddClassBegin(const char *class_name);
00108 
00113   void AddClassBegin(const char *class_name, const char *parent_class);
00114 
00119   void AddClassEnd();
00120 
00124   bool Resume(int suspend = -1);
00125 
00129   void ResumeError();
00130 
00134   void CollectGarbage();
00135 
00136   void InsertResult(bool result);
00137   void InsertResult(int result);
00138   void InsertResult(uint result) { this->InsertResult((int)result); }
00139 
00144   bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend);
00145   bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, NULL, suspend); }
00146   bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend);
00147   bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
00148   bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
00149 
00153   bool MethodExists(HSQOBJECT instance, const char *method_name);
00154 
00164   static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
00165 
00169   bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00170 
00176   static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00177 
00183   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; }
00184 
00188   static const char *ObjectToString(HSQOBJECT *ptr) { return SQ2OTTD(sq_objtostring(ptr)); }
00189 
00193   static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00194 
00198   static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
00199 
00204   void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00205 
00209   static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00210 
00214   void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00215 
00219   void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2SQ(error)); }
00220 
00224   void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00225 
00229   static void DecreaseOps(HSQUIRRELVM vm, int amount);
00230 
00235   bool IsSuspended();
00236 
00240   bool HasScriptCrashed();
00241 
00245   void ResetCrashed();
00246 
00250   void CrashOccurred();
00251 
00255   bool CanSuspend();
00256 };
00257 
00258 #endif /* SQUIRREL_HPP */

Generated on Mon May 9 05:18:59 2011 for OpenTTD by  doxygen 1.6.1