ToluaUtils.cpp 11 KB

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