using Mono.Cecil;
using Mono.Cecil.Cil;
namespace StardewModdingAPI.AssemblyRewriters.Framework
{
/// Base class for a field rewriter.
public abstract class BaseFieldRewriter : IInstructionRewriter
{
/*********
** Public methods
*********/
/// Get whether a CIL instruction should be rewritten.
/// The IL instruction.
/// Whether the mod was compiled on a different platform.
public bool ShouldRewrite(Instruction instruction, bool platformChanged)
{
if (instruction.OpCode != OpCodes.Ldfld && instruction.OpCode != OpCodes.Ldsfld && instruction.OpCode != OpCodes.Stfld && instruction.OpCode != OpCodes.Stsfld)
return false; // not a field reference
return this.ShouldRewrite(instruction, (FieldReference)instruction.Operand, platformChanged);
}
/// 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.
public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap)
{
FieldReference fieldRef = (FieldReference)instruction.Operand;
this.Rewrite(module, cil, instruction, fieldRef, assemblyMap);
}
/*********
** Protected methods
*********/
/// Get whether a field reference should be rewritten.
/// The IL instruction.
/// The field reference.
/// Whether the mod was compiled on a different platform.
protected abstract bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged);
/// Rewrite a method for compatibility.
/// The module being rewritten.
/// The CIL rewriter.
/// The instruction which references the field.
/// The field reference invoked by the .
/// Metadata for mapping assemblies to the current platform.
protected abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap);
}
}