3#if defined fsPACK_FILE_UTILS
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;
16 #include <platform/linux/FileUtils-linux.h>
17 using fsTPlatformFileUtils = ax::FileUtilsLinux;
25 class fsCPackFileStream :
public ax::IFileStream
28 explicit fsCPackFileStream(std::string pData) : mData(std::move(pData)) {}
30 bool open(std::string_view, ax::IFileStream::Mode)
override {
return false; }
37 int64_t seek(int64_t pOffset,
int pOrigin)
const override
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())
48 int read(
void* pBuf,
unsigned int pSize)
const override
50 const auto count =
static_cast<int>(std::min<int64_t>(pSize, size() - mPos));
51 std::memcpy(pBuf, mData.data() + mPos, count);
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; }
62 mutable int64_t mPos = 0;
66 class fsCPackFileUtilsImpl :
public fsTPlatformFileUtils
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;
79 bool packedPathGet(std::string_view pPath, std::string& pRelative)
const;
83void fsCPackFileUtils::installIfPacked()
85 ax::FileUtils*
const current = ax::FileUtils::getInstance();
86 const std::string root = current->getDefaultResourceRootPath();
91 const auto searchPaths = current->getSearchPaths();
92 auto*
const packed =
new fsCPackFileUtilsImpl();
94 packed->setSearchPaths(searchPaths);
96 ax::FileUtils::setDelegate(packed);
99bool fsCPackFileUtilsImpl::packedPathGet(std::string_view pPath, std::string& pRelative)
const
101 std::string_view relative = pPath;
102 if (isAbsolutePath(pPath))
104 const std::string& root = getDefaultResourceRootPath();
105 if (pPath.size() <= root.size() || pPath.compare(0, root.size(), root) != 0)
109 relative = pPath.substr(root.size());
115 pRelative = relative;
121std::string fsCPackFileUtilsImpl::fullPathForFilename(std::string_view pFilename)
const
123 std::string relative;
124 if (!isAbsolutePath(pFilename) && packedPathGet(pFilename, relative))
126 return getDefaultResourceRootPath() + relative;
128 return fsTPlatformFileUtils::fullPathForFilename(pFilename);
131bool fsCPackFileUtilsImpl::isFileExist(std::string_view pFilename)
const
133 std::string relative;
134 return packedPathGet(pFilename, relative) || fsTPlatformFileUtils::isFileExist(pFilename);
137bool fsCPackFileUtilsImpl::isDirectoryExistInternal(std::string_view pDirPath)
const
139 const std::string& root = getDefaultResourceRootPath();
140 if (pDirPath.size() > root.size() && pDirPath.compare(0, root.size(), root) == 0 &&
145 return fsTPlatformFileUtils::isDirectoryExistInternal(pDirPath);
148ax::FileUtils::Status fsCPackFileUtilsImpl::getContents(std::string_view pFilename, ax::ResizableBuffer* pBuffer)
const
150 std::string relative;
151 if (!packedPathGet(fullPathForFilename(pFilename), relative))
153 return fsTPlatformFileUtils::getContents(pFilename, pBuffer);
159 pBuffer->resize_and_overwrite(
static_cast<size_t>(size), [&read, &relative](
void*
const pDest,
const size_t pBytes)
162 return read ? pBytes :
size_t(0);
166 return Status::ReadFailed;
169 pBuffer->resize(
static_cast<size_t>(size));
172 return Status::ReadFailed;
178int64_t fsCPackFileUtilsImpl::getFileSize(std::string_view pFilepath)
const
180 std::string relative;
182 : fsTPlatformFileUtils::getFileSize(pFilepath);
185std::vector<std::string> fsCPackFileUtilsImpl::listFiles(std::string_view pDirPath)
const
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)
194 if (!folder.empty() && folder.back() !=
'/')
200 const std::string path = folder + child;
201 if (std::find(files.begin(), files.end(), path) == files.end())
203 files.push_back(path);
209std::unique_ptr<ax::IFileStream> fsCPackFileUtilsImpl::openFileStream(std::string_view pFilePath, ax::IFileStream::Mode pMode)
const
211 std::string relative;
212 if (pMode != ax::IFileStream::Mode::READ || !packedPathGet(pFilePath, relative))
214 return fsTPlatformFileUtils::openFileStream(pFilePath, pMode);
221 return std::make_unique<fsCPackFileStream>(std::move(data));
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