only rewrite type references if necessary (#532)

This commit is contained in:
Jesse Plamondon-Willard 2018-06-24 23:08:06 -04:00
parent 08b37c70a3
commit 06437b80ac
1 changed files with 18 additions and 25 deletions

View File

@ -44,7 +44,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
// return type // return type
if (this.IsMatch(method.ReturnType)) if (this.IsMatch(method.ReturnType))
{ {
method.ReturnType = this.RewriteIfNeeded(module, method.ReturnType); this.RewriteIfNeeded(module, method.ReturnType, newType => method.ReturnType = newType);
rewritten = true; rewritten = true;
} }
@ -53,7 +53,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{ {
if (this.IsMatch(parameter.ParameterType)) if (this.IsMatch(parameter.ParameterType))
{ {
parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType); this.RewriteIfNeeded(module, parameter.ParameterType, newType => parameter.ParameterType = newType);
rewritten = true; rewritten = true;
} }
} }
@ -64,9 +64,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
var parameter = method.GenericParameters[i]; var parameter = method.GenericParameters[i];
if (this.IsMatch(parameter)) if (this.IsMatch(parameter))
{ {
TypeReference newType = this.RewriteIfNeeded(module, parameter); this.RewriteIfNeeded(module, parameter, newType => method.GenericParameters[i] = new GenericParameter(parameter.Name, newType));
if (newType != parameter)
method.GenericParameters[i] = new GenericParameter(parameter.Name, newType);
rewritten = true; rewritten = true;
} }
} }
@ -76,7 +74,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{ {
if (this.IsMatch(variable.VariableType)) if (this.IsMatch(variable.VariableType))
{ {
variable.VariableType = this.RewriteIfNeeded(module, variable.VariableType); this.RewriteIfNeeded(module, variable.VariableType, newType => variable.VariableType = newType);
rewritten = true; rewritten = true;
} }
} }
@ -101,27 +99,23 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction);
if (fieldRef != null) if (fieldRef != null)
{ {
fieldRef.DeclaringType = this.RewriteIfNeeded(module, fieldRef.DeclaringType); this.RewriteIfNeeded(module, fieldRef.DeclaringType, newType => fieldRef.DeclaringType = newType);
fieldRef.FieldType = this.RewriteIfNeeded(module, fieldRef.FieldType); this.RewriteIfNeeded(module, fieldRef.FieldType, newType => fieldRef.FieldType = newType);
} }
// method reference // method reference
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
if (methodRef != null) if (methodRef != null)
{ {
methodRef.DeclaringType = this.RewriteIfNeeded(module, methodRef.DeclaringType); this.RewriteIfNeeded(module, methodRef.DeclaringType, newType => methodRef.DeclaringType = newType);
methodRef.ReturnType = this.RewriteIfNeeded(module, methodRef.ReturnType); this.RewriteIfNeeded(module, methodRef.ReturnType, newType => methodRef.ReturnType = newType);
foreach (var parameter in methodRef.Parameters) foreach (var parameter in methodRef.Parameters)
parameter.ParameterType = this.RewriteIfNeeded(module, parameter.ParameterType); this.RewriteIfNeeded(module, parameter.ParameterType, newType => parameter.ParameterType = newType);
} }
// type reference // type reference
if (instruction.Operand is TypeReference typeRef) if (instruction.Operand is TypeReference typeRef)
{ this.RewriteIfNeeded(module, typeRef, newType => cil.Replace(instruction, cil.Create(instruction.OpCode, newType)));
TypeReference newRef = this.RewriteIfNeeded(module, typeRef);
if (typeRef != newRef)
cil.Replace(instruction, cil.Create(instruction.OpCode, newRef));
}
return InstructionHandleResult.Rewritten; return InstructionHandleResult.Rewritten;
} }
@ -129,31 +123,30 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters
/********* /*********
** Private methods ** Private methods
*********/ *********/
/// <summary>Get the adjusted type reference if it matches, else the same value.</summary> /// <summary>Change a type reference if needed.</summary>
/// <param name="module">The assembly module containing the instruction.</param> /// <param name="module">The assembly module containing the instruction.</param>
/// <param name="type">The type to replace if it matches.</param> /// <param name="type">The type to replace if it matches.</param>
private TypeReference RewriteIfNeeded(ModuleDefinition module, TypeReference type) /// <param name="set">Assign the new type reference.</param>
private void RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action<TypeReference> set)
{ {
// current type // current type
if (type.FullName == this.FromTypeName) if (type.FullName == this.FromTypeName)
{ {
if (this.ShouldIgnore(type)) if (!this.ShouldIgnore(type))
return type; set(module.ImportReference(this.ToType));
return module.ImportReference(this.ToType); return;
} }
// recurse into generic arguments // recurse into generic arguments
if (type is GenericInstanceType genericType) if (type is GenericInstanceType genericType)
{ {
for (int i = 0; i < genericType.GenericArguments.Count; i++) for (int i = 0; i < genericType.GenericArguments.Count; i++)
genericType.GenericArguments[i] = this.RewriteIfNeeded(module, genericType.GenericArguments[i]); this.RewriteIfNeeded(module, genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef);
} }
// recurse into generic parameters (e.g. constraints) // recurse into generic parameters (e.g. constraints)
for (int i = 0; i < type.GenericParameters.Count; i++) for (int i = 0; i < type.GenericParameters.Count; i++)
type.GenericParameters[i] = new GenericParameter(this.RewriteIfNeeded(module, type.GenericParameters[i])); this.RewriteIfNeeded(module, type.GenericParameters[i], typeRef => type.GenericParameters[i] = new GenericParameter(typeRef));
return type;
} }
} }
} }