mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-08-19 14:14:22 +00:00
remove binary-parsser lib dependency
This commit is contained in:
+15
-19
@@ -1,20 +1,16 @@
|
||||
const { Parser } = require('binary-parser');
|
||||
module.exports = (buffer, dataDecoder) => {
|
||||
let offset = 0;
|
||||
const chunk = {};
|
||||
chunk.id = buffer.readUInt16LE(offset);
|
||||
offset += 2;
|
||||
|
||||
module.exports = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return Number.parseInt(binary.substring(1), 2);
|
||||
},
|
||||
})
|
||||
.seek(-2)
|
||||
.uint16le('has_subchunks', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.buffer('chunk_data', {
|
||||
length: 'data_len',
|
||||
});
|
||||
// NOTE(jwetzell): this data is split up as 1 bit for has_subchunks and 15 bits for the data_len
|
||||
const combinedLengthAndFlag = buffer.readUInt16LE(offset);
|
||||
chunk.has_subchunks = combinedLengthAndFlag > 32768;
|
||||
chunk.data_len = chunk.has_subchunks ? combinedLengthAndFlag - 32768 : combinedLengthAndFlag;
|
||||
offset += 2;
|
||||
chunk.chunk_data = buffer.subarray(offset, offset + chunk.data_len);
|
||||
|
||||
dataDecoder(chunk);
|
||||
return chunk;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const packetHeaderChunk = require('../packet-header-chunk');
|
||||
const dataTrackerListChunk = require('./data-tracker-list-chunk');
|
||||
|
||||
function decodeDataPacketChunk(dataPacketChunk) {
|
||||
if (dataPacketChunk.has_subchunks && dataPacketChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < dataPacketChunk.data_len) {
|
||||
const chunkId = dataPacketChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
dataPacketChunk.packet_header = packetHeaderChunk(dataPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += dataPacketChunk.packet_header.data_len;
|
||||
break;
|
||||
case 0x0001:
|
||||
dataPacketChunk.tracker_list = dataTrackerListChunk(dataPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += dataPacketChunk.tracker_list.data_len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeDataPacketChunk);
|
||||
@@ -0,0 +1,41 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const dataTrackerFieldChunk = require('./data-tracker-field-chunk');
|
||||
|
||||
function decodeTrackerChunk(trackerChunk) {
|
||||
if (trackerChunk.chunk_data && trackerChunk.data_len > 0) {
|
||||
let offset = 0;
|
||||
while (offset < trackerChunk.data_len) {
|
||||
const trackerFieldChunk = dataTrackerFieldChunk(trackerChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += trackerFieldChunk.data_len;
|
||||
switch (trackerFieldChunk.id) {
|
||||
case 0x0000:
|
||||
trackerChunk.pos = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0001:
|
||||
trackerChunk.speed = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0002:
|
||||
trackerChunk.ori = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0003:
|
||||
trackerChunk.status = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0004:
|
||||
trackerChunk.accel = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0005:
|
||||
trackerChunk.trgtpos = trackerFieldChunk;
|
||||
break;
|
||||
case 0x0006:
|
||||
trackerChunk.timestamp = trackerFieldChunk;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeTrackerChunk);
|
||||
@@ -1,29 +1,43 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
/* eslint-disable no-case-declarations */
|
||||
const chunk = require('../chunk');
|
||||
|
||||
module.exports = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return Number.parseInt(binary.substring(1), 2);
|
||||
},
|
||||
})
|
||||
.seek(-2)
|
||||
.uint16le('has_subchunks', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.choice('data', {
|
||||
tag: 'id',
|
||||
choices: {
|
||||
0x0000: new Parser().floatle('x').floatle('y').floatle('z'),
|
||||
0x0001: new Parser().floatle('x').floatle('y').floatle('z'),
|
||||
0x0002: new Parser().floatle('x').floatle('y').floatle('z'),
|
||||
0x0003: new Parser().floatle('validity'),
|
||||
0x0004: new Parser().floatle('x').floatle('y').floatle('z'),
|
||||
0x0005: new Parser().floatle('x').floatle('y').floatle('z'),
|
||||
0x0006: new Parser().uint64le('tracker_timestamp'),
|
||||
},
|
||||
});
|
||||
function readXYZ(trackerFieldChunk, prefix) {
|
||||
if (prefix === undefined) {
|
||||
prefix = '';
|
||||
}
|
||||
|
||||
trackerFieldChunk[`${prefix}x`] = trackerFieldChunk.chunk_data.readFloatLE(0);
|
||||
trackerFieldChunk[`${prefix}y`] = trackerFieldChunk.chunk_data.readFloatLE(4);
|
||||
trackerFieldChunk[`${prefix}z`] = trackerFieldChunk.chunk_data.readFloatLE(8);
|
||||
}
|
||||
function decodeTrackerFieldChunk(trackerFieldChunk) {
|
||||
if (trackerFieldChunk.id !== undefined) {
|
||||
switch (trackerFieldChunk.id) {
|
||||
case 0x0000:
|
||||
readXYZ(trackerFieldChunk, 'pos_');
|
||||
break;
|
||||
case 0x0001:
|
||||
readXYZ(trackerFieldChunk, 'speed_');
|
||||
break;
|
||||
case 0x0002:
|
||||
readXYZ(trackerFieldChunk, 'ori_');
|
||||
break;
|
||||
case 0x0003:
|
||||
trackerFieldChunk.validity = trackerFieldChunk.chunk_data.readFloatLE();
|
||||
break;
|
||||
case 0x0004:
|
||||
readXYZ(trackerFieldChunk, 'accel_');
|
||||
break;
|
||||
case 0x0005:
|
||||
readXYZ(trackerFieldChunk, 'trgtpos_');
|
||||
break;
|
||||
case 0x0006:
|
||||
trackerFieldChunk.tracker_timestamp = trackerFieldChunk.chunk_data.readBigUInt64LE();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeTrackerFieldChunk);
|
||||
|
||||
@@ -1,24 +1,17 @@
|
||||
module.exports = (buffer) => {
|
||||
const trackerList = {
|
||||
trackers: [],
|
||||
};
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const dataTrackerChunk = require('./data-tracker-chunk');
|
||||
|
||||
function decodeDataTrackerListChunk(dataTrackerListChunk) {
|
||||
dataTrackerListChunk.trackers = {};
|
||||
|
||||
// TODO(jwetzell): add error handling
|
||||
let offset = 0;
|
||||
while (offset < buffer.length) {
|
||||
const trackerChunk = {};
|
||||
trackerChunk.id = buffer.readUInt16LE(offset);
|
||||
offset += 2;
|
||||
|
||||
// NOTE(jwetzell): this data is split up as 1 bit for has_subchunks and 15 bits for the data_len
|
||||
const tempBytesAsBinary = buffer.readUInt16LE(offset).toString(2);
|
||||
trackerChunk.has_subchunks = tempBytesAsBinary.charAt(0) === '0';
|
||||
trackerChunk.data_len = parseInt(tempBytesAsBinary.substring(1), 2);
|
||||
offset += 2;
|
||||
trackerChunk.data = buffer.subarray(offset, offset + trackerChunk.data_len);
|
||||
while (offset < dataTrackerListChunk.chunk_data.length) {
|
||||
const trackerChunk = dataTrackerChunk(dataTrackerListChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += trackerChunk.data_len;
|
||||
trackerList.trackers.push(trackerChunk);
|
||||
dataTrackerListChunk.trackers[trackerChunk.id] = trackerChunk;
|
||||
}
|
||||
|
||||
return trackerList;
|
||||
};
|
||||
}
|
||||
module.exports = (buffer) => chunk(buffer, decodeDataTrackerListChunk);
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
const TrackerListChunk = require('./data-tracker-list-chunk');
|
||||
const TrackerFieldChunk = require('./data-tracker-field-chunk');
|
||||
|
||||
module.exports = {
|
||||
TrackerFieldChunk,
|
||||
TrackerListChunk,
|
||||
};
|
||||
@@ -1,17 +0,0 @@
|
||||
const Chunk = require('./chunk');
|
||||
const PacketHeaderChunk = require('./packet-header-chunk');
|
||||
const Packet = require('./packet');
|
||||
const Info = require('./info');
|
||||
const Data = require('./data');
|
||||
|
||||
module.exports = {
|
||||
Chunk,
|
||||
PacketHeaderChunk,
|
||||
Packet,
|
||||
Info: {
|
||||
...Info,
|
||||
},
|
||||
Data: {
|
||||
...Data,
|
||||
},
|
||||
};
|
||||
@@ -1,7 +0,0 @@
|
||||
const SystemNameChunk = require('./info-system-name-chunk');
|
||||
const TrackerListChunk = require('./info-tracker-list-chunk');
|
||||
|
||||
module.exports = {
|
||||
SystemNameChunk,
|
||||
TrackerListChunk,
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const packetHeaderChunk = require('../packet-header-chunk');
|
||||
const infoSystemNameChunk = require('./info-system-name-chunk');
|
||||
const infoTrackerListChunk = require('./info-tracker-list-chunk');
|
||||
|
||||
function decodeInfoPacketChunk(infoPacketChunk) {
|
||||
if (infoPacketChunk.has_subchunks && infoPacketChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < infoPacketChunk.data_len) {
|
||||
const chunkId = infoPacketChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
infoPacketChunk.packet_header = packetHeaderChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoPacketChunk.packet_header.data_len;
|
||||
break;
|
||||
case 0x0001:
|
||||
infoPacketChunk.system_name = infoSystemNameChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoPacketChunk.system_name.data_len;
|
||||
break;
|
||||
case 0x0002:
|
||||
infoPacketChunk.tracker_list = infoTrackerListChunk(infoPacketChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoPacketChunk.tracker_list.data_len;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeInfoPacketChunk);
|
||||
@@ -1,3 +1,9 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
const chunk = require('../chunk');
|
||||
|
||||
module.exports = new Parser().string('system_name', { greedy: true });
|
||||
function decodeSystemNameChunk(systemNameChunk) {
|
||||
if (systemNameChunk.chunk_data !== undefined && systemNameChunk.data_len !== undefined) {
|
||||
systemNameChunk.system_name = systemNameChunk.chunk_data.subarray(0, systemNameChunk.data_len).toString();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeSystemNameChunk);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const infoTrackerNameChunk = require('./info-tracker-name-chunk');
|
||||
|
||||
function decodeInfoTrackerChunk(infoTrackerChunk) {
|
||||
if (infoTrackerChunk.has_subchunks && infoTrackerChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < infoTrackerChunk.data_len) {
|
||||
const chunkId = infoTrackerChunk.chunk_data.readUInt16LE(offset);
|
||||
switch (chunkId) {
|
||||
case 0x0000:
|
||||
infoTrackerChunk.tracker_name = infoTrackerNameChunk(infoTrackerChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += infoTrackerChunk.tracker_name.data_len;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodeInfoTrackerChunk);
|
||||
@@ -1,39 +1,17 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
const { CHUNK_HEADER_SIZE } = require('../../constants');
|
||||
const chunk = require('../chunk');
|
||||
const infoTrackerChunk = require('./info-tracker-chunk');
|
||||
|
||||
const infoTrackerNameParser = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return Number.parseInt(binary.substring(1), 2);
|
||||
},
|
||||
})
|
||||
.seek(-2)
|
||||
.uint16le('has_subchunks', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.string('tracker_name', { length: 'data_len' });
|
||||
|
||||
const infoTrackerParser = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return Number.parseInt(binary.substring(1), 2);
|
||||
},
|
||||
})
|
||||
.seek(-2)
|
||||
.uint16le('has_subchunks', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.nest('tracker_name', {
|
||||
type: infoTrackerNameParser,
|
||||
});
|
||||
|
||||
module.exports = new Parser().array('trackers', { type: infoTrackerParser, readUntil: 'eof' });
|
||||
function decodeInfoTrackerListChunk(infoTrackerListChunk) {
|
||||
infoTrackerListChunk.trackers = {};
|
||||
if (infoTrackerListChunk.has_subchunks && infoTrackerListChunk.chunk_data) {
|
||||
let offset = 0;
|
||||
while (offset < infoTrackerListChunk.data_len) {
|
||||
const trackerChunk = infoTrackerChunk(infoTrackerListChunk.chunk_data.subarray(offset));
|
||||
offset += CHUNK_HEADER_SIZE;
|
||||
offset += trackerChunk.data_len;
|
||||
infoTrackerListChunk.trackers[trackerChunk.id] = trackerChunk;
|
||||
}
|
||||
}
|
||||
}
|
||||
module.exports = (buffer) => chunk(buffer, decodeInfoTrackerListChunk);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
const chunk = require('../chunk');
|
||||
|
||||
function decodeTrackerNameChunk(trackerNameChunk) {
|
||||
if (trackerNameChunk.chunk_data !== undefined && trackerNameChunk.data_len !== undefined) {
|
||||
trackerNameChunk.tracker_name = trackerNameChunk.chunk_data.subarray(0, trackerNameChunk.data_len).toString();
|
||||
}
|
||||
}
|
||||
module.exports = (buffer) => chunk(buffer, decodeTrackerNameChunk);
|
||||
@@ -1,9 +1,12 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
const chunk = require('./chunk');
|
||||
|
||||
module.exports = new Parser()
|
||||
.uint64le('packet_timestamp')
|
||||
.uint8('version_high')
|
||||
.uint8('version_low')
|
||||
.uint8('frame_id')
|
||||
.uint8('frame_packet_count')
|
||||
.seek(4);
|
||||
function decodePacketHeaderChunk(packetHeaderChunk) {
|
||||
// TODO(jwetzell): remove the string conversion
|
||||
packetHeaderChunk.packet_timestamp = packetHeaderChunk.chunk_data.readBigUInt64LE(0);
|
||||
packetHeaderChunk.version_high = packetHeaderChunk.chunk_data.readUInt8(8);
|
||||
packetHeaderChunk.version_low = packetHeaderChunk.chunk_data.readUInt8(9);
|
||||
packetHeaderChunk.frame_id = packetHeaderChunk.chunk_data.readUInt8(10);
|
||||
packetHeaderChunk.frame_packet_count = packetHeaderChunk.chunk_data.readUInt8(11);
|
||||
}
|
||||
|
||||
module.exports = (buffer) => chunk(buffer, decodePacketHeaderChunk);
|
||||
|
||||
+13
-17
@@ -1,18 +1,14 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
const dataPacketChunk = require('./data/data-packet-chunk');
|
||||
const infoPacketChunk = require('./info/info-packet-chunk');
|
||||
|
||||
module.exports = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return Number.parseInt(binary.substring(1), 2);
|
||||
},
|
||||
})
|
||||
.seek(-2)
|
||||
.uint16le('has_subchunks', {
|
||||
formatter: (item) => {
|
||||
const binary = item.toString(2).padStart(16, '0');
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.buffer('chunk_data', { readUntil: 'eof' });
|
||||
module.exports = (buffer) => {
|
||||
const chunkId = buffer.readUInt16LE();
|
||||
switch (chunkId) {
|
||||
case 0x6756:
|
||||
return infoPacketChunk(buffer);
|
||||
case 0x6755:
|
||||
return dataPacketChunk(buffer);
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user