mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-13 09:28:14 +08:00
@@ -1,12 +1,10 @@
|
||||
/**
|
||||
* @file utils/UIAF.ts
|
||||
* @description UIAF工具类
|
||||
* @since Beta v0.3.4
|
||||
* @since Beta v0.4.1
|
||||
*/
|
||||
|
||||
import { app, fs, path } from "@tauri-apps/api";
|
||||
|
||||
import TGSqlite from "../plugins/Sqlite";
|
||||
import { app, fs } from "@tauri-apps/api";
|
||||
|
||||
/**
|
||||
* @description 根据 completed 跟 progress 获取 status
|
||||
@@ -64,32 +62,3 @@ export async function readUiafData(userPath: string): Promise<TGApp.Plugins.UIAF
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
return <TGApp.Plugins.UIAF.Data>JSON.parse(fileData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 根据成就数据导出 UIAF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {TGApp.Plugins.UIAF.Achievement[]} achievementData - 成就数据
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function backupUiafData(
|
||||
achievementData: TGApp.Plugins.UIAF.Achievement[],
|
||||
): Promise<void> {
|
||||
const savePath = `${await path.appLocalDataDir()}userData\\UIAF.json`;
|
||||
await fs.writeTextFile(savePath, JSON.stringify(achievementData, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 根据 UIAF 数据恢复成就数据
|
||||
* @since Alpha v0.2.3
|
||||
* @returns {Promise<boolean>} 恢复的成就数量
|
||||
*/
|
||||
export async function restoreUiafData(): Promise<boolean> {
|
||||
const uiafPath = `${await path.appLocalDataDir()}userData\\UIAF.json`;
|
||||
// 检测是否存在 UIAF 数据
|
||||
if (!(await fs.exists(uiafPath))) {
|
||||
return false;
|
||||
}
|
||||
const uiafData: TGApp.Plugins.UIAF.Achievement[] = JSON.parse(await fs.readTextFile(uiafPath));
|
||||
await TGSqlite.mergeUIAF(uiafData);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file utils/UIGF.ts
|
||||
* @description UIGF工具类
|
||||
* @since Beta v0.3.8
|
||||
* @since Beta v0.4.1
|
||||
*/
|
||||
|
||||
import { app, fs, path } from "@tauri-apps/api";
|
||||
@@ -111,15 +111,17 @@ export async function exportUigfData(
|
||||
|
||||
/**
|
||||
* @description 备份 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @since Beta v0.4.1
|
||||
* @param {string} dirPath - 备份路径
|
||||
* @param {string} uid - UID
|
||||
* @param {TGApp.Sqlite.GachaRecords.SingleTable[]} gachaList - 祈愿列表
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function backupUigfData(
|
||||
dirPath: string,
|
||||
uid: string,
|
||||
gachaList: TGApp.Sqlite.GachaRecords.SingleTable[],
|
||||
): Promise<void> {
|
||||
const savePath = `${await path.appLocalDataDir()}userData\\UIGF_${uid}.json`;
|
||||
await exportUigfData(uid, gachaList, savePath);
|
||||
if (!(await fs.exists(dirPath))) await fs.createDir(dirPath, { recursive: true });
|
||||
await exportUigfData(uid, gachaList, `${dirPath}${path.sep}UIGF_${uid}.json`);
|
||||
}
|
||||
|
||||
122
src/utils/dataBS.ts
Normal file
122
src/utils/dataBS.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* @file utils/dataBS.ts
|
||||
* @description 用户数据的备份、恢复、迁移
|
||||
* @since Beta v0.4.1
|
||||
*/
|
||||
|
||||
import { fs, path } from "@tauri-apps/api";
|
||||
|
||||
import showSnackbar from "../components/func/snackbar";
|
||||
import TGSqlite from "../plugins/Sqlite";
|
||||
|
||||
/**
|
||||
* @description 备份用户数据
|
||||
* @since Beta v0.4.1
|
||||
* @param {string} dir 备份目录路径
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function backUpUserData(dir: string): Promise<void> {
|
||||
if (!(await fs.exists(dir))) {
|
||||
console.log("备份目录不存在,创建备份目录");
|
||||
await fs.createDir(dir, { recursive: true });
|
||||
}
|
||||
// 备份成就数据
|
||||
const dataAchi = await TGSqlite.getUIAF();
|
||||
await fs.writeTextFile(`${dir}${path.sep}UIAF.json`, JSON.stringify(dataAchi));
|
||||
// 备份 ck
|
||||
const dataCK = await TGSqlite.getCookie();
|
||||
await fs.writeTextFile(`${dir}${path.sep}cookie.json`, JSON.stringify(dataCK));
|
||||
// 备份深渊数据
|
||||
const dataAbyss = await TGSqlite.getAbyss();
|
||||
await fs.writeTextFile(`${dir}${path.sep}abyss.json`, JSON.stringify(dataAbyss));
|
||||
// todo 添加祈愿数据备份支持
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 恢复用户数据
|
||||
* @since Beta v0.4.1
|
||||
* @param {string} dir 备份目录路径
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function restoreUserData(dir: string): Promise<void> {
|
||||
let errNum = 0;
|
||||
if (!(await fs.exists(dir))) {
|
||||
showSnackbar({
|
||||
text: "备份目录不存在",
|
||||
color: "error",
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 恢复成就数据
|
||||
const dataAchiPath = `${dir}${path.sep}UIAF.json`;
|
||||
if (await fs.exists(dataAchiPath)) {
|
||||
try {
|
||||
const dataAchi: TGApp.Plugins.UIAF.Achievement[] = JSON.parse(
|
||||
await fs.readTextFile(dataAchiPath),
|
||||
);
|
||||
await TGSqlite.mergeUIAF(dataAchi);
|
||||
} catch (e) {
|
||||
showSnackbar({
|
||||
text: "成就数据恢复失败",
|
||||
color: "error",
|
||||
});
|
||||
errNum++;
|
||||
}
|
||||
} else {
|
||||
showSnackbar({
|
||||
text: "成就数据恢复失败,备份文件不存在",
|
||||
color: "warn",
|
||||
});
|
||||
}
|
||||
// 恢复 ck
|
||||
const dataCKPath = `${dir}${path.sep}cookie.json`;
|
||||
if (await fs.exists(dataCKPath)) {
|
||||
try {
|
||||
const dataCK = await fs.readTextFile(dataCKPath);
|
||||
await TGSqlite.saveAppData("cookie", JSON.stringify(JSON.parse(dataCK)));
|
||||
} catch (e) {
|
||||
showSnackbar({
|
||||
text: "Cookie 数据恢复失败",
|
||||
color: "error",
|
||||
});
|
||||
errNum++;
|
||||
}
|
||||
} else {
|
||||
showSnackbar({
|
||||
text: "Cookie 数据恢复失败,备份文件不存在",
|
||||
color: "warn",
|
||||
});
|
||||
}
|
||||
// 恢复深渊数据
|
||||
const dataAbyssPath = `${dir}${path.sep}abyss.json`;
|
||||
if (await fs.exists(dataAbyssPath)) {
|
||||
try {
|
||||
const dataAbyss: TGApp.Sqlite.Abyss.SingleTable[] = JSON.parse(
|
||||
await fs.readTextFile(dataAbyssPath),
|
||||
);
|
||||
await TGSqlite.restoreAbyss(dataAbyss);
|
||||
} catch (e) {
|
||||
showSnackbar({
|
||||
text: "深渊数据恢复失败",
|
||||
color: "error",
|
||||
});
|
||||
errNum++;
|
||||
}
|
||||
} else {
|
||||
showSnackbar({
|
||||
text: "深渊数据恢复失败,备份文件不存在",
|
||||
color: "warn",
|
||||
});
|
||||
}
|
||||
if (errNum === 0) {
|
||||
showSnackbar({
|
||||
text: "数据恢复成功",
|
||||
color: "success",
|
||||
});
|
||||
} else {
|
||||
showSnackbar({
|
||||
text: `数据恢复失败,失败数量:${errNum}`,
|
||||
color: "error",
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user