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.

151 lines
4.7 KiB

  1. #ifndef SQUIRREL_BINDINGS_UTILS_H
  2. #define SQUIRREL_BINDINGS_UTILS_H
  3. struct ScriptClassMemberDecl {
  4. const SQChar *name;
  5. SQFUNCTION func;
  6. int params;
  7. const SQChar *typemask;
  8. };
  9. struct SquirrelClassDecl {
  10. const SQChar *name;
  11. const SQChar *base;
  12. const ScriptClassMemberDecl *members;
  13. };
  14. struct ScriptConstantDecl {
  15. const SQChar *name;
  16. SQObjectType type;
  17. union value {
  18. value(float v){ f = v; }
  19. value(int v){ i = v; }
  20. value(long int v){ li = v; }
  21. value(const SQChar *v){ s = v; }
  22. float f;
  23. int i;
  24. long int li;
  25. const SQChar *s;
  26. } val;
  27. };
  28. struct ScriptNamespaceDecl {
  29. const SQChar *name;
  30. const ScriptClassMemberDecl *members;
  31. const ScriptConstantDecl *constants;
  32. const ScriptClassMemberDecl *delegate;
  33. };
  34. #define _BEGIN_CLASS(classname) \
  35. int __##classname##__typeof(HSQUIRRELVM v) \
  36. { \
  37. sq_pushstring(v,_T(#classname),-1); \
  38. return 1; \
  39. } \
  40. struct ScriptClassMemberDecl __##classname##_members[] = { \
  41. {_T("_typeof"),__##classname##__typeof,1,NULL},
  42. #define _BEGIN_NAMESPACE(xnamespace) struct ScriptClassMemberDecl __##xnamespace##_members[] = {
  43. #define _BEGIN_NAMESPACE_CONSTANTS(xnamespace) {NULL,NULL,0,NULL}}; \
  44. struct ScriptConstantDecl __##xnamespace##_constants[] = {
  45. #define _BEGIN_DELEGATE(xnamespace) struct ScriptClassMemberDecl __##xnamespace##_delegate[] = {
  46. #define _DELEGATE(xnamespace) __##xnamespace##_delegate
  47. #define _END_DELEGATE(classname) {NULL,NULL,NULL,NULL}};
  48. #define _CONSTANT(name,type,val) {_T(#name),type,val},
  49. #define _CONSTANT_IMPL(name,type) {_T(#name),type,name},
  50. #define _MEMBER_FUNCTION(classname,name,nparams,typemask) \
  51. {_T(#name),__##classname##_##name,nparams,typemask},
  52. #define _END_NAMESPACE(classname,delegate) {NULL,OT_NULL,0}}; \
  53. struct ScriptNamespaceDecl __##classname##_decl = { \
  54. _T(#classname), __##classname##_members,__##classname##_constants,delegate };
  55. #define _END_CLASS(classname) {NULL,NULL,0,NULL}}; \
  56. struct SquirrelClassDecl __##classname##_decl = { \
  57. _T(#classname), NULL, __##classname##_members };
  58. #define _END_CLASS_INHERITANCE(classname,base) {NULL,NULL,NULL,NULL}}; \
  59. struct SquirrelClassDecl __##classname##_decl = { \
  60. _T(#classname), _T(#base), __##classname##_members };
  61. #define _MEMBER_FUNCTION_IMPL(classname,name) \
  62. int __##classname##_##name(HSQUIRRELVM v)
  63. #define _INIT_STATIC_NAMESPACE(classname) CreateStaticNamespace(SquirrelVM::GetVMPtr(),&__##classname##_decl);
  64. #define _INIT_CLASS(classname)CreateClass(SquirrelVM::GetVMPtr(),&__##classname##_decl);
  65. #define _DECL_STATIC_NAMESPACE(xnamespace) extern struct ScriptNamespaceDecl __##xnamespace##_decl;
  66. #define _DECL_CLASS(classname) extern struct SquirrelClassDecl __##classname##_decl;
  67. #define _CHECK_SELF(cppclass,scriptclass) \
  68. cppclass *self = NULL; \
  69. if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)&__##scriptclass##_decl))) { \
  70. return sq_throwerror(v,_T("invalid instance type"));\
  71. }
  72. #define _CHECK_INST_PARAM(pname,idx,cppclass,scriptclass) \
  73. cppclass *pname = NULL; \
  74. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer*)&pname,(SQUserPointer)&__##scriptclass##_decl))) { \
  75. return sq_throwerror(v,_T("invalid instance type"));\
  76. } \
  77. #define _CHECK_INST_PARAM_BREAK(pname,idx,cppclass,scriptclass) \
  78. cppclass *pname = NULL; \
  79. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer*)&pname,(SQUserPointer)&__##scriptclass##_decl))) { \
  80. break; \
  81. } \
  82. #define _CLASS_TAG(classname) ((unsigned int)&__##classname##_decl)
  83. #define _DECL_NATIVE_CONSTRUCTION(classname,cppclass) \
  84. BOOL push_##classname(cppclass &quat); \
  85. SquirrelObject new_##classname(cppclass &quat);
  86. #define _IMPL_NATIVE_CONSTRUCTION(classname,cppclass) \
  87. static int classname##_release_hook(SQUserPointer p, int size) \
  88. { \
  89. if(p) { \
  90. cppclass *pv = (cppclass *)p; \
  91. delete pv; \
  92. } \
  93. return 0; \
  94. } \
  95. BOOL push_##classname(cppclass &quat) \
  96. { \
  97. cppclass *newquat = new cppclass; \
  98. *newquat = quat; \
  99. if(!CreateNativeClassInstance(SquirrelVM::GetVMPtr(),_T(#classname),newquat,classname##_release_hook)) { \
  100. delete newquat; \
  101. return FALSE; \
  102. } \
  103. return TRUE; \
  104. } \
  105. SquirrelObject new_##classname(cppclass &quat) \
  106. { \
  107. SquirrelObject ret; \
  108. if(push_##classname(quat)) { \
  109. ret.AttachToStackObject(-1); \
  110. sq_pop(SquirrelVM::GetVMPtr(),1); \
  111. } \
  112. return ret; \
  113. } \
  114. int construct_##classname(cppclass *p) \
  115. { \
  116. sq_setinstanceup(SquirrelVM::GetVMPtr(),1,p); \
  117. sq_setreleasehook(SquirrelVM::GetVMPtr(),1,classname##_release_hook); \
  118. return 1; \
  119. }
  120. BOOL CreateStaticClass(HSQUIRRELVM v,SquirrelClassDecl *cd);
  121. BOOL CreateStaticNamespace(HSQUIRRELVM v,ScriptNamespaceDecl *sn);
  122. BOOL CreateClass(HSQUIRRELVM v,SquirrelClassDecl *cd);
  123. BOOL InitScriptClasses(HSQUIRRELVM v);
  124. BOOL CreateNativeClassInstance(HSQUIRRELVM v,const SQChar *classname,SQUserPointer ud,SQRELEASEHOOK hook);
  125. #endif // SQUIRREL_BINDINGS_UTILS_H