Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/Simulation2.cpp
Show First 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | void ReportSerializationFailure( | ||||
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> CloneCommandsFromOtherContext(const ScriptInterface& oldScript, const ScriptInterface& newScript, | ||||
const std::vector<SimulationCommand>& commands) | const std::vector<SimulationCommand>& commands) | ||||
{ | { | ||||
JSContext* cxOld = oldScript.GetContext(); | |||||
JSAutoRequest rqOld(cxOld); | |||||
std::vector<SimulationCommand> newCommands; | std::vector<SimulationCommand> newCommands; | ||||
newCommands.reserve(commands.size()); | newCommands.reserve(commands.size()); | ||||
ScriptInterface::Request rqNew(newScript); | |||||
for (const SimulationCommand& command : commands) | for (const SimulationCommand& command : commands) | ||||
{ | { | ||||
JSContext* cxNew = newScript.GetContext(); | JS::RootedValue tmpCommand(rqNew.cx, newScript.CloneValueFromOtherContext(oldScript, command.data)); | ||||
JSAutoRequest rqNew(cxNew); | |||||
JS::RootedValue tmpCommand(cxNew, newScript.CloneValueFromOtherContext(oldScript, command.data)); | |||||
newScript.FreezeObject(tmpCommand, true); | newScript.FreezeObject(tmpCommand, true); | ||||
SimulationCommand cmd(command.player, cxNew, tmpCommand); | SimulationCommand cmd(command.player, rqNew.cx, tmpCommand); | ||||
newCommands.emplace_back(std::move(cmd)); | newCommands.emplace_back(std::move(cmd)); | ||||
} | } | ||||
return newCommands; | return newCommands; | ||||
} | } | ||||
}; | }; | ||||
bool CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts) | bool CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set<VfsPath>* loadedScripts) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 229 Lines • ▼ Show 20 Lines | if (m_EnableSerializationTest || startRejoinTest) | ||||
delete m_SecondaryLoadedScripts; | delete m_SecondaryLoadedScripts; | ||||
m_SecondaryLoadedScripts = new std::set<VfsPath>(); | m_SecondaryLoadedScripts = new std::set<VfsPath>(); | ||||
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. | ||||
{ | { | ||||
JSContext* cx2 = m_SecondaryComponentManager->GetScriptInterface().GetContext(); | ScriptInterface::Request rq2(m_SecondaryComponentManager->GetScriptInterface()); | ||||
JSAutoRequest rq2(cx2); | JS::RootedValue mapSettingsCloned(rq2.cx, | ||||
JS::RootedValue mapSettingsCloned(cx2, | |||||
m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherContext( | m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherContext( | ||||
scriptInterface, m_MapSettings)); | scriptInterface, m_MapSettings)); | ||||
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(); | ||||
▲ Show 20 Lines • Show All 294 Lines • ▼ Show 20 Lines | |||||
ScriptInterface& CSimulation2::GetScriptInterface() const | ScriptInterface& CSimulation2::GetScriptInterface() const | ||||
{ | { | ||||
return m->m_ComponentManager.GetScriptInterface(); | return m->m_ComponentManager.GetScriptInterface(); | ||||
} | } | ||||
void CSimulation2::PreInitGame() | void CSimulation2::PreInitGame() | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | ScriptInterface::Request rq(GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | JS::RootedValue global(rq.cx, GetScriptInterface().GetGlobalObject()); | ||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | |||||
GetScriptInterface().CallFunctionVoid(global, "PreInitGame"); | GetScriptInterface().CallFunctionVoid(global, "PreInitGame"); | ||||
} | } | ||||
void CSimulation2::InitGame() | void CSimulation2::InitGame() | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | ScriptInterface::Request rq(GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | JS::RootedValue global(rq.cx, GetScriptInterface().GetGlobalObject()); | ||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | |||||
JS::RootedValue settings(cx); | JS::RootedValue settings(rq.cx); | ||||
JS::RootedValue tmpInitAttributes(cx, GetInitAttributes()); | JS::RootedValue tmpInitAttributes(rq.cx, GetInitAttributes()); | ||||
GetScriptInterface().GetProperty(tmpInitAttributes, "settings", &settings); | GetScriptInterface().GetProperty(tmpInitAttributes, "settings", &settings); | ||||
GetScriptInterface().CallFunctionVoid(global, "InitGame", settings); | GetScriptInterface().CallFunctionVoid(global, "InitGame", settings); | ||||
} | } | ||||
void CSimulation2::Update(int turnLength) | void CSimulation2::Update(int turnLength) | ||||
{ | { | ||||
std::vector<SimulationCommand> commands; | std::vector<SimulationCommand> commands; | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | |||||
void CSimulation2::GetMapSettings(JS::MutableHandleValue ret) | void CSimulation2::GetMapSettings(JS::MutableHandleValue ret) | ||||
{ | { | ||||
ret.set(m->m_MapSettings); | ret.set(m->m_MapSettings); | ||||
} | } | ||||
void CSimulation2::LoadPlayerSettings(bool newPlayers) | void CSimulation2::LoadPlayerSettings(bool newPlayers) | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | ScriptInterface::Request rq(GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | JS::RootedValue global(rq.cx, GetScriptInterface().GetGlobalObject()); | ||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | |||||
GetScriptInterface().CallFunctionVoid(global, "LoadPlayerSettings", m->m_MapSettings, newPlayers); | GetScriptInterface().CallFunctionVoid(global, "LoadPlayerSettings", m->m_MapSettings, newPlayers); | ||||
} | } | ||||
void CSimulation2::LoadMapSettings() | void CSimulation2::LoadMapSettings() | ||||
{ | { | ||||
JSContext* cx = GetScriptInterface().GetContext(); | ScriptInterface::Request rq(GetScriptInterface()); | ||||
JSAutoRequest rq(cx); | |||||
JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); | JS::RootedValue global(rq.cx, GetScriptInterface().GetGlobalObject()); | ||||
// Initialize here instead of in Update() | // Initialize here instead of in Update() | ||||
GetScriptInterface().CallFunctionVoid(global, "LoadMapSettings", m->m_MapSettings); | GetScriptInterface().CallFunctionVoid(global, "LoadMapSettings", m->m_MapSettings); | ||||
GetScriptInterface().FreezeObject(m->m_InitAttributes, true); | GetScriptInterface().FreezeObject(m->m_InitAttributes, true); | ||||
GetScriptInterface().SetGlobal("InitAttributes", m->m_InitAttributes, true, true, true); | GetScriptInterface().SetGlobal("InitAttributes", m->m_InitAttributes, true, true, true); | ||||
if (!m->m_StartupScript.empty()) | if (!m->m_StartupScript.empty()) | ||||
▲ Show 20 Lines • Show All 117 Lines • ▼ Show 20 Lines | |||||
std::string CSimulation2::GetMapSizes() | std::string CSimulation2::GetMapSizes() | ||||
{ | { | ||||
return ReadJSON(L"simulation/data/settings/map_sizes.json"); | return ReadJSON(L"simulation/data/settings/map_sizes.json"); | ||||
} | } | ||||
std::string CSimulation2::GetAIData() | std::string CSimulation2::GetAIData() | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = GetScriptInterface(); | const ScriptInterface& scriptInterface = GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | ScriptInterface::Request rq(scriptInterface); | ||||
JSAutoRequest rq(cx); | JS::RootedValue aiData(rq.cx, ICmpAIManager::GetAIs(scriptInterface)); | ||||
JS::RootedValue aiData(cx, ICmpAIManager::GetAIs(scriptInterface)); | |||||
// Build single JSON string with array of AI data | // Build single JSON string with array of AI data | ||||
JS::RootedValue ais(cx); | JS::RootedValue ais(rq.cx); | ||||
if (!ScriptInterface::CreateObject(cx, &ais, "AIData", aiData)) | if (!ScriptInterface::CreateObject(rq, &ais, "AIData", aiData)) | ||||
return std::string(); | return std::string(); | ||||
return scriptInterface.StringifyJSON(&ais); | return scriptInterface.StringifyJSON(&ais); | ||||
} | } |
Wildfire Games · Phabricator