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_audioContext (nullptr),
m_audioDriver (nullptr), m_audioDriver (nullptr),
m_audioRecorder (nullptr), m_audioRecorder (nullptr),
m_audioDetector (nullptr),
m_inputContext (nullptr), m_inputContext (nullptr),
m_renderContext (nullptr), m_renderContext (nullptr),
m_videoDriver (nullptr), m_videoDriver (nullptr),
@ -51,6 +52,8 @@ CWallpaperApplication::~CWallpaperApplication () {
delete m_videoDriver; delete m_videoDriver;
delete m_audioContext; delete m_audioContext;
delete m_audioDriver; delete m_audioDriver;
delete m_audioDetector;
delete m_audioRecorder;
delete m_inputContext; delete m_inputContext;
delete m_browserContext; 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) { for (const auto cur : this->m_containers) {
try { try {
// try to read the file on the current container, if the file doesn't exists // 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"); 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) { for (const auto cur : this->m_containers) {
try { try {
// try to read the file on the current container, if the file doesn't exists // try to read the file on the current container, if the file doesn't exists
// an exception will be thrown // an exception will be thrown
return cur->readFile (filename, length); return cur->readFile (filename, length);
} catch (CAssetLoadException&) { } catch (CAssetLoadException& e) {
// not found in this container, next try // 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); void addPkg (const std::filesystem::path& path);
/** @inheritdoc */ /** @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 */ /** @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: private:
/** The list of containers to search files off from */ /** The list of containers to search files off from */

View File

@ -9,13 +9,14 @@
using namespace WallpaperEngine::Assets; 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"); 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) // 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); const uint8_t* textureContents = this->readFile (texture, nullptr);
@ -30,7 +31,7 @@ const ITexture* CContainer::readTexture (const std::string& filename) const {
return result; 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; std::filesystem::path shader = filename;
auto it = shader.begin (); 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 { std::string CContainer::readVertexShader (const std::filesystem::path& filename) const {
return this->readShader (filename + ".vert"); std::filesystem::path shader = filename;
shader.replace_extension (".vert");
return this->readShader (shader);
} }
std::string CContainer::readFragmentShader (const std::string& filename) const { std::string CContainer::readFragmentShader (const std::filesystem::path& filename) const {
return this->readShader (filename + ".frag"); std::filesystem::path shader = filename;
shader.replace_extension (".frag");
return this->readShader (shader);
} }
std::string CContainer::readIncludeShader (const std::string& filename) const { std::string CContainer::readIncludeShader (const std::filesystem::path& filename) const {
return this->readFileAsString ("shaders/" + filename); 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; uint32_t length = 0;
// read file contents and allocate a buffer for a string // read file contents and allocate a buffer for a string

View File

@ -19,7 +19,7 @@ class CContainer {
* @param filename * @param filename
* @return * @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 * Reads the given file from the container and returns it's data
@ -32,7 +32,7 @@ class CContainer {
* *
* @return * @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 * Wrapper for readFile, appends the texture extension at the end of the filename
@ -41,7 +41,7 @@ class CContainer {
* *
* @return * @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 * 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 * @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 * 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 * @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 * 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 * @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 * 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 * @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 * Reads a file as string
@ -86,6 +86,6 @@ class CContainer {
* *
* @return The file's contents as string * @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 } // 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 { try {
std::filesystem::path path = this->m_basepath / filename; std::filesystem::path final = std::filesystem::canonical (this->m_basepath / filename);
std::filesystem::path final = std::filesystem::canonical (path);
// first validate the path, so the message doesn't reflect if the file exists or not unless it's under the actual directory // 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"); 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 { try {
std::filesystem::path final = this->resolveRealFile (filename); 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); explicit CDirectory (const std::filesystem::path& basepath);
/** @inheritdoc */ /** @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 */ /** @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: private:
/** The basepath for the directory */ /** The basepath for the directory */

View File

@ -24,7 +24,7 @@ CPackage::CPackage (std::filesystem::path path) : m_path (std::move (path)) {
this->init (); 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); const auto it = this->m_contents.find (filename);
if (it == this->m_contents.end ()) if (it == this->m_contents.end ())

View File

@ -20,7 +20,7 @@ class CPackage final : public CContainer {
public: public:
explicit CPackage (std::filesystem::path path); 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: protected:
/** /**

View File

@ -5,11 +5,11 @@
using namespace WallpaperEngine::Assets; 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))); 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]; auto* copy = new uint8_t [contents.length () + 1];
// copy the text AND the \0 // 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); 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); const auto cur = this->m_virtualFiles.find (filename);
if (cur == this->m_virtualFiles.end ()) if (cur == this->m_virtualFiles.end ())

View File

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