feat: save data in the current folder

This commit is contained in:
mio 2023-05-03 21:22:37 +08:00
parent b88b57ae62
commit 3dbb746a38
4 changed files with 47 additions and 25 deletions

@ -1,6 +1,6 @@
{ {
"name": "star-rail-warp-export", "name": "star-rail-warp-export",
"version": "0.0.7", "version": "0.0.8",
"main": "./dist/electron/main/main.js", "main": "./dist/electron/main/main.js",
"author": "biuuu <https://github.com/biuuu>", "author": "biuuu <https://github.com/biuuu>",
"license": "MIT", "license": "MIT",

@ -1,4 +1,4 @@
const { readJSON, saveJSON, decipherAes, cipherAes, detectLocale } = require('./utils') const { readJSON, saveJSON, decipherAes, cipherAes, detectLocale, userDataPath, globalUserDataPath } = require('./utils')
const config = { const config = {
urls: [], urls: [],
@ -13,7 +13,10 @@ const config = {
} }
const getLocalConfig = async () => { 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 if (!localConfig) return
const configTemp = {} const configTemp = {}
for (let key in localConfig) { for (let key in localConfig) {

@ -3,7 +3,7 @@ const util = require('util')
const path = require('path') const path = require('path')
const { URL } = require('url') const { URL } = require('url')
const { app, ipcMain, shell } = require('electron') 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 config = require('./config')
const i18n = require('./i18n') const i18n = require('./i18n')
const { enableProxy, disableProxy } = require('./module/system-proxy') const { enableProxy, disableProxy } = require('./module/system-proxy')
@ -29,17 +29,35 @@ const defaultTypeMap = new Map([
['2', '始发跃迁'] ['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 let localDataReaded = false
const readdir = util.promisify(fs.readdir) const readdir = util.promisify(fs.readdir)
const readData = async () => { const readData = async () => {
if (localDataReaded) return if (localDataReaded) return
localDataReaded = true localDataReaded = true
await fs.ensureDir(userDataPath) const fileMap = await collectDataFiles()
const files = await readdir(userDataPath) for (let [name, dataPath] of fileMap) {
for (let name of files) {
if (/^gacha-list-\d+\.json$/.test(name)) {
try { try {
const data = await readJSON(name) const data = await readJSON(dataPath, name)
data.typeMap = new Map(data.typeMap) || defaultTypeMap data.typeMap = new Map(data.typeMap) || defaultTypeMap
data.result = new Map(data.result) data.result = new Map(data.result)
if (data.uid) { if (data.uid) {
@ -49,7 +67,6 @@ const readData = async () => {
sendMsg(e, 'ERROR') sendMsg(e, 'ERROR')
} }
} }
}
if ((!config.current && dataMap.size) || (config.current && dataMap.size && !dataMap.has(config.current))) { if ((!config.current && dataMap.size) || (config.current && dataMap.size && !dataMap.has(config.current))) {
await changeCurrent(dataMap.keys().next().value) await changeCurrent(dataMap.keys().next().value)
} }

@ -11,8 +11,9 @@ const Registry = require('winreg')
const isDev = !app.isPackaged const isDev = !app.isPackaged
const userPath = app.getPath('userData') 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 userDataPath = path.resolve(appRoot, 'userData')
const globalUserDataPath = path.resolve(userPath, 'userData')
let win = null let win = null
const initWindow = () => { const initWindow = () => {
@ -56,7 +57,7 @@ const saveLog = () => {
const text = item[2] const text = item[2]
return `[${type}][${time}]${text}` return `[${type}][${time}]${text}`
}).join('\r\n') }).join('\r\n')
fs.outputFileSync(path.join(userDataPath, 'log.txt'), text) fs.outputFile(path.join(userDataPath, 'log.txt'), text)
} }
const authkeyMask = (text = '') => { const authkeyMask = (text = '') => {
@ -144,19 +145,20 @@ const detectLocale = (value) => {
const saveJSON = async (name, data) => { const saveJSON = async (name, data) => {
try { try {
await fs.outputJSON(path.join(userDataPath, name), data, { await fs.outputJSON(path.join(userDataPath, name), data)
spaces: 2 if (!isDev) {
}) await fs.outputJSON(path.join(globalUserDataPath, name), data)
}
} catch (e) { } catch (e) {
sendMsg(e, 'ERROR') sendMsg(e, 'ERROR')
await sleep(3) await sleep(3)
} }
} }
const readJSON = async (name) => { const readJSON = async (dataPath, name) => {
let data = null let data = null
try { try {
data = await fs.readJSON(path.join(userDataPath, name)) data = await fs.readJSON(path.join(dataPath, name))
} catch (e) {} } catch (e) {}
return data return data
} }
@ -203,5 +205,5 @@ const localIp = () => {
module.exports = { module.exports = {
sleep, request, hash, cipherAes, decipherAes, saveLog, sleep, request, hash, cipherAes, decipherAes, saveLog,
sendMsg, readJSON, saveJSON, initWindow, getWin, localIp, userPath, detectLocale, langMap, sendMsg, readJSON, saveJSON, initWindow, getWin, localIp, userPath, detectLocale, langMap,
appRoot, userDataPath appRoot, userDataPath, globalUserDataPath
} }