From af1753900dd4031097f996f2b995fe1a2918fee2 Mon Sep 17 00:00:00 2001 From: bbbirder <502100554@qq.com> Date: Wed, 13 Dec 2023 11:49:59 +0800 Subject: [PATCH 1/3] fix: add log on external target method --- Editor/InjectHelper.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Editor/InjectHelper.cs b/Editor/InjectHelper.cs index 0b178b0..e83e326 100644 --- a/Editor/InjectHelper.cs +++ b/Editor/InjectHelper.cs @@ -222,6 +222,10 @@ static void AddInjectionMethod( var ReturnType = targetMethod.ReturnType; //redirect method + if (!targetMethod.HasBody) + { + throw new ArgumentException($"method {targetMethod.Name} in type {targetType.Name} dont have a body"); + } targetMethod.Body.Instructions.Clear(); var delegateType = delegateField.FieldType.Resolve(); var ilProcessor = targetMethod.Body.GetILProcessor(); From 68b9754f0cbc98bef555064ecbf10c9de4a988fe Mon Sep 17 00:00:00 2001 From: bbbirder <502100554@qq.com> Date: Wed, 13 Dec 2023 11:50:40 +0800 Subject: [PATCH 2/3] fix: update sample project --- Samples~/Demo.cs | 42 --------- Samples~/MethodFix/Demo.cs | 88 +++++++++++++++++++ Samples~/{ => MethodFix}/Demo.cs.meta | 0 Samples~/{ => MethodFix}/Demo.unity | 40 +++++---- Samples~/{ => MethodFix}/Demo.unity.meta | 0 Samples~/{ => MethodFix}/DemoModal.cs | 1 - Samples~/{ => MethodFix}/DemoModal.cs.meta | 0 .../com.bbbirder.injection.sample.asmdef | 0 .../com.bbbirder.injection.sample.asmdef.meta | 0 package.json | 6 +- 10 files changed, 112 insertions(+), 65 deletions(-) delete mode 100644 Samples~/Demo.cs create mode 100644 Samples~/MethodFix/Demo.cs rename Samples~/{ => MethodFix}/Demo.cs.meta (100%) rename Samples~/{ => MethodFix}/Demo.unity (95%) rename Samples~/{ => MethodFix}/Demo.unity.meta (100%) rename Samples~/{ => MethodFix}/DemoModal.cs (91%) rename Samples~/{ => MethodFix}/DemoModal.cs.meta (100%) rename Samples~/{ => MethodFix}/com.bbbirder.injection.sample.asmdef (100%) rename Samples~/{ => MethodFix}/com.bbbirder.injection.sample.asmdef.meta (100%) diff --git a/Samples~/Demo.cs b/Samples~/Demo.cs deleted file mode 100644 index 1ae65a0..0000000 --- a/Samples~/Demo.cs +++ /dev/null @@ -1,42 +0,0 @@ -using com.bbbirder.injection; -using UnityEngine; -using System; -using UnityEngine.Assertions; - -public class Demo:MonoBehaviour{ - void Start(){ - FixHelper.Install(); - } - void Update(){ - Debug.Log(DemoModal.Salute()); - Debug.Log(new DemoModal().ThisSalute()); - Debug.Log(new DemoModal().Add(4,1.2f)); - } - - #region Fix - static Func RawSalute; - [Fixer(typeof(DemoModal),nameof(DemoModal.Salute),nameof(RawSalute))] - static int Salute(){ - return 2; - } - - static Func RawThisSalute; - [Fixer(typeof(DemoModal),nameof(DemoModal.ThisSalute),nameof(RawThisSalute))] - static int ThisSalute(DemoModal demo){ - return 2; - } - - static Func RawAdd; - [Fixer(typeof(DemoModal),nameof(DemoModal.Add),nameof(RawAdd))] - static float Add(DemoModal demo,int i,float f){ - Debug.Log($"Add {i} + {f} is {i+f}"); - return i+f; - } - - static Action RawLog; - [Fixer(typeof(Debug),nameof(Debug.Log),nameof(RawLog))] - static void Log(object msg){ - RawLog.Invoke("msg:"+msg); - } - #endregion -} \ No newline at end of file diff --git a/Samples~/MethodFix/Demo.cs b/Samples~/MethodFix/Demo.cs new file mode 100644 index 0000000..6a3f7bd --- /dev/null +++ b/Samples~/MethodFix/Demo.cs @@ -0,0 +1,88 @@ +using com.bbbirder.injection; +using UnityEngine; +using System; +using UnityEngine.Assertions; +using System.Reflection; +using System.Collections.Generic; + +public class Demo : MonoBehaviour +{ + void Start() + { + // FixHelper.InstallAll(); + print("press SPACE to replace methods"); + } + + void Update() + { + Debug.Log(DemoModal.Salute()); + Debug.Log(new DemoModal().ThisSalute()); + Debug.Log(new DemoModal().Add(4, 1.2f)); + if (Input.GetKeyDown(KeyCode.Space)) + { + FixHelper.InstallAll(); + } + } + + #region Fix + static Func RawSalute; + static int Salute() + { + return 2; + } + + static Func RawThisSalute; + static int ThisSalute(DemoModal demo) + { + return 2; + } + + static Func RawAdd; + static float Add(DemoModal demo, int i, float f) + { + Debug.Log($"Add {i} + {f} is {i + f}"); + return i + f; + } + + static Action RawLog; + [HideInCallstack] + static void Log(object msg) + { + RawLog?.Invoke("msg:" + msg); + } + + internal class MethodReplacer : IInjection + { + static BindingFlags bindingFlags = 0 + | BindingFlags.Public + | BindingFlags.NonPublic + | BindingFlags.Static + ; + public IEnumerable ProvideInjections() + { + yield return InjectionInfo.Create>( + DemoModal.Salute, + Salute, + f => RawSalute = f + ); + yield return InjectionInfo.Create( + typeof(DemoModal).GetMethod(nameof(DemoModal.ThisSalute)), + typeof(Demo).GetMethod(nameof(Demo.ThisSalute), bindingFlags), + f => RawThisSalute = (Func)f + ); + yield return InjectionInfo.Create( + typeof(DemoModal).GetMethod(nameof(DemoModal.Add)), + typeof(Demo).GetMethod(nameof(Demo.Add), bindingFlags), + f => RawAdd = (Func)f + ); + yield return InjectionInfo.Create>( + Debug.Log, + Log, + f => RawLog = f + ); + } + + } + #endregion + +} \ No newline at end of file diff --git a/Samples~/Demo.cs.meta b/Samples~/MethodFix/Demo.cs.meta similarity index 100% rename from Samples~/Demo.cs.meta rename to Samples~/MethodFix/Demo.cs.meta diff --git a/Samples~/Demo.unity b/Samples~/MethodFix/Demo.unity similarity index 95% rename from Samples~/Demo.unity rename to Samples~/MethodFix/Demo.unity index e8b6a24..74936cf 100644 --- a/Samples~/Demo.unity +++ b/Samples~/MethodFix/Demo.unity @@ -104,7 +104,7 @@ NavMeshSettings: serializedVersion: 2 m_ObjectHideFlags: 0 m_BuildSettings: - serializedVersion: 2 + serializedVersion: 3 agentTypeID: 0 agentRadius: 0.5 agentHeight: 2 @@ -117,7 +117,7 @@ NavMeshSettings: cellSize: 0.16666667 manualTileSize: 0 tileSize: 256 - accuratePlacement: 0 + buildHeightMesh: 0 maxJobWorkers: 0 preserveTilesOutsideBounds: 0 debug: @@ -133,7 +133,6 @@ GameObject: m_Component: - component: {fileID: 1003345722} - component: {fileID: 1003345721} - - component: {fileID: 1003345723} m_Layer: 0 m_Name: Directional Light m_TagString: Untagged @@ -210,26 +209,14 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1003345720} + serializedVersion: 2 m_LocalRotation: {x: 0.40821788, y: -0.23456968, z: 0.10938163, w: 0.8754261} m_LocalPosition: {x: 0, y: 3, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} ---- !u!114 &1003345723 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1003345720} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 474bcb49853aa07438625e644c072ee6, type: 3} - m_Name: - m_EditorClassIdentifier: --- !u!1 &1968521127 GameObject: m_ObjectHideFlags: 0 @@ -270,9 +257,17 @@ Camera: m_projectionMatrixMode: 1 m_GateFitMode: 2 m_FOVAxisMode: 0 + m_Iso: 200 + m_ShutterSpeed: 0.005 + m_Aperture: 16 + m_FocusDistance: 10 + m_FocalLength: 50 + m_BladeCount: 5 + m_Curvature: {x: 2, y: 11} + m_BarrelClipping: 0.25 + m_Anamorphism: 0 m_SensorSize: {x: 36, y: 24} m_LensShift: {x: 0, y: 0} - m_FocalLength: 50 m_NormalizedViewPortRect: serializedVersion: 2 x: 0 @@ -306,13 +301,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1968521127} + serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 1, z: -10} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1993107288 GameObject: @@ -338,13 +333,13 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1993107288} + serializedVersion: 2 m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: -0.36873913, y: -0.39514256, z: 1.0795082} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &1993107291 MonoBehaviour: @@ -358,3 +353,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2c807aa2ed3eacf498e4b8cf0fed1c04, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!1660057539 &9223372036854775807 +SceneRoots: + m_ObjectHideFlags: 0 + m_Roots: + - {fileID: 1968521130} + - {fileID: 1003345722} + - {fileID: 1993107290} diff --git a/Samples~/Demo.unity.meta b/Samples~/MethodFix/Demo.unity.meta similarity index 100% rename from Samples~/Demo.unity.meta rename to Samples~/MethodFix/Demo.unity.meta diff --git a/Samples~/DemoModal.cs b/Samples~/MethodFix/DemoModal.cs similarity index 91% rename from Samples~/DemoModal.cs rename to Samples~/MethodFix/DemoModal.cs index 6dcaf7e..8837cf2 100644 --- a/Samples~/DemoModal.cs +++ b/Samples~/MethodFix/DemoModal.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using UnityEngine; using System; -using com.bbbirder.unity; public class DemoModal{ public static int Salute(){ diff --git a/Samples~/DemoModal.cs.meta b/Samples~/MethodFix/DemoModal.cs.meta similarity index 100% rename from Samples~/DemoModal.cs.meta rename to Samples~/MethodFix/DemoModal.cs.meta diff --git a/Samples~/com.bbbirder.injection.sample.asmdef b/Samples~/MethodFix/com.bbbirder.injection.sample.asmdef similarity index 100% rename from Samples~/com.bbbirder.injection.sample.asmdef rename to Samples~/MethodFix/com.bbbirder.injection.sample.asmdef diff --git a/Samples~/com.bbbirder.injection.sample.asmdef.meta b/Samples~/MethodFix/com.bbbirder.injection.sample.asmdef.meta similarity index 100% rename from Samples~/com.bbbirder.injection.sample.asmdef.meta rename to Samples~/MethodFix/com.bbbirder.injection.sample.asmdef.meta diff --git a/package.json b/package.json index 6d8decf..ac70a50 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.bbbirder.injection", "displayName": "Unity Injection", "description": "Unity注入模块,可以运行时改变被注入函数实现。", - "version": "1.3.19", + "version": "1.3.21", "hideInEditor": false, "author": "bbbirder <502100554@qq.com>", "dependencies": { @@ -11,8 +11,8 @@ }, "samples": [ { - "displayName": "DefaultSample", - "path": "Samples~/" + "displayName": "Method Fix", + "path": "Samples~/MethodFix" } ], "gitHead": "18b3c53f1dec43ebd854d02b30959cf96962da13" From 40b19b743bc43010cf37f3cae3f26817adff4435 Mon Sep 17 00:00:00 2001 From: bbbirder <502100554@qq.com> Date: Wed, 13 Dec 2023 12:21:23 +0800 Subject: [PATCH 3/3] docs: update ReadMe --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1bbd18b..d263d6f 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ using com.bbbirder.injection; using UnityEngine; // this illustration shows how to hook `Debug.Log` -public class FirstPatch:IInjection +public class FirstPatch { // the field to be overwrited to original method static Action RawLog; @@ -86,11 +86,9 @@ public class FirstPatch:IInjection ``` -`FixerAttribute`接收3个参数: +自定类继承自`IInjection`,并实现接口 - 1. 目标类型 - 2. 目标方法名称 - 3. 用于保存原方法的函数成员名称 +`ProvideInjections`中返回一个或多个`InjectionInfo` 初始化的时候调用: