config_detail: move implementation to cpp, relocate sources

This commit is contained in:
Hyper 2024-11-15 21:21:43 +00:00
parent b354c6123d
commit 118c2f3ce2
15 changed files with 91 additions and 97 deletions

View file

@ -28,6 +28,11 @@ set(SWA_PRECOMPILED_HEADERS
"stdafx.h" "stdafx.h"
) )
set(SWA_CFG_CXX_SOURCES
"cfg/config.cpp"
"cfg/config_detail.cpp"
)
set(SWA_KERNEL_CXX_SOURCES set(SWA_KERNEL_CXX_SOURCES
"kernel/imports.cpp" "kernel/imports.cpp"
"kernel/xdm.cpp" "kernel/xdm.cpp"
@ -74,11 +79,11 @@ set(SWA_UI_CXX_SOURCES
) )
set(SWA_CXX_SOURCES set(SWA_CXX_SOURCES
"config.cpp"
"main.cpp" "main.cpp"
"misc_impl.cpp" "misc_impl.cpp"
"stdafx.cpp" "stdafx.cpp"
${SWA_CFG_CXX_SOURCES}
${SWA_KERNEL_CXX_SOURCES} ${SWA_KERNEL_CXX_SOURCES}
${SWA_CPU_CXX_SOURCES} ${SWA_CPU_CXX_SOURCES}
${SWA_GPU_CXX_SOURCES} ${SWA_GPU_CXX_SOURCES}

View file

@ -14,7 +14,7 @@ void Config::Load()
{ {
auto toml = toml::parse_file(configPath.string()); auto toml = toml::parse_file(configPath.string());
for (auto def : g_configDefs) for (auto def : Config::Definitions)
{ {
def->ReadValue(toml); def->ReadValue(toml);
#if _DEBUG #if _DEBUG
@ -38,7 +38,7 @@ void Config::Save()
std::string result; std::string result;
std::string section; std::string section;
for (auto def : g_configDefs) for (auto def : Config::Definitions)
{ {
auto isFirstSection = section.empty(); auto isFirstSection = section.empty();
auto isDefWithSection = section != def->GetSection(); auto isDefWithSection = section != def->GetSection();

View file

@ -5,13 +5,15 @@
class Config class Config
{ {
public: public:
inline static std::vector<IConfigDef*> Definitions{};
CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English); CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English);
CONFIG_DEFINE("System", bool, Hints, true); CONFIG_DEFINE("System", bool, Hints, true);
CONFIG_DEFINE("System", bool, ControlTutorial, true); CONFIG_DEFINE("System", bool, ControlTutorial, true);
CONFIG_DEFINE_ENUM("System", EScoreBehaviour, ScoreBehaviour, EScoreBehaviour::CheckpointReset); CONFIG_DEFINE_ENUM("System", EScoreBehaviour, ScoreBehaviour, EScoreBehaviour::CheckpointReset);
CONFIG_DEFINE("System", bool, UnleashOutOfControlDrain, true); CONFIG_DEFINE("System", bool, UnleashOutOfControlDrain, true);
CONFIG_DEFINE("System", bool, WerehogHubTransformVideo, 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, CameraXInvert, false);
CONFIG_DEFINE("Controls", bool, CameraYInvert, false); CONFIG_DEFINE("Controls", bool, CameraYInvert, false);
@ -25,11 +27,11 @@ public:
CONFIG_DEFINE("Audio", bool, WerehogBattleMusic, true); CONFIG_DEFINE("Audio", bool, WerehogBattleMusic, true);
CONFIG_DEFINE_ENUM("Video", EGraphicsAPI, GraphicsAPI, EGraphicsAPI::D3D12); CONFIG_DEFINE_ENUM("Video", EGraphicsAPI, GraphicsAPI, EGraphicsAPI::D3D12);
CONFIG_DEFINE_HIDE("Video", int32_t, WindowX, WINDOWPOS_CENTRED); CONFIG_DEFINE("Video", int32_t, WindowX, WINDOWPOS_CENTRED);
CONFIG_DEFINE_HIDE("Video", int32_t, WindowY, WINDOWPOS_CENTRED); CONFIG_DEFINE("Video", int32_t, WindowY, WINDOWPOS_CENTRED);
CONFIG_DEFINE("Video", int32_t, WindowWidth, 1280); CONFIG_DEFINE("Video", int32_t, WindowWidth, 1280);
CONFIG_DEFINE("Video", int32_t, WindowHeight, 720); 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, CONFIG_DEFINE_CALLBACK("Video", float, ResolutionScale, 1.0f,
{ {
@ -42,7 +44,7 @@ public:
CONFIG_DEFINE("Video", int32_t, FPS, 60); CONFIG_DEFINE("Video", int32_t, FPS, 60);
CONFIG_DEFINE("Video", float, Brightness, 0.5f); CONFIG_DEFINE("Video", float, Brightness, 0.5f);
CONFIG_DEFINE("Video", size_t, MSAA, 4); 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", EShadowResolution, ShadowResolution, EShadowResolution::x4096);
CONFIG_DEFINE_ENUM("Video", EGITextureFiltering, GITextureFiltering, EGITextureFiltering::Bicubic); CONFIG_DEFINE_ENUM("Video", EGITextureFiltering, GITextureFiltering, EGITextureFiltering::Bicubic);
CONFIG_DEFINE("Video", bool, AlphaToCoverage, true); CONFIG_DEFINE("Video", bool, AlphaToCoverage, true);

View file

@ -0,0 +1,28 @@
#include "config.h"
#include "config_detail.h"
// CONFIG_DEFINE
template<typename T>
ConfigDef<T>::ConfigDef(std::string section, std::string name, T defaultValue) : Section(section), Name(name), DefaultValue(defaultValue)
{
Config::Definitions.emplace_back(this);
}
// CONFIG_DEFINE_ENUM
template<typename T>
ConfigDef<T>::ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map<std::string, T> 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<typename T>
ConfigDef<T>::ConfigDef(std::string section, std::string name, T defaultValue, std::function<void(ConfigDef<T>*)> readCallback)
: Section(section), Name(name), DefaultValue(defaultValue), ReadCallback(readCallback)
{
Config::Definitions.emplace_back(this);
}

View file

@ -7,33 +7,24 @@
#define CONFIG_DEFINE(section, type, name, defaultValue) \ #define CONFIG_DEFINE(section, type, name, defaultValue) \
inline static ConfigDef<type> name{section, #name, defaultValue}; inline static ConfigDef<type> name{section, #name, defaultValue};
#define CONFIG_DEFINE_HIDE(section, type, name, defaultValue) \
inline static ConfigDef<type, false> name{section, #name, defaultValue};
#define CONFIG_DEFINE_ENUM_TEMPLATE(type) \ #define CONFIG_DEFINE_ENUM_TEMPLATE(type) \
inline static std::unordered_map<std::string, type> g_##type##_template = inline static std::unordered_map<std::string, type> g_##type##_template =
#define CONFIG_DEFINE_ENUM(section, type, name, defaultValue) \ #define CONFIG_DEFINE_ENUM(section, type, name, defaultValue) \
inline static ConfigDef<type> name{section, #name, defaultValue, g_##type##_template}; inline static ConfigDef<type> name{section, #name, defaultValue, g_##type##_template};
#define CONFIG_DEFINE_ENUM_HIDE(section, type, name, defaultValue) \
inline static ConfigDef<type, false> name{section, #name, defaultValue, g_##type##_template};
#define CONFIG_DEFINE_IMPL(section, type, name, defaultValue, readImpl) \
inline static ConfigDef<type> name{section, #name, defaultValue, [](ConfigDef<type, true>* def, const toml::v3::table& table) readImpl};
#define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) \ #define CONFIG_DEFINE_CALLBACK(section, type, name, defaultValue, readCallback) \
inline static ConfigDef<type> name{section, #name, defaultValue, [](ConfigDef<type, true>* def) readCallback}; inline static ConfigDef<type> name{section, #name, defaultValue, [](ConfigDef<type>* def) readCallback};
#define CONFIG_GET_DEFAULT(name) Config::name.DefaultValue #define CONFIG_GET_DEFAULT(name) Config::name.DefaultValue
#define CONFIG_SET_DEFAULT(name) Config::name.MakeDefault(); #define CONFIG_SET_DEFAULT(name) Config::name.MakeDefault();
#define WINDOWPOS_CENTRED 0x2FFF0000 #define WINDOWPOS_CENTRED 0x2FFF0000
class ConfigDefBase class IConfigDef
{ {
public: public:
virtual ~ConfigDefBase() = default; virtual ~IConfigDef() = default;
virtual void ReadValue(toml::v3::ex::parse_result& toml) = 0; virtual void ReadValue(toml::v3::ex::parse_result& toml) = 0;
virtual void MakeDefault() = 0; virtual void MakeDefault() = 0;
virtual std::string GetSection() const = 0; virtual std::string GetSection() const = 0;
@ -42,14 +33,9 @@ public:
virtual std::string ToString() const = 0; virtual std::string ToString() const = 0;
}; };
inline static std::vector<ConfigDefBase*> g_configDefs{}; template<typename T>
class ConfigDef : public IConfigDef
template<typename T, bool isMenuOption = true>
class ConfigDef : public ConfigDefBase
{ {
protected:
bool m_isMenuOption{ isMenuOption };
public: public:
std::string Section{}; std::string Section{};
std::string Name{}; std::string Name{};
@ -57,35 +43,16 @@ public:
T Value{ DefaultValue }; T Value{ DefaultValue };
std::unordered_map<std::string, T> EnumTemplate{}; std::unordered_map<std::string, T> EnumTemplate{};
std::unordered_map<T, std::string> EnumTemplateReverse{}; std::unordered_map<T, std::string> EnumTemplateReverse{};
std::function<void(ConfigDef<T, isMenuOption>*, const toml::v3::table&)> ReadImpl; std::function<void(ConfigDef<T>*)> ReadCallback;
std::function<void(ConfigDef<T, isMenuOption>*)> ReadCallback;
ConfigDef(std::string section, std::string name, T defaultValue) // CONFIG_DEFINE
: Section(section), Name(name), DefaultValue(defaultValue) ConfigDef(std::string section, std::string name, T defaultValue);
{
g_configDefs.emplace_back(this);
}
ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map<std::string, T> enumTemplate) // CONFIG_DEFINE_ENUM
: Section(section), Name(name), DefaultValue(defaultValue), EnumTemplate(enumTemplate) ConfigDef(std::string section, std::string name, T defaultValue, std::unordered_map<std::string, T> enumTemplate);
{
for (const auto& pair : EnumTemplate)
EnumTemplateReverse[pair.second] = pair.first;
g_configDefs.emplace_back(this); // CONFIG_DEFINE_CALLBACK
} ConfigDef(std::string section, std::string name, T defaultValue, std::function<void(ConfigDef<T>*)> readCallback);
ConfigDef(std::string section, std::string name, T defaultValue, std::function<void(ConfigDef<T, isMenuOption>*)> 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<void(ConfigDef<T, isMenuOption>*, const toml::v3::table&)> readImpl)
: Section(section), Name(name), DefaultValue(defaultValue), ReadImpl(readImpl)
{
g_configDefs.emplace_back(this);
}
void ReadValue(toml::v3::ex::parse_result& toml) override void ReadValue(toml::v3::ex::parse_result& toml) override
{ {
@ -93,30 +60,23 @@ public:
{ {
const auto& section = *pSection; const auto& section = *pSection;
if (ReadImpl) if constexpr (std::is_same<T, std::string>::value)
{ {
ReadImpl(this, section); Value = section[Name].value_or<std::string>(DefaultValue);
}
else if constexpr (std::is_enum_v<T>)
{
auto it = EnumTemplate.begin();
Value = EnumTemplate[section[Name].value_or<std::string>(static_cast<std::string>(it->first))];
} }
else else
{ {
if constexpr (std::is_same<T, std::string>::value) Value = section[Name].value_or(DefaultValue);
{
Value = section[Name].value_or<std::string>(DefaultValue);
}
else if constexpr (std::is_enum_v<T>)
{
auto it = EnumTemplate.begin();
Value = EnumTemplate[section[Name].value_or<std::string>(static_cast<std::string>(it->first))];
}
else
{
Value = section[Name].value_or(DefaultValue);
}
if (ReadCallback)
ReadCallback(this);
} }
if (ReadCallback)
ReadCallback(this);
} }
} }

View file

@ -9,9 +9,9 @@
#include <shader/shader_cache.h> #include <shader/shader_cache.h>
#include "imgui_snapshot.h" #include "imgui_snapshot.h"
#include "gpu/video.h" #include "video.h"
#include "ui/window.h" #include <ui/window.h>
#include "config.h" #include <cfg/config.h>
#include "shader/copy_vs.hlsl.dxil.h" #include "shader/copy_vs.hlsl.dxil.h"
#include "shader/copy_vs.hlsl.spirv.h" #include "shader/copy_vs.hlsl.spirv.h"

View file

@ -2,7 +2,6 @@
#include <cpu/ppc_context.h> #include <cpu/ppc_context.h>
#include <cpu/guest_thread.h> #include <cpu/guest_thread.h>
#include <apu/audio.h> #include <apu/audio.h>
#include <apu/audio.h>
#include "function.h" #include "function.h"
#include "xex.h" #include "xex.h"
#include "xbox.h" #include "xbox.h"
@ -12,7 +11,7 @@
#include "xam.h" #include "xam.h"
#include "xdm.h" #include "xdm.h"
#include <timeapi.h> #include <timeapi.h>
#include "config.h" #include <cfg/config.h>
#include <ntstatus.h> #include <ntstatus.h>

View file

@ -11,7 +11,7 @@
#include <xex.h> #include <xex.h>
#include <apu/audio.h> #include <apu/audio.h>
#include <hid/hid.h> #include <hid/hid.h>
#include "config.h" #include <cfg/config.h>
#define GAME_XEX_PATH "game:\\default.xex" #define GAME_XEX_PATH "game:\\default.xex"

View file

@ -1,7 +1,7 @@
#include <cpu/guest_code.h> #include <cpu/guest_code.h>
#include "api/SWA.h" #include <api/SWA.h>
#include "ui/window.h" #include <ui/window.h>
#include "config.h" #include <cfg/config.h>
float m_lastLoadingFrameDelta = 0.0f; float m_lastLoadingFrameDelta = 0.0f;
std::chrono::steady_clock::time_point m_lastLoadingFrameTime; std::chrono::steady_clock::time_point m_lastLoadingFrameTime;

View file

@ -1,7 +1,7 @@
#include <cpu/guest_code.h> #include <cpu/guest_code.h>
#include "api/SWA.h" #include <api/SWA.h>
#include "ui/window.h" #include <ui/window.h>
#include "config.h" #include <cfg/config.h>
bool DisableHintsMidAsmHook() bool DisableHintsMidAsmHook()
{ {

View file

@ -1,8 +1,8 @@
#include <cpu/guest_code.h> #include <cpu/guest_code.h>
#include "api/SWA.h" #include <api/SWA.h>
#include "ui/window.h" #include <ui/window.h>
#include "ui/window_events.h" #include <ui/window_events.h>
#include "config.h" #include <cfg/config.h>
uint32_t m_lastCheckpointScore = 0; uint32_t m_lastCheckpointScore = 0;
float m_lastDarkGaiaEnergy = 0.0f; float m_lastDarkGaiaEnergy = 0.0f;

View file

@ -1,5 +1,5 @@
#include <cpu/guest_code.h> #include <cpu/guest_code.h>
#include "config.h" #include <cfg/config.h>
const char* m_pStageID; const char* m_pStageID;

View file

@ -1,7 +1,7 @@
#include <cpu/guest_code.h> #include <cpu/guest_code.h>
#include "api/SWA.h" #include <api/SWA.h>
#include "ui/window.h" #include <ui/window.h>
#include "config.h" #include <cfg/config.h>
constexpr float m_baseAspectRatio = 16.0f / 9.0f; constexpr float m_baseAspectRatio = 16.0f / 9.0f;

View file

@ -1,6 +1,6 @@
#include "window.h" #include "window.h"
#include "sdl_listener.h" #include "sdl_listener.h"
#include <config.h> #include <cfg/config.h>
#include <kernel/function.h> #include <kernel/function.h>
#include <SDL_syswm.h> #include <SDL_syswm.h>

View file

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "res/icon.h" #include <res/icon.h>
#include "res/icon_night.h" #include <res/icon_night.h>
#include "ui/window_events.h" #include <ui/window_events.h>
#include "config.h" #include <cfg/config.h>
#define DEFAULT_WIDTH 1280 #define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720 #define DEFAULT_HEIGHT 720