From 615c89bc0ba19568a147120cfd49c97142f63969 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 24 Feb 2017 18:52:53 -0500 Subject: override content manager (#173) --- src/StardewModdingAPI/Framework/SContentManager.cs | 63 ++++++++++++++++++++++ src/StardewModdingAPI/Framework/SGame.cs | 7 +++ src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 3 files changed, 71 insertions(+) create mode 100644 src/StardewModdingAPI/Framework/SContentManager.cs (limited to 'src') 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 +{ + /// SMAPI's implementation of the game's content manager which lets it raise content events. + internal class SContentManager : LocalizedContentManager + { + /********* + ** Accessors + *********/ + /// Encapsulates monitoring and logging. + private readonly IMonitor Monitor; + + /// The underlying content manager's asset cache. + private readonly IDictionary Cache; + + /// Normalises an asset key to match the cache key. + private readonly IPrivateMethod NormaliseAssetKey; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The service provider to use to locate services. + /// The root directory to search for content. + /// Encapsulates monitoring and logging. + public SContentManager(IServiceProvider serviceProvider, string rootDirectory, IMonitor monitor) + : this(serviceProvider, rootDirectory, Thread.CurrentThread.CurrentUICulture, null, monitor) { } + + /// Construct an instance. + /// The service provider to use to locate services. + /// The root directory to search for content. + /// The current culture for which to localise content. + /// The current language code for which to localise content. + /// Encapsulates monitoring and logging. + 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>(this, "loadedAssets") + .GetValue(); + this.NormaliseAssetKey = reflection.GetPrivateMethod(typeof(TitleContainer), "GetCleanPath"); + } + + /// 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 base.Load(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 /// The method called before XNA or MonoGame loads or reloads graphics resources. 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); } diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 72cc1ed2..ee37379d 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -153,6 +153,7 @@ + -- cgit