Skip to content

Commit

Permalink
feat: oidb_command attr (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn authored Feb 8, 2025
1 parent b0632c1 commit 7497cc2
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 23 deletions.
49 changes: 47 additions & 2 deletions mania-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use md5::{Digest, Md5};
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::parse::Parse;
use syn::{
DeriveInput, ItemFn, ItemStruct, LitStr, Path, parse_macro_input, punctuated::Punctuated,
DeriveInput, ItemFn, ItemStruct, LitInt, LitStr, Path, Token, parse_macro_input,
punctuated::Punctuated,
};

#[proc_macro_attribute]
pub fn commend(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn command(attr: TokenStream, item: TokenStream) -> TokenStream {
let command_str = parse_macro_input!(attr as LitStr);
let command_value = command_str.value();
let input_struct = parse_macro_input!(item as ItemStruct);
Expand All @@ -26,6 +29,48 @@ pub fn commend(attr: TokenStream, item: TokenStream) -> TokenStream {
expanded.into()
}

struct OidbCommandArgs {
command: LitInt,
_comma: Token![,],
sub_command: LitInt,
}

impl Parse for OidbCommandArgs {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(Self {
command: input.parse()?,
_comma: input.parse()?,
sub_command: input.parse()?,
})
}
}

#[proc_macro_attribute]
pub fn oidb_command(attr: TokenStream, item: TokenStream) -> TokenStream {
let args = parse_macro_input!(attr as OidbCommandArgs);
let command = format!(
"OidbSvcTrpcTcp.0x{:x}_{}",
args.command.base10_parse::<u32>().unwrap(),
args.sub_command.base10_parse::<u32>().unwrap()
);
let command_value = LitStr::new(&command, Span::call_site());
let input_struct = parse_macro_input!(item as ItemStruct);
let ident = &input_struct.ident;
let expanded = quote! {
#input_struct
impl CECommandMarker for #ident {
const COMMAND: &'static str = #command_value;
}
inventory::submit! {
ClientEventRegistry {
command: <#ident as CECommandMarker>::COMMAND,
parse_fn: <#ident as ClientEvent>::parse,
}
}
};
expanded.into()
}

#[proc_macro_derive(ServerEvent)]
pub fn derive_server_event(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub(crate) mod prelude {
pub use crate::dda;
pub use bytes::Bytes;
pub use inventory;
pub use mania_macros::{ServerEvent, commend};
pub use mania_macros::{ServerEvent, command, oidb_command};
pub use num_enum::TryFromPrimitive;
pub use prost::Message;
pub use std::convert::TryFrom;
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/login/trans_emp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct NTLoginHttpResponse {
pub face_update_time: i64,
}

#[commend("wtlogin.trans_emp")]
#[command("wtlogin.trans_emp")]
#[derive(Debug, ServerEvent)]
pub struct TransEmp {
pub status: TransEmpStatus,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/login/wtlogin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use chrono::Utc;
use md5::{Digest, Md5};
use std::sync::Arc;

#[commend("wtlogin.login")]
#[command("wtlogin.login")]
#[derive(Debug, Default, ServerEvent)]
pub struct WtLogin {
pub code: i32,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/file_c2c_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::core::protos::service::oidb::{
OidbSvcTrpcTcp0xE371200, OidbSvcTrpcTcp0xE371200body, OidbSvcTrpcTcp0xE371200response,
};

#[commend("OidbSvcTrpcTcp.0xe37_1200")]
#[oidb_command(0xe37, 1200)]
#[derive(Debug, ServerEvent, Default)]
pub struct FileC2CDownloadEvent {
pub sender_uid: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/file_group_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::core::protos::service::oidb::{
OidbSvcTrpcTcp0x6D6, OidbSvcTrpcTcp0x6D6Download, OidbSvcTrpcTcp0x6D6Response,
};

#[commend("OidbSvcTrpcTcp.0x6d6_2")]
#[oidb_command(0x6d6, 2)]
#[derive(Debug, ServerEvent, Default)]
pub struct FileGroupDownloadEvent {
pub group_uin: u32,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/image_c2c_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::protos::service::oidb::{
Ntv2RichMediaReq, Ntv2RichMediaResp, SceneInfo, VideoDownloadExt,
};

#[commend("OidbSvcTrpcTcp.0x11c5_200")]
#[oidb_command(0x11c5, 200)]
#[derive(Debug, ServerEvent, Default)]
pub struct ImageC2CDownloadEvent {
pub self_uid: String,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/image_group_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::protos::service::oidb::{
Ntv2RichMediaReq, Ntv2RichMediaResp, SceneInfo, VideoDownloadExt,
};

#[commend("OidbSvcTrpcTcp.0x11c4_200")]
#[oidb_command(0x11c4, 200)]
#[derive(Debug, ServerEvent, Default)]
pub struct ImageGroupDownloadEvent {
pub group_uin: u32,
Expand Down
4 changes: 2 additions & 2 deletions mania/src/core/event/message/multi_msg_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::core::protos::message::{
use crate::message::chain::MessageChain;
use crate::message::packer::MessagePacker;
use crate::utility::compress::gzip;
use mania_macros::{ServerEvent, commend};
use mania_macros::{ServerEvent, command};

#[commend("trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg")]
#[command("trpc.group.long_msg_interface.MsgService.SsoRecvLongMsg")]
#[derive(Debug, ServerEvent, Default)]
pub struct MultiMsgDownloadEvent {
pub uid: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/push_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum Event0x210SubType {
GroupKickNotice = 212,
}

#[commend("trpc.msg.olpush.OlPushService.MsgPush")]
#[command("trpc.msg.olpush.OlPushService.MsgPush")]
#[derive(Debug, ServerEvent)]
pub struct PushMessageEvent {
pub chain: Option<MessageChain>,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/record_c2c_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::protos::service::oidb::{
Ntv2RichMediaReq, Ntv2RichMediaResp, SceneInfo, VideoDownloadExt,
};

#[commend("OidbSvcTrpcTcp.0x126d_200")]
#[oidb_command(0x126d, 200)]
#[derive(Debug, ServerEvent, Default)]
pub struct RecordC2CDownloadEvent {
pub self_uid: String,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/record_group_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::core::protos::service::oidb::{
Ntv2RichMediaReq, Ntv2RichMediaResp, SceneInfo, VideoDownloadExt,
};

#[commend("OidbSvcTrpcTcp.0x126e_200")]
#[oidb_command(0x126e, 200)]
#[derive(Debug, ServerEvent, Default)]
pub struct RecordGroupDownloadEvent {
pub group_uin: u32,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/video_c2c_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::core::protos::service::oidb::{
};

/// legacy: VideoDownloadEvent
#[commend("OidbSvcTrpcTcp.0x11e9_200")]
#[oidb_command(0x11e9, 200)]
#[derive(Debug, ServerEvent, Default)]
pub struct VideoC2CDownloadEvent {
pub video_url: String,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/message/video_group_download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::core::protos::service::oidb::{
VideoDownloadExt,
};

#[commend("OidbSvcTrpcTcp.0x11ea_200")]
#[oidb_command(0x11ea, 200)]
#[derive(Debug, ServerEvent, Default)]
pub struct VideoGroupDownloadEvent {
pub video_url: String,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/alive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::core::event::prelude::*;

#[commend("Heartbeat.Alive")]
#[command("Heartbeat.Alive")]
#[derive(Debug, ServerEvent)]
pub struct AliveEvent {
pub test: u32, // TODO: remove it
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/fetch_friend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::protos::service::oidb::{
use crate::entity::bot_friend::{BotFriend, BotFriendGroup};
use std::collections::HashMap;

#[commend("OidbSvcTrpcTcp.0xfd4_1")]
#[oidb_command(0xfd4, 1)]
#[derive(Debug, ServerEvent, Default)]
pub struct FetchFriendsEvent {
pub friends: Vec<BotFriend>,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/fetch_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::protos::service::oidb::{
use crate::entity::bot_group_member::{BotGroupMember, GroupMemberPermission};
use chrono::DateTime;

#[commend("OidbSvcTrpcTcp.0xfe7_3")]
#[oidb_command(0xfe7, 3)]
#[derive(Debug, ServerEvent, Default)]
pub struct FetchMembersEvent {
pub group_uin: u32,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/fetch_rkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::core::protos::service::oidb::{
ClientMeta, CommonHead, DownloadRKeyReq, MultiMediaReqHead, Ntv2RichMediaReq, SceneInfo,
};

#[commend("OidbSvcTrpcTcp.0x9067_202")]
#[oidb_command(0x9067, 202)]
#[derive(Debug, ServerEvent)]
pub struct FetchRKeyEvent;

Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/info_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::protos::system::{
use crate::utility::random_gen::RandomGenerator;
use prost::Message;

#[commend("trpc.msg.register_proxy.RegisterProxy.SsoInfoSync")]
#[command("trpc.msg.register_proxy.RegisterProxy.SsoInfoSync")]
#[derive(Debug, ServerEvent)]
pub struct InfoSyncEvent;

Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/kick_nt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::event::prelude::*;
use crate::core::protos::system::ServiceKickNtResponse;

#[commend("trpc.qq_new_tech.status_svc.StatusService.KickNT")]
#[command("trpc.qq_new_tech.status_svc.StatusService.KickNT")]
#[derive(Debug, ServerEvent)]
pub struct KickNTEvent {
tips: String,
Expand Down
2 changes: 1 addition & 1 deletion mania/src/core/event/system/nt_sso_alive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::event::prelude::*;
use crate::core::protos::system::NtSsoHeartBeat;

#[commend("trpc.qq_new_tech.status_svc.StatusService.SsoHeartBeat")]
#[command("trpc.qq_new_tech.status_svc.StatusService.SsoHeartBeat")]
#[derive(Debug, ServerEvent)]
pub struct NtSsoAliveEvent;

Expand Down

0 comments on commit 7497cc2

Please sign in to comment.