Index: source/simulation2/Simulation2.h =================================================================== --- source/simulation2/Simulation2.h +++ source/simulation2/Simulation2.h @@ -104,7 +104,6 @@ * Get the data passed to SetInitAttributes. */ JS::Value GetInitAttributes(); - void GetInitAttributes(JS::MutableHandleValue ret); /** * Set the initial map settings (as a UTF-8-encoded JSON string), @@ -126,11 +125,6 @@ std::string GetMapSettingsString(); /** - * Get the current map settings. - */ - void GetMapSettings(JS::MutableHandleValue ret); - - /** * RegMemFun incremental loader function. */ int ProgressiveLoad(); Index: source/simulation2/Simulation2.cpp =================================================================== --- source/simulation2/Simulation2.cpp +++ source/simulation2/Simulation2.cpp @@ -53,7 +53,7 @@ m_SimContext(), m_ComponentManager(m_SimContext, rt), 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_MapSettings(rt->m_rt), m_InitAttributes(rt->m_rt) + m_InitAttributes(rt->m_rt) { m_SimContext.m_UnitManager = unitManager; m_SimContext.m_Terrain = terrain; @@ -127,7 +127,6 @@ std::string m_StartupScript; JS::PersistentRootedValue m_InitAttributes; - JS::PersistentRootedValue m_MapSettings; std::set m_LoadedScripts; @@ -161,8 +160,13 @@ SerializationTestState* primaryStateBefore, SerializationTestState* primaryStateAfter, SerializationTestState* secondaryStateBefore, SerializationTestState* secondaryStateAfter); - void InitRNGSeedSimulation(); - void InitRNGSeedAI(); + /** + * This returns a reference to the settings object of m_InitAttributes, not a copy! + */ + JS::Value GetMapSettings(); + + void InitRNGSeedSimulation(JS::HandleValue settings); + void InitRNGSeedAI(JS::HandleValue settings); static std::vector CloneCommandsFromOtherContext(const ScriptInterface& oldScript, const ScriptInterface& newScript, const std::vector& commands) @@ -185,6 +189,17 @@ } }; +JS::Value CSimulation2Impl::GetMapSettings() +{ + // Please consider that this function returns a reference, not a copy! + ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); + JSContext* cx = scriptInterface.GetContext(); + JSAutoRequest rq(cx); + JS::RootedValue settingsVal(cx); + scriptInterface.GetProperty(m_InitAttributes, "settings", &settingsVal); + return settingsVal; +} + bool CSimulation2Impl::LoadDefaultScripts(CComponentManager& componentManager, std::set* loadedScripts) { return ( @@ -337,21 +352,25 @@ debug_warn(L"Serialization test failure"); } -void CSimulation2Impl::InitRNGSeedSimulation() +void CSimulation2Impl::InitRNGSeedSimulation(JS::HandleValue settings) { + const ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); + u32 seed = 0; - if (!m_ComponentManager.GetScriptInterface().HasProperty(m_MapSettings, "Seed") || - !m_ComponentManager.GetScriptInterface().GetProperty(m_MapSettings, "Seed", seed)) + if (!scriptInterface.HasProperty(settings, "Seed") || + !scriptInterface.GetProperty(settings, "Seed", seed)) LOGWARNING("CSimulation2Impl::InitRNGSeedSimulation: No seed value specified - using %d", seed); m_ComponentManager.SetRNGSeed(seed); } -void CSimulation2Impl::InitRNGSeedAI() +void CSimulation2Impl::InitRNGSeedAI(JS::HandleValue settings) { + const ScriptInterface& scriptInterface = m_ComponentManager.GetScriptInterface(); + u32 seed = 0; - if (!m_ComponentManager.GetScriptInterface().HasProperty(m_MapSettings, "AISeed") || - !m_ComponentManager.GetScriptInterface().GetProperty(m_MapSettings, "AISeed", seed)) + if (!scriptInterface.HasProperty(settings, "AISeed") || + !scriptInterface.GetProperty(settings, "AISeed", seed)) LOGWARNING("CSimulation2Impl::InitRNGSeedAI: No seed value specified - using %d", seed); CmpPtr cmpAIManager(m_SimContext, SYSTEM_ENTITY); @@ -426,9 +445,9 @@ { JSContext* cx2 = m_SecondaryComponentManager->GetScriptInterface().GetContext(); JSAutoRequest rq2(cx2); + JS::RootedValue mapSettings(cx2, GetMapSettings()); JS::RootedValue mapSettingsCloned(cx2, - m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherContext( - scriptInterface, m_MapSettings)); + m_SecondaryComponentManager->GetScriptInterface().CloneValueFromOtherContext(scriptInterface, mapSettings)); ENSURE(LoadTriggerScripts(*m_SecondaryComponentManager, mapSettingsCloned, m_SecondaryLoadedScripts)); } @@ -744,9 +763,7 @@ JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); JS::RootedValue settings(cx); - JS::RootedValue tmpInitAttributes(cx, GetInitAttributes()); - GetScriptInterface().GetProperty(tmpInitAttributes, "settings", &settings); - + GetScriptInterface().GetProperty(m->m_InitAttributes, "settings", &settings); GetScriptInterface().CallFunctionVoid(global, "InitGame", settings); } @@ -809,40 +826,39 @@ return m->m_InitAttributes.get(); } -void CSimulation2::GetInitAttributes(JS::MutableHandleValue ret) -{ - ret.set(m->m_InitAttributes); -} - void CSimulation2::SetMapSettings(const std::string& settings) { - m->m_ComponentManager.GetScriptInterface().ParseJSON(settings, &m->m_MapSettings); + const ScriptInterface& scriptInterface = GetScriptInterface(); + JSContext* cx = scriptInterface.GetContext(); + JSAutoRequest rq(cx); + JS::RootedValue settingsVal(cx); + scriptInterface.ParseJSON(settings, &settingsVal); + scriptInterface.SetProperty(m->m_InitAttributes, "settings", settingsVal); } void CSimulation2::SetMapSettings(JS::HandleValue settings) { - m->m_MapSettings = settings; - - m->InitRNGSeedSimulation(); - m->InitRNGSeedAI(); + GetScriptInterface().SetProperty(m->m_InitAttributes, "settings", settings); + m->InitRNGSeedSimulation(settings); + m->InitRNGSeedAI(settings); } std::string CSimulation2::GetMapSettingsString() { - return m->m_ComponentManager.GetScriptInterface().StringifyJSON(&m->m_MapSettings); -} - -void CSimulation2::GetMapSettings(JS::MutableHandleValue ret) -{ - ret.set(m->m_MapSettings); + JSContext* cx = GetScriptInterface().GetContext(); + JSAutoRequest rq(cx); + JS::RootedValue settingsVal(cx, m->GetMapSettings()); + return GetScriptInterface().StringifyJSON(&settingsVal); } void CSimulation2::LoadPlayerSettings(bool newPlayers) { - JSContext* cx = GetScriptInterface().GetContext(); + const ScriptInterface& scriptInterface = GetScriptInterface(); + JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); - JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); - GetScriptInterface().CallFunctionVoid(global, "LoadPlayerSettings", m->m_MapSettings, newPlayers); + JS::RootedValue global(cx, scriptInterface.GetGlobalObject()); + JS::RootedValue mapSettings(cx, m->GetMapSettings()); + scriptInterface.CallFunctionVoid(global, "LoadPlayerSettings", mapSettings, newPlayers); } void CSimulation2::LoadMapSettings() @@ -853,13 +869,14 @@ JS::RootedValue global(cx, GetScriptInterface().GetGlobalObject()); // Initialize here instead of in Update() - GetScriptInterface().CallFunctionVoid(global, "LoadMapSettings", m->m_MapSettings); + JS::RootedValue mapSettings(cx, m->GetMapSettings()); + GetScriptInterface().CallFunctionVoid(global, "LoadMapSettings", mapSettings); if (!m->m_StartupScript.empty()) GetScriptInterface().LoadScript(L"map startup script", m->m_StartupScript); // Load the trigger scripts after we have loaded the simulation and the map. - m->LoadTriggerScripts(m->m_ComponentManager, m->m_MapSettings, &m->m_LoadedScripts); + m->LoadTriggerScripts(m->m_ComponentManager, mapSettings, &m->m_LoadedScripts); } int CSimulation2::ProgressiveLoad() Index: source/simulation2/scripting/JSInterface_Simulation.cpp =================================================================== --- source/simulation2/scripting/JSInterface_Simulation.cpp +++ source/simulation2/scripting/JSInterface_Simulation.cpp @@ -42,8 +42,7 @@ JSContext* cx = g_Game->GetSimulation2()->GetScriptInterface().GetContext(); JSAutoRequest rq(cx); - JS::RootedValue initAttribs(cx); - g_Game->GetSimulation2()->GetInitAttributes(&initAttribs); + JS::RootedValue initAttribs(cx, g_Game->GetSimulation2()->GetInitAttributes()); return pCxPrivate->pScriptInterface->CloneValueFromOtherContext( g_Game->GetSimulation2()->GetScriptInterface(),