Skip to content

Commit

Permalink
fix long string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Foghrye4 committed Jul 28, 2018
1 parent 6f2f8b1 commit b88af83
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
13 changes: 13 additions & 0 deletions SubstrateCS/.vs/SubstrateNET4/xs/UserPrefs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Properties>
<MonoDevelop.Ide.Workbench ActiveDocument="Source/Nbt/NbtTree.cs">
<Files>
<File FileName="Source/Nbt/NbtTree.cs" Line="48" Column="10" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
<MultiItemStartupConfigurations />
</Properties>
36 changes: 27 additions & 9 deletions SubstrateCS/Source/Nbt/NbtTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class NbtTree : ICopyable<NbtTree>
private Stream _stream = null;
private TagNodeCompound _root = null;
private string _rootName = "";
List <TagNodeCompound> compoundTagCache = new List<TagNodeCompound> ();
private string lastTagName = "";

private static TagNodeNull _nulltag = new TagNodeNull();

Expand Down Expand Up @@ -83,9 +85,15 @@ public NbtTree (Stream s)
/// <param name="s">An open, readable data stream containing NBT data.</param>
public void ReadFrom (Stream s)
{
if (s != null) {
if (s != null) {

_stream = s;
_root = ReadRoot();
try {
_root = ReadRoot();
}
catch (Exception e) {
Console.Write(e.Message);
}
_stream = null;
}
}
Expand Down Expand Up @@ -249,6 +257,7 @@ private TagNode ReadByteArray ()

int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
Console.Write("Negative lenght while reading byte array with name: " + this.lastTagName);
throw new NBTException(NBTException.MSG_READ_NEG);
}

Expand All @@ -269,11 +278,7 @@ private TagNode ReadString ()
Array.Reverse(lenBytes);
}

short len = BitConverter.ToInt16(lenBytes, 0);
if (len < 0) {
throw new NBTException(NBTException.MSG_READ_NEG);
}

ushort len = BitConverter.ToUInt16(lenBytes, 0);
byte[] strBytes = new byte[len];
_stream.Read(strBytes, 0, len);

Expand Down Expand Up @@ -305,6 +310,7 @@ private TagNode ReadList ()

int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
Console.Write("Negative lenght while reading tag list with name: " + this.lastTagName);
throw new NBTException(NBTException.MSG_READ_NEG);
}

Expand All @@ -322,7 +328,9 @@ private TagNode ReadCompound ()
{
TagNodeCompound val = new TagNodeCompound();

while (ReadTag(val)) ;
while (ReadTag(val)) {

}

return val;
}
Expand All @@ -338,6 +346,7 @@ private TagNode ReadIntArray ()

int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
Console.Write("Negative lenght while reading int array with name: " + this.lastTagName);
throw new NBTException(NBTException.MSG_READ_NEG);
}

Expand Down Expand Up @@ -367,6 +376,7 @@ private TagNode ReadLongArray ()

int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
Console.Write("Negative lenght while reading long array with name: " + this.lastTagName);
throw new NBTException(NBTException.MSG_READ_NEG);
}

Expand Down Expand Up @@ -396,6 +406,7 @@ private TagNode ReadShortArray ()

int length = BitConverter.ToInt32(lenBytes, 0);
if (length < 0) {
Console.Write("Negative lenght while reading short array with name: " + this.lastTagName);
throw new NBTException(NBTException.MSG_READ_NEG);
}

Expand All @@ -419,7 +430,13 @@ private TagNodeCompound ReadRoot ()
TagType type = (TagType)_stream.ReadByte();
if (type == TagType.TAG_COMPOUND) {
_rootName = ReadString().ToTagString().Data; // name
return ReadValue(type) as TagNodeCompound;

TagNodeCompound root1 = ReadValue(type) as TagNodeCompound;
if(root1==null)
Console.WriteLine("Read value return null.");
else
Console.WriteLine("Read value return normal value.");
return root1;
}

return null;
Expand All @@ -430,6 +447,7 @@ private bool ReadTag (TagNodeCompound parent)
TagType type = (TagType)_stream.ReadByte();
if (type != TagType.TAG_END) {
string name = ReadString().ToTagString().Data;
lastTagName = name;
parent[name] = ReadValue(type);
return true;
}
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions SubstrateCS/SubstrateNET4.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubstrateNET4", "SubstrateNET4.csproj", "{7264A1C4-AB4A-4437-B252-7379B98B5509}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit b88af83

Please sign in to comment.