Filesystem.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Copyright (c) 2006-2014 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. #ifndef LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
  21. #define LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H
  22. // STD
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <iostream>
  26. #include <string>
  27. // LOVE
  28. #include "common/Module.h"
  29. #include "common/config.h"
  30. #include "common/int.h"
  31. #include "common/runtime.h"
  32. #include "filesystem/FileData.h"
  33. #include "File.h"
  34. // For great CWD. (Current Working Directory)
  35. // Using this instead of boost::filesystem which totally
  36. // cramped our style.
  37. #ifdef LOVE_WINDOWS
  38. # include <windows.h>
  39. # include <direct.h>
  40. #else
  41. # include <sys/param.h>
  42. # include <unistd.h>
  43. #endif
  44. // In Windows, we would like to use "LOVE" as the
  45. // application folder, but in Linux, we like .love.
  46. #define LOVE_APPDATA_PREFIX ""
  47. #ifdef LOVE_WINDOWS
  48. # define LOVE_APPDATA_FOLDER "LOVE"
  49. # define LOVE_PATH_SEPARATOR "/"
  50. # define LOVE_MAX_PATH _MAX_PATH
  51. #else
  52. # ifdef LOVE_MACOSX
  53. # define LOVE_APPDATA_FOLDER "LOVE"
  54. # elif defined(LOVE_LINUX)
  55. # define LOVE_APPDATA_FOLDER "love"
  56. # else
  57. # define LOVE_APPDATA_PREFIX "."
  58. # define LOVE_APPDATA_FOLDER "love"
  59. # endif
  60. # define LOVE_PATH_SEPARATOR "/"
  61. # define LOVE_MAX_PATH MAXPATHLEN
  62. #endif
  63. namespace love
  64. {
  65. namespace filesystem
  66. {
  67. namespace physfs
  68. {
  69. class Filesystem : public Module
  70. {
  71. public:
  72. Filesystem();
  73. virtual ~Filesystem();
  74. // Implements Module.
  75. virtual ModuleType getModuleType() const { return M_FILESYSTEM; }
  76. const char *getName() const;
  77. void init(const char *arg0);
  78. void setFused(bool fused);
  79. bool isFused() const;
  80. /**
  81. * This sets up the save directory. If the
  82. * it is already set up, nothing happens.
  83. * @return True on success, false otherwise.
  84. **/
  85. bool setupWriteDirectory();
  86. /**
  87. * Sets the name of the save folder.
  88. * @param ident The name of the game. Will be used to
  89. * to create the folder in the LOVE data folder.
  90. **/
  91. bool setIdentity(const char *ident, bool appendToPath = false);
  92. const char *getIdentity() const;
  93. /**
  94. * Sets the path to the game source.
  95. * This can only be set once.
  96. * @param source Path to a directory or a .love-file.
  97. **/
  98. bool setSource(const char *source);
  99. /**
  100. * Gets the path to the game source.
  101. * Returns a 0-length string if the source has not been set.
  102. **/
  103. const char *getSource() const;
  104. bool mount(const char *archive, const char *mountpoint, bool appendToPath = false);
  105. bool unmount(const char *archive);
  106. /**
  107. * Creates a new file.
  108. **/
  109. File *newFile(const char *filename) const;
  110. /**
  111. * Creates a new FileData object. Data will be copied.
  112. * @param data Pointer to the data.
  113. * @param size The size of the data.
  114. * @param filename The full filename used to file type identification.
  115. **/
  116. FileData *newFileData(void *data, unsigned int size, const char *filename) const;
  117. /**
  118. * Creates a new FileData object from base64 data.
  119. * @param b64 The base64 data.
  120. **/
  121. FileData *newFileData(const char *b64, const char *filename) const;
  122. /**
  123. * Gets the current working directory.
  124. **/
  125. const char *getWorkingDirectory();
  126. /**
  127. * Gets the user home directory.
  128. **/
  129. std::string getUserDirectory();
  130. /**
  131. * Gets the APPDATA directory. On Windows, this is the folder
  132. * in the %APPDATA% enviroment variable. On Linux, this is the
  133. * user home folder.
  134. **/
  135. std::string getAppdataDirectory();
  136. /**
  137. * Gets the full path of the save folder.
  138. **/
  139. const char *getSaveDirectory();
  140. /**
  141. * Gets the full path to the directory containing the game source.
  142. * For example if the game source is C:\Games\mygame.love, this will return
  143. * C:\Games.
  144. **/
  145. std::string getSourceBaseDirectory() const;
  146. /**
  147. * Gets the real directory path containing the file.
  148. **/
  149. std::string getRealDirectory(const char *filename) const;
  150. /**
  151. * Checks whether a file exists in the current search path
  152. * or not.
  153. * @param file The filename to check.
  154. **/
  155. bool exists(const char *file) const;
  156. /**
  157. * Checks if an existing file really is a directory.
  158. * @param file The filename to check.
  159. **/
  160. bool isDirectory(const char *file) const;
  161. /**
  162. * Checks if an existing file really is a file,
  163. * and not a directory.
  164. * @param file The filename to check.
  165. **/
  166. bool isFile(const char *file) const;
  167. /**
  168. * Creates a directory. Write dir must be set.
  169. * @param dir The directory to create.
  170. **/
  171. bool createDirectory(const char *dir);
  172. /**
  173. * Removes a file (or directory).
  174. * @param file The file or directory to remove.
  175. **/
  176. bool remove(const char *file);
  177. /**
  178. * Reads data from a file.
  179. * @param filename The name of the file to read from.
  180. * @param size The size in bytes of the data to read.
  181. **/
  182. Data *read(const char *filename, int64 size = File::ALL) const;
  183. /**
  184. * Write data to a file.
  185. * @param filename The name of the file to write to.
  186. * @param data The data to write.
  187. * @param size The size in bytes of the data to write.
  188. **/
  189. void write(const char *filename, const void *data, int64 size) const;
  190. /**
  191. * Append data to a file, creating it if it doesn't exist.
  192. * @param filename The name of the file to write to.
  193. * @param data The data to append.
  194. * @param size The size in bytes of the data to append.
  195. **/
  196. void append(const char *filename, const void *data, int64 size) const;
  197. /**
  198. * This "native" method returns a table of all
  199. * files in a given directory.
  200. **/
  201. int getDirectoryItems(lua_State *L);
  202. /**
  203. * Gets the last modification time of a file, in seconds
  204. * since the Unix epoch.
  205. * @param filename The name of the file.
  206. **/
  207. int64 getLastModified(const char *filename) const;
  208. /**
  209. * Gets the size of a file in bytes.
  210. * @param filename The name of the file.
  211. **/
  212. int64 getSize(const char *filename) const;
  213. /**
  214. * Enable or disable symbolic link support in love.filesystem.
  215. **/
  216. void setSymlinksEnabled(bool enable);
  217. /**
  218. * Gets whether symbolic link support is enabled.
  219. **/
  220. bool areSymlinksEnabled() const;
  221. /**
  222. * Gets whether a filepath is actually a symlink.
  223. * Always returns false if symlinks are not enabled.
  224. **/
  225. bool isSymlink(const char *filename) const;
  226. /**
  227. * Text file line-reading iterator function used and
  228. * pushed on the Lua stack by love.filesystem.lines
  229. * and File:lines.
  230. **/
  231. static int lines_i(lua_State *L);
  232. private:
  233. // Contains the current working directory (UTF8).
  234. std::string cwd;
  235. // %APPDATA% on Windows.
  236. std::string appdata;
  237. // This name will be used to create the folder
  238. // in the appdata/userdata folder.
  239. std::string save_identity;
  240. // Full and relative paths of the game save folder.
  241. // (Relative to the %APPDATA% folder, meaning that the
  242. // relative string will look something like: ./LOVE/game)
  243. std::string save_path_relative, save_path_full;
  244. // The full path to the source of the game.
  245. std::string game_source;
  246. // Workaround for machines without PhysFS 2.0
  247. bool initialized;
  248. // Allow saving outside of the LOVE_APPDATA_FOLDER
  249. // for release 'builds'
  250. bool fused;
  251. bool fusedSet;
  252. }; // Filesystem
  253. } // physfs
  254. } // filesystem
  255. } // love
  256. #endif // LOVE_FILESYSTEM_PHYSFS_FILESYSTEM_H