lua_FileSystem.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "FileSystem.h"
  4. #include "lua_FileSystem.h"
  5. #include "lua_Global.h"
  6. namespace gameplay
  7. {
  8. void luaRegister_FileSystem()
  9. {
  10. ScriptController* sc = ScriptController::getInstance();
  11. const luaL_Reg lua_members[] =
  12. {
  13. {NULL, NULL}
  14. };
  15. const luaL_Reg lua_statics[] =
  16. {
  17. {"fileExists", lua_FileSystem_static_fileExists},
  18. {"getResourcePath", lua_FileSystem_static_getResourcePath},
  19. {"loadResourceAliases", lua_FileSystem_static_loadResourceAliases},
  20. {"readAll", lua_FileSystem_static_readAll},
  21. {"resolvePath", lua_FileSystem_static_resolvePath},
  22. {"setResourcePath", lua_FileSystem_static_setResourcePath},
  23. {NULL, NULL}
  24. };
  25. std::vector<std::string> scopePath;
  26. sc->registerClass("FileSystem", lua_members, NULL, lua_FileSystem__gc, lua_statics, scopePath);
  27. }
  28. static FileSystem* getInstance(lua_State* state)
  29. {
  30. void* userdata = luaL_checkudata(state, 1, "FileSystem");
  31. luaL_argcheck(state, userdata != NULL, 1, "'FileSystem' expected.");
  32. return (FileSystem*)((ScriptController::LuaObject*)userdata)->instance;
  33. }
  34. int lua_FileSystem__gc(lua_State* state)
  35. {
  36. // Get the number of parameters.
  37. int paramCount = lua_gettop(state);
  38. // Attempt to match the parameters to a valid binding.
  39. switch (paramCount)
  40. {
  41. case 1:
  42. {
  43. if ((lua_type(state, 1) == LUA_TUSERDATA))
  44. {
  45. void* userdata = luaL_checkudata(state, 1, "FileSystem");
  46. luaL_argcheck(state, userdata != NULL, 1, "'FileSystem' expected.");
  47. ScriptController::LuaObject* object = (ScriptController::LuaObject*)userdata;
  48. if (object->owns)
  49. {
  50. FileSystem* instance = (FileSystem*)object->instance;
  51. SAFE_DELETE(instance);
  52. }
  53. return 0;
  54. }
  55. else
  56. {
  57. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  58. lua_error(state);
  59. }
  60. break;
  61. }
  62. default:
  63. {
  64. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  65. lua_error(state);
  66. break;
  67. }
  68. }
  69. return 0;
  70. }
  71. int lua_FileSystem_static_fileExists(lua_State* state)
  72. {
  73. // Get the number of parameters.
  74. int paramCount = lua_gettop(state);
  75. // Attempt to match the parameters to a valid binding.
  76. switch (paramCount)
  77. {
  78. case 1:
  79. {
  80. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  81. {
  82. // Get parameter 1 off the stack.
  83. const char* param1 = ScriptController::getInstance()->getString(1, false);
  84. bool result = FileSystem::fileExists(param1);
  85. // Push the return value onto the stack.
  86. lua_pushboolean(state, result);
  87. return 1;
  88. }
  89. else
  90. {
  91. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  92. lua_error(state);
  93. }
  94. break;
  95. }
  96. default:
  97. {
  98. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  99. lua_error(state);
  100. break;
  101. }
  102. }
  103. return 0;
  104. }
  105. int lua_FileSystem_static_getResourcePath(lua_State* state)
  106. {
  107. // Get the number of parameters.
  108. int paramCount = lua_gettop(state);
  109. // Attempt to match the parameters to a valid binding.
  110. switch (paramCount)
  111. {
  112. case 0:
  113. {
  114. const char* result = FileSystem::getResourcePath();
  115. // Push the return value onto the stack.
  116. lua_pushstring(state, result);
  117. return 1;
  118. break;
  119. }
  120. default:
  121. {
  122. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  123. lua_error(state);
  124. break;
  125. }
  126. }
  127. return 0;
  128. }
  129. int lua_FileSystem_static_loadResourceAliases(lua_State* state)
  130. {
  131. // Get the number of parameters.
  132. int paramCount = lua_gettop(state);
  133. // Attempt to match the parameters to a valid binding.
  134. switch (paramCount)
  135. {
  136. case 1:
  137. {
  138. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  139. {
  140. // Get parameter 1 off the stack.
  141. const char* param1 = ScriptController::getInstance()->getString(1, false);
  142. FileSystem::loadResourceAliases(param1);
  143. return 0;
  144. }
  145. else if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TTABLE || lua_type(state, 1) == LUA_TNIL))
  146. {
  147. // Get parameter 1 off the stack.
  148. Properties* param1 = ScriptController::getInstance()->getObjectPointer<Properties>(1, "Properties", false);
  149. FileSystem::loadResourceAliases(param1);
  150. return 0;
  151. }
  152. else
  153. {
  154. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  155. lua_error(state);
  156. }
  157. break;
  158. }
  159. default:
  160. {
  161. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  162. lua_error(state);
  163. break;
  164. }
  165. }
  166. return 0;
  167. }
  168. int lua_FileSystem_static_readAll(lua_State* state)
  169. {
  170. // Get the number of parameters.
  171. int paramCount = lua_gettop(state);
  172. // Attempt to match the parameters to a valid binding.
  173. switch (paramCount)
  174. {
  175. case 1:
  176. {
  177. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  178. {
  179. // Get parameter 1 off the stack.
  180. const char* param1 = ScriptController::getInstance()->getString(1, false);
  181. const char* result = FileSystem::readAll(param1);
  182. // Push the return value onto the stack.
  183. lua_pushstring(state, result);
  184. return 1;
  185. }
  186. else
  187. {
  188. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  189. lua_error(state);
  190. }
  191. break;
  192. }
  193. case 2:
  194. {
  195. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  196. (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA))
  197. {
  198. // Get parameter 1 off the stack.
  199. const char* param1 = ScriptController::getInstance()->getString(1, false);
  200. // Get parameter 2 off the stack.
  201. int* param2 = ScriptController::getInstance()->getIntPointer(2);
  202. const char* result = FileSystem::readAll(param1, param2);
  203. // Push the return value onto the stack.
  204. lua_pushstring(state, result);
  205. return 1;
  206. }
  207. else
  208. {
  209. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  210. lua_error(state);
  211. }
  212. break;
  213. }
  214. default:
  215. {
  216. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  217. lua_error(state);
  218. break;
  219. }
  220. }
  221. return 0;
  222. }
  223. int lua_FileSystem_static_resolvePath(lua_State* state)
  224. {
  225. // Get the number of parameters.
  226. int paramCount = lua_gettop(state);
  227. // Attempt to match the parameters to a valid binding.
  228. switch (paramCount)
  229. {
  230. case 1:
  231. {
  232. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  233. {
  234. // Get parameter 1 off the stack.
  235. const char* param1 = ScriptController::getInstance()->getString(1, false);
  236. const char* result = FileSystem::resolvePath(param1);
  237. // Push the return value onto the stack.
  238. lua_pushstring(state, result);
  239. return 1;
  240. }
  241. else
  242. {
  243. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  244. lua_error(state);
  245. }
  246. break;
  247. }
  248. default:
  249. {
  250. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  251. lua_error(state);
  252. break;
  253. }
  254. }
  255. return 0;
  256. }
  257. int lua_FileSystem_static_setResourcePath(lua_State* state)
  258. {
  259. // Get the number of parameters.
  260. int paramCount = lua_gettop(state);
  261. // Attempt to match the parameters to a valid binding.
  262. switch (paramCount)
  263. {
  264. case 1:
  265. {
  266. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  267. {
  268. // Get parameter 1 off the stack.
  269. const char* param1 = ScriptController::getInstance()->getString(1, false);
  270. FileSystem::setResourcePath(param1);
  271. return 0;
  272. }
  273. else
  274. {
  275. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  276. lua_error(state);
  277. }
  278. break;
  279. }
  280. default:
  281. {
  282. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  283. lua_error(state);
  284. break;
  285. }
  286. }
  287. return 0;
  288. }
  289. }