ToluaUtils.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // Copyright (c) 2008-2017 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_INT64:
  176. variant = (long long)value;
  177. break;
  178. case VAR_BOOL:
  179. variant = value != 0.0f;
  180. break;
  181. case VAR_FLOAT:
  182. variant = (float)value;
  183. break;
  184. default:
  185. variant = value;
  186. }
  187. }
  188. break;
  189. case LUA_TSTRING:
  190. variant = lua_tostring(L, narg);
  191. break;
  192. case LUA_TUSERDATA:
  193. {
  194. if (lua_getmetatable(L, narg))
  195. {
  196. lua_rawget(L, LUA_REGISTRYINDEX); // registry[mt]
  197. const char* typeName = lua_tostring(L, -1);
  198. lua_pop(L, 1);
  199. void* value = tolua_tousertype(L, narg, def);
  200. switch (Variant::GetTypeFromName(typeName))
  201. {
  202. case VAR_NONE:
  203. // Handle special cases
  204. if (typeName)
  205. {
  206. tolua_Error error;
  207. if (strcmp(typeName, "Variant") == 0)
  208. variant = *static_cast<Variant*>(value);
  209. else if (strcmp(typeName, "VectorBuffer") == 0)
  210. variant = *static_cast<VectorBuffer*>(value);
  211. else if (tolua_isusertype(L, narg, "RefCounted", 0, &error))
  212. variant = static_cast<RefCounted*>(value);
  213. else
  214. variant = value; // void*
  215. }
  216. break;
  217. case VAR_VECTOR2:
  218. variant = *static_cast<Vector2*>(value);
  219. break;
  220. case VAR_VECTOR3:
  221. variant = *static_cast<Vector3*>(value);
  222. break;
  223. case VAR_VECTOR4:
  224. variant = *static_cast<Vector4*>(value);
  225. break;
  226. case VAR_QUATERNION:
  227. variant = *static_cast<Quaternion*>(value);
  228. break;
  229. case VAR_COLOR:
  230. variant = *static_cast<Color*>(value);
  231. break;
  232. case VAR_INTRECT:
  233. variant = *static_cast<IntRect*>(value);
  234. break;
  235. case VAR_INTVECTOR2:
  236. variant = *static_cast<IntVector2*>(value);
  237. break;
  238. case VAR_INTVECTOR3:
  239. variant = *static_cast<IntVector3*>(value);
  240. break;
  241. case VAR_MATRIX3:
  242. variant = *static_cast<Matrix3*>(value);
  243. break;
  244. case VAR_MATRIX3X4:
  245. variant = *static_cast<Matrix3x4*>(value);
  246. break;
  247. case VAR_MATRIX4:
  248. variant = *static_cast<Matrix4*>(value);
  249. break;
  250. case VAR_RESOURCEREF:
  251. variant = *static_cast<ResourceRef*>(value);
  252. break;
  253. case VAR_RESOURCEREFLIST:
  254. variant = *static_cast<ResourceRefList*>(value);
  255. break;
  256. case VAR_VARIANTMAP:
  257. variant = *static_cast<VariantMap*>(value);
  258. break;
  259. default: break;
  260. }
  261. };
  262. }
  263. break;
  264. case LUA_TTABLE:
  265. {
  266. tolua_Error error;
  267. if (ToluaIsPODVector<unsigned char>(0.f, L, narg, "unsigned char", 0, &error))
  268. variant = *static_cast<PODVector<unsigned char>*>(ToluaToPODVector<unsigned char>(0.f, L, narg, def));
  269. else if (ToluaIsVector<Variant>(L, narg, "Variant", 0, &error))
  270. variant = *static_cast<VariantVector*>(ToluaToVector<Variant>(L, narg, def));
  271. else if (ToluaIsVector<String>(L, narg, "String", 0, &error))
  272. variant = *static_cast<StringVector*>(ToluaToVector<String>(L, narg, def));
  273. }
  274. break;
  275. default: break;
  276. }
  277. }
  278. void ToluaPushVariant(lua_State* L, const Variant* variant, const char* type)
  279. {
  280. String typeName(type);
  281. switch (variant->GetType())
  282. {
  283. case VAR_INT:
  284. if (typeName == "unsigned" || typeName == "unsigned int" || typeName == "UInt" || typeName == "uint")
  285. tolua_pushnumber(L, (lua_Number)variant->GetUInt());
  286. else if (typeName == "StringHash")
  287. {
  288. // Make a new local copy
  289. tolua_pushusertype(L, Mtolua_new(StringHash(variant->GetStringHash())), "StringHash");
  290. tolua_register_gc(L, lua_gettop(L));
  291. }
  292. else
  293. tolua_pushnumber(L, (lua_Number)variant->GetInt());
  294. break;
  295. case VAR_INT64:
  296. if (typeName == "unsigned" || typeName == "unsigned int" || typeName == "UInt" || typeName == "uint")
  297. tolua_pushnumber(L, (lua_Number)variant->GetUInt64());
  298. else
  299. tolua_pushnumber(L, (lua_Number)variant->GetInt64());
  300. break;
  301. case VAR_BOOL:
  302. tolua_pushboolean(L, (int)variant->GetBool());
  303. break;
  304. case VAR_FLOAT:
  305. tolua_pushnumber(L, (lua_Number)variant->GetFloat());
  306. break;
  307. case VAR_DOUBLE:
  308. tolua_pushnumber(L, (lua_Number)variant->GetDouble());
  309. break;
  310. case VAR_VECTOR2:
  311. case VAR_VECTOR3:
  312. case VAR_VECTOR4:
  313. case VAR_QUATERNION:
  314. case VAR_COLOR:
  315. case VAR_RESOURCEREF:
  316. case VAR_RESOURCEREFLIST:
  317. case VAR_VARIANTMAP:
  318. case VAR_INTRECT:
  319. case VAR_INTVECTOR2:
  320. case VAR_INTVECTOR3:
  321. tolua_pushusertype(L, (void*)variant->Get<const VariantValue*>(), variant->GetTypeName().CString());
  322. break;
  323. case VAR_STRING:
  324. tolua_pushurho3dstring(L, variant->GetString());
  325. break;
  326. case VAR_BUFFER:
  327. if (typeName == "VectorBuffer")
  328. {
  329. tolua_pushusertype(L, Mtolua_new(VectorBuffer(variant->GetVectorBuffer())), "VectorBuffer");
  330. tolua_register_gc(L, lua_gettop(L));
  331. }
  332. else
  333. ToluaPushPODVector<unsigned char>(0.f, L, (void*)&variant->GetBuffer(), "unsigned char");
  334. break;
  335. case VAR_VOIDPTR:
  336. ToluaPushRegisteredUserType(L, static_cast<void*>(variant->GetVoidPtr()), type);
  337. break;
  338. case VAR_PTR:
  339. ToluaPushRegisteredUserType(L, static_cast<void*>(variant->GetPtr()), type);
  340. break;
  341. case VAR_VARIANTVECTOR:
  342. ToluaPushVector<Variant>(L, (void*)&variant->GetVariantVector(), "Variant");
  343. break;
  344. case VAR_STRINGVECTOR:
  345. ToluaPushVector<String>(L, (void*)&variant->GetStringVector(), "String");
  346. break;
  347. case VAR_MATRIX3:
  348. case VAR_MATRIX3X4:
  349. case VAR_MATRIX4:
  350. tolua_pushusertype(L, variant->Get<const VariantValue*>()->ptr_, variant->GetTypeName().CString());
  351. break;
  352. default:
  353. lua_pushnil(L);
  354. break;
  355. }
  356. }
  357. void ToluaPushRegisteredUserType(lua_State* L, void* data, const char* type)
  358. {
  359. if (type)
  360. {
  361. luaL_getmetatable(L, type);
  362. if (!lua_isnil(L, -1))
  363. {
  364. lua_pop(L, 1);
  365. tolua_pushusertype(L, data, type);
  366. }
  367. }
  368. else
  369. lua_pushnil(L);
  370. }
  371. void ToluaPushObject(lua_State* L, void* data, const char* type)
  372. {
  373. tolua_pushusertype(L, data, data ? static_cast<Object*>(data)->GetTypeName().CString() : type);
  374. }