ToluaUtils.cpp 14 KB

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