summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-07-21 01:51:05 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-07-23 12:14:33 -0400
commitc20b21bcaa799a9961799316b7f4b7d8f929ca3f (patch)
tree6455574bc9332c9d9676a572bbfa38e5fe7b5607
parent1bf8e2b95ccb614580da08cded655ffc67b84651 (diff)
downloadSMAPI-c20b21bcaa799a9961799316b7f4b7d8f929ca3f.tar.gz
SMAPI-c20b21bcaa799a9961799316b7f4b7d8f929ca3f.tar.bz2
SMAPI-c20b21bcaa799a9961799316b7f4b7d8f929ca3f.zip
add support for disambiguating IDs in mod compatibility list
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs6
-rw-r--r--src/StardewModdingAPI/Framework/Models/ModCompatibility.cs6
-rw-r--r--src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs57
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs19
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj1
5 files changed, 81 insertions, 8 deletions
diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs
index fdcbdaa7..6b19db5c 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs
@@ -71,9 +71,9 @@ namespace StardewModdingAPI.Framework.ModLoading
compatibility = (
from mod in compatibilityRecords
where
- mod.ID.Contains(key, StringComparer.InvariantCultureIgnoreCase)
- && (mod.LowerVersion == null || !manifest.Version.IsOlderThan(mod.LowerVersion))
- && !manifest.Version.IsNewerThan(mod.UpperVersion)
+ mod.ID.Any(p => p.Matches(key, manifest))
+ && (mod.LowerVersion == null || !manifest.Version.IsOlderThan(mod.LowerVersion))
+ && !manifest.Version.IsNewerThan(mod.UpperVersion)
select mod
).FirstOrDefault();
}
diff --git a/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs b/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs
index 91be1d38..d3a9c533 100644
--- a/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs
+++ b/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs
@@ -9,11 +9,9 @@ namespace StardewModdingAPI.Framework.Models
/*********
** Accessors
*********/
- /****
- ** From config
- ****/
/// <summary>The unique mod IDs.</summary>
- public string[] ID { get; set; }
+ [JsonConverter(typeof(SFieldConverter))]
+ public ModCompatibilityID[] ID { get; set; }
/// <summary>The mod name.</summary>
public string Name { get; set; }
diff --git a/src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs b/src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs
new file mode 100644
index 00000000..98e70116
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs
@@ -0,0 +1,57 @@
+using System;
+using Newtonsoft.Json;
+
+namespace StardewModdingAPI.Framework.Models
+{
+ /// <summary>Uniquely identifies a mod for compatibility checks.</summary>
+ internal class ModCompatibilityID
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The unique mod ID.</summary>
+ public string ID { get; set; }
+
+ /// <summary>The mod name to disambiguate non-unique IDs, or <c>null</c> to ignore the mod name.</summary>
+ public string Name { get; set; }
+
+ /// <summary>The author name to disambiguate non-unique IDs, or <c>null</c> to ignore the author.</summary>
+ public string Author { get; set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ public ModCompatibilityID() { }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="data">The mod ID or a JSON string matching the <see cref="ModCompatibilityID"/> fields.</param>
+ public ModCompatibilityID(string data)
+ {
+ // JSON can be stuffed into the ID string as a convenience hack to keep JSON mod lists
+ // formatted readably. The tradeoff is that the format is a bit more magical, but that's
+ // probably acceptable since players aren't meant to edit it. It's also fairly clear what
+ // the JSON strings do, if not necessarily how.
+ if (data.StartsWith("{"))
+ JsonConvert.PopulateObject(data, this);
+ else
+ this.ID = data;
+ }
+
+ /// <summary>Get whether this ID matches a given mod manifest.</summary>
+ /// <param name="id">The mod's unique ID, or a substitute ID if it isn't set in the manifest.</param>
+ /// <param name="manifest">The manifest to check.</param>
+ public bool Matches(string id, IManifest manifest)
+ {
+ return
+ this.ID.Equals(id, StringComparison.InvariantCultureIgnoreCase)
+ && (
+ this.Author == null
+ || this.Author.Equals(manifest.Author, StringComparison.InvariantCultureIgnoreCase)
+ || (manifest.ExtraFields.ContainsKey("Authour") && this.Author.Equals(manifest.ExtraFields["Authour"].ToString(), StringComparison.InvariantCultureIgnoreCase))
+ )
+ && (this.Name == null || this.Name.Equals(manifest.Name, StringComparison.InvariantCultureIgnoreCase));
+ }
+ }
+}
diff --git a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs
index 9dc62b6a..11ffdccb 100644
--- a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs
+++ b/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs
@@ -24,7 +24,10 @@ namespace StardewModdingAPI.Framework.Serialisation
/// <param name="objectType">The object type.</param>
public override bool CanConvert(Type objectType)
{
- return objectType == typeof(ISemanticVersion) || objectType == typeof(IManifestDependency[]);
+ return
+ objectType == typeof(ISemanticVersion)
+ || objectType == typeof(IManifestDependency[])
+ || objectType == typeof(ModCompatibilityID[]);
}
/// <summary>Reads the JSON representation of the object.</summary>
@@ -83,6 +86,20 @@ namespace StardewModdingAPI.Framework.Serialisation
return result.ToArray();
}
+ // mod compatibility ID
+ if (objectType == typeof(ModCompatibilityID[]))
+ {
+ List<ModCompatibilityID> result = new List<ModCompatibilityID>();
+ foreach (JToken child in JArray.Load(reader).Children())
+ {
+ result.Add(child is JValue value
+ ? new ModCompatibilityID(value.Value<string>())
+ : child.ToObject<ModCompatibilityID>()
+ );
+ }
+ return result.ToArray();
+ }
+
// unknown
throw new NotSupportedException($"Unknown type '{objectType?.FullName}'.");
}
diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj
index 9047aea4..f7992122 100644
--- a/src/StardewModdingAPI/StardewModdingAPI.csproj
+++ b/src/StardewModdingAPI/StardewModdingAPI.csproj
@@ -128,6 +128,7 @@
<Compile Include="Framework\Countdown.cs" />
<Compile Include="Framework\IModMetadata.cs" />
<Compile Include="Framework\Models\DisabledMod.cs" />
+ <Compile Include="Framework\Models\ModCompatibilityID.cs" />
<Compile Include="Framework\ModHelpers\BaseHelper.cs" />
<Compile Include="Framework\ModHelpers\CommandHelper.cs" />
<Compile Include="Framework\ModHelpers\ContentHelper.cs" />