Skip to content

Commit

Permalink
Allow SlippiGame to accept an ArrayBuffer (#77)
Browse files Browse the repository at this point in the history
* Allow SlippiGame to accept an ArrayBuffer

* Allow SlippiGame to accept an ArrayBuffer

* add test for arraybuffer input

Co-authored-by: Vince Au <vinceau09@gmail.com>
  • Loading branch information
frankborden and vinceau committed Aug 16, 2021
1 parent 495258e commit af03a7b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/SlippiGame.ts
Expand Up @@ -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,
Expand All @@ -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");
}
Expand Down
10 changes: 10 additions & 0 deletions test/game.spec.ts
Expand Up @@ -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
Expand Down

0 comments on commit af03a7b

Please sign in to comment.