feat: commit lua code

This commit is contained in:
mrxiaozhuox committed 2022-07-25 08:10:06 +08:00
1 parent ca8855b168
commit bd2853a194
7 files changed
+135 -37

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
use log;
use mlua::UserData;
pub struct PluginLogger;
impl UserData for PluginLogger {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_function("trace", |_, info: String| {
log::trace!("{}", info);
Ok(())
});
methods.add_function("info", |_, info: String| {
log::info!("{}", info);
Ok(())
});
methods.add_function("debug", |_, info: String| {
log::debug!("{}", info);
Ok(())
});
methods.add_function("warn", |_, info: String| {
log::warn!("{}", info);
Ok(())
});
methods.add_function("error", |_, info: String| {
log::error!("{}", info);
Ok(())
});
}
}
+53
View File
@@ -0,0 +1,53 @@
use std::{fs::create_dir_all, path::PathBuf};
use anyhow::Ok;
use mlua::Lua;
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
use crate::tools::app_path;
pub mod log;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginConfig {
available: bool,
required: Vec<String>,
}
pub struct PluginManager {
lua: Lua,
}
impl PluginManager {
pub fn init(config: &PluginConfig) -> Option<Self> {
if config.available {
return None;
}
let lua = Lua::new();
let manager = lua.create_table().ok()?;
let plugin_dir = Self::init_plugin_dir();
for entry in WalkDir::new(plugin_dir).into_iter().filter_map(|e| e.ok()) {
let plugin_dir = entry.path().to_path_buf();
if plugin_dir.is_dir() {
}
}
lua.globals().set("manager", manager).ok()?;
Some(Self { lua })
}
fn init_plugin_dir() -> PathBuf {
let app_path = app_path();
let plugin_path = app_path.join("plugins");
if !plugin_path.is_dir() {
create_dir_all(&plugin_path).unwrap();
}
plugin_path
}
}