summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-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
4 files changed, 80 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}'.");
}