summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/ModHelper.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-02-24 17:54:31 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-02-24 17:54:31 -0500
commit414cf5c197b5b59776d3dda914eb15710efb0868 (patch)
tree0393a95194ad78cf4440c68657b0488b7db6d68b /src/SMAPI/Framework/ModHelpers/ModHelper.cs
parent5da8b707385b9851ff3f6442de58519125f5c96f (diff)
parentf2e8450706d1971d774f870081deffdb0c6b92eb (diff)
downloadSMAPI-414cf5c197b5b59776d3dda914eb15710efb0868.tar.gz
SMAPI-414cf5c197b5b59776d3dda914eb15710efb0868.tar.bz2
SMAPI-414cf5c197b5b59776d3dda914eb15710efb0868.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/ModHelpers/ModHelper.cs')
-rw-r--r--src/SMAPI/Framework/ModHelpers/ModHelper.cs74
1 files changed, 70 insertions, 4 deletions
diff --git a/src/SMAPI/Framework/ModHelpers/ModHelper.cs b/src/SMAPI/Framework/ModHelpers/ModHelper.cs
index 665b9cf4..b5758d21 100644
--- a/src/SMAPI/Framework/ModHelpers/ModHelper.cs
+++ b/src/SMAPI/Framework/ModHelpers/ModHelper.cs
@@ -1,6 +1,10 @@
-using System;
+using System;
+using System.Collections.Generic;
using System.IO;
+using System.Linq;
+using StardewModdingAPI.Framework.Models;
using StardewModdingAPI.Framework.Serialisation;
+using StardewModdingAPI.Framework.Utilities;
namespace StardewModdingAPI.Framework.ModHelpers
{
@@ -13,6 +17,15 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <summary>Encapsulates SMAPI's JSON file parsing.</summary>
private readonly JsonHelper JsonHelper;
+ /// <summary>The content packs loaded for this mod.</summary>
+ private readonly IContentPack[] ContentPacks;
+
+ /// <summary>Create a transitional content pack.</summary>
+ private readonly Func<string, IManifest, IContentPack> CreateContentPack;
+
+ /// <summary>Manages deprecation warnings.</summary>
+ private readonly DeprecationManager DeprecationManager;
+
/*********
** Accessors
@@ -48,9 +61,12 @@ namespace StardewModdingAPI.Framework.ModHelpers
/// <param name="modRegistry">an API for fetching metadata about loaded mods.</param>
/// <param name="reflectionHelper">An API for accessing private game code.</param>
/// <param name="translationHelper">An API for reading translations stored in the mod's <c>i18n</c> folder.</param>
+ /// <param name="contentPacks">The content packs loaded for this mod.</param>
+ /// <param name="createContentPack">Create a transitional content pack.</param>
+ /// <param name="deprecationManager">Manages deprecation warnings.</param>
/// <exception cref="ArgumentNullException">An argument is null or empty.</exception>
/// <exception cref="InvalidOperationException">The <paramref name="modDirectory"/> path does not exist on disk.</exception>
- public ModHelper(string modID, string modDirectory, JsonHelper jsonHelper, IContentHelper contentHelper, ICommandHelper commandHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, ITranslationHelper translationHelper)
+ public ModHelper(string modID, string modDirectory, JsonHelper jsonHelper, IContentHelper contentHelper, ICommandHelper commandHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, ITranslationHelper translationHelper, IEnumerable<IContentPack> contentPacks, Func<string, IManifest, IContentPack> createContentPack, DeprecationManager deprecationManager)
: base(modID)
{
// validate directory
@@ -67,6 +83,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
this.ConsoleCommands = commandHelper ?? throw new ArgumentNullException(nameof(commandHelper));
this.Reflection = reflectionHelper ?? throw new ArgumentNullException(nameof(reflectionHelper));
this.Translation = translationHelper ?? throw new ArgumentNullException(nameof(translationHelper));
+ this.ContentPacks = contentPacks.ToArray();
+ this.CreateContentPack = createContentPack;
+ this.DeprecationManager = deprecationManager;
}
/****
@@ -101,7 +120,7 @@ namespace StardewModdingAPI.Framework.ModHelpers
public TModel ReadJsonFile<TModel>(string path)
where TModel : class
{
- path = Path.Combine(this.DirectoryPath, path);
+ path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path));
return this.JsonHelper.ReadJsonFile<TModel>(path);
}
@@ -112,10 +131,57 @@ namespace StardewModdingAPI.Framework.ModHelpers
public void WriteJsonFile<TModel>(string path, TModel model)
where TModel : class
{
- path = Path.Combine(this.DirectoryPath, path);
+ path = Path.Combine(this.DirectoryPath, PathUtilities.NormalisePathSeparators(path));
this.JsonHelper.WriteJsonFile(path, model);
}
+ /****
+ ** Content packs
+ ****/
+ /// <summary>Manually create a transitional content pack to support pre-SMAPI content packs. This provides a way to access legacy content packs using the SMAPI content pack APIs, but the content pack will not be visible in the log or validated by SMAPI.</summary>
+ /// <param name="directoryPath">The absolute directory path containing the content pack files.</param>
+ /// <param name="id">The content pack's unique ID.</param>
+ /// <param name="name">The content pack name.</param>
+ /// <param name="description">The content pack description.</param>
+ /// <param name="author">The content pack author's name.</param>
+ /// <param name="version">The content pack version.</param>
+ [Obsolete("This method supports mods which previously had their own content packs, and shouldn't be used by new mods. It will be removed in SMAPI 3.0.")]
+ public IContentPack CreateTransitionalContentPack(string directoryPath, string id, string name, string description, string author, ISemanticVersion version)
+ {
+ // raise deprecation notice
+ this.DeprecationManager.Warn($"{nameof(IModHelper)}.{nameof(IModHelper.CreateTransitionalContentPack)}", "2.5", DeprecationLevel.Notice);
+
+ // validate
+ if(string.IsNullOrWhiteSpace(directoryPath))
+ throw new ArgumentNullException(nameof(directoryPath));
+ if(string.IsNullOrWhiteSpace(id))
+ throw new ArgumentNullException(nameof(id));
+ if(string.IsNullOrWhiteSpace(name))
+ throw new ArgumentNullException(nameof(name));
+ if(!Directory.Exists(directoryPath))
+ throw new ArgumentException($"Can't create content pack for directory path '{directoryPath}' because no such directory exists.");
+
+ // create manifest
+ IManifest manifest = new Manifest
+ {
+ Name = name,
+ Author = author,
+ Description = description,
+ Version = version,
+ UniqueID = id,
+ UpdateKeys = new string[0],
+ ContentPackFor = new ManifestContentPackFor { UniqueID = this.ModID }
+ };
+
+ // create content pack
+ return this.CreateContentPack(directoryPath, manifest);
+ }
+
+ /// <summary>Get all content packs loaded for this mod.</summary>
+ public IEnumerable<IContentPack> GetContentPacks()
+ {
+ return this.ContentPacks;
+ }
/****
** Disposal