wrap_Filesystem.cpp 15 KB

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