wrap_Filesystem.cpp 17 KB

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