2022-07-30 12:24:03 -04:00
|
|
|
#pragma once
|
2022-10-06 13:46:07 -04:00
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
#include "Traits.hpp"
|
2022-12-24 13:13:53 -05:00
|
|
|
|
2022-10-08 05:59:06 -04:00
|
|
|
#include <Geode/loader/Loader.hpp>
|
2023-02-08 10:25:07 -05:00
|
|
|
#include "../utils/MiniFunction.hpp"
|
2022-12-24 13:13:53 -05:00
|
|
|
#include <cocos2d.h>
|
2022-07-30 12:24:03 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2022-10-13 05:56:23 -04:00
|
|
|
namespace cocos2d {
|
2022-10-30 14:59:20 -04:00
|
|
|
class CCNode;
|
2022-10-13 05:56:23 -04:00
|
|
|
}
|
|
|
|
|
2022-12-24 13:13:53 -05:00
|
|
|
namespace geode {
|
|
|
|
template <class, class>
|
|
|
|
class Modify;
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:24:03 -04:00
|
|
|
namespace geode::modifier {
|
2022-10-30 14:59:20 -04:00
|
|
|
class FieldContainer {
|
|
|
|
private:
|
|
|
|
std::vector<void*> m_containedFields;
|
2023-02-08 10:25:07 -05:00
|
|
|
std::vector<utils::MiniFunction<void(void*)>> m_destructorFunctions;
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
~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) {
|
2023-03-01 17:02:09 -05:00
|
|
|
m_containedFields.push_back(nullptr);
|
|
|
|
m_destructorFunctions.push_back(nullptr);
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
return m_containedFields.at(index);
|
|
|
|
}
|
|
|
|
|
2023-02-08 10:25:07 -05:00
|
|
|
void* setField(size_t index, size_t size, utils::MiniFunction<void(void*)> destructor) {
|
2022-10-30 14:59:20 -04:00
|
|
|
m_containedFields.at(index) = operator new(size);
|
|
|
|
m_destructorFunctions.at(index) = destructor;
|
|
|
|
return m_containedFields.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FieldContainer* from(cocos2d::CCNode* node) {
|
|
|
|
return node->getFieldContainer();
|
|
|
|
}
|
|
|
|
};
|
2022-12-24 13:13:53 -05:00
|
|
|
|
2023-03-01 17:08:25 -05:00
|
|
|
[[deprecated("Will be removed in 1.0.0")]]
|
|
|
|
GEODE_DLL size_t getFieldIndexForClass(size_t hash);
|
2023-03-01 17:02:09 -05:00
|
|
|
GEODE_DLL size_t getFieldIndexForClass(char const* name);
|
2022-10-30 14:59:20 -04:00
|
|
|
|
2022-11-09 12:11:50 -05:00
|
|
|
template <class Parent, class Base>
|
2022-10-30 14:59:20 -04:00
|
|
|
class FieldIntermediate {
|
2022-12-24 13:13:53 -05:00
|
|
|
using Intermediate = Modify<Parent, Base>;
|
2022-10-30 14:59:20 -04:00
|
|
|
// Padding used for guaranteeing any member of parents
|
|
|
|
// will be in between sizeof(Intermediate) and sizeof(Parent)
|
|
|
|
uintptr_t m_padding;
|
|
|
|
|
|
|
|
public:
|
2022-12-24 13:13:53 -05:00
|
|
|
// the constructor that constructs the fields.
|
|
|
|
// we construct the Parent first,
|
2022-10-30 14:59:20 -04:00
|
|
|
static void fieldConstructor(void* offsetField) {
|
|
|
|
std::array<std::byte, sizeof(Parent)> parentContainer;
|
|
|
|
|
|
|
|
auto parent = new (parentContainer.data()) Parent();
|
|
|
|
|
2022-12-24 13:13:53 -05:00
|
|
|
parent->Intermediate::~Intermediate();
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
std::memcpy(
|
2022-12-24 13:13:53 -05:00
|
|
|
offsetField,
|
|
|
|
std::launder(&parentContainer[sizeof(Intermediate)]),
|
|
|
|
sizeof(Parent) - sizeof(Intermediate)
|
2022-10-30 14:59:20 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fieldDestructor(void* offsetField) {
|
|
|
|
std::array<std::byte, sizeof(Parent)> parentContainer;
|
|
|
|
|
2022-12-24 13:13:53 -05:00
|
|
|
auto parent = new (parentContainer.data()) Intermediate();
|
2022-10-30 14:59:20 -04:00
|
|
|
|
|
|
|
std::memcpy(
|
2022-12-24 13:13:53 -05:00
|
|
|
std::launder(&parentContainer[sizeof(Intermediate)]),
|
|
|
|
offsetField,
|
|
|
|
sizeof(Parent) - sizeof(Intermediate)
|
2022-10-30 14:59:20 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
static_cast<Parent*>(parent)->Parent::~Parent();
|
|
|
|
}
|
|
|
|
|
2023-03-01 17:02:09 -05:00
|
|
|
operator Parent*() {
|
2022-10-30 14:59:20 -04:00
|
|
|
// get the this pointer of the base
|
2022-12-24 13:13:53 -05:00
|
|
|
// field intermediate is the first member of Modify
|
|
|
|
// meaning we canget the base from ourself
|
|
|
|
auto node = reinterpret_cast<Parent*>(reinterpret_cast<std::byte*>(this) - sizeof(Base));
|
2022-10-30 14:59:20 -04:00
|
|
|
static_assert(sizeof(Base) == offsetof(Parent, m_fields), "offsetof not correct");
|
2022-12-24 13:13:53 -05:00
|
|
|
|
|
|
|
// generating the container if it doesn't exist
|
2022-10-30 14:59:20 -04:00
|
|
|
auto container = FieldContainer::from(node);
|
2022-12-24 13:13:53 -05:00
|
|
|
|
|
|
|
// the index is global across all mods, so the
|
|
|
|
// function is defined in the loader source
|
2023-03-01 17:02:09 -05:00
|
|
|
static size_t index = getFieldIndexForClass(typeid(Base).name());
|
2022-12-24 13:13:53 -05:00
|
|
|
|
|
|
|
// the fields are actually offset from their original
|
|
|
|
// offset, this is done to save on allocation and space
|
2022-10-30 14:59:20 -04:00
|
|
|
auto offsetField = container->getField(index);
|
|
|
|
if (!offsetField) {
|
|
|
|
offsetField = container->setField(
|
2022-12-24 13:13:53 -05:00
|
|
|
index, sizeof(Parent) - sizeof(Intermediate), &FieldIntermediate::fieldDestructor
|
2022-10-30 14:59:20 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
FieldIntermediate::fieldConstructor(offsetField);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<Parent*>(
|
2022-12-24 13:13:53 -05:00
|
|
|
reinterpret_cast<std::byte*>(offsetField) - sizeof(Intermediate)
|
2022-10-30 14:59:20 -04:00
|
|
|
);
|
|
|
|
}
|
2023-03-03 04:08:56 -05:00
|
|
|
|
|
|
|
Parent* self() {
|
|
|
|
return this->operator Parent*();
|
|
|
|
}
|
2023-03-01 17:02:09 -05:00
|
|
|
|
|
|
|
Parent* operator->() {
|
|
|
|
return this->operator Parent*();
|
|
|
|
}
|
2022-10-30 14:59:20 -04:00
|
|
|
};
|
2022-07-30 12:24:03 -04:00
|
|
|
|
|
|
|
}
|