From adf858fde9d3f47223376901fd2ecb2cfd63771d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 19 Aug 2018 22:35:55 -0400 Subject: fix mod build package not parsing some valid manifests (#584) --- .../Framework/ModFileManager.cs | 57 ++-------------------- 1 file changed, 4 insertions(+), 53 deletions(-) (limited to 'src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs') diff --git a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs index f4738d71..7ff66695 100644 --- a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs +++ b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs @@ -3,8 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text.RegularExpressions; -using System.Web.Script.Serialization; -using StardewModdingAPI.Toolkit; +using StardewModdingAPI.Toolkit.Serialisation; +using StardewModdingAPI.Toolkit.Serialisation.Models; namespace StardewModdingAPI.ModBuildConfig.Framework { @@ -107,41 +107,10 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// The manifest is missing or invalid. public string GetManifestVersion() { - // get manifest file - if (!this.Files.TryGetValue(this.ManifestFileName, out FileInfo manifestFile)) + if (!this.Files.TryGetValue(this.ManifestFileName, out FileInfo manifestFile) || !new JsonHelper().ReadJsonFileIfExists(manifestFile.FullName, out Manifest manifest)) throw new InvalidOperationException($"The mod does not have a {this.ManifestFileName} file."); // shouldn't happen since we validate in constructor - // read content - string json = File.ReadAllText(manifestFile.FullName); - if (string.IsNullOrWhiteSpace(json)) - throw new UserErrorException("The mod's manifest must not be empty."); - - // parse JSON - IDictionary data; - try - { - data = this.Parse(json); - } - catch (Exception ex) - { - throw new UserErrorException($"The mod's manifest couldn't be parsed. It doesn't seem to be valid JSON.\n{ex}"); - } - - // get version field - object versionObj = data.ContainsKey("Version") ? data["Version"] : null; - if (versionObj == null) - throw new UserErrorException("The mod's manifest must have a version field."); - - // get version string - if (versionObj is IDictionary versionFields) // SMAPI 1.x - { - int major = versionFields.ContainsKey("MajorVersion") ? (int)versionFields["MajorVersion"] : 0; - int minor = versionFields.ContainsKey("MinorVersion") ? (int)versionFields["MinorVersion"] : 0; - int patch = versionFields.ContainsKey("PatchVersion") ? (int)versionFields["PatchVersion"] : 0; - string tag = versionFields.ContainsKey("Build") ? (string)versionFields["Build"] : null; - return new SemanticVersion(major, minor, patch, tag).ToString(); - } - return new SemanticVersion(versionObj.ToString()).ToString(); // SMAPI 2.0+ + return manifest.Version.ToString(); } @@ -174,24 +143,6 @@ namespace StardewModdingAPI.ModBuildConfig.Framework || ignoreFilePatterns.Any(p => p.IsMatch(relativePath)); } - /// Get a case-insensitive dictionary matching the given JSON. - /// The JSON to parse. - private IDictionary Parse(string json) - { - IDictionary MakeCaseInsensitive(IDictionary dict) - { - foreach (var field in dict.ToArray()) - { - if (field.Value is IDictionary value) - dict[field.Key] = MakeCaseInsensitive(value); - } - return new Dictionary(dict, StringComparer.InvariantCultureIgnoreCase); - } - - IDictionary data = (IDictionary)new JavaScriptSerializer().DeserializeObject(json); - return MakeCaseInsensitive(data); - } - /// Get whether a string is equal to another case-insensitively. /// The string value. /// The string to compare with. -- cgit