mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
Fixed instance creation, server reports playercount
This commit is contained in:
1 parent
88238ead27
commit
77d7def6ef
1 file changed
+106
-83
@@ -25,6 +25,7 @@ if (!fs.existsSync("./sharedMods/")) {
|
|||||||
const instance = process.argv[3];
|
const instance = process.argv[3];
|
||||||
const instancedirectory = './instances/' + instance;
|
const instancedirectory = './instances/' + instance;
|
||||||
const command = process.argv[2];
|
const command = process.argv[2];
|
||||||
|
var instanceInfo = {};
|
||||||
|
|
||||||
// Functions
|
// Functions
|
||||||
function deleteFolderRecursive(path) {
|
function deleteFolderRecursive(path) {
|
||||||
@@ -58,70 +59,6 @@ function messageInterface(command, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load plugins and execute onLoad event
|
|
||||||
let pluginDirectories = getDirectories("./sharedPlugins/");
|
|
||||||
let plugins = [];
|
|
||||||
for(i=0; i<pluginDirectories.length; i++) {
|
|
||||||
let I = i
|
|
||||||
let log = function(t) {
|
|
||||||
console.log("Clusterio | "+ pluginDirectories[I] + " | " + t)
|
|
||||||
}
|
|
||||||
|
|
||||||
let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js")
|
|
||||||
plugins.push(child_process.spawn(pluginConfig.binary, [], {
|
|
||||||
cwd: "./sharedPlugins/"+pluginDirectories[i],
|
|
||||||
stdio: ['pipe', 'pipe', 'pipe'],
|
|
||||||
}));
|
|
||||||
|
|
||||||
/*
|
|
||||||
to send to stdin, use:
|
|
||||||
spawn.stdin.write("text\n")
|
|
||||||
*/
|
|
||||||
|
|
||||||
if(!global.subscribedFiles) {
|
|
||||||
global.subscribedFiles = {};
|
|
||||||
}
|
|
||||||
// If plugin has subscribed to a file, send any text appearing in that file to stdin
|
|
||||||
if(pluginConfig.scriptOutputFileSubscription && typeof pluginConfig.scriptOutputFileSubscription == "string") {
|
|
||||||
if(global.subscribedFiles[pluginConfig.scriptOutputFileSubscription]) {
|
|
||||||
// please choose a unique file to subscribe to. If you need plugins to share this interface, set up a direct communication
|
|
||||||
// between those plugins instead.
|
|
||||||
throw "FATAL ERROR IN " + pluginDirectories[i] + " FILE ALREADY SUBSCRIBED " + pluginConfig.scriptOutputFileSubscription;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fs.existsSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription)) {
|
|
||||||
// Do something
|
|
||||||
fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, "")
|
|
||||||
}
|
|
||||||
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
|
||||||
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) {
|
|
||||||
// get array of lines in file
|
|
||||||
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
|
||||||
// if you found anything, reset the file
|
|
||||||
if (stuff[0]) {
|
|
||||||
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "")
|
|
||||||
}
|
|
||||||
for (let i = 0; i < stuff.length; i++) {
|
|
||||||
if (stuff[i]) {
|
|
||||||
plugins[I].stdin.write(stuff[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
console.log("Clusterio | Loaded plugin " + pluginDirectories[i]);
|
|
||||||
plugins[i].stdout.on("data", (data) => {
|
|
||||||
// log("Stdout: " + data);
|
|
||||||
messageInterface(data.toString('utf8'));
|
|
||||||
});
|
|
||||||
plugins[i].stderr.on("data", (data) => {
|
|
||||||
log("STDERR: " + data);
|
|
||||||
})
|
|
||||||
plugins[i].on('close', (code) => {
|
|
||||||
log(`child process exited with code ${code}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// handle commandline parameters
|
// handle commandline parameters
|
||||||
if (!command || command == "help" || command == "--help") {
|
if (!command || command == "help" || command == "--help") {
|
||||||
console.error("Usage: ")
|
console.error("Usage: ")
|
||||||
@@ -315,30 +252,116 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\
|
|||||||
}
|
}
|
||||||
|
|
||||||
function instanceManagement() {
|
function instanceManagement() {
|
||||||
|
// load plugins and execute onLoad event
|
||||||
|
let pluginDirectories = getDirectories("./sharedPlugins/");
|
||||||
|
let plugins = [];
|
||||||
|
for(i=0; i<pluginDirectories.length; i++) {
|
||||||
|
let I = i
|
||||||
|
let log = function(t) {
|
||||||
|
console.log("Clusterio | "+ pluginDirectories[I] + " | " + t)
|
||||||
|
}
|
||||||
|
|
||||||
|
let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js")
|
||||||
|
plugins.push(child_process.spawn(pluginConfig.binary, [], {
|
||||||
|
cwd: "./sharedPlugins/"+pluginDirectories[i],
|
||||||
|
stdio: ['pipe', 'pipe', 'pipe'],
|
||||||
|
}));
|
||||||
|
|
||||||
|
/*
|
||||||
|
to send to stdin, use:
|
||||||
|
spawn.stdin.write("text\n")
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(!global.subscribedFiles) {
|
||||||
|
global.subscribedFiles = {};
|
||||||
|
}
|
||||||
|
// If plugin has subscribed to a file, send any text appearing in that file to stdin
|
||||||
|
if(pluginConfig.scriptOutputFileSubscription && typeof pluginConfig.scriptOutputFileSubscription == "string") {
|
||||||
|
if(global.subscribedFiles[pluginConfig.scriptOutputFileSubscription]) {
|
||||||
|
// please choose a unique file to subscribe to. If you need plugins to share this interface, set up a direct communication
|
||||||
|
// between those plugins instead.
|
||||||
|
throw "FATAL ERROR IN " + pluginDirectories[i] + " FILE ALREADY SUBSCRIBED " + pluginConfig.scriptOutputFileSubscription;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fs.existsSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription)) {
|
||||||
|
// Do something
|
||||||
|
fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, "")
|
||||||
|
}
|
||||||
|
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
||||||
|
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, function (eventType, filename) {
|
||||||
|
// get array of lines in file
|
||||||
|
let stuff = fs.readFileSync(instancedirectory + "/script-output/" + filename, "utf8").split("\n");
|
||||||
|
// if you found anything, reset the file
|
||||||
|
if (stuff[0]) {
|
||||||
|
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "")
|
||||||
|
}
|
||||||
|
for (let i = 0; i < stuff.length; i++) {
|
||||||
|
if (stuff[i]) {
|
||||||
|
plugins[I].stdin.write(stuff[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
console.log("Clusterio | Loaded plugin " + pluginDirectories[i]);
|
||||||
|
plugins[i].stdout.on("data", (data) => {
|
||||||
|
// log("Stdout: " + data);
|
||||||
|
messageInterface(data.toString('utf8'));
|
||||||
|
});
|
||||||
|
plugins[i].stderr.on("data", (data) => {
|
||||||
|
log("STDERR: " + data);
|
||||||
|
})
|
||||||
|
plugins[i].on('close', (code) => {
|
||||||
|
log(`child process exited with code ${code}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// world IDs ------------------------------------------------------------------
|
// world IDs ------------------------------------------------------------------
|
||||||
hashMods(instance, function(modHashes){
|
hashMods(instance, function(modHashes){
|
||||||
setInterval(getID, 10000);
|
setInterval(getID, 10000);
|
||||||
getID()
|
getID()
|
||||||
function getID() {
|
function getID() {
|
||||||
var payload = {
|
messageInterface("/silent-command game.write_file('tempfile.txt', 'connected_players ' .. #game.connected_players .. '\\n', true, 0)", function(err) {setTimeout(function(){
|
||||||
time: Date.now(),
|
// get array of lines in file
|
||||||
rconPort: instanceconfig.clientPort,
|
if(fs.existsSync(instancedirectory + "/script-output/tempfile.txt")) {
|
||||||
rconPassword: instanceconfig.clientPassword,
|
var data = fs.readFileSync(instancedirectory + "/script-output/tempfile.txt", "utf8").split("\n");
|
||||||
serverPort: instanceconfig.factorioPort,
|
// delete when we are done
|
||||||
unique: instanceconfig.clientPassword.hashCode(),
|
fs.unlink(instancedirectory + "/script-output/tempfile.txt");
|
||||||
publicIP: config.publicIP, // IP of the server should be global for all instances, so we pull that straight from the config
|
}
|
||||||
mods:modHashes
|
// if we actually got anything from the file, proceed to categorize it
|
||||||
}
|
if (data && data[0]) {
|
||||||
require('getmac').getMac(function (err, mac) {
|
while (data[0]) {
|
||||||
if (err) throw err
|
let q = data[0].split(" ");
|
||||||
payload.mac = mac
|
// delete array element
|
||||||
console.log(payload)
|
data.splice(0,1)
|
||||||
needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function (err, response, body) {
|
if(q[0] == "connected_players" && Number(q[1]) != NaN) {
|
||||||
if (response && response.body) {
|
instanceInfo.playerCount = q[1];
|
||||||
// In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care.
|
}
|
||||||
console.log(response.body)
|
|
||||||
}
|
}
|
||||||
});
|
} else {
|
||||||
|
instanceInfo.playerCount = 0;
|
||||||
|
}
|
||||||
|
var payload = {
|
||||||
|
time: Date.now(),
|
||||||
|
rconPort: instanceconfig.clientPort,
|
||||||
|
rconPassword: instanceconfig.clientPassword,
|
||||||
|
serverPort: instanceconfig.factorioPort,
|
||||||
|
unique: instanceconfig.clientPassword.hashCode(),
|
||||||
|
publicIP: config.publicIP, // IP of the server should be global for all instances, so we pull that straight from the config
|
||||||
|
mods:modHashes,
|
||||||
|
playerCount: instanceInfo.playerCount || 0,
|
||||||
|
}
|
||||||
|
require('getmac').getMac(function (err, mac) {
|
||||||
|
if (err) throw err
|
||||||
|
payload.mac = mac
|
||||||
|
console.log(payload)
|
||||||
|
needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function (err, response, body) {
|
||||||
|
if (response && response.body) {
|
||||||
|
// In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care.
|
||||||
|
console.log(response.body)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
|
},1000)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -498,7 +521,7 @@ function instanceManagement() {
|
|||||||
}
|
}
|
||||||
// console.log(frameset);
|
// console.log(frameset);
|
||||||
// Send all our compressed frames
|
// Send all our compressed frames
|
||||||
client.exec("/silent-command remote.call('clusterio', 'receiveMany', '" + JSON.stringify(frameset) + "')");
|
messageInterface("/silent-command remote.call('clusterio', 'receiveMany', '" + JSON.stringify(frameset) + "')");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// after fetching all the latest frames, we take a timestamp. During the next iteration, we fetch all frames submitted after this.
|
// after fetching all the latest frames, we take a timestamp. During the next iteration, we fetch all frames submitted after this.
|
||||||
|
|||||||
Reference in new issue
Block a user