CookieMonsterTeam / CookieMonster

Addon for Cookie Clicker that offers a wide range of tools and statistics to enhance the game
MIT License
501 stars 206 forks source link

Feature Request: New modding API save/load #463

Closed hyoretsu closed 3 years ago

hyoretsu commented 3 years ago

Do consider switching from saving Cookie Monster's settings to local storage to saving it alongside Cookie Clicker's save using the new modding API. It is pretty "modular", so you can just use it to save and load your data to use in functions elsewhere. Doing it that way also makes sure that wherever the CC save goes, Cookie Monster's configs will follow. (It can even be uploaded automatically to the cloud alongside the normal save with extensions)

DanielNoord commented 3 years ago

This has been on my radar for some time. We need to integrate CookieMonster with the new modding api anyway. But you are are right, we should add this to the next version as it is a very easy and quick change. The full integration will probably have to wait for a release after that.

DanielNoord commented 3 years ago

This proved to be much more difficult than I initially thought. The whole mode broke down, but the dev-branch now includes the modding API. Would you be so kind to test it?

Furthermore, do you know if there is a way to save Mod-data without saving save-game data? I have looked in the Game code but couldn't find a solution. I quite liked how we currently save the Mod-data upon every change of a setting so that you don't need to wait for an autosave and your settings for CookieMonster will always be kept on refresh/closing the window. We could still do this by saving all save-data, but then we would also save things likes cookies and upgrades upon changes of CookieMonster settings and this is not something that seems intended. Do you know if any of the other modders have found a solution for his?

hyoretsu commented 3 years ago

Everything seems to persist fine through reloads, as intended.

Do you know if any of the other modders have found a solution for his?

Only OmniscientCookies', Cppkies' and Spiced Cookes' devs are active on #dashnet-modding of CC's Discord, but I don't think any of them ever faced this issue.

Furthermore, do you know if there is a way to save Mod-data without saving save-game data?

Orteil saves through Game.WriteSave(), which saves everything and adds mod's saves at the end. There shouldn't normally be a way to only save mod-data, but I spent some time to try and reverse-engineer CC saving process. So basically, if you want to, say, update UpgradeBarFixedPos from "UpgradeBarFixedPos":1 to "UpgradeBarFixedPos":0

oldValue = utf8_to_b64(Game.safeSaveString('UpgradeBarFixedPos":1'))
newValue = utf8_to_b64(Game.safeSaveString('UpgradeBarFixedPos":0'))
CCSave = localStorage.getItem('CookieClickerGame')
newSave = CCSave.replace(oldValue, newValue)
localStorage.setItem('CookieClickerGame', newSave)

Doing it this way keeps the rest of the save the exact way it was when it's last saved but updates the Cookie Monster setting you just changed. You could also just add

let canSave = true;
Game.ObjectsById.forEach(building => {
 if (building.minigameLoading) {
  canSave = false;
  break;
 }
});
if (canSave) {
 Game.WriteSave();
}

when changing values on Settings to force CC to save early, but I think what you're wanting to avoid is saving other things something else while also calling save() early.

Keep in mind that your mod's save data gets sorta minified, so

{
 exampleKey: 12345,
 asd: 1
}

turns to {"exampleKey":12345,"asd":1} Do also keep in mind that you cannot include the initial double quote or the last comma, as it bugs out when running it through utf8_to_b64(). That being, each character you add or remove changes first/last part of the string in B64 I think.

It might fail in the off-event that you have a key that's the exact same as another mod's, but I find that highly unlikely for Cookie Monster. If you wanna be 100% sure just add CM. to the beginning of every key, so UpgradeBarFixedPos = CM.UpgradeBarFixedPos

DanielNoord commented 3 years ago

Thanks for your quick reply!

This indeed seems the best way to solve this. I could also just overwrite all of CookieMonster's data to avoid any problems with double keys, but I might need to check how much of an effect this gives performance-wise. It is too bad Orteil has not provided a solution for this in the base game as I believe it would be very useful.