From 57674de6b850cc9db0ed86d609325a28db97c853 Mon Sep 17 00:00:00 2001 From: Jianqoq Date: Sun, 14 Apr 2024 22:26:11 -0400 Subject: [PATCH] improve get command performance --- Cargo.lock | 1 - src/game.rs | 29 +++------------------ src/game_context.rs | 7 +++--- src/handlers.rs | 61 +++++++++++++++++++++++++-------------------- 4 files changed, 40 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 894e013..1b05ead 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -611,7 +611,6 @@ dependencies = [ "strum", "strum_macros", "winapi", - "windows", ] [[package]] diff --git a/src/game.rs b/src/game.rs index dfea38b..8060376 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,15 +1,12 @@ use std::{collections::HashMap, sync::Arc, time::Duration}; use chrono::{DateTime, TimeDelta, Utc}; -use log::{debug, info}; -use mhw_toolkit::{ - game_util::{self, WeaponType}, - util, -}; +use log::debug; +use mhw_toolkit::{game_util::WeaponType, util}; use once_cell::sync::Lazy; use tokio::sync::{Mutex, Notify}; -use crate::game_context::{ChargeBlade, ChatCommand, Fsm, InsectGlaive, SpecializedTool}; +use crate::game_context::{ChargeBlade, Fsm, InsectGlaive, SpecializedTool}; const QUEST_BASE: *const i32 = 0x14500CAF0 as *const i32; const QUEST_OFFSETS: isize = 0x38; @@ -26,12 +23,6 @@ const WEAPON_DATA_OFFSETS: &[isize] = &[0x50, 0x76B0]; const CHARGE_BLADE_BASE: *const i32 = 0x1450EA510 as *const i32; const CHARGE_BLADE_MAX_PHIALS_OFFSETS: &[isize] = &[0x110, 0x98, 0x58, 0x5F98]; -const CHAT_COMMAND_PREFIX: &str = "!mas "; -static CHAT_MESSAGE_RECV: Lazy = Lazy::new(|| { - let mut instance = game_util::ChatMessageReceiver::new(); - instance.set_prefix_filter(CHAT_COMMAND_PREFIX); - instance -}); static DAMAGE_COLLECTOR: Lazy> = Lazy::new(|| Arc::new(DamageCollector::new())); pub struct DamageCollector { @@ -172,20 +163,6 @@ impl DamageData { } } -pub fn get_chat_command() -> Option { - if let Some(msg) = CHAT_MESSAGE_RECV.try_recv() { - debug!("接收用户命令消息:{}", msg); - let cmd = ChatCommand::from_str(&msg[CHAT_COMMAND_PREFIX.len()..]); - if cmd.is_none() { - info!("无效的命令:{}", msg); - game_util::send_chat_message("无效的命令"); - } - cmd - } else { - None - } -} - pub fn get_quest_state() -> i32 { match util::get_value_with_offset(QUEST_BASE, &[QUEST_OFFSETS]) { Some(qs) => qs, diff --git a/src/game_context.rs b/src/game_context.rs index 321526c..feb7dba 100644 --- a/src/game_context.rs +++ b/src/game_context.rs @@ -1,5 +1,5 @@ use crate::game; -use mhw_toolkit::game_util::WeaponType; +use mhw_toolkit::game_util::{ChatMessageReceiver, WeaponType}; /// 游戏内状态记录上下文 /// @@ -7,7 +7,7 @@ use mhw_toolkit::game_util::WeaponType; #[derive(Clone, Debug)] pub struct Context { pub plugin_enabled: bool, - pub chat_command: Option, + pub command_rev: ChatMessageReceiver<40, 4>, pub quest_state: i32, pub longsword_level: i32, pub weapon_type: WeaponType, @@ -24,7 +24,6 @@ impl Context { pub fn update_context(&mut self) { self.store_last_context(); - self.chat_command = game::get_chat_command(); self.quest_state = game::get_quest_state(); self.weapon_type = game::get_weapon_type(); self.fsm = game::get_fsm(); @@ -57,7 +56,7 @@ impl Default for Context { fn default() -> Self { Self { plugin_enabled: true, - chat_command: Default::default(), + command_rev: ChatMessageReceiver::<40, 4>::new([33, 109, 97, 115]), // [33, 109, 97, 115] => "!mas" quest_state: Default::default(), longsword_level: Default::default(), weapon_type: Default::default(), diff --git a/src/handlers.rs b/src/handlers.rs index 27cffe2..aa77dfb 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -25,37 +25,44 @@ pub async fn event_listener(tx: Sender) { // 更新上下文 ctx.update_context(); - // 检查事件 - // 消息事件 - if let Some(cmd) = &ctx.chat_command { + if ctx.command_rev.try_read_command() { + let cmd = ChatCommand::from_str(ctx.command_rev.read_command()); match cmd { - ChatCommand::ReloadConfig => { - debug!("on {}", "ChatCommand::ReloadConfig"); - info!("接收用户命令:{:?}", cmd); - let trigger_mgr = match load_triggers().await { - Ok(mgr) => mgr, - Err(e) => { - error!("加载配置失败:{}", e); - continue; - } - }; - game_util::show_game_message("已重载配置"); - tx_send_or_break!(tx.send(Event::LoadTriggers { trigger_mgr })); - } - ChatCommand::Enable => { - debug!("on {}", "ChatCommand::Enable"); - info!("接收用户命令:{:?}", cmd); - game_util::show_game_message("已启用插件"); - ctx.plugin_enabled = true; - } - ChatCommand::Disable => { - debug!("on {}", "ChatCommand::Disable"); - info!("接收用户命令:{:?}", cmd); - game_util::show_game_message("已禁用插件"); - ctx.plugin_enabled = false; + Some(cmd) => match cmd { + ChatCommand::ReloadConfig => { + debug!("on {}", "ChatCommand::ReloadConfig"); + info!("接收用户命令:{:?}", cmd); + let trigger_mgr = match load_triggers().await { + Ok(mgr) => mgr, + Err(e) => { + error!("加载配置失败:{}", e); + continue; + } + }; + game_util::show_game_message("已重载配置"); + tx_send_or_break!(tx.send(Event::LoadTriggers { trigger_mgr })); + } + ChatCommand::Enable => { + debug!("on {}", "ChatCommand::Enable"); + info!("接收用户命令:{:?}", cmd); + game_util::show_game_message("已启用插件"); + ctx.plugin_enabled = true; + } + ChatCommand::Disable => { + debug!("on {}", "ChatCommand::Disable"); + info!("接收用户命令:{:?}", cmd); + game_util::show_game_message("已禁用插件"); + ctx.plugin_enabled = false; + } + }, + None => { + info!("无效的命令:{}", ctx.command_rev.read_command()); + game_util::send_chat_message("无效的命令"); } } } + // 检查事件 + // 消息事件 if !ctx.plugin_enabled { continue; }