-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnecesse-wrapper.js
More file actions
103 lines (86 loc) · 3.73 KB
/
Copy pathnecesse-wrapper.js
File metadata and controls
103 lines (86 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const {spawn} = require("child_process");
const fs = require('fs');
function editConfigFile(filePath, settingName, newValue) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
// Define the regular expression pattern to match the setting and its value
const pattern = new RegExp(`(${settingName}\\s*=\\s*)([^,\\/\\s]*)`, 'g');
// Replace the old value with the new value
const newData = data.replace(pattern, `$1${newValue}`);
// Write the modified content back to the file
try{
fs.writeFileSync(filePath, newData, { encoding: 'utf8' });
console.log(`Setting '${settingName}' updated to '${newValue}' in ${filePath}`);
} catch (err) {
console.error('Error writing to file:', err);
return;
}
});
}
const configFilePath = "/root/.config/Necesse/cfg/server.cfg"
editConfigFile(configFilePath, "port", process.env.port)
editConfigFile(configFilePath, "slots", process.env.slots)
editConfigFile(configFilePath, "password", process.env.password)
editConfigFile(configFilePath, "maxClientLatencySeconds", process.env.maxClientLatencySeconds)
editConfigFile(configFilePath, "pauseWhenEmpty", process.env.pauseWhenEmpty)
editConfigFile(configFilePath, "giveClientsPower", process.env.giveClientsPower)
editConfigFile(configFilePath, "logging", process.env.logging)
editConfigFile(configFilePath, "language", process.env.language)
editConfigFile(configFilePath, "unloadLevelsCooldown", process.env.unloadLevelsCooldown)
editConfigFile(configFilePath, "worldBorderSize", process.env.worldBorderSize)
editConfigFile(configFilePath, "droppedItemsLifeMinutes", process.env.droppedItemsLifeMinutes)
editConfigFile(configFilePath, "unloadSettlements", process.env.unloadSettlements)
editConfigFile(configFilePath, "maxSettlementsPerPlayer", process.env.maxSettlementsPerPlayer)
editConfigFile(configFilePath, "maxSettlersPerSettlement", process.env.maxSettlersPerSettlement)
editConfigFile(configFilePath, "jobSearchRange", process.env.jobSearchRange)
editConfigFile(configFilePath, "zipSaves", process.env.zipSaves)
editConfigFile(configFilePath, "MOTD", process.env.MOTD)
// If JVM_OPTS is set, use them
let jvm_opts = ""
console.log("JVM_OPTS: " + process.env.JVM_OPTS)
if (process.env.JVM_OPTS != undefined) {
jvm_opts = process.env.JVM_OPTS
}
// start server
const necesse_server = spawn("/necesse-server/jre/bin/java", [[jvm_opts, "-jar", "/necesse-server/Server.jar", "-nogui", "-world", process.env.world].join(" ")], { detached: true, shell: true })
// set encoding
necesse_server.stdout.setEncoding('utf8');
necesse_server.on('close', (code) => {
console.log(`Child process exited with code ${code}`);
});
// set flag when server is up
let isServerUp = false
necesse_server.stdout.on("data", (message) => {
if (message.match("Type help for list of commands.")) {
isServerUp = true
console.log("Detected Necesse server is up, will attempt to save on SIGTERM and SIGINT.")
}
if (message.match("Exiting in 2 seconds...")) {
isServerUp = false
console.log("Server gracefully shutdown.")
}
})
// pipe stdin to necesse
process.stdin.pipe(necesse_server.stdin)
// pipe necesse stdout to process
necesse_server.stdout.pipe(process.stdout)
// pipe necesse stderr to process
necesse_server.stderr.pipe(process.stderr)
function gracefulShutdown() {
if (isServerUp) {
console.log("Attempting to save and exit necesse...")
necesse_server.stdin.write("exit\n");
}
}
// catch terminations
process.once('SIGTERM', () => {
console.info('SIGTERM signal received.');
gracefulShutdown()
});
process.once('SIGINT', () => {
console.info('SIGINT signal received.');
gracefulShutdown()
});