add contentHelper.GetActualAssetKey(..) to support custom map tilesheets (#257)
This commit is contained in:
parent
f52edf9e0b
commit
3cfe14d279
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
|
@ -21,7 +22,7 @@ namespace StardewModdingAPI.Framework
|
||||||
private readonly string ModFolderPath;
|
private readonly string ModFolderPath;
|
||||||
|
|
||||||
/// <summary>The path to the mod's folder, relative to the game's content folder (e.g. "../Mods/ModName").</summary>
|
/// <summary>The path to the mod's folder, relative to the game's content folder (e.g. "../Mods/ModName").</summary>
|
||||||
private readonly string RelativeContentFolder;
|
private readonly string ModFolderPathFromContent;
|
||||||
|
|
||||||
/// <summary>The friendly mod name for use in errors.</summary>
|
/// <summary>The friendly mod name for use in errors.</summary>
|
||||||
private readonly string ModName;
|
private readonly string ModName;
|
||||||
|
@ -39,7 +40,7 @@ namespace StardewModdingAPI.Framework
|
||||||
this.ContentManager = contentManager;
|
this.ContentManager = contentManager;
|
||||||
this.ModFolderPath = modFolderPath;
|
this.ModFolderPath = modFolderPath;
|
||||||
this.ModName = modName;
|
this.ModName = modName;
|
||||||
this.RelativeContentFolder = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath);
|
this.ModFolderPathFromContent = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
|
/// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary>
|
||||||
|
@ -50,13 +51,7 @@ namespace StardewModdingAPI.Framework
|
||||||
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
|
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
|
||||||
public T Load<T>(string key, ContentSource source)
|
public T Load<T>(string key, ContentSource source)
|
||||||
{
|
{
|
||||||
// validate
|
this.AssertValidAssetKeyFormat(key);
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
throw new ArgumentException("The asset key or local path is empty.");
|
|
||||||
if (key.Intersect(Path.GetInvalidPathChars()).Any())
|
|
||||||
throw new ArgumentException("The asset key or local path contains invalid characters.");
|
|
||||||
|
|
||||||
// load content
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
switch (source)
|
switch (source)
|
||||||
|
@ -72,16 +67,14 @@ namespace StardewModdingAPI.Framework
|
||||||
if (!file.Exists)
|
if (!file.Exists)
|
||||||
throw new ContentLoadException($"There is no file at path '{file.FullName}'.");
|
throw new ContentLoadException($"There is no file at path '{file.FullName}'.");
|
||||||
|
|
||||||
// get content-relative path
|
// get underlying asset key
|
||||||
string contentPath = Path.Combine(this.RelativeContentFolder, key);
|
string actualKey = this.GetActualAssetKey(key, source);
|
||||||
if (contentPath.EndsWith(".xnb"))
|
|
||||||
contentPath = contentPath.Substring(0, contentPath.Length - 4);
|
|
||||||
|
|
||||||
// load content
|
// load content
|
||||||
switch (file.Extension.ToLower())
|
switch (file.Extension.ToLower())
|
||||||
{
|
{
|
||||||
case ".xnb":
|
case ".xnb":
|
||||||
return this.ContentManager.Load<T>(contentPath);
|
return this.ContentManager.Load<T>(actualKey);
|
||||||
|
|
||||||
case ".png":
|
case ".png":
|
||||||
// validate
|
// validate
|
||||||
|
@ -89,15 +82,15 @@ namespace StardewModdingAPI.Framework
|
||||||
throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
|
throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'.");
|
||||||
|
|
||||||
// try cache
|
// try cache
|
||||||
if (this.ContentManager.IsLoaded(contentPath))
|
if (this.ContentManager.IsLoaded(actualKey))
|
||||||
return this.ContentManager.Load<T>(contentPath);
|
return this.ContentManager.Load<T>(actualKey);
|
||||||
|
|
||||||
// fetch & cache
|
// fetch & cache
|
||||||
using (FileStream stream = File.OpenRead(file.FullName))
|
using (FileStream stream = File.OpenRead(file.FullName))
|
||||||
{
|
{
|
||||||
var texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
|
Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream);
|
||||||
texture = this.PremultiplyTransparency(texture);
|
texture = this.PremultiplyTransparency(texture);
|
||||||
this.ContentManager.Inject(contentPath, texture);
|
this.ContentManager.Inject(actualKey, texture);
|
||||||
return (T)(object)texture;
|
return (T)(object)texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,10 +108,44 @@ namespace StardewModdingAPI.Framework
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>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.</summary>
|
||||||
|
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param>
|
||||||
|
/// <param name="source">Where to search for a matching content asset.</param>
|
||||||
|
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
|
||||||
|
public string GetActualAssetKey(string key, ContentSource source)
|
||||||
|
{
|
||||||
|
switch (source)
|
||||||
|
{
|
||||||
|
case ContentSource.GameContent:
|
||||||
|
return this.ContentManager.NormaliseAssetName(key);
|
||||||
|
|
||||||
|
case ContentSource.ModFolder:
|
||||||
|
string contentPath = Path.Combine(this.ModFolderPathFromContent, key);
|
||||||
|
if (contentPath.EndsWith(".xnb"))
|
||||||
|
contentPath = contentPath.Substring(0, contentPath.Length - 4);
|
||||||
|
return this.ContentManager.NormaliseAssetName(contentPath);
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new NotSupportedException($"Unknown content source '{source}'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*********
|
/*********
|
||||||
** Private methods
|
** Private methods
|
||||||
*********/
|
*********/
|
||||||
|
/// <summary>Assert that the given key has a valid format.</summary>
|
||||||
|
/// <param name="key">The asset key to check.</param>
|
||||||
|
/// <exception cref="ArgumentException">The asset key is empty or contains invalid characters.</exception>
|
||||||
|
[SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "Parameter is only used for assertion checks by design.")]
|
||||||
|
private void AssertValidAssetKeyFormat(string key)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(key))
|
||||||
|
throw new ArgumentException("The asset key or local path is empty.");
|
||||||
|
if (key.Intersect(Path.GetInvalidPathChars()).Any())
|
||||||
|
throw new ArgumentException("The asset key or local path contains invalid characters.");
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Get a directory path relative to a given root.</summary>
|
/// <summary>Get a directory path relative to a given root.</summary>
|
||||||
/// <param name="rootPath">The root path from which the path should be relative.</param>
|
/// <param name="rootPath">The root path from which the path should be relative.</param>
|
||||||
/// <param name="targetPath">The target file path.</param>
|
/// <param name="targetPath">The target file path.</param>
|
||||||
|
@ -184,7 +211,7 @@ namespace StardewModdingAPI.Framework
|
||||||
//Game1.graphics.GraphicsDevice.Viewport = originalViewport;
|
//Game1.graphics.GraphicsDevice.Viewport = originalViewport;
|
||||||
|
|
||||||
// store data from render target because the RenderTarget2D is volatile
|
// store data from render target because the RenderTarget2D is volatile
|
||||||
var data = new Color[texture.Width * texture.Height];
|
Color[] data = new Color[texture.Width * texture.Height];
|
||||||
renderTarget.GetData(data);
|
renderTarget.GetData(data);
|
||||||
|
|
||||||
// unset texture from graphic device and set modified data back to it
|
// unset texture from graphic device and set modified data back to it
|
||||||
|
|
|
@ -83,6 +83,26 @@ namespace StardewModdingAPI.Framework
|
||||||
this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic
|
this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Normalise an asset name so it's consistent with the underlying cache.</summary>
|
||||||
|
/// <param name="assetName">The asset key.</param>
|
||||||
|
public string NormaliseAssetName(string assetName)
|
||||||
|
{
|
||||||
|
// ensure name format is consistent
|
||||||
|
string[] parts = assetName.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
assetName = string.Join(SContentManager.PreferredPathSeparator, parts);
|
||||||
|
|
||||||
|
// apply platform normalisation logic
|
||||||
|
return this.NormaliseAssetNameForPlatform(assetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Get whether the content manager has already loaded and cached the given asset.</summary>
|
||||||
|
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
|
||||||
|
public bool IsLoaded(string assetName)
|
||||||
|
{
|
||||||
|
assetName = this.NormaliseAssetName(assetName);
|
||||||
|
return this.IsNormalisedKeyLoaded(assetName);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
|
/// <summary>Load an asset that has been processed by the content pipeline.</summary>
|
||||||
/// <typeparam name="T">The type of asset to load.</typeparam>
|
/// <typeparam name="T">The type of asset to load.</typeparam>
|
||||||
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
|
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
|
||||||
|
@ -116,31 +136,9 @@ namespace StardewModdingAPI.Framework
|
||||||
this.Cache[assetName] = value;
|
this.Cache[assetName] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Get whether the content manager has already loaded and cached the given asset.</summary>
|
|
||||||
/// <param name="assetName">The asset path relative to the loader root directory, not including the <c>.xnb</c> extension.</param>
|
|
||||||
public bool IsLoaded(string assetName)
|
|
||||||
{
|
|
||||||
assetName = this.NormaliseAssetName(assetName);
|
|
||||||
return this.IsNormalisedKeyLoaded(assetName);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*********
|
/*********
|
||||||
** Private methods
|
** Private methods
|
||||||
*********/
|
*********/
|
||||||
/// <summary>Normalise an asset name so it's consistent with the underlying cache.</summary>
|
|
||||||
/// <param name="assetName">The asset key.</param>
|
|
||||||
private string NormaliseAssetName(string assetName)
|
|
||||||
{
|
|
||||||
// ensure name format is consistent
|
|
||||||
string[] parts = assetName.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
|
|
||||||
assetName = string.Join(SContentManager.PreferredPathSeparator, parts);
|
|
||||||
|
|
||||||
// apply platform normalisation logic
|
|
||||||
return this.NormaliseAssetNameForPlatform(assetName);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Get whether an asset has already been loaded.</summary>
|
/// <summary>Get whether an asset has already been loaded.</summary>
|
||||||
/// <param name="normalisedAssetName">The normalised asset name.</param>
|
/// <param name="normalisedAssetName">The normalised asset name.</param>
|
||||||
private bool IsNormalisedKeyLoaded(string normalisedAssetName)
|
private bool IsNormalisedKeyLoaded(string normalisedAssetName)
|
||||||
|
|
|
@ -14,5 +14,11 @@ namespace StardewModdingAPI
|
||||||
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
|
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
|
||||||
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
|
/// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception>
|
||||||
T Load<T>(string key, ContentSource source);
|
T Load<T>(string key, ContentSource source);
|
||||||
|
|
||||||
|
/// <summary>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.</summary>
|
||||||
|
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param>
|
||||||
|
/// <param name="source">Where to search for a matching content asset.</param>
|
||||||
|
/// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception>
|
||||||
|
string GetActualAssetKey(string key, ContentSource source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue