diff options
Diffstat (limited to 'src/packets/snes.ts')
| -rw-r--r-- | src/packets/snes.ts | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/packets/snes.ts b/src/packets/snes.ts new file mode 100644 index 0000000..07ee91b --- /dev/null +++ b/src/packets/snes.ts @@ -0,0 +1,93 @@ +import { PACKET_TYPES } from "../constants"; +import { encodeString, readBoolean, readString, readUint64, readUint8, writeBoolean, writeUint64, writeUint8 } from "../utils"; +import { TASDPacket, buildBuffer } from "./utils"; + +export class SNESClockFilterPacket implements TASDPacket { + constructor(public time: number) {} + get key() { + return PACKET_TYPES.SNES_CLOCK_FILTER; + } + get size() { + return 1; + } + static fromBuffer(buffer: Uint8Array) { + return new this(readUint8(buffer, 0)); + } + toBuffer(g_keylen: number): Uint8Array { + const payload = new Uint8Array(1); + writeUint8(this.time, payload, 0); + return buildBuffer(g_keylen, this.key, payload); + } + toString(): string { + return `SNESClockFilter ${this.time}`; + } +} + +export class SNESOverreadPacket implements TASDPacket { + constructor(public high: boolean) {} + get key() { + return PACKET_TYPES.SNES_OVERREAD; + } + get size() { + return 1; + } + static fromBuffer(buffer: Uint8Array) { + return new this(readBoolean(buffer, 0)); + } + toBuffer(g_keylen: number): Uint8Array { + const payload = new Uint8Array(1); + writeBoolean(this.high, payload, 0); + return buildBuffer(g_keylen, this.key, payload); + } + toString(): string { + return `SNESOverread ${this.high}`; + } +} + +export class SNESGameGenieCodePacket implements TASDPacket { + constructor(public code: string) {} + get key() { + return PACKET_TYPES.SNES_GAME_GENIE_CODE; + } + get size() { + return this.code.length; + } + static fromBuffer(buffer: Uint8Array) { + return new this(readString(buffer, 0, buffer.length)); + } + toBuffer(g_keylen: number): Uint8Array { + return buildBuffer(g_keylen, this.key, encodeString(this.code)); + } + toString(): string { + return `SNESGameGenieCode ${this.code}`; + } +} + +export class SNESLatchTrainPacket implements TASDPacket { + constructor(public trains: bigint[]) {} + get key() { + return PACKET_TYPES.SNES_LATCH_TRAIN; + } + get size() { + return this.trains.length * 8; + } + static fromBuffer(buffer: Uint8Array) { + const trains = []; + for (let index = 0; index < buffer.length; index += 8) { + trains.push(readUint64(buffer, index)); + } + return new this(trains); + } + toBuffer(g_keylen: number): Uint8Array { + const payload = new Uint8Array(this.size); + let index = 0; + for (const train of this.trains) { + writeUint64(train, payload, index); + index += 8; + } + return buildBuffer(g_keylen, this.key, payload); + } + toString(): string { + return `SNESLatchTrain ${this.trains.length}`; + } +} |
