1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-16 18:53:12 +01:00
MLEM/MLEM.Data/ContentExtensions.cs

78 lines
4 KiB
C#
Raw Normal View History

2020-02-27 18:56:49 +01:00
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
2020-02-27 18:56:49 +01:00
using Microsoft.Xna.Framework.Content;
using MLEM.Data.Json;
using Newtonsoft.Json;
namespace MLEM.Data {
/// <summary>
/// A set of extensions for dealing with <see cref="ContentManager"/>
/// </summary>
2020-02-27 18:56:49 +01:00
public static class ContentExtensions {
private static readonly Dictionary<ContentManager, JsonSerializer> Serializers = new Dictionary<ContentManager, JsonSerializer>();
private static readonly string[] JsonExtensions = {".json", ".json5", ".jsonc"};
2020-02-27 18:56:49 +01:00
/// <summary>
/// Adds a <see cref="JsonSerializer"/> to the given content manager, which allows <see cref="LoadJson{T}"/> to load JSON-based content.
/// Note that <see cref="GetJsonSerializer"/> calls this method implicitly if no serializer exists.
/// </summary>
/// <param name="content">The content manager to add the json serializer to</param>
/// <param name="serializer">The json serializer to add</param>
2020-02-27 18:56:49 +01:00
public static void SetJsonSerializer(this ContentManager content, JsonSerializer serializer) {
Serializers[content] = serializer;
}
/// <summary>
/// Returns the given content manager's json serializer.
/// This method sets a new json serializer using <see cref="SetJsonSerializer"/> if the given content manager does not yet have one.
/// </summary>
/// <param name="content">The content manager whose serializer to get</param>
/// <returns>The content manager's serializer</returns>
2020-02-27 18:56:49 +01:00
public static JsonSerializer GetJsonSerializer(this ContentManager content) {
if (!Serializers.TryGetValue(content, out var serializer)) {
serializer = JsonConverters.AddAll(new JsonSerializer());
content.SetJsonSerializer(serializer);
}
return serializer;
}
/// <summary>
/// Adds a <see cref="JsonConverter"/> to the given content manager's <see cref="JsonSerializer"/>.
/// </summary>
/// <param name="content">The content manager to add the converter to</param>
/// <param name="converter">The converter to add</param>
public static void AddJsonConverter(this ContentManager content, JsonConverter converter) {
2020-02-27 18:56:49 +01:00
var serializer = GetJsonSerializer(content);
serializer.Converters.Add(converter);
}
/// <summary>
/// Loads any kind of JSON data using the given content manager's <see cref="JsonSerializer"/>.
/// </summary>
/// <param name="content">The content manager to load content with</param>
/// <param name="name">The name of the file to load</param>
/// <param name="extensions">The file extensions that should be appended, or ".json", ".json5" and ".jsonc" by default.</param>
/// <param name="serializer">The json serializer to use, or <see cref="GetJsonSerializer"/> by default.</param>
/// <typeparam name="T">The type of asset to load</typeparam>
/// <returns>The loaded asset</returns>
public static T LoadJson<T>(this ContentManager content, string name, string[] extensions = null, JsonSerializer serializer = null) {
var triedFiles = new List<string>();
var serializerToUse = serializer ?? content.GetJsonSerializer();
foreach (var extension in extensions ?? JsonExtensions) {
var file = Path.Combine(content.RootDirectory, name + extension);
triedFiles.Add(file);
try {
using (var stream = Path.IsPathRooted(file) ? File.OpenText(file) : new StreamReader(TitleContainer.OpenStream(file))) {
using (var reader = new JsonTextReader(stream))
return serializerToUse.Deserialize<T>(reader);
}
} catch (FileNotFoundException) {
2020-02-27 18:56:49 +01:00
}
}
throw new ContentLoadException($"Asset {name} not found. Tried files {string.Join(", ", triedFiles)}");
2020-02-27 18:56:49 +01:00
}
}
}