some change

This commit is contained in:
ww-rm
2025-03-13 17:29:56 +08:00
parent b4119087fb
commit d251c94638
2 changed files with 63 additions and 18 deletions

View File

@@ -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();
}
}
}

View File

@@ -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()
/// <summary>
/// Json 格式控制
/// </summary>
private static readonly JsonWriterOptions jsonWriterOptions = new()
{
Indented = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
/// <summary>
/// 读取二进制骨骼文件并构造 Json 对象
/// </summary>
protected abstract JsonObject ReadBinary(string binPath);
/// <summary>
/// 将 Json 对象写入二进制骨骼文件
/// </summary>
protected abstract void WriteBinary(JsonObject root, string binPath);
/// <summary>
/// 读取 Json 对象
/// </summary>
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");
}
/// <summary>
/// 写入 Json 对象
/// </summary>
private void WriteJson(JsonObject root, string jsonPath)
{
using var output = File.Create(jsonPath);
using var writer = new Utf8JsonWriter(output, jsonWriterOptions);
root.WriteTo(writer);
}
/// <summary>
/// 二进制转 Json 格式
/// </summary>
public abstract void BinaryToJson(string binPath, string jsonPath);
public void BinaryToJson(string binPath, string jsonPath)
{
WriteJson(ReadBinary(binPath), jsonPath);
}
/// <summary>
/// Json 转二进制格式
/// </summary>
public abstract void JsonToBinary(string jsonPath, string binPath);
public void JsonToBinary(string jsonPath, string binPath)
{
WriteBinary(ReadJson(jsonPath), binPath);
}
protected class SkeletonReader
{
@@ -293,6 +335,5 @@ namespace SpineViewer.Spine
}
public void WriteFully(byte[] buffer, int offset, int length) => output.Write(buffer, offset, length);
}
}
}