summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Toolkit')
-rw-r--r--src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs4
-rw-r--r--src/SMAPI.Toolkit/SemanticVersion.cs9
-rw-r--r--src/SMAPI.Toolkit/Serialization/Converters/NonStandardSemanticVersionConverter.cs15
-rw-r--r--src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs9
4 files changed, 32 insertions, 5 deletions
diff --git a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs
index dadb8c10..188db31d 100644
--- a/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs
+++ b/src/SMAPI.Toolkit/Framework/Clients/WebApi/ModEntryVersionModel.cs
@@ -1,3 +1,6 @@
+using Newtonsoft.Json;
+using StardewModdingAPI.Toolkit.Serialization.Converters;
+
namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi
{
/// <summary>Metadata about a version.</summary>
@@ -7,6 +10,7 @@ namespace StardewModdingAPI.Toolkit.Framework.Clients.WebApi
** Accessors
*********/
/// <summary>The version number.</summary>
+ [JsonConverter(typeof(NonStandardSemanticVersionConverter))]
public ISemanticVersion Version { get; set; }
/// <summary>The mod page URL.</summary>
diff --git a/src/SMAPI.Toolkit/SemanticVersion.cs b/src/SMAPI.Toolkit/SemanticVersion.cs
index 5ead6dc8..86db2820 100644
--- a/src/SMAPI.Toolkit/SemanticVersion.cs
+++ b/src/SMAPI.Toolkit/SemanticVersion.cs
@@ -199,18 +199,19 @@ namespace StardewModdingAPI.Toolkit
/// <returns>Returns whether parsing the version succeeded.</returns>
public static bool TryParse(string version, out ISemanticVersion parsed)
{
- return SemanticVersion.TryParseNonStandard(version, out parsed) && !parsed.IsNonStandard();
+ return SemanticVersion.TryParse(version, allowNonStandard: false, out parsed);
}
- /// <summary>Parse a version string without throwing an exception if it fails, including support for non-standard extensions like <see cref="IPlatformSpecificVersion"/>.</summary>
+ /// <summary>Parse a version string without throwing an exception if it fails.</summary>
/// <param name="version">The version string.</param>
+ /// <param name="allowNonStandard">Whether to allow non-standard extensions to semantic versioning.</param>
/// <param name="parsed">The parsed representation.</param>
/// <returns>Returns whether parsing the version succeeded.</returns>
- public static bool TryParseNonStandard(string version, out ISemanticVersion parsed)
+ public static bool TryParse(string version, bool allowNonStandard, out ISemanticVersion parsed)
{
try
{
- parsed = new SemanticVersion(version, true);
+ parsed = new SemanticVersion(version, allowNonStandard);
return true;
}
catch
diff --git a/src/SMAPI.Toolkit/Serialization/Converters/NonStandardSemanticVersionConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/NonStandardSemanticVersionConverter.cs
new file mode 100644
index 00000000..6f870bcf
--- /dev/null
+++ b/src/SMAPI.Toolkit/Serialization/Converters/NonStandardSemanticVersionConverter.cs
@@ -0,0 +1,15 @@
+namespace StardewModdingAPI.Toolkit.Serialization.Converters
+{
+ /// <summary>Handles deserialization of <see cref="ISemanticVersion"/>, allowing for non-standard extensions.</summary>
+ internal class NonStandardSemanticVersionConverter : SemanticVersionConverter
+ {
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ public NonStandardSemanticVersionConverter()
+ {
+ this.AllowNonStandard = true;
+ }
+ }
+}
diff --git a/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs b/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs
index e1b9db1d..3604956b 100644
--- a/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs
+++ b/src/SMAPI.Toolkit/Serialization/Converters/SemanticVersionConverter.cs
@@ -8,6 +8,13 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters
internal class SemanticVersionConverter : JsonConverter
{
/*********
+ ** Fields
+ *********/
+ /// <summary>Whether to allow non-standard extensions to semantic versioning.</summary>
+ protected bool AllowNonStandard { get; set; }
+
+
+ /*********
** Accessors
*********/
/// <summary>Get whether this converter can read JSON.</summary>
@@ -78,7 +85,7 @@ namespace StardewModdingAPI.Toolkit.Serialization.Converters
{
if (string.IsNullOrWhiteSpace(str))
return null;
- if (!SemanticVersion.TryParse(str, out ISemanticVersion version))
+ if (!SemanticVersion.TryParse(str, allowNonStandard: this.AllowNonStandard, 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;
}