From 12b0c700699763a80addaf7f0341dd140de8e124 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sat, 19 Nov 2022 21:18:10 -0500 Subject: [PATCH] Sync mystery_gift_link --- include/mystery_gift_server.h | 37 ++--- src/mystery_gift_client.c | 14 +- src/mystery_gift_link.c | 291 ++++++++++++++++++---------------- src/mystery_gift_server.c | 10 +- 4 files changed, 178 insertions(+), 174 deletions(-) diff --git a/include/mystery_gift_server.h b/include/mystery_gift_server.h index 2a9070afb..7c71ed856 100644 --- a/include/mystery_gift_server.h +++ b/include/mystery_gift_server.h @@ -5,11 +5,11 @@ #define ME_SEND_BUF_SIZE 0x400 -struct mevent_srv_sub +struct MysteryGiftLink { - s32 seqno; - u8 sendPlayerNo; - u8 recvPlayerNo; + s32 state; + u8 sendPlayerId; + u8 recvPlayerId; u16 recvIdent; u16 recvCounter; u16 recvCRC; @@ -18,17 +18,10 @@ struct mevent_srv_sub u16 sendCounter; u16 sendCRC; u16 sendSize; - void *recvBfr; - const void *sendBfr; - u32 (*recvFunc)(struct mevent_srv_sub *); - u32 (*sendFunc)(struct mevent_srv_sub *); -}; - -struct send_recv_header -{ - u16 ident; - u16 crc; - u16 size; + void *recvBuffer; + const void *sendBuffer; + u32 (*recvFunc)(struct MysteryGiftLink *); + u32 (*sendFunc)(struct MysteryGiftLink *); }; struct mevent_client_cmd @@ -63,7 +56,7 @@ struct mevent_client void *recvBuffer; struct mevent_client_cmd * cmdBuffer; void *buffer; - struct mevent_srv_sub manager; + struct MysteryGiftLink manager; }; struct mevent_server_cmd @@ -108,14 +101,14 @@ struct mevent_srv_common void *sendBuffer2; u32 sendBuffer2Size; u32 sendWord; - struct mevent_srv_sub manager; + struct MysteryGiftLink manager; }; -u32 mevent_srv_sub_recv(struct mevent_srv_sub * svr); -u32 mevent_srv_sub_send(struct mevent_srv_sub * svr); -void mevent_srv_sub_init(struct mevent_srv_sub * svr, u32 sendPlayerNo, u32 recvPlayerNo); -void mevent_srv_sub_init_send(struct mevent_srv_sub * svr, u32 ident, const void *src, u32 size); -void mevent_srv_sub_init_recv(struct mevent_srv_sub * svr, u32 ident, void *dest); +u32 MysteryGiftLink_Recv(struct MysteryGiftLink * link); +u32 MysteryGiftLink_Send(struct MysteryGiftLink * link); +void MysteryGiftLink_Init(struct MysteryGiftLink * link, u32 sendPlayerId, u32 recvPlayerId); +void MysteryGiftLink_InitSend(struct MysteryGiftLink * link, u32 ident, const void *src, u32 size); +void MysteryGiftLink_InitRecv(struct MysteryGiftLink * link, u32 ident, void *dest); void mevent_client_do_init(void); u32 mevent_client_do_exec(u16 * a0); diff --git a/src/mystery_gift_client.c b/src/mystery_gift_client.c index 48fc8ffc3..5844f80e2 100644 --- a/src/mystery_gift_client.c +++ b/src/mystery_gift_client.c @@ -62,7 +62,7 @@ static void mevent_client_init(struct mevent_client * svr, u32 sendPlayerNo, u32 svr->recvBuffer = AllocZeroed(ME_SEND_BUF_SIZE); svr->cmdBuffer = AllocZeroed(ME_SEND_BUF_SIZE); svr->buffer = AllocZeroed(0x40); - mevent_srv_sub_init(&svr->manager, sendPlayerNo, recvPlayerNo); + MysteryGiftLink_Init(&svr->manager, sendPlayerNo, recvPlayerNo); } static void mevent_client_free_resources(struct mevent_client * svr) @@ -83,7 +83,7 @@ static void mevent_client_send_word(struct mevent_client * svr, u32 ident, u32 w { CpuFill32(0, svr->sendBuffer, ME_SEND_BUF_SIZE); *(u32 *)svr->sendBuffer = word; - mevent_srv_sub_init_send(&svr->manager, ident, svr->sendBuffer, sizeof(u32)); + MysteryGiftLink_InitSend(&svr->manager, ident, svr->sendBuffer, sizeof(u32)); } static u32 client_mainseq_0(struct mevent_client * svr) @@ -105,7 +105,7 @@ static u32 client_mainseq_1(struct mevent_client * svr) static u32 client_mainseq_2(struct mevent_client * svr) { // do recv - if (mevent_srv_sub_recv(&svr->manager)) + if (MysteryGiftLink_Recv(&svr->manager)) { svr->mainseqno = 4; svr->flag = 0; @@ -116,7 +116,7 @@ static u32 client_mainseq_2(struct mevent_client * svr) static u32 client_mainseq_3(struct mevent_client * svr) { // do send - if (mevent_srv_sub_send(&svr->manager)) + if (MysteryGiftLink_Send(&svr->manager)) { svr->mainseqno = 4; svr->flag = 0; @@ -139,7 +139,7 @@ static u32 client_mainseq_4(struct mevent_client * svr) svr->flag = 0; break; case 2: - mevent_srv_sub_init_recv(&svr->manager, cmd->parameter, svr->recvBuffer); + MysteryGiftLink_InitRecv(&svr->manager, cmd->parameter, svr->recvBuffer); svr->mainseqno = 2; svr->flag = 0; break; @@ -148,7 +148,7 @@ static u32 client_mainseq_4(struct mevent_client * svr) svr->flag = 0; break; case 20: - mevent_srv_sub_init_send(&svr->manager, 0x14, svr->sendBuffer, 0); + MysteryGiftLink_InitSend(&svr->manager, 0x14, svr->sendBuffer, 0); svr->mainseqno = 3; svr->flag = 0; break; @@ -189,7 +189,7 @@ static u32 client_mainseq_4(struct mevent_client * svr) return 4; case 8: MysteryGift_LoadLinkGameData(svr->sendBuffer); - mevent_srv_sub_init_send(&svr->manager, 0x11, svr->sendBuffer, sizeof(struct MysteryGiftLinkGameData)); + MysteryGiftLink_InitSend(&svr->manager, 0x11, svr->sendBuffer, sizeof(struct MysteryGiftLinkGameData)); break; case 14: mevent_client_send_word(svr, 0x13, svr->param); diff --git a/src/mystery_gift_link.c b/src/mystery_gift_link.c index f90151d6b..0ef2eb84a 100644 --- a/src/mystery_gift_link.c +++ b/src/mystery_gift_link.c @@ -4,200 +4,211 @@ #include "link_rfu.h" #include "mystery_gift_server.h" -static u32 mevent_receive_func(struct mevent_srv_sub *); -static u32 mevent_send_func(struct mevent_srv_sub *); +/* + Handles the link connection functions used by the Mystery Gift client/server. + Note: MysteryGiftLink is shortened to MGL for internal functions. +*/ -u32 mevent_srv_sub_recv(struct mevent_srv_sub * svr) +struct SendRecvHeader { - return svr->recvFunc(svr); + u16 ident; + u16 crc; + u16 size; +}; + +static u32 MGL_Receive(struct MysteryGiftLink *); +static u32 MGL_Send(struct MysteryGiftLink *); + +u32 MysteryGiftLink_Recv(struct MysteryGiftLink * link) +{ + return link->recvFunc(link); } -u32 mevent_srv_sub_send(struct mevent_srv_sub * svr) +u32 MysteryGiftLink_Send(struct MysteryGiftLink * link) { - return svr->sendFunc(svr); + return link->sendFunc(link); } -void mevent_srv_sub_init(struct mevent_srv_sub * svr, u32 sendPlayerNo, u32 recvPlayerNo) +void MysteryGiftLink_Init(struct MysteryGiftLink * link, u32 sendPlayerId, u32 recvPlayerId) { - svr->sendPlayerNo = sendPlayerNo; - svr->recvPlayerNo = recvPlayerNo; - svr->seqno = 0; - svr->sendCRC = 0; - svr->sendSize = 0; - svr->sendCounter = 0; - svr->recvCRC = 0; - svr->recvSize = 0; - svr->recvCounter = 0; - svr->sendBfr = NULL; - svr->recvBfr = NULL; - svr->sendFunc = mevent_send_func; - svr->recvFunc = mevent_receive_func; + link->sendPlayerId = sendPlayerId; + link->recvPlayerId = recvPlayerId; + link->state = 0; + link->sendCRC = 0; + link->sendSize = 0; + link->sendCounter = 0; + link->recvCRC = 0; + link->recvSize = 0; + link->recvCounter = 0; + link->sendBuffer = NULL; + link->recvBuffer = NULL; + link->sendFunc = MGL_Send; + link->recvFunc = MGL_Receive; } -void mevent_srv_sub_init_send(struct mevent_srv_sub * svr, u32 ident, const void *src, u32 size) +void MysteryGiftLink_InitSend(struct MysteryGiftLink * link, u32 ident, const void *src, u32 size) { - svr->seqno = 0; - svr->sendIdent = ident; - svr->sendCounter = 0; - svr->sendCRC = 0; + link->state = 0; + link->sendIdent = ident; + link->sendCounter = 0; + link->sendCRC = 0; if (size != 0) - svr->sendSize = size; + link->sendSize = size; else - svr->sendSize = ME_SEND_BUF_SIZE; - svr->sendBfr = src; + link->sendSize = ME_SEND_BUF_SIZE; + link->sendBuffer = src; } -void mevent_srv_sub_init_recv(struct mevent_srv_sub * svr, u32 ident, void *dest) +void MysteryGiftLink_InitRecv(struct MysteryGiftLink * link, u32 ident, void *dest) { - svr->seqno = 0; - svr->recvIdent = ident; - svr->recvCounter = 0; - svr->recvCRC = 0; - svr->recvSize = 0; - svr->recvBfr = dest; + link->state = 0; + link->recvIdent = ident; + link->recvCounter = 0; + link->recvCRC = 0; + link->recvSize = 0; + link->recvBuffer = dest; } -static void mevent_recv_block(u32 recv_idx, void *dest, size_t size) +static void MGL_ReceiveBlock(u32 playerId, void *dest, size_t size) { - memcpy(dest, gBlockRecvBuffer[recv_idx], size); + memcpy(dest, gBlockRecvBuffer[playerId], size); } -static bool32 mevent_has_received(u32 recv_idx) +static bool32 MGL_HasReceived(u32 playerId) { - if ((GetBlockReceivedStatus() >> recv_idx) & 1) + if ((GetBlockReceivedStatus() >> playerId) & 1) return TRUE; else return FALSE; } -static void mevent_reset_recv(u32 recv_idx) +static void MGL_ResetReceived(u32 playerId) { - ResetBlockReceivedFlag(recv_idx); + ResetBlockReceivedFlag(playerId); } -static bool32 mevent_receive_func(struct mevent_srv_sub * svr) +static bool32 MGL_Receive(struct MysteryGiftLink * link) { - struct send_recv_header header; + struct SendRecvHeader header; - switch (svr->seqno) + switch (link->state) { - case 0: - if (mevent_has_received(svr->recvPlayerNo)) + case 0: + if (MGL_HasReceived(link->recvPlayerId)) + { + MGL_ReceiveBlock(link->recvPlayerId, &header, sizeof(header)); + link->recvSize = header.size; + link->recvCRC = header.crc; + if (link->recvSize > ME_SEND_BUF_SIZE) { - mevent_recv_block(svr->recvPlayerNo, &header, sizeof(header)); - svr->recvSize = header.size; - svr->recvCRC = header.crc; - if (svr->recvSize > ME_SEND_BUF_SIZE) - { - LinkRfu_FatalError(); - return FALSE; - } - else if (svr->recvIdent != header.ident) - { - LinkRfu_FatalError(); - return FALSE; - } - else - { - svr->recvCounter = 0; - mevent_reset_recv(svr->recvPlayerNo); - ++svr->seqno; - } + LinkRfu_FatalError(); + return FALSE; } - break; - case 1: - if (mevent_has_received(svr->recvPlayerNo)) - { - size_t blocksiz = svr->recvCounter * 252; - if (svr->recvSize - blocksiz <= 252) - { - mevent_recv_block(svr->recvPlayerNo, svr->recvBfr + blocksiz, svr->recvSize - blocksiz); - ++svr->recvCounter; - ++svr->seqno; - } - else - { - mevent_recv_block(svr->recvPlayerNo, svr->recvBfr + blocksiz, 252); - ++svr->recvCounter; - } - mevent_reset_recv(svr->recvPlayerNo); - } - break; - case 2: - if (CalcCRC16WithTable(svr->recvBfr, svr->recvSize) != svr->recvCRC) + else if (link->recvIdent != header.ident) { LinkRfu_FatalError(); return FALSE; } else { - svr->seqno = 0; - return TRUE; + link->recvCounter = 0; + MGL_ResetReceived(link->recvPlayerId); + link->state++; } - break; - + } + break; + case 1: + if (MGL_HasReceived(link->recvPlayerId)) + { + size_t blocksize = link->recvCounter * 252; + if (link->recvSize - blocksize <= 252) + { + MGL_ReceiveBlock(link->recvPlayerId, link->recvBuffer + blocksize, link->recvSize - blocksize); + link->recvCounter++; + link->state++; + } + else + { + MGL_ReceiveBlock(link->recvPlayerId, link->recvBuffer + blocksize, 252); + link->recvCounter++; + } + MGL_ResetReceived(link->recvPlayerId); + } + break; + case 2: + if (CalcCRC16WithTable(link->recvBuffer, link->recvSize) != link->recvCRC) + { + LinkRfu_FatalError(); + return FALSE; + } + else + { + link->state = 0; + return TRUE; + } + break; } return FALSE; } -static bool32 mevent_send_func(struct mevent_srv_sub * svr) +static bool32 MGL_Send(struct MysteryGiftLink * link) { - struct send_recv_header header; + struct SendRecvHeader header; - switch (svr->seqno) + switch (link->state) { - case 0: - if (IsLinkTaskFinished()) + case 0: + if (IsLinkTaskFinished()) + { + header.ident = link->sendIdent; + header.size = link->sendSize; + header.crc = CalcCRC16WithTable(link->sendBuffer, link->sendSize); + link->sendCRC = header.crc; + link->sendCounter = 0; + SendBlock(0, &header, sizeof(header)); + ++link->state; + } + break; + case 1: + if (IsLinkTaskFinished()) + { + if (MGL_HasReceived(link->sendPlayerId)) { - header.ident = svr->sendIdent; - header.size = svr->sendSize; - header.crc = CalcCRC16WithTable(svr->sendBfr, svr->sendSize); - svr->sendCRC = header.crc; - svr->sendCounter = 0; - SendBlock(0, &header, sizeof(header)); - ++svr->seqno; - } - break; - case 1: - if (IsLinkTaskFinished()) - { - if (mevent_has_received(svr->sendPlayerNo)) + size_t blocksize; + MGL_ResetReceived(link->sendPlayerId); + blocksize = 252 * link->sendCounter; + if (link->sendSize - blocksize <= 252) { - size_t blocksiz; - mevent_reset_recv(svr->sendPlayerNo); - blocksiz = 252 * svr->sendCounter; - if (svr->sendSize - blocksiz <= 252) - { - SendBlock(0, svr->sendBfr + blocksiz, svr->sendSize - blocksiz); - ++svr->sendCounter; - ++svr->seqno; - } - else - { - SendBlock(0, svr->sendBfr + blocksiz, 252); - ++svr->sendCounter; - } + SendBlock(0, link->sendBuffer + blocksize, link->sendSize - blocksize); + link->sendCounter++; + link->state++; + } + else + { + SendBlock(0, link->sendBuffer + blocksize, 252); + link->sendCounter++; } } - break; - case 2: - if (IsLinkTaskFinished()) - { - if (CalcCRC16WithTable(svr->sendBfr, svr->sendSize) != svr->sendCRC) - LinkRfu_FatalError(); - else - ++svr->seqno; - } - break; - case 3: - if (mevent_has_received(svr->sendPlayerNo)) - { - mevent_reset_recv(svr->sendPlayerNo); - svr->seqno = 0; - return TRUE; - } - break; + } + break; + case 2: + if (IsLinkTaskFinished()) + { + if (CalcCRC16WithTable(link->sendBuffer, link->sendSize) != link->sendCRC) + LinkRfu_FatalError(); + else + link->state++; + } + break; + case 3: + if (MGL_HasReceived(link->sendPlayerId)) + { + MGL_ResetReceived(link->sendPlayerId); + link->state = 0; + return TRUE; + } + break; } return FALSE; diff --git a/src/mystery_gift_server.c b/src/mystery_gift_server.c index d2f15644a..d2f2b2262 100644 --- a/src/mystery_gift_server.c +++ b/src/mystery_gift_server.c @@ -51,7 +51,7 @@ static void mevent_srv_init_common(struct mevent_srv_common * svr, const void *c svr->mevent_unk1442cc = AllocZeroed(sizeof(struct MysteryGiftLinkGameData)); svr->cmdBuffer = cmdBuffer; svr->cmdidx = 0; - mevent_srv_sub_init(&svr->manager, sendPlayerNo, recvPlayerNo); + MysteryGiftLink_Init(&svr->manager, sendPlayerNo, recvPlayerNo); } static void mevent_srv_free_resources(struct mevent_srv_common * svr) @@ -65,7 +65,7 @@ static void mevent_srv_free_resources(struct mevent_srv_common * svr) static void mevent_srv_common_init_send(struct mevent_srv_common * svr, u32 ident, const void *src, u32 size) { AGB_ASSERT_EX(size <= ME_SEND_BUF_SIZE, ABSPATH("mevent_server.c"), 257); - mevent_srv_sub_init_send(&svr->manager, ident, src, size); + MysteryGiftLink_InitSend(&svr->manager, ident, src, size); } static void *mevent_first_if_not_null_else_second(void *a0, void *a1) @@ -102,7 +102,7 @@ static u32 common_mainseq_1(struct mevent_srv_common * svr) static u32 common_mainseq_2(struct mevent_srv_common * svr) { // do recv - if (mevent_srv_sub_recv(&svr->manager)) + if (MysteryGiftLink_Recv(&svr->manager)) svr->mainseqno = 4; return 1; } @@ -110,7 +110,7 @@ static u32 common_mainseq_2(struct mevent_srv_common * svr) static u32 common_mainseq_3(struct mevent_srv_common * svr) { // do send - if (mevent_srv_sub_send(&svr->manager)) + if (MysteryGiftLink_Send(&svr->manager)) svr->mainseqno = 4; return 1; } @@ -134,7 +134,7 @@ static u32 common_mainseq_4(struct mevent_srv_common * svr) break; case 2: AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 364); - mevent_srv_sub_init_recv(&svr->manager, cmd->flag, svr->recvBuffer); + MysteryGiftLink_InitRecv(&svr->manager, cmd->flag, svr->recvBuffer); svr->mainseqno = 2; break; case 3: