wrap_Filesystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * Copyright (c) 2006-2013 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_setRelease(lua_State *L)
  49. {
  50. // no error checking needed, everything, even nothing
  51. // can be converted to a boolean
  52. instance->setRelease(luax_toboolean(L, 1));
  53. return 0;
  54. }
  55. int w_setIdentity(lua_State *L)
  56. {
  57. const char *arg = luaL_checkstring(L, 1);
  58. if (!instance->setIdentity(arg))
  59. return luaL_error(L, "Could not set write directory.");
  60. return 0;
  61. }
  62. int w_getIdentity(lua_State *L)
  63. {
  64. lua_pushstring(L, instance->getIdentity());
  65. return 1;
  66. }
  67. int w_setSource(lua_State *L)
  68. {
  69. const char *arg = luaL_checkstring(L, 1);
  70. if (!instance->setSource(arg))
  71. return luaL_error(L, "Could not set source.");
  72. return 0;
  73. }
  74. int w_newFile(lua_State *L)
  75. {
  76. const char *filename = luaL_checkstring(L, 1);
  77. File *t;
  78. try
  79. {
  80. t = instance->newFile(filename);
  81. }
  82. catch(Exception e)
  83. {
  84. return luaL_error(L, e.what());
  85. }
  86. luax_newtype(L, "File", FILESYSTEM_FILE_T, (void *)t);
  87. return 1;
  88. }
  89. int w_newFileData(lua_State *L)
  90. {
  91. if (!lua_isstring(L, 1))
  92. return luaL_error(L, "String expected.");
  93. if (!lua_isstring(L, 2))
  94. return luaL_error(L, "String expected.");
  95. size_t length = 0;
  96. const char *str = lua_tolstring(L, 1, &length);
  97. const char *filename = lua_tostring(L, 2);
  98. const char *decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0;
  99. FileData::Decoder decoder = FileData::FILE;
  100. if (decstr)
  101. FileData::getConstant(decstr, decoder);
  102. FileData *t = 0;
  103. switch (decoder)
  104. {
  105. case FileData::FILE:
  106. t = instance->newFileData((void *)str, (int)length, filename);
  107. break;
  108. case FileData::BASE64:
  109. t = instance->newFileData(str, filename);
  110. break;
  111. default:
  112. return luaL_error(L, "Unrecognized FileData decoder: %s", decstr);
  113. }
  114. luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void *)t);
  115. return 1;
  116. }
  117. int w_getWorkingDirectory(lua_State *L)
  118. {
  119. lua_pushstring(L, instance->getWorkingDirectory());
  120. return 1;
  121. }
  122. int w_getUserDirectory(lua_State *L)
  123. {
  124. lua_pushstring(L, instance->getUserDirectory());
  125. return 1;
  126. }
  127. int w_getAppdataDirectory(lua_State *L)
  128. {
  129. lua_pushstring(L, instance->getAppdataDirectory());
  130. return 1;
  131. }
  132. int w_getSaveDirectory(lua_State *L)
  133. {
  134. lua_pushstring(L, instance->getSaveDirectory());
  135. return 1;
  136. }
  137. int w_exists(lua_State *L)
  138. {
  139. const char *arg = luaL_checkstring(L, 1);
  140. lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
  141. return 1;
  142. }
  143. int w_isDirectory(lua_State *L)
  144. {
  145. const char *arg = luaL_checkstring(L, 1);
  146. lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
  147. return 1;
  148. }
  149. int w_isFile(lua_State *L)
  150. {
  151. const char *arg = luaL_checkstring(L, 1);
  152. lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
  153. return 1;
  154. }
  155. int w_mkdir(lua_State *L)
  156. {
  157. const char *arg = luaL_checkstring(L, 1);
  158. lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
  159. return 1;
  160. }
  161. int w_remove(lua_State *L)
  162. {
  163. const char *arg = luaL_checkstring(L, 1);
  164. lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
  165. return 1;
  166. }
  167. int w_read(lua_State *L)
  168. {
  169. const char *filename = luaL_checkstring(L, 1);
  170. int64 len = (int64) luaL_optinteger(L, 2, File::ALL);
  171. Data *data = 0;
  172. try
  173. {
  174. data = instance->read(filename, len);
  175. }
  176. catch (love::Exception &e)
  177. {
  178. return luaL_error(L, "%s", e.what());
  179. }
  180. if (data == 0)
  181. return luaL_error(L, "File could not be read.");
  182. // Push the string.
  183. lua_pushlstring(L, (const char *) data->getData(), data->getSize());
  184. // Push the size.
  185. lua_pushinteger(L, data->getSize());
  186. // Lua has a copy now, so we can free it.
  187. data->release();
  188. return 2;
  189. }
  190. static int w_write_or_append(lua_State *L, File::Mode mode)
  191. {
  192. const char *filename = luaL_checkstring(L, 1);
  193. const char *input = 0;
  194. size_t len = 0;
  195. if (luax_istype(L, 2, DATA_T))
  196. {
  197. love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
  198. input = (const char *) data->getData();
  199. len = data->getSize();
  200. }
  201. else if (lua_isstring(L, 2))
  202. input = lua_tolstring(L, 2, &len);
  203. else
  204. return luaL_argerror(L, 2, "string or Data expected");
  205. // Get how much we should write. Length of string default.
  206. len = luaL_optinteger(L, 3, len);
  207. try
  208. {
  209. if (mode == File::APPEND)
  210. instance->append(filename, (const void *) input, len);
  211. else
  212. instance->write(filename, (const void *) input, len);
  213. }
  214. catch (love::Exception &e)
  215. {
  216. return luaL_error(L, "%s", e.what());
  217. }
  218. luax_pushboolean(L, true);
  219. return 1;
  220. }
  221. int w_write(lua_State *L)
  222. {
  223. return w_write_or_append(L, File::WRITE);
  224. }
  225. int w_append(lua_State *L)
  226. {
  227. return w_write_or_append(L, File::APPEND);
  228. }
  229. int w_enumerate(lua_State *L)
  230. {
  231. return instance->enumerate(L);
  232. }
  233. int w_lines(lua_State *L)
  234. {
  235. File *file;
  236. if (lua_isstring(L, 1))
  237. {
  238. file = instance->newFile(lua_tostring(L, 1));
  239. try
  240. {
  241. if (!file->open(File::READ))
  242. return luaL_error(L, "Could not open file.");
  243. }
  244. catch(love::Exception &e)
  245. {
  246. return luaL_error(L, "%s", e.what());
  247. }
  248. luax_newtype(L, "File", FILESYSTEM_FILE_T, file);
  249. }
  250. else
  251. return luaL_error(L, "Expected filename.");
  252. lua_pushcclosure(L, Filesystem::lines_i, 1);
  253. return 1;
  254. }
  255. int w_load(lua_State *L)
  256. {
  257. std::string filename = std::string(luaL_checkstring(L, 1));
  258. Data *data = 0;
  259. try
  260. {
  261. data = instance->read(filename.c_str());
  262. }
  263. catch (love::Exception &e)
  264. {
  265. return luaL_error(L, "%s", e.what());
  266. }
  267. int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
  268. data->release();
  269. // Load the chunk, but don't run it.
  270. switch (status)
  271. {
  272. case LUA_ERRMEM:
  273. return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
  274. case LUA_ERRSYNTAX:
  275. return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
  276. default: // success
  277. return 1;
  278. }
  279. }
  280. int w_getLastModified(lua_State *L)
  281. {
  282. const char *filename = luaL_checkstring(L, 1);
  283. int64 time = 0;
  284. try
  285. {
  286. time = instance->getLastModified(filename);
  287. }
  288. catch (love::Exception &e)
  289. {
  290. lua_pushnil(L);
  291. lua_pushstring(L, e.what());
  292. return 2;
  293. }
  294. lua_pushnumber(L, static_cast<lua_Number>(time));
  295. return 1;
  296. }
  297. int w_getSize(lua_State *L)
  298. {
  299. const char *filename = luaL_checkstring(L, 1);
  300. int64 size = -1;
  301. try
  302. {
  303. size = instance->getSize(filename);
  304. }
  305. catch (love::Exception &e)
  306. {
  307. return luaL_error(L, "%s", e.what());
  308. }
  309. // Error on failure or if size does not fit into a double precision floating-point number.
  310. if (size == -1)
  311. return luaL_error(L, "Could not determine file size.");
  312. else if (size >= 0x20000000000000LL)
  313. return luaL_error(L, "Size too large to fit into a Lua number!");
  314. lua_pushnumber(L, (lua_Number) size);
  315. return 1;
  316. }
  317. int loader(lua_State *L)
  318. {
  319. const char *filename = lua_tostring(L, -1);
  320. std::string tmp(filename);
  321. tmp += ".lua";
  322. int size = tmp.size();
  323. for (int i=0; i<size-4; i++)
  324. {
  325. if (tmp[i] == '.')
  326. {
  327. tmp[i] = '/';
  328. }
  329. }
  330. // Check whether file exists.
  331. if (instance->exists(tmp.c_str()))
  332. {
  333. lua_pop(L, 1);
  334. lua_pushstring(L, tmp.c_str());
  335. // Ok, load it.
  336. return w_load(L);
  337. }
  338. tmp = filename;
  339. size = tmp.size();
  340. for (int i=0; i<size; i++)
  341. {
  342. if (tmp[i] == '.')
  343. {
  344. tmp[i] = '/';
  345. }
  346. }
  347. if (instance->isDirectory(tmp.c_str()))
  348. {
  349. tmp += "/init.lua";
  350. if (instance->exists(tmp.c_str()))
  351. {
  352. lua_pop(L, 1);
  353. lua_pushstring(L, tmp.c_str());
  354. // Ok, load it.
  355. return w_load(L);
  356. }
  357. }
  358. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", (tmp + ".lua").c_str());
  359. return 1;
  360. }
  361. inline const char *library_extension()
  362. {
  363. #ifdef LOVE_WINDOWS
  364. return ".dll";
  365. #else
  366. return ".so";
  367. #endif
  368. }
  369. int extloader(lua_State *L)
  370. {
  371. const char *filename = lua_tostring(L, -1);
  372. std::string tokenized_name(filename);
  373. std::string tokenized_function(filename);
  374. for (unsigned int i = 0; i < tokenized_name.size(); i++)
  375. {
  376. if (tokenized_name[i] == '.')
  377. {
  378. tokenized_name[i] = '/';
  379. tokenized_function[i] = '_';
  380. }
  381. }
  382. tokenized_name += library_extension();
  383. void *handle = SDL_LoadObject((std::string(instance->getAppdataDirectory()) + LOVE_PATH_SEPARATOR LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR + tokenized_name).c_str());
  384. if (!handle && instance->isRelease())
  385. handle = SDL_LoadObject((std::string(instance->getSaveDirectory()) + LOVE_PATH_SEPARATOR + tokenized_name).c_str());
  386. if (!handle)
  387. {
  388. lua_pushfstring(L, "\n\tno extension \"%s\" in LOVE paths.\n", filename);
  389. return 1;
  390. }
  391. void *func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
  392. if (!func)
  393. func = SDL_LoadFunction(handle, ("luaopen_" + tokenized_function).c_str());
  394. if (!func)
  395. {
  396. SDL_UnloadObject(handle);
  397. lua_pushfstring(L, "\n\textension \"%s\" is incompatible.\n", filename);
  398. return 1;
  399. }
  400. lua_pushcfunction(L, (lua_CFunction) func);
  401. return 1;
  402. }
  403. // List of functions to wrap.
  404. static const luaL_Reg functions[] =
  405. {
  406. { "init", w_init },
  407. { "setRelease", w_setRelease },
  408. { "setIdentity", w_setIdentity },
  409. { "getIdentity", w_getIdentity },
  410. { "setSource", w_setSource },
  411. { "newFile", w_newFile },
  412. { "getWorkingDirectory", w_getWorkingDirectory },
  413. { "getUserDirectory", w_getUserDirectory },
  414. { "getAppdataDirectory", w_getAppdataDirectory },
  415. { "getSaveDirectory", w_getSaveDirectory },
  416. { "exists", w_exists },
  417. { "isDirectory", w_isDirectory },
  418. { "isFile", w_isFile },
  419. { "mkdir", w_mkdir },
  420. { "remove", w_remove },
  421. { "read", w_read },
  422. { "write", w_write },
  423. { "append", w_append },
  424. { "enumerate", w_enumerate },
  425. { "lines", w_lines },
  426. { "load", w_load },
  427. { "getLastModified", w_getLastModified },
  428. { "getSize", w_getSize },
  429. { "newFileData", w_newFileData },
  430. { 0, 0 }
  431. };
  432. static const lua_CFunction types[] =
  433. {
  434. luaopen_file,
  435. luaopen_filedata,
  436. 0
  437. };
  438. extern "C" int luaopen_love_filesystem(lua_State *L)
  439. {
  440. if (instance == 0)
  441. {
  442. try
  443. {
  444. instance = new Filesystem();
  445. love::luax_register_searcher(L, loader, 1);
  446. love::luax_register_searcher(L, extloader, 2);
  447. }
  448. catch(Exception &e)
  449. {
  450. return luaL_error(L, e.what());
  451. }
  452. }
  453. else
  454. {
  455. instance->retain();
  456. love::luax_register_searcher(L, loader, 1);
  457. love::luax_register_searcher(L, extloader, 2);
  458. }
  459. WrappedModule w;
  460. w.module = instance;
  461. w.name = "filesystem";
  462. w.flags = MODULE_FILESYSTEM_T;
  463. w.functions = functions;
  464. w.types = types;
  465. return luax_register_module(L, w);
  466. }
  467. } // physfs
  468. } // filesystem
  469. } // love