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 { /********* ** Fields *********/ /// 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, using randomised manifest fields. 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.CreateTemporary(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 CreateTemporary(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); } } }