packets: Fix struct packing related bugs

- sizeof(bool) is implementation defined. The server assumes it's 4 but for me it was 1 (tested with Release too) which caused some bugs
- Structs aren't guaranteed to be packed. The compiler is free to change the layout, which we wouldn't want to for the Packet structs that we deserialize on the server.
This commit is contained in:
Nick Renieris 2022-06-22 20:16:08 +03:00 committed by GRAnimated
parent 5c71569545
commit 9227e37623
14 changed files with 27 additions and 20 deletions

View file

@ -2,7 +2,7 @@
#include "Packet.h" #include "Packet.h"
struct CaptureInf : Packet { struct PACKED CaptureInf : Packet {
CaptureInf() : Packet() { CaptureInf() : Packet() {
this->mType = PacketType::CAPTUREINF; this->mType = PacketType::CAPTUREINF;
mPacketSize = sizeof(CaptureInf) - sizeof(Packet); mPacketSize = sizeof(CaptureInf) - sizeof(Packet);

View file

@ -2,7 +2,7 @@
#include "Packet.h" #include "Packet.h"
struct ChangeStagePacket : Packet { struct PACKED ChangeStagePacket : Packet {
ChangeStagePacket() : Packet() { ChangeStagePacket() : Packet() {
this->mType = PacketType::CHANGESTAGE; this->mType = PacketType::CHANGESTAGE;
mPacketSize = sizeof(ChangeStagePacket) - sizeof(Packet); mPacketSize = sizeof(ChangeStagePacket) - sizeof(Packet);

View file

@ -2,7 +2,7 @@
#include "Packet.h" #include "Packet.h"
struct CostumeInf : Packet { struct PACKED CostumeInf : Packet {
CostumeInf() : Packet() {this->mType = PacketType::COSTUMEINF; mPacketSize = sizeof(CostumeInf) - sizeof(Packet);}; CostumeInf() : Packet() {this->mType = PacketType::COSTUMEINF; mPacketSize = sizeof(CostumeInf) - sizeof(Packet);};
CostumeInf(const char* body, const char* cap) : Packet() { CostumeInf(const char* body, const char* cap) : Packet() {
this->mType = PacketType::COSTUMEINF; this->mType = PacketType::COSTUMEINF;

View file

@ -3,9 +3,9 @@
#include "Packet.h" #include "Packet.h"
#include "al/util.hpp" #include "al/util.hpp"
struct GameInf : Packet { struct PACKED GameInf : Packet {
GameInf() : Packet() {this->mType = PacketType::GAMEINF; mPacketSize = sizeof(GameInf) - sizeof(Packet);}; GameInf() : Packet() {this->mType = PacketType::GAMEINF; mPacketSize = sizeof(GameInf) - sizeof(Packet);};
bool is2D = false; bool4 is2D = false;
u8 scenarioNo = -1; u8 scenarioNo = -1;
char stageName[0x40] = {}; char stageName[0x40] = {};

View file

@ -2,10 +2,10 @@
#include "Packet.h" #include "Packet.h"
struct HackCapInf : Packet { struct PACKED HackCapInf : Packet {
HackCapInf() : Packet() {this->mType = PacketType::HACKCAPINF; mPacketSize = sizeof(HackCapInf) - sizeof(Packet);}; HackCapInf() : Packet() {this->mType = PacketType::HACKCAPINF; mPacketSize = sizeof(HackCapInf) - sizeof(Packet);};
sead::Vector3f capPos; sead::Vector3f capPos;
sead::Quatf capQuat; sead::Quatf capQuat;
bool isCapVisible = false; bool4 isCapVisible = false;
char capAnim[PACKBUFSIZE] = {}; char capAnim[PACKBUFSIZE] = {};
}; };

View file

@ -2,7 +2,7 @@
#include "Packet.h" #include "Packet.h"
struct InitPacket : Packet { struct PACKED InitPacket : Packet {
InitPacket() : Packet() {this->mType = PacketType::CLIENTINIT; mPacketSize = sizeof(InitPacket) - sizeof(Packet);}; InitPacket() : Packet() {this->mType = PacketType::CLIENTINIT; mPacketSize = sizeof(InitPacket) - sizeof(Packet);};
u16 maxPlayers = 0; u16 maxPlayers = 0;
}; };

View file

@ -5,6 +5,8 @@
#include "nn/account.h" #include "nn/account.h"
#include "types.h"
#define PACKBUFSIZE 0x30 #define PACKBUFSIZE 0x30
#define COSTUMEBUFSIZE 0x20 #define COSTUMEBUFSIZE 0x20
@ -27,7 +29,7 @@ enum PacketType : short {
}; };
// attribute otherwise the build log is spammed with unused warnings // attribute otherwise the build log is spammed with unused warnings
__attribute((used)) static const char *packetNames[] = { USED static const char *packetNames[] = {
"Unknown", "Unknown",
"Player Info", "Player Info",
"Player Cap Info", "Player Cap Info",
@ -59,7 +61,7 @@ static const char *senderNames[] = {
}; };
*/ */
struct Packet { struct PACKED Packet {
nn::account::Uid mUserID; // User ID of the packet owner nn::account::Uid mUserID; // User ID of the packet owner
PacketType mType = PacketType::UNKNOWN; PacketType mType = PacketType::UNKNOWN;
short mPacketSize = 0; // represents packet size without size of header short mPacketSize = 0; // represents packet size without size of header

View file

@ -2,7 +2,7 @@
#include "Packet.h" #include "Packet.h"
struct PlayerConnect : Packet { struct PACKED PlayerConnect : Packet {
PlayerConnect() : Packet() {this->mType = PacketType::PLAYERCON; mPacketSize = sizeof(PlayerConnect) - sizeof(Packet);}; PlayerConnect() : Packet() {this->mType = PacketType::PLAYERCON; mPacketSize = sizeof(PlayerConnect) - sizeof(Packet);};
ConnectionTypes conType; ConnectionTypes conType;
u16 maxPlayerCount; u16 maxPlayerCount;

View file

@ -2,6 +2,6 @@
#include "Packet.h" #include "Packet.h"
struct PlayerDC : Packet { struct PACKED PlayerDC : Packet {
PlayerDC() : Packet() {this->mType = PacketType::PLAYERDC; mPacketSize = sizeof(PlayerDC) - sizeof(Packet);}; PlayerDC() : Packet() {this->mType = PacketType::PLAYERDC; mPacketSize = sizeof(PlayerDC) - sizeof(Packet);};
}; };

View file

@ -4,7 +4,7 @@
#include "al/util.hpp" #include "al/util.hpp"
#include "algorithms/PlayerAnims.h" #include "algorithms/PlayerAnims.h"
struct PlayerInf : Packet { struct PACKED PlayerInf : Packet {
PlayerInf() : Packet() {mType = PacketType::PLAYERINF; mPacketSize = sizeof(PlayerInf) - sizeof(Packet);}; PlayerInf() : Packet() {mType = PacketType::PLAYERINF; mPacketSize = sizeof(PlayerInf) - sizeof(Packet);};
sead::Vector3f playerPos; sead::Vector3f playerPos;
sead::Quatf playerRot; sead::Quatf playerRot;

View file

@ -2,7 +2,7 @@
#include "Packet.h" #include "Packet.h"
struct ServerCommand : Packet { struct PACKED ServerCommand : Packet {
ServerCommand(const char *command) : Packet() {this->mType = PacketType::CMD; strcpy(srvCmd, command); mPacketSize = sizeof(ServerCommand) - sizeof(Packet);}; ServerCommand(const char *command) : Packet() {this->mType = PacketType::CMD; strcpy(srvCmd, command); mPacketSize = sizeof(ServerCommand) - sizeof(Packet);};
char srvCmd[PACKBUFSIZE] = {}; char srvCmd[PACKBUFSIZE] = {};
}; };

View file

@ -2,8 +2,8 @@
#include "Packet.h" #include "Packet.h"
struct ShineCollect : Packet { struct PACKED ShineCollect : Packet {
ShineCollect() : Packet() {this->mType = PacketType::SHINECOLL; mPacketSize = sizeof(ShineCollect) - sizeof(Packet);}; ShineCollect() : Packet() {this->mType = PacketType::SHINECOLL; mPacketSize = sizeof(ShineCollect) - sizeof(Packet);};
int shineId = -1; int shineId = -1;
bool isGrand = false; bool4 isGrand = false;
}; };

View file

@ -8,10 +8,10 @@ enum TagUpdateType : u8 {
STATE = 1 << 1 STATE = 1 << 1
}; };
struct TagInf : Packet { struct PACKED TagInf : Packet {
TagInf() : Packet() { this->mType = PacketType::TAGINF; mPacketSize = sizeof(TagInf) - sizeof(Packet);}; TagInf() : Packet() { this->mType = PacketType::TAGINF; mPacketSize = sizeof(TagInf) - sizeof(Packet);};
TagUpdateType updateType; TagUpdateType updateType;
bool isIt = false; bool4 isIt = false;
u8 seconds; u8 seconds;
u16 minutes; u16 minutes;
}; };

View file

@ -18,6 +18,8 @@ typedef signed int s32;
typedef int64_t s64; typedef int64_t s64;
typedef __int128_t s128; typedef __int128_t s128;
typedef u32 bool4; // guaranteed to be 4 bytes, 'bool' is impl. defined
typedef float f32; typedef float f32;
typedef double f64; typedef double f64;
@ -76,3 +78,6 @@ struct Rect
float right; float right;
float top; float top;
}; };
#define PACKED __attribute__((packed))
#define USED __attribute__((used))