Ticket #3292: serializationChange_v2.patch
File serializationChange_v2.patch, 5.1 KB (added by , 8 years ago) |
---|
-
source/simulation2/Simulation2.cpp
44 44 #include "ps/Util.h" 45 45 #include "ps/XML/Xeromyces.h" 46 46 47 47 #include <iomanip> 48 48 49 const int hashStart = 5330; 50 49 51 static std::string Hexify(const std::string& s) // TODO: shouldn't duplicate this function in so many places 50 52 { 51 53 std::stringstream str; 52 54 str << std::hex; 53 55 for (size_t i = 0; i < s.size(); ++i) … … class CSimulation2Impl 59 61 { 60 62 public: 61 63 CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) : 62 64 m_SimContext(), m_ComponentManager(m_SimContext, rt), 63 65 m_EnableOOSLog(false), m_EnableSerializationTest(false), 64 m_MapSettings(rt->m_rt), m_InitAttributes(rt->m_rt) 66 m_MapSettings(rt->m_rt), m_InitAttributes(rt->m_rt), secondaryComponentManager(secondaryContext, rt) 65 67 { 66 68 m_SimContext.m_UnitManager = unitManager; 67 69 m_SimContext.m_Terrain = terrain; 68 70 m_ComponentManager.LoadComponentTypes(); 69 71 … … public: 78 80 79 81 m_OOSLogPath = getDateIndexSubdirectory(psLogDir() / "oos_logs"); 80 82 81 83 if (m_EnableOOSLog) 82 84 debug_printf("Writing ooslogs to %s\n", m_OOSLogPath.string8().c_str()); 85 86 // Initialise the secondary simulation 87 secondaryContext.m_Terrain = &secondaryTerrain; 88 secondaryComponentManager.LoadComponentTypes(); 83 89 } 84 90 85 91 ~CSimulation2Impl() 86 92 { 87 93 UnregisterFileReloadFunc(ReloadChangedFileCB, this); … … public: 133 139 uint32_t m_TurnNumber; 134 140 135 141 bool m_EnableOOSLog; 136 142 OsPath m_OOSLogPath; 137 143 144 // Initialise the secondary simulation 145 CTerrain secondaryTerrain; 146 CSimContext secondaryContext; 147 CComponentManager secondaryComponentManager; 148 std::set<VfsPath> secondaryLoadedScripts; 149 138 150 // Functions and data for the serialization test mode: (see Update() for relevant comments) 139 151 140 152 bool m_EnableSerializationTest; 141 153 142 154 struct SerializationTestState … … void CSimulation2Impl::Update(int turnLe 346 358 const bool serializationTestHash = true; // set true to save and compare hash of state 347 359 348 360 SerializationTestState primaryStateBefore; 349 361 ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); 350 362 351 if (m_EnableSerializationTest) 363 if (m_EnableSerializationTest && m_TurnNumber == hashStart) 364 ENSURE(LoadDefaultScripts(secondaryComponentManager, &secondaryLoadedScripts)); 365 366 if (m_TurnNumber >= hashStart) 352 367 { 353 368 ENSURE(m_ComponentManager.SerializeState(primaryStateBefore.state)); 354 369 if (serializationTestDebugDump) 355 370 ENSURE(m_ComponentManager.DumpDebugState(primaryStateBefore.debug, false)); 356 371 if (serializationTestHash) … … void CSimulation2Impl::Update(int turnLe 360 375 UpdateComponents(m_SimContext, turnLengthFixed, commands); 361 376 362 377 363 378 if (m_EnableSerializationTest) 364 379 { 365 // Initialise the secondary simulation 366 CTerrain secondaryTerrain; 367 CSimContext secondaryContext; 368 secondaryContext.m_Terrain = &secondaryTerrain; 369 CComponentManager secondaryComponentManager(secondaryContext, scriptInterface.GetRuntime()); 370 secondaryComponentManager.LoadComponentTypes(); 371 std::set<VfsPath> secondaryLoadedScripts; 372 ENSURE(LoadDefaultScripts(secondaryComponentManager, &secondaryLoadedScripts)); 380 if (m_TurnNumber == hashStart) 381 { 373 382 ResetComponentState(secondaryComponentManager, false, false); 374 383 375 384 // Load the trigger scripts after we have loaded the simulation. 376 385 { 377 386 JSContext* cx2 = secondaryComponentManager.GetScriptInterface().GetContext(); … … void CSimulation2Impl::Update(int turnLe 405 414 NULL, NULL, &secondaryContext, INVALID_PLAYER, true); // throws exception on failure 406 415 } 407 416 408 417 LDR_EndRegistering(); 409 418 ENSURE(LDR_NonprogressiveLoad() == INFO::OK); 410 411 419 ENSURE(secondaryComponentManager.DeserializeState(primaryStateBefore.state)); 412 420 } 421 if (m_TurnNumber >= hashStart) 422 { 413 423 SerializationTestState secondaryStateBefore; 414 424 ENSURE(secondaryComponentManager.SerializeState(secondaryStateBefore.state)); 415 425 if (serializationTestDebugDump) 416 426 ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateBefore.debug, false)); 417 427 if (serializationTestHash) … … void CSimulation2Impl::Update(int turnLe 443 453 ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateAfter.debug, false)); 444 454 445 455 ReportSerializationFailure(&primaryStateBefore, &primaryStateAfter, &secondaryStateBefore, &secondaryStateAfter); 446 456 } 447 457 } 458 } 448 459 449 460 // Run the GC occasionally 450 461 // No delay because a lot of garbage accumulates in one turn and in non-visual replays there are 451 462 // much more turns in the same time than in normal games. 452 463 // Every 500 turns we run a shrinking GC, which decommits unused memory and frees all JIT code. … … void CSimulation2Impl::Update(int turnLe 459 470 if (m_TurnNumber % 500 == 0) 460 471 scriptInterface.GetRuntime()->ShrinkingGC(); 461 472 else 462 473 scriptInterface.GetRuntime()->MaybeIncrementalGC(0.0f); 463 474 464 if (m_EnableOOSLog )475 if (m_EnableOOSLog && m_TurnNumber >= hashStart) 465 476 DumpState(); 466 477 467 478 // Start computing AI for the next turn 468 479 CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY); 469 480 if (cmpAIManager)