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.

254 lines
5.8 KiB

  1. /* see copyright notice in squirrel.h */
  2. #include <new>
  3. #include <squirrel.h>
  4. #include <sqstdio.h>
  5. #include <string.h>
  6. #include <sqstdblob.h>
  7. #include "sqstdstream.h"
  8. #include "sqstdblobimpl.h"
  9. #if defined(VSCRIPT_DLL_EXPORT) || defined(VSQUIRREL_TEST)
  10. #include "memdbgon.h"
  11. #endif
  12. #define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)
  13. //Blob
  14. #define SETUP_BLOB(v) \
  15. SQBlob *self = NULL; \
  16. { if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) \
  17. return SQ_ERROR; }
  18. static SQInteger _blob_resize(HSQUIRRELVM v)
  19. {
  20. SETUP_BLOB(v);
  21. SQInteger size;
  22. sq_getinteger(v,2,&size);
  23. if(!self->Resize(size))
  24. return sq_throwerror(v,_SC("resize failed"));
  25. return 0;
  26. }
  27. static void __swap_dword(unsigned int *n)
  28. {
  29. *n=(unsigned int)(((*n&0xFF000000)>>24) |
  30. ((*n&0x00FF0000)>>8) |
  31. ((*n&0x0000FF00)<<8) |
  32. ((*n&0x000000FF)<<24));
  33. }
  34. static void __swap_word(unsigned short *n)
  35. {
  36. *n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
  37. }
  38. static SQInteger _blob_swap4(HSQUIRRELVM v)
  39. {
  40. SETUP_BLOB(v);
  41. SQInteger num=(self->Len()-(self->Len()%4))>>2;
  42. unsigned int *t=(unsigned int *)self->GetBuf();
  43. for(SQInteger i = 0; i < num; i++) {
  44. __swap_dword(&t[i]);
  45. }
  46. return 0;
  47. }
  48. static SQInteger _blob_swap2(HSQUIRRELVM v)
  49. {
  50. SETUP_BLOB(v);
  51. SQInteger num=(self->Len()-(self->Len()%2))>>1;
  52. unsigned short *t = (unsigned short *)self->GetBuf();
  53. for(SQInteger i = 0; i < num; i++) {
  54. __swap_word(&t[i]);
  55. }
  56. return 0;
  57. }
  58. static SQInteger _blob__set(HSQUIRRELVM v)
  59. {
  60. SETUP_BLOB(v);
  61. SQInteger idx,val;
  62. sq_getinteger(v,2,&idx);
  63. sq_getinteger(v,3,&val);
  64. if(idx < 0 || idx >= self->Len())
  65. return sq_throwerror(v,_SC("index out of range"));
  66. ((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
  67. sq_push(v,3);
  68. return 1;
  69. }
  70. static SQInteger _blob__get(HSQUIRRELVM v)
  71. {
  72. SETUP_BLOB(v);
  73. SQInteger idx;
  74. sq_getinteger(v,2,&idx);
  75. if(idx < 0 || idx >= self->Len())
  76. return sq_throwerror(v,_SC("index out of range"));
  77. sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
  78. return 1;
  79. }
  80. static SQInteger _blob__nexti(HSQUIRRELVM v)
  81. {
  82. SETUP_BLOB(v);
  83. if(sq_gettype(v,2) == OT_NULL) {
  84. sq_pushinteger(v, 0);
  85. return 1;
  86. }
  87. SQInteger idx;
  88. if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
  89. if(idx+1 < self->Len()) {
  90. sq_pushinteger(v, idx+1);
  91. return 1;
  92. }
  93. sq_pushnull(v);
  94. return 1;
  95. }
  96. return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
  97. }
  98. static SQInteger _blob__typeof(HSQUIRRELVM v)
  99. {
  100. sq_pushstring(v,_SC("blob"),-1);
  101. return 1;
  102. }
  103. static SQInteger _blob_releasehook(SQUserPointer p, SQInteger size)
  104. {
  105. SQBlob *self = (SQBlob*)p;
  106. delete self;
  107. return 1;
  108. }
  109. static SQInteger _blob_constructor(HSQUIRRELVM v)
  110. {
  111. SQInteger nparam = sq_gettop(v);
  112. SQInteger size = 0;
  113. if(nparam == 2) {
  114. sq_getinteger(v, 2, &size);
  115. }
  116. if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
  117. SQBlob *b = new SQBlob(size);
  118. if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
  119. delete b;
  120. return sq_throwerror(v, _SC("cannot create blob with negative size"));
  121. }
  122. sq_setreleasehook(v,1,_blob_releasehook);
  123. return 0;
  124. }
  125. #define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
  126. static SQRegFunction _blob_methods[] = {
  127. _DECL_BLOB_FUNC(constructor,-1,_SC("xn")),
  128. _DECL_BLOB_FUNC(resize,2,_SC("xn")),
  129. _DECL_BLOB_FUNC(swap2,1,_SC("x")),
  130. _DECL_BLOB_FUNC(swap4,1,_SC("x")),
  131. _DECL_BLOB_FUNC(_set,3,_SC("xnn")),
  132. _DECL_BLOB_FUNC(_get,2,_SC("xn")),
  133. _DECL_BLOB_FUNC(_typeof,1,_SC("x")),
  134. _DECL_BLOB_FUNC(_nexti,2,_SC("x")),
  135. {0,0,0,0}
  136. };
  137. //GLOBAL FUNCTIONS
  138. static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
  139. {
  140. SQInteger i;
  141. sq_getinteger(v,2,&i);
  142. sq_pushfloat(v,*((SQFloat *)&i));
  143. return 1;
  144. }
  145. static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
  146. {
  147. SQFloat f;
  148. sq_getfloat(v,2,&f);
  149. sq_pushinteger(v,*((SQInteger *)&f));
  150. return 1;
  151. }
  152. static SQInteger _g_blob_swap2(HSQUIRRELVM v)
  153. {
  154. SQInteger i;
  155. sq_getinteger(v,2,&i);
  156. short s=(short)i;
  157. sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
  158. return 1;
  159. }
  160. static SQInteger _g_blob_swap4(HSQUIRRELVM v)
  161. {
  162. SQInteger i;
  163. sq_getinteger(v,2,&i);
  164. unsigned int t4 = (unsigned int)i;
  165. __swap_dword(&t4);
  166. sq_pushinteger(v,(SQInteger)t4);
  167. return 1;
  168. }
  169. static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
  170. {
  171. SQFloat f;
  172. sq_getfloat(v,2,&f);
  173. __swap_dword((unsigned int *)&f);
  174. sq_pushfloat(v,f);
  175. return 1;
  176. }
  177. #define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
  178. static SQRegFunction bloblib_funcs[]={
  179. _DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
  180. _DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
  181. _DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
  182. _DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
  183. _DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
  184. {0,0}
  185. };
  186. SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
  187. {
  188. SQBlob *blob;
  189. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  190. return -1;
  191. *ptr = blob->GetBuf();
  192. return SQ_OK;
  193. }
  194. SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
  195. {
  196. SQBlob *blob;
  197. if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
  198. return -1;
  199. return blob->Len();
  200. }
  201. SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
  202. {
  203. SQInteger top = sq_gettop(v);
  204. sq_pushregistrytable(v);
  205. sq_pushstring(v,_SC("std_blob"),-1);
  206. if(SQ_SUCCEEDED(sq_get(v,-2))) {
  207. sq_remove(v,-2); //removes the registry
  208. sq_push(v,1); // push the this
  209. sq_pushinteger(v,size); //size
  210. SQBlob *blob = NULL;
  211. if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
  212. && SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
  213. sq_remove(v,-2);
  214. return blob->GetBuf();
  215. }
  216. }
  217. sq_settop(v,top);
  218. return NULL;
  219. }
  220. SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
  221. {
  222. return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
  223. }