mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-24 00:23:12 +09:00
Plugin loading now respects disabled plugins
This commit is contained in:
1 parent
ffa2d0b0fb
commit
765146b559
2 files changed
+35
-99
No files matched your search
@@ -546,7 +546,7 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
|
|||||||
|
|
||||||
// ensure instancemanagement only ever runs once
|
// ensure instancemanagement only ever runs once
|
||||||
_.once(instanceManagement);
|
_.once(instanceManagement);
|
||||||
function instanceManagement() {
|
async function instanceManagement() {
|
||||||
console.log("Started instanceManagement();");
|
console.log("Started instanceManagement();");
|
||||||
|
|
||||||
/* Open websocket connection to master */
|
/* Open websocket connection to master */
|
||||||
@@ -560,27 +560,27 @@ function instanceManagement() {
|
|||||||
setInterval(B=> socket.emit("heartbeat"), 10000);
|
setInterval(B=> socket.emit("heartbeat"), 10000);
|
||||||
|
|
||||||
// load plugins and execute onLoad event
|
// load plugins and execute onLoad event
|
||||||
let pluginDirectories = fileOps.getDirectoriesSync("./sharedPlugins/");
|
const pluginManager = require("./lib/manager/pluginManager.js")(config);
|
||||||
|
let pluginsToLoad = await pluginManager.getPlugins();
|
||||||
let plugins = [];
|
let plugins = [];
|
||||||
for(let i=0; i<pluginDirectories.length; i++) {
|
|
||||||
let I = i
|
for(let i = 0; i < pluginsToLoad.length; i++){
|
||||||
|
let pluginLoadStarted = Date.now();
|
||||||
let log = function(message) {
|
let log = function(message) {
|
||||||
console.log("Clusterio | "+ pluginDirectories[I] + " | " + message);
|
console.log("Clusterio | "+ pluginsToLoad[i].name + " | " + message);
|
||||||
}
|
}
|
||||||
// these are our two config files. We need to send these in case plugin
|
let combinedConfig = deepmerge(instanceconfig,config,{clone:true});
|
||||||
// wants to contact master or know something.
|
let pluginConfig = pluginsToLoad[i];
|
||||||
let combinedConfig = deepmerge(instanceconfig,config,{clone:true})
|
|
||||||
let pluginConfig = require("./sharedPlugins/" + pluginDirectories[i] + "/config.js");
|
|
||||||
|
|
||||||
if(!global.subscribedFiles) {
|
if(!global.subscribedFiles) {
|
||||||
global.subscribedFiles = {};
|
global.subscribedFiles = {};
|
||||||
}
|
}
|
||||||
if(pluginConfig.binary == "nodePackage"){
|
if(pluginConfig.binary == "nodePackage" && pluginConfig.enabled){
|
||||||
// require index.js.main() of plugin and execute it as a class
|
// require plugin class and execute it
|
||||||
let pluginClass = require("./sharedPlugins/" + pluginDirectories[I] + "/index.js");
|
let pluginClass = require(path.resolve(pluginConfig.pluginPath, "index.js"));
|
||||||
plugins[I] = new pluginClass(combinedConfig, async function(data, callback){
|
plugins[i] = new pluginClass(combinedConfig, async function(data, callback){
|
||||||
if(data.toString('utf8')[0] != "/") {
|
if(data.toString('utf8')[0] != "/") {
|
||||||
log("Stdout: " + data.toString('utf8'));
|
log(data.toString('utf8'));
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return messageInterface(data.toString('utf8'), callback);
|
return messageInterface(data.toString('utf8'), callback);
|
||||||
@@ -588,15 +588,15 @@ function instanceManagement() {
|
|||||||
}, { // extra functions to pass in object. Should have done it like this from the start, but won't break backwards compat.
|
}, { // extra functions to pass in object. Should have done it like this from the start, but won't break backwards compat.
|
||||||
socket, // socket.io connection to master (and ES6 destructuring, yay)
|
socket, // socket.io connection to master (and ES6 destructuring, yay)
|
||||||
});
|
});
|
||||||
if(plugins[I].factorioOutput && typeof plugins[I].factorioOutput === "function"){
|
if(plugins[i].factorioOutput && typeof plugins[i].factorioOutput === "function"){
|
||||||
// when factorio logs a line, send it to the plugin. This includes things like autosaves, chat, errors etc
|
// when factorio logs a line, send it to the plugin. This includes things like autosaves, chat, errors etc
|
||||||
serverprocess.stdout.on("data", data => plugins[I].factorioOutput(data.toString()));
|
serverprocess.stdout.on("data", data => plugins[i].factorioOutput(data.toString()));
|
||||||
}
|
}
|
||||||
if(pluginConfig.scriptOutputFileSubscription && typeof pluginConfig.scriptOutputFileSubscription == "string"){
|
if(pluginConfig.scriptOutputFileSubscription && typeof pluginConfig.scriptOutputFileSubscription == "string"){
|
||||||
if(global.subscribedFiles[pluginConfig.scriptOutputFileSubscription]) {
|
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
|
// 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.
|
// between those plugins instead.
|
||||||
throw "FATAL ERROR IN " + pluginDirectories[i] + " FILE ALREADY SUBSCRIBED " + pluginConfig.scriptOutputFileSubscription;
|
throw "FATAL ERROR IN " + pluginConfig.name + " FILE ALREADY SUBSCRIBED " + pluginConfig.scriptOutputFileSubscription;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fs.existsSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription)) {
|
if (!fs.existsSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription)) {
|
||||||
@@ -605,14 +605,13 @@ function instanceManagement() {
|
|||||||
}
|
}
|
||||||
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
global.subscribedFiles[pluginConfig.scriptOutputFileSubscription] = true;
|
||||||
console.log("Clusterio | Registered file subscription on "+instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription);
|
console.log("Clusterio | Registered file subscription on "+instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription);
|
||||||
|
|
||||||
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, fileChangeHandler);
|
fs.watch(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, fileChangeHandler);
|
||||||
// run once in case a plugin wrote out information before the plugin loaded fully
|
// run once in case a plugin wrote out information before the plugin loaded fully
|
||||||
|
|
||||||
// delay, so the socket got enough time to connect
|
// delay, so the socket got enough time to connect
|
||||||
setTimeout(()=> {
|
setTimeout(()=> {
|
||||||
fileChangeHandler(false, pluginConfig.scriptOutputFileSubscription);
|
fileChangeHandler(false, pluginConfig.scriptOutputFileSubscription);
|
||||||
}, 500);
|
}, 500);
|
||||||
|
|
||||||
// send file contents to plugin for processing
|
// send file contents to plugin for processing
|
||||||
function fileChangeHandler(eventType, filename) {
|
function fileChangeHandler(eventType, filename) {
|
||||||
if(filename != null){
|
if(filename != null){
|
||||||
@@ -625,10 +624,10 @@ function instanceManagement() {
|
|||||||
if (stuff[0]) {
|
if (stuff[0]) {
|
||||||
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "");
|
fs.writeFileSync(instancedirectory + "/script-output/" + filename, "");
|
||||||
}
|
}
|
||||||
for(let i = 0; i < stuff.length; i++) {
|
for(let o = 0; o < stuff.length; o++) {
|
||||||
if(stuff[i] && !stuff[i].includes('\u0000\u0000')) {
|
if(stuff[o] && !stuff[o].includes('\u0000\u0000')) {
|
||||||
try{
|
try{
|
||||||
plugins[I].scriptOutput(stuff[i]);
|
plugins[i].scriptOutput(stuff[o]);
|
||||||
}catch(e){console.error(e)}
|
}catch(e){console.error(e)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -638,72 +637,11 @@ function instanceManagement() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log("Clusterio | Loaded plugin " + pluginDirectories[i]);
|
console.log(`Clusterio | Loaded plugin ${pluginsToLoad[i].name} in ${Date.now() - pluginLoadStarted}ms`);
|
||||||
} else if(pluginConfig.binary != "nodePackage" && pluginConfig.binary){
|
|
||||||
// handle as fragile executable plugin
|
|
||||||
let args = pluginConfig.args || [];
|
|
||||||
plugins[I]=child_process.spawn(pluginConfig.binary, args, {
|
|
||||||
cwd: "./sharedPlugins/"+pluginDirectories[i],
|
|
||||||
stdio: ['pipe', 'pipe', 'pipe'],
|
|
||||||
});
|
|
||||||
|
|
||||||
/*
|
|
||||||
to send to stdin, use:
|
|
||||||
spawn.stdin.write("text\n");
|
|
||||||
*/
|
|
||||||
// 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
|
|
||||||
// TODO async
|
|
||||||
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]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// these are our two config files. We need to send these in case plugin
|
|
||||||
// wants to contact master or know something.
|
|
||||||
// send through script-output file, maybe more compat?
|
|
||||||
fs.writeFileSync(instancedirectory + "/script-output/" + pluginConfig.scriptOutputFileSubscription, JSON.stringify(combinedConfig)+"\r\n");
|
|
||||||
// send directly through stdin
|
|
||||||
// plugins[i].stdin.write(JSON.stringify(combinedConfig)+"\n");
|
|
||||||
|
|
||||||
console.log("Clusterio | Loaded plugin " + pluginDirectories[i]);
|
|
||||||
plugins[i].stdout.on("data", (data) => {
|
|
||||||
if(data.toString('utf8')[0] != "/") {
|
|
||||||
log("Stdout: " + data.toString('utf8'))
|
|
||||||
} else {
|
|
||||||
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}`);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
// This plugin doesn't have a client portion
|
// this plugin doesn't have a client portion. Maybe it runs on the master only?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// world IDs ------------------------------------------------------------------
|
// world IDs ------------------------------------------------------------------
|
||||||
hashMods(instance, function(modHashes){
|
hashMods(instance, function(modHashes){
|
||||||
|
|||||||
@@ -1078,7 +1078,7 @@ io.on('connection', function (socket) {
|
|||||||
if(terminatedConnections > 0) console.log("SOCKET | There are currently "+currentConnections+" websocket connections, deleting "+terminatedConnections+" on timeout");
|
if(terminatedConnections > 0) console.log("SOCKET | There are currently "+currentConnections+" websocket connections, deleting "+terminatedConnections+" on timeout");
|
||||||
|
|
||||||
// tell our friend that we are listening
|
// tell our friend that we are listening
|
||||||
socket.emit('hello', { hello: 'world' });
|
setTimeout(()=>socket.emit('hello', { hello: 'world' }), 1000);
|
||||||
|
|
||||||
/* initial processing for remoteMap */
|
/* initial processing for remoteMap */
|
||||||
socket.on('registerSlaveMapper', function (data) {
|
socket.on('registerSlaveMapper', function (data) {
|
||||||
@@ -1120,29 +1120,27 @@ pluginManagement();
|
|||||||
masterPlugins = [];
|
masterPlugins = [];
|
||||||
async function pluginManagement(){
|
async function pluginManagement(){
|
||||||
let startPluginLoad = Date.now();
|
let startPluginLoad = Date.now();
|
||||||
masterPlugins = await getPlugins("sharedPlugins");
|
masterPlugins = await getPlugins();
|
||||||
console.log("All plugins loaded in "+(Date.now() - startPluginLoad)+"ms");
|
console.log("All plugins loaded in "+(Date.now() - startPluginLoad)+"ms");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPlugins(pluginDirectory){
|
async function getPlugins(){
|
||||||
|
const pluginManager = require("./lib/manager/pluginManager.js")(config);
|
||||||
let plugins = [];
|
let plugins = [];
|
||||||
|
let pluginsToLoad = await pluginManager.getPlugins();
|
||||||
let files = await fs.readdir(pluginDirectory);
|
for(let i = 0; i < pluginsToLoad.length; i++){
|
||||||
for(let i in files){
|
|
||||||
let file = files[i];
|
|
||||||
let pluginStartedLoading = Date.now(); // just for logging
|
let pluginStartedLoading = Date.now(); // just for logging
|
||||||
let pluginPath = path.join(pluginDirectory, file);
|
let stats = await fs.stat(pluginsToLoad[i].pluginPath);
|
||||||
let stats = await fs.stat(pluginPath);
|
|
||||||
if(stats.isDirectory()){
|
if(stats.isDirectory()){
|
||||||
let pluginConfig = require(path.resolve(pluginPath, "config"));
|
let pluginConfig = pluginsToLoad[i];
|
||||||
if(pluginConfig.masterPlugin){
|
if(pluginConfig.masterPlugin && pluginConfig.enabled){
|
||||||
let masterPlugin = require(path.resolve(pluginPath, pluginConfig.masterPlugin));
|
let masterPlugin = require(path.resolve(pluginConfig.pluginPath, pluginConfig.masterPlugin));
|
||||||
plugins.push({
|
plugins.push({
|
||||||
main:new masterPlugin({
|
main:new masterPlugin({
|
||||||
config,
|
config,
|
||||||
pluginConfig,
|
pluginConfig,
|
||||||
path: pluginPath,
|
path: pluginConfig.pluginPath,
|
||||||
socketio: io,
|
socketio: io,
|
||||||
express: app,
|
express: app,
|
||||||
}),
|
}),
|
||||||
|
|||||||
Reference in new issue
Block a user