Skip to content

Commit

Permalink
[MKW] Report character and vehicle combinations to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeIsAStar committed Sep 16, 2024
1 parent d7f9130 commit 666635b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 46 deletions.
20 changes: 10 additions & 10 deletions payload/import/mkw/net/matchHeaderHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ class MatchHeaderHandler
{
public:
struct __attribute__((packed)) Packet {
enum class Vehicle : u8 {
enum class Course : u8 {
None = 0xFF,
};

enum class Character : u8 {
None = 0xFF,
};
struct Combination {
enum class Character : u8 {
None = 0xFF,
};

enum class Course : u8 {
None = 0xFF,
};
enum class Vehicle : u8 {
None = 0xFF,
};

struct Player {
/* 0x00 */ Vehicle vehicle;
/* 0x01 */ Character character;
};

static_assert(sizeof(Player) == 0x02);
static_assert(sizeof(Combination) == 0x02);

/* 0x00 */ u8 _00[0x0E - 0x00];
/* 0x0E */ Player player[2];
/* 0x0E */ Combination combination[2];
/* 0x12 */ u8 _12[0x16 - 0x12];
/* 0x16 */ Course course;
/* 0x17 */ u8 _17[0x28 - 0x17];
Expand Down
24 changes: 22 additions & 2 deletions payload/import/mkw/net/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ class NetController
return isAidTheServer(myAid());
}

u8 consolePlayerCount(u8 playerAid) const
{
const ConnectionInfo& connectionInfo = currentConnectionInfo();

if (playerAid == myAid()) {
return connectionInfo.consolePlayerCount;
} else {
return connectionInfo.aidInformation[playerAid].consolePlayerCount;
}
}

bool amITheRoomHost() const
{
return m_joinType == JoinType::RoomHost;
Expand Down Expand Up @@ -183,12 +194,21 @@ class NetController

private:
struct ConnectionInfo {
struct AidInformation {
/* 0x00 */ u8 consolePlayerCount;
/* 0x01 */ u8 _01[0x04 - 0x01];
};

static_assert(sizeof(AidInformation) == 0x04);

/* 0x00 */ u8 _00[0x10 - 0x00];
/* 0x10 */ u32 availableAids;
/* 0x14 */ u8 _14[0x21 - 0x14];
/* 0x14 */ u8 _14[0x20 - 0x14];
/* 0x20 */ u8 consolePlayerCount;
/* 0x21 */ u8 myAid;
/* 0x22 */ u8 serverAid;
/* 0x23 */ u8 _23[0x58 - 0x23];
/* 0x23 */ AidInformation aidInformation[12];
/* 0x53 */ u8 _53[0x58 - 0x53];
};

static_assert(sizeof(ConnectionInfo) == 0x58);
Expand Down
40 changes: 27 additions & 13 deletions payload/import/mkw/net/selectHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,28 @@ class SelectHandler
};

struct Player {
enum class Character : u8 {
NotSelected = 0x30,
};
struct Combination {
enum class Character : u8 {
NotSelected = 0x30,
};

enum class Vehicle : u8 {
NotSelected = 0x24,
};

enum class Vehicle : u8 {
NotSelected = 0x24,
Character character;
Vehicle vehicle;
};

static_assert(sizeof(Combination) == 0x02);

enum class CourseVote : u8 {
NotSelected = 0x43,
Random = 0xFF,
};

/* 0x00 */ u8 _00[0x04 - 0x00];
/* 0x04 */ Character character;
/* 0x05 */ Vehicle vehicle;
/* 0x04 */ Combination combination;
/* 0x06 */ CourseVote courseVote;
/* 0x07 */ u8 _07;
};
Expand All @@ -63,6 +69,15 @@ class SelectHandler

static_assert(sizeof(Packet) == 0x38);

Packet& packet(u8 playerAid)
{
if (playerAid == NetController::Instance()->myAid()) {
return m_sendPacket;
} else {
return m_receivePacket[playerAid];
}
}

Packet& sendPacket()
{
return m_sendPacket;
Expand Down Expand Up @@ -144,14 +159,12 @@ class SelectHandler
}
}

// Support modifications that allow for clients to be connected to more
// than 11 peers at once.
for (size_t n = 0; n < sizeof(aidsStillLoading) * 8; n++) {
if (((aidsStillLoading >> n) & 1) == 0) {
for (u32 playerAid = 0; playerAid < 12; playerAid++) {
if (((aidsStillLoading >> playerAid) & 1) == 0) {
continue;
}

netController->reportAndKick("mkw_room_stall", n);
netController->reportAndKick("mkw_room_stall", playerAid);
}
}

Expand All @@ -168,7 +181,8 @@ class SelectHandler
private:
/* 0x000 */ u8 _000[0x008 - 0x000];
/* 0x008 */ Packet m_sendPacket;
/* 0x040 */ u8 _040[0x3E0 - 0x040];
/* 0x040 */ Packet m_receivePacket[12];
/* 0x2E0 */ u8 _2E0[0x3E0 - 0x2E0];
/* 0x3E0 */ u32 m_aidsWithNewSelectPacket;
/* 0x3E4 */ u8 _3E4[0x3E8 - 0x3E4];
/* 0x3E8 */ u32 m_aidsWithNewMatchSettings;
Expand Down
33 changes: 33 additions & 0 deletions payload/wwfcFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,44 @@ WWFC_DEFINE_PATCH = {
selectHandler->decideCourse();
selectHandler->initPlayerIdsToPlayerAids();

SelectHandler::Packet::Player::Combination combinations[12][2];
NetController* netController = NetController::Instance();
u32 availableAids = netController->availableAids();
for (u8 playerAid = 0; playerAid < 12; playerAid++) {
SelectHandler::Packet::Player::Combination* playerCombination =
&combinations[playerAid][0];
SelectHandler::Packet::Player::Combination* guestCombination =
&combinations[playerAid][1];

playerCombination->character = guestCombination->character =
SelectHandler::Packet::Player::Combination::Character::NotSelected;
playerCombination->vehicle = guestCombination->vehicle =
SelectHandler::Packet::Player::Combination::Vehicle::NotSelected;

if (((availableAids >> playerAid) & 1) == 0) {
continue;
}

const SelectHandler::Packet& packet = selectHandler->packet(playerAid);
playerCombination->character = packet.player[0].combination.character;
playerCombination->vehicle = packet.player[0].combination.vehicle;

if (netController->consolePlayerCount(playerAid) == 1) {
continue;
}

guestCombination->character = packet.player[1].combination.character;
guestCombination->vehicle = packet.player[1].combination.vehicle;
}

SelectHandler::Packet::SelectedCourse selectedCourse =
selectHandler->sendPacket().selectedCourse;
SelectHandler::Packet::EngineClass engineClass =
selectHandler->sendPacket().engineClass;

wwfc::GPReport::ReportB64Encode(
"mkw_select_combos", combinations, sizeof(combinations)
);
wwfc::GPReport::ReportU32(
"mkw_select_course", static_cast<u32>(selectedCourse)
);
Expand Down
45 changes: 24 additions & 21 deletions payload/wwfcSecurity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,23 @@ static bool IsMatchHeaderPacketDataValid(
RaceConfig::Scenario* scenario = &RaceConfig::Instance()->raceScenario();

for (size_t n = 0;
n < ARRAY_ELEMENT_COUNT(MatchHeaderHandler::Packet::player); n++) {
MatchHeaderHandler::Packet::Player player =
matchHeaderPacket->player[n];

MatchHeaderHandler::Packet::Vehicle playerVehicle = player.vehicle;
MatchHeaderHandler::Packet::Character playerCharacter =
player.character;
if (playerVehicle == MatchHeaderHandler::Packet::Vehicle::None &&
playerCharacter == MatchHeaderHandler::Packet::Character::None) {
n < ARRAY_ELEMENT_COUNT(MatchHeaderHandler::Packet::combination);
n++) {
const MatchHeaderHandler::Packet::Combination* combination =
&matchHeaderPacket->combination[n];
MatchHeaderHandler::Packet::Combination::Vehicle selectedVehicle =
combination->vehicle;
MatchHeaderHandler::Packet::Combination::Character selectedCharacter =
combination->character;
if (selectedVehicle ==
MatchHeaderHandler::Packet::Combination::Vehicle::None &&
selectedCharacter ==
MatchHeaderHandler::Packet::Combination::Character::None) {
continue;
}

Vehicle vehicle = static_cast<Vehicle>(playerVehicle);
Character character = static_cast<Character>(playerCharacter);
Vehicle vehicle = static_cast<Vehicle>(selectedVehicle);
Character character = static_cast<Character>(selectedCharacter);
if (scenario->isOnlineVersusRace()) {
if (!IsCombinationValidVS(character, vehicle)) {
return false;
Expand Down Expand Up @@ -353,16 +356,16 @@ IsRoomSelectPacketDataValid(const void* packet, u8 packetSize, u8 playerAid)
reinterpret_cast<const SelectHandler::Packet*>(packet);
for (size_t n = 0;
n < ARRAY_ELEMENT_COUNT(SelectHandler::Packet::player); n++) {
SelectHandler::Packet::Player player = selectPacket->player[n];

SelectHandler::Packet::Player::Character selectedCharacter =
player.character;
SelectHandler::Packet::Player::Vehicle selectedVehicle =
player.vehicle;
if (selectedCharacter !=
SelectHandler::Packet::Player::Character::NotSelected ||
selectedVehicle !=
SelectHandler::Packet::Player::Vehicle::NotSelected) {
const SelectHandler::Packet::Player* player =
&selectPacket->player[n];
SelectHandler::Packet::Player::Combination::Character
selectedCharacter = player->combination.character;
SelectHandler::Packet::Player::Combination::Vehicle
selectedVehicle = player->combination.vehicle;
if (selectedCharacter != SelectHandler::Packet::Player::
Combination::Character::NotSelected ||
selectedVehicle != SelectHandler::Packet::Player::Combination::
Vehicle::NotSelected) {
Character character = static_cast<Character>(selectedCharacter);
Vehicle vehicle = static_cast<Vehicle>(selectedVehicle);
if (scenario->isOnlineVersusRace()) {
Expand Down

0 comments on commit 666635b

Please sign in to comment.