diff --git a/SpineViewer/Dialogs/ConvertFileFormatDialog.Designer.cs b/SpineViewer/Dialogs/ConvertFileFormatDialog.Designer.cs index a5b8d32..c9b298f 100644 --- a/SpineViewer/Dialogs/ConvertFileFormatDialog.Designer.cs +++ b/SpineViewer/Dialogs/ConvertFileFormatDialog.Designer.cs @@ -37,7 +37,7 @@ label1 = new Label(); label4 = new Label(); label3 = new Label(); - comboBox_Version = new ComboBox(); + comboBox_SourceVersion = new ComboBox(); tableLayoutPanel2 = new TableLayoutPanel(); button_Ok = new Button(); button_Cancel = new Button(); @@ -75,7 +75,7 @@ tableLayoutPanel1.Controls.Add(label1, 0, 4); tableLayoutPanel1.Controls.Add(label4, 0, 0); tableLayoutPanel1.Controls.Add(label3, 0, 3); - tableLayoutPanel1.Controls.Add(comboBox_Version, 1, 3); + tableLayoutPanel1.Controls.Add(comboBox_SourceVersion, 1, 3); tableLayoutPanel1.Controls.Add(tableLayoutPanel2, 0, 6); tableLayoutPanel1.Controls.Add(listBox_FilePath, 0, 2); tableLayoutPanel1.Controls.Add(button_SelectSkel, 0, 1); @@ -166,14 +166,14 @@ // // comboBox_Version // - comboBox_Version.Anchor = AnchorStyles.Left; - comboBox_Version.DropDownStyle = ComboBoxStyle.DropDownList; - comboBox_Version.FormattingEnabled = true; - comboBox_Version.Location = new Point(146, 303); - comboBox_Version.Name = "comboBox_Version"; - comboBox_Version.Size = new Size(182, 32); - comboBox_Version.Sorted = true; - comboBox_Version.TabIndex = 13; + comboBox_SourceVersion.Anchor = AnchorStyles.Left; + comboBox_SourceVersion.DropDownStyle = ComboBoxStyle.DropDownList; + comboBox_SourceVersion.FormattingEnabled = true; + comboBox_SourceVersion.Location = new Point(146, 303); + comboBox_SourceVersion.Name = "comboBox_Version"; + comboBox_SourceVersion.Size = new Size(182, 32); + comboBox_SourceVersion.Sorted = true; + comboBox_SourceVersion.TabIndex = 13; // // tableLayoutPanel2 // @@ -338,7 +338,7 @@ private TableLayoutPanel tableLayoutPanel1; private Label label4; private Label label3; - private ComboBox comboBox_Version; + private ComboBox comboBox_SourceVersion; private TableLayoutPanel tableLayoutPanel2; private Button button_Ok; private Button button_Cancel; diff --git a/SpineViewer/Dialogs/ConvertFileFormatDialog.cs b/SpineViewer/Dialogs/ConvertFileFormatDialog.cs index edf0746..0fca2ad 100644 --- a/SpineViewer/Dialogs/ConvertFileFormatDialog.cs +++ b/SpineViewer/Dialogs/ConvertFileFormatDialog.cs @@ -14,16 +14,22 @@ namespace SpineViewer.Dialogs public partial class ConvertFileFormatDialog : Form { public string[] SkelPaths { get; private set; } - public Spine.Version Version { get; private set; } - public bool ConvertToJson { get; private set; } + public Spine.Version SourceVersion { get; private set; } + public Spine.Version TargetVersion { get; private set; } + public bool JsonSource { get; private set; } + public bool JsonTarget { get; private set; } public ConvertFileFormatDialog() { InitializeComponent(); - comboBox_Version.DataSource = VersionHelper.Versions.ToList(); - comboBox_Version.DisplayMember = "Value"; - comboBox_Version.ValueMember = "Key"; - comboBox_Version.SelectedValue = Spine.Version.V38; + comboBox_SourceVersion.DataSource = VersionHelper.Versions.ToList(); + comboBox_SourceVersion.DisplayMember = "Value"; + comboBox_SourceVersion.ValueMember = "Key"; + comboBox_SourceVersion.SelectedValue = Spine.Version.V38; + //comboBox_TargetVersion.DataSource = VersionHelper.Versions.ToList(); + //comboBox_TargetVersion.DisplayMember = "Value"; + //comboBox_TargetVersion.ValueMember = "Key"; + //comboBox_TargetVersion.SelectedValue = Spine.Version.V38; } private void ConvertFileFormatDialog_Load(object sender, EventArgs e) @@ -44,7 +50,10 @@ namespace SpineViewer.Dialogs private void button_Ok_Click(object sender, EventArgs e) { - var version = (Spine.Version)comboBox_Version.SelectedValue; + var sourceVersion = (Spine.Version)comboBox_SourceVersion.SelectedValue; + var targetVersion = (Spine.Version)comboBox_SourceVersion.SelectedValue; // TODO: 增加目标版本 + var jsonSource = radioButton_JsonSource.Checked; + var jsonTarget = radioButton_JsonTarget.Checked; if (listBox_FilePath.Items.Count <= 0) { @@ -61,15 +70,29 @@ namespace SpineViewer.Dialogs } } - if (!SkeletonConverter.ImplementedVersions.Contains(version)) + if (!SkeletonConverter.ImplementedVersions.Contains(sourceVersion)) { - MessageBox.Show($"{version.String()} 版本尚未实现(咕咕咕~)", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show($"{sourceVersion.String()} 版本尚未实现(咕咕咕~)", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + if (!SkeletonConverter.ImplementedVersions.Contains(targetVersion)) + { + MessageBox.Show($"{targetVersion.String()} 版本尚未实现(咕咕咕~)", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information); + return; + } + + if (jsonSource == jsonTarget && sourceVersion == targetVersion) + { + MessageBox.Show($"不需要转换相同的格式和版本", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } SkelPaths = listBox_FilePath.Items.Cast().ToArray(); - Version = version; - ConvertToJson = radioButton_BinarySource.Checked; + SourceVersion = sourceVersion; + TargetVersion = targetVersion; + JsonSource = jsonSource; + JsonTarget = jsonTarget; DialogResult = DialogResult.OK; } diff --git a/SpineViewer/MainForm.cs b/SpineViewer/MainForm.cs index 96a1a2a..c4a7bd8 100644 --- a/SpineViewer/MainForm.cs +++ b/SpineViewer/MainForm.cs @@ -175,15 +175,26 @@ namespace SpineViewer var worker = sender as BackgroundWorker; var arguments = e.Argument as Dialogs.ConvertFileFormatDialog; var skelPaths = arguments.SkelPaths; - var version = arguments.Version; - var convertToJson = arguments.ConvertToJson; - var newSuffix = convertToJson ? ".json" : ".skel"; + var srcVersion = arguments.SourceVersion; + var tgtVersion = arguments.TargetVersion; + var jsonSource = arguments.JsonSource; + var jsonTarget = arguments.JsonTarget; + var newSuffix = jsonTarget ? ".json" : ".skel"; + + if (jsonTarget == jsonSource) + { + if (tgtVersion == srcVersion) + return; + else + newSuffix += $".{tgtVersion.ToString().ToLower()}"; // TODO: 仅转换版本的情况下考虑文件覆盖问题 + } int totalCount = skelPaths.Length; int success = 0; int error = 0; - SkeletonConverter cvter = SkeletonConverter.New(version); + SkeletonConverter srcCvter = SkeletonConverter.New(srcVersion); + SkeletonConverter tgtCvter = tgtVersion == srcVersion ? srcCvter : SkeletonConverter.New(tgtVersion); worker.ReportProgress(0, $"已处理 0/{totalCount}"); for (int i = 0; i < totalCount; i++) @@ -199,10 +210,9 @@ namespace SpineViewer try { - if (convertToJson) - cvter.BinaryToJson(skelPath, newPath); - else - cvter.JsonToBinary(skelPath, newPath); + var root = jsonSource ? srcCvter.ReadJson(skelPath) : srcCvter.ReadBinary(skelPath); + if (tgtVersion != srcVersion) root = srcCvter.ToVersion(root, tgtVersion); + if (jsonTarget) tgtCvter.WriteJson(root, newPath); else tgtCvter.WriteBinary(root, newPath); success++; } catch (Exception ex) diff --git a/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs b/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs index f6ba84c..a543e1a 100644 --- a/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs +++ b/SpineViewer/Spine/Implementations/SkeletonConverter/SkeletonConverter38.cs @@ -19,7 +19,7 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter private readonly List idx2event = []; - protected override JsonObject ReadBinary(string binPath) + public override JsonObject ReadBinary(string binPath) { var root = new JsonObject(); using var input = File.OpenRead(binPath); @@ -809,7 +809,7 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter private readonly Dictionary path2idx = []; private readonly Dictionary event2idx = []; - protected override void WriteBinary(JsonObject root, string binPath, bool nonessential = false) + public override void WriteBinary(JsonObject root, string binPath, bool nonessential = false) { this.nonessential = nonessential; this.root = root; @@ -902,6 +902,16 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter } + public override JsonObject ToVersion(JsonObject root, Version version) + { + root = version switch + { + Version.V38 => root.DeepClone().AsObject(), + _ => throw new NotImplementedException(), + }; + return root; + } + //public void WriteFloatArray(float[] array) //{ // foreach (var i in array) diff --git a/SpineViewer/Spine/SkeletonConverter.cs b/SpineViewer/Spine/SkeletonConverter.cs index 4a62d0a..b5f39cd 100644 --- a/SpineViewer/Spine/SkeletonConverter.cs +++ b/SpineViewer/Spine/SkeletonConverter.cs @@ -82,17 +82,17 @@ namespace SpineViewer.Spine /// /// 读取二进制骨骼文件并构造 Json 对象 /// - protected abstract JsonObject ReadBinary(string binPath); + public abstract JsonObject ReadBinary(string binPath); /// /// 将 Json 对象写入二进制骨骼文件 /// - protected abstract void WriteBinary(JsonObject root, string binPath, bool nonessential = false); + public abstract void WriteBinary(JsonObject root, string binPath, bool nonessential = false); /// /// 读取 Json 对象 /// - private JsonObject ReadJson(string jsonPath) + public JsonObject ReadJson(string jsonPath) { using var input = File.OpenRead(jsonPath); if (JsonNode.Parse(input) is JsonObject root) @@ -104,7 +104,7 @@ namespace SpineViewer.Spine /// /// 写入 Json 对象 /// - private void WriteJson(JsonObject root, string jsonPath) + public void WriteJson(JsonObject root, string jsonPath) { using var output = File.Create(jsonPath); using var writer = new Utf8JsonWriter(output, jsonWriterOptions); @@ -112,20 +112,9 @@ namespace SpineViewer.Spine } /// - /// 二进制转 Json 格式 + /// 转换到目标版本 /// - public void BinaryToJson(string binPath, string jsonPath) - { - WriteJson(ReadBinary(binPath), jsonPath); - } - - /// - /// Json 转二进制格式 - /// - public void JsonToBinary(string jsonPath, string binPath) - { - WriteBinary(ReadJson(jsonPath), binPath); - } + public abstract JsonObject ToVersion(JsonObject root, Version version); protected class SkeletonReader {