mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +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 {
|
||||
|
||||
private static readonly Dictionary<ContentManager, JsonSerializer> Serializers = new Dictionary<ContentManager, JsonSerializer>();
|
||||
private static readonly string[] JsonExtensions = {".json", ".json5", ".jsonc"};
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// <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>
|
||||
/// <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>
|
||||
/// <returns>The loaded asset</returns>
|
||||
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);
|
||||
public static T LoadJson<T>(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<T>(reader);
|
||||
}
|
||||
}
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ namespace MLEM.Data.Json {
|
|||
|
||||
/// <inheritdoc />
|
||||
public override string[] GetFileExtensions() {
|
||||
return new[] {"json"};
|
||||
return new[] {"json", "json5", "jsonc"};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ namespace Sandbox {
|
|||
//this.Content.AddJsonConverter(new CustomConverter());
|
||||
|
||||
var res = this.Content.LoadJson<Test>("Test");
|
||||
//Console.WriteLine(res);
|
||||
Console.WriteLine("The res is " + res);
|
||||
|
||||
/*this.OnDraw += (game, time) => {
|
||||
this.SpriteBatch.Begin();
|
||||
|
|
Loading…
Reference in a new issue