mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
allow loading other json file extensions in MLEM.Data by default
This commit is contained in:
parent
51fbba8731
commit
534194b3ea
3 changed files with 14 additions and 7 deletions
|
@ -11,6 +11,7 @@ namespace MLEM.Data {
|
||||||
public static class ContentExtensions {
|
public static class ContentExtensions {
|
||||||
|
|
||||||
private static readonly Dictionary<ContentManager, JsonSerializer> Serializers = new Dictionary<ContentManager, JsonSerializer>();
|
private static readonly Dictionary<ContentManager, JsonSerializer> Serializers = new Dictionary<ContentManager, JsonSerializer>();
|
||||||
|
private static readonly string[] JsonExtensions = {".json", ".json5", ".jsonc"};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a <see cref="JsonSerializer"/> to the given content manager, which allows <see cref="LoadJson{T}"/> to load JSON-based content.
|
/// Adds a <see cref="JsonSerializer"/> to the given content manager, which allows <see cref="LoadJson{T}"/> to load JSON-based content.
|
||||||
|
@ -51,15 +52,21 @@ namespace MLEM.Data {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="content">The content manager to load content with</param>
|
/// <param name="content">The content manager to load content with</param>
|
||||||
/// <param name="name">The name of the file to load</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>
|
/// <param name="extensions">The file extensions that should be appended, or ".json", ".json5" and ".jsonc" by default.</param>
|
||||||
/// <typeparam name="T">The type of asset to load</typeparam>
|
/// <typeparam name="T">The type of asset to load</typeparam>
|
||||||
/// <returns>The loaded asset</returns>
|
/// <returns>The loaded asset</returns>
|
||||||
public static T LoadJson<T>(this ContentManager content, string name, string extension = ".json") {
|
public static T LoadJson<T>(this ContentManager content, string name, string[] extensions = null) {
|
||||||
using (var stream = File.OpenText(Path.Combine(content.RootDirectory, name + extension))) {
|
foreach (var extension in extensions ?? JsonExtensions) {
|
||||||
using (var reader = new JsonTextReader(stream)) {
|
var file = Path.Combine(content.RootDirectory, name + extension);
|
||||||
return GetJsonSerializer(content).Deserialize<T>(reader);
|
if (!File.Exists(file))
|
||||||
|
continue;
|
||||||
|
using (var stream = File.OpenText(file)) {
|
||||||
|
using (var reader = new JsonTextReader(stream)) {
|
||||||
|
return GetJsonSerializer(content).Deserialize<T>(reader);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace MLEM.Data.Json {
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string[] GetFileExtensions() {
|
public override string[] GetFileExtensions() {
|
||||||
return new[] {"json"};
|
return new[] {"json", "json5", "jsonc"};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,7 +123,7 @@ namespace Sandbox {
|
||||||
//this.Content.AddJsonConverter(new CustomConverter());
|
//this.Content.AddJsonConverter(new CustomConverter());
|
||||||
|
|
||||||
var res = this.Content.LoadJson<Test>("Test");
|
var res = this.Content.LoadJson<Test>("Test");
|
||||||
//Console.WriteLine(res);
|
Console.WriteLine("The res is " + res);
|
||||||
|
|
||||||
/*this.OnDraw += (game, time) => {
|
/*this.OnDraw += (game, time) => {
|
||||||
this.SpriteBatch.Begin();
|
this.SpriteBatch.Begin();
|
||||||
|
|
Loading…
Reference in a new issue