feat: commit codee

This commit is contained in:
mrxiaozhuox 2022-07-28 23:07:55 +08:00
parent 59acefadc9
commit 3630224659
5 changed files with 49 additions and 6 deletions

View file

@ -1,5 +1,6 @@
{
"Lua.diagnostics.globals": [
"plugin_logger"
"plugin_logger",
"PLUGIN_DOWNLOADER"
]
}

View file

@ -0,0 +1,8 @@
use mlua::UserData;
pub struct PluginDownloader;
impl UserData for PluginDownloader {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(_methods: &mut M) {
// methods.add_function("name", function)
}
}

View file

@ -1,4 +1,7 @@
use std::path::PathBuf;
use std::{
fs::{create_dir, create_dir_all},
path::PathBuf, io::{Read, Write},
};
use mlua::UserData;
@ -17,5 +20,34 @@ impl UserData for PluginFileSystem {
let path = PathBuf::from(path);
Ok(path.is_file())
});
methods.add_function("create_dir", |_, args: (String, bool)| {
let path = args.0;
let recursive = args.1;
let path = PathBuf::from(path);
if !path.exists() {
let v = if recursive {
create_dir_all(path)
} else {
create_dir(path)
};
return Ok(v.is_ok());
}
Ok(true)
});
methods.add_function("file_get_content", |_, path: String| {
let path = PathBuf::from(path);
let mut file = std::fs::File::open(path)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
Ok(buffer)
});
methods.add_function("file_set_content", |_, args: (String, String)| {
let path = args.0;
let content = args.1;
let path = PathBuf::from(path);
let mut file = std::fs::File::create(path)?;
file.write_all(content.as_bytes())?;
Ok(())
});
}
}
}

View file

@ -3,6 +3,7 @@ use mlua::{FromLua, Function, ToLua};
pub mod logger;
pub mod command;
pub mod fs;
pub mod download;
#[derive(Debug)]
pub struct PluginInfo<'lua> {

View file

@ -7,7 +7,7 @@ use crate::tools::{app_path, clone_repo};
use self::{
interface::PluginInfo,
interface::{command::PluginCommander, logger::PluginLogger, fs::PluginFileSystem},
interface::{command::PluginCommander, logger::PluginLogger, fs::PluginFileSystem, download::PluginDownloader},
};
pub mod interface;
@ -36,7 +36,8 @@ impl PluginManager {
lua.globals()
.set("PLUGIN_COMMAND", PluginCommander)
.unwrap();
lua.globals().set("PLUGINFS", PluginFileSystem).unwrap();
lua.globals().set("PLUGIN_FS", PluginFileSystem).unwrap();
lua.globals().set("PLUGIN_DOWNLOAD", PluginDownloader).unwrap();
let plugin_dir = Self::init_plugin_dir();
let mut index = 1;
@ -73,7 +74,7 @@ impl PluginManager {
for i in 1..(manager.len()? as i32 + 1) {
let v = manager.get::<i32, PluginInfo>(i)?;
println!("{v:?}");
let code = format!("manager[{i}].onLoad()");
let code = format!("manager[{i}].on_load()");
lua.load(&code).exec()?;
}
Ok(())