wrap_Filesystem.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * Copyright (c) 2006-2010 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. return instance->load(L);
  188. }
  189. int loader(lua_State * L)
  190. {
  191. const char * filename = lua_tostring(L, -1);
  192. std::string tmp(filename);
  193. int size = tmp.size();
  194. if(size <= 4 || strcmp(filename + (size-4), ".lua") != 0)
  195. {
  196. tmp.append(".lua");
  197. size = tmp.size();
  198. }
  199. for(int i=0;i<size-4;i++)
  200. {
  201. if(tmp[i] == '.')
  202. {
  203. tmp[i] = '/';
  204. }
  205. }
  206. // Check whether file exists.
  207. if(instance->exists(tmp.c_str()))
  208. {
  209. lua_pop(L, 1);
  210. lua_pushstring(L, tmp.c_str());
  211. // Ok, load it.
  212. return instance->load(L);
  213. }
  214. tmp = filename;
  215. size = tmp.size();
  216. for(int i=0;i<size;i++)
  217. {
  218. if(tmp[i] == '.')
  219. {
  220. tmp[i] = '/';
  221. }
  222. }
  223. if (instance->isDirectory(tmp.c_str()))
  224. {
  225. tmp += "/init.lua";
  226. if (instance->exists(tmp.c_str())) {
  227. lua_pop(L, 1);
  228. lua_pushstring(L, tmp.c_str());
  229. // Ok, load it.
  230. return instance->load(L);
  231. }
  232. }
  233. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", tmp.c_str());
  234. return 1;
  235. }
  236. // List of functions to wrap.
  237. static const luaL_Reg functions[] = {
  238. { "init", w_init },
  239. { "setIdentity", w_setIdentity },
  240. { "setSource", w_setSource },
  241. { "newFile", w_newFile },
  242. { "getWorkingDirectory", w_getWorkingDirectory },
  243. { "getUserDirectory", w_getUserDirectory },
  244. { "getAppdataDirectory", w_getAppdataDirectory },
  245. { "getSaveDirectory", w_getSaveDirectory },
  246. { "exists", w_exists },
  247. { "isDirectory", w_isDirectory },
  248. { "isFile", w_isFile },
  249. { "mkdir", w_mkdir },
  250. { "remove", w_remove },
  251. { "read", w_read },
  252. { "write", w_write },
  253. { "enumerate", w_enumerate },
  254. { "lines", w_lines },
  255. { "load", w_load },
  256. { "newFileData", w_newFileData },
  257. { 0, 0 }
  258. };
  259. static const lua_CFunction types[] = {
  260. luaopen_file,
  261. luaopen_filedata,
  262. 0
  263. };
  264. int luaopen_love_filesystem(lua_State * L)
  265. {
  266. if(instance == 0)
  267. {
  268. try
  269. {
  270. instance = new Filesystem();
  271. love::luax_register_searcher(L, loader);
  272. }
  273. catch(Exception & e)
  274. {
  275. return luaL_error(L, e.what());
  276. }
  277. }
  278. else
  279. {
  280. instance->retain();
  281. love::luax_register_searcher(L, loader);
  282. }
  283. WrappedModule w;
  284. w.module = instance;
  285. w.name = "filesystem";
  286. w.flags = MODULE_FILESYSTEM_T;
  287. w.functions = functions;
  288. w.types = types;
  289. return luax_register_module(L, w);
  290. }
  291. } // physfs
  292. } // filesystem
  293. } // love