diff --git a/MLEM.Data/ContentExtensions.cs b/MLEM.Data/ContentExtensions.cs new file mode 100644 index 0000000..891fceb --- /dev/null +++ b/MLEM.Data/ContentExtensions.cs @@ -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 Serializers = new Dictionary(); + + 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(this ContentManager content, JsonConverter converter) { + var serializer = GetJsonSerializer(content); + serializer.Converters.Add(converter); + } + + 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); + } + } + } + + } +} \ No newline at end of file diff --git a/MLEM.Data/Json/Direction2Converter.cs b/MLEM.Data/Json/Direction2Converter.cs new file mode 100644 index 0000000..ecceff2 --- /dev/null +++ b/MLEM.Data/Json/Direction2Converter.cs @@ -0,0 +1,18 @@ +using System; +using MLEM.Misc; +using Newtonsoft.Json; + +namespace MLEM.Data.Json { + public class Direction2Converter : JsonConverter { + + 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(reader.Value.ToString(), out var dir); + return dir; + } + + } +} \ No newline at end of file diff --git a/MLEM.Data/Json/JsonConverters.cs b/MLEM.Data/Json/JsonConverters.cs new file mode 100644 index 0000000..5e7cf0b --- /dev/null +++ b/MLEM.Data/Json/JsonConverters.cs @@ -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().ToArray(); + + public static JsonSerializer AddAll(JsonSerializer serializer) { + foreach (var converter in Converters) + serializer.Converters.Add(converter); + return serializer; + } + + } +} \ No newline at end of file diff --git a/MLEM.Data/Json/PointConverter.cs b/MLEM.Data/Json/PointConverter.cs new file mode 100644 index 0000000..76525bf --- /dev/null +++ b/MLEM.Data/Json/PointConverter.cs @@ -0,0 +1,19 @@ +using System; +using System.Globalization; +using Microsoft.Xna.Framework; +using Newtonsoft.Json; + +namespace MLEM.Data.Json { + public class PointConverter : JsonConverter { + + 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)); + } + + } +} \ No newline at end of file diff --git a/MLEM.Data/Json/RectangleConverter.cs b/MLEM.Data/Json/RectangleConverter.cs new file mode 100644 index 0000000..df6eaa6 --- /dev/null +++ b/MLEM.Data/Json/RectangleConverter.cs @@ -0,0 +1,23 @@ +using System; +using System.Globalization; +using Microsoft.Xna.Framework; +using Newtonsoft.Json; + +namespace MLEM.Data.Json { + public class RectangleConverter : JsonConverter { + + 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)); + } + + } +} \ No newline at end of file diff --git a/MLEM.Data/Json/RectangleFConverter.cs b/MLEM.Data/Json/RectangleFConverter.cs new file mode 100644 index 0000000..7261e6a --- /dev/null +++ b/MLEM.Data/Json/RectangleFConverter.cs @@ -0,0 +1,23 @@ +using System; +using System.Globalization; +using MLEM.Misc; +using Newtonsoft.Json; + +namespace MLEM.Data.Json { + public class RectangleFConverter : JsonConverter { + + 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)); + } + + } +} \ No newline at end of file diff --git a/MLEM.Data/Json/Vector2Converter.cs b/MLEM.Data/Json/Vector2Converter.cs new file mode 100644 index 0000000..2525daa --- /dev/null +++ b/MLEM.Data/Json/Vector2Converter.cs @@ -0,0 +1,19 @@ +using System; +using System.Globalization; +using Microsoft.Xna.Framework; +using Newtonsoft.Json; + +namespace MLEM.Data.Json { + public class Vector2Converter : JsonConverter { + + 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)); + } + + } +} \ No newline at end of file diff --git a/Sandbox/Content/Content.mgcb b/Sandbox/Content/Content.mgcb index 635a3e0..42a56b3 100644 --- a/Sandbox/Content/Content.mgcb +++ b/Sandbox/Content/Content.mgcb @@ -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 diff --git a/Sandbox/Content/Test.json b/Sandbox/Content/Test.json new file mode 100644 index 0000000..5f946da --- /dev/null +++ b/Sandbox/Content/Test.json @@ -0,0 +1,7 @@ +{ + "Vec": "10 20", + "Point": "20 30", + "Rectangle": "1 2 3 4", + "RectangleF": "4 5 6 7", + "Dir": "Left" +} diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 2c76ec6..e90a64c 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -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"); + 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}"; + } + + } + } } \ No newline at end of file diff --git a/Sandbox/Program.cs b/Sandbox/Program.cs index 082653c..cd8cb41 100644 --- a/Sandbox/Program.cs +++ b/Sandbox/Program.cs @@ -5,7 +5,7 @@ namespace Sandbox { internal static class Program { private static void Main() { - //TextInputWrapper.Current = new TextInputWrapper.DesktopGl((w, c) => w.TextInput += c); + TextInputWrapper.Current = new TextInputWrapper.DesktopGl((w, c) => w.TextInput += c); using (var game = new GameImpl()) game.Run(); } diff --git a/Sandbox/Sandbox.csproj b/Sandbox/Sandbox.csproj index 75b58ad..db72943 100644 --- a/Sandbox/Sandbox.csproj +++ b/Sandbox/Sandbox.csproj @@ -6,6 +6,7 @@ +