using System;
using System.Collections.Generic;
using StardewModdingAPI.Framework;
namespace StardewModdingAPI.Events
{
/// Events raised when the game loads content.
[Obsolete("This is an undocumented experimental API and may change without warning.")]
public static class ContentEvents
{
/*********
** Properties
*********/
/// Tracks the installed mods.
private static ModRegistry ModRegistry;
/// Encapsulates monitoring and logging.
private static IMonitor Monitor;
/// The mods using the experimental API for which a warning has been raised.
private static readonly HashSet WarnedMods = new HashSet();
/*********
** Events
*********/
/// Raised when an XNB file is being read into the cache. Mods can change the data here before it's cached.
public static event EventHandler AssetLoading;
/*********
** Internal methods
*********/
/// Injects types required for backwards compatibility.
/// Tracks the installed mods.
/// Encapsulates monitoring and logging.
internal static void Shim(ModRegistry modRegistry, IMonitor monitor)
{
ContentEvents.ModRegistry = modRegistry;
ContentEvents.Monitor = monitor;
}
/// Raise an event.
/// Encapsulates monitoring and logging.
/// Encapsulates access and changes to content being read from a data file.
internal static void InvokeAssetLoading(IMonitor monitor, IContentEventHelper contentHelper)
{
// raise warning about experimental API
foreach (Delegate handler in ContentEvents.AssetLoading.GetInvocationList())
{
string modName = ContentEvents.ModRegistry.GetModFrom(handler) ?? "An unknown mod";
if (!ContentEvents.WarnedMods.Contains(modName))
{
ContentEvents.WarnedMods.Add(modName);
ContentEvents.Monitor.Log($"{modName} used the undocumented and experimental content API, which may change or be removed without warning.", LogLevel.Warn);
}
}
// raise event
monitor.SafelyRaiseGenericEvent($"{nameof(ContentEvents)}.{nameof(ContentEvents.AssetLoading)}", ContentEvents.AssetLoading?.GetInvocationList(), null, contentHelper);
}
/// Get whether there are any listeners.
internal static bool HasAssetLoadingListeners()
{
return ContentEvents.AssetLoading != null;
}
}
}