Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit ad5fc04

Browse files
committed
Fix methods with multiple generic constraints
1 parent c39e097 commit ad5fc04

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

src/CachedObjects/CacheObjectBase.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Reflection;
45
using MelonLoader;
56
using UnityEngine;
@@ -400,17 +401,36 @@ public void Draw(Rect window, float labelWidth = 215f)
400401

401402
for (int i = 0; i < cm.GenericArgs.Length; i++)
402403
{
403-
var type = cm.GenericConstraints[i]?.FullName ?? "Any";
404+
string types = "";
405+
if (cm.GenericConstraints[i].Length > 0)
406+
{
407+
foreach (var constraint in cm.GenericConstraints[i])
408+
{
409+
if (types != "") types += ", ";
410+
411+
string type;
412+
413+
if (constraint == null)
414+
type = "Any";
415+
else
416+
type = constraint.ToString();
417+
418+
types += $"<color={UIStyles.Syntax.Class_Instance}>{type}</color>";
419+
}
420+
}
421+
else
422+
{
423+
types = $"<color={UIStyles.Syntax.Class_Instance}>Any</color>";
424+
}
404425
var input = cm.GenericArgInput[i];
405-
var label = $"<color={UIStyles.Syntax.Class_Instance}>{type}</color>";
406426

407427
GUILayout.BeginHorizontal(null);
408428

409429
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
410430
GUILayout.Label($"<color={UIStyles.Syntax.StructGreen}>{cm.GenericArgs[i].Name}</color>", new GUILayoutOption[] { GUILayout.Width(15) });
411431
cm.GenericArgInput[i] = GUILayout.TextField(input, new GUILayoutOption[] { GUILayout.Width(150) });
412432
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
413-
GUILayout.Label(label, null);
433+
GUILayout.Label(types, null);
414434

415435
GUILayout.EndHorizontal();
416436
}

src/CachedObjects/Other/CacheMethod.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CacheMethod : CacheObjectBase
1414
public override bool HasParameters => base.HasParameters || GenericArgs.Length > 0;
1515

1616
public Type[] GenericArgs { get; private set; }
17-
public Type[] GenericConstraints { get; private set; }
17+
public Type[][] GenericConstraints { get; private set; }
1818

1919
public string[] GenericArgInput = new string[0];
2020

@@ -23,8 +23,7 @@ public override void Init()
2323
var mi = (MemInfo as MethodInfo);
2424
GenericArgs = mi.GetGenericArguments();
2525

26-
GenericConstraints = GenericArgs.Select(x => x.GetGenericParameterConstraints()
27-
.FirstOrDefault())
26+
GenericConstraints = GenericArgs.Select(x => x.GetGenericParameterConstraints())
2827
.ToArray();
2928

3029
GenericArgInput = new string[GenericArgs.Length];
@@ -86,21 +85,22 @@ private MethodInfo MakeGenericMethodFromInput()
8685
var input = GenericArgInput[i];
8786
if (ReflectionHelpers.GetTypeByName(input) is Type t)
8887
{
89-
if (GenericConstraints[i] == null)
88+
if (GenericConstraints[i].Length == 0)
9089
{
9190
list.Add(t);
9291
}
9392
else
9493
{
95-
if (GenericConstraints[i].IsAssignableFrom(t))
94+
foreach (var constraint in GenericConstraints[i].Where(x => x != null))
9695
{
97-
list.Add(t);
98-
}
99-
else
100-
{
101-
MelonLogger.LogWarning($"Generic argument #{i} '{input}', is not assignable from the generic constraint!");
102-
return null;
96+
if (!constraint.IsAssignableFrom(t))
97+
{
98+
MelonLogger.LogWarning($"Generic argument #{i}, '{input}' is not assignable from the constraint '{constraint}'!");
99+
return null;
100+
}
103101
}
102+
103+
list.Add(t);
104104
}
105105
}
106106
else

src/Tests/TestClass.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
using System;
44
using UnityEngine;
55

6+
// used to test multiple generic constraints
7+
public class TestGeneric : IComparable<string>
8+
{
9+
public TestGeneric() { }
10+
11+
public int CompareTo(string other) => throw new NotImplementedException();
12+
}
13+
614
namespace Explorer.Tests
715
{
816
public class TestClass
@@ -22,7 +30,8 @@ public TestClass()
2230
public static int StaticField = 5;
2331
public int NonStaticField;
2432

25-
public static string TestGeneric<C, T>(string arg0) where C : Component
33+
34+
public static string TestGeneric<C, T>(string arg0) where C : Component where T : TestGeneric, IComparable<string>
2635
{
2736
return $"C: '{typeof(C).FullName}', T: '{typeof(T).FullName}', arg0: '{arg0}'";
2837
}

0 commit comments

Comments
 (0)