Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow SlippiGame to accept an ArrayBuffer #77

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SlippiGame.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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