diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index 584970303b..53e7123f62 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -69,6 +69,7 @@ namespace Script { class Interface; } namespace Script { class Request; } struct SDL_Event_; struct SGUIImageEffects; +union SDL_Event; extern const double SELECT_DBLCLICK_RATE; @@ -132,11 +133,11 @@ public: void DrawSprite(const CGUISpriteInstance& Sprite, CCanvas2D& canvas, const CRect& Rect, const CRect& Clipping = CRect()); /** - * The replacement of Process(), handles an SDL_Event_ + * The replacement of Process(), handles an SDL_Event * * @param ev SDL Event, like mouse/keyboard input */ - InReaction HandleEvent(const SDL_Event_* ev); + InReaction HandleEvent(const SDL_Event& ev); /** * Load a GUI XML file into the GUI. diff --git a/source/gui/Scripting/GuiScriptConversions.cpp b/source/gui/Scripting/GuiScriptConversions.cpp index 824beacd59..c3acb4058a 100644 --- a/source/gui/Scripting/GuiScriptConversions.cpp +++ b/source/gui/Scripting/GuiScriptConversions.cpp @@ -59,7 +59,7 @@ template<> void Script::ToJSVal(const Script::Request& rq, JS::Mutab { const char* typeName; - switch (val.ev.type) + switch (ev.type) { case SDL_WINDOWEVENT: typeName = "windowevent"; break; case SDL_KEYDOWN: typeName = "keydown"; break; @@ -85,13 +85,13 @@ template<> void Script::ToJSVal(const Script::Request& rq, JS::Mutab SET(obj, "type", typeName); - switch (val.ev.type) + switch (ev.type) { case SDL_KEYDOWN: case SDL_KEYUP: { - // SET(obj, "which", (int)val.ev.key.which); // (not in wsdl.h) - // SET(obj, "state", (int)val.ev.key.state); // (not in wsdl.h) + // SET(obj, "which", static_cast(ev.key.which)); // (not in wsdl.h) + // SET(obj, "state", static_cast(ev.key.state)); // (not in wsdl.h) JS::RootedObject keysym(rq.cx, JS_NewPlainObject(rq.cx)); if (!keysym) @@ -102,9 +102,9 @@ template<> void Script::ToJSVal(const Script::Request& rq, JS::Mutab JS::RootedValue keysymVal(rq.cx, JS::ObjectValue(*keysym)); JS_SetProperty(rq.cx, obj, "keysym", keysymVal); - // SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h) - SET(keysym, "sym", (int)val.ev.key.keysym.sym); - // SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h) + // SET(keysym, "scancode", static_cast(ev.key.keysym.scancode)); // (not in wsdl.h) + SET(keysym, "sym", static_cast(ev.key.keysym.sym)); + // SET(keysym, "mod", static_cast(ev.key.keysym.mod)); // (not in wsdl.h) { SET(keysym, "unicode", JS::UndefinedHandleValue); } @@ -115,23 +115,23 @@ template<> void Script::ToJSVal(const Script::Request& rq, JS::Mutab } case SDL_MOUSEMOTION: { - // SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h) - // SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h) - SET(obj, "x", (int)val.ev.motion.x); - SET(obj, "y", (int)val.ev.motion.y); - // SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h) - // SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h) + // SET(obj, "which", static_cast(ev.motion.which)); // (not in wsdl.h) + // SET(obj, "state", static_cast(ev.motion.state)); // (not in wsdl.h) + SET(obj, "x", static_cast(ev.motion.x)); + SET(obj, "y", static_cast(ev.motion.y)); + // SET(obj, "xrel", static_cast(ev.motion.xrel)); // (not in wsdl.h) + // SET(obj, "yrel", static_cast(ev.motion.yrel)); // (not in wsdl.h) break; } case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { - // SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h) - SET(obj, "button", (int)val.ev.button.button); - SET(obj, "state", (int)val.ev.button.state); - SET(obj, "x", (int)val.ev.button.x); - SET(obj, "y", (int)val.ev.button.y); - SET(obj, "clicks", (int)val.ev.button.clicks); + // SET(obj, "which", static_cast(ev.button.which)); // (not in wsdl.h) + SET(obj, "button", static_cast(ev.button.button)); + SET(obj, "state", static_cast(ev.button.state)); + SET(obj, "x", static_cast(ev.button.x)); + SET(obj, "y", static_cast(ev.button.y)); + SET(obj, "clicks", static_cast(ev.button.clicks)); break; } case SDL_HOTKEYPRESS: @@ -140,7 +140,7 @@ template<> void Script::ToJSVal(const Script::Request& rq, JS::Mutab case SDL_HOTKEYPRESS_SILENT: case SDL_HOTKEYUP_SILENT: { - SET(obj, "hotkey", static_cast(val.ev.user.data1)); + SET(obj, "hotkey", static_cast(ev.user.data1)); break; } } diff --git a/source/network/NetClient.cpp b/source/network/NetClient.cpp index f202a2fe48..3c19ce41e5 100644 --- a/source/network/NetClient.cpp +++ b/source/network/NetClient.cpp @@ -159,6 +159,8 @@ CNetClient::CNetClient(CGame* game, const CStrW& username, const CStr& hostJID, CNetClient::~CNetClient() { + Unregister(nullptr); + // Try to flush messages before dying (probably fails). if (m_ClientTurnManager) m_ClientTurnManager->OnDestroyConnection(); @@ -364,17 +366,25 @@ void CNetClient::CheckServerConnection() JSObject* CNetClient::GetNextGUIMessage(const Script::Interface& guiInterface) { - const Script::Request rq{guiInterface}; + Unregister(nullptr); + const ScriptRequest rq{guiInterface}; m_GuiMessagePoll.emplace(GuiPollData{guiInterface, {rq.cx, JS::NewPromiseObject(rq.cx, nullptr)}}); FetchMessage(); return m_GuiMessagePoll.value().promise; } -void CNetClient::Unregister(const Script::Interface& guiInterface) +void CNetClient::Unregister(const Script::Interface* guiInterface) { - if (m_GuiMessagePoll.has_value() && &m_GuiMessagePoll.value().interface == &guiInterface) - m_GuiMessagePoll.reset(); + if (!m_GuiMessagePoll.has_value() || + (guiInterface && &m_GuiMessagePoll.value().interface != guiInterface)) + { + return; + } + auto& [interface, promise] = m_GuiMessagePoll.value(); + const ScriptRequest oldRq{interface}; + JS::ResolvePromise(oldRq.cx, promise, JS::UndefinedHandleValue); + m_GuiMessagePoll.reset(); } void CNetClient::FetchMessage() diff --git a/source/ps/GameSetup/GameSetup.cpp b/source/ps/GameSetup/GameSetup.cpp index 90ed01b97c..bdb293e648 100644 --- a/source/ps/GameSetup/GameSetup.cpp +++ b/source/ps/GameSetup/GameSetup.cpp @@ -541,7 +541,7 @@ bool Init(const CmdLineArgs& args, int flags) new CProfileViewer; new CProfileManager; // before any script code - g_ScriptStatsTable = new CScriptStatsTable; + g_ScriptStatsTable = new Script::CScriptStatsTable; g_ProfileViewer.AddRootTable(g_ScriptStatsTable); // Set up the console early, so that debugging diff --git a/source/ps/Replay.cpp b/source/ps/Replay.cpp index 045e2b358c..8ef85fa93d 100644 --- a/source/ps/Replay.cpp +++ b/source/ps/Replay.cpp @@ -198,7 +198,7 @@ void CReplayPlayer::Replay(const int serializationtestturn, const int rejointest new CProfileViewer; new CProfileManager; - g_ScriptStatsTable = new CScriptStatsTable; + g_ScriptStatsTable = new Script::CScriptStatsTable; g_ProfileViewer.AddRootTable(g_ScriptStatsTable); const int contextSize = 384 * 1024 * 1024; diff --git a/source/scriptinterface/Stats.cpp b/source/scriptinterface/Stats.cpp index 0f83c59e2d..c80d1d3501 100644 --- a/source/scriptinterface/Stats.cpp +++ b/source/scriptinterface/Stats.cpp @@ -24,7 +24,7 @@ #include #include -CScriptStatsTable* g_ScriptStatsTable; +Script::CScriptStatsTable* g_ScriptStatsTable; enum { @@ -34,16 +34,16 @@ enum NumberRows }; -CScriptStatsTable::CScriptStatsTable() +Script::CScriptStatsTable::CScriptStatsTable() { } -void CScriptStatsTable::Add(const Script::Interface* scriptInterface, const std::string& title) +void Script::CScriptStatsTable::Add(const Script::Interface* scriptInterface, const std::string& title) { m_ScriptInterfaces.emplace_back(scriptInterface, title); } -void CScriptStatsTable::Remove(const Script::Interface* scriptInterface) +void Script::CScriptStatsTable::Remove(const Script::Interface* scriptInterface) { for (size_t i = 0; i < m_ScriptInterfaces.size(); ) { @@ -54,22 +54,22 @@ void CScriptStatsTable::Remove(const Script::Interface* scriptInterface) } } -CStr CScriptStatsTable::GetName() +CStr Script::CScriptStatsTable::GetName() { return "script"; } -CStr CScriptStatsTable::GetTitle() +CStr Script::CScriptStatsTable::GetTitle() { return "Script statistics"; } -size_t CScriptStatsTable::GetNumberRows() +size_t Script::CScriptStatsTable::GetNumberRows() { return NumberRows; } -const std::vector& CScriptStatsTable::GetColumns() +const std::vector& Script::CScriptStatsTable::GetColumns() { m_ColumnDescriptions.clear(); m_ColumnDescriptions.push_back(ProfileColumn("Name", 200)); @@ -78,7 +78,7 @@ const std::vector& CScriptStatsTable::GetColumns() return m_ColumnDescriptions; } -CStr CScriptStatsTable::GetCellText(size_t row, size_t col) +CStr Script::CScriptStatsTable::GetCellText(size_t row, size_t col) { switch(row) { @@ -108,7 +108,7 @@ CStr CScriptStatsTable::GetCellText(size_t row, size_t col) } } -AbstractProfileTable* CScriptStatsTable::GetChild(size_t /*row*/) +AbstractProfileTable* Script::CScriptStatsTable::GetChild(size_t /*row*/) { return 0; } diff --git a/source/scriptinterface/Stats.h b/source/scriptinterface/Stats.h index 69b97f1dc8..9dba00ef19 100644 --- a/source/scriptinterface/Stats.h +++ b/source/scriptinterface/Stats.h @@ -15,8 +15,8 @@ * along with 0 A.D. If not, see . */ -#ifndef INCLUDED_SCRIPTSTATS -#define INCLUDED_SCRIPTSTATS +#ifndef INCLUDED_SCRIPT_STATS +#define INCLUDED_SCRIPT_STATS #include "lib/code_annotation.h" #include "ps/CStr.h" @@ -29,6 +29,8 @@ namespace Script { class Interface; } +namespace Script { + class CScriptStatsTable : public AbstractProfileTable { NONCOPYABLE(CScriptStatsTable); @@ -50,8 +52,10 @@ private: std::vector m_ColumnDescriptions; }; +} + // To simplify the UI we want to use a single table for all script interfaces, // so just make it a global that they can all add themselves to -extern CScriptStatsTable* g_ScriptStatsTable; +extern Script::CScriptStatsTable* g_ScriptStatsTable; -#endif // INCLUDED_SCRIPTSTATS +#endif // INCLUDED_SCRIPT_STATS