Fixed loading and added more plugin loading commands
This commit is contained in:
+61
-5
@@ -1,4 +1,9 @@
|
|||||||
use std::{fs::File, path::PathBuf, str::FromStr, sync::Arc};
|
use std::{
|
||||||
|
fs::{self, File},
|
||||||
|
path::PathBuf,
|
||||||
|
str::FromStr,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
|
|
||||||
use rustyline::{DefaultEditor, error::ReadlineError};
|
use rustyline::{DefaultEditor, error::ReadlineError};
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
@@ -123,23 +128,74 @@ pub fn start_cli(server: Arc<Server>, plugin_loader: PluginLoader) {
|
|||||||
}
|
}
|
||||||
"load" "loads a plugin that has been installed" => {
|
"load" "loads a plugin that has been installed" => {
|
||||||
if require_args(&args, &["<plugin-id>"]) {
|
if require_args(&args, &["<plugin-id>"]) {
|
||||||
plugin_loader.load(&server.root.join("plugins").join(&args[1]));
|
let mut p = plugin_loader.load(&server.root.join("plugins").join(&args[1]));
|
||||||
|
let s = server.clone();
|
||||||
|
server.plugins.lock().unwrap().push(p.clone());
|
||||||
|
std::thread::spawn(move || p.run(&s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"reload" "restarts a plugin that has been installed" => {
|
"stop" "stops a plugin that has been installed" => {
|
||||||
if require_args(&args, &["<plugin-id>"]) {
|
if require_args(&args, &["<plugin-id>"]) {
|
||||||
let target = args[1].as_str();
|
let target = args[1].as_str();
|
||||||
for plugin in server.plugins.lock().unwrap().iter_mut() {
|
let mut stopped_plugins = Vec::new();
|
||||||
|
|
||||||
|
plugin_loader.remove(target);
|
||||||
|
for (i, plugin) in server.plugins.lock().unwrap().iter_mut().enumerate() {
|
||||||
if plugin.get_id() == target {
|
if plugin.get_id() == target {
|
||||||
LOGGER.info("Stopping plugin");
|
LOGGER.info("Stopping plugin");
|
||||||
if let Err(e) = plugin.stop() {
|
if let Err(e) = plugin.stop() {
|
||||||
LOGGER.warn(e.context("Couldn't stop plugin"));
|
LOGGER.warn(e.context("Couldn't stop plugin"));
|
||||||
}
|
}
|
||||||
|
stopped_plugins.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
plugin_loader.load(&server.root.join("plugins").join(&args[1]));
|
|
||||||
|
for i in stopped_plugins {
|
||||||
|
server.plugins.lock().unwrap().remove(i);
|
||||||
|
}
|
||||||
|
LOGGER.info("Plugin stopped!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"reload" "restarts a plugin that has been installed" => {
|
||||||
|
if require_args(&args, &["<plugin-id>"]) {
|
||||||
|
let target = args[1].as_str();
|
||||||
|
let mut stopped_plugins = Vec::new();
|
||||||
|
|
||||||
|
plugin_loader.remove(target);
|
||||||
|
for (i, plugin) in server.plugins.lock().unwrap().iter_mut().enumerate() {
|
||||||
|
if plugin.get_id() == target {
|
||||||
|
LOGGER.info("Stopping plugin");
|
||||||
|
if let Err(e) = plugin.stop() {
|
||||||
|
LOGGER.warn(e.context("Couldn't stop plugin"));
|
||||||
|
}
|
||||||
|
stopped_plugins.push(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in stopped_plugins {
|
||||||
|
server.plugins.lock().unwrap().remove(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut p = plugin_loader.load(&server.root.join("plugins").join(&args[1]));
|
||||||
|
let s = server.clone();
|
||||||
|
server.plugins.lock().unwrap().push(p.clone());
|
||||||
|
std::thread::spawn(move || p.run(&s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"reload-all" "reloads all of the plugins" => {
|
||||||
|
LOGGER.info("Stopping all plugins");
|
||||||
|
for plugin in server.plugins.lock().unwrap().iter_mut() {
|
||||||
|
LOGGER.info(format!("Stopping plugin '{}'", plugin.get_id()));
|
||||||
|
if let Err(e) = plugin.stop() {
|
||||||
|
LOGGER.warn(e.context(format!("Couldn't stop plugin '{}'", plugin.get_id())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
server.plugins.lock().unwrap().clear();
|
||||||
|
plugin_loader.clear();
|
||||||
|
|
||||||
|
plugin_loader.load_all(&server);
|
||||||
|
}
|
||||||
"shutdown" "Softly shuts the server down, may not fully shut everything down" => {
|
"shutdown" "Softly shuts the server down, may not fully shut everything down" => {
|
||||||
server.shutdown();
|
server.shutdown();
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use crate::{
|
|||||||
Plugin,
|
Plugin,
|
||||||
types::{PluginHandshake, PluginJson},
|
types::{PluginHandshake, PluginJson},
|
||||||
},
|
},
|
||||||
|
server::Server,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
@@ -83,6 +84,31 @@ impl PluginLoader {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove(&self, plugin_id: &str) {
|
||||||
|
self.plugin_clients.lock().unwrap().remove(plugin_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&self) {
|
||||||
|
self.plugin_clients.lock().unwrap().clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_all(&self, server: &Arc<Server>) {
|
||||||
|
// Load plugins
|
||||||
|
LOGGER.info("Loading plugins");
|
||||||
|
for entry in fs::read_dir(server.root.join("plugins")).unwrap() {
|
||||||
|
let entry = entry.unwrap();
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
if path.is_dir() {
|
||||||
|
let mut p = self.load(&path);
|
||||||
|
let s = server.clone();
|
||||||
|
server.plugins.lock().unwrap().push(p.clone());
|
||||||
|
std::thread::spawn(move || p.run(&s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOGGER.info("Plugins loaded");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Plugin {
|
impl Clone for Plugin {
|
||||||
|
|||||||
+1
-13
@@ -90,19 +90,7 @@ impl Server {
|
|||||||
plugin_loader.start_server();
|
plugin_loader.start_server();
|
||||||
|
|
||||||
// Load plugins
|
// Load plugins
|
||||||
Self::LOGGER.info("Loading plugins");
|
plugin_loader.load_all(self);
|
||||||
for entry in fs::read_dir(self.root.join("plugins"))? {
|
|
||||||
let entry = entry?;
|
|
||||||
let path = entry.path();
|
|
||||||
|
|
||||||
if path.is_dir() {
|
|
||||||
let mut p = plugin_loader.load(&path);
|
|
||||||
let s = self.clone();
|
|
||||||
self.plugins.lock().unwrap().push(p.clone());
|
|
||||||
std::thread::spawn(move || p.run(&s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Self::LOGGER.info("Plugins loaded");
|
|
||||||
|
|
||||||
// Initialize plugins
|
// Initialize plugins
|
||||||
Self::LOGGER.info("Initializing plugins");
|
Self::LOGGER.info("Initializing plugins");
|
||||||
|
|||||||
Reference in New Issue
Block a user