From 48833b5c306dfc88669e4cf4fc77640c426c519b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 3 Mar 2018 15:47:29 -0500 Subject: move technical compatibility details into TRACE log (#453) --- src/SMAPI/Framework/ModLoading/AssemblyLoader.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index ccbd053e..7dcf94bf 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -270,9 +270,10 @@ namespace StardewModdingAPI.Framework.ModLoading break; case InstructionHandleResult.NotCompatible: + this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Broken code in {filename}: {handler.NounPhrase}."); if (!assumeCompatible) throw new IncompatibleInstructionException(handler.NounPhrase, $"Found an incompatible CIL instruction ({handler.NounPhrase}) while loading assembly {filename}."); - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found an incompatible CIL instruction ({handler.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn); + this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Found broken code ({handler.NounPhrase}) while loading assembly {filename}, but SMAPI is configured to allow it anyway. The mod may crash or behave unexpectedly.", LogLevel.Warn); break; case InstructionHandleResult.DetectedGamePatch: -- cgit From adebec4dd4d3bf4b45f23377a6ab1525fa2d78ef Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 3 Mar 2018 17:49:24 -0500 Subject: automatically detect broken code (#453) --- src/SMAPI/Framework/ModLoading/AssemblyLoader.cs | 5 +- .../Finders/ReferenceToMissingMemberFinder.cs | 87 ++++++++++++++ .../ReferenceToMemberWithUnexpectedTypeFinder.cs | 131 +++++++++++++++++++++ 3 files changed, 222 insertions(+), 1 deletion(-) create mode 100644 src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs create mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index 7dcf94bf..f0e186a1 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -238,10 +238,13 @@ namespace StardewModdingAPI.Framework.ModLoading // check CIL instructions ILProcessor cil = method.Body.GetILProcessor(); - foreach (Instruction instruction in cil.Body.Instructions.ToArray()) + var instructions = cil.Body.Instructions; + // ReSharper disable once ForCanBeConvertedToForeach -- deliberate access by index so each handler sees replacements from previous handlers + for (int offset = 0; offset < instructions.Count; offset++) { foreach (IInstructionHandler handler in handlers) { + Instruction instruction = instructions[offset]; InstructionHandleResult result = handler.Handle(module, cil, instruction, this.AssemblyMap, platformChanged); this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, assumeCompatible, filename); if (result == InstructionHandleResult.Rewritten) diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs new file mode 100644 index 00000000..60373c9d --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs @@ -0,0 +1,87 @@ +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_"); + } + } +} diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs new file mode 100644 index 00000000..f2080e40 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs @@ -0,0 +1,131 @@ +using System.Linq; +using System.Text.RegularExpressions; +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Finds references to a field, property, or method which returns a different type than the code expects. + /// This implementation is purely heuristic. It should never return a false positive, but won't detect all cases. + internal class ReferenceToMemberWithUnexpectedTypeFinder : IInstructionHandler + { + /********* + ** Properties + *********/ + /// A pattern matching type name substrings to strip for display. + private readonly Regex StripTypeNamePattern = new Regex(@"`\d+(?=<)", RegexOptions.Compiled); + + + /********* + ** 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) + { + // get target field + FieldDefinition targetField = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); + if (targetField == null) + return InstructionHandleResult.None; + + // validate return type + string actualReturnTypeID = this.GetComparableTypeID(targetField.FieldType); + string expectedReturnTypeID = this.GetComparableTypeID(fieldRef.FieldType); + if (actualReturnTypeID != expectedReturnTypeID) + { + this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType, actualReturnTypeID)}, not {this.GetFriendlyTypeName(fieldRef.FieldType, expectedReturnTypeID)})"; + return InstructionHandleResult.NotCompatible; + } + } + + // method reference + MethodReference methodReference = RewriteHelper.AsMethodReference(instruction); + if (methodReference != null) + { + // get potential targets + MethodDefinition[] candidateMethods = methodReference.DeclaringType.Resolve()?.Methods.Where(found => found.Name == methodReference.Name).ToArray(); + if (candidateMethods == null || !candidateMethods.Any()) + return InstructionHandleResult.None; + + // compare return types + MethodDefinition methodDef = methodReference.Resolve(); + if (methodDef == null) + { + this.NounPhrase = $"reference to {methodReference.DeclaringType.FullName}.{methodReference.Name} (no such method)"; + return InstructionHandleResult.NotCompatible; + } + + string expectedReturnType = this.GetComparableTypeID(methodDef.ReturnType); + if (candidateMethods.All(method => this.GetComparableTypeID(method.ReturnType) != expectedReturnType)) + { + this.NounPhrase = $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType, expectedReturnType)})"; + return InstructionHandleResult.NotCompatible; + } + } + + return InstructionHandleResult.None; + } + + + /********* + ** Private methods + *********/ + /// Get a unique string representation of a type. + /// The type reference. + private string GetComparableTypeID(TypeReference type) + { + return this.StripTypeNamePattern.Replace(type.FullName, ""); + } + + /// Get a shorter type name for display. + /// The type reference. + /// The comparable type ID from . + private string GetFriendlyTypeName(TypeReference type, string typeID) + { + // most common built-in types + switch (type.FullName) + { + case "System.Boolean": + return "bool"; + case "System.Int32": + return "int"; + case "System.String": + return "string"; + } + + // most common unambiguous namespaces + foreach (string @namespace in new[] { "Microsoft.Xna.Framework", "Netcode", "System", "System.Collections.Generic" }) + { + if (type.Namespace == @namespace) + return typeID.Substring(@namespace.Length + 1); + } + + return typeID; + } + } +} -- cgit From f9dc901994c1a8b17e22de3f68ceb038965e0cc5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 3 Mar 2018 22:03:41 -0500 Subject: fix error in new incompatibility finders when they resolve members in a dependency (#453) --- .../ModLoading/AssemblyDefinitionResolver.cs | 2 +- src/SMAPI/Framework/ModLoading/AssemblyLoader.cs | 24 ++++++++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs b/src/SMAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs index 4378798c..d85a9a28 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyDefinitionResolver.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using Mono.Cecil; namespace StardewModdingAPI.Framework.ModLoading diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index f0e186a1..a60f63da 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -16,17 +16,20 @@ namespace StardewModdingAPI.Framework.ModLoading /********* ** Properties *********/ + /// Encapsulates monitoring and logging. + private readonly IMonitor Monitor; + + /// Whether to enable developer mode logging. + private readonly bool IsDeveloperMode; + /// Metadata for mapping assemblies to the current platform. private readonly PlatformAssemblyMap AssemblyMap; /// A type => assembly lookup for types which should be rewritten. private readonly IDictionary TypeAssemblies; - /// Encapsulates monitoring and logging. - private readonly IMonitor Monitor; - - /// Whether to enable developer mode logging. - private readonly bool IsDeveloperMode; + /// A minimal assembly definition resolver which resolves references to known loaded assemblies. + private readonly AssemblyDefinitionResolver AssemblyDefinitionResolver; /********* @@ -41,6 +44,7 @@ namespace StardewModdingAPI.Framework.ModLoading this.Monitor = monitor; this.IsDeveloperMode = isDeveloperMode; this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform); + this.AssemblyDefinitionResolver = new AssemblyDefinitionResolver(); // generate type => assembly lookup for types which should be rewritten this.TypeAssemblies = new Dictionary(); @@ -69,9 +73,8 @@ namespace StardewModdingAPI.Framework.ModLoading // get referenced local assemblies AssemblyParseResult[] assemblies; { - AssemblyDefinitionResolver resolver = new AssemblyDefinitionResolver(); HashSet visitedAssemblyNames = new HashSet(AppDomain.CurrentDomain.GetAssemblies().Select(p => p.GetName().Name)); // don't try loading assemblies that are already loaded - assemblies = this.GetReferencedLocalAssemblies(new FileInfo(assemblyPath), visitedAssemblyNames, resolver).ToArray(); + assemblies = this.GetReferencedLocalAssemblies(new FileInfo(assemblyPath), visitedAssemblyNames, this.AssemblyDefinitionResolver).ToArray(); } // validate load @@ -94,7 +97,10 @@ namespace StardewModdingAPI.Framework.ModLoading if (assembly.Status == AssemblyLoadStatus.AlreadyLoaded) continue; + // rewrite assembly bool changed = this.RewriteAssembly(mod, assembly.Definition, assumeCompatible, loggedMessages, logPrefix: " "); + + // load assembly if (changed) { if (!oneAssembly) @@ -112,6 +118,9 @@ namespace StardewModdingAPI.Framework.ModLoading this.Monitor.Log($" Loading {assembly.File.Name}...", LogLevel.Trace); lastAssembly = Assembly.UnsafeLoadFrom(assembly.File.FullName); } + + // track loaded assembly for definition resolution + this.AssemblyDefinitionResolver.Add(assembly.Definition); } // last assembly loaded is the root @@ -166,7 +175,6 @@ namespace StardewModdingAPI.Framework.ModLoading yield return new AssemblyParseResult(file, null, AssemblyLoadStatus.AlreadyLoaded); yield break; } - visitedAssemblyNames.Add(assembly.Name.Name); // yield referenced assemblies -- cgit From 01579d63f37e3f2bd48c6e4502047f894ee4e133 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 4 Mar 2018 00:31:48 -0500 Subject: fix default update key not applied if mod sets a blank update key --- src/SMAPI/Framework/ModData/ModDataField.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModData/ModDataField.cs b/src/SMAPI/Framework/ModData/ModDataField.cs index fa8dd6d0..df906103 100644 --- a/src/SMAPI/Framework/ModData/ModDataField.cs +++ b/src/SMAPI/Framework/ModData/ModDataField.cs @@ -66,7 +66,7 @@ namespace StardewModdingAPI.Framework.ModData { // update key case ModDataFieldKey.UpdateKey: - return manifest.UpdateKeys != null && manifest.UpdateKeys.Any(); + return manifest.UpdateKeys != null && manifest.UpdateKeys.Any(p => !string.IsNullOrWhiteSpace(p)); // non-manifest fields case ModDataFieldKey.AlternativeUrl: -- cgit From 19570f43129dbd3dbfa77a9c62e135a72528a68e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 4 Mar 2018 01:07:55 -0500 Subject: simplify and always include default update URL, shorten no-longer-compatible skip messages --- src/SMAPI/Framework/ModLoading/ModResolver.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/ModResolver.cs b/src/SMAPI/Framework/ModLoading/ModResolver.cs index ba6dab1a..f878a1b9 100644 --- a/src/SMAPI/Framework/ModLoading/ModResolver.cs +++ b/src/SMAPI/Framework/ModLoading/ModResolver.cs @@ -98,7 +98,7 @@ namespace StardewModdingAPI.Framework.ModLoading case ModStatus.AssumeBroken: { // get reason - string reasonPhrase = mod.DataRecord.StatusReasonPhrase ?? "it's no longer compatible"; + string reasonPhrase = mod.DataRecord.StatusReasonPhrase ?? "it's outdated"; // get update URLs List updateUrls = new List(); @@ -111,6 +111,9 @@ namespace StardewModdingAPI.Framework.ModLoading if (mod.DataRecord.AlternativeUrl != null) updateUrls.Add(mod.DataRecord.AlternativeUrl); + // default update URL + updateUrls.Add("https://smapi.io/compat"); + // build error string error = $"{reasonPhrase}. Please check for a "; if (mod.DataRecord.StatusUpperVersion == null || mod.Manifest.Version.Equals(mod.DataRecord.StatusUpperVersion)) -- cgit From 9a9622702ab310261ebedd5e0310f5f40f6812a1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 4 Mar 2018 02:17:44 -0500 Subject: fix misplaced file (#453) --- .../ReferenceToMemberWithUnexpectedTypeFinder.cs | 131 +++++++++++++++++++++ .../ReferenceToMemberWithUnexpectedTypeFinder.cs | 131 --------------------- 2 files changed, 131 insertions(+), 131 deletions(-) create mode 100644 src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs delete mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs new file mode 100644 index 00000000..2aee7af4 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs @@ -0,0 +1,131 @@ +using System.Linq; +using System.Text.RegularExpressions; +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.Framework.ModLoading.Finders +{ + /// Finds references to a field, property, or method which returns a different type than the code expects. + /// This implementation is purely heuristic. It should never return a false positive, but won't detect all cases. + internal class ReferenceToMemberWithUnexpectedTypeFinder : IInstructionHandler + { + /********* + ** Properties + *********/ + /// A pattern matching type name substrings to strip for display. + private readonly Regex StripTypeNamePattern = new Regex(@"`\d+(?=<)", RegexOptions.Compiled); + + + /********* + ** 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) + { + // get target field + FieldDefinition targetField = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); + if (targetField == null) + return InstructionHandleResult.None; + + // validate return type + string actualReturnTypeID = this.GetComparableTypeID(targetField.FieldType); + string expectedReturnTypeID = this.GetComparableTypeID(fieldRef.FieldType); + if (actualReturnTypeID != expectedReturnTypeID) + { + this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType, actualReturnTypeID)}, not {this.GetFriendlyTypeName(fieldRef.FieldType, expectedReturnTypeID)})"; + return InstructionHandleResult.NotCompatible; + } + } + + // method reference + MethodReference methodReference = RewriteHelper.AsMethodReference(instruction); + if (methodReference != null) + { + // get potential targets + MethodDefinition[] candidateMethods = methodReference.DeclaringType.Resolve()?.Methods.Where(found => found.Name == methodReference.Name).ToArray(); + if (candidateMethods == null || !candidateMethods.Any()) + return InstructionHandleResult.None; + + // compare return types + MethodDefinition methodDef = methodReference.Resolve(); + if (methodDef == null) + { + this.NounPhrase = $"reference to {methodReference.DeclaringType.FullName}.{methodReference.Name} (no such method)"; + return InstructionHandleResult.NotCompatible; + } + + string expectedReturnType = this.GetComparableTypeID(methodDef.ReturnType); + if (candidateMethods.All(method => this.GetComparableTypeID(method.ReturnType) != expectedReturnType)) + { + this.NounPhrase = $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType, expectedReturnType)})"; + return InstructionHandleResult.NotCompatible; + } + } + + return InstructionHandleResult.None; + } + + + /********* + ** Private methods + *********/ + /// Get a unique string representation of a type. + /// The type reference. + private string GetComparableTypeID(TypeReference type) + { + return this.StripTypeNamePattern.Replace(type.FullName, ""); + } + + /// Get a shorter type name for display. + /// The type reference. + /// The comparable type ID from . + private string GetFriendlyTypeName(TypeReference type, string typeID) + { + // most common built-in types + switch (type.FullName) + { + case "System.Boolean": + return "bool"; + case "System.Int32": + return "int"; + case "System.String": + return "string"; + } + + // most common unambiguous namespaces + foreach (string @namespace in new[] { "Microsoft.Xna.Framework", "Netcode", "System", "System.Collections.Generic" }) + { + if (type.Namespace == @namespace) + return typeID.Substring(@namespace.Length + 1); + } + + return typeID; + } + } +} diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs deleted file mode 100644 index f2080e40..00000000 --- a/src/SMAPI/Framework/ModLoading/Rewriters/ReferenceToMemberWithUnexpectedTypeFinder.cs +++ /dev/null @@ -1,131 +0,0 @@ -using System.Linq; -using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Rewriters -{ - /// Finds references to a field, property, or method which returns a different type than the code expects. - /// This implementation is purely heuristic. It should never return a false positive, but won't detect all cases. - internal class ReferenceToMemberWithUnexpectedTypeFinder : IInstructionHandler - { - /********* - ** Properties - *********/ - /// A pattern matching type name substrings to strip for display. - private readonly Regex StripTypeNamePattern = new Regex(@"`\d+(?=<)", RegexOptions.Compiled); - - - /********* - ** 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) - { - // get target field - FieldDefinition targetField = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); - if (targetField == null) - return InstructionHandleResult.None; - - // validate return type - string actualReturnTypeID = this.GetComparableTypeID(targetField.FieldType); - string expectedReturnTypeID = this.GetComparableTypeID(fieldRef.FieldType); - if (actualReturnTypeID != expectedReturnTypeID) - { - this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType, actualReturnTypeID)}, not {this.GetFriendlyTypeName(fieldRef.FieldType, expectedReturnTypeID)})"; - return InstructionHandleResult.NotCompatible; - } - } - - // method reference - MethodReference methodReference = RewriteHelper.AsMethodReference(instruction); - if (methodReference != null) - { - // get potential targets - MethodDefinition[] candidateMethods = methodReference.DeclaringType.Resolve()?.Methods.Where(found => found.Name == methodReference.Name).ToArray(); - if (candidateMethods == null || !candidateMethods.Any()) - return InstructionHandleResult.None; - - // compare return types - MethodDefinition methodDef = methodReference.Resolve(); - if (methodDef == null) - { - this.NounPhrase = $"reference to {methodReference.DeclaringType.FullName}.{methodReference.Name} (no such method)"; - return InstructionHandleResult.NotCompatible; - } - - string expectedReturnType = this.GetComparableTypeID(methodDef.ReturnType); - if (candidateMethods.All(method => this.GetComparableTypeID(method.ReturnType) != expectedReturnType)) - { - this.NounPhrase = $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType, expectedReturnType)})"; - return InstructionHandleResult.NotCompatible; - } - } - - return InstructionHandleResult.None; - } - - - /********* - ** Private methods - *********/ - /// Get a unique string representation of a type. - /// The type reference. - private string GetComparableTypeID(TypeReference type) - { - return this.StripTypeNamePattern.Replace(type.FullName, ""); - } - - /// Get a shorter type name for display. - /// The type reference. - /// The comparable type ID from . - private string GetFriendlyTypeName(TypeReference type, string typeID) - { - // most common built-in types - switch (type.FullName) - { - case "System.Boolean": - return "bool"; - case "System.Int32": - return "int"; - case "System.String": - return "string"; - } - - // most common unambiguous namespaces - foreach (string @namespace in new[] { "Microsoft.Xna.Framework", "Netcode", "System", "System.Collections.Generic" }) - { - if (type.Namespace == @namespace) - return typeID.Substring(@namespace.Length + 1); - } - - return typeID; - } - } -} -- cgit From d0b66b13bd828f81048074f442653f5467b9b18f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 4 Mar 2018 02:25:37 -0500 Subject: fix false broken-code detection when referencing a generic type (#453) --- .../Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs index 2aee7af4..0e7344a8 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs @@ -48,6 +48,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); if (fieldRef != null) { + // can't compare generic type parameters between definition and reference + if (fieldRef.FieldType.IsGenericInstance || fieldRef.FieldType.IsGenericParameter) + return InstructionHandleResult.None; + // get target field FieldDefinition targetField = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); if (targetField == null) @@ -67,6 +71,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders MethodReference methodReference = RewriteHelper.AsMethodReference(instruction); if (methodReference != null) { + // can't compare generic type parameters between definition and reference + if (methodReference.ReturnType.IsGenericInstance || methodReference.ReturnType.IsGenericParameter) + return InstructionHandleResult.None; + // get potential targets MethodDefinition[] candidateMethods = methodReference.DeclaringType.Resolve()?.Methods.Where(found => found.Name == methodReference.Name).ToArray(); if (candidateMethods == null || !candidateMethods.Any()) -- cgit From 38ca63a8f60adfa17a9a13ba19fdda070a91aebf Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 4 Mar 2018 14:33:50 -0500 Subject: fix null reference when checking FormerIDs field against 'authour' field --- src/SMAPI/Framework/ModData/ModDatabase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModData/ModDatabase.cs b/src/SMAPI/Framework/ModData/ModDatabase.cs index 332c5c48..3fd68440 100644 --- a/src/SMAPI/Framework/ModData/ModDatabase.cs +++ b/src/SMAPI/Framework/ModData/ModDatabase.cs @@ -157,7 +157,7 @@ namespace StardewModdingAPI.Framework.ModData && ( snapshot.Author == null || snapshot.Author.Equals(manifest.Author, StringComparison.InvariantCultureIgnoreCase) - || (manifest.ExtraFields.ContainsKey("Authour") && snapshot.Author.Equals(manifest.ExtraFields["Authour"].ToString(), StringComparison.InvariantCultureIgnoreCase)) + || (manifest.ExtraFields != null && manifest.ExtraFields.ContainsKey("Authour") && snapshot.Author.Equals(manifest.ExtraFields["Authour"].ToString(), StringComparison.InvariantCultureIgnoreCase)) ) && (snapshot.Name == null || snapshot.Name.Equals(manifest.Name, StringComparison.InvariantCultureIgnoreCase)); -- cgit From 99023f94871e1f9bad5ee5f5db0ff041a02e9ed5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 4 Mar 2018 18:46:05 -0500 Subject: add support for mapping non-semantic remote mod versions --- src/SMAPI/Framework/ModData/ModDataRecord.cs | 11 ++++++++--- src/SMAPI/Framework/ModData/ParsedModDataRecord.cs | 2 +- src/SMAPI/Framework/WebApiClient.cs | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModData/ModDataRecord.cs b/src/SMAPI/Framework/ModData/ModDataRecord.cs index 79a954f7..56275f53 100644 --- a/src/SMAPI/Framework/ModData/ModDataRecord.cs +++ b/src/SMAPI/Framework/ModData/ModDataRecord.cs @@ -106,10 +106,10 @@ namespace StardewModdingAPI.Framework.ModData /// Get a semantic local version for update checks. /// The remote version to normalise. - public string GetLocalVersionForUpdateChecks(string version) + public ISemanticVersion GetLocalVersionForUpdateChecks(ISemanticVersion version) { - return this.MapLocalVersions != null && this.MapLocalVersions.TryGetValue(version, out string newVersion) - ? newVersion + return this.MapLocalVersions != null && this.MapLocalVersions.TryGetValue(version.ToString(), out string newVersion) + ? new SemanticVersion(newVersion) : version; } @@ -117,6 +117,11 @@ namespace StardewModdingAPI.Framework.ModData /// The remote version to normalise. public string GetRemoteVersionForUpdateChecks(string version) { + // normalise version if possible + if (SemanticVersion.TryParse(version, out ISemanticVersion parsed)) + version = parsed.ToString(); + + // fetch remote version return this.MapRemoteVersions != null && this.MapRemoteVersions.TryGetValue(version, out string newVersion) ? newVersion : version; diff --git a/src/SMAPI/Framework/ModData/ParsedModDataRecord.cs b/src/SMAPI/Framework/ModData/ParsedModDataRecord.cs index 7f49790d..deb12bdc 100644 --- a/src/SMAPI/Framework/ModData/ParsedModDataRecord.cs +++ b/src/SMAPI/Framework/ModData/ParsedModDataRecord.cs @@ -33,7 +33,7 @@ namespace StardewModdingAPI.Framework.ModData *********/ /// Get a semantic local version for update checks. /// The remote version to normalise. - public string GetLocalVersionForUpdateChecks(string version) + public ISemanticVersion GetLocalVersionForUpdateChecks(ISemanticVersion version) { return this.DataRecord.GetLocalVersionForUpdateChecks(version); } diff --git a/src/SMAPI/Framework/WebApiClient.cs b/src/SMAPI/Framework/WebApiClient.cs index e78ac14b..7f0122cf 100644 --- a/src/SMAPI/Framework/WebApiClient.cs +++ b/src/SMAPI/Framework/WebApiClient.cs @@ -40,7 +40,7 @@ namespace StardewModdingAPI.Framework { return this.Post>( $"v{this.Version}/mods", - new ModSearchModel(modKeys) + new ModSearchModel(modKeys, allowInvalidVersions: true) ); } -- cgit From 8689fe65642d07fa6a2513aa36c1389479e50d0c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 5 Mar 2018 19:07:22 -0500 Subject: fix compatibility heuristics incorrectly flagging mods with missing optional references (#453) --- .../ReferenceToMemberWithUnexpectedTypeFinder.cs | 22 ++++++++++++++++-- .../Finders/ReferenceToMissingMemberFinder.cs | 26 ++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs index 0e7344a8..b5e45742 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using Mono.Cecil; @@ -12,6 +13,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /********* ** Properties *********/ + /// The assembly names to which to heuristically detect broken references. + private readonly HashSet ValidateReferencesToAssemblies; + /// A pattern matching type name substrings to strip for display. private readonly Regex StripTypeNamePattern = new Regex(@"`\d+(?=<)", RegexOptions.Compiled); @@ -26,6 +30,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /********* ** Public methods *********/ + /// Construct an instance. + /// The assembly names to which to heuristically detect broken references. + public ReferenceToMemberWithUnexpectedTypeFinder(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. @@ -46,7 +57,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders { // field reference FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) + if (fieldRef != null && this.ShouldValidate(fieldRef.DeclaringType)) { // can't compare generic type parameters between definition and reference if (fieldRef.FieldType.IsGenericInstance || fieldRef.FieldType.IsGenericParameter) @@ -69,7 +80,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders // method reference MethodReference methodReference = RewriteHelper.AsMethodReference(instruction); - if (methodReference != null) + if (methodReference != null && this.ShouldValidate(methodReference.DeclaringType)) { // can't compare generic type parameters between definition and reference if (methodReference.ReturnType.IsGenericInstance || methodReference.ReturnType.IsGenericParameter) @@ -103,6 +114,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /********* ** 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 a unique string representation of a type. /// The type reference. private string GetComparableTypeID(TypeReference type) diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs index 60373c9d..f5e33313 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Linq; using Mono.Cecil; using Mono.Cecil.Cil; @@ -8,6 +9,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// This implementation is purely heuristic. It should never return a false positive, but won't detect all cases. internal class ReferenceToMissingMemberFinder : IInstructionHandler { + /********* + ** Properties + *********/ + /// The assembly names to which to heuristically detect broken references. + private readonly HashSet ValidateReferencesToAssemblies; + + /********* ** Accessors *********/ @@ -18,6 +26,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /********* ** 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. @@ -38,7 +53,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders { // field reference FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) + if (fieldRef != null && this.ShouldValidate(fieldRef.DeclaringType)) { FieldDefinition target = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); if (target == null) @@ -50,7 +65,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders // method reference MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (methodRef != null && !this.IsUnsupported(methodRef)) + if (methodRef != null && this.ShouldValidate(methodRef.DeclaringType) && !this.IsUnsupported(methodRef)) { MethodDefinition target = methodRef.DeclaringType.Resolve()?.Methods.FirstOrDefault(p => p.Name == methodRef.Name); if (target == null) @@ -69,6 +84,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /********* ** 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) -- cgit From 41715cefcde3c838bb079cb37aac5a3b2dcb1004 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 11 Mar 2018 19:09:08 -0400 Subject: add initial compatibility with Stardew Valley 1.3 (#453) --- src/SMAPI/Framework/GameVersion.cs | 4 +- src/SMAPI/Framework/SContentManager.cs | 69 ++-- src/SMAPI/Framework/SGame.cs | 631 ++++++++++++++++++++++++++++++++- 3 files changed, 677 insertions(+), 27 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/GameVersion.cs b/src/SMAPI/Framework/GameVersion.cs index 1884afe9..c347ffba 100644 --- a/src/SMAPI/Framework/GameVersion.cs +++ b/src/SMAPI/Framework/GameVersion.cs @@ -23,7 +23,9 @@ namespace StardewModdingAPI.Framework ["1.07"] = "1.0.7", ["1.07a"] = "1.0.8-prerelease1", ["1.08"] = "1.0.8", - ["1.11"] = "1.1.1" + ["1.11"] = "1.1.1", + ["1.3.0.1"] = "1.13-alpha.1", + ["1.3.0.2"] = "1.13-alpha.2" }; diff --git a/src/SMAPI/Framework/SContentManager.cs b/src/SMAPI/Framework/SContentManager.cs index fa51bd53..29c6c684 100644 --- a/src/SMAPI/Framework/SContentManager.cs +++ b/src/SMAPI/Framework/SContentManager.cs @@ -41,11 +41,11 @@ namespace StardewModdingAPI.Framework /// The underlying asset cache. private readonly ContentCache Cache; - /// The private method which generates the locale portion of an asset name. - private readonly IReflectedMethod GetKeyLocale; + /// The locale codes used in asset keys indexed by enum value. + private readonly IDictionary Locales; - /// The language codes used in asset keys. - private readonly IDictionary KeyLocales; + /// The language enum values indexed by locale code. + private readonly IDictionary LanguageCodes; /// Provides metadata for core game assets. private readonly CoreAssets CoreAssets; @@ -95,12 +95,12 @@ namespace StardewModdingAPI.Framework // init this.Monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); this.Cache = new ContentCache(this, reflection); - this.GetKeyLocale = reflection.GetMethod(this, "languageCode"); this.ModContentPrefix = this.GetAssetNameFromFilePath(Constants.ModPath); // get asset data - this.CoreAssets = new CoreAssets(this.NormaliseAssetName); - this.KeyLocales = this.GetKeyLocales(reflection); + this.CoreAssets = new CoreAssets(this.NormaliseAssetName, reflection); + this.Locales = this.GetKeyLocales(reflection); + this.LanguageCodes = this.Locales.ToDictionary(p => p.Value, p => p.Key, StringComparer.InvariantCultureIgnoreCase); } /**** @@ -153,7 +153,7 @@ namespace StardewModdingAPI.Framework /// Get the current content locale. public string GetLocale() { - return this.GetKeyLocale.Invoke(); + return this.Locales[this.GetCurrentLanguage()]; } /// Get whether the content manager has already loaded and cached the given asset. @@ -398,29 +398,48 @@ namespace StardewModdingAPI.Framework /// Get the locale codes (like ja-JP) used in asset keys. /// Simplifies access to private game code. - private IDictionary GetKeyLocales(Reflector reflection) + private IDictionary GetKeyLocales(Reflector reflection) { - // get the private code field directly to avoid changed-code logic +#if !STARDEW_VALLEY_1_3 IReflectedField codeField = reflection.GetField(typeof(LocalizedContentManager), "_currentLangCode"); - - // remember previous settings LanguageCode previousCode = codeField.GetValue(); +#endif string previousOverride = this.LanguageCodeOverride; - // create locale => code map - IDictionary map = new Dictionary(StringComparer.InvariantCultureIgnoreCase); - this.LanguageCodeOverride = null; - foreach (LanguageCode code in Enum.GetValues(typeof(LanguageCode))) + try { - codeField.SetValue(code); - map[this.GetKeyLocale.Invoke()] = code; - } + // temporarily disable language override + this.LanguageCodeOverride = null; + + // create locale => code map + IReflectedMethod languageCodeString = reflection +#if STARDEW_VALLEY_1_3 + .GetMethod(this, "languageCodeString"); +#else + .GetMethod(this, "languageCode"); +#endif + IDictionary map = new Dictionary(); + foreach (LanguageCode code in Enum.GetValues(typeof(LanguageCode))) + { +#if STARDEW_VALLEY_1_3 + map[code] = languageCodeString.Invoke(code); +#else + codeField.SetValue(code); + map[code] = languageCodeString.Invoke(); +#endif + } - // restore previous settings - codeField.SetValue(previousCode); - this.LanguageCodeOverride = previousOverride; + return map; + } + finally + { + // restore previous settings + this.LanguageCodeOverride = previousOverride; +#if !STARDEW_VALLEY_1_3 + codeField.SetValue(previousCode); +#endif - return map; + } } /// Get the asset name from a cache key. @@ -444,7 +463,7 @@ namespace StardewModdingAPI.Framework if (lastSepIndex >= 0) { string suffix = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1); - if (this.KeyLocales.ContainsKey(suffix)) + if (this.LanguageCodes.ContainsKey(suffix)) { assetName = cacheKey.Substring(0, lastSepIndex); localeCode = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1); @@ -466,7 +485,7 @@ namespace StardewModdingAPI.Framework private bool IsNormalisedKeyLoaded(string normalisedAssetName) { return this.Cache.ContainsKey(normalisedAssetName) - || this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke()}"); // translated asset + || this.Cache.ContainsKey($"{normalisedAssetName}.{this.Locales[this.GetCurrentLanguage()]}"); // translated asset } /// Track that a content manager loaded an asset. diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 5c45edca..acb3e794 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -9,6 +9,9 @@ using System.Threading.Tasks; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +#if STARDEW_VALLEY_1_3 +using Netcode; +#endif using StardewModdingAPI.Events; using StardewModdingAPI.Framework.Events; using StardewModdingAPI.Framework.Input; @@ -20,7 +23,9 @@ using StardewValley.Locations; using StardewValley.Menus; using StardewValley.Tools; using xTile.Dimensions; +#if !STARDEW_VALLEY_1_3 using xTile.Layers; + #endif namespace StardewModdingAPI.Framework { @@ -145,6 +150,9 @@ namespace StardewModdingAPI.Framework private readonly Action drawFarmBuildings = () => SGame.Reflection.GetMethod(SGame.Instance, nameof(drawFarmBuildings)).Invoke(); private readonly Action drawHUD = () => SGame.Reflection.GetMethod(SGame.Instance, nameof(drawHUD)).Invoke(); private readonly Action drawDialogueBox = () => SGame.Reflection.GetMethod(SGame.Instance, nameof(drawDialogueBox)).Invoke(); +#if STARDEW_VALLEY_1_3 + private readonly Action drawOverlays = spriteBatch => SGame.Reflection.GetMethod(SGame.Instance, nameof(SGame.drawOverlays)).Invoke(spriteBatch); +#endif private readonly Action renderScreenBuffer = () => SGame.Reflection.GetMethod(SGame.Instance, nameof(renderScreenBuffer)).Invoke(); // ReSharper restore ArrangeStaticMemberQualifier, ArrangeThisQualifier, InconsistentNaming @@ -488,6 +496,7 @@ namespace StardewModdingAPI.Framework if (Context.IsWorldReady) { // raise current location changed + // ReSharper disable once PossibleUnintendedReferenceComparison if (Game1.currentLocation != this.PreviousGameLocation) { if (this.VerboseLogging) @@ -523,7 +532,13 @@ namespace StardewModdingAPI.Framework // raise current location's object list changed if (this.GetHash(Game1.currentLocation.objects) != this.PreviousLocationObjects) - this.Events.Location_LocationObjectsChanged.Raise(new EventArgsLocationObjectsChanged(Game1.currentLocation.objects)); + this.Events.Location_LocationObjectsChanged.Raise(new EventArgsLocationObjectsChanged( +#if STARDEW_VALLEY_1_3 + Game1.currentLocation.objects.FieldDict +#else + Game1.currentLocation.objects +#endif + )); // raise time changed if (Game1.timeOfDay != this.PreviousTime) @@ -650,6 +665,619 @@ namespace StardewModdingAPI.Framework [SuppressMessage("ReSharper", "RedundantCast", Justification = "copied from game code as-is")] [SuppressMessage("ReSharper", "RedundantExplicitNullableCreation", Justification = "copied from game code as-is")] [SuppressMessage("ReSharper", "RedundantTypeArgumentsOfMethod", Justification = "copied from game code as-is")] +#if STARDEW_VALLEY_1_3 + private void DrawImpl(GameTime gameTime) + { + if (Game1.debugMode) + { + if (SGame._fpsStopwatch.IsRunning) + { + float totalSeconds = (float)SGame._fpsStopwatch.Elapsed.TotalSeconds; + SGame._fpsList.Add(totalSeconds); + while (SGame._fpsList.Count >= 120) + SGame._fpsList.RemoveAt(0); + float num = 0.0f; + foreach (float fps in SGame._fpsList) + num += fps; + SGame._fps = (float)(1.0 / ((double)num / (double)SGame._fpsList.Count)); + } + SGame._fpsStopwatch.Restart(); + } + else + { + if (SGame._fpsStopwatch.IsRunning) + SGame._fpsStopwatch.Reset(); + SGame._fps = 0.0f; + SGame._fpsList.Clear(); + } + if (SGame._newDayTask != null) + { + this.GraphicsDevice.Clear(this.bgColor); + //base.Draw(gameTime); + } + else + { + if ((double)Game1.options.zoomLevel != 1.0) + this.GraphicsDevice.SetRenderTarget(this.screenWrapper); + if (this.IsSaving) + { + this.GraphicsDevice.Clear(this.bgColor); + IClickableMenu activeClickableMenu = Game1.activeClickableMenu; + if (activeClickableMenu != null) + { + Game1.spri