Ticket #3615: serialization.patch
File serialization.patch, 7.3 KB (added by , 8 years ago) |
---|
-
simulation2/Simulation2.cpp
65 65 CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) : 66 66 m_SimContext(), m_ComponentManager(m_SimContext, rt), 67 67 m_EnableOOSLog(false), m_EnableSerializationTest(false), 68 m_MapSettings(rt->m_rt), m_InitAttributes(rt->m_rt) 68 m_MapSettings(rt->m_rt), m_InitAttributes(rt->m_rt), 69 serializationTestTerrain(NULL), serializationTestContext(NULL), 70 serializationTestComponentManager(NULL) 69 71 { 70 72 m_SimContext.m_UnitManager = unitManager; 71 73 m_SimContext.m_Terrain = terrain; … … 83 85 84 86 ~CSimulation2Impl() 85 87 { 88 delete serializationTestTerrain; 89 delete serializationTestContext; 90 delete serializationTestComponentManager; 91 86 92 UnregisterFileReloadFunc(ReloadChangedFileCB, this); 87 93 } 88 94 … … 145 151 std::string hash; 146 152 }; 147 153 154 // Store pointers to the components used by the serialization test. 155 CTerrain* serializationTestTerrain; 156 CSimContext* serializationTestContext; 157 CComponentManager* serializationTestComponentManager; 158 159 148 160 void DumpSerializationTestState(SerializationTestState& state, const OsPath& path, const OsPath::String& suffix); 149 161 150 162 void ReportSerializationFailure( … … 360 372 361 373 if (m_EnableSerializationTest) 362 374 { 363 // Initialise the secondary simulation 364 CTerrain secondaryTerrain; 365 CSimContext secondaryContext; 366 secondaryContext.m_Terrain = &secondaryTerrain; 367 CComponentManager secondaryComponentManager(secondaryContext, scriptInterface.GetRuntime()); 368 secondaryComponentManager.LoadComponentTypes(); 375 // Initialize the serialization test sim the first time 376 // This avoids creating a map every time. 377 if (!serializationTestComponentManager) 378 { 379 serializationTestTerrain = new CTerrain; 380 serializationTestContext = new CSimContext; 381 serializationTestContext->m_Terrain = serializationTestTerrain; 382 serializationTestComponentManager = new CComponentManager(*serializationTestContext, scriptInterface.GetRuntime()); 383 serializationTestComponentManager->LoadComponentTypes(); 369 384 std::set<VfsPath> secondaryLoadedScripts; 370 ENSURE(LoadDefaultScripts(secondaryComponentManager, &secondaryLoadedScripts));371 ResetComponentState(secondaryComponentManager, false, false);385 ENSURE(LoadDefaultScripts(*serializationTestComponentManager, &secondaryLoadedScripts)); 386 ResetComponentState(*serializationTestComponentManager, false, false); 372 387 373 // Load the trigger scripts after we have loaded the simulation.374 {375 JSContext* cx2 = secondaryComponentManager.GetScriptInterface().GetContext();376 JSAutoRequest rq2(cx2);377 JS::RootedValue mapSettingsCloned(cx2,378 secondaryComponentManager.GetScriptInterface().CloneValueFromOtherContext(379 scriptInterface, m_MapSettings));380 ENSURE(LoadTriggerScripts(secondaryComponentManager, mapSettingsCloned, &secondaryLoadedScripts));381 }382 383 388 // Load the map into the secondary simulation 384 385 389 LDR_BeginRegistering(); 386 390 CMapReader* mapReader = new CMapReader; // automatically deletes itself 387 391 392 JSContext* cx2 = serializationTestComponentManager->GetScriptInterface().GetContext(); 393 JSAutoRequest rq2(cx2); 394 JS::RootedValue mapSettingsCloned(cx2, serializationTestComponentManager->GetScriptInterface().CloneValueFromOtherContext(scriptInterface, m_MapSettings)); 395 396 388 397 std::string mapType; 389 398 scriptInterface.GetProperty(m_InitAttributes, "mapType", mapType); 390 399 if (mapType == "random") 391 400 { 392 // TODO: support random map scripts 393 debug_warn(L"Serialization test mode does not support random maps"); 401 std::wstring scriptFile; 402 scriptInterface.GetProperty(m_InitAttributes, "script", scriptFile); 403 404 mapReader->LoadRandomMap(scriptFile, scriptInterface.GetJSRuntime(), mapSettingsCloned, 405 serializationTestTerrain, NULL, NULL, NULL, NULL, NULL, NULL, 406 NULL, g_Game->GetSimulation2(), INVALID_PLAYER, true); // throws exception on failure 394 407 } 395 408 else 396 409 { … … 399 412 400 413 VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp"); 401 414 mapReader->LoadMap(mapfilename, scriptInterface.GetJSRuntime(), JS::UndefinedHandleValue, 402 &secondaryTerrain, NULL, NULL, NULL, NULL, NULL, NULL,403 NULL, NULL, &secondaryContext, INVALID_PLAYER, true); // throws exception on failure415 serializationTestTerrain, NULL, NULL, NULL, NULL, NULL, NULL, 416 NULL, NULL, serializationTestContext, INVALID_PLAYER, true); // throws exception on failure 404 417 } 418 // Load the trigger scripts after we have loaded the simulation. 419 ENSURE(LoadTriggerScripts(*serializationTestComponentManager, mapSettingsCloned, &secondaryLoadedScripts)); 405 420 406 421 LDR_EndRegistering(); 407 422 ENSURE(LDR_NonprogressiveLoad() == INFO::OK); 423 } 408 424 409 ENSURE(se condaryComponentManager.DeserializeState(primaryStateBefore.state));425 ENSURE(serializationTestComponentManager->DeserializeState(primaryStateBefore.state)); 410 426 411 427 SerializationTestState secondaryStateBefore; 412 ENSURE(se condaryComponentManager.SerializeState(secondaryStateBefore.state));428 ENSURE(serializationTestComponentManager->SerializeState(secondaryStateBefore.state)); 413 429 if (serializationTestDebugDump) 414 ENSURE(se condaryComponentManager.DumpDebugState(secondaryStateBefore.debug, false));430 ENSURE(serializationTestComponentManager->DumpDebugState(secondaryStateBefore.debug, false)); 415 431 if (serializationTestHash) 416 ENSURE(se condaryComponentManager.ComputeStateHash(secondaryStateBefore.hash, false));432 ENSURE(serializationTestComponentManager->ComputeStateHash(secondaryStateBefore.hash, false)); 417 433 418 434 if (primaryStateBefore.state.str() != secondaryStateBefore.state.str() || 419 435 primaryStateBefore.hash != secondaryStateBefore.hash) … … 426 442 if (serializationTestHash) 427 443 ENSURE(m_ComponentManager.ComputeStateHash(primaryStateAfter.hash, false)); 428 444 429 UpdateComponents(secondaryContext, turnLengthFixed, 430 CloneCommandsFromOtherContext(scriptInterface, secondaryComponentManager.GetScriptInterface(), commands)); 445 UpdateComponents(*serializationTestContext, turnLengthFixed, 446 CloneCommandsFromOtherContext(scriptInterface, serializationTestComponentManager->GetScriptInterface(), commands)); 447 431 448 SerializationTestState secondaryStateAfter; 432 ENSURE(se condaryComponentManager.SerializeState(secondaryStateAfter.state));449 ENSURE(serializationTestComponentManager->SerializeState(secondaryStateAfter.state)); 433 450 if (serializationTestHash) 434 ENSURE(se condaryComponentManager.ComputeStateHash(secondaryStateAfter.hash, false));451 ENSURE(serializationTestComponentManager->ComputeStateHash(secondaryStateAfter.hash, false)); 435 452 436 453 if (primaryStateAfter.state.str() != secondaryStateAfter.state.str() || 437 454 primaryStateAfter.hash != secondaryStateAfter.hash) … … 438 455 { 439 456 // Only do the (slow) dumping now we know we're going to need to report it 440 457 ENSURE(m_ComponentManager.DumpDebugState(primaryStateAfter.debug, false)); 441 ENSURE(se condaryComponentManager.DumpDebugState(secondaryStateAfter.debug, false));458 ENSURE(serializationTestComponentManager->DumpDebugState(secondaryStateAfter.debug, false)); 442 459 443 460 ReportSerializationFailure(&primaryStateBefore, &primaryStateAfter, &secondaryStateBefore, &secondaryStateAfter); 444 461 }