wrap_Filesystem.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /**
  2. * Copyright (c) 2006-2013 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. namespace love
  23. {
  24. namespace filesystem
  25. {
  26. namespace physfs
  27. {
  28. static Filesystem *instance = 0;
  29. bool hack_setupWriteDirectory()
  30. {
  31. if (instance != 0)
  32. return instance->setupWriteDirectory();
  33. return false;
  34. }
  35. int w_init(lua_State *L)
  36. {
  37. const char *arg0 = luaL_checkstring(L, 1);
  38. try
  39. {
  40. instance->init(arg0);
  41. }
  42. catch(Exception &e)
  43. {
  44. return luaL_error(L, e.what());
  45. }
  46. return 0;
  47. }
  48. int w_setRelease(lua_State *L)
  49. {
  50. // no error checking needed, everything, even nothing
  51. // can be converted to a boolean
  52. instance->setRelease(luax_toboolean(L, 1));
  53. return 0;
  54. }
  55. int w_setIdentity(lua_State *L)
  56. {
  57. const char *arg = luaL_checkstring(L, 1);
  58. if (!instance->setIdentity(arg))
  59. return luaL_error(L, "Could not set write directory.");
  60. return 0;
  61. }
  62. int w_getIdentity(lua_State *L)
  63. {
  64. lua_pushstring(L, instance->getIdentity());
  65. return 1;
  66. }
  67. int w_setSource(lua_State *L)
  68. {
  69. const char *arg = luaL_checkstring(L, 1);
  70. if (!instance->setSource(arg))
  71. return luaL_error(L, "Could not set source.");
  72. return 0;
  73. }
  74. int w_newFile(lua_State *L)
  75. {
  76. const char *filename = luaL_checkstring(L, 1);
  77. const char *str = 0;
  78. File::Mode mode = File::CLOSED;
  79. if (lua_isstring(L, 2))
  80. {
  81. str = luaL_checkstring(L, 2);
  82. if (!File::getConstant(str, mode))
  83. return luaL_error(L, "Incorrect file open mode: %s", str);
  84. }
  85. File *t = instance->newFile(filename);
  86. if (mode != File::CLOSED)
  87. {
  88. try
  89. {
  90. if (!t->open(mode))
  91. throw love::Exception("Could not open file.");
  92. }
  93. catch (love::Exception &e)
  94. {
  95. t->release();
  96. return luaL_error(L, "%s", e.what());
  97. }
  98. }
  99. luax_newtype(L, "File", FILESYSTEM_FILE_T, (void *)t);
  100. return 1;
  101. }
  102. int w_newFileData(lua_State *L)
  103. {
  104. // Single argument: treat as filepath or File.
  105. if (lua_gettop(L) == 1)
  106. {
  107. if (lua_isstring(L, 1))
  108. luax_convobj(L, 1, "filesystem", "newFile");
  109. // Get FileData from the File.
  110. if (luax_istype(L, 1, FILESYSTEM_FILE_T))
  111. {
  112. File *file = luax_checktype<File>(L, 1, "File", FILESYSTEM_FILE_T);
  113. FileData *data = 0;
  114. try
  115. {
  116. data = file->read();
  117. }
  118. catch (love::Exception &e)
  119. {
  120. return luaL_error(L, "%s", e.what());
  121. }
  122. luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void *) data);
  123. return 1;
  124. }
  125. else
  126. return luaL_argerror(L, 1, "string or File expected");
  127. }
  128. size_t length = 0;
  129. const char *str = luaL_checklstring(L, 1, &length);
  130. const char *filename = luaL_checkstring(L, 2);
  131. const char *decstr = lua_isstring(L, 3) ? lua_tostring(L, 3) : 0;
  132. FileData::Decoder decoder = FileData::FILE;
  133. if (decstr)
  134. FileData::getConstant(decstr, decoder);
  135. FileData *t = 0;
  136. switch (decoder)
  137. {
  138. case FileData::FILE:
  139. t = instance->newFileData((void *)str, (int)length, filename);
  140. break;
  141. case FileData::BASE64:
  142. t = instance->newFileData(str, filename);
  143. break;
  144. default:
  145. return luaL_error(L, "Unrecognized FileData decoder: %s", decstr);
  146. }
  147. luax_newtype(L, "FileData", FILESYSTEM_FILE_DATA_T, (void *)t);
  148. return 1;
  149. }
  150. int w_getWorkingDirectory(lua_State *L)
  151. {
  152. lua_pushstring(L, instance->getWorkingDirectory());
  153. return 1;
  154. }
  155. int w_getUserDirectory(lua_State *L)
  156. {
  157. lua_pushstring(L, instance->getUserDirectory());
  158. return 1;
  159. }
  160. int w_getAppdataDirectory(lua_State *L)
  161. {
  162. lua_pushstring(L, instance->getAppdataDirectory());
  163. return 1;
  164. }
  165. int w_getSaveDirectory(lua_State *L)
  166. {
  167. lua_pushstring(L, instance->getSaveDirectory());
  168. return 1;
  169. }
  170. int w_exists(lua_State *L)
  171. {
  172. const char *arg = luaL_checkstring(L, 1);
  173. lua_pushboolean(L, instance->exists(arg) ? 1 : 0);
  174. return 1;
  175. }
  176. int w_isDirectory(lua_State *L)
  177. {
  178. const char *arg = luaL_checkstring(L, 1);
  179. lua_pushboolean(L, instance->isDirectory(arg) ? 1 : 0);
  180. return 1;
  181. }
  182. int w_isFile(lua_State *L)
  183. {
  184. const char *arg = luaL_checkstring(L, 1);
  185. lua_pushboolean(L, instance->isFile(arg) ? 1 : 0);
  186. return 1;
  187. }
  188. int w_mkdir(lua_State *L)
  189. {
  190. const char *arg = luaL_checkstring(L, 1);
  191. lua_pushboolean(L, instance->mkdir(arg) ? 1 : 0);
  192. return 1;
  193. }
  194. int w_remove(lua_State *L)
  195. {
  196. const char *arg = luaL_checkstring(L, 1);
  197. lua_pushboolean(L, instance->remove(arg) ? 1 : 0);
  198. return 1;
  199. }
  200. int w_read(lua_State *L)
  201. {
  202. const char *filename = luaL_checkstring(L, 1);
  203. int64 len = (int64) luaL_optinteger(L, 2, File::ALL);
  204. Data *data = 0;
  205. try
  206. {
  207. data = instance->read(filename, len);
  208. }
  209. catch (love::Exception &e)
  210. {
  211. return luaL_error(L, "%s", e.what());
  212. }
  213. if (data == 0)
  214. return luaL_error(L, "File could not be read.");
  215. // Push the string.
  216. lua_pushlstring(L, (const char *) data->getData(), data->getSize());
  217. // Push the size.
  218. lua_pushinteger(L, data->getSize());
  219. // Lua has a copy now, so we can free it.
  220. data->release();
  221. return 2;
  222. }
  223. static int w_write_or_append(lua_State *L, File::Mode mode)
  224. {
  225. const char *filename = luaL_checkstring(L, 1);
  226. const char *input = 0;
  227. size_t len = 0;
  228. if (luax_istype(L, 2, DATA_T))
  229. {
  230. love::Data *data = luax_totype<love::Data>(L, 2, "Data", DATA_T);
  231. input = (const char *) data->getData();
  232. len = data->getSize();
  233. }
  234. else if (lua_isstring(L, 2))
  235. input = lua_tolstring(L, 2, &len);
  236. else
  237. return luaL_argerror(L, 2, "string or Data expected");
  238. // Get how much we should write. Length of string default.
  239. len = luaL_optinteger(L, 3, len);
  240. try
  241. {
  242. if (mode == File::APPEND)
  243. instance->append(filename, (const void *) input, len);
  244. else
  245. instance->write(filename, (const void *) input, len);
  246. }
  247. catch (love::Exception &e)
  248. {
  249. return luaL_error(L, "%s", e.what());
  250. }
  251. luax_pushboolean(L, true);
  252. return 1;
  253. }
  254. int w_write(lua_State *L)
  255. {
  256. return w_write_or_append(L, File::WRITE);
  257. }
  258. int w_append(lua_State *L)
  259. {
  260. return w_write_or_append(L, File::APPEND);
  261. }
  262. int w_enumerate(lua_State *L)
  263. {
  264. return instance->enumerate(L);
  265. }
  266. int w_lines(lua_State *L)
  267. {
  268. File *file;
  269. if (lua_isstring(L, 1))
  270. {
  271. file = instance->newFile(lua_tostring(L, 1));
  272. try
  273. {
  274. if (!file->open(File::READ))
  275. return luaL_error(L, "Could not open file.");
  276. }
  277. catch(love::Exception &e)
  278. {
  279. return luaL_error(L, "%s", e.what());
  280. }
  281. luax_newtype(L, "File", FILESYSTEM_FILE_T, file);
  282. }
  283. else
  284. return luaL_error(L, "Expected filename.");
  285. lua_pushcclosure(L, Filesystem::lines_i, 1);
  286. return 1;
  287. }
  288. int w_load(lua_State *L)
  289. {
  290. std::string filename = std::string(luaL_checkstring(L, 1));
  291. Data *data = 0;
  292. try
  293. {
  294. data = instance->read(filename.c_str());
  295. }
  296. catch (love::Exception &e)
  297. {
  298. return luaL_error(L, "%s", e.what());
  299. }
  300. int status = luaL_loadbuffer(L, (const char *)data->getData(), data->getSize(), ("@" + filename).c_str());
  301. data->release();
  302. // Load the chunk, but don't run it.
  303. switch (status)
  304. {
  305. case LUA_ERRMEM:
  306. return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
  307. case LUA_ERRSYNTAX:
  308. return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
  309. default: // success
  310. return 1;
  311. }
  312. }
  313. int w_getLastModified(lua_State *L)
  314. {
  315. const char *filename = luaL_checkstring(L, 1);
  316. int64 time = 0;
  317. try
  318. {
  319. time = instance->getLastModified(filename);
  320. }
  321. catch (love::Exception &e)
  322. {
  323. lua_pushnil(L);
  324. lua_pushstring(L, e.what());
  325. return 2;
  326. }
  327. lua_pushnumber(L, static_cast<lua_Number>(time));
  328. return 1;
  329. }
  330. int w_getSize(lua_State *L)
  331. {
  332. const char *filename = luaL_checkstring(L, 1);
  333. int64 size = -1;
  334. try
  335. {
  336. size = instance->getSize(filename);
  337. }
  338. catch (love::Exception &e)
  339. {
  340. return luaL_error(L, "%s", e.what());
  341. }
  342. // Error on failure or if size does not fit into a double precision floating-point number.
  343. if (size == -1)
  344. return luaL_error(L, "Could not determine file size.");
  345. else if (size >= 0x20000000000000LL)
  346. return luaL_error(L, "Size too large to fit into a Lua number!");
  347. lua_pushnumber(L, (lua_Number) size);
  348. return 1;
  349. }
  350. int loader(lua_State *L)
  351. {
  352. const char *filename = lua_tostring(L, -1);
  353. std::string tmp(filename);
  354. tmp += ".lua";
  355. int size = tmp.size();
  356. for (int i=0; i<size-4; i++)
  357. {
  358. if (tmp[i] == '.')
  359. {
  360. tmp[i] = '/';
  361. }
  362. }
  363. // Check whether file exists.
  364. if (instance->exists(tmp.c_str()))
  365. {
  366. lua_pop(L, 1);
  367. lua_pushstring(L, tmp.c_str());
  368. // Ok, load it.
  369. return w_load(L);
  370. }
  371. tmp = filename;
  372. size = tmp.size();
  373. for (int i=0; i<size; i++)
  374. {
  375. if (tmp[i] == '.')
  376. {
  377. tmp[i] = '/';
  378. }
  379. }
  380. if (instance->isDirectory(tmp.c_str()))
  381. {
  382. tmp += "/init.lua";
  383. if (instance->exists(tmp.c_str()))
  384. {
  385. lua_pop(L, 1);
  386. lua_pushstring(L, tmp.c_str());
  387. // Ok, load it.
  388. return w_load(L);
  389. }
  390. }
  391. lua_pushfstring(L, "\n\tno file \"%s\" in LOVE game directories.\n", (tmp + ".lua").c_str());
  392. return 1;
  393. }
  394. inline const char *library_extension()
  395. {
  396. #ifdef LOVE_WINDOWS
  397. return ".dll";
  398. #else
  399. return ".so";
  400. #endif
  401. }
  402. int extloader(lua_State *L)
  403. {
  404. const char *filename = lua_tostring(L, -1);
  405. std::string tokenized_name(filename);
  406. std::string tokenized_function(filename);
  407. for (unsigned int i = 0; i < tokenized_name.size(); i++)
  408. {
  409. if (tokenized_name[i] == '.')
  410. {
  411. tokenized_name[i] = '/';
  412. tokenized_function[i] = '_';
  413. }
  414. }
  415. tokenized_name += library_extension();
  416. void *handle = SDL_LoadObject((std::string(instance->getAppdataDirectory()) + LOVE_PATH_SEPARATOR LOVE_APPDATA_FOLDER LOVE_PATH_SEPARATOR + tokenized_name).c_str());
  417. if (!handle && instance->isRelease())
  418. handle = SDL_LoadObject((std::string(instance->getSaveDirectory()) + LOVE_PATH_SEPARATOR + tokenized_name).c_str());
  419. if (!handle)
  420. {
  421. lua_pushfstring(L, "\n\tno extension \"%s\" in LOVE paths.\n", filename);
  422. return 1;
  423. }
  424. void *func = SDL_LoadFunction(handle, ("loveopen_" + tokenized_function).c_str());
  425. if (!func)
  426. func = SDL_LoadFunction(handle, ("luaopen_" + tokenized_function).c_str());
  427. if (!func)
  428. {
  429. SDL_UnloadObject(handle);
  430. lua_pushfstring(L, "\n\textension \"%s\" is incompatible.\n", filename);
  431. return 1;
  432. }
  433. lua_pushcfunction(L, (lua_CFunction) func);
  434. return 1;
  435. }
  436. // List of functions to wrap.
  437. static const luaL_Reg functions[] =
  438. {
  439. { "init", w_init },
  440. { "setRelease", w_setRelease },
  441. { "setIdentity", w_setIdentity },
  442. { "getIdentity", w_getIdentity },
  443. { "setSource", w_setSource },
  444. { "newFile", w_newFile },
  445. { "getWorkingDirectory", w_getWorkingDirectory },
  446. { "getUserDirectory", w_getUserDirectory },
  447. { "getAppdataDirectory", w_getAppdataDirectory },
  448. { "getSaveDirectory", w_getSaveDirectory },
  449. { "exists", w_exists },
  450. { "isDirectory", w_isDirectory },
  451. { "isFile", w_isFile },
  452. { "mkdir", w_mkdir },
  453. { "remove", w_remove },
  454. { "read", w_read },
  455. { "write", w_write },
  456. { "append", w_append },
  457. { "enumerate", w_enumerate },
  458. { "lines", w_lines },
  459. { "load", w_load },
  460. { "getLastModified", w_getLastModified },
  461. { "getSize", w_getSize },
  462. { "newFileData", w_newFileData },
  463. { 0, 0 }
  464. };
  465. static const lua_CFunction types[] =
  466. {
  467. luaopen_file,
  468. luaopen_filedata,
  469. 0
  470. };
  471. extern "C" int luaopen_love_filesystem(lua_State *L)
  472. {
  473. if (instance == 0)
  474. {
  475. try
  476. {
  477. instance = new Filesystem();
  478. love::luax_register_searcher(L, loader, 1);
  479. love::luax_register_searcher(L, extloader, 2);
  480. }
  481. catch(Exception &e)
  482. {
  483. return luaL_error(L, e.what());
  484. }
  485. }
  486. else
  487. {
  488. instance->retain();
  489. love::luax_register_searcher(L, loader, 1);
  490. love::luax_register_searcher(L, extloader, 2);
  491. }
  492. WrappedModule w;
  493. w.module = instance;
  494. w.name = "filesystem";
  495. w.flags = MODULE_FILESYSTEM_T;
  496. w.functions = functions;
  497. w.types = types;
  498. return luax_register_module(L, w);
  499. }
  500. } // physfs
  501. } // filesystem
  502. } // love