Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gameengine): prevent save corruption after supply center loss

T0T0W9ebede602616
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c74a73b16..c7cf508f3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,11 @@ found and fixed — EA's own, not port damage.**
---
+## Saving after a supply center is destroyed
+
+- Destroyed supply centers could corrupt the saved supply lists and prevent the game from loading
+ that save. They are now removed from the correct list, so later saves remain readable.
+
## Military priority when selecting a group
- Drag a box around military units and workers together and only the military units are selected,
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h b/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h
index 210aae5ae..55b5dcec4 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/ResourceGatheringManager.h
@@ -58,6 +58,8 @@ public:
protected:
+ Object *findBestSupplyCenterInList(Object *queryObject);
+
// snapshot methods
virtual void crc( Xfer *xfer );
virtual void xfer( Xfer *xfer );
@@ -73,4 +75,4 @@ private:
};
-#endif
\ No newline at end of file
+#endif
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp
index 1eee4168b..c375a2f51 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp
@@ -199,8 +199,6 @@ Object *ResourceGatheringManager::findBestSupplyWarehouse( Object *queryObject )
Object *ResourceGatheringManager::findBestSupplyCenter( Object *queryObject )
{
- Object *bestCenter = NULL;
-
if( ( queryObject == NULL ) || ( queryObject->getAI() == NULL ) )
return NULL;
@@ -224,6 +222,12 @@ Object *ResourceGatheringManager::findBestSupplyCenter( Object *queryObject )
}
//Otherwise, search for a good one.
+ return findBestSupplyCenterInList(queryObject);
+}
+
+Object *ResourceGatheringManager::findBestSupplyCenterInList(Object *queryObject)
+{
+ Object *bestCenter = NULL;
Real bestCost = FLT_MAX;
objectIDListIterator iterator = m_supplyCenters.begin();
@@ -234,7 +238,7 @@ Object *ResourceGatheringManager::findBestSupplyCenter( Object *queryObject )
if( currentCenter == NULL )
{
- iterator = m_supplyWarehouses.erase( iterator );
+ iterator = m_supplyCenters.erase( iterator );
}
else
{
@@ -288,4 +292,3 @@ void ResourceGatheringManager::loadPostProcess( void )
{
} // end loadPostProcess
-
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 8f206e79d..409952fb4 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -12049,3 +12049,4 @@ TEST(camera_preferences_default_to_a_finite_map_margin)
#include "test_production_input.inc"
#include "test_minimap_input.inc"
#include "test_selection_priority.inc"
+#include "test_supply_center_save.inc"
diff --git a/GeneralsMD/Code/Tests/test_supply_center_save.inc b/GeneralsMD/Code/Tests/test_supply_center_save.inc
new file mode 100644
index 000000000..6ffa6b509
--- /dev/null
+++ b/GeneralsMD/Code/Tests/test_supply_center_save.inc
@@ -0,0 +1,88 @@
+#include "Common/ResourceGatheringManager.h"
+#include "Common/XferLoad.h"
+#include "Common/XferSave.h"
+
+namespace
+{
+class SupplyCenterSaveManager : public ResourceGatheringManager
+{
+public:
+ void transfer(Xfer *xfer) { ResourceGatheringManager::xfer(xfer); }
+ Object *search() { return findBestSupplyCenterInList(NULL); }
+};
+
+struct SupplyCenterSaveLogic
+{
+ GameLogic *saved;
+ SupplyCenterSaveLogic() : saved(TheGameLogic)
+ {
+ // GameLogic destruction owns shared globals; keep this empty lookup alive with boot scaffolding.
+ static GameLogic *emptyWorld = NEW GameLogic;
+ TheGameLogic = emptyWorld;
+ }
+ ~SupplyCenterSaveLogic() { TheGameLogic = saved; }
+};
+}
+
+TEST(supply_center_cleanup_keeps_saved_counts_and_lists_consistent)
+{
+ CHECK(bootOnce());
+ SupplyCenterSaveLogic world;
+ const char *path = "test_supply_center_save_tmp.bin";
+ // Exercise both ordinary warehouses and the empty-list underflow case.
+ for (Int warehouseCount = 0; warehouseCount <= 2; warehouseCount += 2)
+ {
+ std::list<ObjectID> warehouses;
+ for (Int i = 0; i < warehouseCount; ++i)
+ warehouses.push_back((ObjectID)(11 + i));
+ std::list<ObjectID> destroyedCenters;
+ destroyedCenters.push_back((ObjectID)91);
+ destroyedCenters.push_back((ObjectID)92);
+ destroyedCenters.push_back((ObjectID)93);
+
+ XferSave write;
+ write.open(AsciiString(path));
+ XferVersion version = 1;
+ write.xferVersion(&version, 1);
+ write.xferSTLObjectIDList(&warehouses);
+ write.xferSTLObjectIDList(&destroyedCenters);
+ write.close();
+
+ SupplyCenterSaveManager manager;
+ XferLoad read;
+ read.open(AsciiString(path));
+ manager.transfer(&read);
+ read.close();
+ CHECK(manager.search() == NULL);
+ write.open(AsciiString(path));
+ manager.transfer(&write);
+ write.close();
+
+ // The old erase removed center nodes but changed the warehouse count instead.
+ // Inspect the wire counts before attempting a reload so that underflow fails cleanly.
+ FILE *fp = fopen(path, "rb");
+ CHECK(fp != NULL);
+ if (!fp) return;
+ UnsignedByte bytes[64] = {};
+ const size_t length = fread(bytes, 1, sizeof(bytes), fp);
+ fclose(fp);
+ const size_t centerHeader = 4 + warehouseCount * sizeof(ObjectID);
+ CHECK(length == centerHeader + 3);
+ const Bool countsValid = bytes[2] == warehouseCount && bytes[3] == 0 &&
+ bytes[centerHeader + 1] == 0 && bytes[centerHeader + 2] == 0;
+ CHECK(countsValid);
+ if (countsValid)
+ {
+ std::list<ObjectID> restoredWarehouses;
+ std::list<ObjectID> restoredCenters;
+ read.open(AsciiString(path));
+ read.xferVersion(&version, 1);
+ read.xferSTLObjectIDList(&restoredWarehouses);
+ read.xferSTLObjectIDList(&restoredCenters);
+ read.close();
+ CHECK(restoredWarehouses == warehouses);
+ CHECK(restoredCenters.empty());
+ }
+ remove(path);
+ }
+}