SQCSimulator2023/SQCSim-common/netprotocol.cpp

1400 lines
39 KiB
C++
Raw Normal View History

2023-09-25 16:30:03 -04:00
#include "netprotocol.h"
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::INPUT;
2023-09-25 16:30:03 -04:00
2023-09-30 10:27:05 -04:00
uint64_t time = in->timestamp;
uint8_t time8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF),
2023-10-27 09:28:34 -04:00
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8) & 0xFF),
(uint8_t)(time & 0xFF) };
2023-09-30 10:27:05 -04:00
memcpy(*buf + 1, time8, sizeof(uint64_t));
uint64_t sid = in->sid;
uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF),
2023-10-27 09:28:34 -04:00
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF) };
2023-09-30 10:27:05 -04:00
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
Keys keys = in->keys;
uint8_t keys8 = // Reste un bit.
2023-12-08 04:31:10 -05:00
(keys.forward ? 0b10000000 : 0) |
(keys.backward ? 0b01000000 : 0) |
(keys.left ? 0b00100000 : 0) |
(keys.right ? 0b00010000 : 0) |
(keys.jump ? 0b00001000 : 0) |
(keys.shoot ? 0b00000100 : 0) |
(keys.block ? 0b00000010 : 0);
2023-09-30 10:27:05 -04:00
2023-10-24 09:17:21 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t));
2023-09-30 10:27:05 -04:00
uint32_t vec[3];
2023-10-27 09:28:34 -04:00
memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour d<>naturer les floats.
2023-09-30 10:27:05 -04:00
uint8_t vec8[3 * sizeof(uint32_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
2023-09-30 10:27:05 -04:00
2023-10-24 09:17:21 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3);
2023-09-30 10:27:05 -04:00
2023-10-24 09:17:21 -04:00
*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::OUTPUT;
2023-10-24 10:43:30 -04:00
2023-10-27 15:07:38 -04:00
uint64_t time = out->timestamp;
uint8_t time8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF),
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8) & 0xFF),
(uint8_t)(time & 0xFF) };
memcpy(*buf + 1, time8, sizeof(uint64_t));
2023-10-24 10:43:30 -04:00
2023-10-27 15:07:38 -04:00
uint64_t sid = out->id;
uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF) };
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
2023-10-31 15:28:10 -04:00
States states = out->states;
uint8_t states8 =
2023-12-08 04:31:10 -05:00
(states.jumping ? 0b10000000 : 0) |
(states.shooting ? 0b01000000 : 0) |
(states.hit ? 0b00100000 : 0) |
(states.powerup ? 0b00010000 : 0) |
(states.dead ? 0b00001000 : 0) |
(states.still ? 0b00000100 : 0) |
(states.jumpshot ? 0b00000010 : 0) |
(states.running ? 0b00000001 : 0);
2023-10-31 15:28:10 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + 1, &states8, sizeof(uint8_t));
uint32_t vec[3];
memcpy(vec, &out->direction, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3);
memcpy(vec, &out->position, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec82[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3, vec82, sizeof(uint32_t) * 3);
*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 6;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::SYNC;
2023-10-27 15:07:38 -04:00
uint64_t time = sync->timestamp;
uint8_t stamp8[sizeof(uint64_t)] = { (uint8_t)((time >> 56) & 0xFF),
(uint8_t)((time >> 48) & 0xFF),
(uint8_t)((time >> 40) & 0xFF),
(uint8_t)((time >> 32) & 0xFF),
(uint8_t)((time >> 24) & 0xFF),
(uint8_t)((time >> 16) & 0xFF),
(uint8_t)((time >> 8) & 0xFF),
(uint8_t)(time & 0xFF) };
memcpy(*buf + 1, stamp8, sizeof(uint64_t));
2023-10-27 15:07:38 -04:00
uint64_t sid = sync->sid;
uint8_t sid8[sizeof(uint64_t)] = { (uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF) };
memcpy(*buf + sizeof(uint64_t) + 1, sid8, sizeof(uint64_t));
uint32_t timer = sync->timer;
uint8_t time8[sizeof(uint32_t)] = { (uint8_t)((timer >> 24) & 0xFF),
2023-10-27 15:07:38 -04:00
(uint8_t)((timer >> 16) & 0xFF),
(uint8_t)((timer >> 8) & 0xFF),
(uint8_t)(timer & 0xFF) };
2023-10-27 15:07:38 -04:00
memcpy(*buf + sizeof(uint64_t) * 2 + 1, time8, sizeof(uint32_t));
uint16_t ammo = sync->ammo;
uint8_t ammo8[sizeof(uint16_t)] = { (uint8_t)((ammo >> 8) & 0xFF),
(uint8_t)(ammo & 0xFF) };
memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + 1, ammo8, sizeof(uint16_t));
uint32_t vec[3];
memcpy(vec, &sync->position, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
2023-10-24 10:43:30 -04:00
2023-12-09 12:02:04 -05:00
memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 1, vec8, sizeof(uint32_t) * 3);
2023-10-24 10:43:30 -04:00
2023-12-06 14:31:05 -05:00
uint32_t hp;
memcpy(&hp, &sync->hp, sizeof(float));
2023-12-08 04:31:10 -05:00
uint8_t hp8[4] = {
2023-12-06 14:31:05 -05:00
(uint8_t)((hp >> 24) & 0xFF),
(uint8_t)((hp >> 16) & 0xFF),
(uint8_t)((hp >> 8) & 0xFF),
(uint8_t)(hp & 0xFF) };
2023-12-09 12:02:04 -05:00
memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 1, hp8, sizeof(float));
2023-12-06 14:31:05 -05:00
*buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + sizeof(float) + 1;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::TEAMINF;
2023-09-25 16:30:03 -04:00
2023-09-29 12:04:08 -04:00
size_t namesize = std::strlen(tinfo->name) + 1;
2023-12-09 12:02:04 -05:00
strcpy(*buf + 1, namesize, tinfo->name);
2023-09-29 12:04:08 -04:00
uint64_t tid = tinfo->id;
uint8_t tid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + namesize + 2, tid8, sizeof(uint64_t));
*buflen = namesize + sizeof(uint64_t) + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::LOGINF;
2023-09-25 16:30:03 -04:00
2023-09-27 15:44:56 -04:00
size_t namesize = std::strlen(linfo->name) + 1;
2023-12-09 12:02:04 -05:00
strcpy(*buf + 1, namesize, linfo->name);
2023-09-25 16:30:03 -04:00
uint64_t sid = linfo->sid;
uint8_t sid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((sid >> 56) & 0xFF),
(uint8_t)((sid >> 48) & 0xFF),
(uint8_t)((sid >> 40) & 0xFF),
(uint8_t)((sid >> 32) & 0xFF),
(uint8_t)((sid >> 24) & 0xFF),
(uint8_t)((sid >> 16) & 0xFF),
(uint8_t)((sid >> 8) & 0xFF),
(uint8_t)(sid & 0xFF)
2023-09-27 11:24:41 -04:00
};
2023-09-25 16:30:03 -04:00
memcpy(*buf + namesize + 2, sid8, sizeof(uint64_t));
uint64_t tid = linfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
};
2023-09-25 16:30:03 -04:00
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::PLAYINF;
2023-09-29 12:04:08 -04:00
size_t namesize = std::strlen(pinfo->name) + 1;
2023-12-09 12:02:04 -05:00
strcpy(*buf + 1, namesize, pinfo->name);
2023-09-29 12:04:08 -04:00
uint64_t id = pinfo->id;
uint8_t id8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((id >> 56) & 0xFF),
(uint8_t)((id >> 48) & 0xFF),
(uint8_t)((id >> 40) & 0xFF),
(uint8_t)((id >> 32) & 0xFF),
(uint8_t)((id >> 24) & 0xFF),
(uint8_t)((id >> 16) & 0xFF),
(uint8_t)((id >> 8) & 0xFF),
(uint8_t)(id & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + namesize + 2, id8, sizeof(uint64_t));
uint64_t tid = pinfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
2023-09-29 12:04:08 -04:00
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::GAMEINFO;
uint64_t game = ginfo->seed;
uint8_t seed8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((game >> 56) & 0xFF),
(uint8_t)((game >> 48) & 0xFF),
(uint8_t)((game >> 40) & 0xFF),
(uint8_t)((game >> 32) & 0xFF),
(uint8_t)((game >> 24) & 0xFF),
(uint8_t)((game >> 16) & 0xFF),
(uint8_t)((game >> 8) & 0xFF),
(uint8_t)(game & 0xFF)
};
memcpy(*buf + 1, seed8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t count8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((game >> 56) & 0xFF),
(uint8_t)((game >> 48) & 0xFF),
(uint8_t)((game >> 40) & 0xFF),
(uint8_t)((game >> 32) & 0xFF),
(uint8_t)((game >> 24) & 0xFF),
(uint8_t)((game >> 16) & 0xFF),
(uint8_t)((game >> 8) & 0xFF),
(uint8_t)(game & 0xFF)
};
memcpy(*buf + sizeof(uint64_t) + 1, count8, sizeof(uint64_t));
game = ginfo->countdown;
uint8_t gtype8[sizeof(uint64_t)] = {
(uint8_t)((game >> 56) & 0xFF),
2023-10-27 09:28:34 -04:00
(uint8_t)((game >> 48) & 0xFF),
(uint8_t)((game >> 40) & 0xFF),
(uint8_t)((game >> 32) & 0xFF),
(uint8_t)((game >> 24) & 0xFF),
(uint8_t)((game >> 16) & 0xFF),
(uint8_t)((game >> 8) & 0xFF),
(uint8_t)(game & 0xFF)
};
memcpy(*buf + sizeof(uint64_t) + 1, gtype8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
*buflen = sizeof(uint64_t) * 3 + 1;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::CHAT;
2023-09-29 12:04:08 -04:00
uint64_t src = chat->src_id;
uint8_t src8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((src >> 56) & 0xFF),
(uint8_t)((src >> 48) & 0xFF),
(uint8_t)((src >> 40) & 0xFF),
(uint8_t)((src >> 32) & 0xFF),
(uint8_t)((src >> 24) & 0xFF),
(uint8_t)((src >> 16) & 0xFF),
(uint8_t)((src >> 8) & 0xFF),
(uint8_t)(src & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + 1, src8, sizeof(uint64_t));
uint64_t dst = chat->dest_id;
uint8_t dst8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((dst >> 56) & 0xFF),
(uint8_t)((dst >> 48) & 0xFF),
(uint8_t)((dst >> 40) & 0xFF),
(uint8_t)((dst >> 32) & 0xFF),
(uint8_t)((dst >> 24) & 0xFF),
(uint8_t)((dst >> 16) & 0xFF),
(uint8_t)((dst >> 8) & 0xFF),
(uint8_t)(dst & 0xFF)
2023-09-29 12:04:08 -04:00
};
memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t));
uint64_t dstteam = chat->dest_id;
uint8_t dstt8[sizeof(uint64_t)] = {
2023-10-27 09:28:34 -04:00
(uint8_t)((dstteam >> 56) & 0xFF),
(uint8_t)((dstteam >> 48) & 0xFF),
(uint8_t)((dstteam >> 40) & 0xFF),
(uint8_t)((dstteam >> 32) & 0xFF),
(uint8_t)((dstteam >> 24) & 0xFF),
(uint8_t)((dstteam >> 16) & 0xFF),
(uint8_t)((dstteam >> 8) & 0xFF),
(uint8_t)(dstteam & 0xFF)
2023-09-29 12:04:08 -04:00
};
2023-09-29 12:04:08 -04:00
memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t));
2023-09-25 16:30:03 -04:00
2023-09-29 12:04:08 -04:00
size_t messize = std::strlen(chat->mess) + 1;
2023-12-06 15:37:08 -05:00
strcpy(*buf + 1 + sizeof(uint64_t) * 3, messize, chat->mess);
2023-09-29 12:04:08 -04:00
*buflen = messize + sizeof(uint64_t) * 3 + 2;
2023-09-25 16:30:03 -04:00
}
void netprot::Serialize(ChunkMod* chmod, char* buf[], uint32_t* buflen) {
*buf[0] = (char)netprot::PACKET_TYPE::CHUNKMOD;
uint32_t vec[3];
memcpy(vec, &chmod->pos, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + 1, vec8, sizeof(uint32_t) * 3);
memcpy(*buf + sizeof(uint32_t) * 3 + 1, &chmod->b_type, sizeof(BlockType));
2023-12-02 11:05:00 -05:00
memcpy(*buf + sizeof(uint32_t) * 3 + 2, &chmod->old_b_type, sizeof(BlockType));
2023-12-02 11:05:00 -05:00
*buflen = sizeof(uint32_t) * 3 + 3;
}
2023-12-05 06:25:48 -05:00
void netprot::Serialize(BulletAdd* bull, char* buf[], uint32_t* buflen) {
*buf[0] = (char)netprot::PACKET_TYPE::BULLET;
uint64_t tstamp = bull->tstamp;
uint8_t ts8[sizeof(uint64_t)] = {
(uint8_t)((tstamp >> 56) & 0xFF),
(uint8_t)((tstamp >> 48) & 0xFF),
(uint8_t)((tstamp >> 40) & 0xFF),
(uint8_t)((tstamp >> 32) & 0xFF),
(uint8_t)((tstamp >> 24) & 0xFF),
(uint8_t)((tstamp >> 16) & 0xFF),
(uint8_t)((tstamp >> 8) & 0xFF),
(uint8_t)(tstamp & 0xFF)
};
memcpy(*buf + 1, ts8, sizeof(uint64_t));
2023-12-06 11:16:39 -05:00
uint64_t tid = bull->id;
2023-12-05 06:25:48 -05:00
uint8_t tid8[sizeof(uint64_t)] = {
(uint8_t)((tid >> 56) & 0xFF),
(uint8_t)((tid >> 48) & 0xFF),
(uint8_t)((tid >> 40) & 0xFF),
(uint8_t)((tid >> 32) & 0xFF),
(uint8_t)((tid >> 24) & 0xFF),
(uint8_t)((tid >> 16) & 0xFF),
(uint8_t)((tid >> 8) & 0xFF),
(uint8_t)(tid & 0xFF)
};
memcpy(*buf + 1 + sizeof(uint64_t), tid8, sizeof(uint64_t));
uint32_t vec[3];
memcpy(vec, &bull->pos, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + 1 + sizeof(uint64_t) * 2, vec8, sizeof(uint32_t) * 3);
memcpy(vec, &bull->dir, sizeof(Vector3f)); // Pour d<>naturer les floats.
uint8_t dir8[3 * sizeof(uint32_t)] = {
(uint8_t)((vec[0] >> 24) & 0xFF),
(uint8_t)((vec[0] >> 16) & 0xFF),
(uint8_t)((vec[0] >> 8) & 0xFF),
(uint8_t)(vec[0] & 0xFF),
(uint8_t)((vec[1] >> 24) & 0xFF),
(uint8_t)((vec[1] >> 16) & 0xFF),
(uint8_t)((vec[1] >> 8) & 0xFF),
(uint8_t)(vec[1] & 0xFF),
(uint8_t)((vec[2] >> 24) & 0xFF),
(uint8_t)((vec[2] >> 16) & 0xFF),
(uint8_t)((vec[2] >> 8) & 0xFF),
(uint8_t)(vec[2] & 0xFF) };
memcpy(*buf + 1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 3, dir8, sizeof(uint32_t) * 3);
*buflen = 1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 6;
}
2023-09-25 16:30:03 -04:00
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
2023-10-27 09:28:34 -04:00
*buf[0] = (char)netprot::PACKET_TYPE::ERRLOG;
2023-09-29 12:04:08 -04:00
size_t messize = std::strlen(errlog->mess) + 1;
memcpy(*buf + 1, &errlog->mess, messize);
memcpy(*buf + 1 + messize, &errlog->is_fatal, sizeof(bool));
*buflen = messize + sizeof(bool) + 1;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(Input* in, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Input))
2023-10-24 07:32:16 -04:00
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
in->timestamp =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
2023-12-08 04:31:10 -05:00
2023-10-24 07:32:16 -04:00
memcpy(diff, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
in->sid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
uint8_t keys = 0;
2023-10-24 09:17:21 -04:00
memcpy(&keys, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t));
in->keys.forward = keys & 0b10000000;
in->keys.backward = keys & 0b01000000;
in->keys.left = keys & 0b00100000;
in->keys.right = keys & 0b00010000;
in->keys.jump = keys & 0b00001000;
in->keys.shoot = keys & 0b00000100;
in->keys.block = keys & 0b00000010;
2023-10-24 07:32:16 -04:00
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
2023-10-24 09:17:21 -04:00
memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12);
2023-10-24 07:32:16 -04:00
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
2023-10-24 07:32:16 -04:00
memcpy(&in->direction, vec, sizeof(uint32_t) * 3);
*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3;
2023-10-24 07:32:16 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(Output* out, char* buf, uint32_t* buflen) {
2023-10-31 15:28:10 -04:00
if (*buflen <= sizeof(Output))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
out->timestamp =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
out->id =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
uint8_t states = 0;
memcpy(&states, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t));
out->states.jumping = states & 0b10000000;
out->states.shooting = states & 0b01000000;
out->states.hit = states & 0b00100000;
2023-12-06 16:49:26 -05:00
out->states.powerup = states & 0b00010000;
out->states.dead = states & 0b00001000;
out->states.still = states & 0b00000100;
out->states.jumpshot = states & 0b00000010;
out->states.running = states & 0b00000001;
2023-10-31 15:28:10 -04:00
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12);
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&out->direction, vec, sizeof(uint32_t) * 3);
memcpy(subvec, &buf[2 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 3], sizeof(uint8_t) * 12);
uint32_t vec2[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&out->position, vec2, sizeof(uint32_t) * 3);
*buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 6;
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(Sync* sync, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Sync))
2023-10-27 15:07:38 -04:00
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
sync->timestamp =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
sync->sid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint32_t));
sync->timer =
(uint32_t)diff[0] << 24 |
(uint32_t)diff[1] << 16 |
(uint32_t)diff[2] << 8 |
(uint32_t)diff[3];
memcpy(diff, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t)], sizeof(uint16_t));
sync->ammo =
(uint16_t)diff[0] << 8 |
(uint16_t)diff[1];
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
2023-12-09 12:02:04 -05:00
memcpy(subvec, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t)], sizeof(uint8_t) * 12);
2023-10-27 15:07:38 -04:00
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&sync->position, vec, sizeof(uint32_t) * 3);
2023-12-06 14:31:05 -05:00
uint8_t hp8[4];
2023-12-09 12:02:04 -05:00
memcpy(&hp8, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t)], sizeof(uint32_t));
2023-12-06 14:31:05 -05:00
uint32_t hp = (uint32_t)hp8[0] << 24 |
2023-12-08 04:31:10 -05:00
(uint32_t)hp8[1] << 16 |
(uint32_t)hp8[2] << 8 |
(uint32_t)hp8[3];
2023-12-06 14:31:05 -05:00
memcpy(&sync->hp, &hp, sizeof(float));
2023-12-09 12:02:04 -05:00
*buflen = 1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + sizeof(float);
2023-10-27 15:07:38 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(LoginInfo))
2023-09-29 12:04:08 -04:00
return false;
size_t namesize = std::strlen(buf) + 1;
if (namesize > 32)
return false;
2023-12-09 12:02:04 -05:00
strcpy(tinfo->name, namesize, &buf[1]);
2023-09-29 12:04:08 -04:00
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
tinfo->id =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = namesize + sizeof(uint64_t) + 2;
2023-09-29 12:04:08 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(LoginInfo* linfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(LoginInfo))
2023-09-25 16:30:03 -04:00
return false;
2023-09-27 15:44:56 -04:00
size_t namesize = std::strlen(buf) + 1;
2023-09-25 16:30:03 -04:00
2023-09-27 15:44:56 -04:00
if (namesize > 32)
return false;
2023-09-25 16:30:03 -04:00
2023-12-09 12:02:04 -05:00
strcpy(linfo->name, namesize, &buf[1]);
2023-09-25 16:30:03 -04:00
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
2023-09-27 11:24:41 -04:00
memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
linfo->sid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
2023-09-25 16:30:03 -04:00
memcpy(diff, &buf[namesize + sizeof(uint64_t) + 1], sizeof(uint64_t));
linfo->tid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
2023-09-25 16:30:03 -04:00
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(PlayerInfo* pinfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(PlayerInfo))
2023-09-29 12:04:08 -04:00
return false;
size_t namesize = std::strlen(buf) + 1;
if (namesize > 32)
return false;
2023-12-09 12:02:04 -05:00
strcpy(pinfo->name, namesize, &buf[1]);
2023-09-29 12:04:08 -04:00
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[namesize + 1], sizeof(uint64_t));
pinfo->id =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[namesize + sizeof(uint64_t) + 1], sizeof(uint64_t));
pinfo->tid =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = namesize + sizeof(uint64_t) * 2 + 2;
2023-09-29 12:04:08 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(GameInfo* ginfo, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(GameInfo))
return false;
uint8_t diff[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(diff, &buf[1], sizeof(uint64_t));
ginfo->seed =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[sizeof(uint64_t) + 1], sizeof(uint64_t));
ginfo->countdown =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
memcpy(diff, &buf[sizeof(uint64_t) * 2 + 1], sizeof(uint64_t));
ginfo->gameType =
(uint64_t)diff[0] << 56 |
(uint64_t)diff[1] << 48 |
(uint64_t)diff[2] << 40 |
(uint64_t)diff[3] << 32 |
(uint64_t)diff[4] << 24 |
(uint64_t)diff[5] << 16 |
(uint64_t)diff[6] << 8 |
(uint64_t)diff[7];
*buflen = sizeof(uint64_t) * 3 + 1;
return true;
2023-09-25 16:30:03 -04:00
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(Chat* chat, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(Chat))
2023-09-29 12:04:08 -04:00
return false;
uint8_t src[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(src, &buf[1], sizeof(uint64_t));
chat->src_id =
(uint64_t)src[0] << 56 |
(uint64_t)src[1] << 48 |
(uint64_t)src[2] << 40 |
(uint64_t)src[3] << 32 |
(uint64_t)src[4] << 24 |
(uint64_t)src[5] << 16 |
(uint64_t)src[6] << 8 |
(uint64_t)src[7];
uint8_t dst[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(dst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
chat->dest_id =
(uint64_t)dst[0] << 56 |
(uint64_t)dst[1] << 48 |
(uint64_t)dst[2] << 40 |
(uint64_t)dst[3] << 32 |
(uint64_t)dst[4] << 24 |
(uint64_t)dst[5] << 16 |
(uint64_t)dst[6] << 8 |
(uint64_t)dst[7];
uint8_t dstt[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(dstt, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint64_t));
chat->dest_team_id =
(uint64_t)dstt[0] << 56 |
(uint64_t)dstt[1] << 48 |
(uint64_t)dstt[2] << 40 |
(uint64_t)dstt[3] << 32 |
(uint64_t)dstt[4] << 24 |
(uint64_t)dstt[5] << 16 |
(uint64_t)dstt[6] << 8 |
(uint64_t)dstt[7];
2023-12-06 16:20:59 -05:00
size_t messsize = std::strlen(&buf[sizeof(uint64_t) * 3]) + 1;
2023-09-29 12:04:08 -04:00
if (messsize > 140)
return false;
2023-12-06 16:36:21 -05:00
char* ciboire = &buf[1 + sizeof(uint64_t) * 3];
strcpy(chat->mess, 140, ciboire);
2023-12-06 16:20:59 -05:00
//*buflen = messsize + sizeof(uint64_t) * 3 + 1;
2023-12-08 04:31:10 -05:00
2023-09-29 12:04:08 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
bool netprot::Deserialize(ChunkMod* chmod, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(ChunkMod))
return false;
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
memcpy(subvec, &buf[1], sizeof(uint8_t) * 12);
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&chmod->pos, vec, sizeof(uint32_t) * 3);
memcpy(&chmod->b_type, &buf[1 + sizeof(uint8_t) * 12], sizeof(BlockType));
2023-12-02 11:05:00 -05:00
memcpy(&chmod->old_b_type, &buf[2 + sizeof(uint8_t) * 12], sizeof(BlockType));
2023-12-02 11:05:00 -05:00
*buflen = sizeof(uint32_t) * 3 + 3;
return true;
}
2023-12-05 06:25:48 -05:00
bool netprot::Deserialize(BulletAdd* bull, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(BulletAdd))
return false;
uint8_t tst[sizeof(uint64_t)] = { 0,0,0,0,0,0,0,0 };
memcpy(tst, &buf[1], sizeof(uint64_t));
bull->tstamp =
(uint64_t)tst[0] << 56 |
(uint64_t)tst[1] << 48 |
(uint64_t)tst[2] << 40 |
(uint64_t)tst[3] << 32 |
(uint64_t)tst[4] << 24 |
(uint64_t)tst[5] << 16 |
(uint64_t)tst[6] << 8 |
(uint64_t)tst[7];
memcpy(tst, &buf[1 + sizeof(uint64_t)], sizeof(uint64_t));
2023-12-06 11:16:39 -05:00
bull->id =
2023-12-05 06:25:48 -05:00
(uint64_t)tst[0] << 56 |
(uint64_t)tst[1] << 48 |
(uint64_t)tst[2] << 40 |
(uint64_t)tst[3] << 32 |
(uint64_t)tst[4] << 24 |
(uint64_t)tst[5] << 16 |
(uint64_t)tst[6] << 8 |
(uint64_t)tst[7];
uint8_t subvec[3 * sizeof(uint32_t)] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
memcpy(subvec, &buf[1 + sizeof(uint64_t) * 2], sizeof(uint8_t) * 12);
uint32_t vec[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&bull->pos, vec, sizeof(uint32_t) * 3);
memcpy(subvec, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint8_t) * 12], sizeof(uint8_t) * 12);
uint32_t dir[3] = {
(uint32_t)subvec[0] << 24 |
(uint32_t)subvec[1] << 16 |
(uint32_t)subvec[2] << 8 |
(uint32_t)subvec[3],
(uint32_t)subvec[4] << 24 |
(uint32_t)subvec[5] << 16 |
(uint32_t)subvec[6] << 8 |
(uint32_t)subvec[7],
(uint32_t)subvec[8] << 24 |
(uint32_t)subvec[9] << 16 |
(uint32_t)subvec[10] << 8 |
(uint32_t)subvec[11] };
memcpy(&bull->dir, dir, sizeof(uint32_t) * 3);
*buflen = 1 + sizeof(uint64_t) * 2 + sizeof(uint8_t) * 24;
return true;
}
2023-12-08 04:31:10 -05:00
bool netprot::Deserialize(ErrorLog* errlog, char* buf, uint32_t* buflen) {
if (*buflen <= sizeof(ErrorLog))
2023-09-29 12:04:08 -04:00
return false;
size_t messsize = std::strlen(buf) + 1;
if (messsize > 140)
return false;
memcpy(&errlog->mess, &buf[1], messsize);
memcpy(&errlog->is_fatal, &buf[1 + messsize], sizeof(bool));
*buflen = messsize + sizeof(uint64_t) * 3 + 2;
2023-09-29 12:04:08 -04:00
return true;
2023-09-25 16:30:03 -04:00
}
2023-10-31 15:30:07 -04:00
2023-10-27 09:28:34 -04:00
netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
if (buflen < 1 ||
buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK ||
2023-10-27 09:28:34 -04:00
buf[0] <= (char)netprot::PACKET_TYPE::ERR)
2023-09-25 16:30:03 -04:00
return netprot::PACKET_TYPE::ERR;
2023-10-27 09:28:34 -04:00
return (netprot::PACKET_TYPE)buf[0];
2023-09-25 16:30:03 -04:00
}
2023-10-24 10:43:30 -04:00
2023-12-08 04:31:10 -05:00
netprot::Packet netprot::getPack(char* buf, uint32_t* buflen) {
2023-10-24 10:43:30 -04:00
Packet pck = { nullptr, PACKET_TYPE::ERR };
Input* in = nullptr;
Output* out = nullptr;
Sync* sync = nullptr;
Chat* chat = nullptr;
GameInfo* ginfo = nullptr;
ErrorLog* errlog = nullptr;
LoginInfo* loginf = nullptr;
2023-10-24 10:43:30 -04:00
switch (getType(buf, *buflen)) {
2023-10-24 10:43:30 -04:00
case PACKET_TYPE::INPUT:
in = new Input();
if (netprot::Deserialize(in, buf, buflen)) {
pck.type = PACKET_TYPE::INPUT;
pck.ptr = (void*)in;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::OUTPUT:
out = new Output();
if (netprot::Deserialize(out, buf, buflen)) {
pck.type = PACKET_TYPE::OUTPUT;
pck.ptr = (void*)out;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::SYNC:
sync = new Sync();
if (netprot::Deserialize(sync, buf, buflen)) {
pck.type = PACKET_TYPE::SYNC;
pck.ptr = (void*)sync;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::CHAT:
chat = new Chat();
if (netprot::Deserialize(chat, buf, buflen)) {
pck.type = PACKET_TYPE::CHAT;
pck.ptr = (void*)chat;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::GAMEINFO:
ginfo = new GameInfo();
if (netprot::Deserialize(ginfo, buf, buflen)) {
pck.type = PACKET_TYPE::GAMEINFO;
pck.ptr = (void*)ginfo;
}
2023-10-24 10:43:30 -04:00
break;
case PACKET_TYPE::ERRLOG:
errlog = new ErrorLog();
if (netprot::Deserialize(errlog, buf, buflen)) {
pck.type = PACKET_TYPE::ERRLOG;
pck.ptr = (void*)errlog;
}
break;
case PACKET_TYPE::LOGINF:
loginf = new LoginInfo();
if (netprot::Deserialize(loginf, buf, buflen)) {
pck.type = PACKET_TYPE::LOGINF;
pck.ptr = (void*)loginf;
}
2023-10-24 10:43:30 -04:00
break;
default:
break;
}
return pck;
}
2023-12-05 06:25:48 -05:00
netprot::Packet netprot::getPack(netprot::Buffer* buf) { return netprot::getPack(buf->ptr, &buf->len); }
2023-10-27 09:28:34 -04:00
bool netprot::emptyPack(netprot::Packet pck) {
switch (pck.type) {
case PACKET_TYPE::INPUT:
delete (Input*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::OUTPUT:
delete (Output*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::SYNC:
delete (Sync*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::CHAT:
delete (Chat*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::GAMEINFO:
delete (GameInfo*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::ERRLOG:
delete (ErrorLog*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
case PACKET_TYPE::LOGINF:
delete (LoginInfo*)pck.ptr;
2023-10-27 12:02:29 -04:00
return true;
default:
return false;
}
}
2023-10-27 12:02:29 -04:00
2023-10-29 14:54:36 -04:00
netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
Packet pck;
pck.ptr = ptr;
pck.type = type;
return pck;
}
void netprot::recvPacks(SOCKET sock, Buffer* buf, std::vector<char*>* lsPck) {
2023-12-08 04:31:10 -05:00
int len = buf->tmp ? buf->tmp - buf->ptr : 0,
2023-11-01 16:40:33 -04:00
end = 0;
2023-12-08 04:31:10 -05:00
char* cursor = buf->tmp ? buf->tmp : nullptr,
* next = buf->tmp ? buf->tmp + 1 : buf->ptr,
* last = buf->tmp ? buf->tmp : buf->ptr;
2023-11-01 16:40:33 -04:00
bool ended = true;
2023-11-06 13:56:12 -05:00
struct pollfd fds[1];
fds[0].fd = sock;
fds[0].events = POLLIN;
2023-10-28 11:18:01 -04:00
while (true) {
2023-11-06 13:56:12 -05:00
if (!poll(fds, 1, 0)) {
if (ended)
buf->tmp = nullptr;
return;
2023-11-06 13:56:12 -05:00
}
2023-12-08 04:31:10 -05:00
int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
2023-11-01 16:40:33 -04:00
if (bytes <= 0) { // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité.
if (ended)
buf->tmp = nullptr;
return;
2023-12-08 04:31:10 -05:00
}
len += bytes;
end = len;
2023-11-01 16:40:33 -04:00
ended = false;
while (true) {
int cmp = 0;
if (cursor)
end -= (cursor - buf->ptr);
if (end < 0)
break;
cursor = (char*)memchr(next, '\r', end);
if (cursor) {
2023-11-06 13:56:12 -05:00
next = cursor;
2023-11-01 06:12:38 -04:00
cursor--;
2023-11-01 09:19:26 -04:00
cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) {
lsPck->push_back(last);
2023-12-08 04:31:10 -05:00
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
}
else {
buf->tmp = last;
cursor = &buf->ptr[len];
next = cursor + 1;
break;
};
}
}
}
void netprot::recvPacksFrom(SOCKET sock, Buffer* buf, sockaddr_in from, std::vector<char*>* lsPck) {
int len = buf->tmp ? buf->tmp - buf->ptr : 0,
end = 0;
char* cursor = buf->tmp ? buf->tmp : nullptr,
* next = buf->tmp ? buf->tmp + 1 : buf->ptr,
* last = buf->tmp ? buf->tmp : buf->ptr;
bool ended = true;
struct pollfd fds[1];
sockaddr_in sockad = from;
addrlen_t socklen = sizeof(sockad);
fds[0].fd = sock;
fds[0].events = POLLIN;
while (true) {
if (!poll(fds, 1, 0)) {
if (ended)
buf->tmp = nullptr;
return;
}
int bytes = recvfrom(sock, &buf->ptr[len], buf->len - len, 0, (sockaddr*)&sockad, &socklen);
if (bytes <= 0) { // si recv() retourne -1 ou 0; ça veut dire qu'il y a plus rien a lire qui n'a pas déjà été traité.
if (ended)
buf->tmp = nullptr;
return;
}
len += bytes;
end = len;
ended = false;
while (true) {
int cmp = 0;
if (cursor)
end -= (cursor - buf->ptr);
if (end < 0)
break;
cursor = (char*)memchr(next, '\r', end);
if (cursor) {
next = cursor;
cursor--;
cmp = memcmp(cursor, Footer, sizeof(uint32_t));
if (cmp == 0) {
lsPck->push_back(last);
2023-12-08 04:31:10 -05:00
cursor += sizeof(uint32_t);
last = cursor;
next = cursor + 1;
}
}
else {
2023-12-08 04:31:10 -05:00
buf->tmp = last;
cursor = &buf->ptr[len];
next = cursor + 1;
break;
};
2023-10-28 11:18:01 -04:00
}
}
}
2023-10-27 12:02:29 -04:00
template <>
2023-11-01 09:19:26 -04:00
void netprot::sendPack<netprot::Packet>(SOCKET sock, Packet* pack, Buffer* buf) {
2023-10-27 12:02:29 -04:00
switch (pack->type) {
case PACKET_TYPE::INPUT:
2023-11-01 09:19:26 -04:00
sendPack<Input>(sock, (Input*)pack->ptr, buf);
return;
case PACKET_TYPE::OUTPUT:
2023-11-01 09:19:26 -04:00
sendPack<Output>(sock, (Output*)pack->ptr, buf);
return;
case PACKET_TYPE::SYNC:
2023-11-01 09:19:26 -04:00
sendPack<Sync>(sock, (Sync*)pack->ptr, buf);
return;
case PACKET_TYPE::TEAMINF:
2023-11-01 09:19:26 -04:00
sendPack<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf);
return;
case PACKET_TYPE::PLAYINF:
2023-11-01 09:19:26 -04:00
sendPack<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf);
return;
case PACKET_TYPE::LOGINF:
2023-11-01 09:19:26 -04:00
sendPack<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf);
return;
case PACKET_TYPE::CHUNKMOD:
2023-11-01 09:19:26 -04:00
//sendPack<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf);
2023-11-01 12:18:27 -04:00
return;
case PACKET_TYPE::PLAYERMOD:
2023-11-01 09:19:26 -04:00
//sendPack<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf);
return;
case PACKET_TYPE::PICKUPMOD:
2023-11-01 09:19:26 -04:00
//sendPack<PickupMod>(sock, (PickupMod*)pack->ptr, buf);
return;
case PACKET_TYPE::GAMEINFO:
2023-11-01 09:19:26 -04:00
sendPack<GameInfo>(sock, (GameInfo*)pack->ptr, buf);
return;
case PACKET_TYPE::ENDINFO:
2023-11-01 09:19:26 -04:00
//sendPack<EndInfo>(sock, (EndInfo*)pack->ptr, buf);
return;
case PACKET_TYPE::CHAT:
2023-11-01 09:19:26 -04:00
sendPack<Chat>(sock, (Chat*)pack->ptr, buf);
return;
case PACKET_TYPE::ERRLOG:
2023-11-01 09:19:26 -04:00
sendPack<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf);
return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]];
default:
return;
}
2023-10-27 12:02:29 -04:00
}
template <>
2023-11-01 09:19:26 -04:00
void netprot::sendPackTo<netprot::Packet>(SOCKET sock, Packet* pack, Buffer* buf, sockaddr_in* sockad) {
2023-10-27 12:02:29 -04:00
switch (pack->type) {
case PACKET_TYPE::INPUT:
2023-11-01 09:19:26 -04:00
sendPackTo<Input>(sock, (Input*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::OUTPUT:
2023-11-01 09:19:26 -04:00
sendPackTo<Output>(sock, (Output*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::SYNC:
2023-11-01 09:19:26 -04:00
sendPackTo<Sync>(sock, (Sync*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::TEAMINF:
2023-11-01 09:19:26 -04:00
sendPackTo<TeamInfo>(sock, (TeamInfo*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::PLAYINF:
2023-11-01 09:19:26 -04:00
sendPackTo<PlayerInfo>(sock, (PlayerInfo*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::LOGINF:
2023-11-01 09:19:26 -04:00
sendPackTo<LoginInfo>(sock, (LoginInfo*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::CHUNKMOD:
2023-11-01 09:19:26 -04:00
//sendPackTo<ChunkMod>(sock, (ChunkMod*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::PLAYERMOD:
2023-11-01 09:19:26 -04:00
//sendPackTo<PlayerMod>(sock, (PlayerMod*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::PICKUPMOD:
2023-11-01 09:19:26 -04:00
//sendPackTo<PickupMod>(sock, (PickupMod*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::GAMEINFO:
2023-11-01 09:19:26 -04:00
sendPackTo<GameInfo>(sock, (GameInfo*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::ENDINFO:
2023-11-01 09:19:26 -04:00
//sendPackTo<EndInfo>(sock, (EndInfo*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::CHAT:
2023-11-01 09:19:26 -04:00
sendPackTo<Chat>(sock, (Chat*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::ERRLOG:
2023-11-01 09:19:26 -04:00
sendPackTo<ErrorLog>(sock, (ErrorLog*)pack->ptr, buf, sockad);
return;
case PACKET_TYPE::LAST_PACK: [[fallthrough]];
case PACKET_TYPE::ERR: [[fallthrough]];
default:
return;
}
}