Skip to content

Commit

Permalink
prevent multiple prd processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jazcash committed Sep 7, 2022
1 parent a958dc9 commit 3be1a71
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/api/comms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { arrayToMap, assign } from "jaz-ts-utils";
import { battleSchema, myUserSchema, TachyonClient } from "tachyon-client";
import { reactive } from "vue";

import { SpadsBattle } from "@/model/battle/tachyon-spads-battle";
import { SpadsBattle } from "@/model/battle/spads-battle";
import { tachyonLog } from "@/utils/tachyon-log";
export class CommsAPI extends TachyonClient {
constructor(config: ConstructorParameters<typeof TachyonClient>[0]) {
Expand Down
21 changes: 15 additions & 6 deletions src/api/content/game-content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { spawn } from "child_process";
import { ChildProcess, spawn } from "child_process";
import * as fs from "fs";
import * as glob from "glob-promise";
import { BufferStream, lastInArray, removeFromArray } from "jaz-ts-utils";
Expand Down Expand Up @@ -36,6 +36,7 @@ export class GameContentAPI extends AbstractContentAPI {
/** Latest version is last item */
public installedVersions: string[] = reactive([]);

protected prdProcess: ChildProcess | null = null;
protected prBinaryPath!: string;
protected ocotokit = new Octokit();
protected md5ToRapidVersionMap: Record<string, RapidVersion> = {};
Expand All @@ -56,14 +57,19 @@ export class GameContentAPI extends AbstractContentAPI {
*/
public async downloadGame(gameVersion = `${contentSources.rapid.game}:test`) {
return new Promise<void>((resolve) => {
const prDownloaderProcess = spawn(`${this.prBinaryPath}`, ["--filesystem-writepath", api.info.contentPath, "--download-game", gameVersion]);
if (this.prdProcess) {
console.warn("Could not spawn new prd instance until existing instance has finished");
return;
}

this.prdProcess = spawn(`${this.prBinaryPath}`, ["--filesystem-writepath", api.info.contentPath, "--download-game", gameVersion]);

console.debug(prDownloaderProcess.spawnargs);
console.debug(this.prdProcess.spawnargs);

let downloadType: DownloadType = DownloadType.Metadata;
let downloadInfo: DownloadInfo | undefined;

prDownloaderProcess.stdout.on("data", (stdout: Buffer) => {
this.prdProcess.stdout?.on("data", (stdout: Buffer) => {
const lines = stdout.toString().trim().split(os.EOL).filter(Boolean);
console.debug(lines.join("\n"));
const messages = lines.map((line) => this.processPrDownloaderLine(line)).filter(Boolean) as Message[];
Expand All @@ -87,11 +93,11 @@ export class GameContentAPI extends AbstractContentAPI {
}
});

prDownloaderProcess.stderr.on("data", (data: Buffer) => {
this.prdProcess.stderr?.on("data", (data: Buffer) => {
console.error(data.toString());
});

prDownloaderProcess.on("exit", async () => {
this.prdProcess.on("exit", async () => {
if (downloadInfo) {
if (!this.installedVersions.includes(downloadInfo.name)) {
this.installedVersions.push(downloadInfo.name);
Expand All @@ -100,6 +106,9 @@ export class GameContentAPI extends AbstractContentAPI {
removeFromArray(this.currentDownloads, downloadInfo);
this.onDownloadComplete.dispatch(downloadInfo);
}

this.prdProcess = null;

resolve();
});
});
Expand Down
3 changes: 3 additions & 0 deletions src/api/content/map-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export class MapContentAPI extends AbstractContentAPI {
// currently reliant on springfiles for scriptname lookup
public async installMapByScriptName(scriptName: string, host = contentSources.maps.http[0]!) {
if (this.getMapByScriptName(scriptName)) {
console.debug(`${scriptName} already installed`);
// TODO: validate if map is cached
return;
}

Expand All @@ -106,6 +108,7 @@ export class MapContentAPI extends AbstractContentAPI {
}

public async installMapByFilename(filename: string, scriptName: string, host = contentSources.maps.http[0]!): Promise<void> {
// TODO: tidy up this logic to avoid downloading/caching the same maps multiple times, or incorrectly assuming maps are downloaded/cached when they're not
if (this.getMapByFileName(filename)) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { reactive, Ref, ref, shallowReactive, shallowRef, toRaw } from "vue";

import { BattleChatMessage } from "@/model/battle/battle-chat";
import { OfflineBattle } from "@/model/battle/offline-battle";
import { SpadsBattle } from "@/model/battle/tachyon-spads-battle";
import { SpadsBattle } from "@/model/battle/spads-battle";
import { CurrentUser, User } from "@/model/user";

export class SessionAPI {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils/start-script-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { assign } from "jaz-ts-utils";

import { AbstractBattle } from "@/model/battle/abstract-battle";
import { OfflineBattle } from "@/model/battle/offline-battle";
import { SpadsBattle } from "@/model/battle/tachyon-spads-battle";
import { SpadsBattle } from "@/model/battle/spads-battle";
import { StartPosType } from "@/model/battle/types";
import type { StartScriptTypes } from "@/model/start-script";

Expand Down
2 changes: 1 addition & 1 deletion src/views/multiplayer/custom.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import BattlePreview from "@/components/battle/BattlePreview.vue";
import HostBattle from "@/components/battle/HostBattle.vue";
import Button from "@/components/inputs/Button.vue";
import Checkbox from "@/components/inputs/Checkbox.vue";
import { SpadsBattle } from "@/model/battle/tachyon-spads-battle";
import { SpadsBattle } from "@/model/battle/spads-battle";
const hostBattleOpen = ref(false);
const hidePvE = ref(false);
Expand Down

0 comments on commit 3be1a71

Please sign in to comment.