2020-02-27 18:56:49 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2021-10-16 20:22:06 +02:00
|
|
|
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 {
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <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>();
|
2020-09-16 23:24:03 +02:00
|
|
|
private static readonly string[] JsonExtensions = {".json", ".json5", ".jsonc"};
|
2020-02-27 18:56:49 +01:00
|
|
|
|
2020-05-22 20:32:38 +02: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) {
|
2022-06-15 11:38:11 +02:00
|
|
|
ContentExtensions.Serializers[content] = serializer;
|
2020-02-27 18:56:49 +01:00
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <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) {
|
2022-06-15 11:38:11 +02:00
|
|
|
if (!ContentExtensions.Serializers.TryGetValue(content, out var serializer)) {
|
2020-02-27 18:56:49 +01:00
|
|
|
serializer = JsonConverters.AddAll(new JsonSerializer());
|
|
|
|
content.SetJsonSerializer(serializer);
|
|
|
|
}
|
|
|
|
return serializer;
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <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>
|
2020-02-27 19:54:44 +01:00
|
|
|
public static void AddJsonConverter(this ContentManager content, JsonConverter converter) {
|
2022-06-15 11:38:11 +02:00
|
|
|
var serializer = content.GetJsonSerializer();
|
2020-02-27 18:56:49 +01:00
|
|
|
serializer.Converters.Add(converter);
|
|
|
|
}
|
|
|
|
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <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>
|
2020-09-16 23:24:03 +02:00
|
|
|
/// <param name="extensions">The file extensions that should be appended, or ".json", ".json5" and ".jsonc" by default.</param>
|
2020-11-22 16:57:44 +01:00
|
|
|
/// <param name="serializer">The json serializer to use, or <see cref="GetJsonSerializer"/> by default.</param>
|
2020-05-22 20:32:38 +02:00
|
|
|
/// <typeparam name="T">The type of asset to load</typeparam>
|
|
|
|
/// <returns>The loaded asset</returns>
|
2020-11-22 16:57:44 +01:00
|
|
|
public static T LoadJson<T>(this ContentManager content, string name, string[] extensions = null, JsonSerializer serializer = null) {
|
2020-11-21 22:49:22 +01:00
|
|
|
var triedFiles = new List<string>();
|
2020-11-22 16:57:44 +01:00
|
|
|
var serializerToUse = serializer ?? content.GetJsonSerializer();
|
2022-06-15 11:38:11 +02:00
|
|
|
foreach (var extension in extensions ?? ContentExtensions.JsonExtensions) {
|
2020-09-16 23:24:03 +02:00
|
|
|
var file = Path.Combine(content.RootDirectory, name + extension);
|
2020-11-21 22:49:22 +01:00
|
|
|
triedFiles.Add(file);
|
2021-10-16 20:22:06 +02:00
|
|
|
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);
|
2020-09-16 23:24:03 +02:00
|
|
|
}
|
2021-12-28 14:56:11 +01:00
|
|
|
} catch (FileNotFoundException) {}
|
2020-02-27 18:56:49 +01:00
|
|
|
}
|
2020-11-21 22:49:22 +01:00
|
|
|
throw new ContentLoadException($"Asset {name} not found. Tried files {string.Join(", ", triedFiles)}");
|
2020-02-27 18:56:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-06-17 18:23:47 +02:00
|
|
|
}
|