Reewrite project #430

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

View File

@ -1,128 +0,0 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series
of actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within
the community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.

View File

@ -1,14 +0,0 @@
# Contributing to Core Core for server management and events
Read Code of Conduct fist!
[Bds Manager Core code of conduct](CODE_OF_CONDUCT.md)
## TL;DR
[Add Platform](https://github.com/The-Bds-Maneger/Bds-Maneger-Core/wiki/Add-new-Platform-to-Core)
Bds Manager Core accepts contributions provided that:
* Be a fork of the repository.
* have the latest repository modifications applied to the repository in the `main` branch.
* to add a platform to the project, maintaining scripts.

122
package-lock.json generated
View File

@ -15,7 +15,7 @@
"android"
],
"dependencies": {
"@the-bds-maneger/server_versions": "^2.3.1",
"@the-bds-maneger/server_versions": "^3.0.0",
"adm-zip": "^0.5.9",
"axios": "^0.27.2",
"cron": "^2.1.0",
@ -27,12 +27,12 @@
"@types/adm-zip": "^0.5.0",
"@types/cron": "^2.0.0",
"@types/fs-extra": "^9.0.13",
"@types/node": "^18.7.8",
"@types/node": "^18.7.13",
"@types/tar": "^6.1.2",
"nodemon": "^2.0.19",
"npm-run-all": "^4.1.5",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
"typescript": "^4.8.2"
},
"engines": {
"node": ">=16.0.0"
@ -76,17 +76,17 @@
}
},
"node_modules/@the-bds-maneger/server_versions": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/@the-bds-maneger/server_versions/-/server_versions-2.3.1.tgz",
"integrity": "sha512-QgiuWOU8b2Y1hrTG0Qxa0mj5CsCozZHK9wXeGSOqLinxF9xair+d831gULhupCgju+MgXt39OB1Lgztjdryw1w==",
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@the-bds-maneger/server_versions/-/server_versions-3.0.0.tgz",
"integrity": "sha512-+t2r7+iJHNbcF1A3o953Dcxm3LLN138tiC7CMWAa7IoJFhpqjYZcijlJEUZ4y6ueTE59yqtwninOtmRnhrqE/g==",
"dependencies": {
"axios": "^0.27.2",
"cors": "^2.8.5",
"express": "^4.18.1",
"jsdom": "^20.0.0",
"mongoose": "^6.2.10",
"typescript": "^4.6.3",
"yaml": "^2.0.1"
"mongoose": "^6.5.2",
"typescript": "^4.7.4",
"yaml": "^2.1.1"
}
},
"node_modules/@tootallnate/once": {
@ -1891,9 +1891,9 @@
}
},
"node_modules/mongoose": {
"version": "6.5.2",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-6.5.2.tgz",
"integrity": "sha512-3CFDrSLtK2qjM1pZeZpLTUyqPRkc11Iuh74ZrwS4IwEJ3K2PqGnmyPLw7ex4Kzu37ujIMp3MAuiBlUjfrcb6hw==",
"version": "6.5.3",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-6.5.3.tgz",
"integrity": "sha512-0L2ZOPzNQ7kcIgpdfpmVXc+/SypdhzcTlaHXYa983u1lrVp7/i3ekwHpPiTXxYBvV6FwBAsFoHI7+Ovf8tp3Mg==",
"dependencies": {
"bson": "^4.6.5",
"kareem": "2.4.1",
@ -2338,6 +2338,11 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/querystringify": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
},
"node_modules/range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -2416,6 +2421,11 @@
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/requires-port": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
"integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ=="
},
"node_modules/resolve": {
"version": "1.22.1",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.1.tgz",
@ -2669,9 +2679,9 @@
}
},
"node_modules/spdx-license-ids": {
"version": "3.0.11",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.11.tgz",
"integrity": "sha512-Ctl2BrFiM0X3MANYgj3CkygxhRmr9mi6xhejbdO960nF6EDJApTYpn0BQnDKlnNBULKiCN1n3w9EBkHK8ZWg+g==",
"version": "3.0.12",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.12.tgz",
"integrity": "sha512-rr+VVSXtRhO4OHbXUiAF7xW3Bo9DuuF6C5jH+q/x15j2jniycgKbxU09Hr0WqlSLUs4i4ltHGXqTe7VHclYWyA==",
"dev": true
},
"node_modules/statuses": {
@ -2822,22 +2832,23 @@
}
},
"node_modules/tough-cookie": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.0.0.tgz",
"integrity": "sha512-tHdtEpQCMrc1YLrMaqXXcj6AxhYi/xgit6mZu1+EDWUn+qhUf8wMQoFIy9NXuq23zAwtcB0t/MjACGR18pcRbg==",
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.2.tgz",
"integrity": "sha512-G9fqXWoYFZgTc2z8Q5zaHy/vJMjm+WV0AkAeHxVCQiEB1b+dGvWzFW6QV07cY5jQ5gRkeid2qIkzkxUnmoQZUQ==",
"dependencies": {
"psl": "^1.1.33",
"punycode": "^2.1.1",
"universalify": "^0.1.2"
"universalify": "^0.2.0",
"url-parse": "^1.5.3"
},
"engines": {
"node": ">=6"
}
},
"node_modules/tough-cookie/node_modules/universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==",
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz",
"integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg==",
"engines": {
"node": ">= 4.0.0"
}
@ -2985,6 +2996,15 @@
"punycode": "^2.1.0"
}
},
"node_modules/url-parse": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz",
"integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==",
"dependencies": {
"querystringify": "^2.1.1",
"requires-port": "^1.0.0"
}
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
@ -3216,17 +3236,17 @@
}
},
"@the-bds-maneger/server_versions": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/@the-bds-maneger/server_versions/-/server_versions-2.3.1.tgz",
"integrity": "sha512-QgiuWOU8b2Y1hrTG0Qxa0mj5CsCozZHK9wXeGSOqLinxF9xair+d831gULhupCgju+MgXt39OB1Lgztjdryw1w==",
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@the-bds-maneger/server_versions/-/server_versions-3.0.0.tgz",
"integrity": "sha512-+t2r7+iJHNbcF1A3o953Dcxm3LLN138tiC7CMWAa7IoJFhpqjYZcijlJEUZ4y6ueTE59yqtwninOtmRnhrqE/g==",
"requires": {
"axios": "^0.27.2",
"cors": "^2.8.5",
"express": "^4.18.1",
"jsdom": "^20.0.0",
"mongoose": "^6.2.10",
"typescript": "^4.6.3",
"yaml": "^2.0.1"
"mongoose": "^6.5.2",
"typescript": "^4.7.4",
"yaml": "^2.1.1"
}
},
"@tootallnate/once": {
@ -4558,9 +4578,9 @@
}
},
"mongoose": {
"version": "6.5.2",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-6.5.2.tgz",
"integrity": "sha512-3CFDrSLtK2qjM1pZeZpLTUyqPRkc11Iuh74ZrwS4IwEJ3K2PqGnmyPLw7ex4Kzu37ujIMp3MAuiBlUjfrcb6hw==",
"version": "6.5.3",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-6.5.3.tgz",
"integrity": "sha512-0L2ZOPzNQ7kcIgpdfpmVXc+/SypdhzcTlaHXYa983u1lrVp7/i3ekwHpPiTXxYBvV6FwBAsFoHI7+Ovf8tp3Mg==",
"requires": {
"bson": "^4.6.5",
"kareem": "2.4.1",
@ -4887,6 +4907,11 @@
"side-channel": "^1.0.4"
}
},
"querystringify": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
},
"range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
@ -4944,6 +4969,11 @@
"functions-have-names": "^1.2.2"
}
},
"requires-port": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
"integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ=="
},
"resolve": {
"version": "1.22.1",
"resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.1.tgz",
@ -5140,9 +5170,9 @@
}
},
"spdx-license-ids": {
"version": "3.0.11",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.11.tgz",
"integrity": "sha512-Ctl2BrFiM0X3MANYgj3CkygxhRmr9mi6xhejbdO960nF6EDJApTYpn0BQnDKlnNBULKiCN1n3w9EBkHK8ZWg+g==",
"version": "3.0.12",
"resolved": "https://registry.npmjs.org/spdx-license-ids/-/spdx-license-ids-3.0.12.tgz",
"integrity": "sha512-rr+VVSXtRhO4OHbXUiAF7xW3Bo9DuuF6C5jH+q/x15j2jniycgKbxU09Hr0WqlSLUs4i4ltHGXqTe7VHclYWyA==",
"dev": true
},
"statuses": {
@ -5254,19 +5284,20 @@
}
},
"tough-cookie": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.0.0.tgz",
"integrity": "sha512-tHdtEpQCMrc1YLrMaqXXcj6AxhYi/xgit6mZu1+EDWUn+qhUf8wMQoFIy9NXuq23zAwtcB0t/MjACGR18pcRbg==",
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.2.tgz",
"integrity": "sha512-G9fqXWoYFZgTc2z8Q5zaHy/vJMjm+WV0AkAeHxVCQiEB1b+dGvWzFW6QV07cY5jQ5gRkeid2qIkzkxUnmoQZUQ==",
"requires": {
"psl": "^1.1.33",
"punycode": "^2.1.1",
"universalify": "^0.1.2"
"universalify": "^0.2.0",
"url-parse": "^1.5.3"
},
"dependencies": {
"universalify": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz",
"integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg=="
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-0.2.0.tgz",
"integrity": "sha512-CJ1QgKmNg3CwvAv/kOFmtnEN05f0D/cn9QntgNOQlQF9dgvVTHj3t+8JPdjqawCHk7V/KA+fbUqzZ9XWhcqPUg=="
}
}
},
@ -5365,6 +5396,15 @@
"punycode": "^2.1.0"
}
},
"url-parse": {
"version": "1.5.10",
"resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz",
"integrity": "sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ==",
"requires": {
"querystringify": "^2.1.1",
"requires-port": "^1.0.0"
}
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",

View File

@ -60,7 +60,7 @@
"android"
],
"dependencies": {
"@the-bds-maneger/server_versions": "^2.3.1",
"@the-bds-maneger/server_versions": "^3.0.0",
"adm-zip": "^0.5.9",
"axios": "^0.27.2",
"cron": "^2.1.0",
@ -72,12 +72,12 @@
"@types/adm-zip": "^0.5.0",
"@types/cron": "^2.0.0",
"@types/fs-extra": "^9.0.13",
"@types/node": "^18.7.8",
"@types/node": "^18.7.13",
"@types/tar": "^6.1.2",
"nodemon": "^2.0.19",
"npm-run-all": "^4.1.5",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
"typescript": "^4.8.2"
},
"nodemonConfig": {
"delay": 2500,

43
src/Proprieties.ts Normal file
View File

@ -0,0 +1,43 @@
export default parse;
/**
* Parse Proprieties files and return a map of properties.
*
* @param Proper - String with the properties or similar files
* @returns
*/
export function parse(Proper: string): {[key: string]: string|number|true|false|null} {
const ProPri = {};
const ProperSplit = Proper.replace(/\r\n/g, "\n").replace(/\\\s+?\n/gi, "").split("\n").map(Line => Line.trim()).filter(line => /.*(\s+)?\=(\s+)?.*/.test(line) && !/^#/.test(line));
for (const Line of ProperSplit) {
const LineMatch = Line.match(/^([^\s\=]+)\s*\=(.*)$/);
const key = LineMatch[1].trim(), value = LineMatch[2].trim();
ProPri[key] = value;
if (ProPri[key] === "") ProPri[key] = null;
else if (ProPri[key] === "true") ProPri[key] = true;
else if (ProPri[key] === "false") ProPri[key] = false;
else if (/^[0-9]+\.[0-9]+/.test(ProPri[key]) && !/^[0-9]+\.[0-9]+\.[0-9]+/.test(ProPri[key])) ProPri[key] = parseFloat(ProPri[key]);
else if (/^[0-9]+/.test(ProPri[key])) ProPri[key] = parseInt(ProPri[key]);
}
return ProPri;
}
/**
* Convert json to properities files.
*
* @param ProPri - String with properties file
* @returns
*/
export function stringify(ProPri: {[key: string]: any}): string {
const Proper = [];
for (const key in Object.keys(ProPri)) {
if (ProPri[key] === null) Proper.push(`${key}=`);
else if (ProPri[key] === true) Proper.push(`${key}=true`);
else if (ProPri[key] === false) Proper.push(`${key}=false`);
else if (typeof ProPri[key] === "number") Proper.push(`${key}=${ProPri[key]}`);
else if (typeof ProPri[key] === "string") Proper.push(`${key}=${ProPri[key]}`);
else if (typeof ProPri[key] === "object") Proper.push(`${key}=${JSON.stringify(ProPri[key])}`);
else console.error(`[Proprieties.stringify] ${key} is not a valid type.`);
}
return Proper.join("\n");
}

View File

@ -1 +1,97 @@
import {} from "./childPromisses";
import * as path from "node:path";
import * as fsOld from 'node:fs';
import * as fs from 'node:fs/promises';
import { getBedrockZip } from "@the-bds-maneger/server_versions";
import admZip from "adm-zip";
import { exec, execAsync } from './childPromisses';
import { serverRoot } from "./pathControl";
import { promisify } from 'node:util';
import { actions, actionConfig } from "./globalPlatfroms"
export const serverPath = path.join(serverRoot, "Bedrock")
// RegExp
export const saveFf = /^(worlds|server\.properties|config|((permissions|allowlist|valid_known_packs)\.json)|(development_.*_packs))$/;
export const portListen = /(IPv[46])\s+supported,\s+port:\s+(.*)/;
export const started = /\[.*\]\s+Server\s+started\./;
export const player = /r\s+(.*)\:\s+(.*)\,\s+xuid\:\s+(.*)/;
export async function installServer(version: string|boolean) {
let arch = process.arch;
if (process.platform === "linux" && process.arch !== "x64") {
if (await execAsync("command -v qemu-x86_64-static").then(() => true).catch(() => false)||await execAsync("command -v box64").then(() => true).catch(() => false)) arch = "x64";
}
const zip = new admZip(await getBedrockZip(version, arch));
if (!fsOld.existsSync(serverPath)) await fs.mkdir(serverPath, {recursive: true});
// Remover files
for (const file of await fs.readdir(serverPath).then(files => files.filter(file => !saveFf.test(file)))) await fs.rm(path.join(serverPath, file), {recursive: true, force: true});
const serverConfig = (await fs.readFile(path.join(serverPath, "server.properties"), "utf8").catch(() => "")).trim();
await promisify(zip.extractAllToAsync)(serverPath, true, true);
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[] = [
{
name: "portListening",
callback(data, done) {
const match = data.match(portListen);
if (!match) return;
const [, protocol, port] = match;
done({protocol: protocol?.trim() === "IPv4" ? "IPv4" : protocol?.trim() === "IPv6" ? "IPv6" : "Unknown", port: parseInt(port)});
}
},
{
name: "serverStarted",
callback(data, done) {
const resulter = data.match(started);
if (!serverStarted && resulter) {
serverStarted = true;
done(new Date())
}
},
},
{
name: "playerConnect",
callback(data, done) {
const match = data.match(player);
if (!match) return;
const [, action, playerName, xuid] = match;
if (action === "connect") done({connectTime: new Date(), playerName: playerName, xuid});
}
},
{
name: "playerDisconnect",
callback(data, done) {
const match = data.match(player);
if (!match) return;
const [, action, playerName, xuid] = match;
if (action === "disconnect") done({connectTime: new Date(), playerName: playerName, xuid});
}
},
{
name: "playerUnknown",
callback(data, done) {
const match = data.match(player);
if (!match) return;
const [, action, playerName, xuid] = match;
if (!(action === "disconnect" || action === "connect")) done({connectTime: new Date(), playerName: playerName, xuid});
}
},
];
const serverProcess = exec(command, args, {cwd: serverPath, maxBuffer: Infinity, env: {LD_LIBRARY_PATH: process.platform !== "win32" ? serverPath:undefined}});
const serverActions = new actions(serverProcess, serverConfig);
return {serverProcess, serverActions};
}

View File

@ -1,9 +1,10 @@
import type { ObjectEncodingOptions } from "node:fs";
import { execFile, ExecFileOptions, ChildProcess } from "node:child_process";
import { execFile, exec as nodeExec, ExecFileOptions, ChildProcess } from "node:child_process";
import { EventEmitter } from "node:events";
import { promisify } from "node:util";
export const execFileAsync = promisify(execFile);
export {execFile};
export const execFileAsync = promisify(execFile);
export const execAsync = promisify(nodeExec)
export class customChild {
private eventMiter = new EventEmitter({captureRejections: false});
@ -11,6 +12,12 @@ export class customChild {
private child?: ChildProcess;
public kill(signal?: number|NodeJS.Signals) {if(this.child?.killed) return this.child?.killed;return this.child?.kill(signal);}
public writeStdin(command: string, args?: string[]) {
let toWrite = command;
if (args?.length > 0) toWrite += (" "+args.join(" "));
toWrite+="\n";
this.child.stdin.write(toWrite);
}
private emit(act: "error", data: Error): this;
private emit(act: "close", data: {code: number, signal: NodeJS.Signals}): this;
@ -60,10 +67,12 @@ export class customChild {
export function exec(command: string): customChild;
export function exec(command: string, args: string[]): customChild;
export function exec(command: string, options: ObjectEncodingOptions & ExecFileOptions): customChild;
export function exec(command: string, args: string[], options: ObjectEncodingOptions & ExecFileOptions): customChild;
export function exec(command: string, args?: ObjectEncodingOptions & ExecFileOptions|string[], options?: ObjectEncodingOptions & ExecFileOptions): customChild {
let childOptions: ObjectEncodingOptions & ExecFileOptions = {};
let childArgs: string[] = [];
if (args instanceof Object) childOptions = args as ObjectEncodingOptions & ExecFileOptions; else if (args instanceof Array) childArgs = args;
if (!options) childOptions = options;
if (childOptions?.env) childOptions.env = {...process.env, ...childOptions.env};
return new customChild(execFile(command, childArgs, childOptions));
}

40
src/globalPlatfroms.ts Normal file
View File

@ -0,0 +1,40 @@
import type { customChild } from "./childPromisses";
import { EventEmitter } from "node:events";
export type playerBase = {playerName: string, connectTime: Date, xuid?: string};
export type actionsPlayer = {
name: "playerConnect"|"playerDisconnect"|"playerUnknown",
callback: (data: string, done: (player: playerBase) => void) => void
}
export type portListen = {port: number, protocol: "IPv4"|"IPv6"|"IPV4/IPv6"|"Unknown"};
export type actionsPort = {
name: "portListening",
callback: (data: string, done: (portInfo: portListen) => void) => void
}
export type serverStarted = Date;
export type actionsServerStarted = {
name: "serverStarted",
callback: (data: string, done: (started: serverStarted) => void) => void
}
export type actionConfig = actionsPlayer|actionsPort|actionsServerStarted|actionsServerStarted;
export class actions {
private events = new EventEmitter({captureRejections: false});
on(act: "playerConnect"|"playerDisconnect"|"playerUnknown", fn: (data: playerBase) => void): this;
on(act: "portListening", fn: (data: portListen) => void): this;
on(act: "serverStarted", fn: (data: serverStarted) => void): this;
on(act: string, fn: (...args: any[]) => void) {this.events.on(act, fn); return this;}
once(act: "playerConnect"|"playerDisconnect"|"playerUnknown", fn: (data: playerBase) => void): this;
once(act: "portListening", fn: (data: portListen) => void): this;
once(act: "serverStarted", fn: (data: serverStarted) => void): this;
once(act: string, fn: (...args: any[]) => void) {this.events.once(act, fn); return this;}
constructor(child: customChild, config: actionConfig[]) {
child.on("breakStdout", data => config.forEach(fn => fn.callback(data, (...args) => this.events.emit(fn.name, ...args))));
child.on("breakStderr", data => config.forEach(fn => fn.callback(data, (...args) => this.events.emit(fn.name, ...args))));
}
}

16
src/pathControl.ts Normal file
View File

@ -0,0 +1,16 @@
import * as fs from "node:fs/promises";
import * as fsOld from "node:fs";
import * as path from "node:path";
import * as os from "node:os";
// bds Root
export const bdsRoot = process.env.BDS_HOME||path.join(os.homedir(), ".bdsManeger");
if (!fsOld.existsSync(bdsRoot)) fs.mkdir(bdsRoot, {recursive: true}).then(() => console.log("Bds Root created"));
// Server Folder
export const serverRoot = path.join(bdsRoot, "Servers");
if (!fsOld.existsSync(serverRoot)) fs.mkdir(serverRoot, {recursive: true});
// Bds backup
export const backupFolder = path.join(bdsRoot, "Backup");
if (!fsOld.existsSync(backupFolder)) fs.mkdir(backupFolder, {recursive: true});