MCSManager/core/Process/Mcserver.js
2017-11-18 11:34:41 +08:00

100 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const ServerProcess = require('./BaseMcserver');
const DataModel = require('../DataModel');
const os = require('os');
const properties = require("properties");
const fs = require('fs');
class MinecraftServer extends ServerProcess {
constructor(name, args) {
super(args);
//这是配置文件
this.dataModel = new DataModel('server/' + name);
let now = new Date().toLocaleString();
this.dataModel.name = name;
this.dataModel.createDate = now;
this.dataModel.lastDate = now;
this.properties = undefined;
this.terminalQueue = [];
this._tmpTerminalLog = [];
this.terminalPageCount = 0;
}
builder(args) {
this.dataModel.addCmd = args.addCmd || ['-server', '-Xincgc'];
this.dataModel.java = args.java || 'java';
this.dataModel.jarName = args.jarName;
this.dataModel.Xmx = args.Xmx;
this.dataModel.Xms = args.Xms;
//cwd 是服务端文件,不是控制面板需要的配置
this.dataModel.cwd = args.cwd || './server/' + this.dataModel.name + '/';
//自定义参数
let tmpCommandeStart = args.highCommande || "";
//自定义参数去掉所有两个空格
tmpCommandeStart = tmpCommandeStart.replace(/ /igm, ' ');
this.dataModel.highCommande = tmpCommandeStart;
this.propertiesLoad();
}
load() {
this.dataModel.load();
this.builder(this.dataModel);
}
save() {
this.dataModel.save();
}
propertiesLoad(callback) {
//配置读取
properties.parse(this.dataModel.cwd + '/server.properties', {
path: true
}, (err, obj) => {
//箭头函数this 并且这个不需要保存到配置文件所以不应该在datamodel
this.properties = obj;
callback && callback(this.properties, err);
});
}
propertiesSave(newProperties, callback) {
//解析
let text = properties.stringify(newProperties);
// 写入数据, 文件不存在会自动创建
fs.writeFile(this.dataModel.cwd + '/server.properties', text, (err) => {
this.propertiesLoad(() => {
callback && callback(this.properties, err);
});
});
}
terminalLog(strLine) {
this.terminalQueue.push(strLine);
if (this.terminalQueue.length > 512) {
this.terminalQueue = this.terminalQueue.slice(400);
}
}
getTerminalLog(print, size) {
if (print == undefined)
return this.terminalQueue || [];
let tmp = [];
for (let i = print; i > (print - size) && i >= 0; i--) {
tmp.unshift(this.terminalQueue[i]);
}
return tmp;
}
}
module.exports = MinecraftServer;