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 
00029   static SQInteger _RunError(HSQUIRRELVM vm);
00030 
00034   HSQUIRRELVM GetVM() { return this->vm; }
00035 
00036 protected:
00040   static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
00041 
00045   static void RunError(HSQUIRRELVM vm, const SQChar *error);
00046 
00050   static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00051 
00055   static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00056 
00057 public:
00058   friend class AIScanner;
00059   friend class AIInstance;
00060   friend void squirrel_register_std(Squirrel *engine);
00061 
00062   Squirrel();
00063   ~Squirrel();
00064 
00070   bool LoadScript(const char *script);
00071   static bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00072 
00076   static SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00077 
00082   void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00083 
00088   void AddConst(const char *var_name, int value);
00089 
00094   void AddConst(const char *var_name, uint value) { this->AddConst(var_name, (int)value); }
00095 
00100   void AddConst(const char *var_name, bool value);
00101 
00106   void AddClassBegin(const char *class_name);
00107 
00112   void AddClassBegin(const char *class_name, const char *parent_class);
00113 
00118   void AddClassEnd();
00119 
00123   bool Resume(int suspend = -1);
00124 
00128   void ResumeError();
00129 
00133   void CollectGarbage();
00134 
00135   void InsertResult(bool result);
00136   void InsertResult(int result);
00137   void InsertResult(uint result) { this->InsertResult((int)result); }
00138 
00143   bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend);
00144   bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, NULL, suspend); }
00145   bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend);
00146   bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
00147   bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
00148 
00152   bool MethodExists(HSQOBJECT instance, const char *method_name);
00153 
00163   static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook);
00164 
00168   bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00169 
00175   static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00176 
00182   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; }
00183 
00187   static const char *ObjectToString(HSQOBJECT *ptr) { return SQ2OTTD(sq_objtostring(ptr)); }
00188 
00192   static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00193 
00197   static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
00198 
00203   void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00204 
00208   static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00209 
00213   void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00214 
00218   void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2SQ(error)); }
00219 
00223   void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00224 
00228   static void DecreaseOps(HSQUIRRELVM vm, int amount);
00229 
00234   bool IsSuspended();
00235 
00239   bool HasScriptCrashed();
00240 
00244   void ResetCrashed();
00245 
00249   void CrashOccurred();
00250 
00254   bool CanSuspend();
00255 };
00256 
00257 #endif /* SQUIRREL_HPP */

Generated on Sun Jan 23 01:49:08 2011 for OpenTTD by  doxygen 1.6.1