truelight@9393: /* $Id$ */ truelight@9393: truelight@9422: /** @file squirrel.hpp defines the Squirrel class */ truelight@9393: truelight@9422: #ifndef SQUIRREL_HPP truelight@9422: #define SQUIRREL_HPP truelight@9393: truelight@9422: class Squirrel { truelight@9393: private: truelight@9393: HSQUIRRELVM vm; ///< The VirtualMachine instnace for squirrel truelight@9393: truelight@9393: /** truelight@9393: * The internal RunError handler. It looks up the real error and calls RunError with it. truelight@9393: */ truelight@9393: static SQInteger _RunError(HSQUIRRELVM vm); truelight@9393: truelight@9393: protected: truelight@9393: /** truelight@9393: * The CompileError handler. truelight@9393: */ truelight@9393: static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column); truelight@9393: truelight@9393: /** truelight@9393: * The RunError handler. truelight@9393: */ truelight@9393: static void RunError(HSQUIRRELVM vm, const char *error); truelight@9393: truelight@9393: /** truelight@9393: * If a user runs 'print' inside a script, this function gets the params. truelight@9393: */ truelight@9393: static void PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...); truelight@9393: truelight@9576: /** truelight@9576: * If an error has to be print, this function is called. truelight@9576: */ truelight@9576: static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...); truelight@9576: truelight@9393: public: truelight@9422: Squirrel(); truelight@9422: ~Squirrel(); truelight@9393: truelight@9393: /** truelight@9393: * Load a script. truelight@9393: * @param script The full script-name to load. truelight@9393: * @return False if loading failed. truelight@9393: */ truelight@9393: bool LoadScript(const char *script); truelight@9393: truelight@9393: /** truelight@9393: * Adds a function to the stack. Depending on the current state this means truelight@9393: * either a method or a global function. truelight@9393: */ truelight@9539: void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam = 0, const char *params = NULL, void *userdata = NULL, int size = 0); truelight@9393: truelight@9393: /** truelight@9525: * Adds a const to the stack. Depending on the current state this means truelight@9525: * either a const to a class or to the global space. truelight@9525: */ truelight@9525: void AddConst(const char *var_name, int value); truelight@9525: truelight@9525: /** truelight@9393: * Adds a class to the global scope. Make sure to call AddClassEnd when you truelight@9393: * are done adding methods. truelight@9393: */ truelight@9393: void AddClassBegin(const char *class_name); truelight@9393: truelight@9393: /** truelight@9393: * Finishes adding a class to the global scope. If this isn't called, no truelight@9393: * class is really created. truelight@9393: */ truelight@9393: void AddClassEnd(); truelight@9393: truelight@9393: /** truelight@9404: * Call a method of an instance, in various flavors. truelight@9393: */ truelight@9404: void CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret); truelight@9404: void CallMethod(HSQOBJECT instance, const char *method_name) { this->CallMethod(instance, method_name, NULL); } truelight@9404: const char *CallStringMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToString(&ret); } truelight@9404: int CallIntegerMethod(HSQOBJECT instance, const char *method_name) { HSQOBJECT ret; this->CallMethod(instance, method_name, &ret); return ObjectToInteger(&ret); } truelight@9404: truelight@9404: /** truelight@9404: * Check if a method exists in an instance. truelight@9404: */ truelight@9404: bool MethodExists(HSQOBJECT instance, const char *method_name); truelight@9393: truelight@9393: /** truelight@9393: * Creates a class instance. truelight@9393: * @param class_name The name of the class of which we create an instance. truelight@9393: * @param real_instance The instance to the real class, if it represents a real class. truelight@9393: * @param instance Returning value with the pointer to the instance. truelight@9393: * @return False if creating failed. truelight@9393: */ truelight@9393: bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance); truelight@9393: truelight@9394: /** truelight@9394: * Get the real-instance pointer. truelight@9394: * @note This will only work just after a function-call from within Squirrel truelight@9394: * to your C++ function. truelight@9394: */ truelight@9404: static bool GetRealInstance(HSQUIRRELVM vm, SQUserPointer *ptr) { return SQ_SUCCEEDED(sq_getinstanceup(vm, 1, ptr, 0)); } truelight@9394: truelight@9404: /** truelight@9404: * Get the Squirrel-instance pointer. truelight@9404: * @note This will only work just after a function-call from within Squirrel truelight@9404: * to your C++ function. truelight@9404: */ truelight@9404: static bool GetInstance(HSQUIRRELVM vm, HSQOBJECT *ptr) { sq_getclass(vm, 1); sq_getstackobj(vm, 1, ptr); return true; } truelight@9404: truelight@9404: /** truelight@9404: * Convert a Squirrel-object to a string. truelight@9404: */ glx@9435: static const char *ObjectToString(HSQOBJECT *ptr) { return FS2OTTD(sq_objtostring(ptr)); } truelight@9404: truelight@9404: /** truelight@9404: * Convert a Squirrel-object to an integer. truelight@9404: */ truelight@9404: static int ObjectToInteger(HSQOBJECT *ptr) { return sq_objtointeger(ptr); } truelight@9404: truelight@9404: /** truelight@9404: * Sets a pointer in the VM that is reasable from where ever you are, what truelight@9404: * ever your state is. Useful to keep track of the main instance. truelight@9404: */ truelight@9404: void SetGlobalPointer(void *ptr) { sq_setforeignptr(this->vm, ptr); } truelight@9404: truelight@9404: /** truelight@9404: * Get the pointer as set by SetGlobalPointer. truelight@9404: */ truelight@9404: static void *GetGlobalPointer(HSQUIRRELVM vm) { return sq_getforeignptr(vm); } truelight@9404: truelight@9404: /** truelight@9404: * Throw a Squirrel error that will be nicely displayed to the user. truelight@9404: */ glx@9435: void ThrowError(const char *error) { sq_throwerror(this->vm, OTTD2FS(error)); } truelight@9393: }; truelight@9393: truelight@9422: #endif /* SQUIRREL_HPP */