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 BdsCore from "../index";
import * as BdsTypes from "../globalType"; import * as BdsTypes from "../globalType";
import { CronJob } from "cron";
const { const {
VERSION = "latest", VERSION = "latest",
@ -13,16 +14,52 @@ const {
ALLOW_COMMADS = "false", ALLOW_COMMADS = "false",
} = process.env; } = process.env;
if (!BdsTypes.PlatformArray.find(p => p === PLATFORM)) {
console.error(`Platform ${PLATFORM} is not supported.`);
process.exit(1);
}
(async () => { (async () => {
let versionDownloaded = "";
if (VERSION === "latest") { 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") { } else if (VERSION !== "latest") {
await BdsCore.DownloadServer.DownloadServer(PLATFORM as BdsTypes.Platform, VERSION); await BdsCore.DownloadServer.DownloadServer(PLATFORM as BdsTypes.Platform, VERSION);
} else { } else {
console.log("Invalid Version"); 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); const Server = await BdsCore.Server.Start(PLATFORM as BdsTypes.Platform);
Server.on("all", data => process.stdout.write(data)); 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 type Platform = "bedrock"|"java"|"pocketmine"|"spigot"|"dragonfly";
export const PlatformArray = ["bedrock", "java", "pocketmine", "spigot", "dragonfly"];

View File

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

View File

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

View File

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