Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 00:32:43 +03:00
1 parent 23e5da53ba
commit e84b2691cf
4 files changed
+101

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <string_view>
#include <Mod/Context.hpp>
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
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <cstdint>
#include <string_view>
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
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include <Mod/Base.hpp>
#include <Mod/Registration.hpp>
#include <memory>
#include <typeinfo>
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<MyMod> { ... };`
// Requires: `MyMod::MyMod(const Context&)` constructor.
template <class T>
class Mod : public Base {
private:
// Factory thunk that Core will call via the registration record.
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
return std::make_unique<T>(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
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <Mod/Context.hpp>
#include <Mod/Base.hpp>
#include <memory>
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<Base> (*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<const void*>(kModRegMagicValue);
} // namespace vl::log