Reduce size of system data by shortening keys

pull/14/head
Flashfyre 2023-12-31 13:39:04 -05:00
parent 7a47c62535
commit 34d91edab1
1 changed files with 25 additions and 2 deletions

View File

@ -118,6 +118,14 @@ export interface DexAttrProps {
formIndex: integer;
}
const systemShortKeys = {
seenAttr: '$sa',
caughtAttr: '$ca',
seenCount: '$s' ,
caughtCount: '$c',
ivs: '$i'
};
export class GameData {
private scene: BattleScene;
@ -256,6 +264,15 @@ export class GameData {
}) as SystemSaveData;
}
private convertSystemDataStr(dataStr: string, shorten: boolean = false): string {
const fromKeys = shorten ? Object.keys(systemShortKeys) : Object.values(systemShortKeys);
const toKeys = shorten ? Object.values(systemShortKeys) : Object.keys(systemShortKeys);
for (let k in fromKeys)
dataStr = dataStr.replace(new RegExp(`${fromKeys[k].replace('$', '\\$')}`, 'g'), toKeys[k]);
return dataStr;
}
public saveSetting(setting: Setting, valueIndex: integer): boolean {
let settings: object = {};
if (localStorage.hasOwnProperty('settings'))
@ -439,7 +456,12 @@ export class GameData {
public exportData(dataType: GameDataType): void {
const dataKey: string = getDataTypeKey(dataType);
const dataStr = atob(localStorage.getItem(dataKey));
let dataStr = atob(localStorage.getItem(dataKey));
switch (dataType) {
case GameDataType.SYSTEM:
dataStr = this.convertSystemDataStr(dataStr, true);
break;
}
const encryptedData = AES.encrypt(dataStr, saveKey);
const blob = new Blob([ encryptedData.toString() ], {type: 'text/json'});
const link = document.createElement('a');
@ -467,11 +489,12 @@ export class GameData {
reader.onload = (_ => {
return e => {
const dataStr = AES.decrypt(e.target.result.toString(), saveKey).toString(enc.Utf8);
let dataStr = AES.decrypt(e.target.result.toString(), saveKey).toString(enc.Utf8);
let valid = false;
try {
switch (dataType) {
case GameDataType.SYSTEM:
dataStr = this.convertSystemDataStr(dataStr);
const systemData = this.parseSystemData(dataStr);
valid = !!systemData.dexData && !!systemData.timestamp;
break;