[feat] account storage migration
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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<cha.emotionImages.length;i++){
|
||||
cha.emotionImages[i][1] = replaceData(cha.emotionImages[i][1])
|
||||
}
|
||||
}
|
||||
if(cha.type !== 'group'){
|
||||
if(cha.additionalAssets){
|
||||
for(let i=0;i<cha.additionalAssets.length;i++){
|
||||
cha.additionalAssets[i][1] = replaceData(cha.additionalAssets[i][1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
async function checkNewFormat() {
|
||||
let db = get(DataBase)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user