Ticket #3292: serializationChange_v2.patch

File serializationChange_v2.patch, 5.1 KB (added by elexis, 8 years ago)

Rebased wraitiis patch starting the serializationtest only after turn N.

  • source/simulation2/Simulation2.cpp

     
    4444#include "ps/Util.h"
    4545#include "ps/XML/Xeromyces.h"
    4646
    4747#include <iomanip>
    4848
     49const int hashStart = 5330;
     50
    4951static std::string Hexify(const std::string& s) // TODO: shouldn't duplicate this function in so many places
    5052{
    5153    std::stringstream str;
    5254    str << std::hex;
    5355    for (size_t i = 0; i < s.size(); ++i)
    class CSimulation2Impl  
    5961{
    6062public:
    6163    CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptRuntime> rt, CTerrain* terrain) :
    6264        m_SimContext(), m_ComponentManager(m_SimContext, rt),
    6365        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)
    6567    {
    6668        m_SimContext.m_UnitManager = unitManager;
    6769        m_SimContext.m_Terrain = terrain;
    6870        m_ComponentManager.LoadComponentTypes();
    6971
    public:  
    7880
    7981        m_OOSLogPath = getDateIndexSubdirectory(psLogDir() / "oos_logs");
    8082
    8183        if (m_EnableOOSLog)
    8284            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();
    8389    }
    8490
    8591    ~CSimulation2Impl()
    8692    {
    8793        UnregisterFileReloadFunc(ReloadChangedFileCB, this);
    public:  
    133139    uint32_t m_TurnNumber;
    134140
    135141    bool m_EnableOOSLog;
    136142    OsPath m_OOSLogPath;
    137143
     144    // Initialise the secondary simulation
     145    CTerrain secondaryTerrain;
     146    CSimContext secondaryContext;
     147    CComponentManager secondaryComponentManager;
     148    std::set<VfsPath> secondaryLoadedScripts;
     149
    138150    // Functions and data for the serialization test mode: (see Update() for relevant comments)
    139151
    140152    bool m_EnableSerializationTest;
    141153
    142154    struct SerializationTestState
    void CSimulation2Impl::Update(int turnLe  
    346358    const bool serializationTestHash = true; // set true to save and compare hash of state
    347359
    348360    SerializationTestState primaryStateBefore;
    349361    ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface();
    350362
    351     if (m_EnableSerializationTest)
     363    if (m_EnableSerializationTest && m_TurnNumber == hashStart)
     364        ENSURE(LoadDefaultScripts(secondaryComponentManager, &secondaryLoadedScripts));
     365
     366    if (m_TurnNumber >= hashStart)
    352367    {
    353368        ENSURE(m_ComponentManager.SerializeState(primaryStateBefore.state));
    354369        if (serializationTestDebugDump)
    355370            ENSURE(m_ComponentManager.DumpDebugState(primaryStateBefore.debug, false));
    356371        if (serializationTestHash)
    void CSimulation2Impl::Update(int turnLe  
    360375    UpdateComponents(m_SimContext, turnLengthFixed, commands);
    361376
    362377
    363378    if (m_EnableSerializationTest)
    364379    {
    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        {
    373382        ResetComponentState(secondaryComponentManager, false, false);
    374383
    375384        // Load the trigger scripts after we have loaded the simulation.
    376385        {
    377386            JSContext* cx2 = secondaryComponentManager.GetScriptInterface().GetContext();
    void CSimulation2Impl::Update(int turnLe  
    405414                NULL, NULL, &secondaryContext, INVALID_PLAYER, true); // throws exception on failure
    406415        }
    407416
    408417        LDR_EndRegistering();
    409418        ENSURE(LDR_NonprogressiveLoad() == INFO::OK);
    410 
    411419        ENSURE(secondaryComponentManager.DeserializeState(primaryStateBefore.state));
    412 
     420        }
     421        if (m_TurnNumber >= hashStart)
     422        {
    413423        SerializationTestState secondaryStateBefore;
    414424        ENSURE(secondaryComponentManager.SerializeState(secondaryStateBefore.state));
    415425        if (serializationTestDebugDump)
    416426            ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateBefore.debug, false));
    417427        if (serializationTestHash)
    void CSimulation2Impl::Update(int turnLe  
    443453            ENSURE(secondaryComponentManager.DumpDebugState(secondaryStateAfter.debug, false));
    444454
    445455            ReportSerializationFailure(&primaryStateBefore, &primaryStateAfter, &secondaryStateBefore, &secondaryStateAfter);
    446456        }
    447457    }
     458    }
    448459
    449460    // Run the GC occasionally
    450461    // No delay because a lot of garbage accumulates in one turn and in non-visual replays there are
    451462    // much more turns in the same time than in normal games.
    452463    // Every 500 turns we run a shrinking GC, which decommits unused memory and frees all JIT code.
    void CSimulation2Impl::Update(int turnLe  
    459470    if (m_TurnNumber % 500 == 0)
    460471        scriptInterface.GetRuntime()->ShrinkingGC();
    461472    else
    462473        scriptInterface.GetRuntime()->MaybeIncrementalGC(0.0f);
    463474
    464     if (m_EnableOOSLog)
     475    if (m_EnableOOSLog && m_TurnNumber >= hashStart)
    465476        DumpState();
    466477
    467478    // Start computing AI for the next turn
    468479    CmpPtr<ICmpAIManager> cmpAIManager(m_SimContext, SYSTEM_ENTITY);
    469480    if (cmpAIManager)