Fix saveload format negotiation

This commit is contained in:
dP
2021-04-02 15:51:55 +03:00
parent da3cfd2821
commit b0026b697a
4 changed files with 22 additions and 3 deletions

View File

@@ -180,6 +180,21 @@ bool Packet::CanReadFromPacket(uint bytes_to_read)
return true;
}
/* CityMania code begin */
bool Packet::CMCheckRead(uint bytes_to_read)
{
/* Don't allow reading from a quit client/client who send bad data */
if (this->cs->HasClientQuit()) return false;
/* Check if variable is within packet-size */
if (this->pos + bytes_to_read > this->size) {
return false;
}
return true;
}
/* CityMania code end */
/**
* Reads the packet size from the raw packet and stores it in the packet->size
*/

View File

@@ -75,6 +75,7 @@ public:
void PrepareToRead();
bool CanReadFromPacket (uint bytes_to_read);
bool CMCheckRead (uint bytes_to_read); // CityMaina needs check that doesn't close connections
bool Recv_bool ();
uint8 Recv_uint8 ();
uint16 Recv_uint16();

View File

@@ -908,7 +908,7 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
p->Recv_string(name, sizeof(name));
playas = (Owner)p->Recv_uint8();
client_lang = (NetworkLanguage)p->Recv_uint8();
uint8 savegame_formats = p->CanReadFromPacket(1) ? p->Recv_uint8() : 23u /* assume non-modded has everything but zstd */;
uint8 savegame_formats = p->CMCheckRead(1) ? p->Recv_uint8() : 23u /* assume non-modded has everything but zstd */;
if (this->HasClientQuit()) return NETWORK_RECV_STATUS_CONN_LOST;
@@ -917,6 +917,8 @@ NetworkRecvStatus ServerNetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet *p)
if (!preset) return this->SendError(NETWORK_ERROR_NOT_EXPECTED);
this->cm_preset = *preset;
DEBUG(sl, 2, "Using saveload preset '%s:%d' for client %d (mask %d)", this->cm_preset.format->name, (int)this->cm_preset.compression_level, (int)this->client_id, (int)savegame_formats);
/* join another company does not affect these values */
switch (playas) {
case COMPANY_NEW_COMPANY: // New company

View File

@@ -2514,11 +2514,10 @@ std::optional<SavePreset> FindCompatibleSavePreset(const std::string &server_for
*/
uint8 GetAvailableLoadFormats()
{
return 3;
uint8 res = 0;
for(auto &slf : _saveload_formats) {
if (slf.init_load != nullptr) {
res &= (1 << slf.id);
res |= (1 << slf.id);
}
}
return res;
@@ -2859,6 +2858,8 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
}
DEBUG(sl, 2, "Using saveload format '%s'", fmt->name);
_sl.lf = fmt->init_load(_sl.lf);
_sl.reader = new ReadBuffer(_sl.lf);
_next_offs = 0;