using System.Linq; using Mono.Cecil; using Mono.Cecil.Cil; namespace StardewModdingAPI.Framework.ModLoading.Finders { /// Finds references to a field, property, or method which no longer exists. /// This implementation is purely heuristic. It should never return a false positive, but won't detect all cases. internal class ReferenceToMissingMemberFinder : IInstructionHandler { /********* ** Accessors *********/ /// A brief noun phrase indicating what the instruction finder matches. public string NounPhrase { get; private set; } = ""; /********* ** Public methods *********/ /// Perform the predefined logic for a method if applicable. /// The assembly module containing the instruction. /// The method definition containing the instruction. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { return InstructionHandleResult.None; } /// Perform the predefined logic for an instruction if applicable. /// The assembly module containing the instruction. /// The CIL processor. /// The instruction to handle. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. public virtual InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { // field reference FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); if (fieldRef != null) { FieldDefinition target = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); if (target == null) { this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (no such field)"; return InstructionHandleResult.NotCompatible; } } // method reference MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); if (methodRef != null && !this.IsUnsupported(methodRef)) { MethodDefinition target = methodRef.DeclaringType.Resolve()?.Methods.FirstOrDefault(p => p.Name == methodRef.Name); if (target == null) { this.NounPhrase = this.IsProperty(methodRef) ? $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name.Substring(4)} (no such property)" : $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no such method)"; return InstructionHandleResult.NotCompatible; } } return InstructionHandleResult.None; } /********* ** Private methods *********/ /// Get whether a method reference is a special case that's not currently supported (e.g. array methods). /// The method reference. private bool IsUnsupported(MethodReference method) { return method.DeclaringType.Name.Contains("["); // array methods } /// Get whether a method reference is a property getter or setter. /// The method reference. private bool IsProperty(MethodReference method) { return method.Name.StartsWith("get_") || method.Name.StartsWith("set_"); } } }