using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.ContentManagers;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Toolkit.Utilities;
using StardewValley;
using xTile;
using xTile.Format;
using xTile.Tiles;
namespace StardewModdingAPI.Framework.ModHelpers
{
/// Provides an API for loading content assets.
internal class ContentHelper : BaseHelper, IContentHelper
{
/*********
** Properties
*********/
/// SMAPI's core content logic.
private readonly ContentCoordinator ContentCore;
/// A content manager for this mod which manages files from the game's Content folder.
private readonly IContentManager GameContentManager;
/// A content manager for this mod which manages files from the mod's folder.
private readonly IContentManager ModContentManager;
/// The absolute path to the mod folder.
private readonly string ModFolderPath;
/// The friendly mod name for use in errors.
private readonly string ModName;
/// Encapsulates monitoring and logging for a given module.
private readonly IMonitor Monitor;
/*********
** Accessors
*********/
/// The game's current locale code (like pt-BR).
public string CurrentLocale => this.GameContentManager.GetLocale();
/// The game's current locale as an enum value.
public LocalizedContentManager.LanguageCode CurrentLocaleConstant => this.GameContentManager.Language;
/// The observable implementation of .
internal ObservableCollection ObservableAssetEditors { get; } = new ObservableCollection();
/// The observable implementation of .
internal ObservableCollection ObservableAssetLoaders { get; } = new ObservableCollection();
/// Interceptors which provide the initial versions of matching content assets.
public IList AssetLoaders => this.ObservableAssetLoaders;
/// Interceptors which edit matching content assets after they're loaded.
public IList AssetEditors => this.ObservableAssetEditors;
/*********
** Public methods
*********/
/// Construct an instance.
/// SMAPI's core content logic.
/// The absolute path to the mod folder.
/// The unique ID of the relevant mod.
/// The friendly mod name for use in errors.
/// Encapsulates monitoring and logging.
public ContentHelper(ContentCoordinator contentCore, string modFolderPath, string modID, string modName, IMonitor monitor)
: base(modID)
{
this.ContentCore = contentCore;
this.GameContentManager = contentCore.CreateGameContentManager(this.ContentCore.GetManagedAssetPrefix(modID) + ".content");
this.ModContentManager = contentCore.CreateModContentManager(this.ContentCore.GetManagedAssetPrefix(modID), rootDirectory: modFolderPath);
this.ModFolderPath = modFolderPath;
this.ModName = modName;
this.Monitor = monitor;
}
/// Load content from the game folder or mod folder (if not already cached), and return it. When loading a .png file, this must be called outside the game's draw loop.
/// The expected data type. The main supported types are , , and dictionaries; other types may be supported by the game's content pipeline.
/// The asset key to fetch (if the is ), or the local path to a content file relative to the mod folder.
/// Where to search for a matching content asset.
/// The is empty or contains invalid characters.
/// The content asset couldn't be loaded (e.g. because it doesn't exist).
public T Load(string key, ContentSource source = ContentSource.ModFolder)
{
SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}: {reasonPhrase}.");
try
{
this.AssertAndNormaliseAssetName(key);
switch (source)
{
case ContentSource.GameContent:
return this.GameContentManager.Load(key);
case ContentSource.ModFolder:
// get file
FileInfo file = this.GetModFile(key);
if (!file.Exists)
throw GetContentError($"there's no matching file at path '{file.FullName}'.");
string internalKey = this.GetInternalModAssetKey(file);
// try cache
if (this.ModContentManager.IsLoaded(internalKey))
return this.ModContentManager.Load(internalKey);
// fix map tilesheets
if (file.Extension.ToLower() == ".tbin")
{
// validate
if (typeof(T) != typeof(Map))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Map)}'.");
// fetch & cache
FormatManager formatManager = FormatManager.Instance;
Map map = formatManager.LoadMap(file.FullName);
this.FixCustomTilesheetPaths(map, relativeMapPath: key);
// inject map
this.ModContentManager.Inject(internalKey, map);
return (T)(object)map;
}
// load through content manager
return this.ModContentManager.Load(internalKey);
default:
throw GetContentError($"unknown content source '{source}'.");
}
}
catch (Exception ex) when (!(ex is SContentLoadException))
{
throw new SContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}.", ex);
}
}
/// Normalise an asset name so it's consistent with those generated by the game. This is mainly useful for string comparisons like on generated asset names, and isn't necessary when passing asset names into other content helper methods.
/// The asset key.
[Pure]
public string NormaliseAssetName(string assetName)
{
return this.ModContentManager.AssertAndNormaliseAssetName(assetName);
}
/// Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists.
/// The asset key to fetch (if the is ), or the local path to a content file relative to the mod folder.
/// Where to search for a matching content asset.
/// The is empty or contains invalid characters.
public string GetActualAssetKey(string key, ContentSource source = ContentSource.ModFolder)
{
switch (source)
{
case ContentSource.GameContent:
return this.GameContentManager.AssertAndNormaliseAssetName(key);
case ContentSource.ModFolder:
FileInfo file = this.GetModFile(key);
return this.GetInternalModAssetKey(file);
default:
throw new NotSupportedException($"Unknown content source '{source}'.");
}
}
/// Remove an asset from the content cache so it's reloaded on the next request. This will reload core game assets if needed, but references to the former asset will still show the previous content.
/// The asset key to invalidate in the content folder.
/// The is empty or contains invalid characters.
/// Returns whether the given asset key was cached.
public bool InvalidateCache(string key)
{
string actualKey = this.GetActualAssetKey(key, ContentSource.GameContent);
this.Monitor.Log($"Requested cache invalidation for '{actualKey}'.", LogLevel.Trace);
return this.ContentCore.InvalidateCache(asset => asset.AssetNameEquals(actualKey)).Any();
}
/// Remove all assets of the given type from the cache so they're reloaded on the next request. This can be a very expensive operation and should only be used in very specific cases. This will reload core game assets if needed, but references to the former assets will still show the previous content.
/// The asset type to remove from the cache.
/// Returns whether any assets were invalidated.
public bool InvalidateCache()
{
this.Monitor.Log($"Requested cache invalidation for all assets of type {typeof(T)}. This is an expensive operation and should be avoided if possible.", LogLevel.Trace);
return this.ContentCore.InvalidateCache((key, type) => typeof(T).IsAssignableFrom(type)).Any();
}
/// Remove matching assets from the content cache so they're reloaded on the next request. This will reload core game assets if needed, but references to the former asset will still show the previous content.
/// A predicate matching the assets to invalidate.
/// Returns whether any cache entries were invalidated.
public bool InvalidateCache(Func predicate)
{
this.Monitor.Log("Requested cache invalidation for all assets matching a predicate.", LogLevel.Trace);
return this.ContentCore.InvalidateCache(predicate).Any();
}
/*********
** Private methods
*********/
/// Assert that the given key has a valid format.
/// The asset key to check.
/// The asset key is empty or contains invalid characters.
[SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local", Justification = "Parameter is only used for assertion checks by design.")]
private void AssertAndNormaliseAssetName(string key)
{
this.ModContentManager.AssertAndNormaliseAssetName(key);
if (Path.IsPathRooted(key))
throw new ArgumentException("The asset key must not be an absolute path.");
}
/// Get the internal key in the content cache for a mod asset.
/// The asset file.
private string GetInternalModAssetKey(FileInfo modFile)
{
string relativePath = PathUtilities.GetRelativePath(this.ModFolderPath, modFile.FullName);
return Path.Combine(this.ModContentManager.Name, relativePath);
}
/// Fix custom map tilesheet paths so they can be found by the content manager.
/// The map whose tilesheets to fix.
/// The relative map path within the mod folder.
/// A map tilesheet couldn't be resolved.
///
/// The game's logic for tilesheets in is a bit specialised. It boils
/// down to this:
/// * If the location is indoors or the desert, or the image source contains 'path' or 'object', it's loaded
/// as-is relative to the Content folder.
/// * Else it's loaded from Content\Maps with a seasonal prefix.
///
/// That logic doesn't work well in our case, mainly because we have no location metadata at this point.
/// Instead we use a more heuristic approach: check relative to the map file first, then relative to
/// Content\Maps, then Content. If the image source filename contains a seasonal prefix, try for a
/// seasonal variation and then an exact match.
///
/// While that doesn't exactly match the game logic, it's close enough that it's unlikely to make a difference.
///
private void FixCustomTilesheetPaths(Map map, string relativeMapPath)
{
// get map info
if (!map.TileSheets.Any())
return;
relativeMapPath = this.ModContentManager.AssertAndNormaliseAssetName(relativeMapPath); // Mono's Path.GetDirectoryName doesn't handle Windows dir separators
string relativeMapFolder = Path.GetDirectoryName(relativeMapPath) ?? ""; // folder path containing the map, relative to the mod folder
// fix tilesheets
foreach (TileSheet tilesheet in map.TileSheets)
{
string imageSource = tilesheet.ImageSource;
// validate tilesheet path
if (Path.IsPathRooted(imageSource) || PathUtilities.GetSegments(imageSource).Contains(".."))
throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded. Tilesheet paths must be a relative path without directory climbing (../).");
// get seasonal name (if applicable)
string seasonalImageSource = null;
if (Game1.currentSeason != null)
{
string filename = Path.GetFileName(imageSource) ?? throw new InvalidOperationException($"The '{imageSource}' tilesheet couldn't be loaded: filename is unexpectedly null.");
bool hasSeasonalPrefix =
filename.StartsWith("spring_", StringComparison.CurrentCultureIgnoreCase)
|| filename.StartsWith("summer_", StringComparison.CurrentCultureIgnoreCase)
|| filename.StartsWith("fall_", StringComparison.CurrentCultureIgnoreCase)
|| filename.StartsWith("winter_", StringComparison.CurrentCultureIgnoreCase);
if (hasSeasonalPrefix && !filename.StartsWith(Game1.currentSeason + "_"))
{
string dirPath = imageSource.Substring(0, imageSource.LastIndexOf(filename, StringComparison.CurrentCultureIgnoreCase));
seasonalImageSource = $"{dirPath}{Game1.currentSeason}_{filename.Substring(filename.IndexOf("_", StringComparison.CurrentCultureIgnoreCase) + 1)}";
}
}
// load best match
try
{
string key =
this.GetTilesheetAssetName(relativeMapFolder, seasonalImageSource)
?? this.GetTilesheetAssetName(relativeMapFolder, imageSource);
if (key != null)
{
tilesheet.ImageSource = key;
continue;
}
}
catch (Exception ex)
{
throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded relative to either map file or the game's content folder.", ex);
}
// none found
throw new ContentLoadException($"The '{imageSource}' tilesheet couldn't be loaded relative to either map file or the game's content folder.");
}
}
/// Get the actual asset name for a tilesheet.
/// The folder path containing the map, relative to the mod folder.
/// The tilesheet image source to load.
/// Returns the asset name.
/// See remarks on .
private string GetTilesheetAssetName(string modRelativeMapFolder, string imageSource)
{
if (imageSource == null)
return null;
// check relative to map file
{
string localKey = Path.Combine(modRelativeMapFolder, imageSource);
FileInfo localFile = this.GetModFile(localKey);
if (localFile.Exists)
return this.GetActualAssetKey(localKey);
}
// check relative to content folder
{
foreach (string candidateKey in new[] { imageSource, $@"Maps\{imageSource}" })
{
string contentKey = candidateKey.EndsWith(".png")
? candidateKey.Substring(0, imageSource.Length - 4)
: candidateKey;
try
{
this.Load(contentKey, ContentSource.GameContent);
return contentKey;
}
catch
{
// ignore file-not-found errors
// TODO: while it's useful to suppress an asset-not-found error here to avoid
// confusion, this is a pretty naive approach. Even if the file doesn't exist,
// the file may have been loaded through an IAssetLoader which failed. So even
// if the content file doesn't exist, that doesn't mean the error here is a
// content-not-found error. Unfortunately XNA doesn't provide a good way to
// detect the error type.
if (this.GetContentFolderFile(contentKey).Exists)
throw;
}
}
}
// not found
return null;
}
/// Get a file from the mod folder.
/// The asset path relative to the mod folder.
private FileInfo GetModFile(string path)
{
// try exact match
path = Path.Combine(this.ModFolderPath, this.ModContentManager.NormalisePathSeparators(path));
FileInfo file = new FileInfo(path);
// try with default extension
if (!file.Exists && file.Extension.ToLower() != ".xnb")
{
FileInfo result = new FileInfo(path + ".xnb");
if (result.Exists)
file = result;
}
return file;
}
/// Get a file from the game's content folder.
/// The asset key.
private FileInfo GetContentFolderFile(string key)
{
// get file path
string path = Path.Combine(this.GameContentManager.FullRootDirectory, key);
if (!path.EndsWith(".xnb"))
path += ".xnb";
// get file
return new FileInfo(path);
}
}
}