-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasePatcher.cs
154 lines (142 loc) · 4.95 KB
/
BasePatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using HarmonyLib;
using StardewModdingAPI;
using System.Reflection;
namespace ImproveGame;
public class BasePatcher
{
public static BindingFlags AllFlags = BindingFlags.Static | BindingFlags.Public
| BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
Type thisType;
public BasePatcher()
{
thisType = GetType();
}
public ConstructorInfo GetConstructor(Type type, Type[] paramTypes)
{
return GetConstructor(type, paramTypes.Select(p => p.Name).ToArray());
}
public ConstructorInfo GetConstructor(Type type, string[] paramTypeNames)
{
var all = type.GetConstructors(AllFlags);
foreach (var ctor in all)
{
var _params = ctor.GetParameters();
if (_params.Length == paramTypeNames.Length)
{
bool isMatch = true;
for (int i = 0; i < _params.Length; i++)
{
var paramTypeName = _params[i].ParameterType.Name.ToLower();
if (paramTypeName != paramTypeNames[i].ToLower())
isMatch = false;
}
if (isMatch)
{
return ctor;
}
}
}
LogError("error not found consturctor: " + type);
return null;
}
public MethodInfo GetMethod(string name) => GetMethod(thisType, name);
public MethodInfo GetMethod(Type type, string name, string[] paramTypeNames)
{
var methods = type.GetMethods(AllFlags).Where(m => m.Name == name).ToArray();
foreach (var method in methods)
{
var _params = method.GetParameters();
if (_params.Length == paramTypeNames.Length)
{
bool isMatch = true;
for (int i = 0; i < _params.Length; i++)
{
var paramTypeName = _params[i].ParameterType.Name.ToLower();
if (paramTypeName != paramTypeNames[i].ToLower())
isMatch = false;
}
if (isMatch)
{
return method;
}
}
}
Log("error not found method: " + type + "::" + name + ", paramTypeCount: " + paramTypeNames.Length);
return null;
}
public MethodInfo GetMethod(Type type, string name) => type.GetMethod(name, AllFlags);
Dictionary<string, Assembly> cacheAssembly = new();
public Type GetType(string asmName, string fullTypeName)
{
if (!cacheAssembly.TryGetValue(asmName, out Assembly assembly))
{
assembly = Assembly.Load(asmName);
if (assembly == null)
return null;
cacheAssembly.Add(asmName, assembly);
}
return assembly.GetType(fullTypeName);
}
public object? ReadField(object obj, string fieldName)
{
var field = obj?.GetType().GetField(fieldName, AllFlags);
var val = field?.GetValue(obj);
if (val == null)
{
Log($"error read field:{fieldName},obj:{obj} is null");
return default;
}
return val;
}
public T ReadField<T>(object obj, string fieldName)
{
var val = ReadField(obj, fieldName);
return (T)val;
}
public List<T>? ReadFieldList<T>(object obj, string fieldName)
{
var val = ReadField(obj, fieldName);
if (val == null)
return null;
return (List<T>)val;
}
public void WriteField(object obj, string fieldName, object value)
{
var field = obj?.GetType().GetField(fieldName, AllFlags);
field?.SetValue(obj, value);
}
public PropertyInfo GetProperty(Type type, string name)
{
return type.GetProperty(name, AllFlags);
}
static Harmony harmony => ModEntry.Instance.harmony;
public void PatchPrefix(MethodInfo original, string prefixMethodName)
{
harmony.Patch(original, prefix: new(GetMethod(prefixMethodName)));
Log("patched method prefix: " + original, LogLevel.Info);
}
public void PatchPostfix(MethodInfo original, string postfixMethodName)
{
harmony.Patch(original, postfix: new(GetMethod(postfixMethodName)));
Log("patched method postfix: " + original, LogLevel.Info);
}
public void PatchPostfix(ConstructorInfo ctor, string postfixMethodName)
{
harmony.Patch(ctor, postfix: new(GetMethod(postfixMethodName)));
Log("patched ctor postfix: " + ctor, LogLevel.Info);
}
public static void Log(string msg, LogLevel logLevel = LogLevel.Trace)
{
msg = $"[Patcher Tool] {msg}";
Logger.Log(msg, logLevel);
}
public static void LogError(string msg) => Log(msg, LogLevel.Error);
public static void LogWarn(string msg) => Log(msg, LogLevel.Warn);
public static void Log(object obj)
{
if (obj == null)
Log("obj null");
else
Log(obj);
}
}