Skip to content

Commit

Permalink
add config to hide non-default delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
art0007i committed Sep 30, 2023
1 parent 6d27bc2 commit 22f7453
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
29 changes: 20 additions & 9 deletions ShowDelegates/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public override int GetHashCode()
return hash;
}
}

}


Expand All @@ -106,22 +105,34 @@ internal class Helper

public static Type ClassifyDelegate(MethodInfo m)
{
/*if (argumentLookup.TryGetValue(new(m), out var t))
if (argumentLookup.TryGetValue(new(m), out var t))
{
return t;
}*/
}
var p = m.GetParameters().Select(para => para.ParameterType).ToArray();
if(m.ReturnType == typeof(void))
if (p.Length == 3 && p[0] == typeof(IButton) && p[1] == typeof(ButtonEventData))
{
return typeof(ButtonEventHandler<>).MakeGenericType(p[2]);
}
return GetFuncOrAction(m, p);

}

public static Type GetFuncOrAction(MethodInfo m)
{
var p = m.GetParameters().Select(para => para.ParameterType).ToArray();
return GetFuncOrAction(m, p);
}

public static Type GetFuncOrAction(MethodInfo m, Type[] p)
{
if (m.ReturnType == typeof(void))
{
/*if(p.Length == 3 && p[0] == typeof(IButton) && p[1] == typeof(ButtonEventData))
{
return typeof(ButtonEventHandler<>).MakeGenericType(p[2]);
}*/
return Expression.GetActionType(p);
}
else
{
p = p.Concat(new[] { m.ReturnType}).ToArray();
p = p.Concat(new[] { m.ReturnType }).ToArray();
return Expression.GetFuncType(p);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ShowDelegates/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.0")]
[assembly: AssemblyFileVersion("2.1.0")]
[assembly: AssemblyVersion("2.2.0")]
[assembly: AssemblyFileVersion("2.2.0")]
37 changes: 30 additions & 7 deletions ShowDelegates/ShowDelegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@
using FrooxEngine;
using FrooxEngine.UIX;
using Elements.Core;
using FrooxEngine.ProtoFlux;

namespace ShowDelegates
{
public class ShowDelegates : ResoniteMod
{
public override string Name => "ShowDelegates";
public override string Author => "art0007i";
public override string Version => "2.1.0";
public override string Version => "2.2.0";
public override string Link => "https://github.com/art0007i/ShowDelegates/";

[AutoRegisterConfigKey]
private static readonly ModConfigurationKey<bool> KEY_SHOW_HIDDEN = new ModConfigurationKey<bool>("show_hidden", "If false items with the hidden HideInInspector attribute will not be shown", () => true);
[AutoRegisterConfigKey]
private static readonly ModConfigurationKey<bool> KEY_DEFAULT_OPEN = new ModConfigurationKey<bool>("default_open", "If true delegates will be expanded by default", () => false);
[AutoRegisterConfigKey]
private static readonly ModConfigurationKey<bool> KEY_SHOW_DELEGATES = new ModConfigurationKey<bool>("show_deleages", "If false delegates will not be shown", () => true);
[AutoRegisterConfigKey]
[AutoRegisterConfigKey]
private static readonly ModConfigurationKey<bool> KEY_SHORT_NAMES = new ModConfigurationKey<bool>("short_names", "Show short delegate names.", () => true);
[AutoRegisterConfigKey]
private static readonly ModConfigurationKey<bool> KEY_SHOW_NON_DEFAULT = new ModConfigurationKey<bool>("show_non_default", "If false only delegates that appear in vanilla will be shown.", () => true);
[AutoRegisterConfigKey]
private static readonly ModConfigurationKey<bool> KEY_SHOW_HIDDEN = new ModConfigurationKey<bool>("show_hidden", "If true items hidden with the HideInInspector attribute will be shown", () => true);

private static ModConfiguration config;

Expand Down Expand Up @@ -94,6 +97,23 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
}
}

[HarmonyPatch(typeof(ProtoFluxTool), "OnCreateDelegateProxy")]
class FixProtoFluxDeleagtes
{
public static void Prefix(IButton button, ButtonEventData eventData, ref Delegate target)
{
try
{
// this could throw in many ways....
var delegateType = Helper.GetFuncOrAction(target.Method);
target = target.Method.CreateDelegate(delegateType, target.Target);
}
catch(Exception e)
{
}
}
}

[HarmonyPatch(typeof(WorkerInspector))]
[HarmonyPatch("BuildInspectorUI")]
class WorkerInspector_BuildInspectorUI_Patch
Expand Down Expand Up @@ -146,7 +166,10 @@ private static bool Prefix(WorkerInspector __instance, Worker worker, UIBuilder
var info = initInfo.syncMethods[j];

var delegateType = info.methodType;
if (!typeof(MulticastDelegate).IsAssignableFrom(delegateType))

if (!config.GetValue(KEY_SHOW_NON_DEFAULT) && delegateType == typeof(Delegate)) continue;

if (!typeof(MulticastDelegate).IsAssignableFrom(delegateType))
{
try
{
Expand All @@ -160,13 +183,13 @@ private static bool Prefix(WorkerInspector __instance, Worker worker, UIBuilder
}
if (delegateType == null)
{
//Error("Unmapped type. Please report this message to the mod author: Could not identify " + info.method + " on type " + info.method.DeclaringType);
Error("Unmapped type. Please report this message to the mod author: Could not identify " + info.method + " on type " + info.method.DeclaringType);
ui.Text("<color=orange>" + funName("<i>unknown</i>", info.method), true, new Alignment?(Alignment.MiddleLeft));
continue;
}
}

var method = info.method.IsStatic ? info.method.CreateDelegate(delegateType) : info.method.CreateDelegate(delegateType, worker);
var method = info.method.IsStatic ? info.method.CreateDelegate(delegateType) : info.method.CreateDelegate(delegateType, worker);

delegateFunc.MakeGenericMethod(delegateType).Invoke(null, new object[]
{
Expand Down

0 comments on commit 22f7453

Please sign in to comment.