Skip to content

Commit

Permalink
chore: rename ProxyData
Browse files Browse the repository at this point in the history
  • Loading branch information
labbbirder committed Aug 24, 2023
1 parent 0eef044 commit 844216a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 77 deletions.
4 changes: 3 additions & 1 deletion Editor/UnityInjectUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
namespace com.bbbirder.unityeditor {
public static class UnityInjectUtils{
const string BACKUP_EXT = ".backup";
public static bool IsEditorBusy=>EditorApplication.isCompiling||EditorApplication.isUpdating;

[MenuItem("Tools/bbbirder/Inject for Editor")]
static void Inject(){
Expand Down Expand Up @@ -65,6 +64,9 @@ public static class UnityInjectUtils{
};
EditorApplication.delayCall += InjectEditorDelayed;
void InjectEditorDelayed(){
var IsEditorBusy = false;
IsEditorBusy |= EditorApplication.isCompiling;
IsEditorBusy |= EditorApplication.isUpdating;
if(IsEditorBusy){
EditorApplication.delayCall -= InjectEditorDelayed;
EditorApplication.delayCall += InjectEditorDelayed;
Expand Down
74 changes: 0 additions & 74 deletions Runtime/IProxyData.cs

This file was deleted.

92 changes: 92 additions & 0 deletions Runtime/ProxyData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Reflection;
using System.Collections.Generic;
using System;
using UnityEngine;

namespace com.bbbirder.unity
{
[InheritRetrieve]
public interface IProxyData : IInjection
{

}
public abstract class ProxyData : IProxyData
{
static readonly BindingFlags bindingFlags = 0
| BindingFlags.Instance
| BindingFlags.NonPublic
| BindingFlags.Public
| BindingFlags.DeclaredOnly
;
protected Dictionary<string, Delegate> getters = new();
protected Dictionary<string, Delegate> setters = new();
public Action<string> OnSetProperty { get; set; }
public Action<string> OnGetProperty { get; set; }
Func<C, T> ProxyGet<C, T>(string name) where C : ProxyData
{
return o =>
{
// Debug.Log("get " + name);
o.OnGetProperty?.Invoke(name);
var getter = o.getters[name];
var method = getter as Func<C, T>;
return method.Invoke(o);
};
}
Action<C, T> ProxySet<C, T>(string name) where C : ProxyData
{
return (o, v) =>
{
var setter = o.setters[name];
var method = setter as Action<C, T>;
method.Invoke(o, v);
// Debug.Log("set " + name + "=" + v);
o.OnSetProperty?.Invoke(name);
};
}
public IEnumerable<InjectionInfo> ProvideInjections()
{
var targetType = this.GetType();
if (targetType.IsAbstract || targetType.IsInterface)
yield break;

var proxyGet = typeof(ProxyData).GetMethod(nameof(ProxyGet), bindingFlags);
var proxySet = typeof(ProxyData).GetMethod(nameof(ProxySet), bindingFlags);
var instMethod = default(MethodInfo);
var nameArgs = new string[1];
foreach (var property in targetType.GetProperties(bindingFlags))
{
var name = property.Name;
nameArgs[0] = name;

instMethod = proxyGet.MakeGenericMethod(targetType, property.PropertyType);
if (property.GetMethod != null)
{
yield return new()
{
InjectedMethod = property.GetMethod,
FixingDelegate = instMethod.Invoke(this, nameArgs) as Delegate,
OriginReceiver = f =>
{
getters[name] = f;
}
};
}
instMethod = proxySet.MakeGenericMethod(targetType, property.PropertyType);
if (property.SetMethod != null)
{
yield return new()
{
InjectedMethod = property.SetMethod,
FixingDelegate = instMethod.Invoke(this, nameArgs) as Delegate,
OriginReceiver = f =>
{
setters[name] = f;
}
};
}
}

}
}
}
File renamed without changes.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "com.bbbirder.injection",
"displayName": "Unity Injection",
"description": "Unity注入模块,可以运行时改变被注入函数实现。",
"version": "1.3.0",
"version": "1.3.1",
"hideInEditor": false,
"author": "bbbirder <502100554@qq.com>",
"dependencies": {
"com.bbbirder.directattribute": "1.0.7",
"com.bbbirder.directattribute": "1.0.8",
"com.unity.nuget.mono-cecil": "1.10.2"
},
"samples": [
Expand Down

0 comments on commit 844216a

Please sign in to comment.