override content manager (#173)

This commit is contained in:
Jesse Plamondon-Willard 2017-02-24 18:52:53 -05:00
parent 60f31b0fc6
commit 615c89bc0b
3 changed files with 71 additions and 0 deletions

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -153,6 +153,7 @@
<Compile Include="Framework\Models\SConfig.cs" />
<Compile Include="Framework\Reflection\PrivateProperty.cs" />
<Compile Include="Framework\RequestExitDelegate.cs" />
<Compile Include="Framework\SContentManager.cs" />
<Compile Include="Framework\Serialisation\JsonHelper.cs" />
<Compile Include="Framework\Serialisation\SelectiveStringEnumConverter.cs" />
<Compile Include="Framework\Serialisation\SemanticVersionConverter.cs" />