Reewrite project #430

Merged
Sirherobrine23 merged 13 commits from reewriteProject into main 2022-08-31 10:03:14 +00:00
4 changed files with 102 additions and 55 deletions
Showing only changes of commit a19ba92c80 - Show all commits

View File

@ -1,11 +1,11 @@
import * as path from "node:path"; import * as path from "node:path";
import * as fsOld from 'node:fs'; import * as fsOld from "node:fs";
import * as fs from 'node:fs/promises'; import * as fs from "node:fs/promises";
import { promisify } from "node:util";
import { getBedrockZip } from "@the-bds-maneger/server_versions"; import { getBedrockZip } from "@the-bds-maneger/server_versions";
import admZip from "adm-zip"; import admZip from "adm-zip";
import { exec, execAsync } from './childPromisses'; import { exec, execAsync } from "./childPromisses";
import { serverRoot } from "./pathControl"; import { serverRoot } from "./pathControl";
import { promisify } from 'node:util';
import { actions, actionConfig } from "./globalPlatfroms" import { actions, actionConfig } from "./globalPlatfroms"
export const serverPath = path.join(serverRoot, "Bedrock") export const serverPath = path.join(serverRoot, "Bedrock")
@ -29,37 +29,21 @@ export async function installServer(version: string|boolean) {
if (serverConfig) await fs.writeFile(path.join(serverPath, "server.properties"), serverConfig); if (serverConfig) await fs.writeFile(path.join(serverPath, "server.properties"), serverConfig);
} }
export async function startServer() { const serverConfig: actionConfig[] = [
if (!fsOld.existsSync(serverPath)) throw new Error("Install server fist");
let command = path.join(serverPath, "bedrock_server");
const args: string[] = [];
if (process.platform === "linux") {
if (process.arch !== "x64") {
args.push(command);
if (await execAsync("command -v qemu-x86_64-static").then(() => true).catch(() => false)) command = "qemu-x86_64-static";
else if (await execAsync("command -v box64").then(() => true).catch(() => false)) command = "box64";
else throw new Error("Cannot emulate x64 architecture. Check the documentents in \"https://github.com/The-Bds-Maneger/Bds-Maneger-Core/wiki/Server-Platforms#minecraft-bedrock-server-alpha\"");
}
}
let serverStarted = false;
const serverConfig: actionConfig[] = [
{ {
name: "portListening", name: "portListening",
callback(data, done) { callback(data, done) {
const match = data.match(portListen); const match = data.match(portListen);
if (!match) return; if (!match) return;
const [, protocol, port] = match; const [, protocol, port] = match;
done({protocol: protocol?.trim() === "IPv4" ? "IPv4" : protocol?.trim() === "IPv6" ? "IPv6" : "Unknown", port: parseInt(port)}); done({port: parseInt(port), type: "UDP", host: "127.0.0.1", protocol: protocol?.trim() === "IPv4" ? "IPv4" : protocol?.trim() === "IPv6" ? "IPv6" : "Unknown"});
} }
}, },
{ {
name: "serverStarted", name: "serverStarted",
callback(data, done) { callback(data, done) {
const resulter = data.match(started); const resulter = data.match(started);
if (!serverStarted && resulter) { if (resulter) done(new Date());
serverStarted = true;
done(new Date())
}
}, },
}, },
{ {
@ -89,7 +73,20 @@ export async function startServer() {
if (!(action === "disconnect" || action === "connect")) done({connectTime: new Date(), playerName: playerName, xuid}); if (!(action === "disconnect" || action === "connect")) done({connectTime: new Date(), playerName: playerName, xuid});
} }
}, },
]; ];
export async function startServer() {
if (!fsOld.existsSync(serverPath)) throw new Error("Install server fist");
let command = path.join(serverPath, "bedrock_server");
const args: string[] = [];
if (process.platform === "linux") {
if (process.arch !== "x64") {
args.push(command);
if (await execAsync("command -v qemu-x86_64-static").then(() => true).catch(() => false)) command = "qemu-x86_64-static";
else if (await execAsync("command -v box64").then(() => true).catch(() => false)) command = "box64";
else throw new Error("Cannot emulate x64 architecture. Check the documentents in \"https://github.com/The-Bds-Maneger/Bds-Maneger-Core/wiki/Server-Platforms#minecraft-bedrock-server-alpha\"");
}
}
const serverProcess = exec(command, args, {cwd: serverPath, maxBuffer: Infinity, env: {LD_LIBRARY_PATH: process.platform !== "win32" ? serverPath:undefined}}); const serverProcess = exec(command, args, {cwd: serverPath, maxBuffer: Infinity, env: {LD_LIBRARY_PATH: process.platform !== "win32" ? serverPath:undefined}});
const serverActions = new actions(serverProcess, serverConfig); const serverActions = new actions(serverProcess, serverConfig);

View File

@ -7,7 +7,7 @@ export type actionsPlayer = {
callback: (data: string, done: (player: playerBase) => void) => void callback: (data: string, done: (player: playerBase) => void) => void
} }
export type portListen = {port: number, protocol: "IPv4"|"IPv6"|"IPV4/IPv6"|"Unknown"}; export type portListen = {port: number, host?: string, type: "TCP"|"UDP"|"TCP/UDP", protocol: "IPv4"|"IPv6"|"IPV4/IPv6"|"Unknown"};
export type actionsPort = { export type actionsPort = {
name: "portListening", name: "portListening",
callback: (data: string, done: (portInfo: portListen) => void) => void callback: (data: string, done: (portInfo: portListen) => void) => void

View File

@ -1 +1,2 @@
export * as bedrock from "./bedrock"; export * as bedrock from "./bedrock";
export * as java from "./java";

49
src/java.ts Normal file
View File

@ -0,0 +1,49 @@
import * as path from "node:path";
import * as fs from "node:fs/promises";
import * as fsOld from "node:fs";
import { getJavaJar } from "@the-bds-maneger/server_versions";
import { serverRoot } from "./pathControl";
import { exec } from "./childPromisses";
import { actions, actionConfig } from './globalPlatfroms';
export const serverPath = path.join(serverRoot, "java");
const jarPath = path.join(serverPath, "server.jar");
export const started = /\[.*\].*\s+Done\s+\(.*\)\!.*/;
export const portListen = /Starting\s+Minecraft\s+server\s+on\s+(.*)\:(\d+)/;
export async function installServer(version: string|boolean) {
if (!fsOld.existsSync(serverPath)) await fs.mkdir(serverPath, {recursive: true});
await fs.writeFile(jarPath, await getJavaJar(version));
}
const serverConfig: actionConfig[] = [
{
name: "serverStarted",
callback(data, done) {
// [22:35:26] [Server thread/INFO]: Done (6.249s)! For help, type "help"
if (started.test(data)) done(new Date());
}
},
{
name: "portListening",
callback(data, done) {
const portParse = data.match(portListen);
if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",});
}
}
];
export async function startServer(Config?: {maxMemory?: number, minMemory?: number}) {
if (!fsOld.existsSync(jarPath)) throw new Error("Install server fist.");
const command = "java";
const args = ["-jar"];
if (Config) {
if (Config?.minMemory) args.push(`-Xms${Config?.minMemory}m`);
if (Config?.maxMemory) args.push(`-Xmx${Config?.maxMemory}m`);
}
args.push(jarPath);
const serverProcess = exec(command, args, {cwd: serverPath, maxBuffer: Infinity});
const serverActions = new actions(serverProcess, serverConfig);
return {serverProcess, serverActions};
}