From f7d1b738067972e12710c4234cff52d2f1b2944d Mon Sep 17 00:00:00 2001 From: FinnHornhoover Date: Sun, 1 Oct 2023 13:49:59 +0300 Subject: [PATCH] implemented delete as ipc, adjusted for cache swap --- assets/js/server-selector.js | 48 +++++++++--------------------------- index.js | 48 ++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 39 deletions(-) diff --git a/assets/js/server-selector.js b/assets/js/server-selector.js index 00287d3..5ea6fc8 100644 --- a/assets/js/server-selector.js +++ b/assets/js/server-selector.js @@ -260,8 +260,6 @@ function startHashCheck(versionString, cacheMode) { } function loadCacheList() { - resetCacheNames(); - var versionjson = remotefs.readJsonSync(versionsPath); versionArray = versionjson["versions"]; @@ -293,19 +291,11 @@ function loadCacheList() { } function deletePlayableCache(versionString) { - if (versionString === "Offline") { - console.log("Cannot delete Offline directory!"); - return; - } - - // TODO: remove this function - resetCacheNames(); - - remotefs.removeSync(path.join(cacheRoot, versionString)); - console.log("Playable cache " + versionString + " has been removed!"); - - // this updates the labels etc. properly - startHashCheck(versionString, "playable"); + ipc.send("delete-files", { + localDir: cacheRoot, + cacheMode: "playable", + versionString: versionString, + }); } function downloadOfflineCache(versionString) { @@ -323,27 +313,11 @@ function downloadOfflineCache(versionString) { } function deleteOfflineCache(versionString) { - remotefs.removeSync(path.join(offlineRoot, versionString)); - console.log("Offline cache " + versionString + " has been removed!"); - - // this updates the labels etc. properly - startHashCheck(versionString, "offline"); -} - -function resetCacheNames() { - var currentCache = path.join(cacheRoot, "Fusionfall"); - var record = path.join(userData, ".lastver"); - - if (!remotefs.existsSync(currentCache)) { - return; - } - - var lastVersion = remotefs.readFileSync(record, (encoding = "utf8")); - remotefs.renameSync( - currentCache, - path.join(cacheRoot, lastVersion) - ); - console.log("Current cache " + lastVersion + " has been renamed to its original name."); + ipc.send("delete-files", { + localDir: offlineRoot, + cacheMode: "offline", + versionString: versionString, + }); } function performCacheSwap(newVersion) { @@ -381,7 +355,7 @@ function performCacheSwap(newVersion) { // Make note of what version we are launching for next launch remotefs.writeFileSync(record, newVersion); - + if (remotefs.existsSync(newCache) && !skip) { // Rename saved cache to FusionFall remotefs.renameSync(newCache, currentCache); diff --git a/index.js b/index.js index 51ac03d..7136f59 100644 --- a/index.js +++ b/index.js @@ -160,7 +160,7 @@ app.on("ready", function () { downloadFiles( arg.cdnDir, - path.join(arg.localDir, arg.versionString), + getSwappedPathSync(arg.localDir, arg.versionString), // this shouldn't matter, for consistency only versionHashes[arg.versionString][arg.cacheMode], function (sizes) { currentSizes.intact += sizes.intact; @@ -185,6 +185,34 @@ app.on("ready", function () { ); }); + ipc.on("delete-files", function (event, arg) { + var deleteDir = getSwappedPathSync(arg.localDir, arg.versionString); + + if (arg.cacheMode === "playable" && path.basename(deleteDir) === "Offline") { + dialog.showErrorBox("Error!", "Cannot delete Offline directory as a playable cache!"); + return; + } + + var currentSizes = versionSizes[arg.versionString][arg.cacheMode]; + currentSizes.intact = 0; + currentSizes.altered = 0; + + mainWindow.webContents.send("storage-loading-start", { + cacheMode: arg.cacheMode, + versionString: arg.versionString, + sizes: currentSizes, + }); + + fs.removeSync(deleteDir); + console.log(arg.versionString + " (" + arg.cacheMode + ") has been removed!"); + + mainWindow.webContents.send("storage-loading-complete", { + cacheMode: arg.cacheMode, + versionString: arg.versionString, + sizes: currentSizes, + }); + }); + ipc.on("hash-check", function (event, arg) { var currentSizes = versionSizes[arg.versionString][arg.cacheMode]; currentSizes.intact = 0; @@ -197,7 +225,7 @@ app.on("ready", function () { }); checkHashes( - path.join(arg.localDir, arg.versionString), + getSwappedPathSync(arg.localDir, arg.versionString), versionHashes[arg.versionString][arg.cacheMode], function (sizes) { currentSizes.intact += sizes.intact; @@ -272,6 +300,22 @@ function showMainWindow() { }); } +function getSwappedPathSync(localDir, versionString) { + var currentCache = path.join(localDir, "FusionFall"); + var versionCache = path.join(localDir, versionString); + var record = path.join(userData, ".lastver"); + + if (!fs.existsSync(versionCache) && + fs.existsSync(currentCache) && + fs.existsSync(record) && + versionString === fs.readFileSync(record, (encoding = "utf8"))) { + + versionCache = currentCache; + } + + return versionCache; +} + function downloadFile(cdnDir, localDir, relativePath, fileHash, callback, updateCallback) { var nginxUrl = cdnDir + "/" + relativePath; var localPath = path.join(localDir, relativePath);