YuKun Liu před 2 roky
rodič
revize
b3dcebcc50
2 změnil soubory, kde provedl 29 přidání a 11 odebrání
  1. 26 4
      src/plugin/interface/fs.rs
  2. 3 7
      src/plugin/mod.rs

+ 26 - 4
src/plugin/interface/fs.rs

@@ -2,9 +2,11 @@ use std::{
     fs::{create_dir, create_dir_all, remove_dir_all},
     path::PathBuf, io::{Read, Write},
 };
+use std::fs::File;
 
 use mlua::UserData;
-
+use flate2::read::GzDecoder;
+use tar::Archive;
 use crate::tools::extract_zip;
 
 pub struct PluginFileSystem;
@@ -48,10 +50,30 @@ impl UserData for PluginFileSystem {
             let file = PathBuf::from(args.0);
             let target = PathBuf::from(args.1);
             let res = extract_zip(&file, &target);
-            if let Err(e) = res {
-                return Err(mlua::Error::RuntimeError(e.to_string()));
+            if let Err(_) = res {
+                return Ok(false);
             }
-            Ok(())
+            Ok(true)
+        });
+        methods.add_function("untar_gz_file", |_, args: (String, String)| {
+
+            let file = PathBuf::from(args.0);
+            let target = PathBuf::from(args.1);
+
+            let tar_gz = if let Ok(v) = File::open(file) {
+                v
+            } else {
+                return Ok(false);
+            };
+
+            let tar = GzDecoder::new(tar_gz);
+            let mut archive = Archive::new(tar);
+            if archive.unpack(&target).is_err() {
+                return Ok(false);
+            }
+
+
+            Ok(true)
         });
     }
 }

+ 3 - 7
src/plugin/mod.rs

@@ -4,7 +4,7 @@ use std::{
     sync::Mutex,
 };
 
-use mlua::{AsChunk, chunk, Lua, Table};
+use mlua::{AsChunk, Lua, Table};
 use serde::{Deserialize, Serialize};
 use serde_json::json;
 
@@ -136,10 +136,10 @@ impl PluginManager {
                 }
                 Err(e) => {
                     // plugin init failed
+                    log::warn!("Plugin init failed: {e}");
                     let _ = lua.load(mlua::chunk! {
                         table.remove(manager, $idx)
                     }).exec();
-                    log::warn!("Plugin init failed: {e}");
                 }
             }
         }
@@ -289,8 +289,4 @@ impl PluginManager {
 
         res
     }
-
-    pub fn plugin_status() {
-        let lua = LUA.lock().unwrap();
-    }
-}
+}