From 73f716da84d42f6f1d0f09c6cdd88c80f540393a Mon Sep 17 00:00:00 2001 From: Almamu Date: Sat, 3 May 2025 16:19:38 +0200 Subject: [PATCH 01/23] refactor: new project parser --- CMakeLists.txt | 37 +- .../Application/CWallpaperApplication.cpp | 21 +- .../Assets/CCombinedContainer.cpp | 6 +- .../Assets/CCombinedContainer.h | 4 +- src/WallpaperEngine/Assets/CContainer.h | 1 + src/WallpaperEngine/Core/Core.cpp | 4 +- .../Core/DynamicValues/CDynamicValue.h | 11 + src/WallpaperEngine/Core/Projects/CProperty.h | 4 + .../Core/UserSettings/CUserSettingBoolean.cpp | 11 +- .../Core/UserSettings/CUserSettingBoolean.h | 2 +- .../Core/UserSettings/CUserSettingFloat.cpp | 11 +- .../Core/UserSettings/CUserSettingFloat.h | 2 +- .../Core/UserSettings/CUserSettingVector3.cpp | 11 +- .../Core/UserSettings/CUserSettingVector3.h | 2 +- .../Data/Builders/UserSettingBuilder.h | 23 + .../Data/Builders/VectorBuilder.cpp | 1 + .../Data/Builders/VectorBuilder.h | 178 ++++++++ .../Data/Dumpers/StringPrinter.cpp | 394 ++++++++++++++++++ .../Data/Dumpers/StringPrinter.h | 130 ++++++ src/WallpaperEngine/Data/JSON.cpp | 12 + src/WallpaperEngine/Data/JSON.h | 168 ++++++++ src/WallpaperEngine/Data/Model/Effect.h | 54 +++ src/WallpaperEngine/Data/Model/Material.h | 36 ++ src/WallpaperEngine/Data/Model/Model.h | 27 ++ src/WallpaperEngine/Data/Model/Object.h | 122 ++++++ src/WallpaperEngine/Data/Model/Project.h | 38 ++ src/WallpaperEngine/Data/Model/Types.h | 86 ++++ src/WallpaperEngine/Data/Model/UserSetting.h | 31 ++ src/WallpaperEngine/Data/Model/Wallpaper.h | 119 ++++++ .../Data/Parsers/EffectParser.cpp | 95 +++++ .../Data/Parsers/EffectParser.h | 20 + .../Data/Parsers/MaterialParser.cpp | 86 ++++ .../Data/Parsers/MaterialParser.h | 19 + .../Data/Parsers/ModelParser.cpp | 31 ++ .../Data/Parsers/ModelParser.h | 15 + .../Data/Parsers/ObjectParser.cpp | 187 +++++++++ .../Data/Parsers/ObjectParser.h | 32 ++ .../Data/Parsers/ProjectParser.cpp | 68 +++ .../Data/Parsers/ProjectParser.h | 24 ++ .../Data/Parsers/ShaderConstantParser.cpp | 77 ++++ .../Data/Parsers/ShaderConstantParser.h | 15 + .../Data/Parsers/UserSettingParser.cpp | 69 +++ .../Data/Parsers/UserSettingParser.h | 16 + .../Data/Parsers/WallpaperParser.cpp | 106 +++++ .../Data/Parsers/WallpaperParser.h | 20 + src/WallpaperEngine/Data/Utils/TypeCaster.cpp | 1 + src/WallpaperEngine/Data/Utils/TypeCaster.h | 36 ++ 47 files changed, 2429 insertions(+), 34 deletions(-) create mode 100644 src/WallpaperEngine/Data/Builders/UserSettingBuilder.h create mode 100644 src/WallpaperEngine/Data/Builders/VectorBuilder.cpp create mode 100644 src/WallpaperEngine/Data/Builders/VectorBuilder.h create mode 100644 src/WallpaperEngine/Data/Dumpers/StringPrinter.cpp create mode 100644 src/WallpaperEngine/Data/Dumpers/StringPrinter.h create mode 100644 src/WallpaperEngine/Data/JSON.cpp create mode 100644 src/WallpaperEngine/Data/JSON.h create mode 100644 src/WallpaperEngine/Data/Model/Effect.h create mode 100644 src/WallpaperEngine/Data/Model/Material.h create mode 100644 src/WallpaperEngine/Data/Model/Model.h create mode 100644 src/WallpaperEngine/Data/Model/Object.h create mode 100644 src/WallpaperEngine/Data/Model/Project.h create mode 100644 src/WallpaperEngine/Data/Model/Types.h create mode 100644 src/WallpaperEngine/Data/Model/UserSetting.h create mode 100644 src/WallpaperEngine/Data/Model/Wallpaper.h create mode 100644 src/WallpaperEngine/Data/Parsers/EffectParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/EffectParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/MaterialParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/MaterialParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/ModelParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/ModelParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/ObjectParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/ObjectParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/ProjectParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/ProjectParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/ShaderConstantParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/ShaderConstantParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/UserSettingParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/UserSettingParser.h create mode 100644 src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp create mode 100644 src/WallpaperEngine/Data/Parsers/WallpaperParser.h create mode 100644 src/WallpaperEngine/Data/Utils/TypeCaster.cpp create mode 100644 src/WallpaperEngine/Data/Utils/TypeCaster.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d999b35..57bea0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -198,7 +198,8 @@ if(X11_SUPPORT_FOUND) src/WallpaperEngine/Render/Drivers/Output/CX11Output.cpp src/WallpaperEngine/Render/Drivers/Output/CX11Output.h src/WallpaperEngine/Render/Drivers/Detectors/CX11FullScreenDetector.cpp - src/WallpaperEngine/Render/Drivers/Detectors/CX11FullScreenDetector.h) + src/WallpaperEngine/Render/Drivers/Detectors/CX11FullScreenDetector.h + src/WallpaperEngine/Data/Model/UserSetting.h) SET(X11_INCLUDES ${X11_INCLUDE_DIR} ${XRANDR_INCLUDE_DIR}) @@ -510,9 +511,41 @@ add_executable( src/WallpaperEngine/Core/Objects/Images/Materials/CPass.cpp src/WallpaperEngine/Core/Objects/Images/Materials/CPass.h + src/WallpaperEngine/Data/Model/Types.h + src/WallpaperEngine/Data/Model/Project.h + src/WallpaperEngine/Data/Model/Wallpaper.h + src/WallpaperEngine/Data/Model/Object.h + src/WallpaperEngine/Data/Model/Material.h + src/WallpaperEngine/Data/Model/Effect.h + src/WallpaperEngine/Data/Model/Model.h + src/WallpaperEngine/Data/Utils/TypeCaster.cpp + src/WallpaperEngine/Data/Utils/TypeCaster.h + src/WallpaperEngine/Data/Parsers/ProjectParser.cpp + src/WallpaperEngine/Data/Parsers/ProjectParser.h + src/WallpaperEngine/Data/Parsers/WallpaperParser.cpp + src/WallpaperEngine/Data/Parsers/WallpaperParser.h + src/WallpaperEngine/Data/Parsers/UserSettingParser.cpp + src/WallpaperEngine/Data/Parsers/UserSettingParser.h + src/WallpaperEngine/Data/Parsers/ObjectParser.cpp + src/WallpaperEngine/Data/Parsers/ObjectParser.h + src/WallpaperEngine/Data/Parsers/MaterialParser.cpp + src/WallpaperEngine/Data/Parsers/MaterialParser.h + src/WallpaperEngine/Data/Parsers/ModelParser.cpp + src/WallpaperEngine/Data/Parsers/ModelParser.h + src/WallpaperEngine/Data/Parsers/ShaderConstantParser.cpp + src/WallpaperEngine/Data/Parsers/ShaderConstantParser.h + src/WallpaperEngine/Data/Builders/UserSettingBuilder.h + src/WallpaperEngine/Data/Builders/VectorBuilder.cpp + src/WallpaperEngine/Data/Builders/VectorBuilder.h + ${WAYLAND_SOURCES} ${X11_SOURCES} - ${DEMOMODE_SOURCES}) + ${DEMOMODE_SOURCES} + src/WallpaperEngine/Data/Dumpers/StringPrinter.cpp + src/WallpaperEngine/Data/Dumpers/StringPrinter.h + src/WallpaperEngine/Data/JSON.cpp + src/WallpaperEngine/Data/Parsers/EffectParser.cpp + src/WallpaperEngine/Data/Parsers/EffectParser.h) target_link_libraries (linux-wallpaperengine PUBLIC ${OPENGL_LIBRARIES} diff --git a/src/WallpaperEngine/Application/CWallpaperApplication.cpp b/src/WallpaperEngine/Application/CWallpaperApplication.cpp index 0c118e1..a83baf3 100644 --- a/src/WallpaperEngine/Application/CWallpaperApplication.cpp +++ b/src/WallpaperEngine/Application/CWallpaperApplication.cpp @@ -12,6 +12,9 @@ #include "WallpaperEngine/Core/Wallpapers/CWeb.h" #include "WallpaperEngine/Render/Drivers/CVideoFactories.h" +#include "WallpaperEngine/Data/Parsers/ProjectParser.h" +#include "WallpaperEngine/Data/Dumpers/StringPrinter.h" + #if DEMOMODE #include "recording.h" #endif /* DEMOMODE */ @@ -37,18 +40,18 @@ CWallpaperApplication::CWallpaperApplication (CApplicationContext& context) : void CWallpaperApplication::setupContainer (const std::shared_ptr& container, const std::string& bg) const { const std::filesystem::path basepath = bg; - container->add (std::make_shared (basepath)); + container->add (std::make_unique (basepath)); container->addPkg (basepath / "scene.pkg"); container->addPkg (basepath / "gifscene.pkg"); try { - container->add (std::make_shared (this->m_context.settings.general.assets)); + container->add (std::make_unique (this->m_context.settings.general.assets)); } catch (CAssetLoadException&) { sLog.exception ("Cannot find a valid assets folder, resolved to ", this->m_context.settings.general.assets); } // TODO: move this somewhere else? - auto virtualContainer = std::make_shared (); + auto virtualContainer = std::make_unique (); // // Had to get a little creative with the effects to achieve the same bloom effect without any custom code @@ -181,7 +184,7 @@ void CWallpaperApplication::setupContainer (const std::shared_ptradd (virtualContainer); + container->add (std::move(virtualContainer)); } void CWallpaperApplication::loadBackgrounds () { @@ -206,6 +209,16 @@ std::shared_ptr CWallpaperApplication::loadBackground (const std this->setupContainer (container, bg); + // do the parsing with the new parser first + auto json = WallpaperEngine::Data::JSON::JSON::parse (container->readFileAsString ("project.json")); + const auto project = WallpaperEngine::Data::Parsers::ProjectParser::parse (json, container); + + auto dumper = WallpaperEngine::Data::Dumpers::StringPrinter (); + + dumper.printWallpaper (*project->wallpaper); + + std::cout << dumper.str () << std::endl; + return Core::CProject::fromFile ("project.json", container); } diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.cpp b/src/WallpaperEngine/Assets/CCombinedContainer.cpp index b4fb32d..c34104b 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.cpp +++ b/src/WallpaperEngine/Assets/CCombinedContainer.cpp @@ -8,14 +8,14 @@ using namespace WallpaperEngine::Assets; CCombinedContainer::CCombinedContainer () : CContainer () {} -void CCombinedContainer::add (const std::shared_ptr& container) { - this->m_containers.emplace_back (container); +void CCombinedContainer::add (std::unique_ptr container) { + this->m_containers.emplace_back (std::move(container)); } void CCombinedContainer::addPkg (const std::filesystem::path& path) { try { // add the package to the list - this->add (std::make_shared (path)); + this->add (std::make_unique (path)); sLog.out ("Detected ", path.filename (), " file at ", path, ". Adding to list of searchable paths"); } catch (CPackageLoadException&) { // ignore this error, the package file was not found diff --git a/src/WallpaperEngine/Assets/CCombinedContainer.h b/src/WallpaperEngine/Assets/CCombinedContainer.h index 147962e..7d15bb7 100644 --- a/src/WallpaperEngine/Assets/CCombinedContainer.h +++ b/src/WallpaperEngine/Assets/CCombinedContainer.h @@ -19,7 +19,7 @@ class CCombinedContainer final : public CContainer { * * @param container */ - void add (const std::shared_ptr& container); + void add (std::unique_ptr container); /** * Adds the given package to the list * @@ -32,6 +32,6 @@ class CCombinedContainer final : public CContainer { private: /** The list of containers to search files off from */ - std::vector> m_containers {}; + std::vector> m_containers {}; }; }; // namespace WallpaperEngine::Assets \ No newline at end of file diff --git a/src/WallpaperEngine/Assets/CContainer.h b/src/WallpaperEngine/Assets/CContainer.h index e77228c..4990145 100644 --- a/src/WallpaperEngine/Assets/CContainer.h +++ b/src/WallpaperEngine/Assets/CContainer.h @@ -6,6 +6,7 @@ #include namespace WallpaperEngine::Assets { + /** * File container, provides access to files for backgrounds */ diff --git a/src/WallpaperEngine/Core/Core.cpp b/src/WallpaperEngine/Core/Core.cpp index 176dc99..93effef 100644 --- a/src/WallpaperEngine/Core/Core.cpp +++ b/src/WallpaperEngine/Core/Core.cpp @@ -509,7 +509,7 @@ template const T* Core::jsonFindUserConfig ( if (it == data->end () || it->type () == nlohmann::detail::value_t::null) return T::fromScalar (defaultValue); - return T::fromJSON (*it, project); + return T::fromJSON (*it, project.getProperties ()); } template const CUserSettingBoolean* Core::jsonFindUserConfig ( @@ -530,7 +530,7 @@ template const T* Core::jsonFindUserConfig ( if (it == data.end () || it->type () == nlohmann::detail::value_t::null) return T::fromScalar (defaultValue); - return T::fromJSON (*it, project); + return T::fromJSON (*it, project.getProperties ()); } template const CUserSettingBoolean* Core::jsonFindUserConfig ( diff --git a/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h b/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h index c971283..177cb97 100644 --- a/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h +++ b/src/WallpaperEngine/Core/DynamicValues/CDynamicValue.h @@ -5,8 +5,19 @@ #include #include +namespace WallpaperEngine::Data::Parsers { +class UserSettingParser; +} + +namespace WallpaperEngine::Data::Builders { +class UserSettingBuilder; +} + namespace WallpaperEngine::Core::DynamicValues { class CDynamicValue { + //TODO: THIS SHOULD BE CHANGED ONCE THINGS ARE FINISHED + friend class WallpaperEngine::Data::Parsers::UserSettingParser; + friend class WallpaperEngine::Data::Builders::UserSettingBuilder; public: virtual ~CDynamicValue (); diff --git a/src/WallpaperEngine/Core/Projects/CProperty.h b/src/WallpaperEngine/Core/Projects/CProperty.h index cc179c9..f172ae4 100644 --- a/src/WallpaperEngine/Core/Projects/CProperty.h +++ b/src/WallpaperEngine/Core/Projects/CProperty.h @@ -14,6 +14,10 @@ using namespace WallpaperEngine::Core::DynamicValues; */ class CProperty : public CDynamicValue { public: + using UniquePtr = std::unique_ptr; + using SharedPtr = std::shared_ptr; + using WeakPtr = std::weak_ptr; + typedef std::function function_type; virtual ~CProperty () = default; static std::shared_ptr fromJSON (const json& data, const std::string& name); diff --git a/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.cpp b/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.cpp index aea3a1a..d2d8611 100644 --- a/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.cpp +++ b/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.cpp @@ -39,7 +39,7 @@ CUserSettingBoolean::CUserSettingBoolean ( } } -const CUserSettingBoolean* CUserSettingBoolean::fromJSON (const nlohmann::json& data, const CProject& project) { +const CUserSettingBoolean* CUserSettingBoolean::fromJSON (const nlohmann::json& data, const std::map >& properties) { bool hasCondition = false; std::shared_ptr sourceProperty = nullptr; bool defaultValue; @@ -61,11 +61,10 @@ const CUserSettingBoolean* CUserSettingBoolean::fromJSON (const nlohmann::json& jsonFindRequired (userIt, "condition", "Condition for conditional setting must be present"); } - for (const auto& [key, property] : project.getProperties ()) { - if (key == source) { - sourceProperty = property; - break; - } + const auto propertyIt = properties.find (source); + + if (propertyIt != properties.end ()) { + sourceProperty = propertyIt->second; } if (sourceProperty == nullptr) { diff --git a/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h b/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h index 71188b1..ebfd4a0 100644 --- a/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h +++ b/src/WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h @@ -12,7 +12,7 @@ class CUserSettingBoolean : public CUserSettingValue { public: typedef bool data_type; - static const CUserSettingBoolean* fromJSON (const nlohmann::json& data, const CProject& project); + static const CUserSettingBoolean* fromJSON (const nlohmann::json& data, const std::map >& properties); static const CUserSettingBoolean* fromScalar (bool value); private: diff --git a/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.cpp b/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.cpp index d6ebe34..36d58e9 100644 --- a/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.cpp +++ b/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.cpp @@ -33,7 +33,7 @@ CUserSettingFloat::CUserSettingFloat ( } } -const CUserSettingFloat* CUserSettingFloat::fromJSON (const nlohmann::json& data, const CProject& project) { +const CUserSettingFloat* CUserSettingFloat::fromJSON (const nlohmann::json& data, const std::map >& properties) { float defaultValue; std::string source; std::string expectedValue; @@ -55,11 +55,10 @@ const CUserSettingFloat* CUserSettingFloat::fromJSON (const nlohmann::json& data jsonFindRequired (userIt, "condition", "Condition for conditional setting must be present"); } - for (const auto& [key, property] : project.getProperties ()) { - if (key == source) { - sourceProperty = property; - break; - } + const auto propertyIt = properties.find (source); + + if (propertyIt != properties.end ()) { + sourceProperty = propertyIt->second; } if (sourceProperty == nullptr) { diff --git a/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.h b/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.h index b359e63..72c13ad 100644 --- a/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.h +++ b/src/WallpaperEngine/Core/UserSettings/CUserSettingFloat.h @@ -12,7 +12,7 @@ class CUserSettingFloat : public CUserSettingValue { public: typedef float data_type; - static const CUserSettingFloat* fromJSON (const nlohmann::json& data, const CProject& project); + static const CUserSettingFloat* fromJSON (const nlohmann::json& data, const std::map >& properties); static const CUserSettingFloat* fromScalar (float value); private: diff --git a/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.cpp b/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.cpp index aa1e2da..a1c7659 100644 --- a/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.cpp +++ b/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.cpp @@ -33,7 +33,7 @@ CUserSettingVector3::CUserSettingVector3 ( } } -const CUserSettingVector3* CUserSettingVector3::fromJSON (const nlohmann::json& data, const CProject& project) { +const CUserSettingVector3* CUserSettingVector3::fromJSON (const nlohmann::json& data, const std::map >& properties) { bool hasCondition = false; std::shared_ptr sourceProperty = nullptr; glm::vec3 defaultValue; @@ -55,11 +55,10 @@ const CUserSettingVector3* CUserSettingVector3::fromJSON (const nlohmann::json& jsonFindRequired (userIt, "condition", "Condition for conditional setting must be present"); } - for (const auto& [key, property] : project.getProperties ()) { - if (key == source) { - sourceProperty = property; - break; - } + const auto propertyIt = properties.find (source); + + if (propertyIt != properties.end ()) { + sourceProperty = propertyIt->second; } if (sourceProperty == nullptr) { diff --git a/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.h b/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.h index b3d5735..c9609bc 100644 --- a/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.h +++ b/src/WallpaperEngine/Core/UserSettings/CUserSettingVector3.h @@ -14,7 +14,7 @@ class CUserSettingVector3 : public CUserSettingValue { public: typedef glm::vec3 data_type; - static const CUserSettingVector3* fromJSON (const nlohmann::json& data, const CProject& project); + static const CUserSettingVector3* fromJSON (const nlohmann::json& data, const std::map >& properties); static const CUserSettingVector3* fromScalar (glm::vec3 value); private: diff --git a/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h b/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h new file mode 100644 index 0000000..fbd63c9 --- /dev/null +++ b/src/WallpaperEngine/Data/Builders/UserSettingBuilder.h @@ -0,0 +1,23 @@ +#pragma once + +#include "WallpaperEngine/Data/Model/Types.h" +#include "WallpaperEngine/Data/Model/UserSetting.h" + +namespace WallpaperEngine::Data::Builders { +using namespace WallpaperEngine::Data::Model; +class UserSettingBuilder { + public: + template + static UserSettingSharedPtr fromValue (T defaultValue) { + DynamicValueUniquePtr value = std::make_unique (); + + value->update (defaultValue); + + return std::make_shared (UserSetting { + .value = std::move (value), + .property = PropertyWeakPtr (), + .condition = std::nullopt, + }); + } +}; +} // namespace WallpaperEngine::Data::Builders \ No newline at end of file diff --git a/src/WallpaperEngine/Data/Builders/VectorBuilder.cpp b/src/WallpaperEngine/Data/Builders/VectorBuilder.cpp new file mode 100644 index 0000000..03d62e2 --- /dev/null +++ b/src/WallpaperEngine/Data/Builders/VectorBuilder.cpp @@ -0,0 +1 @@ +#include "VectorBuilder.h" diff --git a/src/WallpaperEngine/Data/Builders/VectorBuilder.h b/src/WallpaperEngine/Data/Builders/VectorBuilder.h new file mode 100644 index 0000000..5bf6f17 --- /dev/null +++ b/src/WallpaperEngine/Data/Builders/VectorBuilder.h @@ -0,0 +1,178 @@ +#pragma once + +#include +#include +#include + +#include "WallpaperEngine/Logging/CLog.h" + +namespace WallpaperEngine::Data::Builders { +class VectorBuilder { + private: + /** + * Convert template that calls the proper std::strto* function + * based on the incoming type + * + * @tparam type + * @param str + * @return + */ + template + static type convert (const char* str); + + public: + /** + * Takes the string and returns the vector size (2, 3 or 4) + * + * TODO: THIS SHOULD BE MOVED, RENAMED OR PLACED SOMEWHERE WHERE IT MAKES MORE SENSE + * + * @param str + * @return + */ + static int preparseSize (const std::string& str) { + const char* p = str.c_str (); + const char* first = strchr (p, ' '); + const char* second = first ? strchr (first + 1, ' ') : nullptr; + const char* third = second ? strchr (second + 1, ' ') : nullptr; + + if (first == nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too few values, expected: 2, 3 or 4)"); + } else if (second == nullptr) { + return 2; + } else if (third == nullptr) { + return 3; + } else { + return 4; + } + } + + /** + * Takes a string value and parses it into a glm::vec. + * This particular parsing uses spaces as separators and basic std::strto* functions + * for the actual parsing of the values. + * + * @tparam length Vector length + * @tparam type Vector storage type + * @tparam qualifier Precision qualifier + * + * @param str The string to parse the vector from + * + * @return + */ + template + static glm::vec parse (const std::string& str) { + const char* p = str.c_str (); + + // get up to 4 spaces + const char* first = strchr (p, ' '); + const char* second = first ? strchr (first + 1, ' ') : nullptr; + const char* third = second ? strchr (second + 1, ' ') : nullptr; + + // validate lengths against what was found in the strings + if constexpr (length == 1) { + if (first != nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too many values, expected: ", length, ")"); + } + } else if constexpr (length == 2) { + if (first == nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too few values, expected: ", length, ")"); + } else if (second != nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too many values, expected: ", length, ")"); + } + } else if constexpr (length == 3) { + if (first == nullptr || second == nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too few values, expected: ", length, ")"); + } else if (third != nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too many values, expected: ", length, ")"); + } + } else if constexpr (length == 4) { + if (first == nullptr || second == nullptr || third == nullptr) { + sLog.exception ("Invalid vector format: " + str + " (too few values, expected: ", length, ")"); + } + } else { + sLog.exception ("Invalid vector length: ", length); + } + + // lengths validated, values can be used directly without issues + if constexpr (length == 1) { + return { + convert (p) + }; + } else if constexpr (length == 2) { + return { + convert (p), + convert (first + 1) + }; + } else if constexpr (length == 3) { + return { + convert (p), + convert (first + 1), + convert (second + 1) + }; + } else { + return { + convert (p), + convert (first + 1), + convert (second + 1), + convert (third + 1) + }; + } + } +}; + +template <> +inline float VectorBuilder::convert (const char* str) { + return std::strtof (str, nullptr); +} + +template <> +inline int VectorBuilder::convert (const char* str) { + return std::strtol (str, nullptr, 10); +} + +template <> +inline unsigned int VectorBuilder::convert (const char* str) { + return std::strtoul (str, nullptr, 10); +} + +template <> +inline double VectorBuilder::convert (const char* str) { + return std::strtod (str, nullptr); +} + +template <> +inline uint8_t VectorBuilder::convert (const char* str) { + return std::strtoul (str, nullptr, 10); +} + +template <> +inline uint16_t VectorBuilder::convert (const char* str) { + return std::strtoul (str, nullptr, 10); +} + +template <> +inline uint64_t VectorBuilder::convert (const char* str) { + return std::strtoull (str, nullptr, 10); +} + +template <> +inline int8_t VectorBuilder::convert (const char* str) { + return std::strtol (str, nullptr, 10); +} + +template <> +inline int16_t VectorBuilder::convert (const char* str) { + return std::strtol (str, nullptr, 10); +} + +template <> +inline int64_t VectorBuilder::convert (const char* str) { + return std::strtoll (str, nullptr, 10); +} + +template <> +inline bool VectorBuilder::convert (const char* str) { + return std::strtoul (str, nullptr, 10) > 0; +} + +} // namespace WallpaperEngine::Data::Parsers diff --git a/src/WallpaperEngine/Data/Dumpers/StringPrinter.cpp b/src/WallpaperEngine/Data/Dumpers/StringPrinter.cpp new file mode 100644 index 0000000..1c72c1d --- /dev/null +++ b/src/WallpaperEngine/Data/Dumpers/StringPrinter.cpp @@ -0,0 +1,394 @@ +#include + +#include "StringPrinter.h" + +#include "WallpaperEngine/Data/Model/Wallpaper.h" + +using namespace WallpaperEngine::Data::Dumpers; +using namespace WallpaperEngine::Data::Model; + +StringPrinter::StringPrinter (std::string indentationCharacter) : + m_out (&this->m_buffer), + m_indentationCharacter (std::move(indentationCharacter)) { } + +void StringPrinter::printWallpaper (const Wallpaper& wallpaper) { + bool isScene = wallpaper.is (); + bool isVideo = wallpaper.is