private void SetTagValue(TreeNode node, Tag tag) { if (tag is ByteArrayTag) { ByteArrayTag byteArray = (ByteArrayTag)tag; node.Tag = string.Join(Environment.NewLine, byteArray.Data); } else if (tag is IntArrayTag) { IntArrayTag intArray = (IntArrayTag)tag; node.Tag = string.Join(Environment.NewLine, intArray.Data); } else if (tag is LongArrayTag) { LongArrayTag longArray = (LongArrayTag)tag; node.Tag = string.Join(Environment.NewLine, longArray.Data); } else if (tag is ByteTag) { node.Tag = ((ByteTag)tag).Data; } else if (tag is ShortTag) { node.Tag = ((ShortTag)tag).Data; } else if (tag is IntTag) { node.Tag = ((IntTag)tag).Data; } else if (tag is LongTag) { node.Tag = ((LongTag)tag).Data; } else if (tag is FloatTag) { node.Tag = ((FloatTag)tag).Data; } else if (tag is DoubleTag) { node.Tag = ((DoubleTag)tag).Data; } else if (tag is StringTag) { node.Tag = ((StringTag)tag).Data; } }
/// <summary> /// Write the tag serialized data using the supplied writer. /// </summary> /// <remarks> /// This is a really KLUDGY routine. Even though it deals with primitive /// types it has knowledge of what those types mean. For example the UInt16ArrayTag /// doesn't have a length value written, but the UInt32ArrayTag does. This is /// a very brittle solution to this problem and needs to be fixed. The solution is /// to have a serialize method for each tag type (uggh) - so you see why I took /// this shortcut. /// </remarks> /// <param name="tag"></param> /// <param name="writer"></param> private static void SerializeTag(BBeBTag tag, BBeBinaryWriter writer) { ushort id = (ushort)(0xf500 + tag.Id); if (tag.GetType() == typeof(ByteTag)) { ByteTag t = (ByteTag)tag; writer.Write(id); writer.Write(t.Value); } else if (tag.GetType() == typeof(StreamTagGroup)) { StreamTagSerializer.Serialize(writer, (StreamTagGroup)tag); } else if (tag.GetType() == typeof(ByteArrayTag)) { ByteArrayTag t = (ByteArrayTag)tag; if (t.Id == TagId.StreamStart) { byte[] data = t.Value; writer.Write(BBeBLib.TagId.StreamSize); writer.Write((uint)data.Length); writer.Write(id); writer.Write(data); writer.Write(BBeBLib.TagId.StreamEnd); } else { writer.Write(id); writer.Write(t.Value); } } else if (tag.GetType() == typeof(UInt16Tag)) { UInt16Tag t = (UInt16Tag)tag; writer.Write(id); writer.Write(t.Value); } else if (tag.GetType() == typeof(UInt16ArrayTag)) { UInt16ArrayTag t = (UInt16ArrayTag)tag; writer.Write(id); foreach (ushort val in t.Value) { writer.Write(val); } } else if (tag.GetType() == typeof(UInt32Tag)) { UInt32Tag t = (UInt32Tag)tag; // StreamSize is written out by the StreamStart tag as the data may be compressed if (t.Id != TagId.StreamSize) { writer.Write(id); // Zero byte tags (but have hardcoded data associated with them if (t.Id != TagId.BaseButtonStart && t.Id != TagId.FocusinButtonStart && t.Id != TagId.PushButtonStart && t.Id != TagId.UpButtonStart) { writer.Write(t.Value); } } } else if (tag.GetType() == typeof(UInt32ArrayTag)) { UInt32ArrayTag t = (UInt32ArrayTag)tag; writer.Write(id); // JumpTo doesn't have a length set and is hardcoded to 2! if (t.Id != TagId.JumpTo) { writer.Write((ushort)t.Value.Length); } foreach (uint val in t.Value) { writer.Write(val); } } else if (tag.GetType() == typeof(StringTag)) { StringTag t = (StringTag)tag; writer.Write(id); writer.Write(t.Value); } else if (tag.GetType() == typeof(MessageTag)) { MessageTag t = (MessageTag)tag; writer.Write(id); writer.Write((ushort)t.parameters); byte[] data = System.Text.Encoding.Unicode.GetBytes(t.param1); writer.Write((ushort)data.Length); writer.Write(data); data = System.Text.Encoding.Unicode.GetBytes(t.param2); writer.Write((ushort)data.Length); writer.Write(data); } else if (tag.GetType() == typeof(EmpDotsCodeTag)) { EmpDotsCodeTag t = (EmpDotsCodeTag)tag; writer.Write(id); writer.Write((uint)t.Value); SerializeTag(t.FontFace, writer); writer.Write(t.DotsCode); } else if (tag.GetType() == typeof(IDOnlyTag)) { IDOnlyTag t = (IDOnlyTag)tag; if (t.Id != TagId.StreamEnd) { writer.Write(id); } } else if (tag.GetType() == typeof(BBeBTag)) { BBeBTag t = (BBeBTag)tag; if (t.Id != TagId.StreamEnd) { writer.Write(id); } } else { Debug.Assert(false, "Unknown tag type: " + tag.GetType().ToString()); } }