{"id":13662597,"url":"https://github.com/Misaka-Mikoto-Tech/MonoHook","last_synced_at":"2025-04-25T10:32:33.885Z","repository":{"id":47026790,"uuid":"138474009","full_name":"Misaka-Mikoto-Tech/MonoHook","owner":"Misaka-Mikoto-Tech","description":"hook C# method at runtime without modify dll file (such as UnityEditor.dll), works on Windows, Mac, Android il2cpp(armv7a and armv8a)","archived":false,"fork":false,"pushed_at":"2023-09-22T09:16:32.000Z","size":638,"stargazers_count":914,"open_issues_count":7,"forks_count":178,"subscribers_count":32,"default_branch":"master","last_synced_at":"2024-04-13T23:34:59.146Z","etag":null,"topics":["android","dll","hook","il2cpp","jit","mac","unity","unityeditor"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Misaka-Mikoto-Tech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-06-24T10:49:32.000Z","updated_at":"2024-04-13T16:50:11.000Z","dependencies_parsed_at":"2024-01-12T04:03:29.415Z","dependency_job_id":null,"html_url":"https://github.com/Misaka-Mikoto-Tech/MonoHook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Misaka-Mikoto-Tech%2FMonoHook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Misaka-Mikoto-Tech%2FMonoHook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Misaka-Mikoto-Tech%2FMonoHook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Misaka-Mikoto-Tech%2FMonoHook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Misaka-Mikoto-Tech","download_url":"https://codeload.github.com/Misaka-Mikoto-Tech/MonoHook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223996817,"owners_count":17238375,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","dll","hook","il2cpp","jit","mac","unity","unityeditor"],"created_at":"2024-08-02T05:02:03.059Z","updated_at":"2024-11-10T18:30:57.075Z","avatar_url":"https://github.com/Misaka-Mikoto-Tech.png","language":"C#","funding_links":[],"categories":["C\\#","Open Source Repositories","C#"],"sub_categories":["Modding"],"readme":"# MonoHook\n本代码的功能是运行时修改C#函数\n## 特性：\n* 运行时直接修改内存中的 jit / aot 代码，不会修改真实文件。\n* 不影响调试及堆栈回溯。\n* 同时支持 .net 2.x 与 .net 4.x。\n* 支持 unity4.7.2, unity5.x, unity 2017 ~ 2021 (只要Unity不更换 runtime 就可以自动支持以后的版本)。\n* 使用方便，在C#内定义签名与原始方法相同的两个方法注册后即可生效。\n\n## 支持平台\n* Editor (Windows, Mac Intel/Silicon)\n* Android mono/il2cpp(armv7a, armv8a)\n* Windows mono/il2cpp(x86, x64)\n\n## 预览\n![image](Preview/Preview.gif)\n\n## 原理\n* MethodInfo.MethodHandle.GetFunctionPointer().ToPointer() 指向了 jit 后的 native 代码，因此修改此地址的代码即可以修改功能。\n* 通过一系列跳转就可以巧妙的替换原函数实现，同时也保留调用原函数的功能。\n* 本代码的实现与下面 reference 的实现略有不同，比其使用更少的字节，Hook 成功率更高，具体实现可以看代码。\n\n## 使用方法\n```CSharp\n   [InitializeOnLoad] // 最好Editor启动及重新编译完毕就执行\n   public static class HookTest\n   {\n        static HookTest()\n        {\n            if(_hook == null)\n            {\n                Type type = Type.GetType(\"UnityEditor.LogEntries,UnityEditor.dll\");\n                // 找到需要 Hook 的方法\n                MethodInfo miTarget = type.GetMethod(\"Clear\", BindingFlags.Static | BindingFlags.Public);\n\n                type = typeof(PinnedLog);\n\n                // 找到被替换成的新方法\n                MethodInfo miReplacement = type.GetMethod(\"NewClearLog\", BindingFlags.Static | BindingFlags.NonPublic);\n\n                // 这个方法是用来调用原始方法的, 要求必须添加 [MethodImpl(MethodImplOptions.NoOptimization)]，否则在 arm il2cpp 下会随机 crash\n                MethodInfo miProxy = type.GetMethod(\"ProxyClearLog\", BindingFlags.Static | BindingFlags.NonPublic);\n\n                // 创建一个 Hook 并 Install 就OK啦, 之后无论哪个代码再调用原始方法都会重定向到\n                //  我们写的方法ヾ(ﾟ∀ﾟゞ)\n                _hook = new MethodHook(miTarget, miReplacement, miProxy);\n                _hook.Install();\n            }\n        }\n   }\n    \n```\n\n## reference\n* https://github.com/bigbaldy1128/DotNetDetour\n* https://github.com/MonoMod/MonoMod.Common\n* https://github.com/mono/mono/blob/main/mono/mini/mini-arm64.c#L2038","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMisaka-Mikoto-Tech%2FMonoHook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMisaka-Mikoto-Tech%2FMonoHook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMisaka-Mikoto-Tech%2FMonoHook/lists"}