using System.Collections.Generic; using System.IO; using System.Linq; using Newtonsoft.Json; using StardewModdingAPI.Toolkit.Serialization.Models; using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Toolkit.Framework.ModScanning { /// The info about a mod read from its folder. public class ModFolder { /********* ** Fields *********/ /// The backing field for . private DirectoryInfo? DirectoryImpl; /********* ** Accessors *********/ /// A suggested display name for the mod folder. public string DisplayName { get; } /// The folder path containing the mod's manifest.json. public string DirectoryPath { get; } /// The folder containing the mod's manifest.json. [JsonIgnore] public DirectoryInfo Directory => this.DirectoryImpl ??= new DirectoryInfo(this.DirectoryPath); /// The mod type. public ModType Type { get; } /// The mod manifest. public Manifest? Manifest { get; } /// The error which occurred parsing the manifest, if any. public ModParseError ManifestParseError { get; set; } /// A human-readable message for the , if any. public string? ManifestParseErrorText { get; set; } /********* ** Public methods *********/ /// Construct an instance. /// The root folder containing mods. /// The folder containing the mod's manifest.json. /// The mod type. /// The mod manifest. public ModFolder(DirectoryInfo root, DirectoryInfo directory, ModType type, Manifest manifest) : this(root, directory, type, manifest, ModParseError.None, null) { } /// Construct an instance. /// The root folder containing mods. /// The folder containing the mod's manifest.json. /// The mod type. /// The mod manifest. /// The error which occurred parsing the manifest, if any. /// A human-readable message for the , if any. public ModFolder(DirectoryInfo root, DirectoryInfo directory, ModType type, Manifest? manifest, ModParseError manifestParseError, string? manifestParseErrorText) { // save info this.DirectoryImpl = directory; this.DirectoryPath = directory.FullName; this.Type = type; this.Manifest = manifest; this.ManifestParseError = manifestParseError; this.ManifestParseErrorText = manifestParseErrorText; // set display name this.DisplayName = !string.IsNullOrWhiteSpace(manifest?.Name) ? manifest.Name : PathUtilities.GetRelativePath(root.FullName, directory.FullName); } /// Construct an instance. /// A suggested display name for the mod folder. /// The folder path containing the mod's manifest.json. /// The mod type. /// The mod manifest. /// The error which occurred parsing the manifest, if any. /// A human-readable message for the , if any. [JsonConstructor] public ModFolder(string displayName, string directoryPath, ModType type, Manifest? manifest, ModParseError manifestParseError, string? manifestParseErrorText) { this.DisplayName = displayName; this.DirectoryPath = directoryPath; this.Type = type; this.Manifest = manifest; this.ManifestParseError = manifestParseError; this.ManifestParseErrorText = manifestParseErrorText; } /// Get the update keys for a mod. /// The mod manifest. public IEnumerable GetUpdateKeys(Manifest manifest) { return manifest.UpdateKeys .Where(p => !string.IsNullOrWhiteSpace(p)) .ToArray(); } } }