2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
2022-10-06 13:46:07 -04:00
|
|
|
|
2022-10-08 05:41:36 -04:00
|
|
|
#include <Geode/Bindings.hpp>
|
2022-07-30 12:24:03 -04:00
|
|
|
#include "Traits.hpp"
|
2022-10-08 05:59:06 -04:00
|
|
|
#include <Geode/loader/Loader.hpp>
|
2022-07-30 12:24:03 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace geode::modifier {
|
2022-10-06 13:46:07 -04:00
|
|
|
class FieldContainer {
|
|
|
|
private:
|
2022-07-30 12:24:03 -04:00
|
|
|
std::vector<void*> m_containedFields;
|
|
|
|
std::vector<std::function<void(void*)>> m_destructorFunctions;
|
|
|
|
|
2022-10-06 13:46:07 -04:00
|
|
|
public:
|
2022-07-30 12:24:03 -04:00
|
|
|
~FieldContainer() {
|
|
|
|
for (auto i = 0u; i < m_containedFields.size(); i++) {
|
|
|
|
m_destructorFunctions[i](m_containedFields[i]);
|
|
|
|
operator delete(m_containedFields[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void* getField(size_t index) {
|
|
|
|
if (m_containedFields.size() <= index) {
|
|
|
|
m_containedFields.resize(index + 1);
|
|
|
|
m_destructorFunctions.resize(index + 1);
|
|
|
|
}
|
|
|
|
return m_containedFields.at(index);
|
|
|
|
}
|
|
|
|
void* setField(size_t index, size_t size, std::function<void(void*)> destructor) {
|
|
|
|
m_containedFields.at(index) = operator new(size);
|
|
|
|
m_destructorFunctions.at(index) = destructor;
|
|
|
|
return m_containedFields.at(index);
|
|
|
|
}
|
2022-10-06 13:46:07 -04:00
|
|
|
static FieldContainer* from(cocos2d::CCNode* node) {
|
|
|
|
return node->getFieldContainer();
|
|
|
|
}
|
2022-07-30 12:24:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class Base, class Intermediate, class Parent>
|
|
|
|
class FieldIntermediate {
|
|
|
|
// Padding used for guaranteeing any member of parents
|
|
|
|
// will be in between sizeof(Intermediate) and sizeof(Parent)
|
2022-10-05 09:08:54 -04:00
|
|
|
uintptr_t m_padding;
|
2022-07-30 12:24:03 -04:00
|
|
|
public:
|
|
|
|
static void fieldConstructor(void* offsetField) {
|
|
|
|
std::array<std::byte, sizeof(Parent)> parentContainer;
|
|
|
|
|
|
|
|
auto parent = new (parentContainer.data()) Parent();
|
|
|
|
|
|
|
|
parent->Intermediate::~Intermediate();
|
|
|
|
|
|
|
|
std::memcpy(
|
|
|
|
offsetField,
|
|
|
|
std::launder(&parentContainer[sizeof(Intermediate)]),
|
|
|
|
sizeof(Parent) - sizeof(Intermediate)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
static void fieldDestructor(void* offsetField) {
|
|
|
|
std::array<std::byte, sizeof(Parent)> parentContainer;
|
|
|
|
|
|
|
|
auto parent = new (parentContainer.data()) Intermediate();
|
|
|
|
|
|
|
|
std::memcpy(
|
|
|
|
std::launder(&parentContainer[sizeof(Intermediate)]),
|
|
|
|
offsetField,
|
|
|
|
sizeof(Parent) - sizeof(Intermediate)
|
|
|
|
);
|
|
|
|
|
|
|
|
static_cast<Parent*>(parent)->Parent::~Parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class=std::enable_if_t<true>>
|
|
|
|
Parent* operator->() {
|
|
|
|
// get the this pointer of the base
|
2022-10-04 06:32:40 -04:00
|
|
|
auto node = reinterpret_cast<Parent*>(reinterpret_cast<std::byte*>(this) - sizeof(Base));
|
|
|
|
static_assert(sizeof(Base) == offsetof(Parent, m_fields), "offsetof not correct");
|
2022-10-06 13:46:07 -04:00
|
|
|
auto container = FieldContainer::from(node);
|
2022-07-30 12:24:03 -04:00
|
|
|
static size_t index = Loader::get()->getFieldIndexForClass(typeid(Base).hash_code());
|
|
|
|
// this pointer is offset
|
|
|
|
auto offsetField = container->getField(index);
|
|
|
|
if (!offsetField) {
|
|
|
|
offsetField = container->setField(index, sizeof(Parent) - sizeof(Intermediate),
|
|
|
|
&FieldIntermediate::fieldDestructor
|
|
|
|
);
|
|
|
|
|
|
|
|
FieldIntermediate::fieldConstructor(offsetField);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<Parent*>(reinterpret_cast<std::byte*>(offsetField) - sizeof(Intermediate));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|