Giter Site home page Giter Site logo

eerfree / monohook Goto Github PK

View Code? Open in Web Editor NEW

This project forked from misaka-mikoto-tech/monohook

0.0 0.0 0.0 638 KB

hook C# method at runtime without modify dll file (such as UnityEditor.dll), works on Windows, Mac, Android il2cpp(armv7a and armv8a)

License: MIT License

C++ 0.32% C# 99.68%

monohook's Introduction

MonoHook

本代码的功能是运行时修改C#函数

特性:

  • 运行时直接修改内存中的 jit / aot 代码,不会修改真实文件。
  • 不影响调试及堆栈回溯。
  • 同时支持 .net 2.x 与 .net 4.x。
  • 支持 unity4.7.2, unity5.x, unity 2017 ~ 2021 (只要Unity不更换 runtime 就可以自动支持以后的版本)。
  • 使用方便,在C#内定义签名与原始方法相同的两个方法注册后即可生效。

支持平台

  • Editor (Windows, Mac Intel/Silicon)
  • Android mono/il2cpp(armv7a, armv8a)
  • Windows mono/il2cpp(x86, x64)

预览

image

原理

  • MethodInfo.MethodHandle.GetFunctionPointer().ToPointer() 指向了 jit 后的 native 代码,因此修改此地址的代码即可以修改功能。
  • 通过一系列跳转就可以巧妙的替换原函数实现,同时也保留调用原函数的功能。
  • 本代码的实现与下面 reference 的实现略有不同,比其使用更少的字节,Hook 成功率更高,具体实现可以看代码。

使用方法

   [InitializeOnLoad] // 最好Editor启动及重新编译完毕就执行
   public static class HookTest
   {
        static HookTest()
        {
            if(_hook == null)
            {
                Type type = Type.GetType("UnityEditor.LogEntries,UnityEditor.dll");
                // 找到需要 Hook 的方法
                MethodInfo miTarget = type.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public);

                type = typeof(PinnedLog);

                // 找到被替换成的新方法
                MethodInfo miReplacement = type.GetMethod("NewClearLog", BindingFlags.Static | BindingFlags.NonPublic);

                // 这个方法是用来调用原始方法的, 要求必须添加 [MethodImpl(MethodImplOptions.NoOptimization)],否则在 arm il2cpp 下会随机 crash
                MethodInfo miProxy = type.GetMethod("ProxyClearLog", BindingFlags.Static | BindingFlags.NonPublic);

                // 创建一个 Hook 并 Install 就OK啦, 之后无论哪个代码再调用原始方法都会重定向到
                //  我们写的方法ヾ(゚∀゚ゞ)
                _hook = new MethodHook(miTarget, miReplacement, miProxy);
                _hook.Install();
            }
        }
   }
    

reference

monohook's People

Contributors

misaka-mikoto-tech avatar newbiegamecoder avatar friuns2 avatar saneor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.