Big changes to plugin system, fixed inventoryImports

This commit is contained in:
Danielv123 committed 2017-12-10 11:27:02 +01:00
1 parent 36ccf22e02
commit 44b68c92d5
3 files changed
+55 -28

No files matched your search

+36 -6
View File
@@ -415,16 +415,46 @@ function instanceManagement() {
let plugins = []; let plugins = [];
for(let i=0; i<pluginDirectories.length; i++) { for(let i=0; i<pluginDirectories.length; i++) {
let I = i let I = i
let log = function(t) { let log = function(message) {
console.log("Clusterio | "+ pluginDirectories[I] + " | " + t); console.log("Clusterio | "+ pluginDirectories[I] + " | " + message);
} }
// these are our two config files. We need to send these in case plugin
// wants to contact master or know something.
let combinedConfig = deepmerge(instanceconfig,config,{clone:true})
let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js"); let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js");
if(pluginConfig.binary == "nodePackage"){
// require index.js.main() of plugin and execute it as a function
let pluginClass = require("./sharedPlugins/" + pluginDirectories[I] + "/index.js");
plugins[I] = new pluginClass(combinedConfig, function(data){
if(data.toString('utf8')[0] != "/") {
log("Stdout: " + data.toString('utf8'));
} else {
messageInterface(data.toString('utf8'));
}
});
if(pluginConfig.scriptOutputFileSubscription && typeof pluginConfig.scriptOutputFileSubscription == "string"){
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].scriptOutput(stuff[i]);
}
}
});
}
console.log("Clusterio | Loaded plugin " + pluginDirectories[i]);
} else if(pluginConfig.binary != "nodePackage"){
// handle as fragile executable plugin
let args = pluginConfig.args || []; let args = pluginConfig.args || [];
plugins.push(child_process.spawn(pluginConfig.binary, args, { plugins[I]=child_process.spawn(pluginConfig.binary, args, {
cwd: "./sharedPlugins/"+pluginDirectories[i], cwd: "./sharedPlugins/"+pluginDirectories[i],
stdio: ['pipe', 'pipe', 'pipe'], stdio: ['pipe', 'pipe', 'pipe'],
})); });
/* /*
to send to stdin, use: to send to stdin, use:
@@ -463,7 +493,6 @@ function instanceManagement() {
} }
// these are our two config files. We need to send these in case plugin // these are our two config files. We need to send these in case plugin
// wants to contact master or know something. // wants to contact master or know something.
let combinedConfig = deepmerge(instanceconfig,config,{clone:true})
// send through script-output file, maybe more compat? // send through script-output file, maybe more compat?
fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, JSON.stringify(combinedConfig)+"\r\n"); fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, JSON.stringify(combinedConfig)+"\r\n");
// send directly through stdin // send directly through stdin
@@ -484,6 +513,7 @@ function instanceManagement() {
log(`child process exited with code ${code}`); log(`child process exited with code ${code}`);
}); });
} }
}
// world IDs ------------------------------------------------------------------ // world IDs ------------------------------------------------------------------
hashMods(instance, function(modHashes){ hashMods(instance, function(modHashes){
+1 -1
View File
@@ -12,7 +12,7 @@ module.exports = {
version: "1.0.0", version: "1.0.0",
// Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface) // Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface)
// Make sure its cross platform somehow. // Make sure its cross platform somehow.
binary: "node", binary: "nodePackage",
arguments: ["index.js"], arguments: ["index.js"],
description: "Clusterio plugin to fill character logistic slots with master imports", description: "Clusterio plugin to fill character logistic slots with master imports",
// We'll send everything in this file to your stdin. Beware. // We'll send everything in this file to your stdin. Beware.
+18 -21
View File
@@ -4,27 +4,24 @@ const pluginConfig = require("./config.js");
const functions = require("./interfacingFunctions"); const functions = require("./interfacingFunctions");
console.log("/silent-command game.print('InventoryImports enabled')");
var config = {};
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
var chunk = process.stdin.read();
// console.log(chunk)
if (chunk !== null && isNaN(chunk) && config && isJson(chunk) && !JSON.parse(chunk).factorioPort){
let inventory = JSON.parse(chunk)
// process inventory of players and ask master for more stuff
functions.handleInventory(inventory, config, console.log);
} else if(isJson(chunk) && JSON.parse(chunk).factorioPort){
config = JSON.parse(chunk);
}
});
process.stdin.on('end', () => {
process.stdout.write('end');
});
// poll inventories // poll inventories
class inventoryImports {
constructor(slaveConfig, messageInterface){
this.config = slaveConfig;
this.messageInterface = messageInterface;
messageInterface("/silent-command game.print('InventoryImports enabled')");
setInterval(function(){ setInterval(function(){
// console.log("Polling inventories\n") messageInterface("Polling inventories\n")
console.log(functions.pollInventories(pluginConfig.scriptOutputFileSubscription)); messageInterface(functions.pollInventories(pluginConfig.scriptOutputFileSubscription));
}, 10000); }, 10000);
}
scriptOutput(data){
if(data !== null && isJson(data)){
let inventory = JSON.parse(data);
// process inventory of players and ask master for more stuff
functions.handleInventory(inventory, this.config, this.messageInterface);
}
}
}
module.exports = inventoryImports;