mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-08-10 18:03:54 +00:00
convert to typescript
This commit is contained in:
+117
@@ -0,0 +1,117 @@
|
||||
import { DataPacketChunk } from './decoders/data/data-packet-chunk';
|
||||
import { InfoPacketChunk } from './decoders/info/info-packet-chunk';
|
||||
import { InfoTrackerChunk } from './decoders/info/info-tracker-chunk';
|
||||
import packetDecoder from './decoders/packet';
|
||||
import { PacketHeaderChunk } from './decoders/packet-header-chunk';
|
||||
|
||||
export class Decoder {
|
||||
// TODO(jwetzell): check if these last packet headers are necessary to hold on to
|
||||
lastInfoPacketHeader?: PacketHeaderChunk;
|
||||
|
||||
lastDataPacketHeader?: PacketHeaderChunk;
|
||||
|
||||
infoPacketFrames: { [key: number]: InfoPacketChunk[] } = {};
|
||||
|
||||
dataPacketFrames: { [key: number]: DataPacketChunk[] } = {};
|
||||
|
||||
trackers: { [key: string]: InfoTrackerChunk } = {};
|
||||
|
||||
system_name: string = '';
|
||||
constructor() {}
|
||||
|
||||
updateInfo(framePackets: InfoPacketChunk[]) {
|
||||
framePackets.forEach((packet) => {
|
||||
if (packet.system_name) {
|
||||
this.system_name = packet.system_name.system_name;
|
||||
}
|
||||
|
||||
if (packet.tracker_list?.trackers) {
|
||||
Object.entries(packet.tracker_list.trackers).forEach(([trackerId, tracker]) => {
|
||||
if (!this.trackers[trackerId]) {
|
||||
this.trackers[trackerId] = {};
|
||||
}
|
||||
this.trackers[trackerId].tracker_name = tracker.tracker_name;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
updateData(framePackets: DataPacketChunk[]) {
|
||||
framePackets.forEach((packet) => {
|
||||
if (packet.tracker_list) {
|
||||
Object.entries(packet.tracker_list.trackers).forEach(([trackerId, tracker]) => {
|
||||
this.trackers[trackerId] = {
|
||||
...this.trackers[trackerId],
|
||||
...tracker,
|
||||
};
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// TODO(jwetzell): add invalid frame id decoding. Scenario where a frame id is reused before one is complete
|
||||
decode(packetBuf: Buffer): DataPacketChunk | InfoPacketChunk | undefined {
|
||||
const packet = packetDecoder(packetBuf);
|
||||
if (packet === undefined) {
|
||||
return packet;
|
||||
}
|
||||
|
||||
if (packet.id === 0x6756) {
|
||||
const infoPacket = packet as InfoPacketChunk;
|
||||
if (infoPacket && infoPacket.has_subchunks) {
|
||||
const currentInfoPacketHeader = infoPacket.packet_header;
|
||||
if (!currentInfoPacketHeader) {
|
||||
// NOTE(jwetzell): not sure that info packets without a header subchunk are valid?
|
||||
throw new Error('malformed packet');
|
||||
}
|
||||
|
||||
const systemSubChunk = infoPacket.system_name;
|
||||
if (!systemSubChunk) {
|
||||
// NOTE(jwetzell): not sure that info packets without a system subchunk are valid?
|
||||
throw new Error('malformed packet');
|
||||
}
|
||||
if (currentInfoPacketHeader.frame_id) {
|
||||
if (this.infoPacketFrames[currentInfoPacketHeader.frame_id] === undefined) {
|
||||
this.infoPacketFrames[currentInfoPacketHeader.frame_id] = [];
|
||||
}
|
||||
this.infoPacketFrames[currentInfoPacketHeader.frame_id].push(infoPacket);
|
||||
|
||||
if (
|
||||
this.infoPacketFrames[currentInfoPacketHeader.frame_id].length ===
|
||||
currentInfoPacketHeader.frame_packet_count
|
||||
) {
|
||||
this.updateInfo(this.infoPacketFrames[currentInfoPacketHeader.frame_id]);
|
||||
delete this.infoPacketFrames[currentInfoPacketHeader.frame_id];
|
||||
}
|
||||
}
|
||||
|
||||
this.lastInfoPacketHeader = currentInfoPacketHeader;
|
||||
}
|
||||
} else if (packet.id === 0x6755) {
|
||||
const dataPacket = packet as DataPacketChunk;
|
||||
if (dataPacket.has_subchunks) {
|
||||
const currentDataPacketHeader = dataPacket.packet_header;
|
||||
if (!currentDataPacketHeader) {
|
||||
// NOTE(jwetzell): not sure that info packets without a header subchunk are valid?
|
||||
throw new Error('malformed packet');
|
||||
}
|
||||
|
||||
if (currentDataPacketHeader.frame_id) {
|
||||
if (this.dataPacketFrames[currentDataPacketHeader.frame_id] === undefined) {
|
||||
this.dataPacketFrames[currentDataPacketHeader.frame_id] = [];
|
||||
}
|
||||
this.dataPacketFrames[currentDataPacketHeader.frame_id].push(dataPacket);
|
||||
if (
|
||||
this.dataPacketFrames[currentDataPacketHeader.frame_id].length ===
|
||||
currentDataPacketHeader.frame_packet_count
|
||||
) {
|
||||
this.updateData(this.dataPacketFrames[currentDataPacketHeader.frame_id]);
|
||||
delete this.dataPacketFrames[currentDataPacketHeader.frame_id];
|
||||
}
|
||||
}
|
||||
this.lastDataPacketHeader = currentDataPacketHeader;
|
||||
}
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user