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
*********/
/// The mod's display name.
public string DisplayName { get; }
/// The root path containing mods.
public string RootPath { get; }
/// The mod's full directory path within the .
public string DirectoryPath { get; }
/// The relative to the .
public string RelativeDirectoryPath { get; }
/// The mod manifest.
public IManifest Manifest { get; }
/// Metadata about the mod from SMAPI's internal data (if any).
public ModDataRecordVersionedFields DataRecord { get; }
/// The metadata resolution status.
public ModMetadataStatus Status { get; private set; }
/// Indicates non-error issues with the mod.
public ModWarning Warnings { get; private set; }
/// The reason the metadata is invalid, if any.
public string Error { get; private set; }
/// Whether the mod folder should be ignored. This is true if it was found within a folder whose name starts with a dot.
public bool IsIgnored { get; }
/// The mod instance (if loaded and is false).
public IMod Mod { get; private set; }
/// The content pack instance (if loaded and is true).
public IContentPack ContentPack { get; private set; }
/// The translations for this mod (if loaded).
public TranslationHelper Translations { get; private set; }
/// Writes messages to the console and log file as this mod.
public IMonitor Monitor { get; private set; }
/// The mod-provided API (if any).
public object Api { get; private set; }
/// The update-check metadata for this mod (if any).
public ModEntryModel UpdateCheckData { get; private set; }
/// Whether the mod is a content pack.
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;
}
/// Set the mod status.
/// The metadata resolution status.
/// The reason the metadata is invalid, if any.
/// Return the instance for chaining.
public IModMetadata SetStatus(ModMetadataStatus status, string error = null)
{
this.Status = status;
this.Error = error;
return this;
}
/// Set a warning flag for the mod.
/// The warning to set.
public IModMetadata SetWarning(ModWarning warning)
{
this.Warnings |= warning;
return this;
}
/// Set the mod instance.
/// The mod instance to set.
/// The translations for this mod (if loaded).
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;
}
/// Set the mod instance.
/// The contentPack instance to set.
/// Writes messages to the console and log file.
/// The translations for this mod (if loaded).
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;
}
/// Set the mod-provided API instance.
/// The mod-provided API.
public IModMetadata SetApi(object api)
{
this.Api = api;
return this;
}
/// Set the update-check metadata for this mod.
/// The update-check metadata.
public IModMetadata SetUpdateData(ModEntryModel data)
{
this.UpdateCheckData = data;
return this;
}
/// Whether the mod manifest was loaded (regardless of whether the mod itself was loaded).
public bool HasManifest()
{
return this.Manifest != null;
}
/// Whether the mod has an ID (regardless of whether the ID is valid or the mod itself was loaded).
public bool HasID()
{
return
this.HasManifest()
&& !string.IsNullOrWhiteSpace(this.Manifest.UniqueID);
}
/// Whether the mod has the given ID.
/// The mod ID to check.
public bool HasID(string id)
{
return
this.HasID()
&& string.Equals(this.Manifest.UniqueID.Trim(), id?.Trim(), StringComparison.InvariantCultureIgnoreCase);
}
/// Get the defined update keys.
/// Only return valid update keys.
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;
}
}
/// Get the mod IDs that must be installed to load this mod.
/// Whether to include optional dependencies.
public IEnumerable GetRequiredModIds(bool includeOptional = false)
{
HashSet required = new HashSet(StringComparer.InvariantCultureIgnoreCase);
// 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;
}
/// Whether the mod has at least one valid update key set.
public bool HasValidUpdateKeys()
{
return this.GetUpdateKeys(validOnly: true).Any();
}
/// Get whether the mod has a given warning and it hasn't been suppressed in the .
/// The warning to check.
public bool HasUnsuppressWarning(ModWarning warning)
{
return
this.Warnings.HasFlag(warning)
&& (this.DataRecord?.DataRecord == null || !this.DataRecord.DataRecord.SuppressWarnings.HasFlag(warning));
}
/// Get a relative path which includes the root folder name.
public string GetRelativePathWithRoot()
{
string rootFolderName = Path.GetFileName(this.RootPath) ?? "";
return Path.Combine(rootFolderName, this.RelativeDirectoryPath);
}
}
}