using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
using StardewModdingAPI.Framework.ModLoading.Finders;
namespace StardewModdingAPI.Framework.ModLoading.Rewriters
{
/// Rewrites field references into property references.
internal class FieldToPropertyRewriter : FieldFinder
{
/*********
** Properties
*********/
/// The type whose field to which references should be rewritten.
private readonly Type Type;
/// The field name to rewrite.
private readonly string FieldName;
/*********
** Public methods
*********/
/// Construct an instance.
/// The type whose field to which references should be rewritten.
/// The field name to rewrite.
/// A brief noun phrase indicating what the instruction finder matches (or null to generate one).
public FieldToPropertyRewriter(Type type, string fieldName, string nounPhrase = null)
: base(type.FullName, fieldName, nounPhrase)
{
this.Type = type;
this.FieldName = fieldName;
}
/// Rewrite a CIL instruction for compatibility.
/// The mod to which the module belongs.
/// 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 override bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged)
{
if (!this.IsMatch(instruction))
return false;
string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set";
MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}"));
cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef));
return true;
}
}
}