diff --git a/src/Forms/Forms.h b/src/Forms/Forms.h index b157fd1..d5057a7 100644 --- a/src/Forms/Forms.h +++ b/src/Forms/Forms.h @@ -72,7 +72,7 @@ namespace Forms if (bActive) { - logger::info("Loaded from: {:s}"sv, a_path.string()); + F4SE::log::info("Loaded from: {:s}"sv, a_path.string()); } ini_file.Reset(); @@ -117,15 +117,15 @@ namespace Forms "PAFramePerk_DO", RE::ENUM_FORM_ID::kPERK); - logger::debug("Injected DefaultObjects."sv); + F4SE::log::debug("Injected DefaultObjects."sv); return 1; } } void Install() { - REL::Relocation targetDFOB{ REL::ID(599538) }; - stl::asm_replace(targetDFOB.address(), 0x2C, reinterpret_cast(hkDefaultObject)); + REL::Relocation target{ REL::ID(599538) }; + target.replace_func(0x2C, hkDefaultObject); } void InstallDataReady() diff --git a/src/MCM/MCM.h b/src/MCM/MCM.h index bda5790..a42bad4 100644 --- a/src/MCM/MCM.h +++ b/src/MCM/MCM.h @@ -8,7 +8,7 @@ namespace MCM class General { public: - inline static bool bAutoAutoReturn{ true }; + inline static REX::INI::Bool bAutoAutoReturn{ "General", "bAutoAutoReturn", true }; }; class Formatting @@ -26,49 +26,25 @@ namespace MCM inline static std::int32_t iKeyCode; }; - static void Update() + static void Update(bool a_firstRun) { - if (m_FirstRun) + if (a_firstRun) { GetTranslationStrings(); - m_FirstRun = false; } - m_ini_base.LoadFile("Data/MCM/Config/BakaPowerArmorStorage/settings.ini"); - m_ini_user.LoadFile("Data/MCM/Settings/BakaPowerArmorStorage.ini"); - - GetModSettingBool("General", "bAutoAutoReturn", General::bAutoAutoReturn); + const auto ini = REX::INI::SettingStore::GetSingleton(); + ini->Init( + "Data/MCM/Config/BakaPowerArmorStorage/settings.ini", + "Data/MCM/Settings/BakaPowerArmorStorage.ini"); + ini->Load(); HandleKeybinds(); - - m_ini_base.Reset(); - m_ini_user.Reset(); } inline static bool m_FirstRun{ true }; private: - static void GetModSettingChar(const std::string& a_section, const std::string& a_setting, std::string_view& a_value) - { - auto base = m_ini_base.GetValue(a_section.c_str(), a_setting.c_str(), a_value.data()); - auto user = m_ini_user.GetValue(a_section.c_str(), a_setting.c_str(), base); - a_value = user; - } - - 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; - } - - static void GetModSettingLong(const std::string& a_section, const std::string& a_setting, std::int32_t& a_value) - { - auto base = m_ini_base.GetLongValue(a_section.c_str(), a_setting.c_str(), a_value); - auto user = m_ini_user.GetLongValue(a_section.c_str(), a_setting.c_str(), base); - a_value = static_cast(user); - } - static void GetTranslationStrings() { if (auto BSScaleformManager = RE::BSScaleformManager::GetSingleton(); BSScaleformManager && BSScaleformManager->loader) @@ -98,6 +74,7 @@ namespace MCM static void HandleKeybinds() { + /* try { std::ifstream fstream{ "Data/MCM/Settings/Keybinds.json" }; @@ -119,9 +96,7 @@ namespace MCM Runtime::iKeyCode = 0; logger::debug("{:s}"sv, a_exception.what()); } + */ } - - 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 139e384..e846305 100644 --- a/src/PCH.h +++ b/src/PCH.h @@ -1,31 +1,8 @@ -#pragma once - -#define WIN32_LEAN_AND_MEAN -#define NOMINMAX +#pragma once #include "F4SE/F4SE.h" #include "RE/Fallout.h" - -#include -#include -#include -#include -#include - -#define DLLEXPORT __declspec(dllexport) +#include "REX/REX/INI.h" +#include "REX/REX/JSON.h" 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/Scripts/Papyrus.h b/src/Scripts/Papyrus.h index 681dbe7..2ccd84d 100644 --- a/src/Scripts/Papyrus.h +++ b/src/Scripts/Papyrus.h @@ -49,7 +49,7 @@ namespace Papyrus } else { - auto msg = fmt::format(fmt::runtime(MCM::Settings::Formatting::sPARecallTimer.data()), a_time); + auto msg = std::vformat(MCM::Settings::Formatting::sPARecallTimer, std::make_format_args(a_time)); RE::SendHUDMessage::ShowHUDMessage( msg.c_str(), nullptr, @@ -60,7 +60,7 @@ namespace Papyrus void UpdateSettings(std::monostate) { - MCM::Settings::Update(); + MCM::Settings::Update(false); } std::int32_t GetKeyCode(std::monostate) 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/Workshop/Workshop.h b/src/Workshop/Workshop.h index 81ab6ee..32c9da4 100644 --- a/src/Workshop/Workshop.h +++ b/src/Workshop/Workshop.h @@ -265,7 +265,7 @@ namespace Workshop if (a_event->QUserEvent() == "Cancel") { - auto ButtonEvent = stl::unrestricted_cast(a_event); + auto ButtonEvent = RE::stl::unrestricted_cast(a_event); ButtonEvent->strUserEvent = "CloseMenu"; return _HandleEvent(a_this, ButtonEvent); } @@ -283,7 +283,7 @@ namespace Workshop static void Install() { static REL::Relocation target{ REL::ID(179412) }; - stl::asm_replace(target.address(), 0x1C7, reinterpret_cast(UIQualifier)); + target.replace_func(0x1C7, UIQualifier); } private: diff --git a/src/main.cpp b/src/main.cpp index 74eeb48..718dd29 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,46 +5,20 @@ namespace { - void InitializeLog() + void MessageCallback(F4SE::MessagingInterface::Message* a_msg) { - auto path = logger::log_directory(); - if (!path) - { - stl::report_and_fail("Failed to find standard logging directory"sv); - } - - *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); - } - - void MessageHandler(F4SE::MessagingInterface::Message* a_msg) - { - if (!a_msg) - { - return; - } - switch (a_msg->type) { + case F4SE::MessagingInterface::kPostLoad: + Forms::Install(); + Workshop::PlacementMode::Hooks::Install(); + break; case F4SE::MessagingInterface::kGameLoaded: Workshop::PlacementMode::ApplyPerk(); break; case F4SE::MessagingInterface::kGameDataReady: Forms::InstallDataReady(); - MCM::Settings::Update(); + MCM::Settings::Update(true); break; default: break; @@ -52,52 +26,13 @@ namespace } } -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); - F4SE::AllocTrampoline(1u << 10); - - const auto messaging = F4SE::GetMessagingInterface(); - if (!messaging || !messaging->RegisterListener(MessageHandler)) - { - logger::critical("Failed to register messaging handler, marking as incompatible."sv); - return false; - } - const auto papyrus = F4SE::GetPapyrusInterface(); - if (!papyrus || !papyrus->Register(Papyrus::RegisterFunctions)) - { - logger::critical("Failed to register Papyrus functions, marking as incompatible."sv); - return false; - } - - Forms::Install(); - Workshop::PlacementMode::Hooks::Install(); + F4SE::AllocTrampoline(1u << 10); + F4SE::GetMessagingInterface()->RegisterListener(MessageCallback); + F4SE::GetPapyrusInterface()->Register(Papyrus::RegisterFunctions); return true; } diff --git a/xmake.lua b/xmake.lua index ad429a2..c708f44 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("rex_ini", true) +set_config("rex_json", true) + -- setup targets target("BakaPowerArmorStorage") -- bind local dependencies