1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-02 00:10:22 +02:00

Use std::byte in serializer

This commit is contained in:
Ivan Savenko 2024-02-02 02:36:57 +02:00
parent 29c0989849
commit 03fcfe3392
19 changed files with 41 additions and 50 deletions

View File

@ -35,7 +35,7 @@ GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
pos.w = 200; pos.w = 200;
pos.h = 200; pos.h = 200;
background = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h)); filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.login.title")); labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.login.title"));
labelUsername = std::make_shared<CLabel>( 10, 45, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username")); labelUsername = std::make_shared<CLabel>( 10, 45, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.lobby.login.username"));
backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 70, 180, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64)); backgroundUsername = std::make_shared<TransparentFilledRectangle>(Rect(10, 70, 180, 20), ColorRGBA(0,0,0,128), ColorRGBA(64,64,64,64));
@ -44,7 +44,7 @@ GlobalLobbyLoginWindow::GlobalLobbyLoginWindow()
buttonClose = std::make_shared<CButton>(Point(126, 160), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); }); buttonClose = std::make_shared<CButton>(Point(126, 160), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
labelStatus = std::make_shared<CTextBox>( "", Rect(15, 95, 175, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE); labelStatus = std::make_shared<CTextBox>( "", Rect(15, 95, 175, 60), 1, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
background->playerColored(PlayerColor(1)); filledBackground->playerColored(PlayerColor(1));
inputUsername->setText(settings["lobby"]["displayName"].String()); inputUsername->setText(settings["lobby"]["displayName"].String());
inputUsername->cb += [this](const std::string & text) inputUsername->cb += [this](const std::string & text)
{ {

View File

@ -20,7 +20,7 @@ class CButton;
class GlobalLobbyLoginWindow : public CWindowObject class GlobalLobbyLoginWindow : public CWindowObject
{ {
std::shared_ptr<FilledTexturePlayerColored> background; std::shared_ptr<FilledTexturePlayerColored> filledBackground;
std::shared_ptr<CLabel> labelTitle; std::shared_ptr<CLabel> labelTitle;
std::shared_ptr<CLabel> labelUsername; std::shared_ptr<CLabel> labelUsername;
std::shared_ptr<CTextBox> labelStatus; std::shared_ptr<CTextBox> labelStatus;

View File

@ -31,7 +31,7 @@ GlobalLobbyServerSetup::GlobalLobbyServerSetup()
pos.w = 284; pos.w = 284;
pos.h = 340; pos.h = 340;
background = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h)); filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.room.create")); labelTitle = std::make_shared<CLabel>( pos.w / 2, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.room.create"));
labelPlayerLimit = std::make_shared<CLabel>( pos.w / 2, 48, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.room.players.limit")); labelPlayerLimit = std::make_shared<CLabel>( pos.w / 2, 48, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.room.players.limit"));
labelRoomType = std::make_shared<CLabel>( pos.w / 2, 108, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.room.type")); labelRoomType = std::make_shared<CLabel>( pos.w / 2, 108, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.lobby.room.type"));
@ -75,7 +75,7 @@ GlobalLobbyServerSetup::GlobalLobbyServerSetup()
buttonCreate = std::make_shared<CButton>(Point(10, 300), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onCreate(); }); buttonCreate = std::make_shared<CButton>(Point(10, 300), AnimationPath::builtin("MuBchck"), CButton::tooltip(), [this](){ onCreate(); });
buttonClose = std::make_shared<CButton>(Point(210, 300), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); }); buttonClose = std::make_shared<CButton>(Point(210, 300), AnimationPath::builtin("MuBcanc"), CButton::tooltip(), [this](){ onClose(); });
background->playerColored(PlayerColor(1)); filledBackground->playerColored(PlayerColor(1));
updateDescription(); updateDescription();
center(); center();

View File

