From 118c2f3ce2f4f36f0ecaa236fd2906ec789b6c84 Mon Sep 17 00:00:00 2001 From: Hyper <34012267+hyperbx@users.noreply.github.com> Date: Fri, 15 Nov 2024 21:21:43 +0000 Subject: [PATCH] config_detail: move implementation to cpp, relocate sources --- UnleashedRecomp/CMakeLists.txt | 7 +- UnleashedRecomp/{ => cfg}/config.cpp | 4 +- UnleashedRecomp/{ => cfg}/config.h | 12 +-- UnleashedRecomp/cfg/config_detail.cpp | 28 +++++++ UnleashedRecomp/{ => cfg}/config_detail.h | 88 ++++++-------------- UnleashedRecomp/gpu/video.cpp | 6 +- UnleashedRecomp/kernel/imports.cpp | 3 +- UnleashedRecomp/main.cpp | 2 +- UnleashedRecomp/patches/fps_patches.cpp | 6 +- UnleashedRecomp/patches/misc_patches.cpp | 6 +- UnleashedRecomp/patches/player_patches.cpp | 8 +- UnleashedRecomp/patches/resident_patches.cpp | 2 +- UnleashedRecomp/patches/video_patches.cpp | 6 +- UnleashedRecomp/ui/window.cpp | 2 +- UnleashedRecomp/ui/window.h | 8 +- 15 files changed, 91 insertions(+), 97 deletions(-) rename UnleashedRecomp/{ => cfg}/config.cpp (93%) rename UnleashedRecomp/{ => cfg}/config.h (88%) create mode 100644 UnleashedRecomp/cfg/config_detail.cpp rename UnleashedRecomp/{ => cfg}/config_detail.h (68%) diff --git a/UnleashedRecomp/CMakeLists.txt b/UnleashedRecomp/CMakeLists.txt index eaadca2..ff8536d 100644 --- a/UnleashedRecomp/CMakeLists.txt +++ b/UnleashedRecomp/CMakeLists.txt @@ -28,6 +28,11 @@ set(SWA_PRECOMPILED_HEADERS "stdafx.h" ) +set(SWA_CFG_CXX_SOURCES + "cfg/config.cpp" + "cfg/config_detail.cpp" +) + set(SWA_KERNEL_CXX_SOURCES "kernel/imports.cpp" "kernel/xdm.cpp" @@ -74,11 +79,11 @@ set(SWA_UI_CXX_SOURCES ) set(SWA_CXX_SOURCES - "config.cpp" "main.cpp" "misc_impl.cpp" "stdafx.cpp" + ${SWA_CFG_CXX_SOURCES} ${SWA_KERNEL_CXX_SOURCES} ${SWA_CPU_CXX_SOURCES} ${SWA_GPU_CXX_SOURCES} diff --git a/UnleashedRecomp/config.cpp b/UnleashedRecomp/cfg/config.cpp similarity index 93% rename from UnleashedRecomp/config.cpp rename to UnleashedRecomp/cfg/config.cpp index 5643640..810f1f3 100644 --- a/UnleashedRecomp/config.cpp +++ b/UnleashedRecomp/cfg/config.cpp @@ -14,7 +14,7 @@ void Config::Load() { auto toml = toml::parse_file(configPath.string()); - for (auto def : g_configDefs) + for (auto def : Config::Definitions) { def->ReadValue(toml); #if _DEBUG @@ -38,7 +38,7 @@ void Config::Save() std::string result; std::string section; - for (auto def : g_configDefs) + for (auto def : Config::Definitions) { auto isFirstSection = section.empty(); auto isDefWithSection = section != def->GetSection(); diff --git a/UnleashedRecomp/config.h b/UnleashedRecomp/cfg/config.h similarity index 88% rename from UnleashedRecomp/config.h rename to UnleashedRecomp/cfg/config.h index 6a71f3c..7116f27 100644 --- a/UnleashedRecomp/config.h +++ b/UnleashedRecomp/cfg/config.h @@ -5,13 +5,15 @@ class Config { public: + inline static std::vector Definitions{}; + CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English); CONFIG_DEFINE("System", bool, Hints, true); CONFIG_DEFINE("System", bool, ControlTutorial, true); CONFIG_DEFINE_ENUM("System", EScoreBehaviour, ScoreBehaviour, EScoreBehaviour::CheckpointReset); CONFIG_DEFINE("System", bool, UnleashOutOfControlDrain, true); CONFIG_DEFINE("System", bool, WerehogHubTransformVideo, true); - CONFIG_DEFINE_HIDE("System", bool, LogoSkip, false); + CONFIG_DEFINE("System", bool, LogoSkip, false); CONFIG_DEFINE("Controls", bool, CameraXInvert, false); CONFIG_DEFINE("Controls", bool, CameraYInvert, false); @@ -25,11 +27,11 @@ public: CONFIG_DEFINE("Audio", bool, WerehogBattleMusic, true); CONFIG_DEFINE_ENUM("Video", EGraphicsAPI, GraphicsAPI, EGraphicsAPI::D3D12); - CONFIG_DEFINE_HIDE("Video", int32_t, WindowX, WINDOWPOS_CENTRED); - CONFIG_DEFINE_HIDE("Video", int32_t, WindowY, WINDOWPOS_CENTRED); + CONFIG_DEFINE("Video", int32_t, WindowX, WINDOWPOS_CENTRED); + CONFIG_DEFINE("Video", int32_t, WindowY, WINDOWPOS_CENTRED); CONFIG_DEFINE("Video", int32_t, WindowWidth, 1280); CONFIG_DEFINE("Video", int32_t, WindowHeight, 720); - CONFIG_DEFINE_ENUM_HIDE("Video", EWindowState, WindowState, EWindowState::Normal); + CONFIG_DEFINE_ENUM("Video", EWindowState, WindowState, EWindowState::Normal); CONFIG_DEFINE_CALLBACK("Video", float, ResolutionScale, 1.0f, { @@ -42,7 +44,7 @@ public: CONFIG_DEFINE("Video", int32_t, FPS, 60); CONFIG_DEFINE("Video", float, Brightness, 0.5f); CONFIG_DEFINE("Video", size_t, MSAA, 4); - CONFIG_DEFINE_HIDE("Video", size_t, AnisotropicFiltering, 16); + CONFIG_DEFINE("Video", size_t, AnisotropicFiltering, 16); CONFIG_DEFINE_ENUM("Video", EShadowResolution, ShadowResolution, EShadowResolution::x4096); CONFIG_DEFINE_ENUM("Video", EGITextureFiltering, GITextureFiltering, EGITextureFiltering::Bicubic); CONFIG_DEFINE("Video", bool, AlphaToCoverage, true); diff --git a/UnleashedRecomp/cfg/config_detail.cpp b/UnleashedRecomp/cfg/config_detail.cpp new file mode 100644 index 0000000..d48bbec --- /dev/null +++ b/UnleashedRecomp/cfg/config_detail.cpp @@ -0,0 +1,28 @@ +#include "config.h" +#include "config_detail.h" + +// CONFIG_DEFINE +template +ConfigDef::ConfigDef(std::string section, std::string name, T defaultValue) : Section(section), Name(name), DefaultValue(defaultValue) +{ + Config::Definitions.emplace_back(this); +} + +// CONFIG_DEFINE_ENUM +template +ConfigDef::ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map enumTemplate) + : Section(section), Name(name), DefaultValue(defaultValue), EnumTemplate(enumTemplate) +{ + for (const auto& pair : EnumTemplate) + EnumTemplateReverse[pair.second] = pair.first; + + Config::Definitions.emplace_back(this); +} + +// CONFIG_DEFINE_CALLBACK +template +ConfigDef::ConfigDef(std::string section, std::string name, T defaultValue, std::function*)> readCallback) + : Section(section), Name(name), DefaultValue(defaultValue), ReadCallback(readCallback) +{ + Config::Definitions.emplace_back(this); +} diff --git a/UnleashedRecomp/config_detail.h b/UnleashedRecomp/cfg/config_detail.h similarity index 68% rename from UnleashedRecomp/config_detail.h rename to UnleashedRecomp/cfg/config_detail.h index e02e5a9..f2c883f 100644 --- a/UnleashedRecomp/config_detail.h +++ b/UnleashedRecomp/cfg/config_detail.h @@ -7,33 +7,24 @@ #define CONFIG_DEFINE(section, type, name, defaultValue) \ inline static ConfigDef name{section, #name, defaultValue}; -#define CONFIG_DEFINE_HIDE(section, type, name, defaultValue) \ - inline static ConfigDef name{section, #name, defaultValue}; - #define CONFIG_DEFINE_ENUM_TEMPLATE(type) \ inline static std::unordered_map g_##type##_template = #define CONFIG_DEFINE_ENUM(section, type, name, defaultValue) \ inline static ConfigDef name{section, #name, defaultValue, g_##type##_template}; -#define CONFIG_DEFINE_ENUM_HIDE(section, type, name, defaultValue) \ - inline static ConfigDef name{section, #name, defaultValue, g_##type##_template}; - -#define CONFIG_DEFINE_IMPL(section, type, name, defaultValue, readImpl) \ - inline static ConfigDef name{section, #name, defaultValue, [](ConfigDef* def, const toml::v3::table& table) readImpl}; - #define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) \ - inline static ConfigDef name{section, #name, defaultValue, [](ConfigDef* def) readCallback}; + inline static ConfigDef name{section, #name, defaultValue, [](ConfigDef* def) readCallback}; #define CONFIG_GET_DEFAULT(name) Config::name.DefaultValue #define CONFIG_SET_DEFAULT(name) Config::name.MakeDefault(); #define WINDOWPOS_CENTRED 0x2FFF0000 -class ConfigDefBase +class IConfigDef { public: - virtual ~ConfigDefBase() = default; + virtual ~IConfigDef() = default; virtual void ReadValue(toml::v3::ex::parse_result& toml) = 0; virtual void MakeDefault() = 0; virtual std::string GetSection() const = 0; @@ -42,14 +33,9 @@ public: virtual std::string ToString() const = 0; }; -inline static std::vector g_configDefs{}; - -template -class ConfigDef : public ConfigDefBase +template +class ConfigDef : public IConfigDef { -protected: - bool m_isMenuOption{ isMenuOption }; - public: std::string Section{}; std::string Name{}; @@ -57,35 +43,16 @@ public: T Value{ DefaultValue }; std::unordered_map EnumTemplate{}; std::unordered_map EnumTemplateReverse{}; - std::function*, const toml::v3::table&)> ReadImpl; - std::function*)> ReadCallback; + std::function*)> ReadCallback; - ConfigDef(std::string section, std::string name, T defaultValue) - : Section(section), Name(name), DefaultValue(defaultValue) - { - g_configDefs.emplace_back(this); - } + // CONFIG_DEFINE + ConfigDef(std::string section, std::string name, T defaultValue); - ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map enumTemplate) - : Section(section), Name(name), DefaultValue(defaultValue), EnumTemplate(enumTemplate) - { - for (const auto& pair : EnumTemplate) - EnumTemplateReverse[pair.second] = pair.first; + // CONFIG_DEFINE_ENUM + ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map enumTemplate); - g_configDefs.emplace_back(this); - } - - ConfigDef(std::string section, std::string name, T defaultValue, std::function*)> readCallback) - : Section(section), Name(name), DefaultValue(defaultValue), ReadCallback(readCallback) - { - g_configDefs.emplace_back(this); - } - - ConfigDef(std::string section, std::string name, T defaultValue, std::function*, const toml::v3::table&)> readImpl) - : Section(section), Name(name), DefaultValue(defaultValue), ReadImpl(readImpl) - { - g_configDefs.emplace_back(this); - } + // CONFIG_DEFINE_CALLBACK + ConfigDef(std::string section, std::string name, T defaultValue, std::function*)> readCallback); void ReadValue(toml::v3::ex::parse_result& toml) override { @@ -93,30 +60,23 @@ public: { const auto& section = *pSection; - if (ReadImpl) + if constexpr (std::is_same::value) { - ReadImpl(this, section); + Value = section[Name].value_or(DefaultValue); + } + else if constexpr (std::is_enum_v) + { + auto it = EnumTemplate.begin(); + + Value = EnumTemplate[section[Name].value_or(static_cast(it->first))]; } else { - if constexpr (std::is_same::value) - { - Value = section[Name].value_or(DefaultValue); - } - else if constexpr (std::is_enum_v) - { - auto it = EnumTemplate.begin(); - - Value = EnumTemplate[section[Name].value_or(static_cast(it->first))]; - } - else - { - Value = section[Name].value_or(DefaultValue); - } - - if (ReadCallback) - ReadCallback(this); + Value = section[Name].value_or(DefaultValue); } + + if (ReadCallback) + ReadCallback(this); } } diff --git a/UnleashedRecomp/gpu/video.cpp b/UnleashedRecomp/gpu/video.cpp index b08ca8e..d0d6b01 100644 --- a/UnleashedRecomp/gpu/video.cpp +++ b/UnleashedRecomp/gpu/video.cpp @@ -9,9 +9,9 @@ #include #include "imgui_snapshot.h" -#include "gpu/video.h" -#include "ui/window.h" -#include "config.h" +#include "video.h" +#include +#include #include "shader/copy_vs.hlsl.dxil.h" #include "shader/copy_vs.hlsl.spirv.h" diff --git a/UnleashedRecomp/kernel/imports.cpp b/UnleashedRecomp/kernel/imports.cpp index c5b05f9..277cee8 100644 --- a/UnleashedRecomp/kernel/imports.cpp +++ b/UnleashedRecomp/kernel/imports.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "function.h" #include "xex.h" #include "xbox.h" @@ -12,7 +11,7 @@ #include "xam.h" #include "xdm.h" #include -#include "config.h" +#include #include diff --git a/UnleashedRecomp/main.cpp b/UnleashedRecomp/main.cpp index eef5a1b..846ed8f 100644 --- a/UnleashedRecomp/main.cpp +++ b/UnleashedRecomp/main.cpp @@ -11,7 +11,7 @@ #include #include #include -#include "config.h" +#include #define GAME_XEX_PATH "game:\\default.xex" diff --git a/UnleashedRecomp/patches/fps_patches.cpp b/UnleashedRecomp/patches/fps_patches.cpp index 0542c20..d55c5e9 100644 --- a/UnleashedRecomp/patches/fps_patches.cpp +++ b/UnleashedRecomp/patches/fps_patches.cpp @@ -1,7 +1,7 @@ #include -#include "api/SWA.h" -#include "ui/window.h" -#include "config.h" +#include +#include +#include float m_lastLoadingFrameDelta = 0.0f; std::chrono::steady_clock::time_point m_lastLoadingFrameTime; diff --git a/UnleashedRecomp/patches/misc_patches.cpp b/UnleashedRecomp/patches/misc_patches.cpp index e181c1b..3a523ef 100644 --- a/UnleashedRecomp/patches/misc_patches.cpp +++ b/UnleashedRecomp/patches/misc_patches.cpp @@ -1,7 +1,7 @@ #include -#include "api/SWA.h" -#include "ui/window.h" -#include "config.h" +#include +#include +#include bool DisableHintsMidAsmHook() { diff --git a/UnleashedRecomp/patches/player_patches.cpp b/UnleashedRecomp/patches/player_patches.cpp index b48ff5c..6fe6bda 100644 --- a/UnleashedRecomp/patches/player_patches.cpp +++ b/UnleashedRecomp/patches/player_patches.cpp @@ -1,8 +1,8 @@ #include -#include "api/SWA.h" -#include "ui/window.h" -#include "ui/window_events.h" -#include "config.h" +#include +#include +#include +#include uint32_t m_lastCheckpointScore = 0; float m_lastDarkGaiaEnergy = 0.0f; diff --git a/UnleashedRecomp/patches/resident_patches.cpp b/UnleashedRecomp/patches/resident_patches.cpp index 36264d0..f1606b6 100644 --- a/UnleashedRecomp/patches/resident_patches.cpp +++ b/UnleashedRecomp/patches/resident_patches.cpp @@ -1,5 +1,5 @@ #include -#include "config.h" +#include const char* m_pStageID; diff --git a/UnleashedRecomp/patches/video_patches.cpp b/UnleashedRecomp/patches/video_patches.cpp index 8e931b7..3bf6ebe 100644 --- a/UnleashedRecomp/patches/video_patches.cpp +++ b/UnleashedRecomp/patches/video_patches.cpp @@ -1,7 +1,7 @@ #include -#include "api/SWA.h" -#include "ui/window.h" -#include "config.h" +#include +#include +#include constexpr float m_baseAspectRatio = 16.0f / 9.0f; diff --git a/UnleashedRecomp/ui/window.cpp b/UnleashedRecomp/ui/window.cpp index 0ed84ec..7f58704 100644 --- a/UnleashedRecomp/ui/window.cpp +++ b/UnleashedRecomp/ui/window.cpp @@ -1,6 +1,6 @@ #include "window.h" #include "sdl_listener.h" -#include +#include #include #include diff --git a/UnleashedRecomp/ui/window.h b/UnleashedRecomp/ui/window.h index 4aed02c..cab4fbf 100644 --- a/UnleashedRecomp/ui/window.h +++ b/UnleashedRecomp/ui/window.h @@ -1,9 +1,9 @@ #pragma once -#include "res/icon.h" -#include "res/icon_night.h" -#include "ui/window_events.h" -#include "config.h" +#include +#include +#include +#include #define DEFAULT_WIDTH 1280 #define DEFAULT_HEIGHT 720