Move files from Scripts/ to src/ and add 'on' to start server. #144

Merged
Sirherobrine23 merged 8 commits from Changes-and-move-Scripts-dir into main 2021-08-02 22:33:21 +00:00
Showing only changes of commit 4767ffb4a4 - Show all commits

View File

@ -1,4 +1,4 @@
const { exec, execFile } = require("child_process"); const child_process = require("child_process");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const { resolve, join } = require("path"); const { resolve, join } = require("path");
@ -15,35 +15,71 @@ const BdsInfo = require("../BdsManegerInfo.json");
// Set bdsexec functions // Set bdsexec functions
global.BdsExecs = {}; global.BdsExecs = {};
function saveLog(data = "", LogFile = ""){ const UpdateUserJSON = function (Object = Object){}
fs.appendFileSync(path.join(GetPaths("log"), "latest.log"), data)
fs.appendFileSync(LogFile, data) const Player_Actions = function (data = "aaaaaa\n\n\naa", action = "connect", callback = Function){
const Current_platorm = GetPlatform();
if (Current_platorm === "bedrock") {
// "[INFO] Player connected: Sirherobrine, xuid: 2535413418839840",
// "[INFO] Player disconnected: Sirherobrine, xuid: 2535413418839840",
let newdata = data.split(/\n|\r/gi).map(line => {
if (line.includes("connected:")) {
let SplitLine = line.replace(/\[INFO\]\s+Player/, "").trim().split(/\s+/gi);
// player
let Player = line.trim().replace(/disconnected\:|connected\:/, "").trim().split(/,\s+xuid\:/).filter(a=>a).map(a=>a.trim()).filter(a=>a);
// Object Map
const ObjectReturn = {
Player: Player[0],
Action: `${(()=>{if (SplitLine[0].trim() === "connected:") return "connect"; else if (SplitLine[0].trim() === "disconnected") return "disconect";})()}`,
xuid: Player[1] || null,
Date: new Date(),
}
// Return
return ObjectReturn;
} else return false;
}).filter(a=>a);
}
} }
function start() { function start() {
if (BdsDetect()){ if (BdsDetect()){let ErrorReturn = "You already have a server running"; console.warn(ErrorReturn); throw new Error(ErrorReturn);}
let ErrorReturn = "You already have a server running";
console.warn(ErrorReturn); const SetupCommands = {
throw new Error(ErrorReturn); command: String,
args: [],
cwd: String,
env: process.env,
} }
var ServerExec;
// Minecraft Bedrock Oficial // Minecraft Bedrock Oficial
if (GetPlatform() === "bedrock"){ if (GetPlatform() === "bedrock"){
// Check Darwin Platform
if (process.platform === "darwin") throw new Error("Use a imagem Docker"); if (process.platform === "darwin") throw new Error("Use a imagem Docker");
else if (process.platform == "win32") ServerExec = execFile("bedrock_server.exe", {
cwd: GetServerPaths("bedrock"), // Windows Platform
}); else if (process.platform === "win32") {
else if (process.platform == "linux"){ SetupCommands.command = "bedrock_server.exe";
SetupCommands.cwd = GetServerPaths("bedrock")
}
// Linux Platform
else if (process.platform === "linux"){
// Set Executable file // Set Executable file
execFile("chmod 777 bedrock_server", {cwd: GetServerPaths("bedrock")}); try {child_process.execSync("chmod 777 bedrock_server", {cwd: GetServerPaths("bedrock")});} catch (error) {console.log(error);}
var BedrockCommand = "./bedrock_server";
// Emulation of the x86_64 architecture // Set Env and Cwd
if (commandExists("qemu-x86_64-static") && process.arch !== "x64") BedrockCommand = "qemu-x86_64-static "+BedrockCommand; SetupCommands.cwd = GetServerPaths("bedrock");
SetupCommands.env.LD_LIBRARY_PATH = GetServerPaths("bedrock");
// Start Bedrock Server
ServerExec = exec(BedrockCommand, {cwd: GetServerPaths("bedrock"), env: {...process.env, LD_LIBRARY_PATH: GetServerPaths("bedrock")}}) // In case the cpu is different from x64, the command will use qemu static to run the server
if (process.arch !== "x64") {
if (!(commandExists("qemu-x86_64-static"))) throw new Error("Install qemu static")
SetupCommands.command = "qemu-x86_64-static"
SetupCommands.args.push("./bedrock_server");
} else SetupCommands.command = "./bedrock_server";
} else throw new Error("your system does not support Minecraft Bedrock (yet)") } else throw new Error("your system does not support Minecraft Bedrock (yet)")
} }
@ -53,86 +89,85 @@ function start() {
// Checking if java is installed on the device // Checking if java is installed on the device
if (commandExists("java")) { if (commandExists("java")) {
ServerExec = execFile("java", [ SetupCommands.cwd = GetServerPaths("java");
"-jar", SetupCommands.command = "java";
`-Xms${JavaConfig.ram_mb}M`, SetupCommands.args.push("-jar", `-Xms${JavaConfig.ram_mb}M`, `-Xmx${JavaConfig.ram_mb}M`, "MinecraftServerJava.jar", "nogui");
`-Xmx${JavaConfig.ram_mb}M`, } else {require("open")(bds.package_json.docs_base + "Java-Download#windows"); throw new Error(`Open: ${bds.package_json.docs_base + "Java-Download#windows"}`)}
"MinecraftServerJava.jar", }
"nogui"
], {cwd: GetServerPaths("java")}) // Minecraft Bedrock (Pocketmine-MP)
} else { else if (GetPlatform() === "pocketmine") {
var url = bds.package_json.docs_base; if (bds.system == "windows") url += "Java-Download#windows"; else if (bds.system === "linux") url = "Java-Download#linux"; else if (process.platform === "darwin") url = "Java-Download#macos"; else url = "Java-Download";
require("open")(url);
throw new Error(`Open: ${url}`)
}
} else if (GetPlatform() === "pocketmine") {
// Start PocketMine-MP // Start PocketMine-MP
const php_bin_path = join(resolve(GetServerPaths("pocketmine"), "bin", "php7", "bin"), "php"); SetupCommands.command = join(resolve(GetServerPaths("pocketmine"), "bin", "php7", "bin"), "php");
ServerExec = execFile(php_bin_path, [ SetupCommands.args.push("./PocketMine-MP.phar");
"./PocketMine-MP.phar" SetupCommands.cwd = GetServerPaths("pocketmine");
], { }
cwd: GetServerPaths("pocketmine")
}) // Minecraft Bedrock (JSPrismarine)
} else if (GetPlatform() === "jsprismarine") { else if (GetPlatform() === "jsprismarine") {
// Start JSPrismarine // Start JSPrismarine
ServerExec = execFile("node", [ SetupCommands.command = "node";
"./packages/server/dist/Server.js" SetupCommands.args.push("./packages/server/dist/Server.js");
], { SetupCommands.cwd = GetServerPaths("jsprismarine");
cwd: GetServerPaths("jsprismarine")
});
} else throw Error("Bds Config Error") } else throw Error("Bds Config Error")
// Setup commands
const ServerExec = child_process.execFile(SetupCommands.command, SetupCommands.args, {
cwd: SetupCommands.cwd,
env: SetupCommands.env
});
// Post Start // Post Start
if (GetPlatform() === "java") { if (GetPlatform() === "java") {
ServerExec.stdout.on("data", function(data){ ServerExec.stdout.on("data", function(data){
if (data.includes("agree")) if (data.includes("agree") && data.includes("EULA"))
if (data.includes("EULA")){ const eula_file = path.join(GetServerPaths("java"), "eula.txt");
const eula_file = path.join(GetServerPaths("java"), "eula.txt"); fs.writeFileSync(eula_file, fs.readFileSync(eula_file, "utf8").split("eula=false").join("eula=true"));
fs.writeFileSync(eula_file, fs.readFileSync(eula_file, "utf8").split("eula=false").join("eula=true")); throw new Error("Restart application/CLI")
throw new Error("Restart application/CLI")
}
}); });
} }
// Log file // Log file
const LogFile = join(GetPaths("log"), `${bds.date()}_${GetPlatform()}_Bds_log.log`);
// save User in Json
ServerExec.stdout.on("data", data => saveUser(data))
ServerExec.stderr.on("data", data => saveUser(data))
// ---------------------------------------------------
// Clear latest.log
fs.writeFileSync(path.join(GetPaths("log"), "latest.log"), "")
// --------------------------------------------------- const LogFile = join(GetPaths("log"), `${bds.date()}_${GetPlatform()}_Bds_log.log`);
// stdout const LatestLog_Path = path.join(GetPaths("log"), "latest.log");
ServerExec.stdout.on("data", a=>saveLog(a, LogFile)); const LogSaveFunction = data => {
ServerExec.stderr.on("data", a=>saveLog(a, LogFile)); fs.appendFileSync(LogFile, data);
// --------------------------------------------------- fs.appendFileSync(LatestLog_Path, data);
return data;
}
fs.writeFileSync(LatestLog_Path, "");
// save User in Json
ServerExec.stdout.on("data", data => saveUser(data)); ServerExec.stderr.on("data", data => saveUser(data));
// Log File
ServerExec.stdout.on("data", LogSaveFunction); ServerExec.stderr.on("data", LogSaveFunction);
// Global and Run // Global and Run
global.bds_log_string = "" global.bds_log_string = ""
ServerExec.stdout.on("data", function(data){ ServerExec.stdout.on("data", data => {if (global.bds_log_string) global.bds_log_string = data; else global.bds_log_string += data});
if (global.bds_log_string === undefined || global.bds_log_string === "") global.bds_log_string = data; else global.bds_log_string += data
});
global.bds_server_string = ServerExec;
// Functions return
const returnFuntion = { const returnFuntion = {
uuid: randomUUID(), uuid: randomUUID(),
exec: ServerExec,
stop: function (){ stop: function (){
ServerExec.stdin.write(BdsInfo.Servers[GetPlatform()].stop); ServerExec.stdin.write(BdsInfo.Servers[GetPlatform()].stop);
return BdsInfo.Servers[GetPlatform()].stop; return BdsInfo.Servers[GetPlatform()].stop;
}, },
command: function (command = "list", callback){ command: async function (command = "list", callback = data => console.log(data)){
const oldLog = global.bds_log_string; return new Promise((resolve, reject) => {
ServerExec.stdin.write(`${command}\n`); ServerExec.stdin.write(`${command}\n`);
if (typeof callback === "function") { if (typeof callback === "function") {
setTimeout(() => { const TempLog = []
// Run commands from command run in server; const ControlTempHost = data => {TempLog.push(data); return data;}
const log = global.bds_log_string.replace(oldLog, "").split(/\r/).filter(data => {if (data === "") return false; else return true;}).join("\n") ServerExec.stdout.on(data, data => ControlTempHost(data));
if (log.length >= 1) callback(log); else callback("no log") ServerExec.stderr.on(data, data => ControlTempHost(data));
}, 1555); setTimeout(() => {
} callback(TempLog.join("\n"));
resolve(TempLog.join("\n"));
}, 2500);
}
});
}, },
log: function (logCallback = function(data = ""){data.split("\n").filter(d=>{return (d !== "")}).forEach(l=>console.log(l))}){ log: function (logCallback = function(data = ""){data.split("\n").filter(d=>{return (d !== "")}).forEach(l=>console.log(l))}){
if (typeof logCallback !== "function") { if (typeof logCallback !== "function") {
@ -144,7 +179,8 @@ function start() {
}, },
exit: function (exitCallback = process.exit){if ( exit: function (exitCallback = process.exit){if (
typeof exitCallback === "function") ServerExec.on("exit", code => exitCallback(code)); typeof exitCallback === "function") ServerExec.on("exit", code => exitCallback(code));
} },
on: function(action = String, callback = Function) {}
} }
ServerExec.on("exit", ()=>{delete global.BdsExecs[returnFuntion.uuid]}); ServerExec.on("exit", ()=>{delete global.BdsExecs[returnFuntion.uuid]});
global.BdsExecs[returnFuntion.uuid] = returnFuntion; global.BdsExecs[returnFuntion.uuid] = returnFuntion;