LCOV - code coverage report
Current view: top level - source/simulation2/components - CCmpCommandQueue.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 43 55 78.2 %
Date: 2026-12-02 01:21:50 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "ICmpCommandQueue.h"
      21             : 
      22             : #include "ps/CLogger.h"
      23             : #include "ps/Game.h"
      24             : #include "ps/Profiler2.h"
      25             : #include "scriptinterface/FunctionWrapper.h"
      26             : #include "scriptinterface/JSON.h"
      27             : #include "scriptinterface/Request.h"
      28             : #include "simulation2/helpers/Player.h"
      29             : #include "simulation2/helpers/SimulationCommand.h"
      30             : #include "simulation2/system/Component.h"
      31             : #include "simulation2/system/TurnManager.h"
      32             : 
      33             : #include <cstddef>
      34             : #include <js/RootingAPI.h>
      35             : #include <js/TypeDecls.h>
      36             : #include <js/Value.h>
      37             : #include <string>
      38             : #include <vector>
      39             : 
      40             : namespace Script { class Interface; }
      41             : 
      42          10 : class CCmpCommandQueue final : public ICmpCommandQueue
      43             : {
      44             : public:
      45             :     static void ClassInit(CComponentManager&)
      46             :     {
      47             :     }
      48             : 
      49          20 :     DEFAULT_COMPONENT_ALLOCATOR(CommandQueue)
      50             : 
      51             :     std::vector<SimulationCommand> m_LocalQueue;
      52             : 
      53         137 :     static std::string GetSchema()
      54             :     {
      55         274 :         return "<a:component type='system'/><empty/>";
      56             :     }
      57             : 
      58           8 :     void Init(const CParamNode&) override
      59             :     {
      60           8 :     }
      61             : 
      62          10 :     void Deinit() override
      63             :     {
      64          10 :     }
      65             : 
      66          30 :     void Serialize(ISerializer& serialize) override
      67             :     {
      68          30 :         Script::Request rq(GetSimContext().GetScriptInterface());
      69             : 
      70          30 :         serialize.NumberU32_Unbounded("num commands", static_cast<std::uint32_t>(m_LocalQueue.size()));
      71          42 :         for (size_t i = 0; i < m_LocalQueue.size(); ++i)
      72             :         {
      73          12 :             serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player);
      74          12 :             serialize.ScriptVal("data", &m_LocalQueue[i].data);
      75             :         }
      76          30 :     }
      77             : 
      78           2 :     void Deserialize(const CParamNode&, IDeserializer& deserialize) override
      79             :     {
      80           2 :         Script::Request rq(GetSimContext().GetScriptInterface());
      81             : 
      82           2 :         std::uint32_t numCmds;
      83           2 :         deserialize.NumberU32_Unbounded("num commands", numCmds);
      84           4 :         for (size_t i = 0; i < numCmds; ++i)
      85             :         {
      86           2 :             std::int32_t player;
      87           2 :             JS::RootedValue data(rq.cx);
      88           2 :             deserialize.NumberI32_Unbounded("player", player);
      89           2 :             deserialize.ScriptVal("data", &data);
      90           6 :             m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, data));
      91           2 :         }
      92           2 :     }
      93             : 
      94           3 :     void PushLocalCommand(player_id_t player, JS::HandleValue cmd) override
      95             :     {
      96           3 :         Script::Request rq(GetSimContext().GetScriptInterface());
      97           6 :         m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, cmd));
      98           3 :     }
      99             : 
     100           0 :     void PostNetworkCommand(JS::HandleValue cmd1) override
     101             :     {
     102           0 :         Script::Request rq(GetSimContext().GetScriptInterface());
     103             : 
     104             :         // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON.
     105           0 :         JS::RootedValue cmd(rq.cx, cmd1.get());
     106             : 
     107           0 :         PROFILE2_EVENT("post net command");
     108           0 :         PROFILE2_ATTR("command: %s", Script::StringifyJSON(rq, &cmd, false).c_str());
     109             : 
     110             :         // TODO: would be nicer to not use globals
     111           0 :         if (g_Game && g_Game->GetTurnManager())
     112           0 :             g_Game->GetTurnManager()->PostCommand(cmd);
     113           0 :     }
     114             : 
     115         183 :     void FlushTurn(const std::vector<SimulationCommand>& commands) override
     116             :     {
     117         183 :         const Script::Interface& scriptInterface = GetSimContext().GetScriptInterface();
     118         183 :         Script::Request rq(scriptInterface);
     119             : 
     120         183 :         JS::RootedValue global(rq.cx, rq.globalValue());
     121         183 :         std::vector<SimulationCommand> localCommands;
     122         183 :         m_LocalQueue.swap(localCommands);
     123             : 
     124         186 :         for (size_t i = 0; i < localCommands.size(); ++i)
     125             :         {
     126           3 :             bool ok = Script::Function::CallVoid(rq, global, "ProcessCommand", localCommands[i].player, localCommands[i].data);
     127           3 :             if (!ok)
     128           0 :                 LOGERROR("Failed to call ProcessCommand() global script function");
     129             :         }
     130             : 
     131         183 :         for (size_t i = 0; i < commands.size(); ++i)
     132             :         {
     133           0 :             bool ok = Script::Function::CallVoid(rq, global, "ProcessCommand", commands[i].player, commands[i].data);
     134           0 :             if (!ok)
     135           0 :                 LOGERROR("Failed to call ProcessCommand() global script function");
     136             :         }
     137         183 :     }
     138             : };
     139             : 
     140         274 : REGISTER_COMPONENT_TYPE(CommandQueue)

Generated by: LCOV version 1.16