Fix generated patch code for ref and subclasses

This commit is contained in:
Sinai 2022-04-22 21:01:47 +10:00
parent ff882296fd
commit 2dc6e386df

View File

@ -15,8 +15,9 @@ namespace UnityExplorer.Hooks
{ {
// Static // Static
private static readonly StringBuilder evalOutput = new(); //static readonly StringBuilder evalOutput = new();
private static readonly ScriptEvaluator scriptEvaluator = new(new StringWriter(evalOutput)); static readonly StringBuilder evaluatorOutput;
static readonly ScriptEvaluator scriptEvaluator = new(new StringWriter(evaluatorOutput = new StringBuilder()));
static HookInstance() static HookInstance()
{ {
@ -41,8 +42,6 @@ namespace UnityExplorer.Hooks
private MethodInfo finalizer; private MethodInfo finalizer;
private MethodInfo transpiler; private MethodInfo transpiler;
private static readonly HashSet<string> namespaceUsings = new();
public HookInstance(MethodInfo targetMethod) public HookInstance(MethodInfo targetMethod)
{ {
this.TargetMethod = targetMethod; this.TargetMethod = targetMethod;
@ -64,7 +63,6 @@ namespace UnityExplorer.Hooks
Unpatch(); Unpatch();
StringBuilder codeBuilder = new(); StringBuilder codeBuilder = new();
namespaceUsings.Clear();
try try
{ {
@ -72,10 +70,7 @@ namespace UnityExplorer.Hooks
// Dynamically compile the patch method // Dynamically compile the patch method
foreach (string ns in namespaceUsings) codeBuilder.AppendLine($"static class DynamicPatch_{DateTime.Now.Ticks}");
codeBuilder.AppendLine($"using {ns};");
codeBuilder.AppendLine($"public class DynamicPatch_{DateTime.Now.Ticks}");
codeBuilder.AppendLine("{"); codeBuilder.AppendLine("{");
codeBuilder.AppendLine(patchSource); codeBuilder.AppendLine(patchSource);
codeBuilder.AppendLine("}"); codeBuilder.AppendLine("}");
@ -115,29 +110,51 @@ namespace UnityExplorer.Hooks
} }
catch (Exception ex) catch (Exception ex)
{ {
ExplorerCore.LogWarning($"Exception creating patch processor for target method {TargetMethod.FullDescription()}!\r\n{ex}"); if (ex is FormatException)
{
string output = scriptEvaluator._textWriter.ToString();
string[] outputSplit = output.Split('\n');
if (outputSplit.Length >= 2)
output = outputSplit[outputSplit.Length - 2];
evaluatorOutput.Clear();
ExplorerCore.Log(codeBuilder.ToString()); if (ScriptEvaluator._reportPrinter.ErrorsCount > 0)
ExplorerCore.LogWarning($"Unable to compile the code. Evaluator's last output was:\r\n{output}");
else
ExplorerCore.LogWarning($"Exception generating patch source code: {ex}");
}
else
ExplorerCore.LogWarning($"Exception generating patch source code: {ex}");
// ExplorerCore.Log(codeBuilder.ToString());
return false; return false;
} }
} }
static string FullDescriptionClean(Type type)
{
string description = type.FullDescription().Replace("+", ".");
if (description.EndsWith("&"))
description = $"ref {description.Substring(0, description.Length - 1)}";
return description;
}
private string GenerateDefaultPatchSourceCode(MethodInfo targetMethod) private string GenerateDefaultPatchSourceCode(MethodInfo targetMethod)
{ {
StringBuilder codeBuilder = new(); StringBuilder codeBuilder = new();
codeBuilder.Append("public static void Postfix("); // System.Reflection.MethodBase __originalMethod codeBuilder.Append("static void Postfix("); // System.Reflection.MethodBase __originalMethod
bool isStatic = targetMethod.IsStatic; bool isStatic = targetMethod.IsStatic;
if (!isStatic) if (!isStatic)
codeBuilder.Append($"{targetMethod.DeclaringType.FullDescription()} __instance"); codeBuilder.Append($"{FullDescriptionClean(targetMethod.DeclaringType)} __instance");
if (targetMethod.ReturnType != typeof(void)) if (targetMethod.ReturnType != typeof(void))
{ {
if (!isStatic) if (!isStatic)
codeBuilder.Append(", "); codeBuilder.Append(", ");
codeBuilder.Append($"{targetMethod.ReturnType.FullDescription()} __result"); codeBuilder.Append($"{FullDescriptionClean(targetMethod.ReturnType)} __result");
} }
ParameterInfo[] parameters = targetMethod.GetParameters(); ParameterInfo[] parameters = targetMethod.GetParameters();
@ -145,7 +162,7 @@ namespace UnityExplorer.Hooks
int paramIdx = 0; int paramIdx = 0;
foreach (ParameterInfo param in parameters) foreach (ParameterInfo param in parameters)
{ {
codeBuilder.Append($", {param.ParameterType.FullDescription().Replace("&", "")} __{paramIdx}"); codeBuilder.Append($", {FullDescriptionClean(param.ParameterType)} __{paramIdx}");
paramIdx++; paramIdx++;
} }