From 78207ea85d9df880c4ffbca471e5b1e55b0afb10 Mon Sep 17 00:00:00 2001
From: lowercasedonkey <lowercasedonkey@gmail.com>
Date: Tue, 24 Mar 2020 22:42:47 -0400
Subject: [PATCH] protect game state if bc breaks

---
 .../BackwardsCompatibility.tw                 | 40 +------------------
 .../backwardsCompatibility.js                 | 39 ++++++++++++++++++
 2 files changed, 40 insertions(+), 39 deletions(-)

diff --git a/src/data/backwardsCompatibility/BackwardsCompatibility.tw b/src/data/backwardsCompatibility/BackwardsCompatibility.tw
index a2f875078ff..7c5f0ca9f42 100644
--- a/src/data/backwardsCompatibility/BackwardsCompatibility.tw
+++ b/src/data/backwardsCompatibility/BackwardsCompatibility.tw
@@ -7,42 +7,4 @@
 
 /* resetOnNGPlus contains half of the variables we need, but we use it politely here instead of forcing it so it fills in holes instead of overwriting data */
 <<run App.Update.setNonexistantProperties(V, App.Data.resetOnNGPlus)>>
-
-<div>
-	<<= App.Update.oldVersions()>>
-</div>
-
-<div>
-	<<= App.Update.genePoolRecords()>>
-</div>
-
-<div>
-	<<= App.Update.globalVariables()>>
-</div>
-
-<div>
-	<<= App.Update.slaveVariables()>>
-</div>
-
-<div>
-	<<= App.Update.slaveRecords()>>
-</div>
-
-<div>
-	<<= App.Update.RAassistantData()>>
-</div>
-
-/* Sec Exp */
-<div>
-	<<= App.SecExp.Check.general()>>
-	<<if $secExpEnabled == 1>>
-		<br> <<include "SecExpBackwardCompatibility">>
-	<</if>>
-</div>
-<div>
-	<<= App.Update.autoshred()>>
-</div>
-/* leave this at the bottom of BC */
-<div>
-	<<= App.Update.cleanUp()>>
-</div>
+<<= App.Update.backwardsCompatibility()>>
\ No newline at end of file
diff --git a/src/data/backwardsCompatibility/backwardsCompatibility.js b/src/data/backwardsCompatibility/backwardsCompatibility.js
index af6491d0340..e7b44c351e4 100644
--- a/src/data/backwardsCompatibility/backwardsCompatibility.js
+++ b/src/data/backwardsCompatibility/backwardsCompatibility.js
@@ -51,6 +51,45 @@ App.Update.setExistantProperties = function(obj, array) {
 	}
 };
 
+App.Update.backwardsCompatibility = function() {
+	const backup = State.variables.clone();
+	let r = "";
+	try {
+		r += `<div>`;
+		r += App.Update.oldVersions();
+		r += `</div><div>`;
+		r += App.Update.genePoolRecords();
+		r += `</div><div>`;
+		r += App.Update.globalVariables();
+		r += `</div><div>`;
+		r += App.Update.slaveVariables();
+		r += `</div><div>`;
+		r += App.Update.slaveRecords();
+		r += `</div><div>`;
+		r += App.Update.RAassistantData();
+		r += `</div><div>`;
+
+		/* Sec Exp */
+		r += App.SecExp.Check.general();
+		r += `</div><div>`;
+		if (V.secExpEnabled === 1) {
+			r += `include "SecExpBackwardCompatibility"`;
+		}
+		r += `</div><div>`;
+		r += App.Update.autoshred();
+		/* leave this at the bottom of BC */
+		r += `</div><div>`;
+		r += App.Update.cleanUp();
+		r += `</div>`;
+	} catch (error) {
+		r += `print an error message that says backcompat failed and was reversed, please report it to us`;
+		V = backup;
+		throw (error); // rethrow the exception to Sugarcube so we get a fancy stack trace
+	}
+	console.log(r);
+	return r;
+};
+
 App.Update.globalVariables = function() {
 	let r = `Setting missing global variables... `;
 	if (Array.isArray(V.nationalities)) {
-- 
GitLab