Line data Source code
1 : /* Copyright (C) 2022 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 "simulation2/system/Component.h"
21 : #include "ICmpCommandQueue.h"
22 :
23 : #include "ps/CLogger.h"
24 : #include "ps/Game.h"
25 : #include "ps/Profile.h"
26 : #include "scriptinterface/FunctionWrapper.h"
27 : #include "scriptinterface/JSON.h"
28 : #include "simulation2/system/TurnManager.h"
29 :
30 18 : class CCmpCommandQueue final : public ICmpCommandQueue
31 : {
32 : public:
33 116 : static void ClassInit(CComponentManager& UNUSED(componentManager))
34 : {
35 116 : }
36 :
37 12 : DEFAULT_COMPONENT_ALLOCATOR(CommandQueue)
38 :
39 : std::vector<SimulationCommand> m_LocalQueue;
40 :
41 116 : static std::string GetSchema()
42 : {
43 116 : return "<a:component type='system'/><empty/>";
44 : }
45 :
46 4 : void Init(const CParamNode& UNUSED(paramNode)) override
47 : {
48 4 : }
49 :
50 6 : void Deinit() override
51 : {
52 6 : }
53 :
54 12 : void Serialize(ISerializer& serialize) override
55 : {
56 24 : ScriptRequest rq(GetSimContext().GetScriptInterface());
57 :
58 12 : serialize.NumberU32_Unbounded("num commands", (u32)m_LocalQueue.size());
59 24 : for (size_t i = 0; i < m_LocalQueue.size(); ++i)
60 : {
61 12 : serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player);
62 12 : serialize.ScriptVal("data", &m_LocalQueue[i].data);
63 : }
64 12 : }
65 :
66 2 : void Deserialize(const CParamNode& UNUSED(paramNode), IDeserializer& deserialize) override
67 : {
68 4 : ScriptRequest rq(GetSimContext().GetScriptInterface());
69 :
70 : u32 numCmds;
71 2 : deserialize.NumberU32_Unbounded("num commands", numCmds);
72 4 : for (size_t i = 0; i < numCmds; ++i)
73 : {
74 : i32 player;
75 4 : JS::RootedValue data(rq.cx);
76 2 : deserialize.NumberI32_Unbounded("player", player);
77 2 : deserialize.ScriptVal("data", &data);
78 2 : m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, data));
79 : }
80 2 : }
81 :
82 3 : void PushLocalCommand(player_id_t player, JS::HandleValue cmd) override
83 : {
84 6 : ScriptRequest rq(GetSimContext().GetScriptInterface());
85 3 : m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, cmd));
86 3 : }
87 :
88 0 : void PostNetworkCommand(JS::HandleValue cmd1) override
89 : {
90 0 : ScriptRequest rq(GetSimContext().GetScriptInterface());
91 :
92 : // TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON.
93 0 : JS::RootedValue cmd(rq.cx, cmd1.get());
94 :
95 0 : PROFILE2_EVENT("post net command");
96 0 : PROFILE2_ATTR("command: %s", Script::StringifyJSON(rq, &cmd, false).c_str());
97 :
98 : // TODO: would be nicer to not use globals
99 0 : if (g_Game && g_Game->GetTurnManager())
100 0 : g_Game->GetTurnManager()->PostCommand(cmd);
101 0 : }
102 :
103 3 : void FlushTurn(const std::vector<SimulationCommand>& commands) override
104 : {
105 3 : const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
106 6 : ScriptRequest rq(scriptInterface);
107 :
108 6 : JS::RootedValue global(rq.cx, rq.globalValue());
109 6 : std::vector<SimulationCommand> localCommands;
110 3 : m_LocalQueue.swap(localCommands);
111 :
112 6 : for (size_t i = 0; i < localCommands.size(); ++i)
113 : {
114 3 : bool ok = ScriptFunction::CallVoid(rq, global, "ProcessCommand", localCommands[i].player, localCommands[i].data);
115 3 : if (!ok)
116 0 : LOGERROR("Failed to call ProcessCommand() global script function");
117 : }
118 :
119 3 : for (size_t i = 0; i < commands.size(); ++i)
120 : {
121 0 : bool ok = ScriptFunction::CallVoid(rq, global, "ProcessCommand", commands[i].player, commands[i].data);
122 0 : if (!ok)
123 0 : LOGERROR("Failed to call ProcessCommand() global script function");
124 : }
125 3 : }
126 : };
127 :
128 119 : REGISTER_COMPONENT_TYPE(CommandQueue)
|