Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 yaml 格式存储配置文件 #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ npm-debug.log.*
thumbs.db
!.gitkeep
.vscode/yuki.code-workspace
config/games.json
config/gui.json
config/texts.json
config/*.json
config/games.yaml
config/gui.yaml
config/texts.yaml
lib/dict/lingoes
package-lock.json
98 changes: 0 additions & 98 deletions config/config.json

This file was deleted.

71 changes: 71 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
localeChangers:
alphaROMdiE:
enable: false
exec: 'C:\Resources\Games\AlphaROMdiE.exe %GAME_PATH%'
name: AlphaROMdiE
localeEmulator:
enable: true
exec: 'C:\LocaleEmulator\LEProc.exe %GAME_PATH%'
name: Locale Emulator
noChanger:
enable: false
exec: '%GAME_PATH%'
name: No Changer
ntleas:
enable: false
exec: 'C:\ntleas046_x64\x86\ntleas.exe %GAME_PATH%'
name: Ntleas
onlineApis:
- enable: true
external: true
jsFile: config\qqApi.js
name: 腾讯
- enable: false
external: true
jsFile: config\tencentApi.js
name: 腾讯云
- enable: true
external: true
jsFile: config\youdaoApi.js
name: 有道
- enable: false
method: POST
name: 谷歌
requestBodyFormat: 'X{"q": %TEXT%, "sl": "ja", "tl": "zh-CN"}'
responseBodyPattern: 'Rclass="t0">([^<]*)<'
url: 'https://translate.google.cn/m'
- enable: false
method: POST
name: 彩云
requestBodyFormat: >-
J{"source": %TEXT%, "trans_type": "ja2zh", "request_id": "demo", "detect":
"true"}
requestHeaders: '{"X-Authorization": "token 3975l6lr5pcbvidl6jl2"}'
responseBodyPattern: J%RESPONSE%.target
url: 'https://api.interpreter.caiyunai.com/v1/translator'
- enable: false
external: true
jsFile: config\azureApi.js
name: Azure
- enable: false
external: true
jsFile: config\baiduApi.js
name: 百度
- enable: false
external: true
jsFile: config\newBaiduApi.js
name: 百度开放平台
translators:
jBeijing:
dictPath: 'C:\YUKI\yuki\lib\dict\jb'
enable: true
path: 'C:\JBeijing7'
dictionaries:
lingoes:
enable: true
path: 'C:\YUKI\libraries\dict\lingoes\njcd.db'
mecab:
enable: true
path: 'C:\YUKI\libraries\pos\mecab-ipadic'
librariesRepoUrl: 'https://github.com/project-yuki/libraries/raw/master/_pack/'
language: zh
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"extract-zip": "^1.6.7",
"ffi": "^2.3.0",
"fstream": "^1.0.12",
"jsonfile": "^5.0.0",
"js-yaml": "^3.14.0",
"mecab-ffi": "^0.2.0",
"ref": "^1.3.5",
"request": "^2.88.0",
Expand All @@ -83,7 +83,7 @@
"@mdi/font": "^4.5.95",
"@types/chai": "^4.1.7",
"@types/ffi": "^0.2.1",
"@types/jsonfile": "^4.0.1",
"@types/js-yaml": "^3.12.5",
"@types/lodash": "^4.14.121",
"@types/mocha": "^5.2.5",
"@types/request-promise-native": "^1.0.15",
Expand Down Expand Up @@ -159,4 +159,4 @@
"resolutions": {
"electron-rebuild/node-gyp": "^6.0.1"
}
}
}
33 changes: 29 additions & 4 deletions src/main/config/Config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app, ipcMain } from 'electron'
import * as fs from 'fs'
import * as jsonfile from 'jsonfile'
import { safeDump, safeLoad } from 'js-yaml'
import * as path from 'path'
import IpcTypes from '../../common/IpcTypes'
const debug = require('debug')('yuki:config')
Expand All @@ -14,10 +14,12 @@ abstract class Config {
protected config: any

protected filePath!: string
protected filePathOld!: string
protected isSaving: boolean = false

public init () {
this.filePath = path.resolve(global.__baseDir, `config/${this.getFilename()}.json`)
this.filePath = path.resolve(global.__baseDir, `config/${this.getFilename()}.yaml`)
this.filePathOld = path.resolve(global.__baseDir, `config/${this.getFilename()}.json`)
this.load()
this.save()
debug('%s loaded with pre-save', this.filePath)
Expand All @@ -28,7 +30,13 @@ abstract class Config {
public load () {
let fileContent
try {
fileContent = jsonfile.readFileSync(this.filePath)
let filePath = this.filePath
if (!fs.existsSync(this.filePath)) {
filePath = this.filePathOld
} else {
this.filePathOld = ''
}
fileContent = safeLoad(fs.readFileSync(filePath, { encoding: 'utf-8' }), { json: true }) as Object
} catch (e) {
debug('%s loads failed !> %s', this.filePath, e)
fileContent = {}
Expand All @@ -41,11 +49,28 @@ abstract class Config {

public save () {
try {
jsonfile.writeFileSync(this.filePath, this.config, Config.FILE_OPTIONS)
Config.FILE_OPTIONS;
fs.writeFileSync(
this.filePath,
safeDump(
this.config,
{ indent: Config.FILE_OPTIONS.spaces }
).replace("\n", Config.FILE_OPTIONS.EOL)
)
debug('%s saved', this.filePath)
} catch (e) {
debug('%s saves failed !> %s', this.filePath, e)
}

try {
if (this.filePathOld) {
fs.unlinkSync(this.filePathOld)
this.filePathOld = ''
debug('%s deleted', this.filePathOld)
}
} catch (e) {
debug('%s deletes failed !> %s', this.filePathOld, e)
}
}

public get () {
Expand Down
24 changes: 14 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
dependencies:
"@types/node" "*"

"@types/js-yaml@^3.12.5":
version "3.12.5"
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-3.12.5.tgz#136d5e6a57a931e1cce6f9d8126aa98a9c92a6bb"
integrity sha512-JCcp6J0GV66Y4ZMDAQCXot4xprYB+Zfd3meK9+INSJeVZwJmHAW30BBEEkPzXswMXuiyReUGOP3GxrADc9wPww==

"@types/jsonfile@^4.0.1":
version "4.0.1"
resolved "http://registry.npm.taobao.org/@types/jsonfile/download/@types/jsonfile-4.0.1.tgz#be43f5096f1b3c97180e7ae419d747287d24d494"
Expand Down Expand Up @@ -5682,6 +5687,14 @@ js-yaml@^3.12.1:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^3.14.0:
version "3.14.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.0.tgz#a7a34170f26a21bb162424d8adacb4113a69e482"
integrity sha512-/4IbIeHcD9VMHFqDR/gQ7EdZdLimOvW2DdcxFjdyyZ9NsbS+ccrXqVWDtab/lRl5AlUqmpBx8EhPaWR+OtY17A==
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^3.7.0:
version "3.12.2"
resolved "http://registry.npm.taobao.org/js-yaml/download/js-yaml-3.12.2.tgz#ef1d067c5a9d9cb65bd72f285b5d8105c77f14fc"
Expand Down Expand Up @@ -5769,15 +5782,6 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsonfile@^5.0.0:
version "5.0.0"
resolved "http://registry.npm.taobao.org/jsonfile/download/jsonfile-5.0.0.tgz#e6b718f73da420d612823996fdf14a03f6ff6922"
integrity sha1-5rcY9z2kINYSgjmW/fFKA/b/aSI=
dependencies:
universalify "^0.1.2"
optionalDependencies:
graceful-fs "^4.1.6"

jsprim@^1.2.2:
version "1.4.1"
resolved "http://registry.npm.taobao.org/jsprim/download/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
Expand Down Expand Up @@ -9748,7 +9752,7 @@ unique-string@^1.0.0:
dependencies:
crypto-random-string "^1.0.0"

universalify@^0.1.0, universalify@^0.1.2:
universalify@^0.1.0:
version "0.1.2"
resolved "http://registry.npm.taobao.org/universalify/download/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha1-tkb2m+OULavOzJ1mOcgNwQXvqmY=
Expand Down