[feat] account storage migration
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import localforage from "localforage"
|
import localforage from "localforage"
|
||||||
import { isNodeServer } from "./globalApi"
|
import { getUnpargeables, isNodeServer, replaceDbResources } from "./globalApi"
|
||||||
import { NodeStorage } from "./nodeStorage"
|
import { NodeStorage } from "./nodeStorage"
|
||||||
import { OpfsStorage } from "./opfsStorage"
|
import { OpfsStorage } from "./opfsStorage"
|
||||||
import { alertConfirm, alertStore } from "../alert"
|
import { alertConfirm, alertStore } from "../alert"
|
||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
import { DataBase } from "./database"
|
import { DataBase } from "./database"
|
||||||
import { AccountStorage } from "./accountStorage"
|
import { AccountStorage } from "./accountStorage"
|
||||||
|
import { encodeRisuSave } from "./risuSave";
|
||||||
|
|
||||||
export class AutoStorage{
|
export class AutoStorage{
|
||||||
isAccount:boolean = false
|
isAccount:boolean = false
|
||||||
@@ -35,20 +36,53 @@ export class AutoStorage{
|
|||||||
return await this.realStorage.removeItem(key)
|
return await this.realStorage.removeItem(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkAccountSync(){
|
async checkAccountSync(){
|
||||||
let db = get(DataBase)
|
let db = get(DataBase)
|
||||||
if(db.account?.useSync){
|
if(db.account?.useSync && (localStorage.getItem('accountst') !== 'able')){
|
||||||
console.log("using account storage")
|
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)
|
sessionStorage.setItem('fallbackRisuToken',db.account?.token)
|
||||||
this.realStorage = new AccountStorage()
|
|
||||||
this.isAccount = true
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
else if(localStorage.getItem('accountst') === 'able'){
|
||||||
|
this.realStorage = new AccountStorage()
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Init(){
|
private async Init(){
|
||||||
if(!this.realStorage){
|
if(!this.realStorage){
|
||||||
|
if(localStorage.getItem('accountst') === 'able'){
|
||||||
|
this.realStorage = new AccountStorage()
|
||||||
|
return
|
||||||
|
}
|
||||||
if(isNodeServer){
|
if(isNodeServer){
|
||||||
console.log("using node storage")
|
console.log("using node storage")
|
||||||
this.realStorage = new NodeStorage()
|
this.realStorage = new NodeStorage()
|
||||||
|
|||||||
@@ -341,7 +341,7 @@ export async function loadData() {
|
|||||||
throw "Your save file is corrupted"
|
throw "Your save file is corrupted"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(forageStorage.checkAccountSync()){
|
if(await forageStorage.checkAccountSync()){
|
||||||
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin')
|
let gotStorage:Uint8Array = await forageStorage.getItem('database/database.bin')
|
||||||
if(checkNullish(gotStorage)){
|
if(checkNullish(gotStorage)){
|
||||||
gotStorage = encodeRisuSave({})
|
gotStorage = encodeRisuSave({})
|
||||||
@@ -712,6 +712,40 @@ export function getUnpargeables(db:Database) {
|
|||||||
return unpargeable
|
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() {
|
async function checkNewFormat() {
|
||||||
let db = get(DataBase)
|
let db = get(DataBase)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user