Skip to content

Commit

Permalink
fix【复制命令】对其它包的干扰
Browse files Browse the repository at this point in the history
  • Loading branch information
super1207 committed Jun 23, 2024
1 parent f9ee1ac commit 799efe8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ lazy_static! {
pub static ref G_MSG_ID_MAP:RwLock<HashMap<String,VecDeque<String>>> = RwLock::new(HashMap::new());
// 用于记录自定义的命令(x)
pub static ref G_CMD_MAP:RwLock<HashMap<String,HashMap<String, String>>> = RwLock::new(HashMap::new());
// 用于记录命令
// 用于记录命令(x)
pub static ref G_CMD_FUN_MAP:RwLock<HashMap<String, fn(&mut RedLang,&[String]) -> Result<Option<String>, Box<dyn std::error::Error>>>> = RwLock::new(HashMap::new());
// 异步事件循环
pub static ref RT_PTR:Arc<tokio::runtime::Runtime> = Arc::new(tokio::runtime::Runtime::new().unwrap());
Expand Down Expand Up @@ -270,6 +270,20 @@ pub fn del_pkg_memory(pkg_name:&str) {
lk.remove(&key);
}
}
// 删除自定义的内置命令
{
let mut lk = G_CMD_FUN_MAP.write().unwrap();
let mut to_remove_key = vec![];
for key in &*lk {
if key.0.starts_with(&format!("{pkg_name}eb4d8f3e-1c82-653b-5b26-3be3abb007bc")) {
to_remove_key.push(key.0.to_owned());
}
}
for key in &to_remove_key {
lk.remove(key);
}
}

}


Expand Down
61 changes: 36 additions & 25 deletions src/redlang/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,29 @@ pub fn get_random() -> Result<usize, getrandom::Error> {
}


fn get_core_cmd(cmd:&str,pkg_name:&str) -> Option<fn(&mut RedLang, &[String]) -> Result<Option<String>, Box<dyn std::error::Error>>> {
let mut rfun;
let cmd_t = cmd.to_uppercase();
let r = crate::G_CMD_FUN_MAP.read().unwrap();

// 先查看包对应的命令
let cmd_tt = format!("{pkg_name}eb4d8f3e-1c82-653b-5b26-3be3abb007bc{cmd_t}");
rfun = match r.get(&cmd_tt) {
Some(fun) => Some(fun.clone()),
None => None,
};

// 再查看内置命令
if rfun.is_none() {
rfun = match r.get(&cmd_t) {
Some(fun) => Some(fun.clone()),
None => None,
};
}
rfun
}


pub fn init_core_fun_map() {
fn add_fun(k_vec:Vec<&str>,fun:fn(&mut RedLang,params: &[String]) -> Result<Option<String>, Box<dyn std::error::Error>>){
let mut w = crate::G_CMD_FUN_MAP.write().unwrap();
Expand Down Expand Up @@ -1618,26 +1641,22 @@ pub fn init_core_fun_map() {
let old_cmd = self_t.get_param(params, 0)?;
let new_cmd = self_t.get_param(params, 1)?;

let exret;
let pkg_name = &self_t.pkg_name;

// 如果旧命令不存在,则什么也不做
{
let cmd_t = old_cmd.to_uppercase();
let r = crate::G_CMD_FUN_MAP.read()?;
exret = match r.get(&cmd_t) {
Some(fun) => Some(fun.clone()),
None => None,
};

if exret == None {
return Ok(Some("".to_string()));
}
let exret = get_core_cmd(&old_cmd, pkg_name);
if exret == None {
return Ok(Some("".to_string()));
}
// 构造新命令
let fun = exret.unwrap();
let mut w = crate::G_CMD_FUN_MAP.write().unwrap();
let k = new_cmd.to_uppercase();
let k_t = crate::mytool::str_to_ft(&k);
w.insert(k.clone(), fun);
w.insert(k_t, fun);
let k_t: String = crate::mytool::str_to_ft(&k);

// 添加新命令
let mut w = crate::G_CMD_FUN_MAP.write().unwrap();
w.insert(format!("{pkg_name}eb4d8f3e-1c82-653b-5b26-3be3abb007bc{k}"), fun);
w.insert(format!("{pkg_name}eb4d8f3e-1c82-653b-5b26-3be3abb007bc{k_t}"), fun);
return Ok(Some("".to_string()));
});
add_fun(vec!["进制转化","进制转换"],|self_t,params|{
Expand Down Expand Up @@ -1971,15 +1990,7 @@ let k = &*self.exmap;

// 执行核心命令与拓展命令
let exret;
let rfun;
{
let cmd_t = cmd.to_uppercase();
let r = crate::G_CMD_FUN_MAP.read()?;
rfun = match r.get(&cmd_t) {
Some(fun) => Some(fun.clone()),
None => None,
};
}
let rfun = get_core_cmd(cmd,&self.pkg_name);

exret = match rfun {
Some(fun) => fun(self,params)?,
Expand Down

0 comments on commit 799efe8

Please sign in to comment.