ToluaUtils.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Ptr.h"
  24. #include "tolua++.h"
  25. #include "ToluaUtils.h"
  26. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str)
  27. {
  28. const char* s = tolua_tostring(L, narg, str);
  29. return s ? s : "";
  30. }
  31. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str)
  32. {
  33. return tolua_tourho3dstring(L, narg, str.CString());
  34. }
  35. // Lua state to context mapping
  36. static HashMap<void*, Context*> contextMapping;
  37. void SetContext(lua_State* L, Context* context)
  38. {
  39. if (context == 0)
  40. contextMapping.Erase(L);
  41. else
  42. contextMapping[L] = context;
  43. }
  44. Context* GetContext(lua_State* L)
  45. {
  46. HashMap<void*, Context*>::ConstIterator i = contextMapping.Find(L);
  47. if (i == contextMapping.End())
  48. return GetContext(lua_getmainthread(L));
  49. return i->second_;
  50. }
  51. template<> int ToluaIsVector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  52. {
  53. if (lua_istable(L, lo))
  54. {
  55. int length = lua_objlen(L, lo);
  56. for (int i = 1; i <= length; ++i)
  57. {
  58. lua_pushinteger(L, i);
  59. lua_gettable(L, lo);
  60. if (!lua_isstring(L, -1))
  61. {
  62. lua_pop(L, 1);
  63. err->index = lo;
  64. err->array = 0;
  65. err->type = type;
  66. return 0;
  67. }
  68. lua_pop(L, 1);
  69. }
  70. return 1;
  71. }
  72. err->index = lo;
  73. err->array = 0;
  74. err->type = type;
  75. return 0;
  76. }
  77. template<> void* ToluaToVector<String>(lua_State* L, int narg, void* def)
  78. {
  79. if (!lua_istable(L, narg))
  80. return 0;
  81. static Vector<String> result;
  82. result.Clear();
  83. int length = lua_objlen(L, narg);
  84. for (int i = 1; i <= length; ++i)
  85. {
  86. lua_pushinteger(L, i);
  87. lua_gettable(L, narg);
  88. if (!lua_isstring(L, -1))
  89. {
  90. lua_pop(L, 1);
  91. return 0;
  92. }
  93. String string = tolua_tourho3dstring(L, -1, "");
  94. result.Push(string);
  95. lua_pop(L, 1);
  96. }
  97. return &result;
  98. }
  99. template<> int ToluaPushVector<String>(lua_State* L, void* data, const char* type)
  100. {
  101. const Vector<String>& vectorstring = *((const Vector<String>*)data);
  102. lua_newtable(L);
  103. for (unsigned i = 0; i < vectorstring.Size(); ++i)
  104. {
  105. tolua_pushurho3dstring(L, vectorstring[i]);
  106. lua_rawseti(L, -2, i + 1);
  107. }
  108. return 1;
  109. }
  110. template<> int ToluaIsPODVector<unsigned>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  111. {
  112. if (lua_istable(L, lo))
  113. {
  114. int length = lua_objlen(L, lo);
  115. for (int i = 1; i <= length; ++i)
  116. {
  117. lua_pushinteger(L, i);
  118. lua_gettable(L, lo);
  119. if (!lua_isnumber(L, -1))
  120. {
  121. lua_pop(L, 1);
  122. err->index = lo;
  123. err->array = 0;
  124. err->type = type;
  125. return 0;
  126. }
  127. lua_pop(L, 1);
  128. }
  129. return 1;
  130. }
  131. err->index = lo;
  132. err->array = 0;
  133. err->type = type;
  134. return 0;
  135. }
  136. template<> void* ToluaToPODVector<unsigned>(lua_State* L, int narg, void* def)
  137. {
  138. if (!lua_istable(L, narg))
  139. return 0;
  140. static Vector<unsigned> result;
  141. result.Clear();
  142. int length = lua_objlen(L, narg);
  143. for (int i = 1; i <= length; ++i)
  144. {
  145. lua_pushinteger(L, i);
  146. lua_gettable(L, narg);
  147. if (!lua_isnumber(L, -1))
  148. {
  149. lua_pop(L, 1);
  150. return 0;
  151. }
  152. unsigned value = (unsigned)tolua_tonumber(L, -1, 0);
  153. result.Push(value);
  154. lua_pop(L, 1);
  155. }
  156. return &result;
  157. }
  158. template<> int ToluaPushPODVector<int>(lua_State* L, void* data, const char*)
  159. {
  160. const PODVector<int>& vector = *((const PODVector<int>*)data);
  161. lua_newtable(L);
  162. for (unsigned i = 0; i < vector.Size(); ++i)
  163. {
  164. lua_pushinteger(L, vector[i]);
  165. lua_rawseti(L, -2, i + 1);
  166. }
  167. return 1;
  168. }
  169. template<> int ToluaPushPODVector<unsigned>(lua_State* L, void* data, const char*)
  170. {
  171. const PODVector<unsigned>& vector = *((const PODVector<unsigned>*)data);
  172. lua_newtable(L);
  173. for (unsigned i = 0; i < vector.Size(); ++i)
  174. {
  175. lua_pushinteger(L, vector[i]);
  176. lua_rawseti(L, -2, i + 1);
  177. }
  178. return 1;
  179. }
  180. template<> int ToluaPushPODVector<SoundSource*>(lua_State* L, void* data, const char*)
  181. {
  182. const PODVector<SoundSource*>& vector = *((const PODVector<SoundSource*>*)data);
  183. lua_newtable(L);
  184. for (unsigned i = 0; i < vector.Size(); ++i)
  185. {
  186. tolua_pushusertype(L, vector[i], "SoundSource");
  187. lua_rawseti(L, -2, i + 1);
  188. }
  189. return 1;
  190. }
  191. template<> int ToluaPushPODVector<UIElement*>(lua_State* L, void* data, const char*)
  192. {
  193. const PODVector<UIElement*>& vector = *((const PODVector<UIElement*>*)data);
  194. lua_newtable(L);
  195. for (unsigned i = 0; i < vector.Size(); ++i)
  196. {
  197. tolua_pushusertype(L, vector[i], "UIElement");
  198. lua_rawseti(L, -2, i + 1);
  199. }
  200. return 1;
  201. }
  202. template<typename T> int tolua_pushurho3dpodvectorusertype(lua_State* L, const PODVector<T>& vector, const char* typeName)
  203. {
  204. lua_newtable(L);
  205. for (unsigned i = 0; i < vector.Size(); ++i)
  206. {
  207. void* tolua_obj = Mtolua_new((T)(vector[i]));
  208. tolua_pushusertype(L, tolua_obj, typeName);
  209. tolua_register_gc(L,lua_gettop(L));
  210. lua_rawseti(L, -2, i + 1);
  211. }
  212. return 1;
  213. }
  214. template<> int ToluaPushPODVector<Vector3>(lua_State* L, void* data, const char*)
  215. {
  216. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<Vector3>*)data), "Vector3");
  217. }
  218. template<> int ToluaPushPODVector<IntVector2>(lua_State* L, void* data, const char*)
  219. {
  220. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<IntVector2>*)data), "IntVector2");
  221. }
  222. template<> int ToluaPushPODVector<OctreeQueryResult>(lua_State* L, void* data, const char*)
  223. {
  224. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<OctreeQueryResult>*)data), "OctreeQueryResult");
  225. }
  226. template<> int ToluaPushPODVector<PhysicsRaycastResult>(lua_State* L, void* data, const char*)
  227. {
  228. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<PhysicsRaycastResult>*)data), "PhysicsRaycastResult");
  229. }
  230. template<> int ToluaPushPODVector<RayQueryResult>(lua_State* L, void* data, const char*)
  231. {
  232. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<RayQueryResult>*)data), "RayQueryResult");
  233. }