Merge branch 'feature/355-asset-cache-invalidation' into develop
This commit is contained in:
commit
586b50dc84
|
@ -99,7 +99,7 @@ namespace StardewModdingAPI
|
|||
|
||||
|
||||
/*********
|
||||
** Protected methods
|
||||
** Internal methods
|
||||
*********/
|
||||
/// <summary>Get metadata for mapping assemblies to the current platform.</summary>
|
||||
/// <param name="targetPlatform">The target game platform.</param>
|
||||
|
@ -220,6 +220,10 @@ namespace StardewModdingAPI
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
/*********
|
||||
** Private methods
|
||||
*********/
|
||||
/// <summary>Get the name of a save directory for the current player.</summary>
|
||||
private static string GetSaveFolderName()
|
||||
{
|
||||
|
|
|
@ -33,6 +33,9 @@ namespace StardewModdingAPI.Framework.ModHelpers
|
|||
/// <summary>The friendly mod name for use in errors.</summary>
|
||||
private readonly string ModName;
|
||||
|
||||
/// <summary>Encapsulates monitoring and logging for a given module.</summary>
|
||||
private readonly IMonitor Monitor;
|
||||
|
||||
|
||||
/*********
|
||||
** Accessors
|
||||
|
@ -58,13 +61,15 @@ namespace StardewModdingAPI.Framework.ModHelpers
|
|||
/// <param name="modFolderPath">The absolute path to the mod folder.</param>
|
||||
/// <param name="modID">The unique ID of the relevant mod.</param>
|
||||
/// <param name="modName">The friendly mod name for use in errors.</param>
|
||||
public ContentHelper(SContentManager contentManager, string modFolderPath, string modID, string modName)
|
||||
/// <param name="monitor">Encapsulates monitoring and logging.</param>
|
||||
public ContentHelper(SContentManager contentManager, string modFolderPath, string modID, string modName, IMonitor monitor)
|
||||
: base(modID)
|
||||
{
|
||||
this.ContentManager = contentManager;
|
||||
this.ModFolderPath = modFolderPath;
|
||||
this.ModName = modName;
|
||||
this.ModFolderPathFromContent = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath);
|
||||
this.Monitor = monitor;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
|
@ -176,6 +181,26 @@ namespace StardewModdingAPI.Framework.ModHelpers
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>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.</summary>
|
||||
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content 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>
|
||||
/// <returns>Returns whether the given asset key was cached.</returns>
|
||||
public bool InvalidateCache(string key, ContentSource source = ContentSource.ModFolder)
|
||||
{
|
||||
this.Monitor.Log($"Requested cache invalidation for '{key}' in {source}.", LogLevel.Trace);
|
||||
string actualKey = this.GetActualAssetKey(key, source);
|
||||
return this.ContentManager.InvalidateCache((otherKey, type) => otherKey.Equals(actualKey, StringComparison.InvariantCultureIgnoreCase));
|
||||
}
|
||||
|
||||
/// <summary>Remove all assets of the given type from the cache so they're reloaded on the next request. <b>This can be a very expensive operation and should only be used in very specific cases.</b> This will reload core game assets if needed, but references to the former assets will still show the previous content.</summary>
|
||||
/// <typeparam name="T">The asset type to remove from the cache.</typeparam>
|
||||
/// <returns>Returns whether any assets were invalidated.</returns>
|
||||
public bool InvalidateCache<T>()
|
||||
{
|
||||
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.ContentManager.InvalidateCache((key, type) => typeof(T).IsAssignableFrom(type));
|
||||
}
|
||||
|
||||
/*********
|
||||
** Private methods
|
||||
|
|
|
@ -5,14 +5,11 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using StardewModdingAPI.AssemblyRewriters;
|
||||
using StardewModdingAPI.Framework.Content;
|
||||
using StardewModdingAPI.Framework.Reflection;
|
||||
using StardewModdingAPI.Metadata;
|
||||
using StardewValley;
|
||||
using StardewValley.BellsAndWhistles;
|
||||
using StardewValley.Objects;
|
||||
using StardewValley.Projectiles;
|
||||
|
||||
namespace StardewModdingAPI.Framework
|
||||
{
|
||||
|
@ -40,6 +37,12 @@ namespace StardewModdingAPI.Framework
|
|||
/// <summary>The private <see cref="LocalizedContentManager"/> method which generates the locale portion of an asset name.</summary>
|
||||
private readonly IPrivateMethod GetKeyLocale;
|
||||
|
||||
/// <summary>The language codes used in asset keys.</summary>
|
||||
private readonly IDictionary<string, LanguageCode> KeyLocales;
|
||||
|
||||
/// <summary>Provides metadata for core game assets.</summary>
|
||||
private readonly CoreAssets CoreAssets;
|
||||
|
||||
|
||||
/*********
|
||||
** Accessors
|
||||
|
@ -86,6 +89,38 @@ namespace StardewModdingAPI.Framework
|
|||
}
|
||||
else
|
||||
this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load<T> logic
|
||||
|
||||
// get asset data
|
||||
this.CoreAssets = new CoreAssets(this.NormaliseAssetName);
|
||||
this.KeyLocales = this.GetKeyLocales(reflection);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>Get the locale codes (like <c>ja-JP</c>) used in asset keys.</summary>
|
||||
/// <param name="reflection">Simplifies access to private game code.</param>
|
||||
private IDictionary<string, LanguageCode> GetKeyLocales(Reflector reflection)
|
||||
{
|
||||
// get the private code field directly to avoid changed-code logic
|
||||
IPrivateField<LanguageCode> codeField = reflection.GetPrivateField<LanguageCode>(typeof(LocalizedContentManager), "_currentLangCode");
|
||||
|
||||
// remember previous settings
|
||||
LanguageCode previousCode = codeField.GetValue();
|
||||
string previousOverride = this.LanguageCodeOverride;
|
||||
|
||||
// create locale => code map
|
||||
IDictionary<string, LanguageCode> map = new Dictionary<string, LanguageCode>(StringComparer.InvariantCultureIgnoreCase);
|
||||
this.LanguageCodeOverride = null;
|
||||
foreach (LanguageCode code in Enum.GetValues(typeof(LanguageCode)))
|
||||
{
|
||||
codeField.SetValue(code);
|
||||
map[this.GetKeyLocale.Invoke<string>()] = code;
|
||||
}
|
||||
|
||||
// restore previous settings
|
||||
codeField.SetValue(previousCode);
|
||||
this.LanguageCodeOverride = previousOverride;
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// <summary>Normalise path separators in a file path. For asset keys, see <see cref="NormaliseAssetName"/> instead.</summary>
|
||||
|
@ -159,54 +194,61 @@ namespace StardewModdingAPI.Framework
|
|||
return this.GetKeyLocale.Invoke<string>();
|
||||
}
|
||||
|
||||
/// <summary>Reset the asset cache and reload the game's static assets.</summary>
|
||||
/// <remarks>This implementation is derived from <see cref="Game1.LoadContent"/>.</remarks>
|
||||
public void Reset()
|
||||
/// <summary>Get the cached asset keys.</summary>
|
||||
public IEnumerable<string> GetAssetKeys()
|
||||
{
|
||||
this.Monitor.Log("Resetting asset cache...", LogLevel.Trace);
|
||||
this.Cache.Clear();
|
||||
IEnumerable<string> GetAllAssetKeys()
|
||||
{
|
||||
foreach (string cacheKey in this.Cache.Keys)
|
||||
{
|
||||
this.ParseCacheKey(cacheKey, out string assetKey, out string _);
|
||||
yield return assetKey;
|
||||
}
|
||||
}
|
||||
|
||||
// from Game1.LoadContent
|
||||
Game1.daybg = this.Load<Texture2D>("LooseSprites\\daybg");
|
||||
Game1.nightbg = this.Load<Texture2D>("LooseSprites\\nightbg");
|
||||
Game1.menuTexture = this.Load<Texture2D>("Maps\\MenuTiles");
|
||||
Game1.lantern = this.Load<Texture2D>("LooseSprites\\Lighting\\lantern");
|
||||
Game1.windowLight = this.Load<Texture2D>("LooseSprites\\Lighting\\windowLight");
|
||||
Game1.sconceLight = this.Load<Texture2D>("LooseSprites\\Lighting\\sconceLight");
|
||||
Game1.cauldronLight = this.Load<Texture2D>("LooseSprites\\Lighting\\greenLight");
|
||||
Game1.indoorWindowLight = this.Load<Texture2D>("LooseSprites\\Lighting\\indoorWindowLight");
|
||||
Game1.shadowTexture = this.Load<Texture2D>("LooseSprites\\shadow");
|
||||
Game1.mouseCursors = this.Load<Texture2D>("LooseSprites\\Cursors");
|
||||
Game1.controllerMaps = this.Load<Texture2D>("LooseSprites\\ControllerMaps");
|
||||
Game1.animations = this.Load<Texture2D>("TileSheets\\animations");
|
||||
Game1.achievements = this.Load<Dictionary<int, string>>("Data\\Achievements");
|
||||
Game1.NPCGiftTastes = this.Load<Dictionary<string, string>>("Data\\NPCGiftTastes");
|
||||
Game1.dialogueFont = this.Load<SpriteFont>("Fonts\\SpriteFont1");
|
||||
Game1.smallFont = this.Load<SpriteFont>("Fonts\\SmallFont");
|
||||
Game1.tinyFont = this.Load<SpriteFont>("Fonts\\tinyFont");
|
||||
Game1.tinyFontBorder = this.Load<SpriteFont>("Fonts\\tinyFontBorder");
|
||||
Game1.objectSpriteSheet = this.Load<Texture2D>("Maps\\springobjects");
|
||||
Game1.cropSpriteSheet = this.Load<Texture2D>("TileSheets\\crops");
|
||||
Game1.emoteSpriteSheet = this.Load<Texture2D>("TileSheets\\emotes");
|
||||
Game1.debrisSpriteSheet = this.Load<Texture2D>("TileSheets\\debris");
|
||||
Game1.bigCraftableSpriteSheet = this.Load<Texture2D>("TileSheets\\Craftables");
|
||||
Game1.rainTexture = this.Load<Texture2D>("TileSheets\\rain");
|
||||
Game1.buffsIcons = this.Load<Texture2D>("TileSheets\\BuffsIcons");
|
||||
Game1.objectInformation = this.Load<Dictionary<int, string>>("Data\\ObjectInformation");
|
||||
Game1.bigCraftablesInformation = this.Load<Dictionary<int, string>>("Data\\BigCraftablesInformation");
|
||||
FarmerRenderer.hairStylesTexture = this.Load<Texture2D>("Characters\\Farmer\\hairstyles");
|
||||
FarmerRenderer.shirtsTexture = this.Load<Texture2D>("Characters\\Farmer\\shirts");
|
||||
FarmerRenderer.hatsTexture = this.Load<Texture2D>("Characters\\Farmer\\hats");
|
||||
FarmerRenderer.accessoriesTexture = this.Load<Texture2D>("Characters\\Farmer\\accessories");
|
||||
Furniture.furnitureTexture = this.Load<Texture2D>("TileSheets\\furniture");
|
||||
SpriteText.spriteTexture = this.Load<Texture2D>("LooseSprites\\font_bold");
|
||||
SpriteText.coloredTexture = this.Load<Texture2D>("LooseSprites\\font_colored");
|
||||
Tool.weaponsTexture = this.Load<Texture2D>("TileSheets\\weapons");
|
||||
Projectile.projectileSheet = this.Load<Texture2D>("TileSheets\\Projectiles");
|
||||
return GetAllAssetKeys().Distinct();
|
||||
}
|
||||
|
||||
// from Farmer constructor
|
||||
if (Game1.player != null)
|
||||
Game1.player.FarmerRenderer = new FarmerRenderer(this.Load<Texture2D>("Characters\\Farmer\\farmer_" + (Game1.player.isMale ? "" : "girl_") + "base"));
|
||||
/// <summary>Reset the asset cache and reload the game's static assets.</summary>
|
||||
/// <param name="predicate">Matches the asset keys to invalidate.</param>
|
||||
/// <returns>Returns whether any cache entries were invalidated.</returns>
|
||||
/// <remarks>This implementation is derived from <see cref="Game1.LoadContent"/>.</remarks>
|
||||
public bool InvalidateCache(Func<string, Type, bool> predicate)
|
||||
{
|
||||
// find matching asset keys
|
||||
HashSet<string> purgeCacheKeys = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
|
||||
HashSet<string> purgeAssetKeys = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
|
||||
foreach (string cacheKey in this.Cache.Keys)
|
||||
{
|
||||
this.ParseCacheKey(cacheKey, out string assetKey, out string localeCode);
|
||||
Type type = this.Cache[cacheKey].GetType();
|
||||
if (predicate(assetKey, type))
|
||||
{
|
||||
purgeAssetKeys.Add(assetKey);
|
||||
purgeCacheKeys.Add(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
// purge from cache
|
||||
foreach (string key in purgeCacheKeys)
|
||||
this.Cache.Remove(key);
|
||||
|
||||
// reload core game assets
|
||||
int reloaded = 0;
|
||||
foreach (string key in purgeAssetKeys)
|
||||
{
|
||||
if(this.CoreAssets.ReloadForKey(this, key))
|
||||
reloaded++;
|
||||
}
|
||||
|
||||
// report result
|
||||
if (purgeCacheKeys.Any())
|
||||
{
|
||||
this.Monitor.Log($"Invalidated {purgeCacheKeys.Count} cache entries for {purgeAssetKeys.Count} asset keys: {string.Join(", ", purgeCacheKeys.OrderBy(p => p, StringComparer.InvariantCultureIgnoreCase))}. Reloaded {reloaded} core assets.", LogLevel.Trace);
|
||||
return true;
|
||||
}
|
||||
this.Monitor.Log("Invalidated 0 cache entries.", LogLevel.Trace);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
@ -221,6 +263,33 @@ namespace StardewModdingAPI.Framework
|
|||
|| this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke<string>()}"); // translated asset
|
||||
}
|
||||
|
||||
/// <summary>Parse a cache key into its component parts.</summary>
|
||||
/// <param name="cacheKey">The input cache key.</param>
|
||||
/// <param name="assetKey">The original asset key.</param>
|
||||
/// <param name="localeCode">The asset locale code (or <c>null</c> if not localised).</param>
|
||||
private void ParseCacheKey(string cacheKey, out string assetKey, out string localeCode)
|
||||
{
|
||||
// handle localised key
|
||||
if (!string.IsNullOrWhiteSpace(cacheKey))
|
||||
{
|
||||
int lastSepIndex = cacheKey.LastIndexOf(".", StringComparison.InvariantCulture);
|
||||
if (lastSepIndex >= 0)
|
||||
{
|
||||
string suffix = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1);
|
||||
if (this.KeyLocales.ContainsKey(suffix))
|
||||
{
|
||||
assetKey = cacheKey.Substring(0, lastSepIndex);
|
||||
localeCode = cacheKey.Substring(lastSepIndex + 1, cacheKey.Length - lastSepIndex - 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle simple key
|
||||
assetKey = cacheKey;
|
||||
localeCode = null;
|
||||
}
|
||||
|
||||
/// <summary>Load the initial asset from the registered <see cref="Loaders"/>.</summary>
|
||||
/// <param name="info">The basic asset metadata.</param>
|
||||
/// <returns>Returns the loaded asset metadata, or <c>null</c> if no loader matched.</returns>
|
||||
|
@ -365,7 +434,8 @@ namespace StardewModdingAPI.Framework
|
|||
// can't know which assets are meant to be disposed. Here we remove current assets from
|
||||
// the cache, but don't dispose them to avoid crashing any code that still references
|
||||
// them. The garbage collector will eventually clean up any unused assets.
|
||||
this.Reset();
|
||||
this.Monitor.Log("Content manager disposed, resetting cache.", LogLevel.Trace);
|
||||
this.InvalidateCache((key, type) => true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,5 +20,19 @@ namespace StardewModdingAPI
|
|||
/// <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 = ContentSource.ModFolder);
|
||||
|
||||
#if !SMAPI_1_x
|
||||
/// <summary>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.</summary>
|
||||
/// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to a content 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>
|
||||
/// <returns>Returns whether the given asset key was cached.</returns>
|
||||
bool InvalidateCache(string key, ContentSource source = ContentSource.ModFolder);
|
||||
|
||||
/// <summary>Remove all assets of the given type from the cache so they're reloaded on the next request. <b>This can be a very expensive operation and should only be used in very specific cases.</b> This will reload core game assets if needed, but references to the former assets will still show the previous content.</summary>
|
||||
/// <typeparam name="T">The asset type to remove from the cache.</typeparam>
|
||||
/// <returns>Returns whether any assets were invalidated.</returns>
|
||||
bool InvalidateCache<T>();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,163 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using StardewModdingAPI.Framework;
|
||||
using StardewValley;
|
||||
using StardewValley.BellsAndWhistles;
|
||||
using StardewValley.Buildings;
|
||||
using StardewValley.Locations;
|
||||
using StardewValley.Objects;
|
||||
using StardewValley.Projectiles;
|
||||
using StardewValley.TerrainFeatures;
|
||||
|
||||
namespace StardewModdingAPI.Metadata
|
||||
{
|
||||
/// <summary>Provides metadata about core assets in the game.</summary>
|
||||
internal class CoreAssets
|
||||
{
|
||||
/*********
|
||||
** Properties
|
||||
*********/
|
||||
/// <summary>Normalises an asset key to match the cache key.</summary>
|
||||
protected readonly Func<string, string> GetNormalisedPath;
|
||||
|
||||
/// <summary>Setters which update static or singleton texture fields indexed by normalised asset key.</summary>
|
||||
private readonly IDictionary<string, Action<SContentManager, string>> SingletonSetters;
|
||||
|
||||
|
||||
/*********
|
||||
** Public methods
|
||||
*********/
|
||||
/// <summary>Initialise the core asset data.</summary>
|
||||
/// <param name="getNormalisedPath">Normalises an asset key to match the cache key.</param>
|
||||
public CoreAssets(Func<string, string> getNormalisedPath)
|
||||
{
|
||||
this.GetNormalisedPath = getNormalisedPath;
|
||||
this.SingletonSetters =
|
||||
new Dictionary<string, Action<SContentManager, string>>
|
||||
{
|
||||
// from Game1.loadContent
|
||||
["LooseSprites\\daybg"] = (content, key) => Game1.daybg = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\nightbg"] = (content, key) => Game1.nightbg = content.Load<Texture2D>(key),
|
||||
["Maps\\MenuTiles"] = (content, key) => Game1.menuTexture = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\Lighting\\lantern"] = (content, key) => Game1.lantern = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\Lighting\\windowLight"] = (content, key) => Game1.windowLight = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\Lighting\\sconceLight"] = (content, key) => Game1.sconceLight = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\Lighting\\greenLight"] = (content, key) => Game1.cauldronLight = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\Lighting\\indoorWindowLight"] = (content, key) => Game1.indoorWindowLight = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\shadow"] = (content, key) => Game1.shadowTexture = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\Cursors"] = (content, key) => Game1.mouseCursors = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\ControllerMaps"] = (content, key) => Game1.controllerMaps = content.Load<Texture2D>(key),
|
||||
["TileSheets\\animations"] = (content, key) => Game1.animations = content.Load<Texture2D>(key),
|
||||
["Data\\Achievements"] = (content, key) => Game1.achievements = content.Load<Dictionary<int, string>>(key),
|
||||
["Data\\NPCGiftTastes"] = (content, key) => Game1.NPCGiftTastes = content.Load<Dictionary<string, string>>(key),
|
||||
["Fonts\\SpriteFont1"] = (content, key) => Game1.dialogueFont = content.Load<SpriteFont>(key),
|
||||
["Fonts\\SmallFont"] = (content, key) => Game1.smallFont = content.Load<SpriteFont>(key),
|
||||
["Fonts\\tinyFont"] = (content, key) => Game1.tinyFont = content.Load<SpriteFont>(key),
|
||||
["Fonts\\tinyFontBorder"] = (content, key) => Game1.tinyFontBorder = content.Load<SpriteFont>(key),
|
||||
["Maps\\springobjects"] = (content, key) => Game1.objectSpriteSheet = content.Load<Texture2D>(key),
|
||||
["TileSheets\\crops"] = (content, key) => Game1.cropSpriteSheet = content.Load<Texture2D>(key),
|
||||
["TileSheets\\emotes"] = (content, key) => Game1.emoteSpriteSheet = content.Load<Texture2D>(key),
|
||||
["TileSheets\\debris"] = (content, key) => Game1.debrisSpriteSheet = content.Load<Texture2D>(key),
|
||||
["TileSheets\\Craftables"] = (content, key) => Game1.bigCraftableSpriteSheet = content.Load<Texture2D>(key),
|
||||
["TileSheets\\rain"] = (content, key) => Game1.rainTexture = content.Load<Texture2D>(key),
|
||||
["TileSheets\\BuffsIcons"] = (content, key) => Game1.buffsIcons = content.Load<Texture2D>(key),
|
||||
["Data\\ObjectInformation"] = (content, key) => Game1.objectInformation = content.Load<Dictionary<int, string>>(key),
|
||||
["Data\\BigCraftablesInformation"] = (content, key) => Game1.bigCraftablesInformation = content.Load<Dictionary<int, string>>(key),
|
||||
["Characters\\Farmer\\hairstyles"] = (content, key) => FarmerRenderer.hairStylesTexture = content.Load<Texture2D>(key),
|
||||
["Characters\\Farmer\\shirts"] = (content, key) => FarmerRenderer.shirtsTexture = content.Load<Texture2D>(key),
|
||||
["Characters\\Farmer\\hats"] = (content, key) => FarmerRenderer.hatsTexture = content.Load<Texture2D>(key),
|
||||
["Characters\\Farmer\\accessories"] = (content, key) => FarmerRenderer.accessoriesTexture = content.Load<Texture2D>(key),
|
||||
["TileSheets\\furniture"] = (content, key) => Furniture.furnitureTexture = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\font_bold"] = (content, key) => SpriteText.spriteTexture = content.Load<Texture2D>(key),
|
||||
["LooseSprites\\font_colored"] = (content, key) => SpriteText.coloredTexture = content.Load<Texture2D>(key),
|
||||
["TileSheets\\weapons"] = (content, key) => Tool.weaponsTexture = content.Load<Texture2D>(key),
|
||||
["TileSheets\\Projectiles"] = (content, key) => Projectile.projectileSheet = content.Load<Texture2D>(key),
|
||||
|
||||
// from Bush
|
||||
["TileSheets\\bushes"] = (content, key) => Bush.texture = content.Load<Texture2D>(key),
|
||||
|
||||
// from Critter
|
||||
["TileSheets\\critters"] = (content, key) => Critter.critterTexture = content.Load<Texture2D>(key),
|
||||
|
||||
// from Farm
|
||||
["Buildings\\houses"] = (content, key) =>
|
||||
{
|
||||
Farm farm = Game1.getFarm();
|
||||
if (farm != null)
|
||||
farm.houseTextures = content.Load<Texture2D>(key);
|
||||
},
|
||||
|
||||
// from Farmer
|
||||
["Characters\\Farmer\\farmer_base"] = (content, key) =>
|
||||
{
|
||||
if (Game1.player != null && Game1.player.isMale)
|
||||
Game1.player.FarmerRenderer = new FarmerRenderer(content.Load<Texture2D>(key));
|
||||
},
|
||||
["Characters\\Farmer\\farmer_girl_base"] = (content, key) =>
|
||||
{
|
||||
if (Game1.player != null && !Game1.player.isMale)
|
||||
Game1.player.FarmerRenderer = new FarmerRenderer(content.Load<Texture2D>(key));
|
||||
},
|
||||
|
||||
// from Flooring
|
||||
["TerrainFeatures\\Flooring"] = (content, key) => Flooring.floorsTexture = content.Load<Texture2D>(key),
|
||||
|
||||
// from FruitTree
|
||||
["TileSheets\\fruitTrees"] = (content, key) => FruitTree.texture = content.Load<Texture2D>(key),
|
||||
|
||||
// from HoeDirt
|
||||
["TerrainFeatures\\hoeDirt"] = (content, key) => HoeDirt.lightTexture = content.Load<Texture2D>(key),
|
||||
["TerrainFeatures\\hoeDirtDark"] = (content, key) => HoeDirt.darkTexture = content.Load<Texture2D>(key),
|
||||
["TerrainFeatures\\hoeDirtSnow"] = (content, key) => HoeDirt.snowTexture = content.Load<Texture2D>(key),
|
||||
|
||||
// from Wallpaper
|
||||
["Maps\\walls_and_floors"] = (content, key) => Wallpaper.wallpaperTexture = content.Load<Texture2D>(key)
|
||||
}
|
||||
.ToDictionary(p => getNormalisedPath(p.Key), p => p.Value);
|
||||
}
|
||||
|
||||
/// <summary>Reload one of the game's core assets (if applicable).</summary>
|
||||
/// <param name="content">The content manager through which to reload the asset.</param>
|
||||
/// <param name="key">The asset key to reload.</param>
|
||||
/// <returns>Returns whether an asset was reloaded.</returns>
|
||||
public bool ReloadForKey(SContentManager content, string key)
|
||||
{
|
||||
// static assets
|
||||
if (this.SingletonSetters.TryGetValue(key, out Action<SContentManager, string> reload))
|
||||
{
|
||||
reload(content, key);
|
||||
return true;
|
||||
}
|
||||
|
||||
// building textures
|
||||
if (key.StartsWith(this.GetNormalisedPath("Buildings\\")))
|
||||
{
|
||||
Building[] buildings = this.GetAllBuildings().Where(p => key == this.GetNormalisedPath($"Buildings\\{p.buildingType}")).ToArray();
|
||||
if (buildings.Any())
|
||||
{
|
||||
Texture2D texture = content.Load<Texture2D>(key);
|
||||
foreach (Building building in buildings)
|
||||
building.texture = texture;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*********
|
||||
** Private methods
|
||||
*********/
|
||||
/// <summary>Get all player-constructed buildings in the world.</summary>
|
||||
private IEnumerable<Building> GetAllBuildings()
|
||||
{
|
||||
return Game1.locations
|
||||
.OfType<BuildableGameLocation>()
|
||||
.SelectMany(p => p.buildings);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -707,15 +707,16 @@ namespace StardewModdingAPI
|
|||
|
||||
// inject data
|
||||
{
|
||||
IMonitor monitor = this.GetSecondaryMonitor(metadata.DisplayName);
|
||||
ICommandHelper commandHelper = new CommandHelper(manifest.UniqueID, metadata.DisplayName, this.CommandManager);
|
||||
IContentHelper contentHelper = new ContentHelper(contentManager, metadata.DirectoryPath, manifest.UniqueID, metadata.DisplayName);
|
||||
IContentHelper contentHelper = new ContentHelper(contentManager, metadata.DirectoryPath, manifest.UniqueID, metadata.DisplayName, monitor);
|
||||
IReflectionHelper reflectionHelper = new ReflectionHelper(manifest.UniqueID, this.Reflection);
|
||||
IModRegistry modRegistryHelper = new ModRegistryHelper(manifest.UniqueID, this.ModRegistry);
|
||||
ITranslationHelper translationHelper = new TranslationHelper(manifest.UniqueID, manifest.Name, contentManager.GetLocale(), contentManager.GetCurrentLanguage());
|
||||
|
||||
mod.ModManifest = manifest;
|
||||
mod.Helper = new ModHelper(manifest.UniqueID, metadata.DirectoryPath, jsonHelper, contentHelper, commandHelper, modRegistryHelper, reflectionHelper, translationHelper);
|
||||
mod.Monitor = this.GetSecondaryMonitor(metadata.DisplayName);
|
||||
mod.Monitor = monitor;
|
||||
#if SMAPI_1_x
|
||||
mod.PathOnDisk = metadata.DirectoryPath;
|
||||
#endif
|
||||
|
@ -796,7 +797,7 @@ namespace StardewModdingAPI
|
|||
if (this.DeprecationManager.IsVirtualMethodImplemented(mod.GetType(), typeof(Mod), nameof(Mod.Entry), new[] { typeof(object[]) }))
|
||||
deprecationWarnings.Add(() => this.DeprecationManager.Warn(metadata.DisplayName, $"{nameof(Mod)}.{nameof(Mod.Entry)}(object[]) instead of {nameof(Mod)}.{nameof(Mod.Entry)}({nameof(IModHelper)})", "1.0", DeprecationLevel.PendingRemoval));
|
||||
#else
|
||||
if (!this.DeprecationManager.IsVirtualMethodImplemented(mod.GetType(), typeof(Mod), nameof(Mod.Entry), new[] {typeof(IModHelper)}))
|
||||
if (!this.DeprecationManager.IsVirtualMethodImplemented(mod.GetType(), typeof(Mod), nameof(Mod.Entry), new[] { typeof(IModHelper) }))
|
||||
this.Monitor.Log($"{metadata.DisplayName} doesn't implement Entry() and may not work correctly.", LogLevel.Error);
|
||||
#endif
|
||||
}
|
||||
|
@ -812,19 +813,27 @@ namespace StardewModdingAPI
|
|||
{
|
||||
if (metadata.Mod.Helper.Content is ContentHelper helper)
|
||||
{
|
||||
// TODO: optimise by only reloading assets the new editors/loaders can intercept
|
||||
helper.ObservableAssetEditors.CollectionChanged += (sender, e) =>
|
||||
{
|
||||
if (e.NewItems.Count > 0)
|
||||
this.ContentManager.Reset();
|
||||
{
|
||||
this.Monitor.Log("Detected new asset editor, resetting cache...", LogLevel.Trace);
|
||||
this.ContentManager.InvalidateCache((key, type) => true);
|
||||
}
|
||||
};
|
||||
helper.ObservableAssetLoaders.CollectionChanged += (sender, e) =>
|
||||
{
|
||||
if (e.NewItems.Count > 0)
|
||||
this.ContentManager.Reset();
|
||||
{
|
||||
this.Monitor.Log("Detected new asset loader, resetting cache...", LogLevel.Trace);
|
||||
this.ContentManager.InvalidateCache((key, type) => true);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
this.ContentManager.Reset();
|
||||
this.Monitor.Log("Resetting cache to enable interception...", LogLevel.Trace);
|
||||
this.ContentManager.InvalidateCache((key, type) => true);
|
||||
}
|
||||
|
||||
/// <summary>Reload translations for all mods.</summary>
|
||||
|
|
|
@ -91,6 +91,7 @@
|
|||
<Link>Properties\GlobalAssemblyInfo.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Command.cs" />
|
||||
<Compile Include="Metadata\CoreAssets.cs" />
|
||||
<Compile Include="ContentSource.cs" />
|
||||
<Compile Include="Events\ContentEvents.cs" />
|
||||
<Compile Include="Events\EventArgsInput.cs" />
|
||||
|
|
Loading…
Reference in New Issue