Skip to content

Commit

Permalink
new feature: replace static assets in game
Browse files Browse the repository at this point in the history
  • Loading branch information
biuuu committed Mar 22, 2018
1 parent 59368a1 commit acd3e3b
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 17 deletions.
Binary file added assets/tips.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const { USER_DATA_PATH } = require('./store/')
const config = {
// 游戏的主要数据接口
apiHostNames: ['game.granbluefantasy.jp', 'gbf.game.mbga.jp'],
staticHostNames: [
'game-a.granbluefantasy.jp', 'game-a1.granbluefantasy.jp', 'game-a2.granbluefantasy.jp', 'game-a3.granbluefantasy.jp',
'gbf.game-a.mbga.jp', 'gbf.game-a1.mbga.jp', 'gbf.game-a2.mbga.jp', 'gbf.game-a3.mbga.jp'
],
// 是否启用剧情翻译
transScenario: true,
// 是否启用常见短语翻译
Expand All @@ -21,6 +25,10 @@ const config = {
webInterface: true, // 是否启用 anyProxy 的 web 界面
webPort: 8002, // anyProxy 的 web 界面端口

// 是否启用静态文件 http 服务
staticServer: true,
staticPort: 8003,

// 是否解析 https 请求
proxyHttps: false,

Expand All @@ -47,16 +55,15 @@ const getLocalConfig = () => {
try {
const buffer = fs.readFileSync(LOCAL_CONFIG_PATH)
localConfig = JSON.parse(buffer.toString())
Object.assign(config, localConfig)
} catch (err) {
if (err.code === 'ENOENT') {
mkdirp(dirname(LOCAL_CONFIG_PATH), (err) => {
if (err) return
fs.writeFileSync(LOCAL_CONFIG_PATH, JSON.stringify(config, null, 2))
})
}
}

Object.assign(config, localConfig)
fs.writeFileSync(LOCAL_CONFIG_PATH, JSON.stringify(config, null, 2))
}

getLocalConfig()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
</div>
<div v-else>
<h3>碧蓝幻想翻译启动!</h3>
<el-button icon="el-icon-document" size="small" @click="openCsvFolder">打开数据文件夹</el-button>
<el-button icon="el-icon-document" size="small" @click="openCsvFolder">打开数据目录</el-button>
<el-button icon="el-icon-document" size="small" @click="openStaticFolder">静态文件</el-button>
<p>代理地址:<br>127.0.0.1:{{port}}(本机访问)
<br>{{ip}}:{{port}}(局域网其它设备)</p>
<p>查看代理监控:<a href="#" @click="openProxyWeb">http://localhost:{{webPort}}</a></p>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blhxfy",
"version": "0.2.5",
"version": "0.2.6",
"description": "碧蓝幻想微风机翻团",
"main": "main.js",
"scripts": {
Expand All @@ -20,6 +20,7 @@
"electron-log": "^2.2.14",
"electron-updater": "^2.20.1",
"element-ui": "^2.2.1",
"fs-extra": "^5.0.0",
"glob": "^7.1.2",
"ip": "^1.1.5",
"lodash": "^4.17.5",
Expand Down
39 changes: 30 additions & 9 deletions proxy/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ const CONFIG = require('../config.js')
const URI = require('urijs')
const { processResponseBody } = require('../utils/')
const users = require('../store/users')
const staticMap = require('../store/staticMap')

const apiHostNames = CONFIG.apiHostNames
const staticHostNames = CONFIG.staticHostNames

const searchSomething = (responseDetail, uri) => {
const body = responseDetail.response.body.toString()
Expand Down Expand Up @@ -38,24 +40,43 @@ module.exports = {
return { response: { statusCode: 200, header: {}, body: 'started' } }
}
}
if (CONFIG.frontAgent) {
newRequestOptions.hostname = CONFIG.frontAgentHost
newRequestOptions.port = CONFIG.frontAgentPort
newRequestOptions.path = requestDetail.url
}

const uri = URI(requestDetail.url)
const pathname = uri.pathname()
const hostname = uri.hostname()
const query = uri.search(true)
if (CONFIG.interceptTwitterWidgets && requestDetail.url === 'http://platform.twitter.com/widgets.js') {
return { response: { statusCode: 200, header: {}, body: '' } }
}

if (pathname === '/rest/sound/btn_se') {
const user = users.get(query.uid)
if (user && !user.name) {
newRequestOptions.path = newRequestOptions.path.replace('/rest/sound/btn_se', '/profile/content/setting')
// 数据接口
if (apiHostNames.includes(hostname)) {
if (pathname === '/rest/sound/btn_se') {
const user = users.get(query.uid)
if (user && !user.name) {
newRequestOptions.path = newRequestOptions.path.replace('/rest/sound/btn_se', '/profile/content/setting')
}
}
}

// 静态文件
let toLocal = false
if (staticHostNames.includes(hostname)) {
const newPathname = pathname.replace('/assets_en/', '/assets/')
if (CONFIG.staticServer && staticMap.has(newPathname)) {
toLocal = true
newRequestOptions.hostname = '127.0.0.1'
newRequestOptions.port = CONFIG.staticPort
newRequestOptions.path = newRequestOptions.path.replace(pathname , `/${staticMap.get(newPathname)}`)
}
}

if (CONFIG.frontAgent && !toLocal) {
newRequestOptions.hostname = CONFIG.frontAgentHost
newRequestOptions.port = CONFIG.frontAgentPort
newRequestOptions.path = requestDetail.url
}

return requestDetail
},
async beforeSendResponse(requestDetail, responseDetail) {
Expand Down
9 changes: 7 additions & 2 deletions renderer/form-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ Vue.component('form-config', {
methods: {
onSubmit () {
this.bthLoading = true
if (this.configForm.port === this.configForm.webPort) {
this.configForm.webPort = this.configForm.port + 1
const { port, webPort, staticPort } = this.configForm
if (port === webPort || port === staticPort || webPort === staticPort) {
this.configForm.webPort = port + 1
this.configForm.staticPort = port + 2
}
saveConfig(this.configForm)
ipcRenderer.send('update-config', this.configForm)
Expand Down Expand Up @@ -71,6 +73,9 @@ Vue.component('form-config', {
<el-form-item label="监控页端口">
<el-input-number v-model="configForm.webPort" :min="0" :max="65535"></el-input-number>
</el-form-item>
<el-form-item label="静态文件端口">
<el-input-number v-model="configForm.staticPort" :min="0" :max="65535"></el-input-number>
</el-form-item>
<el-form-item label="前置代理">
<el-switch
v-model="configForm.frontAgent"
Expand Down
5 changes: 5 additions & 0 deletions renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require('./form-config')
Vue.use(ElementUI)

const CSV_FOLDER_PATH = path.resolve(remote.app.getPath('userData'), 'local/*')
const STATIC_FOLDER_PATH = path.resolve(remote.app.getPath('userData'), 'static/local/*')

let vueApp
ipcRenderer.on('config-data', (evt, data) => {
vueApp.port = data.port
Expand Down Expand Up @@ -42,6 +44,9 @@ vueApp = new Vue({
openCsvFolder () {
shell.showItemInFolder(CSV_FOLDER_PATH)
},
openStaticFolder () {
shell.showItemInFolder(STATIC_FOLDER_PATH)
},
openProxyWeb () {
shell.openExternal(`http://localhost:${this.webPort}`)
},
Expand Down
3 changes: 2 additions & 1 deletion store/scenarioState.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ glob('local/scenario/*.csv', { cwd: USER_DATA_PATH }, (err, files) => {

setTimeout(() => {
chokidar.watch('local/scenario/*.csv', {
cwd: USER_DATA_PATH
cwd: USER_DATA_PATH,
ignoreInitial: true
}).on('add', file => {
readInfo(file)
})
Expand Down
75 changes: 75 additions & 0 deletions store/staticMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const glob = require('glob')
const path = require('path')
const chokidar = require('chokidar')
const { STATIC_PATH } = require('../store')
const serve = require('serve')
const CONFIG = require('../config')
const fse = require('fs-extra')
const { app } = require('electron')

const staticMap = new Map()

try {
if (app) {
fse.copySync(path.resolve(__dirname, '../data/static/default/'), path.resolve(STATIC_PATH, 'default/'))
}
} catch (e) {
console.err(e)
}

const collectFiles = (type, once) => {
glob(`${type}/**/*`, {
cwd: STATIC_PATH,
nodir: true
}, (err, files) => {
if (!err) {
files.forEach((filePath) => {
const key = filePath.replace(type, '')
if (!staticMap.has(key)) {
staticMap.set(key, filePath)
}
})
}
if (!once && type !== 'default') collectFiles('default')
})
}

collectFiles('local')

const server = serve(STATIC_PATH, {
port: CONFIG.staticPort,
cors: true
})

const watchFile = (type) => {
chokidar.watch(`${type}/**/?*`, {
cwd: STATIC_PATH,
ignored: /(^|[\/\\])\../,
ignoreInitial: true
}).on('add', file => {
const key = file.replace(/\\/g, '/').replace(type, '')
staticMap.set(key, `${type}${key}`)
}).on('unlink', file => {
const key = file.replace(/\\/g, '/').replace(type, '')
if (staticMap.has(key)) {
staticMap.delete(key)
}
if (type === 'local') {
collectFiles(type, true)
}
}).on('error', error => console.error(`Watcher error: ${error}`))
}

fse.pathExists(path.resolve(STATIC_PATH, 'local'), (err, exists) => {
if (!exists) {
fse.copy(path.resolve(__dirname, '../assets/tips.png'), path.resolve(STATIC_PATH, 'local/assets/img/sp/quest/assist/parts/tips.png'))
staticMap.set('/assets/img/sp/quest/assist/parts/tips.png', 'local/assets/img/sp/quest/assist/parts/tips.png')
}
})

setTimeout(() => {
watchFile('local')
watchFile('default')
}, 3000)

module.exports = staticMap

0 comments on commit acd3e3b

Please sign in to comment.