using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text;
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; } = 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.NormalizeField(uniqueId);
this.Name = this.NormalizeField(name, replaceSquareBrackets: true);
this.Author = this.NormalizeField(author);
this.Description = this.NormalizeField(description);
this.Version = version;
this.MinimumApiVersion = minimumApiVersion;
this.EntryDll = this.NormalizeField(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 a manifest field to strip newlines, trim whitespace, and optionally strip square brackets.
/// The input to strip.
/// Whether to replace square brackets with round ones. This is used in the mod name to avoid breaking the log format.
#if NET5_0_OR_GREATER
[return: NotNullIfNotNull("input")]
#endif
private string? NormalizeField(string? input, bool replaceSquareBrackets = false)
{
input = input?.Trim();
if (!string.IsNullOrEmpty(input))
{
StringBuilder? builder = null;
for (int i = 0; i < input.Length; i++)
{
switch (input[i])
{
case '\r':
case '\n':
builder ??= new StringBuilder(input);
builder[i] = ' ';
break;
case '[' when replaceSquareBrackets:
builder ??= new StringBuilder(input);
builder[i] = '(';
break;
case ']' when replaceSquareBrackets:
builder ??= new StringBuilder(input);
builder[i] = ')';
break;
}
}
if (builder != null)
input = builder.ToString();
}
return input;
}
}
}