wrap_Filesystem.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. * Copyright (c) 2006-2011 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "wrap_Filesystem.h"
  22. namespace love
  23. {
  24. namespace filesystem
  25. {
  26. namespace physfs
  27. {
  28. static Filesystem * instance = 0;
  29. bool hack_setupWriteDirectory()
  30. {
  31. if(instance != 0)
  32. return instance->setupWriteDirectory();
  33. return false;
  34. }
  35. int w_init(lua_State * L)
  36. {
  37. const char * arg0 = luaL_checkstring(L, 1);
  38. try
  39. {
  40. instance->init(arg0);
  41. }
  42. catch(Exception & e)
  43. {
  44. return luaL_error(L, e.what());
  45. }
  46. return 0;
  47. }
  48. int w_setIdentity(lua_State * L)
  49. {
  50. const char * arg = luaL_checkstring(L, 1);
  51. if(!instance->setIdentity(arg))
  52. return luaL_error(L, "Could not set write directory.");
  53. return 0;
  54. }
  55. int w_setSource(lua_State * L)
  56. {
  57. const char * arg = luaL_checkstring(L, 1);
  58. if(!instance->setSource(arg))
  59. return luaL_error(L, "Could not set source.");
  60. return 0;
  61. }
  62. int w_newFile(lua_State * L)
  63. {
  64. const char * filename = luaL_checkstring(L, 1);
  65. File * t;
  66. try
  67. {
  68. t = instance->newFile(filename);
  69. }
  70. catch(Exception e)
  71. {
  72. return luaL_error(L, e.what());
  73. }
  74. luax_newtype(L, "File", FILESYSTEM_FILE_T, (void*)t);
  75. return 1;
  76. }
  77. int w_newFileData(lua_State * L)
  78. {
  79. if(!lua_isstring(L, 1))
  80. return luaL_error(L, "String expected.");
  81. if(!lua_isstring(L, 2))
  82. return luaL_error(L, "String expected.");
  83. size_t length = 0;
  84. const char * str = lua_tolstring(L, 1, &length);
  85. const char * filename = lua_tostring(L, 2);
  86. const char * decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0;
  87. FileData::Decoder decoder = FileData::FILE;
  88. if(decstr)
  89. FileData::getConstant(decstr, decoder);
  90. FileData * t = 0;
  91. switch(decoder)
  92. {
  93. case FileData::FILE:
  94. t = instance->newFileData((void*)str, (int)length, filename);
  95. break;
  96. case FileData::BASE64:
  97. t = instance->newFileData(str, filename);
  98. break;
  99. default:
  100. return luaL_error(L, "Unrecognized FileData decoder: %s", decstr);
  101. }
  102. luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void*)t);
  103. return 1;
  104. }
  105. int w_getWorkingDirectory(lua_State * L)
  106. {
  107. lua_pushstring(L, instance->getWorkingDirectory());
  108. return 1;
  109. }
  110. int w_getUserDirectory(lua_State * L)
  111. {
  112. lua_pushstring(L, instance->getUserDirectory());
  113. return 1;
  114. }
  115. int w_getAppdataDirectory(lua_State * L)
  116. {
  117. lua_pushstring(L, instance->getAppdataDirectory());
  118. return 1;
  119. }
  120. int w_getSaveDirectory(lua_State * L)
  121. {
  122. lua_pushstring(L, instance->getSaveDirectory());
  123. return 1;
  124. }
  125. int w_exists(lua_State * L)
  126. {
  127. const char * arg = luaL_checkstring(L, 1);
  128. lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
  129. return 1;
  130. }
  131. int w_isDirectory(lua_State * L)
  132. {
  133. const char * arg = luaL_checkstring(L, 1);
  134. lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
  135. return 1;
  136. }
  137. int w_isFile(lua_State * L)
  138. {
  139. const char * arg = luaL_checkstring(L, 1);
  140. lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
  141. return 1;
  142. }
  143. int w_mkdir(lua_State * L)
  144. {
  145. const char * arg = luaL_checkstring(L, 1);
  146. lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
  147. return 1;
  148. }
  149. int w_remove(lua_State * L)
  150. {
  151. const char * arg = luaL_checkstring(L, 1);
  152. lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
  153. return 1;
  154. }
  155. int w_read(lua_State * L)
  156. {
  157. try
  158. {
  159. return instance->read(L);
  160. }
  161. catch(Exception e)
  162. {
  163. return luaL_error(L, e.what());
  164. }
  165. }
  166. int w_write(lua_State * L)
  167. {
  168. try
  169. {
  170. return instance->write(L);
  171. }
  172. catch(Exception e)
  173. {
  174. return luaL_error(L, e.what());
  175. }
  176. }
  177. int w_enumerate(lua_State * L)
  178. {
  179. return instance->enumerate(L);
  180. }
  181. int w_lines(lua_State * L)
  182. {
  183. return instance->lines(L);
  184. }
  185. int w_load(lua_State * L)
  186. {
  187. try {
  188. return instance->load(L);
  189. }
  190. catch (love::Exception & e) {
  191. return luaL_error(L, e.what());
  192. }
  193. }
  194. int w_getLastModified(lua_State * L)
  195. {
  196. return instance->getLastModified(L);
  197. }
  198. int loader(lua_State * L)
  199. {
  200. const char * filename = lua_tostring(L, -1);
  201. std::string tmp(filename);
  202. int size = tmp.size();
  203. if(size <= 4 || strcmp(filename + (size-4), ".lua") != 0)
  204. {
  205. tmp.append(".lua");
  206. size = tmp.size();
  207. }
  208. for(int i=0;i<size-4;i++)
  209. {
  210. if(tmp[i] == '.')
  211. {
  212. tmp[i] = '/';
  213. }
  214. }
  215. // Check whether file exists.
  216. if(instance->exists(tmp.c_str()))
  217. {
  218. lua_pop(L, 1);
  219. lua_pushstring(L, tmp.c_str());
  220. // Ok, load it.
  221. return instance->load(L);
  222. }
  223. tmp = filename;
  224. size = tmp.size();
  225. for(int i=0;i<size;i++)
  226. {
  227. if(tmp[i] == '.')
  228. {
  229. tmp[i] = '/';
  230. }
  231. }
  232. if (instance->isDirectory(tmp.c_str()))
  233. {
  234. tmp += "/init.lua";
  235. if (instance->exists(tmp.c_str())) {
  236. lua_pop(L, 1);
  237. lua_pushstring(L, tmp.c_str());
  238. // Ok, load it.
  239. return instance->load(L);
  240. }
  241. }
  242. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", tmp.c_str());
  243. return 1;
  244. }
  245. // List of functions to wrap.
  246. static const luaL_Reg functions[] = {
  247. { "init", w_init },
  248. { "setIdentity", w_setIdentity },
  249. { "setSource", w_setSource },
  250. { "newFile", w_newFile },
  251. { "getWorkingDirectory", w_getWorkingDirectory },
  252. { "getUserDirectory", w_getUserDirectory },
  253. { "getAppdataDirectory", w_getAppdataDirectory },
  254. { "getSaveDirectory", w_getSaveDirectory },
  255. { "exists", w_exists },
  256. { "isDirectory", w_isDirectory },
  257. { "isFile", w_isFile },
  258. { "mkdir", w_mkdir },
  259. { "remove", w_remove },
  260. { "read", w_read },
  261. { "write", w_write },
  262. { "enumerate", w_enumerate },
  263. { "lines", w_lines },
  264. { "load", w_load },
  265. { "getLastModified", w_getLastModified },
  266. { "newFileData", w_newFileData },
  267. { 0, 0 }
  268. };
  269. static const lua_CFunction types[] = {
  270. luaopen_file,
  271. luaopen_filedata,
  272. 0
  273. };
  274. int luaopen_love_filesystem_physfs(lua_State * L)
  275. {
  276. if(instance == 0)
  277. {
  278. try
  279. {
  280. instance = new Filesystem();
  281. love::luax_register_searcher(L, loader);
  282. }
  283. catch(Exception & e)
  284. {
  285. return luaL_error(L, e.what());
  286. }
  287. }
  288. else
  289. {
  290. instance->retain();
  291. love::luax_register_searcher(L, loader);
  292. }
  293. WrappedModule w;
  294. w.module = instance;
  295. w.name = "filesystem";
  296. w.flags = MODULE_FILESYSTEM_T;
  297. w.functions = functions;
  298. w.types = types;
  299. return luax_register_module(L, w);
  300. }
  301. } // physfs
  302. } // filesystem
  303. } // love