From b58c3ea292b2b63cc4812d6390641a92214eed1a Mon Sep 17 00:00:00 2001 From: Daniel van Noord Date: Mon, 28 Dec 2020 10:53:58 +0100 Subject: [PATCH] Allow saving outside of autosave-interval (#469) --- CookieMonster.js | 31 +++++++++++++++++-------------- src/Config.js | 26 +++++++++++++++----------- src/Disp.js | 6 +++--- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/CookieMonster.js b/CookieMonster.js index 6ad740f..6b2149e 100644 --- a/CookieMonster.js +++ b/CookieMonster.js @@ -652,15 +652,18 @@ CM.Cache.spawnedGoldenShimmer = 0; * Section: Functions related to saving, loading and restoring configs */ /** - * @deprecated - * This function (used to) save the config of CookieMonster + * This function saves the config of CookieMonster without saving any of the other save-data + * This allows saving in between the autosave intervals * It is called by CM.Config.LoadConfig(), CM.Config.RestoreDefault(), CM.Config.ToggleConfig(), * CM.ToggleConfigVolume() and changes in options with type "url", "color" or "numscale" - * TODO: See if there is a way to force Cookie CLICKER to save only the mod-data and not also Save-data. - * Otherwise this can be removed */ -CM.Config.SaveConfig = function(config) { - CM.save(); +CM.Config.SaveConfig = function() { + let saveString = b64_to_utf8(unescape(localStorage.getItem('CookieClickerGame')).split('!END!')[0]); + CookieMonsterSave = saveString.match(/CookieMonster.*(;|$)/); + if (CookieMonsterSave != null) { + newSaveString = saveString.replace(CookieMonsterSave[0], "CookieMonster:" + CM.save()); + localStorage.setItem('CookieClickerGame', escape(utf8_to_b64(newSaveString)+'!END!')) + } } /** @@ -713,7 +716,7 @@ CM.Config.LoadConfig = function(settings) { } } } - if (mod) CM.Config.SaveConfig(CM.Options); + if (mod) CM.Config.SaveConfig(); CM.Loop(); // Do loop once for (var i in CM.Data.ConfigDefault) { if (i != 'Header' && typeof CM.ConfigData[i].func !== 'undefined') { @@ -732,7 +735,7 @@ CM.Config.LoadConfig = function(settings) { */ CM.Config.RestoreDefault = function() { CM.Config.LoadConfig(CM.Data.ConfigDefault); - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); Game.UpdateMenu(); } @@ -758,7 +761,7 @@ CM.Config.ToggleConfig = function(config) { } l(CM.ConfigPrefix + config).innerHTML = CM.ConfigData[config].label[CM.Options[config]]; - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); } /** @@ -771,7 +774,7 @@ CM.Config.ToggleConfigVolume = function(config) { l("slider" + config + "right").innerHTML = l("slider" + config).value + "%"; CM.Options[config] = Math.round(l("slider" + config).value); } - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); } /** @@ -782,7 +785,7 @@ CM.Config.ToggleConfigVolume = function(config) { CM.Config.ToggleHeader = function(config) { CM.Options.Header[config]++; if (CM.Options.Header[config] > 1) CM.Options.Header[config] = 0; - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); } /******** @@ -3145,7 +3148,7 @@ CM.Disp.CreatePrefOption = function(config) { inputPrompt.setAttribute('value', CM.Options[config]); var a = document.createElement('a'); a.className = 'option'; - a.onclick = function() {Game.Prompt(inputPrompt.outerHTML, [['Save', 'CM.Options[\'' + config + '\'] = l(CM.ConfigPrefix + \'' + config + '\' + \'Prompt\').value; CM.Config.SaveConfig(CM.Options); Game.ClosePrompt(); Game.UpdateMenu();'], 'Cancel']);}; + a.onclick = function() {Game.Prompt(inputPrompt.outerHTML, [['Save', 'CM.Options[\'' + config + '\'] = l(CM.ConfigPrefix + \'' + config + '\' + \'Prompt\').value; CM.Config.SaveConfig(); Game.ClosePrompt(); Game.UpdateMenu();'], 'Cancel']);}; a.textContent = 'Edit'; div.appendChild(a); var label = document.createElement('label'); @@ -3163,7 +3166,7 @@ CM.Disp.CreatePrefOption = function(config) { input.style.width = '65px'; input.setAttribute('value', CM.Options.Colors[CM.Disp.colors[i]]); div.appendChild(input); - eval('var change = function() {CM.Options.Colors[\'' + CM.Disp.colors[i] + '\'] = l(CM.ConfigPrefix + \'Color\' + \'' + CM.Disp.colors[i] + '\').value; CM.Disp.UpdateColors(); CM.Config.SaveConfig(CM.Options);}'); + eval('var change = function() {CM.Options.Colors[\'' + CM.Disp.colors[i] + '\'] = l(CM.ConfigPrefix + \'Color\' + \'' + CM.Disp.colors[i] + '\').value; CM.Disp.UpdateColors(); CM.Config.SaveConfig();}'); var jscolorpicker = new jscolor.color(input, {hash: true, caps: false, pickerZIndex: 1000000, pickerPosition: 'right', onImmediateChange: change}); var label = document.createElement('label'); label.textContent = CM.ConfigData.Colors.desc[CM.Disp.colors[i]]; @@ -3187,7 +3190,7 @@ CM.Disp.CreatePrefOption = function(config) { input.max = CM.ConfigData[config].max; input.oninput = function() {if (this.value > this.max) console.log("TEST"); CM.Options[config] = this.value; - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); CM.Disp.RefreshScale() } div.appendChild(input); diff --git a/src/Config.js b/src/Config.js index dd86c78..e9b8717 100644 --- a/src/Config.js +++ b/src/Config.js @@ -6,15 +6,19 @@ * Section: Functions related to saving, loading and restoring configs */ /** - * @deprecated - * This function (used to) save the config of CookieMonster + * This function saves the config of CookieMonster without saving any of the other save-data + * This allows saving in between the autosave intervals * It is called by CM.Config.LoadConfig(), CM.Config.RestoreDefault(), CM.Config.ToggleConfig(), * CM.ToggleConfigVolume() and changes in options with type "url", "color" or "numscale" - * TODO: See if there is a way to force Cookie CLICKER to save only the mod-data and not also Save-data. - * Otherwise this can be removed */ -CM.Config.SaveConfig = function(config) { - CM.save(); +CM.Config.SaveConfig = function() { + let saveString = b64_to_utf8(unescape(localStorage.getItem('CookieClickerGame')).split('!END!')[0]); + let pattern = /CookieMonster.*[;$]/; + CookieMonsterSave = saveString.match(/CookieMonster.*(;|$)/); + if (CookieMonsterSave != null) { + newSaveString = saveString.replace(CookieMonsterSave[0], "CookieMonster:" + CM.save()); + localStorage.setItem('CookieClickerGame', escape(utf8_to_b64(newSaveString)+'!END!')) + } } /** @@ -67,7 +71,7 @@ CM.Config.LoadConfig = function(settings) { } } } - if (mod) CM.Config.SaveConfig(CM.Options); + if (mod) CM.Config.SaveConfig(); CM.Loop(); // Do loop once for (var i in CM.Data.ConfigDefault) { if (i != 'Header' && typeof CM.ConfigData[i].func !== 'undefined') { @@ -86,7 +90,7 @@ CM.Config.LoadConfig = function(settings) { */ CM.Config.RestoreDefault = function() { CM.Config.LoadConfig(CM.Data.ConfigDefault); - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); Game.UpdateMenu(); } @@ -112,7 +116,7 @@ CM.Config.ToggleConfig = function(config) { } l(CM.ConfigPrefix + config).innerHTML = CM.ConfigData[config].label[CM.Options[config]]; - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); } /** @@ -125,7 +129,7 @@ CM.Config.ToggleConfigVolume = function(config) { l("slider" + config + "right").innerHTML = l("slider" + config).value + "%"; CM.Options[config] = Math.round(l("slider" + config).value); } - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); } /** @@ -136,7 +140,7 @@ CM.Config.ToggleConfigVolume = function(config) { CM.Config.ToggleHeader = function(config) { CM.Options.Header[config]++; if (CM.Options.Header[config] > 1) CM.Options.Header[config] = 0; - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); } /******** diff --git a/src/Disp.js b/src/Disp.js index bb4bac8..f61e8a6 100644 --- a/src/Disp.js +++ b/src/Disp.js @@ -2029,7 +2029,7 @@ CM.Disp.CreatePrefOption = function(config) { inputPrompt.setAttribute('value', CM.Options[config]); var a = document.createElement('a'); a.className = 'option'; - a.onclick = function() {Game.Prompt(inputPrompt.outerHTML, [['Save', 'CM.Options[\'' + config + '\'] = l(CM.ConfigPrefix + \'' + config + '\' + \'Prompt\').value; CM.Config.SaveConfig(CM.Options); Game.ClosePrompt(); Game.UpdateMenu();'], 'Cancel']);}; + a.onclick = function() {Game.Prompt(inputPrompt.outerHTML, [['Save', 'CM.Options[\'' + config + '\'] = l(CM.ConfigPrefix + \'' + config + '\' + \'Prompt\').value; CM.Config.SaveConfig(); Game.ClosePrompt(); Game.UpdateMenu();'], 'Cancel']);}; a.textContent = 'Edit'; div.appendChild(a); var label = document.createElement('label'); @@ -2047,7 +2047,7 @@ CM.Disp.CreatePrefOption = function(config) { input.style.width = '65px'; input.setAttribute('value', CM.Options.Colors[CM.Disp.colors[i]]); div.appendChild(input); - eval('var change = function() {CM.Options.Colors[\'' + CM.Disp.colors[i] + '\'] = l(CM.ConfigPrefix + \'Color\' + \'' + CM.Disp.colors[i] + '\').value; CM.Disp.UpdateColors(); CM.Config.SaveConfig(CM.Options);}'); + eval('var change = function() {CM.Options.Colors[\'' + CM.Disp.colors[i] + '\'] = l(CM.ConfigPrefix + \'Color\' + \'' + CM.Disp.colors[i] + '\').value; CM.Disp.UpdateColors(); CM.Config.SaveConfig();}'); var jscolorpicker = new jscolor.color(input, {hash: true, caps: false, pickerZIndex: 1000000, pickerPosition: 'right', onImmediateChange: change}); var label = document.createElement('label'); label.textContent = CM.ConfigData.Colors.desc[CM.Disp.colors[i]]; @@ -2071,7 +2071,7 @@ CM.Disp.CreatePrefOption = function(config) { input.max = CM.ConfigData[config].max; input.oninput = function() {if (this.value > this.max) console.log("TEST"); CM.Options[config] = this.value; - CM.Config.SaveConfig(CM.Options); + CM.Config.SaveConfig(); CM.Disp.RefreshScale() } div.appendChild(input);