Skip to content

Commit

Permalink
refactoring and bas tagwriter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ForeverZer0 committed Aug 26, 2023
1 parent 3d99ee5 commit 36add31
Show file tree
Hide file tree
Showing 12 changed files with 457 additions and 199 deletions.
4 changes: 4 additions & 0 deletions SharpNBT.Test/SharpNBT.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<Folder Include="Data\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SharpNBT\SharpNBT.csproj" />
</ItemGroup>

</Project>
48 changes: 24 additions & 24 deletions SharpNBT/IO/BedrockFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ private int ReadInt32()
}

/// <inheritdoc />
public override ByteTag ReadByte(bool named)
protected override ByteTag ReadByte(bool named)
{
var name = named ? ReadString() : null;
return new ByteTag(name, BaseStream.ReadByte());
}

/// <inheritdoc />
public override ShortTag ReadShort(bool named)
protected override ShortTag ReadShort(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(short)];
Expand All @@ -66,14 +66,14 @@ public override ShortTag ReadShort(bool named)
}

/// <inheritdoc />
public override IntTag ReadInt(bool named)
protected override IntTag ReadInt(bool named)
{
var name = named ? ReadString() : null;
return new IntTag(name, ReadInt32());
}

/// <inheritdoc />
public override LongTag ReadLong(bool named)
protected override LongTag ReadLong(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(long)];
Expand All @@ -82,7 +82,7 @@ public override LongTag ReadLong(bool named)
}

/// <inheritdoc />
public override FloatTag ReadFloat(bool named)
protected override FloatTag ReadFloat(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(float)];
Expand All @@ -91,7 +91,7 @@ public override FloatTag ReadFloat(bool named)
}

/// <inheritdoc />
public override DoubleTag ReadDouble(bool named)
protected override DoubleTag ReadDouble(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(double)];
Expand All @@ -100,14 +100,14 @@ public override DoubleTag ReadDouble(bool named)
}

/// <inheritdoc />
public override StringTag ReadString(bool named)
protected override StringTag ReadString(bool named)
{
var name = named ? ReadString() : null;
return new StringTag(name, ReadString());
}

/// <inheritdoc />
public override ByteArrayTag ReadByteArray(bool named)
protected override ByteArrayTag ReadByteArray(bool named)
{
var name = named ? ReadString() : null;
var length = ReadInt32();
Expand All @@ -120,7 +120,7 @@ public override ByteArrayTag ReadByteArray(bool named)
}

/// <inheritdoc />
public override IntArrayTag ReadIntArray(bool named)
protected override IntArrayTag ReadIntArray(bool named)
{
var name = named ? ReadString() : null;
var length = ReadInt32();
Expand All @@ -129,18 +129,18 @@ public override IntArrayTag ReadIntArray(bool named)

var values = new int[length];
BaseStream.ReadExactly(MemoryMarshal.Cast<int, byte>(values));

if (BitConverter.IsLittleEndian)
return new IntArrayTag(name, values);

if (!BitConverter.IsLittleEndian)
{
for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();
}
for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();

return new IntArrayTag(name, values);
}

/// <inheritdoc />
public override LongArrayTag ReadLongArray(bool named)
protected override LongArrayTag ReadLongArray(bool named)
{
var name = named ? ReadString() : null;
var length = ReadInt32();
Expand All @@ -149,39 +149,39 @@ public override LongArrayTag ReadLongArray(bool named)

var values = new long[length];
BaseStream.ReadExactly(MemoryMarshal.Cast<long, byte>(values));

if (BitConverter.IsLittleEndian)
return new LongArrayTag(name, values);

if (!BitConverter.IsLittleEndian)
{
for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();
}
for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();

return new LongArrayTag(name, values);
}

/// <inheritdoc />
public override ListTag ReadList(bool named)
protected override ListTag ReadList(bool named)
{
var name = named ? ReadString() : null;
var childType = (TagType)BaseStream.ReadByte();
var length = ReadInt32();

var list = new ListTag(name);
for (var i = 0; i < length; i++)
list.Add(ReadTag(false, childType));
list.Add(Read(false, childType));

return list;
}

