mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-30 08:47:14 +09:00
Big changes to plugin system, fixed inventoryImports
This commit is contained in:
1 parent
36ccf22e02
commit
44b68c92d5
3 files changed
+55
-28
No files matched your search
@@ -415,16 +415,46 @@ function instanceManagement() {
|
||||
let plugins = [];
|
||||
for(let i=0; i<pluginDirectories.length; i++) {
|
||||
let I = i
|
||||
let log = function(t) {
|
||||
console.log("Clusterio | "+ pluginDirectories[I] + " | " + t);
|
||||
let log = function(message) {
|
||||
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");
|
||||
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 || [];
|
||||
plugins.push(child_process.spawn(pluginConfig.binary, args, {
|
||||
plugins[I]=child_process.spawn(pluginConfig.binary, args, {
|
||||
cwd: "./sharedPlugins/"+pluginDirectories[i],
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
}));
|
||||
});
|
||||
|
||||
/*
|
||||
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
|
||||
// wants to contact master or know something.
|
||||
let combinedConfig = deepmerge(instanceconfig,config,{clone:true})
|
||||
// send through script-output file, maybe more compat?
|
||||
fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, JSON.stringify(combinedConfig)+"\r\n");
|
||||
// send directly through stdin
|
||||
@@ -484,6 +513,7 @@ function instanceManagement() {
|
||||
log(`child process exited with code ${code}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// world IDs ------------------------------------------------------------------
|
||||
hashMods(instance, function(modHashes){
|
||||
|
||||
@@ -12,7 +12,7 @@ module.exports = {
|
||||
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)
|
||||
// Make sure its cross platform somehow.
|
||||
binary: "node",
|
||||
binary: "nodePackage",
|
||||
arguments: ["index.js"],
|
||||
description: "Clusterio plugin to fill character logistic slots with master imports",
|
||||
// We'll send everything in this file to your stdin. Beware.
|
||||
|
||||
@@ -4,27 +4,24 @@ const pluginConfig = require("./config.js");
|
||||
|
||||
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
|
||||
|
||||
class inventoryImports {
|
||||
constructor(slaveConfig, messageInterface){
|
||||
this.config = slaveConfig;
|
||||
this.messageInterface = messageInterface;
|
||||
messageInterface("/silent-command game.print('InventoryImports enabled')");
|
||||
setInterval(function(){
|
||||
// console.log("Polling inventories\n")
|
||||
console.log(functions.pollInventories(pluginConfig.scriptOutputFileSubscription));
|
||||
messageInterface("Polling inventories\n")
|
||||
messageInterface(functions.pollInventories(pluginConfig.scriptOutputFileSubscription));
|
||||
}, 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;
|
||||
Reference in new issue
Block a user