lua_Bundle.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_Bundle.h"
  4. #include "Base.h"
  5. #include "Bundle.h"
  6. #include "FileSystem.h"
  7. #include "Game.h"
  8. #include "Joint.h"
  9. #include "MeshPart.h"
  10. #include "Ref.h"
  11. #include "Scene.h"
  12. namespace gameplay
  13. {
  14. void luaRegister_Bundle()
  15. {
  16. const luaL_Reg lua_members[] =
  17. {
  18. {"addRef", lua_Bundle_addRef},
  19. {"contains", lua_Bundle_contains},
  20. {"getObjectCount", lua_Bundle_getObjectCount},
  21. {"getObjectId", lua_Bundle_getObjectId},
  22. {"getRefCount", lua_Bundle_getRefCount},
  23. {"loadFont", lua_Bundle_loadFont},
  24. {"loadMesh", lua_Bundle_loadMesh},
  25. {"loadNode", lua_Bundle_loadNode},
  26. {"loadScene", lua_Bundle_loadScene},
  27. {"release", lua_Bundle_release},
  28. {NULL, NULL}
  29. };
  30. const luaL_Reg lua_statics[] =
  31. {
  32. {"create", lua_Bundle_static_create},
  33. {NULL, NULL}
  34. };
  35. std::vector<std::string> scopePath;
  36. ScriptUtil::registerClass("Bundle", lua_members, NULL, lua_Bundle__gc, lua_statics, scopePath);
  37. }
  38. static Bundle* getInstance(lua_State* state)
  39. {
  40. void* userdata = luaL_checkudata(state, 1, "Bundle");
  41. luaL_argcheck(state, userdata != NULL, 1, "'Bundle' expected.");
  42. return (Bundle*)((ScriptUtil::LuaObject*)userdata)->instance;
  43. }
  44. int lua_Bundle__gc(lua_State* state)
  45. {
  46. // Get the number of parameters.
  47. int paramCount = lua_gettop(state);
  48. // Attempt to match the parameters to a valid binding.
  49. switch (paramCount)
  50. {
  51. case 1:
  52. {
  53. if ((lua_type(state, 1) == LUA_TUSERDATA))
  54. {
  55. void* userdata = luaL_checkudata(state, 1, "Bundle");
  56. luaL_argcheck(state, userdata != NULL, 1, "'Bundle' expected.");
  57. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)userdata;
  58. if (object->owns)
  59. {
  60. Bundle* instance = (Bundle*)object->instance;
  61. SAFE_RELEASE(instance);
  62. }
  63. return 0;
  64. }
  65. lua_pushstring(state, "lua_Bundle__gc - Failed to match the given parameters to a valid function signature.");
  66. lua_error(state);
  67. break;
  68. }
  69. default:
  70. {
  71. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  72. lua_error(state);
  73. break;
  74. }
  75. }
  76. return 0;
  77. }
  78. int lua_Bundle_addRef(lua_State* state)
  79. {
  80. // Get the number of parameters.
  81. int paramCount = lua_gettop(state);
  82. // Attempt to match the parameters to a valid binding.
  83. switch (paramCount)
  84. {
  85. case 1:
  86. {
  87. if ((lua_type(state, 1) == LUA_TUSERDATA))
  88. {
  89. Bundle* instance = getInstance(state);
  90. instance->addRef();
  91. return 0;
  92. }
  93. lua_pushstring(state, "lua_Bundle_addRef - Failed to match the given parameters to a valid function signature.");
  94. lua_error(state);
  95. break;
  96. }
  97. default:
  98. {
  99. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  100. lua_error(state);
  101. break;
  102. }
  103. }
  104. return 0;
  105. }
  106. int lua_Bundle_contains(lua_State* state)
  107. {
  108. // Get the number of parameters.
  109. int paramCount = lua_gettop(state);
  110. // Attempt to match the parameters to a valid binding.
  111. switch (paramCount)
  112. {
  113. case 2:
  114. {
  115. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  116. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  117. {
  118. // Get parameter 1 off the stack.
  119. const char* param1 = ScriptUtil::getString(2, false);
  120. Bundle* instance = getInstance(state);
  121. bool result = instance->contains(param1);
  122. // Push the return value onto the stack.
  123. lua_pushboolean(state, result);
  124. return 1;
  125. }
  126. lua_pushstring(state, "lua_Bundle_contains - Failed to match the given parameters to a valid function signature.");
  127. lua_error(state);
  128. break;
  129. }
  130. default:
  131. {
  132. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  133. lua_error(state);
  134. break;
  135. }
  136. }
  137. return 0;
  138. }
  139. int lua_Bundle_getObjectCount(lua_State* state)
  140. {
  141. // Get the number of parameters.
  142. int paramCount = lua_gettop(state);
  143. // Attempt to match the parameters to a valid binding.
  144. switch (paramCount)
  145. {
  146. case 1:
  147. {
  148. if ((lua_type(state, 1) == LUA_TUSERDATA))
  149. {
  150. Bundle* instance = getInstance(state);
  151. unsigned int result = instance->getObjectCount();
  152. // Push the return value onto the stack.
  153. lua_pushunsigned(state, result);
  154. return 1;
  155. }
  156. lua_pushstring(state, "lua_Bundle_getObjectCount - Failed to match the given parameters to a valid function signature.");
  157. lua_error(state);
  158. break;
  159. }
  160. default:
  161. {
  162. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  163. lua_error(state);
  164. break;
  165. }
  166. }
  167. return 0;
  168. }
  169. int lua_Bundle_getObjectId(lua_State* state)
  170. {
  171. // Get the number of parameters.
  172. int paramCount = lua_gettop(state);
  173. // Attempt to match the parameters to a valid binding.
  174. switch (paramCount)
  175. {
  176. case 2:
  177. {
  178. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  179. lua_type(state, 2) == LUA_TNUMBER)
  180. {
  181. // Get parameter 1 off the stack.
  182. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  183. Bundle* instance = getInstance(state);
  184. const char* result = instance->getObjectId(param1);
  185. // Push the return value onto the stack.
  186. lua_pushstring(state, result);
  187. return 1;
  188. }
  189. lua_pushstring(state, "lua_Bundle_getObjectId - Failed to match the given parameters to a valid function signature.");
  190. lua_error(state);
  191. break;
  192. }
  193. default:
  194. {
  195. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  196. lua_error(state);
  197. break;
  198. }
  199. }
  200. return 0;
  201. }
  202. int lua_Bundle_getRefCount(lua_State* state)
  203. {
  204. // Get the number of parameters.
  205. int paramCount = lua_gettop(state);
  206. // Attempt to match the parameters to a valid binding.
  207. switch (paramCount)
  208. {
  209. case 1:
  210. {
  211. if ((lua_type(state, 1) == LUA_TUSERDATA))
  212. {
  213. Bundle* instance = getInstance(state);
  214. unsigned int result = instance->getRefCount();
  215. // Push the return value onto the stack.
  216. lua_pushunsigned(state, result);
  217. return 1;
  218. }
  219. lua_pushstring(state, "lua_Bundle_getRefCount - Failed to match the given parameters to a valid function signature.");
  220. lua_error(state);
  221. break;
  222. }
  223. default:
  224. {
  225. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  226. lua_error(state);
  227. break;
  228. }
  229. }
  230. return 0;
  231. }
  232. int lua_Bundle_loadFont(lua_State* state)
  233. {
  234. // Get the number of parameters.
  235. int paramCount = lua_gettop(state);
  236. // Attempt to match the parameters to a valid binding.
  237. switch (paramCount)
  238. {
  239. case 2:
  240. {
  241. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  242. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  243. {
  244. // Get parameter 1 off the stack.
  245. const char* param1 = ScriptUtil::getString(2, false);
  246. Bundle* instance = getInstance(state);
  247. void* returnPtr = (void*)instance->loadFont(param1);
  248. if (returnPtr)
  249. {
  250. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  251. object->instance = returnPtr;
  252. object->owns = true;
  253. luaL_getmetatable(state, "Font");
  254. lua_setmetatable(state, -2);
  255. }
  256. else
  257. {
  258. lua_pushnil(state);
  259. }
  260. return 1;
  261. }
  262. lua_pushstring(state, "lua_Bundle_loadFont - Failed to match the given parameters to a valid function signature.");
  263. lua_error(state);
  264. break;
  265. }
  266. default:
  267. {
  268. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  269. lua_error(state);
  270. break;
  271. }
  272. }
  273. return 0;
  274. }
  275. int lua_Bundle_loadMesh(lua_State* state)
  276. {
  277. // Get the number of parameters.
  278. int paramCount = lua_gettop(state);
  279. // Attempt to match the parameters to a valid binding.
  280. switch (paramCount)
  281. {
  282. case 2:
  283. {
  284. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  285. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  286. {
  287. // Get parameter 1 off the stack.
  288. const char* param1 = ScriptUtil::getString(2, false);
  289. Bundle* instance = getInstance(state);
  290. void* returnPtr = (void*)instance->loadMesh(param1);
  291. if (returnPtr)
  292. {
  293. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  294. object->instance = returnPtr;
  295. object->owns = true;
  296. luaL_getmetatable(state, "Mesh");
  297. lua_setmetatable(state, -2);
  298. }
  299. else
  300. {
  301. lua_pushnil(state);
  302. }
  303. return 1;
  304. }
  305. lua_pushstring(state, "lua_Bundle_loadMesh - Failed to match the given parameters to a valid function signature.");
  306. lua_error(state);
  307. break;
  308. }
  309. default:
  310. {
  311. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  312. lua_error(state);
  313. break;
  314. }
  315. }
  316. return 0;
  317. }
  318. int lua_Bundle_loadNode(lua_State* state)
  319. {
  320. // Get the number of parameters.
  321. int paramCount = lua_gettop(state);
  322. // Attempt to match the parameters to a valid binding.
  323. switch (paramCount)
  324. {
  325. case 2:
  326. {
  327. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  328. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  329. {
  330. // Get parameter 1 off the stack.
  331. const char* param1 = ScriptUtil::getString(2, false);
  332. Bundle* instance = getInstance(state);
  333. void* returnPtr = (void*)instance->loadNode(param1);
  334. if (returnPtr)
  335. {
  336. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  337. object->instance = returnPtr;
  338. object->owns = true;
  339. luaL_getmetatable(state, "Node");
  340. lua_setmetatable(state, -2);
  341. }
  342. else
  343. {
  344. lua_pushnil(state);
  345. }
  346. return 1;
  347. }
  348. lua_pushstring(state, "lua_Bundle_loadNode - Failed to match the given parameters to a valid function signature.");
  349. lua_error(state);
  350. break;
  351. }
  352. default:
  353. {
  354. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  355. lua_error(state);
  356. break;
  357. }
  358. }
  359. return 0;
  360. }
  361. int lua_Bundle_loadScene(lua_State* state)
  362. {
  363. // Get the number of parameters.
  364. int paramCount = lua_gettop(state);
  365. // Attempt to match the parameters to a valid binding.
  366. switch (paramCount)
  367. {
  368. case 1:
  369. {
  370. if ((lua_type(state, 1) == LUA_TUSERDATA))
  371. {
  372. Bundle* instance = getInstance(state);
  373. void* returnPtr = (void*)instance->loadScene();
  374. if (returnPtr)
  375. {
  376. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  377. object->instance = returnPtr;
  378. object->owns = true;
  379. luaL_getmetatable(state, "Scene");
  380. lua_setmetatable(state, -2);
  381. }
  382. else
  383. {
  384. lua_pushnil(state);
  385. }
  386. return 1;
  387. }
  388. lua_pushstring(state, "lua_Bundle_loadScene - Failed to match the given parameters to a valid function signature.");
  389. lua_error(state);
  390. break;
  391. }
  392. case 2:
  393. {
  394. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  395. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  396. {
  397. // Get parameter 1 off the stack.
  398. const char* param1 = ScriptUtil::getString(2, false);
  399. Bundle* instance = getInstance(state);
  400. void* returnPtr = (void*)instance->loadScene(param1);
  401. if (returnPtr)
  402. {
  403. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  404. object->instance = returnPtr;
  405. object->owns = true;
  406. luaL_getmetatable(state, "Scene");
  407. lua_setmetatable(state, -2);
  408. }
  409. else
  410. {
  411. lua_pushnil(state);
  412. }
  413. return 1;
  414. }
  415. lua_pushstring(state, "lua_Bundle_loadScene - Failed to match the given parameters to a valid function signature.");
  416. lua_error(state);
  417. break;
  418. }
  419. default:
  420. {
  421. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  422. lua_error(state);
  423. break;
  424. }
  425. }
  426. return 0;
  427. }
  428. int lua_Bundle_release(lua_State* state)
  429. {
  430. // Get the number of parameters.
  431. int paramCount = lua_gettop(state);
  432. // Attempt to match the parameters to a valid binding.
  433. switch (paramCount)
  434. {
  435. case 1:
  436. {
  437. if ((lua_type(state, 1) == LUA_TUSERDATA))
  438. {
  439. Bundle* instance = getInstance(state);
  440. instance->release();
  441. return 0;
  442. }
  443. lua_pushstring(state, "lua_Bundle_release - Failed to match the given parameters to a valid function signature.");
  444. lua_error(state);
  445. break;
  446. }
  447. default:
  448. {
  449. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  450. lua_error(state);
  451. break;
  452. }
  453. }
  454. return 0;
  455. }
  456. int lua_Bundle_static_create(lua_State* state)
  457. {
  458. // Get the number of parameters.
  459. int paramCount = lua_gettop(state);
  460. // Attempt to match the parameters to a valid binding.
  461. switch (paramCount)
  462. {
  463. case 1:
  464. {
  465. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  466. {
  467. // Get parameter 1 off the stack.
  468. const char* param1 = ScriptUtil::getString(1, false);
  469. void* returnPtr = (void*)Bundle::create(param1);
  470. if (returnPtr)
  471. {
  472. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  473. object->instance = returnPtr;
  474. object->owns = true;
  475. luaL_getmetatable(state, "Bundle");
  476. lua_setmetatable(state, -2);
  477. }
  478. else
  479. {
  480. lua_pushnil(state);
  481. }
  482. return 1;
  483. }
  484. lua_pushstring(state, "lua_Bundle_static_create - Failed to match the given parameters to a valid function signature.");
  485. lua_error(state);
  486. break;
  487. }
  488. default:
  489. {
  490. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  491. lua_error(state);
  492. break;
  493. }
  494. }
  495. return 0;
  496. }
  497. }