private void SavePayload(ref BinaryWriter writer, byte type, dynamic payload) { switch (type) { case 0: writer.Write((byte)0); break; case 1: writer.Write((byte)payload); break; case 2: writer.Write(EndiannessConverter.ToInt16(payload)); break; case 3: writer.Write(EndiannessConverter.ToInt32(payload)); break; case 4: writer.Write(EndiannessConverter.ToInt64(payload)); break; case 5: writer.Write(EndiannessConverter.ToSingle(payload)); break; case 6: writer.Write(EndiannessConverter.ToDouble(payload)); break; case 7: writer.Write(EndiannessConverter.ToInt32(payload.Length)); for (int i = 0; i < payload.Length; i++) { writer.Write(payload[i]); } break; case 8: writer.Write(EndiannessConverter.ToInt16((short)payload.Length)); byte[] oString = Encoding.UTF8.GetBytes(payload); for (int i = 0; i < oString.Length; i++) { writer.Write(oString[i]); } break; case 9: writer.Write(payload[0].Type); writer.Write(EndiannessConverter.ToInt32(payload.Count)); foreach (NBTTag tag in payload) { SavePayload(ref writer, tag.Type, tag.Payload); } break; case 10: foreach (KeyValuePair <string, NBTTag> tag in payload) { writer.Write(tag.Value.Type); writer.Write(EndiannessConverter.ToInt16((short)tag.Key.Length)); byte[] cString = Encoding.UTF8.GetBytes(tag.Key); for (int i = 0; i < cString.Length; i++) { writer.Write(cString[i]); } SavePayload(ref writer, tag.Value.Type, tag.Value.Payload); } writer.Write((byte)0); break; } }
private dynamic ReadPayload(ref BinaryReader reader, byte type) { switch (type) { case 0: return(0); case 1: return(reader.ReadByte()); case 2: return(EndiannessConverter.ToInt16(reader.ReadInt16())); case 3: return(EndiannessConverter.ToInt32(reader.ReadInt32())); case 4: return(EndiannessConverter.ToInt64(reader.ReadInt64())); case 5: return(EndiannessConverter.ToSingle(reader.ReadSingle())); case 6: return(EndiannessConverter.ToDouble(reader.ReadDouble())); case 7: return(reader.ReadBytes(EndiannessConverter.ToInt32(reader.ReadInt32()))); case 8: return(Encoding.UTF8.GetString(reader.ReadBytes(EndiannessConverter.ToInt16(reader.ReadInt16())))); case 9: { List <NBTTag> ret = new List <NBTTag>(); { byte containerType = reader.ReadByte(); int containerSize = EndiannessConverter.ToInt32(reader.ReadInt32()); for (int i = 0; i < containerSize; i++) { ret.Add(new NBTTag("", containerType, ReadPayload(ref reader, containerType))); } } return(ret); } case 10: { Dictionary <string, NBTTag> dic = new Dictionary <string, NBTTag>(); { byte containerType; while ((containerType = reader.ReadByte()) != 0) { string containerName = Encoding.UTF8.GetString(reader.ReadBytes(EndiannessConverter.ToInt16(reader.ReadInt16()))); dic.Add(containerName, new NBTTag(containerName, containerType, ReadPayload(ref reader, containerType))); } } return(dic); } default: throw new NotSupportedException("Tag type is invalid!"); } }