diff --git a/.gitignore b/.gitignore index cb5417d..6ea8f0d 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/config/config.json b/config/config.json deleted file mode 100644 index 2f7df30..0000000 --- a/config/config.json +++ /dev/null @@ -1,98 +0,0 @@ -{ - "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" -} diff --git a/config/config.yaml b/config/config.yaml new file mode 100644 index 0000000..efdf1df --- /dev/null +++ b/config/config.yaml @@ -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 diff --git a/package.json b/package.json index 925bf61..b822449 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -159,4 +159,4 @@ "resolutions": { "electron-rebuild/node-gyp": "^6.0.1" } -} +} \ No newline at end of file diff --git a/src/main/config/Config.ts b/src/main/config/Config.ts index a7e889c..f9b7a88 100644 --- a/src/main/config/Config.ts +++ b/src/main/config/Config.ts @@ -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') @@ -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) @@ -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 = {} @@ -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 () { diff --git a/yarn.lock b/yarn.lock index 2a869c1..1999d9f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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=