diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-21 20:32:02 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-21 20:32:02 -0400 |
commit | 1beee07a3548f4f7efe9d4044ec7a3eb78987f89 (patch) | |
tree | 757f4e87a7f294794b90a274a8785bbdfebd8f63 /src/SMAPI/Framework | |
parent | 310eb1fe9afdb820d5584a46200bf073fc00ccb7 (diff) | |
download | SMAPI-1beee07a3548f4f7efe9d4044ec7a3eb78987f89.tar.gz SMAPI-1beee07a3548f4f7efe9d4044ec7a3eb78987f89.tar.bz2 SMAPI-1beee07a3548f4f7efe9d4044ec7a3eb78987f89.zip |
rewrite method overrides (#711)
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs index 3b8cda88..a0f075bd 100644 --- a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs @@ -82,6 +82,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework foreach (ParameterDefinition parameter in method.Parameters) anyRewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType); + foreach (var methodOverride in method.Overrides) + anyRewritten |= this.RewriteMethodReference(methodOverride); + if (method.HasBody) { foreach (VariableDefinition variable in method.Body.Variables) @@ -133,25 +136,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework // method reference MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); if (methodRef != null) - { - rewritten |= this.RewriteTypeReference(methodRef.DeclaringType, newType => - { - // note: generic methods are wrapped into a MethodSpecification which doesn't allow changing the - // declaring type directly. For our purposes we want to change all generic versions of a matched - // method anyway, so we can use GetElementMethod to get the underlying method here. - methodRef.GetElementMethod().DeclaringType = newType; - }); - rewritten |= this.RewriteTypeReference(methodRef.ReturnType, newType => methodRef.ReturnType = newType); - - foreach (var parameter in methodRef.Parameters) - rewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType); - - if (methodRef is GenericInstanceMethod genericRef) - { - for (int i = 0; i < genericRef.GenericArguments.Count; i++) - rewritten |= this.RewriteTypeReference(genericRef.GenericArguments[i], newType => genericRef.GenericArguments[i] = newType); - } - } + this.RewriteMethodReference(methodRef); // type reference if (instruction.Operand is TypeReference typeRef) @@ -169,6 +154,33 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework return rewritten; } + /// <summary>Rewrite a method reference if needed.</summary> + /// <param name="methodRef">The current method reference.</param> + private bool RewriteMethodReference(MethodReference methodRef) + { + bool rewritten = false; + + rewritten |= this.RewriteTypeReference(methodRef.DeclaringType, newType => + { + // note: generic methods are wrapped into a MethodSpecification which doesn't allow changing the + // declaring type directly. For our purposes we want to change all generic versions of a matched + // method anyway, so we can use GetElementMethod to get the underlying method here. + methodRef.GetElementMethod().DeclaringType = newType; + }); + rewritten |= this.RewriteTypeReference(methodRef.ReturnType, newType => methodRef.ReturnType = newType); + + foreach (var parameter in methodRef.Parameters) + rewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType); + + if (methodRef is GenericInstanceMethod genericRef) + { + for (int i = 0; i < genericRef.GenericArguments.Count; i++) + rewritten |= this.RewriteTypeReference(genericRef.GenericArguments[i], newType => genericRef.GenericArguments[i] = newType); + } + + return rewritten; + } + /// <summary>Rewrite a type reference if needed.</summary> /// <param name="type">The current type reference.</param> /// <param name="replaceWith">Replaces the type reference with a new one.</param> |