-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from obstudio/develop
Version 0.2.0
- Loading branch information
Showing
7 changed files
with
185 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
node_modules/ | ||
config.json | ||
locale/zh-cn/custom.json | ||
locale/en-us/custom.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,86 @@ | ||
const fs = require('fs') | ||
#!/usr/bin/env node | ||
|
||
const Iconv = require('iconv').Iconv | ||
const config = require('./config') | ||
const parse = require('./parse') | ||
const send = require('./send') | ||
const os = require('os') | ||
const process = require('process') | ||
const child_process = require('child_process') | ||
const config = require(process.cwd() + '/config') | ||
|
||
const OFFLINE_TIMEOUT = (config.offlineTimeout || 0) * 1000 | ||
|
||
const gbk2utf8 = new Iconv('GBK', 'UTF-8') | ||
const offlinePlayers = new Set() | ||
|
||
fs.watchFile(config.logFile, (curr, prev) => { | ||
if (curr.size - prev.size > 0) { | ||
fs.open(config.logFile, 'r', (err, fd) => { | ||
if (err) throw err | ||
const isWindows = os.type() === 'Windows_NT' | ||
|
||
buffer = Buffer.alloc(curr.size - prev.size) | ||
fs.read(fd, buffer, 0, curr.size - prev.size, prev.size, (err, bytesRead, buffer) => { | ||
if (err) throw err | ||
let serverProcess | ||
|
||
let content | ||
if (os.type() === 'Windows_NT') { | ||
content = gbk2utf8.convert(buffer).toString().split('\r\n').filter(s => s) | ||
} else { | ||
content = buffer.toString().split('\n').filter(s => s) | ||
let autoRestart = config.autoRestart | ||
function newServerProcess() { | ||
return child_process.execFile(config.serverStart, { encoding: 'buffer' }, (error) => { | ||
if (error) { | ||
throw error | ||
} | ||
serverProcessStopped() | ||
}) | ||
} | ||
|
||
function serverProcessInit() { | ||
//when serverProcess have output message | ||
serverProcess.stdout.on('data', (data) => { | ||
const content = isWindows ? gbk2utf8.convert(data).toString().trim() : data.toString().trim() | ||
if (content) { | ||
console.log(content) | ||
} | ||
const info = parse(content) | ||
if (info) { | ||
if (info.type === 'leave' && OFFLINE_TIMEOUT) { | ||
offlinePlayers.add(info.target) | ||
setTimeout(() => { | ||
if (offlinePlayers.delete(info.target)) { | ||
try { | ||
send(info.message) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
}, OFFLINE_TIMEOUT) | ||
} else { | ||
if (info.type === 'join') { | ||
if (offlinePlayers.delete(info.target)) return | ||
} | ||
try { | ||
send(info.message) | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
info = content.map(parse).filter(s => s) | ||
info.forEach(send) | ||
}) | ||
//send the parent process input to serverProcess input | ||
process.stdin.pipe(serverProcess.stdin) | ||
} | ||
|
||
fs.close(fd, (err) => { | ||
if (err) throw err | ||
}) | ||
}) | ||
function serverProcessStopped() { | ||
if (autoRestart) { | ||
console.log('Server is restarting.') | ||
serverProcess = newServerProcess() | ||
serverProcessInit() | ||
} else { | ||
console.log('MC-Repeater stopped.') | ||
process.exit() | ||
} | ||
}) | ||
} | ||
|
||
process.stdin.on('data', (data) => { | ||
if (data.toString().trim() === 'stop') { | ||
autoRestart = false | ||
} | ||
}) | ||
|
||
//create mc server child process | ||
serverProcess = newServerProcess() | ||
serverProcessInit() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.