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 bool nonessential = false;
private JsonObject root = null; 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); reader = new(input);
root = []; var result = root = [];
root["skeleton"] = ReadSkeleton(); root["skeleton"] = ReadSkeleton();
ReadStrings(); ReadStrings();
root["bones"] = ReadBones(); root["bones"] = ReadBones();
@@ -33,9 +33,11 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter
root["events"] = ReadEvents(); root["events"] = ReadEvents();
root["aimations"] = ReadAnimations(); root["aimations"] = ReadAnimations();
using var output = new FileStream(jsonPath, FileMode.Create, FileAccess.Write, FileShare.None); reader = null;
using var writer = new Utf8JsonWriter(output, jsonWriterOptions); nonessential = false;
root.WriteTo(writer); root = null;
return result;
} }
private JsonObject ReadSkeleton() private JsonObject ReadSkeleton()
@@ -203,7 +205,6 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
return skins; return skins;
} }
@@ -232,15 +233,18 @@ namespace SpineViewer.Spine.Implementations.SkeletonConverter
private JsonObject ReadAnimations() private JsonObject ReadAnimations()
{ {
JsonObject animations = []; JsonObject animations = [];
int count = reader.ReadVarInt();
for (int i = 0; i < count; i++)
{
throw new NotImplementedException();
}
return animations; return animations;
} }
public override void JsonToBinary(string jsonPath, string binPath) protected override void WriteBinary(JsonObject root, string binPath)
{ {
// 按顺序把 json 的内容写入到骨骼 binary 缓冲区
// 然后在文件中依次写入文件头和骨骼数据
throw new NotImplementedException(); throw new NotImplementedException();
} }
} }
} }

View File

@@ -7,7 +7,8 @@ using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Text.Json.Nodes;
using System.Text.Encodings.Web;
namespace SpineViewer.Spine namespace SpineViewer.Spine
{ {
@@ -64,21 +65,62 @@ namespace SpineViewer.Spine
return (SkeletonConverter)Activator.CreateInstance(cvterType); return (SkeletonConverter)Activator.CreateInstance(cvterType);
} }
protected static readonly JsonWriterOptions jsonWriterOptions = new() /// <summary>
/// Json 格式控制
/// </summary>
private static readonly JsonWriterOptions jsonWriterOptions = new()
{ {
Indented = true, 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> /// <summary>
/// 二进制转 Json 格式 /// 二进制转 Json 格式
/// </summary> /// </summary>
public abstract void BinaryToJson(string binPath, string jsonPath); public void BinaryToJson(string binPath, string jsonPath)
{
WriteJson(ReadBinary(binPath), jsonPath);
}
/// <summary> /// <summary>
/// Json 转二进制格式 /// Json 转二进制格式
/// </summary> /// </summary>
public abstract void JsonToBinary(string jsonPath, string binPath); public void JsonToBinary(string jsonPath, string binPath)
{
WriteBinary(ReadJson(jsonPath), binPath);
}
protected class SkeletonReader protected class SkeletonReader
{ {
@@ -113,7 +155,7 @@ namespace SpineViewer.Spine
return (bytesBigEndian[0] << 24) return (bytesBigEndian[0] << 24)
| (bytesBigEndian[1] << 16) | (bytesBigEndian[1] << 16)
| (bytesBigEndian[2] << 8) | (bytesBigEndian[2] << 8)
| bytesBigEndian[3]; | bytesBigEndian[3];
} }
public long ReadLong() public long ReadLong()
{ {
@@ -293,6 +335,5 @@ namespace SpineViewer.Spine
} }
public void WriteFully(byte[] buffer, int offset, int length) => output.Write(buffer, offset, length); public void WriteFully(byte[] buffer, int offset, int length) => output.Write(buffer, offset, length);
} }
} }
} }