1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

added some json utilities to Data

This commit is contained in:
Ellpeck 2020-02-27 18:56:49 +01:00
parent ae0fc22de3
commit 9f870738b0
12 changed files with 224 additions and 15 deletions

View 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);
}
}
}
}
}

View 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;
}
}
}

View 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;
}
}
}

View 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));
}
}
}

View 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));
}
}
}

View 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));
}
}
}

View 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));
}
}
}

View file

@ -14,15 +14,15 @@
#---------------------------------- Content ---------------------------------# #---------------------------------- Content ---------------------------------#
#begin Tiled/Map.tmx #begin Fonts/TestFont.spritefont
/importer:TiledMapImporter /importer:FontDescriptionImporter
/processor:TiledMapProcessor /processor:FontDescriptionProcessor
/build:Tiled/Map.tmx /processorParam:PremultiplyAlpha=True
/processorParam:TextureFormat=Compressed
/build:Fonts/TestFont.spritefont
#begin Tiled/Tileset.tsx #begin Test.json
/importer:TiledMapTilesetImporter /copy:Test.json
/processor:TiledMapTilesetProcessor
/build:Tiled/Tileset.tsx
#begin Textures/Test.png #begin Textures/Test.png
/importer:TextureImporter /importer:TextureImporter
@ -36,10 +36,13 @@
/processorParam:TextureFormat=Color /processorParam:TextureFormat=Color
/build:Textures/Test.png /build:Textures/Test.png
#begin Fonts/TestFont.spritefont #begin Tiled/Map.tmx
/importer:FontDescriptionImporter /importer:TiledMapImporter
/processor:FontDescriptionProcessor /processor:TiledMapProcessor
/processorParam:PremultiplyAlpha=True /build:Tiled/Map.tmx
/processorParam:TextureFormat=Compressed
/build:Fonts/TestFont.spritefont #begin Tiled/Tileset.tsx
/importer:TiledMapTilesetImporter
/processor:TiledMapTilesetProcessor
/build:Tiled/Tileset.tsx

View file

@ -0,0 +1,7 @@
{
"Vec": "10 20",
"Point": "20 30",
"Rectangle": "1 2 3 4",
"RectangleF": "4 5 6 7",
"Dir": "Left"
}

View file

@ -1,12 +1,15 @@
using System; using System;
using System.IO;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using MLEM.Cameras; using MLEM.Cameras;
using MLEM.Data;
using MLEM.Extended.Extensions; using MLEM.Extended.Extensions;
using MLEM.Extended.Tiled; using MLEM.Extended.Tiled;
using MLEM.Extensions; using MLEM.Extensions;
using MLEM.Font; using MLEM.Font;
using MLEM.Misc;
using MLEM.Startup; using MLEM.Startup;
using MLEM.Textures; using MLEM.Textures;
using MLEM.Ui; using MLEM.Ui;
@ -14,6 +17,8 @@ using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
using MonoGame.Extended; using MonoGame.Extended;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
using Newtonsoft.Json.Linq;
using RectangleF = MonoGame.Extended.RectangleF;
namespace Sandbox { namespace Sandbox {
public class GameImpl : MlemGame { 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.AutoLeft, new Vector2(100, 10)));
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10))); panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
this.UiSystem.Add("Panel", panel);*/ 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) { protected override void DoUpdate(GameTime gameTime) {
@ -88,5 +112,19 @@ namespace Sandbox {
base.DoDraw(gameTime); 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}";
}
}
} }
} }

View file

@ -5,7 +5,7 @@ namespace Sandbox {
internal static class Program { internal static class Program {
private static void Main() { 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()) using (var game = new GameImpl())
game.Run(); game.Run();
} }

View file

@ -6,6 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MLEM.Data\MLEM.Data.csproj" />
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" /> <ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
<ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" /> <ProjectReference Include="..\MLEM.Startup\MLEM.Startup.csproj" />
<ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" /> <ProjectReference Include="..\MLEM.Ui\MLEM.Ui.csproj" />