From 6936a27db62fef3a84a7507dc23b3abbbe678093 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Thu, 21 Nov 2024 00:36:13 -0700 Subject: [PATCH] refactor: wip xmake work --- src/MCM/MCM.h | 78 ++++++++++++------------------------------ src/PCH.cpp | 37 -------------------- src/PCH.h | 25 ++------------ src/Settings.h | 46 ------------------------- src/main.cpp | 93 +++++++++++++++----------------------------------- xmake.lua | 4 +++ 6 files changed, 55 insertions(+), 228 deletions(-) delete mode 100644 src/PCH.cpp delete mode 100644 src/Settings.h diff --git a/src/MCM/MCM.h b/src/MCM/MCM.h index bd8f063..464066b 100644 --- a/src/MCM/MCM.h +++ b/src/MCM/MCM.h @@ -8,72 +8,36 @@ namespace MCM class General { public: - inline static bool bEnable{ true }; + inline static REX::INI::Bool bEnable{ "Genreal", "bEnable", true }; }; class Setting { public: - inline static bool bAidWeight{ true }; - inline static bool bAmmoWeight{ true }; - inline static bool bConsole{ true }; - inline static bool bEnemyMarkers{ true }; - inline static bool bFastTravel{ true }; - inline static bool bLocationMarkers{ true }; - inline static bool bMenuSaving{ true }; - inline static bool bQuickSaveLoad{ true }; - inline static bool bSaveOnLevel{ true }; - inline static bool bSaveOnPip{ true }; - inline static bool bSaveOnSleep{ true }; - inline static bool bSaveOnTravel{ true }; - inline static bool bSaveOnWorkshop{ true }; - inline static bool bSurvivalLock{ true }; - inline static bool bToggleGodMode{ true }; + inline static REX::INI::Bool bAidWeight{ "Setting", "bAidWeight", true }; + inline static REX::INI::Bool bAmmoWeight{ "Setting", "bAmmoWeight", true }; + inline static REX::INI::Bool bConsole{ "Setting", "bConsole", true }; + inline static REX::INI::Bool bEnemyMarkers{ "Setting", "bEnemyMarkers", true }; + inline static REX::INI::Bool bFastTravel{ "Setting", "bFastTravel", true }; + inline static REX::INI::Bool bLocationMarkers{ "Setting", "bLocationMarkers", true }; + inline static REX::INI::Bool bMenuSaving{ "Setting", "bMenuSaving", true }; + inline static REX::INI::Bool bQuickSaveLoad{ "Setting", "bQuickSaveLoad", true }; + inline static REX::INI::Bool bSaveOnLevel{ "Setting", "bSaveOnLevel", true }; + inline static REX::INI::Bool bSaveOnPip{ "Setting", "bSaveOnPip", true }; + inline static REX::INI::Bool bSaveOnSleep{ "Setting", "bSaveOnSleep", true }; + inline static REX::INI::Bool bSaveOnTravel{ "Setting", "bSaveOnTravel", true }; + inline static REX::INI::Bool bSaveOnWorkshop{ "Setting", "bSaveOnWorkshop", true }; + inline static REX::INI::Bool bSurvivalLock{ "Setting", "bSurvivalLock", true }; + inline static REX::INI::Bool bToggleGodMode{ "Setting", "bToggleGodMode", true }; }; static void Update() { - if (m_FirstRun) - { - m_FirstRun = false; - } - - m_ini_base.LoadFile("Data/MCM/Config/BakaSurvivalSettings/settings.ini"); - m_ini_user.LoadFile("Data/MCM/Settings/BakaSurvivalSettings.ini"); - - GetModSettingBool("General", "bEnable", General::bEnable); - - GetModSettingBool("Setting", "bAidWeight", Setting::bAidWeight); - GetModSettingBool("Setting", "bAmmoWeight", Setting::bAmmoWeight); - GetModSettingBool("Setting", "bConsole", Setting::bConsole); - GetModSettingBool("Setting", "bEnemyMarkers", Setting::bEnemyMarkers); - GetModSettingBool("Setting", "bFastTravel", Setting::bFastTravel); - GetModSettingBool("Setting", "bLocationMarkers", Setting::bLocationMarkers); - GetModSettingBool("Setting", "bMenuSaving", Setting::bMenuSaving); - GetModSettingBool("Setting", "bQuickSaveLoad", Setting::bQuickSaveLoad); - GetModSettingBool("Setting", "bSaveOnLevel", Setting::bSaveOnLevel); - GetModSettingBool("Setting", "bSaveOnPip", Setting::bSaveOnPip); - GetModSettingBool("Setting", "bSaveOnSleep", Setting::bSaveOnSleep); - GetModSettingBool("Setting", "bSaveOnTravel", Setting::bSaveOnTravel); - GetModSettingBool("Setting", "bSaveOnWorkshop", Setting::bSaveOnWorkshop); - GetModSettingBool("Setting", "bSurvivalLock", Setting::bSurvivalLock); - GetModSettingBool("Setting", "bToggleGodMode", Setting::bToggleGodMode); - - m_ini_base.Reset(); - m_ini_user.Reset(); + const auto ini = REX::INI::SettingStore::GetSingleton(); + ini->Init( + "Data/MCM/Config/BakaSurvivalSettings/settings.ini", + "Data/MCM/Settings/BakaSurvivalSettings.ini"); + ini->Load(); } - - inline static bool m_FirstRun{ true }; - - private: - static void GetModSettingBool(const std::string& a_section, const std::string& a_setting, bool& a_value) - { - auto base = m_ini_base.GetBoolValue(a_section.c_str(), a_setting.c_str(), a_value); - auto user = m_ini_user.GetBoolValue(a_section.c_str(), a_setting.c_str(), base); - a_value = user; - } - - inline static CSimpleIniA m_ini_base{ true }; - inline static CSimpleIniA m_ini_user{ true }; }; } diff --git a/src/PCH.cpp b/src/PCH.cpp deleted file mode 100644 index 8607860..0000000 --- a/src/PCH.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "PCH.h" - -namespace stl -{ - namespace detail - { - struct asm_patch : - Xbyak::CodeGenerator - { - asm_patch(std::uintptr_t a_dst) - { - Xbyak::Label dst; - - jmp(ptr[rip + dst]); - - L(dst); - dq(a_dst); - } - }; - } - - void asm_jump(std::uintptr_t a_from, [[maybe_unused]] std::size_t a_size, std::uintptr_t a_to) - { - detail::asm_patch p{ a_to }; - p.ready(); - assert(p.getSize() <= a_size); - REL::safe_write( - a_from, - std::span{ p.getCode(), p.getSize() }); - } - - void asm_replace(std::uintptr_t a_from, std::size_t a_size, std::uintptr_t a_to) - { - REL::safe_fill(a_from, REL::INT3, a_size); - asm_jump(a_from, a_size, a_to); - } -} diff --git a/src/PCH.h b/src/PCH.h index cbff35f..ee07078 100644 --- a/src/PCH.h +++ b/src/PCH.h @@ -1,30 +1,9 @@ -#pragma once - -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX +#pragma once #include "F4SE/F4SE.h" #include "RE/Fallout.h" +#include "REX/REX/INI.h" -#include -#include -#include #include -#define DLLEXPORT __declspec(dllexport) - using namespace std::literals; - -namespace logger = F4SE::log; - -namespace stl -{ - using namespace F4SE::stl; - - void asm_replace(std::uintptr_t a_from, std::size_t a_size, std::uintptr_t a_to); -} - -// clang-format off -#include "Version.h" -#include "Settings.h" -// clang-format on diff --git a/src/Settings.h b/src/Settings.h deleted file mode 100644 index ce29340..0000000 --- a/src/Settings.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -namespace Settings -{ - namespace - { - using bSetting = AutoTOML::bSetting; - using ISetting = AutoTOML::ISetting; - } - - namespace General - { - inline bSetting EnableDebugLogging{ "General"s, "EnableDebugLogging"s, false }; - } - - inline void Load() - { - try - { - const auto table = toml::parse_file( - fmt::format(FMT_STRING("Data/F4SE/Plugins/{:s}.toml"sv), Version::PROJECT)); - for (const auto& setting : ISetting::get_settings()) - { - setting->load(table); - } - } - catch (const toml::parse_error& e) - { - std::ostringstream ss; - ss - << "Error parsing file \'" << *e.source().path << "\':\n" - << '\t' << e.description() << '\n' - << "\t\t(" << e.source().begin << ')'; - logger::error(FMT_STRING("{:s}"sv), ss.str()); - stl::report_and_fail("Failed to load settings."sv); - } - catch (const std::exception& e) - { - stl::report_and_fail(e.what()); - } - catch (...) - { - stl::report_and_fail("Unknown failure."sv); - } - } -} diff --git a/src/main.cpp b/src/main.cpp index 629efe5..e0d57e4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,44 +5,44 @@ class Hooks public: static void Install() { - if (!MCM::Settings::General::bEnable) + if (!MCM::Settings::General::bEnable.GetValue()) { return; } - if (!MCM::Settings::Setting::bAidWeight) + if (!MCM::Settings::Setting::bAidWeight.GetValue()) { hkcmpEAX<1321341, 0x97>::Install(); // TESWeightForm::GetFormWeight } - if (!MCM::Settings::Setting::bAmmoWeight) + if (!MCM::Settings::Setting::bAmmoWeight.GetValue()) { hkcmpEAX<1321341, 0x121>::Install(); // TESWeightForm::GetFormWeight } - if (!MCM::Settings::Setting::bConsole) + if (!MCM::Settings::Setting::bConsole.GetValue()) { hkcmpEAX<927099, 0x20F>::Install(); // MenuOpenHandler::HandleEvent } - if (!MCM::Settings::Setting::bEnemyMarkers) + if (!MCM::Settings::Setting::bEnemyMarkers.GetValue()) { hkcmpEAX<1475119, 0x23>::Install(); // HUDMarkerUtils::GetHostileEnemyMaxDistance } - if (!MCM::Settings::Setting::bFastTravel) + if (!MCM::Settings::Setting::bFastTravel.GetValue()) { hkcmpEAX<712982, 0x323>::Install(); // PipboyMenu::PipboyMenu hkcmpEAX<1327120, 0x18>::Install(); // nsPipboyMenu::CheckHardcoreFastTravel } - if (!MCM::Settings::Setting::bLocationMarkers) + if (!MCM::Settings::Setting::bLocationMarkers.GetValue()) { hkcmpEAX<1301956, 0x10>::Install(); // HUDMarkerUtils::GetLocationMaxDistance hkcmpEAX<1153736, 0xA7>::Install(); // CalculateCompassMarkersFunctor::UpdateLocationMarkers } - if (!MCM::Settings::Setting::bMenuSaving) + if (!MCM::Settings::Setting::bMenuSaving.GetValue()) { hkcmpEAX<1330449, 0xC6>::Install(); // PauseMenu::InitMainList hkcmpEAX<425422, 0x4C>::Install(); // PauseMenu::CheckIfSaveLoadPossible @@ -50,39 +50,39 @@ class Hooks hkcmpEBX<1103363, 0x81A>::Install(); // StartMenuBase::SendGameplayOptions } - if (!MCM::Settings::Setting::bQuickSaveLoad) + if (!MCM::Settings::Setting::bQuickSaveLoad.GetValue()) { hkcmpEAX<1470086, 0x71>::Install(); // QuickSaveLoadHandler::HandleEvent } - if (!MCM::Settings::Setting::bSaveOnLevel) + if (!MCM::Settings::Setting::bSaveOnLevel.GetValue()) { hkcmpEAX<1158548, 0x53>::Install(); // LevelUpMenu::~LevelUpMenu } - if (!MCM::Settings::Setting::bSaveOnPip) + if (!MCM::Settings::Setting::bSaveOnPip.GetValue()) { hkcmpEAX<1231000, 0x18F>::Install(); // PipboyManager::OnPipboyCloseAnim } - if (!MCM::Settings::Setting::bSaveOnSleep) + if (!MCM::Settings::Setting::bSaveOnSleep.GetValue()) { hkcmpEAX<1551767, 0xCC>::Install(); // PlayerCharacter::WakeUp hkcmpEAX<1551767, 0x14B>::Install(); // PlayerCharacter::WakeUp } - if (!MCM::Settings::Setting::bSaveOnTravel) + if (!MCM::Settings::Setting::bSaveOnTravel.GetValue()) { hkcmpEAX<146861, 0x67D>::Install(); // PlayerCharacter::HandlePositionPlayerRequest hkcmpEAX<374033, 0x2B>::Install(); // PlayerCharacter::RequestQueueDoorAutosave } - if (!MCM::Settings::Setting::bSaveOnWorkshop) + if (!MCM::Settings::Setting::bSaveOnWorkshop.GetValue()) { hkcmpEAX<98443, 0x198>::Install(); // WorkshopMenu::~WorkshopMenu } - if (!MCM::Settings::Setting::bSurvivalLock) + if (!MCM::Settings::Setting::bSurvivalLock.GetValue()) { // PauseMenu::CheckIfSaveLoadPossible static REL::Relocation target{ REL::ID(425422), 0x14D }; @@ -91,7 +91,7 @@ class Hooks hkmovEDX<402595, 0x28>::Install(); // ExitSurvivalModeCallback::operator() } - if (!MCM::Settings::Setting::bToggleGodMode) + if (!MCM::Settings::Setting::bToggleGodMode.GetValue()) { hkcmpEAX<1032309, 0x35>::Install(); // PlayerCharacter::IsGodMode hkcmpEAX<500346, 0x35>::Install(); // PlayerCharacter::IsImmortal @@ -176,63 +176,26 @@ class Hooks namespace { - void InitializeLog() + void MessageCallback(F4SE::MessagingInterface::Message* a_msg) { - auto path = logger::log_directory(); - if (!path) + switch (a_msg->type) { - stl::report_and_fail("Failed to find standard logging directory"sv); + case F4SE::MessagingInterface::kPostLoad: + { + MCM::Settings::Update(); + Hooks::Install(); + break; + } + default: + break; } - - *path /= fmt::format(FMT_STRING("{:s}.log"sv), Version::PROJECT); - auto sink = std::make_shared(path->string(), true); - - auto log = std::make_shared("global log"s, std::move(sink)); - auto lvl = *Settings::General::EnableDebugLogging - ? spdlog::level::trace - : spdlog::level::info; - - log->set_level(lvl); - log->flush_on(lvl); - - spdlog::set_default_logger(std::move(log)); - spdlog::set_pattern("[%m/%d/%Y - %T] [%^%l%$] %v"s); - - logger::info(FMT_STRING("{:s} v{:s}"sv), Version::PROJECT, Version::NAME); - } -} - -extern "C" DLLEXPORT bool F4SEAPI F4SEPlugin_Query(const F4SE::QueryInterface* a_F4SE, F4SE::PluginInfo* a_info) -{ - a_info->infoVersion = F4SE::PluginInfo::kVersion; - a_info->name = Version::PROJECT.data(); - a_info->version = Version::MAJOR; - - const auto rtv = a_F4SE->RuntimeVersion(); - if (rtv < F4SE::RUNTIME_LATEST) - { - stl::report_and_fail( - fmt::format( - FMT_STRING("{:s} does not support runtime v{:s}."sv), - Version::PROJECT, - rtv.string())); } - - return true; } -extern "C" DLLEXPORT bool F4SEAPI F4SEPlugin_Load(const F4SE::LoadInterface* a_F4SE) +F4SEPluginLoad(const F4SE::LoadInterface* a_F4SE) { - Settings::Load(); - InitializeLog(); - - logger::info(FMT_STRING("{:s} loaded."sv), Version::PROJECT); - logger::debug("Debug logging enabled."sv); - F4SE::Init(a_F4SE); - - MCM::Settings::Update(); - Hooks::Install(); + F4SE::GetMessagingInterface()->RegisterListener(MessageCallback); return true; } diff --git a/xmake.lua b/xmake.lua index bdcac43..6ae2f92 100644 --- a/xmake.lua +++ b/xmake.lua @@ -23,6 +23,10 @@ set_policy("build.optimization.lto", true) add_rules("mode.debug", "mode.releasedbg") add_rules("plugin.vsxmake.autoupdate") +-- add config +set_config("f4se_xbyak", true) +set_config("rex_ini", true) + -- setup targets target("BakaSurvivalSettings") -- bind local dependencies