fucking hell
This commit is contained in:
parent
5ad73d9efe
commit
fe729f2622
|
|
@ -1,5 +1,34 @@
|
|||
var modName = "mods/save_loading.js";
|
||||
|
||||
try {
|
||||
//https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
|
||||
function storageAvailable(type) {
|
||||
let storage;
|
||||
try {
|
||||
storage = window[type];
|
||||
const x = "__storage_test__";
|
||||
storage.setItem(x, x);
|
||||
storage.removeItem(x);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return (
|
||||
e instanceof DOMException &&
|
||||
// everything except Firefox
|
||||
(e.code === 22 ||
|
||||
// Firefox
|
||||
e.code === 1014 ||
|
||||
// test name field too, because code might not be present
|
||||
// everything except Firefox
|
||||
e.name === "QuotaExceededError" ||
|
||||
// Firefox
|
||||
e.name === "NS_ERROR_DOM_QUOTA_REACHED") &&
|
||||
// acknowledge QuotaExceededError only if there's something already stored
|
||||
storage &&
|
||||
storage.length !== 0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function zeroToNull(val) {
|
||||
if(val === 0) { return null };
|
||||
return val;
|
||||
|
|
@ -122,6 +151,53 @@ function downloadSave(filename=null) {
|
|||
saveTemplateAsFile(filename, getSimulationState());
|
||||
};
|
||||
|
||||
rebuildCurrentPixels ??= function() {
|
||||
var currPix = []; //rebuild currentPixels from pixelMap to try to fix bug
|
||||
for(pmi = 0; pmi < pixelMap.length; pmi++) {
|
||||
var pixelMapPart = pixelMap[pmi];
|
||||
for(pmj = 0; pmj < pixelMapPart.length; pmj++) {
|
||||
var pixelMapUnit = pixelMapPart[pmj];
|
||||
if(typeof(pixelMapUnit) === "object") {
|
||||
if(pixelMapUnit !== null) {
|
||||
currPix.push(pixelMapUnit);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
currentPixels = currPix;
|
||||
};
|
||||
|
||||
function quicksave(doSuccessAlert=true,doFailAlert=true) {
|
||||
if(storageAvailable("localStorage")) {
|
||||
rebuildCurrentPixels();
|
||||
localStorage.setItem("quicksave",JSON.stringify(getSimulationState()));
|
||||
if(doSuccessAlert) { alert("Quicksave saved") };
|
||||
return true;
|
||||
} else {
|
||||
if(doFailAlert) { alert("Could not save quicksave in localStorage") };
|
||||
throw new Error("Could not save quicksave in localStorage");
|
||||
};
|
||||
};
|
||||
|
||||
function quickload(pause=true,doSuccessAlert=true,doFailAlert=true) {
|
||||
clearAll();
|
||||
rebuildCurrentPixels();
|
||||
var save = localStorage.getItem("quicksave");
|
||||
if(!save) {
|
||||
if(doFailAlert) { alert("No save exists") };
|
||||
return false;
|
||||
} else {
|
||||
importJsonState(JSON.parse(save));
|
||||
if(doSuccessAlert) { alert("Quicksave loaded") };
|
||||
if(pause) {
|
||||
paused = true;
|
||||
document.getElementById("pauseButton").setAttribute("on","true");
|
||||
};
|
||||
return true;
|
||||
};
|
||||
rebuildCurrentPixels();
|
||||
};
|
||||
|
||||
function copySaveJSON(doAlert=true) {
|
||||
copyToClipboard(JSON.stringify(getSimulationState()));
|
||||
if(doAlert) { alert("Save copied as JSON") };
|
||||
|
|
@ -134,7 +210,7 @@ function loadFile() {
|
|||
//load JSON
|
||||
var file = document.getElementById('myfile').files[0];
|
||||
if(file === undefined) {
|
||||
if(document.getElementById("fileFormStatus") !== "null") {
|
||||
if(document.getElementById("fileFormStatus") !== null) {
|
||||
document.getElementById("fileFormStatus").style.color = "red";
|
||||
document.getElementById("fileFormStatus").innerHTML = "No file was uploaded!";
|
||||
};
|
||||
|
|
@ -150,14 +226,14 @@ function loadFile() {
|
|||
try {
|
||||
json = JSON.parse(json);
|
||||
} catch (error) {
|
||||
if(document.getElementById("fileFormStatus") !== "null") {
|
||||
if(document.getElementById("fileFormStatus") !== null) {
|
||||
document.getElementById("fileFormStatus").style.color = "red";
|
||||
document.getElementById("fileFormStatus").innerHTML = "The file wasn't valid JSON!";
|
||||
};
|
||||
throw error;
|
||||
};
|
||||
|
||||
if(document.getElementById("fileFormStatus") !== "null") {
|
||||
if(document.getElementById("fileFormStatus") !== null) {
|
||||
document.getElementById("fileFormStatus").style.color = "yellow";
|
||||
document.getElementById("fileFormStatus").innerHTML = "JSON was parsed successfully";
|
||||
};
|
||||
|
|
@ -174,7 +250,7 @@ function loadText() {
|
|||
//load JSON
|
||||
var json = document.getElementById('mytext').value;
|
||||
if(json === "") {
|
||||
if(document.getElementById("textFormStatus") !== "null") {
|
||||
if(document.getElementById("textFormStatus") !== null) {
|
||||
document.getElementById("textFormStatus").style.color = "red";
|
||||
document.getElementById("textFormStatus").innerHTML = "No text was present!";
|
||||
};
|
||||
|
|
@ -185,14 +261,14 @@ function loadText() {
|
|||
try {
|
||||
json = JSON.parse(json);
|
||||
} catch (error) {
|
||||
if(document.getElementById("textFormStatus") !== "null") {
|
||||
if(document.getElementById("textFormStatus") !== null) {
|
||||
document.getElementById("textFormStatus").style.color = "red";
|
||||
document.getElementById("textFormStatus").innerHTML = "The text wasn't valid JSON!";
|
||||
};
|
||||
throw error;
|
||||
};
|
||||
|
||||
if(document.getElementById("textFormStatus") !== "null") {
|
||||
if(document.getElementById("textFormStatus") !== null) {
|
||||
document.getElementById("textFormStatus").style.color = "yellow";
|
||||
document.getElementById("textFormStatus").innerHTML = "JSON was parsed successfully";
|
||||
};
|
||||
|
|
@ -214,7 +290,7 @@ function importJsonState(json) {
|
|||
};
|
||||
};
|
||||
if(!hasrequiredKeys) {
|
||||
if(document.getElementById("fileFormStatus") !== "null") {
|
||||
if(document.getElementById("fileFormStatus") !== null) {
|
||||
document.getElementById("fileFormStatus").style.color = "red";
|
||||
document.getElementById("fileFormStatus").innerHTML = "JSON is not a valid save!";
|
||||
};
|
||||
|
|
@ -269,7 +345,7 @@ function importJsonState(json) {
|
|||
};
|
||||
currentPixels = currPix;
|
||||
|
||||
if(document.getElementById("fileFormStatus") !== "null") {
|
||||
if(document.getElementById("fileFormStatus") !== null) {
|
||||
document.getElementById("fileFormStatus").style.color = "green";
|
||||
document.getElementById("fileFormStatus").innerHTML = "JSON was loaded successfully.";
|
||||
};
|
||||
|
|
@ -278,7 +354,7 @@ function importJsonState(json) {
|
|||
|
||||
function setPixelSize(size=null) {
|
||||
if(size === null) {
|
||||
if(document.getElementById("pixelSize") !== "null") {
|
||||
if(document.getElementById("pixelSize") !== null) {
|
||||
size = document.getElementById("pixelSize").value;
|
||||
} else {
|
||||
throw new Error("No size could be read");
|
||||
|
|
@ -287,14 +363,14 @@ function setPixelSize(size=null) {
|
|||
|
||||
size = parseFloat(size);
|
||||
if(isNaN(size) || size <= 0) { //NaN check
|
||||
if(document.getElementById("pixelSizeStatus") !== "null") {
|
||||
if(document.getElementById("pixelSizeStatus") !== null) {
|
||||
document.getElementById("pixelSizeStatus").style.color = "red";
|
||||
document.getElementById("pixelSizeStatus").innerHTML = "Pixel size is empty or invalid";
|
||||
};
|
||||
throw new Error("NaN or negative size");
|
||||
};
|
||||
|
||||
if(document.getElementById("pixelSizeStatus") !== "null") {
|
||||
if(document.getElementById("pixelSizeStatus") !== null) {
|
||||
document.getElementById("pixelSizeStatus").style.color = "green";
|
||||
document.getElementById("pixelSizeStatus").innerHTML = "Pixel size set successfully";
|
||||
};
|
||||
|
|
@ -319,15 +395,57 @@ Pixel size (rendering only): <input id="pixelSize"> (Use if the save looks cut o
|
|||
<button id="sizeButton" onclick=setPixelSize() style="color: #FF00FF;">Set pixel size</button>
|
||||
</div>`;
|
||||
|
||||
runAfterLoad(function() { //somehow it gets defined before elements on xbox
|
||||
elements ??= {};
|
||||
elements.save_loader = {
|
||||
behavior: behaviors.SELFDELETE,
|
||||
excludeRandom: true,
|
||||
color: "#FFFFFF",
|
||||
desc: saveLoaderDescription,
|
||||
};
|
||||
});
|
||||
|
||||
//Somehow, for some illogical reason, quicksaving causes updateStats to somehow disregard its if-statement and fucking TypeError when you mouse over an empty space; this is an attempt to fix it with overkill-level existence checks.
|
||||
function updateStats() {
|
||||
var statsDiv = document.getElementById("stats");
|
||||
var stats = "<span id='stat-pos' class='stat'>x"+mousePos.x+",y"+mousePos.y+"</span>";
|
||||
stats += "<span id='stat-pixels' class='stat'>Pxls:" + currentPixels.length+"</span>";
|
||||
stats += "<span id='stat-tps' class='stat'>" + tps+"tps</span>";
|
||||
stats += "<span id='stat-ticks' class='stat'>" + pixelTicks+"</span>";
|
||||
if ((typeof pixelMap).length === 9) { return; }
|
||||
if (pixelMap[mousePos.x] !== undefined) {
|
||||
var currentPixel = pixelMap[mousePos.x][mousePos.y];
|
||||
if (typeof(currentPixel) !== "undefined" && currentPixel && currentPixel !== undefined && currentPixel.element) {
|
||||
stats += "<span id='stat-element' class='stat'>Elem:"+(elements[currentPixel?.element]?.name || currentPixel?.element)+"</span>";
|
||||
stats += "<span id='stat-temperature' class='stat'>Temp:"+formatTemp(currentPixel.temp)+"</span>";
|
||||
if (currentPixel.charge) {
|
||||
stats += "<span id='stat-charge' class='stat'>C"+currentPixel.charge+"</span>";
|
||||
}
|
||||
if (currentPixel.burning) {
|
||||
stats += "<span id='stat-burning' class='stat'>Burning</span>";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (shiftDown) {
|
||||
stats += "<span id='stat-shift' class='stat'>"+shiftDownTypes[shiftDown]+"</span>";
|
||||
}
|
||||
// If the view is not null, show the view in all caps
|
||||
if (view !== null) {
|
||||
stats += "<span id='stat-view' class='stat'>"+viewKey[view]+"</span>";
|
||||
}
|
||||
statsDiv.innerHTML = stats;
|
||||
}
|
||||
|
||||
//Element-based quicksave toggle has proven impossible due to numerous irreparable illogic incidents, so have some buttons instead
|
||||
var qsb = document.createElement("button");
|
||||
qsb.setAttribute("id","quicksaveButton");
|
||||
qsb.setAttribute("onclick","quicksave()");
|
||||
qsb.innerText = "Quicksave";
|
||||
document.getElementById("gameDiv").before(qsb);
|
||||
qsb.after(document.createTextNode(String.fromCharCode(160)));
|
||||
var qlb = document.createElement("button");
|
||||
qlb.setAttribute("id","quickloadButton");
|
||||
qlb.setAttribute("onclick","clearAll(); quickload()");
|
||||
qlb.innerText = "Quickload";
|
||||
document.getElementById("gameDiv").before(qlb);
|
||||
document.getElementById("gameDiv").before(document.createElement("br"));
|
||||
} catch (error) {
|
||||
alert(`save_loading error: ${error.message}`);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue