LuaStack.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. #pragma once
  24. #include "lua.hpp"
  25. #include "Types.h"
  26. #include "LuaSystem.h"
  27. #include "Vector3.h"
  28. #include "Vector2.h"
  29. #include "Quaternion.h"
  30. #include "Matrix4x4.h"
  31. #include "StringUtils.h"
  32. namespace crown
  33. {
  34. class PhysicsWorld;
  35. class SoundWorld;
  36. class World;
  37. struct Actor;
  38. struct Camera;
  39. struct Controller;
  40. struct Gui;
  41. struct Mesh;
  42. struct ResourcePackage;
  43. struct Sprite;
  44. struct Unit;
  45. struct DebugLine;
  46. struct Raycast;
  47. typedef Id SoundInstanceId;
  48. typedef Id GuiId;
  49. typedef Id GuiComponentId;
  50. typedef int (*MetamethodFunction)(lua_State*);
  51. void clear_lua_temporaries();
  52. class LuaStack
  53. {
  54. public:
  55. //-----------------------------------------------------------------------------
  56. LuaStack(lua_State* L)
  57. : m_state(L)
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. lua_State* state()
  62. {
  63. return m_state;
  64. }
  65. /// Returns the number of elements in the stack.
  66. /// When called inside a function, it can be used to count
  67. /// the number of arguments passed to the function itself.
  68. int32_t num_args()
  69. {
  70. return lua_gettop(m_state);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void push_nil()
  74. {
  75. lua_pushnil(m_state);
  76. }
  77. //-----------------------------------------------------------------------------
  78. void push_bool(bool value)
  79. {
  80. lua_pushboolean(m_state, value);
  81. }
  82. //-----------------------------------------------------------------------------
  83. void push_int32(int32_t value)
  84. {
  85. lua_pushinteger(m_state, value);
  86. }
  87. //-----------------------------------------------------------------------------
  88. void push_uint32(uint32_t value)
  89. {
  90. lua_pushinteger(m_state, value);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void push_int64(int64_t value)
  94. {
  95. lua_pushinteger(m_state, value);
  96. }
  97. //-----------------------------------------------------------------------------
  98. void push_uint64(uint64_t value)
  99. {
  100. lua_pushinteger(m_state, value);
  101. }
  102. //-----------------------------------------------------------------------------
  103. void push_float(float value)
  104. {
  105. lua_pushnumber(m_state, value);
  106. }
  107. //-----------------------------------------------------------------------------
  108. void push_string(const char* s)
  109. {
  110. lua_pushstring(m_state, s);
  111. }
  112. //-----------------------------------------------------------------------------
  113. void push_literal(const char* s, size_t len)
  114. {
  115. lua_pushlstring(m_state, s, len);
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool get_bool(int32_t index)
  119. {
  120. return (bool) lua_toboolean(m_state, index);
  121. }
  122. //-----------------------------------------------------------------------------
  123. int32_t get_int(int32_t index)
  124. {
  125. return luaL_checkinteger(m_state, index);
  126. }
  127. //-----------------------------------------------------------------------------
  128. float get_float(int32_t index)
  129. {
  130. return luaL_checknumber(m_state, index);
  131. }
  132. //-----------------------------------------------------------------------------
  133. const char* get_string(int32_t index)
  134. {
  135. return luaL_checkstring(m_state, index);
  136. }
  137. /// Pushes an empty table onto the stack.
  138. /// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
  139. /// and LuaStack::push_key_end() as in the following example:
  140. ///
  141. /// LuaStack stack(L)
  142. /// stack.push_table()
  143. /// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
  144. /// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
  145. /// return 1;
  146. void push_table()
  147. {
  148. lua_newtable(m_state);
  149. }
  150. /// See Stack::push_table()
  151. void push_key_begin(const char* key)
  152. {
  153. lua_pushstring(m_state, key);
  154. }
  155. /// See Stack::push_table()
  156. void push_key_begin(int32_t i)
  157. {
  158. lua_pushnumber(m_state, i);
  159. }
  160. /// See Stack::push_table()
  161. void push_key_end()
  162. {
  163. lua_settable(m_state, -3);
  164. }
  165. /// Pushes an empty metatable onto the stack.
  166. /// When you want to associate a metamethod to a function, you have to use
  167. /// LuaStack::set_metamethod_function(const char* key, MetamethodFunction func).
  168. /// Call LuaStack::end_metatable() to associate this metatable to userdata of any kind.
  169. ///
  170. /// stack.push_userdata(); // Could be Vector3Box or a table
  171. /// stack.begin_metatable("Foo_mt");
  172. /// stack.set_metamethod_function("__index", function_ptr);
  173. /// stack.set_metamethod_function("__newindex", another_function_ptr);
  174. /// stack.end_metatable();
  175. void create_metatable(const char* name)
  176. {
  177. luaL_newmetatable(m_state, name);
  178. }
  179. /// See LuaStack::begin_metatable()
  180. void set_metatable_function(const char* key, MetamethodFunction func)
  181. {
  182. lua_pushcfunction(m_state, func);
  183. lua_setfield(m_state, -2, key);
  184. }
  185. /// Sets table on top of stack to metafield @a key of another table.
  186. /// Only '__index' and '__newindex' metamethods are available.
  187. void set_metatable_table(const char* key)
  188. {
  189. CE_ASSERT(string::strcmp(key, "__index") == 0 || string::strcmp(key, "__newindex") == 0, "Illegal metamethod");
  190. lua_setfield(m_state, -2, key);
  191. }
  192. /// Sets table's __index metamethod to its self
  193. void set_self_index()
  194. {
  195. lua_pushvalue(m_state, -1);
  196. lua_setfield(m_state, -2, "__index");
  197. }
  198. /// See LuaStack::begin_metatable()
  199. void set_metatable()
  200. {
  201. lua_setmetatable(m_state, -2);
  202. }
  203. ///
  204. void get_global(const char* name)
  205. {
  206. lua_getglobal(m_state, name);
  207. }
  208. ///
  209. void get_global_metatable(const char* name)
  210. {
  211. luaL_getmetatable(m_state, name);
  212. }
  213. //-----------------------------------------------------------------------------
  214. void push_resource_package(ResourcePackage* package)
  215. {
  216. lua_pushlightuserdata(m_state, package);
  217. }
  218. //-----------------------------------------------------------------------------
  219. ResourcePackage* get_resource_package(int32_t index)
  220. {
  221. return (ResourcePackage*) lua_touserdata(m_state, index);
  222. }
  223. //-----------------------------------------------------------------------------
  224. void push_world(World* world)
  225. {
  226. lua_pushlightuserdata(m_state, world);
  227. }
  228. //-----------------------------------------------------------------------------
  229. World* get_world(int32_t index)
  230. {
  231. return (World*) lua_touserdata(m_state, index);
  232. }
  233. //-----------------------------------------------------------------------------
  234. void push_sound_world(SoundWorld* sw)
  235. {
  236. lua_pushlightuserdata(m_state, sw);
  237. }
  238. //-----------------------------------------------------------------------------
  239. SoundWorld* get_sound_world(int32_t index)
  240. {
  241. return (SoundWorld*) lua_touserdata(m_state, index);
  242. }
  243. //-----------------------------------------------------------------------------
  244. void push_unit(Unit* unit)
  245. {
  246. lua_pushlightuserdata(m_state, unit);
  247. }
  248. //-----------------------------------------------------------------------------
  249. Unit* get_unit(int32_t index)
  250. {
  251. return (Unit*) lua_touserdata(m_state, index);
  252. }
  253. //-----------------------------------------------------------------------------
  254. void push_camera(Camera* camera)
  255. {
  256. lua_pushlightuserdata(m_state, camera);
  257. }
  258. //-----------------------------------------------------------------------------
  259. Camera* get_camera(int32_t index)
  260. {
  261. return (Camera*) lua_touserdata(m_state, index);
  262. }
  263. //-----------------------------------------------------------------------------
  264. void push_mesh(Mesh* mesh)
  265. {
  266. lua_pushlightuserdata(m_state, mesh);
  267. }
  268. //-----------------------------------------------------------------------------
  269. Mesh* get_mesh(int32_t index)
  270. {
  271. return (Mesh*) lua_touserdata(m_state, index);
  272. }
  273. //-----------------------------------------------------------------------------
  274. void push_sprite(Sprite* sprite)
  275. {
  276. lua_pushlightuserdata(m_state, sprite);
  277. }
  278. //-----------------------------------------------------------------------------
  279. Sprite* get_sprite(int32_t index)
  280. {
  281. return (Sprite*) lua_touserdata(m_state, index);
  282. }
  283. //-----------------------------------------------------------------------------
  284. PhysicsWorld* get_physics_world(int32_t index)
  285. {
  286. return (PhysicsWorld*) lua_touserdata(m_state, index);
  287. }
  288. //-----------------------------------------------------------------------------
  289. void push_physics_world(PhysicsWorld* world)
  290. {
  291. lua_pushlightuserdata(m_state, world);
  292. }
  293. //-----------------------------------------------------------------------------
  294. void push_actor(Actor* actor)
  295. {
  296. lua_pushlightuserdata(m_state, actor);
  297. }
  298. //-----------------------------------------------------------------------------
  299. Actor* get_actor(int32_t index)
  300. {
  301. return (Actor*) lua_touserdata(m_state, index);
  302. }
  303. //-----------------------------------------------------------------------------
  304. void push_controller(Controller* controller)
  305. {
  306. lua_pushlightuserdata(m_state, controller);
  307. }
  308. //-----------------------------------------------------------------------------
  309. Controller* get_controller(int32_t index)
  310. {
  311. return (Controller*) lua_touserdata(m_state, index);
  312. }
  313. //-----------------------------------------------------------------------------
  314. void push_raycast(Raycast* raycast)
  315. {
  316. lua_pushlightuserdata(m_state, raycast);
  317. }
  318. //-----------------------------------------------------------------------------
  319. Raycast* get_raycast(int32_t index)
  320. {
  321. return (Raycast*) lua_touserdata(m_state, index);
  322. }
  323. //-----------------------------------------------------------------------------
  324. void push_sound_instance_id(const SoundInstanceId id)
  325. {
  326. uintptr_t enc = id.encode();
  327. lua_pushlightuserdata(m_state, (void*)enc);
  328. }
  329. //-----------------------------------------------------------------------------
  330. SoundInstanceId get_sound_instance_id(int32_t index)
  331. {
  332. uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
  333. SoundInstanceId id;
  334. id.decode(enc);
  335. return id;
  336. }
  337. //-----------------------------------------------------------------------------
  338. void push_gui(Gui* gui)
  339. {
  340. lua_pushlightuserdata(m_state, gui);
  341. }
  342. //-----------------------------------------------------------------------------
  343. Gui* get_gui(int32_t index)
  344. {
  345. return (Gui*) lua_touserdata(m_state, index);
  346. }
  347. //-----------------------------------------------------------------------------
  348. void push_gui_component_id(GuiComponentId id)
  349. {
  350. uintptr_t enc = id.encode();
  351. lua_pushlightuserdata(m_state, (void*)enc);
  352. }
  353. //-----------------------------------------------------------------------------
  354. GuiComponentId get_gui_component_id(int32_t index)
  355. {
  356. uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
  357. GuiComponentId id;
  358. id.decode(enc);
  359. return id;
  360. }
  361. //-----------------------------------------------------------------------------
  362. void push_debug_line(DebugLine* line)
  363. {
  364. lua_pushlightuserdata(m_state, line);
  365. }
  366. //-----------------------------------------------------------------------------
  367. DebugLine* get_debug_line(int32_t index)
  368. {
  369. return (DebugLine*) lua_touserdata(m_state, index);
  370. }
  371. //-----------------------------------------------------------------------------
  372. Vector2& get_vector2(int32_t index)
  373. {
  374. void* v = lua_touserdata(m_state, index);
  375. if (!lua_system::is_vector2(index))
  376. {
  377. luaL_typerror(m_state, index, "Vector2");
  378. }
  379. return *(Vector2*)v;
  380. }
  381. //-----------------------------------------------------------------------------
  382. Vector3& get_vector3(int32_t index)
  383. {
  384. void* v = lua_touserdata(m_state, index);
  385. if (!lua_system::is_vector3(index))
  386. {
  387. luaL_typerror(m_state, index, "Vector3");
  388. }
  389. return *(Vector3*)v;
  390. }
  391. //-----------------------------------------------------------------------------
  392. Matrix4x4& get_matrix4x4(int32_t index)
  393. {
  394. void* m = lua_touserdata(m_state, index);
  395. if (!lua_system::is_matrix4x4(index))
  396. {
  397. luaL_typerror(m_state, index, "Matrix4x4");
  398. }
  399. return *(Matrix4x4*)m;
  400. }
  401. //-----------------------------------------------------------------------------
  402. Quaternion& get_quaternion(int32_t index)
  403. {
  404. void* q = lua_touserdata(m_state, index);
  405. if (!lua_system::is_quaternion(index))
  406. {
  407. luaL_typerror(m_state, index, "Quaternion");
  408. }
  409. return *(Quaternion*)q;
  410. }
  411. //-----------------------------------------------------------------------------
  412. void push_vector2(const Vector2& v)
  413. {
  414. lua_pushlightuserdata(m_state, lua_system::next_vector2(v));
  415. }
  416. //-----------------------------------------------------------------------------
  417. void push_vector3(const Vector3& v)
  418. {
  419. lua_pushlightuserdata(m_state, lua_system::next_vector3(v));
  420. }
  421. //-----------------------------------------------------------------------------
  422. void push_matrix4x4(const Matrix4x4& m)
  423. {
  424. lua_pushlightuserdata(m_state, lua_system::next_matrix4x4(m));
  425. }
  426. //-----------------------------------------------------------------------------
  427. void push_quaternion(const Quaternion& q)
  428. {
  429. lua_pushlightuserdata(m_state, lua_system::next_quaternion(q));
  430. }
  431. /// Allocates a new Vector3Box (full userdata)
  432. Vector3& push_vector3box()
  433. {
  434. return *(Vector3*) lua_newuserdata(m_state, sizeof(Vector3));
  435. }
  436. /// Retrieves Vector3Box (full userdata)
  437. Vector3& get_vector3box(uint32_t index)
  438. {
  439. CE_ASSERT(lua_isuserdata(m_state, index), "Unable to retrieve 'Vector3Box' (wrong index)");
  440. Vector3* ud = (Vector3*) luaL_checkudata(m_state, index, "Vector3Box_i_mt");
  441. return *ud;
  442. }
  443. /// Allocates a new QuaternionBox (full userdata)
  444. Quaternion& push_quaternionbox()
  445. {
  446. return *(Quaternion*) lua_newuserdata(m_state, sizeof(Quaternion));
  447. }
  448. /// Retrieves QuaternionBox (full userdata)
  449. Quaternion& get_quaternionbox(uint32_t index)
  450. {
  451. CE_ASSERT(lua_isuserdata(m_state, index), "Unable to retrieve QuaternionBox (wrong index)");
  452. Quaternion* ud = (Quaternion*) luaL_checkudata(m_state, index, "QuaternionBox_i_mt");
  453. return *ud;
  454. }
  455. /// Allocates a new Matrix4x4Box (full userdata)
  456. Matrix4x4& push_matrix4x4box()
  457. {
  458. return *(Matrix4x4*) lua_newuserdata(m_state, sizeof(Matrix4x4));
  459. }
  460. /// Retrieves QuaternionBox (full userdata)
  461. Matrix4x4& get_matrix4x4box(uint32_t index)
  462. {
  463. CE_ASSERT(lua_isuserdata(m_state, index), "Unable to retrieve Matrix4x4Box (wrong index)");
  464. Matrix4x4* ud = (Matrix4x4*) luaL_checkudata(m_state, index, "Matrix4x4Box_i_mt");
  465. return *ud;
  466. }
  467. private:
  468. lua_State* m_state;
  469. };
  470. } // namespace crown