mirror of
https://github.com/jwetzell/psn-js.git
synced 2026-08-18 05:34:15 +00:00
format
This commit is contained in:
+133
-108
@@ -2,131 +2,156 @@ import { Constants } from './constants.js';
|
||||
import { Encoders, Tracker } from './index.js';
|
||||
|
||||
export class Encoder {
|
||||
private systemName: string;
|
||||
private versionHigh: number;
|
||||
private versionLow: number;
|
||||
dataFrameId: number = 1;
|
||||
infoFrameId: number = 1;
|
||||
constructor(systemName: string, versionHigh: number = 2, versionLow: number = 3) {
|
||||
this.systemName = systemName;
|
||||
this.versionHigh = versionHigh;
|
||||
this.versionLow = versionLow;
|
||||
}
|
||||
private systemName: string;
|
||||
private versionHigh: number;
|
||||
private versionLow: number;
|
||||
dataFrameId: number = 1;
|
||||
infoFrameId: number = 1;
|
||||
constructor(
|
||||
systemName: string,
|
||||
versionHigh: number = 2,
|
||||
versionLow: number = 3
|
||||
) {
|
||||
this.systemName = systemName;
|
||||
this.versionHigh = versionHigh;
|
||||
this.versionLow = versionLow;
|
||||
}
|
||||
|
||||
setSystemName(systemName: string) {
|
||||
this.systemName = systemName;
|
||||
}
|
||||
setVersionHigh(versionHigh: number) {
|
||||
this.versionHigh = versionHigh;
|
||||
}
|
||||
setSystemName(systemName: string) {
|
||||
this.systemName = systemName;
|
||||
}
|
||||
setVersionHigh(versionHigh: number) {
|
||||
this.versionHigh = versionHigh;
|
||||
}
|
||||
|
||||
setVersionLow(versionLow: number) {
|
||||
this.versionLow = versionLow;
|
||||
}
|
||||
setVersionLow(versionLow: number) {
|
||||
this.versionLow = versionLow;
|
||||
}
|
||||
|
||||
resetDataFrameId() {
|
||||
this.dataFrameId = 1;
|
||||
}
|
||||
resetDataFrameId() {
|
||||
this.dataFrameId = 1;
|
||||
}
|
||||
|
||||
resetInfoFrameId() {
|
||||
this.infoFrameId = 1;
|
||||
}
|
||||
resetInfoFrameId() {
|
||||
this.infoFrameId = 1;
|
||||
}
|
||||
|
||||
getInfoPackets(timestamp: bigint, trackers: Tracker[], frameId?: number) {
|
||||
if (frameId !== undefined) {
|
||||
if (!Number.isInteger(frameId)) {
|
||||
throw new Error('frame id must be an integer');
|
||||
}
|
||||
getInfoPackets(timestamp: bigint, trackers: Tracker[], frameId?: number) {
|
||||
if (frameId !== undefined) {
|
||||
if (!Number.isInteger(frameId)) {
|
||||
throw new Error('frame id must be an integer');
|
||||
}
|
||||
|
||||
if (frameId > 255 || frameId < 0) {
|
||||
throw new Error('frame id must be >= 0 and <= 255');
|
||||
}
|
||||
}
|
||||
if (frameId > 255 || frameId < 0) {
|
||||
throw new Error('frame id must be >= 0 and <= 255');
|
||||
}
|
||||
}
|
||||
|
||||
const systemNameChunk = Encoders.InfoSystemNameChunk(this.systemName);
|
||||
const systemNameChunk = Encoders.InfoSystemNameChunk(this.systemName);
|
||||
|
||||
const trackerChunks = trackers.map((tracker: Tracker) => tracker.getInfoChunk());
|
||||
const trackerChunks = trackers.map((tracker: Tracker) =>
|
||||
tracker.getInfoChunk()
|
||||
);
|
||||
|
||||
const infoPackets: Uint8Array[] = [];
|
||||
const infoPackets: Uint8Array[] = [];
|
||||
|
||||
const trackerChunksLists: Uint8Array[][] = [];
|
||||
let currentTrackerList: Uint8Array[] = [];
|
||||
const trackerChunksLists: Uint8Array[][] = [];
|
||||
let currentTrackerList: Uint8Array[] = [];
|
||||
|
||||
let currentInfoPacketSize = Constants.PACKET_HEADER_SIZE + systemNameChunk.length + Constants.CHUNK_HEADER_SIZE;
|
||||
let currentInfoPacketSize =
|
||||
Constants.PACKET_HEADER_SIZE +
|
||||
systemNameChunk.length +
|
||||
Constants.CHUNK_HEADER_SIZE;
|
||||
|
||||
trackerChunks.forEach((trackerChunk: Uint8Array) => {
|
||||
if (currentInfoPacketSize + trackerChunk.length > Constants.MAX_UDP_PACKET_SIZE) {
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
currentTrackerList = [];
|
||||
currentInfoPacketSize = 0;
|
||||
}
|
||||
currentTrackerList.push(trackerChunk);
|
||||
currentInfoPacketSize += trackerChunk.length;
|
||||
});
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
const header = Encoders.PacketHeaderChunk(
|
||||
timestamp,
|
||||
this.versionHigh,
|
||||
this.versionLow,
|
||||
frameId ? frameId : this.infoFrameId,
|
||||
trackerChunksLists.length
|
||||
);
|
||||
trackerChunksLists.forEach((trackerChunkList) => {
|
||||
infoPackets.push(
|
||||
Encoders.InfoPacketChunk(header, systemNameChunk, Encoders.InfoTrackerListChunk(trackerChunkList))
|
||||
);
|
||||
});
|
||||
this.infoFrameId += 1;
|
||||
if (this.infoFrameId > 255) {
|
||||
this.infoFrameId = 0;
|
||||
}
|
||||
return infoPackets;
|
||||
}
|
||||
trackerChunks.forEach((trackerChunk: Uint8Array) => {
|
||||
if (
|
||||
currentInfoPacketSize + trackerChunk.length >
|
||||
Constants.MAX_UDP_PACKET_SIZE
|
||||
) {
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
currentTrackerList = [];
|
||||
currentInfoPacketSize = 0;
|
||||
}
|
||||
currentTrackerList.push(trackerChunk);
|
||||
currentInfoPacketSize += trackerChunk.length;
|
||||
});
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
const header = Encoders.PacketHeaderChunk(
|
||||
timestamp,
|
||||
this.versionHigh,
|
||||
this.versionLow,
|
||||
frameId ? frameId : this.infoFrameId,
|
||||
trackerChunksLists.length
|
||||
);
|
||||
trackerChunksLists.forEach((trackerChunkList) => {
|
||||
infoPackets.push(
|
||||
Encoders.InfoPacketChunk(
|
||||
header,
|
||||
systemNameChunk,
|
||||
Encoders.InfoTrackerListChunk(trackerChunkList)
|
||||
)
|
||||
);
|
||||
});
|
||||
this.infoFrameId += 1;
|
||||
if (this.infoFrameId > 255) {
|
||||
this.infoFrameId = 0;
|
||||
}
|
||||
return infoPackets;
|
||||
}
|
||||
|
||||
getDataPackets(timestamp: bigint, trackers: Tracker[], frameId?: number) {
|
||||
if (frameId !== undefined) {
|
||||
if (!Number.isInteger(frameId)) {
|
||||
throw new Error('frame id must be an integer');
|
||||
}
|
||||
getDataPackets(timestamp: bigint, trackers: Tracker[], frameId?: number) {
|
||||
if (frameId !== undefined) {
|
||||
if (!Number.isInteger(frameId)) {
|
||||
throw new Error('frame id must be an integer');
|
||||
}
|
||||
|
||||
if (frameId > 255 || frameId < 0) {
|
||||
throw new Error('frame id must be >= 0 and <= 255');
|
||||
}
|
||||
}
|
||||
if (frameId > 255 || frameId < 0) {
|
||||
throw new Error('frame id must be >= 0 and <= 255');
|
||||
}
|
||||
}
|
||||
|
||||
const allTrackerChunks = trackers.map((tracker) => tracker.getDataChunk());
|
||||
const dataPackets: Uint8Array[] = [];
|
||||
const allTrackerChunks = trackers.map((tracker) => tracker.getDataChunk());
|
||||
const dataPackets: Uint8Array[] = [];
|
||||
|
||||
const trackerChunksLists: Uint8Array[][] = [];
|
||||
let currentTrackerList: Uint8Array[] = [];
|
||||
const trackerChunksLists: Uint8Array[][] = [];
|
||||
let currentTrackerList: Uint8Array[] = [];
|
||||
|
||||
let currentDataPacketSize = Constants.PACKET_HEADER_SIZE + Constants.CHUNK_HEADER_SIZE;
|
||||
let currentDataPacketSize =
|
||||
Constants.PACKET_HEADER_SIZE + Constants.CHUNK_HEADER_SIZE;
|
||||
|
||||
allTrackerChunks.forEach((trackerChunk) => {
|
||||
if (currentDataPacketSize + trackerChunk.length > Constants.MAX_UDP_PACKET_SIZE) {
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
currentTrackerList = [];
|
||||
currentDataPacketSize = 0;
|
||||
}
|
||||
currentTrackerList.push(trackerChunk);
|
||||
currentDataPacketSize += trackerChunk.length;
|
||||
});
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
allTrackerChunks.forEach((trackerChunk) => {
|
||||
if (
|
||||
currentDataPacketSize + trackerChunk.length >
|
||||
Constants.MAX_UDP_PACKET_SIZE
|
||||
) {
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
currentTrackerList = [];
|
||||
currentDataPacketSize = 0;
|
||||
}
|
||||
currentTrackerList.push(trackerChunk);
|
||||
currentDataPacketSize += trackerChunk.length;
|
||||
});
|
||||
trackerChunksLists.push(currentTrackerList);
|
||||
|
||||
const header = Encoders.PacketHeaderChunk(
|
||||
timestamp,
|
||||
this.versionHigh,
|
||||
this.versionLow,
|
||||
frameId ? frameId : this.dataFrameId,
|
||||
trackerChunksLists.length
|
||||
);
|
||||
trackerChunksLists.forEach((trackerChunks) => {
|
||||
dataPackets.push(Encoders.DataPacketChunk(header, Encoders.DataTrackerListChunk(trackerChunks)));
|
||||
});
|
||||
this.dataFrameId += 1;
|
||||
if (this.dataFrameId > 255) {
|
||||
this.dataFrameId = 0;
|
||||
}
|
||||
return dataPackets;
|
||||
}
|
||||
const header = Encoders.PacketHeaderChunk(
|
||||
timestamp,
|
||||
this.versionHigh,
|
||||
this.versionLow,
|
||||
frameId ? frameId : this.dataFrameId,
|
||||
trackerChunksLists.length
|
||||
);
|
||||
trackerChunksLists.forEach((trackerChunks) => {
|
||||
dataPackets.push(
|
||||
Encoders.DataPacketChunk(
|
||||
header,
|
||||
Encoders.DataTrackerListChunk(trackerChunks)
|
||||
)
|
||||
);
|
||||
});
|
||||
this.dataFrameId += 1;
|
||||
if (this.dataFrameId > 255) {
|
||||
this.dataFrameId = 0;
|
||||
}
|
||||
return dataPackets;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user