ToluaUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "Object.h"
  24. #include "Ptr.h"
  25. #include "tolua++.h"
  26. #include "ToluaUtils.h"
  27. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str)
  28. {
  29. const char* s = tolua_tostring(L, narg, str);
  30. return s ? s : "";
  31. }
  32. const char* tolua_tourho3dstring(lua_State* L, int narg, const String& str)
  33. {
  34. return tolua_tourho3dstring(L, narg, str.CString());
  35. }
  36. // Lua state to context mapping
  37. static HashMap<void*, Context*> contextMapping;
  38. void SetContext(lua_State* L, Context* context)
  39. {
  40. if (context == 0)
  41. contextMapping.Erase(L);
  42. else
  43. contextMapping[L] = context;
  44. }
  45. Context* GetContext(lua_State* L)
  46. {
  47. HashMap<void*, Context*>::ConstIterator i = contextMapping.Find(L);
  48. if (i == contextMapping.End())
  49. {
  50. lua_State* L1 = lua_getmainthread(L);
  51. return (L == L1) ? 0 : GetContext(L1);
  52. }
  53. return i->second_;
  54. }
  55. template<> int ToluaIsVector<String>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  56. {
  57. if (lua_istable(L, lo))
  58. {
  59. int length = lua_objlen(L, lo);
  60. for (int i = 1; i <= length; ++i)
  61. {
  62. lua_pushinteger(L, i);
  63. lua_gettable(L, lo);
  64. if (!lua_isstring(L, -1))
  65. {
  66. lua_pop(L, 1);
  67. err->index = lo;
  68. err->array = 0;
  69. err->type = type;
  70. return 0;
  71. }
  72. lua_pop(L, 1);
  73. }
  74. return 1;
  75. }
  76. err->index = lo;
  77. err->array = 0;
  78. err->type = type;
  79. return 0;
  80. }
  81. template<> void* ToluaToVector<String>(lua_State* L, int narg, void* def)
  82. {
  83. if (!lua_istable(L, narg))
  84. return 0;
  85. static Vector<String> result;
  86. result.Clear();
  87. int length = lua_objlen(L, narg);
  88. for (int i = 1; i <= length; ++i)
  89. {
  90. lua_pushinteger(L, i);
  91. lua_gettable(L, narg);
  92. if (!lua_isstring(L, -1))
  93. {
  94. lua_pop(L, 1);
  95. return 0;
  96. }
  97. String string = tolua_tourho3dstring(L, -1, "");
  98. result.Push(string);
  99. lua_pop(L, 1);
  100. }
  101. return &result;
  102. }
  103. template<> int ToluaPushVector<String>(lua_State* L, void* data, const char* type)
  104. {
  105. const Vector<String>& vectorstring = *((const Vector<String>*)data);
  106. lua_newtable(L);
  107. for (unsigned i = 0; i < vectorstring.Size(); ++i)
  108. {
  109. tolua_pushurho3dstring(L, vectorstring[i]);
  110. lua_rawseti(L, -2, i + 1);
  111. }
  112. return 1;
  113. }
  114. template<> int ToluaPushVector<StringHash>(lua_State* L, void* data, const char* type)
  115. {
  116. Vector<StringHash>& vector = *((Vector<StringHash>*)data);
  117. lua_newtable(L);
  118. for (unsigned i = 0; i < vector.Size(); ++i)
  119. {
  120. tolua_pushusertype(L, &vector[i], "StringHash");
  121. lua_rawseti(L, -2, i + 1);
  122. }
  123. return 1;
  124. }
  125. template<> int ToluaIsPODVector<unsigned>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  126. {
  127. if (lua_istable(L, lo))
  128. {
  129. int length = lua_objlen(L, lo);
  130. for (int i = 1; i <= length; ++i)
  131. {
  132. lua_pushinteger(L, i);
  133. lua_gettable(L, lo);
  134. if (!lua_isnumber(L, -1))
  135. {
  136. lua_pop(L, 1);
  137. err->index = lo;
  138. err->array = 0;
  139. err->type = type;
  140. return 0;
  141. }
  142. lua_pop(L, 1);
  143. }
  144. return 1;
  145. }
  146. err->index = lo;
  147. err->array = 0;
  148. err->type = type;
  149. return 0;
  150. }
  151. template<> int ToluaIsPODVector<Vector2>(lua_State* L, int lo, const char* type, int def, tolua_Error* err)
  152. {
  153. if (lua_istable(L, lo))
  154. {
  155. int length = lua_objlen(L, lo);
  156. for (int i = 1; i <= length; ++i)
  157. {
  158. lua_pushinteger(L, i);
  159. lua_gettable(L, lo);
  160. if (!tolua_isusertype(L, -1, "Vector2", 0, err))
  161. {
  162. lua_pop(L, 1);
  163. return 0;
  164. }
  165. lua_pop(L, 1);
  166. }
  167. return 1;
  168. }
  169. err->index = lo;
  170. err->array = 0;
  171. err->type = type;
  172. return 0;
  173. }
  174. template<> void* ToluaToPODVector<unsigned>(lua_State* L, int narg, void* def)
  175. {
  176. if (!lua_istable(L, narg))
  177. return 0;
  178. static PODVector<unsigned> result;
  179. result.Clear();
  180. int length = lua_objlen(L, narg);
  181. for (int i = 1; i <= length; ++i)
  182. {
  183. lua_pushinteger(L, i);
  184. lua_gettable(L, narg);
  185. if (!lua_isnumber(L, -1))
  186. {
  187. lua_pop(L, 1);
  188. return 0;
  189. }
  190. unsigned value = (unsigned)tolua_tonumber(L, -1, 0);
  191. result.Push(value);
  192. lua_pop(L, 1);
  193. }
  194. return &result;
  195. }
  196. template<> void* ToluaToPODVector<Vector2>(lua_State* L, int narg, void* def)
  197. {
  198. if (!lua_istable(L, narg))
  199. return 0;
  200. static PODVector<Vector2> result;
  201. result.Clear();
  202. tolua_Error tolua_err;
  203. int length = lua_objlen(L, narg);
  204. for (int i = 1; i <= length; ++i)
  205. {
  206. lua_pushinteger(L, i);
  207. lua_gettable(L, narg);
  208. if (!tolua_isusertype(L, -1, "Vector2", 0, &tolua_err))
  209. {
  210. lua_pop(L, 1);
  211. return 0;
  212. }
  213. Vector2* value = (Vector2*)tolua_touserdata(L, -1, 0);
  214. result.Push(*value);
  215. lua_pop(L, 1);
  216. }
  217. return &result;
  218. }
  219. template<> int ToluaPushPODVector<int>(lua_State* L, void* data, const char*)
  220. {
  221. const PODVector<int>& vector = *((const PODVector<int>*)data);
  222. lua_newtable(L);
  223. for (unsigned i = 0; i < vector.Size(); ++i)
  224. {
  225. lua_pushinteger(L, vector[i]);
  226. lua_rawseti(L, -2, i + 1);
  227. }
  228. return 1;
  229. }
  230. template<> int ToluaPushPODVector<unsigned>(lua_State* L, void* data, const char*)
  231. {
  232. const PODVector<unsigned>& vector = *((const PODVector<unsigned>*)data);
  233. lua_newtable(L);
  234. for (unsigned i = 0; i < vector.Size(); ++i)
  235. {
  236. lua_pushinteger(L, vector[i]);
  237. lua_rawseti(L, -2, i + 1);
  238. }
  239. return 1;
  240. }
  241. template<> int ToluaPushPODVector<SoundSource*>(lua_State* L, void* data, const char*)
  242. {
  243. const PODVector<SoundSource*>& vector = *((const PODVector<SoundSource*>*)data);
  244. lua_newtable(L);
  245. for (unsigned i = 0; i < vector.Size(); ++i)
  246. {
  247. tolua_pushusertype(L, vector[i], "SoundSource");
  248. lua_rawseti(L, -2, i + 1);
  249. }
  250. return 1;
  251. }
  252. template<> int ToluaPushPODVector<UIElement*>(lua_State* L, void* data, const char*)
  253. {
  254. const PODVector<UIElement*>& vector = *((const PODVector<UIElement*>*)data);
  255. lua_newtable(L);
  256. for (unsigned i = 0; i < vector.Size(); ++i)
  257. {
  258. tolua_pushusertype(L, vector[i], "UIElement");
  259. lua_rawseti(L, -2, i + 1);
  260. }
  261. return 1;
  262. }
  263. #ifdef URHO3D_PHYSICS
  264. template<> int ToluaPushPODVector<RigidBody*>(lua_State* L, void* data, const char*)
  265. {
  266. const PODVector<RigidBody*>& vector = *((const PODVector<RigidBody*>*)data);
  267. lua_newtable(L);
  268. for (unsigned i = 0; i < vector.Size(); ++i)
  269. {
  270. tolua_pushusertype(L, vector[i], "RigidBody");
  271. lua_rawseti(L, -2, i + 1);
  272. }
  273. return 1;
  274. }
  275. #endif
  276. #ifdef URHO3D_URHO2D
  277. template<> int ToluaPushPODVector<RigidBody2D*>(lua_State* L, void* data, const char*)
  278. {
  279. const PODVector<RigidBody2D*>& vector = *((const PODVector<RigidBody2D*>*)data);
  280. lua_newtable(L);
  281. for (unsigned i = 0; i < vector.Size(); ++i)
  282. {
  283. tolua_pushusertype(L, vector[i], "RigidBody2D");
  284. lua_rawseti(L, -2, i + 1);
  285. }
  286. return 1;
  287. }
  288. #endif
  289. template<typename T> int tolua_pushurho3dpodvectorusertype(lua_State* L, const PODVector<T>& vector, const char* typeName)
  290. {
  291. lua_newtable(L);
  292. for (unsigned i = 0; i < vector.Size(); ++i)
  293. {
  294. void* tolua_obj = Mtolua_new((T)(vector[i]));
  295. tolua_pushusertype(L, tolua_obj, typeName);
  296. tolua_register_gc(L,lua_gettop(L));
  297. lua_rawseti(L, -2, i + 1);
  298. }
  299. return 1;
  300. }
  301. template<> int ToluaPushPODVector<Vector3>(lua_State* L, void* data, const char*)
  302. {
  303. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<Vector3>*)data), "Vector3");
  304. }
  305. template<> int ToluaPushPODVector<IntVector2>(lua_State* L, void* data, const char*)
  306. {
  307. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<IntVector2>*)data), "IntVector2");
  308. }
  309. template<> int ToluaPushPODVector<OctreeQueryResult>(lua_State* L, void* data, const char*)
  310. {
  311. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<OctreeQueryResult>*)data), "OctreeQueryResult");
  312. }
  313. #ifdef URHO3D_PHYSICS
  314. template<> int ToluaPushPODVector<PhysicsRaycastResult>(lua_State* L, void* data, const char*)
  315. {
  316. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<PhysicsRaycastResult>*)data), "PhysicsRaycastResult");
  317. }
  318. #endif
  319. #ifdef URHO3D_URHO2D
  320. template<> int ToluaPushPODVector<PhysicsRaycastResult2D>(lua_State* L, void* data, const char*)
  321. {
  322. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<PhysicsRaycastResult2D>*)data), "PhysicsRaycastResult2D");
  323. }
  324. #endif
  325. template<> int ToluaPushPODVector<RayQueryResult>(lua_State* L, void* data, const char*)
  326. {
  327. return tolua_pushurho3dpodvectorusertype(L, *((const PODVector<RayQueryResult>*)data), "RayQueryResult");
  328. }
  329. template<> int ToluaPushPODVector<Pass*>(lua_State* L, void* data, const char*)
  330. {
  331. const PODVector<Pass*>& vector = *((const PODVector<Pass*>*)data);
  332. lua_newtable(L);
  333. for (unsigned i = 0; i < vector.Size(); ++i)
  334. {
  335. tolua_pushusertype(L, vector[i], "Pass");
  336. lua_rawseti(L, -2, i + 1);
  337. }
  338. return 1;
  339. }
  340. void ToluaPushObject(lua_State*L, void* data, const char* type)
  341. {
  342. tolua_pushusertype(L, data, data ? static_cast<Object*>(data)->GetTypeName().CString() : type);
  343. }