mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-09-10 16:49:46 +00:00
completely rework the parsing structure because I now realize how frames work
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const Parser = require('binary-parser').Parser;
|
||||
|
||||
const PSN_PACKET_HEADER_PARSER = new Parser()
|
||||
const ChunkParser = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
@@ -15,6 +15,11 @@ const PSN_PACKET_HEADER_PARSER = new Parser()
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.buffer('chunk_data', {
|
||||
length: 'data_len',
|
||||
});
|
||||
|
||||
const HeaderParser = new Parser()
|
||||
.uint64le('packet_timestamp')
|
||||
.uint8('version_high')
|
||||
.uint8('version_low')
|
||||
@@ -23,5 +28,6 @@ const PSN_PACKET_HEADER_PARSER = new Parser()
|
||||
.seek(4);
|
||||
|
||||
module.exports = {
|
||||
PSN_PACKET_HEADER_PARSER,
|
||||
ChunkParser,
|
||||
HeaderParser,
|
||||
};
|
||||
|
||||
+4
-26
@@ -1,7 +1,6 @@
|
||||
const { PSN_PACKET_HEADER_PARSER } = require('../common');
|
||||
const Parser = require('binary-parser').Parser;
|
||||
|
||||
const PSN_DATA_TRACKER_FIELD_PARSER = new Parser()
|
||||
const DataTrackerFieldParser = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
formatter: (item) => {
|
||||
@@ -48,30 +47,9 @@ const PSN_DATA_TRACKER_PARSER = new Parser()
|
||||
length: 'data_len',
|
||||
});
|
||||
|
||||
const PSN_DATA_TRACKER_LIST_PARSER = 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';
|
||||
},
|
||||
})
|
||||
.array('trackers', { type: PSN_DATA_TRACKER_PARSER, lengthInBytes: 'data_len' });
|
||||
|
||||
const PSN_DATA_CHUNK_DATA_PARSER = new Parser()
|
||||
.nest('packet_header', { type: PSN_PACKET_HEADER_PARSER })
|
||||
.nest('tracker_list', { type: PSN_DATA_TRACKER_LIST_PARSER });
|
||||
const DataTrackerListParser = new Parser().array('trackers', { type: PSN_DATA_TRACKER_PARSER, readUntil: 'eof' });
|
||||
|
||||
module.exports = {
|
||||
PSN_DATA_CHUNK_DATA_PARSER,
|
||||
PSN_DATA_TRACKER_FIELD_PARSER,
|
||||
PSN_DATA_TRACKER_LIST_PARSER,
|
||||
PSN_DATA_TRACKER_PARSER,
|
||||
DataTrackerFieldParser,
|
||||
DataTrackerListParser,
|
||||
};
|
||||
|
||||
+1
-10
@@ -1,6 +1,3 @@
|
||||
const { PSN_DATA_CHUNK_DATA_PARSER } = require('./data');
|
||||
const { PSN_INFO_CHUNK_DATA_PARSER } = require('./info');
|
||||
|
||||
const Parser = require('binary-parser').Parser;
|
||||
|
||||
const PSN_PACKET_PARSER = new Parser()
|
||||
@@ -18,12 +15,6 @@ const PSN_PACKET_PARSER = new Parser()
|
||||
return binary.charAt(0) === '1';
|
||||
},
|
||||
})
|
||||
.choice('chunk_data', {
|
||||
tag: 'id',
|
||||
choices: {
|
||||
0x6755: PSN_DATA_CHUNK_DATA_PARSER,
|
||||
0x6756: PSN_INFO_CHUNK_DATA_PARSER,
|
||||
},
|
||||
});
|
||||
.buffer('chunk_data', { readUntil: 'eof' });
|
||||
|
||||
module.exports = (buffer) => PSN_PACKET_PARSER.parse(buffer);
|
||||
|
||||
+4
-44
@@ -1,24 +1,5 @@
|
||||
const { PSN_PACKET_HEADER_PARSER } = require('../common');
|
||||
|
||||
const Parser = require('binary-parser').Parser;
|
||||
|
||||
const PSN_INFO_SYSTEM_NAME_PARSER = 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('system_name', { length: 'data_len' });
|
||||
|
||||
const PSN_INFO_TRACKER_NAME_PARSER = new Parser()
|
||||
.uint16le('id')
|
||||
.uint16le('data_len', {
|
||||
@@ -55,32 +36,11 @@ const PSN_INFO_TRACKER_PARSER = new Parser()
|
||||
type: PSN_INFO_TRACKER_NAME_PARSER,
|
||||
});
|
||||
|
||||
const PSN_INFO_TRACKER_LIST_PARSER = 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';
|
||||
},
|
||||
})
|
||||
.array('trackers', { type: PSN_INFO_TRACKER_PARSER, lengthInBytes: 'data_len' });
|
||||
const InfoSystemNameParser = new Parser().string('system_name', { greedy: true });
|
||||
|
||||
const PSN_INFO_CHUNK_DATA_PARSER = new Parser()
|
||||
.nest('packet_header', { type: PSN_PACKET_HEADER_PARSER })
|
||||
.nest('system_name', { type: PSN_INFO_SYSTEM_NAME_PARSER })
|
||||
.nest('tracker_list', { type: PSN_INFO_TRACKER_LIST_PARSER });
|
||||
const InfoTrackerListParser = new Parser().array('trackers', { type: PSN_INFO_TRACKER_PARSER, readUntil: 'eof' });
|
||||
|
||||
module.exports = {
|
||||
PSN_INFO_SYSTEM_NAME_PARSER,
|
||||
PSN_INFO_TRACKER_NAME_PARSER,
|
||||
PSN_INFO_TRACKER_PARSER,
|
||||
PSN_INFO_TRACKER_LIST_PARSER,
|
||||
PSN_INFO_CHUNK_DATA_PARSER,
|
||||
InfoSystemNameParser,
|
||||
InfoTrackerListParser,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user