1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-21 21:17:49 +02:00

Simplified integer serialization code

This commit is contained in:
Ivan Savenko 2024-05-16 18:53:49 +00:00
parent 84bc6c42db
commit 42f7a48fa4
5 changed files with 16 additions and 33 deletions

View File

@ -197,7 +197,7 @@ public:
void serialize(Handler & h) void serialize(Handler & h)
{ {
std::string key; std::string key;
auto sz = stringsLocalizations.size(); int64_t sz = stringsLocalizations.size();
h & sz; h & sz;
if(h.saving) if(h.saving)
{ {

View File

@ -270,7 +270,7 @@ void CGCreature::initObj(CRandomGenerator & rand)
} }
} }
temppower = stacks[SlotID(0)]->count * static_cast<ui64>(1000); temppower = stacks[SlotID(0)]->count * static_cast<int64_t>(1000);
refusedJoining = false; refusedJoining = false;
} }

View File

@ -35,7 +35,7 @@ public:
ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
bool neverFlees = false; //if true, the troops will never flee bool neverFlees = false; //if true, the troops will never flee
bool notGrowingTeam = false; //if true, number of units won't grow bool notGrowingTeam = false; //if true, number of units won't grow
ui64 temppower = 0; //used to handle fractional stack growth for tiny stacks int64_t temppower = 0; //used to handle fractional stack growth for tiny stacks
bool refusedJoining = false; bool refusedJoining = false;

View File

@ -177,12 +177,9 @@ public:
return * this; return * this;
} }
template< typename IntegerType> int64_t loadEncodedInteger()
void loadEncodedInteger(IntegerType & value)
{ {
using UnsignedType = std::make_unsigned_t<IntegerType>; uint64_t valueUnsigned = 0;
UnsignedType valueUnsigned = 0;
uint_fast8_t offset = 0; uint_fast8_t offset = 0;
for (;;) for (;;)
@ -198,18 +195,11 @@ public:
else else
{ {
valueUnsigned |= (byteValue & 0x3f) << offset; valueUnsigned |= (byteValue & 0x3f) << offset;
bool isNegative = (byteValue & 0x40) != 0;
if constexpr(std::is_signed_v<IntegerType>) if (isNegative)
{ return -static_cast<int64_t>(valueUnsigned);
bool isNegative = (byteValue & 0x40) != 0;
if (isNegative)
value = -valueUnsigned;
else
value = valueUnsigned;
}
else else
value = valueUnsigned; return valueUnsigned;
return;
} }
} }
} }
@ -229,8 +219,9 @@ public:
} }
else else
{ {
static_assert(!std::is_same_v<uint64_t, T>, "Serialization of unsigned 64-bit value may not work in some cases");
if (hasFeature(Version::COMPACT_INTEGER_SERIALIZATION)) if (hasFeature(Version::COMPACT_INTEGER_SERIALIZATION))
loadEncodedInteger(data); data = loadEncodedInteger();
else else
this->read(static_cast<void *>(&data), sizeof(data), reverseEndianness); this->read(static_cast<void *>(&data), sizeof(data), reverseEndianness);
} }

View File

@ -141,15 +141,9 @@ public:
return * this; return * this;
} }
template< typename IntegerType> void saveEncodedInteger(int64_t value)
void saveEncodedInteger(const IntegerType & value)
{ {
using UnsignedType = std::make_unsigned_t<IntegerType>; uint64_t valueUnsigned = std::abs(value);
UnsignedType valueUnsigned;
if constexpr(std::is_signed_v<IntegerType>)
valueUnsigned = std::abs(value);
else
valueUnsigned = value;
while (valueUnsigned > 0x3f) while (valueUnsigned > 0x3f)
{ {
@ -159,11 +153,9 @@ public:
} }
uint8_t lastByteValue = valueUnsigned & 0x3f; uint8_t lastByteValue = valueUnsigned & 0x3f;
if constexpr(std::is_signed_v<IntegerType>) if (value < 0)
{ lastByteValue |= 0x40;
if (value < 0)
lastByteValue |= 0x40;
}
save(lastByteValue); save(lastByteValue);
} }