packet 0x2c: writing fixed, UUID format simplified

This commit is contained in:
Xabier de Zuazo 2013-07-12 13:19:34 +02:00
parent 634ad002f4
commit c0a66738a3
3 changed files with 29 additions and 13 deletions

View file

@ -168,7 +168,7 @@ Where elementList is an array with the following structure:
```js
[
{ uuid_msb: 123, uuid_lsb: 456, amount: 0.5, operation: 1 },
{ uuid: [ 123, 456, 78, 90 ], amount: 0.5, operation: 1 },
...
]
```

View file

@ -534,6 +534,7 @@ var types = {
'byteVector': [readByteVector, writeByteVector, 3],
'byteVectorArray': [readByteVectorArray, writeByteVectorArray, sizeOfByteVectorArray],
'stringArray': [readStringArray, writeStringArray, sizeOfStringArray],
'UUID': [readUUID, writeUUID, 16],
'propertyArray': [readPropertyArray, writePropertyArray, sizeOfPropertyArray]
};
@ -730,14 +731,22 @@ function writeStringArray(value, buffer, offset) {
function sizeOfPropertyArray(value) {
var size = 4;
for (var i = 0; i < value.length; ++i) {
size += sizeOfString(value[i].key) + types['double'][2];
size += sizeOfString(value[i].key) + types['double'][2] + types['short'][2];
for (var j = 0; j < value[i].elementList.length; j++) {
size += types['long'][2] + types['long'][2] + types['double'][2] + types['byte'][2];
size += types['UUID'][2] + types['double'][2] + types['byte'][2];
}
}
return size;
}
function writeUUID(value, buffer, offset) {
buffer.writeInt32BE(value[0], offset);
buffer.writeInt32BE(value[1], offset + 4);
buffer.writeInt32BE(value[2], offset + 8);
buffer.writeInt32BE(value[3], offset + 12);
return offset + 16;
}
function writePropertyArray(value, buffer, offset) {
buffer.writeInt32BE(value.length, offset);
offset += 4;
@ -746,8 +755,7 @@ function writePropertyArray(value, buffer, offset) {
offset = writeDouble(value[i].value, buffer, offset);
offset = writeShort(value[i].elementList.length, buffer, offset);
for (var j = 0; j < value[i].elementList.length; j++) {
offset = writeLong(value[i].elementList[j].uuid_msb, buffer, offset);
offset = writeLong(value[i].elementList[j].uuid_lsb, buffer, offset);
offset = writeUUID(value[i].elementList[j].uuid, buffer, offset);
offset = writeDouble(value[i].elementList[j].amount, buffer, offset);
offset = writeByte(value[i].elementList[j].operation, buffer, offset);
}
@ -1058,6 +1066,19 @@ function readStringArray (buffer, offset) {
};
}
function readUUID(buffer, offset) {
if (offset + 16 > buffer.length) return null;
return {
value: [
buffer.readInt32BE(offset),
buffer.readInt32BE(offset + 4),
buffer.readInt32BE(offset + 8),
buffer.readInt32BE(offset + 12),
],
size: 16,
};
}
function readPropertyArray (buffer, offset) {
var results = readInt(buffer, offset);
if (! results) return null;
@ -1088,14 +1109,9 @@ function readPropertyArray (buffer, offset) {
for (var j = 0; j < elementListLength ; j++) {
property.elementList[j] = {}
results = readLong(buffer, cursor);
results = readUUID(buffer, cursor);
if (! results) return null;
property.elementList[j].uuid_msb = results.value;
cursor += results.size;
results = readLong(buffer, cursor);
if (! results) return null;
property.elementList[j].uuid_lsb = results.value;
property.elementList[j].uuid = results.value;
cursor += results.size;
results = readDouble(buffer, cursor);

View file

@ -72,7 +72,7 @@ var values = {
nbtData: new Buffer(0),
}],
'stringArray': ['hello', 'dude'],
'propertyArray': [{ key: 'generic.maxHealth', value: 1.5 }],
'propertyArray': [{ key: 'generic.maxHealth', value: 1.5, elementList: [ { uuid: [ 123, 456, 78, 90 ], amount: 0.5, operation: 1 } ] }],
'mapChunkBulk': {
skyLightSent: true,
compressedChunkData: new Buffer(1234),