wrap_Filesystem.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. FileData * t = instance->newFileData((void*)str, (int)length, filename);
  87. luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void*)t);
  88. return 1;
  89. }
  90. int w_getWorkingDirectory(lua_State * L)
  91. {
  92. lua_pushstring(L, instance->getWorkingDirectory());
  93. return 1;
  94. }
  95. int w_getUserDirectory(lua_State * L)
  96. {
  97. lua_pushstring(L, instance->getUserDirectory());
  98. return 1;
  99. }
  100. int w_getAppdataDirectory(lua_State * L)
  101. {
  102. lua_pushstring(L, instance->getAppdataDirectory());
  103. return 1;
  104. }
  105. int w_getSaveDirectory(lua_State * L)
  106. {
  107. lua_pushstring(L, instance->getSaveDirectory());
  108. return 1;
  109. }
  110. int w_exists(lua_State * L)
  111. {
  112. const char * arg = luaL_checkstring(L, 1);
  113. lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
  114. return 1;
  115. }
  116. int w_isDirectory(lua_State * L)
  117. {
  118. const char * arg = luaL_checkstring(L, 1);
  119. lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
  120. return 1;
  121. }
  122. int w_isFile(lua_State * L)
  123. {
  124. const char * arg = luaL_checkstring(L, 1);
  125. lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
  126. return 1;
  127. }
  128. int w_mkdir(lua_State * L)
  129. {
  130. const char * arg = luaL_checkstring(L, 1);
  131. lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
  132. return 1;
  133. }
  134. int w_remove(lua_State * L)
  135. {
  136. const char * arg = luaL_checkstring(L, 1);
  137. lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
  138. return 1;
  139. }
  140. int w_read(lua_State * L)
  141. {
  142. try
  143. {
  144. return instance->read(L);
  145. }
  146. catch(Exception e)
  147. {
  148. return luaL_error(L, e.what());
  149. }
  150. }
  151. int w_write(lua_State * L)
  152. {
  153. try
  154. {
  155. return instance->write(L);
  156. }
  157. catch(Exception e)
  158. {
  159. return luaL_error(L, e.what());
  160. }
  161. }
  162. int w_enumerate(lua_State * L)
  163. {
  164. return instance->enumerate(L);
  165. }
  166. int w_lines(lua_State * L)
  167. {
  168. return instance->lines(L);
  169. }
  170. int w_load(lua_State * L)
  171. {
  172. return instance->load(L);
  173. }
  174. int loader(lua_State * L)
  175. {
  176. const char * filename = lua_tostring(L, -1);
  177. std::string tmp(filename);
  178. int size = tmp.size();
  179. if(size <= 4 || strcmp(filename + (size-4), ".lua") != 0)
  180. tmp.append(".lua");
  181. for(int i=0;i<size-4;i++)
  182. {
  183. if(tmp[i] == '.')
  184. {
  185. tmp[i] = '/';
  186. }
  187. }
  188. // Check whether file exists.
  189. if(!instance->exists(tmp.c_str()))
  190. {
  191. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", tmp.c_str());
  192. return 1;
  193. }
  194. lua_pop(L, 1);
  195. lua_pushstring(L, tmp.c_str());
  196. // Ok, load it.
  197. return instance->load(L);
  198. }
  199. // List of functions to wrap.
  200. static const luaL_Reg functions[] = {
  201. { "init", w_init },
  202. { "setIdentity", w_setIdentity },
  203. { "setSource", w_setSource },
  204. { "newFile", w_newFile },
  205. { "getWorkingDirectory", w_getWorkingDirectory },
  206. { "getUserDirectory", w_getUserDirectory },
  207. { "getAppdataDirectory", w_getAppdataDirectory },
  208. { "getSaveDirectory", w_getSaveDirectory },
  209. { "exists", w_exists },
  210. { "isDirectory", w_isDirectory },
  211. { "isFile", w_isFile },
  212. { "mkdir", w_mkdir },
  213. { "remove", w_remove },
  214. { "read", w_read },
  215. { "write", w_write },
  216. { "enumerate", w_enumerate },
  217. { "lines", w_lines },
  218. { "load", w_load },
  219. { "newFileData", w_newFileData },
  220. { 0, 0 }
  221. };
  222. static const lua_CFunction types[] = {
  223. luaopen_file,
  224. luaopen_filedata,
  225. 0
  226. };
  227. int luaopen_love_filesystem(lua_State * L)
  228. {
  229. if(instance == 0)
  230. {
  231. try
  232. {
  233. instance = new Filesystem();
  234. love::luax_register_searcher(L, loader);
  235. }
  236. catch(Exception & e)
  237. {
  238. return luaL_error(L, e.what());
  239. }
  240. }
  241. else
  242. instance->retain();
  243. WrappedModule w;
  244. w.module = instance;
  245. w.name = "filesystem";
  246. w.flags = MODULE_FILESYSTEM_T;
  247. w.functions = functions;
  248. w.types = types;
  249. return luax_register_module(L, w);
  250. }
  251. } // physfs
  252. } // filesystem
  253. } // love