From d251c94638bc4d93a684e30ee095441b9bc55341 Mon Sep 17 00:00:00 2001 From: ww-rm Date: Thu, 13 Mar 2025 17:29:56 +0800 Subject: [PATCH] some change --- .../SkeletonConverter/SkeletonConverter38.cs | 26 +++++---- SpineViewer/Spine/SkeletonConverter.cs | 55 ++++++++++++++++--- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs b/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs index f5b970f..0299c02 100644 --- a/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs +++ b/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs @@ -16,12 +16,12 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter private bool nonessential = false; private JsonObject root = null; - public override void BinaryToJson(string binPath, string jsonPath) + protected override JsonObject ReadBinary(string binPath) { - using var input = new FileStream(binPath, FileMode.Open, FileAccess.Read, FileShare.Read); + using var input = File.OpenRead(binPath); reader = new(input); - root = []; + var result = root = []; root["skeleton"] = ReadSkeleton(); ReadStrings(); root["bones"] = ReadBones(); @@ -33,9 +33,11 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter root["events"] = ReadEvents(); root["aimations"] = ReadAnimations(); - using var output = new FileStream(jsonPath, FileMode.Create, FileAccess.Write, FileShare.None); - using var writer = new Utf8JsonWriter(output, jsonWriterOptions); - root.WriteTo(writer); + reader = null; + nonessential = false; + root = null; + + return result; } private JsonObject ReadSkeleton() @@ -203,7 +205,6 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter { throw new NotImplementedException(); } - return skins; } @@ -232,15 +233,18 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter private JsonObject ReadAnimations() { JsonObject animations = []; - + int count = reader.ReadVarInt(); + for (int i = 0; i < count; i++) + { + throw new NotImplementedException(); + } return animations; } - public override void JsonToBinary(string jsonPath, string binPath) + protected override void WriteBinary(JsonObject root, string binPath) { - // 按顺序把 json 的内容写入到骨骼 binary 缓冲区 - // 然后在文件中依次写入文件头和骨骼数据 throw new NotImplementedException(); } + } } diff --git a/SpineViewer/Spine/SkeletonConverter.cs b/SpineViewer/Spine/SkeletonConverter.cs index bfb00dc..b663d2d 100644 --- a/SpineViewer/Spine/SkeletonConverter.cs +++ b/SpineViewer/Spine/SkeletonConverter.cs @@ -7,7 +7,8 @@ using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Text.Json; -using System.Threading.Tasks; +using System.Text.Json.Nodes; +using System.Text.Encodings.Web; namespace SpineViewer.Spine { @@ -64,21 +65,62 @@ namespace SpineViewer.Spine return (SkeletonConverter)Activator.CreateInstance(cvterType); } - protected static readonly JsonWriterOptions jsonWriterOptions = new() + /// + /// Json 格式控制 + /// + private static readonly JsonWriterOptions jsonWriterOptions = new() { Indented = true, - Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; + /// + /// 读取二进制骨骼文件并构造 Json 对象 + /// + protected abstract JsonObject ReadBinary(string binPath); + + /// + /// 将 Json 对象写入二进制骨骼文件 + /// + protected abstract void WriteBinary(JsonObject root, string binPath); + + /// + /// 读取 Json 对象 + /// + private JsonObject ReadJson(string jsonPath) + { + using var input = File.OpenRead(jsonPath); + if (JsonNode.Parse(input) is JsonObject root) + return root; + else + throw new InvalidOperationException($"{jsonPath} is not a valid json object"); + } + + /// + /// 写入 Json 对象 + /// + private void WriteJson(JsonObject root, string jsonPath) + { + using var output = File.Create(jsonPath); + using var writer = new Utf8JsonWriter(output, jsonWriterOptions); + root.WriteTo(writer); + } + /// /// 二进制转 Json 格式 /// - public abstract void BinaryToJson(string binPath, string jsonPath); + public void BinaryToJson(string binPath, string jsonPath) + { + WriteJson(ReadBinary(binPath), jsonPath); + } /// /// Json 转二进制格式 /// - public abstract void JsonToBinary(string jsonPath, string binPath); + public void JsonToBinary(string jsonPath, string binPath) + { + WriteBinary(ReadJson(jsonPath), binPath); + } protected class SkeletonReader { @@ -113,7 +155,7 @@ namespace SpineViewer.Spine return (bytesBigEndian[0] << 24) | (bytesBigEndian[1] << 16) | (bytesBigEndian[2] << 8) - | bytesBigEndian[3]; + | bytesBigEndian[3]; } public long ReadLong() { @@ -293,6 +335,5 @@ namespace SpineViewer.Spine } public void WriteFully(byte[] buffer, int offset, int length) => output.Write(buffer, offset, length); } - } }