From 03a5198ed35f8beff8a9caa7c032dbd61f9115a2 Mon Sep 17 00:00:00 2001 From: phosit Date: Tue, 4 Nov 2025 09:14:18 +0100 Subject: [PATCH] Remove g_GUI usage in simulation2/* This is the last remaining inclusion of a gui file in simulation2/. --- source/gui/GUIManager.cpp | 22 +++++----- source/gui/GUIManager.h | 6 +-- source/main.cpp | 9 +++- source/network/NetClientTurnManager.cpp | 4 +- source/network/NetClientTurnManager.h | 4 +- source/network/tests/test_Net.h | 43 +++++++++++-------- source/ps/Game.cpp | 3 +- source/rlinterface/RLInterface.cpp | 5 ++- .../simulation2/system/LocalTurnManager.cpp | 4 +- source/simulation2/system/LocalTurnManager.h | 4 +- .../simulation2/system/ReplayTurnManager.cpp | 15 +++---- source/simulation2/system/ReplayTurnManager.h | 7 +-- source/simulation2/system/TurnManager.cpp | 4 +- source/simulation2/system/TurnManager.h | 8 +++- 14 files changed, 78 insertions(+), 60 deletions(-) diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index 7bc5f7c0ae..73e6d948f1 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -387,21 +387,19 @@ InReaction CGUIManager::HandleEvent(const SDL_Event_* ev) return IN_PASS; } -void CGUIManager::SendEventToAll(const CStr& eventName) const + +void CGUIManager::SendEventToAll(const CStr& eventName, + std::optional paramData) const { const auto pageStack = GetCopyOfFrozenStack(); for (const SGUIPage& p : pageStack) - p.gui->SendEventToAll(eventName); - -} - -void CGUIManager::SendEventToAll(const CStr& eventName, JS::HandleValueArray paramData) const -{ - const auto pageStack = GetCopyOfFrozenStack(); - - for (const SGUIPage& p : pageStack) - p.gui->SendEventToAll(eventName, paramData); + { + if (paramData.has_value()) + p.gui->SendEventToAll(eventName, paramData.value()); + else + p.gui->SendEventToAll(eventName); + } } std::optional CGUIManager::TickObjects() diff --git a/source/gui/GUIManager.h b/source/gui/GUIManager.h index 02ef174369..d96af3738d 100644 --- a/source/gui/GUIManager.h +++ b/source/gui/GUIManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -103,8 +103,8 @@ public: /** * See CGUI::SendEventToAll; applies to the currently active page. */ - void SendEventToAll(const CStr& eventName) const; - void SendEventToAll(const CStr& eventName, JS::HandleValueArray paramData) const; + void SendEventToAll(const CStr& eventName, + std::optional paramData = std::nullopt) const; /** * See CGUI::TickObjects; applies to @em all loaded pages. diff --git a/source/main.cpp b/source/main.cpp index f2168dad29..24432f8f69 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -501,8 +501,13 @@ static void NonVisualFrame() static u32 turn = 0; if (g_Game && g_Game->IsGameStarted() && g_Game->GetTurnManager()) - if (g_Game->GetTurnManager()->Update(DEFAULT_TURN_LENGTH, 1)) + { + if (g_Game->GetTurnManager()->Update(DEFAULT_TURN_LENGTH, 1, + std::bind_front(&CGUIManager::SendEventToAll, g_GUI))) + { debug_printf("Turn %u (%u)...\n", turn++, DEFAULT_TURN_LENGTH); + } + } g_Profiler.Frame(); diff --git a/source/network/NetClientTurnManager.cpp b/source/network/NetClientTurnManager.cpp index 9c5cf20a36..34c540b4c3 100644 --- a/source/network/NetClientTurnManager.cpp +++ b/source/network/NetClientTurnManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -79,7 +79,7 @@ void CNetClientTurnManager::NotifyFinishedOwnCommands(u32 turn) m_NetClient.SendMessage(&msg); } -void CNetClientTurnManager::NotifyFinishedUpdate(u32 turn) +void CNetClientTurnManager::NotifyFinishedUpdate(u32 turn, const UpdateCallback&) { bool quick = !TurnNeedsFullHash(turn); std::string hash; diff --git a/source/network/NetClientTurnManager.h b/source/network/NetClientTurnManager.h index 03cb4956c6..9dc15a8b1c 100644 --- a/source/network/NetClientTurnManager.h +++ b/source/network/NetClientTurnManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -54,7 +54,7 @@ public: private: void NotifyFinishedOwnCommands(u32 turn) override; - void NotifyFinishedUpdate(u32 turn) override; + void NotifyFinishedUpdate(u32 turn, const UpdateCallback& sendEventToAll) override; CNetClient& m_NetClient; }; diff --git a/source/network/tests/test_Net.h b/source/network/tests/test_Net.h index 6d45b8de79..f8359a15d4 100644 --- a/source/network/tests/test_Net.h +++ b/source/network/tests/test_Net.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -17,6 +17,7 @@ #include "lib/self_test.h" +#include "gui/GUIManager.h" #include "lib/debug.h" #include "lib/external_libraries/enet.h" #include "lib/file/file_system.h" @@ -46,6 +47,14 @@ #include #include +namespace +{ +void UpdateGame(CGame& game) +{ + game.GetTurnManager()->Update(1.0f, 1, std::bind_front(&CGUIManager::SendEventToAll, g_GUI)); +} +} + class TestNetComms : public CxxTest::TestSuite { std::optional xeromycesEngine; @@ -216,13 +225,13 @@ public: } wait(clients, 100); - client1Game.GetTurnManager()->Update(1.0f, 1); - client2Game.GetTurnManager()->Update(1.0f, 1); - client3Game.GetTurnManager()->Update(1.0f, 1); + UpdateGame(client1Game); + UpdateGame(client2Game); + UpdateGame(client3Game); wait(clients, 100); - client1Game.GetTurnManager()->Update(1.0f, 1); - client2Game.GetTurnManager()->Update(1.0f, 1); - client3Game.GetTurnManager()->Update(1.0f, 1); + UpdateGame(client1Game); + UpdateGame(client2Game); + UpdateGame(client3Game); wait(clients, 100); } @@ -286,9 +295,9 @@ public: } wait(clients, 100); - client1Game.GetTurnManager()->Update(1.0f, 1); - client2Game.GetTurnManager()->Update(1.0f, 1); - client3Game.GetTurnManager()->Update(1.0f, 1); + UpdateGame(client1Game); + UpdateGame(client2Game); + UpdateGame(client3Game); wait(clients, 100); { @@ -339,12 +348,12 @@ public: wait(clients, 100); - client1Game.GetTurnManager()->Update(1.0f, 1); - client3Game.GetTurnManager()->Update(1.0f, 1); + UpdateGame(client1Game); + UpdateGame(client3Game); wait(clients, 100); server.SetTurnLength(100); - client1Game.GetTurnManager()->Update(1.0f, 1); - client3Game.GetTurnManager()->Update(1.0f, 1); + UpdateGame(client1Game); + UpdateGame(client3Game); wait(clients, 100); // (This SetTurnLength thing doesn't actually detect errors unless you change @@ -380,9 +389,9 @@ public: for (size_t i = 0; i < 3; ++i) { - client1Game.GetTurnManager()->Update(1.0f, 1); - client2BGame.GetTurnManager()->Update(1.0f, 1); - client3Game.GetTurnManager()->Update(1.0f, 1); + UpdateGame(client1Game); + UpdateGame(client2BGame); + UpdateGame(client3Game); wait(clients, 100); } } diff --git a/source/ps/Game.cpp b/source/ps/Game.cpp index f57b78f56b..a7dddac1b0 100644 --- a/source/ps/Game.cpp +++ b/source/ps/Game.cpp @@ -431,7 +431,8 @@ void CGame::Update(const double deltaRealTime, bool doInterpolate) // so just use the sim rate itself as the number of turns per frame. size_t maxTurns = (size_t)m_SimRate; - if (m_TurnManager->Update(deltaSimTime, maxTurns)) + if (m_TurnManager->Update(deltaSimTime, maxTurns, + std::bind_front(&CGUIManager::SendEventToAll, g_GUI))) { { PROFILE3("gui sim update"); diff --git a/source/rlinterface/RLInterface.cpp b/source/rlinterface/RLInterface.cpp index 84137f87c3..9ffb050ab7 100644 --- a/source/rlinterface/RLInterface.cpp +++ b/source/rlinterface/RLInterface.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -407,7 +407,8 @@ void Interface::ApplyMessage(const GameMessage& msg) { const double deltaSimTime = deltaRealTime * g_Game->GetSimRate(); const size_t maxTurns = static_cast(g_Game->GetSimRate()); - g_Game->GetTurnManager()->Update(deltaSimTime, maxTurns); + g_Game->GetTurnManager()->Update(deltaSimTime, maxTurns, + std::bind_front(&CGUIManager::SendEventToAll, g_GUI)); } else g_Game->Update(deltaRealTime); diff --git a/source/simulation2/system/LocalTurnManager.cpp b/source/simulation2/system/LocalTurnManager.cpp index 2fb6b6d70f..321106912e 100644 --- a/source/simulation2/system/LocalTurnManager.cpp +++ b/source/simulation2/system/LocalTurnManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -47,7 +47,7 @@ void CLocalTurnManager::NotifyFinishedOwnCommands(u32 turn) FinishedAllCommands(turn, m_TurnLength); } -void CLocalTurnManager::NotifyFinishedUpdate(u32 /*turn*/) +void CLocalTurnManager::NotifyFinishedUpdate(u32 /*turn*/, const UpdateCallback&) { #if 0 // this hurts performance and is only useful for verifying log replays std::string hash; diff --git a/source/simulation2/system/LocalTurnManager.h b/source/simulation2/system/LocalTurnManager.h index f6afe5d631..c7086f85b0 100644 --- a/source/simulation2/system/LocalTurnManager.h +++ b/source/simulation2/system/LocalTurnManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -43,7 +43,7 @@ public: protected: void NotifyFinishedOwnCommands(u32 turn) override; - virtual void NotifyFinishedUpdate(u32 turn) override; + virtual void NotifyFinishedUpdate(u32 turn, const UpdateCallback& sendEventToAll) override; }; #endif // INCLUDED_LOCALTURNMANAGER diff --git a/source/simulation2/system/ReplayTurnManager.cpp b/source/simulation2/system/ReplayTurnManager.cpp index eb246a1c6c..34211184ad 100644 --- a/source/simulation2/system/ReplayTurnManager.cpp +++ b/source/simulation2/system/ReplayTurnManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,7 +19,6 @@ #include "ReplayTurnManager.h" -#include "gui/GUIManager.h" #include "lib/debug.h" #include "ps/CLogger.h" #include "ps/Util.h" @@ -73,15 +72,15 @@ void CReplayTurnManager::StoreFinalReplayTurn(u32 turn) m_FinalTurn = turn; } -void CReplayTurnManager::NotifyFinishedUpdate(u32 turn) +void CReplayTurnManager::NotifyFinishedUpdate(u32 turn, const UpdateCallback& sendEventToAll) { if (turn == 1 && m_FinalTurn == 0) - g_GUI->SendEventToAll(EventNameReplayFinished); + sendEventToAll(EventNameReplayFinished, std::nullopt); if (turn > m_FinalTurn) return; - DoTurn(turn); + DoTurn(turn, sendEventToAll); // Compare hash if it exists in the replay and if we didn't have an OOS already std::map>::iterator turnHashIt = m_ReplayHash.find(turn); @@ -117,10 +116,10 @@ void CReplayTurnManager::NotifyFinishedUpdate(u32 turn) Script::ToJSVal(rq, &expectedHashVal, expectedHash); std::ignore = paramData.append(expectedHashVal); - g_GUI->SendEventToAll(EventNameReplayOutOfSync, paramData); + sendEventToAll(EventNameReplayOutOfSync, paramData); } -void CReplayTurnManager::DoTurn(u32 turn) +void CReplayTurnManager::DoTurn(u32 turn, const UpdateCallback& sendEventToAll) { debug_printf("Executing turn %u of %u\n", turn, m_FinalTurn); @@ -137,5 +136,5 @@ void CReplayTurnManager::DoTurn(u32 turn) } if (turn == m_FinalTurn) - g_GUI->SendEventToAll(EventNameReplayFinished); + sendEventToAll(EventNameReplayFinished, std::nullopt); } diff --git a/source/simulation2/system/ReplayTurnManager.h b/source/simulation2/system/ReplayTurnManager.h index 880e6e8929..7335b7c540 100644 --- a/source/simulation2/system/ReplayTurnManager.h +++ b/source/simulation2/system/ReplayTurnManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -23,6 +23,7 @@ #include "simulation2/helpers/Player.h" #include "simulation2/system/LocalTurnManager.h" +#include #include #include #include @@ -48,9 +49,9 @@ public: void StoreFinalReplayTurn(u32 turn); private: - void NotifyFinishedUpdate(u32 turn) override; + void NotifyFinishedUpdate(u32 turn, const UpdateCallback& sendEventToAll) override; - void DoTurn(u32 turn); + void DoTurn(u32 turn, const UpdateCallback& sendEventToAll); static const CStr EventNameReplayFinished; static const CStr EventNameReplayOutOfSync; diff --git a/source/simulation2/system/TurnManager.cpp b/source/simulation2/system/TurnManager.cpp index c45ccdcc59..54e46b6943 100644 --- a/source/simulation2/system/TurnManager.cpp +++ b/source/simulation2/system/TurnManager.cpp @@ -70,7 +70,7 @@ void CTurnManager::SetPlayerID(int playerId) m_PlayerId = playerId; } -bool CTurnManager::Update(float simFrameLength, size_t maxTurns) +bool CTurnManager::Update(float simFrameLength, size_t maxTurns, const UpdateCallback& sendEventToAll) { if (m_CurrentTurn > m_FinalTurn) return false; @@ -153,7 +153,7 @@ bool CTurnManager::Update(float simFrameLength, size_t maxTurns) m_Simulation2.Update(m_TurnLength, commands); - NotifyFinishedUpdate(m_CurrentTurn); + NotifyFinishedUpdate(m_CurrentTurn, sendEventToAll); // Set the time for the next turn update m_DeltaSimTime -= m_TurnLength / 1000.f; diff --git a/source/simulation2/system/TurnManager.h b/source/simulation2/system/TurnManager.h index c6826b1947..4168568bbd 100644 --- a/source/simulation2/system/TurnManager.h +++ b/source/simulation2/system/TurnManager.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -88,6 +89,9 @@ class CTurnManager { NONCOPYABLE(CTurnManager); public: + using UpdateCallback = + std::function)>; + static const CStr EventNameSavegameLoaded; /** @@ -112,7 +116,7 @@ public: * @param simFrameLength Length of the previous frame, in simulation seconds * @param maxTurns Maximum number of turns to simulate at once */ - bool Update(float simFrameLength, size_t maxTurns); + bool Update(float simFrameLength, size_t maxTurns, const UpdateCallback& sendEventToAll); /** * Advance the simulation by as much as possible. Intended for catching up @@ -181,7 +185,7 @@ protected: /** * Called when this client has finished a simulation update. */ - virtual void NotifyFinishedUpdate(u32 turn) = 0; + virtual void NotifyFinishedUpdate(u32 turn, const UpdateCallback& sendEventToAll) = 0; /** * Returns whether we should compute a complete state hash for the given turn,