Filesystem.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Copyright (c) 2006-2020 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_FILESYSTEM_H
  21. #define LOVE_FILESYSTEM_FILESYSTEM_H
  22. // LOVE
  23. #include "common/config.h"
  24. #include "common/Module.h"
  25. #include "common/int.h"
  26. #include "common/StringMap.h"
  27. #include "FileData.h"
  28. #include "File.h"
  29. // C++
  30. #include <string>
  31. #include <vector>
  32. // In Windows, we would like to use "LOVE" as the
  33. // application folder, but in Linux, we like .love.
  34. #define LOVE_APPDATA_PREFIX ""
  35. #ifdef LOVE_WINDOWS
  36. # define LOVE_APPDATA_FOLDER "LOVE"
  37. # define LOVE_PATH_SEPARATOR "/"
  38. # define LOVE_MAX_PATH _MAX_PATH
  39. #else
  40. # if defined(LOVE_MACOS) || defined(LOVE_IOS)
  41. # define LOVE_APPDATA_FOLDER "LOVE"
  42. # elif defined(LOVE_LINUX)
  43. # define LOVE_APPDATA_FOLDER "love"
  44. # else
  45. # define LOVE_APPDATA_PREFIX "."
  46. # define LOVE_APPDATA_FOLDER "love"
  47. # endif
  48. # define LOVE_PATH_SEPARATOR "/"
  49. # define LOVE_MAX_PATH MAXPATHLEN
  50. #endif
  51. namespace love
  52. {
  53. namespace filesystem
  54. {
  55. class Filesystem : public Module
  56. {
  57. public:
  58. enum FileType
  59. {
  60. FILETYPE_FILE,
  61. FILETYPE_DIRECTORY,
  62. FILETYPE_SYMLINK,
  63. FILETYPE_OTHER,
  64. FILETYPE_MAX_ENUM
  65. };
  66. enum CommonPath
  67. {
  68. COMMONPATH_APP_IDENTITY,
  69. COMMONPATH_APP_DOCUMENTS,
  70. COMMONPATH_APP_TEMP,
  71. COMMONPATH_USER_HOME,
  72. COMMONPATH_USER_APPDATA,
  73. COMMONPATH_USER_DESKTOP,
  74. COMMONPATH_USER_DOCUMENTS,
  75. COMMONPATH_MAX_ENUM
  76. };
  77. enum MountPermissions
  78. {
  79. MOUNT_PERMISSIONS_READ,
  80. MOUNT_PERMISSIONS_READWRITE,
  81. MOUNT_PERMISSIONS_MAX_ENUM
  82. };
  83. struct Info
  84. {
  85. // Numbers will be -1 if they cannot be determined.
  86. int64 size;
  87. int64 modtime;
  88. FileType type;
  89. bool readonly;
  90. };
  91. static love::Type type;
  92. Filesystem();
  93. virtual ~Filesystem();
  94. // Implements Module.
  95. virtual ModuleType getModuleType() const { return M_FILESYSTEM; }
  96. virtual void init(const char *arg0) = 0;
  97. virtual void setFused(bool fused) = 0;
  98. virtual bool isFused() const = 0;
  99. /**
  100. * This sets up the save directory. If the
  101. * it is already set up, nothing happens.
  102. * @return True on success, false otherwise.
  103. **/
  104. virtual bool setupWriteDirectory() = 0;
  105. /**
  106. * This sets the save location on Android.
  107. * False for internal, true for external
  108. * @param external Bool for whether
  109. * Android should use external file storage.
  110. **/
  111. virtual void setAndroidSaveExternal(bool useExternal = false);
  112. /**
  113. * Gets whether the Android save is external.
  114. * Returns a bool.
  115. **/
  116. virtual bool isAndroidSaveExternal() const;
  117. /**
  118. * Sets the name of the save folder.
  119. * @param ident The name of the game. Will be used to
  120. * to create the folder in the LOVE data folder.
  121. **/
  122. virtual bool setIdentity(const char *ident, bool appendToPath = false) = 0;
  123. virtual const char *getIdentity() const = 0;
  124. /**
  125. * Sets the path to the game source.
  126. * This can only be set once.
  127. * @param source Path to a directory or a .love-file.
  128. **/
  129. virtual bool setSource(const char *source) = 0;
  130. /**
  131. * Gets the path to the game source.
  132. * Returns a 0-length string if the source has not been set.
  133. **/
  134. virtual const char *getSource() const = 0;
  135. virtual bool mount(const char *archive, const char *mountpoint, bool appendToPath = false) = 0;
  136. virtual bool mount(Data *data, const char *archivename, const char *mountpoint, bool appendToPath = false) = 0;
  137. virtual bool mountFullPath(const char *archive, const char *mountpoint, MountPermissions permissions, bool appendToPath = false) = 0;
  138. virtual bool mountCommonPath(CommonPath path, const char *mountpoint, MountPermissions permissions, bool appendToPath = false) = 0;
  139. virtual bool unmount(const char *archive) = 0;
  140. virtual bool unmount(Data *data) = 0;
  141. virtual bool unmount(CommonPath path) = 0;
  142. /**
  143. * Creates a new file.
  144. **/
  145. virtual File *newFile(const char *filename) const = 0;
  146. /**
  147. * Creates a new FileData object. Data will be copied.
  148. * @param data Pointer to the data.
  149. * @param size The size of the data.
  150. * @param filename The full filename used to file type identification.
  151. **/
  152. virtual FileData *newFileData(const void *data, size_t size, const char *filename) const;
  153. virtual std::string getFullCommonPath(CommonPath path) = 0;
  154. /**
  155. * Gets the current working directory.
  156. **/
  157. virtual const char *getWorkingDirectory() = 0;
  158. /**
  159. * Gets the user home directory.
  160. **/
  161. virtual std::string getUserDirectory() = 0;
  162. /**
  163. * Gets the APPDATA directory. On Windows, this is the folder
  164. * in the %APPDATA% enviroment variable. On Linux, this is the
  165. * user home folder.
  166. **/
  167. virtual std::string getAppdataDirectory() = 0;
  168. /**
  169. * Gets the full path of the save folder.
  170. **/
  171. virtual const char *getSaveDirectory() = 0;
  172. /**
  173. * Gets the full path to the directory containing the game source.
  174. * For example if the game source is C:\Games\mygame.love, this will return
  175. * C:\Games.
  176. **/
  177. virtual std::string getSourceBaseDirectory() const = 0;
  178. /**
  179. * Gets the real directory path containing the file.
  180. **/
  181. virtual std::string getRealDirectory(const char *filename) const = 0;
  182. /**
  183. * Gets information about the item at the specified filepath. Returns false
  184. * if nothing exists at the path.
  185. **/
  186. virtual bool getInfo(const char *filepath, Info &info) const = 0;
  187. /**
  188. * Creates a directory. Write dir must be set.
  189. * @param dir The directory to create.
  190. **/
  191. virtual bool createDirectory(const char *dir) = 0;
  192. /**
  193. * Removes a file (or directory).
  194. * @param file The file or directory to remove.
  195. **/
  196. virtual bool remove(const char *file) = 0;
  197. /**
  198. * Reads data from a file.
  199. * @param filename The name of the file to read from.
  200. * @param size The size in bytes of the data to read.
  201. **/
  202. virtual FileData *read(const char *filename, int64 size = File::ALL) const = 0;
  203. /**
  204. * Write data to a file.
  205. * @param filename The name of the file to write to.
  206. * @param data The data to write.
  207. * @param size The size in bytes of the data to write.
  208. **/
  209. virtual void write(const char *filename, const void *data, int64 size) const = 0;
  210. /**
  211. * Append data to a file, creating it if it doesn't exist.
  212. * @param filename The name of the file to write to.
  213. * @param data The data to append.
  214. * @param size The size in bytes of the data to append.
  215. **/
  216. virtual void append(const char *filename, const void *data, int64 size) const = 0;
  217. /**
  218. * This "native" method returns a table of all
  219. * files in a given directory.
  220. **/
  221. virtual bool getDirectoryItems(const char *dir, std::vector<std::string> &items) = 0;
  222. /**
  223. * Enable or disable symbolic link support in love.filesystem.
  224. **/
  225. virtual void setSymlinksEnabled(bool enable) = 0;
  226. /**
  227. * Gets whether symbolic link support is enabled.
  228. **/
  229. virtual bool areSymlinksEnabled() const = 0;
  230. // Require path accessors
  231. // Not const because it's R/W
  232. virtual std::vector<std::string> &getRequirePath() = 0;
  233. virtual std::vector<std::string> &getCRequirePath() = 0;
  234. /**
  235. * Allows a full (OS-dependent) path to be used with Filesystem::mount.
  236. **/
  237. virtual void allowMountingForPath(const std::string &path) = 0;
  238. /**
  239. * Gets whether the given full (OS-dependent) path is a directory.
  240. **/
  241. virtual bool isRealDirectory(const std::string &path) const;
  242. /**
  243. * Gets the full platform-dependent path to the executable.
  244. **/
  245. virtual std::string getExecutablePath() const;
  246. STRINGMAP_CLASS_DECLARE(FileType);
  247. STRINGMAP_CLASS_DECLARE(CommonPath);
  248. STRINGMAP_CLASS_DECLARE(MountPermissions);
  249. private:
  250. // Should we save external or internal for Android
  251. bool useExternal;
  252. }; // Filesystem
  253. } // filesystem
  254. } // love
  255. #endif // LOVE_FILESYSTEM_FILESYSTEM_H