mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 04:53:29 +01:00
added some json utilities to Data
This commit is contained in:
parent
ae0fc22de3
commit
9f870738b0
12 changed files with 224 additions and 15 deletions
38
MLEM.Data/ContentExtensions.cs
Normal file
38
MLEM.Data/ContentExtensions.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using MLEM.Data.Json;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data {
|
||||
public static class ContentExtensions {
|
||||
|
||||
private static readonly Dictionary<ContentManager, JsonSerializer> Serializers = new Dictionary<ContentManager, JsonSerializer>();
|
||||
|
||||
public static void SetJsonSerializer(this ContentManager content, JsonSerializer serializer) {
|
||||
Serializers[content] = serializer;
|
||||
}
|
||||
|
||||
public static JsonSerializer GetJsonSerializer(this ContentManager content) {
|
||||
if (!Serializers.TryGetValue(content, out var serializer)) {
|
||||
serializer = JsonConverters.AddAll(new JsonSerializer());
|
||||
content.SetJsonSerializer(serializer);
|
||||
}
|
||||
return serializer;
|
||||
}
|
||||
|
||||
public static void AddJsonConverter<T>(this ContentManager content, JsonConverter<T> converter) {
|
||||
var serializer = GetJsonSerializer(content);
|
||||
serializer.Converters.Add(converter);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
18
MLEM.Data/Json/Direction2Converter.cs
Normal file
18
MLEM.Data/Json/Direction2Converter.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using MLEM.Misc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data.Json {
|
||||
public class Direction2Converter : JsonConverter<Direction2> {
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Direction2 value, JsonSerializer serializer) {
|
||||
writer.WriteValue(value.ToString());
|
||||
}
|
||||
|
||||
public override Direction2 ReadJson(JsonReader reader, Type objectType, Direction2 existingValue, bool hasExistingValue, JsonSerializer serializer) {
|
||||
Enum.TryParse<Direction2>(reader.Value.ToString(), out var dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
20
MLEM.Data/Json/JsonConverters.cs
Normal file
20
MLEM.Data/Json/JsonConverters.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data.Json {
|
||||
public class JsonConverters {
|
||||
|
||||
public static readonly JsonConverter[] Converters = Assembly.GetExecutingAssembly().GetExportedTypes()
|
||||
.Where(t => t.Namespace == typeof(JsonConverters).Namespace && t.IsSubclassOf(typeof(JsonConverter)))
|
||||
.Select(t => t.GetConstructor(Type.EmptyTypes).Invoke(null)).Cast<JsonConverter>().ToArray();
|
||||
|
||||
public static JsonSerializer AddAll(JsonSerializer serializer) {
|
||||
foreach (var converter in Converters)
|
||||
serializer.Converters.Add(converter);
|
||||
return serializer;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
19
MLEM.Data/Json/PointConverter.cs
Normal file
19
MLEM.Data/Json/PointConverter.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data.Json {
|
||||
public class PointConverter : JsonConverter<Point> {
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Point value, JsonSerializer serializer) {
|
||||
writer.WriteValue(value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public override Point ReadJson(JsonReader reader, Type objectType, Point existingValue, bool hasExistingValue, JsonSerializer serializer) {
|
||||
var value = reader.Value.ToString().Split(' ');
|
||||
return new Point(int.Parse(value[0], CultureInfo.InvariantCulture), int.Parse(value[1], CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
23
MLEM.Data/Json/RectangleConverter.cs
Normal file
23
MLEM.Data/Json/RectangleConverter.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data.Json {
|
||||
public class RectangleConverter : JsonConverter<Rectangle> {
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Rectangle value, JsonSerializer serializer) {
|
||||
writer.WriteValue(
|
||||
value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture) + " " +
|
||||
value.Width.ToString(CultureInfo.InvariantCulture) + " " + value.Height.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public override Rectangle ReadJson(JsonReader reader, Type objectType, Rectangle existingValue, bool hasExistingValue, JsonSerializer serializer) {
|
||||
var value = reader.Value.ToString().Split(' ');
|
||||
return new Rectangle(
|
||||
int.Parse(value[0], CultureInfo.InvariantCulture), int.Parse(value[1], CultureInfo.InvariantCulture),
|
||||
int.Parse(value[2], CultureInfo.InvariantCulture), int.Parse(value[3], CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
23
MLEM.Data/Json/RectangleFConverter.cs
Normal file
23
MLEM.Data/Json/RectangleFConverter.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using MLEM.Misc;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data.Json {
|
||||
public class RectangleFConverter : JsonConverter<RectangleF> {
|
||||
|
||||
public override void WriteJson(JsonWriter writer, RectangleF value, JsonSerializer serializer) {
|
||||
writer.WriteValue(
|
||||
value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture) + " " +
|
||||
value.Width.ToString(CultureInfo.InvariantCulture) + " " + value.Height.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public override RectangleF ReadJson(JsonReader reader, Type objectType, RectangleF existingValue, bool hasExistingValue, JsonSerializer serializer) {
|
||||
var value = reader.Value.ToString().Split(' ');
|
||||
return new RectangleF(
|
||||
float.Parse(value[0], CultureInfo.InvariantCulture), float.Parse(value[1], CultureInfo.InvariantCulture),
|
||||
float.Parse(value[2], CultureInfo.InvariantCulture), float.Parse(value[3], CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
19
MLEM.Data/Json/Vector2Converter.cs
Normal file
19
MLEM.Data/Json/Vector2Converter.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MLEM.Data.Json {
|
||||
public class Vector2Converter : JsonConverter<Vector2> {
|
||||
|
||||
public override void WriteJson(JsonWriter writer, Vector2 value, JsonSerializer serializer) {
|
||||
writer.WriteValue(value.X.ToString(CultureInfo.InvariantCulture) + " " + value.Y.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
public override Vector2 ReadJson(JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer) {
|
||||
var value = reader.Value.ToString().Split(' ');
|
||||
return new Vector2(float.Parse(value[0], CultureInfo.InvariantCulture), float.Parse(value[1], CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -14,15 +14,15 @@
|
|||
|
||||
#---------------------------------- Content ---------------------------------#
|
||||
|
||||
#begin Tiled/Map.tmx
|
||||
/importer:TiledMapImporter
|
||||
/processor:TiledMapProcessor
|
||||
/build:Tiled/Map.tmx
|
||||
#begin Fonts/TestFont.spritefont
|
||||
/importer:FontDescriptionImporter
|
||||
/processor:FontDescriptionProcessor
|
||||
/processorParam:PremultiplyAlpha=True
|
||||
/processorParam:TextureFormat=Compressed
|
||||
/build:Fonts/TestFont.spritefont
|
||||
|
||||
#begin Tiled/Tileset.tsx
|
||||
/importer:TiledMapTilesetImporter
|
||||
/processor:TiledMapTilesetProcessor
|
||||
/build:Tiled/Tileset.tsx
|
||||
#begin Test.json
|
||||
/copy:Test.json
|
||||
|
||||
#begin Textures/Test.png
|
||||
/importer:TextureImporter
|
||||
|
@ -36,10 +36,13 @@
|
|||
/processorParam:TextureFormat=Color
|
||||
/build:Textures/Test.png
|
||||
|
||||
#begin Fonts/TestFont.spritefont
|
||||
/importer:FontDescriptionImporter
|
||||
/processor:FontDescriptionProcessor
|
||||
/processorParam:PremultiplyAlpha=True
|
||||
/processorParam:TextureFormat=Compressed
|
||||
/build:Fonts/TestFont.spritefont
|
||||
#begin Tiled/Map.tmx
|
||||
/importer:TiledMapImporter
|
||||
/processor:TiledMapProcessor
|
||||
/build:Tiled/Map.tmx
|
||||
|
||||
#begin Tiled/Tileset.tsx
|
||||
/importer:TiledMapTilesetImporter
|
||||
/processor:TiledMapTilesetProcessor
|
||||
/build:Tiled/Tileset.tsx
|
||||
|
||||
|
|
7
Sandbox/Content/Test.json
Normal file
7
Sandbox/Content/Test.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"Vec": "10 20",
|
||||
"Point": "20 30",
|
||||
"Rectangle": "1 2 3 4",
|
||||
"RectangleF": "4 5 6 7",
|
||||
"Dir": "Left"
|
||||
}
|
|
@ -1,12 +1,15 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MLEM.Cameras;
|
||||
using MLEM.Data;
|
||||
using MLEM.Extended.Extensions;
|
||||
using MLEM.Extended.Tiled;
|
||||
using MLEM.Extensions;
|
||||
using MLEM.Font;
|
||||
using MLEM.Misc;
|
||||
using MLEM.Startup;
|
||||
using MLEM.Textures;
|
||||
using MLEM.Ui;
|
||||
|
@ -14,6 +17,8 @@ using MLEM.Ui.Elements;
|
|||
using MLEM.Ui.Style;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.Tiled;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using RectangleF = MonoGame.Extended.RectangleF;
|
||||
|
||||
namespace Sandbox {
|
||||
public class GameImpl : MlemGame {
|
||||
|
@ -60,6 +65,25 @@ namespace Sandbox {
|
|||
panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10)));
|
||||
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
|
||||
this.UiSystem.Add("Panel", panel);*/
|
||||
|
||||
var obj = new Test{
|
||||
Vec = new Vector2(10, 20),
|
||||
Point = new Point(20, 30),
|
||||
Rectangle = new Rectangle(1, 2, 3, 4),
|
||||
RectangleF = new RectangleF(4, 5, 6, 7).ToMlem(),
|
||||
Dir = Direction2.Left
|
||||
};
|
||||
|
||||
var writer = new StringWriter();
|
||||
this.Content.GetJsonSerializer().Serialize(writer, obj);
|
||||
Console.WriteLine(writer.ToString());
|
||||
// {"Vec":"10 20","Point":"20 30","Rectangle":"1 2 3 4","RectangleF":"4 5 6 7"}
|
||||
|
||||
// Also:
|
||||
//this.Content.AddJsonConverter(new CustomConverter());
|
||||
|
||||
var res = this.Content.LoadJson<Test>("Test");
|
||||
Console.WriteLine(res);
|
||||
}
|
||||
|
||||
protected override void DoUpdate(GameTime gameTime) {
|
||||
|
@ -88,5 +112,19 @@ namespace Sandbox {
|
|||
base.DoDraw(gameTime);
|
||||
}
|
||||
|
||||
private class Test {
|
||||
|
||||
public Vector2 Vec;
|
||||
public Point Point;
|
||||
public Rectangle Rectangle;
|
||||
public MLEM.Misc.RectangleF RectangleF;
|
||||
public Direction2 Dir;
|
||||
|
||||
public override string ToString() {
|
||||
return $"{nameof(this.Vec)}: {this.Vec}, {nameof(this.Point)}: {this.Point}, {nameof(this.Rectangle)}: {this.Rectangle}, {nameof(this.RectangleF)}: {this.RectangleF}, {nameof(this.Dir)}: {this.Dir}";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ namespace Sandbox {
|
|||
internal static class Program {
|
||||
|
||||
private static void Main() {
|
||||
//TextInputWrapper.Current = new TextInputWrapper.DesktopGl<TextInputEventArgs>((w, c) => w.TextInput += c);
|
||||
TextInputWrapper.Current = new TextInputWrapper.DesktopGl<TextInputEventArgs>((w, c) => w.TextInput += c);
|
||||
using (var game = new GameImpl())
|
||||
game.Run();
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MLEM.Data\MLEM.Data.csproj" />
|
||||
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
|
||||
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
|
||||
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />
|
||||
|
|
Loading…
Reference in a new issue