From 69821f3e2c1b328b03e678173b5a1f47a8286299 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Mon, 4 Sep 2023 22:36:33 +0300 Subject: [PATCH] Add disable auto enable to Patch --- loader/include/Geode/loader/Hook.hpp | 19 ++++++++++++++++--- loader/src/loader/ModImpl.cpp | 10 ++++++++-- loader/src/loader/Patch.cpp | 10 ++++++++++ loader/src/platform/mac/gdstdlib.cpp | 2 +- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/loader/include/Geode/loader/Hook.hpp b/loader/include/Geode/loader/Hook.hpp index 51ffdf00..c4457f5c 100644 --- a/loader/include/Geode/loader/Hook.hpp +++ b/loader/include/Geode/loader/Hook.hpp @@ -12,7 +12,7 @@ namespace geode { class Mod; class Loader; - class GEODE_DLL Hook { + class GEODE_DLL Hook final { private: class Impl; std::shared_ptr m_impl; @@ -143,20 +143,21 @@ namespace geode { void setAutoEnable(bool autoEnable); }; - class GEODE_DLL Patch { + class GEODE_DLL Patch final { protected: Mod* m_owner; void* m_address; ByteVector m_original; ByteVector m_patch; bool m_applied; + bool m_autoEnable; // Only allow friend classes to create // patches. Whatever method created the // patches should take care of populating // m_owner, m_address, m_original and // m_patch. - Patch() : m_applied(false) {} + Patch(); // no copying Patch(Patch const&) = delete; @@ -198,5 +199,17 @@ namespace geode { * @note For IPC */ json::Value getRuntimeInfo() const; + + /** + * Get whether the patch should be auto enabled or not. + * @returns Auto enable + */ + bool getAutoEnable() const; + + /** + * Set whether the patch should be auto enabled or not. + * @param autoEnable Auto enable + */ + void setAutoEnable(bool autoEnable); }; } diff --git a/loader/src/loader/ModImpl.cpp b/loader/src/loader/ModImpl.cpp index 0030b3c3..dc1c838a 100644 --- a/loader/src/loader/ModImpl.cpp +++ b/loader/src/loader/ModImpl.cpp @@ -351,10 +351,16 @@ Result<> Mod::Impl::loadBinary() { } for (auto const& patch : m_patches) { - if (!patch->apply()) { - log::warn("Unable to apply patch at {}", patch->getAddress()); + if (!patch) { + log::warn("Patch is null in mod \"{}\"", m_metadata.getName()); continue; } + if (patch->getAutoEnable()) { + if (!patch->apply()) { + log::warn("Unable to apply patch at {}", patch->getAddress()); + continue; + } + } } m_enabled = true; diff --git a/loader/src/loader/Patch.cpp b/loader/src/loader/Patch.cpp index c7a71c1f..37b821df 100644 --- a/loader/src/loader/Patch.cpp +++ b/loader/src/loader/Patch.cpp @@ -11,6 +11,16 @@ bool Patch::restore() { return bool(tulip::hook::writeMemory(m_address, m_original.data(), m_original.size())); } +Patch::Patch() : m_owner(nullptr), m_address(nullptr), m_applied(false), m_autoEnable(true) {} + +void Patch::setAutoEnable(bool autoEnable) { + m_autoEnable = autoEnable; +} + +bool Patch::getAutoEnable() const { + return m_autoEnable; +} + template <> struct json::Serialize { static json::Value to_json(ByteVector const& bytes) { diff --git a/loader/src/platform/mac/gdstdlib.cpp b/loader/src/platform/mac/gdstdlib.cpp index 0c508a74..ce6ff4fa 100644 --- a/loader/src/platform/mac/gdstdlib.cpp +++ b/loader/src/platform/mac/gdstdlib.cpp @@ -57,7 +57,7 @@ namespace gd { return std::string(*this) == std::string(other); } - // TODO: these need to stay for old mods linking against geode <1.2.0 + // TODO: these need to stay for old mods linking against geode <1.2.0, remove in 2.0.0 template class map; template class map; template class map;