Change to compressedNbt, and add zlib gunzip decompression

This commit is contained in:
deathcap 2016-02-14 23:16:35 -08:00
parent a929294177
commit b25ac14a13

View File

@ -1,11 +1,12 @@
const nbt = require('prismarine-nbt');
const UUID = require('uuid-1345');
const zlib = require('zlib');
module.exports = {
'UUID': [readUUID, writeUUID, 16],
'nbt': [readNbt, writeNbt, sizeOfNbt],
'optionalNbt':[readOptionalNbt,writeOptionalNbt,sizeOfOptionalNbt],
'optionalLengthPrefixedNbt':[readOptionalLengthPrefixedNbt,writeOptionalLengthPrefixedNbt,sizeOfOptionalLengthPrefixedNbt],
'compressedNbt':[readCompressedNbt,writeCompressedNbt,sizeOfCompressedNbt],
'restBuffer': [readRestBuffer, writeRestBuffer, sizeOfRestBuffer],
'entityMetadataLoop': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata]
};
@ -55,13 +56,19 @@ function sizeOfOptionalNbt(value) {
return nbt.proto.sizeOf(value,"nbt");
}
// Length-prefixed optional NBT, short instead of null byte: http://wiki.vg/index.php?title=Slot_Data&diff=6056&oldid=4753
function readOptionalLengthPrefixedNbt(buffer, offset) {
if(buffer.readInt16BE(offset) == -1) return {size:2};
return nbt.proto.read(buffer,offset+2,"nbt");
// Length-prefixed compressed NBT, see differences: http://wiki.vg/index.php?title=Slot_Data&diff=6056&oldid=4753
function readCompressedNbt(buffer, offset) {
const length = buffer.readInt16BE(offset);
if(length == -1) return {size:2};
const compressedNbt = buffer.slice(offset+2, offset+2+length);
const nbtBuffer = zlib.gunzipSync(compressedNbt); // TODO: async
return nbt.proto.read(nbtBuffer,0,"nbt");
}
function writeOptionalLengthPrefixedNbt(value, buffer, offset) {
function writeCompressedNbt(value, buffer, offset) {
if(value==undefined) {
buffer.writeInt16BE(-1,offset);
return offset+2;
@ -70,7 +77,7 @@ function writeOptionalLengthPrefixedNbt(value, buffer, offset) {
return nbt.proto.write(value,buffer,offset+2,"nbt");
}
function sizeOfOptionalLengthPrefixedNbt(value) {
function sizeOfCompressedNbt(value) {
if(value==undefined)
return 2;
return 2+nbt.proto.sizeOf(value,"nbt");