From 5e3bd972e58fa7db8e4c2c4c8e7c773538938639 Mon Sep 17 00:00:00 2001 From: ww-rm Date: Sat, 29 Mar 2025 17:04:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E5=8A=A8GetVersion=E8=87=B3SpineHelpe?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpineViewer/Controls/SkelFileListBox.cs | 7 +-- SpineViewer/Controls/SpineListView.cs | 4 +- SpineViewer/MainForm.cs | 2 +- SpineViewer/Spine/Spine.cs | 60 +------------------------ SpineViewer/Spine/SpineHelper.cs | 58 ++++++++++++++++++++++-- 5 files changed, 62 insertions(+), 69 deletions(-) diff --git a/SpineViewer/Controls/SkelFileListBox.cs b/SpineViewer/Controls/SkelFileListBox.cs index a94fafb..512f8f3 100644 --- a/SpineViewer/Controls/SkelFileListBox.cs +++ b/SpineViewer/Controls/SkelFileListBox.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; +using SpineViewer.Spine; namespace SpineViewer.Controls { @@ -33,14 +34,14 @@ namespace SpineViewer.Controls { if (File.Exists(path)) { - if (Spine.Spine.CommonSkelSuffix.Contains(Path.GetExtension(path).ToLower())) + if (SpineHelper.CommonSkelSuffix.Contains(Path.GetExtension(path).ToLower())) listBox.Items.Add(Path.GetFullPath(path)); } else if (Directory.Exists(path)) { foreach (var file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)) { - if (Spine.Spine.CommonSkelSuffix.Contains(Path.GetExtension(file).ToLower())) + if (SpineHelper.CommonSkelSuffix.Contains(Path.GetExtension(file).ToLower())) listBox.Items.Add(file); } } @@ -57,7 +58,7 @@ namespace SpineViewer.Controls { foreach (var file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)) { - if (Spine.Spine.CommonSkelSuffix.Contains(Path.GetExtension(file).ToLower())) + if (SpineHelper.CommonSkelSuffix.Contains(Path.GetExtension(file).ToLower())) listBox.Items.Add(file); } } diff --git a/SpineViewer/Controls/SpineListView.cs b/SpineViewer/Controls/SpineListView.cs index 18d2db4..3586474 100644 --- a/SpineViewer/Controls/SpineListView.cs +++ b/SpineViewer/Controls/SpineListView.cs @@ -199,14 +199,14 @@ namespace SpineViewer.Controls { if (File.Exists(path)) { - if (Spine.Spine.CommonSkelSuffix.Contains(Path.GetExtension(path).ToLower())) + if (SpineHelper.CommonSkelSuffix.Contains(Path.GetExtension(path).ToLower())) validPaths.Add(path); } else if (Directory.Exists(path)) { foreach (var file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)) { - if (Spine.Spine.CommonSkelSuffix.Contains(Path.GetExtension(file).ToLower())) + if (SpineHelper.CommonSkelSuffix.Contains(Path.GetExtension(file).ToLower())) validPaths.Add(file); } } diff --git a/SpineViewer/MainForm.cs b/SpineViewer/MainForm.cs index 842abe7..8d32a1c 100644 --- a/SpineViewer/MainForm.cs +++ b/SpineViewer/MainForm.cs @@ -204,7 +204,7 @@ namespace SpineViewer { try { - srcCvter = SkeletonConverter.New(Spine.Spine.GetVersion(skelPath)); + srcCvter = SkeletonConverter.New(SpineHelper.GetVersion(skelPath)); } catch (Exception ex) { diff --git a/SpineViewer/Spine/Spine.cs b/SpineViewer/Spine/Spine.cs index 917bcf2..44ec418 100644 --- a/SpineViewer/Spine/Spine.cs +++ b/SpineViewer/Spine/Spine.cs @@ -23,11 +23,6 @@ namespace SpineViewer.Spine /// public abstract class Spine : ImplementationResolver, SFML.Graphics.Drawable, IDisposable { - /// - /// 常规骨骼文件后缀集合 - /// - public static readonly ImmutableHashSet CommonSkelSuffix = [".skel", ".json"]; - /// /// 空动画标记 /// @@ -48,66 +43,13 @@ namespace SpineViewer.Spine /// public const float SCALE_MIN = 0.001f; - /// - /// 尝试检测骨骼文件版本 - /// - /// - /// - /// - public static SpineVersion GetVersion(string skelPath) - { - string versionString = null; - using var input = File.OpenRead(skelPath); - var reader = new SkeletonConverter.BinaryReader(input); - - // try json format - try - { - if (JsonNode.Parse(input) is JsonObject root && root.TryGetPropertyValue("skeleton", out var node) && - node is JsonObject _skeleton && _skeleton.TryGetPropertyValue("spine", out var _version)) - versionString = (string)_version; - } - catch { } - - // try v4 binary format - if (versionString is null) - { - try - { - input.Position = 0; - var hash = reader.ReadLong(); - var versionPosition = input.Position; - var versionByteCount = reader.ReadVarInt(); - input.Position = versionPosition; - if (versionByteCount <= 13) - versionString = reader.ReadString(); - } - catch { } - } - - // try v3 binary format - if (versionString is null) - { - try - { - input.Position = 0; - var hash = reader.ReadString(); - versionString = reader.ReadString(); - } - catch { } - } - - if (versionString is null) - throw new InvalidDataException($"No verison detected: {skelPath}"); - return SpineHelper.GetVersion(versionString); - } /// /// 创建特定版本的 Spine /// public static Spine New(SpineVersion version, string skelPath, string? atlasPath = null) { - if (version == SpineVersion.Auto) version = GetVersion(skelPath); + if (version == SpineVersion.Auto) version = SpineHelper.GetVersion(skelPath); var spine = New(version, [skelPath, atlasPath]); // 统一初始化 diff --git a/SpineViewer/Spine/SpineHelper.cs b/SpineViewer/Spine/SpineHelper.cs index c23ae25..209ed65 100644 --- a/SpineViewer/Spine/SpineHelper.cs +++ b/SpineViewer/Spine/SpineHelper.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; +using System.Text.Json.Nodes; using System.Threading.Tasks; namespace SpineViewer.Spine @@ -87,14 +89,62 @@ namespace SpineViewer.Spine } /// - /// 获取字符串对应的版本号 + /// 常规骨骼文件后缀集合 /// - /// + public static readonly ImmutableHashSet CommonSkelSuffix = [".skel", ".json"]; + + /// + /// 尝试检测骨骼文件版本 + /// + /// /// /// - public static SpineVersion GetVersion(string versionString) + public static SpineVersion GetVersion(string skelPath) { - ArgumentNullException.ThrowIfNullOrEmpty(versionString); + string versionString = null; + using var input = File.OpenRead(skelPath); + var reader = new SkeletonConverter.BinaryReader(input); + + // try json format + try + { + if (JsonNode.Parse(input) is JsonObject root && root.TryGetPropertyValue("skeleton", out var node) && + node is JsonObject _skeleton && _skeleton.TryGetPropertyValue("spine", out var _version)) + versionString = (string)_version; + } + catch { } + + // try v4 binary format + if (versionString is null) + { + try + { + input.Position = 0; + var hash = reader.ReadLong(); + var versionPosition = input.Position; + var versionByteCount = reader.ReadVarInt(); + input.Position = versionPosition; + if (versionByteCount <= 13) + versionString = reader.ReadString(); + } + catch { } + } + + // try v3 binary format + if (versionString is null) + { + try + { + input.Position = 0; + var hash = reader.ReadString(); + versionString = reader.ReadString(); + } + catch { } + } + + if (versionString is null) + throw new InvalidDataException($"No verison detected: {skelPath}"); + if (versionString.StartsWith("2.1.")) return SpineVersion.V21; else if (versionString.StartsWith("3.6.")) return SpineVersion.V36; else if (versionString.StartsWith("3.7.")) return SpineVersion.V37;