Reewrite project #430
@ -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,19 +29,6 @@ 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() {
|
|
||||||
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[] = [
|
const serverConfig: actionConfig[] = [
|
||||||
{
|
{
|
||||||
name: "portListening",
|
name: "portListening",
|
||||||
@ -49,17 +36,14 @@ export async function startServer() {
|
|||||||
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())
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -91,6 +75,19 @@ export async function startServer() {
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
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);
|
||||||
return {serverProcess, serverActions};
|
return {serverProcess, serverActions};
|
||||||
|
@ -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
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * as bedrock from "./bedrock";
|
export * as bedrock from "./bedrock";
|
||||||
|
export * as java from "./java";
|
49
src/java.ts
Normal file
49
src/java.ts
Normal 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};
|
||||||
|
}
|
Reference in New Issue
Block a user