From dde9e6b82da6479ba928c5ecf0ac3c6cacf8ee71 Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Fri, 17 Oct 2025 16:24:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E4=BA=8C=E8=BF=9B?= =?UTF-8?q?=E5=88=B6=E5=BA=8F=E5=88=97=E5=8C=96=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RScriptSerializer.cs | 151 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 RScriptSerializer.cs diff --git a/RScriptSerializer.cs b/RScriptSerializer.cs new file mode 100644 index 0000000..cb7cd6e --- /dev/null +++ b/RScriptSerializer.cs @@ -0,0 +1,151 @@ +using System; +using System.IO; +using static Convention.RScript.RScriptContext; + +namespace Convention.RScript +{ + public static class RScriptSerializer + { + public static byte[] SerializeClass(SerializableClass data) + { + using (var stream = new MemoryStream()) + using (var writer = new BinaryWriter(stream)) + { + // 序列化 Sentences 数组 + writer.Write(data.Sentences?.Length ?? 0); + if (data.Sentences != null) + { + foreach (var sentence in data.Sentences) + { + writer.Write(sentence.content ?? ""); + writer.Write(sentence.info?.Length ?? 0); + if (sentence.info != null) + { + foreach (var info in sentence.info) + { + writer.Write(info ?? ""); + } + } + writer.Write((int)sentence.mode); + } + } + + // 序列化 Labels 数组 + writer.Write(data.Labels?.Length ?? 0); + if (data.Labels != null) + { + foreach (var label in data.Labels) + { + writer.Write(label.Item1 ?? ""); + writer.Write(label.Item2); + } + } + + // 序列化 NamespaceLayer 数组 + writer.Write(data.NamespaceLayer?.Length ?? 0); + if (data.NamespaceLayer != null) + { + foreach (var layer in data.NamespaceLayer) + { + writer.Write(layer.Item1); + writer.Write(layer.Item2); + } + } + + // 序列化 NamespaceLabels 数组 + writer.Write(data.NamespaceLabels?.Length ?? 0); + if (data.NamespaceLabels != null) + { + foreach (var nsLabel in data.NamespaceLabels) + { + writer.Write(nsLabel.Item1 ?? ""); + writer.Write(nsLabel.Item2); + } + } + + // 这里需要根据 ExpressionParser.SerializableParser 的结构来序列化 + // writer.Write(...); // CompileParser 的序列化 + + return stream.ToArray(); + } + } + + public static SerializableClass DeserializeClass(byte[] data) + { + using (var stream = new MemoryStream(data)) + using (var reader = new BinaryReader(stream)) + { + var result = new SerializableClass(); + + // 反序列化 Sentences 数组 + int sentencesLength = reader.ReadInt32(); + if (sentencesLength > 0) + { + result.Sentences = new RScriptSentence[sentencesLength]; + for (int i = 0; i < sentencesLength; i++) + { + var sentence = new RScriptSentence(); + sentence.content = reader.ReadString(); + + int infoLength = reader.ReadInt32(); + if (infoLength > 0) + { + sentence.info = new string[infoLength]; + for (int j = 0; j < infoLength; j++) + { + sentence.info[j] = reader.ReadString(); + } + } + + sentence.mode = (RScriptSentence.Mode)reader.ReadInt32(); + result.Sentences[i] = sentence; + } + } + + // 反序列化 Labels 数组 + int labelsLength = reader.ReadInt32(); + if (labelsLength > 0) + { + result.Labels = new Tuple[labelsLength]; + for (int i = 0; i < labelsLength; i++) + { + string item1 = reader.ReadString(); + int item2 = reader.ReadInt32(); + result.Labels[i] = Tuple.Create(item1, item2); + } + } + + // 反序列化 NamespaceLayer 数组 + int namespaceLayerLength = reader.ReadInt32(); + if (namespaceLayerLength > 0) + { + result.NamespaceLayer = new Tuple[namespaceLayerLength]; + for (int i = 0; i < namespaceLayerLength; i++) + { + int item1 = reader.ReadInt32(); + int item2 = reader.ReadInt32(); + result.NamespaceLayer[i] = Tuple.Create(item1, item2); + } + } + + // 反序列化 NamespaceLabels 数组 + int namespaceLabelsLength = reader.ReadInt32(); + if (namespaceLabelsLength > 0) + { + result.NamespaceLabels = new Tuple[namespaceLabelsLength]; + for (int i = 0; i < namespaceLabelsLength; i++) + { + string item1 = reader.ReadString(); + int item2 = reader.ReadInt32(); + result.NamespaceLabels[i] = Tuple.Create(item1, item2); + } + } + + // 反序列化 CompileParser + // result.CompileParser = ...; // 根据具体结构实现 + + return result; + } + } + } +}