wrap_Filesystem.cpp 15 KB

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