Reewrite project #430
19
.vscode/launch.json
vendored
19
.vscode/launch.json
vendored
@ -2,15 +2,20 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Bds Test",
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"runtimeExecutable": "node",
|
||||
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
|
||||
"args": ["testProject.ts"],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"name": "Mocha Tests",
|
||||
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js",
|
||||
"internalConsoleOptions": "openOnSessionStart",
|
||||
"skipFiles": ["<node_internals>/**", "node_modules/**"]
|
||||
"request": "launch",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"args": [
|
||||
"-r", "ts-node/register",
|
||||
"--colors",
|
||||
"${workspaceFolder}/tests/**/*.ts"
|
||||
],
|
||||
}
|
||||
|
||||
]
|
||||
}
|
@ -73,6 +73,10 @@ const serverConfig: actionConfig[] = [
|
||||
if (!(action === "disconnect" || action === "connect")) done({connectTime: new Date(), playerName: playerName, xuid});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "serverStop",
|
||||
run: (child) => child.writeStdin("stop")
|
||||
}
|
||||
];
|
||||
|
||||
export async function startServer() {
|
||||
|
@ -70,17 +70,21 @@ export class customChild {
|
||||
child.stderr.on("data", data => this.eventMiter.emit("stderrRaw", data instanceof Buffer ? data.toString("utf8"):data));
|
||||
// Storage tmp lines
|
||||
const parseLog = (to: "breakStdout"|"breakStderr", data: string): any => {
|
||||
const lines = data.split(/\r?\n/);
|
||||
if (lines.length === 1) {
|
||||
if (this.tempLog[to] === undefined) this.tempLog[to] = "";
|
||||
return this.tempLog[to] += lines[0];
|
||||
}
|
||||
const lines = data.split(/\r?\n/);
|
||||
if (lines.length === 1) return this.tempLog[to] += lines[0];
|
||||
const a = lines.pop();
|
||||
if (a !== "") lines.push(a);
|
||||
for (const line of lines) {
|
||||
if (!this.tempLog[to]) return this.eventMiter.emit(to, line);
|
||||
this.eventMiter.emit(to, this.tempLog[to]+line);
|
||||
delete this.tempLog[to];
|
||||
if (!this.tempLog[to]) {
|
||||
// console.log(this.tempLog, lines);
|
||||
this.eventMiter.emit(to, line);
|
||||
continue;
|
||||
}
|
||||
console.log(this.tempLog, lines);
|
||||
this.tempLog[to]+=line;
|
||||
this.eventMiter.emit(to, this.tempLog[to]);
|
||||
this.tempLog[to] = "";
|
||||
}
|
||||
}
|
||||
child.stdout.on("data", data => parseLog("breakStdout", data));
|
||||
|
@ -35,7 +35,7 @@ export type actionCallback = actionsPlayer|actionsPort|actionsServerStarted|acti
|
||||
export type actionConfig = actionCallback|actionRun;
|
||||
export class actions {
|
||||
private events = new EventEmitter({captureRejections: false});
|
||||
private childProcess: customChild;
|
||||
public childProcess: customChild;
|
||||
private stopServerFunction?: (childProcess: customChild) => void;
|
||||
private tpfunction?: (childProcess: customChild, x: number|string, y: number|string, z: number|string) => void;
|
||||
|
||||
|
@ -29,6 +29,10 @@ const serverConfig: actionConfig[] = [
|
||||
const portParse = data.match(portListen);
|
||||
if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "serverStop",
|
||||
run: (child) => child.writeStdin("stop")
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -88,11 +88,26 @@ export async function installServer(version: string|boolean) {
|
||||
await fs.writeFile(serverPhar, await getPocketminePhar(version));
|
||||
}
|
||||
|
||||
export const portListen = /\[.*\]:\s+Minecraft\s+network\s+interface\s+running\s+on\s+(.*)/;
|
||||
// [16:47:35.405] [Server thread/INFO]: Minecraft network interface running on 0.0.0.0:19132
|
||||
export const portListen = /\[.*\]:\s+Minecraft\s+network\s+interface\s+running\s+on\s+(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[A-Za-z0-9:]+|):([0-9]+))/;
|
||||
export const started = /\[.*\].*\s+Done\s+\(.*\)\!.*/;
|
||||
export const player = /[.*]:\s+(.*)\s+(.*)\s+the\s+game/gi;
|
||||
|
||||
const serverConfig: actionConfig[] = [
|
||||
{
|
||||
name: "portListening",
|
||||
callback(data, done) {
|
||||
const portParse = data.match(portListen);
|
||||
if (!portParse) return;
|
||||
const [,, host, port] = portParse;
|
||||
done({
|
||||
port: parseInt(port),
|
||||
host: host?.trim(),
|
||||
type: "UDP",
|
||||
protocol: "IPV4/IPv6"
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "serverStarted",
|
||||
callback(data, done) {
|
||||
@ -101,11 +116,8 @@ const serverConfig: actionConfig[] = [
|
||||
}
|
||||
},
|
||||
{
|
||||
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",});
|
||||
}
|
||||
name: "serverStop",
|
||||
run: (child) => child.writeStdin("stop")
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -29,6 +29,10 @@ const serverConfig: actionConfig[] = [
|
||||
const portParse = data.match(portListen);
|
||||
if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "serverStop",
|
||||
run: (child) => child.writeStdin("stop")
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
import { createWriteStream, writeFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { installServer, startServer } from "../src/pocketmine";
|
||||
|
||||
describe("Pocketmine", () => {
|
||||
@ -6,6 +8,7 @@ describe("Pocketmine", () => {
|
||||
await installServer("latest");
|
||||
const serverManeger = await startServer();
|
||||
serverManeger.on("log_stdout", console.log);
|
||||
serverManeger.on("portListening", console.log);
|
||||
serverManeger.on("log_stdout", data => {
|
||||
if(/set-up.*wizard/.test(data)) {
|
||||
serverManeger.runCommand("eng");
|
||||
@ -15,6 +18,9 @@ describe("Pocketmine", () => {
|
||||
}
|
||||
});
|
||||
serverManeger.on("serverStarted", () => serverManeger.stopServer());
|
||||
writeFileSync(resolve(__dirname, "../server.log"), "");
|
||||
const log = createWriteStream(resolve(__dirname, "../server.log"), {flags: "a"});
|
||||
serverManeger.childProcess.child?.stdout?.pipe(log);
|
||||
return new Promise((done, reject) => serverManeger.on("exit", ({code}) => code === 0?done():reject(new Error("Exit another code "+code))));
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user