diff --git a/.gitignore b/.gitignore
index f15149d..8ffde58 100644
--- a/.gitignore
+++ b/.gitignore
@@ -378,3 +378,5 @@ FodyWeavers.xsd
/SQCSim2021/cmake/*
!/SQCSim2021/cmake/CMakeLists.txt
/SQCSim2021/SQCSim-client
+SQCSim2021/SQCSim2021.vcxproj.filters
+SQCSim2021/SQCSim2021.vcxproj
diff --git a/SQCSim-common/SQCSim-common.vcxproj b/SQCSim-common/SQCSim-common.vcxproj
index 991cd1d..4cebfaf 100644
--- a/SQCSim-common/SQCSim-common.vcxproj
+++ b/SQCSim-common/SQCSim-common.vcxproj
@@ -76,6 +76,7 @@
true
WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
+ stdcpp20
Console
@@ -90,6 +91,7 @@
true
WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
+ stdcpp20
Console
@@ -104,6 +106,7 @@
true
_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
+ stdcpp20
Console
@@ -118,7 +121,7 @@
true
NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
true
- stdcpp17
+ stdcpp20
Console
diff --git a/SQCSim-common/define.h b/SQCSim-common/define.h
index f65bc2b..05d90da 100644
--- a/SQCSim-common/define.h
+++ b/SQCSim-common/define.h
@@ -4,8 +4,10 @@
#include
#include
-#define SRV_PORT 1025
-#define CLI_PORT 1026
+#define SRV_PORT 10000
+#define CLI_PORT 10001
+
+#define BUFFER_LENGTH 150
#define CHUNK_SIZE_X 4
#define CHUNK_SIZE_Y 64
@@ -29,6 +31,8 @@
#define TEXTURE_SIZE 512
#define MAX_BULLETS 512
+#define BULLET_TIME .1
+
typedef uint8_t BlockType;
enum BLOCK_TYPE { BTYPE_AIR, BTYPE_DIRT, BTYPE_GRASS, BTYPE_METAL, BTYPE_ICE, BTYPE_LAST };
typedef uint64_t Timestamp;
@@ -42,6 +46,11 @@ typedef uint64_t Timestamp;
#include
#include
+#define flag_t u_long
+#define addrlen_t int
+#define ioctl ioctlsocket
+#define SOCK_NONBLOCK FIONBIO
+#define strcpy strcpy_s
#define popen _popen
#define pclose _pclose
@@ -51,10 +60,13 @@ typedef uint64_t Timestamp;
#include
#include
#include
+#include
#include
#include
#include
+#define flag_t unsigned int
+#define addrlen_t unsigned int
#define SOCKET int
#define INVALID_SOCKET -1
#define closesocket close
diff --git a/SQCSim-common/netprotocol.cpp b/SQCSim-common/netprotocol.cpp
index d4edca2..d31d425 100644
--- a/SQCSim-common/netprotocol.cpp
+++ b/SQCSim-common/netprotocol.cpp
@@ -1,29 +1,29 @@
#include "netprotocol.h"
void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::INPUT;
+ *buf[0] = (char)netprot::PACKET_TYPE::INPUT;
uint64_t time = in->timestamp;
- uint8_t time8[sizeof(uint64_t)] = {(time >> 56) & 0xFF,
- (time >> 48) & 0xFF,
- (time >> 40) & 0xFF,
- (time >> 32) & 0xFF,
- (time >> 24) & 0xFF,
- (time >> 16) & 0xFF,
- (time >> 8 ) & 0xFF,
- time & 0xFF};
+ 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));
uint64_t sid = in->sid;
- uint8_t sid8[sizeof(uint64_t)] = {(sid >> 56) & 0xFF,
- (sid >> 48) & 0xFF,
- (sid >> 40) & 0xFF,
- (sid >> 32) & 0xFF,
- (sid >> 24) & 0xFF,
- (sid >> 16) & 0xFF,
- (sid >> 8 ) & 0xFF,
- sid & 0xFF};
+ 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));
@@ -37,54 +37,140 @@ void netprot::Serialize(Input* in, char* buf[], uint32_t* buflen) {
keys.shoot & 0b00000100 |
keys.block & 0b00000010 ;
- memcpy(*buf + sizeof(uint64_t) + 2, &keys8, sizeof(uint8_t));
+ memcpy(*buf + sizeof(uint64_t) * 2 + 1, &keys8, sizeof(uint8_t));
uint32_t vec[3];
- memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour dénaturer les floats.
+ memcpy(vec, &in->direction, sizeof(Vector3f)); // Pour d�naturer les floats.
uint8_t vec8[3 * sizeof(uint32_t)] = {
- (vec[0] >> 24) & 0xFF,
- (vec[0] >> 16) & 0xFF,
- (vec[0] >> 8) & 0xFF,
- vec[0] & 0xFF,
- (vec[1] >> 24) & 0xFF,
- (vec[1] >> 16) & 0xFF,
- (vec[1] >> 8) & 0xFF,
- vec[1] & 0xFF,
- (vec[2] >> 24) & 0xFF,
- (vec[2] >> 16) & 0xFF,
- (vec[2] >> 8) & 0xFF,
- vec[2] & 0xFF};
+ (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) + 3, vec8, sizeof(uint32_t) * 3);
+ memcpy(*buf + sizeof(uint64_t) * 2 + 2, vec8, sizeof(uint32_t) * 3);
- *buflen = sizeof(uint64_t) + 3 + sizeof(uint32_t) * 3;
+ *buflen = sizeof(uint64_t) * 2 + 2 + sizeof(uint32_t) * 3;
}
void netprot::Serialize(Output* out, char* buf[], uint32_t* buflen) {
+ *buf[0] = (char)netprot::PACKET_TYPE::OUTPUT;
+ 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));
+
+ 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));
+
+ // TODO: Finir ca.
}
void netprot::Serialize(Sync* sync, char* buf[], uint32_t* buflen) {
+ *buf[0] = (char)netprot::PACKET_TYPE::SYNC;
+ 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));
+ 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),
+ (uint8_t)((timer >> 16) & 0xFF),
+ (uint8_t)((timer >> 8) & 0xFF),
+ (uint8_t)(timer & 0xFF) };
+
+ 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));
+
+ memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 1, &sync->hp, sizeof(uint8_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) };
+
+ memcpy(*buf + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t) + 2, vec8, sizeof(uint32_t) * 3);
+
+ *buflen = sizeof(uint64_t) * 2 + sizeof(uint32_t) * 4 + sizeof(uint16_t) + 2;
}
void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::TEAMINF;
+ *buf[0] = (char)netprot::PACKET_TYPE::TEAMINF;
size_t namesize = std::strlen(tinfo->name) + 1;
memcpy(*buf + 1, &tinfo->name, namesize);
uint64_t tid = tinfo->id;
uint8_t tid8[sizeof(uint64_t)] = {
- (tid >> 56) & 0xFF,
- (tid >> 48) & 0xFF,
- (tid >> 40) & 0xFF,
- (tid >> 32) & 0xFF,
- (tid >> 24) & 0xFF,
- (tid >> 16) & 0xFF,
- (tid >> 8) & 0xFF,
- tid & 0xFF
+ (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 + namesize + 2, tid8, sizeof(uint64_t));
@@ -93,35 +179,35 @@ void netprot::Serialize(TeamInfo* tinfo, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::LOGINF;
+ *buf[0] = (char)netprot::PACKET_TYPE::LOGINF;
size_t namesize = std::strlen(linfo->name) + 1;
memcpy(*buf + 1, &linfo->name, namesize);
uint64_t sid = linfo->sid;
uint8_t sid8[sizeof(uint64_t)] = {
- (sid >> 56) & 0xFF,
- (sid >> 48) & 0xFF,
- (sid >> 40) & 0xFF,
- (sid >> 32) & 0xFF,
- (sid >> 24) & 0xFF,
- (sid >> 16) & 0xFF,
- (sid >> 8) & 0xFF,
- sid & 0xFF
+ (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 + namesize + 2, sid8, sizeof(uint64_t));
uint64_t tid = linfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
- (tid >> 56) & 0xFF,
- (tid >> 48) & 0xFF,
- (tid >> 40) & 0xFF,
- (tid >> 32) & 0xFF,
- (tid >> 24) & 0xFF,
- (tid >> 16) & 0xFF,
- (tid >> 8) & 0xFF,
- tid & 0xFF
+ (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 + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
@@ -130,35 +216,35 @@ void netprot::Serialize(LoginInfo* linfo, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::PLAYINF;
+ *buf[0] = (char)netprot::PACKET_TYPE::PLAYINF;
size_t namesize = std::strlen(pinfo->name) + 1;
memcpy(*buf + 1, &pinfo->name, namesize);
uint64_t id = pinfo->id;
uint8_t id8[sizeof(uint64_t)] = {
- (id >> 56) & 0xFF,
- (id >> 48) & 0xFF,
- (id >> 40) & 0xFF,
- (id >> 32) & 0xFF,
- (id >> 24) & 0xFF,
- (id >> 16) & 0xFF,
- (id >> 8) & 0xFF,
- id & 0xFF
+ (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)
};
memcpy(*buf + namesize + 2, id8, sizeof(uint64_t));
uint64_t tid = pinfo->tid;
uint8_t tid8[sizeof(uint64_t)] = {
- (tid >> 56) & 0xFF,
- (tid >> 48) & 0xFF,
- (tid >> 40) & 0xFF,
- (tid >> 32) & 0xFF,
- (tid >> 24) & 0xFF,
- (tid >> 16) & 0xFF,
- (tid >> 8) & 0xFF,
- tid & 0xFF
+ (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 + namesize + 2 + sizeof(uint64_t), tid8, sizeof(uint64_t));
@@ -167,46 +253,46 @@ void netprot::Serialize(PlayerInfo* pinfo, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::GAMEINFO;
+ *buf[0] = (char)netprot::PACKET_TYPE::GAMEINFO;
uint64_t game = ginfo->seed;
uint8_t seed8[sizeof(uint64_t)] = {
- (game >> 56) & 0xFF,
- (game >> 48) & 0xFF,
- (game >> 40) & 0xFF,
- (game >> 32) & 0xFF,
- (game >> 24) & 0xFF,
- (game >> 16) & 0xFF,
- (game >> 8) & 0xFF,
- game & 0xFF
+ (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)] = {
- (game >> 56) & 0xFF,
- (game >> 48) & 0xFF,
- (game >> 40) & 0xFF,
- (game >> 32) & 0xFF,
- (game >> 24) & 0xFF,
- (game >> 16) & 0xFF,
- (game >> 8) & 0xFF,
- game & 0xFF
+ (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)] = {
- (game >> 56) & 0xFF,
- (game >> 48) & 0xFF,
- (game >> 40) & 0xFF,
- (game >> 32) & 0xFF,
- (game >> 24) & 0xFF,
- (game >> 16) & 0xFF,
- (game >> 8) & 0xFF,
- game & 0xFF
+ (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, gtype8, sizeof(uint64_t));
@@ -215,46 +301,46 @@ void netprot::Serialize(GameInfo* ginfo, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::CHAT;
+ *buf[0] = (char)netprot::PACKET_TYPE::CHAT;
uint64_t src = chat->src_id;
uint8_t src8[sizeof(uint64_t)] = {
- (src >> 56) & 0xFF,
- (src >> 48) & 0xFF,
- (src >> 40) & 0xFF,
- (src >> 32) & 0xFF,
- (src >> 24) & 0xFF,
- (src >> 16) & 0xFF,
- (src >> 8) & 0xFF,
- src & 0xFF
+ (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)
};
memcpy(*buf + 1, src8, sizeof(uint64_t));
uint64_t dst = chat->dest_id;
uint8_t dst8[sizeof(uint64_t)] = {
- (dst >> 56) & 0xFF,
- (dst >> 48) & 0xFF,
- (dst >> 40) & 0xFF,
- (dst >> 32) & 0xFF,
- (dst >> 24) & 0xFF,
- (dst >> 16) & 0xFF,
- (dst >> 8) & 0xFF,
- dst & 0xFF
+ (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)
};
memcpy(*buf + 1 + sizeof(uint64_t), dst8, sizeof(uint64_t));
uint64_t dstteam = chat->dest_id;
uint8_t dstt8[sizeof(uint64_t)] = {
- (dstteam >> 56) & 0xFF,
- (dstteam >> 48) & 0xFF,
- (dstteam >> 40) & 0xFF,
- (dstteam >> 32) & 0xFF,
- (dstteam >> 24) & 0xFF,
- (dstteam >> 16) & 0xFF,
- (dstteam >> 8) & 0xFF,
- dstteam & 0xFF
+ (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)
};
memcpy(*buf + 1 + sizeof(uint64_t) * 2, dstt8, sizeof(uint64_t));
@@ -267,7 +353,7 @@ void netprot::Serialize(Chat* chat, char* buf[], uint32_t* buflen) {
}
void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
- *buf[0] = netprot::PACKET_TYPE::ERRLOG;
+ *buf[0] = (char)netprot::PACKET_TYPE::ERRLOG;
size_t messize = std::strlen(errlog->mess) + 1;
@@ -281,7 +367,61 @@ void netprot::Serialize(ErrorLog* errlog, char* buf[], uint32_t* buflen) {
bool netprot::Deserialize(Input* in, char* buf, const uint32_t buflen) {
- return false;
+ if (buflen <= sizeof(Input))
+ 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];
+
+ 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;
+ 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;
+
+ 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(&in->direction, vec, sizeof(uint32_t) * 3);
+
+ return true;
}
bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) {
@@ -289,7 +429,65 @@ bool netprot::Deserialize(Output* out, char* buf, const uint32_t buflen) {
}
bool netprot::Deserialize(Sync* sync, char* buf, const uint32_t buflen) {
- return false;
+ if (buflen <= sizeof(Sync))
+ 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];
+
+ memcpy(&sync->hp, &buf[1 + sizeof(uint64_t) * 2 + sizeof(uint32_t) + sizeof(uint16_t)], sizeof(uint8_t));
+
+ 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(uint32_t) + sizeof(uint16_t)], 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(&sync->position, vec, sizeof(uint32_t) * 3);
+
+ return true;
}
bool netprot::Deserialize(TeamInfo* tinfo, char* buf, const uint32_t buflen) {
@@ -497,10 +695,246 @@ bool netprot::Deserialize(ErrorLog* errlog, char* buf, const uint32_t buflen) {
return true;
}
-netprot::PacketType netprot::getType(char* buf, const uint32_t buflen) {
+netprot::PACKET_TYPE netprot::getType(char* buf, const uint32_t buflen) {
if (buflen < 1 ||
- buf[0] >= netprot::PACKET_TYPE::LAST_PACK ||
- buf[0] <= netprot::PACKET_TYPE::ERR)
+ buf[0] >= (char)netprot::PACKET_TYPE::LAST_PACK ||
+ buf[0] <= (char)netprot::PACKET_TYPE::ERR)
return netprot::PACKET_TYPE::ERR;
- return buf[0];
+ return (netprot::PACKET_TYPE)buf[0];
+}
+
+netprot::Packet netprot::getPack(char* buf, uint32_t buflen) {
+ 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;
+
+ switch (getType(buf, buflen)) {
+ case PACKET_TYPE::INPUT:
+ in = new Input();
+ if (netprot::Deserialize(in, buf, buflen)) {
+ pck.type = PACKET_TYPE::INPUT;
+ pck.ptr = (void*)in;
+ }
+ break;
+ case PACKET_TYPE::OUTPUT:
+ out = new Output();
+ if (netprot::Deserialize(out, buf, buflen)) {
+ pck.type = PACKET_TYPE::OUTPUT;
+ pck.ptr = (void*)out;
+ }
+ break;
+ case PACKET_TYPE::SYNC:
+ sync = new Sync();
+ if (netprot::Deserialize(sync, buf, buflen)) {
+ pck.type = PACKET_TYPE::SYNC;
+ pck.ptr = (void*)sync;
+ }
+ break;
+ case PACKET_TYPE::CHAT:
+ chat = new Chat();
+ if (netprot::Deserialize(chat, buf, buflen)) {
+ pck.type = PACKET_TYPE::CHAT;
+ pck.ptr = (void*)chat;
+ }
+ break;
+ case PACKET_TYPE::GAMEINFO:
+ ginfo = new GameInfo();
+ if (netprot::Deserialize(ginfo, buf, buflen)) {
+ pck.type = PACKET_TYPE::GAMEINFO;
+ pck.ptr = (void*)ginfo;
+ }
+ 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;
+ }
+ break;
+ default:
+ break;
+ }
+ return pck;
+}
+
+netprot::Packet netprot::getPack(netprot::Buffer* buf) {
+ return netprot::getPack(buf->ptr, buf->len);
+}
+
+bool netprot::emptyPack(netprot::Packet pck) {
+ switch (pck.type) {
+ case PACKET_TYPE::INPUT:
+ delete (Input*)pck.ptr;
+ return true;
+ case PACKET_TYPE::OUTPUT:
+ delete (Output*)pck.ptr;
+ return true;
+ case PACKET_TYPE::SYNC:
+ delete (Sync*)pck.ptr;
+ return true;
+ case PACKET_TYPE::CHAT:
+ delete (Chat*)pck.ptr;
+ return true;
+ case PACKET_TYPE::GAMEINFO:
+ delete (GameInfo*)pck.ptr;
+ return true;
+ case PACKET_TYPE::ERRLOG:
+ delete (ErrorLog*)pck.ptr;
+ return true;
+ case PACKET_TYPE::LOGINF:
+ delete (LoginInfo*)pck.ptr;
+ return true;
+ default:
+ return false;
+ }
+}
+
+netprot::Packet netprot::makePack(void* ptr, PACKET_TYPE type) {
+ Packet pck;
+ pck.ptr = ptr;
+ pck.type = type;
+ return pck;
+}
+
+std::vector netprot::recvPacks(SOCKET sock, Buffer* buf) {
+ std::vector lsPck;
+ int len = 0, end = 0;
+ bool pck_received = false;
+ char* cursor = nullptr;
+
+ while (true) {
+ while (!pck_received) {
+ int bytes = recv(sock, &buf->ptr[len], buf->len - len, 0);
+ if (bytes < 0) // si recv() retourne -1; ça veut dire qu'il y a plus rien a lire.
+ return lsPck;
+ len += bytes;
+ // TODO: Voir si on trouve un footer (5 '\0' d'affilee).
+ // Si oui, mettre l'index du debut du footer dans
+ // la variable "end" et mettre pck_received a true.
+ }
+
+ cursor = &buf->ptr[end];
+
+ lsPck.push_back(getPack(buf));
+
+ while (*cursor == '\0') {
+ cursor++;
+ end++;
+ }
+
+ if (cursor) { // mettre le début du prochain paquet au début du buffer.
+ memcpy(buf->ptr, cursor, buf->len - end); // et voir s'il y a en un autre
+ len = 0;
+ pck_received = true;
+ }
+ }
+}
+
+template <>
+void netprot::sendPack(SOCKET sock, Packet* pack, char** buf, uint32_t* buflen) {
+ switch (pack->type) {
+ case PACKET_TYPE::INPUT:
+ sendPack(sock, (Input*)pack->ptr, buf, buflen);
+ return;
+ case PACKET_TYPE::OUTPUT:
+ sendPack