From 5009ef479f5e74a9d1feac3216d047d32d72bee1 Mon Sep 17 00:00:00 2001 From: ww-rm Date: Mon, 17 Mar 2025 21:11:09 +0800 Subject: [PATCH] add something --- .../SkeletonConverter/SkeletonConverter38.cs | 100 +++++++++++++++++- SpineViewer/Spine/SkeletonConverter.cs | 12 +-- 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs b/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs index 57400af..c3ef920 100644 --- a/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs +++ b/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs @@ -801,9 +801,105 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter } } - protected override void WriteBinary(JsonObject root, string binPath) + private SkeletonWriter writer; + private readonly Dictionary bone2idx = []; + private readonly Dictionary slot2idx = []; + private readonly Dictionary ik2idx = []; + private readonly Dictionary transform2idx = []; + private readonly Dictionary path2idx = []; + private readonly Dictionary event2idx = []; + + protected override void WriteBinary(JsonObject root, string binPath, bool nonessential = false) { - throw new NotImplementedException(); + this.nonessential = nonessential; + using var outputBody = new MemoryStream(); // 先把主体写入内存缓冲区 + + writer = new(outputBody); + this.root = root; + + WriteBones(); + WriteSlots(); + WriteIK(); + WriteTransform(); + WritePath(); + WriteSkins(); + WriteEvents(); + WriteAnimations(); + + using var output = File.Create(binPath); // 将数据写入文件 + writer = new(output); + + WriteSkeleton(); + WriteStrings(); + output.Write(outputBody.GetBuffer()); + + writer = null; + this.root = null; + } + + private void WriteSkeleton() + { + JsonObject skeleton = root["skeleton"].AsObject(); + writer.WriteString(skeleton["hash"].GetValue()); + writer.WriteString(skeleton["spine"].GetValue()); + if (skeleton.TryGetPropertyValue("x", out var x)) writer.WriteFloat(x.GetValue()); else writer.WriteFloat(0); + if (skeleton.TryGetPropertyValue("y", out var y)) writer.WriteFloat(y.GetValue()); else writer.WriteFloat(0); + if (skeleton.TryGetPropertyValue("width", out var width)) writer.WriteFloat(width.GetValue()); else writer.WriteFloat(0); + if (skeleton.TryGetPropertyValue("height", out var height)) writer.WriteFloat(height.GetValue()); else writer.WriteFloat(0); + writer.WriteBoolean(nonessential); + if (nonessential) + { + if (skeleton.TryGetPropertyValue("fps", out var fps)) writer.WriteFloat(fps.GetValue()); else writer.WriteFloat(30); + if (skeleton.TryGetPropertyValue("images", out var images)) writer.WriteString(images.GetValue()); else writer.WriteString(null); + if (skeleton.TryGetPropertyValue("audio", out var audio)) writer.WriteString(audio.GetValue()); else writer.WriteString(null); + } + } + + private void WriteStrings() + { + writer.WriteVarInt(writer.StringTable.Count); + foreach (var s in writer.StringTable) + writer.WriteString(s); + } + + private void WriteBones() + { + + } + + private void WriteSlots() + { + + } + + private void WriteIK() + { + + } + + private void WriteTransform() + { + + } + + private void WritePath() + { + + } + + private void WriteSkins() + { + + } + + private void WriteEvents() + { + + } + + private void WriteAnimations() + { + } //public void WriteFloatArray(float[] array) diff --git a/SpineViewer/Spine/SkeletonConverter.cs b/SpineViewer/Spine/SkeletonConverter.cs index b36e396..4a62d0a 100644 --- a/SpineViewer/Spine/SkeletonConverter.cs +++ b/SpineViewer/Spine/SkeletonConverter.cs @@ -87,7 +87,7 @@ namespace SpineViewer.Spine /// /// 将 Json 对象写入二进制骨骼文件 /// - protected abstract void WriteBinary(JsonObject root, string binPath); + protected abstract void WriteBinary(JsonObject root, string binPath, bool nonessential = false); /// /// 读取 Json 对象 @@ -234,7 +234,7 @@ namespace SpineViewer.Spine { protected byte[] buffer = new byte[32]; protected byte[] bytesBigEndian = new byte[8]; - public readonly List Strings = new(32); + public readonly List StringTable = new(32); protected Stream output; public SkeletonWriter(Stream output) { this.output = output; } @@ -323,18 +323,18 @@ namespace SpineViewer.Spine System.Text.Encoding.UTF8.GetBytes(val, 0, val.Length, buffer, 0); WriteFully(buffer, 0, byteCount); } - public void WriteStringRef(List strings, string val) + public void WriteStringRef(string val) { if (val is null) { WriteVarInt(0); return; } - int index = strings.IndexOf(val); + int index = StringTable.IndexOf(val); if (index < 0) { - strings.Add(val); - index = strings.Count - 1; + StringTable.Add(val); + index = StringTable.Count - 1; } WriteVarInt(index + 1); }