From 5b65abe7b9dabe6357ad9c43454e0dc5850a9167 Mon Sep 17 00:00:00 2001 From: Almamu Date: Sat, 13 Sep 2025 19:10:09 +0200 Subject: [PATCH] feat: added asset locator and replaced filesystem direct usage by asset locator --- CMakeLists.txt | 2 + .../Application/WallpaperApplication.cpp | 9 +- .../Application/WallpaperApplication.h | 7 +- .../Assets/AssetLoadException.cpp | 10 +- .../Assets/AssetLoadException.h | 15 +-- src/WallpaperEngine/Assets/AssetLocator.cpp | 99 +++++++++++++++++++ src/WallpaperEngine/Assets/AssetLocator.h | 27 +++++ src/WallpaperEngine/Data/Model/Project.h | 7 +- .../Data/Parsers/EffectParser.cpp | 2 +- .../Data/Parsers/MaterialParser.cpp | 2 +- .../Data/Parsers/ModelParser.cpp | 2 +- .../Data/Parsers/ProjectParser.cpp | 4 +- .../Data/Parsers/ProjectParser.h | 4 +- .../Data/Parsers/WallpaperParser.cpp | 2 +- .../FileSystem/Adapters/Directory.cpp | 2 +- .../FileSystem/Adapters/Package.cpp | 2 +- .../FileSystem/Adapters/Virtual.cpp | 2 +- src/WallpaperEngine/FileSystem/Container.cpp | 6 +- src/WallpaperEngine/Render/CObject.cpp | 4 +- src/WallpaperEngine/Render/CObject.h | 2 +- src/WallpaperEngine/Render/CWallpaper.cpp | 4 +- src/WallpaperEngine/Render/CWallpaper.h | 2 +- src/WallpaperEngine/Render/Objects/CSound.cpp | 2 +- .../Render/Objects/Effects/CPass.cpp | 2 +- src/WallpaperEngine/Render/Shaders/Shader.cpp | 52 ++-------- src/WallpaperEngine/Render/Shaders/Shader.h | 4 +- .../Render/Shaders/ShaderUnit.cpp | 8 +- .../Render/Shaders/ShaderUnit.h | 7 +- src/WallpaperEngine/Render/TextureCache.cpp | 4 +- .../Render/Wallpapers/CVideo.cpp | 2 +- .../WebBrowser/CEF/WPSchemeHandler.cpp | 5 +- .../WebBrowser/CEF/WPSchemeHandler.h | 6 +- 32 files changed, 197 insertions(+), 111 deletions(-) create mode 100644 src/WallpaperEngine/Assets/AssetLocator.cpp create mode 100644 src/WallpaperEngine/Assets/AssetLocator.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d89428..264fcb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -269,6 +269,8 @@ add_executable( src/WallpaperEngine/Assets/AssetLoadException.cpp src/WallpaperEngine/Assets/AssetLoadException.h + src/WallpaperEngine/Assets/AssetLocator.cpp + src/WallpaperEngine/Assets/AssetLocator.h src/WallpaperEngine/FileSystem/Container.h src/WallpaperEngine/FileSystem/Container.cpp diff --git a/src/WallpaperEngine/Application/WallpaperApplication.cpp b/src/WallpaperEngine/Application/WallpaperApplication.cpp index 62481e6..e82b529 100644 --- a/src/WallpaperEngine/Application/WallpaperApplication.cpp +++ b/src/WallpaperEngine/Application/WallpaperApplication.cpp @@ -29,6 +29,7 @@ float g_Time; float g_TimeLast; float g_Daytime; +using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Application; using namespace WallpaperEngine::Data::Model; using namespace WallpaperEngine::FileSystem; @@ -40,10 +41,10 @@ WallpaperApplication::WallpaperApplication (ApplicationContext& context) : this->setupBrowser(); } -ContainerUniquePtr WallpaperApplication::setupContainer (const std::string& bg) const { +AssetLocatorUniquePtr WallpaperApplication::setupAssetLocator (const std::string& bg) const { auto container = std::make_unique (); - std::filesystem::path path (bg); + const std::filesystem::path path = bg; container->mount (path, "/"); try { @@ -193,7 +194,7 @@ ContainerUniquePtr WallpaperApplication::setupContainer (const std::string& bg) "}" ); - return container; + return std::make_unique (std::move (container)); } void WallpaperApplication::loadBackgrounds () { @@ -214,7 +215,7 @@ void WallpaperApplication::loadBackgrounds () { } ProjectUniquePtr WallpaperApplication::loadBackground (const std::string& bg) { - auto container = this->setupContainer (bg); + auto container = this->setupAssetLocator (bg); auto json = WallpaperEngine::Data::JSON::JSON::parse (container->readString ("project.json")); return WallpaperEngine::Data::Parsers::ProjectParser::parse (json, std::move(container)); diff --git a/src/WallpaperEngine/Application/WallpaperApplication.h b/src/WallpaperEngine/Application/WallpaperApplication.h index 1f709dd..264a4ba 100644 --- a/src/WallpaperEngine/Application/WallpaperApplication.h +++ b/src/WallpaperEngine/Application/WallpaperApplication.h @@ -1,6 +1,7 @@ #pragma once #include "WallpaperEngine/Application/ApplicationContext.h" +#include "WallpaperEngine/Assets/AssetLocator.h" #include "WallpaperEngine/Render/CWallpaper.h" #include "WallpaperEngine/Render/Drivers/Detectors/FullScreenDetector.h" @@ -16,6 +17,7 @@ #include "WallpaperEngine/Data/Model/Types.h" namespace WallpaperEngine::Application { +using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Data::Model; /** * Small wrapper class over the actual wallpaper's main application skeleton @@ -53,12 +55,11 @@ class WallpaperApplication { private: /** - * Sets up a combined container for the given background, adding default files and directories to the list + * Sets up an asset locator for the given background * - * @param container * @param bg */ - ContainerUniquePtr setupContainer (const std::string& bg) const; + AssetLocatorUniquePtr setupAssetLocator (const std::string& bg) const; /** * Loads projects based off the settings */ diff --git a/src/WallpaperEngine/Assets/AssetLoadException.cpp b/src/WallpaperEngine/Assets/AssetLoadException.cpp index db439ee..060a76c 100644 --- a/src/WallpaperEngine/Assets/AssetLoadException.cpp +++ b/src/WallpaperEngine/Assets/AssetLoadException.cpp @@ -1,10 +1,6 @@ #include "AssetLoadException.h" -using namespace WallpaperEngine::Render; +using namespace WallpaperEngine::Assets; -AssetLoadException::AssetLoadException (const std::string& filename, const std::string& extrainfo) : - m_message ("Cannot find file " + filename + ": " + extrainfo) {} - -const char* AssetLoadException::what () const noexcept { - return this->m_message.c_str (); -} +AssetLoadException::AssetLoadException (const std::filesystem::filesystem_error& filesystem_error) noexcept +: std::filesystem::filesystem_error (filesystem_error) {} diff --git a/src/WallpaperEngine/Assets/AssetLoadException.h b/src/WallpaperEngine/Assets/AssetLoadException.h index 225dc2d..76f62b8 100644 --- a/src/WallpaperEngine/Assets/AssetLoadException.h +++ b/src/WallpaperEngine/Assets/AssetLoadException.h @@ -1,16 +1,11 @@ #pragma once -#include -#include +#include -// TODO: REWRITE THIS ONE TO MAKE MORE SENSE, IT REALLY MEANS "FILE-RELATED EXCEPTION" -namespace WallpaperEngine::Render { -class AssetLoadException final : public std::exception { +namespace WallpaperEngine::Assets { +class AssetLoadException final : public std::filesystem::filesystem_error { public: - explicit AssetLoadException (const std::string& filename, const std::string& extrainfo = ""); - [[nodiscard]] const char* what () const noexcept override; - - private: - std::string m_message {}; + using std::filesystem::filesystem_error::filesystem_error; + explicit AssetLoadException (const std::filesystem::filesystem_error& filesystem_error) noexcept; }; } // namespace WallpaperEngine::Assets \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/AssetLocator.cpp b/src/WallpaperEngine/Assets/AssetLocator.cpp new file mode 100644 index 0000000..f68e194 --- /dev/null +++ b/src/WallpaperEngine/Assets/AssetLocator.cpp @@ -0,0 +1,99 @@ +#include "AssetLocator.h" + +#include "AssetLoadException.h" + +using namespace WallpaperEngine::Assets; + +AssetLocator::AssetLocator (ContainerUniquePtr filesystem) : m_filesystem (std::move (filesystem)) {} + +std::string AssetLocator::shader (const std::filesystem::path& filename) const { + try { + std::filesystem::path shader = filename; + + // detect workshop shaders and check if there's a + if (auto it = shader.begin (); *it++ == "workshop") { + const std::filesystem::path workshopId = *it++; + + if (++it != shader.end ()) { + const std::filesystem::path& shaderfile = *it; + + try { + shader = std::filesystem::path ("zcompat") / "scene" / "shaders" / workshopId / shaderfile; + // replace the old path with the new one + std::string contents = this->m_filesystem->readString (shader); + + sLog.out ("Replaced ", filename, " with compat ", shader); + + return contents; + } catch (std::filesystem::filesystem_error&) { + // these exceptions can be ignored because the replacement file might not exist + } + } + } + + return this->m_filesystem->readString ("shaders" / filename); + } catch (std::filesystem::filesystem_error& base) { + throw AssetLoadException (base); + } +} + +std::string AssetLocator::fragmentShader (const std::filesystem::path& filename) const { + auto final = filename; + + final.replace_extension ("frag"); + + return this->shader (final); +} + +std::string AssetLocator::vertexShader (const std::filesystem::path& filename) const { + auto final = filename; + + final.replace_extension ("vert"); + + return this->shader (final); +} + +std::string AssetLocator::includeShader (const std::filesystem::path& filename) const { + auto final = filename; + + final.replace_extension ("h"); + + return this->shader (final); +} + +std::string AssetLocator::readString (const std::filesystem::path& filename) const { + try { + return this->m_filesystem->readString (filename); + } catch (std::filesystem::filesystem_error& base) { + throw AssetLoadException (base); + } +} + + +ReadStreamSharedPtr AssetLocator::texture (const std::filesystem::path& filename) const { + auto final = filename; + + final.replace_extension ("tex"); + + try { + return this->m_filesystem->read (final); + } catch (std::filesystem::filesystem_error& base) { + throw AssetLoadException (base); + } +} + +ReadStreamSharedPtr AssetLocator::read (const std::filesystem::path& path) const { + try { + return this->m_filesystem->read (path); + } catch (std::filesystem::filesystem_error& base) { + throw AssetLoadException (base); + } +} + +std::filesystem::path AssetLocator::physicalPath (const std::filesystem::path& path) const { + try { + return this->m_filesystem->physicalPath (path); + } catch (std::filesystem::filesystem_error& base) { + throw AssetLoadException (base); + } +} \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/AssetLocator.h b/src/WallpaperEngine/Assets/AssetLocator.h new file mode 100644 index 0000000..2339fc5 --- /dev/null +++ b/src/WallpaperEngine/Assets/AssetLocator.h @@ -0,0 +1,27 @@ +#pragma once + +#include "WallpaperEngine/FileSystem/Container.h" + +namespace WallpaperEngine::Assets { +using namespace WallpaperEngine::FileSystem; +using namespace WallpaperEngine::Data::Model; +class AssetLocator { + public: + explicit AssetLocator (ContainerUniquePtr filesystem); + + std::string vertexShader (const std::filesystem::path& filename) const; + std::string fragmentShader (const std::filesystem::path& filename) const; + std::string includeShader (const std::filesystem::path& filename) const; + ReadStreamSharedPtr texture (const std::filesystem::path& filename) const; + std::string readString (const std::filesystem::path& filename) const; + ReadStreamSharedPtr read (const std::filesystem::path& path) const; + std::filesystem::path physicalPath (const std::filesystem::path& path) const; + + private: + std::string shader (const std::filesystem::path& filename) const; + + ContainerUniquePtr m_filesystem; +}; + +using AssetLocatorUniquePtr = std::unique_ptr; +} \ No newline at end of file diff --git a/src/WallpaperEngine/Data/Model/Project.h b/src/WallpaperEngine/Data/Model/Project.h index f9b4f53..31a2187 100644 --- a/src/WallpaperEngine/Data/Model/Project.h +++ b/src/WallpaperEngine/Data/Model/Project.h @@ -4,9 +4,10 @@ #include #include "Types.h" -#include "WallpaperEngine/FileSystem/Container.h" +#include "WallpaperEngine/Assets/AssetLocator.h" namespace WallpaperEngine::Data::Model { +using namespace WallpaperEngine::Assets; /** * Represents a wallpaper engine project */ @@ -30,7 +31,7 @@ struct Project { Properties properties; /** The wallpaper this project defines */ WallpaperUniquePtr wallpaper; - /** VFS to access the project's files */ - ContainerUniquePtr container; + /** Abstraction over asset loading to provide access to them */ + AssetLocatorUniquePtr assetLocator; }; }; diff --git a/src/WallpaperEngine/Data/Parsers/EffectParser.cpp b/src/WallpaperEngine/Data/Parsers/EffectParser.cpp index 7446815..3be38b2 100644 --- a/src/WallpaperEngine/Data/Parsers/EffectParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/EffectParser.cpp @@ -10,7 +10,7 @@ using namespace WallpaperEngine::Data::Parsers; using namespace WallpaperEngine::Data::Model; EffectUniquePtr EffectParser::load (const Project& project, const std::string& filename) { - const auto effectJson = JSON::parse (project.container->readString (filename)); + const auto effectJson = JSON::parse (project.assetLocator->readString (filename)); return parse (effectJson, project); } diff --git a/src/WallpaperEngine/Data/Parsers/MaterialParser.cpp b/src/WallpaperEngine/Data/Parsers/MaterialParser.cpp index 57a909a..39a887e 100644 --- a/src/WallpaperEngine/Data/Parsers/MaterialParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/MaterialParser.cpp @@ -8,7 +8,7 @@ using namespace WallpaperEngine::Data::Parsers; using namespace WallpaperEngine::Data::Model; MaterialUniquePtr MaterialParser::load (const Project& project, const std::string& filename) { - const auto materialJson = JSON::parse (project.container->readString (filename)); + const auto materialJson = JSON::parse (project.assetLocator->readString (filename)); return parse (materialJson, filename); } diff --git a/src/WallpaperEngine/Data/Parsers/ModelParser.cpp b/src/WallpaperEngine/Data/Parsers/ModelParser.cpp index dfde4a3..0f1a194 100644 --- a/src/WallpaperEngine/Data/Parsers/ModelParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/ModelParser.cpp @@ -10,7 +10,7 @@ using namespace WallpaperEngine::Data::Parsers; using namespace WallpaperEngine::Data::Model; ModelUniquePtr ModelParser::load (const Project& project, const std::string& filename) { - const auto model = JSON::parse (project.container->readString (filename)); + const auto model = JSON::parse (project.assetLocator->readString (filename)); return parse (model, project, filename); } diff --git a/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp b/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp index d087abd..65fad4d 100644 --- a/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/ProjectParser.cpp @@ -13,7 +13,7 @@ using namespace WallpaperEngine::Data::Parsers; static int backgroundId = 0; -ProjectUniquePtr ProjectParser::parse (const JSON& data, ContainerUniquePtr container) { +ProjectUniquePtr ProjectParser::parse (const JSON& data, AssetLocatorUniquePtr container) { const auto general = data.optional ("general"); const auto workshopId = data.optional ("workshopid"); auto actualWorkshopId = std::to_string (--backgroundId); @@ -38,7 +38,7 @@ ProjectUniquePtr ProjectParser::parse (const JSON& data, ContainerUniquePtr cont .workshopId = actualWorkshopId, .supportsAudioProcessing = general.has_value () && general.value ().optional ("supportsaudioprocessing", false), .properties = parseProperties (general), - .container = std::move(container), + .assetLocator = std::move(container), }); result->wallpaper = WallpaperParser::parse (data.require ("file", "Project's main file missing"), *result); diff --git a/src/WallpaperEngine/Data/Parsers/ProjectParser.h b/src/WallpaperEngine/Data/Parsers/ProjectParser.h index bb6508a..c3422fd 100644 --- a/src/WallpaperEngine/Data/Parsers/ProjectParser.h +++ b/src/WallpaperEngine/Data/Parsers/ProjectParser.h @@ -4,10 +4,12 @@ #include "WallpaperEngine/Data/JSON.h" #include "WallpaperEngine/Data/Model/Project.h" +#include "WallpaperEngine/Assets/AssetLocator.h" namespace WallpaperEngine::Data::Parsers { using JSON = WallpaperEngine::Data::JSON::JSON; +using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Data::Model; /** @@ -15,7 +17,7 @@ using namespace WallpaperEngine::Data::Model; */ class ProjectParser { public: - static ProjectUniquePtr parse (const JSON& data, ContainerUniquePtr container); + static ProjectUniquePtr parse (const JSON& data, AssetLocatorUniquePtr container); private: static Project::Type parseType (const std::string& type); diff --git a/src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp b/src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp index edf1bbc..44f6b57 100644 --- a/src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp +++ b/src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp @@ -22,7 +22,7 @@ WallpaperUniquePtr WallpaperParser::parse (const JSON& file, Project& project) { } SceneUniquePtr WallpaperParser::parseScene (const JSON& file, Project& project) { - const auto scene = JSON::parse (project.container->readString (file)); + const auto scene = JSON::parse (project.assetLocator->readString (file)); const auto camera = scene.require ("camera", "Scenes must have a camera section"); const auto general = scene.require ("general", "Scenes must have a general section"); const auto projection = general.require ("orthogonalprojection", "General section must have orthogonal projection info"); diff --git a/src/WallpaperEngine/FileSystem/Adapters/Directory.cpp b/src/WallpaperEngine/FileSystem/Adapters/Directory.cpp index 91d09bc..5f088ce 100644 --- a/src/WallpaperEngine/FileSystem/Adapters/Directory.cpp +++ b/src/WallpaperEngine/FileSystem/Adapters/Directory.cpp @@ -57,7 +57,7 @@ std::filesystem::path DirectoryAdapter::physicalPath (const std::filesystem::pat auto finalpath = std::filesystem::canonical(this->basepath / path); if (finalpath.string ().find (this->basepath.string ()) != 0) { - throw Render::AssetLoadException ("Cannot find file", path); + throw std::filesystem::filesystem_error ("Cannot find file", path, std::error_code ()); } return finalpath; diff --git a/src/WallpaperEngine/FileSystem/Adapters/Package.cpp b/src/WallpaperEngine/FileSystem/Adapters/Package.cpp index cf3cdf5..ac43768 100644 --- a/src/WallpaperEngine/FileSystem/Adapters/Package.cpp +++ b/src/WallpaperEngine/FileSystem/Adapters/Package.cpp @@ -45,7 +45,7 @@ bool PackageAdapter::exists (const std::filesystem::path& path) const { } std::filesystem::path PackageAdapter::physicalPath (const std::filesystem::path& path) const { - throw Render::AssetLoadException ("Package adapter does not support realpath", path); + throw std::filesystem::filesystem_error ("Package adapter does not support realpath", path, std::error_code ()); } bool PackageFactory::handlesMountpoint (const std::filesystem::path& path) const { diff --git a/src/WallpaperEngine/FileSystem/Adapters/Virtual.cpp b/src/WallpaperEngine/FileSystem/Adapters/Virtual.cpp index 7363ec4..bec3576 100644 --- a/src/WallpaperEngine/FileSystem/Adapters/Virtual.cpp +++ b/src/WallpaperEngine/FileSystem/Adapters/Virtual.cpp @@ -22,7 +22,7 @@ bool VirtualAdapter::exists (const std::filesystem::path& path) const { } std::filesystem::path VirtualAdapter::physicalPath (const std::filesystem::path& path) const { - throw Render::AssetLoadException ("Virtual adapter does not support realpath", path); + throw std::filesystem::filesystem_error ("Virtual adapter does not support realpath", path, std::error_code ()); } diff --git a/src/WallpaperEngine/FileSystem/Container.cpp b/src/WallpaperEngine/FileSystem/Container.cpp index 0b03c5b..ada4c68 100644 --- a/src/WallpaperEngine/FileSystem/Container.cpp +++ b/src/WallpaperEngine/FileSystem/Container.cpp @@ -81,7 +81,6 @@ std::filesystem::path Container::physicalPath (const std::filesystem::path& path return this->resolveAdapterForFile (path).physicalPath (normalized); } - AdapterSharedPtr Container::mount (const std::filesystem::path& path, const std::filesystem::path& mountPoint) { // check if any adapter can handle the path for (const auto& factory : this->m_factories) { @@ -92,7 +91,8 @@ AdapterSharedPtr Container::mount (const std::filesystem::path& path, const std: return this->m_mountpoints.emplace_back (mountPoint, factory->create (path)).second; } - sLog.exception("The specified mount (", path, ") cannot be handled by any of the filesystem adapters"); + throw std::filesystem::filesystem_error ( + "The specified mount cannot be handled by any of the filesystem adapters", path, std::error_code ()); } VirtualAdapter& Container::getVFS () const { @@ -120,5 +120,5 @@ Adapter& Container::resolveAdapterForFile (const std::filesystem::path& path) co return this->resolveAdapterForFile ("/" + normalized.string()); } - throw Render::AssetLoadException ("Cannot find requested file ", path); + throw std::filesystem::filesystem_error ("Cannot find requested file in any of the mountpoints", path, std::error_code ()); } diff --git a/src/WallpaperEngine/Render/CObject.cpp b/src/WallpaperEngine/Render/CObject.cpp index e897a7f..b3efae2 100644 --- a/src/WallpaperEngine/Render/CObject.cpp +++ b/src/WallpaperEngine/Render/CObject.cpp @@ -15,8 +15,8 @@ Wallpapers::CScene& CObject::getScene () const { return this->m_scene; } -const Container& CObject::getContainer () const { - return this->getScene ().getContainer (); +const AssetLocator& CObject::getAssetLocator () const { + return this->getScene ().getAssetLocator (); } int CObject::getId () const { diff --git a/src/WallpaperEngine/Render/CObject.h b/src/WallpaperEngine/Render/CObject.h index 52c643d..64acda1 100644 --- a/src/WallpaperEngine/Render/CObject.h +++ b/src/WallpaperEngine/Render/CObject.h @@ -36,7 +36,7 @@ class CObject : public Helpers::ContextAware { virtual void render () = 0; [[nodiscard]] Wallpapers::CScene& getScene () const; - [[nodiscard]] const Container& getContainer () const; + [[nodiscard]] const AssetLocator& getAssetLocator () const; [[nodiscard]] int getId () const; protected: diff --git a/src/WallpaperEngine/Render/CWallpaper.cpp b/src/WallpaperEngine/Render/CWallpaper.cpp index fe74109..5bd6d0a 100644 --- a/src/WallpaperEngine/Render/CWallpaper.cpp +++ b/src/WallpaperEngine/Render/CWallpaper.cpp @@ -45,8 +45,8 @@ CWallpaper::CWallpaper ( CWallpaper::~CWallpaper () = default; -const Container& CWallpaper::getContainer () const { - return *this->m_wallpaperData.project.container; +const AssetLocator& CWallpaper::getAssetLocator () const { + return *this->m_wallpaperData.project.assetLocator; } const Wallpaper& CWallpaper::getWallpaperData () const { diff --git a/src/WallpaperEngine/Render/CWallpaper.h b/src/WallpaperEngine/Render/CWallpaper.h index 0dd7270..28d2ffc 100644 --- a/src/WallpaperEngine/Render/CWallpaper.h +++ b/src/WallpaperEngine/Render/CWallpaper.h @@ -65,7 +65,7 @@ class CWallpaper : public Helpers::ContextAware, public FBOProvider { /** * @return The container to resolve files for this wallpaper */ - [[nodiscard]] const Container& getContainer () const; + [[nodiscard]] const AssetLocator& getAssetLocator () const; /** * @return The current audio context for this wallpaper diff --git a/src/WallpaperEngine/Render/Objects/CSound.cpp b/src/WallpaperEngine/Render/Objects/CSound.cpp index 814a8a4..bb82da8 100644 --- a/src/WallpaperEngine/Render/Objects/CSound.cpp +++ b/src/WallpaperEngine/Render/Objects/CSound.cpp @@ -23,7 +23,7 @@ CSound::~CSound() { void CSound::load () { for (const auto& cur : this->m_sound.sounds) { uint32_t filesize = 0; - auto filebuffer = this->getContainer ().read (cur); + auto filebuffer = this->getAssetLocator ().read (cur); auto stream = new Audio::AudioStream (this->getScene ().getAudioContext (), filebuffer, filesize); stream->setRepeat (this->m_sound.playbackmode.has_value() && this->m_sound.playbackmode == "loop"); diff --git a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp index 0e23322..d6aba30 100644 --- a/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp +++ b/src/WallpaperEngine/Render/Objects/Effects/CPass.cpp @@ -427,7 +427,7 @@ void CPass::setupShaders () { // TODO: USED TO BUILD THE TEXTURES LATER // use the combos copied from the pass so it includes the texture format this->m_shader = new Render::Shaders::Shader ( - this->m_image.getContainer (), this->m_pass.shader, this->m_combos, this->m_override.combos, + this->m_image.getAssetLocator (), this->m_pass.shader, this->m_combos, this->m_override.combos, this->m_pass.textures, this->m_override.textures, this->m_override.constants ); diff --git a/src/WallpaperEngine/Render/Shaders/Shader.cpp b/src/WallpaperEngine/Render/Shaders/Shader.cpp index e833290..ee88b16 100644 --- a/src/WallpaperEngine/Render/Shaders/Shader.cpp +++ b/src/WallpaperEngine/Render/Shaders/Shader.cpp @@ -10,62 +10,24 @@ #include "GLSLContext.h" #include "WallpaperEngine/Assets/AssetLoadException.h" +#include "WallpaperEngine/Assets/AssetLocator.h" #include "WallpaperEngine/FileSystem/Container.h" -using namespace WallpaperEngine::Render; +using namespace WallpaperEngine::Assets; namespace WallpaperEngine::Render::Shaders { -// TODO: MOVE THIS INTO AN ASSET RESOLVER OR SOMETHING SIMILAR AS IT DOESN'T REALLY BELONG HERE BUT GETS THE CHANGESET OUT -std::string readShader (const std::filesystem::path& filename, const Container& container) { - std::filesystem::path shader = filename; - auto it = shader.begin (); - - // detect workshop shaders and check if there's a - if (*it++ == "workshop") { - const std::filesystem::path workshopId = *it++; - - if (++it != shader.end ()) { - const std::filesystem::path& shaderfile = *it; - - try { - shader = std::filesystem::path ("zcompat") / "scene" / "shaders" / workshopId / shaderfile; - // replace the old path with the new one - std::string contents = container.readString (shader); - - sLog.out ("Replaced ", filename, " with compat ", shader); - - return contents; - } catch (AssetLoadException&) { } - } - } - - return container.readString ("shaders" / filename); -} - -std::string readVertex (const std::filesystem::path& filename, const Container& container) { - std::filesystem::path shader = filename; - shader.replace_extension (".vert"); - return readShader (shader, container); -} - -std::string readFragment (const std::filesystem::path& filename, const Container& container) { - std::filesystem::path shader = filename; - shader.replace_extension (".frag"); - return readShader (shader, container); -} - Shader::Shader ( - const Container& container, std::string filename, + const AssetLocator& assetLocator, std::string filename, const ComboMap& combos, const ComboMap& overrideCombos, const TextureMap& textures, const TextureMap& overrideTextures, const ShaderConstantMap& constants ) : m_vertex ( - GLSLContext::UnitType_Vertex, filename, readVertex (filename, container), - container, constants, textures, overrideTextures, combos, overrideCombos), + GLSLContext::UnitType_Vertex, filename, assetLocator.vertexShader (filename), + assetLocator, constants, textures, overrideTextures, combos, overrideCombos), m_fragment ( - GLSLContext::UnitType_Fragment, filename, readFragment (filename, container), - container, constants, textures, overrideTextures, combos, overrideCombos), + GLSLContext::UnitType_Fragment, filename, assetLocator.fragmentShader (filename), + assetLocator, constants, textures, overrideTextures, combos, overrideCombos), m_file (std::move (filename)), m_combos (combos), m_overrideCombos (overrideCombos), diff --git a/src/WallpaperEngine/Render/Shaders/Shader.h b/src/WallpaperEngine/Render/Shaders/Shader.h index 0411c2a..50b51e1 100644 --- a/src/WallpaperEngine/Render/Shaders/Shader.h +++ b/src/WallpaperEngine/Render/Shaders/Shader.h @@ -31,7 +31,7 @@ class Shader { * the pre-processing and compilation of the shader, adding * required definitions if needed * - * @param container The container to use for file lookup + * @param assetLocator The asset locator where to ask for assets for * @param filename The file to load * @param combos Settings for the shader * @param overrideCombos List of override combos to use @@ -40,7 +40,7 @@ class Shader { * @param constants Default values for shader variables */ Shader ( - const Container& container, std::string filename, + const AssetLocator& assetLocator, std::string filename, const ComboMap& combos, const ComboMap& overrideCombos, const TextureMap& textures, const TextureMap& overrideTextures, const ShaderConstantMap& constants); diff --git a/src/WallpaperEngine/Render/Shaders/ShaderUnit.cpp b/src/WallpaperEngine/Render/Shaders/ShaderUnit.cpp index 62272fd..499cff1 100644 --- a/src/WallpaperEngine/Render/Shaders/ShaderUnit.cpp +++ b/src/WallpaperEngine/Render/Shaders/ShaderUnit.cpp @@ -53,7 +53,7 @@ using namespace WallpaperEngine::Data::Builders; using namespace WallpaperEngine::Render::Shaders; ShaderUnit::ShaderUnit ( - GLSLContext::UnitType type, std::string file, std::string content, const Container& container, + GLSLContext::UnitType type, std::string file, std::string content, const AssetLocator& assetLocator, const ShaderConstantMap& constants, const TextureMap& passTextures, const TextureMap& overrideTextures, const ComboMap& combos, const ComboMap& overrideCombos ) : @@ -66,7 +66,7 @@ ShaderUnit::ShaderUnit ( m_passTextures (passTextures), m_overrideTextures (overrideTextures), m_link (nullptr), - m_container (container) { + m_assetLocator (assetLocator) { // pre-process the shader so the units are clear this->preprocess (); } @@ -153,7 +153,7 @@ void ShaderUnit::preprocessIncludes () { content += filename; content += "\n"; // TODO: MOVE SHADER PATH FROM HERE TO SOMEWHERE LIKE AN ASSET RESOLVER OR SOMETHING LIKE THAT - content += this->m_container.readString (std::filesystem::path("shaders") / filename); + content += this->m_assetLocator.readString (std::filesystem::path("shaders") / filename); content += "\n// end of included from file "; content += filename; content += "\n"; @@ -193,7 +193,7 @@ void ShaderUnit::preprocessIncludes () { content += filename; content += "\n"; // TODO: MOVE SHADER PATH FROM HERE TO SOMEWHERE LIKE AN ASSET RESOLVER OR SOMETHING LIKE THAT - content += this->m_container.readString (std::filesystem::path("shaders") / filename); + content += this->m_assetLocator.readString (std::filesystem::path("shaders") / filename); content += "\n// end of included from file "; content += filename; content += "\n"; diff --git a/src/WallpaperEngine/Render/Shaders/ShaderUnit.h b/src/WallpaperEngine/Render/Shaders/ShaderUnit.h index 00ed412..2a805ef 100644 --- a/src/WallpaperEngine/Render/Shaders/ShaderUnit.h +++ b/src/WallpaperEngine/Render/Shaders/ShaderUnit.h @@ -6,6 +6,7 @@ #include "GLSLContext.h" #include "WallpaperEngine/Data/JSON.h" +#include "WallpaperEngine/Assets/AssetLocator.h" #include "WallpaperEngine/Render/Shaders/Variables/ShaderVariable.h" #include "nlohmann/json.hpp" @@ -13,7 +14,7 @@ namespace WallpaperEngine::Render::Shaders { using JSON = WallpaperEngine::Data::JSON::JSON; -using namespace WallpaperEngine::FileSystem; +using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Data::Model; /** @@ -22,7 +23,7 @@ using namespace WallpaperEngine::Data::Model; class ShaderUnit { public: ShaderUnit ( - GLSLContext::UnitType type, std::string file, std::string content, const Container& container, + GLSLContext::UnitType type, std::string file, std::string content, const AssetLocator& assetLocator, const ShaderConstantMap& constants, const TextureMap& passTextures, const TextureMap& overrideTextures, const ComboMap& combos, const ComboMap& overrideCombos); ~ShaderUnit () = default; @@ -158,6 +159,6 @@ class ShaderUnit { /** * The container to source files from */ - const Container& m_container; + const AssetLocator& m_assetLocator; }; } diff --git a/src/WallpaperEngine/Render/TextureCache.cpp b/src/WallpaperEngine/Render/TextureCache.cpp index a2aa139..8588bec 100644 --- a/src/WallpaperEngine/Render/TextureCache.cpp +++ b/src/WallpaperEngine/Render/TextureCache.cpp @@ -25,7 +25,7 @@ std::shared_ptr TextureCache::resolve (const std::string& // search for the texture in all the different containers just in case for (const auto& [name, project] : this->getContext ().getApp ().getBackgrounds ()) { try { - auto contents = project->container->read (finalFilename); + auto contents = project->assetLocator->read (finalFilename); auto stream = BinaryReader (contents); auto parsedTexture = TextureParser::parse (stream); @@ -39,7 +39,7 @@ std::shared_ptr TextureCache::resolve (const std::string& } } - throw AssetLoadException (filename, "Cannot find file"); + throw AssetLoadException ("Cannot find file", filename, std::error_code ()); } void TextureCache::store (const std::string& name, std::shared_ptr texture) { diff --git a/src/WallpaperEngine/Render/Wallpapers/CVideo.cpp b/src/WallpaperEngine/Render/Wallpapers/CVideo.cpp index 5602bf1..31211a3 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CVideo.cpp +++ b/src/WallpaperEngine/Render/Wallpapers/CVideo.cpp @@ -57,7 +57,7 @@ CVideo::CVideo ( sLog.exception ("Failed to initialize MPV's GL context"); const std::filesystem::path videopath = - this->getVideo ().project.container->physicalPath (this->getVideo ().filename); + this->getVideo ().project.assetLocator->physicalPath (this->getVideo ().filename); // build the path to the video file const char* command [] = {"loadfile", videopath.c_str (), nullptr}; diff --git a/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.cpp b/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.cpp index d02aa55..9dc3116 100644 --- a/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.cpp +++ b/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.cpp @@ -7,12 +7,11 @@ #include "WallpaperEngine/Data/Model/Project.h" -using namespace WallpaperEngine::Render; using namespace WallpaperEngine::WebBrowser::CEF; WPSchemeHandler::WPSchemeHandler(const Project& project) : m_project (project), - m_container (*this->m_project.container) { + m_assetLoader (*this->m_project.assetLocator) { } bool WPSchemeHandler::Open(CefRefPtr request, @@ -48,7 +47,7 @@ bool WPSchemeHandler::Open(CefRefPtr request, this->m_mimeType = mime; } - this->m_contents = this->m_container.read (file); + this->m_contents = this->m_assetLoader.read (file); callback->Continue (); } catch (AssetLoadException&) { #if !NDEBUG diff --git a/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.h b/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.h index 60640cf..8753a74 100644 --- a/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.h +++ b/src/WallpaperEngine/WebBrowser/CEF/WPSchemeHandler.h @@ -2,7 +2,7 @@ #include -#include "WallpaperEngine/FileSystem/Container.h" +#include "WallpaperEngine/Assets/AssetLocator.h" #include "include/cef_resource_handler.h" #include "include/wrapper/cef_helpers.h" @@ -13,7 +13,7 @@ struct Project; namespace WallpaperEngine::WebBrowser::CEF { -using namespace WallpaperEngine::FileSystem; +using namespace WallpaperEngine::Assets; using namespace WallpaperEngine::Data::Model; /** @@ -39,7 +39,7 @@ class WPSchemeHandler : public CefResourceHandler { private: const Project& m_project; - const Container& m_container; + const AssetLocator& m_assetLoader; ReadStreamSharedPtr m_contents = nullptr; std::string m_mimeType;