Skip to content

Commit b88af83

Browse files
committed
fix long string handling
1 parent 6f2f8b1 commit b88af83

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Properties>
2+
<MonoDevelop.Ide.Workbench ActiveDocument="Source/Nbt/NbtTree.cs">
3+
<Files>
4+
<File FileName="Source/Nbt/NbtTree.cs" Line="48" Column="10" />
5+
</Files>
6+
</MonoDevelop.Ide.Workbench>
7+
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
8+
<MonoDevelop.Ide.DebuggingService.Breakpoints>
9+
<BreakpointStore />
10+
</MonoDevelop.Ide.DebuggingService.Breakpoints>
11+
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
12+
<MultiItemStartupConfigurations />
13+
</Properties>

SubstrateCS/Source/Nbt/NbtTree.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class NbtTree : ICopyable<NbtTree>
2020
private Stream _stream = null;
2121
private TagNodeCompound _root = null;
2222
private string _rootName = "";
23+
List <TagNodeCompound> compoundTagCache = new List<TagNodeCompound> ();
24+
private string lastTagName = "";
2325

2426
private static TagNodeNull _nulltag = new TagNodeNull();
2527

@@ -83,9 +85,15 @@ public NbtTree (Stream s)
8385
/// <param name="s">An open, readable data stream containing NBT data.</param>
8486
public void ReadFrom (Stream s)
8587
{
86-
if (s != null) {
88+
if (s != null) {
89+
8790
_stream = s;
88-
_root = ReadRoot();
91+
try {
92+
_root = ReadRoot();
93+
}
94+
catch (Exception e) {
95+
Console.Write(e.Message);
96+
}
8997
_stream = null;
9098
}
9199
}
@@ -249,6 +257,7 @@ private TagNode ReadByteArray ()
249257

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

@@ -269,11 +278,7 @@ private TagNode ReadString ()
269278
Array.Reverse(lenBytes);
270279
}
271280

272-
short len = BitConverter.ToInt16(lenBytes, 0);
273-
if (len < 0) {
274-
throw new NBTException(NBTException.MSG_READ_NEG);
275-
}
276-
281+
ushort len = BitConverter.ToUInt16(lenBytes, 0);
277282
byte[] strBytes = new byte[len];
278283
_stream.Read(strBytes, 0, len);
279284

@@ -305,6 +310,7 @@ private TagNode ReadList ()
305310

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

@@ -322,7 +328,9 @@ private TagNode ReadCompound ()
322328
{
323329
TagNodeCompound val = new TagNodeCompound();
324330

325-
while (ReadTag(val)) ;
331+
while (ReadTag(val)) {
332+
333+
}
326334

327335
return val;
328336
}
@@ -338,6 +346,7 @@ private TagNode ReadIntArray ()
338346

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

@@ -367,6 +376,7 @@ private TagNode ReadLongArray ()
367376

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

@@ -396,6 +406,7 @@ private TagNode ReadShortArray ()
396406

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

@@ -419,7 +430,13 @@ private TagNodeCompound ReadRoot ()
419430
TagType type = (TagType)_stream.ReadByte();
420431
if (type == TagType.TAG_COMPOUND) {
421432
_rootName = ReadString().ToTagString().Data; // name
422-
return ReadValue(type) as TagNodeCompound;
433+
434+
TagNodeCompound root1 = ReadValue(type) as TagNodeCompound;
435+
if(root1==null)
436+
Console.WriteLine("Read value return null.");
437+
else
438+
Console.WriteLine("Read value return normal value.");
439+
return root1;
423440
}
424441

425442
return null;
@@ -430,6 +447,7 @@ private bool ReadTag (TagNodeCompound parent)
430447
TagType type = (TagType)_stream.ReadByte();
431448
if (type != TagType.TAG_END) {
432449
string name = ReadString().ToTagString().Data;
450+
lastTagName = name;
433451
parent[name] = ReadValue(type);
434452
return true;
435453
}
File renamed without changes.

SubstrateCS/SubstrateNET4.sln

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubstrateNET4", "SubstrateNET4.csproj", "{7264A1C4-AB4A-4437-B252-7379B98B5509}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{7264A1C4-AB4A-4437-B252-7379B98B5509}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
EndGlobal

0 commit comments

Comments
 (0)