Browse Source

Simplified integer serialization code

Ivan Savenko 1 year ago
parent
commit
42f7a48fa4

+ 1 - 1
lib/CGeneralTextHandler.h

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

+ 1 - 1
lib/mapObjects/CGCreature.cpp

@@ -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;
 }
 

+ 1 - 1
lib/mapObjects/CGCreature.h

@@ -35,7 +35,7 @@ public:
 	ArtifactID gainedArtifact; //ID of artifact gained to hero, -1 if none
 	bool neverFlees = false; //if true, the troops will never flee
 	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;
 

+ 8 - 17
lib/serializer/BinaryDeserializer.h

@@ -177,12 +177,9 @@ public:
 		return * this;
 	}
 
-	template< typename IntegerType>
-	void loadEncodedInteger(IntegerType & value)
+	int64_t loadEncodedInteger()
 	{
-		using UnsignedType = std::make_unsigned_t<IntegerType>;
-		UnsignedType valueUnsigned = 0;
-
+		uint64_t valueUnsigned = 0;
 		uint_fast8_t offset = 0;
 
 		for (;;)
@@ -198,18 +195,11 @@ public:
 			else
 			{
 				valueUnsigned |= (byteValue & 0x3f) << offset;
-
-				if constexpr(std::is_signed_v<IntegerType>)
-				{
-					bool isNegative = (byteValue & 0x40) != 0;
-					if (isNegative)
-						value = -valueUnsigned;
-					else
-						value = valueUnsigned;
-				}
+				bool isNegative = (byteValue & 0x40) != 0;
+				if (isNegative)
+					return -static_cast<int64_t>(valueUnsigned);
 				else
-					value = valueUnsigned;
-				return;
+					return valueUnsigned;
 			}
 		}
 	}
@@ -229,8 +219,9 @@ public:
 		}
 		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))
-				loadEncodedInteger(data);
+				data = loadEncodedInteger();
 			else
 				this->read(static_cast<void *>(&data), sizeof(data), reverseEndianness);
 		}

+ 5 - 13
lib/serializer/BinarySerializer.h

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