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

66 lines
3.2 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.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>();
/// <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="extension">The file extension of the file to load, or ".json" by default</param>
/// <typeparam name="T">The type of asset to load</typeparam>
/// <returns>The loaded asset</returns>
2020-02-27 18:56:49 +01:00
public static T LoadJson<T>(this ContentManager content, string name, string extension = ".json") {
using (var stream = File.OpenText(Path.Combine(content.RootDirectory, name + extension))) {
using (var reader = new JsonTextReader(stream)) {
return GetJsonSerializer(content).Deserialize<T>(reader);
}
}
}
}
}