Fixed NBTSizeTracker missing a lot of data being read. Also made new NBT object allocation claim 32-bits in the size tracker.

This commit is contained in:
Lex Manos 2015-04-17 02:07:01 -07:00
parent 5ac654dc1c
commit de066a86da
7 changed files with 131 additions and 2 deletions

View file

@ -17,7 +17,25 @@
public static void func_74793_a(NBTTagCompound p_74793_0_, File p_74793_1_) throws IOException public static void func_74793_a(NBTTagCompound p_74793_0_, File p_74793_1_) throws IOException
{ {
File file2 = new File(p_74793_1_.getAbsolutePath() + "_tmp"); File file2 = new File(p_74793_1_.getAbsolutePath() + "_tmp");
@@ -146,7 +143,6 @@ @@ -120,6 +117,7 @@
private static NBTBase func_152455_a(DataInput p_152455_0_, int p_152455_1_, NBTSizeTracker p_152455_2_) throws IOException
{
byte b0 = p_152455_0_.readByte();
+ p_152455_2_.func_152450_a(8); // Forge: Count everything!
if (b0 == 0)
{
@@ -127,7 +125,8 @@
}
else
{
- p_152455_0_.readUTF();
+ NBTSizeTracker.readUTF(p_152455_2_, p_152455_0_.readUTF()); //Forge: Count this string.
+ p_152455_2_.func_152450_a(32); //Forge: 4 extra bytes for the object allocation.
NBTBase nbtbase = NBTBase.func_150284_a(b0);
try
@@ -146,7 +145,6 @@
} }
} }
@ -25,7 +43,7 @@
public static void func_74795_b(NBTTagCompound p_74795_0_, File p_74795_1_) throws IOException public static void func_74795_b(NBTTagCompound p_74795_0_, File p_74795_1_) throws IOException
{ {
DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(p_74795_1_)); DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(p_74795_1_));
@@ -161,7 +157,6 @@ @@ -161,7 +159,6 @@
} }
} }

View file

@ -0,0 +1,37 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTSizeTracker.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTSizeTracker.java
@@ -25,4 +25,34 @@
throw new RuntimeException("Tried to read NBT tag that was too big; tried to allocate: " + this.field_152453_c + "bytes where max allowed: " + this.field_152452_b);
}
}
+
+ /*
+ * UTF8 is not a simple encoding system, each character can be either
+ * 1, 2, or 3 bytes. Depending on where it's numerical value falls.
+ * We have to count up each character individually to see the true
+ * length of the data.
+ *
+ * Basic concept is that it uses the MSB of each byte as a 'read more' signal.
+ * So it has to shift each 7-bit segment.
+ *
+ * This will accurately count the correct byte length to encode this string, plus the 2 bytes for it's length prefix.
+ */
+ public static void readUTF(NBTSizeTracker tracker, String data)
+ {
+ tracker.func_152450_a(16); //Header length
+ if (data == null)
+ return;
+
+ int len = data.length();
+ int utflen = 0;
+
+ for (int i = 0; i < len; i++)
+ {
+ int c = data.charAt(i);
+ if ((c >= 0x0001) && (c <= 0x007F)) utflen += 1;
+ else if (c > 0x07FF) utflen += 3;
+ else utflen += 2;
+ }
+ tracker.func_152450_a(8 * utflen);
+ }
}

View file

@ -0,0 +1,10 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagByteArray.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagByteArray.java
@@ -25,6 +25,7 @@
void func_152446_a(DataInput p_152446_1_, int p_152446_2_, NBTSizeTracker p_152446_3_) throws IOException
{
+ p_152446_3_.func_152450_a(32); //Forge: Count the length as well
int j = p_152446_1_.readInt();
p_152446_3_.func_152450_a((long)(8 * j));
this.field_74754_a = new byte[j];

View file

@ -0,0 +1,27 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagCompound.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagCompound.java
@@ -48,7 +48,7 @@
while ((b0 = func_152447_a(p_152446_1_, p_152446_3_)) != 0)
{
String s = func_152448_b(p_152446_1_, p_152446_3_);
- p_152446_3_.func_152450_a((long)(16 * s.length()));
+ NBTSizeTracker.readUTF(p_152446_3_, s); // Forge: Correctly read String length including header.
NBTBase nbtbase = func_152449_a(b0, s, p_152446_1_, p_152446_2_ + 1, p_152446_3_);
this.field_74784_a.put(s, nbtbase);
}
@@ -396,6 +396,7 @@
private static byte func_152447_a(DataInput p_152447_0_, NBTSizeTracker p_152447_1_) throws IOException
{
+ p_152447_1_.func_152450_a(8);
return p_152447_0_.readByte();
}
@@ -406,6 +407,7 @@
static NBTBase func_152449_a(byte p_152449_0_, String p_152449_1_, DataInput p_152449_2_, int p_152449_3_, NBTSizeTracker p_152449_4_)
{
+ p_152449_4_.func_152450_a(32); //Forge: 4 extra bytes for the object allocation.
NBTBase nbtbase = NBTBase.func_150284_a(p_152449_0_);
try

View file

@ -0,0 +1,10 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagIntArray.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagIntArray.java
@@ -29,6 +29,7 @@
void func_152446_a(DataInput p_152446_1_, int p_152446_2_, NBTSizeTracker p_152446_3_) throws IOException
{
+ p_152446_3_.func_152450_a(32); //Forge: Count the length as well
int j = p_152446_1_.readInt();
p_152446_3_.func_152450_a((long)(32 * j));
this.field_74749_a = new int[j];

View file

@ -0,0 +1,16 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagList.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagList.java
@@ -46,11 +46,13 @@
{
p_152446_3_.func_152450_a(8L);
this.field_74746_b = p_152446_1_.readByte();
+ p_152446_3_.func_152450_a(32); //Forge: Count the length as well
int j = p_152446_1_.readInt();
this.field_74747_a = Lists.newArrayList();
for (int k = 0; k < j; ++k)
{
+ p_152446_3_.func_152450_a(32); //Forge: 4 extra bytes for the object allocation.
NBTBase nbtbase = NBTBase.func_150284_a(this.field_74746_b);
nbtbase.func_152446_a(p_152446_1_, p_152446_2_ + 1, p_152446_3_);
this.field_74747_a.add(nbtbase);

View file

@ -0,0 +1,11 @@
--- ../src-base/minecraft/net/minecraft/nbt/NBTTagString.java
+++ ../src-work/minecraft/net/minecraft/nbt/NBTTagString.java
@@ -32,7 +32,7 @@
void func_152446_a(DataInput p_152446_1_, int p_152446_2_, NBTSizeTracker p_152446_3_) throws IOException
{
this.field_74751_a = p_152446_1_.readUTF();
- p_152446_3_.func_152450_a((long)(16 * this.field_74751_a.length()));
+ NBTSizeTracker.readUTF(p_152446_3_, field_74751_a); // Forge: Correctly read String length including header.
}
public byte func_74732_a()