geode/loader/include/Geode/modify/Modify.hpp

492 lines
26 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#pragma once
#include "AsStaticFunction.hpp"
2022-11-09 12:11:50 -05:00
#include "Field.hpp"
#include <Geode/Enums.hpp>
2022-12-12 10:42:56 -05:00
#include "IDManager.hpp"
2022-10-30 14:59:20 -04:00
2022-10-08 09:55:40 -04:00
#include <Geode/loader/Loader.hpp>
#include <Geode/loader/Mod.hpp>
2022-07-30 12:24:03 -04:00
#include <iostream>
2022-12-12 10:42:56 -05:00
#include <tulip/TulipHook.hpp>
2022-07-30 12:24:03 -04:00
#define GEODE_APPLY_MODIFY_FOR_FUNCTION(AddressInline_, Convention_, ClassName_, FunctionName_, ...) \
do { \
static auto constexpr different = Unique::different< \
Resolve<__VA_ARGS__>::func(&Base::FunctionName_), \
Resolve<__VA_ARGS__>::func(&Derived::FunctionName_) \
>(); \
using BaseFuncType = decltype(Resolve<__VA_ARGS__>::func(&Base::FunctionName_)); \
using DerivedFuncType = decltype(Resolve<__VA_ARGS__>::func(&Derived::FunctionName_)); \
if constexpr (different) { \
static auto address = AddressInline_; \
2024-06-03 16:21:17 -04:00
static_assert( \
!different || !std::is_same_v<typename ReturnType<BaseFuncType>::type, TodoReturn>, \
"Function" #ClassName_ "::" #FunctionName_ " has a TodoReturn type, " \
"please fix it by editing the bindings." \
); \
if (address == 0) { \
log::error( \
"Address of {} returned nullptr, can't hook", #ClassName_ "::" #FunctionName_ \
); \
break; \
} \
auto hook = Hook::create( \
reinterpret_cast<void*>(address), \
AsStaticFunction_##FunctionName_< \
Derived, \
DerivedFuncType>::value, \
#ClassName_ "::" #FunctionName_, \
tulip::hook::TulipConvention::Convention_ \
); \
this->m_hooks[#ClassName_ "::" #FunctionName_] = hook; \
} \
} while (0);
#define GEODE_APPLY_MODIFY_FOR_FUNCTION_ERROR(ClassName_, FunctionName_, ...) \
do { \
static_assert(!FunctionExists_##FunctionName_<Derived __VA_ARGS__>, \
"Function " #ClassName_ "::" #FunctionName_ " does not have an available address in the" \
2024-07-06 18:06:40 -04:00
" bindings, please add it to the bindings to hook it." \
); \
} while (0);
#define GEODE_APPLY_MODIFY_FOR_FUNCTION_ERROR_DEFINED(ClassName_, FunctionName_, ...) \
do { \
static auto constexpr different = Unique::different< \
Resolve<__VA_ARGS__>::func(&Base::FunctionName_), \
Resolve<__VA_ARGS__>::func(&Derived::FunctionName_) \
>(); \
static_assert(!different, \
"Function " #ClassName_ "::" #FunctionName_ " does not have an available address in the" \
" bindings, please add it to the bindings to hook it." \
); \
} while (0);
#define GEODE_APPLY_MODIFY_FOR_FUNCTION_ERROR_INLINE(ClassName_, FunctionName_, ...) \
do { \
static auto constexpr different = Unique::different< \
Resolve<__VA_ARGS__>::func(&Base::FunctionName_), \
Resolve<__VA_ARGS__>::func(&Derived::FunctionName_) \
>(); \
static_assert(!different, \
"Function " #ClassName_ "::" #FunctionName_ " cannot be hooked due to an inline definition" \
" existing for the function." \
); \
} while (0);
2023-12-31 16:39:34 -05:00
#define GEODE_APPLY_MODIFY_FOR_CONSTRUCTOR(AddressInline_, Convention_, ClassName_, ...) \
do { \
if constexpr (HasConstructor<Derived>) { \
static auto address = AddressInline_; \
auto hook = Hook::create( \
reinterpret_cast<void*>(address), \
AsStaticFunction_##constructor< \
Derived, \
decltype(Resolve<__VA_ARGS__>::func(&Derived::constructor))>::value, \
#ClassName_ "::" #ClassName_, \
tulip::hook::TulipConvention::Convention_ \
); \
this->m_hooks[#ClassName_ "::" #ClassName_] = hook; \
} \
} while (0);
2023-12-31 16:39:34 -05:00
#define GEODE_APPLY_MODIFY_FOR_DESTRUCTOR(AddressInline_, Convention_, ClassName_) \
do { \
if constexpr (HasDestructor<Derived>) { \
static auto address = AddressInline_; \
auto hook = Hook::create( \
reinterpret_cast<void*>(address), \
AsStaticFunction_##destructor<Derived, decltype(Resolve<>::func(&Derived::destructor))>::value, \
#ClassName_ "::" #ClassName_, \
tulip::hook::TulipConvention::Convention_ \
); \
this->m_hooks[#ClassName_ "::" #ClassName_] = hook; \
} \
2022-12-13 08:32:49 -05:00
} while (0);
2022-07-30 12:24:03 -04:00
namespace geode {
class Priority {
public:
static inline constexpr int32_t First = -3000;
static inline constexpr int32_t VeryEarly = -2000;
static inline constexpr int32_t Early = -1000;
static inline constexpr int32_t Normal = 0;
static inline constexpr int32_t Late = 1000;
static inline constexpr int32_t VeryLate = 2000;
static inline constexpr int32_t Last = 3000;
static inline constexpr int32_t FirstPre = First;
static inline constexpr int32_t VeryEarlyPre = VeryEarly;
static inline constexpr int32_t EarlyPre = Early;
static inline constexpr int32_t NormalPre = Normal;
static inline constexpr int32_t LatePre = Late;
static inline constexpr int32_t VeryLatePre = VeryLate;
static inline constexpr int32_t LastPre = Last;
static inline constexpr int32_t FirstPost = Last;
static inline constexpr int32_t VeryEarlyPost = VeryLate;
static inline constexpr int32_t EarlyPost = Late;
static inline constexpr int32_t NormalPost = Normal;
static inline constexpr int32_t LatePost = Early;
static inline constexpr int32_t VeryLatePost = VeryEarly;
static inline constexpr int32_t LastPost = First;
};
}
2022-07-30 12:24:03 -04:00
namespace geode::modifier {
2022-10-30 14:59:20 -04:00
template <class Derived, class Base>
2022-11-09 12:11:50 -05:00
class ModifyDerive;
2022-07-30 12:24:03 -04:00
2022-12-12 10:42:56 -05:00
template <class ModifyDerived>
2022-10-30 14:59:20 -04:00
class ModifyBase {
public:
2024-01-15 09:57:43 -05:00
std::map<std::string, std::shared_ptr<Hook>> m_hooks;
2022-12-12 10:42:56 -05:00
/// @brief Get a hook by name
/// @param name The name of the hook to get
/// @returns Ok if the hook was found, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<Hook*> getHook(std::string_view name) {
auto key = std::string(name);
if (m_hooks.find(key) == m_hooks.end()) {
2022-12-12 10:42:56 -05:00
return Err("Hook not in this modify");
}
2024-11-16 06:53:21 -05:00
return Ok(m_hooks[key].get());
2022-12-12 10:42:56 -05:00
}
/// @brief Set the priority of a hook
/// @param name The name of the hook to set the priority of
/// @param priority The priority to set the hook to
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriority(std::string_view name, int32_t priority = Priority::Normal) {
2024-11-11 14:43:04 -05:00
GEODE_UNWRAP_INTO(auto hook, this->getHook(name));
hook->setPriority(priority);
2023-01-24 11:52:29 -05:00
return Ok();
}
/// @brief Set the priority of a hook
/// @param name The name of the hook to set the priority of
/// @param priority The priority to set the hook to
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityPre(std::string_view name, int32_t priority = Priority::Normal) {
return this->setHookPriority(name, priority);
}
/// @brief Set the priority of a hook
/// @param name The name of the hook to set the priority of
/// @param priority The priority to set the hook to
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityPost(std::string_view name, int32_t priority = Priority::Normal) {
return this->setHookPriority(name, -priority);
}
/// @brief Set the priority of a hook to be after another hook in different mods
/// @param name The name of the hook to set the priority of
2024-11-16 06:53:21 -05:00
/// @param after The mod to set the priority after
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityAfter(std::string_view name, Mod* mod) {
2024-11-11 14:43:04 -05:00
GEODE_UNWRAP_INTO(auto hook, this->getHook(name));
2024-11-16 06:53:21 -05:00
auto func = [=](ModStateEvent* event){
auto hooks = mod->getHooks();
2024-11-16 06:53:21 -05:00
for (auto modHook : hooks) {
if (modHook->getAddress() != hook->getAddress()) continue;
auto priority = modHook->getPriority();
if (hook->getPriority() <= priority) {
hook->setPriority(priority + 1);
}
}
2024-11-16 06:53:21 -05:00
return ListenerResult::Propagate;
};
if (mod->isEnabled()) {
func(nullptr);
}
else {
new EventListener(func, ModStateFilter(mod, ModEventType::Loaded));
}
return Ok();
}
2024-11-16 06:53:21 -05:00
/// @brief Set the priority of a hook to be after another hook in different mods
/// @param name The name of the hook to set the priority of
/// @param after The mod id of the mod to set the priority after
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
Result<> setHookPriorityAfter(std::string_view name, std::string_view after) {
auto mod = Loader::get()->getInstalledMod(std::string(after));
if (!mod) return Err("Mod not found");
return this->setHookPriorityAfter(name, mod);
}
/// @brief Set the priority of a hook to be before another hook in different mods
/// @param name The name of the hook to set the priority of
2024-11-16 06:53:21 -05:00
/// @param before The mod to set the priority before
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityBefore(std::string_view name, Mod* mod) {
2024-11-11 14:43:04 -05:00
GEODE_UNWRAP_INTO(auto hook, this->getHook(name));
2024-11-16 06:53:21 -05:00
auto func = [=](ModStateEvent* event){
auto hooks = mod->getHooks();
2024-11-16 06:53:21 -05:00
for (auto modHook : hooks) {
if (modHook->getAddress() != hook->getAddress()) continue;
auto priority = modHook->getPriority();
if (hook->getPriority() >= priority) {
hook->setPriority(priority - 1);
}
}
2024-11-16 06:53:21 -05:00
return ListenerResult::Propagate;
};
if (mod->isEnabled()) {
func(nullptr);
}
else {
new EventListener(func, ModStateFilter(mod, ModEventType::Loaded));
}
return Ok();
}
2024-11-16 06:53:21 -05:00
/// @brief Set the priority of a hook to be before another hook in different mods
/// @param name The name of the hook to set the priority of
/// @param before The mod id of the mod to set the priority before
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
Result<> setHookPriorityBefore(std::string_view name, std::string_view before) {
auto mod = Loader::get()->getInstalledMod(std::string(before));
if (!mod) return Err("Mod not found");
return this->setHookPriorityBefore(name, mod);
}
/// @brief Set the priority of a hook to be after another hook in different mods
/// @param name The name of the hook to set the priority of
/// @param after The mod id of the mod to set the priority after
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
Result<> setHookPriorityAfterPre(std::string_view name, std::string_view after) {
return this->setHookPriorityAfter(name, after);
}
/// @brief Set the priority of a hook to be after another hook in different mods
/// @param name The name of the hook to set the priority of
2024-11-16 06:53:21 -05:00
/// @param before The mod to set the priority after
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityAfterPre(std::string_view name, Mod* mod) {
return this->setHookPriorityAfter(name, mod);
}
/// @brief Set the priority of a hook to be before another hook in different mods
/// @param name The name of the hook to set the priority of
2024-11-16 06:53:21 -05:00
/// @param before The mod id of the mod to set the priority before
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
Result<> setHookPriorityBeforePre(std::string_view name, std::string_view before) {
2024-11-16 07:04:25 -05:00
return this->setHookPriorityBefore(name, before);
2024-11-16 06:53:21 -05:00
}
/// @brief Set the priority of a hook to be before another hook in different mods
/// @param name The name of the hook to set the priority of
/// @param before The mod to set the priority before
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
Result<> setHookPriorityBeforePre(std::string_view name, Mod* mod) {
return this->setHookPriorityBefore(name, mod);
}
/// @brief Set the priority of a hook to be after another hook in different mods
/// @param name The name of the hook to set the priority of
/// @param after The mod id of the mod to set the priority after
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityAfterPost(std::string_view name, std::string_view after) {
return this->setHookPriorityBefore(name, after);
}
/// @brief Set the priority of a hook to be after another hook in different mods
/// @param name The name of the hook to set the priority of
2024-11-16 06:53:21 -05:00
/// @param before The mod to set the priority after
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
Result<> setHookPriorityAfterPost(std::string_view name, Mod* mod) {
return this->setHookPriorityBefore(name, mod);
}
/// @brief Set the priority of a hook to be before another hook in different mods
/// @param name The name of the hook to set the priority of
/// @param before The mod id of the mod to set the priority before
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityBeforePost(std::string_view name, std::string_view before) {
return this->setHookPriorityAfter(name, before);
}
/// @brief Set the priority of a hook to be before another hook in different mods
/// @param name The name of the hook to set the priority of
2024-11-16 06:53:21 -05:00
/// @param before The mod to set the priority before
/// @returns Ok if the hook was found and the priority was set, Err if the hook was not found
2024-11-16 06:53:21 -05:00
Result<> setHookPriorityBeforePost(std::string_view name, Mod* mod) {
return this->setHookPriorityAfter(name, mod);
}
2022-10-30 14:59:20 -04:00
// unordered_map<handles> idea
ModifyBase() {
2024-06-20 13:09:58 -04:00
struct EboCheck : ModifyDerived::Base {
2024-06-20 13:14:32 -04:00
std::aligned_storage_t<
std::alignment_of_v<typename ModifyDerived::Base>,
std::alignment_of_v<typename ModifyDerived::Base>
> m_padding;
2024-06-20 13:09:58 -04:00
};
static constexpr auto baseSize = sizeof(typename ModifyDerived::Base);
static constexpr auto derivedSize = sizeof(typename ModifyDerived::Derived);
static constexpr auto alignment = std::alignment_of_v<typename ModifyDerived::Base>;
static constexpr bool hasEbo = sizeof(EboCheck) == sizeof(typename ModifyDerived::Base);
static constexpr bool hasImproperCustomFields = hasEbo ? derivedSize != baseSize : derivedSize != baseSize + alignment;
static_assert(!hasImproperCustomFields,
"\n--- Error in modify class:\n"
" Do not add members to a modify class, use `struct Fields` instead.\n"
" See https://docs.geode-sdk.org/tutorials/fields for more info."
"\n---"
);
2022-12-13 08:32:49 -05:00
// i really dont want to recompile codegen
auto test = static_cast<ModifyDerived*>(this);
test->ModifyDerived::apply();
2022-12-13 05:30:34 -05:00
ModifyDerived::Derived::onModify(*this);
2024-01-15 09:57:43 -05:00
std::vector<std::string> added;
2022-12-13 05:30:34 -05:00
for (auto& [uuid, hook] : m_hooks) {
auto res = Mod::get()->claimHook(hook);
2022-12-13 05:30:34 -05:00
if (!res) {
2024-11-04 15:24:20 -05:00
log::error("Failed to claim hook {}: {}", hook->getDisplayName(), res.unwrapErr());
2022-12-12 10:42:56 -05:00
}
2024-01-15 09:57:43 -05:00
else {
added.push_back(uuid);
}
}
for (auto& uuid : added) {
m_hooks.erase(uuid);
2022-12-13 05:30:34 -05:00
}
2022-10-30 14:59:20 -04:00
}
2022-12-12 10:42:56 -05:00
virtual void apply() {}
2022-10-30 14:59:20 -04:00
template <class, class>
2022-11-09 12:11:50 -05:00
friend class ModifyDerive;
2022-10-30 14:59:20 -04:00
// explicit Modify(Property property) idea
};
2022-07-30 12:24:03 -04:00
2022-10-30 14:59:20 -04:00
template <class Derived, class Base>
2022-11-09 12:11:50 -05:00
class ModifyDerive {
2022-10-30 14:59:20 -04:00
public:
2022-11-09 12:11:50 -05:00
ModifyDerive() {
static_assert(
alwaysFalse<Derived>,
"Modified class not recognized, please include <Geode/modify/ClassName.hpp> to be "
"able to use it."
);
2022-10-30 14:59:20 -04:00
}
};
2022-07-30 12:24:03 -04:00
}
2022-11-09 12:11:50 -05:00
namespace geode {
// The intellisense compiler is quite dumb, and will very often error on modify classes
// with an error of "incomplete type is not allowed", despite not being an issue in actual compilation.
// So as a workaround use the compiler defined "__INTELLISENSE__" macro, which gets set to 1 on the intellisense pass.
// See https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170#microsoft-specific-predefined-macros
#if __INTELLISENSE__ != 1 && !defined(__CLION_IDE__)
2022-11-09 12:11:50 -05:00
template <class Derived, class Base>
class Modify : public Base {
private:
static inline modifier::ModifyDerive<Derived, Base> s_apply;
// because for some reason we need it
static inline auto s_applyRef = &Modify::s_apply;
public:
// abusing the internal stuff
// basically we dont want modify to invoke base ctors and dtors
// we already have utilities for these, which are ccdestructor
2023-03-06 13:24:38 -05:00
// and the cutoff constructor
Modify() : Base(CutoffConstructor, sizeof(Base)) {}
~Modify() {
cocos2d::CCDestructor::lock(this) = true;
}
Modify(Modify const&) = delete;
Modify(Modify&&) = delete;
Modify& operator=(Modify const&) = delete;
Modify& operator=(Modify&&) = delete;
2022-11-09 12:11:50 -05:00
modifier::FieldIntermediate<Derived, Base> m_fields;
2022-12-12 10:42:56 -05:00
static void onModify(auto& self) {}
2022-11-09 12:11:50 -05:00
};
#else
template <class Derived, class Base>
class Modify : public Base {
public:
modifier::FieldIntermediate<Derived, Base> m_fields;
};
#endif
2022-11-09 12:11:50 -05:00
}
2023-02-08 08:42:34 -05:00
/**
* Main class implementation, it has the structure
*
* class hook0Dummy;
* template<typename>
* struct hook0 {};
* namespace {
* struct hook0Parent {};
* }
* template<>
* struct GEODE_HIDDEN hook0<hook0Parent> : Modify<hook0<hook0Parent>, MenuLayer> {
* // code stuff idk
* };
*
* I tried to make the macro as verbose as it can be but
* I am bad at this stuff
*/
#if __INTELLISENSE__ != 1 && !defined(__CLION_IDE__)
2023-02-08 08:42:34 -05:00
#define GEODE_MODIFY_DECLARE_ANONYMOUS(base, derived) \
derived##Dummy; \
template <class> \
struct derived {}; \
namespace { \
struct derived##Parent {}; \
} \
template <> \
struct GEODE_HIDDEN derived<derived##Parent> : geode::Modify<derived<derived##Parent>, base>
#define GEODE_MODIFY_DECLARE(base, derived) \
derived##Dummy; \
struct GEODE_HIDDEN derived : geode::Modify<derived, base>
#else
// Simplify the modify macro for intellisense, to hopefully help perfomance a bit
#define GEODE_MODIFY_DECLARE(base, derived) \
derived##Dummy; \
struct derived : geode::Modify<derived, base>
#define GEODE_MODIFY_DECLARE_ANONYMOUS(base, derived) GEODE_MODIFY_DECLARE(base, derived)
#endif
2023-02-08 08:42:34 -05:00
#define GEODE_MODIFY_REDIRECT4(base, derived) GEODE_MODIFY_DECLARE(base, derived)
#define GEODE_MODIFY_REDIRECT3(base, derived) GEODE_MODIFY_DECLARE_ANONYMOUS(base, derived)
#define GEODE_MODIFY_REDIRECT2(base) GEODE_MODIFY_REDIRECT3(base, GEODE_CONCAT(hook, __LINE__))
#define GEODE_MODIFY_REDIRECT1(base) GEODE_MODIFY_REDIRECT2(base)
/**
* Interfaces for the class implementation
*
* class $modify(MenuLayer) {};
* class $modify(MyMenuLayerInterface, MenuLayer) {};
*/
#define GEODE_CRTP1(base) GEODE_MODIFY_REDIRECT1(base)
#define GEODE_CRTP2(derived, base) GEODE_MODIFY_REDIRECT4(base, derived)
#define $modify(...) \
GEODE_INVOKE(GEODE_CONCAT(GEODE_CRTP, GEODE_NUMBER_OF_ARGS(__VA_ARGS__)), __VA_ARGS__)
2024-02-07 14:14:09 -05:00
/**
* This function is meant to hook / override a GD function in a Modified class.
* **This is merely an annotation for clarity** - while there may be linters that
* check for it, it is not required
*/
#define $override