From 203dc8137e78fc887cda5fc3d576e17c4233001c Mon Sep 17 00:00:00 2001 From: Daniel van Noord Date: Mon, 21 Dec 2020 22:53:23 +0100 Subject: [PATCH 1/2] Incorporated Modding API --- CookieMonster.js | 92 +++++++++++++++++++++++++++++------------------- src/Cache.js | 7 ---- src/Config.js | 19 +++++----- src/Disp.js | 11 ------ src/Footer.js | 80 ++++++++++++++++++++++++++++++++++++++++- src/Header.js | 2 ++ src/Main.js | 27 -------------- src/Sim.js | 1 + 8 files changed, 147 insertions(+), 92 deletions(-) diff --git a/CookieMonster.js b/CookieMonster.js index b805f7a..b139ddc 100644 --- a/CookieMonster.js +++ b/CookieMonster.js @@ -190,7 +190,7 @@ CM.Cache.CacheMissingUpgrades = function() { CM.Cache.AddQueue = function() { CM.Cache.Queue = document.createElement('script'); CM.Cache.Queue.type = 'text/javascript'; - CM.Cache.Queue.setAttribute('src', 'https://aktanusa.github.io/CookieMonster/queue/queue.js'); + CM.Cache.Queue.src = 'https://aktanusa.github.io/CookieMonster/queue/queue.js'; document.head.appendChild(CM.Cache.Queue); } @@ -682,16 +682,16 @@ CM.Cache.spawnedGoldenShimmer = 0; * @param {object} config The Config to be saved (normally CM.Options) */ CM.Config.SaveConfig = function(config) { - localStorage.setItem(CM.ConfigPrefix, JSON.stringify(config)); + CM.save(); } /** * This function loads the config of CookieMonster saved in localStorage and loads it into CM.Options * It is called by CM.DelayInit() and CM.Config.RestoreDefault() */ -CM.Config.LoadConfig = function() { - if (localStorage.getItem(CM.ConfigPrefix) != null) { - CM.Options = JSON.parse(localStorage.getItem(CM.ConfigPrefix)); +CM.Config.LoadConfig = function(settings) { + if (settings != null) { + CM.Options = settings; // Check values var mod = false; @@ -749,9 +749,8 @@ CM.Config.LoadConfig = function() { * It is called by resDefBut.onclick loaded in the options page or by CM.Config.LoadConfig if no localStorage is found */ CM.Config.RestoreDefault = function() { - CM.Options = {}; - CM.Config.SaveConfig(CM.Data.ConfigDefault); - CM.Config.LoadConfig(); + CM.Config.LoadConfig(CM.Data.ConfigDefault); + CM.Config.SaveConfig(); Game.UpdateMenu(); } @@ -1386,7 +1385,7 @@ CM.Disp.CreateCssArea = function() { /** * TODO: What does this do? @Aktanusa - * It is called by CM.Init() + * It is called by CM.init, which is called by Game.registerMod */ CM.Disp.AddJscolor = function() { CM.Disp.Jscolor = document.createElement('script'); @@ -4090,24 +4089,6 @@ CM.Loop = function() { CM.Cache.UpdateAvgCPS() } -CM.Init = function() { - var proceed = true; - if (Game.version != CM.VersionMajor) { - proceed = confirm('Cookie Monster version ' + CM.VersionMajor + '.' + CM.VersionMinor + ' is meant for Game version ' + CM.VersionMajor + '. Loading a different version may cause errors. Do you still want to load Cookie Monster?'); - } - if (proceed) { - CM.Cache.AddQueue(); - CM.Disp.AddJscolor(); - - var delay = setInterval(function() { - if (typeof Queue !== 'undefined' && typeof jscolor !== 'undefined') { - CM.DelayInit(); - clearInterval(delay); - } - }, 500); - } -} - CM.DelayInit = function() { CM.Sim.InitData(); CM.Cache.InitCache(); @@ -4144,18 +4125,9 @@ CM.DelayInit = function() { l("upgrades").style.display = "flex"; l("upgrades").style["flex-wrap"] = "wrap"; - CM.Main.RegisterHooks(); - Game.Win('Third-party'); } -/** - * Hook custom methods into the game - */ -CM.Main.RegisterHooks = function() { - Game.registerHook('draw', CM.Disp.Draw); -} - /******** * Section: Functions related to checking for changes in Minigames/GC's/Ticker * TODO: Possibly move this section */ @@ -4580,6 +4552,7 @@ CM.Sim.InitData = function() { for (var i in Game.Achievements) { CM.Sim.Achievements[i] = CM.Sim.InitAchievement(i); } + CM.Sim.CopyData } CM.Sim.CopyData = function() { @@ -5275,8 +5248,53 @@ CM.Sim.mouseCps = function() { * Footer * **********/ +/******** + * Section: Functions related to base game modding API */ + +/** + * TODO: This should at one point be used to register hooks + */ +CM.init = function() { + var proceed = true; + if (Game.version != CM.VersionMajor) { + proceed = confirm('Cookie Monster version ' + CM.VersionMajor + '.' + CM.VersionMinor + ' is meant for Game version ' + CM.VersionMajor + '. Loading a different version may cause errors. Do you still want to load Cookie Monster?'); + } + if (proceed) { + CM.DelayInit(); + Game.registerHook('draw', CM.Disp.Draw); + } +} + +/** + * This registers a save function. Per Game code/comments: + * "use this to store persistent data associated with your mod + * return 'a string to be saved';" + */ +CM.save = function() { + return JSON.stringify({ + settings: CM.Options, + version: CM.VersionMajor + '.' + CM.VersionMinor, + }); +} + +/** + * This registers a load function. Per Game code/comments: + * "do stuff with the string data you saved previously" + */ +CM.load = function(str) { + let save = JSON.parse(str); + CM.Config.LoadConfig(save.settings); +} + if (!CM.isRunning) { - CM.Init(); + CM.Cache.AddQueue(); + CM.Disp.AddJscolor(); + var delay = setInterval(function() { + if (typeof Queue !== 'undefined' && typeof jscolor !== 'undefined') { + Game.registerMod('CookieMonster', CM); + clearInterval(delay); + } + }, 500); CM.isRunning = 1 } diff --git a/src/Cache.js b/src/Cache.js index f60ecce..97600f3 100644 --- a/src/Cache.js +++ b/src/Cache.js @@ -158,13 +158,6 @@ CM.Cache.CacheMissingUpgrades = function() { /******** * Section: UNSORTED */ -CM.Cache.AddQueue = function() { - CM.Cache.Queue = document.createElement('script'); - CM.Cache.Queue.type = 'text/javascript'; - CM.Cache.Queue.setAttribute('src', 'https://aktanusa.github.io/CookieMonster/queue/queue.js'); - document.head.appendChild(CM.Cache.Queue); -} - CM.Cache.NextNumber = function(base) { var count = base > Math.pow(2, 53) ? Math.pow(2, Math.floor(Math.log(base) / Math.log(2)) - 53) : 1; while (base == base + count) { diff --git a/src/Config.js b/src/Config.js index 6b750ed..066b88d 100644 --- a/src/Config.js +++ b/src/Config.js @@ -6,22 +6,24 @@ * Section: Functions related to saving, loading and restoring configs */ /** - * This function saves the config of CookieMonster to localStorage + * @deprecated + * This function (used to) save the config of CookieMonster * 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" - * @param {object} config The Config to be saved (normally CM.Options) + * 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) { - localStorage.setItem(CM.ConfigPrefix, JSON.stringify(config)); + CM.save(); } /** * This function loads the config of CookieMonster saved in localStorage and loads it into CM.Options * It is called by CM.DelayInit() and CM.Config.RestoreDefault() */ -CM.Config.LoadConfig = function() { - if (localStorage.getItem(CM.ConfigPrefix) != null) { - CM.Options = JSON.parse(localStorage.getItem(CM.ConfigPrefix)); +CM.Config.LoadConfig = function(settings) { + if (settings != null) { + CM.Options = settings; // Check values var mod = false; @@ -79,9 +81,8 @@ CM.Config.LoadConfig = function() { * It is called by resDefBut.onclick loaded in the options page or by CM.Config.LoadConfig if no localStorage is found */ CM.Config.RestoreDefault = function() { - CM.Options = {}; - CM.Config.SaveConfig(CM.Data.ConfigDefault); - CM.Config.LoadConfig(); + CM.Config.LoadConfig(CM.Data.ConfigDefault); + CM.Config.SaveConfig(CM.Options); Game.UpdateMenu(); } diff --git a/src/Disp.js b/src/Disp.js index acbf0bb..705700f 100644 --- a/src/Disp.js +++ b/src/Disp.js @@ -264,17 +264,6 @@ CM.Disp.CreateCssArea = function() { document.head.appendChild(CM.Disp.Css); } -/** - * TODO: What does this do? @Aktanusa - * It is called by CM.Init() - */ -CM.Disp.AddJscolor = function() { - CM.Disp.Jscolor = document.createElement('script'); - CM.Disp.Jscolor.type = 'text/javascript'; - CM.Disp.Jscolor.setAttribute('src', 'https://aktanusa.github.io/CookieMonster/jscolor/jscolor.js'); - document.head.appendChild(CM.Disp.Jscolor); -} - /** * This function sets the size of the background of the full game and the left column * depending on whether certain abrs are activated diff --git a/src/Footer.js b/src/Footer.js index c40dd31..45eb3a8 100644 --- a/src/Footer.js +++ b/src/Footer.js @@ -2,8 +2,86 @@ * Footer * **********/ +/******** + * Section: Functions related to base game modding API */ + +/** + * This register a init function to the CM object. Per Game code/comments: + * "this function is called as soon as the mod is registered + * declare hooks here" + * It starts the further initialization of CookieMonster and registers hooks + */ +CM.init = function() { + var proceed = true; + if (Game.version != CM.VersionMajor) { + proceed = confirm('Cookie Monster version ' + CM.VersionMajor + '.' + CM.VersionMinor + ' is meant for Game version ' + CM.VersionMajor + '. Loading a different version may cause errors. Do you still want to load Cookie Monster?'); + } + if (proceed) { + CM.DelayInit(); + Game.registerHook('draw', CM.Disp.Draw); + } +} + +/** + * This registers a save function to the CM object. Per Game code/comments: + * "use this to store persistent data associated with your mod + * return 'a string to be saved';" + */ +CM.save = function() { + return JSON.stringify({ + settings: CM.Options, + version: CM.VersionMajor + '.' + CM.VersionMinor, + }); +} + +/** + * This registers a load function to the CM object. Per Game code/comments: + * "do stuff with the string data you saved previously" + */ +CM.load = function(str) { + let save = JSON.parse(str); + CM.Config.LoadConfig(save.settings); +} + +/******** + * Section: Functions related to the initialization of CookieMonster */ + +/** + * This functions loads an external script (on the same repository) that creates a Queue() function + * It is called by the last function in the footer + */ +CM.Footer.AddQueue = function() { + CM.Footer.Queue = document.createElement('script'); + CM.Footer.Queue.type = 'text/javascript'; + CM.Footer.Queue.src = 'https://aktanusa.github.io/CookieMonster/queue/queue.js'; + document.head.appendChild(CM.Footer.Queue); +} + +/** + * This functions loads an external script (on the same repository) that creates the + * functionality needed to dynamiccaly change colours + * It is called by the last function in the footer + */ +CM.Footer.AddJscolor = function() { + CM.Footer.Jscolor = document.createElement('script'); + CM.Footer.Jscolor.type = 'text/javascript'; + CM.Footer.Jscolor.setAttribute('src', 'https://aktanusa.github.io/CookieMonster/jscolor/jscolor.js'); + document.head.appendChild(CM.Footer.Jscolor); +} + +/** + * This functions starts the initizialization and register CookieMonster + * It is called as the last function in this script's execution + */ if (!CM.isRunning) { - CM.Init(); + CM.Footer.AddQueue(); + CM.Footer.AddJscolor(); + var delay = setInterval(function() { + if (typeof Queue !== 'undefined' && typeof jscolor !== 'undefined') { + Game.registerMod('CookieMonster', CM); + clearInterval(delay); + } + }, 500); CM.isRunning = 1 } diff --git a/src/Header.js b/src/Header.js index 11f5e28..aa8446f 100644 --- a/src/Header.js +++ b/src/Header.js @@ -16,6 +16,8 @@ RunCookieMonsterHeader = function() { CM.Disp = {}; + CM.Footer = {}; + CM.Main = {}; CM.Options = {}; diff --git a/src/Main.js b/src/Main.js index 72a9f15..3306791 100644 --- a/src/Main.js +++ b/src/Main.js @@ -211,24 +211,6 @@ CM.Loop = function() { CM.Cache.UpdateAvgCPS() } -CM.Init = function() { - var proceed = true; - if (Game.version != CM.VersionMajor) { - proceed = confirm('Cookie Monster version ' + CM.VersionMajor + '.' + CM.VersionMinor + ' is meant for Game version ' + CM.VersionMajor + '. Loading a different version may cause errors. Do you still want to load Cookie Monster?'); - } - if (proceed) { - CM.Cache.AddQueue(); - CM.Disp.AddJscolor(); - - var delay = setInterval(function() { - if (typeof Queue !== 'undefined' && typeof jscolor !== 'undefined') { - CM.DelayInit(); - clearInterval(delay); - } - }, 500); - } -} - CM.DelayInit = function() { CM.Sim.InitData(); CM.Cache.InitCache(); @@ -265,18 +247,9 @@ CM.DelayInit = function() { l("upgrades").style.display = "flex"; l("upgrades").style["flex-wrap"] = "wrap"; - CM.Main.RegisterHooks(); - Game.Win('Third-party'); } -/** - * Hook custom methods into the game - */ -CM.Main.RegisterHooks = function() { - Game.registerHook('draw', CM.Disp.Draw); -} - /******** * Section: Functions related to checking for changes in Minigames/GC's/Ticker * TODO: Possibly move this section */ diff --git a/src/Sim.js b/src/Sim.js index d4fc227..52ead0c 100644 --- a/src/Sim.js +++ b/src/Sim.js @@ -207,6 +207,7 @@ CM.Sim.InitData = function() { for (var i in Game.Achievements) { CM.Sim.Achievements[i] = CM.Sim.InitAchievement(i); } + CM.Sim.CopyData } CM.Sim.CopyData = function() { From 72aef84fcc5a0474976673f1bc142c59c6014aa9 Mon Sep 17 00:00:00 2001 From: Daniel van Noord Date: Mon, 21 Dec 2020 22:54:14 +0100 Subject: [PATCH 2/2] Combined CookieMonster (#364) --- CookieMonster.js | 71 ++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/CookieMonster.js b/CookieMonster.js index b139ddc..289ee6a 100644 --- a/CookieMonster.js +++ b/CookieMonster.js @@ -16,6 +16,8 @@ RunCookieMonsterHeader = function() { CM.Disp = {}; + CM.Footer = {}; + CM.Main = {}; CM.Options = {}; @@ -187,13 +189,6 @@ CM.Cache.CacheMissingUpgrades = function() { /******** * Section: UNSORTED */ -CM.Cache.AddQueue = function() { - CM.Cache.Queue = document.createElement('script'); - CM.Cache.Queue.type = 'text/javascript'; - CM.Cache.Queue.src = 'https://aktanusa.github.io/CookieMonster/queue/queue.js'; - document.head.appendChild(CM.Cache.Queue); -} - CM.Cache.NextNumber = function(base) { var count = base > Math.pow(2, 53) ? Math.pow(2, Math.floor(Math.log(base) / Math.log(2)) - 53) : 1; while (base == base + count) { @@ -676,10 +671,12 @@ CM.Cache.spawnedGoldenShimmer = 0; * Section: Functions related to saving, loading and restoring configs */ /** - * This function saves the config of CookieMonster to localStorage + * @deprecated + * This function (used to) save the config of CookieMonster * 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" - * @param {object} config The Config to be saved (normally CM.Options) + * 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(); @@ -750,7 +747,7 @@ CM.Config.LoadConfig = function(settings) { */ CM.Config.RestoreDefault = function() { CM.Config.LoadConfig(CM.Data.ConfigDefault); - CM.Config.SaveConfig(); + CM.Config.SaveConfig(CM.Options); Game.UpdateMenu(); } @@ -1383,17 +1380,6 @@ CM.Disp.CreateCssArea = function() { document.head.appendChild(CM.Disp.Css); } -/** - * TODO: What does this do? @Aktanusa - * It is called by CM.init, which is called by Game.registerMod - */ -CM.Disp.AddJscolor = function() { - CM.Disp.Jscolor = document.createElement('script'); - CM.Disp.Jscolor.type = 'text/javascript'; - CM.Disp.Jscolor.setAttribute('src', 'https://aktanusa.github.io/CookieMonster/jscolor/jscolor.js'); - document.head.appendChild(CM.Disp.Jscolor); -} - /** * This function sets the size of the background of the full game and the left column * depending on whether certain abrs are activated @@ -5252,7 +5238,10 @@ CM.Sim.mouseCps = function() { * Section: Functions related to base game modding API */ /** - * TODO: This should at one point be used to register hooks + * This register a init function to the CM object. Per Game code/comments: + * "this function is called as soon as the mod is registered + * declare hooks here" + * It starts the further initialization of CookieMonster and registers hooks */ CM.init = function() { var proceed = true; @@ -5266,7 +5255,7 @@ CM.init = function() { } /** - * This registers a save function. Per Game code/comments: + * This registers a save function to the CM object. Per Game code/comments: * "use this to store persistent data associated with your mod * return 'a string to be saved';" */ @@ -5278,7 +5267,7 @@ CM.save = function() { } /** - * This registers a load function. Per Game code/comments: + * This registers a load function to the CM object. Per Game code/comments: * "do stuff with the string data you saved previously" */ CM.load = function(str) { @@ -5286,9 +5275,39 @@ CM.load = function(str) { CM.Config.LoadConfig(save.settings); } +/******** + * Section: Functions related to the initialization of CookieMonster */ + +/** + * This functions loads an external script (on the same repository) that creates a Queue() function + * It is called by the last function in the footer + */ +CM.Footer.AddQueue = function() { + CM.Footer.Queue = document.createElement('script'); + CM.Footer.Queue.type = 'text/javascript'; + CM.Footer.Queue.src = 'https://aktanusa.github.io/CookieMonster/queue/queue.js'; + document.head.appendChild(CM.Footer.Queue); +} + +/** + * This functions loads an external script (on the same repository) that creates the + * functionality needed to dynamiccaly change colours + * It is called by the last function in the footer + */ +CM.Footer.AddJscolor = function() { + CM.Footer.Jscolor = document.createElement('script'); + CM.Footer.Jscolor.type = 'text/javascript'; + CM.Footer.Jscolor.setAttribute('src', 'https://aktanusa.github.io/CookieMonster/jscolor/jscolor.js'); + document.head.appendChild(CM.Footer.Jscolor); +} + +/** + * This functions starts the initizialization and register CookieMonster + * It is called as the last function in this script's execution + */ if (!CM.isRunning) { - CM.Cache.AddQueue(); - CM.Disp.AddJscolor(); + CM.Footer.AddQueue(); + CM.Footer.AddJscolor(); var delay = setInterval(function() { if (typeof Queue !== 'undefined' && typeof jscolor !== 'undefined') { Game.registerMod('CookieMonster', CM);