wrap_Filesystem.cpp 23 KB

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