forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmystery_gift_link.c
222 lines (205 loc) · 5.6 KB
/
mystery_gift_link.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "global.h"
#include "malloc.h"
#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"
#include "mystery_gift.h"
#include "mystery_gift_link.h"
/*
Handles the link connection functions used by the Mystery Gift client/server.
Note: MysteryGiftLink is shortened to MGL for internal functions.
*/
struct SendRecvHeader
{
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 MysteryGiftLink_Send(struct MysteryGiftLink * link)
{
return link->sendFunc(link);
}
void MysteryGiftLink_Init(struct MysteryGiftLink * link, u32 sendPlayerId, u32 recvPlayerId)
{
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 MysteryGiftLink_InitSend(struct MysteryGiftLink * link, u32 ident, const void * src, u32 size)
{
link->state = 0;
link->sendIdent = ident;
link->sendCounter = 0;
link->sendCRC = 0;
if (size != 0)
link->sendSize = size;
else
link->sendSize = MG_LINK_BUFFER_SIZE;
link->sendBuffer = src;
}
void MysteryGiftLink_InitRecv(struct MysteryGiftLink * link, u32 ident, void * dest)
{
link->state = 0;
link->recvIdent = ident;
link->recvCounter = 0;
link->recvCRC = 0;
link->recvSize = 0;
link->recvBuffer = dest;
}
static void MGL_ReceiveBlock(u32 playerId, void * dest, size_t size)
{
memcpy(dest, gBlockRecvBuffer[playerId], size);
}
static bool32 MGL_HasReceived(u32 playerId)
{
if ((GetBlockReceivedStatus() >> playerId) & 1)
return TRUE;
else
return FALSE;
}
static void MGL_ResetReceived(u32 playerId)
{
ResetBlockReceivedFlag(playerId);
}
static bool32 MGL_Receive(struct MysteryGiftLink * link)
{
struct SendRecvHeader header;
switch (link->state)
{
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)
{
LinkRfu_FatalError();
return FALSE;
}
else if (link->recvIdent != header.ident)
{
LinkRfu_FatalError();
return FALSE;
}
else
{
link->recvCounter = 0;
MGL_ResetReceived(link->recvPlayerId);
link->state++;
}
}
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 MGL_Send(struct MysteryGiftLink * link)
{
struct SendRecvHeader header;
switch (link->state)
{
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))
{
size_t blocksize;
MGL_ResetReceived(link->sendPlayerId);
blocksize = 252 * link->sendCounter;
if (link->sendSize - blocksize <= 252)
{
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(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;
}