载入中...
搜索中...
未找到
Filesystem.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include "common/Capability.h"
10#include "common/Exception.h"
11#include "common/Module.h"
12#include "common/Result.h"
14#include "common/config.h"
15#include "filesystem/File.h"
16#include "filesystem/FileData.h"
17
18#include <cstdint>
19
20#ifdef EVENGINE_WINDOWS
21#define EVENGINE_APPDATA_PREFIX ""
22#define EVENGINE_APPDATA_FOLDER "EVE"
23#define EVENGINE_PATH_SEPARATOR "/"
24#define EVENGINE_MAX_PATH _MAX_PATH
25#else
26#if defined(EVENGINE_MACOSX) || defined(EVENGINE_IOS)
27#define EVENGINE_APPDATA_PREFIX ""
28#define EVENGINE_APPDATA_FOLDER "EVE"
29#elif defined(EVENGINE_LINUX)
30#define EVENGINE_APPDATA_PREFIX ""
31#define EVENGINE_APPDATA_FOLDER "eve"
32#else
33#define EVENGINE_APPDATA_PREFIX "."
34#define EVENGINE_APPDATA_FOLDER "eve"
35#endif
36#define EVENGINE_PATH_SEPARATOR "/"
37#define EVENGINE_MAX_PATH MAXPATHLEN
38#endif
39
40namespace eve::filesystem {
41
44public:
46
48 struct Info {
49 // Numbers will be -1 if they cannot be determined.
50 int64_t size;
51 int64_t modtime;
52 std::string type; // file, directory, symlink, other
53 };
54
55 Filesystem() { eve::cap::provide<eve::service::IFileSystem>(this); }
56 virtual ~Filesystem() {}
57
58 bool readFile(const std::string &path, std::vector<uint8_t> &out) override {
59 try {
60 FileData *fd = read(path);
61 if (!fd) return false;
62 const auto *data = static_cast<const uint8_t *>(fd->getData());
63 out.assign(data, data + fd->getSize());
64 delete fd;
65 return true;
66 } catch (const eve::Exception &) {
67 return false;
68 }
69 }
70
71 bool writeFile(const std::string &path, const void *data, size_t size) override {
72 try {
73 write(path, data, int64_t(size));
74 return true;
75 } catch (const eve::Exception &) {
76 return false;
77 }
78 }
79
80 bool fileExists(const std::string &path) override {
81 Info info;
82 return getInfo(path, info);
83 }
84
85 virtual void init(const char* arg0) = 0;
86
87 virtual void setFused(bool fused) = 0;
88 virtual bool isFused() const = 0;
89
95 virtual bool setupWriteDirectory() = 0;
96
103 virtual void setAndroidSaveExternal(bool external = false) { this->useExternal = external; }
104
109 virtual bool isAndroidSaveExternal() const { return useExternal; }
110
116 virtual bool setIdentity(std::string ident, bool appendToPath = false) = 0;
117 virtual std::string getIdentity() const = 0;
118
126 virtual bool setSource(std::string source) = 0;
127
136 virtual bool setSourceFromMemory(const void* data, size_t size) = 0;
137
142 virtual std::string getSource() const = 0;
143
149 virtual bool mount(std::string archive, std::string mountpoint, bool appendToPath = false) = 0;
150 virtual bool mount(Data *data, std::string archivename, std::string mountpoint, bool appendToPath = false) = 0;
151
160 virtual bool mountRealDirectory(std::string realDir, std::string mountpoint, bool appendToPath = false) = 0;
161
163 virtual bool unmountRealDirectory(std::string realDir) = 0;
164
166 virtual bool unmount(std::string archive) = 0;
168 virtual bool unmount(Data *data) = 0;
169
173 virtual File *newFile(std::string filename) const = 0;
174
181 virtual FileData *newFileData(const void *data, std::string filename, size_t size) const;
182
186 virtual std::string getWorkingDirectory() = 0;
187
191 virtual std::string getUserDirectory() = 0;
192
198 virtual std::string getAppdataDirectory() = 0;
199
203 virtual std::string getSaveDirectory() = 0;
204
210 virtual std::string getSourceBaseDirectory() const = 0;
211
215 virtual std::string getRealDirectory(std::string filename) const = 0;
216
221 virtual bool getInfo(std::string filepath, Info &info) const = 0;
222
227 virtual bool createDirectory(std::string dir) = 0;
228
233 virtual bool remove(std::string file) = 0;
234
240 virtual FileData *read(std::string filename, int64_t size = File::ALL) const = 0;
241
248 virtual void write(std::string filename, const void *data, int64_t size) const = 0;
249
256 virtual void append(std::string filename, const void *data, int64_t size) const = 0;
257
264 bool writeText(const std::string &filename, const std::string &text) const;
265
277 [[nodiscard]] eve::Result<void> writeTextAtomic(std::string_view filename,
278 std::string_view text);
279
285 std::string readText(const std::string &filename) const;
286
291 virtual std::vector<std::string> getDirectoryItems(std::string dir) = 0;
292
296 virtual void setSymlinksEnabled(bool enable) = 0;
297
301 virtual bool areSymlinksEnabled() const = 0;
302
303 // Require path accessors
304 // Not const because it's R/W
305 virtual std::vector<std::string> &getRequirePath() = 0;
306 virtual std::vector<std::string> &getCRequirePath() = 0;
307
311 virtual void allowMountingForPath(const std::string &path) = 0;
312
322 bool mountExternalReadOnly(const std::string &path, const std::string &mountpoint);
323
327 virtual bool isRealDirectory(const std::string &path) const;
328
332 virtual std::string getExecutablePath() const;
333
334 // --- File watch (backend-specific; main-thread poll) ---
335
342 virtual bool watch(std::string path) = 0;
343
353 [[nodiscard]] virtual eve::Result<void> watchRealDirectory(std::string realDir,
354 std::string reportPath) = 0;
355
357 virtual bool unwatch(std::string path) = 0;
358 virtual void unwatchAll() = 0;
359 virtual int getWatchCount() const = 0;
360
365 virtual std::string pollWatch() = 0;
366 virtual std::string getLastWatchPath() const = 0;
367 virtual std::string getLastWatchRealPath() const = 0;
368
369private:
370 // Should we save external or internal for Android
371 bool useExternal = false;
372}; // Filesystem
373
374} // namespace eve::filesystem
filesystem::File * file
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::string text
std::string path
Definition PlayHost.cpp:110
Move-only, checked operation results for the common layer.
float size
Definition TreeMesh.cpp:156
V3 dir
Definition TreeMesh.cpp:150
const UnitySourceAsset & source
Data public API.
Definition Data.h:8
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Data buffer paired with a filename (used for type identification).
Definition FileData.h:14
size_t getSize() const
Byte length of the owned buffer.
Definition FileData.h:29
void * getData() const
Pointer to the owned byte buffer.
Definition FileData.h:27
A File interface, providing generic means of reading from and writing to files.
Definition File.h:14
EVENGINE_API_FOUNDATION public API.
Definition Filesystem.h:43
virtual eve::Result< void > watchRealDirectory(std::string realDir, std::string reportPath)=0
Watch a real OS directory and report events under a virtual-relative path.
virtual bool unmount(std::string archive)=0
Unmounts a previously mounted archive.
virtual std::string getSourceBaseDirectory() const =0
Gets the full path to the directory containing the game source. For example if the game source is C:\...
virtual void allowMountingForPath(const std::string &path)=0
Allows a full (OS-dependent) path to be used with Filesystem::mount.
virtual void init(const char *arg0)=0
virtual bool unmount(Data *data)=0
Unmounts a previously mounted in-memory archive.
virtual bool isAndroidSaveExternal() const
Gets whether the Android save is external. Returns a bool.
Definition Filesystem.h:109
virtual std::string getUserDirectory()=0
Gets the user home directory.
virtual bool getInfo(std::string filepath, Info &info) const =0
Gets information about the item at the specified filepath. Returns false if nothing exists at the pat...
virtual bool mount(Data *data, std::string archivename, std::string mountpoint, bool appendToPath=false)=0
virtual bool setSource(std::string source)=0
Sets the path to the game source. This can only be set once.
virtual bool setIdentity(std::string ident, bool appendToPath=false)=0
Sets the name of the save folder.
virtual bool mountRealDirectory(std::string realDir, std::string mountpoint, bool appendToPath=false)=0
virtual std::vector< std::string > & getRequirePath()=0
virtual std::string getLastWatchRealPath() const =0
virtual int getWatchCount() const =0
virtual File * newFile(std::string filename) const =0
Creates a new file.
virtual std::string getWorkingDirectory()=0
Gets the current working directory.
virtual void setFused(bool fused)=0
virtual std::vector< std::string > getDirectoryItems(std::string dir)=0
This "native" method returns a table of all files in a given directory.
bool readFile(const std::string &path, std::vector< uint8_t > &out) override
Reads an entire file.
Definition Filesystem.h:58
virtual std::string pollWatch()=0
Pop next watch event kind: "added"|"removed"|"modified"|"movedFrom"|"movedTo". Empty string if queue ...
virtual void unwatchAll()=0
virtual bool unwatch(std::string path)=0
Stop watching a path previously passed to watch().
bool fileExists(const std::string &path) override
True when the path exists (file or directory).
Definition Filesystem.h:80
virtual std::string getRealDirectory(std::string filename) const =0
Gets the real directory path containing the file.
virtual bool isFused() const =0
virtual std::string getAppdataDirectory()=0
Gets the APPDATA directory. On Windows, this is the folder in the APPDATA% enviroment variable....
virtual std::string getSaveDirectory()=0
Gets the full path of the save folder.
virtual bool setSourceFromMemory(const void *data, size_t size)=0
Loads the game source from a packaged archive (.eve / zip) held entirely in memory and mounts it at "...
virtual std::string getLastWatchPath() const =0
virtual bool remove(std::string file)=0
Removes a file (or directory).
virtual bool mount(std::string archive, std::string mountpoint, bool appendToPath=false)=0
Mounts an archive (path or in-memory Data) at a virtual mountpoint.
virtual bool createDirectory(std::string dir)=0
Creates a directory. Write dir must be set.
virtual std::string getIdentity() const =0
virtual bool unmountRealDirectory(std::string realDir)=0
virtual bool areSymlinksEnabled() const =0
Gets whether symbolic link support is enabled.
virtual void write(std::string filename, const void *data, int64_t size) const =0
Write data to a file.
bool writeFile(const std::string &path, const void *data, size_t size) override
Writes bytes to a file.
Definition Filesystem.h:71
virtual bool setupWriteDirectory()=0
This sets up the save directory. If the it is already set up, nothing happens.
virtual FileData * read(std::string filename, int64_t size=File::ALL) const =0
Reads data from a file.
virtual void append(std::string filename, const void *data, int64_t size) const =0
Append data to a file, creating it if it doesn't exist.
virtual void setSymlinksEnabled(bool enable)=0
Enable or disable symbolic link support in love.filesystem.
virtual std::string getSource() const =0
Gets the path to the game source. Returns a 0-length string if the source has not been set.
virtual void setAndroidSaveExternal(bool external=false)
This sets the save location on Android. False for internal, true for external
Definition Filesystem.h:103
virtual std::vector< std::string > & getCRequirePath()=0
virtual bool watch(std::string path)=0
Watch a virtual or absolute OS path (file or directory). File watches monitor the parent directory an...
Minimal file read / write / existence surface. Providers: eve::filesystem::Filesystem....
glm::uvec4 info