Fission FSN
Loading...
Searching...
No Matches
fsCPackFileUtils.cpp
Go to the documentation of this file.
1#include "fsCPackFileUtils.h"
2
3#if defined fsPACK_FILE_UTILS
4
5#include <algorithm>
6#include <cstring>
7
8// Built as Objective-C++ on the Mac, which FileUtils-apple.h needs.
9#if AX_TARGET_PLATFORM == AX_PLATFORM_MAC
10 #include <platform/apple/FileUtils-apple.h>
11 using fsTPlatformFileUtils = ax::FileUtilsApple;
12#elif AX_TARGET_PLATFORM == AX_PLATFORM_WIN32
13 #include <platform/win32/FileUtils-win32.h>
14 using fsTPlatformFileUtils = ax::FileUtilsWin32;
15#else
16 #include <platform/linux/FileUtils-linux.h>
17 using fsTPlatformFileUtils = ax::FileUtilsLinux;
18#endif
19
21
22namespace
23{
24 // A packed file, already unscrambled into memory, for streaming readers such as audio.
25 class fsCPackFileStream : public ax::IFileStream
26 {
27 public:
28 explicit fsCPackFileStream(std::string pData) : mData(std::move(pData)) {}
29
30 bool open(std::string_view, ax::IFileStream::Mode) override { return false; }
31 int close() override
32 {
33 mOpen = false;
34 mData.clear();
35 return 0;
36 }
37 int64_t seek(int64_t pOffset, int pOrigin) const override
38 {
39 const int64_t base = pOrigin == SEEK_CUR ? mPos : (pOrigin == SEEK_END ? size() : 0);
40 const int64_t pos = base + pOffset;
41 if (pos < 0 || pos > size())
42 {
43 return -1;
44 }
45 mPos = pos;
46 return mPos;
47 }
48 int read(void* pBuf, unsigned int pSize) const override
49 {
50 const auto count = static_cast<int>(std::min<int64_t>(pSize, size() - mPos));
51 std::memcpy(pBuf, mData.data() + mPos, count);
52 mPos += count;
53 return count;
54 }
55 int write(const void*, unsigned int) const override { return -1; }
56 int64_t tell() const override { return mPos; }
57 int64_t size() const override { return static_cast<int64_t>(mData.size()); }
58 bool isOpen() const override { return mOpen; }
59
60 private:
61 std::string mData;
62 mutable int64_t mPos = 0;
63 bool mOpen = true;
64 };
65
66 class fsCPackFileUtilsImpl : public fsTPlatformFileUtils
67 {
68 public:
69 std::string fullPathForFilename(std::string_view pFilename) const override;
70 bool isFileExist(std::string_view pFilename) const override;
71 bool isDirectoryExistInternal(std::string_view pDirPath) const override;
72 Status getContents(std::string_view pFilename, ax::ResizableBuffer* pBuffer) const override;
73 int64_t getFileSize(std::string_view pFilepath) const override;
74 std::vector<std::string> listFiles(std::string_view pDirPath) const override;
75 std::unique_ptr<ax::IFileStream> openFileStream(std::string_view pFilePath, ax::IFileStream::Mode pMode) const override;
76
77 private:
78 // The Content-relative path of a packed file, if pPath names one.
79 bool packedPathGet(std::string_view pPath, std::string& pRelative) const;
80 };
81}
82
83void fsCPackFileUtils::installIfPacked()
84{
85 ax::FileUtils* const current = ax::FileUtils::getInstance();
86 const std::string root = current->getDefaultResourceRootPath();
87 if (!fsCAssetPack::mount(root))
88 {
89 return;
90 }
91 const auto searchPaths = current->getSearchPaths();
92 auto* const packed = new fsCPackFileUtilsImpl();
93 packed->init();
94 packed->setSearchPaths(searchPaths);
95 // No logging here: this runs before fission's debug output exists.
96 ax::FileUtils::setDelegate(packed); // deletes the platform instance
97}
98
99bool fsCPackFileUtilsImpl::packedPathGet(std::string_view pPath, std::string& pRelative) const
100{
101 std::string_view relative = pPath;
102 if (isAbsolutePath(pPath))
103 {
104 const std::string& root = getDefaultResourceRootPath();
105 if (pPath.size() <= root.size() || pPath.compare(0, root.size(), root) != 0)
106 {
107 return false;
108 }
109 relative = pPath.substr(root.size());
110 }
111 if (!fsCAssetPack::fileExistsGet(relative))
112 {
113 return false;
114 }
115 pRelative = relative;
116 return true;
117}
118
119// Packs hold the resource root, which is searched first, so a packed file wins
120// over the same name in a later search path, as the loose root file did.
121std::string fsCPackFileUtilsImpl::fullPathForFilename(std::string_view pFilename) const
122{
123 std::string relative;
124 if (!isAbsolutePath(pFilename) && packedPathGet(pFilename, relative))
125 {
126 return getDefaultResourceRootPath() + relative;
127 }
128 return fsTPlatformFileUtils::fullPathForFilename(pFilename);
129}
130
131bool fsCPackFileUtilsImpl::isFileExist(std::string_view pFilename) const
132{
133 std::string relative;
134 return packedPathGet(pFilename, relative) || fsTPlatformFileUtils::isFileExist(pFilename);
135}
136
137bool fsCPackFileUtilsImpl::isDirectoryExistInternal(std::string_view pDirPath) const
138{
139 const std::string& root = getDefaultResourceRootPath();
140 if (pDirPath.size() > root.size() && pDirPath.compare(0, root.size(), root) == 0 &&
141 fsCAssetPack::folderExistsGet(pDirPath.substr(root.size())))
142 {
143 return true;
144 }
145 return fsTPlatformFileUtils::isDirectoryExistInternal(pDirPath);
146}
147
148ax::FileUtils::Status fsCPackFileUtilsImpl::getContents(std::string_view pFilename, ax::ResizableBuffer* pBuffer) const
149{
150 std::string relative;
151 if (!packedPathGet(fullPathForFilename(pFilename), relative))
152 {
153 return fsTPlatformFileUtils::getContents(pFilename, pBuffer);
154 }
155 const int64_t size = fsCAssetPack::fileSizeGet(relative);
156#ifdef AXMOL_V3
157 // v3 sizes and fills the buffer in one call; the fill returns how many bytes it wrote.
158 bool read = true;
159 pBuffer->resize_and_overwrite(static_cast<size_t>(size), [&read, &relative](void* const pDest, const size_t pBytes)
160 {
161 read = pBytes == 0 || fsCAssetPack::fileRead(relative, pDest);
162 return read ? pBytes : size_t(0);
163 });
164 if (!read)
165 {
166 return Status::ReadFailed;
167 }
168#else
169 pBuffer->resize(static_cast<size_t>(size));
170 if (size > 0 && !fsCAssetPack::fileRead(relative, pBuffer->buffer()))
171 {
172 return Status::ReadFailed;
173 }
174#endif
175 return Status::OK;
176}
177
178int64_t fsCPackFileUtilsImpl::getFileSize(std::string_view pFilepath) const
179{
180 std::string relative;
181 return packedPathGet(fullPathForFilename(pFilepath), relative) ? fsCAssetPack::fileSizeGet(relative)
182 : fsTPlatformFileUtils::getFileSize(pFilepath);
183}
184
185std::vector<std::string> fsCPackFileUtilsImpl::listFiles(std::string_view pDirPath) const
186{
187 auto files = fsTPlatformFileUtils::listFiles(pDirPath);
188 const std::string& root = getDefaultResourceRootPath();
189 std::string folder = fullPathForDirectory(pDirPath);
190 if (folder.size() < root.size() || folder.compare(0, root.size(), root) != 0)
191 {
192 return files;
193 }
194 if (!folder.empty() && folder.back() != '/')
195 {
196 folder += '/';
197 }
198 for (const auto& child : fsCAssetPack::folderListGet(std::string_view(folder).substr(root.size())))
199 {
200 const std::string path = folder + child;
201 if (std::find(files.begin(), files.end(), path) == files.end())
202 {
203 files.push_back(path);
204 }
205 }
206 return files;
207}
208
209std::unique_ptr<ax::IFileStream> fsCPackFileUtilsImpl::openFileStream(std::string_view pFilePath, ax::IFileStream::Mode pMode) const
210{
211 std::string relative;
212 if (pMode != ax::IFileStream::Mode::READ || !packedPathGet(pFilePath, relative))
213 {
214 return fsTPlatformFileUtils::openFileStream(pFilePath, pMode);
215 }
216 std::string data(static_cast<size_t>(fsCAssetPack::fileSizeGet(relative)), '\0');
217 if (!data.empty() && !fsCAssetPack::fileRead(relative, data.data()))
218 {
219 return nullptr;
220 }
221 return std::make_unique<fsCPackFileStream>(std::move(data));
222}
223
224#endif
std::vector< std::string > folderListGet(std::string_view pPath)
Definition fsCAssetPack.cpp:236
bool fileExistsGet(std::string_view pPath)
Definition fsCAssetPack.cpp:193
bool fileRead(std::string_view pPath, void *pDest)
Definition fsCAssetPack.cpp:218
bool mount(const std::string &pFolder)
Definition fsCAssetPack.cpp:162
bool folderExistsGet(std::string_view pPath)
Definition fsCAssetPack.cpp:198
std::int64_t fileSizeGet(std::string_view pPath)
Definition fsCAssetPack.cpp:212
decltype(sizeof(void *)) size_t
Definition doctest.h:524