added comments

This commit is contained in:
FinnHornhoover 2023-11-18 01:21:31 +03:00
parent fe6f42f67c
commit 7956f653fb

View File

@ -312,6 +312,7 @@ function editConfig() {
remotefs.writeFileSync(configPath, JSON.stringify(jsonToModify, null, 4)); remotefs.writeFileSync(configPath, JSON.stringify(jsonToModify, null, 4));
loadConfig(); loadConfig();
// check all offline caches if the offline root changes
if (shouldChangeRoot) handleCache("hash-check", null, "offline"); if (shouldChangeRoot) handleCache("hash-check", null, "offline");
} }
@ -350,6 +351,7 @@ function loadConfig() {
$("#editconfig-enableofflinecache").prop("checked", config["enable-offline-cache"]); $("#editconfig-enableofflinecache").prop("checked", config["enable-offline-cache"]);
$("#editconfig-verifyofflinecache").prop("checked", config["verify-offline-cache"]); $("#editconfig-verifyofflinecache").prop("checked", config["verify-offline-cache"]);
// alter offline root globally
offlineRoot = config["offline-cache-location"] || offlineRootDefault; offlineRoot = config["offline-cache-location"] || offlineRootDefault;
$("#editconfig-offlinecachelocation:text").val(offlineRoot); $("#editconfig-offlinecachelocation:text").val(offlineRoot);
@ -360,7 +362,7 @@ function loadServerList() {
var serverJson = remotefs.readJsonSync(serversPath); var serverJson = remotefs.readJsonSync(serversPath);
serverArray = serverJson["servers"]; serverArray = serverJson["servers"];
deselectServer(); // Disable buttons until another server is selected deselectServer(); // Remove selection and disable buttons until another server is selected
$(".server-listing-entry").remove(); // Clear out old stuff, if any $(".server-listing-entry").remove(); // Clear out old stuff, if any
if (serverArray.length > 0) { if (serverArray.length > 0) {
@ -391,6 +393,7 @@ function loadCacheList() {
// we might want to use a new version right away, so reload them // we might want to use a new version right away, so reload them
loadGameVersions(); loadGameVersions();
// load default hashes.json for reference while running the cache handler
if (!defaultHashes) { if (!defaultHashes) {
defaultHashes = remotefs.readJsonSync(path.join( defaultHashes = remotefs.readJsonSync(path.join(
__dirname, __dirname,
@ -398,8 +401,8 @@ function loadCacheList() {
)); ));
} }
deselectVersion(); deselectVersion(); // Remove selection and disable buttons until another server is selected
$(".cache-listing-entry").remove(); $(".cache-listing-entry").remove(); // Clear out old stuff, if any
$.each(versionArray, function (key, value) { $.each(versionArray, function (key, value) {
var row = document.createElement("tr"); var row = document.createElement("tr");
@ -420,6 +423,7 @@ function loadCacheList() {
$("#cache-tablebody").append(row); $("#cache-tablebody").append(row);
}); });
// simulate a cache handler run so that the buttons update properly
storageLoadingStart(); storageLoadingStart();
storageLoadingUpdate(cacheSizes); storageLoadingUpdate(cacheSizes);
storageLoadingComplete(cacheSizes); storageLoadingComplete(cacheSizes);
@ -477,6 +481,7 @@ function getCacheInfoCell(versionString, cacheMode) {
var labelCache = document.createElement("label"); var labelCache = document.createElement("label");
labelCache.setAttribute("id", labelID); labelCache.setAttribute("id", labelID);
labelCache.setAttribute("for", divID); labelCache.setAttribute("for", divID);
// pull existing info from cacheSizes when available
labelCache.innerHTML = getCacheLabelText( labelCache.innerHTML = getCacheLabelText(
(cacheSizes && cacheSizes[versionString]) ? (cacheSizes && cacheSizes[versionString]) ?
cacheSizes[versionString][cacheMode] : cacheSizes[versionString][cacheMode] :
@ -487,6 +492,7 @@ function getCacheInfoCell(versionString, cacheMode) {
divCacheButtons.setAttribute("id", labelID); divCacheButtons.setAttribute("id", labelID);
$.each(settings, function (buttonMode, config) { $.each(settings, function (buttonMode, config) {
// only delete allowed for playable game caches
if (cacheMode === "playable" && buttonMode !== "delete") { if (cacheMode === "playable" && buttonMode !== "delete") {
return; return;
} }
@ -501,6 +507,7 @@ function getCacheInfoCell(versionString, cacheMode) {
buttonCache.setAttribute("class", config.class); buttonCache.setAttribute("class", config.class);
buttonCache.setAttribute("title", config.tooltip); buttonCache.setAttribute("title", config.tooltip);
buttonCache.setAttribute("type", "button"); buttonCache.setAttribute("type", "button");
// handler setup
buttonCache.setAttribute("onclick", "handleCache(\"" + buttonMode + "\", \"" + versionString + "\", \"" + cacheMode + "\");"); buttonCache.setAttribute("onclick", "handleCache(\"" + buttonMode + "\", \"" + versionString + "\", \"" + cacheMode + "\");");
buttonCache.appendChild(iconItalic); buttonCache.appendChild(iconItalic);
@ -515,6 +522,7 @@ function getCacheInfoCell(versionString, cacheMode) {
} }
function storageLoadingStart(vString, cMode) { function storageLoadingStart(vString, cMode) {
// determine which cache versions and modes to visually update
var versionStrings = []; var versionStrings = [];
$.each(versionArray, function (key, value) { $.each(versionArray, function (key, value) {
if (vString) { if (vString) {
@ -526,9 +534,11 @@ function storageLoadingStart(vString, cMode) {
}); });
var cacheModes = (cMode) ? [cMode] : ["offline", "playable"]; var cacheModes = (cMode) ? [cMode] : ["offline", "playable"];
// deselect and disable the add version button until they are re-enabled
deselectVersion(); deselectVersion();
disableVersionAddButton(); disableVersionAddButton();
// turn buttons into spinners
$.each(versionStrings, function (vKey, versionString) { $.each(versionStrings, function (vKey, versionString) {
$.each(cacheModes, function (cKey, cacheMode) { $.each(cacheModes, function (cKey, cacheMode) {
var buttonDelete = document.getElementById(getCacheButtonID(versionString, cacheMode, "delete")); var buttonDelete = document.getElementById(getCacheButtonID(versionString, cacheMode, "delete"));
@ -552,6 +562,7 @@ function storageLoadingStart(vString, cMode) {
} }
function storageLoadingUpdate(allSizes) { function storageLoadingUpdate(allSizes) {
// update cacheSizes and display results
$.each(allSizes, function (versionString, vSizes) { $.each(allSizes, function (versionString, vSizes) {
$.each(vSizes, function (cacheMode, sizes) { $.each(vSizes, function (cacheMode, sizes) {
var label = document.getElementById(getCacheElemID(versionString, cacheMode, "label")); var label = document.getElementById(getCacheElemID(versionString, cacheMode, "label"));
@ -568,6 +579,7 @@ function storageLoadingUpdate(allSizes) {
} }
function storageLoadingComplete(allSizes) { function storageLoadingComplete(allSizes) {
// re-enable buttons according to the sizes that were read
$.each(allSizes, function (versionString, vSizes) { $.each(allSizes, function (versionString, vSizes) {
$.each(vSizes, function (cacheMode, sizes) { $.each(vSizes, function (cacheMode, sizes) {
var buttonDelete = document.getElementById(getCacheButtonID(versionString, cacheMode, "delete")); var buttonDelete = document.getElementById(getCacheButtonID(versionString, cacheMode, "delete"));
@ -604,24 +616,33 @@ function storageLoadingComplete(allSizes) {
}); });
}); });
// finally, re-enable the version add button
enableVersionAddButton(); enableVersionAddButton();
} }
function handleCache(operation, versionString, cacheMode, callback) { function handleCache(operation, versionString, cacheMode, callback) {
// see if any versions match (could be undefined or null)
var versions = versionArray.filter(function (obj) { var versions = versionArray.filter(function (obj) {
return obj.name === versionString; return obj.name === versionString;
}); });
// pull version url from the found object, if none found, use the default cdn link
var cdnRoot = (versions.length === 0) ? cdnString : versions[0].url; var cdnRoot = (versions.length === 0) ? cdnString : versions[0].url;
var lastSizes = { intact: 0, altered: 0, total: 0 }; var lastSizes = { intact: 0, altered: 0, total: 0 };
var buf = ""; var buf = "";
// start loading on the given version and mode (could be undefined or null, which means 'all')
storageLoadingStart(versionString, cacheMode); storageLoadingStart(versionString, cacheMode);
// create the server and socket listener
var server = net.createServer(function (sock) { var server = net.createServer(function (sock) {
sock.setEncoding("utf8"); sock.setEncoding("utf8");
sock.on("data", function (data) { sock.on("data", function (data) {
// read data until the next \n, and keep reading
// sometimes the updates are buffered, so there might be multiple objects
// per update, and partial objects as well
// buffer parsing allows us to handle these cases
buf += data; buf += data;
var end = buf.indexOf("\n"); var end = buf.indexOf("\n");
@ -630,6 +651,7 @@ function handleCache(operation, versionString, cacheMode, callback) {
var sub = buf.substring(0, end); var sub = buf.substring(0, end);
buf = buf.substring(end + 1); buf = buf.substring(end + 1);
// run a storage update here
lastSizes = JSON.parse(sub); lastSizes = JSON.parse(sub);
storageLoadingUpdate(lastSizes); storageLoadingUpdate(lastSizes);
@ -638,6 +660,7 @@ function handleCache(operation, versionString, cacheMode, callback) {
}); });
}); });
// start listening on a randomly acquired port, and spawn cache handler when ready
server.listen(0, "localhost", function () { server.listen(0, "localhost", function () {
spawn( spawn(
path.join(__dirname, "lib", "cache_handler.exe"), path.join(__dirname, "lib", "cache_handler.exe"),
@ -651,7 +674,9 @@ function handleCache(operation, versionString, cacheMode, callback) {
"--cdn-root", cdnRoot, "--cdn-root", cdnRoot,
"--cache-mode", cacheMode || "all", "--cache-mode", cacheMode || "all",
"--cache-version", versionString || "all", "--cache-version", versionString || "all",
// learn port from the server object and tell the script where to connect
"--port", server.address().port, "--port", server.address().port,
// tell the script which versions and caches are official
"--official-caches" "--official-caches"
].concat(Object.keys(defaultHashes)), ].concat(Object.keys(defaultHashes)),
{ {
@ -665,10 +690,12 @@ function handleCache(operation, versionString, cacheMode, callback) {
); );
} }
// when the script exits, close the server
server.close(); server.close();
// set button state accordingly
storageLoadingComplete(lastSizes); storageLoadingComplete(lastSizes);
if (callback) // then run the given callback (if any)
callback(lastSizes); if (callback) callback(lastSizes);
}); });
}); });
} }
@ -753,7 +780,7 @@ function prepGameInfo(serverUUID) {
return; return;
} }
// if main.unity3d is present, use the offline cache // otherwise, if main.unity3d is present, use the offline cache
var mainPath = path.join(offlinePath, "main.unity3d"); var mainPath = path.join(offlinePath, "main.unity3d");
var versionURL = remotefs.existsSync(mainPath) ? versionInfo.url : offlineURL; var versionURL = remotefs.existsSync(mainPath) ? versionInfo.url : offlineURL;
setGameInfo(serverInfo, versionURL); setGameInfo(serverInfo, versionURL);
@ -761,6 +788,7 @@ function prepGameInfo(serverUUID) {
// For writing loginInfo.php, assetInfo.php, etc. // For writing loginInfo.php, assetInfo.php, etc.
function setGameInfo(serverInfo, versionURL) { function setGameInfo(serverInfo, versionURL) {
// slash fix if people mess it up via text editors
var versionURLRoot = versionURL.endsWith("/") ? versionURL : versionURL + "/"; var versionURLRoot = versionURL.endsWith("/") ? versionURL : versionURL + "/";
window.assetUrl = versionURLRoot; // game-client.js needs to access this window.assetUrl = versionURLRoot; // game-client.js needs to access this
console.log("Cache will expand from " + versionURLRoot); console.log("Cache will expand from " + versionURLRoot);
@ -838,6 +866,7 @@ function getSelectedServer() {
return $("#server-tablebody > tr.bg-primary").prop("id"); return $("#server-tablebody > tr.bg-primary").prop("id");
} }
// Returns the name of the version with the selected background color.
function getSelectedVersion() { function getSelectedVersion() {
return $("#cache-tablebody > tr.bg-primary").prop("id"); return $("#cache-tablebody > tr.bg-primary").prop("id");
} }
@ -863,16 +892,19 @@ function deselectServer() {
$(".server-listing-entry").removeClass("bg-primary"); $(".server-listing-entry").removeClass("bg-primary");
} }
// If applicable, deselect currently selected version.
function deselectVersion() { function deselectVersion() {
disableVersionListButtons(); disableVersionListButtons();
$(".cache-listing-entry").removeClass("bg-primary"); $(".cache-listing-entry").removeClass("bg-primary");
} }
// Select a server
$("#server-table").on("click", ".server-listing-entry", function (event) { $("#server-table").on("click", ".server-listing-entry", function (event) {
enableServerListButtons(); enableServerListButtons();
$(this).addClass("bg-primary").siblings().removeClass("bg-primary"); $(this).addClass("bg-primary").siblings().removeClass("bg-primary");
}); });
// Select a version (if allowed)
$("#cache-table").on("click", ".cache-listing-entry", function (event) { $("#cache-table").on("click", ".cache-listing-entry", function (event) {
// wait for the add button to be re-enabled first // wait for the add button to be re-enabled first
if ($("#of-addversion-button").prop("disabled")) return; if ($("#of-addversion-button").prop("disabled")) return;
@ -898,6 +930,7 @@ $("#of-addversionmodal").on("show.bs.modal", function (e) {
}); });
$("#of-editservermodal").on("show.bs.modal", function (e) { $("#of-editservermodal").on("show.bs.modal", function (e) {
// populate the edit modal with existing values
var jsonToModify = remotefs.readJsonSync(serversPath); var jsonToModify = remotefs.readJsonSync(serversPath);
$.each(jsonToModify["servers"], function (key, value) { $.each(jsonToModify["servers"], function (key, value) {
@ -919,6 +952,7 @@ $("#of-editservermodal").on("show.bs.modal", function (e) {
}); });
$("#of-editversionmodal").on("show.bs.modal", function (e) { $("#of-editversionmodal").on("show.bs.modal", function (e) {
// populate the edit modal with existing values
var jsonToModify = remotefs.readJsonSync(versionsPath); var jsonToModify = remotefs.readJsonSync(versionsPath);
$.each(jsonToModify["versions"], function (key, value) { $.each(jsonToModify["versions"], function (key, value) {
@ -931,6 +965,7 @@ $("#of-editversionmodal").on("show.bs.modal", function (e) {
validateVersionSave("edit"); validateVersionSave("edit");
}); });
// Replace server name to delete
$("#of-deleteservermodal").on("show.bs.modal", function (e) { $("#of-deleteservermodal").on("show.bs.modal", function (e) {
var result = serverArray.filter(function (obj) { var result = serverArray.filter(function (obj) {
return obj.uuid === getSelectedServer(); return obj.uuid === getSelectedServer();
@ -938,6 +973,7 @@ $("#of-deleteservermodal").on("show.bs.modal", function (e) {
$("#deleteserver-servername").html(result.description); $("#deleteserver-servername").html(result.description);
}); });
// Replace version name to delete
$("#of-deleteversionmodal").on("show.bs.modal", function (e) { $("#of-deleteversionmodal").on("show.bs.modal", function (e) {
var result = versionArray.filter(function (obj) { var result = versionArray.filter(function (obj) {
return obj.name === getSelectedVersion(); return obj.name === getSelectedVersion();
@ -945,11 +981,13 @@ $("#of-deleteversionmodal").on("show.bs.modal", function (e) {
$("#deleteversion-versionname").html(result.name); $("#deleteversion-versionname").html(result.name);
}); });
// Run the global hash check once and only if the cache modal is ever shown
$("#of-editcacheconfigmodal").on("show.bs.modal", function (e) { $("#of-editcacheconfigmodal").on("show.bs.modal", function (e) {
if (!cacheSizes) handleCache("hash-check"); if (!cacheSizes) handleCache("hash-check");
}); });
// Keep all config values synced on modal show
// Avoids cases where people forget that they changed the offline root but did not save
$("#of-editconfigmodal").on("show.bs.modal", function (e) { $("#of-editconfigmodal").on("show.bs.modal", function (e) {
// best to keep this synced on modal show
loadConfig(); loadConfig();
}); });