From e447ce225f46f60131519a1ff77dfddf520696bb Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 13 Dec 2018 01:16:38 -0500 Subject: add content pack API --- src/SMAPI/Framework/ModHelpers/CommandHelper.cs | 2 +- .../Framework/ModHelpers/ContentPackHelper.cs | 82 ++++++++++++++++++++++ src/SMAPI/Framework/ModHelpers/ModHelper.cs | 59 +++------------- 3 files changed, 94 insertions(+), 49 deletions(-) create mode 100644 src/SMAPI/Framework/ModHelpers/ContentPackHelper.cs (limited to 'src/SMAPI/Framework/ModHelpers') diff --git a/src/SMAPI/Framework/ModHelpers/CommandHelper.cs b/src/SMAPI/Framework/ModHelpers/CommandHelper.cs index 5a3304f3..a4fd21c1 100644 --- a/src/SMAPI/Framework/ModHelpers/CommandHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/CommandHelper.cs @@ -6,7 +6,7 @@ namespace StardewModdingAPI.Framework.ModHelpers internal class CommandHelper : BaseHelper, ICommandHelper { /********* - ** Accessors + ** Properties *********/ /// The mod using this instance. private readonly IModMetadata Mod; diff --git a/src/SMAPI/Framework/ModHelpers/ContentPackHelper.cs b/src/SMAPI/Framework/ModHelpers/ContentPackHelper.cs new file mode 100644 index 00000000..c4b86cda --- /dev/null +++ b/src/SMAPI/Framework/ModHelpers/ContentPackHelper.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using StardewModdingAPI.Toolkit.Serialisation.Models; + +namespace StardewModdingAPI.Framework.ModHelpers +{ + /// Provides an API for managing content packs. + internal class ContentPackHelper : BaseHelper, IContentPackHelper + { + /********* + ** Properties + *********/ + /// The content packs loaded for this mod. + private readonly Lazy ContentPacks; + + /// Create a temporary content pack. + private readonly Func CreateContentPack; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The unique ID of the relevant mod. + /// The content packs loaded for this mod. + /// Create a temporary content pack. + public ContentPackHelper(string modID, Lazy contentPacks, Func createContentPack) + : base(modID) + { + this.ContentPacks = contentPacks; + this.CreateContentPack = createContentPack; + } + + /// Get all content packs loaded for this mod. + public IEnumerable GetOwned() + { + return this.ContentPacks.Value; + } + + /// Create a temporary content pack to read files from a directory. This will generate fake manifest data; any manifest.json in the directory will be ignored. Temporary content packs will not appear in the SMAPI log and update checks will not be performed. + /// The absolute directory path containing the content pack files. + public IContentPack CreateFake(string directoryPath) + { + string id = Guid.NewGuid().ToString("N"); + return this.CreateFake(directoryPath, id, id, id, id, new SemanticVersion(1, 0, 0)); + } + + /// Create a temporary content pack to read files from a directory. Temporary content packs will not appear in the SMAPI log and update checks will not be performed. + /// The absolute directory path containing the content pack files. + /// The content pack's unique ID. + /// The content pack name. + /// The content pack description. + /// The content pack author's name. + /// The content pack version. + public IContentPack CreateFake(string directoryPath, string id, string name, string description, string author, ISemanticVersion version) + { + // 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( + uniqueID: id, + name: name, + author: author, + description: description, + version: version, + contentPackFor: this.ModID + ); + + // create content pack + return this.CreateContentPack(directoryPath, manifest); + } + } +} diff --git a/src/SMAPI/Framework/ModHelpers/ModHelper.cs b/src/SMAPI/Framework/ModHelpers/ModHelper.cs index 070d9c65..87d405e9 100644 --- a/src/SMAPI/Framework/ModHelpers/ModHelper.cs +++ b/src/SMAPI/Framework/ModHelpers/ModHelper.cs @@ -15,12 +15,6 @@ namespace StardewModdingAPI.Framework.ModHelpers /********* ** Properties *********/ - /// The content packs loaded for this mod. - private readonly Lazy ContentPacks; - - /// Create a transitional content pack. - private readonly Func CreateContentPack; - #if !SMAPI_3_0_STRICT /// Manages deprecation warnings. private readonly DeprecationManager DeprecationManager; @@ -44,6 +38,9 @@ namespace StardewModdingAPI.Framework.ModHelpers /// An API for loading content assets. public IContentHelper Content { get; } + /// An API for managing content packs. + public IContentPackHelper ContentPacks { get; } + /// An API for reading and writing persistent mod data. public IDataHelper Data { get; } @@ -76,18 +73,17 @@ namespace StardewModdingAPI.Framework.ModHelpers /// Manages the game's input state. /// Manages access to events raised by SMAPI. /// An API for loading content assets. + /// An API for managing content packs. /// An API for managing console commands. /// An API for reading and writing persistent mod data. /// an API for fetching metadata about loaded mods. /// An API for accessing private game code. /// Provides multiplayer utilities. /// An API for reading translations stored in the mod's i18n folder. - /// The content packs loaded for this mod. - /// Create a transitional content pack. /// Manages deprecation warnings. /// An argument is null or empty. /// The path does not exist on disk. - public ModHelper(string modID, string modDirectory, JsonHelper jsonHelper, SInputState inputState, IModEvents events, IContentHelper contentHelper, ICommandHelper commandHelper, IDataHelper dataHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, IMultiplayerHelper multiplayer, ITranslationHelper translationHelper, Func contentPacks, Func createContentPack, DeprecationManager deprecationManager) + public ModHelper(string modID, string modDirectory, JsonHelper jsonHelper, SInputState inputState, IModEvents events, IContentHelper contentHelper, IContentPackHelper contentPackHelper, ICommandHelper commandHelper, IDataHelper dataHelper, IModRegistry modRegistry, IReflectionHelper reflectionHelper, IMultiplayerHelper multiplayer, ITranslationHelper translationHelper, DeprecationManager deprecationManager) : base(modID) { // validate directory @@ -99,6 +95,7 @@ namespace StardewModdingAPI.Framework.ModHelpers // initialise this.DirectoryPath = modDirectory; this.Content = contentHelper ?? throw new ArgumentNullException(nameof(contentHelper)); + this.ContentPacks = contentPackHelper ?? throw new ArgumentNullException(nameof(contentPackHelper)); this.Data = dataHelper ?? throw new ArgumentNullException(nameof(dataHelper)); this.Input = new InputHelper(modID, inputState); this.ModRegistry = modRegistry ?? throw new ArgumentNullException(nameof(modRegistry)); @@ -106,8 +103,6 @@ namespace StardewModdingAPI.Framework.ModHelpers this.Reflection = reflectionHelper ?? throw new ArgumentNullException(nameof(reflectionHelper)); this.Multiplayer = multiplayer ?? throw new ArgumentNullException(nameof(multiplayer)); this.Translation = translationHelper ?? throw new ArgumentNullException(nameof(translationHelper)); - this.ContentPacks = new Lazy(contentPacks); - this.CreateContentPack = createContentPack; this.Events = events; #if !SMAPI_3_0_STRICT this.JsonHelper = jsonHelper ?? throw new ArgumentNullException(nameof(jsonHelper)); @@ -171,39 +166,6 @@ namespace StardewModdingAPI.Framework.ModHelpers /**** ** Content packs ****/ - /// Create a temporary content pack to read files from a directory. Temporary content packs will not appear in the SMAPI log and update checks will not be performed. - /// The absolute directory path containing the content pack files. - /// The content pack's unique ID. - /// The content pack name. - /// The content pack description. - /// The content pack author's name. - /// The content pack version. - public IContentPack CreateTemporaryContentPack(string directoryPath, string id, string name, string description, string author, ISemanticVersion version) - { - // 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( - uniqueID: id, - name: name, - author: author, - description: description, - version: version, - contentPackFor: this.ModID - ); - - // create content pack - return this.CreateContentPack(directoryPath, manifest); - } - #if !SMAPI_3_0_STRICT /// 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. /// The absolute directory path containing the content pack files. @@ -212,19 +174,20 @@ namespace StardewModdingAPI.Framework.ModHelpers /// The content pack description. /// The content pack author's name. /// The content pack version. - [Obsolete("Use " + nameof(IModHelper) + "." + nameof(IModHelper.CreateTemporaryContentPack) + " instead")] + [Obsolete("Use " + nameof(IModHelper) + "." + nameof(IModHelper.ContentPacks) + "." + nameof(IContentPackHelper.CreateFake) + " instead")] public IContentPack CreateTransitionalContentPack(string directoryPath, string id, string name, string description, string author, ISemanticVersion version) { this.DeprecationManager.Warn($"{nameof(IModHelper)}.{nameof(IModHelper.CreateTransitionalContentPack)}", "2.5", DeprecationLevel.Notice); - return this.CreateTemporaryContentPack(directoryPath, id, name, description, author, version); + return this.ContentPacks.CreateFake(directoryPath, id, name, description, author, version); } -#endif /// Get all content packs loaded for this mod. + [Obsolete("Use " + nameof(IModHelper) + "." + nameof(IModHelper.ContentPacks) + "." + nameof(IContentPackHelper.GetOwned) + " instead")] public IEnumerable GetContentPacks() { - return this.ContentPacks.Value; + return this.ContentPacks.GetOwned(); } +#endif /**** ** Disposal -- cgit