From 9fd3ec35be6e390bd5981c588498c3a4abd6e96b Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Fri, 12 Dec 2025 01:21:29 +0100 Subject: [PATCH] Core(`AtlasGroupJsonConverter`) - Move class into its own cs file --- PckStudio.Core/AtlasGroup.cs | 48 ++--------------- PckStudio.Core/AtlasGroupJsonConverter.cs | 64 +++++++++++++++++++++++ 2 files changed, 67 insertions(+), 45 deletions(-) create mode 100644 PckStudio.Core/AtlasGroupJsonConverter.cs diff --git a/PckStudio.Core/AtlasGroup.cs b/PckStudio.Core/AtlasGroup.cs index 70d81c95..c492a72d 100644 --- a/PckStudio.Core/AtlasGroup.cs +++ b/PckStudio.Core/AtlasGroup.cs @@ -19,58 +19,16 @@ using System; using System.Data.SqlTypes; using System.Drawing; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; namespace PckStudio.Core { - public class AtlasGroupJsonConverter : JsonConverter - { - public override AtlasGroup ReadJson(JsonReader reader, Type objectType, AtlasGroup existingValue, bool hasExistingValue, JsonSerializer serializer) - { - JObject jObject = JObject.Load(reader); - - if (!TryGetValue(jObject, "name", out string name) || !TryGetValue(jObject, "row", out int row) || !TryGetValue(jObject, "column", out int column)) - return default; - - int frameTime = 1; - int frameCount = 0; - int rowSpan = 0; - int columnSpan = 0; - ImageLayoutDirection direction = default; - bool isAnimation = TryGetValue(jObject, "frameTime", out frameTime) && - TryGetValue(jObject, "frameCount", out frameCount) && - TryGetValue(jObject, "direction", out direction); - bool isLargeTile = TryGetValue(jObject, "rowSpan", out rowSpan) && TryGetValue(jObject, "columnSpan", out columnSpan); - if (isAnimation && isLargeTile) - return new AtlasGroupLargeTileAnimation(name, row, column, rowSpan, columnSpan, frameCount, direction, frameTime); - if (isAnimation) - return new AtlasGroupAnimation(name, row, column, frameCount, direction, frameTime); - if (isLargeTile) - return new AtlasGroupLargeTile(name, row, column, rowSpan, columnSpan); - return default; - } - - bool TryGetValue(JObject jObject, string propertyName, out T value) - { - if (!jObject.TryGetValue(propertyName, out JToken token)) - { - value = default; - return false; - } - value = token.ToObject() ?? default; - return value is T; - } - - public override void WriteJson(JsonWriter writer, AtlasGroup value, JsonSerializer serializer) => serializer.Serialize(writer, value); - } - public abstract class AtlasGroup { - [JsonProperty("name", Required = Required.Always)] + [JsonProperty("name", Required = Required.Always, Order = 0)] public string Name { get; set; } - [JsonProperty("row", Required = Required.Always)] + [JsonProperty("row", Required = Required.Always, Order = 1)] public int Row { get; } - [JsonProperty("column", Required = Required.Always)] + [JsonProperty("column", Required = Required.Always, Order = 2)] public int Column { get; } [JsonIgnore] diff --git a/PckStudio.Core/AtlasGroupJsonConverter.cs b/PckStudio.Core/AtlasGroupJsonConverter.cs new file mode 100644 index 00000000..9187b439 --- /dev/null +++ b/PckStudio.Core/AtlasGroupJsonConverter.cs @@ -0,0 +1,64 @@ +/* Copyright (c) 2025-present miku-666 + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1.The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. +**/ +using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace PckStudio.Core +{ + public class AtlasGroupJsonConverter : JsonConverter + { + public override AtlasGroup ReadJson(JsonReader reader, Type objectType, AtlasGroup existingValue, bool hasExistingValue, JsonSerializer serializer) + { + JObject jObject = JObject.Load(reader); + + if (!TryGetValue(jObject, "name", out string name) || !TryGetValue(jObject, "row", out int row) || !TryGetValue(jObject, "column", out int column)) + return default; + + int frameTime = 1; + int frameCount = 0; + int rowSpan = 0; + int columnSpan = 0; + ImageLayoutDirection direction = default; + bool isAnimation = TryGetValue(jObject, "frameTime", out frameTime) && + TryGetValue(jObject, "frameCount", out frameCount) && + TryGetValue(jObject, "direction", out direction); + bool isLargeTile = TryGetValue(jObject, "rowSpan", out rowSpan) && TryGetValue(jObject, "columnSpan", out columnSpan); + if (isAnimation && isLargeTile) + return new AtlasGroupLargeTileAnimation(name, row, column, rowSpan, columnSpan, frameCount, direction, frameTime); + if (isAnimation) + return new AtlasGroupAnimation(name, row, column, frameCount, direction, frameTime); + if (isLargeTile) + return new AtlasGroupLargeTile(name, row, column, rowSpan, columnSpan); + return default; + } + + bool TryGetValue(JObject jObject, string propertyName, out T value) + { + if (!jObject.TryGetValue(propertyName, out JToken token)) + { + value = default; + return false; + } + value = token.ToObject() ?? default; + return value is T; + } + + public override void WriteJson(JsonWriter writer, AtlasGroup value, JsonSerializer serializer) => serializer.Serialize(writer, value); + } +} \ No newline at end of file