Reewrite project #430

Merged
Sirherobrine23 merged 13 commits from reewriteProject into main 2022-08-31 10:03:14 +00:00
8 changed files with 64 additions and 25 deletions
Showing only changes of commit 6cc03ccd44 - Show all commits

25
.vscode/launch.json vendored
View File

@ -1,16 +1,21 @@
{ {
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "Bds Test",
"type": "node", "type": "node",
"request": "launch", "name": "Mocha Tests",
"runtimeExecutable": "node", "program": "${workspaceFolder}/node_modules/mocha/bin/mocha.js",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"args": ["testProject.ts"],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart", "internalConsoleOptions": "openOnSessionStart",
"skipFiles": ["<node_internals>/**", "node_modules/**"] "request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"args": [
"-r", "ts-node/register",
"--colors",
"${workspaceFolder}/tests/**/*.ts"
],
} }
]
]
} }

View File

@ -73,6 +73,10 @@ const serverConfig: actionConfig[] = [
if (!(action === "disconnect" || action === "connect")) done({connectTime: new Date(), playerName: playerName, xuid}); if (!(action === "disconnect" || action === "connect")) done({connectTime: new Date(), playerName: playerName, xuid});
} }
}, },
{
name: "serverStop",
run: (child) => child.writeStdin("stop")
}
]; ];
export async function startServer() { export async function startServer() {

View File

@ -70,17 +70,21 @@ export class customChild {
child.stderr.on("data", data => this.eventMiter.emit("stderrRaw", data instanceof Buffer ? data.toString("utf8"):data)); child.stderr.on("data", data => this.eventMiter.emit("stderrRaw", data instanceof Buffer ? data.toString("utf8"):data));
// Storage tmp lines // Storage tmp lines
const parseLog = (to: "breakStdout"|"breakStderr", data: string): any => { const parseLog = (to: "breakStdout"|"breakStderr", data: string): any => {
if (this.tempLog[to] === undefined) this.tempLog[to] = "";
const lines = data.split(/\r?\n/); const lines = data.split(/\r?\n/);
if (lines.length === 1) { if (lines.length === 1) return this.tempLog[to] += lines[0];
if (this.tempLog[to] === undefined) this.tempLog[to] = "";
return this.tempLog[to] += lines[0];
}
const a = lines.pop(); const a = lines.pop();
if (a !== "") lines.push(a); if (a !== "") lines.push(a);
for (const line of lines) { for (const line of lines) {
if (!this.tempLog[to]) return this.eventMiter.emit(to, line); if (!this.tempLog[to]) {
this.eventMiter.emit(to, this.tempLog[to]+line); // console.log(this.tempLog, lines);
delete this.tempLog[to]; 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)); child.stdout.on("data", data => parseLog("breakStdout", data));

View File

@ -35,7 +35,7 @@ export type actionCallback = actionsPlayer|actionsPort|actionsServerStarted|acti
export type actionConfig = actionCallback|actionRun; export type actionConfig = actionCallback|actionRun;
export class actions { export class actions {
private events = new EventEmitter({captureRejections: false}); private events = new EventEmitter({captureRejections: false});
private childProcess: customChild; public childProcess: customChild;
private stopServerFunction?: (childProcess: customChild) => void; private stopServerFunction?: (childProcess: customChild) => void;
private tpfunction?: (childProcess: customChild, x: number|string, y: number|string, z: number|string) => void; private tpfunction?: (childProcess: customChild, x: number|string, y: number|string, z: number|string) => void;

View File

@ -29,6 +29,10 @@ const serverConfig: actionConfig[] = [
const portParse = data.match(portListen); const portParse = data.match(portListen);
if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",}); if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",});
} }
},
{
name: "serverStop",
run: (child) => child.writeStdin("stop")
} }
]; ];

View File

@ -88,11 +88,26 @@ export async function installServer(version: string|boolean) {
await fs.writeFile(serverPhar, await getPocketminePhar(version)); 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 started = /\[.*\].*\s+Done\s+\(.*\)\!.*/;
export const player = /[.*]:\s+(.*)\s+(.*)\s+the\s+game/gi; export const player = /[.*]:\s+(.*)\s+(.*)\s+the\s+game/gi;
const serverConfig: actionConfig[] = [ 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", name: "serverStarted",
callback(data, done) { callback(data, done) {
@ -101,11 +116,8 @@ const serverConfig: actionConfig[] = [
} }
}, },
{ {
name: "portListening", name: "serverStop",
callback(data, done) { run: (child) => child.writeStdin("stop")
const portParse = data.match(portListen);
if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",});
}
} }
]; ];

View File

@ -29,6 +29,10 @@ const serverConfig: actionConfig[] = [
const portParse = data.match(portListen); const portParse = data.match(portListen);
if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",}); if (!!portParse) done({port: parseInt(portParse[2]), host: (portParse[1]||"").trim()||undefined, type: "TCP", protocol: "IPV4/IPv6",});
} }
},
{
name: "serverStop",
run: (child) => child.writeStdin("stop")
} }
]; ];

View File

@ -1,3 +1,5 @@
import { createWriteStream, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { installServer, startServer } from "../src/pocketmine"; import { installServer, startServer } from "../src/pocketmine";
describe("Pocketmine", () => { describe("Pocketmine", () => {
@ -6,6 +8,7 @@ describe("Pocketmine", () => {
await installServer("latest"); await installServer("latest");
const serverManeger = await startServer(); const serverManeger = await startServer();
serverManeger.on("log_stdout", console.log); serverManeger.on("log_stdout", console.log);
serverManeger.on("portListening", console.log);
serverManeger.on("log_stdout", data => { serverManeger.on("log_stdout", data => {
if(/set-up.*wizard/.test(data)) { if(/set-up.*wizard/.test(data)) {
serverManeger.runCommand("eng"); serverManeger.runCommand("eng");
@ -15,6 +18,9 @@ describe("Pocketmine", () => {
} }
}); });
serverManeger.on("serverStarted", () => serverManeger.stopServer()); 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)))); return new Promise((done, reject) => serverManeger.on("exit", ({code}) => code === 0?done():reject(new Error("Exit another code "+code))));
}); });
}); });