chore: fix path detection being wrong, make use of std::filesystem::path on CContainers

This commit is contained in:
Almamu 2025-04-13 16:11:43 +02:00
parent 079dbd6e74
commit 5e95fda88b
11 changed files with 47 additions and 40 deletions

View File

@ -36,6 +36,7 @@ CWallpaperApplication::CWallpaperApplication (CApplicationContext& context) :
m_audioContext (nullptr),
m_audioDriver (nullptr),
m_audioRecorder (nullptr),
m_audioDetector (nullptr),
m_inputContext (nullptr),
m_renderContext (nullptr),
m_videoDriver (nullptr),
@ -51,6 +52,8 @@ CWallpaperApplication::~CWallpaperApplication () {
delete m_videoDriver;
delete m_audioContext;
delete m_audioDriver;
delete m_audioDetector;
delete m_audioRecorder;
delete m_inputContext;
delete m_browserContext;
}

View File

@ -26,7 +26,7 @@ void CCombinedContainer::addPkg (const std::filesystem::path& path) {
}
}
std::filesystem::path CCombinedContainer::resolveRealFile (const std::string& filename) const {
std::filesystem::path CCombinedContainer::resolveRealFile (const std::filesystem::path& filename) const {
for (const auto cur : this->m_containers) {
try {
// try to read the file on the current container, if the file doesn't exists
@ -41,13 +41,13 @@ std::filesystem::path CCombinedContainer::resolveRealFile (const std::string& fi
throw CAssetLoadException (filename, "Cannot resolve file in any of the containers");
}
const uint8_t* CCombinedContainer::readFile (const std::string& filename, uint32_t* length) const {
const uint8_t* CCombinedContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const {
for (const auto cur : this->m_containers) {
try {
// try to read the file on the current container, if the file doesn't exists
// an exception will be thrown
return cur->readFile (filename, length);
} catch (CAssetLoadException&) {
} catch (CAssetLoadException& e) {
// not found in this container, next try
}
}

View File

@ -28,9 +28,9 @@ class CCombinedContainer final : public CContainer {
void addPkg (const std::filesystem::path& path);
/** @inheritdoc */
[[nodiscard]] std::filesystem::path resolveRealFile (const std::string& filename) const override;
[[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override;
/** @inheritdoc */
[[nodiscard]] const uint8_t* readFile (const std::string& filename, uint32_t* length) const override;
[[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override;
private:
/** The list of containers to search files off from */

View File

@ -9,13 +9,14 @@
using namespace WallpaperEngine::Assets;
std::filesystem::path CContainer::resolveRealFile (const std::string& filename) const {
std::filesystem::path CContainer::resolveRealFile (const std::filesystem::path& filename) const {
throw CAssetLoadException (filename, "Cannot resolve physical file in this container");
}
const ITexture* CContainer::readTexture (const std::string& filename) const {
const ITexture* CContainer::readTexture (const std::filesystem::path& filename) const {
// get the texture's filename (usually .tex)
const std::string texture = "materials/" + filename + ".tex";
std::filesystem::path texture = "materials" / filename;
texture.replace_extension (".tex");
const uint8_t* textureContents = this->readFile (texture, nullptr);
@ -30,7 +31,7 @@ const ITexture* CContainer::readTexture (const std::string& filename) const {
return result;
}
std::string CContainer::readShader (const std::string& filename) const {
std::string CContainer::readShader (const std::filesystem::path& filename) const {
std::filesystem::path shader = filename;
auto it = shader.begin ();
@ -53,22 +54,26 @@ std::string CContainer::readShader (const std::string& filename) const {
}
}
return this->readFileAsString ("shaders/" + filename);
return this->readFileAsString ("shaders" / filename);
}
std::string CContainer::readVertexShader (const std::string& filename) const {
return this->readShader (filename + ".vert");
std::string CContainer::readVertexShader (const std::filesystem::path& filename) const {
std::filesystem::path shader = filename;
shader.replace_extension (".vert");
return this->readShader (shader);
}
std::string CContainer::readFragmentShader (const std::string& filename) const {
return this->readShader (filename + ".frag");
std::string CContainer::readFragmentShader (const std::filesystem::path& filename) const {
std::filesystem::path shader = filename;
shader.replace_extension (".frag");
return this->readShader (shader);
}
std::string CContainer::readIncludeShader (const std::string& filename) const {
return this->readFileAsString ("shaders/" + filename);
std::string CContainer::readIncludeShader (const std::filesystem::path& filename) const {
return this->readFileAsString ("shaders" / filename);
}
std::string CContainer::readFileAsString (const std::string& filename) const {
std::string CContainer::readFileAsString (const std::filesystem::path& filename) const {
uint32_t length = 0;
// read file contents and allocate a buffer for a string

View File

@ -19,7 +19,7 @@ class CContainer {
* @param filename
* @return
*/
[[nodiscard]] virtual std::filesystem::path resolveRealFile (const std::string& filename) const;
[[nodiscard]] virtual std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const;
/**
* Reads the given file from the container and returns it's data
@ -32,7 +32,7 @@ class CContainer {
*
* @return
*/
[[nodiscard]] virtual const uint8_t* readFile (const std::string& filename, uint32_t* length) const = 0;
[[nodiscard]] virtual const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const = 0;
/**
* Wrapper for readFile, appends the texture extension at the end of the filename
@ -41,7 +41,7 @@ class CContainer {
*
* @return
*/
[[nodiscard]] const ITexture* readTexture (const std::string& filename) const;
[[nodiscard]] const ITexture* readTexture (const std::filesystem::path& filename) const;
/**
* Wrapper for readFile, checks for compat versions of the given shader file
@ -50,7 +50,7 @@ class CContainer {
*
* @return The shader code as an string to be used
*/
[[nodiscard]] std::string readShader (const std::string& filename) const;
[[nodiscard]] std::string readShader (const std::filesystem::path& filename) const;
/**
* Wrapper for readFile, appends the .vert extension at the end and opens the given shader file
@ -59,7 +59,7 @@ class CContainer {
*
* @return The shader code as an string to be used
*/
[[nodiscard]] std::string readVertexShader (const std::string& filename) const;
[[nodiscard]] std::string readVertexShader (const std::filesystem::path& filename) const;
/**
* Wrapper for readFile, appends the .frag extension at the end and opens the given shader file
@ -68,7 +68,7 @@ class CContainer {
*
* @return The shader code as an string to be used
*/
[[nodiscard]] std::string readFragmentShader (const std::string& filename) const;
[[nodiscard]] std::string readFragmentShader (const std::filesystem::path& filename) const;
/**
* Wrapper for readFile, appends the .h extension at the end and opens the given shader file
@ -77,7 +77,7 @@ class CContainer {
*
* @return The shader code as an string to be used
*/
[[nodiscard]] std::string readIncludeShader (const std::string& filename) const;
[[nodiscard]] std::string readIncludeShader (const std::filesystem::path& filename) const;
/**
* Reads a file as string
@ -86,6 +86,6 @@ class CContainer {
*
* @return The file's contents as string
*/
[[nodiscard]] std::string readFileAsString (const std::string& filename) const;
[[nodiscard]] std::string readFileAsString (const std::filesystem::path& filename) const;
};
} // namespace WallpaperEngine::Assets

View File

@ -25,13 +25,12 @@ CDirectory::CDirectory (const std::filesystem::path& basepath) {
}
}
std::filesystem::path CDirectory::resolveRealFile (const std::string& filename) const {
std::filesystem::path CDirectory::resolveRealFile (const std::filesystem::path& filename) const {
try {
std::filesystem::path path = this->m_basepath / filename;
std::filesystem::path final = std::filesystem::canonical (path);
std::filesystem::path final = std::filesystem::canonical (this->m_basepath / filename);
// first validate the path, so the message doesn't reflect if the file exists or not unless it's under the actual directory
if (this->m_basepath.string ().find (final.string ()) != 0) {
if (final.string ().find (this->m_basepath.string ()) != 0) {
throw CAssetLoadException (filename, "File is not a child of the given directory");
}
@ -51,7 +50,7 @@ std::filesystem::path CDirectory::resolveRealFile (const std::string& filename)
}
}
const uint8_t* CDirectory::readFile (const std::string& filename, uint32_t* length) const {
const uint8_t* CDirectory::readFile (const std::filesystem::path& filename, uint32_t* length) const {
try {
std::filesystem::path final = this->resolveRealFile (filename);

View File

@ -17,9 +17,9 @@ class CDirectory final : public CContainer {
explicit CDirectory (const std::filesystem::path& basepath);
/** @inheritdoc */
[[nodiscard]] std::filesystem::path resolveRealFile (const std::string& filename) const override;
[[nodiscard]] std::filesystem::path resolveRealFile (const std::filesystem::path& filename) const override;
/** @inheritdoc */
[[nodiscard]] const uint8_t* readFile (const std::string& filename, uint32_t* length) const override;
[[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override;
private:
/** The basepath for the directory */

View File

@ -24,7 +24,7 @@ CPackage::CPackage (std::filesystem::path path) : m_path (std::move (path)) {
this->init ();
}
const uint8_t* CPackage::readFile (const std::string& filename, uint32_t* length) const {
const uint8_t* CPackage::readFile (const std::filesystem::path& filename, uint32_t* length) const {
const auto it = this->m_contents.find (filename);
if (it == this->m_contents.end ())

View File

@ -20,7 +20,7 @@ class CPackage final : public CContainer {
public:
explicit CPackage (std::filesystem::path path);
[[nodiscard]] const uint8_t* readFile (const std::string& filename, uint32_t* length) const override;
[[nodiscard]] const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override;
protected:
/**

View File

@ -5,11 +5,11 @@
using namespace WallpaperEngine::Assets;
void CVirtualContainer::add (const std::string& filename, const uint8_t* contents, uint32_t length) {
void CVirtualContainer::add (const std::filesystem::path& filename, const uint8_t* contents, uint32_t length) {
this->m_virtualFiles.insert (std::make_pair (filename, new CFileEntry (contents, length)));
}
void CVirtualContainer::add (const std::string& filename, const std::string& contents) {
void CVirtualContainer::add (const std::filesystem::path& filename, const std::string& contents) {
auto* copy = new uint8_t [contents.length () + 1];
// copy the text AND the \0
@ -19,7 +19,7 @@ void CVirtualContainer::add (const std::string& filename, const std::string& con
this->add (filename, copy, contents.length () + 1);
}
const uint8_t* CVirtualContainer::readFile (const std::string& filename, uint32_t* length) const {
const uint8_t* CVirtualContainer::readFile (const std::filesystem::path& filename, uint32_t* length) const {
const auto cur = this->m_virtualFiles.find (filename);
if (cur == this->m_virtualFiles.end ())

View File

@ -19,7 +19,7 @@ class CVirtualContainer final : public CContainer {
* @param contents
* @param length
*/
void add (const std::string& filename, const uint8_t* contents, uint32_t length);
void add (const std::filesystem::path& filename, const uint8_t* contents, uint32_t length);
/**
* Adds a new file to the virtual container
@ -27,9 +27,9 @@ class CVirtualContainer final : public CContainer {
* @param filename
* @param contents
*/
void add (const std::string& filename, const std::string& contents);
void add (const std::filesystem::path& filename, const std::string& contents);
/** @inheritdoc */
const uint8_t* readFile (const std::string& filename, uint32_t* length) const override;
const uint8_t* readFile (const std::filesystem::path& filename, uint32_t* length) const override;
private:
/** The recorded files in this virtual container */