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 zip::ZipArchive;
|
||||
@@ -123,22 +128,73 @@ pub fn start_cli(server: Arc<Server>, plugin_loader: PluginLoader) {
|
||||
}
|
||||
"load" "loads a plugin that has been installed" => {
|
||||
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>"]) {
|
||||
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 {
|
||||
LOGGER.info("Stopping plugin");
|
||||
if let Err(e) = plugin.stop() {
|
||||
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" => {
|
||||
server.shutdown();
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::{
|
||||
Plugin,
|
||||
types::{PluginHandshake, PluginJson},
|
||||
},
|
||||
server::Server,
|
||||
};
|
||||
use std::{
|
||||
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 {
|
||||
|
||||
+1
-13
@@ -90,19 +90,7 @@ impl Server {
|
||||
plugin_loader.start_server();
|
||||
|
||||
// Load plugins
|
||||
Self::LOGGER.info("Loading plugins");
|
||||
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");
|
||||
plugin_loader.load_all(self);
|
||||
|
||||
// Initialize plugins
|
||||
Self::LOGGER.info("Initializing plugins");
|
||||
|
||||
Reference in New Issue
Block a user