Fix: Modify template process

This commit is contained in:
Unitwk 2024-04-25 11:19:51 +08:00
parent 4b37194ee6
commit ee19316672
2 changed files with 24 additions and 20 deletions

View File

@ -89,24 +89,28 @@ export default class Instance extends EventEmitter {
this.startCount = 0;
}
isStoppedOrBusy() {
return [Instance.STATUS_STOP, Instance.STATUS_BUSY].includes(this.status());
}
// Pass in instance configuration, loosely and dynamically set configuration items for instance parameters
parameters(cfg: any, persistence = true) {
// If the instance type changes, default commands and lifecycle events must be reset
if (cfg?.type && cfg?.type != this.config.type) {
if (this.status() !== Instance.STATUS_STOP && this.status() !== Instance.STATUS_BUSY)
if (!this.isStoppedOrBusy())
throw new Error($t("TXT_CODE_instanceConf.cantModifyInstanceType"));
configureEntityParams(this.config, cfg, "type", String);
this.forceExec(new FunctionDispatcher());
}
if (cfg?.enableRcon != null && cfg?.enableRcon !== this.config.enableRcon) {
if (this.status() != Instance.STATUS_STOP) throw new Error($t("TXT_CODE_bdfa3457"));
if (!this.isStoppedOrBusy()) throw new Error($t("TXT_CODE_bdfa3457"));
configureEntityParams(this.config, cfg, "enableRcon", Boolean);
this.forceExec(new FunctionDispatcher());
}
if (cfg?.processType && cfg?.processType !== this.config.processType) {
if (this.status() !== Instance.STATUS_STOP && this.status() !== Instance.STATUS_BUSY)
if (!this.isStoppedOrBusy())
throw new Error($t("TXT_CODE_instanceConf.cantModifyProcessType"));
configureEntityParams(this.config, cfg, "processType", String);
this.forceExec(new FunctionDispatcher());
@ -117,15 +121,14 @@ export default class Instance extends EventEmitter {
cfg?.terminalOption?.pty != null &&
cfg?.terminalOption?.pty !== this.config.terminalOption.pty
) {
if (this.status() != Instance.STATUS_STOP)
throw new Error($t("TXT_CODE_instanceConf.cantModifyPtyModel"));
if (!this.isStoppedOrBusy()) throw new Error($t("TXT_CODE_instanceConf.cantModifyPtyModel"));
// if (!fs.existsSync(PTY_PATH) && cfg?.terminalOption?.pty === true)
// throw new Error($t("TXT_CODE_instanceConf.ptyNotExist", { path: PTY_PATH }));
configureEntityParams(this.config.terminalOption, cfg.terminalOption, "pty", Boolean);
this.forceExec(new FunctionDispatcher());
}
// Only allow some configuration items to be modified when the server is stopped
if (this.status() === Instance.STATUS_STOP && cfg.terminalOption) {
if (this.isStoppedOrBusy() && cfg.terminalOption) {
configureEntityParams(this.config.terminalOption, cfg.terminalOption, "ptyWindowCol", Number);
configureEntityParams(this.config.terminalOption, cfg.terminalOption, "ptyWindowRow", Number);
}

View File

@ -11,6 +11,7 @@ import { getFileManager } from "../file_router_service";
import { IAsyncTaskJSON, TaskCenter, AsyncTask } from "./index";
import logger from "../log";
import { t } from "i18next";
import type { IJsonData } from "common/global";
export class QuickInstallTask extends AsyncTask {
public static TYPE = "QuickInstallTask";
@ -21,9 +22,6 @@ export class QuickInstallTask extends AsyncTask {
public zipPath = "";
private downloadStream?: fs.WriteStream;
private JAVA_17_PATH = path.normalize(
path.join(process.cwd(), "lib", "jre17", "bin", "java.exe")
);
constructor(
public instanceName: string,
@ -40,8 +38,6 @@ export class QuickInstallTask extends AsyncTask {
this.instance = InstanceSubsystem.createInstance(config);
} else {
this.instance = curInstance;
config.cwd = this.instance.config.cwd;
this.instance.config = config;
}
this.taskId = `${QuickInstallTask.TYPE}-${this.instance.instanceUuid}-${v4()}`;
this.type = QuickInstallTask.TYPE;
@ -70,10 +66,6 @@ export class QuickInstallTask extends AsyncTask {
});
}
private hasJava17() {
return fs.existsSync(this.JAVA_17_PATH);
}
async onStarted() {
const fileManager = getFileManager(this.instance.instanceUuid);
try {
@ -90,11 +82,20 @@ export class QuickInstallTask extends AsyncTask {
config = JSON.parse(await fileManager.readFile(this.ZIP_CONFIG_JSON));
}
if (config.startCommand && config.startCommand.includes("{{java}}")) {
if (this.hasJava17()) {
config.startCommand = config.startCommand.replace("{{java}}", `"${this.JAVA_17_PATH}"`);
} else {
config.startCommand = config.startCommand.replace("{{java}}", "java");
if (config.startCommand) {
const ENV_MAP: IJsonData = {
java: "java",
cwd: this.instance.config.cwd,
rconIp: this.instance.config.rconIp,
rconPort: String(this.instance.config.rconPort),
rconPassword: this.instance.config.rconPassword,
nickname: this.instance.config.nickname,
instanceUuid: this.instance.instanceUuid
};
for (const key in ENV_MAP) {
const varDefine = `{{${key}}}`;
while (config.startCommand.includes(varDefine))
config.startCommand = config.startCommand?.replace(varDefine, ENV_MAP[key] || "");
}
}