Skip to content

Commit

Permalink
fix: update sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
labbbirder committed Dec 13, 2023
1 parent af17539 commit 68b9754
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 65 deletions.
42 changes: 0 additions & 42 deletions Samples~/Demo.cs

This file was deleted.

88 changes: 88 additions & 0 deletions Samples~/MethodFix/Demo.cs
Original file line number Diff line number Diff line change
@@ -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<int> RawSalute;
static int Salute()
{
return 2;
}

static Func<DemoModal, int> RawThisSalute;
static int ThisSalute(DemoModal demo)
{
return 2;
}

static Func<DemoModal, int, float, float> RawAdd;
static float Add(DemoModal demo, int i, float f)
{
Debug.Log($"Add {i} + {f} is {i + f}");
return i + f;
}

static Action<object> 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<InjectionInfo> ProvideInjections()
{
yield return InjectionInfo.Create<Func<int>>(
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<DemoModal, int>)f
);
yield return InjectionInfo.Create(
typeof(DemoModal).GetMethod(nameof(DemoModal.Add)),
typeof(Demo).GetMethod(nameof(Demo.Add), bindingFlags),
f => RawAdd = (Func<DemoModal, int, float, float>)f
);
yield return InjectionInfo.Create<Action<object>>(
Debug.Log,
Log,
f => RawLog = f
);
}

}
#endregion

}
File renamed without changes.
40 changes: 21 additions & 19 deletions Samples~/Demo.unity → Samples~/MethodFix/Demo.unity
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
Expand All @@ -117,7 +117,7 @@ NavMeshSettings:
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
accuratePlacement: 0
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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}
File renamed without changes.
1 change: 0 additions & 1 deletion Samples~/DemoModal.cs → Samples~/MethodFix/DemoModal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using UnityEngine;
using System;
using com.bbbirder.unity;

public class DemoModal{
public static int Salute(){
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -11,8 +11,8 @@
},
"samples": [
{
"displayName": "DefaultSample",
"path": "Samples~/"
"displayName": "Method Fix",
"path": "Samples~/MethodFix"
}
],
"gitHead": "18b3c53f1dec43ebd854d02b30959cf96962da13"
Expand Down

0 comments on commit 68b9754

Please sign in to comment.