From af6837c093c7fbd9eeda74e64fa1e6b24e4a8c2d Mon Sep 17 00:00:00 2001 From: GriffinR Date: Sat, 19 Nov 2022 21:49:50 -0500 Subject: [PATCH] Sync mystery gift server, client, scripts --- include/link_rfu.h | 2 +- include/mystery_gift.h | 4 +- include/mystery_gift_client.h | 91 ++++++++ include/mystery_gift_link.h | 50 +++++ include/mystery_gift_server.h | 170 +++++++------- src/mystery_gift.c | 4 +- src/mystery_gift_client.c | 359 +++++++++++++++--------------- src/mystery_gift_menu.c | 57 ++--- src/mystery_gift_scripts.c | 280 +++++++++++------------ src/mystery_gift_server.c | 406 +++++++++++++++++----------------- src/union_room_message.c | 25 ++- 11 files changed, 800 insertions(+), 648 deletions(-) create mode 100644 include/mystery_gift_client.h create mode 100644 include/mystery_gift_link.h diff --git a/include/link_rfu.h b/include/link_rfu.h index 65d1cf6dd..9610a9682 100644 --- a/include/link_rfu.h +++ b/include/link_rfu.h @@ -325,6 +325,6 @@ void SetUnionRoomChatPlayerData(u32 numPlayers); void ClearRecvCommands(void); #include "mystery_gift_server.h" -extern const struct mevent_server_cmd gServerScript_ClientCanceledCard[]; +extern const struct MysteryGiftServerCmd gServerScript_ClientCanceledCard[]; #endif //GUARD_LINK_RFU_H diff --git a/include/mystery_gift.h b/include/mystery_gift.h index a8ec84f86..a59b60ec8 100644 --- a/include/mystery_gift.h +++ b/include/mystery_gift.h @@ -79,8 +79,8 @@ void DisableWonderCardSending(struct WonderCard * card); bool32 MysteryGift_TrySaveStamp(const u16 * stamp); void MysteryGift_LoadLinkGameData(struct MysteryGiftLinkGameData * data); bool32 MysteryGift_ValidateLinkGameData(const struct MysteryGiftLinkGameData * data); -u32 MysteryGift_CompareCardFlags(const u16 * flagId, const struct MysteryGiftLinkGameData * data, void *unused); -u32 MysteryGift_CheckStamps(const u16 * stamp, const struct MysteryGiftLinkGameData * data, void *unused); +u32 MysteryGift_CompareCardFlags(const u16 * flagId, const struct MysteryGiftLinkGameData * data, const void *unused); +u32 MysteryGift_CheckStamps(const u16 * stamp, const struct MysteryGiftLinkGameData * data, const void *unused); bool32 MysteryGift_DoesQuestionnaireMatch(const struct MysteryGiftLinkGameData * data, const u16 * words); u16 MysteryGift_GetCardStatFromLinkData(const struct MysteryGiftLinkGameData * data, u32 stat); bool32 WonderCard_Init(struct WonderCard * card, struct WonderCardMetadata * metadata); diff --git a/include/mystery_gift_client.h b/include/mystery_gift_client.h new file mode 100644 index 000000000..c7817d161 --- /dev/null +++ b/include/mystery_gift_client.h @@ -0,0 +1,91 @@ +#ifndef GUARD_MYSTERY_GIFT_CLIENT_H +#define GUARD_MYSTERY_GIFT_CLIENT_H + +#include "mystery_gift_link.h" + +// Return values for client functions called by MysteryGiftClient_Run +enum { + CLI_RET_INIT, + CLI_RET_ACTIVE, + CLI_RET_YES_NO, + CLI_RET_PRINT_MSG, + CLI_RET_ASK_TOSS, + CLI_RET_COPY_MSG, + CLI_RET_END, +}; + +// IDs for client script instructions +enum { + CLI_NONE, + CLI_RETURN, + CLI_RECV, + CLI_SEND_LOADED, + CLI_COPY_RECV, + CLI_YES_NO, + CLI_COPY_RECV_IF_N, + CLI_COPY_RECV_IF, + CLI_LOAD_GAME_DATA, + CLI_SAVE_NEWS, + CLI_SAVE_CARD, + CLI_PRINT_MSG, + CLI_COPY_MSG, + CLI_ASK_TOSS, + CLI_LOAD_TOSS_RESPONSE, + CLI_RUN_MEVENT_SCRIPT, + CLI_SAVE_STAMP, + CLI_SAVE_RAM_SCRIPT, + CLI_RECV_EREADER_TRAINER, + CLI_SEND_STAT, + CLI_SEND_READY_END, + CLI_RUN_BUFFER_SCRIPT, +}; + +// IDs for client messages when ending a script. +// Given as the parameter to CLI_RETURN, and resolved to text in GetClientResultMessage +enum { + CLI_MSG_NOTHING_SENT, + CLI_MSG_RECORD_UPLOADED, + CLI_MSG_CARD_RECEIVED, + CLI_MSG_NEWS_RECEIVED, + CLI_MSG_STAMP_RECEIVED, + CLI_MSG_HAD_CARD, + CLI_MSG_HAD_STAMP, + CLI_MSG_HAD_NEWS, + CLI_MSG_NO_ROOM_STAMPS, + CLI_MSG_COMM_CANCELED, + CLI_MSG_CANT_ACCEPT, + CLI_MSG_COMM_ERROR, + CLI_MSG_TRAINER_RECEIVED, + CLI_MSG_BUFFER_SUCCESS, + CLI_MSG_BUFFER_FAILURE, +}; + +#define CLIENT_MAX_MSG_SIZE 64 + +struct MysteryGiftClientCmd +{ + u32 instr; + u32 parameter; +}; + +struct MysteryGiftClient +{ + u32 unused; + u32 param; + u32 funcId; + u32 funcState; + u32 cmdidx; + void *sendBuffer; + void *recvBuffer; + struct MysteryGiftClientCmd * script; + void *msg; + struct MysteryGiftLink link; +}; + +void MysteryGiftClient_Create(void); +u32 MysteryGiftClient_Run(u16 * endVal); +void MysteryGiftClient_AdvanceState(void); +void * MysteryGiftClient_GetMsg(void); +void MysteryGiftClient_SetParam(u32 value); + +#endif //GUARD_MYSTERY_GIFT_CLIENT_H diff --git a/include/mystery_gift_link.h b/include/mystery_gift_link.h new file mode 100644 index 000000000..8f36d9108 --- /dev/null +++ b/include/mystery_gift_link.h @@ -0,0 +1,50 @@ +#ifndef GUARD_MYSTERY_GIFT_LINK_H +#define GUARD_MYSTERY_GIFT_LINK_H + +#define MG_LINK_BUFFER_SIZE 0x400 +#define ME_SEND_BUF_SIZE MG_LINK_BUFFER_SIZE + +// Send/receive ids for the Client/Server to make sure +// they're sending/receiving the same thing +enum { + MG_LINKID_CLIENT_SCRIPT = 16, + MG_LINKID_GAME_DATA, + MG_LINKID_GAME_STAT, + MG_LINKID_RESPONSE, + MG_LINKID_READY_END, + MG_LINKID_DYNAMIC_MSG, + MG_LINKID_CARD, + MG_LINKID_NEWS, + MG_LINKID_STAMP, + MG_LINKID_RAM_SCRIPT, + MG_LINKID_EREADER_TRAINER, + MG_LINKID_UNK_1, + MG_LINKID_UNK_2, +}; + +struct MysteryGiftLink +{ + s32 state; + u8 sendPlayerId; + u8 recvPlayerId; + u16 recvIdent; + u16 recvCounter; + u16 recvCRC; + u16 recvSize; + u16 sendIdent; + u16 sendCounter; + u16 sendCRC; + u16 sendSize; + void * recvBuffer; + const void * sendBuffer; + u32 (*recvFunc)(struct MysteryGiftLink *); + u32 (*sendFunc)(struct MysteryGiftLink *); +}; + +void MysteryGiftLink_Init(struct MysteryGiftLink * link, u32 sendPlayerId, u32 recvPlayerId); +void MysteryGiftLink_InitSend(struct MysteryGiftLink * link, u32 ident, const void * src, u32 size); +bool32 MysteryGiftLink_Recv(struct MysteryGiftLink * link); +bool32 MysteryGiftLink_Send(struct MysteryGiftLink * link); +void MysteryGiftLink_InitRecv(struct MysteryGiftLink * link, u32 ident, void * dest); + +#endif //GUARD_MYSTERY_GIFT_LINK_H diff --git a/include/mystery_gift_server.h b/include/mystery_gift_server.h index 7c71ed856..64bb9e9bf 100644 --- a/include/mystery_gift_server.h +++ b/include/mystery_gift_server.h @@ -2,121 +2,103 @@ #define GUARD_MYSTERY_GIFT_SERVER_H #include "global.h" +#include "mystery_gift_link.h" -#define ME_SEND_BUF_SIZE 0x400 - -struct MysteryGiftLink -{ - s32 state; - u8 sendPlayerId; - u8 recvPlayerId; - u16 recvIdent; - u16 recvCounter; - u16 recvCRC; - u16 recvSize; - u16 sendIdent; - u16 sendCounter; - u16 sendCRC; - u16 sendSize; - void *recvBuffer; - const void *sendBuffer; - u32 (*recvFunc)(struct MysteryGiftLink *); - u32 (*sendFunc)(struct MysteryGiftLink *); +// Return values for Server_* functions. +// Other than SVR_RET_END, effectively useless (not checked for). +enum { + SVR_RET_INIT, + SVR_RET_ACTIVE, + SVR_RET_UNUSED, + SVR_RET_END }; -struct mevent_client_cmd -{ - u32 instr; - u32 parameter; +// IDs for server script instructions +enum { + SVR_RETURN, + SVR_SEND, + SVR_RECV, + SVR_GOTO, + SVR_GOTO_IF_EQ, + SVR_COPY_GAME_DATA, + SVR_CHECK_GAME_DATA, // In Emerald, this was separated into SVR_CHECK_GAME_DATA_CARD and SVR_CHECK_GAME_DATA_NEWS + SVR_CHECK_EXISTING_CARD, + SVR_READ_RESPONSE, + SVR_CHECK_EXISTING_STAMPS, + SVR_GET_CARD_STAT, + SVR_CHECK_QUESTIONNAIRE, + SVR_COMPARE, + SVR_LOAD_CARD, + SVR_LOAD_NEWS, + SVR_LOAD_RAM_SCRIPT, + SVR_LOAD_STAMP, + SVR_LOAD_UNK_2, + SVR_LOAD_CLIENT_SCRIPT, + SVR_LOAD_EREADER_TRAINER, + SVR_LOAD_MSG, + SVR_COPY_STAMP, + SVR_COPY_CARD, + SVR_COPY_NEWS, + SVR_SET_RAM_SCRIPT, + SVR_SET_CLIENT_SCRIPT, + SVR_COPY_SAVED_CARD, + SVR_COPY_SAVED_NEWS, + SVR_COPY_SAVED_RAM_SCRIPT, + SVR_LOAD_UNK_1, }; -// Client commands -#define CLI_RETURN(x) {.instr = 1, .parameter = x} -#define CLI_RECEIVE(x) {.instr = 2, .parameter = x} -#define CLI_WAITSND {.instr = 3, .parameter = 0} -#define CLI_JUMPBUF {.instr = 4, .parameter = 0} -#define CLI_SNDHEAD {.instr = 8, .parameter = 0} -#define CLI_VLDNEWS {.instr = 9, .parameter = 0} -#define CLI_RECVSAV {.instr = 10, .parameter = 0} -#define CLI_RECVBUF {.instr = 12, .parameter = 0} -#define CLI_REQWORD {.instr = 13, .parameter = 0} -#define CLI_SNDWORD {.instr = 14, .parameter = 0} -#define CLI_RECVMON {.instr = 16, .parameter = 0} -#define CLI_RECVRAM {.instr = 17, .parameter = 0} -#define CLI_SENDALL {.instr = 20, .parameter = 0} +// Create arguments for SVR_LOAD_CLIENT_SCRIPT or SVR_LOAD_MSG +// (a script/text size and pointer to send to the client) +#define PTR_ARG(pointer) .flag = sizeof(pointer), .parameter = pointer -struct mevent_client -{ - u32 unk_00; - u32 param; - u32 mainseqno; - u32 flag; - u32 cmdidx; - void *sendBuffer; - void *recvBuffer; - struct mevent_client_cmd * cmdBuffer; - void *buffer; - struct MysteryGiftLink manager; +// IDs for server messages when ending a script. +// Given as the parameter to SVR_RETURN, and resolved to text in GetServerResultMessage +enum { + SVR_MSG_NOTHING_SENT, + SVR_MSG_RECORD_UPLOADED, + SVR_MSG_CARD_SENT, + SVR_MSG_NEWS_SENT, + SVR_MSG_STAMP_SENT, + SVR_MSG_HAS_CARD, + SVR_MSG_HAS_STAMP, + SVR_MSG_HAS_NEWS, + SVR_MSG_NO_ROOM_STAMPS, + SVR_MSG_CLIENT_CANCELED, + SVR_MSG_CANT_SEND_GIFT_1, + SVR_MSG_COMM_ERROR, + SVR_MSG_GIFT_SENT_1, + SVR_MSG_GIFT_SENT_2, + SVR_MSG_CANT_SEND_GIFT_2, }; -struct mevent_server_cmd +struct MysteryGiftServerCmd { u32 instr; bool32 flag; - void *parameter; + const void *parameter; }; -// Server commands -#define SRV_RETURN(x) {.instr = 0, .flag = x} -#define SRV_WAITSND {.instr = 1} -#define SRV_RECV(x) {.instr = 2, .flag = x} -#define SRV_BRANCH(y) {.instr = 3, .parameter = (void *)y} -#define SRV_BRANCHIF(x, y) {.instr = 4, .flag = x, .parameter = (void *)y} -#define SRV_READ_1442CC {.instr = 5} -#define SRV_VALID_1442CC {.instr = 6} -#define SRV_CHECK_1442CC_14 {.instr = 7} -#define SRV_READWORD {.instr = 8} -#define SRV_SEND_CARD {.instr = 13} -#define SRV_SEND_NEWS {.instr = 14} -#define SRV_BUFFER_SEND {.instr = 15} -#define SRV_SEND(x, y) {.instr = 18, .flag = x, .parameter = (void *)y} -#define SRV_SENDSTR(x, y) {.instr = 20, .flag = x, .parameter = (void *)y} -#define SRV_BUFFER_CARD {.instr = 26} -#define SRV_BUFFER_NEWS {.instr = 27} -#define SRV_RAM_SCRIPT_IF_VALID {.instr = 28} - -struct mevent_srv_common +struct MysteryGiftServer { - u32 unk_00; + u32 unused; u32 param; - u32 mainseqno; + u32 funcId; u32 cmdidx; - const struct mevent_server_cmd * cmdBuffer; + const struct MysteryGiftServerCmd * script; void *recvBuffer; struct WonderCard * card; struct WonderNews * news; - struct MysteryGiftLinkGameData * mevent_unk1442cc; - void *sendBuffer1; - u32 sendBuffer1Size; - void *sendBuffer2; - u32 sendBuffer2Size; - u32 sendWord; + struct MysteryGiftLinkGameData * linkGameData; + const void *ramScript; + u32 ramScriptSize; + const void *clientScript; + u32 clientScriptSize; + u32 stamp; struct MysteryGiftLink manager; }; -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); -void mevent_client_inc_flag(void); -void *mevent_client_get_buffer(void); -void mevent_client_set_param(u32 a0); -void mevent_srv_init_wnews(void); -void mevent_srv_new_wcard(void); -u32 mevent_srv_common_do_exec(u16 * a0); +void MysterGiftServer_CreateForNews(void); +void MysterGiftServer_CreateForCard(void); +u32 MysterGiftServer_Run(u16 * endVal); #endif //GUARD_MYSTERY_GIFT_SERVER_H diff --git a/src/mystery_gift.c b/src/mystery_gift.c index fa164b924..8db1ee5e0 100644 --- a/src/mystery_gift.c +++ b/src/mystery_gift.c @@ -810,7 +810,7 @@ bool32 MysteryGift_ValidateLinkGameData(const struct MysteryGiftLinkGameData * d return TRUE; } -u32 MysteryGift_CompareCardFlags(const u16 * flagId, const struct MysteryGiftLinkGameData * data, void *unused) +u32 MysteryGift_CompareCardFlags(const u16 * flagId, const struct MysteryGiftLinkGameData * data, const void *unused) { // Has a Wonder Card already? if (data->flagId == 0) @@ -824,7 +824,7 @@ u32 MysteryGift_CompareCardFlags(const u16 * flagId, const struct MysteryGiftLin return 2; } -u32 MysteryGift_CheckStamps(const u16 * stamp, const struct MysteryGiftLinkGameData * data, void *unused) +u32 MysteryGift_CheckStamps(const u16 * stamp, const struct MysteryGiftLinkGameData * data, const void *unused) { s32 stampsMissing = data->maxStamps - GetNumStampsInMetadata(&data->cardMetadata, data->maxStamps); diff --git a/src/mystery_gift_client.c b/src/mystery_gift_client.c index 5844f80e2..dc1830b60 100644 --- a/src/mystery_gift_client.c +++ b/src/mystery_gift_client.c @@ -6,285 +6,296 @@ #include "battle_tower.h" #include "mystery_event_script.h" #include "mystery_gift.h" +#include "mystery_gift_client.h" #include "mystery_gift_server.h" -static EWRAM_DATA struct mevent_client * s_mevent_client_ptr = NULL; +enum { + FUNC_INIT, + FUNC_DONE, + FUNC_RECV, + FUNC_SEND, + FUNC_RUN, + FUNC_WAIT, + FUNC_RUN_MEVENT, + FUNC_RUN_BUFFER, +}; -static void mevent_client_init(struct mevent_client *, u32, u32); -static u32 mevent_client_exec(struct mevent_client *); -static void mevent_client_free_resources(struct mevent_client *); +static EWRAM_DATA struct MysteryGiftClient * sClient = NULL; -extern const struct mevent_client_cmd gMEventClientScript_InitialListen[]; +static void MysteryGiftClient_Init(struct MysteryGiftClient *, u32, u32); +static u32 MysteryGiftClient_CallFunc(struct MysteryGiftClient *); +static void MysteryGiftClient_Free(struct MysteryGiftClient *); -void mevent_client_do_init(void) +extern const struct MysteryGiftClientCmd gMysteryGiftClientScript_Init[]; + +void MysteryGiftClient_Create(void) { - s_mevent_client_ptr = AllocZeroed(sizeof(struct mevent_client)); - mevent_client_init(s_mevent_client_ptr, 1, 0); + sClient = AllocZeroed(sizeof(*sClient)); + MysteryGiftClient_Init(sClient, 1, 0); } -u32 mevent_client_do_exec(u16 * a0) +u32 MysteryGiftClient_Run(u16 * endVal) { u32 result; - if (s_mevent_client_ptr == NULL) - return 6; - result = mevent_client_exec(s_mevent_client_ptr); - if (result == 6) + if (sClient == NULL) + return CLI_RET_END; + result = MysteryGiftClient_CallFunc(sClient); + if (result == CLI_RET_END) { - *a0 = s_mevent_client_ptr->param; - mevent_client_free_resources(s_mevent_client_ptr); - Free(s_mevent_client_ptr); - s_mevent_client_ptr = NULL; + *endVal = sClient->param; + MysteryGiftClient_Free(sClient); + FREE_AND_SET_NULL(sClient); } return result; } -void mevent_client_inc_flag(void) +void MysteryGiftClient_AdvanceState(void) { - s_mevent_client_ptr->flag++; + sClient->funcState++; } -void *mevent_client_get_buffer(void) +void *MysteryGiftClient_GetMsg(void) { - return s_mevent_client_ptr->buffer; + return sClient->msg; } -void mevent_client_set_param(u32 a0) +void MysteryGiftClient_SetParam(u32 val) { - s_mevent_client_ptr->param = a0; + sClient->param = val; } -static void mevent_client_init(struct mevent_client * svr, u32 sendPlayerNo, u32 recvPlayerNo) +static void MysteryGiftClient_Init(struct MysteryGiftClient * client, u32 sendPlayerId, u32 recvPlayerId) { - svr->unk_00 = 0; - svr->mainseqno = 0; - svr->flag = 0; - svr->sendBuffer = AllocZeroed(ME_SEND_BUF_SIZE); - svr->recvBuffer = AllocZeroed(ME_SEND_BUF_SIZE); - svr->cmdBuffer = AllocZeroed(ME_SEND_BUF_SIZE); - svr->buffer = AllocZeroed(0x40); - MysteryGiftLink_Init(&svr->manager, sendPlayerNo, recvPlayerNo); + client->unused = 0; + client->funcId = FUNC_INIT; + client->funcState = 0; + client->sendBuffer = AllocZeroed(MG_LINK_BUFFER_SIZE); + client->recvBuffer = AllocZeroed(MG_LINK_BUFFER_SIZE); + client->script = AllocZeroed(MG_LINK_BUFFER_SIZE); + client->msg = AllocZeroed(CLIENT_MAX_MSG_SIZE); + MysteryGiftLink_Init(&client->link, sendPlayerId, recvPlayerId); } -static void mevent_client_free_resources(struct mevent_client * svr) +static void MysteryGiftClient_Free(struct MysteryGiftClient * client) { - Free(svr->sendBuffer); - Free(svr->recvBuffer); - Free(svr->cmdBuffer); - Free(svr->buffer); + Free(client->sendBuffer); + Free(client->recvBuffer); + Free(client->script); + Free(client->msg); } -static void mevent_client_jmp_buffer(struct mevent_client * svr) +static void MysteryGiftClient_CopyRecvScript(struct MysteryGiftClient * client) { - memcpy(svr->cmdBuffer, svr->recvBuffer, ME_SEND_BUF_SIZE); - svr->cmdidx = 0; + memcpy(client->script, client->recvBuffer, MG_LINK_BUFFER_SIZE); + client->cmdidx = 0; } -static void mevent_client_send_word(struct mevent_client * svr, u32 ident, u32 word) +static void MysteryGiftClient_InitSendWord(struct MysteryGiftClient * client, u32 ident, u32 word) { - CpuFill32(0, svr->sendBuffer, ME_SEND_BUF_SIZE); - *(u32 *)svr->sendBuffer = word; - MysteryGiftLink_InitSend(&svr->manager, ident, svr->sendBuffer, sizeof(u32)); + CpuFill32(0, client->sendBuffer, MG_LINK_BUFFER_SIZE); + *(u32 *)client->sendBuffer = word; + MysteryGiftLink_InitSend(&client->link, ident, client->sendBuffer, sizeof(word)); } -static u32 client_mainseq_0(struct mevent_client * svr) +static u32 Client_Init(struct MysteryGiftClient * client) { // init - memcpy(svr->cmdBuffer, gMEventClientScript_InitialListen, ME_SEND_BUF_SIZE); - svr->cmdidx = 0; - svr->mainseqno = 4; - svr->flag = 0; - return 0; + memcpy(client->script, gMysteryGiftClientScript_Init, MG_LINK_BUFFER_SIZE); + client->cmdidx = 0; + client->funcId = FUNC_RUN; + client->funcState = 0; + return CLI_RET_INIT; } -static u32 client_mainseq_1(struct mevent_client * svr) +static u32 Client_Done(struct MysteryGiftClient * client) { - // done - return 6; + return CLI_RET_END; } -static u32 client_mainseq_2(struct mevent_client * svr) +static u32 Client_Recv(struct MysteryGiftClient * client) { - // do recv - if (MysteryGiftLink_Recv(&svr->manager)) + if (MysteryGiftLink_Recv(&client->link)) { - svr->mainseqno = 4; - svr->flag = 0; + client->funcId = FUNC_RUN; + client->funcState = 0; } - return 1; + return CLI_RET_ACTIVE; } -static u32 client_mainseq_3(struct mevent_client * svr) +static u32 Client_Send(struct MysteryGiftClient * client) { - // do send - if (MysteryGiftLink_Send(&svr->manager)) + if (MysteryGiftLink_Send(&client->link)) { - svr->mainseqno = 4; - svr->flag = 0; + client->funcId = FUNC_RUN; + client->funcState = 0; } - return 1; + return CLI_RET_ACTIVE; } -static u32 client_mainseq_4(struct mevent_client * svr) +static u32 Client_Run(struct MysteryGiftClient * client) { // process command - struct mevent_client_cmd * cmd = &svr->cmdBuffer[svr->cmdidx]; - ++svr->cmdidx; + struct MysteryGiftClientCmd * cmd = &client->script[client->cmdidx]; + client->cmdidx++; switch (cmd->instr) { - case 0: + case CLI_NONE: break; - case 1: - svr->param = cmd->parameter; - svr->mainseqno = 1; - svr->flag = 0; + case CLI_RETURN: + client->param = cmd->parameter; // Set for endVal in MysteryGiftClient_Run + client->funcId = FUNC_DONE; + client->funcState = 0; break; - case 2: - MysteryGiftLink_InitRecv(&svr->manager, cmd->parameter, svr->recvBuffer); - svr->mainseqno = 2; - svr->flag = 0; + case CLI_RECV: + MysteryGiftLink_InitRecv(&client->link, cmd->parameter, client->recvBuffer); + client->funcId = FUNC_RECV; + client->funcState = 0; break; - case 3: - svr->mainseqno = 3; - svr->flag = 0; + case CLI_SEND_LOADED: + // Send without a MysteryGiftLink_InitSend + // Sends whatever has been loaded already + client->funcId = FUNC_SEND; + client->funcState = 0; break; - case 20: - MysteryGiftLink_InitSend(&svr->manager, 0x14, svr->sendBuffer, 0); - svr->mainseqno = 3; - svr->flag = 0; + case CLI_SEND_READY_END: + MysteryGiftLink_InitSend(&client->link, MG_LINKID_READY_END, client->sendBuffer, 0); + client->funcId = FUNC_SEND; + client->funcState = 0; break; - case 19: - mevent_client_send_word(svr, 0x12, GetGameStat(cmd->parameter)); - svr->mainseqno = 3; - svr->flag = 0; + case CLI_SEND_STAT: + MysteryGiftClient_InitSendWord(client, MG_LINKID_GAME_STAT, GetGameStat(cmd->parameter)); + client->funcId = FUNC_SEND; + client->funcState = 0; break; - case 6: - if (svr->param == 0) - mevent_client_jmp_buffer(svr); + case CLI_COPY_RECV_IF_N: + if (client->param == FALSE) + MysteryGiftClient_CopyRecvScript(client); break; - case 7: - if (svr->param == 1) - mevent_client_jmp_buffer(svr); + case CLI_COPY_RECV_IF: + if (client->param == TRUE) + MysteryGiftClient_CopyRecvScript(client); break; - case 4: - mevent_client_jmp_buffer(svr); + case CLI_COPY_RECV: + MysteryGiftClient_CopyRecvScript(client); break; - case 5: - memcpy(svr->buffer, svr->recvBuffer, 0x40); - svr->mainseqno = 5; - svr->flag = 0; - return 2; - case 11: - memcpy(svr->buffer, svr->recvBuffer, 0x40); - svr->mainseqno = 5; - svr->flag = 0; - return 3; - case 12: - memcpy(svr->buffer, svr->recvBuffer, 0x40); - svr->mainseqno = 5; - svr->flag = 0; - return 5; - case 13: - svr->mainseqno = 5; - svr->flag = 0; - return 4; - case 8: - MysteryGift_LoadLinkGameData(svr->sendBuffer); - MysteryGiftLink_InitSend(&svr->manager, 0x11, svr->sendBuffer, sizeof(struct MysteryGiftLinkGameData)); + case CLI_YES_NO: + memcpy(client->msg, client->recvBuffer, CLIENT_MAX_MSG_SIZE); + client->funcId = FUNC_WAIT; + client->funcState = 0; + return CLI_RET_YES_NO; + case CLI_PRINT_MSG: + memcpy(client->msg, client->recvBuffer, CLIENT_MAX_MSG_SIZE); + client->funcId = FUNC_WAIT; + client->funcState = 0; + return CLI_RET_PRINT_MSG; + case CLI_COPY_MSG: + memcpy(client->msg, client->recvBuffer, CLIENT_MAX_MSG_SIZE); + client->funcId = FUNC_WAIT; + client->funcState = 0; + return CLI_RET_COPY_MSG; + case CLI_ASK_TOSS: + client->funcId = FUNC_WAIT; + client->funcState = 0; + return CLI_RET_ASK_TOSS; + case CLI_LOAD_GAME_DATA: + MysteryGift_LoadLinkGameData(client->sendBuffer); + MysteryGiftLink_InitSend(&client->link, MG_LINKID_GAME_DATA, client->sendBuffer, sizeof(struct MysteryGiftLinkGameData)); break; - case 14: - mevent_client_send_word(svr, 0x13, svr->param); + case CLI_LOAD_TOSS_RESPONSE: + // param here is set by MG_STATE_CLIENT_ASK_TOSS or MG_STATE_CLIENT_ASK_TOSS_UNRECEIVED + MysteryGiftClient_InitSendWord(client, MG_LINKID_RESPONSE, client->param); break; - case 10: - SaveWonderCard(svr->recvBuffer); + case CLI_SAVE_CARD: + SaveWonderCard(client->recvBuffer); break; - case 9: - if (!IsWonderNewsSameAsSaved(svr->recvBuffer)) + case CLI_SAVE_NEWS: + if (!IsWonderNewsSameAsSaved(client->recvBuffer)) { - SaveWonderNews(svr->recvBuffer); - mevent_client_send_word(svr, 0x13, 0); + SaveWonderNews(client->recvBuffer); + MysteryGiftClient_InitSendWord(client, MG_LINKID_RESPONSE, FALSE); } else - // Other trainer already has news - mevent_client_send_word(svr, 0x13, 1); + { + // Wonder News has already been saved (or is invalid). + // Prepare a signal to indicate it was not saved. + MysteryGiftClient_InitSendWord(client, MG_LINKID_RESPONSE, TRUE); + } break; - case 15: - svr->mainseqno = 6; - svr->flag = 0; + case CLI_RUN_MEVENT_SCRIPT: + client->funcId = FUNC_RUN_MEVENT; + client->funcState = 0; break; - case 16: - MysteryGift_TrySaveStamp(svr->recvBuffer); + case CLI_SAVE_STAMP: + MysteryGift_TrySaveStamp(client->recvBuffer); break; - case 17: - InitRamScript_NoObjectEvent(svr->recvBuffer, 1000); + case CLI_SAVE_RAM_SCRIPT: + InitRamScript_NoObjectEvent(client->recvBuffer, sizeof(struct RamScriptData)); break; - case 18: - memcpy(&gSaveBlock2Ptr->battleTower.ereaderTrainer, svr->recvBuffer, sizeof(struct BattleTowerEReaderTrainer)); + case CLI_RECV_EREADER_TRAINER: + memcpy(&gSaveBlock2Ptr->battleTower.ereaderTrainer, client->recvBuffer, sizeof(gSaveBlock2Ptr->battleTower.ereaderTrainer)); ValidateEReaderTrainer(); break; - case 21: - memcpy(gDecompressionBuffer, svr->recvBuffer, ME_SEND_BUF_SIZE); - svr->mainseqno = 7; - svr->flag = 0; + case CLI_RUN_BUFFER_SCRIPT: + memcpy(gDecompressionBuffer, client->recvBuffer, MG_LINK_BUFFER_SIZE); + client->funcId = FUNC_RUN_BUFFER; + client->funcState = 0; break; } - return 1; + return CLI_RET_ACTIVE; } -static u32 client_mainseq_5(struct mevent_client * svr) +static u32 Client_Wait(struct MysteryGiftClient * client) { - // wait flag - if (svr->flag) + if (client->funcState) { - svr->mainseqno = 4; - svr->flag = 0; + client->funcId = FUNC_RUN; + client->funcState = 0; } - return 1; + return CLI_RET_ACTIVE; } -static u32 client_mainseq_6(struct mevent_client * svr) +static u32 Client_RunMysteryEventScript(struct MysteryGiftClient * client) { - // Run mevent buffer script - switch (svr->flag) + switch (client->funcState) { case 0: - MEventScript_InitContext(svr->recvBuffer); - ++svr->flag; + MEventScript_InitContext(client->recvBuffer); + client->funcState++; break; case 1: - if (!MEventScript_Run(&svr->param)) + if (!MEventScript_Run(&client->param)) { - svr->mainseqno = 4; - svr->flag = 0; + client->funcId = FUNC_RUN; + client->funcState = 0; } break; } - return 1; + return CLI_RET_ACTIVE; } -static u32 client_mainseq_7(struct mevent_client * svr) +static u32 Client_RunBufferScript(struct MysteryGiftClient * client) { - // exec arbitrary code u32 (*func)(u32 *, struct SaveBlock2 *, struct SaveBlock1 *) = (void *)gDecompressionBuffer; - if (func(&svr->param, gSaveBlock2Ptr, gSaveBlock1Ptr) == 1) + if (func(&client->param, gSaveBlock2Ptr, gSaveBlock1Ptr) == 1) { - svr->mainseqno = 4; - svr->flag = 0; + client->funcId = FUNC_RUN; + client->funcState = 0; } - return 1; + return CLI_RET_ACTIVE; } -static u32 mevent_client_exec(struct mevent_client * svr) +static u32 MysteryGiftClient_CallFunc(struct MysteryGiftClient * client) { - u32 (*funcs[])(struct mevent_client *) = { - client_mainseq_0, - client_mainseq_1, - client_mainseq_2, - client_mainseq_3, - client_mainseq_4, - client_mainseq_5, - client_mainseq_6, - client_mainseq_7 + u32 (*funcs[])(struct MysteryGiftClient *) = { + [FUNC_INIT] = Client_Init, + [FUNC_DONE] = Client_Done, + [FUNC_RECV] = Client_Recv, + [FUNC_SEND] = Client_Send, + [FUNC_RUN] = Client_Run, + [FUNC_WAIT] = Client_Wait, + [FUNC_RUN_MEVENT] = Client_RunMysteryEventScript, + [FUNC_RUN_BUFFER] = Client_RunBufferScript }; - return funcs[svr->mainseqno](svr); + return funcs[client->funcId](client); } diff --git a/src/mystery_gift_menu.c b/src/mystery_gift_menu.c index cb8a5bfe9..a06b93ba3 100644 --- a/src/mystery_gift_menu.c +++ b/src/mystery_gift_menu.c @@ -14,6 +14,7 @@ #include "link.h" #include "event_data.h" #include "mystery_gift_server.h" +#include "mystery_gift_client.h" #include "wonder_news.h" #include "help_system.h" #include "strings.h" @@ -1222,7 +1223,7 @@ void task00_mystery_gift(u8 taskId) { ClearScreenInBg0(TRUE); data->state = 7; - mevent_client_do_init(); + MysteryGiftClient_Create(); } else if (gSpecialVar_Result == 5) { @@ -1235,7 +1236,7 @@ void task00_mystery_gift(u8 taskId) data->state = 8; break; case 8: - switch (mevent_client_do_exec(&data->curPromptWindowId)) + switch (MysteryGiftClient_Run(&data->curPromptWindowId)) { case 6: // done Rfu_SetCloseLinkCallback(); @@ -1243,8 +1244,8 @@ void task00_mystery_gift(u8 taskId) data->state = 13; break; case 5: - memcpy(data->buffer, mevent_client_get_buffer(), 0x40); - mevent_client_inc_flag(); + memcpy(data->buffer, MysteryGiftClient_GetMsg(), 0x40); + MysteryGiftClient_AdvanceState(); break; case 3: data->state = 10; @@ -1259,30 +1260,30 @@ void task00_mystery_gift(u8 taskId) } break; case 9: - flag = DoMysteryGiftYesNo(&data->textState, &data->curPromptWindowId, FALSE, mevent_client_get_buffer()); + flag = DoMysteryGiftYesNo(&data->textState, &data->curPromptWindowId, FALSE, MysteryGiftClient_GetMsg()); switch (flag) { case 0: - mevent_client_set_param(0); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(0); + MysteryGiftClient_AdvanceState(); data->state = 7; break; case 1: - mevent_client_set_param(1); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(1); + MysteryGiftClient_AdvanceState(); data->state = 7; break; case -1u: - mevent_client_set_param(1); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(1); + MysteryGiftClient_AdvanceState(); data->state = 7; break; } break; case 10: - if (PrintMysteryGiftMenuMessage(&data->textState, mevent_client_get_buffer())) + if (PrintMysteryGiftMenuMessage(&data->textState, MysteryGiftClient_GetMsg())) { - mevent_client_inc_flag(); + MysteryGiftClient_AdvanceState(); data->state = 7; } break; @@ -1297,19 +1298,19 @@ void task00_mystery_gift(u8 taskId) } else { - mevent_client_set_param(0); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(0); + MysteryGiftClient_AdvanceState(); data->state = 7; } break; case 1: - mevent_client_set_param(1); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(1); + MysteryGiftClient_AdvanceState(); data->state = 7; break; case -1u: - mevent_client_set_param(1); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(1); + MysteryGiftClient_AdvanceState(); data->state = 7; break; } @@ -1319,18 +1320,18 @@ void task00_mystery_gift(u8 taskId) switch (flag) { case 0: - mevent_client_set_param(0); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(0); + MysteryGiftClient_AdvanceState(); data->state = 7; break; case 1: - mevent_client_set_param(1); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(1); + MysteryGiftClient_AdvanceState(); data->state = 7; break; case -1u: - mevent_client_set_param(1); - mevent_client_inc_flag(); + MysteryGiftClient_SetParam(1); + MysteryGiftClient_AdvanceState(); data->state = 7; break; } @@ -1567,17 +1568,17 @@ void task00_mystery_gift(u8 taskId) if (data->IsCardOrNews == 0) { AddTextPrinterToWindow1(gText_SendingWonderCard); - mevent_srv_new_wcard(); + MysterGiftServer_CreateForCard(); } else { AddTextPrinterToWindow1(gText_SendingWonderNews); - mevent_srv_init_wnews(); + MysterGiftServer_CreateForNews(); } data->state = 32; break; case 32: - if (mevent_srv_common_do_exec(&data->curPromptWindowId) == 3) + if (MysterGiftServer_Run(&data->curPromptWindowId) == 3) { data->prevPromptWindowId = data->curPromptWindowId; data->state = 33; diff --git a/src/mystery_gift_scripts.c b/src/mystery_gift_scripts.c index b99c16405..0af0f2346 100644 --- a/src/mystery_gift_scripts.c +++ b/src/mystery_gift_scripts.c @@ -1,192 +1,198 @@ #include "global.h" #include "mystery_gift_server.h" +#include "mystery_gift_client.h" +#include "constants/mystery_gift.h" -extern const struct mevent_server_cmd gServerScript_ClientCanceledCard[]; +extern const struct MysteryGiftServerCmd gServerScript_ClientCanceledCard[]; // Unreferenced -const u8 gUnknown_84687A0[] = _("You have collected all STAMPs!\nWant to input a CARD as a prize?"); +static const u8 sText_CollectedAllStamps[] = _("You have collected all STAMPs!\nWant to input a CARD as a prize?"); -/* CLIENT SCRIPTS */ +//================== +// Client scripts +//================== -const struct mevent_client_cmd gMEventClientScript_InitialListen[] = { // 84687E0 - CLI_RECEIVE(0x10), - CLI_JUMPBUF +const struct MysteryGiftClientCmd gMysteryGiftClientScript_Init[] = { + {CLI_RECV, MG_LINKID_CLIENT_SCRIPT}, + {CLI_COPY_RECV} }; -const struct mevent_client_cmd gMEventClientScript_Send1442CC[] = { - CLI_SNDHEAD, - CLI_WAITSND, - CLI_RECEIVE(0x10), - CLI_JUMPBUF +static const struct MysteryGiftClientCmd sClientScript_SendGameData[] = { + {CLI_LOAD_GAME_DATA}, + {CLI_SEND_LOADED}, + {CLI_RECV, MG_LINKID_CLIENT_SCRIPT}, + {CLI_COPY_RECV} }; -const struct mevent_client_cmd gMEventClientScript_UnableToRecv[] = { // can't accept card or news - CLI_SENDALL, - CLI_RETURN(0x0a) +static const struct MysteryGiftClientCmd sClientScript_CantAccept[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_CANT_ACCEPT} }; -const struct mevent_client_cmd gMEventClientScript_CommError[] = { // comm error - CLI_SENDALL, - CLI_RETURN(0x0b) +static const struct MysteryGiftClientCmd sClientScript_CommError[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_COMM_ERROR} }; -const struct mevent_client_cmd gMEventClientScript_NothingSentOver[] = { // nothing sent - CLI_SENDALL, - CLI_RETURN(0x00) +static const struct MysteryGiftClientCmd sClientScript_NothingSent[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_NOTHING_SENT} }; -const struct mevent_client_cmd gMEventClientScript_ReceiveCardAndReturnSuccess[] = { // card success - CLI_RECEIVE(0x16), - CLI_RECVSAV, - CLI_RECEIVE(0x19), - CLI_RECVRAM, - CLI_SENDALL, - CLI_RETURN(0x02) +static const struct MysteryGiftClientCmd sClientScript_SaveCard[] = { + {CLI_RECV, MG_LINKID_CARD}, + {CLI_SAVE_CARD}, + {CLI_RECV, MG_LINKID_RAM_SCRIPT}, + {CLI_SAVE_RAM_SCRIPT}, + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_CARD_RECEIVED} }; -const struct mevent_client_cmd gMEventClientScript_ReceiveNewsAndValidate[] = { - CLI_RECEIVE(0x17), - CLI_VLDNEWS, - CLI_WAITSND, - CLI_RECEIVE(0x10), - CLI_JUMPBUF +static const struct MysteryGiftClientCmd sClientScript_SaveNews[] = { + {CLI_RECV, MG_LINKID_NEWS}, + {CLI_SAVE_NEWS}, + {CLI_SEND_LOADED}, // Send whether or not the News was saved (read by sServerScript_SendNews) + {CLI_RECV, MG_LINKID_CLIENT_SCRIPT}, + {CLI_COPY_RECV} }; -const struct mevent_client_cmd gMEventClientScript_AlreadyHadNews[] = { // already had news - CLI_SENDALL, - CLI_RETURN(0x07) +static const struct MysteryGiftClientCmd sClientScript_HadNews[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_HAD_NEWS} }; -const struct mevent_client_cmd gMEventClientScript_RecvNewsSuccess[] = { // news success - CLI_SENDALL, - CLI_RETURN(0x03) +static const struct MysteryGiftClientCmd sClientScript_NewsReceived[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_NEWS_RECEIVED} }; -const struct mevent_client_cmd gMEventClientScript_AskWouldLikeToTossCard[] = { - CLI_REQWORD, - CLI_SNDWORD, - CLI_WAITSND, - CLI_RECEIVE(0x10), - CLI_JUMPBUF +static const struct MysteryGiftClientCmd sClientScript_AskToss[] = { + {CLI_ASK_TOSS}, + {CLI_LOAD_TOSS_RESPONSE}, + {CLI_SEND_LOADED}, + {CLI_RECV, MG_LINKID_CLIENT_SCRIPT}, + {CLI_COPY_RECV} }; -const struct mevent_client_cmd gMEventClientScript_OtherTrainerCanceled[] = { // comm canceled - CLI_SENDALL, - CLI_RETURN(0x09) +static const struct MysteryGiftClientCmd sClientScript_Canceled[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_COMM_CANCELED} }; -const struct mevent_client_cmd gMEventClientScript_AlreadyHadCard[] = { // already had card - CLI_SENDALL, - CLI_RETURN(0x05) +static const struct MysteryGiftClientCmd sClientScript_HadCard[] = { + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_HAD_CARD} }; -const struct mevent_client_cmd gMEventClientScript_SuccessFromBuffer[] = { // success from buffer - CLI_RECEIVE(0x15), - CLI_RECVBUF, - CLI_SENDALL, - CLI_RETURN(0x0d) +static const struct MysteryGiftClientCmd sClientScript_DynamicSuccess[] = { + {CLI_RECV, MG_LINKID_DYNAMIC_MSG}, + {CLI_COPY_MSG}, + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_BUFFER_SUCCESS} }; -/* SERVER SCRIPTS */ +//================== +// Server scripts +//================== -const struct mevent_server_cmd gMEventSrvScript_UnableToSend[] = { - SRV_SEND(0x10, gMEventClientScript_UnableToRecv), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x0a) +static const struct MysteryGiftServerCmd sServerScript_CantSend[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_CantAccept)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_CANT_SEND_GIFT_1} }; -const struct mevent_server_cmd gUnknown_8468950[] = { - SRV_SEND(0x10, gMEventClientScript_CommError), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x0b) +static const struct MysteryGiftServerCmd sServerScript_CommError[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_CommError)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_COMM_ERROR} }; -const struct mevent_server_cmd gUnknown_8468980[] = { - SRV_SEND(0x10, gMEventClientScript_OtherTrainerCanceled), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x09) +static const struct MysteryGiftServerCmd sServerScript_ClientCanceledNews[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_Canceled)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_CLIENT_CANCELED} }; -const struct mevent_server_cmd gMEventSrvScript_OtherTrnHasNews[] = { - SRV_SEND(0x10, gMEventClientScript_AlreadyHadNews), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x07) +static const struct MysteryGiftServerCmd sServerScript_HasNews[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_HadNews)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_HAS_NEWS} }; -const struct mevent_server_cmd gMEventSrvScript_SentNewsSuccess[] = { - SRV_SEND(0x28, gMEventClientScript_ReceiveNewsAndValidate), - SRV_WAITSND, - SRV_SEND_NEWS, - SRV_WAITSND, - SRV_RECV(0x13), - SRV_READWORD, - SRV_BRANCHIF(0x01, gMEventSrvScript_OtherTrnHasNews), - SRV_SEND(0x10, gMEventClientScript_RecvNewsSuccess), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x03) +static const struct MysteryGiftServerCmd sServerScript_SendNews[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_SaveNews)}, + {SVR_SEND}, + {SVR_LOAD_NEWS}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_RESPONSE}, + {SVR_READ_RESPONSE}, + {SVR_GOTO_IF_EQ, TRUE, sServerScript_HasNews}, // Wonder News was not saved + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_NewsReceived)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_NEWS_SENT} }; -const struct mevent_server_cmd gMEventSrvScript_SendCardSuccess[] = { - SRV_SEND(0x30, gMEventClientScript_ReceiveCardAndReturnSuccess), - SRV_WAITSND, - SRV_SEND_CARD, - SRV_WAITSND, - SRV_BUFFER_SEND, - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x02) +static const struct MysteryGiftServerCmd sServerScript_SendCard[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_SaveCard)}, + {SVR_SEND}, + {SVR_LOAD_CARD}, + {SVR_SEND}, + {SVR_LOAD_RAM_SCRIPT}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_CARD_SENT} }; -const struct mevent_server_cmd gMEventSrvScript_AskClientToOverwriteCard[] = { - SRV_SEND(0x28, gMEventClientScript_AskWouldLikeToTossCard), - SRV_WAITSND, - SRV_RECV(0x13), - SRV_READWORD, - SRV_BRANCHIF(0x00, gMEventSrvScript_SendCardSuccess), - SRV_BRANCH(gServerScript_ClientCanceledCard) +static const struct MysteryGiftServerCmd sServerScript_TossPrompt[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_AskToss)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_RESPONSE}, + {SVR_READ_RESPONSE}, + {SVR_GOTO_IF_EQ, FALSE, sServerScript_SendCard}, // Tossed old card, send new one + {SVR_GOTO, .parameter = gServerScript_ClientCanceledCard} // Kept old card, cancel new one }; -const struct mevent_server_cmd gMEventSrvScript_OtherTrnHasCard[] = { - SRV_SEND(0x10, gMEventClientScript_AlreadyHadCard), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x05) +static const struct MysteryGiftServerCmd sServerScript_HasCard[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_HadCard)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_HAS_CARD} }; -const struct mevent_server_cmd gUnknown_8468B3C[] = { - SRV_SEND(0x10, gMEventClientScript_NothingSentOver), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x00) +static const struct MysteryGiftServerCmd sServerScript_NothingSent[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_NothingSent)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_NOTHING_SENT} }; -const struct mevent_server_cmd gMEventSrvScript_SendNews[] = { - SRV_BUFFER_NEWS, - SRV_SEND(0x20, gMEventClientScript_Send1442CC), - SRV_WAITSND, - SRV_RECV(0x11), - SRV_READ_1442CC, - SRV_VALID_1442CC, - SRV_BRANCHIF(0x00, gMEventSrvScript_UnableToSend), - SRV_BRANCH(gMEventSrvScript_SentNewsSuccess) +const struct MysteryGiftServerCmd gMysteryGiftServerScript_SendWonderNews[] = { + {SVR_COPY_SAVED_NEWS}, + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_SendGameData)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_GAME_DATA}, + {SVR_COPY_GAME_DATA}, + {SVR_CHECK_GAME_DATA}, + {SVR_GOTO_IF_EQ, FALSE, sServerScript_CantSend}, + {SVR_GOTO, .parameter = sServerScript_SendNews}, }; -const struct mevent_server_cmd gMEventSrvScript_SendCard[] = { - SRV_BUFFER_CARD, - SRV_RAM_SCRIPT_IF_VALID, - SRV_SEND(0x20, gMEventClientScript_Send1442CC), - SRV_WAITSND, - SRV_RECV(0x11), - SRV_READ_1442CC, - SRV_VALID_1442CC, - SRV_BRANCHIF(0x00, gMEventSrvScript_UnableToSend), - SRV_CHECK_1442CC_14, - SRV_BRANCHIF(0x02, gMEventSrvScript_AskClientToOverwriteCard), - SRV_BRANCHIF(0x00, gMEventSrvScript_SendCardSuccess), - SRV_BRANCH(gMEventSrvScript_OtherTrnHasCard) +const struct MysteryGiftServerCmd gMysteryGiftServerScript_SendWonderCard[] = { + {SVR_COPY_SAVED_CARD}, + {SVR_COPY_SAVED_RAM_SCRIPT}, + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_SendGameData)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_GAME_DATA}, + {SVR_COPY_GAME_DATA}, + {SVR_CHECK_GAME_DATA}, + {SVR_GOTO_IF_EQ, FALSE, sServerScript_CantSend}, + {SVR_CHECK_EXISTING_CARD}, + {SVR_GOTO_IF_EQ, HAS_DIFF_CARD, sServerScript_TossPrompt}, + {SVR_GOTO_IF_EQ, HAS_NO_CARD, sServerScript_SendCard}, + {SVR_GOTO, .parameter = sServerScript_HasCard} // HAS_SAME_CARD }; diff --git a/src/mystery_gift_server.c b/src/mystery_gift_server.c index d2f2b2262..9c6868b6e 100644 --- a/src/mystery_gift_server.c +++ b/src/mystery_gift_server.c @@ -4,276 +4,286 @@ #include "mystery_gift.h" #include "mystery_gift_server.h" -EWRAM_DATA struct mevent_srv_common * s_mevent_srv_common_ptr = NULL; +enum { + FUNC_INIT, + FUNC_DONE, + FUNC_RECV, + FUNC_SEND, + FUNC_RUN, +}; -static void mevent_srv_init_common(struct mevent_srv_common *, const void *, u32, u32); -static void mevent_srv_free_resources(struct mevent_srv_common *); -static u32 mevent_srv_exec_common(struct mevent_srv_common *); +static EWRAM_DATA struct MysteryGiftServer * sServer = NULL; -extern const struct mevent_server_cmd gMEventSrvScript_SendNews[]; -extern const struct mevent_server_cmd gMEventSrvScript_SendCard[]; +static void MysteryGiftServer_Init(struct MysteryGiftServer *, const void *, u32, u32); +static void MysteryGiftServer_Free(struct MysteryGiftServer *); +static u32 MysteryGiftServer_CallFunc(struct MysteryGiftServer *); -void mevent_srv_init_wnews(void) +extern const struct MysteryGiftServerCmd gMysteryGiftServerScript_SendWonderNews[]; +extern const struct MysteryGiftServerCmd gMysteryGiftServerScript_SendWonderCard[]; + +void MysterGiftServer_CreateForNews(void) { - s_mevent_srv_common_ptr = AllocZeroed(sizeof(struct mevent_srv_common)); - mevent_srv_init_common(s_mevent_srv_common_ptr, gMEventSrvScript_SendNews, 0, 1); + sServer = AllocZeroed(sizeof(*sServer)); + MysteryGiftServer_Init(sServer, gMysteryGiftServerScript_SendWonderNews, 0, 1); } -void mevent_srv_new_wcard(void) +void MysterGiftServer_CreateForCard(void) { - s_mevent_srv_common_ptr = AllocZeroed(sizeof(struct mevent_srv_common)); - mevent_srv_init_common(s_mevent_srv_common_ptr, gMEventSrvScript_SendCard, 0, 1); + sServer = AllocZeroed(sizeof(*sServer)); + MysteryGiftServer_Init(sServer, gMysteryGiftServerScript_SendWonderCard, 0, 1); } -u32 mevent_srv_common_do_exec(u16 * a0) +u32 MysterGiftServer_Run(u16 * endVal) { u32 result; - if (s_mevent_srv_common_ptr == NULL) - return 3; - result = mevent_srv_exec_common(s_mevent_srv_common_ptr); - if (result == 3) + if (sServer == NULL) + return SVR_RET_END; + result = MysteryGiftServer_CallFunc(sServer); + if (result == SVR_RET_END) { - *a0 = s_mevent_srv_common_ptr->param; - mevent_srv_free_resources(s_mevent_srv_common_ptr); - Free(s_mevent_srv_common_ptr); - s_mevent_srv_common_ptr = NULL; + *endVal = sServer->param; + MysteryGiftServer_Free(sServer); + FREE_AND_SET_NULL(sServer); } return result; } -static void mevent_srv_init_common(struct mevent_srv_common * svr, const void *cmdBuffer, u32 sendPlayerNo, u32 recvPlayerNo) +static void MysteryGiftServer_Init(struct MysteryGiftServer * svr, const void *script, u32 sendPlayerId, u32 recvPlayerId) { - svr->unk_00 = 0; - svr->mainseqno = 0; - svr->card = AllocZeroed(sizeof(struct WonderCard)); - svr->news = AllocZeroed(sizeof(struct WonderNews)); + svr->unused = 0; + svr->funcId = FUNC_INIT; + svr->card = AllocZeroed(sizeof(*svr->card)); + svr->news = AllocZeroed(sizeof(*svr->news)); svr->recvBuffer = AllocZeroed(ME_SEND_BUF_SIZE); - svr->mevent_unk1442cc = AllocZeroed(sizeof(struct MysteryGiftLinkGameData)); - svr->cmdBuffer = cmdBuffer; + svr->linkGameData = AllocZeroed(sizeof(*svr->linkGameData)); + svr->script = script; svr->cmdidx = 0; - MysteryGiftLink_Init(&svr->manager, sendPlayerNo, recvPlayerNo); + MysteryGiftLink_Init(&svr->manager, sendPlayerId, recvPlayerId); } -static void mevent_srv_free_resources(struct mevent_srv_common * svr) +static void MysteryGiftServer_Free(struct MysteryGiftServer * svr) { Free(svr->card); Free(svr->news); Free(svr->recvBuffer); - Free(svr->mevent_unk1442cc); + Free(svr->linkGameData); } -static void mevent_srv_common_init_send(struct mevent_srv_common * svr, u32 ident, const void *src, u32 size) +static void MysteryGiftServer_InitSend(struct MysteryGiftServer * svr, u32 ident, const void *src, u32 size) { - AGB_ASSERT_EX(size <= ME_SEND_BUF_SIZE, ABSPATH("mevent_server.c"), 257); + AGB_ASSERT_EX(size <= ME_SEND_BUF_SIZE, ABSPATH("mevent_server.c"), 257); MysteryGiftLink_InitSend(&svr->manager, ident, src, size); } -static void *mevent_first_if_not_null_else_second(void *a0, void *a1) +// Given the command pointer parameter and the 'default' normal data. +// If the command's pointer is not empty use that as the send data, otherwise use the default. +static const void *MysteryGiftServer_GetSendData(const void *dynamicData, const void *defaultData) { - if (a0 != NULL) - return a0; + if (dynamicData != NULL) + return dynamicData; else - return a1; + return defaultData; } -static u32 mevent_compare_pointers(void *a0, void *a1) +static u32 MysteryGiftServer_Compare(const void *a, const void *b) { - if (a1 < a0) + if (b < a) return 0; - else if (a1 == a0) + else if (b == a) return 1; else return 2; } -static u32 common_mainseq_0(struct mevent_srv_common * svr) +static u32 Server_Init(struct MysteryGiftServer * svr) { - // start - svr->mainseqno = 4; - return 0; + svr->funcId = FUNC_RUN; + return SVR_RET_INIT; } -static u32 common_mainseq_1(struct mevent_srv_common * svr) +static u32 Server_Done(struct MysteryGiftServer * svr) { - // done - return 3; + return SVR_RET_END; } -static u32 common_mainseq_2(struct mevent_srv_common * svr) +static u32 Server_Recv(struct MysteryGiftServer * svr) { - // do recv if (MysteryGiftLink_Recv(&svr->manager)) - svr->mainseqno = 4; - return 1; + svr->funcId = FUNC_RUN; + return SVR_RET_ACTIVE; } -static u32 common_mainseq_3(struct mevent_srv_common * svr) +static u32 Server_Send(struct MysteryGiftServer * svr) { - // do send if (MysteryGiftLink_Send(&svr->manager)) - svr->mainseqno = 4; - return 1; + svr->funcId = FUNC_RUN; + return SVR_RET_ACTIVE; } -static u32 common_mainseq_4(struct mevent_srv_common * svr) +static u32 Server_Run(struct MysteryGiftServer * svr) { // process command - const struct mevent_server_cmd * cmd = &svr->cmdBuffer[svr->cmdidx]; - void *ptr; + const struct MysteryGiftServerCmd * cmd = &svr->script[svr->cmdidx]; + const void *ptr; svr->cmdidx++; switch (cmd->instr) { - case 0: - AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 354); - svr->mainseqno = 1; - svr->param = cmd->flag; - break; - case 1: - svr->mainseqno = 3; - break; - case 2: - AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 364); - MysteryGiftLink_InitRecv(&svr->manager, cmd->flag, svr->recvBuffer); - svr->mainseqno = 2; - break; - case 3: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 370); + case SVR_RETURN: + AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 354); + svr->funcId = FUNC_DONE; + svr->param = cmd->flag; // Set for endVal in MysteryGiftServer_Run + break; + case SVR_SEND: + svr->funcId = FUNC_SEND; + break; + case SVR_RECV: + AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 364); + MysteryGiftLink_InitRecv(&svr->manager, cmd->flag, svr->recvBuffer); + svr->funcId = FUNC_RECV; + break; + case SVR_GOTO: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 370); + svr->cmdidx = 0; + svr->script = cmd->parameter; + break; + case SVR_COPY_GAME_DATA: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 376); + AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 377); + memcpy(svr->linkGameData, svr->recvBuffer, sizeof(*svr->linkGameData)); + break; + case SVR_CHECK_GAME_DATA: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 382); + AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 383); + svr->param = MysteryGift_ValidateLinkGameData(svr->linkGameData); + break; + case SVR_GOTO_IF_EQ: + if (svr->param == cmd->flag) + { svr->cmdidx = 0; - svr->cmdBuffer = cmd->parameter; - break; - case 5: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 376); - AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 377); - memcpy(svr->mevent_unk1442cc, svr->recvBuffer, sizeof(struct MysteryGiftLinkGameData)); - break; - case 6: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 382); - AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 383); - svr->param = MysteryGift_ValidateLinkGameData(svr->mevent_unk1442cc); - break; - case 4: - if (svr->param == cmd->flag) - { - svr->cmdidx = 0; - svr->cmdBuffer = cmd->parameter; - } - break; - case 7: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 396); - ptr = mevent_first_if_not_null_else_second(cmd->parameter, svr->card); - svr->param = MysteryGift_CompareCardFlags(ptr, svr->mevent_unk1442cc, ptr); - break; - case 8: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 402); - AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 403); - svr->param = *(u32 *)svr->recvBuffer; - break; - case 9: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 408); - ptr = mevent_first_if_not_null_else_second(cmd->parameter, &svr->sendWord); - svr->param = MysteryGift_CheckStamps(ptr, svr->mevent_unk1442cc, ptr); - break; - case 10: - AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 415); - svr->param = MysteryGift_GetCardStatFromLinkData(svr->mevent_unk1442cc, cmd->flag); - break; - case 11: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 420); - svr->param = MysteryGift_DoesQuestionnaireMatch(svr->mevent_unk1442cc, cmd->parameter); - break; - case 12: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 426); - svr->param = mevent_compare_pointers(cmd->parameter, *(void **)svr->recvBuffer); - break; - case 14: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 432); - mevent_srv_common_init_send(svr, 0x17, mevent_first_if_not_null_else_second(cmd->parameter, svr->news), sizeof(struct WonderNews)); - break; - case 13: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 438); - mevent_srv_common_init_send(svr, 0x16, mevent_first_if_not_null_else_second(cmd->parameter, svr->card), sizeof(struct WonderCard)); - break; - case 16: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 444); - mevent_srv_common_init_send(svr, 0x18, mevent_first_if_not_null_else_second(cmd->parameter, &svr->sendWord), 4); - break; - case 15: - if (cmd->parameter == NULL) - mevent_srv_common_init_send(svr, 0x19, svr->sendBuffer1, svr->sendBuffer1Size); - else - mevent_srv_common_init_send(svr, 0x19, cmd->parameter, cmd->flag); - break; - case 18: - if (cmd->parameter == NULL) - mevent_srv_common_init_send(svr, 0x10, svr->sendBuffer2, svr->sendBuffer2Size); - else - mevent_srv_common_init_send(svr, 0x10, cmd->parameter, cmd->flag); - break; - case 19: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 466); - mevent_srv_common_init_send(svr, 0x1a, cmd->parameter, 188); - break; - case 20: - mevent_srv_common_init_send(svr, 0x15, cmd->parameter, cmd->flag); - break; - case 17: - mevent_srv_common_init_send(svr, 0x1c, cmd->parameter, cmd->flag); - break; - case 22: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 481); - memcpy(svr->card, cmd->parameter, 332); - break; - case 23: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 486); - memcpy(svr->news, cmd->parameter, 444); - break; - case 21: - AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 491); - svr->sendWord = *(u32 *)cmd->parameter; - break; - case 24: - svr->sendBuffer1 = cmd->parameter; - svr->sendBuffer1Size = cmd->flag; - break; - case 25: - svr->sendBuffer2 = cmd->parameter; - svr->sendBuffer2Size = cmd->flag; - break; - case 26: - AGB_ASSERT_EX(cmd->flag == FALSE && cmd->parameter == NULL, ABSPATH("mevent_server.c"), 506); - memcpy(svr->card, GetSavedWonderCard(), 332); - DisableWonderCardSending(svr->card); - break; - case 27: - AGB_ASSERT_EX(cmd->flag == FALSE && cmd->parameter == NULL, ABSPATH("mevent_server.c"), 512); - memcpy(svr->news, GetSavedWonderNews(), 444); - break; - case 28: - AGB_ASSERT_EX(cmd->flag == FALSE && cmd->parameter == NULL, ABSPATH("mevent_server.c"), 517); - svr->sendBuffer1 = GetSavedRamScriptIfValid(); - break; - case 29: - mevent_srv_common_init_send(svr, 0x1b, cmd->parameter, cmd->flag); - break; + svr->script = cmd->parameter; + } + break; + case SVR_CHECK_EXISTING_CARD: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 396); + ptr = MysteryGiftServer_GetSendData(cmd->parameter, svr->card); + svr->param = MysteryGift_CompareCardFlags(ptr, svr->linkGameData, ptr); + break; + case SVR_READ_RESPONSE: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 402); + AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 403); + svr->param = *(u32 *)svr->recvBuffer; + break; + case SVR_CHECK_EXISTING_STAMPS: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 408); + ptr = MysteryGiftServer_GetSendData(cmd->parameter, &svr->stamp); + svr->param = MysteryGift_CheckStamps(ptr, svr->linkGameData, ptr); + break; + case SVR_GET_CARD_STAT: + AGB_ASSERT_EX(cmd->parameter == NULL, ABSPATH("mevent_server.c"), 415); + svr->param = MysteryGift_GetCardStatFromLinkData(svr->linkGameData, cmd->flag); + break; + case SVR_CHECK_QUESTIONNAIRE: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 420); + svr->param = MysteryGift_DoesQuestionnaireMatch(svr->linkGameData, cmd->parameter); + break; + case SVR_COMPARE: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 426); + svr->param = MysteryGiftServer_Compare(cmd->parameter, *(void **)svr->recvBuffer); + break; + case SVR_LOAD_NEWS: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 432); + MysteryGiftServer_InitSend(svr, MG_LINKID_NEWS, MysteryGiftServer_GetSendData(cmd->parameter, svr->news), sizeof(struct WonderNews)); + break; + case SVR_LOAD_CARD: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 438); + MysteryGiftServer_InitSend(svr, MG_LINKID_CARD, MysteryGiftServer_GetSendData(cmd->parameter, svr->card), sizeof(struct WonderCard)); + break; + case SVR_LOAD_STAMP: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 444); + MysteryGiftServer_InitSend(svr, MG_LINKID_STAMP, MysteryGiftServer_GetSendData(cmd->parameter, &svr->stamp), sizeof(svr->stamp)); + break; + case SVR_LOAD_RAM_SCRIPT: + if (cmd->parameter == NULL) + MysteryGiftServer_InitSend(svr, MG_LINKID_RAM_SCRIPT, svr->ramScript, svr->ramScriptSize); + else + MysteryGiftServer_InitSend(svr, MG_LINKID_RAM_SCRIPT, cmd->parameter, cmd->flag); + break; + case SVR_LOAD_CLIENT_SCRIPT: + if (cmd->parameter == NULL) + MysteryGiftServer_InitSend(svr, MG_LINKID_CLIENT_SCRIPT, svr->clientScript, svr->clientScriptSize); + else + MysteryGiftServer_InitSend(svr, MG_LINKID_CLIENT_SCRIPT, cmd->parameter, cmd->flag); + break; + case SVR_LOAD_EREADER_TRAINER: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 466); + MysteryGiftServer_InitSend(svr, MG_LINKID_EREADER_TRAINER, cmd->parameter, 188); + break; + case SVR_LOAD_MSG: + MysteryGiftServer_InitSend(svr, MG_LINKID_DYNAMIC_MSG, cmd->parameter, cmd->flag); + break; + case SVR_LOAD_UNK_2: + MysteryGiftServer_InitSend(svr, MG_LINKID_UNK_2, cmd->parameter, cmd->flag); + break; + case SVR_COPY_CARD: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 481); + memcpy(svr->card, cmd->parameter, sizeof(*svr->card)); + break; + case SVR_COPY_NEWS: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 486); + memcpy(svr->news, cmd->parameter, sizeof(*svr->news)); + break; + case SVR_COPY_STAMP: + AGB_ASSERT_EX(cmd->flag == FALSE, ABSPATH("mevent_server.c"), 491); + svr->stamp = *(u32 *)cmd->parameter; + break; + case SVR_SET_RAM_SCRIPT: + svr->ramScript = cmd->parameter; + svr->ramScriptSize = cmd->flag; + break; + case SVR_SET_CLIENT_SCRIPT: + svr->clientScript = cmd->parameter; + svr->clientScriptSize = cmd->flag; + break; + case SVR_COPY_SAVED_CARD: + AGB_ASSERT_EX(cmd->flag == FALSE && cmd->parameter == NULL, ABSPATH("mevent_server.c"), 506); + memcpy(svr->card, GetSavedWonderCard(), sizeof(*svr->card)); + DisableWonderCardSending(svr->card); + break; + case SVR_COPY_SAVED_NEWS: + AGB_ASSERT_EX(cmd->flag == FALSE && cmd->parameter == NULL, ABSPATH("mevent_server.c"), 512); + memcpy(svr->news, GetSavedWonderNews(), sizeof(*svr->news)); + break; + case SVR_COPY_SAVED_RAM_SCRIPT: + AGB_ASSERT_EX(cmd->flag == FALSE && cmd->parameter == NULL, ABSPATH("mevent_server.c"), 517); + svr->ramScript = GetSavedRamScriptIfValid(); + break; + case SVR_LOAD_UNK_1: + MysteryGiftServer_InitSend(svr, MG_LINKID_UNK_1, cmd->parameter, cmd->flag); + break; } - return 1; + return SVR_RET_ACTIVE; } -static u32 (*const func_tbl[])(struct mevent_srv_common *) = { - common_mainseq_0, - common_mainseq_1, - common_mainseq_2, - common_mainseq_3, - common_mainseq_4 +static u32 (*const sFuncTable[])(struct MysteryGiftServer *) = { + [FUNC_INIT] = Server_Init, + [FUNC_DONE] = Server_Done, + [FUNC_RECV] = Server_Recv, + [FUNC_SEND] = Server_Send, + [FUNC_RUN] = Server_Run }; -static u32 mevent_srv_exec_common(struct mevent_srv_common * svr) +static u32 MysteryGiftServer_CallFunc(struct MysteryGiftServer * svr) { +// Original GF names +#define mainseqno funcId +#define func_tbl sFuncTable u32 response; AGB_ASSERT_EX(svr->mainseqno < NELEMS(func_tbl), ABSPATH("mevent_server.c"), 546); - response = func_tbl[svr->mainseqno](svr); + response = sFuncTable[svr->funcId](svr); AGB_ASSERT_EX(svr->mainseqno < NELEMS(func_tbl), ABSPATH("mevent_server.c"), 548); return response; +#undef mainseqno +#undef func_tbl } diff --git a/src/union_room_message.c b/src/union_room_message.c index d3c3c6e46..a1368e9fa 100644 --- a/src/union_room_message.c +++ b/src/union_room_message.c @@ -1,6 +1,7 @@ #include "global.h" #include "link_rfu.h" #include "mystery_gift_server.h" +#include "mystery_gift_client.h" #include "constants/union_room.h" ALIGNED(4) const u8 gText_UR_EmptyString[] = _(""); @@ -558,18 +559,18 @@ const u8 *const gTexts_UR_GladToMeetYou[GENDER_COUNT] = { ALIGNED(4) const u8 gText_UR_FinishedCheckingPlayersTrainerCard[] = _("Finished checking {SPECIAL_F7 0x01}'s\nTRAINER CARD.{PAUSE 60}"); ALIGNED(4) static const u8 sText_CanceledReadingCard[] = _("Canceled reading the Card."); -static const struct mevent_client_cmd sClientScript_DynamicError[] = { - CLI_RECEIVE(0x15), - CLI_RECVBUF, - CLI_SENDALL, - CLI_RETURN(0x0e) +static const struct MysteryGiftClientCmd sClientScript_DynamicError[] = { + {CLI_RECV, MG_LINKID_DYNAMIC_MSG}, + {CLI_COPY_MSG}, + {CLI_SEND_READY_END}, + {CLI_RETURN, CLI_MSG_BUFFER_FAILURE} }; -const struct mevent_server_cmd gServerScript_ClientCanceledCard[] = { - SRV_SEND(0x20, sClientScript_DynamicError), - SRV_WAITSND, - SRV_SENDSTR(0x1b, sText_CanceledReadingCard), - SRV_WAITSND, - SRV_RECV(0x14), - SRV_RETURN(0x09) +const struct MysteryGiftServerCmd gServerScript_ClientCanceledCard[] = { + {SVR_LOAD_CLIENT_SCRIPT, PTR_ARG(sClientScript_DynamicError)}, + {SVR_SEND}, + {SVR_LOAD_MSG, PTR_ARG(sText_CanceledReadingCard)}, + {SVR_SEND}, + {SVR_RECV, MG_LINKID_READY_END}, + {SVR_RETURN, SVR_MSG_CLIENT_CANCELED} };