From 3dbb746a38b50b24e1e8abceea49875354d34c6f Mon Sep 17 00:00:00 2001 From: mio <10892119+biuuu@users.noreply.github.com> Date: Wed, 3 May 2023 21:22:37 +0800 Subject: [PATCH] feat: save data in the current folder --- package.json | 2 +- src/main/config.js | 7 +++++-- src/main/getData.js | 45 +++++++++++++++++++++++++++++++-------------- src/main/utils.js | 18 ++++++++++-------- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index ce54a9d..8216924 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "star-rail-warp-export", - "version": "0.0.7", + "version": "0.0.8", "main": "./dist/electron/main/main.js", "author": "biuuu ", "license": "MIT", diff --git a/src/main/config.js b/src/main/config.js index a46a084..b7bb77e 100644 --- a/src/main/config.js +++ b/src/main/config.js @@ -1,4 +1,4 @@ -const { readJSON, saveJSON, decipherAes, cipherAes, detectLocale } = require('./utils') +const { readJSON, saveJSON, decipherAes, cipherAes, detectLocale, userDataPath, globalUserDataPath } = require('./utils') const config = { urls: [], @@ -13,7 +13,10 @@ const config = { } const getLocalConfig = async () => { - const localConfig = await readJSON('config.json') + let localConfig = await readJSON(userDataPath, 'config.json') + if (!localConfig) { + localConfig = await readJSON(globalUserDataPath, 'config.json') + } if (!localConfig) return const configTemp = {} for (let key in localConfig) { diff --git a/src/main/getData.js b/src/main/getData.js index 1ca5b1a..837e1e8 100644 --- a/src/main/getData.js +++ b/src/main/getData.js @@ -3,7 +3,7 @@ const util = require('util') const path = require('path') const { URL } = require('url') const { app, ipcMain, shell } = require('electron') -const { sleep, request, sendMsg, readJSON, saveJSON, detectLocale, userDataPath, userPath, localIp, langMap } = require('./utils') +const { sleep, request, sendMsg, readJSON, saveJSON, detectLocale, userDataPath, userPath, localIp, langMap, globalUserDataPath } = require('./utils') const config = require('./config') const i18n = require('./i18n') const { enableProxy, disableProxy } = require('./module/system-proxy') @@ -29,25 +29,42 @@ const defaultTypeMap = new Map([ ['2', '始发跃迁'] ]) +const findDataFiles = async (dataPath, fileMap) => { + const files = await readdir(dataPath) + if (files?.length) { + for (let name of files) { + if (/^gacha-list-\d+\.json$/.test(name) && !fileMap.has(name)) { + fileMap.set(name, dataPath) + } + } + } +} + +const collectDataFiles = async () => { + await fs.ensureDir(userDataPath) + await fs.ensureDir(globalUserDataPath) + const fileMap = new Map() + await findDataFiles(userDataPath, fileMap) + await findDataFiles(globalUserDataPath, fileMap) + return fileMap +} + let localDataReaded = false const readdir = util.promisify(fs.readdir) const readData = async () => { if (localDataReaded) return localDataReaded = true - await fs.ensureDir(userDataPath) - const files = await readdir(userDataPath) - for (let name of files) { - if (/^gacha-list-\d+\.json$/.test(name)) { - try { - const data = await readJSON(name) - data.typeMap = new Map(data.typeMap) || defaultTypeMap - data.result = new Map(data.result) - if (data.uid) { - dataMap.set(data.uid, data) - } - } catch (e) { - sendMsg(e, 'ERROR') + const fileMap = await collectDataFiles() + for (let [name, dataPath] of fileMap) { + try { + const data = await readJSON(dataPath, name) + data.typeMap = new Map(data.typeMap) || defaultTypeMap + data.result = new Map(data.result) + if (data.uid) { + dataMap.set(data.uid, data) } + } catch (e) { + sendMsg(e, 'ERROR') } } if ((!config.current && dataMap.size) || (config.current && dataMap.size && !dataMap.has(config.current))) { diff --git a/src/main/utils.js b/src/main/utils.js index 1bcfb5e..2041a96 100644 --- a/src/main/utils.js +++ b/src/main/utils.js @@ -11,8 +11,9 @@ const Registry = require('winreg') const isDev = !app.isPackaged const userPath = app.getPath('userData') -const appRoot = isDev ? path.resolve(__dirname, '..', '..') : userPath +const appRoot = isDev ? path.resolve(__dirname, '..', '..') : path.resolve(app.getAppPath(), '..', '..') const userDataPath = path.resolve(appRoot, 'userData') +const globalUserDataPath = path.resolve(userPath, 'userData') let win = null const initWindow = () => { @@ -56,7 +57,7 @@ const saveLog = () => { const text = item[2] return `[${type}][${time}]${text}` }).join('\r\n') - fs.outputFileSync(path.join(userDataPath, 'log.txt'), text) + fs.outputFile(path.join(userDataPath, 'log.txt'), text) } const authkeyMask = (text = '') => { @@ -144,19 +145,20 @@ const detectLocale = (value) => { const saveJSON = async (name, data) => { try { - await fs.outputJSON(path.join(userDataPath, name), data, { - spaces: 2 - }) + await fs.outputJSON(path.join(userDataPath, name), data) + if (!isDev) { + await fs.outputJSON(path.join(globalUserDataPath, name), data) + } } catch (e) { sendMsg(e, 'ERROR') await sleep(3) } } -const readJSON = async (name) => { +const readJSON = async (dataPath, name) => { let data = null try { - data = await fs.readJSON(path.join(userDataPath, name)) + data = await fs.readJSON(path.join(dataPath, name)) } catch (e) {} return data } @@ -203,5 +205,5 @@ const localIp = () => { module.exports = { sleep, request, hash, cipherAes, decipherAes, saveLog, sendMsg, readJSON, saveJSON, initWindow, getWin, localIp, userPath, detectLocale, langMap, - appRoot, userDataPath + appRoot, userDataPath, globalUserDataPath } \ No newline at end of file