using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Reflection;
using StardewModdingAPI.Toolkit.Serialization;
using StardewModdingAPI.Toolkit.Utilities;
using StardewValley;
using xTile;
using xTile.Format;
using xTile.ObjectModel;
using xTile.Tiles;
namespace StardewModdingAPI.Framework.ContentManagers
{
/// A content manager which handles reading files from a SMAPI mod folder with support for unpacked files.
internal class ModContentManager : BaseContentManager
{
/*********
** Fields
*********/
/// Encapsulates SMAPI's JSON file parsing.
private readonly JsonHelper JsonHelper;
/// The mod display name to show in errors.
private readonly string ModName;
/// The game content manager used for map tilesheets not provided by the mod.
private readonly IContentManager GameContentManager;
/// The language code for language-agnostic mod assets.
private readonly LanguageCode DefaultLanguage = Constants.DefaultLanguage;
/*********
** Public methods
*********/
/// Construct an instance.
/// A name for the mod manager. Not guaranteed to be unique.
/// The game content manager used for map tilesheets not provided by the mod.
/// The service provider to use to locate services.
/// The mod display name to show in errors.
/// The root directory to search for content.
/// The current culture for which to localize content.
/// The central coordinator which manages content managers.
/// Encapsulates monitoring and logging.
/// Simplifies access to private code.
/// Encapsulates SMAPI's JSON file parsing.
/// A callback to invoke when the content manager is being disposed.
public ModContentManager(string name, IContentManager gameContentManager, IServiceProvider serviceProvider, string modName, string rootDirectory, CultureInfo currentCulture, ContentCoordinator coordinator, IMonitor monitor, Reflector reflection, JsonHelper jsonHelper, Action onDisposing)
: base(name, serviceProvider, rootDirectory, currentCulture, coordinator, monitor, reflection, onDisposing, isNamespaced: true)
{
this.GameContentManager = gameContentManager;
this.JsonHelper = jsonHelper;
this.ModName = modName;
}
/// Load an asset that has been processed by the content pipeline.
/// The type of asset to load.
/// The asset path relative to the loader root directory, not including the .xnb extension.
public override T Load(string assetName)
{
return this.Load(assetName, this.DefaultLanguage, useCache: false);
}
/// Load an asset that has been processed by the content pipeline.
/// The type of asset to load.
/// The asset path relative to the loader root directory, not including the .xnb extension.
/// The language code for which to load content.
public override T Load(string assetName, LanguageCode language)
{
return this.Load(assetName, language, useCache: false);
}
/// Load an asset that has been processed by the content pipeline.
/// The type of asset to load.
/// The asset path relative to the loader root directory, not including the .xnb extension.
/// The language code for which to load content.
/// Whether to read/write the loaded asset to the asset cache.
public override T Load(string assetName, LanguageCode language, bool useCache)
{
assetName = this.AssertAndNormalizeAssetName(assetName);
// disable caching
// This is necessary to avoid assets being shared between content managers, which can
// cause changes to an asset through one content manager affecting the same asset in
// others (or even fresh content managers). See https://www.patreon.com/posts/27247161
// for more background info.
if (useCache)
throw new InvalidOperationException("Mod content managers don't support asset caching.");
// disable language handling
// Mod files don't support automatic translation logic, so this should never happen.
if (language != this.DefaultLanguage)
throw new InvalidOperationException("Localized assets aren't supported by the mod content manager.");
// resolve managed asset key
{
if (this.Coordinator.TryParseManagedAssetKey(assetName, out string contentManagerID, out string relativePath))
{
if (contentManagerID != this.Name)
throw new SContentLoadException($"Can't load managed asset key '{assetName}' through content manager '{this.Name}' for a different mod.");
assetName = relativePath;
}
}
// get local asset
SContentLoadException GetContentError(string reasonPhrase) => new SContentLoadException($"Failed loading asset '{assetName}' from {this.Name}: {reasonPhrase}");
T asset;
try
{
// get file
FileInfo file = this.GetModFile(assetName);
if (!file.Exists)
throw GetContentError("the specified path doesn't exist.");
// load content
switch (file.Extension.ToLower())
{
// XNB file
case ".xnb":
{
asset = this.RawLoad(assetName, useCache: false);
if (asset is Map map)
{
this.NormalizeTilesheetPaths(map);
this.FixCustomTilesheetPaths(map, relativeMapPath: assetName);
}
}
break;
// unpacked data
case ".json":
{
if (!this.JsonHelper.ReadJsonFileIfExists(file.FullName, out asset))
throw GetContentError("the JSON file is invalid."); // should never happen since we check for file existence above
}
break;
// unpacked image
case ".png":
{
// validate
if (typeof(T) != typeof(Texture2D))
throw GetContentError($"can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
// fetch & cache
using FileStream stream = File.OpenRead(file.FullName);
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
texture = this.PremultiplyTransparency(texture);
asset = (T)(object)texture;
}
break;
// unpacked map
case ".tbin":
case ".tmx":
{
// 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.NormalizeTilesheetPaths(map);
this.FixCustomTilesheetPaths(map, relativeMapPath: assetName);
asset = (T)(object)map;
}
break;
default:
throw GetContentError($"unknown file extension '{file.Extension}'; must be one of '.json', '.png', '.tbin', or '.xnb'.");
}
}
catch (Exception ex) when (!(ex is SContentLoadException))
{
if (ex.GetInnermostException() is DllNotFoundException dllEx && dllEx.Message == "libgdiplus.dylib")
throw GetContentError("couldn't find libgdiplus, which is needed to load mod images. Make sure Mono is installed and you're running the game through the normal launcher.");
throw new SContentLoadException($"The content manager failed loading content asset '{assetName}' from {this.Name}.", ex);
}
// track & return asset
this.TrackAsset(assetName, asset, language, useCache);
return asset;
}
/// Create a new content manager for temporary use.
public override LocalizedContentManager CreateTemporary()
{
throw new NotSupportedException("Can't create a temporary mod content manager.");
}
/// Get the underlying key in the game's content cache for an asset. This does not validate whether the asset exists.
/// The local path to a content file relative to the mod folder.
/// The is empty or contains invalid characters.
public string GetInternalAssetKey(string key)
{
FileInfo file = this.GetModFile(key);
string relativePath = PathUtilities.GetRelativePath(this.RootDirectory, file.FullName);
return Path.Combine(this.Name, relativePath);
}
/*********
** Private methods
*********/
/// Get whether an asset has already been loaded.
/// The normalized asset name.
protected override bool IsNormalizedKeyLoaded(string normalizedAssetName)
{
return this.Cache.ContainsKey(normalizedAssetName);
}
/// Get a file from the mod folder.
/// The asset path relative to the content folder.
private FileInfo GetModFile(string path)
{
// try exact match
FileInfo file = new FileInfo(Path.Combine(this.FullRootDirectory, path));
// try with default extension
if (!file.Exists && file.Extension.ToLower() != ".xnb")
{
FileInfo result = new FileInfo(file.FullName + ".xnb");
if (result.Exists)
file = result;
}
return file;
}
/// Premultiply a texture's alpha values to avoid transparency issues in the game.
/// The texture to premultiply.
/// Returns a premultiplied texture.
/// Based on code by David Gouveia.
private Texture2D PremultiplyTransparency(Texture2D texture)
{
// Textures loaded by Texture2D.FromStream are already premultiplied on Linux/Mac, even
// though the XNA documentation explicitly says otherwise. That's a glitch in MonoGame
// fixed in newer versions, but the game uses a bundled version that will always be
// affected. See https://github.com/MonoGame/MonoGame/issues/4820 for more info.
if (Constants.TargetPlatform != GamePlatform.Windows)
return texture;
// premultiply pixels
Color[] data = new Color[texture.Width * texture.Height];
texture.GetData(data);
for (int i = 0; i < data.Length; i++)
{
if (data[i].A == byte.MinValue || data[i].A == byte.MaxValue)
continue; // no need to change fully transparent/opaque pixels
data[i] = Color.FromNonPremultiplied(data[i].ToVector4());
}
texture.SetData(data);
return texture;
}
/// Normalize map tilesheet paths for the current platform.
/// The map whose tilesheets to fix.
private void NormalizeTilesheetPaths(Map map)
{
foreach (TileSheet tilesheet in map.TileSheets)
tilesheet.ImageSource = this.NormalizePathSeparators(tilesheet.ImageSource);
}
/// 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 specialized. 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.AssertAndNormalizeAssetName(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
bool isOutdoors = map.Properties.TryGetValue("Outdoors", out PropertyValue outdoorsProperty) && outdoorsProperty != null;
// fix tilesheets
foreach (TileSheet tilesheet in map.TileSheets)
{
string imageSource = tilesheet.ImageSource;
string errorPrefix = $"{this.ModName} loaded map '{relativeMapPath}' with invalid tilesheet path '{imageSource}'.";
// validate tilesheet path
if (Path.IsPathRooted(imageSource) || PathUtilities.GetSegments(imageSource).Contains(".."))
throw new SContentLoadException($"{errorPrefix} Tilesheet paths must be a relative path without directory climbing (../).");
// load best match
try
{
if (!this.TryGetTilesheetAssetName(relativeMapFolder, imageSource, isOutdoors, out string assetName, out string error))
throw new SContentLoadException($"{errorPrefix} {error}");
tilesheet.ImageSource = assetName;
}
catch (Exception ex) when (!(ex is SContentLoadException))
{
throw new SContentLoadException($"{errorPrefix} The tilesheet couldn't be loaded.", ex);
}
}
}
/// Get the actual asset name for a tilesheet.
/// The folder path containing the map, relative to the mod folder.
/// The tilesheet path to load.
/// Whether the game will apply seasonal logic to the tilesheet.
/// The found asset name.
/// A message indicating why the file couldn't be loaded.
/// Returns whether the asset name was found.
/// See remarks on .
private bool TryGetTilesheetAssetName(string modRelativeMapFolder, string originalPath, bool willSeasonalize, out string assetName, out string error)
{
assetName = null;
error = null;
// nothing to do
if (string.IsNullOrWhiteSpace(originalPath))
{
assetName = originalPath;
return true;
}
// parse path
string filename = Path.GetFileName(originalPath);
bool isSeasonal = filename.StartsWith("spring_", StringComparison.CurrentCultureIgnoreCase)
|| filename.StartsWith("summer_", StringComparison.CurrentCultureIgnoreCase)
|| filename.StartsWith("fall_", StringComparison.CurrentCultureIgnoreCase)
|| filename.StartsWith("winter_", StringComparison.CurrentCultureIgnoreCase);
string relativePath = originalPath;
if (willSeasonalize && isSeasonal)
{
string dirPath = Path.GetDirectoryName(originalPath);
relativePath = Path.Combine(dirPath, $"{Game1.currentSeason}_{filename.Substring(filename.IndexOf("_", StringComparison.CurrentCultureIgnoreCase) + 1)}");
}
// get relative to map file
{
string localKey = Path.Combine(modRelativeMapFolder, relativePath);
if (this.GetModFile(localKey).Exists)
{
assetName = this.GetInternalAssetKey(localKey);
return true;
}
}
// get from game assets
{
string contentKey = Path.Combine("Maps", relativePath);
if (contentKey.EndsWith(".png"))
contentKey = contentKey.Substring(0, contentKey.Length - 4);
try
{
this.GameContentManager.Load(contentKey, this.Language, useCache: true); // no need to bypass cache here, since we're not storing the asset
assetName = contentKey;
return true;
}
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.GetContentFolderFileExists(contentKey))
throw;
}
}
// not found
error = "The tilesheet couldn't be found relative to either map file or the game's content folder.";
return false;
}
/// Get whether a file from the game's content folder exists.
/// The asset key.
private bool GetContentFolderFileExists(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).Exists;
}
}
}