Fixes for hybrid saving

This commit is contained in:
Flashfyre 2024-05-14 23:53:04 -04:00
parent c8d61b134d
commit 9a965055b1
3 changed files with 47 additions and 58 deletions

View File

@ -3696,7 +3696,7 @@ export class PostGameOverPhase extends Phase {
start() { start() {
super.start(); super.start();
this.scene.gameData.saveSystem().then(success => { this.scene.gameData.saveAll(this.scene, true, true, true).then(success => {
if (!success) if (!success)
return this.scene.reset(true); return this.scene.reset(true);
this.scene.gameData.tryClearSession(this.scene, this.scene.sessionSlotId).then((success: boolean | [boolean, boolean]) => { this.scene.gameData.tryClearSession(this.scene, this.scene.sessionSlotId).then((success: boolean | [boolean, boolean]) => {

View File

@ -67,6 +67,18 @@ export function getDataTypeKey(dataType: GameDataType, slotId: integer = 0): str
} }
} }
function encrypt(data: string, bypassLogin: boolean): string {
return (bypassLogin
? (data: string) => btoa(data)
: (data: string) => AES.encrypt(data, saveKey))(data);
}
function decrypt(data: string, bypassLogin: boolean): string {
return (bypassLogin
? (data: string) => atob(data)
: (data: string) => AES.decrypt(data, saveKey).toString(enc.Utf8))(data);
}
interface SystemSaveData { interface SystemSaveData {
trainerId: integer; trainerId: integer;
secretId: integer; secretId: integer;
@ -277,13 +289,9 @@ export class GameData {
const maxIntAttrValue = Math.pow(2, 31); const maxIntAttrValue = Math.pow(2, 31);
const systemData = JSON.stringify(data, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v); const systemData = JSON.stringify(data, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v);
const encFunc = bypassLogin localStorage.setItem(`data_${loggedInUser.username}`, encrypt(systemData, bypassLogin));
? (data: string) => btoa(data)
: (data: string) => AES.encrypt(data, saveKey);
localStorage.setItem(`data_${loggedInUser.username}`, encFunc(systemData)); if (!bypassLogin) {
if (!bypassLogin && !localStorage.getItem(`data_${loggedInUser.username}`)) {
Utils.apiPost(`savedata/update?datatype=${GameDataType.SYSTEM}`, systemData, undefined, true) Utils.apiPost(`savedata/update?datatype=${GameDataType.SYSTEM}`, systemData, undefined, true)
.then(response => response.text()) .then(response => response.text())
.then(error => { .then(error => {
@ -335,12 +343,8 @@ export class GameData {
const cachedSystem = localStorage.getItem(`data_${loggedInUser.username}`); const cachedSystem = localStorage.getItem(`data_${loggedInUser.username}`);
this.initSystem(response, cachedSystem ? AES.decrypt(cachedSystem, saveKey).toString(enc.Utf8) : null).then(resolve); this.initSystem(response, cachedSystem ? AES.decrypt(cachedSystem, saveKey).toString(enc.Utf8) : null).then(resolve);
}); });
} else { } else
const decFunc = bypassLogin this.initSystem(decrypt(localStorage.getItem(`data_${loggedInUser.username}`), bypassLogin)).then(resolve);
? (data: string) => atob(data)
: (data: string) => AES.decrypt(data, saveKey).toString(enc.Utf8);
this.initSystem(decFunc(localStorage.getItem(`data_${loggedInUser.username}`))).then(resolve);
}
}); });
} }
@ -498,24 +502,23 @@ export class GameData {
return dataStr; return dataStr;
} }
public verify(): Promise<boolean> { public async verify(): Promise<boolean> {
return new Promise<boolean>(resolve => { if (bypassLogin)
if (bypassLogin) return true;
return resolve(true);
Utils.apiFetch(`savedata/system/verify?clientSessionId=${clientSessionId}`, true) const response = await Utils.apiPost(`savedata/system/verify`, JSON.stringify({ clientSessionId: clientSessionId }), undefined, true)
.then(response => response.json()) .then(response => response.json());
.then(response => {
if (!response.valid) { if (!response.valid) {
this.scene.clearPhaseQueue(); this.scene.clearPhaseQueue();
this.scene.unshiftPhase(new ReloadSessionPhase(this.scene, JSON.stringify(response.systemData))); this.scene.unshiftPhase(new ReloadSessionPhase(this.scene, JSON.stringify(response.systemData)));
localStorage.removeItem(`data_${loggedInUser.username}`); localStorage.removeItem(`data_${loggedInUser.username}`);
for (let s = 0; s < 5; s++) for (let s = 0; s < 5; s++)
localStorage.removeItem(`sessionData${s ? s : ''}_${loggedInUser.username}`); localStorage.removeItem(`sessionData${s ? s : ''}_${loggedInUser.username}`);
return resolve(false); return false;
} }
resolve(true);
}); return true;
});
} }
public saveSetting(setting: Setting, valueIndex: integer): boolean { public saveSetting(setting: Setting, valueIndex: integer): boolean {
@ -624,16 +627,15 @@ export class GameData {
return resolve(null); return resolve(null);
} }
localStorage.setItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`, encrypt(response, bypassLogin));
await handleSessionData(response); await handleSessionData(response);
}); });
} else { } else {
const sessionData = localStorage.getItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`); const sessionData = localStorage.getItem(`sessionData${slotId ? slotId : ''}_${loggedInUser.username}`);
if (sessionData) { if (sessionData)
const decFunc = bypassLogin await handleSessionData(decrypt(sessionData, bypassLogin));
? (data: string) => atob(data) else
: (data: string) => AES.decrypt(data, saveKey).toString(enc.Utf8);
await handleSessionData(decFunc(sessionData));
} else
return resolve(null); return resolve(null);
} }
}); });
@ -871,14 +873,14 @@ export class GameData {
}) as SessionSaveData; }) as SessionSaveData;
} }
saveAll(scene: BattleScene, skipVerification: boolean = false, sync: boolean = false): Promise<boolean> { saveAll(scene: BattleScene, skipVerification: boolean = false, sync: boolean = false, useCachedSession: boolean = false): Promise<boolean> {
return new Promise<boolean>(resolve => { return new Promise<boolean>(resolve => {
Utils.executeIf(!skipVerification, updateUserInfo).then(success => { Utils.executeIf(!skipVerification, updateUserInfo).then(success => {
if (success !== null && !success) if (success !== null && !success)
return resolve(false); return resolve(false);
if (sync) if (sync)
this.scene.ui.savingIcon.show(); this.scene.ui.savingIcon.show();
const sessionData = this.getSessionSaveData(scene); const sessionData = useCachedSession ? this.parseSessionData(decrypt(localStorage.getItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}_${loggedInUser.username}`), bypassLogin)) : this.getSessionSaveData(scene);
const maxIntAttrValue = Math.pow(2, 31); const maxIntAttrValue = Math.pow(2, 31);
const systemData = this.getSystemSaveData(); const systemData = this.getSystemSaveData();
@ -890,13 +892,9 @@ export class GameData {
clientSessionId: clientSessionId clientSessionId: clientSessionId
}; };
const encFunc = bypassLogin localStorage.setItem(`data_${loggedInUser.username}`, encrypt(JSON.stringify(systemData, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v), bypassLogin));
? (data: string) => btoa(data)
: (data: string) => AES.encrypt(data, saveKey);
localStorage.setItem(`data_${loggedInUser.username}`, encFunc(JSON.stringify(systemData, (k: any, v: any) => typeof v === 'bigint' ? v <= maxIntAttrValue ? Number(v) : v.toString() : v))); localStorage.setItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}_${loggedInUser.username}`, encrypt(JSON.stringify(sessionData), bypassLogin));
localStorage.setItem(`sessionData${scene.sessionSlotId ? scene.sessionSlotId : ''}_${loggedInUser.username}`, encFunc(JSON.stringify(sessionData)));
console.debug('Session data saved'); console.debug('Session data saved');
@ -961,12 +959,8 @@ export class GameData {
}); });
} else { } else {
const data = localStorage.getItem(dataKey); const data = localStorage.getItem(dataKey);
if (data) { if (data)
const decFunc = bypassLogin handleData(decrypt(data, bypassLogin));
? (data: string) => atob(data)
: (data: string) => AES.decrypt(data, saveKey).toString(enc.Utf8);
handleData(decFunc(data));
}
resolve(!!data); resolve(!!data);
} }
}); });
@ -1034,12 +1028,7 @@ export class GameData {
return this.scene.ui.showText(`Your ${dataName} data could not be loaded. It may be corrupted.`, null, () => this.scene.ui.showText(null, 0), Utils.fixedInt(1500)); return this.scene.ui.showText(`Your ${dataName} data could not be loaded. It may be corrupted.`, null, () => this.scene.ui.showText(null, 0), Utils.fixedInt(1500));
this.scene.ui.showText(`Your ${dataName} data will be overridden and the page will reload. Proceed?`, null, () => { this.scene.ui.showText(`Your ${dataName} data will be overridden and the page will reload. Proceed?`, null, () => {
this.scene.ui.setOverlayMode(Mode.CONFIRM, () => { this.scene.ui.setOverlayMode(Mode.CONFIRM, () => {
const hasLocalData = !!localStorage.getItem(dataKey); localStorage.setItem(dataKey, encrypt(dataStr, bypassLogin));
const encFunc = bypassLogin || dataType === GameDataType.SETTINGS
? (data: string) => btoa(data)
: (data: string) => AES.encrypt(data, saveKey);
localStorage.setItem(dataKey, encFunc(dataStr));
if (!bypassLogin && dataType < GameDataType.SETTINGS) { if (!bypassLogin && dataType < GameDataType.SETTINGS) {
updateUserInfo().then(success => { updateUserInfo().then(success => {

View File

@ -372,7 +372,7 @@ export default class EggGachaUiHandler extends MessageUiHandler {
this.scene.gameData.gameStats.eggsPulled++; this.scene.gameData.gameStats.eggsPulled++;
} }
this.scene.gameData.saveSystem().then(success => { this.scene.gameData.saveAll(this.scene, true, true, true).then(success => {
if (!success) if (!success)
return this.scene.reset(true); return this.scene.reset(true);
doPull(); doPull();