Ticket #3615: serialization.patch

File serialization.patch, 7.3 KB (added by wraitii, 8 years ago)
  • simulation2/Simulation2.cpp

     
    6565    CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) :
    6666        m_SimContext(), m_ComponentManager(m_SimContext, rt),
    6767        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)
    6971    {
    7072        m_SimContext.m_UnitManager = unitManager;
    7173        m_SimContext.m_Terrain = terrain;
     
    8385
    8486    ~CSimulation2Impl()
    8587    {
     88        delete serializationTestTerrain;
     89        delete serializationTestContext;
     90        delete serializationTestComponentManager;
     91       
    8692        UnregisterFileReloadFunc(ReloadChangedFileCB, this);
    8793    }
    8894
     
    145151        std::string hash;
    146152    };
    147153
     154    // Store pointers to the components used by the serialization test.
     155    CTerrain* serializationTestTerrain;
     156    CSimContext* serializationTestContext;
     157    CComponentManager* serializationTestComponentManager;
     158
     159
    148160    void DumpSerializationTestState(SerializationTestState& state, const OsPath& path, const OsPath::String& suffix);
    149161
    150162    void ReportSerializationFailure(
     
    360372
    361373    if (m_EnableSerializationTest)
    362374    {
    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();
    369384        std::set<VfsPath> secondaryLoadedScripts;
    370         ENSURE(LoadDefaultScripts(secondaryComponentManager, &secondaryLoadedScripts));
    371         ResetComponentState(secondaryComponentManager, false, false);
     385            ENSURE(LoadDefaultScripts(*serializationTestComponentManager, &secondaryLoadedScripts));
     386            ResetComponentState(*serializationTestComponentManager, false, false);
    372387
    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 
    383388        // Load the map into the secondary simulation
    384 
    385389        LDR_BeginRegistering();
    386390        CMapReader* mapReader = new CMapReader; // automatically deletes itself
    387391           
     392            JSContext* cx2 = serializationTestComponentManager->GetScriptInterface().GetContext();
     393            JSAutoRequest rq2(cx2);
     394            JS::RootedValue mapSettingsCloned(cx2, serializationTestComponentManager->GetScriptInterface().CloneValueFromOtherContext(scriptInterface, m_MapSettings));
     395
     396           
    388397        std::string mapType;
    389398        scriptInterface.GetProperty(m_InitAttributes, "mapType", mapType);
    390399        if (mapType == "random")
    391400        {
    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
    394407        }
    395408        else
    396409        {
     
    399412
    400413            VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp");
    401414            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 failure
     415                                   serializationTestTerrain, NULL, NULL, NULL, NULL, NULL, NULL,
     416                                   NULL, NULL, serializationTestContext, INVALID_PLAYER, true); // throws exception on failure
    404417        }
     418            // Load the trigger scripts after we have loaded the simulation.
     419            ENSURE(LoadTriggerScripts(*serializationTestComponentManager, mapSettingsCloned, &secondaryLoadedScripts));
    405420
    406421        LDR_EndRegistering();
    407422        ENSURE(LDR_NonprogressiveLoad() == INFO::OK);
     423        }
    408424
    409         ENSURE(secondaryComponentManager.DeserializeState(primaryStateBefore.state));
     425        ENSURE(serializationTestComponentManager->DeserializeState(primaryStateBefore.state));
    410426
    411427        SerializationTestState secondaryStateBefore;
    412         ENSURE(secondaryComponentManager.SerializeState(secondaryStateBefore.state));
     428        ENSURE(serializationTestComponentManager->SerializeState(secondaryStateBefore.state));
    413429        if (serializationTestDebugDump)
    414             ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateBefore.debug, false));
     430            ENSURE(serializationTestComponentManager->DumpDebugState(secondaryStateBefore.debug, false));
    415431        if (serializationTestHash)
    416             ENSURE(secondaryComponentManager.ComputeStateHash(secondaryStateBefore.hash, false));
     432            ENSURE(serializationTestComponentManager->ComputeStateHash(secondaryStateBefore.hash, false));
    417433
    418434        if (primaryStateBefore.state.str() != secondaryStateBefore.state.str() ||
    419435            primaryStateBefore.hash != secondaryStateBefore.hash)
     
    426442        if (serializationTestHash)
    427443            ENSURE(m_ComponentManager.ComputeStateHash(primaryStateAfter.hash, false));
    428444
    429         UpdateComponents(secondaryContext, turnLengthFixed,
    430             CloneCommandsFromOtherContext(scriptInterface, secondaryComponentManager.GetScriptInterface(), commands));
     445        UpdateComponents(*serializationTestContext, turnLengthFixed,
     446            CloneCommandsFromOtherContext(scriptInterface, serializationTestComponentManager->GetScriptInterface(), commands));
     447
    431448        SerializationTestState secondaryStateAfter;
    432         ENSURE(secondaryComponentManager.SerializeState(secondaryStateAfter.state));
     449        ENSURE(serializationTestComponentManager->SerializeState(secondaryStateAfter.state));
    433450        if (serializationTestHash)
    434             ENSURE(secondaryComponentManager.ComputeStateHash(secondaryStateAfter.hash, false));
     451            ENSURE(serializationTestComponentManager->ComputeStateHash(secondaryStateAfter.hash, false));
    435452
    436453        if (primaryStateAfter.state.str() != secondaryStateAfter.state.str() ||
    437454            primaryStateAfter.hash != secondaryStateAfter.hash)
     
    438455        {
    439456            // Only do the (slow) dumping now we know we're going to need to report it
    440457            ENSURE(m_ComponentManager.DumpDebugState(primaryStateAfter.debug, false));
    441             ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateAfter.debug, false));
     458            ENSURE(serializationTestComponentManager->DumpDebugState(secondaryStateAfter.debug, false));
    442459
    443460            ReportSerializationFailure(&primaryStateBefore, &primaryStateAfter, &secondaryStateBefore, &secondaryStateAfter);
    444461        }