wrap_Filesystem.cpp 13 KB

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