ToluaUtils.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //
  2. // Copyright (c) 2008-2016 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 "../Graphics/IndexBuffer.h"
  24. #include "../Graphics/VertexBuffer.h"
  25. #include "../IO/VectorBuffer.h"
  26. #include <toluapp/tolua++.h>
  27. #include "../LuaScript/ToluaUtils.h"
  28. const char* tolua_tourho3dstring(lua_State* L, int narg, const char* str)
  29. {
  30. return tolua_tostring(L, narg, str);
  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. void SetContext(lua_State* L, Context* context)
  37. {
  38. assert(L && context);
  39. tolua_pushusertype(L, static_cast<void*>(context), "Context");
  40. lua_setglobal(L, ".context"); // This property is internal, the exposed 'context' property is obtained via GetContext() call
  41. }
  42. Context* GetContext(lua_State* L)
  43. {
  44. lua_getglobal(L, ".context");
  45. if (lua_isnil(L, -1))
  46. {
  47. lua_State* L1 = lua_getmainthread(L);
  48. return (L == L1) ? 0 : GetContext(L1);
  49. }
  50. tolua_Error error; // Ensure we are indeed getting a Context object before casting
  51. return tolua_isusertype(L, -1, "Context", 0, &error) ? static_cast<Context*>(tolua_tousertype(L, -1, 0)) : 0;
  52. }
  53. // Explicit template specialization for StringVector
  54. template <> int ToluaIsVector<String>(lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  55. {
  56. return tolua_isstringarray(L, lo, -1, def, err);
  57. }
  58. template <> void* ToluaToVector<String>(lua_State* L, int narg, void* /*def*/)
  59. {
  60. if (!lua_istable(L, narg))
  61. return 0;
  62. static Vector<String> result;
  63. result.Clear();
  64. result.Resize((unsigned)lua_objlen(L, narg));
  65. for (unsigned i = 0; i < result.Size(); ++i)
  66. {
  67. lua_rawgeti(L, narg, i + 1);
  68. result[i] = tolua_tourho3dstring(L, -1, "");
  69. lua_pop(L, 1);
  70. }
  71. return &result;
  72. }
  73. template <> int ToluaPushVector<String>(lua_State* L, void* data, const char* /*type*/)
  74. {
  75. lua_newtable(L);
  76. const Vector<String>& vector = *static_cast<const Vector<String>*>(data);
  77. for (unsigned i = 0; i < vector.Size(); ++i)
  78. {
  79. tolua_pushurho3dstring(L, vector[i]);
  80. lua_rawseti(L, -2, i + 1);
  81. }
  82. return 1;
  83. }
  84. // Explicit template specialization for boolean
  85. template <> int ToluaIsPODVector<bool>(double /*overload*/, lua_State* L, int lo, const char* /*type*/, int def, tolua_Error* err)
  86. {
  87. return tolua_isbooleanarray(L, lo, -1, def, err);
  88. }
  89. template <> void* ToluaToPODVector<bool>(double /*overload*/, lua_State* L, int narg, void* /*def*/)
  90. {
  91. if (!lua_istable(L, narg))
  92. return 0;
  93. static PODVector<bool> result;
  94. result.Clear();
  95. result.Resize((unsigned)lua_objlen(L, narg));
  96. for (unsigned i = 0; i < result.Size(); ++i)
  97. {
  98. lua_rawgeti(L, narg, i + 1);
  99. result[i] = (bool)tolua_toboolean(L, -1, 0);
  100. lua_pop(L, 1);
  101. }
  102. return &result;
  103. }
  104. template <> int ToluaPushPODVector<bool>(double /*overload*/, lua_State* L, void* data, const char* /*type*/)
  105. {
  106. lua_newtable(L);
  107. const PODVector<bool>& vector = *static_cast<const PODVector<bool>*>(data);
  108. for (unsigned i = 0; i < vector.Size(); ++i)
  109. {
  110. lua_pushboolean(L, vector[i]);
  111. lua_rawseti(L, -2, i + 1);
  112. }
  113. return 1;
  114. }
  115. // Explicit template specialization for Vector<SharedPtr<IndexBuffer> > and Vector<SharedPtr<VertexBuffer> >
  116. template <> void* ToluaToVector<SharedPtr<IndexBuffer> >(lua_State* L, int narg, void* def)
  117. {
  118. if (!lua_istable(L, narg))
  119. return 0;
  120. static Vector<SharedPtr<IndexBuffer> > result;
  121. result.Clear();
  122. result.Resize((unsigned)lua_objlen(L, narg));
  123. for (unsigned i = 0; i < result.Size(); ++i)
  124. {
  125. lua_rawgeti(L, narg, i + 1); // Lua index starts from 1
  126. result[i] = SharedPtr<IndexBuffer>(static_cast<IndexBuffer*>(tolua_tousertype(L, -1, def)));
  127. lua_pop(L, 1);
  128. }
  129. return &result;
  130. }
  131. template <> void* ToluaToVector<SharedPtr<VertexBuffer> >(lua_State* L, int narg, void* def)
  132. {
  133. if (!lua_istable(L, narg))
  134. return 0;
  135. static Vector<SharedPtr<VertexBuffer> > result;
  136. result.Clear();
  137. result.Resize((unsigned)lua_objlen(L, narg));
  138. for (unsigned i = 0; i < result.Size(); ++i)
  139. {
  140. lua_rawgeti(L, narg, i + 1); // Lua index starts from 1
  141. result[i] = SharedPtr<VertexBuffer>(static_cast<VertexBuffer*>(tolua_tousertype(L, -1, def)));
  142. lua_pop(L, 1);
  143. }
  144. return &result;
  145. }
  146. namespace Urho3D
  147. {
  148. template <> const VariantValue* Variant::Get<const VariantValue*>() const
  149. {
  150. return &value_;
  151. }
  152. }
  153. void ToluaToVariant(lua_State* L, int narg, void* def, Variant& variant)
  154. {
  155. switch (lua_type(L, narg)) // Use the type of lua object to determine the final variant type
  156. {
  157. case LUA_TNIL:
  158. variant = Variant::EMPTY;
  159. break;
  160. case LUA_TBOOLEAN:
  161. variant = (bool)lua_toboolean(L, narg); // Still need to cast to bool as Lua/LuaJIT return it as int
  162. break;
  163. case LUA_TNUMBER:
  164. {
  165. // Use the original variant type to further determine the final variant type
  166. // CAVEAT: if lhs has integral data type and double is desired then lhs needs to be reset first before assigning
  167. double value = lua_tonumber(L, narg);
  168. switch (variant.GetType())
  169. {
  170. case VAR_INT:
  171. variant = (int)value;
  172. break;
  173. case VAR_BOOL:
  174. variant = value != 0.0f;
  175. break;
  176. case VAR_FLOAT:
  177. variant = (float)value;
  178. break;
  179. default:
  180. variant = value;
  181. }
  182. }
  183. break;
  184. case LUA_TSTRING:
  185. variant = lua_tostring(L, narg);
  186. break;
  187. case LUA_TUSERDATA:
  188. {
  189. if (lua_getmetatable(L, narg))
  190. {
  191. lua_rawget(L, LUA_REGISTRYINDEX); // registry[mt]
  192. const char* typeName = lua_tostring(L, -1);
  193. lua_pop(L, 1);
  194. void* value = tolua_tousertype(L, narg, def);
  195. switch (Variant::GetTypeFromName(typeName))
  196. {
  197. case VAR_NONE:
  198. // Handle special cases
  199. if (typeName)
  200. {
  201. tolua_Error error;
  202. if (strcmp(typeName, "Variant") == 0)
  203. variant = *static_cast<Variant*>(value);
  204. else if (strcmp(typeName, "VectorBuffer") == 0)
  205. variant = *static_cast<VectorBuffer*>(value);
  206. else if (tolua_isusertype(L, narg, "RefCounted", 0, &error))
  207. variant = static_cast<RefCounted*>(value);
  208. else
  209. variant = value; // void*
  210. }
  211. break;
  212. case VAR_VECTOR2:
  213. variant = *static_cast<Vector2*>(value);
  214. break;
  215. case VAR_VECTOR3:
  216. variant = *static_cast<Vector3*>(value);
  217. break;
  218. case VAR_VECTOR4:
  219. variant = *static_cast<Vector4*>(value);
  220. break;
  221. case VAR_QUATERNION:
  222. variant = *static_cast<Quaternion*>(value);
  223. break;
  224. case VAR_COLOR:
  225. variant = *static_cast<Color*>(value);
  226. break;
  227. case VAR_INTRECT:
  228. variant = *static_cast<IntRect*>(value);
  229. break;
  230. case VAR_INTVECTOR2:
  231. variant = *static_cast<IntVector2*>(value);
  232. break;
  233. case VAR_MATRIX3:
  234. variant = *static_cast<Matrix3*>(value);
  235. break;
  236. case VAR_MATRIX3X4:
  237. variant = *static_cast<Matrix3x4*>(value);
  238. break;
  239. case VAR_MATRIX4:
  240. variant = *static_cast<Matrix4*>(value);
  241. break;
  242. case VAR_RESOURCEREF:
  243. variant = *static_cast<ResourceRef*>(value);
  244. break;
  245. case VAR_RESOURCEREFLIST:
  246. variant = *static_cast<ResourceRefList*>(value);
  247. break;
  248. case VAR_VARIANTMAP:
  249. variant = *static_cast<VariantMap*>(value);
  250. break;
  251. default: break;
  252. }
  253. };
  254. }
  255. break;
  256. case LUA_TTABLE:
  257. {
  258. tolua_Error error;
  259. if (ToluaIsPODVector<unsigned char>(0.f, L, narg, "unsigned char", 0, &error))
  260. variant = *static_cast<PODVector<unsigned char>*>(ToluaToPODVector<unsigned char>(0.f, L, narg, def));
  261. else if (ToluaIsVector<Variant>(L, narg, "Variant", 0, &error))
  262. variant = *static_cast<VariantVector*>(ToluaToVector<Variant>(L, narg, def));
  263. else if (ToluaIsVector<String>(L, narg, "String", 0, &error))
  264. variant = *static_cast<StringVector*>(ToluaToVector<String>(L, narg, def));
  265. }
  266. break;
  267. default: break;
  268. }
  269. }
  270. void ToluaPushVariant(lua_State* L, const Variant* variant, const char* type)
  271. {
  272. String typeName(type);
  273. switch (variant->GetType())
  274. {
  275. case VAR_INT:
  276. if (typeName == "unsigned" || typeName == "unsigned int" || typeName == "UInt" || typeName == "uint")
  277. tolua_pushnumber(L, (lua_Number)variant->GetUInt());
  278. else if (typeName == "StringHash")
  279. {
  280. // Make a new local copy
  281. tolua_pushusertype(L, Mtolua_new(StringHash(variant->GetStringHash())), "StringHash");
  282. tolua_register_gc(L, lua_gettop(L));
  283. }
  284. else
  285. tolua_pushnumber(L, (lua_Number)variant->GetInt());
  286. break;
  287. case VAR_BOOL:
  288. tolua_pushboolean(L, (int)variant->GetBool());
  289. break;
  290. case VAR_FLOAT:
  291. tolua_pushnumber(L, (lua_Number)variant->GetFloat());
  292. break;
  293. case VAR_DOUBLE:
  294. tolua_pushnumber(L, (lua_Number)variant->GetDouble());
  295. break;
  296. case VAR_VECTOR2:
  297. case VAR_VECTOR3:
  298. case VAR_VECTOR4:
  299. case VAR_QUATERNION:
  300. case VAR_COLOR:
  301. case VAR_RESOURCEREF:
  302. case VAR_RESOURCEREFLIST:
  303. case VAR_VARIANTMAP:
  304. case VAR_INTRECT:
  305. case VAR_INTVECTOR2:
  306. tolua_pushusertype(L, (void*)variant->Get<const VariantValue*>(), variant->GetTypeName().CString());
  307. break;
  308. case VAR_STRING:
  309. tolua_pushurho3dstring(L, variant->GetString());
  310. break;
  311. case VAR_BUFFER:
  312. if (typeName == "VectorBuffer")
  313. {
  314. tolua_pushusertype(L, Mtolua_new(VectorBuffer(variant->GetVectorBuffer())), "VectorBuffer");
  315. tolua_register_gc(L, lua_gettop(L));
  316. }
  317. else
  318. ToluaPushPODVector<unsigned char>(0.f, L, (void*)&variant->GetBuffer(), "unsigned char");
  319. break;
  320. case VAR_VOIDPTR:
  321. ToluaPushRegisteredUserType(L, static_cast<void*>(variant->GetVoidPtr()), type);
  322. break;
  323. case VAR_PTR:
  324. ToluaPushRegisteredUserType(L, static_cast<void*>(variant->GetPtr()), type);
  325. break;
  326. case VAR_VARIANTVECTOR:
  327. ToluaPushVector<Variant>(L, (void*)&variant->GetVariantVector(), "Variant");
  328. break;
  329. case VAR_STRINGVECTOR:
  330. ToluaPushVector<String>(L, (void*)&variant->GetStringVector(), "String");
  331. break;
  332. case VAR_MATRIX3:
  333. case VAR_MATRIX3X4:
  334. case VAR_MATRIX4:
  335. tolua_pushusertype(L, variant->Get<const VariantValue*>()->ptr_, variant->GetTypeName().CString());
  336. break;
  337. default:
  338. lua_pushnil(L);
  339. break;
  340. }
  341. }
  342. void ToluaPushRegisteredUserType(lua_State* L, void* data, const char* type)
  343. {
  344. if (type)
  345. {
  346. luaL_getmetatable(L, type);
  347. if (!lua_isnil(L, -1))
  348. {
  349. lua_pop(L, 1);
  350. tolua_pushusertype(L, data, type);
  351. }
  352. }
  353. else
  354. lua_pushnil(L);
  355. }
  356. void ToluaPushObject(lua_State* L, void* data, const char* type)
  357. {
  358. tolua_pushusertype(L, data, data ? static_cast<Object*>(data)->GetTypeName().CString() : type);
  359. }