using System; using System.Collections.Generic; using System.IO; using System.Linq; using StardewModdingAPI.Framework.ModHelpers; using StardewModdingAPI.Toolkit.Framework.Clients.WebApi; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.Framework.ModLoading { /// Metadata for a mod. internal class ModMetadata : IModMetadata { /********* ** Accessors *********/ /// public string DisplayName { get; } /// public string RootPath { get; } /// public string DirectoryPath { get; } /// public string RelativeDirectoryPath { get; } /// public IManifest Manifest { get; } /// public ModDataRecordVersionedFields DataRecord { get; } /// public ModMetadataStatus Status { get; private set; } /// public ModWarning Warnings { get; private set; } /// public string Error { get; private set; } /// public string ErrorDetails { get; private set; } /// public bool IsIgnored { get; } /// public IMod Mod { get; private set; } /// public IContentPack ContentPack { get; private set; } /// public TranslationHelper Translations { get; private set; } /// public IMonitor Monitor { get; private set; } /// public object Api { get; private set; } /// public ModEntryModel UpdateCheckData { get; private set; } /// public bool IsContentPack => this.Manifest?.ContentPackFor != null; /********* ** Public methods *********/ /// Construct an instance. /// The mod's display name. /// The mod's full directory path within the . /// The root path containing mods. /// The mod manifest. /// Metadata about the mod from SMAPI's internal data (if any). /// Whether the mod folder should be ignored. This should be true if it was found within a folder whose name starts with a dot. public ModMetadata(string displayName, string directoryPath, string rootPath, IManifest manifest, ModDataRecordVersionedFields dataRecord, bool isIgnored) { this.DisplayName = displayName; this.DirectoryPath = directoryPath; this.RootPath = rootPath; this.RelativeDirectoryPath = PathUtilities.GetRelativePath(this.RootPath, this.DirectoryPath); this.Manifest = manifest; this.DataRecord = dataRecord; this.IsIgnored = isIgnored; } /// public IModMetadata SetStatus(ModMetadataStatus status, string error = null, string errorDetails = null) { this.Status = status; this.Error = error; this.ErrorDetails = errorDetails; return this; } /// public IModMetadata SetWarning(ModWarning warning) { this.Warnings |= warning; return this; } /// public IModMetadata SetMod(IMod mod, TranslationHelper translations) { if (this.ContentPack != null) throw new InvalidOperationException("A mod can't be both an assembly mod and content pack."); this.Mod = mod; this.Monitor = mod.Monitor; this.Translations = translations; return this; } /// public IModMetadata SetMod(IContentPack contentPack, IMonitor monitor, TranslationHelper translations) { if (this.Mod != null) throw new InvalidOperationException("A mod can't be both an assembly mod and content pack."); this.ContentPack = contentPack; this.Monitor = monitor; this.Translations = translations; return this; } /// public IModMetadata SetApi(object api) { this.Api = api; return this; } /// public IModMetadata SetUpdateData(ModEntryModel data) { this.UpdateCheckData = data; return this; } /// public bool HasManifest() { return this.Manifest != null; } /// public bool HasID() { return this.HasManifest() && !string.IsNullOrWhiteSpace(this.Manifest.UniqueID); } /// public bool HasID(string id) { return this.HasID() && string.Equals(this.Manifest.UniqueID.Trim(), id?.Trim(), StringComparison.OrdinalIgnoreCase); } /// public IEnumerable GetUpdateKeys(bool validOnly = false) { foreach (string rawKey in this.Manifest?.UpdateKeys ?? new string[0]) { UpdateKey updateKey = UpdateKey.Parse(rawKey); if (updateKey.LooksValid || !validOnly) yield return updateKey; } } /// public IEnumerable GetRequiredModIds(bool includeOptional = false) { HashSet required = new HashSet(StringComparer.OrdinalIgnoreCase); // yield dependencies if (this.Manifest?.Dependencies != null) { foreach (var entry in this.Manifest?.Dependencies) { if ((entry.IsRequired || includeOptional) && required.Add(entry.UniqueID)) yield return entry.UniqueID; } } // yield content pack parent if (this.Manifest?.ContentPackFor?.UniqueID != null && required.Add(this.Manifest.ContentPackFor.UniqueID)) yield return this.Manifest.ContentPackFor.UniqueID; } /// public bool HasValidUpdateKeys() { return this.GetUpdateKeys(validOnly: true).Any(); } /// public bool HasUnsuppressedWarnings(params ModWarning[] warnings) { return warnings.Any(warning => this.Warnings.HasFlag(warning) && (this.DataRecord?.DataRecord == null || !this.DataRecord.DataRecord.SuppressWarnings.HasFlag(warning)) ); } /// public string GetRelativePathWithRoot() { string rootFolderName = Path.GetFileName(this.RootPath) ?? ""; return Path.Combine(rootFolderName, this.RelativeDirectoryPath); } } }