-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.NET: Add support for hooking via attributes
- Loading branch information
Showing
6 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#include "MethodHook.hpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "../TDB.hpp" | ||
#include "../Method.hpp" | ||
#include "../MethodHook.hpp" | ||
|
||
namespace REFrameworkNET { | ||
ref class TypeDefinition; | ||
ref class Method; | ||
|
||
public enum class MethodHookType : uint8_t { | ||
Pre, | ||
Post | ||
}; | ||
} | ||
|
||
namespace REFrameworkNET::Attributes { | ||
/// <summary>Attribute to mark a method as a hook.</summary> | ||
[System::AttributeUsage(System::AttributeTargets::Method)] | ||
public ref class MethodHookAttribute : public System::Attribute { | ||
internal: | ||
void BaseConstructor(System::Type^ declaringType, System::String^ methodSignature, MethodHookType type) { | ||
if (declaringType == nullptr) { | ||
throw gcnew System::ArgumentNullException("declaringType"); | ||
} | ||
|
||
// new static readonly TypeDefinition REFType = TDB.Get().FindType("app.Collision.HitController.DamageInfo"); | ||
auto refTypeField = declaringType->GetField("REFType", System::Reflection::BindingFlags::Static | System::Reflection::BindingFlags::Public); | ||
|
||
if (refTypeField == nullptr) { | ||
throw gcnew System::ArgumentException("Type does not have a REFrameworkNET::TypeDefinition field"); | ||
} | ||
|
||
m_declaringType = (TypeDefinition^)refTypeField->GetValue(nullptr); | ||
|
||
if (m_declaringType == nullptr) { | ||
throw gcnew System::ArgumentException("Type does not have a REFrameworkNET::TypeDefinition field"); | ||
} | ||
|
||
m_method = m_declaringType != nullptr ? m_declaringType->GetMethod(methodSignature) : nullptr; | ||
m_hookType = type; | ||
} | ||
|
||
public: | ||
MethodHookAttribute(System::Type^ declaringType, System::String^ methodSignature, MethodHookType type) { | ||
BaseConstructor(declaringType, methodSignature, type); | ||
} | ||
|
||
MethodHookAttribute(System::Type^ declaringType, System::String^ methodSignature, MethodHookType type, bool skipJmp) { | ||
BaseConstructor(declaringType, methodSignature, type); | ||
m_skipJmp = skipJmp; | ||
} | ||
|
||
property bool Valid { | ||
public: | ||
bool get() { | ||
return m_declaringType != nullptr && m_method != nullptr; | ||
} | ||
} | ||
|
||
bool Install(System::Reflection::MethodInfo^ destination) { | ||
if (!Valid) { | ||
throw gcnew System::ArgumentException("Invalid method hook"); | ||
} | ||
|
||
if (!destination->IsStatic) { | ||
throw gcnew System::ArgumentException("Destination method must be static"); | ||
} | ||
|
||
auto hook = REFrameworkNET::MethodHook::Create(m_method, m_skipJmp); | ||
|
||
if (m_hookType == MethodHookType::Pre) { | ||
auto del = System::Delegate::CreateDelegate(REFrameworkNET::MethodHook::PreHookDelegate::typeid, destination); | ||
hook->AddPre((REFrameworkNET::MethodHook::PreHookDelegate^)del); | ||
} else if (m_hookType == MethodHookType::Post) { | ||
auto del = System::Delegate::CreateDelegate(REFrameworkNET::MethodHook::PostHookDelegate::typeid, destination); | ||
hook->AddPost((REFrameworkNET::MethodHook::PostHookDelegate^)del); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected: | ||
REFrameworkNET::TypeDefinition^ m_declaringType; | ||
REFrameworkNET::Method^ m_method; | ||
MethodHookType m_hookType; | ||
bool m_skipJmp{ false }; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters