forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmystery_gift_client.c
302 lines (278 loc) · 8.78 KB
/
mystery_gift_client.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "global.h"
#include "malloc.h"
#include "decompress.h"
#include "overworld.h"
#include "script.h"
#include "battle_tower.h"
#include "mystery_gift.h"
#include "mystery_event_script.h"
#include "mystery_gift_client.h"
enum {
FUNC_INIT,
FUNC_DONE,
FUNC_RECV,
FUNC_SEND,
FUNC_RUN,
FUNC_WAIT,
FUNC_RUN_MEVENT,
FUNC_RUN_BUFFER,
};
EWRAM_DATA static struct MysteryGiftClient * sClient = NULL;
static void MysteryGiftClient_Init(struct MysteryGiftClient *, u32, u32);
static u32 MysteryGiftClient_CallFunc(struct MysteryGiftClient *);
static void MysteryGiftClient_Free(struct MysteryGiftClient *);
extern const struct MysteryGiftClientCmd gMysteryGiftClientScript_Init[];
void MysteryGiftClient_Create(bool32 isWonderNews)
{
sClient = AllocZeroed(sizeof(*sClient));
MysteryGiftClient_Init(sClient, 1, 0);
sClient->isWonderNews = isWonderNews;
}
u32 MysteryGiftClient_Run(u16 * endVal)
{
u32 result;
if (sClient == NULL)
return CLI_RET_END;
result = MysteryGiftClient_CallFunc(sClient);
if (result == CLI_RET_END)
{
*endVal = sClient->param;
MysteryGiftClient_Free(sClient);
FREE_AND_SET_NULL(sClient);
}
return result;
}
void MysteryGiftClient_AdvanceState(void)
{
sClient->funcState++;
}
void * MysteryGiftClient_GetMsg(void)
{
return sClient->msg;
}
void MysteryGiftClient_SetParam(u32 val)
{
sClient->param = val;
}
static void MysteryGiftClient_Init(struct MysteryGiftClient * client, u32 sendPlayerId, u32 recvPlayerId)
{
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 MysteryGiftClient_Free(struct MysteryGiftClient * client)
{
Free(client->sendBuffer);
Free(client->recvBuffer);
Free(client->script);
Free(client->msg);
}
static void MysteryGiftClient_CopyRecvScript(struct MysteryGiftClient * client)
{
memcpy(client->script, client->recvBuffer, MG_LINK_BUFFER_SIZE);
client->cmdidx = 0;
}
static void MysteryGiftClient_InitSendWord(struct MysteryGiftClient * client, u32 ident, u32 word)
{
CpuFill32(0, client->sendBuffer, MG_LINK_BUFFER_SIZE);
*(u32 *)client->sendBuffer = word;
MysteryGiftLink_InitSend(&client->link, ident, client->sendBuffer, sizeof(word));
}
static u32 Client_Init(struct MysteryGiftClient * client)
{
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_Done(struct MysteryGiftClient * client)
{
return CLI_RET_END;
}
static u32 Client_Recv(struct MysteryGiftClient * client)
{
if (MysteryGiftLink_Recv(&client->link))
{
client->funcId = FUNC_RUN;
client->funcState = 0;
}
return CLI_RET_ACTIVE;
}
static u32 Client_Send(struct MysteryGiftClient * client)
{
if (MysteryGiftLink_Send(&client->link))
{
client->funcId = FUNC_RUN;
client->funcState = 0;
}
return CLI_RET_ACTIVE;
}
static u32 Client_Run(struct MysteryGiftClient * client)
{
// process command
struct MysteryGiftClientCmd * cmd = &client->script[client->cmdidx];
client->cmdidx++;
switch (cmd->instr)
{
case CLI_NONE:
break;
case CLI_RETURN:
client->param = cmd->parameter; // Set for endVal in MysteryGiftClient_Run
client->funcId = FUNC_DONE;
client->funcState = 0;
break;
case CLI_RECV:
MysteryGiftLink_InitRecv(&client->link, cmd->parameter, client->recvBuffer);
client->funcId = FUNC_RECV;
client->funcState = 0;
break;
case CLI_SEND_LOADED:
// Send without a MysteryGiftLink_InitSend
// Sends whatever has been loaded already
client->funcId = FUNC_SEND;
client->funcState = 0;
break;
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 CLI_SEND_STAT:
MysteryGiftClient_InitSendWord(client, MG_LINKID_GAME_STAT, GetGameStat(cmd->parameter));
client->funcId = FUNC_SEND;
client->funcState = 0;
break;
case CLI_COPY_RECV_IF_N:
if (client->param == FALSE)
MysteryGiftClient_CopyRecvScript(client);
break;
case CLI_COPY_RECV_IF:
if (client->param == TRUE)
MysteryGiftClient_CopyRecvScript(client);
break;
case CLI_COPY_RECV:
MysteryGiftClient_CopyRecvScript(client);
break;
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, client->isWonderNews);
MysteryGiftLink_InitSend(&client->link, MG_LINKID_GAME_DATA, client->sendBuffer, sizeof(struct MysteryGiftLinkGameData));
break;
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 CLI_SAVE_CARD:
SaveWonderCard(client->recvBuffer);
break;
case CLI_SAVE_NEWS:
if (!IsWonderNewsSameAsSaved(client->recvBuffer))
{
SaveWonderNews(client->recvBuffer);
MysteryGiftClient_InitSendWord(client, MG_LINKID_RESPONSE, FALSE);
}
else
{
// 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 CLI_RUN_MEVENT_SCRIPT:
client->funcId = FUNC_RUN_MEVENT;
client->funcState = 0;
break;
case CLI_SAVE_STAMP:
MysteryGift_TrySaveStamp(client->recvBuffer);
break;
case CLI_SAVE_RAM_SCRIPT:
InitRamScript_NoObjectEvent(client->recvBuffer, sizeof(struct RamScriptData));
break;
case CLI_RECV_EREADER_TRAINER:
memcpy(&gSaveBlock2Ptr->frontier.ereaderTrainer, client->recvBuffer, sizeof(gSaveBlock2Ptr->frontier.ereaderTrainer));
ValidateEReaderTrainer();
break;
case CLI_RUN_BUFFER_SCRIPT:
memcpy(gDecompressionBuffer, client->recvBuffer, MG_LINK_BUFFER_SIZE);
client->funcId = FUNC_RUN_BUFFER;
client->funcState = 0;
break;
}
return CLI_RET_ACTIVE;
}
static u32 Client_Wait(struct MysteryGiftClient * client)
{
if (client->funcState)
{
client->funcId = FUNC_RUN;
client->funcState = 0;
}
return CLI_RET_ACTIVE;
}
static u32 Client_RunMysteryEventScript(struct MysteryGiftClient * client)
{
switch (client->funcState)
{
case 0:
InitMysteryEventScriptContext(client->recvBuffer);
client->funcState++;
break;
case 1:
if (!RunMysteryEventScriptContextCommand(&client->param))
{
client->funcId = FUNC_RUN;
client->funcState = 0;
}
break;
}
return CLI_RET_ACTIVE;
}
static u32 Client_RunBufferScript(struct MysteryGiftClient * client)
{
// exec arbitrary code
u32 (*func)(u32 *, struct SaveBlock2 *, struct SaveBlock1 *) = (void *)gDecompressionBuffer;
if (func(&client->param, gSaveBlock2Ptr, gSaveBlock1Ptr) == 1)
{
client->funcId = FUNC_RUN;
client->funcState = 0;
}
return CLI_RET_ACTIVE;
}
static u32 MysteryGiftClient_CallFunc(struct MysteryGiftClient * client)
{
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[client->funcId](client);
}