wrap_Filesystem.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /**
  2. * Copyright (c) 2006-2017 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 "common/config.h"
  22. #include "wrap_Filesystem.h"
  23. #include "wrap_File.h"
  24. #include "wrap_DroppedFile.h"
  25. #include "wrap_FileData.h"
  26. #include "data/wrap_Data.h"
  27. #include "physfs/Filesystem.h"
  28. // SDL
  29. #include <SDL_loadso.h>
  30. // STL
  31. #include <vector>
  32. #include <string>
  33. #include <sstream>
  34. #include <algorithm>
  35. namespace love
  36. {
  37. namespace filesystem
  38. {
  39. #define instance() (Module::getInstance<Filesystem>(Module::M_FILESYSTEM))
  40. bool hack_setupWriteDirectory()
  41. {
  42. if (instance() != 0)
  43. return instance()->setupWriteDirectory();
  44. return false;
  45. }
  46. int w_init(lua_State *L)
  47. {
  48. const char *arg0 = luaL_checkstring(L, 1);
  49. luax_catchexcept(L, [&](){ instance()->init(arg0); });
  50. return 0;
  51. }
  52. int w_setFused(lua_State *L)
  53. {
  54. // no error checking needed, everything, even nothing
  55. // can be converted to a boolean
  56. instance()->setFused(luax_toboolean(L, 1));
  57. return 0;
  58. }
  59. int w_isFused(lua_State *L)
  60. {
  61. luax_pushboolean(L, instance()->isFused());
  62. return 1;
  63. }
  64. int w_setAndroidSaveExternal(lua_State *L)
  65. {
  66. bool useExternal = luax_optboolean(L, 1, false);
  67. instance()->setAndroidSaveExternal(useExternal);
  68. return 0;
  69. }
  70. int w_setIdentity(lua_State *L)
  71. {
  72. const char *arg = luaL_checkstring(L, 1);
  73. bool append = luax_optboolean(L, 2, false);
  74. if (!instance()->setIdentity(arg, append))
  75. return luaL_error(L, "Could not set write directory.");
  76. return 0;
  77. }
  78. int w_getIdentity(lua_State *L)
  79. {
  80. lua_pushstring(L, instance()->getIdentity());
  81. return 1;
  82. }
  83. int w_setSource(lua_State *L)
  84. {
  85. const char *arg = luaL_checkstring(L, 1);
  86. if (!instance()->setSource(arg))
  87. return luaL_error(L, "Could not set source.");
  88. return 0;
  89. }
  90. int w_getSource(lua_State *L)
  91. {
  92. lua_pushstring(L, instance()->getSource());
  93. return 1;
  94. }
  95. int w_mount(lua_State *L)
  96. {
  97. std::string archive;
  98. if (luax_istype(L, 1, DroppedFile::type))
  99. {
  100. DroppedFile *file = luax_totype<DroppedFile>(L, 1);
  101. archive = file->getFilename();
  102. }
  103. else
  104. archive = luax_checkstring(L, 1);
  105. const char *mountpoint = luaL_checkstring(L, 2);
  106. bool append = luax_optboolean(L, 3, false);
  107. luax_pushboolean(L, instance()->mount(archive.c_str(), mountpoint, append));
  108. return 1;
  109. }
  110. int w_unmount(lua_State *L)
  111. {
  112. const char *archive = luaL_checkstring(L, 1);
  113. luax_pushboolean(L, instance()->unmount(archive));
  114. return 1;
  115. }
  116. int w_newFile(lua_State *L)
  117. {
  118. const char *filename = luaL_checkstring(L, 1);
  119. const char *str = 0;
  120. File::Mode mode = File::MODE_CLOSED;
  121. if (lua_isstring(L, 2))
  122. {
  123. str = luaL_checkstring(L, 2);
  124. if (!File::getConstant(str, mode))
  125. return luaL_error(L, "Incorrect file open mode: %s", str);
  126. }
  127. File *t = instance()->newFile(filename);
  128. if (mode != File::MODE_CLOSED)
  129. {
  130. try
  131. {
  132. if (!t->open(mode))
  133. throw love::Exception("Could not open file.");
  134. }
  135. catch (love::Exception &e)
  136. {
  137. t->release();
  138. return luax_ioError(L, "%s", e.what());
  139. }
  140. }
  141. luax_pushtype(L, t);
  142. t->release();
  143. return 1;
  144. }
  145. File *luax_getfile(lua_State *L, int idx)
  146. {
  147. File *file = nullptr;
  148. if (lua_isstring(L, idx))
  149. {
  150. const char *filename = luaL_checkstring(L, idx);
  151. file = instance()->newFile(filename);
  152. }
  153. else
  154. file = luax_checkfile(L, idx);
  155. return file;
  156. }
  157. FileData *luax_getfiledata(lua_State *L, int idx)
  158. {
  159. FileData *data = nullptr;
  160. File *file = nullptr;
  161. if (lua_isstring(L, idx) || luax_istype(L, idx, File::type))
  162. {
  163. file = luax_getfile(L, idx);
  164. file->retain();
  165. }
  166. else if (luax_istype(L, idx, FileData::type))
  167. {
  168. data = luax_checkfiledata(L, idx);
  169. data->retain();
  170. }
  171. if (!data && !file)
  172. {
  173. luaL_argerror(L, idx, "filename, File, or FileData expected");
  174. return nullptr; // Never reached.
  175. }
  176. if (file)
  177. {
  178. luax_catchexcept(L,
  179. [&]() { data = file->read(); },
  180. [&](bool) { file->release(); }
  181. );
  182. }
  183. return data;
  184. }
  185. Data *luax_getdata(lua_State *L, int idx)
  186. {
  187. Data *data = nullptr;
  188. File *file = nullptr;
  189. if (lua_isstring(L, idx) || luax_istype(L, idx, File::type))
  190. {
  191. file = luax_getfile(L, idx);
  192. file->retain();
  193. }
  194. else if (luax_istype(L, idx, Data::type))
  195. {
  196. data = data::luax_checkdata(L, idx);
  197. data->retain();
  198. }
  199. if (!data && !file)
  200. {
  201. luaL_argerror(L, idx, "filename, File, or Data expected");
  202. return nullptr; // Never reached.
  203. }
  204. if (file)
  205. {
  206. luax_catchexcept(L,
  207. [&]() { data = file->read(); },
  208. [&](bool) { file->release(); }
  209. );
  210. }
  211. return data;
  212. }
  213. bool luax_cangetfiledata(lua_State *L, int idx)
  214. {
  215. return lua_isstring(L, idx) || luax_istype(L, idx, File::type) || luax_istype(L, idx, FileData::type);
  216. }
  217. bool luax_cangetdata(lua_State *L, int idx)
  218. {
  219. return lua_isstring(L, idx) || luax_istype(L, idx, File::type) || luax_istype(L, idx, Data::type);
  220. }
  221. int w_newFileData(lua_State *L)
  222. {
  223. // Single argument: treat as filepath or File.
  224. if (lua_gettop(L) == 1)
  225. {
  226. // We don't use luax_getfiledata because we want to use an ioError.
  227. if (lua_isstring(L, 1))
  228. luax_convobj(L, 1, "filesystem", "newFile");
  229. // Get FileData from the File.
  230. if (luax_istype(L, 1, File::type))
  231. {
  232. File *file = luax_checkfile(L, 1);
  233. StrongRef<FileData> data;
  234. try
  235. {
  236. data.set(file->read(), Acquire::NORETAIN);
  237. }
  238. catch (love::Exception &e)
  239. {
  240. return luax_ioError(L, "%s", e.what());
  241. }
  242. luax_pushtype(L, data);
  243. return 1;
  244. }
  245. else
  246. return luaL_argerror(L, 1, "filename or File expected");
  247. }
  248. size_t length = 0;
  249. const char *str = luaL_checklstring(L, 1, &length);
  250. const char *filename = luaL_checkstring(L, 2);
  251. FileData *t = nullptr;
  252. luax_catchexcept(L, [&](){ t = instance()->newFileData(str, length, filename); });
  253. luax_pushtype(L, t);
  254. t->release();
  255. return 1;
  256. }
  257. int w_getWorkingDirectory(lua_State *L)
  258. {
  259. lua_pushstring(L, instance()->getWorkingDirectory());
  260. return 1;
  261. }
  262. int w_getUserDirectory(lua_State *L)
  263. {
  264. luax_pushstring(L, instance()->getUserDirectory());
  265. return 1;
  266. }
  267. int w_getAppdataDirectory(lua_State *L)
  268. {
  269. luax_pushstring(L, instance()->getAppdataDirectory());
  270. return 1;
  271. }
  272. int w_getSaveDirectory(lua_State *L)
  273. {
  274. lua_pushstring(L, instance()->getSaveDirectory());
  275. return 1;
  276. }
  277. int w_getSourceBaseDirectory(lua_State *L)
  278. {
  279. luax_pushstring(L, instance()->getSourceBaseDirectory());
  280. return 1;
  281. }
  282. int w_getRealDirectory(lua_State *L)
  283. {
  284. const char *filename = luaL_checkstring(L, 1);
  285. std::string dir;
  286. try
  287. {
  288. dir = instance()->getRealDirectory(filename);
  289. }
  290. catch (love::Exception &e)
  291. {
  292. return luax_ioError(L, "%s", e.what());
  293. }
  294. lua_pushstring(L, dir.c_str());
  295. return 1;
  296. }
  297. int w_getExecutablePath(lua_State *L)
  298. {
  299. luax_pushstring(L, instance()->getExecutablePath());
  300. return 1;
  301. }
  302. int w_getInfo(lua_State *L)
  303. {
  304. const char *filepath = luaL_checkstring(L, 1);
  305. Filesystem::Info info = {};
  306. if (instance()->getInfo(filepath, info))
  307. {
  308. const char *typestr = nullptr;
  309. if (!Filesystem::getConstant(info.type, typestr))
  310. return luaL_error(L, "Unknown file type.");
  311. if (lua_istable(L, 2))
  312. lua_pushvalue(L, 2);
  313. else
  314. lua_createtable(L, 0, 3);
  315. lua_pushstring(L, typestr);
  316. lua_setfield(L, -2, "type");
  317. // Lua numbers (doubles) can't fit the full range of 64 bit ints.
  318. info.size = std::min<int64>(info.size, 0x20000000000000LL);
  319. if (info.size >= 0)
  320. {
  321. lua_pushnumber(L, (lua_Number) info.size);
  322. lua_setfield(L, -2, "size");
  323. }
  324. info.modtime = std::min<int64>(info.modtime, 0x20000000000000LL);
  325. if (info.modtime >= 0)
  326. {
  327. lua_pushnumber(L, (lua_Number) info.modtime);
  328. lua_setfield(L, -2, "modtime");
  329. }
  330. }
  331. else
  332. lua_pushnil(L);
  333. return 1;
  334. }
  335. int w_createDirectory(lua_State *L)
  336. {
  337. const char *arg = luaL_checkstring(L, 1);
  338. luax_pushboolean(L, instance()->createDirectory(arg));
  339. return 1;
  340. }
  341. int w_remove(lua_State *L)
  342. {
  343. const char *arg = luaL_checkstring(L, 1);
  344. luax_pushboolean(L, instance()->remove(arg));
  345. return 1;
  346. }
  347. int w_read(lua_State *L)
  348. {
  349. const char *filename = luaL_checkstring(L, 1);
  350. int64 len = (int64) luaL_optinteger(L, 2, File::ALL);
  351. Data *data = nullptr;
  352. try
  353. {
  354. data = instance()->read(filename, len);
  355. }
  356. catch (love::Exception &e)
  357. {
  358. return luax_ioError(L, "%s", e.what());
  359. }
  360. if (data == nullptr)
  361. return luax_ioError(L, "File could not be read.");
  362. // Push the string.
  363. lua_pushlstring(L, (const char *) data->getData(), data->getSize());
  364. // Push the size.
  365. lua_pushinteger(L, data->getSize());
  366. // Lua has a copy now, so we can free it.
  367. data->release();
  368. return 2;
  369. }
  370. static int w_write_or_append(lua_State *L, File::Mode mode)
  371. {
  372. const char *filename = luaL_checkstring(L, 1);
  373. const char *input = nullptr;
  374. size_t len = 0;
  375. if (luax_istype(L, 2, love::Data::type))
  376. {
  377. love::Data *data = luax_totype<love::Data>(L, 2);
  378. input = (const char *) data->getData();
  379. len = data->getSize();
  380. }
  381. else if (lua_isstring(L, 2))
  382. input = lua_tolstring(L, 2, &len);
  383. else
  384. return luaL_argerror(L, 2, "string or Data expected");
  385. // Get how much we should write. Length of string default.
  386. len = luaL_optinteger(L, 3, len);
  387. try
  388. {
  389. if (mode == File::MODE_APPEND)
  390. instance()->append(filename, (const void *) input, len);
  391. else
  392. instance()->write(filename, (const void *) input, len);
  393. }
  394. catch (love::Exception &e)
  395. {
  396. return luax_ioError(L, "%s", e.what());
  397. }
  398. luax_pushboolean(L, true);
  399. return 1;
  400. }
  401. int w_write(lua_State *L)
  402. {
  403. return w_write_or_append(L, File::MODE_WRITE);
  404. }
  405. int w_append(lua_State *L)
  406. {
  407. return w_write_or_append(L, File::MODE_APPEND);
  408. }
  409. int w_getDirectoryItems(lua_State *L)
  410. {
  411. const char *dir = luaL_checkstring(L, 1);
  412. std::vector<std::string> items;
  413. instance()->getDirectoryItems(dir, items);
  414. lua_createtable(L, (int) items.size(), 0);
  415. for (int i = 0; i < (int) items.size(); i++)
  416. {
  417. lua_pushstring(L, items[i].c_str());
  418. lua_rawseti(L, -2, i + 1);
  419. }
  420. // Return the table.
  421. return 1;
  422. }
  423. int w_lines(lua_State *L)
  424. {
  425. if (lua_isstring(L, 1))
  426. {
  427. File *file = instance()->newFile(lua_tostring(L, 1));
  428. bool success = false;
  429. luax_catchexcept(L, [&](){ success = file->open(File::MODE_READ); });
  430. if (!success)
  431. {
  432. file->release();
  433. return luaL_error(L, "Could not open file.");
  434. }
  435. luax_pushtype(L, file);
  436. file->release();
  437. }
  438. else
  439. return luaL_argerror(L, 1, "expected filename.");
  440. lua_pushcclosure(L, w_File_lines_i, 1);
  441. return 1;
  442. }
  443. int w_load(lua_State *L)
  444. {
  445. std::string filename = std::string(luaL_checkstring(L, 1));
  446. Data *data = nullptr;
  447. try
  448. {
  449. data = instance()->read(filename.c_str());
  450. }
  451. catch (love::Exception &e)
  452. {
  453. return luax_ioError(L, "%s", e.what());
  454. }
  455. int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
  456. data->release();
  457. // Load the chunk, but don't run it.
  458. switch (status)
  459. {
  460. case LUA_ERRMEM:
  461. return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
  462. case LUA_ERRSYNTAX:
  463. return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
  464. default: // success
  465. return 1;
  466. }
  467. }
  468. int w_setSymlinksEnabled(lua_State *L)
  469. {
  470. instance()->setSymlinksEnabled(luax_checkboolean(L, 1));
  471. return 0;
  472. }
  473. int w_areSymlinksEnabled(lua_State *L)
  474. {
  475. luax_pushboolean(L, instance()->areSymlinksEnabled());
  476. return 1;
  477. }
  478. int w_getRequirePath(lua_State *L)
  479. {
  480. std::stringstream path;
  481. bool seperator = false;
  482. for (auto &element : instance()->getRequirePath())
  483. {
  484. if (seperator)
  485. path << ";";
  486. else
  487. seperator = true;
  488. path << element;
  489. }
  490. luax_pushstring(L, path.str());
  491. return 1;
  492. }
  493. int w_getCRequirePath(lua_State *L)
  494. {
  495. std::stringstream path;
  496. bool seperator = false;
  497. for (auto &element : instance()->getCRequirePath())
  498. {
  499. if (seperator)
  500. path << ";";
  501. else
  502. seperator = true;
  503. path << element;
  504. }
  505. luax_pushstring(L, path.str());
  506. return 1;
  507. }
  508. int w_setRequirePath(lua_State *L)
  509. {
  510. std::string element = luax_checkstring(L, 1);
  511. auto &requirePath = instance()->getRequirePath();
  512. requirePath.clear();
  513. std::stringstream path;
  514. path << element;
  515. while(std::getline(path, element, ';'))
  516. requirePath.push_back(element);
  517. return 0;
  518. }
  519. int w_setCRequirePath(lua_State *L)
  520. {
  521. std::string element = luax_checkstring(L, 1);
  522. auto &requirePath = instance()->getCRequirePath();
  523. requirePath.clear();
  524. std::stringstream path;
  525. path << element;
  526. while(std::getline(path, element, ';'))
  527. requirePath.push_back(element);
  528. return 0;
  529. }
  530. static void replaceAll(std::string &str, const std::string &substr, const std::string &replacement)
  531. {
  532. std::vector<size_t> locations;
  533. size_t pos = 0;
  534. size_t sublen = substr.length();
  535. while ((pos = str.find(substr, pos)) != std::string::npos)
  536. {
  537. locations.push_back(pos);
  538. pos += sublen;
  539. }
  540. for (int i = (int) locations.size() - 1; i >= 0; i--)
  541. str.replace(locations[i], sublen, replacement);
  542. }
  543. int loader(lua_State *L)
  544. {
  545. std::string modulename = luax_tostring(L, 1);
  546. for (char &c : modulename)
  547. {
  548. if (c == '.')
  549. c = '/';
  550. }
  551. auto *inst = instance();
  552. for (std::string element : inst->getRequirePath())
  553. {
  554. replaceAll(element, "?", modulename);
  555. Filesystem::Info info = {};
  556. if (inst->getInfo(element.c_str(), info) && info.type != Filesystem::FILETYPE_DIRECTORY)
  557. {
  558. lua_pop(L, 1);
  559. lua_pushstring(L, element.c_str());
  560. return w_load(L);
  561. }
  562. }
  563. std::string errstr = "\n\tno '%s' in LOVE game directories.";
  564. lua_pushfstring(L, errstr.c_str(), modulename.c_str());
  565. return 1;
  566. }
  567. static const char *library_extensions[] =
  568. {
  569. #ifdef LOVE_WINDOWS
  570. ".dll"
  571. #elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
  572. ".dylib", ".so"
  573. #else
  574. ".so"
  575. #endif
  576. };
  577. int extloader(lua_State *L)
  578. {
  579. const char *filename = lua_tostring(L, -1);
  580. std::string tokenized_name(filename);
  581. std::string tokenized_function(filename);
  582. // We need both the tokenized filename (dots replaced with slashes)
  583. // and the tokenized function name (dots replaced with underscores)
  584. // NOTE: Lua's loader queries more names than this one.
  585. for (unsigned int i = 0; i < tokenized_name.size(); i++)
  586. {
  587. if (tokenized_name[i] == '.')
  588. {
  589. tokenized_name[i] = '/';
  590. tokenized_function[i] = '_';
  591. }
  592. }
  593. void *handle = nullptr;
  594. auto *inst = instance();
  595. for (const std::string &el : inst->getCRequirePath())
  596. {
  597. for (const char *ext : library_extensions)
  598. {
  599. std::string element = el;
  600. // Replace ?? with the filename and extension
  601. replaceAll(element, "??", tokenized_name + ext);
  602. // And ? with just the filename
  603. replaceAll(element, "?", tokenized_name);
  604. Filesystem::Info info = {};
  605. if (!inst->getInfo(element.c_str(), info) || info.type == Filesystem::FILETYPE_DIRECTORY)
  606. continue;
  607. // Now resolve the full path, as we're bypassing physfs for the next part.
  608. std::string filepath = inst->getRealDirectory(element.c_str()) + LOVE_PATH_SEPARATOR + element;
  609. handle = SDL_LoadObject(filepath.c_str());
  610. // Can fail, for instance if it turned out the source was a zip
  611. if (handle)
  612. break;
  613. }
  614. if (handle)
  615. break;
  616. }
  617. if (!handle)
  618. {
  619. lua_pushfstring(L, "\n\tno file '%s' in LOVE paths.", tokenized_name.c_str());
  620. return 1;
  621. }
  622. // We look for both loveopen_ and luaopen_, so libraries with specific love support
  623. // can tell when they've been loaded by love.
  624. void *func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
  625. if (!func)
  626. func = SDL_LoadFunction(handle, ("luaopen_" + tokenized_function).c_str());
  627. if (!func)
  628. {
  629. SDL_UnloadObject(handle);
  630. lua_pushfstring(L, "\n\tC library '%s' is incompatible.", tokenized_name.c_str());
  631. return 1;
  632. }
  633. lua_pushcfunction(L, (lua_CFunction) func);
  634. return 1;
  635. }
  636. // Deprecated functions.
  637. int w_exists(lua_State *L)
  638. {
  639. luax_markdeprecated(L, "love.filesystem.exists", API_FUNCTION, DEPRECATED_REPLACED, "love.filesystem.getInfo");
  640. const char *arg = luaL_checkstring(L, 1);
  641. Filesystem::Info info = {};
  642. luax_pushboolean(L, instance()->getInfo(arg, info));
  643. return 1;
  644. }
  645. int w_isDirectory(lua_State *L)
  646. {
  647. luax_markdeprecated(L, "love.filesystem.exists", API_FUNCTION, DEPRECATED_REPLACED, "love.filesystem.getInfo");
  648. const char *arg = luaL_checkstring(L, 1);
  649. Filesystem::Info info = {};
  650. bool exists = instance()->getInfo(arg, info);
  651. luax_pushboolean(L, exists && info.type == Filesystem::FILETYPE_DIRECTORY);
  652. return 1;
  653. }
  654. int w_isFile(lua_State *L)
  655. {
  656. luax_markdeprecated(L, "love.filesystem.isFile", API_FUNCTION, DEPRECATED_REPLACED, "love.filesystem.getInfo");
  657. const char *arg = luaL_checkstring(L, 1);
  658. Filesystem::Info info = {};
  659. bool exists = instance()->getInfo(arg, info);
  660. luax_pushboolean(L, exists && info.type == Filesystem::FILETYPE_FILE);
  661. return 1;
  662. }
  663. int w_isSymlink(lua_State *L)
  664. {
  665. luax_markdeprecated(L, "love.filesystem.isSymlink", API_FUNCTION, DEPRECATED_REPLACED, "love.filesystem.getInfo");
  666. const char *filename = luaL_checkstring(L, 1);
  667. Filesystem::Info info = {};
  668. bool exists = instance()->getInfo(filename, info);
  669. luax_pushboolean(L, exists && info.type == Filesystem::FILETYPE_SYMLINK);
  670. return 1;
  671. }
  672. int w_getLastModified(lua_State *L)
  673. {
  674. luax_markdeprecated(L, "love.filesystem.getLastModified", API_FUNCTION, DEPRECATED_REPLACED, "love.filesystem.getInfo");
  675. const char *filename = luaL_checkstring(L, 1);
  676. Filesystem::Info info = {};
  677. bool exists = instance()->getInfo(filename, info);
  678. if (!exists)
  679. return luax_ioError(L, "File does not exist");
  680. else if (info.modtime == -1)
  681. return luax_ioError(L, "Could not determine file modification date.");
  682. lua_pushnumber(L, (lua_Number) info.modtime);
  683. return 1;
  684. }
  685. int w_getSize(lua_State *L)
  686. {
  687. luax_markdeprecated(L, "love.filesystem.getSize", API_FUNCTION, DEPRECATED_REPLACED, "love.filesystem.getInfo");
  688. const char *filename = luaL_checkstring(L, 1);
  689. Filesystem::Info info = {};
  690. bool exists = instance()->getInfo(filename, info);
  691. if (!exists)
  692. luax_ioError(L, "File does not exist");
  693. else if (info.size == -1)
  694. return luax_ioError(L, "Could not determine file size.");
  695. else if (info.size >= 0x20000000000000LL)
  696. return luax_ioError(L, "Size too large to fit into a Lua number!");
  697. lua_pushnumber(L, (lua_Number) info.size);
  698. return 1;
  699. }
  700. // List of functions to wrap.
  701. static const luaL_Reg functions[] =
  702. {
  703. { "init", w_init },
  704. { "setFused", w_setFused },
  705. { "isFused", w_isFused },
  706. { "_setAndroidSaveExternal", w_setAndroidSaveExternal },
  707. { "setIdentity", w_setIdentity },
  708. { "getIdentity", w_getIdentity },
  709. { "setSource", w_setSource },
  710. { "getSource", w_getSource },
  711. { "mount", w_mount },
  712. { "unmount", w_unmount },
  713. { "newFile", w_newFile },
  714. { "getWorkingDirectory", w_getWorkingDirectory },
  715. { "getUserDirectory", w_getUserDirectory },
  716. { "getAppdataDirectory", w_getAppdataDirectory },
  717. { "getSaveDirectory", w_getSaveDirectory },
  718. { "getSourceBaseDirectory", w_getSourceBaseDirectory },
  719. { "getRealDirectory", w_getRealDirectory },
  720. { "getExecutablePath", w_getExecutablePath },
  721. { "createDirectory", w_createDirectory },
  722. { "remove", w_remove },
  723. { "read", w_read },
  724. { "write", w_write },
  725. { "append", w_append },
  726. { "getDirectoryItems", w_getDirectoryItems },
  727. { "lines", w_lines },
  728. { "load", w_load },
  729. { "getInfo", w_getInfo },
  730. { "setSymlinksEnabled", w_setSymlinksEnabled },
  731. { "areSymlinksEnabled", w_areSymlinksEnabled },
  732. { "newFileData", w_newFileData },
  733. { "getRequirePath", w_getRequirePath },
  734. { "setRequirePath", w_setRequirePath },
  735. { "getCRequirePath", w_getCRequirePath },
  736. { "setCRequirePath", w_setCRequirePath },
  737. // Deprecated.
  738. { "exists", w_exists },
  739. { "isDirectory", w_isDirectory },
  740. { "isFile", w_isFile },
  741. { "isSymlink", w_isSymlink },
  742. { "getLastModified", w_getLastModified },
  743. { "getSize", w_getSize },
  744. { 0, 0 }
  745. };
  746. static const lua_CFunction types[] =
  747. {
  748. luaopen_file,
  749. luaopen_droppedfile,
  750. luaopen_filedata,
  751. 0
  752. };
  753. extern "C" int luaopen_love_filesystem(lua_State *L)
  754. {
  755. Filesystem *instance = instance();
  756. if (instance == nullptr)
  757. {
  758. luax_catchexcept(L, [&](){ instance = new physfs::Filesystem(); });
  759. }
  760. else
  761. instance->retain();
  762. // The love loaders should be tried after package.preload.
  763. love::luax_register_searcher(L, loader, 2);
  764. love::luax_register_searcher(L, extloader, 3);
  765. WrappedModule w;
  766. w.module = instance;
  767. w.name = "filesystem";
  768. w.type = &Filesystem::type;
  769. w.functions = functions;
  770. w.types = types;
  771. return luax_register_module(L, w);
  772. }
  773. } // filesystem
  774. } // love