2019-04-02 14:43:16 +02:00
|
|
|
#include "global.h"
|
2019-09-09 03:07:54 +02:00
|
|
|
#include "malloc.h"
|
2019-04-02 16:54:51 +02:00
|
|
|
#include "script.h"
|
2021-10-17 09:00:48 +02:00
|
|
|
#include "mystery_gift.h"
|
|
|
|
#include "mystery_gift_server.h"
|
|
|
|
#include "mystery_gift_link.h"
|
2019-04-02 14:43:16 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
enum {
|
|
|
|
FUNC_INIT,
|
|
|
|
FUNC_DONE,
|
|
|
|
FUNC_RECV,
|
|
|
|
FUNC_SEND,
|
|
|
|
FUNC_RUN,
|
|
|
|
};
|
|
|
|
|
|
|
|
EWRAM_DATA static struct MysteryGiftServer * sServer = NULL;
|
2019-04-02 14:43:16 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static void MysteryGiftServer_Init(struct MysteryGiftServer *, const void *, u32, u32);
|
|
|
|
static void MysteryGiftServer_Free(struct MysteryGiftServer *);
|
|
|
|
static u32 MysteryGiftServer_CallFunc(struct MysteryGiftServer *);
|
2019-04-02 14:43:16 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
extern const struct MysteryGiftServerCmd gMysteryGiftServerScript_SendWonderNews[];
|
|
|
|
extern const struct MysteryGiftServerCmd gMysteryGiftServerScript_SendWonderCard[];
|
2019-04-02 14:43:16 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
void MysterGiftServer_CreateForNews(void)
|
2019-04-02 14:43:16 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
sServer = AllocZeroed(sizeof(*sServer));
|
|
|
|
MysteryGiftServer_Init(sServer, gMysteryGiftServerScript_SendWonderNews, 0, 1);
|
2019-04-02 14:43:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
void MysterGiftServer_CreateForCard(void)
|
2019-04-02 14:43:16 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
sServer = AllocZeroed(sizeof(*sServer));
|
|
|
|
MysteryGiftServer_Init(sServer, gMysteryGiftServerScript_SendWonderCard, 0, 1);
|
2019-04-02 14:43:16 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
u32 MysterGiftServer_Run(u16 * endVal)
|
2019-04-02 14:43:16 +02:00
|
|
|
{
|
|
|
|
u32 result;
|
2021-10-15 18:56:14 +02:00
|
|
|
if (sServer == NULL)
|
|
|
|
return SVR_RET_END;
|
|
|
|
result = MysteryGiftServer_CallFunc(sServer);
|
|
|
|
if (result == SVR_RET_END)
|
2019-04-02 14:43:16 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
*endVal = sServer->param;
|
|
|
|
MysteryGiftServer_Free(sServer);
|
2022-06-03 16:56:11 +02:00
|
|
|
FREE_AND_SET_NULL(sServer);
|
2019-04-02 14:43:16 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static void MysteryGiftServer_Init(struct MysteryGiftServer * svr, const void * script, u32 sendPlayerId, u32 recvPlayerId)
|
2019-04-02 14:43:16 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
svr->unused = 0;
|
|
|
|
svr->funcId = FUNC_INIT;
|
|
|
|
svr->card = AllocZeroed(sizeof(*svr->card));
|
|
|
|
svr->news = AllocZeroed(sizeof(*svr->news));
|
|
|
|
svr->recvBuffer = AllocZeroed(MG_LINK_BUFFER_SIZE);
|
|
|
|
svr->linkGameData = AllocZeroed(sizeof(*svr->linkGameData));
|
|
|
|
svr->script = script;
|
2019-04-02 14:43:16 +02:00
|
|
|
svr->cmdidx = 0;
|
2021-10-15 18:56:14 +02:00
|
|
|
MysteryGiftLink_Init(&svr->link, sendPlayerId, recvPlayerId);
|
2019-04-02 14:43:16 +02:00
|
|
|
}
|
2019-04-02 16:54:51 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static void MysteryGiftServer_Free(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
Free(svr->card);
|
|
|
|
Free(svr->news);
|
2019-04-02 16:54:51 +02:00
|
|
|
Free(svr->recvBuffer);
|
2021-10-15 18:56:14 +02:00
|
|
|
Free(svr->linkGameData);
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static void MysteryGiftServer_InitSend(struct MysteryGiftServer * svr, u32 ident, const void * src, u32 size)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
AGB_ASSERT(size <= MG_LINK_BUFFER_SIZE);
|
|
|
|
MysteryGiftLink_InitSend(&svr->link, ident, src, size);
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
// 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)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
if (dynamicData != NULL)
|
|
|
|
return dynamicData;
|
2019-04-02 16:54:51 +02:00
|
|
|
else
|
2021-10-15 18:56:14 +02:00
|
|
|
return defaultData;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 MysteryGiftServer_Compare(const void * a, const void * b)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
if (b < a)
|
2019-04-02 16:54:51 +02:00
|
|
|
return 0;
|
2021-10-15 18:56:14 +02:00
|
|
|
else if (b == a)
|
2019-04-02 16:54:51 +02:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 Server_Init(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
svr->funcId = FUNC_RUN;
|
|
|
|
return SVR_RET_INIT;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 Server_Done(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
return SVR_RET_END;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 Server_Recv(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
if (MysteryGiftLink_Recv(&svr->link))
|
|
|
|
svr->funcId = FUNC_RUN;
|
|
|
|
return SVR_RET_ACTIVE;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 Server_Send(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
if (MysteryGiftLink_Send(&svr->link))
|
|
|
|
svr->funcId = FUNC_RUN;
|
|
|
|
return SVR_RET_ACTIVE;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 Server_Run(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
|
|
|
// process command
|
2021-10-15 18:56:14 +02:00
|
|
|
const struct MysteryGiftServerCmd * cmd = &svr->script[svr->cmdidx];
|
2019-04-02 19:26:06 +02:00
|
|
|
const void * ptr;
|
2019-04-02 16:54:51 +02:00
|
|
|
svr->cmdidx++;
|
|
|
|
|
|
|
|
switch (cmd->instr)
|
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_RETURN:
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
|
|
|
svr->funcId = FUNC_DONE;
|
|
|
|
svr->param = cmd->parameter; // Set for endVal in MysteryGiftServer_Run
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_SEND:
|
|
|
|
svr->funcId = FUNC_SEND;
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_RECV:
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
|
|
|
MysteryGiftLink_InitRecv(&svr->link, cmd->parameter, svr->recvBuffer);
|
|
|
|
svr->funcId = FUNC_RECV;
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_GOTO:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
2019-04-02 16:54:51 +02:00
|
|
|
svr->cmdidx = 0;
|
2021-10-15 18:56:14 +02:00
|
|
|
svr->script = cmd->ptr;
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_GAME_DATA:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
|
|
|
memcpy(svr->linkGameData, svr->recvBuffer, sizeof(*svr->linkGameData));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_CHECK_GAME_DATA_CARD:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
|
|
|
svr->param = MysteryGift_ValidateLinkGameData(svr->linkGameData, FALSE);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_CHECK_GAME_DATA_NEWS:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
|
|
|
svr->param = MysteryGift_ValidateLinkGameData(svr->linkGameData, TRUE);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_GOTO_IF_EQ:
|
|
|
|
if (svr->param == cmd->parameter)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
|
|
|
svr->cmdidx = 0;
|
2021-10-15 18:56:14 +02:00
|
|
|
svr->script = cmd->ptr;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_CHECK_EXISTING_CARD:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
ptr = MysteryGiftServer_GetSendData(cmd->ptr, svr->card);
|
|
|
|
svr->param = MysteryGift_CompareCardFlags(ptr, svr->linkGameData, ptr);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_READ_RESPONSE:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
2019-04-02 16:54:51 +02:00
|
|
|
svr->param = *(u32 *)svr->recvBuffer;
|
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_CHECK_EXISTING_STAMPS:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
ptr = MysteryGiftServer_GetSendData(cmd->ptr, &svr->stamp);
|
|
|
|
svr->param = MysteryGift_CheckStamps(ptr, svr->linkGameData, ptr);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_GET_CARD_STAT:
|
|
|
|
AGB_ASSERT(cmd->ptr == NULL);
|
|
|
|
svr->param = MysteryGift_GetCardStatFromLinkData(svr->linkGameData, cmd->parameter);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_CHECK_QUESTIONNAIRE:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
svr->param = MysteryGift_DoesQuestionnaireMatch(svr->linkGameData, cmd->ptr);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COMPARE:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
svr->param = MysteryGiftServer_Compare(cmd->ptr, *(void **)svr->recvBuffer);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_NEWS:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_NEWS, MysteryGiftServer_GetSendData(cmd->ptr, svr->news), sizeof(*svr->news));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_CARD:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_CARD, MysteryGiftServer_GetSendData(cmd->ptr, svr->card), sizeof(*svr->card));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_STAMP:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_STAMP, MysteryGiftServer_GetSendData(cmd->ptr, &svr->stamp), sizeof(svr->stamp));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_RAM_SCRIPT:
|
|
|
|
if (cmd->ptr == NULL)
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_RAM_SCRIPT, svr->ramScript, svr->ramScriptSize);
|
2019-04-02 16:54:51 +02:00
|
|
|
else
|
2021-10-15 18:56:14 +02:00
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_RAM_SCRIPT, cmd->ptr, cmd->parameter);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_CLIENT_SCRIPT:
|
|
|
|
if (cmd->ptr == NULL)
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_CLIENT_SCRIPT, svr->clientScript, svr->clientScriptSize);
|
2019-04-02 16:54:51 +02:00
|
|
|
else
|
2021-10-15 18:56:14 +02:00
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_CLIENT_SCRIPT, cmd->ptr, cmd->parameter);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_EREADER_TRAINER:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_EREADER_TRAINER, cmd->ptr, sizeof(struct BattleTowerEReaderTrainer));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_MSG:
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_DYNAMIC_MSG, cmd->ptr, cmd->parameter);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_UNK_2:
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_UNK_2, cmd->ptr, cmd->parameter);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_CARD:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
memcpy(svr->card, cmd->ptr, sizeof(*svr->card));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_NEWS:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
memcpy(svr->news, cmd->ptr, sizeof(*svr->news));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_STAMP:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0);
|
|
|
|
svr->stamp = *(u32 *)cmd->ptr;
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_SET_RAM_SCRIPT:
|
|
|
|
svr->ramScript = cmd->ptr;
|
|
|
|
svr->ramScriptSize = cmd->parameter;
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_SET_CLIENT_SCRIPT:
|
|
|
|
svr->clientScript = cmd->ptr;
|
|
|
|
svr->clientScriptSize = cmd->parameter;
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_SAVED_CARD:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0 && cmd->ptr == NULL);
|
|
|
|
memcpy(svr->card, GetSavedWonderCard(), sizeof(*svr->card));
|
|
|
|
DisableWonderCardSending(svr->card);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_SAVED_NEWS:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0 && cmd->ptr == NULL);
|
|
|
|
memcpy(svr->news, GetSavedWonderNews(), sizeof(*svr->news));
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_COPY_SAVED_RAM_SCRIPT:
|
|
|
|
AGB_ASSERT(cmd->parameter == 0 && cmd->ptr == NULL);
|
|
|
|
svr->ramScript = GetSavedRamScriptIfValid();
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
2021-10-15 18:56:14 +02:00
|
|
|
case SVR_LOAD_UNK_1:
|
|
|
|
MysteryGiftServer_InitSend(svr, MG_LINKID_UNK_1, cmd->ptr, cmd->parameter);
|
2019-04-02 16:54:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
return SVR_RET_ACTIVE;
|
2019-04-02 16:54:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
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
|
2019-04-02 16:54:51 +02:00
|
|
|
};
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static u32 MysteryGiftServer_CallFunc(struct MysteryGiftServer * svr)
|
2019-04-02 16:54:51 +02:00
|
|
|
{
|
|
|
|
u32 response;
|
2021-10-15 18:56:14 +02:00
|
|
|
AGB_ASSERT(svr->funcId < ARRAY_COUNT(sFuncTable));
|
|
|
|
response = sFuncTable[svr->funcId](svr);
|
|
|
|
AGB_ASSERT(svr->funcId < ARRAY_COUNT(sFuncTable));
|
2019-04-02 16:54:51 +02:00
|
|
|
return response;
|
|
|
|
}
|