Skip to content

Commit

Permalink
chore: fix style, update dependency, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerxes-2 committed Jul 28, 2024
1 parent 07e6a50 commit d7e8da7
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 134 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "majsoul_max_rs"
version = "0.4.9"
version = "0.4.10"
edition = "2021"
rust-version = "1.80"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

1. 为什么要自动更新liqi和lqc.lqbin?更新失败有什么影响?
- liqi:
- 共有2个文件,包括`liqi.json`和根据其生成的`liqi.proto`,用于解析雀魂protobuf消息
- 共有3个文件,包括`liqi.json`和根据其生成的`liqi.proto``liqi.desc`,用于解析雀魂protobuf消息
- 如果更新失败,可能会导致消息无法解析(如新活动的消息)
- lqc.lqbin:
- 用于获取全部角色、装扮、物品等游戏资源
Expand Down
72 changes: 28 additions & 44 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use reqwest::Client;
use serde::Serialize;
use serde_json::{json, Map, Value as JsonValue};
use std::{future::Future, sync::LazyLock};
use tokio::{sync::mpsc::Receiver, time::sleep};
use tokio::{spawn, sync::mpsc::Receiver, time::sleep};
use tracing::{debug, error, info};

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -38,24 +38,24 @@ pub async fn helper_worker(mut receiver: Receiver<(Bytes, char)>, mut parser: Pa
}
})
.collect::<String>();
debug!("{} {}", direction_char, hex);
debug!("{direction_char} {hex}");
let parsed = parser.parse(buf.clone());
let parsed = match parsed {
Ok(parsed) => parsed,
Err(e) => {
error!("Failed to parse message: {:?}", e);
error!("Failed to parse message: {e}");
continue;
}
};
debug!(
"Method: {}, {}, {:?}, {}",
direction_char, parsed.id, parsed.msg_type, parsed.method_name
"Method: {direction_char}, {}, {:?}, {}",
parsed.id, parsed.msg_type, parsed.method_name
);
if direction_char == '\u{2191}' {
continue;
}
if let Err(e) = process_message(parsed, &mut parser) {
error!("Failed to process message: {:?}", e);
error!("Failed to process message: {e}");
}
}
}
Expand All @@ -72,19 +72,15 @@ fn process_message(mut parsed: LiqiMessage, parser: &mut Parser) -> Result<()> {
}
let json_data = match parsed.method_name.as_ref() {
".lq.ActionPrototype" => {
let name = parsed
.data
.get("name")
.and_then(|n| n.as_str())
let name = parsed.data["name"]
.as_str()
.ok_or(anyhow!("name field invalid"))?;
if !SETTINGS.is_action(name) {
return Ok(());
}
if name == "ActionNewRound" {
parsed
.data
.get_mut("data")
.and_then(|d| d.as_object_mut())
parsed.data["data"]
.as_object_mut()
.ok_or(anyhow!("data field invalid"))?
.insert("md5".to_string(), json!(ARBITRARY_MD5));
}
Expand All @@ -95,23 +91,13 @@ fn process_message(mut parsed: LiqiMessage, parser: &mut Parser) -> Result<()> {
.take()
}
".lq.FastTest.syncGame" => {
let game_restore = parsed
.data
.get("game_restore")
.and_then(|n| n.get("actions"))
.and_then(|n| n.as_array())
let game_restore = parsed.data["game_restore"]["actions"]
.as_array()
.ok_or(anyhow!("actions field invalid"))?;
let mut actions: Vec<Action> = vec![];
for item in game_restore.iter() {
let action_name = item
.get("name")
.and_then(|n| n.as_str())
.ok_or(anyhow!("name field invalid"))?;
let action_data = item
.get("data")
.ok_or(anyhow!("No data field"))?
.as_str()
.unwrap_or_default();
let action_name = item["name"].as_str().ok_or(anyhow!("name field invalid"))?;
let action_data = item["data"].as_str().unwrap_or_default();
if action_data.is_empty() {
let action = Action {
name: action_name.to_string(),
Expand Down Expand Up @@ -144,31 +130,29 @@ fn process_message(mut parsed: LiqiMessage, parser: &mut Parser) -> Result<()> {
};

// post data to API, no verification
let future = CLIENT.post(&SETTINGS.api_url).json(&json_data).send();
let res = CLIENT.post(&SETTINGS.api_url).json(&json_data).send();

handle_future(future);
info!("已发送至助手");
spawn(handle_response(res));
info!("发送至助手……");

if let Some(liqi_data) = json_data.get("liqi") {
let res = CLIENT.post(&SETTINGS.api_url).json(liqi_data).send();
handle_future(res);
info!("已发送立直至助手");
spawn(handle_response(res));
info!("发送立直至助手……");
}

Ok(())
}

fn handle_future(
future: impl Future<Output = Result<reqwest::Response, reqwest::Error>> + Send + 'static,
async fn handle_response(
res: impl Future<Output = Result<reqwest::Response, reqwest::Error>> + Send + 'static,
) {
tokio::spawn(async {
match future.await {
Ok(_) => {
info!("小助手已接收");
}
Err(e) => {
error!("请求失败: {:?}", e);
}
match res.await {
Ok(_) => {
info!("请求小助手已接收");
}
});
Err(e) => {
error!("请求小助手失败: {e}");
}
}
}
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl WebSocketHandler for Handler {
if let WebSocketContext::ServerToClient { .. } = ctx {
if let Some(msg) = self.inject_msg.take() {
if let Err(e) = sink.send(msg).await {
error!("Failed to send injected message: {:?}", e);
error!("Failed to send injected message: {e}");
}
}
}
Expand All @@ -49,16 +49,16 @@ impl WebSocketHandler for Handler {

match sink.send(message).await {
Err(tungstenite::Error::ConnectionClosed) => (),
Err(e) => error!("WebSocket send error: {}", e),
Err(e) => error!("WebSocket send error: {e}"),
_ => (),
}
}
Err(e) => {
error!("WebSocket message error: {}", e);
error!("WebSocket message error: {e}");

match sink.send(Message::Close(None)).await {
Err(tungstenite::Error::ConnectionClosed) => (),
Err(e) => error!("WebSocket close error: {}", e),
Err(e) => error!("WebSocket close error: {e}"),
_ => (),
};

Expand All @@ -79,7 +79,7 @@ impl WebSocketHandler for Handler {
return Some(msg);
}

debug!("{} {}", direction_char, uri);
debug!("{direction_char} {uri}");

if SETTINGS.helper_on() {
if let Message::Binary(ref buf) = msg {
Expand All @@ -88,7 +88,7 @@ impl WebSocketHandler for Handler {
.send((Bytes::copy_from_slice(buf), direction_char))
.await
{
error!("Failed to send message to channel: {:?}", e);
error!("Failed to send message to channel: {e}");
}
}
}
Expand Down Expand Up @@ -154,8 +154,8 @@ async fn main() {
Ok(addr) => addr,
Err(e) => {
error!(
"Failed to parse proxy address: {:?}, url: {}",
e, SETTINGS.proxy_addr
"Failed to parse proxy address: {e}, url: {}",
SETTINGS.proxy_addr
);
return;
}
Expand All @@ -165,7 +165,7 @@ async fn main() {
info!("自动更新liqi已开启");
let mut new_settings = SETTINGS.clone();
match new_settings.update().await {
Err(e) => warn!("更新liqi失败: {}", e),
Err(e) => warn!("更新liqi失败: {e}"),
Ok(true) => {
info!("liqi更新成功, 请重启程序");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
Expand All @@ -191,13 +191,13 @@ async fn main() {
info!("自动更新mod已开启");
let mut new_mod_settings = MOD_SETTINGS.read().await.clone();
match new_mod_settings.get_lqc().await {
Err(e) => warn!("更新mod失败: {}", e),
Ok(false) => {}
Err(e) => warn!("更新mod失败: {e}"),
Ok(true) => {
info!("mod更新成功, 请重启程序");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
return;
}
Ok(false) => (),
}
}
Some(Arc::new(Modder::new().await))
Expand Down Expand Up @@ -225,6 +225,6 @@ async fn main() {
}

if let Err(e) = proxy.start().await {
error!("{}", e);
error!("{e}");
}
}
Loading

0 comments on commit d7e8da7

Please sign in to comment.