summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-07-06 17:46:04 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-07-06 17:46:04 -0400
commitd928bf188e9ab171223bc07d7209d2887d954642 (patch)
tree66b41d22d97ad2f7e6cfe1773fdf8527abe19b08 /src/StardewModdingAPI/Framework
parente2b9a4bab3e078851a289ad0a19b555dde09308e (diff)
downloadSMAPI-d928bf188e9ab171223bc07d7209d2887d954642.tar.gz
SMAPI-d928bf188e9ab171223bc07d7209d2887d954642.tar.bz2
SMAPI-d928bf188e9ab171223bc07d7209d2887d954642.zip
add optional mod dependencies in SMAPI 2.0 (#287)
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r--src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs25
-rw-r--r--src/StardewModdingAPI/Framework/Models/ManifestDependency.cs14
-rw-r--r--src/StardewModdingAPI/Framework/Serialisation/ManifestFieldConverter.cs5
3 files changed, 38 insertions, 6 deletions
diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs
index 9c56aaa4..38dddce7 100644
--- a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs
+++ b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs
@@ -228,13 +228,24 @@ namespace StardewModdingAPI.Framework.ModLoading
from entry in mod.Manifest.Dependencies
let dependencyMod = mods.FirstOrDefault(m => string.Equals(m.Manifest?.UniqueID, entry.UniqueID, StringComparison.InvariantCultureIgnoreCase))
orderby entry.UniqueID
- select new { ID = entry.UniqueID, MinVersion = entry.MinimumVersion, Mod = dependencyMod }
+ select new
+ {
+ ID = entry.UniqueID,
+ MinVersion = entry.MinimumVersion,
+ Mod = dependencyMod,
+ IsRequired =
+#if SMAPI_2_0
+ entry.IsRequired
+#else
+ true
+#endif
+ }
)
.ToArray();
// missing required dependencies, mark failed
{
- string[] failedIDs = (from entry in dependencies where entry.Mod == null select entry.ID).ToArray();
+ string[] failedIDs = (from entry in dependencies where entry.IsRequired && entry.Mod == null select entry.ID).ToArray();
if (failedIDs.Any())
{
sortedMods.Push(mod);
@@ -248,7 +259,7 @@ namespace StardewModdingAPI.Framework.ModLoading
string[] failedLabels =
(
from entry in dependencies
- where entry.MinVersion != null && entry.MinVersion.IsNewerThan(entry.Mod.Manifest.Version)
+ where entry.Mod != null && entry.MinVersion != null && entry.MinVersion.IsNewerThan(entry.Mod.Manifest.Version)
select $"{entry.Mod.DisplayName} (needs {entry.MinVersion} or later)"
)
.ToArray();
@@ -265,11 +276,15 @@ namespace StardewModdingAPI.Framework.ModLoading
states[mod] = ModDependencyStatus.Checking;
// recursively sort dependencies
- IModMetadata[] modsToLoadFirst = dependencies.Select(p => p.Mod).ToArray();
- foreach (IModMetadata requiredMod in modsToLoadFirst)
+ foreach (var dependency in dependencies)
{
+ IModMetadata requiredMod = dependency.Mod;
var subchain = new List<IModMetadata>(currentChain) { mod };
+ // ignore missing optional dependency
+ if (!dependency.IsRequired && requiredMod == null)
+ continue;
+
// detect dependency loop
if (states[requiredMod] == ModDependencyStatus.Checking)
{
diff --git a/src/StardewModdingAPI/Framework/Models/ManifestDependency.cs b/src/StardewModdingAPI/Framework/Models/ManifestDependency.cs
index a0ff0c90..25d92a29 100644
--- a/src/StardewModdingAPI/Framework/Models/ManifestDependency.cs
+++ b/src/StardewModdingAPI/Framework/Models/ManifestDependency.cs
@@ -12,6 +12,10 @@
/// <summary>The minimum required version (if any).</summary>
public ISemanticVersion MinimumVersion { get; set; }
+#if SMAPI_2_0
+ /// <summary>Whether the dependency must be installed to use the mod.</summary>
+ public bool IsRequired { get; set; }
+#endif
/*********
** Public methods
@@ -19,12 +23,20 @@
/// <summary>Construct an instance.</summary>
/// <param name="uniqueID">The unique mod ID to require.</param>
/// <param name="minimumVersion">The minimum required version (if any).</param>
- public ManifestDependency(string uniqueID, string minimumVersion)
+ /// <param name="required">Whether the dependency must be installed to use the mod.</param>
+ public ManifestDependency(string uniqueID, string minimumVersion
+#if SMAPI_2_0
+ , bool required = true
+#endif
+ )
{
this.UniqueID = uniqueID;
this.MinimumVersion = !string.IsNullOrWhiteSpace(minimumVersion)
? new SemanticVersion(minimumVersion)
: null;
+#if SMAPI_2_0
+ this.IsRequired = required;
+#endif
}
}
}
diff --git a/src/StardewModdingAPI/Framework/Serialisation/ManifestFieldConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/ManifestFieldConverter.cs
index e6d62d50..5be0f0b6 100644
--- a/src/StardewModdingAPI/Framework/Serialisation/ManifestFieldConverter.cs
+++ b/src/StardewModdingAPI/Framework/Serialisation/ManifestFieldConverter.cs
@@ -73,7 +73,12 @@ namespace StardewModdingAPI.Framework.Serialisation
{
string uniqueID = obj.Value<string>(nameof(IManifestDependency.UniqueID));
string minVersion = obj.Value<string>(nameof(IManifestDependency.MinimumVersion));
+#if SMAPI_2_0
+ bool required = obj.Value<bool?>(nameof(IManifestDependency.IsRequired)) ?? true;
+ result.Add(new ManifestDependency(uniqueID, minVersion, required));
+#else
result.Add(new ManifestDependency(uniqueID, minVersion));
+#endif
}
return result.ToArray();
}