Merge pull request #465 from DanielNoord/saveapi (#463)

Incorporated Modding API
This commit is contained in:
DanielNoord
2020-12-21 22:55:39 +01:00
committed by GitHub
8 changed files with 184 additions and 110 deletions

View File

@@ -16,6 +16,8 @@ RunCookieMonsterHeader = function() {
CM.Disp = {}; CM.Disp = {};
CM.Footer = {};
CM.Main = {}; CM.Main = {};
CM.Options = {}; CM.Options = {};
@@ -187,13 +189,6 @@ CM.Cache.CacheMissingUpgrades = function() {
/******** /********
* Section: UNSORTED */ * 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) { 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; var count = base > Math.pow(2, 53) ? Math.pow(2, Math.floor(Math.log(base) / Math.log(2)) - 53) : 1;
while (base == base + count) { while (base == base + count) {
@@ -676,22 +671,24 @@ CM.Cache.spawnedGoldenShimmer = 0;
* Section: Functions related to saving, loading and restoring configs */ * 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(), * 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" * 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.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 * 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() * It is called by CM.DelayInit() and CM.Config.RestoreDefault()
*/ */
CM.Config.LoadConfig = function() { CM.Config.LoadConfig = function(settings) {
if (localStorage.getItem(CM.ConfigPrefix) != null) { if (settings != null) {
CM.Options = JSON.parse(localStorage.getItem(CM.ConfigPrefix)); CM.Options = settings;
// Check values // Check values
var mod = false; var mod = false;
@@ -749,9 +746,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 * 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.Config.RestoreDefault = function() {
CM.Options = {}; CM.Config.LoadConfig(CM.Data.ConfigDefault);
CM.Config.SaveConfig(CM.Data.ConfigDefault); CM.Config.SaveConfig(CM.Options);
CM.Config.LoadConfig();
Game.UpdateMenu(); Game.UpdateMenu();
} }
@@ -1384,17 +1380,6 @@ CM.Disp.CreateCssArea = function() {
document.head.appendChild(CM.Disp.Css); 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 * This function sets the size of the background of the full game and the left column
* depending on whether certain abrs are activated * depending on whether certain abrs are activated
@@ -4090,24 +4075,6 @@ CM.Loop = function() {
CM.Cache.UpdateAvgCPS() 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.DelayInit = function() {
CM.Sim.InitData(); CM.Sim.InitData();
CM.Cache.InitCache(); CM.Cache.InitCache();
@@ -4144,18 +4111,9 @@ CM.DelayInit = function() {
l("upgrades").style.display = "flex"; l("upgrades").style.display = "flex";
l("upgrades").style["flex-wrap"] = "wrap"; l("upgrades").style["flex-wrap"] = "wrap";
CM.Main.RegisterHooks();
Game.Win('Third-party'); 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 * Section: Functions related to checking for changes in Minigames/GC's/Ticker
* TODO: Possibly move this section */ * TODO: Possibly move this section */
@@ -4580,6 +4538,7 @@ CM.Sim.InitData = function() {
for (var i in Game.Achievements) { for (var i in Game.Achievements) {
CM.Sim.Achievements[i] = CM.Sim.InitAchievement(i); CM.Sim.Achievements[i] = CM.Sim.InitAchievement(i);
} }
CM.Sim.CopyData
} }
CM.Sim.CopyData = function() { CM.Sim.CopyData = function() {
@@ -5275,8 +5234,86 @@ CM.Sim.mouseCps = function() {
* Footer * * 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) { 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 CM.isRunning = 1
} }

View File

@@ -158,13 +158,6 @@ CM.Cache.CacheMissingUpgrades = function() {
/******** /********
* Section: UNSORTED */ * 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) { 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; var count = base > Math.pow(2, 53) ? Math.pow(2, Math.floor(Math.log(base) / Math.log(2)) - 53) : 1;
while (base == base + count) { while (base == base + count) {

View File

@@ -6,22 +6,24 @@
* Section: Functions related to saving, loading and restoring configs */ * 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(), * 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" * 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.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 * 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() * It is called by CM.DelayInit() and CM.Config.RestoreDefault()
*/ */
CM.Config.LoadConfig = function() { CM.Config.LoadConfig = function(settings) {
if (localStorage.getItem(CM.ConfigPrefix) != null) { if (settings != null) {
CM.Options = JSON.parse(localStorage.getItem(CM.ConfigPrefix)); CM.Options = settings;
// Check values // Check values
var mod = false; 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 * 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.Config.RestoreDefault = function() {
CM.Options = {}; CM.Config.LoadConfig(CM.Data.ConfigDefault);
CM.Config.SaveConfig(CM.Data.ConfigDefault); CM.Config.SaveConfig(CM.Options);
CM.Config.LoadConfig();
Game.UpdateMenu(); Game.UpdateMenu();
} }

View File

@@ -264,17 +264,6 @@ CM.Disp.CreateCssArea = function() {
document.head.appendChild(CM.Disp.Css); 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 * This function sets the size of the background of the full game and the left column
* depending on whether certain abrs are activated * depending on whether certain abrs are activated

View File

@@ -2,8 +2,86 @@
* Footer * * 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) { 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 CM.isRunning = 1
} }

View File

@@ -16,6 +16,8 @@ RunCookieMonsterHeader = function() {
CM.Disp = {}; CM.Disp = {};
CM.Footer = {};
CM.Main = {}; CM.Main = {};
CM.Options = {}; CM.Options = {};

View File

@@ -211,24 +211,6 @@ CM.Loop = function() {
CM.Cache.UpdateAvgCPS() 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.DelayInit = function() {
CM.Sim.InitData(); CM.Sim.InitData();
CM.Cache.InitCache(); CM.Cache.InitCache();
@@ -265,18 +247,9 @@ CM.DelayInit = function() {
l("upgrades").style.display = "flex"; l("upgrades").style.display = "flex";
l("upgrades").style["flex-wrap"] = "wrap"; l("upgrades").style["flex-wrap"] = "wrap";
CM.Main.RegisterHooks();
Game.Win('Third-party'); 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 * Section: Functions related to checking for changes in Minigames/GC's/Ticker
* TODO: Possibly move this section */ * TODO: Possibly move this section */

View File

@@ -207,6 +207,7 @@ CM.Sim.InitData = function() {
for (var i in Game.Achievements) { for (var i in Game.Achievements) {
CM.Sim.Achievements[i] = CM.Sim.InitAchievement(i); CM.Sim.Achievements[i] = CM.Sim.InitAchievement(i);
} }
CM.Sim.CopyData
} }
CM.Sim.CopyData = function() { CM.Sim.CopyData = function() {