rework patches

This commit is contained in:
Mister_Nebula 2021-08-21 19:52:35 +01:00
parent bc41702ac2
commit 222c4047a5

View File

@ -35,15 +35,15 @@ namespace QSB.Patches
_patchedMethods.Add(method);
}
public void Prefix(string patchName)
=> DoPrefixPostfix(true, patchName);
public void Prefix(string patchName, params Type[] args)
=> DoPrefixPostfix(true, patchName, args);
public void Postfix(string patchName)
=> DoPrefixPostfix(false, patchName);
public void Postfix(string patchName, params Type[] args)
=> DoPrefixPostfix(false, patchName, args);
private void DoPrefixPostfix(bool isPrefix, string patchName)
private void DoPrefixPostfix(bool isPrefix, string patchName, params Type[] args)
{
var method = GetMethodInfo(patchName);
var method = GetMethodInfo(patchName, args);
if (method != null)
{
@ -62,7 +62,7 @@ namespace QSB.Patches
//DebugLog.DebugWrite($"{(isPrefix ? "[Prefix]" : "[Postfix]")} {patchName}", method == null ? MessageType.Error : MessageType.Success);
}
private MethodInfo GetMethodInfo(string patchName)
private MethodInfo GetMethodInfo(string patchName, params Type[] args)
{
var splitName = patchName.Split('_');
var typeName = splitName[0];
@ -75,14 +75,41 @@ namespace QSB.Patches
return null;
}
var method = type.GetAnyMethod(methodName);
if (method == null)
var allMethodsOfName = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Where(x => x.Name == methodName);
if (allMethodsOfName.Count() == 0)
{
DebugLog.ToConsole($"Error - Couldn't find method for patch name {patchName}!", MessageType.Error);
DebugLog.ToConsole($"Error - Could not find method {methodName} in type {typeName}.", MessageType.Error);
return null;
}
return method;
if (allMethodsOfName.Count() == 1)
{
return allMethodsOfName.First();
}
DebugLog.DebugWrite($"More than one method found with name {methodName} in type {typeName}");
foreach (var method in allMethodsOfName)
{
DebugLog.DebugWrite($"checking {method.Name}");
var paramList = method.GetParameters().Select(x => x.ParameterType);
if (Enumerable.SequenceEqual(args, paramList))
{
DebugLog.DebugWrite($"match!");
return method;
}
}
DebugLog.DebugWrite($"nothing found");
DebugLog.ToConsole($"Error - Could not find method {methodName} in type {typeName} with parameter list of {string.Join(", ", args.Select(x => x.FullName).ToArray())}", MessageType.Error);
foreach (var method in allMethodsOfName)
{
var paramList = method.GetParameters().Select(x => x.ParameterType);
DebugLog.ToConsole($"- Found {method.Name}, but with params {string.Join(", ", paramList.Select(x => x.FullName).ToArray())}", MessageType.Error);
}
return null;
}
private Type GetFirstTypeByName(string typeName)