@ -19,7 +19,7 @@ class CToggleGroup;
class GlobalLobbyServerSetup : public CWindowObject class GlobalLobbyServerSetup : public CWindowObject
{ {
std::shared_ptr<FilledTexturePlayerColored> background; std::shared_ptr<FilledTexturePlayerColored> filledBackground;
std::shared_ptr<CLabel> labelTitle; std::shared_ptr<CLabel> labelTitle;
std::shared_ptr<CLabel> labelPlayerLimit; std::shared_ptr<CLabel> labelPlayerLimit;

View File

@ -24,7 +24,7 @@ class GlobalLobbyWidget : public InterfaceObjectConfigurable
std::shared_ptr<CIntObject> buildRoomList(const JsonNode &) const; std::shared_ptr<CIntObject> buildRoomList(const JsonNode &) const;
public: public:
GlobalLobbyWidget(GlobalLobbyWindow * window); explicit GlobalLobbyWidget(GlobalLobbyWindow * window);
std::shared_ptr<CLabel> getAccountNameLabel(); std::shared_ptr<CLabel> getAccountNameLabel();
std::shared_ptr<CTextInput> getMessageInput(); std::shared_ptr<CTextInput> getMessageInput();

View File

@ -72,10 +72,6 @@ JsonNode::JsonNode(JsonType Type)
setType(Type); setType(Type);
} }
JsonNode::JsonNode(const uint8_t *data, size_t datasize)
:JsonNode(reinterpret_cast<const char*>(data), datasize)
{}
JsonNode::JsonNode(const std::byte *data, size_t datasize) JsonNode::JsonNode(const std::byte *data, size_t datasize)
:JsonNode(reinterpret_cast<const char*>(data), datasize) :JsonNode(reinterpret_cast<const char*>(data), datasize)
{} {}

View File

@ -51,7 +51,6 @@ public:
JsonNode(JsonType Type = JsonType::DATA_NULL); JsonNode(JsonType Type = JsonType::DATA_NULL);
//Create tree from Json-formatted input //Create tree from Json-formatted input
explicit JsonNode(const char * data, size_t datasize); explicit JsonNode(const char * data, size_t datasize);
explicit JsonNode(const uint8_t * data, size_t datasize);
explicit JsonNode(const std::byte * data, size_t datasize); explicit JsonNode(const std::byte * data, size_t datasize);
//Create tree from JSON file //Create tree from JSON file
explicit JsonNode(const JsonPath & fileURI); explicit JsonNode(const JsonPath & fileURI);

View File

@ -23,9 +23,13 @@ protected:
public: public:
CLoaderBase(IBinaryReader * r): reader(r){}; CLoaderBase(IBinaryReader * r): reader(r){};
inline int read(void * data, unsigned size) inline void read(void * data, unsigned size, bool reverseEndianess)
{ {
return reader->read(data, size); auto bytePtr = reinterpret_cast<std::byte*>(data);
reader->read(bytePtr, size);
if(reverseEndianess)
std::reverse(bytePtr, bytePtr + size);
}; };
}; };
@ -170,11 +174,7 @@ public:
template < class T, typename std::enable_if < std::is_fundamental<T>::value && !std::is_same<T, bool>::value, int >::type = 0 > template < class T, typename std::enable_if < std::is_fundamental<T>::value && !std::is_same<T, bool>::value, int >::type = 0 >
void load(T &data) void load(T &data)
{ {
unsigned length = sizeof(data); this->read(static_cast<void *>(&data), sizeof(data), reverseEndianess);
char * dataPtr = reinterpret_cast<char *>(&data);
this->read(dataPtr,length);
if(reverseEndianess)
std::reverse(dataPtr, dataPtr + length);
} }
template < typename T, typename std::enable_if < is_serializeable<BinaryDeserializer, T>::value, int >::type = 0 > template < typename T, typename std::enable_if < is_serializeable<BinaryDeserializer, T>::value, int >::type = 0 >
@ -439,7 +439,7 @@ public:
{ {
ui32 length = readAndCheckLength(); ui32 length = readAndCheckLength();
data.resize(length); data.resize(length);
this->read((void*)data.c_str(),length); this->read(static_cast<void *>(data.data()), length, false);
} }
template<typename... TN> template<typename... TN>

View File

@ -23,9 +23,9 @@ protected:
public: public:
CSaverBase(IBinaryWriter * w): writer(w){}; CSaverBase(IBinaryWriter * w): writer(w){};
inline int write(const void * data, unsigned size) inline void write(const void * data, unsigned size)
{ {
return writer->write(data, size); writer->write(reinterpret_cast<const std::byte*>(data), size);
}; };
}; };
@ -145,7 +145,7 @@ public:
void save(const T &data) void save(const T &data)
{ {
// save primitive - simply dump binary data to output // save primitive - simply dump binary data to output
this->write(&data,sizeof(data)); this->write(static_cast<const void *>(&data), sizeof(data));
} }
template < typename T, typename std::enable_if < std::is_enum<T>::value, int >::type = 0 > template < typename T, typename std::enable_if < std::is_enum<T>::value, int >::type = 0 >
@ -312,7 +312,7 @@ public:
void save(const std::string &data) void save(const std::string &data)
{ {
save(ui32(data.length())); save(ui32(data.length()));
this->write(data.c_str(),(unsigned int)data.size()); this->write(static_cast<const void *>(data.data()), data.size());
} }
template <typename T1, typename T2> template <typename T1, typename T2>
void save(const std::pair<T1,T2> &data) void save(const std::pair<T1,T2> &data)

View File

@ -21,7 +21,7 @@ CLoadFile::CLoadFile(const boost::filesystem::path & fname, ESerializationVersio
//must be instantiated in .cpp file for access to complete types of all member fields //must be instantiated in .cpp file for access to complete types of all member fields
CLoadFile::~CLoadFile() = default; CLoadFile::~CLoadFile() = default;
int CLoadFile::read(void * data, unsigned size) int CLoadFile::read(std::byte * data, unsigned size)
{ {
sfile->read(reinterpret_cast<char *>(data), size); sfile->read(reinterpret_cast<char *>(data), size);
return size; return size;
@ -92,7 +92,7 @@ void CLoadFile::clear()
void CLoadFile::checkMagicBytes(const std::string &text) void CLoadFile::checkMagicBytes(const std::string &text)
{ {
std::string loaded = text; std::string loaded = text;
read((void *)loaded.data(), static_cast<unsigned int>(text.length())); read(reinterpret_cast<std::byte*>(loaded.data()), text.length());
if(loaded != text) if(loaded != text)
throw std::runtime_error("Magic bytes doesn't match!"); throw std::runtime_error("Magic bytes doesn't match!");
} }

View File

@ -23,7 +23,7 @@ public:
CLoadFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion = ESerializationVersion::CURRENT); //throws! CLoadFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion = ESerializationVersion::CURRENT); //throws!
virtual ~CLoadFile(); virtual ~CLoadFile();
int read(void * data, unsigned size) override; //throws! int read(std::byte * data, unsigned size) override; //throws!
void openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion); //throws! void openNextFile(const boost::filesystem::path & fname, ESerializationVersion minimalVersion); //throws!
void clear(); void clear();

View File

@ -12,7 +12,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
int CMemorySerializer::read(void * data, unsigned size) int CMemorySerializer::read(std::byte * data, unsigned size)
{ {
if(buffer.size() < readPos + size) if(buffer.size() < readPos + size)
throw std::runtime_error(boost::str(boost::format("Cannot read past the buffer (accessing index %d, while size is %d)!") % (readPos + size - 1) % buffer.size())); throw std::runtime_error(boost::str(boost::format("Cannot read past the buffer (accessing index %d, while size is %d)!") % (readPos + size - 1) % buffer.size()));
@ -22,7 +22,7 @@ int CMemorySerializer::read(void * data, unsigned size)
return size; return size;
} }
int CMemorySerializer::write(const void * data, unsigned size) int CMemorySerializer::write(const std::byte * data, unsigned size)
{ {
auto oldSize = buffer.size(); //and the pos to write from auto oldSize = buffer.size(); //and the pos to write from
buffer.resize(oldSize + size); buffer.resize(oldSize + size);

View File

@ -25,8 +25,8 @@ public:
BinaryDeserializer iser; BinaryDeserializer iser;
BinarySerializer oser; BinarySerializer oser;
int read(void * data, unsigned size) override; //throws! int read(std::byte * data, unsigned size) override; //throws!
int write(const void * data, unsigned size) override; int write(const std::byte * data, unsigned size) override;
CMemorySerializer(); CMemorySerializer();

View File

@ -21,9 +21,9 @@ CSaveFile::CSaveFile(const boost::filesystem::path &fname)
//must be instantiated in .cpp file for access to complete types of all member fields //must be instantiated in .cpp file for access to complete types of all member fields
CSaveFile::~CSaveFile() = default; CSaveFile::~CSaveFile() = default;
int CSaveFile::write(const void * data, unsigned size) int CSaveFile::write(const std::byte * data, unsigned size)
{ {
sfile->write((char *)data,size); sfile->write(reinterpret_cast<const char *>(data), size);
return size; return size;
} }
@ -66,7 +66,7 @@ void CSaveFile::clear()
void CSaveFile::putMagicBytes(const std::string &text) void CSaveFile::putMagicBytes(const std::string &text)
{ {
write(text.c_str(), static_cast<unsigned int>(text.length())); write(reinterpret_cast<const std::byte*>(text.c_str()), text.length());
} }
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -23,7 +23,7 @@ public:
CSaveFile(const boost::filesystem::path &fname); //throws! CSaveFile(const boost::filesystem::path &fname); //throws!
~CSaveFile(); ~CSaveFile();
int write(const void * data, unsigned size) override; int write(const std::byte * data, unsigned size) override;
void openNextFile(const boost::filesystem::path &fname); //throws! void openNextFile(const boost::filesystem::path &fname); //throws!
void clear(); void clear();

View File

@ -175,14 +175,14 @@ struct VectorizedIDType<CGHeroInstance>
class DLL_LINKAGE IBinaryReader : public virtual CSerializer class DLL_LINKAGE IBinaryReader : public virtual CSerializer
{ {
public: public:
virtual int read(void * data, unsigned size) = 0; virtual int read(std::byte * data, unsigned size) = 0;
}; };
/// Base class for serializers /// Base class for serializers
class DLL_LINKAGE IBinaryWriter : public virtual CSerializer class DLL_LINKAGE IBinaryWriter : public virtual CSerializer
{ {
public: public:
virtual int write(const void * data, unsigned size) = 0; virtual int write(const std::byte * data, unsigned size) = 0;
}; };
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -24,7 +24,7 @@ class DLL_LINKAGE ConnectionPackWriter final : public IBinaryWriter
public: public:
std::vector<std::byte> buffer; std::vector<std::byte> buffer;
int write(const void * data, unsigned size) final; int write(const std::byte * data, unsigned size) final;
}; };
class DLL_LINKAGE ConnectionPackReader final : public IBinaryReader class DLL_LINKAGE ConnectionPackReader final : public IBinaryReader
@ -33,25 +33,21 @@ public:
const std::vector<std::byte> * buffer; const std::vector<std::byte> * buffer;
size_t position; size_t position;
int read(void * data, unsigned size) final; int read(std::byte * data, unsigned size) final;
}; };
int ConnectionPackWriter::write(const void * data, unsigned size) int ConnectionPackWriter::write(const std::byte * data, unsigned size)
{ {
const std::byte * begin_ptr = static_cast<const std::byte *>(data); buffer.insert(buffer.end(), data, data + size);
const std::byte * end_ptr = begin_ptr + size;
buffer.insert(buffer.end(), begin_ptr, end_ptr);
return size; return size;
} }
int ConnectionPackReader::read(void * data, unsigned size) int ConnectionPackReader::read(std::byte * data, unsigned size)
{ {
if (position + size > buffer->size()) if (position + size > buffer->size())
throw std::runtime_error("End of file reached when reading received network pack!"); throw std::runtime_error("End of file reached when reading received network pack!");
std::byte * begin_ptr = static_cast<std::byte *>(data); std::copy_n(buffer->begin() + position, size, data);
std::copy_n(buffer->begin() + position, size, begin_ptr);
position += size; position += size;
return size; return size;
} }

View File

@ -46,7 +46,7 @@ public:
std::string uuid; std::string uuid;
int connectionID; int connectionID;
CConnection(std::weak_ptr<INetworkConnection> networkConnection); explicit CConnection(std::weak_ptr<INetworkConnection> networkConnection);
~CConnection(); ~CConnection();
void sendPack(const CPack * pack); void sendPack(const CPack * pack);

View File

@ -205,10 +205,10 @@ void LobbyServer::onNewConnection(const NetworkConnectionPtr & connection)
void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection, const std::string & errorMessage) void LobbyServer::onDisconnected(const NetworkConnectionPtr & connection, const std::string & errorMessage)
{ {
if (activeAccounts.count(connection)) if(activeAccounts.count(connection))
database->setAccountOnline(activeAccounts.at(connection), false); database->setAccountOnline(activeAccounts.at(connection), false);
if (activeGameRooms.count(connection)) if(activeGameRooms.count(connection))
database->setGameRoomStatus(activeGameRooms.at(connection), LobbyRoomState::CLOSED); database->setGameRoomStatus(activeGameRooms.at(connection), LobbyRoomState::CLOSED);
// NOTE: lost connection can be in only one of these lists (or in none of them) // NOTE: lost connection can be in only one of these lists (or in none of them)