载入中...
搜索中...
未找到
Filesystem.cpp
浏览该文件的文档.
1
3
4#include <sys/stat.h>
5#include <sys/types.h>
6
7#include <cstring>
8#include <simplesquirrel/simplesquirrel.hpp>
9
10#include "common/Assert.h"
12
13#if defined(EVENGINE_MACOSX)
14#include "macosx/macosx.h"
15#elif defined(EVENGINE_IOS)
16#include "ios/ios.h"
17#elif defined(EVENGINE_ANDROID)
18#include "android/android.h"
19#elif defined(EVENGINE_WEBGPU)
20#include "webgpu/webplatform.h"
21#elif defined(EVENGINE_WINDOWS)
22#include <windows.h>
23
24#include "common/utf8.h"
25#elif defined(EVENGINE_LINUX)
26#include <unistd.h>
27#endif
28
29namespace eve {
30namespace filesystem {
31
33
34void Filesystem::expose(ssq::Table &table) {
35 auto cls = table.addClass(name, Filesystem::create, false);
36 expose(cls);
37}
38
39void Filesystem::expose(ssq::Class &cls) {
40 cls.addFunc("getName", &Filesystem::getName);
41 cls.addFunc("setFused", &Filesystem::setFused);
42 cls.addFunc("isFused", &Filesystem::isFused);
43 cls.addFunc("setupWriteDirectory", &Filesystem::setupWriteDirectory);
44 cls.addFunc("setAndroidSaveExternal", &Filesystem::setAndroidSaveExternal);
45 cls.addFunc("isAndroidSaveExternal", &Filesystem::isAndroidSaveExternal);
46 cls.addFunc("setIdentity", &Filesystem::setIdentity);
47 cls.addFunc("getIdentity", &Filesystem::getIdentity);
48 cls.addFunc("setSource", &Filesystem::setSource);
49 cls.addFunc("getSource", &Filesystem::getSource);
50 cls.addFunc("allowMountingForPath", &Filesystem::allowMountingForPath);
51 cls.addFunc("mountPath", [](Filesystem *self, const std::string &archive,
52 const std::string &mountpoint, bool appendToPath) {
53 return self && self->mount(archive, mountpoint, appendToPath);
54 });
55 cls.addFunc("unmountPath", [](Filesystem *self, const std::string &archive) {
56 return self && self->unmount(archive);
57 });
58 cls.addFunc("newFile", &Filesystem::newFile);
59 cls.addFunc("newFileData", &Filesystem::newFileData);
60 cls.addFunc("getWorkingDirectory", &Filesystem::getWorkingDirectory);
61 cls.addFunc("getUserDirectory", &Filesystem::getUserDirectory);
62 cls.addFunc("getAppdataDirectory", &Filesystem::getAppdataDirectory);
63 cls.addFunc("getSaveDirectory", &Filesystem::getSaveDirectory);
64 cls.addFunc("getSourceBaseDirectory", &Filesystem::getSourceBaseDirectory);
65 cls.addFunc("getRealDirectory", &Filesystem::getRealDirectory);
66 cls.addFunc("getExecutablePath", &Filesystem::getExecutablePath);
67 cls.addFunc("createDirectory", &Filesystem::createDirectory);
68 cls.addFunc("remove", &Filesystem::remove);
69 cls.addFunc("read", &Filesystem::read);
70 cls.addFunc("write", &Filesystem::write);
71 cls.addFunc("append", &Filesystem::append);
72 cls.addFunc("getDirectoryItems", &Filesystem::getDirectoryItems);
73 cls.addFunc("setSymlinksEnabled", &Filesystem::setSymlinksEnabled);
74 cls.addFunc("areSymlinksEnabled", &Filesystem::areSymlinksEnabled);
75 cls.addFunc("getRequirePath", &Filesystem::getRequirePath);
76 cls.addFunc("getCRequirePath", &Filesystem::getCRequirePath);
77 cls.addFunc("getRequirePath", &Filesystem::getRequirePath);
78 cls.addFunc("isRealDirectory", &Filesystem::isRealDirectory);
79 cls.addFunc("watch", &Filesystem::watch);
80 cls.addFunc("unwatch", &Filesystem::unwatch);
81 cls.addFunc("unwatchAll", &Filesystem::unwatchAll);
82 cls.addFunc("getWatchCount", &Filesystem::getWatchCount);
83 cls.addFunc("pollWatch", &Filesystem::pollWatch);
84 cls.addFunc("getLastWatchPath", &Filesystem::getLastWatchPath);
85 cls.addFunc("getLastWatchRealPath", &Filesystem::getLastWatchRealPath);
86}
87
88FileData *Filesystem::newFileData(const void *data, std::string filename, size_t size) const {
89 const bool validData = size == 0 || data != nullptr;
90 EV_PARAM_CHECK(validData, "file data must not be null when size > 0");
91 FileData *fd = new FileData(std::string(filename), size);
92 memcpy(fd->getData(), data, size);
93 return fd;
94}
95
96bool Filesystem::isRealDirectory(const std::string &path) const {
97#ifdef EVENGINE_WINDOWS
98 // make sure non-ASCII paths work.
99 std::wstring wpath = to_widestr(path);
100
101 struct _stat buf;
102 if (_wstat(wpath.c_str(), &buf) != 0) return false;
103 return (buf.st_mode & _S_IFDIR) == _S_IFDIR;
104#else
105 // Assume POSIX support...
106 struct stat buf;
107 if (stat(path.c_str(), &buf) != 0) return false;
108 return S_ISDIR(buf.st_mode) != 0;
109#endif
110}
111
112std::string Filesystem::getExecutablePath() const {
113#if defined(EVENGINE_MACOSX)
114 return macosx::getExecutablePath();
115#elif defined(EVENGINE_IOS)
116 return ios::getExecutablePath();
117#elif defined(EVENGINE_ANDROID)
118 return android::getExecutablePath();
119#elif defined(EVENGINE_WEBGPU)
120 return eve::webgpu_platform::getExecutablePath();
121#elif defined(EVENGINE_WINDOWS)
122 wchar_t buffer[MAX_PATH + 1] = {0};
123 if (GetModuleFileNameW(nullptr, buffer, MAX_PATH) == 0) return "";
124 return to_utf8(buffer);
125#elif defined(EVENGINE_LINUX)
126 char buffer[2048] = {0};
127 ssize_t len = readlink("/proc/self/exe", buffer, 2048);
128 if (len <= 0) return "";
129 return std::string(buffer, len);
130#else
131#error Missing implementation for Filesystem::getExecutablePath!
132#endif
133}
134
135} // namespace filesystem
136} // namespace eve
EVEngine assertion entry point, backed by zeroerr.
#define EV_PARAM_CHECK(cond,...)
Validate a function parameter / public API precondition.
Definition Assert.h:30
HSQOBJECT cls
Definition ECS.cpp:21
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
Light2D::Data * data
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
Data buffer paired with a filename (used for type identification).
Definition FileData.h:12
void * getData() const
Gets a pointer to the data. This pointer will obviously not be valid if the Data object is destroyed.
Definition FileData.h:23
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 bool isAndroidSaveExternal() const
Gets whether the Android save is external. Returns a bool.
Definition Filesystem.h:69
virtual std::string getUserDirectory()=0
Gets the user home directory.
virtual bool setSource(std::string source)=0
Sets the path to the game source. This can only be set once.
virtual std::string getExecutablePath() const
Gets the full platform-dependent path to the executable.
virtual bool setIdentity(std::string ident, bool appendToPath=false)=0
Sets the name of the save folder.
virtual std::vector< std::string > & getRequirePath()=0
virtual std::string getLastWatchRealPath() const =0
virtual FileData * newFileData(const void *data, std::string filename, size_t size) const
Creates a new FileData object. Data will be copied.
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.
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().
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 isRealDirectory(const std::string &path) const
Gets whether the given full (OS-dependent) path is a directory.
virtual std::string getLastWatchPath() const =0
virtual bool remove(std::string file)=0
Removes a file (or directory).
virtual bool createDirectory(std::string dir)=0
Creates a directory. Write dir must be set.
virtual std::string getIdentity() const =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.
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 setAndroidSaveExternal(bool useExternal=false)
This sets the save location on Android. False for internal, true for external
Definition Filesystem.h:63
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 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...
PhysFS 文件系统后端实现(挂载/解挂、读写、热重载)。
Definition Filesystem.h:19
Definition Build.cpp:11