2019-04-02 16:54:51 +02:00
|
|
|
#include "global.h"
|
2019-09-09 03:07:54 +02:00
|
|
|
#include "malloc.h"
|
2019-04-02 18:22:39 +02:00
|
|
|
#include "decompress.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "link.h"
|
|
|
|
#include "link_rfu.h"
|
|
|
|
#include "overworld.h"
|
|
|
|
#include "script.h"
|
|
|
|
#include "battle_tower.h"
|
|
|
|
#include "mystery_event_script.h"
|
2021-10-17 09:00:48 +02:00
|
|
|
#include "mystery_gift.h"
|
|
|
|
#include "mystery_gift_link.h"
|
2019-04-02 18:22:39 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
/*
|
|
|
|
Handles the link connection functions used by the Mystery Gift client/server.
|
|
|
|
Note: MysteryGiftLink is shortened to MGL for internal functions.
|
|
|
|
*/
|
2019-04-02 18:22:39 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
struct SendRecvHeader
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
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);
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
u32 MysteryGiftLink_Send(struct MysteryGiftLink * link)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
return link->sendFunc(link);
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
void MysteryGiftLink_Init(struct MysteryGiftLink * link, u32 sendPlayerId, u32 recvPlayerId)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
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;
|
2021-10-17 08:18:24 +02:00
|
|
|
link->sendBuffer = NULL;
|
|
|
|
link->recvBuffer = NULL;
|
2021-10-15 18:56:14 +02:00
|
|
|
link->sendFunc = MGL_Send;
|
|
|
|
link->recvFunc = MGL_Receive;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
void MysteryGiftLink_InitSend(struct MysteryGiftLink * link, u32 ident, const void * src, u32 size)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
link->state = 0;
|
|
|
|
link->sendIdent = ident;
|
|
|
|
link->sendCounter = 0;
|
|
|
|
link->sendCRC = 0;
|
2019-04-02 18:22:39 +02:00
|
|
|
if (size != 0)
|
2021-10-15 18:56:14 +02:00
|
|
|
link->sendSize = size;
|
2019-04-02 18:22:39 +02:00
|
|
|
else
|
2021-10-15 18:56:14 +02:00
|
|
|
link->sendSize = MG_LINK_BUFFER_SIZE;
|
2021-10-17 08:18:24 +02:00
|
|
|
link->sendBuffer = src;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
void MysteryGiftLink_InitRecv(struct MysteryGiftLink * link, u32 ident, void * dest)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
link->state = 0;
|
|
|
|
link->recvIdent = ident;
|
|
|
|
link->recvCounter = 0;
|
|
|
|
link->recvCRC = 0;
|
|
|
|
link->recvSize = 0;
|
2021-10-17 08:18:24 +02:00
|
|
|
link->recvBuffer = dest;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static void MGL_ReceiveBlock(u32 playerId, void * dest, size_t size)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
memcpy(dest, gBlockRecvBuffer[playerId], size);
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static bool32 MGL_HasReceived(u32 playerId)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
if ((GetBlockReceivedStatus() >> playerId) & 1)
|
2019-04-02 18:22:39 +02:00
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static void MGL_ResetReceived(u32 playerId)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
ResetBlockReceivedFlag(playerId);
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static bool32 MGL_Receive(struct MysteryGiftLink * link)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
struct SendRecvHeader header;
|
2019-04-02 18:22:39 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
switch (link->state)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
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 > MG_LINK_BUFFER_SIZE)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
LinkRfu_FatalError();
|
|
|
|
return FALSE;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
2021-10-15 18:56:14 +02:00
|
|
|
else if (link->recvIdent != header.ident)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2019-04-03 15:55:17 +02:00
|
|
|
LinkRfu_FatalError();
|
2019-04-02 18:22:39 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
link->recvCounter = 0;
|
|
|
|
MGL_ResetReceived(link->recvPlayerId);
|
|
|
|
link->state++;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
2021-10-15 18:56:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (MGL_HasReceived(link->recvPlayerId))
|
|
|
|
{
|
|
|
|
size_t blocksize = link->recvCounter * 252;
|
|
|
|
if (link->recvSize - blocksize <= 252)
|
|
|
|
{
|
2021-10-17 08:18:24 +02:00
|
|
|
MGL_ReceiveBlock(link->recvPlayerId, link->recvBuffer + blocksize, link->recvSize - blocksize);
|
2021-10-15 18:56:14 +02:00
|
|
|
link->recvCounter++;
|
|
|
|
link->state++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-17 08:18:24 +02:00
|
|
|
MGL_ReceiveBlock(link->recvPlayerId, link->recvBuffer + blocksize, 252);
|
2021-10-15 18:56:14 +02:00
|
|
|
link->recvCounter++;
|
|
|
|
}
|
|
|
|
MGL_ResetReceived(link->recvPlayerId);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
2021-10-17 08:18:24 +02:00
|
|
|
if (CalcCRC16WithTable(link->recvBuffer, link->recvSize) != link->recvCRC)
|
2021-10-15 18:56:14 +02:00
|
|
|
{
|
|
|
|
LinkRfu_FatalError();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
link->state = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
static bool32 MGL_Send(struct MysteryGiftLink * link)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
struct SendRecvHeader header;
|
2019-04-02 18:22:39 +02:00
|
|
|
|
2021-10-15 18:56:14 +02:00
|
|
|
switch (link->state)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
case 0:
|
|
|
|
if (IsLinkTaskFinished())
|
|
|
|
{
|
|
|
|
header.ident = link->sendIdent;
|
|
|
|
header.size = link->sendSize;
|
2021-10-17 08:18:24 +02:00
|
|
|
header.crc = CalcCRC16WithTable(link->sendBuffer, link->sendSize);
|
2021-10-15 18:56:14 +02:00
|
|
|
link->sendCRC = header.crc;
|
|
|
|
link->sendCounter = 0;
|
|
|
|
SendBlock(0, &header, sizeof(header));
|
|
|
|
link->state++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (IsLinkTaskFinished())
|
|
|
|
{
|
|
|
|
if (MGL_HasReceived(link->sendPlayerId))
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-15 18:56:14 +02:00
|
|
|
size_t blocksize;
|
|
|
|
MGL_ResetReceived(link->sendPlayerId);
|
|
|
|
blocksize = 252 * link->sendCounter;
|
|
|
|
if (link->sendSize - blocksize <= 252)
|
2019-04-02 18:22:39 +02:00
|
|
|
{
|
2021-10-17 08:18:24 +02:00
|
|
|
SendBlock(0, link->sendBuffer + blocksize, link->sendSize - blocksize);
|
2021-10-15 18:56:14 +02:00
|
|
|
link->sendCounter++;
|
|
|
|
link->state++;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
else
|
2021-10-15 18:56:14 +02:00
|
|
|
{
|
2021-10-17 08:18:24 +02:00
|
|
|
SendBlock(0, link->sendBuffer + blocksize, 252);
|
2021-10-15 18:56:14 +02:00
|
|
|
link->sendCounter++;
|
|
|
|
}
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
2021-10-15 18:56:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (IsLinkTaskFinished())
|
|
|
|
{
|
2021-10-17 08:18:24 +02:00
|
|
|
if (CalcCRC16WithTable(link->sendBuffer, link->sendSize) != link->sendCRC)
|
2021-10-15 18:56:14 +02:00
|
|
|
LinkRfu_FatalError();
|
|
|
|
else
|
|
|
|
link->state++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
if (MGL_HasReceived(link->sendPlayerId))
|
|
|
|
{
|
|
|
|
MGL_ResetReceived(link->sendPlayerId);
|
|
|
|
link->state = 0;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
break;
|
2019-04-02 18:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|