From 534194b3eabd99916d071f9c1e50a57f0a6c612a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 16 Sep 2020 23:24:03 +0200 Subject: [PATCH] allow loading other json file extensions in MLEM.Data by default --- MLEM.Data/ContentExtensions.cs | 17 ++++++++++++----- MLEM.Data/Json/RawJsonReader.cs | 2 +- Sandbox/GameImpl.cs | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/MLEM.Data/ContentExtensions.cs b/MLEM.Data/ContentExtensions.cs index 0e64a9e..46e806a 100644 --- a/MLEM.Data/ContentExtensions.cs +++ b/MLEM.Data/ContentExtensions.cs @@ -11,6 +11,7 @@ namespace MLEM.Data { public static class ContentExtensions { private static readonly Dictionary Serializers = new Dictionary(); + private static readonly string[] JsonExtensions = {".json", ".json5", ".jsonc"}; /// /// Adds a to the given content manager, which allows to load JSON-based content. @@ -51,15 +52,21 @@ namespace MLEM.Data { /// /// The content manager to load content with /// The name of the file to load - /// The file extension of the file to load, or ".json" by default + /// The file extensions that should be appended, or ".json", ".json5" and ".jsonc" by default. /// The type of asset to load /// The loaded asset - public static T LoadJson(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(reader); + public static T LoadJson(this ContentManager content, string name, string[] extensions = null) { + foreach (var extension in extensions ?? JsonExtensions) { + var file = Path.Combine(content.RootDirectory, name + extension); + if (!File.Exists(file)) + continue; + using (var stream = File.OpenText(file)) { + using (var reader = new JsonTextReader(stream)) { + return GetJsonSerializer(content).Deserialize(reader); + } } } + return default; } } diff --git a/MLEM.Data/Json/RawJsonReader.cs b/MLEM.Data/Json/RawJsonReader.cs index 050185f..32e2090 100644 --- a/MLEM.Data/Json/RawJsonReader.cs +++ b/MLEM.Data/Json/RawJsonReader.cs @@ -20,7 +20,7 @@ namespace MLEM.Data.Json { /// public override string[] GetFileExtensions() { - return new[] {"json"}; + return new[] {"json", "json5", "jsonc"}; } } diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index d130701..ca3c3c0 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -123,7 +123,7 @@ namespace Sandbox { //this.Content.AddJsonConverter(new CustomConverter()); var res = this.Content.LoadJson("Test"); - //Console.WriteLine(res); + Console.WriteLine("The res is " + res); /*this.OnDraw += (game, time) => { this.SpriteBatch.Begin();