Skip to content

Commit 414e016

Browse files
committed
Añade soporte para detectar atributos MethodFlux en interfaces y actualiza la documentación correspondiente en README.md
1 parent a8b5739 commit 414e016

2 files changed

Lines changed: 64 additions & 5 deletions

File tree

Editor/Editors/MonoFluxEditor.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public partial class MonoFluxEditor : UnityEditor.Editor
3535
private MethodInfo[] methods_subscribeAttrb;
3636
private Dictionary<MethodInfo, object> dic_method_parameters;
3737
private Dictionary<MethodInfo, object> dic_method_outputs;
38+
#pragma warning disable CS0618
39+
private Dictionary<MethodInfo, FluxAttribute> dic_method_attributes;
40+
#pragma warning restore CS0618
3841
private static bool ShowMethods
3942
{
4043
get => PlayerPrefs.GetInt("__UniFlux.MonoFluxEditor.ShowBox", default) == default;
@@ -45,7 +48,43 @@ private void OnEnable()
4548
Type type = target.GetType();
4649
var methods = type.GetMethods((BindingFlags)(-1));
4750
#pragma warning disable CS0618
48-
methods_subscribeAttrb = methods.Where(m => m.GetCustomAttributes(typeof(FluxAttribute), true).Length > 0).ToArray();
51+
var discoveredMethods = new List<MethodInfo>();
52+
dic_method_attributes = new Dictionary<MethodInfo, FluxAttribute>();
53+
54+
// Phase 1: methods with FluxAttribute on the concrete class
55+
foreach (var m in methods)
56+
{
57+
if (Attribute.GetCustomAttributes(m).FirstOrDefault(a => a is FluxAttribute) is FluxAttribute attr)
58+
{
59+
discoveredMethods.Add(m);
60+
dic_method_attributes[m] = attr;
61+
}
62+
}
63+
64+
// Phase 2: FluxAttributes declared on interface method definitions
65+
var interfaces = type.GetInterfaces();
66+
for (int i = 0; i < interfaces.Length; i++)
67+
{
68+
var map = type.GetInterfaceMap(interfaces[i]);
69+
for (int j = 0; j < map.InterfaceMethods.Length; j++)
70+
{
71+
var ifaceMethod = map.InterfaceMethods[j];
72+
if (Attribute.GetCustomAttributes(ifaceMethod).FirstOrDefault(a => a is FluxAttribute) is FluxAttribute attr)
73+
{
74+
var implMethod = map.TargetMethods[j];
75+
if (!dic_method_attributes.ContainsKey(implMethod))
76+
{
77+
dic_method_attributes[implMethod] = attr;
78+
}
79+
if (!discoveredMethods.Contains(implMethod))
80+
{
81+
discoveredMethods.Add(implMethod);
82+
}
83+
}
84+
}
85+
}
86+
87+
methods_subscribeAttrb = discoveredMethods.ToArray();
4988
#pragma warning restore CS0618
5089
dic_method_parameters = methods_subscribeAttrb.Select(m => new { Method = m, Parameters = null as object }).ToDictionary(mp => mp.Method, mp => mp.Parameters);
5190
dic_method_outputs = methods_subscribeAttrb.Select(m => new { Method = m, Return = null as object }).ToDictionary(mp => mp.Method, mp => mp.Return);
@@ -88,17 +127,15 @@ private void _Draw()
88127
foreach (var item in methods_subscribeAttrb)
89128
{
90129
#pragma warning disable CS0618
91-
var atribute = item.GetCustomAttribute<FluxAttribute>();
130+
var atribute = dic_method_attributes[item];
92131
#pragma warning restore CS0618
93132
var parameters = item.GetParameters();
94133
var isParameters = parameters.Length > 0;
95134
var isReturn = item.ReturnType != typeof(void);
96135
var isErr_static = item.IsStatic;
97136
var isError = isErr_static;
98137
string key_color = isError ? "yellow" : "white";
99-
#pragma warning disable CS0618
100-
var atrbtype = item.GetCustomAttributes(typeof(FluxAttribute), true)[0];
101-
#pragma warning restore CS0618
138+
var atrbtype = atribute;
102139

103140

104141
int opt_status = (param: isParameters, rtrn: isReturn) switch

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ npm i com.xavierarpa.uniflux
5555

5656
Note: Storing (ADD and REMOVE) by design is planned to do it once so there's no problem in performance.
5757

58+
# Tip: MethodFlux in Interfaces
59+
60+
You can use `[MethodFlux]` directly in an interface so that implementations are automatically detected. Example:
61+
62+
```csharp
63+
public interface IService<T>
64+
{
65+
T Service
66+
{
67+
[MethodFlux(0)] get; // attribute lives on the interface
68+
}
69+
}
70+
71+
internal class AudioModule : MonoFlux, IService<IAudioService>
72+
{
73+
IAudioService IService<IAudioService>.Service => this;
74+
// ↑ Automatically subscribed — UniFlux reads the attribute from the interface
75+
}
76+
```
77+
78+
_Personal recommendation from Xavier!_
79+
5880
## Star History
5981

6082
[![Star History Chart](https://api.star-history.com/svg?repos=xavierarpa/uniflux&type=Date)](https://star-history.com/#xavierarpa/uniflux&Date)

0 commit comments

Comments
 (0)