Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/Simulation2.cpp
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | |||||
#include "ps/Util.h" | #include "ps/Util.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#include <iomanip> | #include <iomanip> | ||||
class CSimulation2Impl | class CSimulation2Impl | ||||
{ | { | ||||
public: | public: | ||||
CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptContext> rt, CTerrain* terrain) : | CSimulation2Impl(CUnitManager* unitManager, shared_ptr<ScriptContext> cx, CTerrain* terrain) : | ||||
m_SimContext(), m_ComponentManager(m_SimContext, rt), | m_SimContext(), m_ComponentManager(m_SimContext, cx), | ||||
m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false), | m_EnableOOSLog(false), m_EnableSerializationTest(false), m_RejoinTestTurn(-1), m_TestingRejoin(false), | ||||
m_SecondaryTerrain(nullptr), m_SecondaryContext(nullptr), m_SecondaryComponentManager(nullptr), m_SecondaryLoadedScripts(nullptr), | m_SecondaryTerrain(nullptr), m_SecondaryContext(nullptr), m_SecondaryComponentManager(nullptr), m_SecondaryLoadedScripts(nullptr), | ||||
m_MapSettings(rt->GetJSRuntime()), m_InitAttributes(rt->GetJSRuntime()) | m_MapSettings(cx->GetGeneralJSContext()), m_InitAttributes(cx->GetGeneralJSContext()) | ||||
{ | { | ||||
m_SimContext.m_UnitManager = unitManager; | m_SimContext.m_UnitManager = unitManager; | ||||
m_SimContext.m_Terrain = terrain; | m_SimContext.m_Terrain = terrain; | ||||
m_ComponentManager.LoadComponentTypes(); | m_ComponentManager.LoadComponentTypes(); | ||||
RegisterFileReloadFunc(ReloadChangedFileCB, this); | RegisterFileReloadFunc(ReloadChangedFileCB, this); | ||||
// Tests won't have config initialised | // Tests won't have config initialised | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | public: | ||||
void ReportSerializationFailure( | void ReportSerializationFailure( | ||||
SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, | SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, | ||||
SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter); | SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter); | ||||
void InitRNGSeedSimulation(); | void InitRNGSeedSimulation(); | ||||
void InitRNGSeedAI(); | void InitRNGSeedAI(); | ||||
static std::vector<SimulationCommand> CloneCommandsFromOtherContext(const ScriptInterface& oldScript, const ScriptInterface& newScript, | static std::vector<SimulationCommand> CloneCommandsFromOtherCompartment(const ScriptInterface& oldScript, const ScriptInterface& newScript, | ||||
const std::vector<SimulationCommand>& commands) | const std::vector<SimulationCommand>& commands) | ||||
{ | { | ||||
std::vector<SimulationCommand> newCommands; | std::vector<SimulationCommand> newCommands; | ||||
newCommands.reserve(commands.size()); | newCommands.reserve(commands.size()); | ||||
ScriptRequest rqNew(newScript); | ScriptRequest rqNew(newScript); | ||||
for (const SimulationCommand& command : commands) | for (const SimulationCommand& command : commands) | ||||
{ | { | ||||
JS::RootedValue tmpCommand(rqNew.cx, newScript.CloneValueFromOtherCompartment(oldScript, command.data)); | JS::RootedValue tmpCommand(rqNew.cx, newScript.CloneValueFromOtherCompartment(oldScript, command.data, true)); | ||||
newScript.FreezeObject(tmpCommand, true); | newScript.FreezeObject(tmpCommand, true); | ||||
SimulationCommand cmd(command.player, rqNew.cx, tmpCommand); | SimulationCommand cmd(command.player, rqNew.cx, tmpCommand); | ||||
newCommands.emplace_back(std::move(cmd)); | newCommands.emplace_back(std::move(cmd)); | ||||
} | } | ||||
return newCommands; | return newCommands; | ||||
} | } | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 234 Lines • ▼ Show 20 Lines | if (m_EnableSerializationTest || startRejoinTest) | ||||
ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts)); | ENSURE(LoadDefaultScripts(*m_SecondaryComponentManager, m_SecondaryLoadedScripts)); | ||||
ResetComponentState(*m_SecondaryComponentManager, false, false); | ResetComponentState(*m_SecondaryComponentManager, false, false); | ||||
// Load the trigger scripts after we have loaded the simulation. | // Load the trigger scripts after we have loaded the simulation. | ||||
{ | { | ||||
ScriptRequest rq2(m_SecondaryComponentManager->GetScriptInterface()); | ScriptRequest rq2(m_SecondaryComponentManager->GetScriptInterface()); | ||||
JS::RootedValue mapSettingsCloned(rq2.cx, | JS::RootedValue mapSettingsCloned(rq2.cx, | ||||
m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherCompartment( | m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherCompartment( | ||||
scriptInterface, m_MapSettings)); | scriptInterface, m_MapSettings, true)); | ||||
ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts)); | ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts)); | ||||
} | } | ||||
// Load the map into the secondary simulation | // Load the map into the secondary simulation | ||||
LDR_BeginRegistering(); | LDR_BeginRegistering(); | ||||
std::unique_ptr<CMapReader> mapReader(new CMapReader); | std::unique_ptr<CMapReader> mapReader(new CMapReader); | ||||
std::string mapType; | std::string mapType; | ||||
scriptInterface.GetProperty(m_InitAttributes, "mapType", mapType); | scriptInterface.GetProperty(m_InitAttributes, "mapType", mapType); | ||||
if (mapType == "random") | if (mapType == "random") | ||||
{ | { | ||||
// TODO: support random map scripts | // TODO: support random map scripts | ||||
debug_warn(L"Serialization test mode does not support random maps"); | debug_warn(L"Serialization test mode does not support random maps"); | ||||
} | } | ||||
else | else | ||||
{ | { | ||||
std::wstring mapFile; | std::wstring mapFile; | ||||
scriptInterface.GetProperty(m_InitAttributes, "map", mapFile); | scriptInterface.GetProperty(m_InitAttributes, "map", mapFile); | ||||
VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp"); | VfsPath mapfilename = VfsPath(mapFile).ChangeExtension(L".pmp"); | ||||
mapReader->LoadMap(mapfilename, scriptInterface.GetJSRuntime(), JS::UndefinedHandleValue, | mapReader->LoadMap(mapfilename, *scriptInterface.GetContext(), JS::UndefinedHandleValue, | ||||
m_SecondaryTerrain, NULL, NULL, NULL, NULL, NULL, NULL, | m_SecondaryTerrain, NULL, NULL, NULL, NULL, NULL, NULL, | ||||
NULL, NULL, m_SecondaryContext, INVALID_PLAYER, true); // throws exception on failure | NULL, NULL, m_SecondaryContext, INVALID_PLAYER, true); // throws exception on failure | ||||
} | } | ||||
LDR_EndRegistering(); | LDR_EndRegistering(); | ||||
ENSURE(LDR_NonprogressiveLoad() == INFO::OK); | ENSURE(LDR_NonprogressiveLoad() == INFO::OK); | ||||
ENSURE(m_SecondaryComponentManager->DeserializeState(primaryStateBefore.state)); | ENSURE(m_SecondaryComponentManager->DeserializeState(primaryStateBefore.state)); | ||||
} | } | ||||
Show All 14 Lines | if (m_EnableSerializationTest || m_TestingRejoin) | ||||
} | } | ||||
SerializationTestState primaryStateAfter; | SerializationTestState primaryStateAfter; | ||||
ENSURE(m_ComponentManager.SerializeState(primaryStateAfter.state)); | ENSURE(m_ComponentManager.SerializeState(primaryStateAfter.state)); | ||||
if (serializationTestHash) | if (serializationTestHash) | ||||
ENSURE(m_ComponentManager.ComputeStateHash(primaryStateAfter.hash, false)); | ENSURE(m_ComponentManager.ComputeStateHash(primaryStateAfter.hash, false)); | ||||
UpdateComponents(*m_SecondaryContext, turnLengthFixed, | UpdateComponents(*m_SecondaryContext, turnLengthFixed, | ||||
CloneCommandsFromOtherContext(scriptInterface, m_SecondaryComponentManager->GetScriptInterface(), commands)); | CloneCommandsFromOtherCompartment(scriptInterface, m_SecondaryComponentManager->GetScriptInterface(), commands)); | ||||
SerializationTestState secondaryStateAfter; | SerializationTestState secondaryStateAfter; | ||||
ENSURE(m_SecondaryComponentManager->SerializeState(secondaryStateAfter.state)); | ENSURE(m_SecondaryComponentManager->SerializeState(secondaryStateAfter.state)); | ||||
if (serializationTestHash) | if (serializationTestHash) | ||||
ENSURE(m_SecondaryComponentManager->ComputeStateHash(secondaryStateAfter.hash, false)); | ENSURE(m_SecondaryComponentManager->ComputeStateHash(secondaryStateAfter.hash, false)); | ||||
if (primaryStateAfter.state.str() != secondaryStateAfter.state.str() || | if (primaryStateAfter.state.str() != secondaryStateAfter.state.str() || | ||||
primaryStateAfter.hash != secondaryStateAfter.hash) | primaryStateAfter.hash != secondaryStateAfter.hash) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 144 Lines • ▼ Show 20 Lines | void CSimulation2Impl::DumpState() | ||||
m_ComponentManager.DumpDebugState(file, true); | m_ComponentManager.DumpDebugState(file, true); | ||||
std::ofstream binfile (OsString(path.ChangeExtension(L".dat")).c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); | std::ofstream binfile (OsString(path.ChangeExtension(L".dat")).c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary); | ||||
m_ComponentManager.SerializeState(binfile); | m_ComponentManager.SerializeState(binfile); | ||||
} | } | ||||
//////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////// | ||||
CSimulation2::CSimulation2(CUnitManager* unitManager, shared_ptr<ScriptContext> rt, CTerrain* terrain) : | CSimulation2::CSimulation2(CUnitManager* unitManager, shared_ptr<ScriptContext> cx, CTerrain* terrain) : | ||||
m(new CSimulation2Impl(unitManager, rt, terrain)) | m(new CSimulation2Impl(unitManager, cx, terrain)) | ||||
{ | { | ||||
} | } | ||||
CSimulation2::~CSimulation2() | CSimulation2::~CSimulation2() | ||||
{ | { | ||||
delete m; | delete m; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 345 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator