Improve SFMLHelper

Switch to using additional overloads of sf::Packet, so we can eliminate
some of the messy code and just use the normal syntax for
BigEndianValue.

We can't avoid helper functions with u64 due to SFML's non-standard way
of defining 64-bit integer types.
This commit is contained in:
Techjar 2018-10-02 04:45:36 -04:00
parent 58b0a284a8
commit c5b4e1a5f6
3 changed files with 35 additions and 24 deletions

View File

@ -6,24 +6,34 @@
#include <SFML/Network/Packet.hpp> #include <SFML/Network/Packet.hpp>
namespace Common sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u16>& data)
{
// This only exists as a helper for BigEndianValue
u16 PacketReadU16(sf::Packet& packet)
{ {
u16 tmp; u16 tmp;
packet >> tmp; packet >> tmp;
return tmp; data = tmp;
return packet;
} }
// This only exists as a helper for BigEndianValue sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u32>& data)
u32 PacketReadU32(sf::Packet& packet)
{ {
u32 tmp; u32 tmp;
packet >> tmp; packet >> tmp;
return tmp; data = tmp;
return packet;
} }
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u64>& data)
{
sf::Uint64 tmp;
packet >> tmp;
data = tmp;
return packet;
}
namespace Common
{
// SFML's Uint64 type is different depending on platform,
// so we have this for cleaner code.
u64 PacketReadU64(sf::Packet& packet) u64 PacketReadU64(sf::Packet& packet)
{ {
sf::Uint64 value; sf::Uint64 value;

View File

@ -5,15 +5,18 @@
#pragma once #pragma once
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Swap.h"
namespace sf namespace sf
{ {
class Packet; class Packet;
} }
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u16>& data);
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u32>& data);
sf::Packet& operator>>(sf::Packet& packet, Common::BigEndianValue<u64>& data);
namespace Common namespace Common
{ {
u16 PacketReadU16(sf::Packet& packet);
u32 PacketReadU32(sf::Packet& packet);
u64 PacketReadU64(sf::Packet& packet); u64 PacketReadU64(sf::Packet& packet);
} // namespace Common } // namespace Common

View File

@ -748,29 +748,29 @@ unsigned int NetPlayClient::OnData(sf::Packet& packet)
{ {
// Header // Header
WiiSave::Header header; WiiSave::Header header;
header.tid = Common::PacketReadU64(packet); packet >> header.tid;
header.banner_size = Common::PacketReadU32(packet); packet >> header.banner_size;
packet >> header.permissions; packet >> header.permissions;
packet >> header.unk1; packet >> header.unk1;
for (size_t i = 0; i < header.md5.size(); i++) for (size_t i = 0; i < header.md5.size(); i++)
packet >> header.md5[i]; packet >> header.md5[i];
header.unk2 = Common::PacketReadU16(packet); packet >> header.unk2;
for (size_t i = 0; i < header.banner_size; i++) for (size_t i = 0; i < header.banner_size; i++)
packet >> header.banner[i]; packet >> header.banner[i];
// BkHeader // BkHeader
WiiSave::BkHeader bk_header; WiiSave::BkHeader bk_header;
bk_header.size = Common::PacketReadU32(packet); packet >> bk_header.size;
bk_header.magic = Common::PacketReadU32(packet); packet >> bk_header.magic;
bk_header.ngid = Common::PacketReadU32(packet); packet >> bk_header.ngid;
bk_header.number_of_files = Common::PacketReadU32(packet); packet >> bk_header.number_of_files;
bk_header.size_of_files = Common::PacketReadU32(packet); packet >> bk_header.size_of_files;
bk_header.unk1 = Common::PacketReadU32(packet); packet >> bk_header.unk1;
bk_header.unk2 = Common::PacketReadU32(packet); packet >> bk_header.unk2;
bk_header.total_size = Common::PacketReadU32(packet); packet >> bk_header.total_size;
for (size_t i = 0; i < bk_header.unk3.size(); i++) for (size_t i = 0; i < bk_header.unk3.size(); i++)
packet >> bk_header.unk3[i]; packet >> bk_header.unk3[i];
bk_header.tid = Common::PacketReadU64(packet); packet >> bk_header.tid;
for (size_t i = 0; i < bk_header.mac_address.size(); i++) for (size_t i = 0; i < bk_header.mac_address.size(); i++)
packet >> bk_header.mac_address[i]; packet >> bk_header.mac_address[i];
@ -1196,7 +1196,6 @@ void NetPlayClient::SyncSaveDataResponse(const bool success)
bool NetPlayClient::DecompressPacketIntoFile(sf::Packet& packet, const std::string& file_path) bool NetPlayClient::DecompressPacketIntoFile(sf::Packet& packet, const std::string& file_path)
{ {
u64 file_size = Common::PacketReadU64(packet); u64 file_size = Common::PacketReadU64(packet);
;
if (file_size == 0) if (file_size == 0)
return true; return true;
@ -1245,7 +1244,6 @@ bool NetPlayClient::DecompressPacketIntoFile(sf::Packet& packet, const std::stri
std::optional<std::vector<u8>> NetPlayClient::DecompressPacketIntoBuffer(sf::Packet& packet) std::optional<std::vector<u8>> NetPlayClient::DecompressPacketIntoBuffer(sf::Packet& packet)
{ {
u64 size = Common::PacketReadU64(packet); u64 size = Common::PacketReadU64(packet);
;
std::vector<u8> out_buffer(size); std::vector<u8> out_buffer(size);