diff options
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r-- | src/StardewModdingAPI/Framework/SContentManager.cs | 63 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/SGame.cs | 7 |
2 files changed, 70 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs new file mode 100644 index 00000000..27001a06 --- /dev/null +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Threading; +using Microsoft.Xna.Framework; +using StardewModdingAPI.Framework.Reflection; +using StardewValley; + +namespace StardewModdingAPI.Framework +{ + /// <summary>SMAPI's implementation of the game's content manager which lets it raise content events.</summary> + internal class SContentManager : LocalizedContentManager + { + /********* + ** Accessors + *********/ + /// <summary>Encapsulates monitoring and logging.</summary> + private readonly IMonitor Monitor; + + /// <summary>The underlying content manager's asset cache.</summary> + private readonly IDictionary<string, object> Cache; + + /// <summary>Normalises an asset key to match the cache key.</summary> + private readonly IPrivateMethod NormaliseAssetKey; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="serviceProvider">The service provider to use to locate services.</param> + /// <param name="rootDirectory">The root directory to search for content.</param> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + public SContentManager(IServiceProvider serviceProvider, string rootDirectory, IMonitor monitor) + : this(serviceProvider, rootDirectory, Thread.CurrentThread.CurrentUICulture, null, monitor) { } + + /// <summary>Construct an instance.</summary> + /// <param name="serviceProvider">The service provider to use to locate services.</param> + /// <param name="rootDirectory">The root directory to search for content.</param> + /// <param name="currentCulture">The current culture for which to localise content.</param> + /// <param name="languageCodeOverride">The current language code for which to localise content.</param> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + public SContentManager(IServiceProvider serviceProvider, string rootDirectory, CultureInfo currentCulture, string languageCodeOverride, IMonitor monitor) + : base(serviceProvider, rootDirectory, currentCulture, languageCodeOverride) + { + this.Monitor = monitor; + + IReflectionHelper reflection = new ReflectionHelper(); + this.Cache = reflection + .GetPrivateField<Dictionary<string, object>>(this, "loadedAssets") + .GetValue(); + this.NormaliseAssetKey = reflection.GetPrivateMethod(typeof(TitleContainer), "GetCleanPath"); + } + + /// <summary>Load an asset that has been processed by the Content Pipeline.</summary> + /// <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> + public override T Load<T>(string assetName) + { + return base.Load<T>(assetName); + } + } +} diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 56631260..aab44b59 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -288,7 +288,14 @@ namespace StardewModdingAPI.Framework /// <summary>The method called before XNA or MonoGame loads or reloads graphics resources.</summary> protected override void LoadContent() { + // override content manager + LocalizedContentManager contentManager = Game1.content; + Game1.content = new SContentManager(this.Content.ServiceProvider, this.Content.RootDirectory, this.Monitor); + + // defer to game logic base.LoadContent(); + + // raise load content event GameEvents.InvokeLoadContent(this.Monitor); } |