Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.8 KiB

  1. #ifndef _SQUIRREL_VM_H_
  2. #define _SQUIRREL_VM_H_
  3. struct SquirrelError {
  4. SquirrelError();
  5. SquirrelError(const SQChar* s):desc(s){}
  6. const SQChar *desc;
  7. };
  8. struct SquirrelVMSys {
  9. HSQUIRRELVM _VM;
  10. SquirrelObject * _root;
  11. };
  12. class SquirrelVM
  13. {
  14. friend class SquirrelObject;
  15. friend struct SquirrelError;
  16. public:
  17. static void Init();
  18. static BOOL IsInitialized(){return _VM == NULL?FALSE:TRUE;}
  19. static void Shutdown();
  20. static void Cleanup();
  21. static BOOL Update(); //debugger and maybe GC later
  22. static SquirrelObject CompileScript(const SQChar *s);
  23. static SquirrelObject CompileBuffer(const SQChar *s,const SQChar * debugInfo=_T("console_buffer"));
  24. static SquirrelObject RunScript(const SquirrelObject &o,SquirrelObject *_this = NULL);
  25. static void PrintFunc(HSQUIRRELVM v,const SQChar* s,...);
  26. static BOOL BeginCall(const SquirrelObject &func);
  27. static BOOL BeginCall(const SquirrelObject &func,SquirrelObject &_this);
  28. static void PushParam(const SquirrelObject &o);
  29. static void PushParam(const SQChar *s);
  30. static void PushParam(SQInteger n);
  31. static void PushParam(SQFloat f);
  32. static void PushParam(SQUserPointer up);
  33. static void PushParamNull();
  34. static SquirrelObject EndCall();
  35. static SquirrelObject CreateString(const SQChar *s);
  36. static SquirrelObject CreateTable();
  37. static SquirrelObject CreateArray(int size);
  38. static SquirrelObject CreateInstance(SquirrelObject &oclass); // oclass is an existing class. Create an 'instance' (OT_INSTANCE) of oclass.
  39. static SquirrelObject CreateFunction(SQFUNCTION func);
  40. static SquirrelObject CreateUserData(int size);
  41. static const SquirrelObject &GetRootTable();
  42. static HSQUIRRELVM GetVMPtr() { return _VM; }
  43. #if 0
  44. static void SetVMPtr(HSQUIRRELVM v) {
  45. _VM = v;
  46. } // setVMPtr
  47. #endif
  48. static void GetVMSys(SquirrelVMSys & vmSys) {
  49. vmSys._VM = _VM;
  50. vmSys._root = _root;
  51. } // GetVMSys
  52. static void SetVMSys(const SquirrelVMSys & vmSys) {
  53. _VM = vmSys._VM;
  54. _root = vmSys._root;
  55. } // SetVMSys
  56. static void PushValue(INT val) {
  57. sq_pushinteger(_VM,val);
  58. } // PushValue
  59. static void PushValue(FLOAT val) {
  60. sq_pushfloat(_VM,val);
  61. } // PushValue
  62. static void PushValue(bool val) { // Compiler treats SQBool as INT.
  63. sq_pushbool(_VM,val);
  64. } // PushValue
  65. static void PushValue(SQChar * val) {
  66. sq_pushstring(_VM,val,-1);
  67. } // PushValue
  68. static void PushValue(SQUserPointer val) {
  69. sq_pushuserpointer(_VM,val);
  70. } // PushValue
  71. static void PushValue(const SQChar * val) {
  72. sq_pushstring(_VM,val,-1);
  73. } // PushValue
  74. static void PushObject(SquirrelObject & so) {
  75. sq_pushobject(_VM,so._o);
  76. } // PushObject
  77. static void Pop(SQInteger nelemstopop) {
  78. sq_pop(_VM,nelemstopop);
  79. } // Pop
  80. static void PushRootTable(void);
  81. // Create/bind a function on the table currently on the stack.
  82. static SquirrelObject CreateFunction(SQFUNCTION func,const SQChar * scriptFuncName,const SQChar * typeMask=0);
  83. // Create/bind a function on the table so. typeMask: standard Squirrel types plus: no typemask means no args, "*" means any type of args.
  84. static SquirrelObject CreateFunction(SquirrelObject & so,SQFUNCTION func,const SQChar * scriptFuncName,const SQChar * typeMask=0);
  85. // Create/bind a function to the root table. typeMask: standard Squirrel types plus: no typemask means no args, "*" means any type of args.
  86. static SquirrelObject CreateFunctionGlobal(SQFUNCTION func,const SQChar * scriptFuncName,const SQChar * typeMask=0);
  87. private:
  88. static HSQUIRRELVM _VM;
  89. static int _CallState;
  90. static SquirrelObject * _root;
  91. };
  92. template<typename T>
  93. inline BOOL SquirrelObject::ArrayAppend(T item) {
  94. sq_pushobject(SquirrelVM::_VM,GetObjectHandle());
  95. SquirrelVM::PushValue(item);
  96. BOOL res = sq_arrayappend(SquirrelVM::_VM,-2) == SQ_OK;
  97. sq_pop(SquirrelVM::_VM,1);
  98. return res;
  99. } // ArrayAppend
  100. #endif //_SQUIRREL_VM_H_