Skip to content

Commit

Permalink
refactor: match getMetadata function pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceau committed Nov 3, 2022
1 parent d82a140 commit f2e04cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
11 changes: 7 additions & 4 deletions src/SlippiGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import type {
} from "./types";
import { SlpParser, SlpParserEvent } from "./utils/slpParser";
import type { SlpReadInput } from "./utils/slpReader";
import { getGameEnd } from "./utils/slpReader";
import { closeSlpFile, getMetadata, iterateEvents, openSlpFile, SlpInputSource } from "./utils/slpReader";
import { closeSlpFile, getGameEnd, getMetadata, iterateEvents, openSlpFile, SlpInputSource } from "./utils/slpReader";

/**
* Slippi Game class that wraps a file
Expand Down Expand Up @@ -124,7 +123,11 @@ export class SlippiGame {

public getGameEnd(options: { skipProcessing?: boolean } = {}): GameEndType | null {
if (options?.skipProcessing) {
return getGameEnd(this.input);
// Read game end block directly
const slpfile = openSlpFile(this.input);
const gameEnd = getGameEnd(slpfile);
closeSlpFile(slpfile);
return gameEnd;
}

this._process();
Expand Down Expand Up @@ -210,7 +213,7 @@ export class SlippiGame {
}

public getWinners(): PlacementType[] {
const gameEnd = getGameEnd(this.input);
const gameEnd = this.getGameEnd({ skipProcessing: true });
if (!gameEnd) {
return [];
}
Expand Down
47 changes: 19 additions & 28 deletions src/utils/slpReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,37 +645,28 @@ export function getMetadata(slpFile: SlpFileType): MetadataType | null {
return metadata;
}

export function getGameEnd(input: SlpReadInput): GameEndType | null {
let slpFile: SlpFileType | undefined;

try {
slpFile = openSlpFile(input);
const { rawDataPosition, rawDataLength, messageSizes } = slpFile;
const gameEndSize = messageSizes[Command.GAME_END];
if (!exists(gameEndSize)) {
return null;
}

// Subtract one to account for command byte
const gameEndPosition = rawDataPosition + rawDataLength - gameEndSize - 1;
export function getGameEnd(slpFile: SlpFileType): GameEndType | null {
const { rawDataPosition, rawDataLength, messageSizes } = slpFile;
const gameEndPayloadSize = messageSizes[Command.GAME_END];
if (!exists(gameEndPayloadSize) || gameEndPayloadSize <= 0) {
return null;
}

// Add one to include command byte in payload
const buffer = new Uint8Array(gameEndSize + 1);
readRef(slpFile.ref, buffer, 0, buffer.length, gameEndPosition);
if (buffer[0] !== Command.GAME_END) {
// This isn't even a game end payload
return null;
}
// Add one to account for command byte
const gameEndSize = gameEndPayloadSize + 1;
const gameEndPosition = rawDataPosition + rawDataLength - gameEndSize;

const gameEndMessage = parseMessage(Command.GAME_END, buffer);
if (!gameEndMessage) {
return null;
}
const buffer = new Uint8Array(gameEndSize);
readRef(slpFile.ref, buffer, 0, buffer.length, gameEndPosition);
if (buffer[0] !== Command.GAME_END) {
// This isn't even a game end payload
return null;
}

return gameEndMessage as GameEndType;
} catch (err) {
const gameEndMessage = parseMessage(Command.GAME_END, buffer);
if (!gameEndMessage) {
return null;
} finally {
slpFile && closeSlpFile(slpFile);
}

return gameEndMessage as GameEndType;
}
7 changes: 5 additions & 2 deletions test/slpReader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";

import { SlippiGame } from "../src";
import { getGameEnd, SlpInputSource } from "../src/utils/slpReader";
import { closeSlpFile, getGameEnd, openSlpFile, SlpInputSource } from "../src/utils/slpReader";

describe("when reading game end directly", () => {
it("should return the same game end object", () => {
Expand Down Expand Up @@ -59,5 +59,8 @@ describe("when reading game end directly", () => {
});

function getManualGameEnd(filePath: string) {
return getGameEnd({ source: SlpInputSource.FILE, filePath });
const slpfile = openSlpFile({ source: SlpInputSource.FILE, filePath });
const gameEnd = getGameEnd(slpfile);
closeSlpFile(slpfile);
return gameEnd;
}

0 comments on commit f2e04cb

Please sign in to comment.