From 7eb6907eef999bde3aa5e00573d329bc4133a8d4 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Fri, 30 Jun 2023 06:35:08 +0900 Subject: [PATCH] [feat] account storage migration --- src/ts/storage/autoStorage.ts | 46 ++++++++++++++++++++++++++++++----- src/ts/storage/globalApi.ts | 36 ++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/ts/storage/autoStorage.ts b/src/ts/storage/autoStorage.ts index 2fe17873..722cfcc5 100644 --- a/src/ts/storage/autoStorage.ts +++ b/src/ts/storage/autoStorage.ts @@ -1,11 +1,12 @@ import localforage from "localforage" -import { isNodeServer } from "./globalApi" +import { getUnpargeables, isNodeServer, replaceDbResources } from "./globalApi" import { NodeStorage } from "./nodeStorage" import { OpfsStorage } from "./opfsStorage" import { alertConfirm, alertStore } from "../alert" import { get } from "svelte/store" import { DataBase } from "./database" import { AccountStorage } from "./accountStorage" +import { encodeRisuSave } from "./risuSave"; export class AutoStorage{ isAccount:boolean = false @@ -35,20 +36,53 @@ export class AutoStorage{ return await this.realStorage.removeItem(key) } - checkAccountSync(){ + async checkAccountSync(){ let db = get(DataBase) - if(db.account?.useSync){ - console.log("using account storage") + if(db.account?.useSync && (localStorage.getItem('accountst') !== 'able')){ + getUnpargeables(db) + console.log("migrating") + const keys = await this.realStorage.keys() + let i = 0; + const accountStorage = new AccountStorage() + let replaced:{[key:string]:string} = {} + + for(const key of keys){ + alertStore.set({ + type: "wait", + msg: `Migrating your data...(${i}/${keys.length})` + }) + const rkey = await accountStorage.setItem(key,await this.realStorage.getItem(key)) + if(rkey !== key){ + replaced[key] = rkey + } + i += 1 + } + + const dba = replaceDbResources(db, replaced) + await accountStorage.setItem('database/database.bin', encodeRisuSave(dba)) + + this.realStorage = accountStorage + alertStore.set({ + type: "none", + msg: "" + }) + + localStorage.setItem('accountst', 'able') sessionStorage.setItem('fallbackRisuToken',db.account?.token) - this.realStorage = new AccountStorage() - this.isAccount = true return true } + else if(localStorage.getItem('accountst') === 'able'){ + this.realStorage = new AccountStorage() + } return false } private async Init(){ if(!this.realStorage){ + if(localStorage.getItem('accountst') === 'able'){ + this.realStorage = new AccountStorage() + return + } if(isNodeServer){ console.log("using node storage") this.realStorage = new NodeStorage() diff --git a/src/ts/storage/globalApi.ts b/src/ts/storage/globalApi.ts index 58afca5d..d68567e6 100644 --- a/src/ts/storage/globalApi.ts +++ b/src/ts/storage/globalApi.ts @@ -341,7 +341,7 @@ export async function loadData() { throw "Your save file is corrupted" } } - if(forageStorage.checkAccountSync()){ + if(await forageStorage.checkAccountSync()){ let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin') if(checkNullish(gotStorage)){ gotStorage = encodeRisuSave({}) @@ -712,6 +712,40 @@ export function getUnpargeables(db:Database) { return unpargeable } + +export function replaceDbResources(db:Database,replacer:{[key:string]:string}) { + let unpargeable:string[] = [] + + function replaceData(data:string){ + if(!data){ + return data + } + return replacer[data] ?? data + } + + db.customBackground = replaceData(db.customBackground) + db.userIcon = replaceData(db.userIcon) + + for(const cha of db.characters){ + if(cha.image){ + cha.image = replaceData(cha.image) + } + if(cha.emotionImages){ + for(let i=0;i