From e84b2691cf2097b5482772c2c731b9df6c5cad02 Mon Sep 17 00:00:00 2001 From: Faumaray Date: Mon, 20 Oct 2025 00:32:43 +0300 Subject: [PATCH] Mod Base Signed-off-by: Faumaray --- include/Mod/Base.hpp | 19 ++++++++++++++++++ include/Mod/Context.hpp | 21 +++++++++++++++++++ include/Mod/Mod.hpp | 39 ++++++++++++++++++++++++++++++++++++ include/Mod/Registration.hpp | 22 ++++++++++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 include/Mod/Base.hpp create mode 100644 include/Mod/Context.hpp create mode 100644 include/Mod/Mod.hpp create mode 100644 include/Mod/Registration.hpp diff --git a/include/Mod/Base.hpp b/include/Mod/Base.hpp new file mode 100644 index 0000000..12d0746 --- /dev/null +++ b/include/Mod/Base.hpp @@ -0,0 +1,19 @@ +#pragma once +#include +#include + +namespace vl::mod { + +class Base { +public: + virtual ~Base() = default; + + virtual void onStart(Context* /*ctx*/) = 0; + + virtual void onStop() = 0; + + virtual std::string_view name() const noexcept = 0; + virtual std::string_view version() const noexcept = 0; +}; + +} // namespace vl::log diff --git a/include/Mod/Context.hpp b/include/Mod/Context.hpp new file mode 100644 index 0000000..dfcc818 --- /dev/null +++ b/include/Mod/Context.hpp @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +namespace vl::mod { + +struct ApiVersion { + std::uint8_t major; + std::uint8_t minor; + std::uint8_t patch; +}; + +struct Context { + // Example fields (optional, can be nullptr/0 if not provided by Core) + std::string_view gameDir{}; // e.g., Minecraft instance dir + std::string_view dataDir{}; // mod data/config directory + std::string_view logDir{}; // mod data/logs directory + ApiVersion apiVersion{1, 0, 0}; +}; + +} // namespace vl::log diff --git a/include/Mod/Mod.hpp b/include/Mod/Mod.hpp new file mode 100644 index 0000000..efe7733 --- /dev/null +++ b/include/Mod/Mod.hpp @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include + +namespace vl::mod { + +// ---- platform section helpers (ELF) ---- +#if defined(__ELF__) + #define VL_MOD_SECTION __attribute__((section("vlmods"), used)) +#else + // Fallback: keep the variable, but discovery code will need a different path on non-ELF. + #define VL_MOD_SECTION __attribute__((used)) +#endif + +// CRTP base: derive as `class MyMod : public Mod { ... };` +// Requires: `MyMod::MyMod(const Context&)` constructor. +template +class Mod : public Base { +private: + // Factory thunk that Core will call via the registration record. + static std::unique_ptr _vl_factory(const Context& ctx) { + return std::make_unique(ctx); + } + + // One registration record per T in a well-known section. + // This creates *no* runtime cost for mods beyond a single static object. + static inline const Registration _vl_reg VL_MOD_SECTION = { + kModRegMagic, + &_vl_factory, + typeid(T).name() + }; + +public: + // The rest (onStart/onStop/name/version/ctor) is implemented by T. +}; + +} // namespace vl::log diff --git a/include/Mod/Registration.hpp b/include/Mod/Registration.hpp new file mode 100644 index 0000000..650b0ed --- /dev/null +++ b/include/Mod/Registration.hpp @@ -0,0 +1,22 @@ +#pragma once +#include +#include +#include + +namespace vl::mod { + +// Registration record stored in the module's custom section. +// Core will scan this in the loaded .so to find available mods. +struct Registration { + // magic helps Core validate the record + const void* magic; + // factory that constructs a ModBase-derived object with Context + std::unique_ptr (*factory)(const Context&); + // an optional RTTI-ish string (e.g., typeid(T).name()) + const char* typeName; +}; + +inline constexpr std::uintptr_t kModRegMagicValue = 0xBADC0FFEEULL; +inline const void* kModRegMagic = reinterpret_cast(kModRegMagicValue); + +} // namespace vl::log