diff --git a/Cargo.toml b/Cargo.toml index 2247f34..d9da2c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ serde = { version = "1.0.219", features = ["serde_derive"] } serde_json = "1.0.143" sha1 = "0.10.6" ureq = "3.1.2" +zip = "7.0.0" [features] default = [] diff --git a/src/plugin/loader.rs b/src/plugin/loader.rs index d43a9da..26d1f4c 100644 --- a/src/plugin/loader.rs +++ b/src/plugin/loader.rs @@ -28,14 +28,14 @@ impl PluginLoader { } } - pub fn load(&self, json_path: &Path) -> Plugin { - let json_string = fs::read_to_string(json_path).unwrap(); + pub fn load(&self, path: &Path) -> Plugin { + let json_string = fs::read_to_string(path.join("plugin.json")).unwrap(); let plugin_json: PluginJson = serde_json::from_str(&json_string).unwrap(); LOGGER.info(format!("Loading {}", plugin_json.id)); Command::new(plugin_json.file) .args(plugin_json.args) - .current_dir("plugins") + .current_dir(path) .spawn() .unwrap(); diff --git a/src/server.rs b/src/server.rs index 0385b5a..78827e9 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,14 +1,16 @@ use std::{ collections::HashSet, - fs, + fs::{self, File}, net::{TcpListener, TcpStream}, path::{Path, PathBuf}, sync::{Arc, Mutex}, }; +use zip::ZipArchive; + use crate::{ logger, - plugin::{Plugin, loader::PluginLoader, types::LoaderMessage}, + plugin::{self, Plugin, loader::PluginLoader, types::LoaderMessage}, types, utils::{self, client::Client}, }; @@ -78,8 +80,22 @@ impl Server { let entry = entry?; let path = entry.path(); - if path.extension().and_then(|s| s.to_str()) == Some("json") { - let mut p = plugin_loader.load(&path); + let p = if path.extension().and_then(|s| s.to_str()) == Some("vxp") { + plugin::loader::LOGGER.info(format!("Installing {:?}", path)); + let d = path.with_extension(""); + if !d.exists() { + let file = File::open(&path)?; + let mut archive = ZipArchive::new(file)?; + archive.extract(&d)?; + } + + d + } else { + path + }; + + if p.is_dir() { + let mut p = plugin_loader.load(&p); let s = self.clone(); self.plugins.lock().unwrap().push(p.clone()); std::thread::spawn(move || p.run(&s));