/// <inheritdoc />
public override CompoundTag ReadCompound(bool named)
protected override CompoundTag ReadCompound(bool named)
{
var name = named ? ReadString() : null;
var compound = new CompoundTag(name);

while (true)
{
var child = ReadTag(true);
var child = Read(true);
if (child is EndTag)
break;
compound.Add(child);
Expand Down
50 changes: 24 additions & 26 deletions SharpNBT/IO/BedrockNetworkReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ private string ReadString()
}

/// <inheritdoc />
public override ByteTag ReadByte(bool named)
protected override ByteTag ReadByte(bool named)
{
var name = named ? ReadString() : null;
return new ByteTag(name, BaseStream.ReadByte());
}

/// <inheritdoc />
public override ShortTag ReadShort(bool named)
protected override ShortTag ReadShort(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(short)];
Expand All @@ -95,21 +95,21 @@ public override ShortTag ReadShort(bool named)
}

/// <inheritdoc />
public override IntTag ReadInt(bool named)
protected override IntTag ReadInt(bool named)
{
var name = named ? ReadString() : null;
return new IntTag(name, (int) ReadVarInt(32, true));
}

/// <inheritdoc />
public override LongTag ReadLong(bool named)
protected override LongTag ReadLong(bool named)
{
var name = named ? ReadString() : null;
return new LongTag(name, ReadVarInt(64, true));
}

/// <inheritdoc />
public override FloatTag ReadFloat(bool named)
protected override FloatTag ReadFloat(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(float)];
Expand All @@ -118,7 +118,7 @@ public override FloatTag ReadFloat(bool named)
}

/// <inheritdoc />
public override DoubleTag ReadDouble(bool named)
protected override DoubleTag ReadDouble(bool named)
{
var name = named ? ReadString() : null;
Span<byte> buffer = stackalloc byte[sizeof(double)];
Expand All @@ -127,14 +127,14 @@ public override DoubleTag ReadDouble(bool named)
}

/// <inheritdoc />
public override StringTag ReadString(bool named)
protected override StringTag ReadString(bool named)
{
var name = named ? ReadString() : null;
return new StringTag(name, ReadString());
}

/// <inheritdoc />
public override ByteArrayTag ReadByteArray(bool named)
protected override ByteArrayTag ReadByteArray(bool named)
{
byte[] values;
var name = named ? ReadString() : null;
Expand All @@ -152,7 +152,7 @@ public override ByteArrayTag ReadByteArray(bool named)
}

/// <inheritdoc />
public override IntArrayTag ReadIntArray(bool named)
protected override IntArrayTag ReadIntArray(bool named)
{
var name = named ? ReadString() : null;
var length = (int)ReadVarInt(32, true);
Expand All @@ -161,18 +161,17 @@ public override IntArrayTag ReadIntArray(bool named)

var values = new int[length];
BaseStream.ReadExactly(MemoryMarshal.Cast<int, byte>(values));

if (!BitConverter.IsLittleEndian)
{
for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();
}

if (BitConverter.IsLittleEndian)
return new IntArrayTag(name, values);

for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();
return new IntArrayTag(name, values);
}

/// <inheritdoc />
public override LongArrayTag ReadLongArray(bool named)
protected override LongArrayTag ReadLongArray(bool named)
{
var name = named ? ReadString() : null;
var length = (int)ReadVarInt(32, true);
Expand All @@ -181,39 +180,38 @@ public override LongArrayTag ReadLongArray(bool named)

var values = new long[length];
BaseStream.ReadExactly(MemoryMarshal.Cast<long, byte>(values));

if (!BitConverter.IsLittleEndian)
{
for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();
}

if (BitConverter.IsLittleEndian)
return new LongArrayTag(name, values);

for (var i = 0; i < length; i++)
values[i] = values[i].SwapEndian();
return new LongArrayTag(name, values);
}

/// <inheritdoc />
public override ListTag ReadList(bool named)
protected override ListTag ReadList(bool named)
{
var name = named ? ReadString() : null;
var childType = (TagType)BaseStream.ReadByte();
var length = (int)ReadVarInt(32, true);

var list = new ListTag(name);
for (var i = 0; i < length; i++)
list.Add(ReadTag(false, childType));
list.Add(Read(false, childType));

return list;
}

/// <inheritdoc />
public override CompoundTag ReadCompound(bool named)
protected override CompoundTag ReadCompound(bool named)
{
var name = named ? ReadString() : null;
var compound = new CompoundTag(name);

while (true)
{
var child = ReadTag(true);
var child = Read(true);
if (child is EndTag)
break;
compound.Add(child);
Expand Down
Loading

0 comments on commit 36add31

Please sign in to comment.