using System; using System.Collections.Generic; using System.Linq; namespace StardewModdingAPI.Toolkit.Framework.ModData { /// Handles access to SMAPI's internal mod metadata list. public class ModDatabase { /********* ** Fields *********/ /// The underlying mod data records indexed by default display name. private readonly ModDataRecord[] Records; /// Get an update URL for an update key (if valid). private readonly Func GetUpdateUrl; /********* ** Public methods *********/ /// Construct an empty instance. public ModDatabase() : this(Array.Empty(), _ => null) { } /// Construct an instance. /// The underlying mod data records indexed by default display name. /// Get an update URL for an update key (if valid). public ModDatabase(IEnumerable records, Func getUpdateUrl) { this.Records = records.ToArray(); this.GetUpdateUrl = getUpdateUrl; } /// Get all mod data records. public IEnumerable GetAll() { return this.Records; } /// Get a mod data record. /// The unique mod ID. public ModDataRecord? Get(string? modID) { return !string.IsNullOrWhiteSpace(modID) ? this.Records.FirstOrDefault(p => p.HasID(modID)) : null; } /// Get the mod page URL for a mod (if available). /// The unique mod ID. public string? GetModPageUrlFor(string? id) { // get update key ModDataRecord? record = this.Get(id); ModDataField? updateKeyField = record?.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey); if (updateKeyField == null) return null; // get update URL return this.GetUpdateUrl(updateKeyField.Value); } } }