using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json; using StardewModdingAPI.Toolkit.Serialization.Converters; namespace StardewModdingAPI.Toolkit.Serialization.Models { /// A manifest which describes a mod for SMAPI. public class Manifest : IManifest { /********* ** Accessors *********/ /// The mod name. public string Name { get; } /// A brief description of the mod. public string Description { get; } /// The mod author's name. public string Author { get; } /// The mod version. public ISemanticVersion Version { get; } /// The minimum SMAPI version required by this mod, if any. public ISemanticVersion? MinimumApiVersion { get; } /// The name of the DLL in the directory that has the Entry method. Mutually exclusive with . public string? EntryDll { get; } /// The mod which will read this as a content pack. Mutually exclusive with . [JsonConverter(typeof(ManifestContentPackForConverter))] public IManifestContentPackFor? ContentPackFor { get; } /// The other mods that must be loaded before this mod. [JsonConverter(typeof(ManifestDependencyArrayConverter))] public IManifestDependency[] Dependencies { get; } /// The namespaced mod IDs to query for updates (like Nexus:541). public string[] UpdateKeys { get; private set; } /// The unique mod ID. public string UniqueID { get; } /// Any manifest fields which didn't match a valid field. [JsonExtensionData] public IDictionary ExtraFields { get; set; } = new Dictionary(); /********* ** Public methods *********/ /// Construct an instance for a transitional content pack. /// The unique mod ID. /// The mod name. /// The mod author's name. /// A brief description of the mod. /// The mod version. /// The modID which will read this as a content pack. public Manifest(string uniqueID, string name, string author, string description, ISemanticVersion version, string? contentPackFor = null) : this( uniqueId: uniqueID, name: name, author: author, description: description, version: version, minimumApiVersion: null, entryDll: null, contentPackFor: contentPackFor != null ? new ManifestContentPackFor(contentPackFor, null) : null, dependencies: null, updateKeys: null ) { } /// Construct an instance for a transitional content pack. /// The unique mod ID. /// The mod name. /// The mod author's name. /// A brief description of the mod. /// The mod version. /// The minimum SMAPI version required by this mod, if any. /// The name of the DLL in the directory that has the Entry method. Mutually exclusive with . /// The modID which will read this as a content pack. /// The other mods that must be loaded before this mod. /// The namespaced mod IDs to query for updates (like Nexus:541). [JsonConstructor] public Manifest(string uniqueId, string name, string author, string description, ISemanticVersion version, ISemanticVersion? minimumApiVersion, string? entryDll, IManifestContentPackFor? contentPackFor, IManifestDependency[]? dependencies, string[]? updateKeys) { this.UniqueID = this.NormalizeWhitespace(uniqueId); this.Name = this.NormalizeWhitespace(name); this.Author = this.NormalizeWhitespace(author); this.Description = this.NormalizeWhitespace(description); this.Version = version; this.MinimumApiVersion = minimumApiVersion; this.EntryDll = this.NormalizeWhitespace(entryDll); this.ContentPackFor = contentPackFor; this.Dependencies = dependencies ?? Array.Empty(); this.UpdateKeys = updateKeys ?? Array.Empty(); } /// Override the update keys loaded from the mod info. /// The new update keys to set. internal void OverrideUpdateKeys(params string[] updateKeys) { this.UpdateKeys = updateKeys; } /********* ** Private methods *********/ /// Normalize whitespace in a raw string. /// The input to strip. #if NET5_0_OR_GREATER [return: NotNullIfNotNull("input")] #endif private string? NormalizeWhitespace(string? input) { return input ?.Trim() .Replace("\r", "") .Replace("\n", ""); } } }