Skip to content

Commit

Permalink
refactor: wip xmake work
Browse files Browse the repository at this point in the history
  • Loading branch information
shad0wshayd3 committed Nov 21, 2024
1 parent 52de444 commit 6ecff36
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 227 deletions.
8 changes: 4 additions & 4 deletions src/Forms/Forms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<std::uintptr_t> targetDFOB{ REL::ID(599538) };
stl::asm_replace(targetDFOB.address(), 0x2C, reinterpret_cast<std::uintptr_t>(hkDefaultObject));
REL::Relocation<std::uintptr_t> target{ REL::ID(599538) };
target.replace_func(0x2C, hkDefaultObject);
}

void InstallDataReady()
Expand Down
45 changes: 10 additions & 35 deletions src/MCM/MCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<std::int32_t>(user);
}

static void GetTranslationStrings()
{
if (auto BSScaleformManager = RE::BSScaleformManager::GetSingleton(); BSScaleformManager && BSScaleformManager->loader)
Expand Down Expand Up @@ -98,6 +74,7 @@ namespace MCM

static void HandleKeybinds()
{
/*
try
{
std::ifstream fstream{ "Data/MCM/Settings/Keybinds.json" };
Expand All @@ -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 };
};
}
37 changes: 0 additions & 37 deletions src/PCH.cpp

This file was deleted.

29 changes: 3 additions & 26 deletions src/PCH.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
#pragma once

#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#pragma once

#include "F4SE/F4SE.h"
#include "RE/Fallout.h"

#include <AutoTOML.hpp>
#include <SimpleIni.h>
#include <nlohmann/json.hpp>
#include <spdlog/sinks/basic_file_sink.h>
#include <xbyak/xbyak.h>

#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
4 changes: 2 additions & 2 deletions src/Scripts/Papyrus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -60,7 +60,7 @@ namespace Papyrus

void UpdateSettings(std::monostate)
{
MCM::Settings::Update();
MCM::Settings::Update(false);
}

std::int32_t GetKeyCode(std::monostate)
Expand Down
46 changes: 0 additions & 46 deletions src/Settings.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/Workshop/Workshop.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ namespace Workshop

if (a_event->QUserEvent() == "Cancel")
{
auto ButtonEvent = stl::unrestricted_cast<RE::ButtonEvent*>(a_event);
auto ButtonEvent = RE::stl::unrestricted_cast<RE::ButtonEvent*>(a_event);
ButtonEvent->strUserEvent = "CloseMenu";
return _HandleEvent(a_this, ButtonEvent);
}
Expand All @@ -283,7 +283,7 @@ namespace Workshop
static void Install()
{
static REL::Relocation<std::uintptr_t> target{ REL::ID(179412) };
stl::asm_replace(target.address(), 0x1C7, reinterpret_cast<std::uintptr_t>(UIQualifier));
target.replace_func(0x1C7, UIQualifier);
}

private:
Expand Down
85 changes: 10 additions & 75 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,34 @@

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<spdlog::sinks::basic_file_sink_mt>(path->string(), true);

auto log = std::make_shared<spdlog::logger>("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;
}
}
}

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;
}
4 changes: 4 additions & 0 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6ecff36

Please sign in to comment.