mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-08 04:30:47 +00:00
Updates to plugins
This commit is contained in:
parent
0aae8c6c8e
commit
b15f3941f1
3 changed files with 63 additions and 15 deletions
|
|
@ -87,6 +87,10 @@ function inject(serv, player, options) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (results = command.match(/^reload/)) {
|
||||||
|
serv.reloadPlugins();
|
||||||
|
player.chat('Reloaded ' + serv.plugins.length + ' plugins.');
|
||||||
|
}
|
||||||
else
|
else
|
||||||
player.chat("Invalid command.");
|
player.chat("Invalid command.");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ function inject(serv,options)
|
||||||
player.plugins = Array();
|
player.plugins = Array();
|
||||||
for(var pluginName in serv.plugins) { // External plugins
|
for(var pluginName in serv.plugins) { // External plugins
|
||||||
player.plugins[serv.plugins[pluginName].id] = {}; // Give object to save data per plugin per player, referenced by plugin ID
|
player.plugins[serv.plugins[pluginName].id] = {}; // Give object to save data per plugin per player, referenced by plugin ID
|
||||||
require(serv.plugins[pluginName].path)(serv, player, serv.plugins[pluginName], options);
|
var plug = require(serv.plugins[pluginName].path)(serv, player, serv.plugins[pluginName], options);
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -3,7 +3,54 @@ module.exports = inject;
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
|
||||||
function inject(serv) {
|
function inject(serv) {
|
||||||
getNodeModules(serv, setPluginsFromModules);
|
|
||||||
|
serv.loadPlugins = loadPlugins;
|
||||||
|
|
||||||
|
serv.loadPlugins();
|
||||||
|
|
||||||
|
function resetPlayers() {
|
||||||
|
for (var p in serv.players) {
|
||||||
|
var player = serv.players[p]
|
||||||
|
player.plugins = Array();
|
||||||
|
for (var pl in serv.plugins) {
|
||||||
|
var plugin = serv.plugins[pl];
|
||||||
|
player.plugins[plugin.id] = {};
|
||||||
|
require(plugin.path)(serv, player, plugin);
|
||||||
|
}
|
||||||
|
console.log(serv.players[p].plugins);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serv.reloadPlugins = function() {
|
||||||
|
console.log('RELOADING DOES NOT WORK');
|
||||||
|
return;
|
||||||
|
serv.emit('pluginend');
|
||||||
|
resetPlayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
serv.fullReloadPlugins = function(cb) {
|
||||||
|
console.log('RELOADING DOES NOT WORK');
|
||||||
|
return;
|
||||||
|
serv.emit('pluginend');
|
||||||
|
serv.loadPlugins(function() {
|
||||||
|
resetPlayers();
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
serv.getPlugin = function(name) {
|
||||||
|
return serv.plugins[name] || null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadPlugins(cb) {
|
||||||
|
var serv = this;
|
||||||
|
serv.plugins = Array();
|
||||||
|
|
||||||
|
loadCount = 0;
|
||||||
|
allPlugins = null;
|
||||||
|
|
||||||
|
getNodeModules(serv, setPluginsFromModules, cb);
|
||||||
|
|
||||||
var pluginPath = __dirname.match(/(.*?)\/lib/)[1] + '/plugins'; // Prob a cleaner way to do this
|
var pluginPath = __dirname.match(/(.*?)\/lib/)[1] + '/plugins'; // Prob a cleaner way to do this
|
||||||
fs.readdir(pluginPath, function(err, arr) {
|
fs.readdir(pluginPath, function(err, arr) {
|
||||||
|
|
@ -17,17 +64,12 @@ function inject(serv) {
|
||||||
path: pluginPath + '/' + arr[a]
|
path: pluginPath + '/' + arr[a]
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
setPlugins(plugins, serv);
|
setPlugins(plugins, serv, cb);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
serv.plugins = Array();
|
|
||||||
serv.getPlugin = function(name) {
|
|
||||||
return serv.plugins[name] || null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setPluginsFromModules(err, modules, serv) {
|
function setPluginsFromModules(err, modules, serv, cb) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('ERROR: Error loading node_modules; Cannot load external plugins! /lib/serverPlugins/plugins.js');
|
console.log('ERROR: Error loading node_modules; Cannot load external plugins! /lib/serverPlugins/plugins.js');
|
||||||
serv.emit('error',err);
|
serv.emit('error',err);
|
||||||
|
|
@ -44,12 +86,12 @@ function setPluginsFromModules(err, modules, serv) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
setPlugins(plugins, serv);
|
setPlugins(plugins, serv, cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
var loadCount = 0;
|
var loadCount = 0;
|
||||||
var allPlugins;
|
var allPlugins;
|
||||||
function setPlugins(plugins, serv) {
|
function setPlugins(plugins, serv, cb) {
|
||||||
loadCount++;
|
loadCount++;
|
||||||
if (loadCount < 2) { // Wait for both plugins folder and node_modules to load
|
if (loadCount < 2) { // Wait for both plugins folder and node_modules to load
|
||||||
allPlugins = plugins;
|
allPlugins = plugins;
|
||||||
|
|
@ -67,16 +109,17 @@ function setPlugins(plugins, serv) {
|
||||||
};
|
};
|
||||||
console.log('Loaded plugin: ' + plugins[p].name);
|
console.log('Loaded plugin: ' + plugins[p].name);
|
||||||
id++;
|
id++;
|
||||||
if (p < plugins.length-1 && plugins[p].name == plugins[p+1].name) { // Only checks for two duplicates, TODO: check for 3+ duplicates
|
if (p < plugins.length-1 && plugins[p].name == plugins[parseInt(p)+1].name) { // Only checks for two duplicates, TODO: check for 3+ duplicates
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('Loaded ' + id + ' Plugin' + (id != 1 ? 's' : '') );
|
console.log('Loaded ' + id + ' Plugin' + (id != 1 ? 's' : '') );
|
||||||
|
if (cb) cb();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getNodeModules(serv, cb) {
|
function getNodeModules(serv, cb, cb2) {
|
||||||
require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
|
require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
|
||||||
if (err) return cb(err, null, serv);
|
if (err) return cb(err, null, serv, cb2);
|
||||||
cb(null, JSON.parse(stdout).dependencies, serv);
|
cb(null, JSON.parse(stdout).dependencies, serv, cb2);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue