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 
00018 enum ScriptType {
00019   ST_AI, 
00020 };
00021 
00022 class Squirrel {
00023 private:
00024   typedef void (SQPrintFunc)(bool error_msg, const SQChar *message);
00025 
00026   HSQUIRRELVM vm;          
00027   void *global_pointer;    
00028   SQPrintFunc *print_func; 
00029   bool crashed;            
00030   int overdrawn_ops;       
00031   const char *APIName;     
00032 
00036   static SQInteger _RunError(HSQUIRRELVM vm);
00037 
00041   const char *GetAPIName() { return this->APIName; }
00042 
00043 protected:
00047   static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column);
00048 
00052   static void RunError(HSQUIRRELVM vm, const SQChar *error);
00053 
00057   static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00058 
00062   static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
00063 
00064 public:
00065   Squirrel(const char *APIName);
00066   ~Squirrel();
00067 
00071   HSQUIRRELVM GetVM() { return this->vm; }
00072 
00078   bool LoadScript(const char *script);
00079   bool LoadScript(HSQUIRRELVM vm, const char *script, bool in_root = true);
00080 
00084   SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror);
00085 
00090   void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0);
00091 
00096   void AddConst(const char *var_name, int value);
00097 
00102   void AddConst(const char *var_name, uint value) { this->AddConst(var_name, (int)value); }
00103 
00108   void AddConst(const char *var_name, bool value);
00109 
00114   void AddClassBegin(const char *class_name);
00115 
00120   void AddClassBegin(const char *class_name, const char *parent_class);
00121 
00126   void AddClassEnd();
00127 
00131   bool Resume(int suspend = -1);
00132 
00136   void ResumeError();
00137 
00141   void CollectGarbage();
00142 
00143   void InsertResult(bool result);
00144   void InsertResult(int result);
00145   void InsertResult(uint result) { this->InsertResult((int)result); }
00146 
00151   bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend);
00152   bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, NULL, suspend); }
00153   bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend);
00154   bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
00155   bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
00156 
00160   bool MethodExists(HSQOBJECT instance, const char *method_name);
00161 
00172   static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name = false);
00173 
00177   bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance);
00178 
00184   static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); }
00185 
00191   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; }
00192 
00196   static const char *ObjectToString(HSQOBJECT *ptr) { return SQ2OTTD(sq_objtostring(ptr)); }
00197 
00201   static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); }
00202 
00206   static bool ObjectToBool(HSQOBJECT *ptr) { return sq_objtobool(ptr) == 1; }
00207 
00212   void SetGlobalPointer(void *ptr) { this->global_pointer = ptr; }
00213 
00217   static void *GetGlobalPointer(HSQUIRRELVM vm) { return ((Squirrel *)sq_getforeignptr(vm))->global_pointer; }
00218 
00222   void SetPrintFunction(SQPrintFunc *func) { this->print_func = func; }
00223 
00227   void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2SQ(error)); }
00228 
00232   void ReleaseObject(HSQOBJECT *ptr) { sq_release(this->vm, ptr); }
00233 
00237   static void DecreaseOps(HSQUIRRELVM vm, int amount);
00238 
00243   bool IsSuspended();
00244 
00248   bool HasScriptCrashed();
00249 
00253   void ResetCrashed();
00254 
00258   void CrashOccurred();
00259 
00263   bool CanSuspend();
00264 
00268   SQInteger GetOpsTillSuspend();
00269 };
00270 
00271 #endif /* SQUIRREL_HPP */