summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Toolkit/Framework/UpdateData
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-09-02 19:06:37 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-09-02 19:06:37 -0400
commit093117d777a84e3f1e3aaa8a1337059fb805a7dd (patch)
tree29b28d2648340d510f1cba1d392c2bfbb3fa4d5f /src/StardewModdingAPI.Toolkit/Framework/UpdateData
parent7cbeb358cad40b2580fa54de8d9f960ef81d502e (diff)
downloadSMAPI-093117d777a84e3f1e3aaa8a1337059fb805a7dd.tar.gz
SMAPI-093117d777a84e3f1e3aaa8a1337059fb805a7dd.tar.bz2
SMAPI-093117d777a84e3f1e3aaa8a1337059fb805a7dd.zip
add update key parsing to toolkit (#592)
Diffstat (limited to 'src/StardewModdingAPI.Toolkit/Framework/UpdateData')
-rw-r--r--src/StardewModdingAPI.Toolkit/Framework/UpdateData/ModRepositoryKey.cs18
-rw-r--r--src/StardewModdingAPI.Toolkit/Framework/UpdateData/UpdateKey.cs65
2 files changed, 83 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.Toolkit/Framework/UpdateData/ModRepositoryKey.cs b/src/StardewModdingAPI.Toolkit/Framework/UpdateData/ModRepositoryKey.cs
new file mode 100644
index 00000000..7ca32f04
--- /dev/null
+++ b/src/StardewModdingAPI.Toolkit/Framework/UpdateData/ModRepositoryKey.cs
@@ -0,0 +1,18 @@
+namespace StardewModdingAPI.Toolkit.Framework.UpdateData
+{
+ /// <summary>A mod repository which SMAPI can check for updates.</summary>
+ public enum ModRepositoryKey
+ {
+ /// <summary>An unknown or invalid mod repository.</summary>
+ Unknown,
+
+ /// <summary>The Chucklefish mod repository.</summary>
+ Chucklefish,
+
+ /// <summary>A GitHub project containing releases.</summary>
+ GitHub,
+
+ /// <summary>The Nexus Mods mod repository.</summary>
+ Nexus
+ }
+}
diff --git a/src/StardewModdingAPI.Toolkit/Framework/UpdateData/UpdateKey.cs b/src/StardewModdingAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
new file mode 100644
index 00000000..49b7a272
--- /dev/null
+++ b/src/StardewModdingAPI.Toolkit/Framework/UpdateData/UpdateKey.cs
@@ -0,0 +1,65 @@
+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);
+ }
+ }
+}