2025-06-03 23:10:15 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2010-01-09 11:20:14 -08:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2010-01-09 11:20:14 -08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2010-01-09 11:20:14 -08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-09 11:20:14 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "simulation2/system/Component.h"
|
|
|
|
|
#include "ICmpCommandQueue.h"
|
|
|
|
|
|
|
|
|
|
#include "ps/CLogger.h"
|
2010-05-19 17:59:01 -07:00
|
|
|
#include "ps/Game.h"
|
2016-06-21 03:33:11 -07:00
|
|
|
#include "ps/Profile.h"
|
2021-05-01 07:04:53 -07:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2021-05-14 03:18:03 -07:00
|
|
|
#include "scriptinterface/JSON.h"
|
2017-01-23 18:04:50 -08:00
|
|
|
#include "simulation2/system/TurnManager.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2022-03-03 14:42:26 -08:00
|
|
|
class CCmpCommandQueue final : public ICmpCommandQueue
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
public:
|
2025-06-03 23:10:15 -07:00
|
|
|
static void ClassInit(CComponentManager&)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEFAULT_COMPONENT_ALLOCATOR(CommandQueue)
|
|
|
|
|
|
2010-05-19 17:59:01 -07:00
|
|
|
std::vector<SimulationCommand> m_LocalQueue;
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2010-04-23 09:09:03 -07:00
|
|
|
static std::string GetSchema()
|
|
|
|
|
{
|
|
|
|
|
return "<a:component type='system'/><empty/>";
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 23:10:15 -07:00
|
|
|
void Init(const CParamNode&) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Deinit() override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void Serialize(ISerializer& serialize) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(GetSimContext().GetScriptInterface());
|
2015-01-24 06:46:52 -08:00
|
|
|
|
2010-05-19 17:59:01 -07:00
|
|
|
serialize.NumberU32_Unbounded("num commands", (u32)m_LocalQueue.size());
|
|
|
|
|
for (size_t i = 0; i < m_LocalQueue.size(); ++i)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2010-05-19 17:59:01 -07:00
|
|
|
serialize.NumberI32_Unbounded("player", m_LocalQueue[i].player);
|
2015-01-24 06:46:52 -08:00
|
|
|
serialize.ScriptVal("data", &m_LocalQueue[i].data);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-03 23:10:15 -07:00
|
|
|
void Deserialize(const CParamNode&, IDeserializer& deserialize) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(GetSimContext().GetScriptInterface());
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
u32 numCmds;
|
2010-09-17 10:53:26 -07:00
|
|
|
deserialize.NumberU32_Unbounded("num commands", numCmds);
|
2010-01-09 11:20:14 -08:00
|
|
|
for (size_t i = 0; i < numCmds; ++i)
|
|
|
|
|
{
|
|
|
|
|
i32 player;
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue data(rq.cx);
|
2010-09-17 10:53:26 -07:00
|
|
|
deserialize.NumberI32_Unbounded("player", player);
|
2014-07-31 12:18:40 -07:00
|
|
|
deserialize.ScriptVal("data", &data);
|
2020-11-13 05:18:22 -08:00
|
|
|
m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, data));
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void PushLocalCommand(player_id_t player, JS::HandleValue cmd) override
|
2010-05-19 17:59:01 -07:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(GetSimContext().GetScriptInterface());
|
2020-11-13 05:18:22 -08:00
|
|
|
m_LocalQueue.emplace_back(SimulationCommand(player, rq.cx, cmd));
|
2010-05-19 17:59:01 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void PostNetworkCommand(JS::HandleValue cmd1) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(GetSimContext().GetScriptInterface());
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
// TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON.
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue cmd(rq.cx, cmd1.get());
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2011-11-09 05:09:01 -08:00
|
|
|
PROFILE2_EVENT("post net command");
|
2021-05-14 03:18:03 -07:00
|
|
|
PROFILE2_ATTR("command: %s", Script::StringifyJSON(rq, &cmd, false).c_str());
|
2011-11-09 05:09:01 -08:00
|
|
|
|
2010-05-19 17:59:01 -07:00
|
|
|
// TODO: would be nicer to not use globals
|
2013-03-24 22:01:36 -07:00
|
|
|
if (g_Game && g_Game->GetTurnManager())
|
2015-01-24 06:46:52 -08:00
|
|
|
g_Game->GetTurnManager()->PostCommand(cmd);
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 15:04:11 -08:00
|
|
|
void FlushTurn(const std::vector<SimulationCommand>& commands) override
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2017-11-24 22:49:58 -08:00
|
|
|
const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface();
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 10:29:17 -08:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2020-11-14 00:46:32 -08:00
|
|
|
JS::RootedValue global(rq.cx, rq.globalValue());
|
2010-05-19 17:59:01 -07:00
|
|
|
std::vector<SimulationCommand> localCommands;
|
|
|
|
|
m_LocalQueue.swap(localCommands);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < localCommands.size(); ++i)
|
2010-01-09 11:20:14 -08:00
|
|
|
{
|
2021-05-01 07:04:53 -07:00
|
|
|
bool ok = ScriptFunction::CallVoid(rq, global, "ProcessCommand", localCommands[i].player, localCommands[i].data);
|
2010-01-09 11:20:14 -08:00
|
|
|
if (!ok)
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Failed to call ProcessCommand() global script function");
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
|
2010-05-19 17:59:01 -07:00
|
|
|
for (size_t i = 0; i < commands.size(); ++i)
|
|
|
|
|
{
|
2021-05-01 07:04:53 -07:00
|
|
|
bool ok = ScriptFunction::CallVoid(rq, global, "ProcessCommand", commands[i].player, commands[i].data);
|
2010-05-19 17:59:01 -07:00
|
|
|
if (!ok)
|
2015-01-22 12:31:30 -08:00
|
|
|
LOGERROR("Failed to call ProcessCommand() global script function");
|
2010-05-19 17:59:01 -07:00
|
|
|
}
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
REGISTER_COMPONENT_TYPE(CommandQueue)
|