summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-13 18:24:54 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-09-13 18:24:54 -0400
commit6521df7b131924835eb797251c1e956fae0d6e13 (patch)
treeb704dc64b6b6fef72615bac8950d5eff3c80ea89 /src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
parente22a54212182d0adc443ac95bc791e83c90f7e10 (diff)
parentb7b8b001c5c2dc5d2c9fc1347532ca29368c2325 (diff)
downloadSMAPI-6521df7b131924835eb797251c1e956fae0d6e13.tar.gz
SMAPI-6521df7b131924835eb797251c1e956fae0d6e13.tar.bz2
SMAPI-6521df7b131924835eb797251c1e956fae0d6e13.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs')
-rw-r--r--src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
new file mode 100644
index 00000000..865ebcf7
--- /dev/null
+++ b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
@@ -0,0 +1,73 @@
+using System;
+
+namespace StardewModdingAPI.Toolkit.Framework.UpdateData
+{
+ /// <summary>A namespaced mod ID which uniquely identifies a mod within a mod repository.</summary>
+ public class UpdateKey
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The raw update key text.</summary>
+ public string RawText { get; }
+
+ /// <summary>The mod repository containing the mod.</summary>
+ public ModRepositoryKey Repository { get; }
+
+ /// <summary>The mod ID within the repository.</summary>
+ public string ID { get; }
+
+ /// <summary>Whether the update key seems to be valid.</summary>
+ public bool LooksValid { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="rawText">The raw update key text.</param>
+ /// <param name="repository">The mod repository containing the mod.</param>
+ /// <param name="id">The mod ID within the repository.</param>
+ public UpdateKey(string rawText, ModRepositoryKey repository, string id)
+ {
+ this.RawText = rawText;
+ this.Repository = repository;
+ this.ID = id;
+ this.LooksValid =
+ repository != ModRepositoryKey.Unknown
+ && !string.IsNullOrWhiteSpace(id);
+ }
+
+ /// <summary>Parse a raw update key.</summary>
+ /// <param name="raw">The raw update key to parse.</param>
+ public static UpdateKey Parse(string raw)
+ {
+ // split parts
+ string[] parts = raw?.Split(':');
+ if (parts == null || parts.Length != 2)
+ return new UpdateKey(raw, ModRepositoryKey.Unknown, null);
+
+ // extract parts
+ string repositoryKey = parts[0].Trim();
+ string id = parts[1].Trim();
+ if (string.IsNullOrWhiteSpace(id))
+ id = null;
+
+ // parse
+ if (!Enum.TryParse(repositoryKey, true, out ModRepositoryKey repository))
+ return new UpdateKey(raw, ModRepositoryKey.Unknown, id);
+ if (id == null)
+ return new UpdateKey(raw, repository, null);
+
+ return new UpdateKey(raw, repository, id);
+ }
+
+ /// <summary>Get a string that represents the current object.</summary>
+ public override string ToString()
+ {
+ return this.LooksValid
+ ? $"{this.Repository}:{this.ID}"
+ : this.RawText;
+ }
+ }
+}