using System.Collections.Generic; 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 { /********* ** Fields *********/ /// The assembly names to which to heuristically detect broken references. private readonly HashSet ValidateReferencesToAssemblies; /********* ** Accessors *********/ /// A brief noun phrase indicating what the instruction finder matches. public string NounPhrase { get; private set; } = ""; /********* ** Public methods *********/ /// Construct an instance. /// The assembly names to which to heuristically detect broken references. public ReferenceToMissingMemberFinder(string[] validateReferencesToAssemblies) { this.ValidateReferencesToAssemblies = new HashSet(validateReferencesToAssemblies); } /// 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 && this.ShouldValidate(fieldRef.DeclaringType)) { 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.ShouldValidate(methodRef.DeclaringType) && !this.IsUnsupported(methodRef)) { MethodDefinition target = methodRef.Resolve(); if (target == null) { if (this.IsProperty(methodRef)) this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name.Substring(4)} (no such property)"; else if (methodRef.Name == ".ctor") this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no matching constructor)"; else this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no such method)"; return InstructionHandleResult.NotCompatible; } } return InstructionHandleResult.None; } /********* ** Private methods *********/ /// Whether references to the given type should be validated. /// The type reference. private bool ShouldValidate(TypeReference type) { return type != null && this.ValidateReferencesToAssemblies.Contains(type.Scope.Name); } /// 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_"); } } }