LuaSystem.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "LuaSystem.h"
  24. #include "lua.hpp"
  25. #include "Config.h"
  26. #include "LuaStack.h"
  27. #include "Assert.h"
  28. #include "Vector2.h"
  29. #include "Vector3.h"
  30. #include "Matrix4x4.h"
  31. #include "Quaternion.h"
  32. #include "Device.h"
  33. #include "ResourceManager.h"
  34. #include "LuaResource.h"
  35. #include "LuaEnvironment.h"
  36. #include "LuaStack.h"
  37. namespace crown
  38. {
  39. extern int vector2_add(lua_State* L);
  40. extern int vector2_subtract(lua_State* L);
  41. extern int vector2_multiply(lua_State* L);
  42. extern int vector2_divide(lua_State* L);
  43. extern int vector2_negate(lua_State* L);
  44. extern int vector3_add(lua_State* L);
  45. extern int vector3_subtract(lua_State* L);
  46. extern int vector3_multiply(lua_State* L);
  47. extern int vector3_divide(lua_State* L);
  48. extern int vector3_negate(lua_State* L);
  49. extern int vector2_new(lua_State* L);
  50. extern int vector3_new(lua_State* L);
  51. extern int matrix4x4(lua_State* L);
  52. extern int quaternion_new(lua_State* L);
  53. extern int matrix4x4box(lua_State* L);
  54. extern int quaternionbox(lua_State* L);
  55. extern int vector3box(lua_State* L);
  56. extern int vector3box_get_value(lua_State* L);
  57. extern int vector3box_set_value(lua_State* L);
  58. //-----------------------------------------------------------------------------
  59. static int crown_lua_vector2_call(lua_State* L)
  60. {
  61. lua_remove(L, 1);
  62. return vector2_new(L);
  63. }
  64. //-----------------------------------------------------------------------------
  65. static int crown_lua_vector3_call(lua_State* L)
  66. {
  67. lua_remove(L, 1);
  68. return vector3_new(L);
  69. }
  70. //-----------------------------------------------------------------------------
  71. static int crown_lua_matrix4x4_call(lua_State* L)
  72. {
  73. lua_remove(L, 1);
  74. return matrix4x4(L);
  75. }
  76. //-----------------------------------------------------------------------------
  77. static int crown_lua_quaternion_call(lua_State* L)
  78. {
  79. lua_remove(L, 1);
  80. return quaternion_new(L);
  81. }
  82. //-----------------------------------------------------------------------------
  83. static int crown_lua_lightuserdata_add(lua_State* L)
  84. {
  85. LuaStack stack(L);
  86. if (lua_system::is_vector3(1))
  87. {
  88. return vector3_add(L);
  89. }
  90. return vector2_add(L);
  91. }
  92. //-----------------------------------------------------------------------------
  93. static int crown_lua_lightuserdata_sub(lua_State* L)
  94. {
  95. LuaStack stack(L);
  96. if (lua_system::is_vector3(1))
  97. {
  98. return vector3_subtract(L);
  99. }
  100. return vector2_subtract(L);
  101. }
  102. //-----------------------------------------------------------------------------
  103. static int crown_lua_lightuserdata_mul(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. if (lua_system::is_vector3(1))
  107. {
  108. return vector3_multiply(L);
  109. }
  110. return vector2_multiply(L);
  111. }
  112. //-----------------------------------------------------------------------------
  113. static int crown_lua_lightuserdata_div(lua_State* L)
  114. {
  115. LuaStack stack(L);
  116. if (lua_system::is_vector3(1))
  117. {
  118. return vector3_divide(L);
  119. }
  120. return vector2_divide(L);
  121. }
  122. //-----------------------------------------------------------------------------
  123. static int crown_lua_lightuserdata_unm(lua_State* L)
  124. {
  125. LuaStack stack(L);
  126. if (lua_system::is_vector3(1))
  127. {
  128. return vector3_negate(L);
  129. }
  130. return vector2_negate(L);
  131. }
  132. //-----------------------------------------------------------------------------
  133. static int crown_lua_vector3box_call(lua_State* L)
  134. {
  135. lua_remove(L, 1);
  136. return vector3box(L);
  137. }
  138. //-----------------------------------------------------------------------------
  139. static int crown_lua_quaternionbox_call(lua_State* L)
  140. {
  141. lua_remove(L, 1);
  142. return quaternionbox(L);
  143. }
  144. //-----------------------------------------------------------------------------
  145. static int crown_lua_matrix4x4box_call(lua_State* L)
  146. {
  147. lua_remove(L, 1);
  148. return matrix4x4box(L);
  149. }
  150. //-----------------------------------------------------------------------------
  151. static int crown_lua_require(lua_State* L)
  152. {
  153. LuaStack stack(L);
  154. const char* filename = stack.get_string(1);
  155. const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
  156. device()->resource_manager()->flush();
  157. const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
  158. luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
  159. device()->resource_manager()->unload(lua_res);
  160. return 1;
  161. }
  162. namespace lua_system
  163. {
  164. static lua_State* s_L;
  165. static uint32_t s_vec2_used = 0;
  166. static Vector2 s_vec2_buffer[CE_MAX_LUA_VECTOR2];
  167. static uint32_t s_vec3_used = 0;
  168. static Vector3 s_vec3_buffer[CE_MAX_LUA_VECTOR3];
  169. static uint32_t s_mat4_used = 0;
  170. static Matrix4x4 s_mat4_buffer[CE_MAX_LUA_MATRIX4X4];
  171. static uint32_t s_quat_used = 0;
  172. static Quaternion s_quat_buffer[CE_MAX_LUA_QUATERNION];
  173. //-----------------------------------------------------------------------------
  174. void init()
  175. {
  176. s_L = luaL_newstate();
  177. CE_ASSERT(s_L, "Unable to create lua state");
  178. // Open default libraries
  179. luaL_openlibs(s_L);
  180. // Register crown libraries
  181. LuaEnvironment env(s_L);
  182. load_accelerometer(env);
  183. load_actor(env);
  184. load_camera(env);
  185. load_controller(env);
  186. load_debug_line(env);
  187. load_device(env);
  188. load_float_setting(env);
  189. load_gui(env);
  190. load_int_setting(env);
  191. load_keyboard(env);
  192. load_math(env);
  193. load_matrix4x4(env);
  194. load_mesh(env);
  195. load_mouse(env);
  196. load_physics_world(env);
  197. load_quaternion(env);
  198. load_raycast(env);
  199. load_resource_package(env);
  200. load_sound_world(env);
  201. load_sprite(env);
  202. load_string_setting(env);
  203. load_touch(env);
  204. load_unit(env);
  205. load_vector2(env);
  206. load_vector3(env);
  207. load_vector3box(env);
  208. load_window(env);
  209. load_world(env);
  210. // Register custom loader
  211. lua_getfield(s_L, LUA_GLOBALSINDEX, "package");
  212. lua_getfield(s_L, -1, "loaders");
  213. lua_remove(s_L, -2);
  214. int num_loaders = 0;
  215. lua_pushnil(s_L);
  216. while (lua_next(s_L, -2) != 0)
  217. {
  218. lua_pop(s_L, 1);
  219. num_loaders++;
  220. }
  221. lua_pushinteger(s_L, num_loaders + 1);
  222. lua_pushcfunction(s_L, crown_lua_require);
  223. lua_rawset(s_L, -3);
  224. lua_pop(s_L, 1);
  225. LuaStack stack(s_L);
  226. // Create metatable for lightuserdata
  227. lua_pushlightuserdata(s_L, (void*)0x0); // Just dummy userdata
  228. stack.create_metatable("Lightuserdata_mt");
  229. stack.set_metatable();
  230. lua_pop(s_L, -1); // Pop dummy userdata
  231. stack.get_global_metatable("Lightuserdata_mt");
  232. stack.set_metatable_function("__add", crown_lua_lightuserdata_add);
  233. stack.set_metatable_function("__sub", crown_lua_lightuserdata_sub);
  234. stack.set_metatable_function("__mul", crown_lua_lightuserdata_mul);
  235. stack.set_metatable_function("__div", crown_lua_lightuserdata_div);
  236. stack.set_metatable_function("__unm", crown_lua_lightuserdata_unm);
  237. // Vector2 metatable
  238. stack.get_global("Vector2");
  239. stack.create_metatable("Vector2_mt");
  240. stack.set_self_index();
  241. stack.set_metatable_function("__call", crown_lua_vector2_call);
  242. stack.set_metatable();
  243. // Vector3 metatable
  244. stack.get_global("Vector3");
  245. stack.create_metatable("Vector3_mt");
  246. stack.set_self_index();
  247. stack.set_metatable_function("__call", crown_lua_vector3_call);
  248. stack.set_metatable();
  249. // Matrix4x4 metatable
  250. stack.get_global("Matrix4x4");
  251. stack.create_metatable("Matrix4x4_mt");
  252. stack.set_self_index();
  253. stack.set_metatable_function("__call", crown_lua_matrix4x4_call);
  254. stack.set_metatable();
  255. // Quaternion metatable
  256. stack.get_global("Quaternion");
  257. stack.create_metatable("Quaternion_mt");
  258. stack.set_self_index();
  259. stack.set_metatable_function("__call", crown_lua_quaternion_call);
  260. stack.set_metatable();
  261. // Create and attach metatable to global table Vector3Box
  262. stack.get_global("Vector3Box");
  263. stack.create_metatable("Vector3Box_g_mt");
  264. stack.set_self_index();
  265. stack.set_metatable_function("__call", crown_lua_vector3box_call);
  266. stack.set_metatable();
  267. // Create metatable for userdata Vector3Box (instance)
  268. // Used for type checking and also provide access facilities to data
  269. // e.g:
  270. // inst = Vector3Box(0, 0, 0)
  271. // print (inst.x) <= cannot do it without the following code
  272. stack.create_metatable("Vector3Box_i_mt");
  273. stack.set_metatable_function("__index", vector3box_get_value);
  274. stack.set_metatable_function("__newindex", vector3box_set_value);
  275. // Create and attach metatable to global table Quaternion
  276. stack.get_global("QuaternionBox");
  277. stack.create_metatable("QuaternionBox_g_mt");
  278. stack.set_self_index();
  279. stack.set_metatable_function("__call", crown_lua_quaternionbox_call);
  280. stack.set_metatable();
  281. // Create metatable for userdata QuaternionBox (instance)
  282. // Used for type checking
  283. stack.create_metatable("QuaternionBox_i_mt");
  284. // Create and attach metatable to global table Matrix4x4
  285. stack.get_global("Matrix4x4");
  286. stack.create_metatable("Matrix4x4_g_mt");
  287. stack.set_self_index();
  288. stack.set_metatable_function("__call", crown_lua_matrix4x4box_call);
  289. stack.set_metatable();
  290. // Create metatable for userdata QuaternionBox (instance)
  291. // Used for type checking
  292. stack.create_metatable("Matrix4x4_i_mt");
  293. Log::d("Lua stack size = %d", lua_gettop(s_L));
  294. }
  295. //-----------------------------------------------------------------------------
  296. void shutdown()
  297. {
  298. lua_close(s_L);
  299. }
  300. //-----------------------------------------------------------------------------
  301. lua_State* state()
  302. {
  303. return s_L;
  304. }
  305. //-----------------------------------------------------------------------------
  306. Vector2* next_vector2(const Vector2& v)
  307. {
  308. CE_ASSERT(s_vec2_used < CE_MAX_LUA_VECTOR2, "Maximum number of Vector2 reached");
  309. return &(s_vec2_buffer[s_vec2_used++] = v);
  310. }
  311. //-----------------------------------------------------------------------------
  312. Vector3* next_vector3(const Vector3& v)
  313. {
  314. CE_ASSERT(s_vec3_used < CE_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  315. return &(s_vec3_buffer[s_vec3_used++] = v);
  316. }
  317. //-----------------------------------------------------------------------------
  318. Matrix4x4* next_matrix4x4(const Matrix4x4& m)
  319. {
  320. CE_ASSERT(s_mat4_used < CE_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  321. return &(s_mat4_buffer[s_mat4_used++] = m);
  322. }
  323. //-----------------------------------------------------------------------------
  324. Quaternion* next_quaternion(const Quaternion& q)
  325. {
  326. CE_ASSERT(s_quat_used < CE_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  327. return &(s_quat_buffer[s_quat_used++] = q);
  328. }
  329. //-----------------------------------------------------------------------------
  330. bool is_vector2(int32_t index)
  331. {
  332. void* type = lua_touserdata(s_L, index);
  333. return (type >= &s_vec2_buffer[0] && type <= &s_vec2_buffer[CE_MAX_LUA_VECTOR2 - 1]);
  334. }
  335. //-----------------------------------------------------------------------------
  336. bool is_vector3(int32_t index)
  337. {
  338. void* type = lua_touserdata(s_L, index);
  339. return (type >= &s_vec3_buffer[0] && type <= &s_vec3_buffer[CE_MAX_LUA_VECTOR3 - 1]);
  340. }
  341. //-----------------------------------------------------------------------------
  342. bool is_matrix4x4(int32_t index)
  343. {
  344. void* type = lua_touserdata(s_L, index);
  345. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CE_MAX_LUA_MATRIX4X4 - 1]);
  346. }
  347. //-----------------------------------------------------------------------------
  348. bool is_quaternion(int32_t index)
  349. {
  350. void* type = lua_touserdata(s_L, index);
  351. return (type >= &s_quat_buffer[0] && type <= &s_quat_buffer[CE_MAX_LUA_QUATERNION - 1]);
  352. }
  353. //-----------------------------------------------------------------------------
  354. void clear_temporaries()
  355. {
  356. s_vec2_used = 0;
  357. s_vec3_used = 0;
  358. s_mat4_used = 0;
  359. s_quat_used = 0;
  360. }
  361. } // namespace lua_system
  362. } // namespace crown