Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f622d9b8f | ||
|
|
19fd9b3129 |
No files matched your search
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
|
||||
namespace vl::mixin {
|
||||
template<class Ret>
|
||||
struct CallbackInfoReturnable;
|
||||
// Base callback info: allows cancellation of original behavior.
|
||||
struct CallbackInfo {
|
||||
void cancel() noexcept { cancelled_ = true; }
|
||||
bool isCancelled() const noexcept { return cancelled_; }
|
||||
private:
|
||||
bool cancelled_ = false;
|
||||
template<class Ret>
|
||||
friend class CallbackInfoReturnable;
|
||||
};
|
||||
|
||||
// Returnable variant: holds an optional return value.
|
||||
template<class Ret>
|
||||
struct CallbackInfoReturnable : CallbackInfo {
|
||||
template<class T>
|
||||
void setReturnValue(T&& v) {
|
||||
value_ = static_cast<Ret>(std::forward<T>(v));
|
||||
this->cancel(); // setting a value implies skipping original unless user calls uncancel()
|
||||
}
|
||||
bool hasReturnValue() const noexcept { return value_.has_value(); }
|
||||
const Ret& getReturnValue() const { return *value_; }
|
||||
Ret& getReturnValue() { return *value_; }
|
||||
void clearReturnValue() { value_.reset(); }
|
||||
void uncancel() { if (!value_) /*no explicit value*/ cancelled_ = false; }
|
||||
private:
|
||||
using CallbackInfo::cancelled_;
|
||||
std::optional<Ret> value_;
|
||||
};
|
||||
|
||||
} // namespace vl::mixin
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <Mixin/CallbackInfo.hpp>
|
||||
|
||||
namespace vl::mixin {
|
||||
|
||||
// For INVOKE_AFTER: exposes access to callee result.
|
||||
template<class Ret>
|
||||
struct CallbackInfoInvokeAfter : CallbackInfoReturnable<Ret> {
|
||||
using Base = CallbackInfoReturnable<Ret>;
|
||||
Ret& originalReturn() noexcept { return orig_; }
|
||||
const Ret& originalReturn() const noexcept { return orig_; }
|
||||
|
||||
void setOriginalReturn(const Ret& v) noexcept { orig_ = v; }
|
||||
|
||||
private:
|
||||
Ret orig_{};
|
||||
};
|
||||
|
||||
// For FIELD_GET / FIELD_PUT:
|
||||
// GET -> exposes current field value, PUT -> exposes incoming value.
|
||||
template<class T>
|
||||
struct CallbackInfoField : CallbackInfoReturnable<T> {
|
||||
using Base = CallbackInfoReturnable<T>;
|
||||
// For GET before/after:
|
||||
void setFieldValue(const T& v) { field_ = v; }
|
||||
const T& fieldValue() const { return field_; }
|
||||
|
||||
private:
|
||||
T field_{};
|
||||
};
|
||||
|
||||
} // namespace vl::mixin
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <shared_mutex>
|
||||
#include <vector>
|
||||
#include <string_view>
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace vl::mixin {
|
||||
|
||||
class CallbackRegistry {
|
||||
public:
|
||||
static CallbackId add(ErasedCallback cb) {
|
||||
static CallbackId next = 1;
|
||||
std::unique_lock lk(mu_);
|
||||
auto id = next++;
|
||||
table_.emplace(id, std::move(cb));
|
||||
return id;
|
||||
}
|
||||
|
||||
static ErasedCallback* get(CallbackId id) {
|
||||
std::shared_lock lk(mu_);
|
||||
auto it = table_.find(id);
|
||||
if (it == table_.end()) return nullptr;
|
||||
return &it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
static inline std::shared_mutex mu_;
|
||||
static inline std::unordered_map<CallbackId, ErasedCallback> table_;
|
||||
};
|
||||
|
||||
} // namespace vl::mixin
|
||||
@@ -0,0 +1,168 @@
|
||||
#pragma once
|
||||
#include <Mixin/Mixin.hpp>
|
||||
#include <Mixin/CallbackRegistry.hpp>
|
||||
#include <Reflect/Method.hpp>
|
||||
#include <Mixin/TypedAdapter.hpp>
|
||||
|
||||
|
||||
#define MIXIN_INJECT(MethodPtr, AT_ENUM) \
|
||||
static_assert(::vl::reflect::is_valid_method<MethodPtr>(), \
|
||||
"MIXIN_INJECT: invalid method pointer — missing or wrong reflection"); \
|
||||
struct _VL_INJECT_REG_##MethodPtr { \
|
||||
_VL_INJECT_REG_##MethodPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::InjectionSpec spec{ \
|
||||
::vl::reflect::method_owner<MethodPtr>(), \
|
||||
::vl::reflect::method_name<MethodPtr>(), \
|
||||
::vl::reflect::method_desc<MethodPtr>(), \
|
||||
AT_ENUM, ::vl::mixin::Side::Before, \
|
||||
::vl::mixin::CallbackRegistry::add(_vl_make_callback_MethodPtr())\
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_inject_reg_##MethodPtr; \
|
||||
static ::vl::mixin::ErasedCallback _vl_make_callback_##MethodPtr()
|
||||
|
||||
#define MIXIN_REDIRECT(MethodPtr) \
|
||||
static_assert(::vl::reflect::is_valid_method<MethodPtr>(), \
|
||||
"MIXIN_REDIRECT: invalid method pointer — missing or wrong reflection"); \
|
||||
struct _VL_REDIRECT_REG_##MethodPtr { \
|
||||
_VL_REDIRECT_REG_##MethodPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::RedirectSpec spec{ \
|
||||
::vl::reflect::method_owner<MethodPtr>(), \
|
||||
::vl::reflect::method_name<MethodPtr>(), \
|
||||
::vl::reflect::method_desc<MethodPtr>(), \
|
||||
::vl::mixin::CallbackRegistry::add(_vl_make_callback_MethodPtr())\
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_redirect_reg_##MethodPtr; \
|
||||
static ::vl::mixin::ErasedCallback _vl_make_callback_##MethodPtr()
|
||||
|
||||
#define MIXIN_MODIFY_CONST_INT(MethodPtr, VALUE_LITERAL) \
|
||||
static_assert(::vl::reflect::is_valid_method<MethodPtr>(), \
|
||||
"MIXIN_MODIFY_CONST_INT: invalid method pointer"); \
|
||||
struct _VL_CONST_REG_##MethodPtr { \
|
||||
_VL_CONST_REG_##MethodPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::ModifyConstSpec spec{ \
|
||||
::vl::reflect::method_owner<MethodPtr>(), \
|
||||
::vl::reflect::method_name<MethodPtr>(), \
|
||||
::vl::reflect::method_desc<MethodPtr>(), \
|
||||
::vl::mixin::ModifyConstSpec::Int, (long long)(VALUE_LITERAL), 0.0, {}, \
|
||||
0 /* static replacement by Core; no cb */ \
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_const_reg_##MethodPtr;
|
||||
|
||||
|
||||
// Inject with typed lambda: signature (Self[, Args...], CallbackInfo&)
|
||||
#define MIXIN_INJECT_TYPED(MethodPtr, AT_ENUM, LAMBDA) \
|
||||
struct _VL_INJECT_T_REG_##MethodPtr { \
|
||||
_VL_INJECT_T_REG_##MethodPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::InjectionSpec spec{ \
|
||||
::vl::reflect::method_owner<MethodPtr>(), \
|
||||
::vl::reflect::method_name<MethodPtr>(), \
|
||||
::vl::reflect::method_desc<MethodPtr>(), \
|
||||
AT_ENUM, ::vl::mixin::Side::Before, \
|
||||
::vl::mixin::CallbackRegistry::add( \
|
||||
::vl::mixin::make_typed_inject<MethodPtr>(LAMBDA) \
|
||||
) \
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_inject_typed_reg_##MethodPtr;
|
||||
|
||||
// Redirect with typed lambda: signature (Self[, Args...], CallbackInfoReturnable<Ret>&) -> Ret or void (set via ci)
|
||||
#define MIXIN_REDIRECT_TYPED(MethodPtr, LAMBDA) \
|
||||
struct _VL_REDIRECT_T_REG_##MethodPtr { \
|
||||
_VL_REDIRECT_T_REG_##MethodPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::RedirectSpec spec{ \
|
||||
::vl::reflect::method_owner<MethodPtr>(), \
|
||||
::vl::reflect::method_name<MethodPtr>(), \
|
||||
::vl::reflect::method_desc<MethodPtr>(), \
|
||||
::vl::mixin::CallbackRegistry::add( \
|
||||
::vl::mixin::make_typed_redirect<MethodPtr>(LAMBDA) \
|
||||
) \
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_redirect_typed_reg_##MethodPtr;
|
||||
|
||||
|
||||
#define MIXIN_FIELD_GET_TYPED(FieldPtr, LAMBDA, FieldT, SelfT) \
|
||||
struct _VL_FIELD_GET_T_REG_##FieldPtr { \
|
||||
_VL_FIELD_GET_T_REG_##FieldPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(FieldPtr)>; \
|
||||
::vl::mixin::InjectionSpec spec{ \
|
||||
::vl::reflect::method_owner<FieldPtr>(), \
|
||||
::vl::reflect::method_name<FieldPtr>(), \
|
||||
::vl::reflect::method_desc<FieldPtr>(), \
|
||||
::vl::mixin::At::FIELD_GET, ::vl::mixin::Side::Before, \
|
||||
::vl::mixin::CallbackRegistry::add( \
|
||||
::vl::mixin::make_typed_field_get<FieldPtr,decltype(LAMBDA),FieldT,SelfT>(LAMBDA) \
|
||||
) \
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_field_get_typed_reg_##FieldPtr;
|
||||
|
||||
#define MIXIN_FIELD_PUT_TYPED(FieldPtr, LAMBDA, FieldT, SelfT) \
|
||||
struct _VL_FIELD_PUT_T_REG_##FieldPtr { \
|
||||
_VL_FIELD_PUT_T_REG_##FieldPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(FieldPtr)>; \
|
||||
::vl::mixin::InjectionSpec spec{ \
|
||||
::vl::reflect::method_owner<FieldPtr>(), \
|
||||
::vl::reflect::method_name<FieldPtr>(), \
|
||||
::vl::reflect::method_desc<FieldPtr>(), \
|
||||
::vl::mixin::At::FIELD_PUT, ::vl::mixin::Side::Before, \
|
||||
::vl::mixin::CallbackRegistry::add( \
|
||||
::vl::mixin::make_typed_field_put<FieldPtr,decltype(LAMBDA),FieldT,SelfT>(LAMBDA) \
|
||||
) \
|
||||
}; \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_field_put_typed_reg_##FieldPtr;
|
||||
|
||||
#define MIXIN_INVOKE_BEFORE_TYPED(MethodPtr, InvokeTargetPtr, LAMBDA) \
|
||||
struct _VL_INVOKE_B_T_REG_##MethodPtr##_##InvokeTargetPtr { \
|
||||
_VL_INVOKE_B_T_REG_##MethodPtr##_##InvokeTargetPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::InjectionSpec spec{}; \
|
||||
spec.owner = ::vl::reflect::method_owner<MethodPtr>(); \
|
||||
spec.methodName = ::vl::reflect::method_name<MethodPtr>(); \
|
||||
spec.methodDesc = ::vl::reflect::method_desc<MethodPtr>(); \
|
||||
spec.kind = ::vl::mixin::PointKind::Invoke; \
|
||||
spec.when = ::vl::mixin::Side::Before; \
|
||||
spec.cb = ::vl::mixin::CallbackRegistry::add( \
|
||||
::vl::mixin::make_typed_invoke_before<InvokeTargetPtr>(LAMBDA)); \
|
||||
spec.invoke.owner = ::vl::reflect::method_owner<InvokeTargetPtr>(); \
|
||||
spec.invoke.name = ::vl::reflect::method_name<InvokeTargetPtr>(); \
|
||||
spec.invoke.desc = ::vl::reflect::method_desc<InvokeTargetPtr>(); \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_invoke_before_typed_reg_##MethodPtr##_##InvokeTargetPtr;
|
||||
|
||||
// After a specific invoke inside a method
|
||||
#define MIXIN_INVOKE_AFTER_TYPED(MethodPtr, InvokeTargetPtr, LAMBDA) \
|
||||
struct _VL_INVOKE_A_T_REG_##MethodPtr##_##InvokeTargetPtr { \
|
||||
_VL_INVOKE_A_T_REG_##MethodPtr##_##InvokeTargetPtr() { \
|
||||
using _Target = typename std::remove_pointer_t<decltype(MethodPtr)>; \
|
||||
::vl::mixin::InjectionSpec spec{}; \
|
||||
spec.owner = ::vl::reflect::method_owner<MethodPtr>(); \
|
||||
spec.methodName = ::vl::reflect::method_name<MethodPtr>(); \
|
||||
spec.methodDesc = ::vl::reflect::method_desc<MethodPtr>(); \
|
||||
spec.kind = ::vl::mixin::PointKind::Invoke; \
|
||||
spec.when = ::vl::mixin::Side::After; \
|
||||
spec.cb = ::vl::mixin::CallbackRegistry::add( \
|
||||
::vl::mixin::make_typed_invoke_after<InvokeTargetPtr>(LAMBDA)); \
|
||||
spec.invoke.owner = ::vl::reflect::method_owner<InvokeTargetPtr>(); \
|
||||
spec.invoke.name = ::vl::reflect::method_name<InvokeTargetPtr>(); \
|
||||
spec.invoke.desc = ::vl::reflect::method_desc<InvokeTargetPtr>(); \
|
||||
::vl::mixin::Mixin<_Target>::add(spec); \
|
||||
} \
|
||||
} _vl_invoke_after_typed_reg_##MethodPtr##_##InvokeTargetPtr;
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <Mixin/Types.hpp>
|
||||
#include <Mixin/Registrer.hpp>
|
||||
#include <Reflect/Method.hpp>
|
||||
|
||||
namespace vl::mixin {
|
||||
|
||||
template<class Target>
|
||||
class Mixin {
|
||||
public:
|
||||
using TargetClass = Target;
|
||||
static constexpr std::string_view Owner = Target::Meta.internalName;
|
||||
|
||||
protected:
|
||||
// internal accumulation (per mixin type)
|
||||
static Mixin& instance() { static Mixin inst; return inst; }
|
||||
|
||||
static void add(const InjectionSpec& s) { instance().spec_.injections.push_back(s); }
|
||||
static void add(const RedirectSpec& s) { instance().spec_.redirects.push_back(s); }
|
||||
static void add(const ModifyConstSpec&s){ instance().spec_.constants.push_back(s); }
|
||||
static void add(const ModifyVarSpec& s) { instance().spec_.vars.push_back(s); }
|
||||
static void set_replace(CallbackId provider) { instance().spec_.replaceWholeClass = true; instance().spec_.classBodyProvider = provider; }
|
||||
|
||||
private:
|
||||
// Auto-register once per T
|
||||
static inline bool _autoReg = []{
|
||||
auto &inst = instance();
|
||||
inst.spec_.owner = Owner;
|
||||
Registrar::submit(inst.spec_);
|
||||
return true;
|
||||
}();
|
||||
|
||||
MixinSpec spec_{};
|
||||
};
|
||||
|
||||
} // namespace vl::mixin
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <Mixin/Types.hpp>
|
||||
|
||||
namespace vl::mixin {
|
||||
|
||||
// Core provides a function that consumes specs. API buffers until set.
|
||||
using RegistrarFn = void(*)(const MixinSpec&);
|
||||
|
||||
class Registrar {
|
||||
public:
|
||||
static void set_sink(RegistrarFn fn) {
|
||||
std::lock_guard lk(mu_);
|
||||
sink_ = fn;
|
||||
if (!pending_.empty()) {
|
||||
for (auto &s : pending_) sink_(s);
|
||||
pending_.clear();
|
||||
pending_.shrink_to_fit();
|
||||
}
|
||||
}
|
||||
static void submit(const MixinSpec& spec) {
|
||||
std::lock_guard lk(mu_);
|
||||
if (sink_) { sink_(spec); }
|
||||
else { pending_.push_back(spec); }
|
||||
}
|
||||
|
||||
private:
|
||||
static inline std::mutex mu_;
|
||||
static inline RegistrarFn sink_ = nullptr;
|
||||
static inline std::vector<MixinSpec> pending_;
|
||||
};
|
||||
|
||||
} // namespace vl::mixin
|
||||
@@ -0,0 +1,207 @@
|
||||
#pragma once
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <Mixin/Types.hpp>
|
||||
#include <Mixin/CallbackInfo.hpp>
|
||||
#include <Mixin/CallbackInfoExtended.hpp>
|
||||
#include <Reflect/Signature.hpp>
|
||||
|
||||
// ---- Slot decoding helpers: these are pure API conventions that Core obeys ----
|
||||
namespace vl::mixin::detail {
|
||||
|
||||
// Core passes args in this order:
|
||||
// - instance methods: slot0 = pointer to Self (wrapper); then parameters 0..N-1
|
||||
// - static methods: parameters 0..N-1
|
||||
// Each slot is a pointer to the exact wrapper or to a trivially copyable primitive storage.
|
||||
//
|
||||
// from_slot<T>(void*)
|
||||
// Overloads convert the slot to the target C++ type T.
|
||||
|
||||
template<class T>
|
||||
inline T from_slot(void* p) {
|
||||
// Default: value type stored directly in the slot's memory.
|
||||
return *reinterpret_cast<std::remove_reference_t<T>*>(p);
|
||||
}
|
||||
|
||||
// Specialize for references if you want by-ref semantics later.
|
||||
// For object wrapper types we still copy by value (cheap handle).
|
||||
template<class T>
|
||||
inline T& from_slot_ref(void* p) {
|
||||
return *reinterpret_cast<T*>(p);
|
||||
}
|
||||
|
||||
// Build tuple of arguments from slots [base .. base+N)
|
||||
template<class Tuple, std::size_t...I>
|
||||
Tuple build_tuple(void** slots, std::index_sequence<I...>) {
|
||||
return Tuple{ from_slot<std::tuple_element_t<I, Tuple>>(slots[I])... };
|
||||
}
|
||||
|
||||
// Apply callable to (Self?, Args..., CallbackInfo &/Returnable&)
|
||||
template<class F, class Self, class ArgsTuple, class CI>
|
||||
auto call_with(F&& f, Self* self, void** argSlots, CI& ci) {
|
||||
if constexpr (std::is_same_v<ArgsTuple, std::tuple<>>) {
|
||||
if constexpr (std::is_same_v<Self, void>) {
|
||||
return std::invoke(std::forward<F>(f), ci);
|
||||
} else {
|
||||
return std::invoke(std::forward<F>(f), *self, ci);
|
||||
}
|
||||
} else {
|
||||
constexpr auto N = std::tuple_size_v<ArgsTuple>;
|
||||
auto args = build_tuple<ArgsTuple>(argSlots, std::make_index_sequence<N>{});
|
||||
if constexpr (std::is_same_v<Self, void>) {
|
||||
return std::apply([&](auto&&... unpacked){
|
||||
return std::invoke(std::forward<F>(f), std::forward<decltype(unpacked)>(unpacked)..., ci);
|
||||
}, std::move(args));
|
||||
} else {
|
||||
return std::apply([&](auto&&... unpacked){
|
||||
return std::invoke(std::forward<F>(f), *self, std::forward<decltype(unpacked)>(unpacked)..., ci);
|
||||
}, std::move(args));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vl::mixin::detail
|
||||
|
||||
namespace vl::mixin {
|
||||
// ----------------------- INJECT typed adapter ----------------------------------
|
||||
// Accepts F with signature: (Self[, Args...], CallbackInfo&)
|
||||
// Returns ErasedCallback that Core can call.
|
||||
template<auto MethodPtr, class F>
|
||||
vl::mixin::ErasedCallback make_typed_inject(F&& f) {
|
||||
using Sig = ::vl::reflect::method_signature<MethodPtr>;
|
||||
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
|
||||
using ArgsT = typename Sig::Args;
|
||||
|
||||
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
|
||||
// Layout: [self?], [arg0..argN-1]
|
||||
void** cursor = args;
|
||||
SelfT* selfPtr = nullptr;
|
||||
if constexpr (!Sig::is_static) {
|
||||
selfPtr = reinterpret_cast<SelfT*>(*cursor++);
|
||||
}
|
||||
// Consume parameters from remaining slots
|
||||
(void)argc; // not needed with typed tuple
|
||||
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfo*>(cbinfo);
|
||||
vl::mixin::detail::call_with(fn, selfPtr, cursor, ci);
|
||||
return nullptr; // inject doesn't produce a direct return value
|
||||
};
|
||||
}
|
||||
|
||||
// ----------------------- REDIRECT typed adapter --------------------------------
|
||||
// Accepts F with signature: (Self[, Args...], CallbackInfoReturnable<Ret>&) -> Ret or void (set via ci)
|
||||
template<auto MethodPtr, class F>
|
||||
vl::mixin::ErasedCallback make_typed_redirect(F&& f) {
|
||||
using Sig = ::vl::reflect::method_signature<MethodPtr>;
|
||||
static_assert(!std::is_same_v<typename Sig::Ret, void>,
|
||||
"REDIRECT requires method with non-void return type");
|
||||
|
||||
using RetT = typename Sig::Ret;
|
||||
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
|
||||
using ArgsT = typename Sig::Args;
|
||||
|
||||
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
|
||||
void** cursor = args;
|
||||
SelfT* selfPtr = nullptr;
|
||||
if constexpr (!Sig::is_static) selfPtr = reinterpret_cast<SelfT*>(*cursor++);
|
||||
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfoReturnable<RetT>*>(cbinfo);
|
||||
|
||||
if constexpr (std::is_void_v<std::invoke_result_t<F, SelfT&, /*Args...*/ std::tuple_element_t<0, ArgsT>, vl::mixin::CallbackInfoReturnable<RetT>&>>) {
|
||||
// callable returns void, must set ci.setReturnValue(...)
|
||||
vl::mixin::detail::call_with<F, SelfT, ArgsT, vl::mixin::CallbackInfoReturnable<RetT>>(fn, selfPtr, cursor, ci);
|
||||
if (ci.hasReturnValue()) {
|
||||
// Core expects pointer to value storage
|
||||
return const_cast<RetT*>(&ci.getReturnValue());
|
||||
}
|
||||
return nullptr; // Core will fall back to original if not cancelled
|
||||
} else {
|
||||
// callable returns Ret directly
|
||||
static RetT storage{};
|
||||
storage = vl::mixin::detail::call_with<F, SelfT, ArgsT, vl::mixin::CallbackInfoReturnable<RetT>>(fn, selfPtr, cursor, ci);
|
||||
ci.setReturnValue(storage); // also mark cancelled
|
||||
return &storage;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// --------------------------- FIELD_GET ------------------------------------
|
||||
// lambda signature: (Self[, CallbackInfoField<FieldT>&]) -> void
|
||||
// if cancelled and ci.hasReturnValue(), value replaces field read
|
||||
template<auto FieldPtr, class F, class FieldT, class SelfT>
|
||||
vl::mixin::ErasedCallback make_typed_field_get(F&& fn) {
|
||||
return [f = std::forward<F>(fn)](void** args, int argc, void* cbinfo) -> void* {
|
||||
(void)argc;
|
||||
SelfT* self = reinterpret_cast<SelfT*>(args[0]);
|
||||
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfoField<FieldT>*>(cbinfo);
|
||||
std::invoke(f, *self, ci);
|
||||
if (ci.hasReturnValue()) return const_cast<FieldT*>(&ci.getReturnValue());
|
||||
return nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
// --------------------------- FIELD_PUT ------------------------------------
|
||||
// lambda signature: (Self, FieldT&, CallbackInfoField<FieldT>&)
|
||||
template<auto FieldPtr, class F, class FieldT, class SelfT>
|
||||
vl::mixin::ErasedCallback make_typed_field_put(F&& fn) {
|
||||
return [f = std::forward<F>(fn)](void** args, int argc, void* cbinfo) -> void* {
|
||||
(void)argc;
|
||||
SelfT* self = reinterpret_cast<SelfT*>(args[0]);
|
||||
FieldT* incoming = reinterpret_cast<FieldT*>(args[1]);
|
||||
auto& ci = *reinterpret_cast<vl::mixin::CallbackInfoField<FieldT>*>(cbinfo);
|
||||
std::invoke(f, *self, *incoming, ci);
|
||||
return nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
// ------------------- INVOKE BEFORE -------------------------------------
|
||||
// lambda signature: (Self[, Args...], CallbackInfoReturnable<Ret>&)
|
||||
// may cancel original call and set replacement return
|
||||
template<auto TargetMethodPtr, class F>
|
||||
ErasedCallback make_typed_invoke_before(F&& f) {
|
||||
using Sig = ::vl::reflect::method_signature<TargetMethodPtr>;
|
||||
using RetT = typename Sig::Ret;
|
||||
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
|
||||
using ArgsT = typename Sig::Args;
|
||||
|
||||
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
|
||||
void** cursor = args;
|
||||
SelfT* selfPtr = nullptr;
|
||||
if constexpr (!Sig::is_static) selfPtr = reinterpret_cast<SelfT*>(*cursor++);
|
||||
auto& ci = *reinterpret_cast<CallbackInfoReturnable<RetT>*>(cbinfo);
|
||||
detail::call_with(fn, selfPtr, cursor, ci);
|
||||
if (ci.isCancelled() && ci.hasReturnValue()) {
|
||||
return const_cast<RetT*>(&ci.getReturnValue());
|
||||
}
|
||||
return nullptr; // continue to original invoke
|
||||
};
|
||||
}
|
||||
|
||||
// ------------------- INVOKE AFTER --------------------------------------
|
||||
// lambda signature: (Self[, Args...], Ret&, CallbackInfoInvokeAfter<Ret>&)
|
||||
template<auto TargetMethodPtr, class F>
|
||||
ErasedCallback make_typed_invoke_after(F&& f) {
|
||||
using Sig = ::vl::reflect::method_signature<TargetMethodPtr>;
|
||||
using RetT = typename Sig::Ret;
|
||||
using SelfT = std::conditional_t<Sig::is_static, void, typename Sig::Self>;
|
||||
using ArgsT = typename Sig::Args;
|
||||
|
||||
return [fn = std::forward<F>(f)](void** args, int argc, void* cbinfo) -> void* {
|
||||
void** cursor = args;
|
||||
SelfT* selfPtr = nullptr;
|
||||
if constexpr (!Sig::is_static) selfPtr = reinterpret_cast<SelfT*>(*cursor++);
|
||||
auto& ci = *reinterpret_cast<CallbackInfoInvokeAfter<RetT>*>(cbinfo);
|
||||
|
||||
// Last slot in args[] is original return value (Core places it)
|
||||
RetT* retSlot = reinterpret_cast<RetT*>(cursor[argc - 1]);
|
||||
ci.setOriginalReturn(*retSlot);
|
||||
|
||||
// Call the mod callback
|
||||
std::invoke(fn, *selfPtr, *retSlot, ci);
|
||||
|
||||
// If cancelled and ci.hasReturnValue(), Core replaces return with it
|
||||
if (ci.isCancelled() && ci.hasReturnValue())
|
||||
return const_cast<RetT*>(&ci.getReturnValue());
|
||||
return nullptr;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace vl::mixin {
|
||||
|
||||
enum class At : std::uint8_t { HEAD, RETURN, INVOKE, FIELD_GET, FIELD_PUT };
|
||||
enum class Side: std::uint8_t { Before, After };
|
||||
|
||||
using CallbackId = std::size_t;
|
||||
|
||||
// Erased ABI for callbacks: Core marshals args and provides a CallbackInfo*.
|
||||
using ErasedCallback = std::function<void*(void** args, int argc, void* cbinfo)>;
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// INVOKE sub-descriptor
|
||||
// ---------------------------------------------------------------------
|
||||
struct InvokeTarget {
|
||||
std::string owner; // internal class name (e.g. net/minecraft/client/Minecraft)
|
||||
std::string name; // method name to match (e.g. "doAttack")
|
||||
std::string desc; // full JVM descriptor (e.g. "(I)Z")
|
||||
};
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Injection spec (for all @At types)
|
||||
// ---------------------------------------------------------------------
|
||||
struct InjectionSpec {
|
||||
std::string owner; // the target class where we inject
|
||||
std::string methodName; // the method name where we inject
|
||||
std::string methodDesc; // the method descriptor
|
||||
At at; // HEAD / RETURN / INVOKE / FIELD_GET / FIELD_PUT
|
||||
Side when; // Before / After (for INVOKE etc.)
|
||||
CallbackId cb; // callback id
|
||||
|
||||
// For INVOKE / FIELD hooks, optional nested target to match the inside call or field
|
||||
InvokeTarget invoke;
|
||||
std::string fieldOwner;
|
||||
std::string fieldName;
|
||||
std::string fieldDesc;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Other specs unchanged
|
||||
// ---------------------------------------------------------------------
|
||||
struct RedirectSpec {
|
||||
std::string owner;
|
||||
std::string methodName;
|
||||
std::string methodDesc;
|
||||
CallbackId cb;
|
||||
};
|
||||
|
||||
struct ModifyConstSpec {
|
||||
enum Kind { Int, Long, Float, Double, String } kind{};
|
||||
std::string owner;
|
||||
std::string methodName;
|
||||
std::string methodDesc;
|
||||
CallbackId cb;
|
||||
int64_t i64{};
|
||||
double fp{};
|
||||
std::string str;
|
||||
};
|
||||
|
||||
struct ModifyVarSpec {
|
||||
std::string owner;
|
||||
std::string methodName;
|
||||
std::string methodDesc;
|
||||
CallbackId cb;
|
||||
int varIndex{};
|
||||
};
|
||||
// Whole-class mixin description
|
||||
struct MixinSpec {
|
||||
std::string owner;
|
||||
std::vector<InjectionSpec> injections;
|
||||
std::vector<RedirectSpec> redirects;
|
||||
std::vector<ModifyConstSpec> constants;
|
||||
std::vector<ModifyVarSpec> vars;
|
||||
bool replaceWholeClass{false};
|
||||
CallbackId classBodyProvider{};
|
||||
};
|
||||
|
||||
} // namespace vl::mixin
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
|
||||
namespace vl::reflect {
|
||||
|
||||
// Generator fills these specializations for every discoverable method pointer.
|
||||
template<auto MethodPtr>
|
||||
struct method_info {
|
||||
static_assert(sizeof(MethodPtr) == 0,
|
||||
"No reflection metadata found for this method pointer. "
|
||||
"Did you include the generated header for this class?");
|
||||
static constexpr std::string_view owner = ""; // "net/minecraft/.../LocalPlayer"
|
||||
static constexpr std::string_view name = ""; // "tick"
|
||||
static constexpr std::string_view desc = ""; // "()V"
|
||||
static constexpr bool is_static = false;
|
||||
};
|
||||
|
||||
template<auto MethodPtr>
|
||||
constexpr bool is_valid_method() {
|
||||
// If the primary template is instantiated, owner=="" and will trip this.
|
||||
return method_info<MethodPtr>::owner.size() > 0;
|
||||
}
|
||||
|
||||
template<auto MethodPtr>
|
||||
constexpr std::string_view method_owner() { return method_info<MethodPtr>::owner; }
|
||||
template<auto MethodPtr>
|
||||
constexpr std::string_view method_name() { return method_info<MethodPtr>::name; }
|
||||
template<auto MethodPtr>
|
||||
constexpr std::string_view method_desc() { return method_info<MethodPtr>::desc; }
|
||||
template<auto MethodPtr>
|
||||
constexpr bool method_is_static() { return method_info<MethodPtr>::is_static; }
|
||||
|
||||
} // namespace vl::reflect
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include <type_traits>
|
||||
#include <tuple>
|
||||
|
||||
namespace vl::reflect {
|
||||
|
||||
// Primary template: intentionally incomplete; generator will specialize.
|
||||
template<auto MethodPtr>
|
||||
struct method_signature; // exposes: using Self, using Ret, using Args = std::tuple<...>; static constexpr bool is_static;
|
||||
|
||||
template<class T> struct is_void : std::is_same<T, void> {};
|
||||
template<class T> inline constexpr bool is_void_v = is_void<T>::value;
|
||||
|
||||
} // namespace vl::reflect
|
||||
Reference in new issue
Block a user