diff options
author | PikalaxALT <pikalaxalt@gmail.com> | 2019-04-02 12:22:39 -0400 |
---|---|---|
committer | huderlem <huderlem@gmail.com> | 2019-04-03 17:04:42 -0500 |
commit | 6c12d154e7a6a0bc60c988497b3daa4f8126a5b2 (patch) | |
tree | 89acffeaf3f31bc4444cdef8a2a073ee0d5a5b1a /src | |
parent | 03badd9c40de5d50562376d39348fb5c85f8340b (diff) |
Reformat mevent server and client scripts
Diffstat (limited to 'src')
-rw-r--r-- | src/mevent_server_helpers.c | 210 | ||||
-rw-r--r-- | src/mevent_server_ish.c | 4 | ||||
-rw-r--r-- | src/util.c | 4 |
3 files changed, 213 insertions, 5 deletions
diff --git a/src/mevent_server_helpers.c b/src/mevent_server_helpers.c index dc4f98a6e..af14b514c 100644 --- a/src/mevent_server_helpers.c +++ b/src/mevent_server_helpers.c @@ -1 +1,211 @@ #include "global.h" +#include "alloc.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 "mevent.h" +#include "mevent_server_helpers.h" + +static u32 mevent_receive_func(struct mevent_srv_sub *); +static u32 mevent_send_func(struct mevent_srv_sub *); + +u32 mevent_srv_sub_recv(struct mevent_srv_sub * svr) +{ + return svr->recvFunc(svr); +} + +u32 mevent_srv_sub_send(struct mevent_srv_sub * svr) +{ + return svr->sendFunc(svr); +} + +void mevent_srv_sub_init(struct mevent_srv_sub * svr, u32 sendPlayerNo, u32 recvPlayerNo) +{ + svr->sendPlayerNo = sendPlayerNo; + svr->recvPlayerNo = recvPlayerNo; + svr->seqno = 0; + svr->sendCRC = 0; + svr->sendSize = 0; + svr->sendCounter = 0; + svr->recvCRC = 0; + svr->recvSize = 0; + svr->recvCounter = 0; + svr->sendBfr = NULL; + svr->recvBfr = NULL; + svr->sendFunc = mevent_send_func; + svr->recvFunc = mevent_receive_func; +} + +void mevent_srv_sub_init_send(struct mevent_srv_sub * svr, u32 ident, const void * src, u32 size) +{ + svr->seqno = 0; + svr->sendIdent = ident; + svr->sendCounter = 0; + svr->sendCRC = 0; + if (size != 0) + svr->sendSize = size; + else + svr->sendSize = ME_SEND_BUF_SIZE; + svr->sendBfr = src; +} + +void mevent_srv_sub_init_recv(struct mevent_srv_sub * svr, u32 ident, void * dest) +{ + svr->seqno = 0; + svr->recvIdent = ident; + svr->recvCounter = 0; + svr->recvCRC = 0; + svr->recvSize = 0; + svr->recvBfr = dest; +} + +static void mevent_recv_block(u32 recv_idx, void * dest, size_t size) +{ + memcpy(dest, gBlockRecvBuffer[recv_idx], size); +} + +static bool32 mevent_has_received(u32 recv_idx) +{ + if ((GetBlockReceivedStatus() >> recv_idx) & 1) + return TRUE; + else + return FALSE; +} + +static void mevent_reset_recv(u32 recv_idx) +{ + ResetBlockReceivedFlag(recv_idx); +} + +static bool32 mevent_receive_func(struct mevent_srv_sub * svr) +{ + struct send_recv_header header; + + switch (svr->seqno) + { + case 0: + if (mevent_has_received(svr->recvPlayerNo)) + { + mevent_recv_block(svr->recvPlayerNo, &header, sizeof(header)); + svr->recvSize = header.size; + svr->recvCRC = header.crc; + if (svr->recvSize > ME_SEND_BUF_SIZE) + { + sub_8010198(); + return FALSE; + } + else if (svr->recvIdent != header.ident) + { + sub_8010198(); + return FALSE; + } + else + { + svr->recvCounter = 0; + mevent_reset_recv(svr->recvPlayerNo); + ++svr->seqno; + } + } + break; + case 1: + if (mevent_has_received(svr->recvPlayerNo)) + { + size_t blocksiz = svr->recvCounter * 252; + if (svr->recvSize - blocksiz <= 252) + { + mevent_recv_block(svr->recvPlayerNo, svr->recvBfr + blocksiz, svr->recvSize - blocksiz); + ++svr->recvCounter; + ++svr->seqno; + } + else + { + mevent_recv_block(svr->recvPlayerNo, svr->recvBfr + blocksiz, 252); + ++svr->recvCounter; + } + mevent_reset_recv(svr->recvPlayerNo); + } + break; + case 2: + if (CalcCRC16WithTable(svr->recvBfr, svr->recvSize) != svr->recvCRC) + { + sub_8010198(); + return FALSE; + } + else + { + svr->seqno = 0; + return TRUE; + } + break; + + } + + return FALSE; +} + +static bool32 mevent_send_func(struct mevent_srv_sub * svr) +{ + struct send_recv_header header; + + switch (svr->seqno) + { + case 0: + if (IsLinkTaskFinished()) + { + header.ident = svr->sendIdent; + header.size = svr->sendSize; + header.crc = CalcCRC16WithTable(svr->sendBfr, svr->sendSize); + svr->sendCRC = header.crc; + svr->sendCounter = 0; + SendBlock(0, &header, sizeof(header)); + ++svr->seqno; + } + break; + case 1: + if (IsLinkTaskFinished()) + { + if (mevent_has_received(svr->sendPlayerNo)) + { + size_t blocksiz; + mevent_reset_recv(svr->sendPlayerNo); + blocksiz = 252 * svr->sendCounter; + if (svr->sendSize - blocksiz <= 252) + { + SendBlock(0, svr->sendBfr + blocksiz, svr->sendSize - blocksiz); + ++svr->sendCounter; + ++svr->seqno; + } + else + { + SendBlock(0, svr->sendBfr + blocksiz, 252); + ++svr->sendCounter; + } + } + } + break; + case 2: + if (IsLinkTaskFinished()) + { + if (CalcCRC16WithTable(svr->sendBfr, svr->sendSize) != svr->sendCRC) + sub_8010198(); + else + ++svr->seqno; + } + break; + case 3: + if (mevent_has_received(svr->sendPlayerNo)) + { + mevent_reset_recv(svr->sendPlayerNo); + svr->seqno = 0; + return TRUE; + } + break; + } + + return FALSE; +} diff --git a/src/mevent_server_ish.c b/src/mevent_server_ish.c index 2b310c981..0303bfd9b 100644 --- a/src/mevent_server_ish.c +++ b/src/mevent_server_ish.c @@ -14,9 +14,7 @@ static void mevent_srv_ish_init(struct mevent_srv_ish *, u32, u32); static u32 mevent_srv_ish_exec(struct mevent_srv_ish *); static void mevent_srv_ish_free_resources(struct mevent_srv_ish *); -extern const u8 gUnknown_082F2598[]; -extern const struct mevent_cmd gUnknown_8468B6C[]; -extern const struct mevent_cmd gUnknown_8468BCC[]; +extern const struct mevent_cmd_ish gUnknown_082F2598[]; void mevent_srv_ish_do_init(u32 arg) { diff --git a/src/util.c b/src/util.c index 47112774a..8aa54857a 100644 --- a/src/util.c +++ b/src/util.c @@ -218,7 +218,7 @@ int CountTrailingZeroBits(u32 value) return 0; } -u16 CalcCRC16(u8 *data, s32 length) +u16 CalcCRC16(const u8 *data, s32 length) { u16 i, j; u16 crc = 0x1121; @@ -237,7 +237,7 @@ u16 CalcCRC16(u8 *data, s32 length) return ~crc; } -u16 CalcCRC16WithTable(u8 *data, u32 length) +u16 CalcCRC16WithTable(const u8 *data, u32 length) { u16 i; u16 crc = 0x1121; |