From af03a7b374e24abb59461150d1818b74d16fc816 Mon Sep 17 00:00:00 2001 From: Frank Borden <53030687+frankborden@users.noreply.github.com> Date: Mon, 16 Aug 2021 07:54:40 +0200 Subject: [PATCH] Allow SlippiGame to accept an ArrayBuffer (#77) * Allow SlippiGame to accept an ArrayBuffer * Allow SlippiGame to accept an ArrayBuffer * add test for arraybuffer input Co-authored-by: Vince Au --- src/SlippiGame.ts | 7 ++++++- test/game.spec.ts | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/SlippiGame.ts b/src/SlippiGame.ts index 83e8791e..c08a75fd 100644 --- a/src/SlippiGame.ts +++ b/src/SlippiGame.ts @@ -32,7 +32,7 @@ export class SlippiGame { private inputComputer: InputComputer = new InputComputer(); private statsComputer: Stats; - public constructor(input: string | Buffer, opts?: StatOptions) { + public constructor(input: string | Buffer | ArrayBuffer, opts?: StatOptions) { if (_.isString(input)) { this.input = { source: SlpInputSource.FILE, @@ -43,6 +43,11 @@ export class SlippiGame { source: SlpInputSource.BUFFER, buffer: input, }; + } else if (input instanceof ArrayBuffer) { + this.input = { + source: SlpInputSource.BUFFER, + buffer: Buffer.from(input), + }; } else { throw new Error("Cannot create SlippiGame with input of that type"); } diff --git a/test/game.spec.ts b/test/game.spec.ts index f962301c..fb9fac4e 100644 --- a/test/game.spec.ts +++ b/test/game.spec.ts @@ -119,6 +119,16 @@ it("should be able to support reading from a buffer input", () => { expect(_.last(settings.players).characterId).toBe(0xe); }); +it("should be able to support reading from an array buffer input", () => { + const buf = fs.readFileSync("slp/sheik_vs_ics_yoshis.slp"); + const arrayBuf = buf.buffer; + const game = new SlippiGame(arrayBuf); + const settings = game.getSettings(); + expect(settings.stageId).toBe(8); + expect(_.first(settings.players).characterId).toBe(0x13); + expect(_.last(settings.players).characterId).toBe(0xe); +}); + it("should support realtime parsing", () => { const fullData = fs.readFileSync("slp/realtimeTest.slp"); const buf = Buffer.alloc(100e6); // Allocate 100 MB of space