Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
/* Copyright (C) 2026 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 "ICmpCommandQueue.h"
|
|
|
|
|
|
2025-08-02 12:24:35 -07:00
|
|
|
#include "lib/types.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "ps/CLogger.h"
|
2010-05-19 17:59:01 -07:00
|
|
|
#include "ps/Game.h"
|
2025-08-02 12:24:35 -07:00
|
|
|
#include "ps/Profiler2.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"
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
#include "scriptinterface/Request.h"
|
2025-08-02 12:24:35 -07:00
|
|
|
#include "simulation2/helpers/Player.h"
|
|
|
|
|
#include "simulation2/helpers/SimulationCommand.h"
|
|
|
|
|
#include "simulation2/system/Component.h"
|
2017-01-23 18:04:50 -08:00
|
|
|
#include "simulation2/system/TurnManager.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
|
2025-08-02 12:24:35 -07:00
|
|
|
#include <cstddef>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <js/Value.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
namespace Script { class Interface; }
|
2025-08-02 12:24:35 -07: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
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request 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
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request 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
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request 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
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
Script::Request 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
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
const Script::Interface& scriptInterface = GetSimContext().GetScriptInterface();
|
|
|
|
|
Script::Request 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
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
bool ok = Script::Function::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)
|
|
|
|
|
{
|
Move Script* classes to Script namespace
- Rename ScriptEngine, ScriptContext, ScriptInterface, ScriptRequest to
Script::Engine, Script::Context, Script::Interface, Script::Request
- Remove 'Script' prefix from filenames:
ScriptContext.* → Context.*
ScriptInterface.* → Interface.*
ScriptRequest.* → Request.*
ScriptEngine.* → Engine.*
ScriptConversions.* → Conversions.*
ScriptExceptions.* → Exceptions.*
ScriptForward.* → ForwardDeclarations.*
ScriptStats.* → Stats.*
- Update all includes, forward declarations, and friend classes
- Use namespace Script { ... } in .cpp definitions to avoid repetitive
Script:: prefix (keeping global callbacks outside)
- Rename internal implementation structs:
ScriptInterface_impl → Interface_impl
ScriptFunction → Function
- Update copyright year to 2026 in all touched files
- Suppress pre-existing cppcheck warnings (uninitvar, nullPointer, unknown
macro) by adding them to suppressions-list.txt (these are not caused
by this refactor)
Fixes #7516
2026-06-02 14:06:50 -07:00
|
|
|
bool ok = Script::Function::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)
|