lua_Bundle.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. else
  66. {
  67. lua_pushstring(state, "lua_Bundle__gc - Failed to match the given parameters to a valid function signature.");
  68. lua_error(state);
  69. }
  70. break;
  71. }
  72. default:
  73. {
  74. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  75. lua_error(state);
  76. break;
  77. }
  78. }
  79. return 0;
  80. }
  81. int lua_Bundle_addRef(lua_State* state)
  82. {
  83. // Get the number of parameters.
  84. int paramCount = lua_gettop(state);
  85. // Attempt to match the parameters to a valid binding.
  86. switch (paramCount)
  87. {
  88. case 1:
  89. {
  90. if ((lua_type(state, 1) == LUA_TUSERDATA))
  91. {
  92. Bundle* instance = getInstance(state);
  93. instance->addRef();
  94. return 0;
  95. }
  96. else
  97. {
  98. lua_pushstring(state, "lua_Bundle_addRef - Failed to match the given parameters to a valid function signature.");
  99. lua_error(state);
  100. }
  101. break;
  102. }
  103. default:
  104. {
  105. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  106. lua_error(state);
  107. break;
  108. }
  109. }
  110. return 0;
  111. }
  112. int lua_Bundle_contains(lua_State* state)
  113. {
  114. // Get the number of parameters.
  115. int paramCount = lua_gettop(state);
  116. // Attempt to match the parameters to a valid binding.
  117. switch (paramCount)
  118. {
  119. case 2:
  120. {
  121. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  122. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  123. {
  124. // Get parameter 1 off the stack.
  125. const char* param1 = ScriptUtil::getString(2, false);
  126. Bundle* instance = getInstance(state);
  127. bool result = instance->contains(param1);
  128. // Push the return value onto the stack.
  129. lua_pushboolean(state, result);
  130. return 1;
  131. }
  132. else
  133. {
  134. lua_pushstring(state, "lua_Bundle_contains - Failed to match the given parameters to a valid function signature.");
  135. lua_error(state);
  136. }
  137. break;
  138. }
  139. default:
  140. {
  141. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  142. lua_error(state);
  143. break;
  144. }
  145. }
  146. return 0;
  147. }
  148. int lua_Bundle_getObjectCount(lua_State* state)
  149. {
  150. // Get the number of parameters.
  151. int paramCount = lua_gettop(state);
  152. // Attempt to match the parameters to a valid binding.
  153. switch (paramCount)
  154. {
  155. case 1:
  156. {
  157. if ((lua_type(state, 1) == LUA_TUSERDATA))
  158. {
  159. Bundle* instance = getInstance(state);
  160. unsigned int result = instance->getObjectCount();
  161. // Push the return value onto the stack.
  162. lua_pushunsigned(state, result);
  163. return 1;
  164. }
  165. else
  166. {
  167. lua_pushstring(state, "lua_Bundle_getObjectCount - Failed to match the given parameters to a valid function signature.");
  168. lua_error(state);
  169. }
  170. break;
  171. }
  172. default:
  173. {
  174. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  175. lua_error(state);
  176. break;
  177. }
  178. }
  179. return 0;
  180. }
  181. int lua_Bundle_getObjectID(lua_State* state)
  182. {
  183. // Get the number of parameters.
  184. int paramCount = lua_gettop(state);
  185. // Attempt to match the parameters to a valid binding.
  186. switch (paramCount)
  187. {
  188. case 2:
  189. {
  190. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  191. lua_type(state, 2) == LUA_TNUMBER)
  192. {
  193. // Get parameter 1 off the stack.
  194. unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
  195. Bundle* instance = getInstance(state);
  196. const char* result = instance->getObjectID(param1);
  197. // Push the return value onto the stack.
  198. lua_pushstring(state, result);
  199. return 1;
  200. }
  201. else
  202. {
  203. lua_pushstring(state, "lua_Bundle_getObjectID - Failed to match the given parameters to a valid function signature.");
  204. lua_error(state);
  205. }
  206. break;
  207. }
  208. default:
  209. {
  210. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  211. lua_error(state);
  212. break;
  213. }
  214. }
  215. return 0;
  216. }
  217. int lua_Bundle_getRefCount(lua_State* state)
  218. {
  219. // Get the number of parameters.
  220. int paramCount = lua_gettop(state);
  221. // Attempt to match the parameters to a valid binding.
  222. switch (paramCount)
  223. {
  224. case 1:
  225. {
  226. if ((lua_type(state, 1) == LUA_TUSERDATA))
  227. {
  228. Bundle* instance = getInstance(state);
  229. unsigned int result = instance->getRefCount();
  230. // Push the return value onto the stack.
  231. lua_pushunsigned(state, result);
  232. return 1;
  233. }
  234. else
  235. {
  236. lua_pushstring(state, "lua_Bundle_getRefCount - Failed to match the given parameters to a valid function signature.");
  237. lua_error(state);
  238. }
  239. break;
  240. }
  241. default:
  242. {
  243. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  244. lua_error(state);
  245. break;
  246. }
  247. }
  248. return 0;
  249. }
  250. int lua_Bundle_loadFont(lua_State* state)
  251. {
  252. // Get the number of parameters.
  253. int paramCount = lua_gettop(state);
  254. // Attempt to match the parameters to a valid binding.
  255. switch (paramCount)
  256. {
  257. case 2:
  258. {
  259. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  260. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  261. {
  262. // Get parameter 1 off the stack.
  263. const char* param1 = ScriptUtil::getString(2, false);
  264. Bundle* instance = getInstance(state);
  265. void* returnPtr = (void*)instance->loadFont(param1);
  266. if (returnPtr)
  267. {
  268. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  269. object->instance = returnPtr;
  270. object->owns = true;
  271. luaL_getmetatable(state, "Font");
  272. lua_setmetatable(state, -2);
  273. }
  274. else
  275. {
  276. lua_pushnil(state);
  277. }
  278. return 1;
  279. }
  280. else
  281. {
  282. lua_pushstring(state, "lua_Bundle_loadFont - Failed to match the given parameters to a valid function signature.");
  283. lua_error(state);
  284. }
  285. break;
  286. }
  287. default:
  288. {
  289. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  290. lua_error(state);
  291. break;
  292. }
  293. }
  294. return 0;
  295. }
  296. int lua_Bundle_loadMesh(lua_State* state)
  297. {
  298. // Get the number of parameters.
  299. int paramCount = lua_gettop(state);
  300. // Attempt to match the parameters to a valid binding.
  301. switch (paramCount)
  302. {
  303. case 2:
  304. {
  305. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  306. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  307. {
  308. // Get parameter 1 off the stack.
  309. const char* param1 = ScriptUtil::getString(2, false);
  310. Bundle* instance = getInstance(state);
  311. void* returnPtr = (void*)instance->loadMesh(param1);
  312. if (returnPtr)
  313. {
  314. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  315. object->instance = returnPtr;
  316. object->owns = true;
  317. luaL_getmetatable(state, "Mesh");
  318. lua_setmetatable(state, -2);
  319. }
  320. else
  321. {
  322. lua_pushnil(state);
  323. }
  324. return 1;
  325. }
  326. else
  327. {
  328. lua_pushstring(state, "lua_Bundle_loadMesh - Failed to match the given parameters to a valid function signature.");
  329. lua_error(state);
  330. }
  331. break;
  332. }
  333. default:
  334. {
  335. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  336. lua_error(state);
  337. break;
  338. }
  339. }
  340. return 0;
  341. }
  342. int lua_Bundle_loadNode(lua_State* state)
  343. {
  344. // Get the number of parameters.
  345. int paramCount = lua_gettop(state);
  346. // Attempt to match the parameters to a valid binding.
  347. switch (paramCount)
  348. {
  349. case 2:
  350. {
  351. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  352. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  353. {
  354. // Get parameter 1 off the stack.
  355. const char* param1 = ScriptUtil::getString(2, false);
  356. Bundle* instance = getInstance(state);
  357. void* returnPtr = (void*)instance->loadNode(param1);
  358. if (returnPtr)
  359. {
  360. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  361. object->instance = returnPtr;
  362. object->owns = true;
  363. luaL_getmetatable(state, "Node");
  364. lua_setmetatable(state, -2);
  365. }
  366. else
  367. {
  368. lua_pushnil(state);
  369. }
  370. return 1;
  371. }
  372. else
  373. {
  374. lua_pushstring(state, "lua_Bundle_loadNode - Failed to match the given parameters to a valid function signature.");
  375. lua_error(state);
  376. }
  377. break;
  378. }
  379. default:
  380. {
  381. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  382. lua_error(state);
  383. break;
  384. }
  385. }
  386. return 0;
  387. }
  388. int lua_Bundle_loadScene(lua_State* state)
  389. {
  390. // Get the number of parameters.
  391. int paramCount = lua_gettop(state);
  392. // Attempt to match the parameters to a valid binding.
  393. switch (paramCount)
  394. {
  395. case 1:
  396. {
  397. if ((lua_type(state, 1) == LUA_TUSERDATA))
  398. {
  399. Bundle* instance = getInstance(state);
  400. void* returnPtr = (void*)instance->loadScene();
  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. else
  416. {
  417. lua_pushstring(state, "lua_Bundle_loadScene - Failed to match the given parameters to a valid function signature.");
  418. lua_error(state);
  419. }
  420. break;
  421. }
  422. case 2:
  423. {
  424. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  425. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  426. {
  427. // Get parameter 1 off the stack.
  428. const char* param1 = ScriptUtil::getString(2, false);
  429. Bundle* instance = getInstance(state);
  430. void* returnPtr = (void*)instance->loadScene(param1);
  431. if (returnPtr)
  432. {
  433. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  434. object->instance = returnPtr;
  435. object->owns = true;
  436. luaL_getmetatable(state, "Scene");
  437. lua_setmetatable(state, -2);
  438. }
  439. else
  440. {
  441. lua_pushnil(state);
  442. }
  443. return 1;
  444. }
  445. else
  446. {
  447. lua_pushstring(state, "lua_Bundle_loadScene - Failed to match the given parameters to a valid function signature.");
  448. lua_error(state);
  449. }
  450. break;
  451. }
  452. default:
  453. {
  454. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  455. lua_error(state);
  456. break;
  457. }
  458. }
  459. return 0;
  460. }
  461. int lua_Bundle_release(lua_State* state)
  462. {
  463. // Get the number of parameters.
  464. int paramCount = lua_gettop(state);
  465. // Attempt to match the parameters to a valid binding.
  466. switch (paramCount)
  467. {
  468. case 1:
  469. {
  470. if ((lua_type(state, 1) == LUA_TUSERDATA))
  471. {
  472. Bundle* instance = getInstance(state);
  473. instance->release();
  474. return 0;
  475. }
  476. else
  477. {
  478. lua_pushstring(state, "lua_Bundle_release - Failed to match the given parameters to a valid function signature.");
  479. lua_error(state);
  480. }
  481. break;
  482. }
  483. default:
  484. {
  485. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  486. lua_error(state);
  487. break;
  488. }
  489. }
  490. return 0;
  491. }
  492. int lua_Bundle_static_create(lua_State* state)
  493. {
  494. // Get the number of parameters.
  495. int paramCount = lua_gettop(state);
  496. // Attempt to match the parameters to a valid binding.
  497. switch (paramCount)
  498. {
  499. case 1:
  500. {
  501. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  502. {
  503. // Get parameter 1 off the stack.
  504. const char* param1 = ScriptUtil::getString(1, false);
  505. void* returnPtr = (void*)Bundle::create(param1);
  506. if (returnPtr)
  507. {
  508. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  509. object->instance = returnPtr;
  510. object->owns = true;
  511. luaL_getmetatable(state, "Bundle");
  512. lua_setmetatable(state, -2);
  513. }
  514. else
  515. {
  516. lua_pushnil(state);
  517. }
  518. return 1;
  519. }
  520. else
  521. {
  522. lua_pushstring(state, "lua_Bundle_static_create - Failed to match the given parameters to a valid function signature.");
  523. lua_error(state);
  524. }
  525. break;
  526. }
  527. default:
  528. {
  529. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  530. lua_error(state);
  531. break;
  532. }
  533. }
  534. return 0;
  535. }
  536. }