summaryrefslogtreecommitdiff
path: root/src/SMAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI')
-rw-r--r--src/SMAPI/Framework/Models/ManifestContentPackFor.cs2
-rw-r--r--src/SMAPI/Framework/Serialisation/SemanticVersionConverter.cs40
-rw-r--r--src/SMAPI/Program.cs3
-rw-r--r--src/SMAPI/StardewModdingAPI.csproj1
4 files changed, 44 insertions, 2 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;
+ }
+ }
+}
diff --git a/src/SMAPI/Program.cs b/src/SMAPI/Program.cs
index 05a3134e..cc59c0cd 100644
--- a/src/SMAPI/Program.cs
+++ b/src/SMAPI/Program.cs
@@ -159,7 +159,8 @@ namespace StardewModdingAPI
new StringEnumConverter<SButton>(),
new ColorConverter(),
new PointConverter(),
- new RectangleConverter()
+ new RectangleConverter(),
+ new Framework.Serialisation.SemanticVersionConverter()
};
foreach (JsonConverter converter in converters)
this.JsonHelper.JsonSettings.Converters.Add(converter);
diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj
index 3c953ec5..7eb5f95a 100644
--- a/src/SMAPI/StardewModdingAPI.csproj
+++ b/src/SMAPI/StardewModdingAPI.csproj
@@ -121,6 +121,7 @@
<Compile Include="Framework\Models\ManifestContentPackFor.cs" />
<Compile Include="Framework\Models\ManifestDependency.cs" />
<Compile Include="Framework\ModHelpers\InputHelper.cs" />
+ <Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" />
<Compile Include="IInputHelper.cs" />
<Compile Include="Framework\Input\SInputState.cs" />
<Compile Include="Framework\Input\InputStatus.cs" />