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
This commit is contained in:
CakeLancelot 2023-02-08 14:06:54 -06:00
parent 51f7eaf33d
commit c1db7bc047
2 changed files with 53 additions and 30 deletions

View File

@ -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. // For writing loginInfo.php, assetInfo.php, etc.
function setGameInfo(serverUUID) { function setGameInfo(serverUUID) {
var result = serverarray.filter(function (obj) { var result = serverarray.filter(function (obj) {
@ -164,36 +208,15 @@ function setGameInfo(serverUUID) {
return obj.name === result.version; return obj.name === result.version;
})[0]; })[0];
// if cache swapping property exists AND is `true`, run cache swapping logic
if (config["cache-swapping"]) { if (config["cache-swapping"]) {
// if cache swapping property exists AND is `true`, run cache swapping logic try {
// Cache folder renaming performCacheSwap(gameversion.name);
var cachedir = userdir + "\\..\\..\\LocalLow\\Unity\\Web Player\\Cache"; } catch (ex) {
var curversion = cachedir + "\\Fusionfall"; console.log(
var newversion = cachedir + "\\" + gameversion.name; "Error when swapping cache, it may get overwritten:\n" + ex
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"
);
}
} }
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 window.asseturl = gameversion.url; // gameclient.js needs to access this

View File

@ -110,8 +110,8 @@ app.on("ready", function () {
showMainWindow(); showMainWindow();
} }
} }
} catch (e) { } catch (ex) {
console.log("An error occurred while checking for the config."); console.log("An error occurred while checking for the config");
} }
// Makes it so external links are opened in the system browser, not Electron // Makes it so external links are opened in the system browser, not Electron