libER 0.1.4.2
ELDEN RING API library
Loading...
Searching...
No Matches
reflection.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include <detail/literal_string.hpp>
11#include <detail/optref.hpp>
12#include <detail/preprocessor.hpp>
13
16
18
19#include <cstddef>
20#include <cstdint>
21#include <string>
22#include <utility>
23
24namespace from {
25// Dantelion reflection
26namespace DLRF {
27// DLMethodInvoker trait tags
31
36template <typename Ctx = DLMethodInvokeContext>
38 virtual int invoke(Ctx* context) LIBER_INTERFACE_ONLY;
39
40public:
41 virtual ~DLMethodInvokerImpl() LIBER_INTERFACE_ONLY;
42
43private:
44 virtual int arg_count() LIBER_INTERFACE_ONLY;
45 virtual void zero_context1(Ctx* context) LIBER_INTERFACE_ONLY;
46 virtual void zero_context2(Ctx* context) LIBER_INTERFACE_ONLY;
47 virtual char* ref_byte() LIBER_INTERFACE_ONLY;
48};
49
55
60struct LIBER_DUMMY DLConcreteMethodInvoker : public DLMethodInvoker {
65 void* method;
66};
67
68// Forward declaration
69class DLRuntimeClass;
70
76 LIBER_CLASS(DLRuntimeMethod);
77
78 // DLRuntimeMethod() noexcept
79 // : owner(nullptr), method_name(nullptr), method_name_w(nullptr) {}
80
88 DLRuntimeMethod(DLRuntimeClass* owner, const char* method_name,
89 const wchar_t* method_name_w) noexcept
90 : owner(owner), method_name(method_name), method_name_w(method_name_w) {
91 }
92
98 const char* get_method_name() const noexcept {
99 return this->method_name;
100 }
101
107 const wchar_t* get_method_name_w() const noexcept {
108 return this->method_name_w;
109 }
110
118 return this->invokers;
119 }
120
121private:
122 friend class DLRuntimeClass;
123
124 DLRuntimeClass* owner;
125 const char* method_name;
126 const wchar_t* method_name_w;
128 from::vector<void*> liber_unknown;
129 void* liber_unknown = nullptr;
130 DLKR::DLPlainMutex mutex;
131};
132
139template <typename T>
141 LIBER_CLASS(DLRuntimeObjectHolder);
142
150 DLRuntimeObjectHolder(from::unique_ptr<T>&& method, const char* name,
151 const wchar_t* name_w) noexcept
152 : object(std::move(method)), name(name), name_w(name_w),
153 length(std::strlen(name)) {}
154
160 const T* get() const noexcept {
161 return this->object.get();
162 }
163
164private:
165 friend class DLRuntimeClass;
166
167 from::unique_ptr<T> object;
168 const char* name;
169 const wchar_t* name_w;
170 size_t length;
171};
172
178
184
189using DLRuntimeClassPair = std::pair<char*, DLRuntimeClass*>;
190
200public:
201 LIBER_CLASS(DLRuntimeClass);
202
203 DLRuntimeClass() noexcept : base_class(nullptr) {}
204
205 virtual ~DLRuntimeClass() = default;
206
212 virtual const char* class_name() const noexcept = 0;
213
219 virtual const wchar_t* class_name_w() const noexcept = 0;
220
221private:
222 // Methods that return pointers to null
223 // bytes in static memory (bad for re-implementation),
224 // adresses of which are used for sorting DLRuntimeClass instances
225 virtual char* ref_byte1() = 0;
226 virtual char* ref_byte2() = 0;
227 virtual char* ref_byte3() = 0;
228 virtual char* ref_byte4() = 0;
229
230 // Always false?
231 virtual bool unk_always_false() = 0;
232
233 // Potentially a method that frees a base class of
234 // a DLRuntimeClass, or a DLRuntimeClass from a similar structure
235 virtual void free_base(DLRuntimeClass** base_of,
236 DLKR::DLAllocator* allocator) = 0;
237
238public:
244 virtual size_t class_size() const noexcept = 0;
245
254 LIBERAPI virtual void add_constructor_invoker(DLMethodInvoker* invoker,
255 const char* method_name, const wchar_t* method_name_w);
256
264 LIBERAPI virtual void add_method_invoker(DLMethodInvoker* invoker,
265 const char* method_name, const wchar_t* method_name_w);
266
272 const DLRuntimeClass* get_base() const noexcept {
273 return this->base_class;
274 }
275
282 DLRuntimeMethod* method_ptr = this->runtime_constructor.get();
283 if (!method_ptr)
284 return std::nullopt;
285 return *method_ptr;
286 }
287
295 return this->runtime_methods;
296 }
297
305 const std::string_view& method_name) noexcept;
306
318 LIBERAPI static const from::vector<DLRuntimeClassHolder>&
320
332 LIBERAPI static const from::vector<DLRuntimeClassPair>&
334
335private:
336 // A pointer to the base class, if the class is derived
337 DLRuntimeClass* base_class;
338 // A DLRuntimeMethod of the class's constructor
339 from::unique_ptr<DLRuntimeMethod> runtime_constructor;
340 // A vector of DLRuntimeMethodHolders sorted by string length
341 from::vector<DLRuntimeMethodHolder> runtime_methods;
342};
343
349template <class Impl>
351public:
352 LIBER_CLASS(DLRuntimeClassImpl);
353
354 virtual ~DLRuntimeClassImpl() = default;
355
363 const wchar_t* class_name_w) noexcept
364 : DLRuntimeClass(), _class_name(class_name),
365 _class_name_w(class_name_w) {}
366
367 const char* class_name() const noexcept override {
368 return this->_class_name;
369 }
370 const wchar_t* class_name_w() const noexcept override {
371 return this->_class_name_w;
372 }
373
374private:
375 char* ref_byte1() LIBER_INTERFACE_OVERRIDE;
376 char* ref_byte2() LIBER_INTERFACE_OVERRIDE;
377 char* ref_byte3() LIBER_INTERFACE_OVERRIDE;
378 char* ref_byte4() LIBER_INTERFACE_OVERRIDE;
379
380 bool unk_always_false() LIBER_INTERFACE_OVERRIDE;
381 void free_base(DLRuntimeClass**,
382 DLKR::DLAllocator*) LIBER_INTERFACE_OVERRIDE;
383
384public:
385 size_t class_size() const noexcept override {
386 return sizeof(Impl);
387 }
388
389private:
390 const char* _class_name;
391 const wchar_t* _class_name_w;
392};
393
400template <class Impl, liber::literal_string ImplName>
402 static constexpr auto dl_runtime_class_name = ImplName.trunc();
403 static constexpr auto dl_runtime_class_name_w = ImplName.widen();
404
405public:
412 inline static DLRF::DLRuntimeClassImpl<Impl> dl_runtime_class{
413 DLRuntimeClassTemplate::dl_runtime_class_name.string,
414 DLRuntimeClassTemplate::dl_runtime_class_name_w.string
415 };
416};
417
418LIBER_ASSERTS_BEGIN(DLRuntimeMethod);
419LIBER_ASSERT_SIZE(0x70);
420LIBER_ASSERT_OFFS(0x18, invokers);
421LIBER_ASSERT_OFFS(0x60, mutex);
422LIBER_ASSERTS_END;
423
424LIBER_ASSERTS_TEMPLATE_BEGIN(DLRuntimeObjectHolder, DLRuntimeMethod);
425LIBER_ASSERT_SIZE(0x20);
426LIBER_ASSERTS_END;
427
428LIBER_ASSERTS_BEGIN(DLRuntimeClass);
429LIBER_ASSERT_SIZE(0x38);
430LIBER_ASSERTS_END;
431
432LIBER_ASSERTS_TEMPLATE_BEGIN(DLRuntimeClassImpl, liber::dummy);
433LIBER_ASSERT_SIZE(0x48);
434LIBER_ASSERTS_END;
435} // namespace DLRF
436} // namespace from
A wrapper around a Windows kernel mutex.
Definition kernel_runtime.hpp:106
Layout of a Dantelion2 method invoker interface.
Definition reflection.hpp:37
Concrete DLRuntimeClass reflection for class Impl.
Definition reflection.hpp:350
DLRuntimeClassImpl(const char *class_name, const wchar_t *class_name_w) noexcept
Construct a new DLRuntimeClassImpl object.
Definition reflection.hpp:362
const char * class_name() const noexcept override
Get the name of the type.
Definition reflection.hpp:367
const wchar_t * class_name_w() const noexcept override
Get the name of the type (wide).
Definition reflection.hpp:370
The libER reflection implementation of DLRuntimeClass.
Definition reflection.hpp:401
Type reflection object for an implementing class.
Definition reflection.hpp:199
const from::vector< DLRuntimeMethodHolder > & get_methods() const noexcept
Get every bound method.
Definition reflection.hpp:294
liber::optref< const DLRuntimeMethod > get_constructor() const noexcept
Get the constructor method (if bound)
Definition reflection.hpp:281
virtual LIBERAPI void add_method_invoker(DLMethodInvoker *invoker, const char *method_name, const wchar_t *method_name_w)
Add an invoker to a vector of invokers for a given method.
virtual LIBERAPI void add_constructor_invoker(DLMethodInvoker *invoker, const char *method_name, const wchar_t *method_name_w)
Add an invoker to a vector of invokers for the class's constructor.
LIBERAPI liber::optref< DLRuntimeMethod > find_method(const std::string_view &method_name) noexcept
Find a bound method by name (if it exists).
const DLRuntimeClass * get_base() const noexcept
Get a derived class's base class.
Definition reflection.hpp:272
virtual const wchar_t * class_name_w() const noexcept=0
Get the name of the type (wide).
virtual const char * class_name() const noexcept=0
Get the name of the type.
static LIBERAPI const from::vector< DLRuntimeClassHolder > & get_registered_classes() noexcept
Get a vector of all globally registered DLRuntimeClasses.
virtual size_t class_size() const noexcept=0
Size of the type.
static LIBERAPI const from::vector< DLRuntimeClassPair > & get_runtime_pairs() noexcept
Get a vector of all globally registered DLRuntimeClass pairs.
The main libER stand-in for ER allocator proxies.
Definition from_allocator.hpp:310
from::unique_ptr based on std::unique_ptr
std::unique_ptr< T, from::delay_delete< T, AllocatorTag > > unique_ptr
std::unique_ptr with from::allocator and from::delay_delete.
Definition from_unique_ptr.hpp:22
from::vector based on std::vector
std::vector< T, from::allocator< T, AllocatorTag > > vector
std::vector with from::allocator.
Definition from_vector.hpp:23
Dantelion2 kernel object management and synchronization.
Optional references based on std::optional.
std::pair< char *, DLRuntimeClass * > DLRuntimeClassPair
A pair of a char and a DLRuntimeClass pointers.
Definition reflection.hpp:189
Concrete method invoker, exposition only.
Definition reflection.hpp:60
void * method
The bound method.
Definition reflection.hpp:65
Definition reflection.hpp:29
Definition reflection.hpp:28
Definition reflection.hpp:30
A wrapper that represents a method with a vector of invoker objects.
Definition reflection.hpp:75
const char * get_method_name() const noexcept
Get the method name.
Definition reflection.hpp:98
const from::vector< DLMethodInvoker * > & get_invokers() const noexcept
Get all the invokers bound to this method.
Definition reflection.hpp:117
DLRuntimeMethod(DLRuntimeClass *owner, const char *method_name, const wchar_t *method_name_w) noexcept
Construct a new DLRuntimeMethod object.
Definition reflection.hpp:88
const wchar_t * get_method_name_w() const noexcept
Get the method name (wide)
Definition reflection.hpp:107
A holder for a DLRF::DLRuntime object.
Definition reflection.hpp:140
const T * get() const noexcept
Get a pointer to the held method instance.
Definition reflection.hpp:160
DLRuntimeObjectHolder(from::unique_ptr< T > &&method, const char *name, const wchar_t *name_w) noexcept
Construct a new DLRuntimeObjectHolder object.
Definition reflection.hpp:150
A std::optional<std::reference_wrapper<T>> wrapper for lvalue references.
Definition optref.hpp:31