using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace StardewModdingAPI.AssemblyRewriters.Rewriters
{
/// Rewrites method references from one parent type to another if the signatures match.
public class MethodParentRewriter : IInstructionRewriter
{
/*********
** Properties
*********/
/// The type whose methods to remap.
private readonly Type FromType;
/// The type with methods to map to.
private readonly Type ToType;
/// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on.
private readonly bool OnlyIfPlatformChanged;
/*********
** Accessors
*********/
/// A brief noun phrase indicating what the instruction finder matches.
public string NounPhrase { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The type whose methods to remap.
/// The type with methods to map to.
/// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on.
/// A brief noun phrase indicating what the instruction finder matches (or null to generate one).
public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null)
{
this.FromType = fromType;
this.ToType = toType;
this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods";
this.OnlyIfPlatformChanged = onlyIfPlatformChanged;
}
/// Rewrite a method definition for compatibility.
/// The module being rewritten.
/// The method definition to rewrite.
/// Metadata for mapping assemblies to the current platform.
/// Whether the mod was compiled on a different platform.
/// Returns whether the instruction was rewritten.
/// The CIL instruction is not compatible, and can't be rewritten.
public bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
return false;
}
/// Rewrite a CIL instruction for compatibility.
/// The module being rewritten.
/// The CIL rewriter.
/// The instruction to rewrite.
/// Metadata for mapping assemblies to the current platform.
/// Whether the mod was compiled on a different platform.
/// Returns whether the instruction was rewritten.
/// The CIL instruction is not compatible, and can't be rewritten.
public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction, platformChanged))
return false;
MethodReference methodRef = (MethodReference)instruction.Operand;
methodRef.DeclaringType = module.Import(this.ToType);
return true;
}
/*********
** Protected methods
*********/
/// Get whether a CIL instruction matches.
/// The IL instruction.
/// Whether the mod was compiled on a different platform.
protected bool IsMatch(Instruction instruction, bool platformChanged)
{
MethodReference methodRef = RewriteHelper.AsMethodReference(instruction);
return
methodRef != null
&& (platformChanged || !this.OnlyIfPlatformChanged)
&& methodRef.DeclaringType.FullName == this.FromType.FullName
&& RewriteHelper.HasMatchingSignature(this.ToType, methodRef);
}
}
}