From 5e95fda88b09940e6ebbb51f05d49b56e3a37bb8 Mon Sep 17 00:00:00 2001 From: Almamu Date: Sun, 13 Apr 2025 16:11:43 +0200 Subject: [PATCH] chore: fix path detection being wrong, make use of std::filesystem::path on CContainers --- .../Application/CWallpaperApplication.cpp | 3 ++ .../Assets/CCombinedContainer.cpp | 6 ++-- .../Assets/CCombinedContainer.h | 4 +-- src/WallpaperEngine/Assets/CContainer.cpp | 29 +++++++++++-------- src/WallpaperEngine/Assets/CContainer.h | 16 +++++----- src/WallpaperEngine/Assets/CDirectory.cpp | 9 +++--- src/WallpaperEngine/Assets/CDirectory.h | 4 +-- src/WallpaperEngine/Assets/CPackage.cpp | 2 +- src/WallpaperEngine/Assets/CPackage.h | 2 +- .../Assets/CVirtualContainer.cpp | 6 ++-- .../Assets/CVirtualContainer.h | 6 ++-- 11 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/WallpaperEngine/Application/CWallpaperApplication.cpp b/src/WallpaperEngine/Application/CWallpaperApplication.cpp index 1aaef6c..496a4c4 100644 --- a/src/WallpaperEngine/Application/CWallpaperApplication.cpp +++ b/src/WallpaperEngine/Application/CWallpaperApplication.cpp @@ -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; } diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.cpp b/src/WallpaperEngine/Assets/CCombinedContainer.cpp index 533a71a..50d3995 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.cpp +++ b/src/WallpaperEngine/Assets/CCombinedContainer.cpp @@ -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 } } diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.h b/src/WallpaperEngine/Assets/CCombinedContainer.h index 2ccceb5..338e8a9 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.h +++ b/src/WallpaperEngine/Assets/CCombinedContainer.h @@ -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 */ diff --git a/src/WallpaperEngine/Assets/CContainer.cpp b/src/WallpaperEngine/Assets/CContainer.cpp index 68030d6..02410d6 100644 --- a/src/WallpaperEngine/Assets/CContainer.cpp +++ b/src/WallpaperEngine/Assets/CContainer.cpp @@ -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 diff --git a/src/WallpaperEngine/Assets/CContainer.h b/src/WallpaperEngine/Assets/CContainer.h index cd2caa7..611f27f 100644 --- a/src/WallpaperEngine/Assets/CContainer.h +++ b/src/WallpaperEngine/Assets/CContainer.h @@ -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 \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CDirectory.cpp b/src/WallpaperEngine/Assets/CDirectory.cpp index 9cb6a68..09cd671 100644 --- a/src/WallpaperEngine/Assets/CDirectory.cpp +++ b/src/WallpaperEngine/Assets/CDirectory.cpp @@ -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); diff --git a/src/WallpaperEngine/Assets/CDirectory.h b/src/WallpaperEngine/Assets/CDirectory.h index c49581a..6cc84e5 100644 --- a/src/WallpaperEngine/Assets/CDirectory.h +++ b/src/WallpaperEngine/Assets/CDirectory.h @@ -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 */ diff --git a/src/WallpaperEngine/Assets/CPackage.cpp b/src/WallpaperEngine/Assets/CPackage.cpp index f7452bb..4a3da0d 100644 --- a/src/WallpaperEngine/Assets/CPackage.cpp +++ b/src/WallpaperEngine/Assets/CPackage.cpp @@ -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 ()) diff --git a/src/WallpaperEngine/Assets/CPackage.h b/src/WallpaperEngine/Assets/CPackage.h index ea41bb0..2e64788 100644 --- a/src/WallpaperEngine/Assets/CPackage.h +++ b/src/WallpaperEngine/Assets/CPackage.h @@ -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: /** diff --git a/src/WallpaperEngine/Assets/CVirtualContainer.cpp b/src/WallpaperEngine/Assets/CVirtualContainer.cpp index 5526def..63324af 100644 --- a/src/WallpaperEngine/Assets/CVirtualContainer.cpp +++ b/src/WallpaperEngine/Assets/CVirtualContainer.cpp @@ -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 ()) diff --git a/src/WallpaperEngine/Assets/CVirtualContainer.h b/src/WallpaperEngine/Assets/CVirtualContainer.h index 4181ac3..1befb6a 100644 --- a/src/WallpaperEngine/Assets/CVirtualContainer.h +++ b/src/WallpaperEngine/Assets/CVirtualContainer.h @@ -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 */