using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
namespace StardewModdingAPI.Web.Framework.Caching.Wiki
{
/// Manages cached wiki data in-memory.
internal class WikiCacheMemoryRepository : BaseCacheRepository, IWikiCacheRepository
{
/*********
** Fields
*********/
/// The saved wiki metadata.
private CachedWikiMetadata Metadata;
/// The cached wiki data.
private CachedWikiMod[] Mods = new CachedWikiMod[0];
/*********
** Public methods
*********/
/// Get the cached wiki metadata.
/// The fetched metadata.
public bool TryGetWikiMetadata(out CachedWikiMetadata metadata)
{
metadata = this.Metadata;
return metadata != null;
}
/// Get the cached wiki mods.
/// A filter to apply, if any.
public IEnumerable GetWikiMods(Expression> filter = null)
{
return filter != null
? this.Mods.Where(filter.Compile())
: this.Mods.ToArray();
}
/// Save data fetched from the wiki compatibility list.
/// The current stable Stardew Valley version.
/// The current beta Stardew Valley version.
/// The mod data.
/// The stored metadata record.
/// The stored mod records.
public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable mods, out CachedWikiMetadata cachedMetadata, out CachedWikiMod[] cachedMods)
{
this.Metadata = cachedMetadata = new CachedWikiMetadata(stableVersion, betaVersion);
this.Mods = cachedMods = mods.Select(mod => new CachedWikiMod(mod)).ToArray();
}
}
}