mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-15 09:48:14 +08:00
🌱 初步完成UIGF 导入导出备份恢复,并参照优化UIAF相关代码
This commit is contained in:
@@ -46,55 +46,47 @@ export async function getUiafHeader(): Promise<TGApp.Plugins.UIAF.Export> {
|
||||
/**
|
||||
* @description 检测是否存在 UIAF 数据
|
||||
* @description 粗略检测,不保证数据完整性
|
||||
* @since Alpha v0.1.3
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} path - UIAF 数据路径
|
||||
* @returns {Promise<boolean>} 是否存在 UIAF 数据
|
||||
*/
|
||||
export async function verifyUiafData(path: string): Promise<boolean> {
|
||||
const fileData: string = await fs.readTextFile(path);
|
||||
const UiafData: TGApp.Plugins.UIAF.Export = JSON.parse(fileData).info;
|
||||
return UiafData.uiaf_version !== undefined;
|
||||
const UiafData: TGApp.Plugins.UIAF.Export = JSON.parse(fileData)?.info;
|
||||
return UiafData?.uiaf_version !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 读取 UIAF 数据
|
||||
* @since Alpha v0.1.3
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} userPath - UIAF 数据路径
|
||||
* @returns {Promise<string|false>} UIAF 数据
|
||||
* @returns {Promise<TGApp.Plugins.UIAF.Data>} UIAF 数据
|
||||
*/
|
||||
export async function readUiafData(userPath: string): Promise<string | false> {
|
||||
if (await fs.exists(userPath)) {
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
if (fileData !== undefined && fileData !== null && fileData !== "" && fileData !== "{}") {
|
||||
return fileData;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
export async function readUiafData(userPath: string): Promise<TGApp.Plugins.UIAF.Data> {
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
return <TGApp.Plugins.UIAF.Data>JSON.parse(fileData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 根据成就数据导出 UIAF 数据
|
||||
* @since Alpha v0.1.4
|
||||
* @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`;
|
||||
const savePath = `${await path.appLocalDataDir()}userData\\UIAF.json`;
|
||||
await fs.writeTextFile(savePath, JSON.stringify(achievementData, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 根据 UIAF 数据恢复成就数据
|
||||
* @since Alpha v0.1.4
|
||||
* @since Alpha v0.2.3
|
||||
* @returns {Promise<boolean>} 恢复的成就数量
|
||||
*/
|
||||
export async function restoreUiafData(): Promise<boolean> {
|
||||
const uiafPath = `${await path.appLocalDataDir()}\\userData\\UIAF.json`;
|
||||
const uiafPath = `${await path.appLocalDataDir()}userData\\UIAF.json`;
|
||||
// 检测是否存在 UIAF 数据
|
||||
if (!(await fs.exists(uiafPath))) {
|
||||
return false;
|
||||
|
||||
@@ -30,6 +30,30 @@ export async function getUigfHeader(uid: string): Promise<TGApp.Plugins.UIGF.Exp
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 数据转换-数据库到 UIGF
|
||||
* @since Alpha v0.2.3
|
||||
* @param {TGApp.Sqlite.GachaRecords.SingleTable[]} data - 数据库数据
|
||||
* @returns {TGApp.Plugins.UIGF.GachaItem[]} UIGF 数据
|
||||
*/
|
||||
export function convertDataToUigf(
|
||||
data: TGApp.Sqlite.GachaRecords.SingleTable[],
|
||||
): TGApp.Plugins.UIGF.GachaItem[] {
|
||||
return data.map((gacha) => {
|
||||
return {
|
||||
gacha_type: gacha.gachaType,
|
||||
item_id: gacha.itemId,
|
||||
count: gacha.count,
|
||||
time: gacha.time,
|
||||
name: gacha.name,
|
||||
item_type: gacha.type,
|
||||
rank_type: gacha.rank,
|
||||
id: gacha.id,
|
||||
uigf_gacha_type: gacha.uigfType,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 检测是否存在 UIGF 数据
|
||||
* @description 粗略检测,不保证数据完整性
|
||||
@@ -39,42 +63,55 @@ export async function getUigfHeader(uid: string): Promise<TGApp.Plugins.UIGF.Exp
|
||||
*/
|
||||
export async function verifyUigfData(path: string): Promise<boolean> {
|
||||
const fileData: string = await fs.readTextFile(path);
|
||||
const UigfData: TGApp.Plugins.UIGF.Export = JSON.parse(fileData).info;
|
||||
return UigfData.uigf_version !== undefined;
|
||||
const UigfData: TGApp.Plugins.UIGF.Export = JSON.parse(fileData)?.info;
|
||||
return UigfData?.uigf_version !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 读取 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} userPath - UIGF 数据路径
|
||||
* @returns {Promise<string|false>} UIGF 数据
|
||||
* @returns {Promise<TGApp.Plugins.UIGF.FullData>} UIGF 数据
|
||||
*/
|
||||
export async function readUigfData(userPath: string): Promise<string | false> {
|
||||
if (await fs.exists(userPath)) {
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
if (fileData !== undefined && fileData !== null && fileData !== "" && fileData !== "{}") {
|
||||
return fileData;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
export async function readUigfData(userPath: string): Promise<TGApp.Plugins.UIGF.FullData> {
|
||||
const fileData = await fs.readTextFile(userPath);
|
||||
return <TGApp.Plugins.UIGF.FullData>JSON.parse(fileData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 导出 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {string} uid - UID
|
||||
* @param {TGApp.Sqlite.GachaRecords.SingleTable[]} gachaList - 祈愿列表
|
||||
* @param {string} savePath - 保存路径
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function exportUigfData(
|
||||
uid: string,
|
||||
gachaList: TGApp.Sqlite.GachaRecords.SingleTable[],
|
||||
savePath?: string,
|
||||
): Promise<void> {
|
||||
const UigfData = {
|
||||
info: await getUigfHeader(uid),
|
||||
list: convertDataToUigf(gachaList),
|
||||
};
|
||||
const filePath = savePath ?? `${await path.appLocalDataDir()}userData\\UIGF_${uid}.json`;
|
||||
await fs.writeTextFile(filePath, JSON.stringify(UigfData));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 备份 UIGF 数据
|
||||
* @since Alpha v0.2.3
|
||||
* @param {TGApp.Plugins.UIAF.GachaItem[]} gachaList - 祈愿列表
|
||||
* @param {string} uid - UID
|
||||
* @param {TGApp.Sqlite.GachaRecords.SingleTable[]} gachaList - 祈愿列表
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function backupUigfData(
|
||||
gachaList: TGApp.Plugins.UIGF.GachaItem[],
|
||||
uid: string,
|
||||
gachaList: TGApp.Sqlite.GachaRecords.SingleTable[],
|
||||
): Promise<void> {
|
||||
const savePath = `${await path.appLocalDataDir()}\\userData\\UIGF_${uid}.json`;
|
||||
await fs.writeTextFile(savePath, JSON.stringify(gachaList, null, 2));
|
||||
const savePath = `${await path.appLocalDataDir()}userData\\UIGF_${uid}.json`;
|
||||
await exportUigfData(uid, gachaList, savePath);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,9 +121,11 @@ export async function backupUigfData(
|
||||
* @returns {Promise<boolean>} UIGF 数据
|
||||
*/
|
||||
export async function restoreUigfData(uid: string): Promise<boolean> {
|
||||
const uigfPath = `${await path.appLocalDataDir()}\\userData\\UIGF_${uid}.json`;
|
||||
const uigfPath = `${await path.appLocalDataDir()}userData\\UIGF_${uid}.json`;
|
||||
if (!(await fs.exists(uigfPath))) {
|
||||
return false;
|
||||
}
|
||||
const uigfData = await readUigfData(uigfPath);
|
||||
if (uigfData === false) return false;
|
||||
await TGSqlite.mergeUIGF(uid, uigfData);
|
||||
await TGSqlite.mergeUIGF(uid, uigfData.list);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user