Skip to content

Commit 777631c

Browse files
committed
fix broken saving and broken modded upgrade loading
1 parent de1f46e commit 777631c

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

src/ts/clickercookie.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const defaultUpgradeDescriptions = {
2323
let cookieProductionStopped = false;
2424

2525
interface ClickerCookieSaveData {
26-
version: string;
26+
version: number;
2727

2828
// core
2929
cookies: number;
@@ -639,7 +639,7 @@ export default class ClickerCookie extends Mod {
639639
// ------------ All of ClickerCookie's SaveProvider stuff ------------
640640
getSaveData(): ClickerCookieSaveData {
641641
return {
642-
version: Game.VERSION,
642+
version: 0, // todo: this will be 1 when full release
643643
cookies: this.cookies,
644644
totalCookies: this.totalCookies,
645645
cookiesPerClick: this.cookiesPerClick,

src/ts/handlers.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Building } from "./buildings.js";
44
import { Handler, Identifier, UniqueKeyHandler } from "./handler.js";
55
import { url } from "./helper.js";
6-
import { Game } from "./main.js";
6+
import { Game, GameSaveData } from "./main.js";
77
import { Mod } from "./mods.js";
88
import { Background, CurrentlyClickedObject } from "./personalization.js";
99
import { AdvancedPopup, SimplePopup } from "./popup.js";
@@ -209,6 +209,8 @@ export class UpgradeHandler extends Handler<Upgrade> {
209209

210210
if (namespace !== undefined && id.namespace !== namespace) continue;
211211

212+
if (this.getFromIdentifier(id) === undefined) continue; // if upgrades are not registered we obviously can't load them. todo: should this warn?
213+
212214
this.getFromIdentifier(id).bought = saveObj[stringifiedIdentifier].bought;
213215
this.getFromIdentifier(id).unlocked = saveObj[stringifiedIdentifier].unlocked;
214216
}
@@ -236,6 +238,9 @@ export class ModHandler extends UniqueKeyHandler<Mod> {
236238

237239
Game.getInstance().isModded = true;
238240

241+
// load game save data to todo: write
242+
Game.getInstance().loadSaveData(Game.getInstance().savinator5000.getLocalStorageSave().getData("game") as GameSaveData);
243+
239244
console.log("Loaded mod from URL: "+url);
240245
}
241246

@@ -260,6 +265,9 @@ export class ModHandler extends UniqueKeyHandler<Mod> {
260265

261266
Game.getInstance().isModded = true;
262267

268+
// load game save data to todo: write
269+
Game.getInstance().loadSaveData(Game.getInstance().savinator5000.getLocalStorageSave().getData("game") as GameSaveData)
270+
263271
console.log("Successfully added mod from file: "+file.name);
264272
};
265273
}
@@ -350,7 +358,7 @@ export class ModHandler extends UniqueKeyHandler<Mod> {
350358
export class SaveHandler extends UniqueKeyHandler<SaveProvider> {
351359
dumpSaveData(): Record<string, unknown> {
352360
const saveData: Record<string, unknown> = {};
353-
for (const namespace in this.registered) {
361+
for (const namespace of this.registered.keys()) {
354362
saveData[namespace] = this.registered.get(namespace).getSaveData();
355363
}
356364
return saveData;

src/ts/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export enum VersionBranch {
6767

6868
type MiddleState = "none" | "stats" | "info" | "options";
6969

70-
interface GameSaveData {
70+
export interface GameSaveData {
7171
hasCheated: boolean;
7272
isModded: boolean;
7373
autoSavingAllowed: boolean;
@@ -346,6 +346,9 @@ export class Game extends SaveProvider {
346346
}
347347

348348
getSaveData(): GameSaveData {
349+
/** savedata is null on first boot */
350+
const originalUpgradeSave = (this.savinator5000.getLocalStorageSave()) ? (this.savinator5000.getLocalStorageSave().getData("game") as GameSaveData).upgradesSave : {}
351+
349352
return {
350353
hasCheated: this.hasCheated,
351354
isModded: this.isModded,
@@ -355,7 +358,10 @@ export class Game extends SaveProvider {
355358
currentlyClickedObjectKey: Handlers.CURRENTLY_CLICKED.getKeyFromValue(Handlers.CURRENTLY_CLICKED.getCurrentlyClicked()),
356359
currentBackgroundKey: Handlers.BACKGROUND.getKeyFromValue(Handlers.BACKGROUND.getCurrentBackground()), // todo: make better
357360

358-
upgradesSave: Handlers.UPGRADE.dumpSave()
361+
upgradesSave: { // merge to preserve unregistered upgrades
362+
...originalUpgradeSave,
363+
...Handlers.UPGRADE.dumpSave()
364+
}
359365
}
360366
}
361367

src/ts/saving.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ export class Savinator {
8686
load() {
8787
const localStorageSave = this.getLocalStorageSave();
8888
const providers = this.saveHandler.getProviders();
89-
for (const namespace in providers) { //? should this go over providers or the localStorageSave.getNamespaces()? is there any benefit to one or the other?
90-
if (localStorageSave.getNamespaces().includes(namespace) && namespace !== "game") { // if the provider namespace is present in the local storage save
91-
providers.get(namespace).loadSaveData(localStorageSave.getData(namespace));
92-
console.log(`Loaded save data for ${namespace} namespace.`);
89+
for (const provider of this.saveHandler) {
90+
const namespace = this.saveHandler.getKeyFromValue(provider);
91+
if (localStorageSave.getNamespaces().includes(namespace) && namespace !== "game") {
92+
provider.loadSaveData(localStorageSave.getData(namespace));
93+
console.log(`Loaded save data for "${namespace}" namespace.`);
9394
}
9495
}
9596
if (providers.get("game") !== undefined) { // game should always be there, but just in case it isn't we check
@@ -200,11 +201,15 @@ export class Save {
200201
this.data.data[namespace] = value;
201202
}
202203

203-
getData(namespace: string): unknown {
204-
if (this.data.data[namespace] === undefined || this.data.data[namespace] === null)
205-
throw new Error(`Tried to obtain data from a Save with a namespace (${namespace}) that does not exist on the save.`);
206-
207-
return this.data.data[namespace];
204+
getData(namespace: string=undefined): unknown {
205+
if (namespace) {
206+
if (this.data.data[namespace] === undefined || this.data.data[namespace] === null)
207+
throw new Error(`Tried to obtain data from a Save with a namespace (${namespace}) that does not exist on the save.`);
208+
209+
return this.data.data[namespace];
210+
} else {
211+
return this.data.data;
212+
}
208213
}
209214
getHeader() {
210215
return this.data.header;

0 commit comments

Comments
 (0)