Skip to content

Commit 79ac32f

Browse files
committed
feat: add debug functionality
1 parent 6b430ef commit 79ac32f

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

COTL_API/Debug/DebugManager.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Reflection;
12
using COTL_API.CustomFollowerCommand;
23
using COTL_API.CustomInventory;
34
using COTL_API.CustomLocalization;
@@ -9,6 +10,7 @@
910
using COTL_API.CustomTarotCard;
1011
using COTL_API.CustomTasks;
1112
using Lamb.UI;
13+
using Lamb.UI.FollowerInteractionWheel;
1214
using UnityEngine;
1315

1416
namespace COTL_API.Debug;
@@ -26,6 +28,76 @@ internal class DebugManager
2628

2729
internal static RelicType DebugRelic { get; private set; }
2830

31+
internal static Dictionary<Type, Type> CustomClassMappings = new()
32+
{
33+
{ typeof(InventoryItem), typeof(CustomInventoryItem) },
34+
// { typeof(CommandItem), typeof(CustomFollowerCommand.CustomFollowerCommand) },
35+
// { typeof(RelicData), typeof(CustomRelicData) },
36+
// { typeof(StructureBrain), typeof(CustomStructure) },
37+
{ typeof(TarotCards), typeof(CustomTarotCard.CustomTarotCard) }
38+
};
39+
40+
private static string BeautifyNamespace(string? str)
41+
{
42+
return str is null or "" ? "" : str + ".";
43+
}
44+
45+
private static void ShowDiff(Type a, Type b)
46+
{
47+
LogDebug($"Showing diff between {BeautifyNamespace(a.Namespace)}{a.Name} and {BeautifyNamespace(b.Namespace)}{b.Name}");
48+
49+
LogDebug("Methods");
50+
foreach (var method in a.GetMethods())
51+
{
52+
if (method.Name.StartsWith("get_") || method.Name.StartsWith("set_"))
53+
continue;
54+
55+
var corrspondingMethod = b.GetMethods().FirstOrDefault(m => m.Name == method.Name);
56+
57+
if (corrspondingMethod is null)
58+
{
59+
LogDebug($"{BeautifyNamespace(a.Namespace)}{a.Name}.{method.Name}: missing corrsponding method");
60+
continue;
61+
}
62+
63+
if (!method.ReturnType.IsAssignableFrom(corrspondingMethod.ReturnType))
64+
{
65+
LogDebug($"{BeautifyNamespace(a.Namespace)}{a.Name}.{method.Name} and {BeautifyNamespace(b.Namespace)}{b.Name}.{method.Name} have mismatched return type");
66+
continue;
67+
}
68+
69+
var corrospondingParameters = method.GetParameters();
70+
71+
var parameters = method.GetParameters();
72+
73+
if (parameters.Length == 1 && parameters[0].GetType().IsEnum)
74+
parameters = parameters.Skip(1).ToArray();
75+
76+
var parameterMatches = parameters.Length == corrospondingParameters.Length && parameters.All(info =>
77+
{
78+
return info.ParameterType.IsAssignableFrom(corrospondingParameters[info.Position].ParameterType);
79+
});
80+
81+
if (!parameterMatches)
82+
{
83+
LogDebug($"{BeautifyNamespace(a.Namespace)}{a.Name}.{method.Name} and {BeautifyNamespace(b.Namespace)}{b.Name}.{method.Name} have mismatched parameters");
84+
LogDebug($"{BeautifyNamespace(a.Namespace)}{a.Name}.{method.Name}: {parameters}");
85+
LogDebug($"{BeautifyNamespace(b.Namespace)}{b.Name}.{method.Name}: {corrospondingParameters}");
86+
continue;
87+
}
88+
89+
LogDebug($"{BeautifyNamespace(a.Namespace)}{a.Name}.{method.Name} matches {BeautifyNamespace(b.Namespace)}{b.Name}.{method.Name}");
90+
}
91+
}
92+
93+
internal static void CheckCustomClasses()
94+
{
95+
foreach (var type in CustomClassMappings)
96+
{
97+
ShowDiff(type.Key, type.Value);
98+
}
99+
}
100+
29101
internal static void AddDebugContent()
30102
{
31103
if (DebugContentAdded) return;

COTL_API/Plugin.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ private void Awake()
192192

193193
CustomSettingsManager.AddBepInExConfig("API", "Unity debug logging", UnityDebug);
194194

195-
if (Debug) DebugManager.AddDebugContent();
195+
if (Debug)
196+
{
197+
DebugManager.AddDebugContent();
198+
DebugManager.CheckCustomClasses();
199+
}
196200

197201
LogInfo($"{MyPluginInfo.PLUGIN_NAME} loaded!");
198202
}

0 commit comments

Comments
 (0)