wrap_Filesystem.cpp 23 KB

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