Skip to content

Commit

Permalink
Merge pull request #240 from 1-alex98/feature/#147-assoicate-replay-f…
Browse files Browse the repository at this point in the history
…iles

Associating replays
  • Loading branch information
Jazcash authored Aug 31, 2023
2 parents 0f20322 + 89a6041 commit 9ffd6ae
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 17 deletions.
7 changes: 7 additions & 0 deletions .electron-builder.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const config = {
category: "Game",
},
publish: null,
fileAssociations: [
{
"ext": "sdfz",
"description": "BAR Replay File",
"role": "Viewer"
}
]
};

module.exports = config;
55 changes: 50 additions & 5 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,72 @@ export class Application {
this.mainWindow.window.on("restore", () => this.mainWindow?.window.flashFrame(false));

this.setupHandlers();
this.setupSecondInstanceOpened();
}

protected setupSecondInstanceOpened() {
app.on("second-instance", (_event, commandLine, _workingDirectory, _additionalData) => {
console.log("Second Instance opening with command line: " + commandLine);

this.focusWindows();

this.openFile(commandLine[commandLine.length - 1]);
});

app.on("open-file", (_, path) => {
console.log("Mac OS opening file: " + path);

this.focusWindows();

this.openFile(path);
});
}

protected openFile(path: string) {
if (!path.endsWith(".sdfz")) {
return;
}
this.mainWindow?.window.webContents.send("open-replay", path);
}

private focusWindows() {
if (this.mainWindow?.window) {
if (this.mainWindow?.window.isMinimized()) this.mainWindow?.window.restore();
this.mainWindow?.window.focus();
}
}

protected setupHandlers() {
ipcMain.handle("getInfo", async () => {
return this.getInfo();
});

ipcMain.handle("flashFrame", (event, flag: boolean) => {
ipcMain.handle("flashFrame", (_event, flag: boolean) => {
this.mainWindow?.window.flashFrame(flag);
});

ipcMain.handle("encryptString", async (event, str: string) => {
ipcMain.handle("encryptString", async (_event, str: string) => {
if (safeStorage.isEncryptionAvailable()) {
return safeStorage.encryptString(str);
}
console.warn(`encryption not available, storing as plaintext`);
return str;
});

ipcMain.handle("decryptString", async (event, buffer: Buffer) => {
ipcMain.handle("decryptString", async (_event, buffer: Buffer) => {
if (safeStorage.isEncryptionAvailable()) {
return safeStorage.decryptString(buffer);
}
console.warn(`encryption not available, returning buffer`);
return buffer.toString();
});
let openedReplayAlready = false;
ipcMain.handle("opened-replay", () => {
console.log(process.argv);
if (process.argv.length == 0 || openedReplayAlready) return null;
openedReplayAlready = true; //in case of reloading the app do not open replay again
return process.argv[process.argv.length - 1].endsWith(".sdfz") ? process.argv[process.argv.length - 1] : null;
});
}

protected getInfo() {
Expand Down Expand Up @@ -161,6 +201,11 @@ export class Application {
}
}

unhandled();
const gotTheLock = app.requestSingleInstanceLock();

new Application();
if (!gotTheLock) {
app.quit();
} else {
unhandled();
new Application();
}
8 changes: 8 additions & 0 deletions src/renderer/api/cache-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ export class CacheDbAPI extends Kysely<CacheDatabase> {
.execute();
},
},
"2023-06-20": {
async up(db) {
await db.schema
.alterTable("replay")
.addColumn("filePath", "varchar", (col) => col)
.execute();
},
},
};
}

Expand Down
7 changes: 5 additions & 2 deletions src/renderer/api/content/replay-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export class ReplayContentAPI {
return query.offset(options.offset).limit(options.limit).execute();
}

public async parseAndLaunchReplay(replayPath: string) {
const replay = await this.parseReplay(replayPath);
api.game.launch((await replay) as Replay);
}

public async getReplayById(replayId: number) {
return api.cacheDb.selectFrom("replay").selectAll().where("replayId", "=", replayId).executeTakeFirst();
}
Expand Down Expand Up @@ -124,11 +129,9 @@ export class ReplayContentAPI {
}
}
}

protected async cacheReplay(replayFilePath: string) {
const replayFileName = path.parse(replayFilePath).base;
console.debug(`Caching: ${replayFileName}`);

try {
const replayData = await this.parseReplay(replayFilePath);

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/api/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class GameAPI {
const scriptPath = (launchArg = path.join(api.info.contentPath, this.scriptName));
await fs.promises.writeFile(scriptPath, script);
} else if (isReplay(arg)) {
launchArg = path.join(api.content.replays.replaysDir, arg.fileName);
launchArg = arg.filePath ? arg.filePath : path.join(api.content.replays.replaysDir, arg.fileName);
}

const args = ["--write-dir", api.info.contentPath, "--isolation", launchArg];
Expand Down
22 changes: 13 additions & 9 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "flag-icons/css/flag-icons.min.css";
import "primeicons/primeicons.css";
import "@/styles/styles.scss";

import { ipcRenderer } from "electron";
import path from "path";
import PrimeVue from "primevue/config";
import Tooltip from "primevue/tooltip";
Expand Down Expand Up @@ -44,15 +45,7 @@ declare module "vue-router" {
}
});

// window.addEventListener("beforeunload", async (event) => {
// console.debug("beforeunload", event);
// event.preventDefault();
// if (api.comms.isConnected.value) {
// //await api.comms.request("c.auth.disconnect", {});
// api.comms.disconnect();
// }
// return event;
// });
await replayOpenedHandlers();
})();

async function setupVue() {
Expand All @@ -75,6 +68,17 @@ async function setupVue() {
}
}

async function replayOpenedHandlers() {
const replay = await ipcRenderer.invoke("opened-replay");
if (replay) {
api.content.replays.parseAndLaunchReplay(replay);
}
ipcRenderer.on("open-replay", (_event, arg) => {
console.log("renderer recaeived replay to launch:" + arg);
api.content.replays.parseAndLaunchReplay(arg);
});
}

async function setupI18n() {
const myLocale = Intl.DateTimeFormat().resolvedOptions().locale.split("-")[0];

Expand Down
1 change: 1 addition & 0 deletions src/renderer/model/cache/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type ReplayTable = {
replayId: Generated<number>;
gameId: string;
fileName: string;
filePath: string | null;
engineVersion: string;
gameVersion: string;
mapScriptName: string;
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/views/library/replays.vue
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ function watchReplay(replay: Replay) {
}
function showReplayFile(replay: Replay) {
if (replay.filePath) {
shell.showItemInFolder(replay.filePath);
return;
}
shell.showItemInFolder(path.join(api.content.replays.replaysDir, replay.fileName));
}
</script>
Expand Down
1 change: 1 addition & 0 deletions src/renderer/workers/parse-replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const parseReplay = exposeWorkerFunction(async (replayPath: string) => {
return {
gameId: replayData.header.gameId,
fileName: path.parse(replayPath).base,
filePath: replayPath,
engineVersion: replayData.info.meta.engine,
gameVersion: replayData.info.meta.game,
mapScriptName: replayData.info.meta.map,
Expand Down

0 comments on commit 9ffd6ae

Please sign in to comment.