From c1db7bc0476133c815a76efcfa2ad230cb9fb1c8 Mon Sep 17 00:00:00 2001 From: CakeLancelot Date: Wed, 8 Feb 2023 14:06:54 -0600 Subject: [PATCH] Refactor cache swapping logic * Hoist everything into a function * Add error handling * Rename vars for clarity * Skip renaming if the current and new version are the same: this along with the error handling should fix the black screen when using multiple clients --- assets/js/serverselector.js | 79 ++++++++++++++++++++++++------------- index.js | 4 +- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/assets/js/serverselector.js b/assets/js/serverselector.js index 3d8c4e6..05e1e71 100644 --- a/assets/js/serverselector.js +++ b/assets/js/serverselector.js @@ -155,6 +155,50 @@ function loadServerList() { } } +function performCacheSwap(newversion) { + var cacheroot = userdir + "\\..\\..\\LocalLow\\Unity\\Web Player\\Cache"; + var currentcache = cacheroot + "\\Fusionfall"; + var newcache = cacheroot + "\\" + newversion; + var record = userdir + "\\.lastver"; + + // if cache renaming would result in a no-op (ex. launching the same version + // two times), then skip it. this avoids permissions errors with multiple clients + // (file/folder is already open in another process) + var skip = false; + + if (remotefs.existsSync(currentcache)) { + // cache already exists, find out what version it belongs to + if (remotefs.existsSync(record)) { + lastversion = remotefs.readFileSync(record); + if (lastversion != newversion) { + remotefs.renameSync( + currentcache, + cacheroot + "\\" + lastversion + ); + } else { + console.log( + "Cached version unchanged, renaming will be skipped" + ); + skip = true; + } + console.log("Current cache is " + lastversion); + } else { + console.log( + "Couldn't find last version record; cache may get overwritten" + ); + } + } + + if (remotefs.existsSync(newcache) || !skip) { + // rename saved cache to FusionFall + remotefs.renameSync(newcache, currentcache); + console.log("Current cache swapped to " + newversion); + } + + // make note of what version we are launching for next launch + remotefs.writeFileSync(record, newversion); +} + // For writing loginInfo.php, assetInfo.php, etc. function setGameInfo(serverUUID) { var result = serverarray.filter(function (obj) { @@ -164,36 +208,15 @@ function setGameInfo(serverUUID) { return obj.name === result.version; })[0]; + // if cache swapping property exists AND is `true`, run cache swapping logic if (config["cache-swapping"]) { - // if cache swapping property exists AND is `true`, run cache swapping logic - // Cache folder renaming - var cachedir = userdir + "\\..\\..\\LocalLow\\Unity\\Web Player\\Cache"; - var curversion = cachedir + "\\Fusionfall"; - var newversion = cachedir + "\\" + gameversion.name; - var record = userdir + "\\.lastver"; - - if (remotefs.existsSync(curversion)) { - // cache already exists - // find out what version it belongs to - if (remotefs.existsSync(record)) { - var lastversion = remotefs.readFileSync(record); - remotefs.renameSync(curversion, cachedir + "\\" + lastversion); - console.log("Cached version " + lastversion); - } else { - console.log( - "Couldn't find last version record; cache may get overwritten" - ); - } + try { + performCacheSwap(gameversion.name); + } catch (ex) { + console.log( + "Error when swapping cache, it may get overwritten:\n" + ex + ); } - - if (remotefs.existsSync(newversion)) { - // rename saved cache to FusionFall - remotefs.renameSync(newversion, curversion); - console.log("Loaded cached " + gameversion.name); - } - - // make note of what version we are launching for next launch - remotefs.writeFileSync(record, gameversion.name); } window.asseturl = gameversion.url; // gameclient.js needs to access this diff --git a/index.js b/index.js index e962dbc..89a8abe 100644 --- a/index.js +++ b/index.js @@ -110,8 +110,8 @@ app.on("ready", function () { showMainWindow(); } } - } catch (e) { - console.log("An error occurred while checking for the config."); + } catch (ex) { + console.log("An error occurred while checking for the config"); } // Makes it so external links are opened in the system browser, not Electron