EZPPLauncher/app.js

138 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2022-10-16 13:54:29 +00:00
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const { setupTitlebar, attachTitlebarToWindow } = require('custom-electron-titlebar/main');
const windowManager = require('./ui/windowManager');
2022-10-16 14:47:31 +00:00
const osuUtil = require('./osuUtil');
2022-10-16 16:41:53 +00:00
const config = require('./config');
const fs = require('fs');
let tempOsuPath;
2022-10-16 13:54:29 +00:00
const run = () => {
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit();
return;
}
2022-10-16 16:41:53 +00:00
2022-10-16 13:54:29 +00:00
setupTitlebar();
let mainWindow;
app.whenReady().then(() => {
2022-10-16 16:41:53 +00:00
mainWindow = createWindow();
mainWindow.on('show', async () => {
2022-10-16 20:44:56 +00:00
await doUpdateCheck(mainWindow);
})
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) mainWindow = createWindow();
})
app.on('window-all-closed', () => {
app.quit()
})
2022-10-16 21:10:07 +00:00
ipcMain.handle('launch', async () => {
const result = await osuUtil.startOsuWithDevServer(tempOsuPath, "ez-pp.farm", async () => {
await doUpdateCheck(mainWindow);
});
return result;
})
2022-10-16 20:44:56 +00:00
ipcMain.on('do-update-check', async () => {
await doUpdateCheck(mainWindow);
})
ipcMain.on('do-update', async () => {
2022-10-16 16:41:53 +00:00
const osuPath = await config.get("osuPath", "");
2022-10-16 20:20:50 +00:00
const isValid = await osuUtil.isValidOsuFolder(osuPath);
if (osuPath.trim == "" || !isValid) {
mainWindow.webContents.send('status_update', {
2022-10-16 20:44:56 +00:00
type: "error",
message: "Invalid osu! folder"
});
2022-10-16 20:20:50 +00:00
return;
}
2022-10-16 16:52:34 +00:00
if (fs.existsSync(osuPath)) {
2022-10-16 16:41:53 +00:00
tempOsuPath = osuPath;
const osuConfig = await osuUtil.getLatestConfig(tempOsuPath);
const lastVersion = await osuConfig.get("LastVersion");
2022-10-16 16:52:34 +00:00
let releaseStream = "stable40";
if (lastVersion.endsWith("cuttingedge"))
releaseStream = "cuttingedge"
else if (lastVersion.endsWith("beta"))
releaseStream = "beta";
2022-10-16 19:13:30 +00:00
2022-10-16 16:52:34 +00:00
const releaseFiles = await osuUtil.getUpdateFiles(releaseStream);
2022-10-16 19:13:30 +00:00
const filesToDownload = await osuUtil.filesThatNeedUpdate(tempOsuPath, releaseFiles);
2022-10-16 20:44:56 +00:00
const downloadTask = await osuUtil.downloadUpdateFiles(osuPath, filesToDownload);
downloadTask.on('completed', () => {
mainWindow.webContents.send('status_update', {
type: "update-complete"
})
});
2022-10-16 20:20:50 +00:00
} else
mainWindow.webContents.send('status_update', {
2022-10-16 20:44:56 +00:00
type: "error",
message: "Invalid osu! folder"
});
2022-10-16 13:54:29 +00:00
})
2022-10-16 14:47:31 +00:00
ipcMain.handle('set-osu-dir', async (event) => {
2022-10-16 13:54:29 +00:00
const yes = await dialog.showOpenDialog({
properties: ['openDirectory']
})
2022-10-16 14:47:31 +00:00
if (yes.filePaths.length <= 0)
return undefined;
const folderPath = yes.filePaths[0];
2022-10-16 20:20:50 +00:00
const validOsuDir = await osuUtil.isValidOsuFolder(folderPath);
2022-10-16 16:41:53 +00:00
if (validOsuDir) await config.set("osuPath", folderPath);
return validOsuDir;
2022-10-16 13:54:29 +00:00
})
})
}
2022-10-16 20:44:56 +00:00
async function doUpdateCheck(window) {
const osuPath = await config.get("osuPath", "");
const isValid = await osuUtil.isValidOsuFolder(osuPath);
if (osuPath.trim == "" || !isValid) {
window.webContents.send('status_update', {
type: "missing-folder"
})
return;
}
if (fs.existsSync(osuPath)) {
tempOsuPath = osuPath;
const osuConfig = await osuUtil.getLatestConfig(tempOsuPath);
const lastVersion = await osuConfig.get("LastVersion");
let releaseStream = "stable40";
if (lastVersion.endsWith("cuttingedge"))
releaseStream = "cuttingedge"
else if (lastVersion.endsWith("beta"))
releaseStream = "beta";
const releaseFiles = await osuUtil.getUpdateFiles(releaseStream);
const filesToDownload = await osuUtil.filesThatNeedUpdate(tempOsuPath, releaseFiles);
window.webContents.send('status_update', {
type: filesToDownload.length > 0 ? "update-available" : "up-to-date"
})
} else
window.webContents.send('status_update', {
type: "missing-folder"
})
}
2022-10-16 13:54:29 +00:00
function createWindow() {
// Create the browser window.
2022-10-16 20:51:16 +00:00
const win = windowManager.createWindow(520, 350);
2022-10-16 13:54:29 +00:00
win.loadFile('./html/index.html');
attachTitlebarToWindow(win);
win.webContents.setWindowOpenHandler(() => "deny");
2022-10-16 13:54:29 +00:00
win.webContents.on('did-finish-load', function () {
if (win.webContents.getZoomFactor() != 0.9)
win.webContents.setZoomFactor(0.9)
});
return win;
}
run();