Add disable auto enable to Patch

This commit is contained in:
altalk23 2023-09-04 22:36:33 +03:00
parent 23c3095263
commit 69821f3e2c
4 changed files with 35 additions and 6 deletions

View file

@ -12,7 +12,7 @@ namespace geode {
class Mod; class Mod;
class Loader; class Loader;
class GEODE_DLL Hook { class GEODE_DLL Hook final {
private: private:
class Impl; class Impl;
std::shared_ptr<Impl> m_impl; std::shared_ptr<Impl> m_impl;
@ -143,20 +143,21 @@ namespace geode {
void setAutoEnable(bool autoEnable); void setAutoEnable(bool autoEnable);
}; };
class GEODE_DLL Patch { class GEODE_DLL Patch final {
protected: protected:
Mod* m_owner; Mod* m_owner;
void* m_address; void* m_address;
ByteVector m_original; ByteVector m_original;
ByteVector m_patch; ByteVector m_patch;
bool m_applied; bool m_applied;
bool m_autoEnable;
// Only allow friend classes to create // Only allow friend classes to create
// patches. Whatever method created the // patches. Whatever method created the
// patches should take care of populating // patches should take care of populating
// m_owner, m_address, m_original and // m_owner, m_address, m_original and
// m_patch. // m_patch.
Patch() : m_applied(false) {} Patch();
// no copying // no copying
Patch(Patch const&) = delete; Patch(Patch const&) = delete;
@ -198,5 +199,17 @@ namespace geode {
* @note For IPC * @note For IPC
*/ */
json::Value getRuntimeInfo() const; 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);
}; };
} }

View file

@ -351,10 +351,16 @@ Result<> Mod::Impl::loadBinary() {
} }
for (auto const& patch : m_patches) { for (auto const& patch : m_patches) {
if (!patch->apply()) { if (!patch) {
log::warn("Unable to apply patch at {}", patch->getAddress()); log::warn("Patch is null in mod \"{}\"", m_metadata.getName());
continue; continue;
} }
if (patch->getAutoEnable()) {
if (!patch->apply()) {
log::warn("Unable to apply patch at {}", patch->getAddress());
continue;
}
}
} }
m_enabled = true; m_enabled = true;

View file

@ -11,6 +11,16 @@ bool Patch::restore() {
return bool(tulip::hook::writeMemory(m_address, m_original.data(), m_original.size())); 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 <> template <>
struct json::Serialize<ByteVector> { struct json::Serialize<ByteVector> {
static json::Value to_json(ByteVector const& bytes) { static json::Value to_json(ByteVector const& bytes) {

View file

@ -57,7 +57,7 @@ namespace gd {
return std::string(*this) == std::string(other); 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<int, int>; template class map<int, int>;
template class map<gd::string, gd::string>; template class map<gd::string, gd::string>;
template class map<gd::string, bool>; template class map<gd::string, bool>;