From aa88f6d3dc95d4d1de1823a6ebf25a3bf4b2c266 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Fri, 6 Sep 2024 21:04:56 -0600 Subject: [PATCH 1/3] optional SKSE::Init log, PluginVersionData::GetSingleton --- include/SKSE/API.h | 2 +- include/SKSE/Interfaces.h | 32 +++++++++++++++++++++++--------- include/SKSE/Logger.h | 2 ++ src/SKSE/API.cpp | 6 +++++- src/SKSE/Interfaces.cpp | 5 +++++ src/SKSE/Logger.cpp | 30 ++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/include/SKSE/API.h b/include/SKSE/API.h index f84eb7f48..830307c7d 100644 --- a/include/SKSE/API.h +++ b/include/SKSE/API.h @@ -12,7 +12,7 @@ namespace SKSE { - void Init(const LoadInterface* a_intfc) noexcept; + void Init(const LoadInterface* a_intfc, const bool a_log = false) noexcept; void RegisterForAPIInitEvent(std::function a_fn); PluginHandle GetPluginHandle() noexcept; diff --git a/include/SKSE/Interfaces.h b/include/SKSE/Interfaces.h index a519212cd..1cca5e5c0 100644 --- a/include/SKSE/Interfaces.h +++ b/include/SKSE/Interfaces.h @@ -381,9 +381,30 @@ namespace SKSE kVersionIndependentEx_NoStructUse = 1 << 0, }; - constexpr void AuthorEmail(std::string_view a_email) noexcept { SetCharBuffer(a_email, std::span{ supportEmail }); } + constexpr void PluginVersion(REL::Version a_version) noexcept { pluginVersion = a_version.pack(); } + + [[nodiscard]] constexpr REL::Version GetPluginVersion() const noexcept { return REL::Version::unpack(pluginVersion); } + + constexpr void PluginName(std::string_view a_plugin) noexcept { SetCharBuffer(a_plugin, std::span{ pluginName }); } + + [[nodiscard]] constexpr std::string_view GetPluginName() const noexcept { return std::string_view{ pluginName }; } + constexpr void AuthorName(std::string_view a_name) noexcept { SetCharBuffer(a_name, std::span{ author }); } + [[nodiscard]] constexpr std::string_view GetAuthorName() const noexcept { return std::string_view{ author }; } + + constexpr void AuthorEmail(std::string_view a_email) noexcept { SetCharBuffer(a_email, std::span{ supportEmail }); } + + [[nodiscard]] constexpr std::string_view GetAuthorName() const noexcept { return std::string_view{ supportEmail }; } + + constexpr void UsesAddressLibrary() noexcept { versionIndependence |= kVersionIndependent_AddressLibraryPostAE; } + constexpr void UsesSigScanning() noexcept { versionIndependence |= kVersionIndependent_Signatures; } + constexpr void UsesUpdatedStructs() noexcept { versionIndependence |= kVersionIndependent_StructsPost629; } + + constexpr void UsesNoStructs() noexcept { versionIndependenceEx |= kVersionIndependentEx_NoStructUse; } + + constexpr void MinimumRequiredXSEVersion(REL::Version a_version) noexcept { xseMinimum = a_version.pack(); } + constexpr void CompatibleVersions(std::initializer_list a_versions) noexcept { assert(a_versions.size() < std::size(compatibleVersions) - 1); @@ -394,14 +415,7 @@ namespace SKSE [](const REL::Version& a_version) noexcept { return a_version.pack(); }); } - constexpr void MinimumRequiredXSEVersion(REL::Version a_version) noexcept { xseMinimum = a_version.pack(); } - constexpr void PluginName(std::string_view a_plugin) noexcept { SetCharBuffer(a_plugin, std::span{ pluginName }); } - constexpr void PluginVersion(REL::Version a_version) noexcept { pluginVersion = a_version.pack(); } - constexpr void UsesAddressLibrary() noexcept { versionIndependence |= kVersionIndependent_AddressLibraryPostAE; } - constexpr void UsesSigScanning() noexcept { versionIndependence |= kVersionIndependent_Signatures; } - constexpr void UsesUpdatedStructs() noexcept { versionIndependence |= kVersionIndependent_StructsPost629; } - - constexpr void UsesNoStructs() noexcept { versionIndependenceEx |= kVersionIndependentEx_NoStructUse; } + [[nodiscard]] static const PluginVersionData* GetSingleton() noexcept; const std::uint32_t dataVersion{ kVersion }; std::uint32_t pluginVersion = 0; diff --git a/include/SKSE/Logger.h b/include/SKSE/Logger.h index 338367225..1e1d09a6e 100644 --- a/include/SKSE/Logger.h +++ b/include/SKSE/Logger.h @@ -39,6 +39,8 @@ namespace SKSE::log void add_papyrus_sink(std::regex a_filter); void remove_papyrus_sink(); + + void init(); } #undef SKSE_MAKE_SOURCE_LOGGER diff --git a/src/SKSE/API.cpp b/src/SKSE/API.cpp index c157b8283..5587a9579 100644 --- a/src/SKSE/API.cpp +++ b/src/SKSE/API.cpp @@ -69,12 +69,16 @@ namespace SKSE } } - void Init(const LoadInterface* a_intfc) noexcept + void Init(const LoadInterface* a_intfc, const bool a_log) noexcept { if (!a_intfc) { stl::report_and_fail("interface is null"sv); } + if (a_log) { + log::init(); + } + (void)REL::Module::get(); (void)REL::IDDatabase::get(); diff --git a/src/SKSE/Interfaces.cpp b/src/SKSE/Interfaces.cpp index 5fa091224..6cf961398 100644 --- a/src/SKSE/Interfaces.cpp +++ b/src/SKSE/Interfaces.cpp @@ -341,4 +341,9 @@ namespace SKSE assert(this); return reinterpret_cast(this); } + + const PluginVersionData* PluginVersionData::GetSingleton() noexcept + { + return reinterpret_cast(REX::W32::GetProcAddress(REX::W32::GetCurrentModule(), "SKSEPlugin_Version")); + } } diff --git a/src/SKSE/Logger.cpp b/src/SKSE/Logger.cpp index 424ba6fed..bf3809246 100644 --- a/src/SKSE/Logger.cpp +++ b/src/SKSE/Logger.cpp @@ -9,6 +9,9 @@ #include "SKSE/API.h" +#include +#include + namespace SKSE { namespace Impl @@ -113,5 +116,32 @@ namespace SKSE } }); } + + void init() + { + auto path = log_directory(); + if (!path) { + return; + } + + const auto data = PluginVersionData::GetSingleton(); + *path /= std::format("{}.log", data->GetPluginName()); + + std::vector sinks{ + std::make_shared(path->string(), true), + std::make_shared() + }; + + auto logger = std::make_shared("Global", sinks.begin(), sinks.end()); +#ifndef NDEBUG + logger->set_level(spdlog::level::debug); + logger->flush_on(spdlog::level::debug); +#else + logger->set_level(spdlog::level::info); + logger->flush_on(spdlog::level::info); +#endif + spdlog::set_default_logger(std::move(logger)); + spdlog::set_pattern("[%T.%e] [%=5t] [%L] %v"); + } } } From ca84f60212176d4fc7639b248286278518241eb1 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Fri, 6 Sep 2024 21:14:48 -0600 Subject: [PATCH 2/3] add SKSEPlugin defines --- include/SKSE/Interfaces.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/SKSE/Interfaces.h b/include/SKSE/Interfaces.h index 1cca5e5c0..0f1c8c12c 100644 --- a/include/SKSE/Interfaces.h +++ b/include/SKSE/Interfaces.h @@ -449,3 +449,6 @@ namespace SKSE static_assert(sizeof(PluginVersionData) == 0x350); #endif } + +#define SKSEPluginLoad(...) extern "C" [[maybe_unused]] __declspec(dllexport) bool SKSEPlugin_Load(__VA_ARGS__) +#define SKSEPluginVersion extern "C" [[maybe_unused]] __declspec(dllexport) constinit SKSE::PluginVersionData SKSEPlugin_Version From 2b9e07c106a7efa5ff1071cac015ec8b4d2b4d85 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 Date: Fri, 6 Sep 2024 22:03:50 -0600 Subject: [PATCH 3/3] fix duplicated function name --- include/SKSE/Interfaces.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/SKSE/Interfaces.h b/include/SKSE/Interfaces.h index 0f1c8c12c..4a1086ad4 100644 --- a/include/SKSE/Interfaces.h +++ b/include/SKSE/Interfaces.h @@ -395,7 +395,7 @@ namespace SKSE constexpr void AuthorEmail(std::string_view a_email) noexcept { SetCharBuffer(a_email, std::span{ supportEmail }); } - [[nodiscard]] constexpr std::string_view GetAuthorName() const noexcept { return std::string_view{ supportEmail }; } + [[nodiscard]] constexpr std::string_view GetAuthorEmail() const noexcept { return std::string_view{ supportEmail }; } constexpr void UsesAddressLibrary() noexcept { versionIndependence |= kVersionIndependent_AddressLibraryPostAE; } constexpr void UsesSigScanning() noexcept { versionIndependence |= kVersionIndependent_Signatures; }