Merge branch 'main' of https://github.com/geode-sdk/geode into main

This commit is contained in:
HJfod 2023-02-26 12:47:26 +02:00
commit 277ad8c159

View file

@ -83,12 +83,22 @@ namespace geode::addresser {
// } // }
// I extra gave up // I extra gave up
template <class = void>
static cocos2d::extension::CCScrollView* generateInstance(cocos2d::extension::CCScrollView*) { static cocos2d::extension::CCScrollView* generateInstance(cocos2d::extension::CCScrollView*) {
return cocos2d::extension::CCScrollView::create({0.0f, 0.0f}, cocos2d::CCLayer::create()); return cocos2d::extension::CCScrollView::create({0.0f, 0.0f}, cocos2d::CCLayer::create());
} }
template <class = void>
static cocos2d::CCFileUtils* generateInstance(cocos2d::CCFileUtils*) {
return cocos2d::CCFileUtils::sharedFileUtils();
}
template <class Class> template <class Class>
static Class* generateInstance(Class*) { static Class* generateInstance(Class*) {
if constexpr (std::is_abstract_v<Class>) {
// Cant construct abstract classes, so fail early
return nullptr;
} else {
// Create a random memory block with the size of Class // Create a random memory block with the size of Class
// Assign a pointer to that block and cast it to type Class* // Assign a pointer to that block and cast it to type Class*
uint8_t dum[sizeof(Class)]{}; uint8_t dum[sizeof(Class)]{};
@ -101,10 +111,11 @@ namespace geode::addresser {
// this is how the first human was made // this is how the first human was made
return ins; return ins;
} }
}
template <class Class> template <class Class>
static Class* cachedInstance() { static Class* cachedInstance() {
static auto ret = generateInstance<Class>(nullptr); static auto ret = generateInstance(static_cast<Class*>(nullptr));
return ret; return ret;
} }
@ -112,24 +123,20 @@ namespace geode::addresser {
* Specialized functionss * Specialized functionss
*/ */
template <typename R, typename T, typename... Ps> template <typename R, typename T, typename... Ps>
static intptr_t addressOfVirtual( static intptr_t addressOfVirtual(R (T::*func)(Ps...)) {
R (T::*func)(Ps...), typename std::enable_if_t<!std::is_abstract_v<T>>* = 0
) {
using geode::cast::reference_cast; using geode::cast::reference_cast;
auto ins = cachedInstance<T>();
// generateInstance will return nullptr on most abstract classes,
// so dont bother getting the address
if (ins == nullptr) {
return 0;
}
auto index = indexOf(func); auto index = indexOf(func);
auto thunk = thunkOf(func); auto thunk = thunkOf(func);
auto ins = cachedInstance<T>();
// log::debug("[[" + utils::intToHex((void*)ins) + " + " + utils::intToHex(thunk) + "] + // [[this + thunk] + offset] is the function we want
// " + utils::intToHex(index) + "]"); auto address = *reinterpret_cast<intptr_t*>(*reinterpret_cast<intptr_t*>(reinterpret_cast<intptr_t>(ins) + thunk) + index);
// log::debug(
// "[[{} + {}] + {}]", utils::intToHex((void*)ins), utils::intToHex(thunk),
// utils::intToHex(index)
// );
// [[this + thunk] + offset] is the f we want
auto address = *(intptr_t*)(*(intptr_t*)(reference_cast<intptr_t>(ins) + thunk) + index);
address = followThunkFunction(address); address = followThunkFunction(address);
@ -137,27 +144,10 @@ namespace geode::addresser {
} }
template <typename R, typename T, typename... Ps> template <typename R, typename T, typename... Ps>
static intptr_t addressOfVirtual( static intptr_t addressOfVirtual(R (T::*func)(Ps...) const) {
R (T::*func)(Ps...) const,
typename std::enable_if_t<std::is_copy_constructible_v<T> && !std::is_abstract_v<T>>* = 0
) {
return addressOfVirtual(reinterpret_cast<R (T::*)(Ps...)>(func)); return addressOfVirtual(reinterpret_cast<R (T::*)(Ps...)>(func));
} }
template <typename R, typename T, typename... Ps>
static intptr_t addressOfVirtual(
R (T::*func)(Ps...), typename std::enable_if_t<std::is_abstract_v<T>>* = 0
) {
return 0;
}
template <typename R, typename T, typename... Ps>
static intptr_t addressOfVirtual(
R (T::*func)(Ps...) const, typename std::enable_if_t<std::is_abstract_v<T>>* = 0
) {
return 0;
}
template <typename R, typename T, typename... Ps> template <typename R, typename T, typename... Ps>
static intptr_t addressOfNonVirtual(R (T::*func)(Ps...) const) { static intptr_t addressOfNonVirtual(R (T::*func)(Ps...) const) {
return addressOfNonVirtual(reinterpret_cast<R (T::*)(Ps...)>(func)); return addressOfNonVirtual(reinterpret_cast<R (T::*)(Ps...)>(func));