Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve get command performance #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 3 additions & 26 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<game_util::ChatMessageReceiver> = Lazy::new(|| {
let mut instance = game_util::ChatMessageReceiver::new();
instance.set_prefix_filter(CHAT_COMMAND_PREFIX);
instance
});
static DAMAGE_COLLECTOR: Lazy<Arc<DamageCollector>> = Lazy::new(|| Arc::new(DamageCollector::new()));

pub struct DamageCollector {
Expand Down Expand Up @@ -172,20 +163,6 @@ impl DamageData {
}
}

pub fn get_chat_command() -> Option<ChatCommand> {
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,
Expand Down
7 changes: 3 additions & 4 deletions src/game_context.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::game;
use mhw_toolkit::game_util::WeaponType;
use mhw_toolkit::game_util::{ChatMessageReceiver, WeaponType};

/// 游戏内状态记录上下文
///
/// 记录当前游戏内某些值
#[derive(Clone, Debug)]
pub struct Context {
pub plugin_enabled: bool,
pub chat_command: Option<ChatCommand>,
pub command_rev: ChatMessageReceiver<40, 4>,
pub quest_state: i32,
pub longsword_level: i32,
pub weapon_type: WeaponType,
Expand All @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down
61 changes: 34 additions & 27 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,44 @@ pub async fn event_listener(tx: Sender<Event>) {
// 更新上下文
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;
}
Expand Down