summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-06-05 23:03:26 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-06-05 23:03:26 -0400
commit6eba10948bf39d5e05505ec060f6920f84610d58 (patch)
tree24fbaade58c3909127353d2d4fd1ce2dadec0727 /src/SMAPI/Framework
parent3c06a496a0255106076b4f6ade3ed9019dbda4f3 (diff)
downloadSMAPI-6eba10948bf39d5e05505ec060f6920f84610d58.tar.gz
SMAPI-6eba10948bf39d5e05505ec060f6920f84610d58.tar.bz2
SMAPI-6eba10948bf39d5e05505ec060f6920f84610d58.zip
fix version parsing issues in new toolkit code (#532)
Diffstat (limited to 'src/SMAPI/Framework')
-rw-r--r--src/SMAPI/Framework/Models/ManifestContentPackFor.cs2
-rw-r--r--src/SMAPI/Framework/Serialisation/SemanticVersionConverter.cs40
2 files changed, 41 insertions, 1 deletions
diff --git a/src/SMAPI/Framework/Models/ManifestContentPackFor.cs b/src/SMAPI/Framework/Models/ManifestContentPackFor.cs
index cdad8893..90e20c6a 100644
--- a/src/SMAPI/Framework/Models/ManifestContentPackFor.cs
+++ b/src/SMAPI/Framework/Models/ManifestContentPackFor.cs
@@ -21,7 +21,7 @@ namespace StardewModdingAPI.Framework.Models
public ManifestContentPackFor(Toolkit.Serialisation.Models.ManifestContentPackFor contentPackFor)
{
this.UniqueID = contentPackFor.UniqueID;
- this.MinimumVersion = new SemanticVersion(contentPackFor.MinimumVersion);
+ this.MinimumVersion = contentPackFor.MinimumVersion != null ? new SemanticVersion(contentPackFor.MinimumVersion) : null;
}
/// <summary>Construct an instance.</summary>
diff --git a/src/SMAPI/Framework/Serialisation/SemanticVersionConverter.cs b/src/SMAPI/Framework/Serialisation/SemanticVersionConverter.cs
new file mode 100644
index 00000000..3e05a440
--- /dev/null
+++ b/src/SMAPI/Framework/Serialisation/SemanticVersionConverter.cs
@@ -0,0 +1,40 @@
+using Newtonsoft.Json.Linq;
+using StardewModdingAPI.Toolkit.Serialisation;
+using StardewModdingAPI.Toolkit.Serialisation.Converters;
+
+namespace StardewModdingAPI.Framework.Serialisation
+{
+ /// <summary>Handles deserialisation of <see cref="ISemanticVersion"/>.</summary>
+ internal class SemanticVersionConverter : SimpleReadOnlyConverter<ISemanticVersion>
+ {
+ /*********
+ ** Protected methods
+ *********/
+ /// <summary>Read a JSON object.</summary>
+ /// <param name="obj">The JSON object to read.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override ISemanticVersion ReadObject(JObject obj, string path)
+ {
+ int major = obj.ValueIgnoreCase<int>("MajorVersion");
+ int minor = obj.ValueIgnoreCase<int>("MinorVersion");
+ int patch = obj.ValueIgnoreCase<int>("PatchVersion");
+ string build = obj.ValueIgnoreCase<string>("Build");
+ if (build == "0")
+ build = null; // '0' from incorrect examples in old SMAPI documentation
+
+ return new SemanticVersion(major, minor, patch, build);
+ }
+
+ /// <summary>Read a JSON string.</summary>
+ /// <param name="str">The JSON string value.</param>
+ /// <param name="path">The path to the current JSON node.</param>
+ protected override ISemanticVersion ReadString(string str, string path)
+ {
+ if (string.IsNullOrWhiteSpace(str))
+ return null;
+ if (!SemanticVersion.TryParse(str, out ISemanticVersion version))
+ throw new SParseException($"Can't parse semantic version from invalid value '{str}', should be formatted like 1.2, 1.2.30, or 1.2.30-beta (path: {path}).");
+ return version;
+ }
+ }
+}