Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpCommandQueue.cpp
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | public: | ||||
} | } | ||||
virtual void Deinit() | virtual void Deinit() | ||||
{ | { | ||||
} | } | ||||
virtual void Serialize(ISerializer& serialize) | virtual void Serialize(ISerializer& serialize) | ||||
{ | { | ||||
ScriptInterface::Request rq(GetSimContext().GetScriptInterface()); | ScriptRequest rq(GetSimContext().GetScriptInterface()); | ||||
serialize.NumberU32_Unbounded("num commands", (u32)m_LocalQueue.size()); | serialize.NumberU32_Unbounded("num commands", (u32)m_LocalQueue.size()); | ||||
for (size_t i = 0; i < m_LocalQueue.size(); ++i) | for (size_t i = 0; i < m_LocalQueue.size(); ++i) | ||||
{ | { | ||||
serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player); | serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player); | ||||
serialize.ScriptVal("data", &m_LocalQueue[i].data); | serialize.ScriptVal("data", &m_LocalQueue[i].data); | ||||
} | } | ||||
} | } | ||||
virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) | virtual void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) | ||||
{ | { | ||||
ScriptInterface::Request rq(GetSimContext().GetScriptInterface()); | ScriptRequest rq(GetSimContext().GetScriptInterface()); | ||||
u32 numCmds; | u32 numCmds; | ||||
deserialize.NumberU32_Unbounded("num commands", numCmds); | deserialize.NumberU32_Unbounded("num commands", numCmds); | ||||
for (size_t i = 0; i < numCmds; ++i) | for (size_t i = 0; i < numCmds; ++i) | ||||
{ | { | ||||
i32 player; | i32 player; | ||||
JS::RootedValue data(rq.cx); | JS::RootedValue data(rq.cx); | ||||
deserialize.NumberI32_Unbounded("player", player); | deserialize.NumberI32_Unbounded("player", player); | ||||
deserialize.ScriptVal("data", &data); | deserialize.ScriptVal("data", &data); | ||||
m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, data)); | m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, data)); | ||||
} | } | ||||
} | } | ||||
virtual void PushLocalCommand(player_id_t player, JS::HandleValue cmd) | virtual void PushLocalCommand(player_id_t player, JS::HandleValue cmd) | ||||
{ | { | ||||
ScriptInterface::Request rq(GetSimContext().GetScriptInterface()); | ScriptRequest rq(GetSimContext().GetScriptInterface()); | ||||
m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, cmd)); | m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, cmd)); | ||||
} | } | ||||
virtual void PostNetworkCommand(JS::HandleValue cmd1) | virtual void PostNetworkCommand(JS::HandleValue cmd1) | ||||
{ | { | ||||
ScriptInterface::Request rq(GetSimContext().GetScriptInterface()); | ScriptRequest rq(GetSimContext().GetScriptInterface()); | ||||
// TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. | // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON. | ||||
JS::RootedValue cmd(rq.cx, cmd1.get()); | JS::RootedValue cmd(rq.cx, cmd1.get()); | ||||
PROFILE2_EVENT("post net command"); | PROFILE2_EVENT("post net command"); | ||||
PROFILE2_ATTR("command: %s", GetSimContext().GetScriptInterface().StringifyJSON(&cmd, false).c_str()); | PROFILE2_ATTR("command: %s", GetSimContext().GetScriptInterface().StringifyJSON(&cmd, false).c_str()); | ||||
// TODO: would be nicer to not use globals | // TODO: would be nicer to not use globals | ||||
if (g_Game && g_Game->GetTurnManager()) | if (g_Game && g_Game->GetTurnManager()) | ||||
g_Game->GetTurnManager()->PostCommand(cmd); | g_Game->GetTurnManager()->PostCommand(cmd); | ||||
} | } | ||||
virtual void FlushTurn(const std::vector<SimulationCommand>& commands) | virtual void FlushTurn(const std::vector<SimulationCommand>& commands) | ||||
{ | { | ||||
const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); | const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); | ||||
ScriptInterface::Request rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
JS::RootedValue global(rq.cx, rq.globalValue()); | JS::RootedValue global(rq.cx, rq.globalValue()); | ||||
std::vector<SimulationCommand> localCommands; | std::vector<SimulationCommand> localCommands; | ||||
m_LocalQueue.swap(localCommands); | m_LocalQueue.swap(localCommands); | ||||
for (size_t i = 0; i < localCommands.size(); ++i) | for (size_t i = 0; i < localCommands.size(); ++i) | ||||
{ | { | ||||
bool ok = scriptInterface.CallFunctionVoid(global, "ProcessCommand", localCommands[i].player, localCommands[i].data); | bool ok = scriptInterface.CallFunctionVoid(global, "ProcessCommand", localCommands[i].player, localCommands[i].data); | ||||
Show All 14 Lines |
Wildfire Games · Phabricator