1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 14:08:34 +01:00
MLEM/MLEM.Data/Content/RawContentManager.cs

156 lines
7.4 KiB
C#
Raw Normal View History

2020-04-22 00:30:55 +02:00
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
namespace MLEM.Data.Content {
2020-05-20 23:59:40 +02:00
/// <summary>
2022-06-24 14:01:26 +02:00
/// Represents a version of <see cref="ContentManager"/> that doesn't load content binary <c>xnb</c> files, but rather as their regular formats.
2020-05-20 23:59:40 +02:00
/// </summary>
2020-04-22 00:30:55 +02:00
public class RawContentManager : ContentManager, IGameComponent {
2020-05-20 23:59:40 +02:00
/// <summary>
/// The graphics device that this content manager uses
/// </summary>
2020-04-22 00:30:55 +02:00
public readonly GraphicsDevice GraphicsDevice;
2022-06-24 14:01:26 +02:00
private readonly List<IDisposable> disposableAssets = new List<IDisposable>();
private readonly List<RawContentReader> readers;
#if FNA || KNI
2022-06-24 14:01:26 +02:00
private Dictionary<string, object> LoadedAssets { get; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
2022-12-13 13:11:36 +01:00
#endif
2022-06-24 14:01:26 +02:00
2020-05-20 23:59:40 +02:00
/// <summary>
/// Creates a new content manager with an optionally specified root directory.
/// Each <see cref="RawContentReader"/> required for asset loading is gathered and instantiated automatically from the loaded assemblies.
2020-05-20 23:59:40 +02:00
/// </summary>
/// <param name="serviceProvider">The service provider of your game</param>
/// <param name="rootDirectory">The root directory. Defaults to "Content"</param>
2022-12-13 13:11:36 +01:00
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Automatically gathered RawContentReader types might be removed, use other constructor to add readers manually")]
2022-12-13 13:11:36 +01:00
#endif
2020-04-22 00:30:55 +02:00
public RawContentManager(IServiceProvider serviceProvider, string rootDirectory = "Content") :
this(serviceProvider, RawContentManager.CollectContentReaders(), rootDirectory) {}
/// <summary>
/// Creates a new content manager with an optionally specified root directory.
/// Each <see cref="RawContentReader"/> required for asset loading has to be passed using <paramref name="readers"/>.
/// </summary>
/// <param name="serviceProvider">The service provider of your game</param>
/// <param name="readers">The raw content readers to use, which can be modified externally afterwards to add additional readers if desired.</param>
/// <param name="rootDirectory">The root directory. Defaults to "Content"</param>
public RawContentManager(IServiceProvider serviceProvider, List<RawContentReader> readers, string rootDirectory) :
2020-04-22 00:30:55 +02:00
base(serviceProvider, rootDirectory) {
if (serviceProvider.GetService(typeof(IGraphicsDeviceService)) is IGraphicsDeviceService s)
this.GraphicsDevice = s.GraphicsDevice;
this.readers = readers;
2020-04-22 00:30:55 +02:00
}
2020-05-20 23:59:40 +02:00
/// <summary>
/// Loads a raw asset with the given name, based on the <see cref="ContentManager.RootDirectory"/>.
/// If the asset was previously loaded using this method, the cached asset is merely returned.
/// </summary>
/// <param name="assetName">The path and name of the asset to load, without extension.</param>
/// <typeparam name="T">The type of asset to load</typeparam>
/// <returns>The asset, either loaded from the cache, or from disk.</returns>
2020-04-22 00:30:55 +02:00
public override T Load<T>(string assetName) {
if (this.LoadedAssets.TryGetValue(assetName, out var ret) && ret is T t)
return t;
return this.Read<T>(assetName, default);
}
/// <summary>
/// Reloads the asset of the given type, with the given original name.
/// </summary>
/// <param name="originalAssetName">The original name of the asset.</param>
/// <param name="currentAsset">The current asset instance.</param>
/// <typeparam name="T">The asset's type.</typeparam>
2022-06-24 14:01:26 +02:00
protected
#if !FNA && !KNI
2022-06-24 14:01:26 +02:00
override
2022-12-13 13:11:36 +01:00
#endif
2022-06-24 14:01:26 +02:00
void ReloadAsset<T>(string originalAssetName, T currentAsset) {
2020-04-22 00:30:55 +02:00
this.Read(originalAssetName, currentAsset);
}
private T Read<T>(string assetName, T existing) {
var triedFiles = new List<string>();
foreach (var reader in this.readers) {
if (!reader.CanRead(typeof(T)))
continue;
foreach (var ext in reader.GetFileExtensions()) {
var file = Path.Combine(this.RootDirectory, $"{assetName}.{ext}");
triedFiles.Add(file);
try {
using (var stream = Path.IsPathRooted(file) ? File.OpenRead(file) : TitleContainer.OpenStream(file)) {
var read = reader.Read(this, assetName, stream, typeof(T), existing);
if (!(read is T t))
throw new ContentLoadException($"{reader} returned non-{typeof(T)} for asset {assetName}");
this.LoadedAssets[assetName] = t;
if (t is IDisposable d && !this.disposableAssets.Contains(d))
this.disposableAssets.Add(d);
if (t is GraphicsResource r)
r.Name = assetName;
return t;
}
} catch (IOException) {}
2020-04-22 00:30:55 +02:00
}
}
throw new ContentLoadException($"Asset {assetName} not found. Tried files {string.Join(", ", triedFiles)}");
2020-04-22 00:30:55 +02:00
}
/// <summary>
/// Unloads this content manager, disposing all of the assets that it loaded.
/// </summary>
2020-04-22 00:30:55 +02:00
public override void Unload() {
foreach (var d in this.disposableAssets)
d.Dispose();
this.disposableAssets.Clear();
base.Unload();
}
/// <summary>
/// Initializes the component. Used to load non-graphical resources.
/// </summary>
2021-12-28 14:56:11 +01:00
public void Initialize() {}
2020-04-22 00:30:55 +02:00
2022-12-13 13:11:36 +01:00
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("Automatically gathered RawContentReader types might be removed, use other constructor to add readers manually")]
2022-12-13 13:11:36 +01:00
#endif
private static List<RawContentReader> CollectContentReaders() {
var ret = new List<RawContentReader>();
var assemblyExceptions = new List<Exception>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
try {
if (assembly.IsDynamic)
continue;
foreach (var type in assembly.GetExportedTypes()) {
try {
if (type.IsAbstract)
continue;
if (!type.IsSubclassOf(typeof(RawContentReader)))
continue;
ret.Add((RawContentReader) Activator.CreateInstance(type));
} catch (Exception e) {
throw new NotSupportedException($"The type {type} cannot be constructed by a RawContentManager. Does it have a visible parameterless constructor?", e);
}
}
} catch (Exception e) {
assemblyExceptions.Add(e);
}
}
if (ret.Count <= 0)
throw new AggregateException("Failed to construct any RawContentReader instances", assemblyExceptions);
return ret;
}
2020-04-22 00:30:55 +02:00
}
2022-06-17 18:23:47 +02:00
}