From abee15418af61835434a9a9cc0a93de526931f07 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 12 Feb 2023 18:00:54 +0100 Subject: [PATCH] Improve chat history and disconnects --- src/client/client.cpp | 5 +---- src/core/connection.cpp | 9 ++++++--- src/core/macros.h | 2 ++ src/gui/gameplay.cpp | 20 ++++++++++++------- src/gui/gui.cpp | 43 +++++++++++++++++++++++++---------------- src/gui/gui.h | 1 + src/main.cpp | 17 +++++++++++++--- 7 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/client/client.cpp b/src/client/client.cpp index f7883fe..9115366 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -238,13 +238,10 @@ void Client::onPeerDisconnected(peer_t peer_id) // send event for client destruction { - GameEvent e(GameEvent::C2G_DIALOG); - e.text = new std::string("Server disconnected"); - sendNewEvent(e); - GameEvent e2(GameEvent::C2G_DISCONNECT); sendNewEvent(e2); } + printf("Client: Disconnected from the server\n"); } void Client::processPacket(peer_t peer_id, Packet &pkt) diff --git a/src/core/connection.cpp b/src/core/connection.cpp index eb83bd5..1d3a92e 100644 --- a/src/core/connection.cpp +++ b/src/core/connection.cpp @@ -7,7 +7,7 @@ #include // strerror #include -#if 1 +#if 0 #define DEBUGLOG(...) printf(__VA_ARGS__) #else #define DEBUGLOG(...) /* SILENCE */ @@ -72,8 +72,9 @@ Connection::Connection(Connection::ConnectionType type, const char *name) Connection::~Connection() { - DEBUGLOG("--- ENet %s: Cleaning up ...\n", m_name); m_running = false; + if (!m_host) + return; for (size_t i = 0; i < m_host->peerCount; ++i) enet_peer_disconnect_later(&m_host->peers[i], 0); @@ -84,6 +85,8 @@ Connection::~Connection() ERRORLOG("--- ENet %s: Failed to join thread status=%d\n", m_name, errno); } } + while (m_thread) + sleep(1); // Apply force if needed for (size_t i = 0; i < m_host->peerCount; ++i) @@ -161,7 +164,7 @@ void Connection::disconnect(peer_t peer_id) if (!peer) return; - enet_peer_disconnect(peer, 0); + enet_peer_disconnect_later(peer, 0); // Actual handling done in async event handler } diff --git a/src/core/macros.h b/src/core/macros.h index 935478c..bf6ae09 100644 --- a/src/core/macros.h +++ b/src/core/macros.h @@ -17,6 +17,8 @@ typedef std::unique_lock SimpleLock; typedef uint32_t peer_t; // same as in ENetPeer +void sleep_ms(long delay); + // Auto-unlock wrapper for larger operations template class PtrLock { diff --git a/src/gui/gameplay.cpp b/src/gui/gameplay.cpp index aa48f06..e624238 100644 --- a/src/gui/gameplay.cpp +++ b/src/gui/gameplay.cpp @@ -80,7 +80,7 @@ void SceneGameplay::draw() core::dimension2di(wsize.Width - SIZEW, wsize.Height - 50 - 160) ); if (m_chathistory_text.empty()) - m_chathistory_text = L"Chat history:\n"; + m_chathistory_text = L"--- Start of chat history ---\n"; auto e = m_gui->gui->addEditBox(m_chathistory_text.c_str(), rect_ch, true); e->setAutoScroll(true); @@ -88,7 +88,7 @@ void SceneGameplay::draw() e->setWordWrap(true); e->setEnabled(false); e->setDrawBackground(false); - e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT); + e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_LOWERRIGHT); e->setOverrideColor(0xFFCCCCCC); m_chathistory = e; } @@ -209,6 +209,16 @@ bool SceneGameplay::OnEvent(const SEvent &e) } break; case EMIE_MOUSE_WHEEL: + { + core::vector2di pos(e.MouseInput.X, e.MouseInput.Y); + auto root = m_gui->gui->getRootGUIElement(); + auto element = root->getElementFromPoint(pos); + if (element && element != root) { + // Forward inputs to the corresponding element + return false; + } + } + { float dir = e.MouseInput.Wheel > 0 ? 1 : -1; @@ -326,11 +336,8 @@ bool SceneGameplay::OnEvent(GameEvent &e) } return true; case E::C2G_DIALOG: - printf(" * SYSTEM: %s\n", e.text->c_str()); + // TODO: show an actual dialog? break; - case E::C2G_DISCONNECT: - m_gui->disconnect(); - return true; default: break; } return false; @@ -389,7 +396,6 @@ video::ITexture *SceneGameplay::generateTexture(const wchar_t *text, u32 color) return texture; } - void SceneGameplay::updateWorld() { auto world = m_gui->getClient()->getWorld(); diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 3eb62a1..d47baa1 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -9,7 +9,7 @@ #include "lobby.h" #include "gameplay.h" -void sleep_ms(long delay); +extern const char *VERSION_STRING; Gui::Gui() { @@ -20,7 +20,12 @@ Gui::Gui() ASSERT_FORCED(device, "Failed to initialize driver"); - device->setWindowCaption(L"OpenEdits v1.0.2-dev"); + { + // Title bar + core::stringw version; + core::multibyteToWString(version, VERSION_STRING); + device->setWindowCaption(version.c_str()); + } scenemgr = device->getSceneManager(); gui = device->getGUIEnvironment(); @@ -95,6 +100,18 @@ void Gui::run() t_last = t_now; } + if (m_pending_disconnect) { + if (m_client) { + delete m_client; + m_client = nullptr; + } + if (m_server) { + delete m_server; + m_server = nullptr; + } + m_pending_disconnect = false; + } + if (m_client) { m_client->step(dtime); } @@ -171,6 +188,10 @@ bool Gui::OnEvent(GameEvent &e) if (!m_initialized) return false; + if (e.type_c2g == GameEvent::C2G_DISCONNECT) { + disconnect(); + return true; + } return getHandler(m_scenetype)->OnEvent(e); } @@ -222,12 +243,8 @@ void Gui::connect(SceneConnect *sc) m_client->setEventHandler(this); setNextScene(SceneHandlerType::Lobby); } else { - puts("Wait timed out."); - delete m_client; - m_client = nullptr; - - delete m_server; - m_server = nullptr; + puts("Connection timed out: Server is not reachable."); + m_pending_disconnect = true; } } @@ -235,15 +252,7 @@ void Gui::disconnect() { setNextScene(SceneHandlerType::Connect); - if (m_client) { - delete m_client; - m_client = nullptr; - } - - if (m_server) { - delete m_server; - m_server = nullptr; - } + m_pending_disconnect = true; } void Gui::joinWorld(SceneLobby *sc) diff --git a/src/gui/gui.h b/src/gui/gui.h index 985ef86..7bcd6da 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -79,6 +79,7 @@ class Gui : public IEventReceiver, public GameEventHandler { inline void setNextScene(SceneHandlerType type) { m_scenetype_next = type; } bool m_initialized = false; + bool m_pending_disconnect = false; SceneHandlerType m_scenetype_next; SceneHandlerType m_scenetype; diff --git a/src/main.cpp b/src/main.cpp index c3b1d76..a0dcf8c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,8 @@ void sleep_ms(long delay) std::this_thread::sleep_for(std::chrono::milliseconds(delay)); } +const char *VERSION_STRING = "OpenEdits v1.0.3-dev"; + void unittest(); static void exit_cleanup() @@ -86,9 +88,18 @@ int main(int argc, char *argv[]) g_blockmanager = new BlockManager(); register_packs(); - if (argc >= 2 && strcmp(argv[1], "--unittest") == 0) { - unittest(); - return EXIT_SUCCESS; + if (argc >= 2) { + if (strcmp(argv[1], "--version") == 0) { + puts(VERSION_STRING); + return EXIT_SUCCESS; + } + if (strcmp(argv[1], "--unittest") == 0) { + // Depends on BlockManager and ENet + unittest(); + return EXIT_SUCCESS; + } + puts("-!- Unknown command line option."); + return EXIT_FAILURE; } Gui gui;