Rewrite in typescrypt #327

Merged
Sirherobrine23 merged 13 commits from re_Typescript into main 2022-03-28 21:43:14 +00:00
5 changed files with 79 additions and 17 deletions
Showing only changes of commit 93e9b64e19 - Show all commits

View File

@ -1,5 +1,6 @@
import * as BdsCore from "../index";
import * as BdsTypes from "../globalType";
import { CronJob } from "cron";
const {
VERSION = "latest",
@ -13,16 +14,52 @@ const {
ALLOW_COMMADS = "false",
} = process.env;
if (!BdsTypes.PlatformArray.find(p => p === PLATFORM)) {
console.error(`Platform ${PLATFORM} is not supported.`);
process.exit(1);
}
(async () => {
let versionDownloaded = "";
if (VERSION === "latest") {
await BdsCore.DownloadServer.DownloadServer(PLATFORM as BdsTypes.Platform, true);
const DownloadRes = await BdsCore.DownloadServer.DownloadServer(PLATFORM as BdsTypes.Platform, true);
versionDownloaded = DownloadRes.Version;
} else if (VERSION !== "latest") {
await BdsCore.DownloadServer.DownloadServer(PLATFORM as BdsTypes.Platform, VERSION);
} else {
console.log("Invalid Version");
}
BdsCore.serverConfig.createConfig(PLATFORM as BdsTypes.Platform, {
description: DESCRIPTION,
world: WORLD_NAME,
gamemode: GAMEMODE as any,
difficulty: DIFFICULTY as any,
players: parseInt(MAXPLAYERS),
require_login: REQUIRED_LOGIN === "true",
cheats_command: ALLOW_COMMADS === "true"
});
let lockExit = false;
const start = async () => {
const Server = await BdsCore.Server.Start(PLATFORM as BdsTypes.Platform);
Server.on("all", data => process.stdout.write(data));
Server.exit(process.exit);
Server.exit(code => {
if (lockExit) return;
process.exit(code);
});
return Server;
};
if (VERSION === "latest") {
let sessionStart = await start();
const cronUpdate = new CronJob("0 */1 * * * *", async () => {
const DownloadInfo = await BdsCore.DownloadServer.getVersions();
if (DownloadInfo.latest[PLATFORM as BdsTypes.Platform] === versionDownloaded) return;
lockExit = true;
await sessionStart.commands.stop();
await BdsCore.DownloadServer.DownloadServer(PLATFORM as BdsTypes.Platform, true);
sessionStart = await start();
});
cronUpdate.start();
} else await start();
})();

View File

@ -1 +1,2 @@
export type Platform = "bedrock"|"java"|"pocketmine"|"spigot"|"dragonfly";
export const PlatformArray = ["bedrock", "java", "pocketmine", "spigot", "dragonfly"];

View File

@ -25,6 +25,7 @@ type BdsSession = {
commands: {
tpPlayer: (username: string, x: number, y: number, z: number) => void;
execCommand: (command: string) => void;
stop: () => Promise<number|null>
}
};
@ -145,6 +146,15 @@ export async function Start(Platform: bdsTypes.Platform): Promise<BdsSession> {
return;
}
// Stop Server
const stopServer: () => Promise<number|null> = () => {
execCommand("stop");
return new Promise((accept, reject) => {
ServerProcess.on("exit", code => code === 0 ? accept(code) : reject(code));
setTimeout(() => accept(null), 1000);
})
}
// Return Session
const Seesion: BdsSession = {
id: crypto.randomUUID(),
@ -163,6 +173,7 @@ export async function Start(Platform: bdsTypes.Platform): Promise<BdsSession> {
await bdsBackup.CreateBackup(Platform);
if (Platform === "bedrock") execCommand("save resume");
});
ServerProcess.on("exit", () => cronJob.stop());
cronJob.start();
return;
},
@ -173,6 +184,7 @@ export async function Start(Platform: bdsTypes.Platform): Promise<BdsSession> {
commands: {
execCommand: execCommand,
tpPlayer: tpPlayer,
stop: stopServer
}
};
if (Platform === "bedrock") {
@ -186,5 +198,6 @@ export async function Start(Platform: bdsTypes.Platform): Promise<BdsSession> {
logStream.write(`[${StartDate.toISOString()}] Server started\n\n`);
onLog("all", data => logStream.write(data));
Sessions[Seesion.id] = Seesion;
ServerProcess.on("exit", () => {delete Sessions[Seesion.id];});
return Seesion;
}

View File

@ -46,21 +46,28 @@ export async function parseConfig(Platform: bdsType.Platform): Promise<BdsConfig
export type BdsConfigSet = {
world: string;
seed?: string;
description: string;
gamemode: "survival"|"creative"|"adventure"|"hardcore";
difficulty: "peaceful"|"easy"|"normal"|"hard";
players: number;
whitelist: true|false;
require_login: true|false;
cheats_command: true|false;
portv4: number;
portv6: number;
seed?: string;
players?: number;
whitelist?: true|false;
require_login?: true|false;
cheats_command?: true|false;
portv4?: number;
portv6?: number;
}
export async function createConfig(Platform: bdsType.Platform, config: BdsConfigSet): Promise<void> {
const serverPath = path.resolve(process.env.SERVER_PATH||path.join(os.homedir(), "bds_core/servers"), Platform);
if (Platform === "bedrock") {
if (!(config.seed && typeof config.seed === "string")) config.seed = "";
if (!(config.players && typeof config.players === "number")) config.players = 20;
if (!(config.whitelist && typeof config.whitelist === "boolean")) config.whitelist = false;
if (!(config.require_login && typeof config.require_login === "boolean")) config.require_login = false;
if (!(config.cheats_command && typeof config.cheats_command === "boolean")) config.cheats_command = false;
if (!(config.portv4 && typeof config.portv4 === "number")) config.portv4 = 19132;
if (!(config.portv6 && typeof config.portv6 === "number")) config.portv6 = 19133;
const bedrockConfigArray = [
"view-distance=32",
"tick-distance=4",
@ -95,6 +102,13 @@ export async function createConfig(Platform: bdsType.Platform, config: BdsConfig
fs.writeFileSync(path.join(serverPath, "server.properties"), bedrockConfig);
return;
} else if (Platform === "java") {
if (!(config.seed && typeof config.seed === "string")) config.seed = "";
if (!(config.players && typeof config.players === "number")) config.players = 20;
if (!(config.whitelist && typeof config.whitelist === "boolean")) config.whitelist = false;
if (!(config.require_login && typeof config.require_login === "boolean")) config.require_login = false;
if (!(config.cheats_command && typeof config.cheats_command === "boolean")) config.cheats_command = false;
if (!(config.portv4 && typeof config.portv4 === "number")) config.portv4 = 25565;
if (!(config.portv6 && typeof config.portv6 === "number")) config.portv6 = 255656;
const javaConfigArray = [
"query.port=65551",
"enable-jmx-monitoring=false",

View File

@ -1,8 +1,5 @@
{
"include": [
"src/",
"src/download_server.ts"
],
"include": ["src/"],
"exclude": ["node_modules/"],
"compilerOptions": {
"outDir": "./dist/cjs",