From 4bbd9c7b8e036813e81941075f872dc3962cc0ec Mon Sep 17 00:00:00 2001 From: Youssef Maged Date: Wed, 9 Sep 2026 04:27:30 +0300 Subject: [PATCH 1/9] Refactor Browser Functions to use new parser --- .../logic/luadefs/CLuaBrowserDefs.cpp | 981 +++++------------- .../logic/luadefs/CLuaBrowserDefs.h | 63 +- 2 files changed, 284 insertions(+), 760 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp index 06ce7a875b2..fb21467c5b3 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp @@ -17,36 +17,36 @@ void CLuaBrowserDefs::LoadFunctions() { // Define browser functions constexpr static const std::pair functions[]{ - {"createBrowser", CreateBrowser}, - {"requestBrowserDomains", RequestBrowserDomains}, - {"loadBrowserURL", LoadBrowserURL}, - {"isBrowserLoading", IsBrowserLoading}, - {"injectBrowserMouseMove", InjectBrowserMouseMove}, - {"injectBrowserMouseDown", InjectBrowserMouseDown}, - {"injectBrowserMouseUp", InjectBrowserMouseUp}, - {"injectBrowserMouseWheel", InjectBrowserMouseWheel}, - {"getBrowserTitle", GetBrowserTitle}, - {"getBrowserURL", GetBrowserURL}, - {"setBrowserRenderingPaused", SetBrowserRenderingPaused}, + {"createBrowser", ArgumentParserWarn}, + {"requestBrowserDomains", ArgumentParserWarn}, + {"loadBrowserURL", ArgumentParserWarn}, + {"isBrowserLoading", ArgumentParserWarn}, + {"injectBrowserMouseMove", ArgumentParserWarn}, + {"injectBrowserMouseDown", ArgumentParserWarn}, + {"injectBrowserMouseUp", ArgumentParserWarn}, + {"injectBrowserMouseWheel", ArgumentParserWarn}, + {"getBrowserTitle", ArgumentParserWarn}, + {"getBrowserURL", ArgumentParserWarn}, + {"setBrowserRenderingPaused", ArgumentParserWarn}, {"isBrowserRenderingPaused", ArgumentParser}, - {"executeBrowserJavascript", ExecuteBrowserJavascript}, + {"executeBrowserJavascript", ArgumentParserWarn}, {"getBrowserVolume", GetBrowserVolume}, - {"setBrowserVolume", SetBrowserVolume}, - {"isBrowserDomainBlocked", IsBrowserDomainBlocked}, - {"focusBrowser", FocusBrowser}, - {"isBrowserFocused", IsBrowserFocused}, - {"setBrowserProperty", SetBrowserProperty}, - {"getBrowserProperty", GetBrowserProperty}, - {"getBrowserSettings", GetBrowserSettings}, - {"getBrowserSource", GetBrowserSource}, - {"setBrowserAjaxHandler", SetBrowserAjaxHandler}, - {"canBrowserNavigateBack", CanBrowserNavigateBack}, - {"canBrowserNavigateForward", CanBrowserNavigateForward}, - {"navigateBrowserBack", NavigateBrowserBack}, - {"navigateBrowserForward", NavigateBrowserForward}, - {"reloadBrowserPage", ReloadBrowserPage}, - {"toggleBrowserDevTools", ToggleBrowserDevTools}, - {"resizeBrowser", ResizeBrowser}, + {"setBrowserVolume", ArgumentParserWarn}, + {"isBrowserDomainBlocked", ArgumentParserWarn}, + {"focusBrowser", ArgumentParserWarn}, + {"isBrowserFocused", ArgumentParserWarn}, + {"setBrowserProperty", ArgumentParserWarn}, + {"getBrowserProperty", ArgumentParserWarn}, + {"getBrowserSettings", ArgumentParserWarn}, + {"getBrowserSource", ArgumentParserWarn}, + {"setBrowserAjaxHandler", ArgumentParserWarn}, + {"canBrowserNavigateBack", ArgumentParserWarn}, + {"canBrowserNavigateForward", ArgumentParserWarn}, + {"navigateBrowserBack", ArgumentParserWarn}, + {"navigateBrowserForward", ArgumentParserWarn}, + {"reloadBrowserPage", ArgumentParserWarn}, + {"toggleBrowserDevTools", ArgumentParserWarn}, + {"resizeBrowser", ArgumentParserWarn}, {"guiCreateBrowser", GUICreateBrowser}, {"guiGetBrowser", GUIGetBrowser}, {"isBrowserGPUEnabled", ArgumentParser}, @@ -110,49 +110,22 @@ void CLuaBrowserDefs::AddClass(lua_State* luaVM) lua_registerclass(luaVM, "GuiBrowser", "GuiElement"); } -int CLuaBrowserDefs::CreateBrowser(lua_State* luaVM) +std::variant CLuaBrowserDefs::CreateBrowser(lua_State* luaVM, CVector2D vecSize, bool bIsLocal, std::optional bTransparent) { // texture createBrowser ( int width, int height, bool isLocal [, bool transparent = false] ) - CVector2D vecSize; - bool bIsLocal; - bool bTransparent; - - CScriptArgReader argStream(luaVM); - argStream.ReadVector2D(vecSize); - argStream.ReadBool(bIsLocal); - argStream.ReadBool(bTransparent, false); - - if (!argStream.HasErrors()) - { - if (vecSize.fX < 0) - { - argStream.SetCustomError("Browser width is smaller than 0", "Invalid parameter"); - } - else if (vecSize.fY < 0) - { - argStream.SetCustomError("Browser height is smaller than 0", "Invalid parameter"); - } - else if (vecSize.fX == 0 || vecSize.fY == 0) - { - argStream.SetCustomError("A browser must be at least 1x1 in size.", "Invalid parameter"); - } - } - - if (argStream.HasErrors()) - return luaL_error(luaVM, argStream.GetFullErrorMessage()); + if (vecSize.fX < 0) + throw std::invalid_argument("Browser width is smaller than 0"); + else if (vecSize.fY < 0) + throw std::invalid_argument("Browser height is smaller than 0"); + else if (vecSize.fX == 0 || vecSize.fY == 0) + throw std::invalid_argument("A browser must be at least 1x1 in size."); const auto pWebCore = g_pCore->GetWebCore(); if (!pWebCore) - { - lua_pushboolean(luaVM, false); - return 1; - } + return false; if (!bIsLocal && !pWebCore->GetRemotePagesEnabled()) - { - lua_pushboolean(luaVM, false); - return 1; - } + return false; CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); if (pLuaMain) @@ -160,7 +133,7 @@ int CLuaBrowserDefs::CreateBrowser(lua_State* luaVM) CResource* pParentResource = pLuaMain->GetResource(); CClientWebBrowser* pBrowserTexture = - g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser((int)vecSize.fX, (int)vecSize.fY, bIsLocal, bTransparent); + g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser((int)vecSize.fX, (int)vecSize.fY, bIsLocal, bTransparent.value_or(false)); if (pBrowserTexture) { // Make it a child of the resource's file root ** CHECK Should parent be pFileResource, and element added to pParentResource's ElementGroup? ** @@ -169,312 +142,138 @@ int CLuaBrowserDefs::CreateBrowser(lua_State* luaVM) // Set our owner resource pBrowserTexture->SetResource(pParentResource); } - lua_pushelement(luaVM, pBrowserTexture); - return 1; + return pBrowserTexture; } - lua_pushboolean(luaVM, false); - return 1; + return false; } -int CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM) +bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional bIsURL, + std::optional callbackFunction) { // bool requestBrowserDomains ( table domains, bool isURL [, function callback ] ) - std::vector pages; - bool bIsURL; - CLuaFunctionRef callbackFunction; + std::vector sPages(pages.begin(), pages.end()); - CScriptArgReader argStream(luaVM); - argStream.ReadStringTable(pages); - argStream.ReadBool(bIsURL, false); - argStream.ReadFunction(callbackFunction, LUA_REFNIL); - argStream.ReadFunctionComplete(); + // Remove whitespaces + for (auto& url : sPages) + url.erase(std::remove_if(url.begin(), url.end(), [](unsigned char c) { return std::isspace(c); }), url.end()); - if (!argStream.HasErrors()) - { - // Remove whitespaces - for (auto& url : pages) - url.erase(std::remove_if(url.begin(), url.end(), [](unsigned char c) { return std::isspace(c); }), url.end()); + // Remove empty and invalid URLs + std::regex invalidSynmbolsRegex("[^A-Za-z0-9._~!#$&'()*+,;=:@/?%-]"); - // Remove empty and invalid URLs - std::regex invalidSynmbolsRegex("[^A-Za-z0-9._~!#$&'()*+,;=:@/?%-]"); + sPages.erase(std::remove_if(sPages.begin(), sPages.end(), + [&invalidSynmbolsRegex](const auto& url) { return url.empty() || std::regex_search(url, invalidSynmbolsRegex); }), + sPages.end()); - pages.erase(std::remove_if(pages.begin(), pages.end(), - [&invalidSynmbolsRegex](const auto& url) { return url.empty() || std::regex_search(url, invalidSynmbolsRegex); }), - pages.end()); - - // Convert to domains if we got a list of URLs - if (bIsURL) - { - auto pWebCore = g_pCore->GetWebCore(); - if (!pWebCore) - { - lua_pushboolean(luaVM, false); - return 1; - } - std::transform(pages.begin(), pages.end(), pages.begin(), [pWebCore](const auto& url) { return pWebCore->GetDomainFromURL(url); }); - } + // Convert to domains if we got a list of URLs + if (bIsURL.value_or(false)) + { + auto pWebCore = g_pCore->GetWebCore(); + if (!pWebCore) + return false; + std::transform(sPages.begin(), sPages.end(), sPages.begin(), [pWebCore](const auto& url) { return pWebCore->GetDomainFromURL(url); }); + } - WebRequestCallback callback = [=](bool bAllow, const std::unordered_set& domains) + CLuaFunctionRef functionRef = callbackFunction.value_or(CLuaFunctionRef()); + WebRequestCallback callback = [=](bool bAllow, const std::unordered_set& domains) + { + // Test if luaVM is still available + if (m_pLuaManager->IsLuaVMValid(luaVM) && VERIFY_FUNCTION(functionRef)) { - // Test if luaVM is still available - if (m_pLuaManager->IsLuaVMValid(luaVM) && VERIFY_FUNCTION(callbackFunction)) - { - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (!pLuaMain) - return; + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (!pLuaMain) + return; - CLuaArguments arguments; - arguments.PushBoolean(bAllow); + CLuaArguments arguments; + arguments.PushBoolean(bAllow); - CLuaArguments LuaTable; - int i = 0; - for (const auto& domain : domains) - { - LuaTable.PushNumber(++i); - LuaTable.PushString(domain); - } - arguments.PushTable(&LuaTable); - arguments.Call(pLuaMain, callbackFunction); + CLuaArguments LuaTable; + int i = 0; + for (const auto& domain : domains) + { + LuaTable.PushNumber(++i); + LuaTable.PushString(domain); } - }; - - auto pWebCore = g_pCore->GetWebCore(); - if (!pWebCore) - { - lua_pushboolean(luaVM, false); - return 1; + arguments.PushTable(&LuaTable); + arguments.Call(pLuaMain, functionRef); } - pWebCore->RequestPages(pages, VERIFY_FUNCTION(callbackFunction) ? &callback : nullptr); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); + }; - lua_pushboolean(luaVM, false); - return 1; + auto pWebCore = g_pCore->GetWebCore(); + if (!pWebCore) + return false; + pWebCore->RequestPages(sPages, VERIFY_FUNCTION(functionRef) ? &callback : nullptr); + return true; } -int CLuaBrowserDefs::LoadBrowserURL(lua_State* luaVM) +bool CLuaBrowserDefs::LoadBrowserURL(CClientWebBrowser* pWebBrowser, const std::string strURL, std::optional strPostData, + std::optional bURLEncoded) { // bool loadBrowserURL ( browser webBrowser, string url [, string postData = "", bool postURLEncoded = true ] ) - CClientWebBrowser* pWebBrowser; - SString strURL; - SString strPostData; - bool bURLEncoded; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadString(strURL); - argStream.ReadString(strPostData, ""); - argStream.ReadBool(bURLEncoded, true); - - if (!argStream.HasErrors()) + // Are we dealing with a remote website? + if (strURL.substr(0, 7) == "http://" || strURL.substr(0, 8) == "https://") { - // Are we dealing with a remote website? - if (strURL.substr(0, 7) == "http://" || strURL.substr(0, 8) == "https://") - { - bool isLocalURL = strURL.substr(0, 11) == "http://mta/"; - if (pWebBrowser->IsLocal() != isLocalURL) - { - lua_pushboolean(luaVM, false); - return 1; - } + bool isLocalURL = strURL.substr(0, 11) == "http://mta/"; + if (pWebBrowser->IsLocal() != isLocalURL) + return false; - lua_pushboolean(luaVM, pWebBrowser->LoadURL(strURL, !isLocalURL, strPostData, bURLEncoded)); - return 1; - } - else - { - argStream.SetCustomError("Invalid URL scheme provided. Only http:// and https:// is supported.", "Invalid parameter"); - } + return pWebBrowser->LoadURL(strURL, !isLocalURL, strPostData.value_or(""), bURLEncoded.value_or(true)); } - if (argStream.HasErrors()) - return luaL_error(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + throw std::invalid_argument("Invalid URL scheme provided. Only http:// and https:// is supported."); } -int CLuaBrowserDefs::IsBrowserLoading(lua_State* luaVM) +bool CLuaBrowserDefs::IsBrowserLoading(CClientWebBrowser* pWebBrowser) { // bool isBrowserLoading(browser webBrowser) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->IsLoading()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + return pWebBrowser->IsLoading(); } -int CLuaBrowserDefs::InjectBrowserMouseMove(lua_State* luaVM) +bool CLuaBrowserDefs::InjectBrowserMouseMove(CClientWebBrowser* pWebBrowser, CVector2D vecPosition) { // bool injectBrowserMouseMove(browser webBrowser, int x, int y) - CClientWebBrowser* pWebBrowser; - CVector2D vecPosition; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadVector2D(vecPosition); - - if (!argStream.HasErrors()) - { - pWebBrowser->InjectMouseMove((int)vecPosition.fX, (int)vecPosition.fY); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser->InjectMouseMove((int)vecPosition.fX, (int)vecPosition.fY); + return true; } -int CLuaBrowserDefs::InjectBrowserMouseDown(lua_State* luaVM) +bool CLuaBrowserDefs::InjectBrowserMouseDown(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton, std::optional doubleClick) { // bool injectBrowserMouseDown ( browser webBrowser, string mouseButton [ , bool doubleClick = false ] ) - CClientWebBrowser* pWebBrowser{}; - eWebBrowserMouseButton mouseButton{}; - bool doubleClick{}; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadEnumString(mouseButton); - argStream.ReadBool(doubleClick, false); - - if (!argStream.HasErrors()) - { - pWebBrowser->InjectMouseDown(mouseButton, doubleClick ? 2 : 1); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser->InjectMouseDown(mouseButton, doubleClick.value_or(false) ? 2 : 1); + return true; } -int CLuaBrowserDefs::InjectBrowserMouseUp(lua_State* luaVM) +bool CLuaBrowserDefs::InjectBrowserMouseUp(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton) { // bool injectBrowserMouseUp ( browser webBrowser, string mouseButton ) - CClientWebBrowser* pWebBrowser; - eWebBrowserMouseButton mouseButton; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadEnumString(mouseButton); - - if (!argStream.HasErrors()) - { - pWebBrowser->InjectMouseUp(mouseButton); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser->InjectMouseUp(mouseButton); + return true; } -int CLuaBrowserDefs::InjectBrowserMouseWheel(lua_State* luaVM) +bool CLuaBrowserDefs::InjectBrowserMouseWheel(CClientWebBrowser* pWebBrowser, int iScrollVert, int iScrollHorz) { // bool injectMouseWheel ( browser webBrowser, int scrollVertical, int scrollHorizontal ) - CClientWebBrowser* pWebBrowser; - int iScrollVert; - int iScrollHorz; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadNumber(iScrollVert); - argStream.ReadNumber(iScrollHorz); - - if (!argStream.HasErrors()) - { - pWebBrowser->InjectMouseWheel(iScrollVert, iScrollHorz); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser->InjectMouseWheel(iScrollVert, iScrollHorz); + return true; } -int CLuaBrowserDefs::GetBrowserTitle(lua_State* luaVM) +SString CLuaBrowserDefs::GetBrowserTitle(CClientWebBrowser* pWebBrowser) { // string getBrowserPageTitle ( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushstring(luaVM, pWebBrowser->GetTitle()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return pWebBrowser->GetTitle(); } -int CLuaBrowserDefs::GetBrowserURL(lua_State* luaVM) +SString CLuaBrowserDefs::GetBrowserURL(CClientWebBrowser* pWebBrowser) { // string getBrowserURL ( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushstring(luaVM, pWebBrowser->GetURL()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return pWebBrowser->GetURL(); } -int CLuaBrowserDefs::SetBrowserRenderingPaused(lua_State* luaVM) +bool CLuaBrowserDefs::SetBrowserRenderingPaused(CClientWebBrowser* pWebBrowser, bool bPaused) { // bool setBrowserRenderingPaused ( browser webBrowser, bool paused ) - CClientWebBrowser* pWebBrowser; - bool bPaused; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadBool(bPaused); - - if (!argStream.HasErrors()) - { - pWebBrowser->SetRenderingPaused(bPaused); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser->SetRenderingPaused(bPaused); + return true; } bool CLuaBrowserDefs::IsBrowserRenderingPaused(CClientWebBrowser* browser) @@ -482,32 +281,13 @@ bool CLuaBrowserDefs::IsBrowserRenderingPaused(CClientWebBrowser* browser) return browser->GetRenderingPaused(); } -int CLuaBrowserDefs::ExecuteBrowserJavascript(lua_State* luaVM) +bool CLuaBrowserDefs::ExecuteBrowserJavascript(CClientWebBrowser* pWebBrowser, const std::string strJavascriptCode) { // bool executeBrowserJavascript ( browser webBrowser, string jsCode ) - CClientWebBrowser* pWebBrowser; - SString strJavascriptCode; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadString(strJavascriptCode); - - if (!argStream.HasErrors()) - { - if (strJavascriptCode.empty() || pWebBrowser->ExecuteJavascript(strJavascriptCode)) - { - lua_pushboolean(luaVM, true); - return 1; - } - else - argStream.SetCustomError("This function does not work with remote browsers"); - } - - if (argStream.HasErrors()) - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); + if (strJavascriptCode.empty() || pWebBrowser->ExecuteJavascript(strJavascriptCode)) + return true; - lua_pushboolean(luaVM, false); - return 1; + throw std::invalid_argument("This function does not work with remote browsers"); } int CLuaBrowserDefs::GetBrowserVolume(lua_State* luaVM) @@ -530,396 +310,163 @@ int CLuaBrowserDefs::GetBrowserVolume(lua_State* luaVM) return 1; } -int CLuaBrowserDefs::SetBrowserVolume(lua_State* luaVM) +bool CLuaBrowserDefs::SetBrowserVolume(std::variant webBrowserOrVolume, std::optional fVolume) { // bool setBrowserVolume ( float volume ) // bool setBrowserVolume ( browser webBrowser, float volume ) - CClientWebBrowser* pWebBrowser; - float fVolume; - - CScriptArgReader argStream(luaVM); - if (argStream.NextIsNumber()) + if (std::holds_alternative(webBrowserOrVolume)) { - argStream.ReadNumber(fVolume); auto pWebCore = g_pCore->GetWebCore(); - lua_pushboolean(luaVM, pWebCore ? pWebCore->SetGlobalAudioVolume(fVolume) : false); - return 1; - } - - argStream.ReadUserData(pWebBrowser); - argStream.ReadNumber(fVolume); - - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->SetAudioVolume(fVolume)); - return 1; + return pWebCore ? pWebCore->SetGlobalAudioVolume(std::get(webBrowserOrVolume)) : false; } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - lua_pushboolean(luaVM, false); - return 1; + return std::get(webBrowserOrVolume)->SetAudioVolume(fVolume.value_or(0.0f)); } -int CLuaBrowserDefs::IsBrowserDomainBlocked(lua_State* luaVM) +std::optional CLuaBrowserDefs::IsBrowserDomainBlocked(const std::string strURL, std::optional bIsURL) { // bool isBrowserDomainBlocked ( string domain, bool isURL ) - SString strURL; - bool bIsURL; - - CScriptArgReader argStream(luaVM); - argStream.ReadString(strURL); - argStream.ReadBool(bIsURL, false); - - if (!argStream.HasErrors()) - { - auto pWebCore = g_pCore->GetWebCore(); - if (!pWebCore) - { - lua_pushnil(luaVM); - return 1; - } + auto pWebCore = g_pCore->GetWebCore(); + if (!pWebCore) + return std::nullopt; - if (bIsURL) - strURL = pWebCore->GetDomainFromURL(strURL); + SString strDomain = SString(strURL); + if (bIsURL.value_or(false)) + strDomain = pWebCore->GetDomainFromURL(strDomain); - if (!strURL.empty()) - { - lua_pushboolean(luaVM, pWebCore->GetDomainState(strURL) != eURLState::WEBPAGE_ALLOWED); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); + if (!strDomain.empty()) + return pWebCore->GetDomainState(strDomain) != eURLState::WEBPAGE_ALLOWED; - lua_pushnil(luaVM); - return 1; + return std::nullopt; } -int CLuaBrowserDefs::FocusBrowser(lua_State* luaVM) +bool CLuaBrowserDefs::FocusBrowser(std::optional pWebBrowser) { // focusBrowser ( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - if (argStream.NextIsNil() || argStream.NextIsNone()) + if (!pWebBrowser.has_value()) { auto pWebCore = g_pCore->GetWebCore(); if (pWebCore) pWebCore->SetFocusedWebView(NULL); - lua_pushboolean(luaVM, true); - return 1; - } - - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - pWebBrowser->Focus(); - lua_pushboolean(luaVM, true); - return 1; + return true; } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser.value()->Focus(); + return true; } -int CLuaBrowserDefs::IsBrowserFocused(lua_State* luaVM) +bool CLuaBrowserDefs::IsBrowserFocused(CClientWebBrowser* pWebBrowser) { - // browser isBrowserFocused () - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - auto pWebCore = g_pCore->GetWebCore(); - CWebViewInterface* pWebView = pWebCore ? pWebCore->GetFocusedWebView() : nullptr; - lua_pushboolean(luaVM, pWebBrowser->GetWebView() == pWebView); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + // browser isBrowserFocused ( browser webBrowser ) + auto pWebCore = g_pCore->GetWebCore(); + CWebViewInterface* pWebView = pWebCore ? pWebCore->GetFocusedWebView() : nullptr; + return pWebBrowser->GetWebView() == pWebView; } -int CLuaBrowserDefs::SetBrowserProperty(lua_State* luaVM) +bool CLuaBrowserDefs::SetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey, const std::string strValue) { // bool setBrowserProperty ( browser webBrowser, string key, string value ) - CClientWebBrowser* pWebBrowser; - SString strKey; - SString strValue; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadString(strKey); - argStream.ReadString(strValue); - - if (!argStream.HasErrors()) - { - if (pWebBrowser->SetProperty(strKey, strValue)) - { - lua_pushboolean(luaVM, true); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return pWebBrowser->SetProperty(strKey, strValue); } -int CLuaBrowserDefs::GetBrowserProperty(lua_State* luaVM) +std::optional CLuaBrowserDefs::GetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey) { // string getBrowserProperty ( browser webBrowser, string key ) - CClientWebBrowser* pWebBrowser; - SString strKey; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadString(strKey); - - if (!argStream.HasErrors()) - { - SString strValue; - if (pWebBrowser->GetProperty(strKey, strValue)) - { - lua_pushstring(luaVM, strValue); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); + SString strValue; + if (pWebBrowser->GetProperty(strKey, strValue)) + return strValue; - lua_pushnil(luaVM); - return 1; + return std::nullopt; } -int CLuaBrowserDefs::GetBrowserSettings(lua_State* luaVM) +std::unordered_map CLuaBrowserDefs::GetBrowserSettings() { // table getBrowserSettings () auto pWebCore = g_pCore->GetWebCore(); - lua_createtable(luaVM, 0, 3); - - lua_pushstring(luaVM, "RemoteEnabled"); - lua_pushboolean(luaVM, pWebCore ? pWebCore->GetRemotePagesEnabled() : false); - lua_settable(luaVM, -3); - - lua_pushstring(luaVM, "RemoteJavascript"); - lua_pushboolean(luaVM, pWebCore ? pWebCore->GetRemoteJavascriptEnabled() : false); - lua_settable(luaVM, -3); - - lua_pushstring(luaVM, "PluginsEnabled"); - lua_pushboolean(luaVM, false); - lua_settable(luaVM, -3); - - return 1; + return { + {"RemoteEnabled", pWebCore ? pWebCore->GetRemotePagesEnabled() : false}, + {"RemoteJavascript", pWebCore ? pWebCore->GetRemoteJavascriptEnabled() : false}, + {"PluginsEnabled", false}, + }; } -int CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM) +bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWebBrowser, CLuaFunctionRef callbackFunction) { - // bool getBrowserSource ( function callback ) - CClientWebBrowser* pWebBrowser; - CLuaFunctionRef callbackFunction; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadFunction(callbackFunction); - argStream.ReadFunctionComplete(); - - if (!argStream.HasErrors()) + // bool getBrowserSource ( browser webBrowser, function callback ) + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (pLuaMain && VERIFY_FUNCTION(callbackFunction)) { - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (pLuaMain && VERIFY_FUNCTION(callbackFunction)) - { - pWebBrowser->GetSourceCode( - [callbackFunction, pLuaMain, pWebBrowser](const std::string& code) + pWebBrowser->GetSourceCode( + [callbackFunction, pLuaMain, pWebBrowser](const std::string& code) + { + /* + This function should not be called when the resource is about to stop as + stopping the resource destroys the browser element and thus cancels the + CefStringVisitor callback class (see CWebView::GetSourceCode::MyStringVisitor) + */ + if (VERIFY_FUNCTION(callbackFunction)) { - /* - This function should not be called when the resource is about to stop as - stopping the resource destroys the browser element and thus cancels the - CefStringVisitor callback class (see CWebView::GetSourceCode::MyStringVisitor) - */ - if (VERIFY_FUNCTION(callbackFunction)) - { - CLuaArguments arguments; - // TODO: Use SCharStringRef/direct string access instead of copying strings around - arguments.PushString(code); - arguments.PushElement(pWebBrowser); - arguments.Call(pLuaMain, callbackFunction); - } - }); - - lua_pushboolean(luaVM, true); - return 1; - } + CLuaArguments arguments; + // TODO: Use SCharStringRef/direct string access instead of copying strings around + arguments.PushString(code); + arguments.PushElement(pWebBrowser); + arguments.Call(pLuaMain, callbackFunction); + } + }); + + return true; } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - lua_pushboolean(luaVM, false); - return 1; + return false; } -int CLuaBrowserDefs::ToggleBrowserDevTools(lua_State* luaVM) +bool CLuaBrowserDefs::ToggleBrowserDevTools(CClientWebBrowser* pWebBrowser, bool visible) { // bool toggleBrowserDevTools ( browser webBrowser, bool visible ) - CClientWebBrowser* pWebBrowser; - bool visible; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadBool(visible); - - if (!argStream.HasErrors()) - { - auto pWebCore = g_pCore->GetWebCore(); - if (pWebCore && pWebCore->IsTestModeEnabled()) - { - lua_pushboolean(luaVM, pWebBrowser->ToggleDevTools(visible)); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, "toggleBrowserDevtools can only be used in development mode"); - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); + auto pWebCore = g_pCore->GetWebCore(); + if (pWebCore && pWebCore->IsTestModeEnabled()) + return pWebBrowser->ToggleDevTools(visible); - lua_pushboolean(luaVM, false); - return 1; + throw std::invalid_argument("toggleBrowserDevtools can only be used in development mode"); } -int CLuaBrowserDefs::ResizeBrowser(lua_State* luaVM) +bool CLuaBrowserDefs::ResizeBrowser(CClientWebBrowser* pWebBrowser, CVector2D size) { // bool resizeBrowser(browser webBrowser, float width, float height) - CClientWebBrowser* pWebBrowser; - CVector2D size; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadVector2D(size); - - if (!argStream.HasErrors()) - { - pWebBrowser->Resize(size); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + pWebBrowser->Resize(size); + return true; } -int CLuaBrowserDefs::CanBrowserNavigateBack(lua_State* luaVM) +bool CLuaBrowserDefs::CanBrowserNavigateBack(CClientWebBrowser* pWebBrowser) { // bool canBrowserNavigateBack( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->CanGoBack()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + return pWebBrowser->CanGoBack(); } -int CLuaBrowserDefs::CanBrowserNavigateForward(lua_State* luaVM) +bool CLuaBrowserDefs::CanBrowserNavigateForward(CClientWebBrowser* pWebBrowser) { // bool canBrowserNavigateForward( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->CanGoForward()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + return pWebBrowser->CanGoForward(); } -int CLuaBrowserDefs::NavigateBrowserBack(lua_State* luaVM) +bool CLuaBrowserDefs::NavigateBrowserBack(CClientWebBrowser* pWebBrowser) { // bool navigateBrowserBack( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->GoBack()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + return pWebBrowser->GoBack(); } -int CLuaBrowserDefs::NavigateBrowserForward(lua_State* luaVM) +bool CLuaBrowserDefs::NavigateBrowserForward(CClientWebBrowser* pWebBrowser) { // bool navigateBrowserForward( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->GoForward()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + return pWebBrowser->GoForward(); } -int CLuaBrowserDefs::ReloadBrowserPage(lua_State* luaVM) +bool CLuaBrowserDefs::ReloadBrowserPage(CClientWebBrowser* pWebBrowser, std::optional bIgnoreCache) { - // bool reloadBrowserPage( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - bool bIgnoreCache; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadIfNextIsBool(bIgnoreCache, false); - - if (!argStream.HasErrors()) - { - pWebBrowser->Refresh(bIgnoreCache); - lua_pushboolean(luaVM, true); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushnil(luaVM); - return 1; + // bool reloadBrowserPage( browser webBrowser [, bool ignoreCache = false] ) + pWebBrowser->Refresh(bIgnoreCache.value_or(false)); + return true; } int CLuaBrowserDefs::GUICreateBrowser(lua_State* luaVM) @@ -1014,92 +561,66 @@ int CLuaBrowserDefs::GUIGetBrowser(lua_State* luaVM) // Or rather guiGetBrowser return 1; } -int CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM) +bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* pWebBrowser, const std::string strURL, + std::optional callbackFunction) { // bool setBrowserAjaxHandler ( browser browser, string URL[, function callback] ) - CClientWebBrowser* pWebBrowser; - SString strURL; - CLuaFunctionRef callbackFunction; + if (!callbackFunction.has_value()) + return pWebBrowser->RemoveAjaxHandler(strURL); - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - argStream.ReadString(strURL); - - if (argStream.NextIsNil() || argStream.NextIsNone()) - { - if (!argStream.HasErrors()) - { - lua_pushboolean(luaVM, pWebBrowser->RemoveAjaxHandler(strURL)); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - } - else + CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (pLuaMain && VERIFY_FUNCTION(callbackFunction.value())) { - argStream.ReadFunction(callbackFunction); - argStream.ReadFunctionComplete(); - if (!argStream.HasErrors()) - { - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (pLuaMain && VERIFY_FUNCTION(callbackFunction)) - { - CResource* pResource = pLuaMain->GetResource(); - CResourceManager* pResourceManager = m_pResourceManager; - auto netId = pResource->GetNetID(); - - bool bResult = pWebBrowser->AddAjaxHandler(strURL, - [=](std::vector& vecGet, std::vector& vecPost) -> const std::string - { - // Make sure the resource is still running - if (!pResourceManager->Exists(pResource) || pResource->GetNetID() != netId) - { - return ""; - } - - // Make sure the function is valid - if (VERIFY_FUNCTION(callbackFunction)) - { - CLuaArguments arguments; - CLuaArguments getArguments; - CLuaArguments postArguments; - - for (auto&& param : vecGet) - getArguments.PushString(param); - - for (auto&& param : vecPost) - postArguments.PushString(param); - - arguments.PushTable(&getArguments); - arguments.PushTable(&postArguments); - - CLuaArguments result; - - arguments.Call(pLuaMain, callbackFunction, &result); - - if (result.IsEmpty()) - return ""; - - CLuaArgument* returnedValue = *result.begin(); - if (returnedValue->GetType() == LUA_TSTRING) - return std::string(returnedValue->GetString()); - else - return ""; - } - else - return ""; - }); - - lua_pushboolean(luaVM, bResult); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); + CResource* pResource = pLuaMain->GetResource(); + CResourceManager* pResourceManager = m_pResourceManager; + auto netId = pResource->GetNetID(); + + bool bResult = pWebBrowser->AddAjaxHandler(strURL, + [=](std::vector& vecGet, std::vector& vecPost) -> const std::string + { + // Make sure the resource is still running + if (!pResourceManager->Exists(pResource) || pResource->GetNetID() != netId) + { + return ""; + } + + // Make sure the function is valid + if (VERIFY_FUNCTION(callbackFunction.value())) + { + CLuaArguments arguments; + CLuaArguments getArguments; + CLuaArguments postArguments; + + for (auto&& param : vecGet) + getArguments.PushString(param); + + for (auto&& param : vecPost) + postArguments.PushString(param); + + arguments.PushTable(&getArguments); + arguments.PushTable(&postArguments); + + CLuaArguments result; + + arguments.Call(pLuaMain, callbackFunction.value(), &result); + + if (result.IsEmpty()) + return ""; + + CLuaArgument* returnedValue = *result.begin(); + if (returnedValue->GetType() == LUA_TSTRING) + return std::string(returnedValue->GetString()); + else + return ""; + } + else + return ""; + }); + + return bResult; } - lua_pushboolean(luaVM, false); - return 1; + return false; } bool CLuaBrowserDefs::IsBrowserGPUEnabled() noexcept diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h index 0d214954ad5..bb07a3e8d00 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h @@ -11,6 +11,7 @@ #pragma once #include "CLuaDefs.h" +#include class CLuaBrowserDefs : public CLuaDefs { @@ -18,37 +19,39 @@ class CLuaBrowserDefs : public CLuaDefs static void LoadFunctions(); static void AddClass(lua_State* luaVM); - LUA_DECLARE(CreateBrowser); - LUA_DECLARE(IsBrowserSupported); - LUA_DECLARE(RequestBrowserDomains); - LUA_DECLARE(LoadBrowserURL); - LUA_DECLARE(IsBrowserLoading); - LUA_DECLARE(InjectBrowserMouseMove); - LUA_DECLARE(InjectBrowserMouseDown); - LUA_DECLARE(InjectBrowserMouseUp); - LUA_DECLARE(InjectBrowserMouseWheel); - LUA_DECLARE(GetBrowserTitle); - LUA_DECLARE(GetBrowserURL); - LUA_DECLARE(SetBrowserRenderingPaused); - static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); - LUA_DECLARE(ExecuteBrowserJavascript); + static std::variant CreateBrowser(lua_State* luaVM, CVector2D vecSize, bool bIsLocal, std::optional bTransparent); + static bool RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional bIsURL, + std::optional callbackFunction); + static bool LoadBrowserURL(CClientWebBrowser* pWebBrowser, const std::string strURL, std::optional strPostData, + std::optional bURLEncoded); + static bool IsBrowserLoading(CClientWebBrowser* pWebBrowser); + static bool InjectBrowserMouseMove(CClientWebBrowser* pWebBrowser, CVector2D vecPosition); + static bool InjectBrowserMouseDown(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton, std::optional doubleClick); + static bool InjectBrowserMouseUp(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton); + static bool InjectBrowserMouseWheel(CClientWebBrowser* pWebBrowser, int iScrollVert, int iScrollHorz); + static SString GetBrowserTitle(CClientWebBrowser* pWebBrowser); + static SString GetBrowserURL(CClientWebBrowser* pWebBrowser); + static bool SetBrowserRenderingPaused(CClientWebBrowser* pWebBrowser, bool bPaused); + static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); + static bool ExecuteBrowserJavascript(CClientWebBrowser* pWebBrowser, const std::string strJavascriptCode); LUA_DECLARE(GetBrowserVolume); - LUA_DECLARE(SetBrowserVolume); - LUA_DECLARE(IsBrowserDomainBlocked); - LUA_DECLARE(FocusBrowser); - LUA_DECLARE(IsBrowserFocused); - LUA_DECLARE(SetBrowserProperty); - LUA_DECLARE(GetBrowserProperty); - LUA_DECLARE(GetBrowserSettings); - LUA_DECLARE(GetBrowserSource); - LUA_DECLARE(SetBrowserAjaxHandler); - LUA_DECLARE(CanBrowserNavigateBack); - LUA_DECLARE(CanBrowserNavigateForward); - LUA_DECLARE(NavigateBrowserForward); - LUA_DECLARE(NavigateBrowserBack); - LUA_DECLARE(ReloadBrowserPage); - LUA_DECLARE(ToggleBrowserDevTools); - LUA_DECLARE(ResizeBrowser); + static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional fVolume); + static std::optional IsBrowserDomainBlocked(const std::string strURL, std::optional bIsURL); + static bool FocusBrowser(std::optional pWebBrowser); + static bool IsBrowserFocused(CClientWebBrowser* pWebBrowser); + static bool SetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey, const std::string strValue); + static std::optional GetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey); + static std::unordered_map GetBrowserSettings(); + static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWebBrowser, CLuaFunctionRef callbackFunction); + static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* pWebBrowser, const std::string strURL, + std::optional callbackFunction); + static bool CanBrowserNavigateBack(CClientWebBrowser* pWebBrowser); + static bool CanBrowserNavigateForward(CClientWebBrowser* pWebBrowser); + static bool NavigateBrowserBack(CClientWebBrowser* pWebBrowser); + static bool NavigateBrowserForward(CClientWebBrowser* pWebBrowser); + static bool ReloadBrowserPage(CClientWebBrowser* pWebBrowser, std::optional bIgnoreCache); + static bool ToggleBrowserDevTools(CClientWebBrowser* pWebBrowser, bool visible); + static bool ResizeBrowser(CClientWebBrowser* pWebBrowser, CVector2D size); LUA_DECLARE(GUICreateBrowser); LUA_DECLARE(GUIGetBrowser); static bool IsBrowserGPUEnabled() noexcept; From 120f1b995f5dbefd5ce21e62d1836c873ca8cc9d Mon Sep 17 00:00:00 2001 From: Youssef Maged Date: Wed, 9 Sep 2026 04:30:32 +0300 Subject: [PATCH 2/9] Run clang-format --- .../logic/luadefs/CLuaBrowserDefs.cpp | 4 +- .../logic/luadefs/CLuaBrowserDefs.h | 48 +++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp index fb21467c5b3..54a7e9ca052 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp @@ -174,7 +174,7 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vectorGetDomainFromURL(url); }); } - CLuaFunctionRef functionRef = callbackFunction.value_or(CLuaFunctionRef()); + CLuaFunctionRef functionRef = callbackFunction.value_or(CLuaFunctionRef()); WebRequestCallback callback = [=](bool bAllow, const std::unordered_set& domains) { // Test if luaVM is still available @@ -207,7 +207,7 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vector strPostData, - std::optional bURLEncoded) + std::optional bURLEncoded) { // bool loadBrowserURL ( browser webBrowser, string url [, string postData = "", bool postURLEncoded = true ] ) // Are we dealing with a remote website? diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h index bb07a3e8d00..9018c48e835 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h @@ -21,37 +21,37 @@ class CLuaBrowserDefs : public CLuaDefs static std::variant CreateBrowser(lua_State* luaVM, CVector2D vecSize, bool bIsLocal, std::optional bTransparent); static bool RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional bIsURL, - std::optional callbackFunction); - static bool LoadBrowserURL(CClientWebBrowser* pWebBrowser, const std::string strURL, std::optional strPostData, - std::optional bURLEncoded); - static bool IsBrowserLoading(CClientWebBrowser* pWebBrowser); - static bool InjectBrowserMouseMove(CClientWebBrowser* pWebBrowser, CVector2D vecPosition); - static bool InjectBrowserMouseDown(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton, std::optional doubleClick); - static bool InjectBrowserMouseUp(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton); - static bool InjectBrowserMouseWheel(CClientWebBrowser* pWebBrowser, int iScrollVert, int iScrollHorz); + std::optional callbackFunction); + static bool LoadBrowserURL(CClientWebBrowser* pWebBrowser, const std::string strURL, std::optional strPostData, + std::optional bURLEncoded); + static bool IsBrowserLoading(CClientWebBrowser* pWebBrowser); + static bool InjectBrowserMouseMove(CClientWebBrowser* pWebBrowser, CVector2D vecPosition); + static bool InjectBrowserMouseDown(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton, std::optional doubleClick); + static bool InjectBrowserMouseUp(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton); + static bool InjectBrowserMouseWheel(CClientWebBrowser* pWebBrowser, int iScrollVert, int iScrollHorz); static SString GetBrowserTitle(CClientWebBrowser* pWebBrowser); static SString GetBrowserURL(CClientWebBrowser* pWebBrowser); static bool SetBrowserRenderingPaused(CClientWebBrowser* pWebBrowser, bool bPaused); static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); static bool ExecuteBrowserJavascript(CClientWebBrowser* pWebBrowser, const std::string strJavascriptCode); LUA_DECLARE(GetBrowserVolume); - static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional fVolume); - static std::optional IsBrowserDomainBlocked(const std::string strURL, std::optional bIsURL); - static bool FocusBrowser(std::optional pWebBrowser); - static bool IsBrowserFocused(CClientWebBrowser* pWebBrowser); - static bool SetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey, const std::string strValue); - static std::optional GetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey); + static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional fVolume); + static std::optional IsBrowserDomainBlocked(const std::string strURL, std::optional bIsURL); + static bool FocusBrowser(std::optional pWebBrowser); + static bool IsBrowserFocused(CClientWebBrowser* pWebBrowser); + static bool SetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey, const std::string strValue); + static std::optional GetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey); static std::unordered_map GetBrowserSettings(); - static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWebBrowser, CLuaFunctionRef callbackFunction); - static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* pWebBrowser, const std::string strURL, - std::optional callbackFunction); - static bool CanBrowserNavigateBack(CClientWebBrowser* pWebBrowser); - static bool CanBrowserNavigateForward(CClientWebBrowser* pWebBrowser); - static bool NavigateBrowserBack(CClientWebBrowser* pWebBrowser); - static bool NavigateBrowserForward(CClientWebBrowser* pWebBrowser); - static bool ReloadBrowserPage(CClientWebBrowser* pWebBrowser, std::optional bIgnoreCache); - static bool ToggleBrowserDevTools(CClientWebBrowser* pWebBrowser, bool visible); - static bool ResizeBrowser(CClientWebBrowser* pWebBrowser, CVector2D size); + static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWebBrowser, CLuaFunctionRef callbackFunction); + static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* pWebBrowser, const std::string strURL, + std::optional callbackFunction); + static bool CanBrowserNavigateBack(CClientWebBrowser* pWebBrowser); + static bool CanBrowserNavigateForward(CClientWebBrowser* pWebBrowser); + static bool NavigateBrowserBack(CClientWebBrowser* pWebBrowser); + static bool NavigateBrowserForward(CClientWebBrowser* pWebBrowser); + static bool ReloadBrowserPage(CClientWebBrowser* pWebBrowser, std::optional bIgnoreCache); + static bool ToggleBrowserDevTools(CClientWebBrowser* pWebBrowser, bool visible); + static bool ResizeBrowser(CClientWebBrowser* pWebBrowser, CVector2D size); LUA_DECLARE(GUICreateBrowser); LUA_DECLARE(GUIGetBrowser); static bool IsBrowserGPUEnabled() noexcept; From fc0b02f48dc08f6b6471373828e3c2c9e2c052e4 Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Wed, 9 Sep 2026 06:38:58 +0300 Subject: [PATCH 3/9] Refactor CLuaBrowserDefs methods for consistency --- .../logic/luadefs/CLuaBrowserDefs.cpp | 278 +++++++++--------- 1 file changed, 139 insertions(+), 139 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp index 54a7e9ca052..ec2160a6b7a 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp @@ -110,21 +110,21 @@ void CLuaBrowserDefs::AddClass(lua_State* luaVM) lua_registerclass(luaVM, "GuiBrowser", "GuiElement"); } -std::variant CLuaBrowserDefs::CreateBrowser(lua_State* luaVM, CVector2D vecSize, bool bIsLocal, std::optional bTransparent) +std::variant CLuaBrowserDefs::CreateBrowser(lua_State* luaVM, CVector2D size, bool isLocal, std::optional transparent) { // texture createBrowser ( int width, int height, bool isLocal [, bool transparent = false] ) - if (vecSize.fX < 0) + if (size.fX < 0) throw std::invalid_argument("Browser width is smaller than 0"); - else if (vecSize.fY < 0) + else if (size.fY < 0) throw std::invalid_argument("Browser height is smaller than 0"); - else if (vecSize.fX == 0 || vecSize.fY == 0) + else if (size.fX == 0 || size.fY == 0) throw std::invalid_argument("A browser must be at least 1x1 in size."); - const auto pWebCore = g_pCore->GetWebCore(); - if (!pWebCore) + const auto webCore = g_pCore->GetWebCore(); + if (!webCore) return false; - if (!bIsLocal && !pWebCore->GetRemotePagesEnabled()) + if (!isLocal && !webCore->GetRemotePagesEnabled()) return false; CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); @@ -132,50 +132,50 @@ std::variant CLuaBrowserDefs::CreateBrowser(lua_State* { CResource* pParentResource = pLuaMain->GetResource(); - CClientWebBrowser* pBrowserTexture = - g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser((int)vecSize.fX, (int)vecSize.fY, bIsLocal, bTransparent.value_or(false)); - if (pBrowserTexture) + CClientWebBrowser* browserTexture = + g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser((int)size.fX, (int)size.fY, isLocal, transparent.value_or(false)); + if (browserTexture) { // Make it a child of the resource's file root ** CHECK Should parent be pFileResource, and element added to pParentResource's ElementGroup? ** - pBrowserTexture->SetParent(pParentResource->GetResourceDynamicEntity()); + browserTexture->SetParent(pParentResource->GetResourceDynamicEntity()); // Set our owner resource - pBrowserTexture->SetResource(pParentResource); + browserTexture->SetResource(pParentResource); } - return pBrowserTexture; + return browserTexture; } return false; } -bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional bIsURL, +bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional isURL, std::optional callbackFunction) { // bool requestBrowserDomains ( table domains, bool isURL [, function callback ] ) - std::vector sPages(pages.begin(), pages.end()); + std::vector urls(pages.begin(), pages.end()); // Remove whitespaces - for (auto& url : sPages) + for (auto& url : urls) url.erase(std::remove_if(url.begin(), url.end(), [](unsigned char c) { return std::isspace(c); }), url.end()); // Remove empty and invalid URLs std::regex invalidSynmbolsRegex("[^A-Za-z0-9._~!#$&'()*+,;=:@/?%-]"); - sPages.erase(std::remove_if(sPages.begin(), sPages.end(), - [&invalidSynmbolsRegex](const auto& url) { return url.empty() || std::regex_search(url, invalidSynmbolsRegex); }), - sPages.end()); + urls.erase(std::remove_if(urls.begin(), urls.end(), + [&invalidSynmbolsRegex](const auto& url) { return url.empty() || std::regex_search(url, invalidSynmbolsRegex); }), + urls.end()); // Convert to domains if we got a list of URLs - if (bIsURL.value_or(false)) + if (isURL.value_or(false)) { - auto pWebCore = g_pCore->GetWebCore(); - if (!pWebCore) + auto webCore = g_pCore->GetWebCore(); + if (!webCore) return false; - std::transform(sPages.begin(), sPages.end(), sPages.begin(), [pWebCore](const auto& url) { return pWebCore->GetDomainFromURL(url); }); + std::transform(urls.begin(), urls.end(), urls.begin(), [webCore](const auto& url) { return webCore->GetDomainFromURL(url); }); } CLuaFunctionRef functionRef = callbackFunction.value_or(CLuaFunctionRef()); - WebRequestCallback callback = [=](bool bAllow, const std::unordered_set& domains) + WebRequestCallback callback = [=](bool allow, const std::unordered_set& domains) { // Test if luaVM is still available if (m_pLuaManager->IsLuaVMValid(luaVM) && VERIFY_FUNCTION(functionRef)) @@ -185,7 +185,7 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vectorGetWebCore(); - if (!pWebCore) + auto webCore = g_pCore->GetWebCore(); + if (!webCore) return false; - pWebCore->RequestPages(sPages, VERIFY_FUNCTION(functionRef) ? &callback : nullptr); + webCore->RequestPages(urls, VERIFY_FUNCTION(functionRef) ? &callback : nullptr); return true; } -bool CLuaBrowserDefs::LoadBrowserURL(CClientWebBrowser* pWebBrowser, const std::string strURL, std::optional strPostData, - std::optional bURLEncoded) +bool CLuaBrowserDefs::LoadBrowserURL(CClientWebBrowser* browser, const std::string url, std::optional postData, std::optional urlEncoded) { // bool loadBrowserURL ( browser webBrowser, string url [, string postData = "", bool postURLEncoded = true ] ) // Are we dealing with a remote website? - if (strURL.substr(0, 7) == "http://" || strURL.substr(0, 8) == "https://") + if (url.substr(0, 7) == "http://" || url.substr(0, 8) == "https://") { - bool isLocalURL = strURL.substr(0, 11) == "http://mta/"; - if (pWebBrowser->IsLocal() != isLocalURL) + bool isLocalURL = url.substr(0, 11) == "http://mta/"; + if (browser->IsLocal() != isLocalURL) return false; - return pWebBrowser->LoadURL(strURL, !isLocalURL, strPostData.value_or(""), bURLEncoded.value_or(true)); + return browser->LoadURL(url, !isLocalURL, postData.value_or(""), urlEncoded.value_or(true)); } throw std::invalid_argument("Invalid URL scheme provided. Only http:// and https:// is supported."); } -bool CLuaBrowserDefs::IsBrowserLoading(CClientWebBrowser* pWebBrowser) +bool CLuaBrowserDefs::IsBrowserLoading(CClientWebBrowser* browser) { // bool isBrowserLoading(browser webBrowser) - return pWebBrowser->IsLoading(); + return browser->IsLoading(); } -bool CLuaBrowserDefs::InjectBrowserMouseMove(CClientWebBrowser* pWebBrowser, CVector2D vecPosition) +bool CLuaBrowserDefs::InjectBrowserMouseMove(CClientWebBrowser* browser, CVector2D position) { // bool injectBrowserMouseMove(browser webBrowser, int x, int y) - pWebBrowser->InjectMouseMove((int)vecPosition.fX, (int)vecPosition.fY); + browser->InjectMouseMove((int)position.fX, (int)position.fY); return true; } -bool CLuaBrowserDefs::InjectBrowserMouseDown(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton, std::optional doubleClick) +bool CLuaBrowserDefs::InjectBrowserMouseDown(CClientWebBrowser* browser, eWebBrowserMouseButton mouseButton, std::optional doubleClick) { // bool injectBrowserMouseDown ( browser webBrowser, string mouseButton [ , bool doubleClick = false ] ) - pWebBrowser->InjectMouseDown(mouseButton, doubleClick.value_or(false) ? 2 : 1); + browser->InjectMouseDown(mouseButton, doubleClick.value_or(false) ? 2 : 1); return true; } -bool CLuaBrowserDefs::InjectBrowserMouseUp(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton) +bool CLuaBrowserDefs::InjectBrowserMouseUp(CClientWebBrowser* browser, eWebBrowserMouseButton mouseButton) { // bool injectBrowserMouseUp ( browser webBrowser, string mouseButton ) - pWebBrowser->InjectMouseUp(mouseButton); + browser->InjectMouseUp(mouseButton); return true; } -bool CLuaBrowserDefs::InjectBrowserMouseWheel(CClientWebBrowser* pWebBrowser, int iScrollVert, int iScrollHorz) +bool CLuaBrowserDefs::InjectBrowserMouseWheel(CClientWebBrowser* browser, int scrollVert, int scrollHorz) { // bool injectMouseWheel ( browser webBrowser, int scrollVertical, int scrollHorizontal ) - pWebBrowser->InjectMouseWheel(iScrollVert, iScrollHorz); + browser->InjectMouseWheel(scrollVert, scrollHorz); return true; } -SString CLuaBrowserDefs::GetBrowserTitle(CClientWebBrowser* pWebBrowser) +std::string CLuaBrowserDefs::GetBrowserTitle(CClientWebBrowser* browser) { // string getBrowserPageTitle ( browser webBrowser ) - return pWebBrowser->GetTitle(); + return browser->GetTitle(); } -SString CLuaBrowserDefs::GetBrowserURL(CClientWebBrowser* pWebBrowser) +std::string CLuaBrowserDefs::GetBrowserURL(CClientWebBrowser* browser) { // string getBrowserURL ( browser webBrowser ) - return pWebBrowser->GetURL(); + return browser->GetURL(); } -bool CLuaBrowserDefs::SetBrowserRenderingPaused(CClientWebBrowser* pWebBrowser, bool bPaused) +bool CLuaBrowserDefs::SetBrowserRenderingPaused(CClientWebBrowser* browser, bool paused) { // bool setBrowserRenderingPaused ( browser webBrowser, bool paused ) - pWebBrowser->SetRenderingPaused(bPaused); + browser->SetRenderingPaused(paused); return true; } @@ -281,10 +280,10 @@ bool CLuaBrowserDefs::IsBrowserRenderingPaused(CClientWebBrowser* browser) return browser->GetRenderingPaused(); } -bool CLuaBrowserDefs::ExecuteBrowserJavascript(CClientWebBrowser* pWebBrowser, const std::string strJavascriptCode) +bool CLuaBrowserDefs::ExecuteBrowserJavascript(CClientWebBrowser* browser, const std::string javascriptCode) { // bool executeBrowserJavascript ( browser webBrowser, string jsCode ) - if (strJavascriptCode.empty() || pWebBrowser->ExecuteJavascript(strJavascriptCode)) + if (javascriptCode.empty() || browser->ExecuteJavascript(javascriptCode)) return true; throw std::invalid_argument("This function does not work with remote browsers"); @@ -310,95 +309,96 @@ int CLuaBrowserDefs::GetBrowserVolume(lua_State* luaVM) return 1; } -bool CLuaBrowserDefs::SetBrowserVolume(std::variant webBrowserOrVolume, std::optional fVolume) +bool CLuaBrowserDefs::SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume) { // bool setBrowserVolume ( float volume ) // bool setBrowserVolume ( browser webBrowser, float volume ) if (std::holds_alternative(webBrowserOrVolume)) { - auto pWebCore = g_pCore->GetWebCore(); - return pWebCore ? pWebCore->SetGlobalAudioVolume(std::get(webBrowserOrVolume)) : false; + auto webCore = g_pCore->GetWebCore(); + return webCore ? webCore->SetGlobalAudioVolume(std::get(webBrowserOrVolume)) : false; } - return std::get(webBrowserOrVolume)->SetAudioVolume(fVolume.value_or(0.0f)); + return std::get(webBrowserOrVolume)->SetAudioVolume(volume.value_or(0.0f)); } -std::optional CLuaBrowserDefs::IsBrowserDomainBlocked(const std::string strURL, std::optional bIsURL) +std::optional CLuaBrowserDefs::IsBrowserDomainBlocked(const std::string url, std::optional isURL) { // bool isBrowserDomainBlocked ( string domain, bool isURL ) - auto pWebCore = g_pCore->GetWebCore(); - if (!pWebCore) + auto webCore = g_pCore->GetWebCore(); + if (!webCore) return std::nullopt; - SString strDomain = SString(strURL); - if (bIsURL.value_or(false)) - strDomain = pWebCore->GetDomainFromURL(strDomain); + SString domain = SString(url); + if (isURL.value_or(false)) + domain = webCore->GetDomainFromURL(domain); - if (!strDomain.empty()) - return pWebCore->GetDomainState(strDomain) != eURLState::WEBPAGE_ALLOWED; + if (!domain.empty()) + return webCore->GetDomainState(domain) != eURLState::WEBPAGE_ALLOWED; return std::nullopt; } -bool CLuaBrowserDefs::FocusBrowser(std::optional pWebBrowser) +bool CLuaBrowserDefs::FocusBrowser(std::optional browser) { // focusBrowser ( browser webBrowser ) - if (!pWebBrowser.has_value()) + if (!browser.has_value()) { - auto pWebCore = g_pCore->GetWebCore(); - if (pWebCore) - pWebCore->SetFocusedWebView(NULL); + auto webCore = g_pCore->GetWebCore(); + if (webCore) + webCore->SetFocusedWebView(nullptr); return true; } - pWebBrowser.value()->Focus(); + browser.value()->Focus(); return true; } -bool CLuaBrowserDefs::IsBrowserFocused(CClientWebBrowser* pWebBrowser) +bool CLuaBrowserDefs::IsBrowserFocused(CClientWebBrowser* browser) { // browser isBrowserFocused ( browser webBrowser ) - auto pWebCore = g_pCore->GetWebCore(); - CWebViewInterface* pWebView = pWebCore ? pWebCore->GetFocusedWebView() : nullptr; - return pWebBrowser->GetWebView() == pWebView; + auto webCore = g_pCore->GetWebCore(); + CWebViewInterface* webView = webCore ? webCore->GetFocusedWebView() : nullptr; + return browser->GetWebView() == webView; } -bool CLuaBrowserDefs::SetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey, const std::string strValue) +bool CLuaBrowserDefs::SetBrowserProperty(CClientWebBrowser* browser, const std::string key, const std::string value) { // bool setBrowserProperty ( browser webBrowser, string key, string value ) - return pWebBrowser->SetProperty(strKey, strValue); + return browser->SetProperty(key, value); } -std::optional CLuaBrowserDefs::GetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey) +std::optional CLuaBrowserDefs::GetBrowserProperty(CClientWebBrowser* browser, const std::string key) { // string getBrowserProperty ( browser webBrowser, string key ) - SString strValue; - if (pWebBrowser->GetProperty(strKey, strValue)) - return strValue; + SString value; + if (browser->GetProperty(key, value)) + return value; return std::nullopt; } -std::unordered_map CLuaBrowserDefs::GetBrowserSettings() +auto CLuaBrowserDefs::GetBrowserSettings() { // table getBrowserSettings () - auto pWebCore = g_pCore->GetWebCore(); + auto webCore = g_pCore->GetWebCore(); - return { - {"RemoteEnabled", pWebCore ? pWebCore->GetRemotePagesEnabled() : false}, - {"RemoteJavascript", pWebCore ? pWebCore->GetRemoteJavascriptEnabled() : false}, + std::unordered_map settings{ + {"RemoteEnabled", webCore ? webCore->GetRemotePagesEnabled() : false}, + {"RemoteJavascript", webCore ? webCore->GetRemoteJavascriptEnabled() : false}, {"PluginsEnabled", false}, }; + return settings; } -bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWebBrowser, CLuaFunctionRef callbackFunction) +bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction) { // bool getBrowserSource ( browser webBrowser, function callback ) CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); if (pLuaMain && VERIFY_FUNCTION(callbackFunction)) { - pWebBrowser->GetSourceCode( - [callbackFunction, pLuaMain, pWebBrowser](const std::string& code) + browser->GetSourceCode( + [callbackFunction, pLuaMain, browser](const std::string& code) { /* This function should not be called when the resource is about to stop as @@ -410,7 +410,7 @@ bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWeb CLuaArguments arguments; // TODO: Use SCharStringRef/direct string access instead of copying strings around arguments.PushString(code); - arguments.PushElement(pWebBrowser); + arguments.PushElement(browser); arguments.Call(pLuaMain, callbackFunction); } }); @@ -421,51 +421,51 @@ bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWeb return false; } -bool CLuaBrowserDefs::ToggleBrowserDevTools(CClientWebBrowser* pWebBrowser, bool visible) +bool CLuaBrowserDefs::ToggleBrowserDevTools(CClientWebBrowser* browser, bool visible) { // bool toggleBrowserDevTools ( browser webBrowser, bool visible ) - auto pWebCore = g_pCore->GetWebCore(); - if (pWebCore && pWebCore->IsTestModeEnabled()) - return pWebBrowser->ToggleDevTools(visible); + auto webCore = g_pCore->GetWebCore(); + if (webCore && webCore->IsTestModeEnabled()) + return browser->ToggleDevTools(visible); throw std::invalid_argument("toggleBrowserDevtools can only be used in development mode"); } -bool CLuaBrowserDefs::ResizeBrowser(CClientWebBrowser* pWebBrowser, CVector2D size) +bool CLuaBrowserDefs::ResizeBrowser(CClientWebBrowser* browser, CVector2D size) { // bool resizeBrowser(browser webBrowser, float width, float height) - pWebBrowser->Resize(size); + browser->Resize(size); return true; } -bool CLuaBrowserDefs::CanBrowserNavigateBack(CClientWebBrowser* pWebBrowser) +bool CLuaBrowserDefs::CanBrowserNavigateBack(CClientWebBrowser* browser) { // bool canBrowserNavigateBack( browser webBrowser ) - return pWebBrowser->CanGoBack(); + return browser->CanGoBack(); } -bool CLuaBrowserDefs::CanBrowserNavigateForward(CClientWebBrowser* pWebBrowser) +bool CLuaBrowserDefs::CanBrowserNavigateForward(CClientWebBrowser* browser) { // bool canBrowserNavigateForward( browser webBrowser ) - return pWebBrowser->CanGoForward(); + return browser->CanGoForward(); } -bool CLuaBrowserDefs::NavigateBrowserBack(CClientWebBrowser* pWebBrowser) +bool CLuaBrowserDefs::NavigateBrowserBack(CClientWebBrowser* browser) { // bool navigateBrowserBack( browser webBrowser ) - return pWebBrowser->GoBack(); + return browser->GoBack(); } -bool CLuaBrowserDefs::NavigateBrowserForward(CClientWebBrowser* pWebBrowser) +bool CLuaBrowserDefs::NavigateBrowserForward(CClientWebBrowser* browser) { // bool navigateBrowserForward( browser webBrowser ) - return pWebBrowser->GoForward(); + return browser->GoForward(); } -bool CLuaBrowserDefs::ReloadBrowserPage(CClientWebBrowser* pWebBrowser, std::optional bIgnoreCache) +bool CLuaBrowserDefs::ReloadBrowserPage(CClientWebBrowser* browser, std::optional ignoreCache) { // bool reloadBrowserPage( browser webBrowser [, bool ignoreCache = false] ) - pWebBrowser->Refresh(bIgnoreCache.value_or(false)); + browser->Refresh(ignoreCache.value_or(false)); return true; } @@ -561,12 +561,12 @@ int CLuaBrowserDefs::GUIGetBrowser(lua_State* luaVM) // Or rather guiGetBrowser return 1; } -bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* pWebBrowser, const std::string strURL, +bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* browser, const std::string url, std::optional callbackFunction) { // bool setBrowserAjaxHandler ( browser browser, string URL[, function callback] ) if (!callbackFunction.has_value()) - return pWebBrowser->RemoveAjaxHandler(strURL); + return browser->RemoveAjaxHandler(url); CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); if (pLuaMain && VERIFY_FUNCTION(callbackFunction.value())) @@ -575,47 +575,47 @@ bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* CResourceManager* pResourceManager = m_pResourceManager; auto netId = pResource->GetNetID(); - bool bResult = pWebBrowser->AddAjaxHandler(strURL, - [=](std::vector& vecGet, std::vector& vecPost) -> const std::string + bool bResult = browser->AddAjaxHandler(url, + [=](std::vector& vecGet, std::vector& vecPost) -> const std::string + { + // Make sure the resource is still running + if (!pResourceManager->Exists(pResource) || pResource->GetNetID() != netId) { - // Make sure the resource is still running - if (!pResourceManager->Exists(pResource) || pResource->GetNetID() != netId) - { - return ""; - } + return ""; + } - // Make sure the function is valid - if (VERIFY_FUNCTION(callbackFunction.value())) - { - CLuaArguments arguments; - CLuaArguments getArguments; - CLuaArguments postArguments; + // Make sure the function is valid + if (VERIFY_FUNCTION(callbackFunction.value())) + { + CLuaArguments arguments; + CLuaArguments getArguments; + CLuaArguments postArguments; - for (auto&& param : vecGet) - getArguments.PushString(param); + for (auto&& param : vecGet) + getArguments.PushString(param); - for (auto&& param : vecPost) - postArguments.PushString(param); + for (auto&& param : vecPost) + postArguments.PushString(param); - arguments.PushTable(&getArguments); - arguments.PushTable(&postArguments); + arguments.PushTable(&getArguments); + arguments.PushTable(&postArguments); - CLuaArguments result; + CLuaArguments result; - arguments.Call(pLuaMain, callbackFunction.value(), &result); + arguments.Call(pLuaMain, callbackFunction.value(), &result); - if (result.IsEmpty()) - return ""; + if (result.IsEmpty()) + return ""; - CLuaArgument* returnedValue = *result.begin(); - if (returnedValue->GetType() == LUA_TSTRING) - return std::string(returnedValue->GetString()); - else - return ""; - } + CLuaArgument* returnedValue = *result.begin(); + if (returnedValue->GetType() == LUA_TSTRING) + return std::string(returnedValue->GetString()); else return ""; - }); + } + else + return ""; + }); return bResult; } @@ -625,6 +625,6 @@ bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* bool CLuaBrowserDefs::IsBrowserGPUEnabled() noexcept { - auto pWebCore = g_pCore->GetWebCore(); - return pWebCore ? pWebCore->GetGPUEnabled() : false; + auto webCore = g_pCore->GetWebCore(); + return webCore ? webCore->GetGPUEnabled() : false; } From 579ccb89850b830222a4b1a85a47fb6713767772 Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Wed, 9 Sep 2026 06:39:16 +0300 Subject: [PATCH 4/9] Refactor browser function signatures for consistency --- .../logic/luadefs/CLuaBrowserDefs.h | 60 +++++++++---------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h index 9018c48e835..0be34d324ac 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h @@ -19,39 +19,37 @@ class CLuaBrowserDefs : public CLuaDefs static void LoadFunctions(); static void AddClass(lua_State* luaVM); - static std::variant CreateBrowser(lua_State* luaVM, CVector2D vecSize, bool bIsLocal, std::optional bTransparent); - static bool RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional bIsURL, + static std::variant CreateBrowser(lua_State* luaVM, CVector2D size, bool isLocal, std::optional transparent); + static bool RequestBrowserDomains(lua_State* luaVM, std::vector pages, std::optional isURL, std::optional callbackFunction); - static bool LoadBrowserURL(CClientWebBrowser* pWebBrowser, const std::string strURL, std::optional strPostData, - std::optional bURLEncoded); - static bool IsBrowserLoading(CClientWebBrowser* pWebBrowser); - static bool InjectBrowserMouseMove(CClientWebBrowser* pWebBrowser, CVector2D vecPosition); - static bool InjectBrowserMouseDown(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton, std::optional doubleClick); - static bool InjectBrowserMouseUp(CClientWebBrowser* pWebBrowser, eWebBrowserMouseButton mouseButton); - static bool InjectBrowserMouseWheel(CClientWebBrowser* pWebBrowser, int iScrollVert, int iScrollHorz); - static SString GetBrowserTitle(CClientWebBrowser* pWebBrowser); - static SString GetBrowserURL(CClientWebBrowser* pWebBrowser); - static bool SetBrowserRenderingPaused(CClientWebBrowser* pWebBrowser, bool bPaused); - static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); - static bool ExecuteBrowserJavascript(CClientWebBrowser* pWebBrowser, const std::string strJavascriptCode); + static bool LoadBrowserURL(CClientWebBrowser* browser, const std::string url, std::optional postData, std::optional urlEncoded); + static bool IsBrowserLoading(CClientWebBrowser* browser); + static bool InjectBrowserMouseMove(CClientWebBrowser* browser, CVector2D position); + static bool InjectBrowserMouseDown(CClientWebBrowser* browser, eWebBrowserMouseButton mouseButton, std::optional doubleClick); + static bool InjectBrowserMouseUp(CClientWebBrowser* browser, eWebBrowserMouseButton mouseButton); + static bool InjectBrowserMouseWheel(CClientWebBrowser* browser, int scrollVert, int scrollHorz); + static std::string GetBrowserTitle(CClientWebBrowser* browser); + static std::string GetBrowserURL(CClientWebBrowser* browser); + static bool SetBrowserRenderingPaused(CClientWebBrowser* browser, bool paused); + static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); + static bool ExecuteBrowserJavascript(CClientWebBrowser* browser, const std::string javascriptCode); LUA_DECLARE(GetBrowserVolume); - static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional fVolume); - static std::optional IsBrowserDomainBlocked(const std::string strURL, std::optional bIsURL); - static bool FocusBrowser(std::optional pWebBrowser); - static bool IsBrowserFocused(CClientWebBrowser* pWebBrowser); - static bool SetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey, const std::string strValue); - static std::optional GetBrowserProperty(CClientWebBrowser* pWebBrowser, const std::string strKey); - static std::unordered_map GetBrowserSettings(); - static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* pWebBrowser, CLuaFunctionRef callbackFunction); - static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* pWebBrowser, const std::string strURL, - std::optional callbackFunction); - static bool CanBrowserNavigateBack(CClientWebBrowser* pWebBrowser); - static bool CanBrowserNavigateForward(CClientWebBrowser* pWebBrowser); - static bool NavigateBrowserBack(CClientWebBrowser* pWebBrowser); - static bool NavigateBrowserForward(CClientWebBrowser* pWebBrowser); - static bool ReloadBrowserPage(CClientWebBrowser* pWebBrowser, std::optional bIgnoreCache); - static bool ToggleBrowserDevTools(CClientWebBrowser* pWebBrowser, bool visible); - static bool ResizeBrowser(CClientWebBrowser* pWebBrowser, CVector2D size); + static std::variant SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume); + static std::optional IsBrowserDomainBlocked(const std::string url, std::optional isURL); + static bool FocusBrowser(std::optional browser); + static bool IsBrowserFocused(CClientWebBrowser* browser); + static bool SetBrowserProperty(CClientWebBrowser* browser, const std::string key, const std::string value); + static std::optional GetBrowserProperty(CClientWebBrowser* browser, const std::string key); + static auto GetBrowserSettings(); + static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction); + static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* browser, const std::string url, std::optional callbackFunction); + static bool CanBrowserNavigateBack(CClientWebBrowser* browser); + static bool CanBrowserNavigateForward(CClientWebBrowser* browser); + static bool NavigateBrowserBack(CClientWebBrowser* browser); + static bool NavigateBrowserForward(CClientWebBrowser* browser); + static bool ReloadBrowserPage(CClientWebBrowser* browser, std::optional ignoreCache); + static bool ToggleBrowserDevTools(CClientWebBrowser* browser, bool visible); + static bool ResizeBrowser(CClientWebBrowser* browser, CVector2D size); LUA_DECLARE(GUICreateBrowser); LUA_DECLARE(GUIGetBrowser); static bool IsBrowserGPUEnabled() noexcept; From 392ab092b5f03e507202f5d2bcd4624035f513da Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Wed, 9 Sep 2026 06:59:10 +0300 Subject: [PATCH 5/9] Refactor LoadFunctions and AddClass methods --- .../logic/luadefs/CLuaBrowserDefs.cpp | 194 +++++++++--------- 1 file changed, 97 insertions(+), 97 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp index ec2160a6b7a..4686feea314 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp @@ -13,103 +13,6 @@ #include "lua/CLuaFunctionParser.h" #include -void CLuaBrowserDefs::LoadFunctions() -{ - // Define browser functions - constexpr static const std::pair functions[]{ - {"createBrowser", ArgumentParserWarn}, - {"requestBrowserDomains", ArgumentParserWarn}, - {"loadBrowserURL", ArgumentParserWarn}, - {"isBrowserLoading", ArgumentParserWarn}, - {"injectBrowserMouseMove", ArgumentParserWarn}, - {"injectBrowserMouseDown", ArgumentParserWarn}, - {"injectBrowserMouseUp", ArgumentParserWarn}, - {"injectBrowserMouseWheel", ArgumentParserWarn}, - {"getBrowserTitle", ArgumentParserWarn}, - {"getBrowserURL", ArgumentParserWarn}, - {"setBrowserRenderingPaused", ArgumentParserWarn}, - {"isBrowserRenderingPaused", ArgumentParser}, - {"executeBrowserJavascript", ArgumentParserWarn}, - {"getBrowserVolume", GetBrowserVolume}, - {"setBrowserVolume", ArgumentParserWarn}, - {"isBrowserDomainBlocked", ArgumentParserWarn}, - {"focusBrowser", ArgumentParserWarn}, - {"isBrowserFocused", ArgumentParserWarn}, - {"setBrowserProperty", ArgumentParserWarn}, - {"getBrowserProperty", ArgumentParserWarn}, - {"getBrowserSettings", ArgumentParserWarn}, - {"getBrowserSource", ArgumentParserWarn}, - {"setBrowserAjaxHandler", ArgumentParserWarn}, - {"canBrowserNavigateBack", ArgumentParserWarn}, - {"canBrowserNavigateForward", ArgumentParserWarn}, - {"navigateBrowserBack", ArgumentParserWarn}, - {"navigateBrowserForward", ArgumentParserWarn}, - {"reloadBrowserPage", ArgumentParserWarn}, - {"toggleBrowserDevTools", ArgumentParserWarn}, - {"resizeBrowser", ArgumentParserWarn}, - {"guiCreateBrowser", GUICreateBrowser}, - {"guiGetBrowser", GUIGetBrowser}, - {"isBrowserGPUEnabled", ArgumentParser}, - }; - - // Add browser functions - for (const auto& [name, func] : functions) - CLuaCFunctions::AddFunction(name, func); -} - -void CLuaBrowserDefs::AddClass(lua_State* luaVM) -{ - lua_newclass(luaVM); - - lua_classfunction(luaVM, "create", "createBrowser"); - lua_classfunction(luaVM, "loadURL", "loadBrowserURL"); - lua_classfunction(luaVM, "isLoading", "isBrowserLoading"); - lua_classfunction(luaVM, "injectMouseMove", "injectBrowserMouseMove"); - lua_classfunction(luaVM, "injectMouseDown", "injectBrowserMouseDown"); - lua_classfunction(luaVM, "injectMouseUp", "injectBrowserMouseUp"); - lua_classfunction(luaVM, "injectMouseWheel", "injectBrowserMouseWheel"); - lua_classfunction(luaVM, "getTitle", "getBrowserTitle"); - lua_classfunction(luaVM, "getURL", "getBrowserURL"); - lua_classfunction(luaVM, "setRenderingPaused", "setBrowserRenderingPaused"); - lua_classfunction(luaVM, "isRenderingPaused", "isBrowserRenderingPaused"); - lua_classfunction(luaVM, "executeJavascript", "executeBrowserJavascript"); - lua_classfunction(luaVM, "getVolume", "getBrowserVolume"); - lua_classfunction(luaVM, "setVolume", "setBrowserVolume"); - lua_classfunction(luaVM, "focus", "focusBrowser"); - lua_classfunction(luaVM, "isFocused", "isBrowserFocused"); - lua_classfunction(luaVM, "setProperty", "setBrowserProperty"); - lua_classfunction(luaVM, "getProperty", "getBrowserProperty"); - lua_classfunction(luaVM, "getSource", "getBrowserSource"); - lua_classfunction(luaVM, "setAjaxHandler", "setBrowserAjaxHandler"); - lua_classfunction(luaVM, "canNavigateBack", "canBrowserNavigateBack"); - lua_classfunction(luaVM, "canNavigateForward", "canBrowserNavigateForward"); - lua_classfunction(luaVM, "navigateBack", "navigateBrowserBack"); - lua_classfunction(luaVM, "navigateForward", "navigateBrowserForward"); - lua_classfunction(luaVM, "reloadPage", "reloadBrowserPage"); - lua_classfunction(luaVM, "toggleDevTools", "toggleBrowserDevTools"); - lua_classfunction(luaVM, "resize", "resizeBrowser"); - - lua_classfunction(luaVM, "requestDomains", "requestBrowserDomains"); - lua_classfunction(luaVM, "isDomainBlocked", "isBrowserDomainBlocked"); - - lua_classvariable(luaVM, "url", "loadBrowserURL", "getBrowserURL"); - lua_classvariable(luaVM, "loading", nullptr, "isBrowserLoading"); - lua_classvariable(luaVM, "title", nullptr, "getBrowserTitle"); - lua_classvariable(luaVM, "renderingPaused", "setBrowserRenderingPaused", "isBrowserRenderingPaused"); - lua_classvariable(luaVM, "volume", "setBrowserVolume", "getBrowserVolume"); - lua_classvariable(luaVM, "devTools", "toggleBrowserDevTools", nullptr); - lua_classvariable(luaVM, "gpuEnabled", nullptr, "isBrowserGPUEnabled"); - - lua_registerclass(luaVM, "Browser", "DxTexture"); - - // Add GUI browser class - lua_newclass(luaVM); - lua_classfunction(luaVM, "create", "guiCreateBrowser"); - lua_classfunction(luaVM, "getBrowser", "guiGetBrowser"); - lua_classvariable(luaVM, "browser", nullptr, "guiGetBrowser"); - lua_registerclass(luaVM, "GuiBrowser", "GuiElement"); -} - std::variant CLuaBrowserDefs::CreateBrowser(lua_State* luaVM, CVector2D size, bool isLocal, std::optional transparent) { // texture createBrowser ( int width, int height, bool isLocal [, bool transparent = false] ) @@ -628,3 +531,100 @@ bool CLuaBrowserDefs::IsBrowserGPUEnabled() noexcept auto webCore = g_pCore->GetWebCore(); return webCore ? webCore->GetGPUEnabled() : false; } + +void CLuaBrowserDefs::LoadFunctions() +{ + // Define browser functions + constexpr static const std::pair functions[]{ + {"createBrowser", ArgumentParserWarn}, + {"requestBrowserDomains", ArgumentParserWarn}, + {"loadBrowserURL", ArgumentParserWarn}, + {"isBrowserLoading", ArgumentParserWarn}, + {"injectBrowserMouseMove", ArgumentParserWarn}, + {"injectBrowserMouseDown", ArgumentParserWarn}, + {"injectBrowserMouseUp", ArgumentParserWarn}, + {"injectBrowserMouseWheel", ArgumentParserWarn}, + {"getBrowserTitle", ArgumentParserWarn}, + {"getBrowserURL", ArgumentParserWarn}, + {"setBrowserRenderingPaused", ArgumentParserWarn}, + {"isBrowserRenderingPaused", ArgumentParser}, + {"executeBrowserJavascript", ArgumentParserWarn}, + {"getBrowserVolume", GetBrowserVolume}, + {"setBrowserVolume", ArgumentParserWarn}, + {"isBrowserDomainBlocked", ArgumentParserWarn}, + {"focusBrowser", ArgumentParserWarn}, + {"isBrowserFocused", ArgumentParserWarn}, + {"setBrowserProperty", ArgumentParserWarn}, + {"getBrowserProperty", ArgumentParserWarn}, + {"getBrowserSettings", ArgumentParserWarn}, + {"getBrowserSource", ArgumentParserWarn}, + {"setBrowserAjaxHandler", ArgumentParserWarn}, + {"canBrowserNavigateBack", ArgumentParserWarn}, + {"canBrowserNavigateForward", ArgumentParserWarn}, + {"navigateBrowserBack", ArgumentParserWarn}, + {"navigateBrowserForward", ArgumentParserWarn}, + {"reloadBrowserPage", ArgumentParserWarn}, + {"toggleBrowserDevTools", ArgumentParserWarn}, + {"resizeBrowser", ArgumentParserWarn}, + {"guiCreateBrowser", GUICreateBrowser}, + {"guiGetBrowser", GUIGetBrowser}, + {"isBrowserGPUEnabled", ArgumentParser}, + }; + + // Add browser functions + for (const auto& [name, func] : functions) + CLuaCFunctions::AddFunction(name, func); +} + +void CLuaBrowserDefs::AddClass(lua_State* luaVM) +{ + lua_newclass(luaVM); + + lua_classfunction(luaVM, "create", "createBrowser"); + lua_classfunction(luaVM, "loadURL", "loadBrowserURL"); + lua_classfunction(luaVM, "isLoading", "isBrowserLoading"); + lua_classfunction(luaVM, "injectMouseMove", "injectBrowserMouseMove"); + lua_classfunction(luaVM, "injectMouseDown", "injectBrowserMouseDown"); + lua_classfunction(luaVM, "injectMouseUp", "injectBrowserMouseUp"); + lua_classfunction(luaVM, "injectMouseWheel", "injectBrowserMouseWheel"); + lua_classfunction(luaVM, "getTitle", "getBrowserTitle"); + lua_classfunction(luaVM, "getURL", "getBrowserURL"); + lua_classfunction(luaVM, "setRenderingPaused", "setBrowserRenderingPaused"); + lua_classfunction(luaVM, "isRenderingPaused", "isBrowserRenderingPaused"); + lua_classfunction(luaVM, "executeJavascript", "executeBrowserJavascript"); + lua_classfunction(luaVM, "getVolume", "getBrowserVolume"); + lua_classfunction(luaVM, "setVolume", "setBrowserVolume"); + lua_classfunction(luaVM, "focus", "focusBrowser"); + lua_classfunction(luaVM, "isFocused", "isBrowserFocused"); + lua_classfunction(luaVM, "setProperty", "setBrowserProperty"); + lua_classfunction(luaVM, "getProperty", "getBrowserProperty"); + lua_classfunction(luaVM, "getSource", "getBrowserSource"); + lua_classfunction(luaVM, "setAjaxHandler", "setBrowserAjaxHandler"); + lua_classfunction(luaVM, "canNavigateBack", "canBrowserNavigateBack"); + lua_classfunction(luaVM, "canNavigateForward", "canBrowserNavigateForward"); + lua_classfunction(luaVM, "navigateBack", "navigateBrowserBack"); + lua_classfunction(luaVM, "navigateForward", "navigateBrowserForward"); + lua_classfunction(luaVM, "reloadPage", "reloadBrowserPage"); + lua_classfunction(luaVM, "toggleDevTools", "toggleBrowserDevTools"); + lua_classfunction(luaVM, "resize", "resizeBrowser"); + + lua_classfunction(luaVM, "requestDomains", "requestBrowserDomains"); + lua_classfunction(luaVM, "isDomainBlocked", "isBrowserDomainBlocked"); + + lua_classvariable(luaVM, "url", "loadBrowserURL", "getBrowserURL"); + lua_classvariable(luaVM, "loading", nullptr, "isBrowserLoading"); + lua_classvariable(luaVM, "title", nullptr, "getBrowserTitle"); + lua_classvariable(luaVM, "renderingPaused", "setBrowserRenderingPaused", "isBrowserRenderingPaused"); + lua_classvariable(luaVM, "volume", "setBrowserVolume", "getBrowserVolume"); + lua_classvariable(luaVM, "devTools", "toggleBrowserDevTools", nullptr); + lua_classvariable(luaVM, "gpuEnabled", nullptr, "isBrowserGPUEnabled"); + + lua_registerclass(luaVM, "Browser", "DxTexture"); + + // Add GUI browser class + lua_newclass(luaVM); + lua_classfunction(luaVM, "create", "guiCreateBrowser"); + lua_classfunction(luaVM, "getBrowser", "guiGetBrowser"); + lua_classvariable(luaVM, "browser", nullptr, "guiGetBrowser"); + lua_registerclass(luaVM, "GuiBrowser", "GuiElement"); +} From 13095342d33a92157b2df9508c5966a5c87ccfb9 Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Wed, 9 Sep 2026 06:59:24 +0300 Subject: [PATCH 6/9] Reorder static function declarations in CLuaBrowserDefs.h --- .../deathmatch/logic/luadefs/CLuaBrowserDefs.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h index 0be34d324ac..f4f29123d26 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h @@ -34,14 +34,14 @@ class CLuaBrowserDefs : public CLuaDefs static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); static bool ExecuteBrowserJavascript(CClientWebBrowser* browser, const std::string javascriptCode); LUA_DECLARE(GetBrowserVolume); - static std::variant SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume); - static std::optional IsBrowserDomainBlocked(const std::string url, std::optional isURL); - static bool FocusBrowser(std::optional browser); - static bool IsBrowserFocused(CClientWebBrowser* browser); - static bool SetBrowserProperty(CClientWebBrowser* browser, const std::string key, const std::string value); - static std::optional GetBrowserProperty(CClientWebBrowser* browser, const std::string key); - static auto GetBrowserSettings(); - static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction); + static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume); + static std::optional IsBrowserDomainBlocked(const std::string url, std::optional isURL); + static bool FocusBrowser(std::optional browser); + static bool IsBrowserFocused(CClientWebBrowser* browser); + static bool SetBrowserProperty(CClientWebBrowser* browser, const std::string key, const std::string value); + static std::optional GetBrowserProperty(CClientWebBrowser* browser, const std::string key); + static auto GetBrowserSettings(); + static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction); static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* browser, const std::string url, std::optional callbackFunction); static bool CanBrowserNavigateBack(CClientWebBrowser* browser); static bool CanBrowserNavigateForward(CClientWebBrowser* browser); From ce71994003745c46c21a45177d94419349fdfb52 Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Wed, 9 Sep 2026 22:48:07 +0300 Subject: [PATCH 7/9] Refactor CLuaBrowserDefs for improved readability --- .../logic/luadefs/CLuaBrowserDefs.cpp | 472 ++++++++---------- 1 file changed, 212 insertions(+), 260 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp index 4686feea314..b1505215ffb 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp @@ -11,8 +11,106 @@ #include "StdInc.h" #include "lua/CLuaFunctionParser.h" +#include #include +void CLuaBrowserDefs::LoadFunctions() +{ + // Define browser functions + constexpr static const std::pair functions[]{ + {"createBrowser", ArgumentParserWarn}, + {"requestBrowserDomains", ArgumentParserWarn}, + {"loadBrowserURL", ArgumentParserWarn}, + {"isBrowserLoading", ArgumentParserWarn}, + {"injectBrowserMouseMove", ArgumentParserWarn}, + {"injectBrowserMouseDown", ArgumentParserWarn}, + {"injectBrowserMouseUp", ArgumentParserWarn}, + {"injectBrowserMouseWheel", ArgumentParserWarn}, + {"getBrowserTitle", ArgumentParserWarn}, + {"getBrowserURL", ArgumentParserWarn}, + {"setBrowserRenderingPaused", ArgumentParserWarn}, + {"isBrowserRenderingPaused", ArgumentParser}, + {"executeBrowserJavascript", ArgumentParserWarn}, + {"getBrowserVolume", ArgumentParserWarn}, + {"setBrowserVolume", ArgumentParserWarn}, + {"isBrowserDomainBlocked", ArgumentParserWarn}, + {"focusBrowser", ArgumentParserWarn}, + {"isBrowserFocused", ArgumentParserWarn}, + {"setBrowserProperty", ArgumentParserWarn}, + {"getBrowserProperty", ArgumentParserWarn}, + {"getBrowserSettings", ArgumentParserWarn}, + {"getBrowserSource", ArgumentParserWarn}, + {"setBrowserAjaxHandler", ArgumentParserWarn}, + {"canBrowserNavigateBack", ArgumentParserWarn}, + {"canBrowserNavigateForward", ArgumentParserWarn}, + {"navigateBrowserBack", ArgumentParserWarn}, + {"navigateBrowserForward", ArgumentParserWarn}, + {"reloadBrowserPage", ArgumentParserWarn}, + {"toggleBrowserDevTools", ArgumentParserWarn}, + {"resizeBrowser", ArgumentParserWarn}, + {"guiCreateBrowser", ArgumentParserWarn}, + {"guiGetBrowser", ArgumentParserWarn}, + {"isBrowserGPUEnabled", ArgumentParser}, + }; + + // Add browser functions + for (const auto& [name, func] : functions) + CLuaCFunctions::AddFunction(name, func); +} + +void CLuaBrowserDefs::AddClass(lua_State* luaVM) +{ + lua_newclass(luaVM); + + lua_classfunction(luaVM, "create", "createBrowser"); + lua_classfunction(luaVM, "loadURL", "loadBrowserURL"); + lua_classfunction(luaVM, "isLoading", "isBrowserLoading"); + lua_classfunction(luaVM, "injectMouseMove", "injectBrowserMouseMove"); + lua_classfunction(luaVM, "injectMouseDown", "injectBrowserMouseDown"); + lua_classfunction(luaVM, "injectMouseUp", "injectBrowserMouseUp"); + lua_classfunction(luaVM, "injectMouseWheel", "injectBrowserMouseWheel"); + lua_classfunction(luaVM, "getTitle", "getBrowserTitle"); + lua_classfunction(luaVM, "getURL", "getBrowserURL"); + lua_classfunction(luaVM, "setRenderingPaused", "setBrowserRenderingPaused"); + lua_classfunction(luaVM, "isRenderingPaused", "isBrowserRenderingPaused"); + lua_classfunction(luaVM, "executeJavascript", "executeBrowserJavascript"); + lua_classfunction(luaVM, "getVolume", "getBrowserVolume"); + lua_classfunction(luaVM, "setVolume", "setBrowserVolume"); + lua_classfunction(luaVM, "focus", "focusBrowser"); + lua_classfunction(luaVM, "isFocused", "isBrowserFocused"); + lua_classfunction(luaVM, "setProperty", "setBrowserProperty"); + lua_classfunction(luaVM, "getProperty", "getBrowserProperty"); + lua_classfunction(luaVM, "getSource", "getBrowserSource"); + lua_classfunction(luaVM, "setAjaxHandler", "setBrowserAjaxHandler"); + lua_classfunction(luaVM, "canNavigateBack", "canBrowserNavigateBack"); + lua_classfunction(luaVM, "canNavigateForward", "canBrowserNavigateForward"); + lua_classfunction(luaVM, "navigateBack", "navigateBrowserBack"); + lua_classfunction(luaVM, "navigateForward", "navigateBrowserForward"); + lua_classfunction(luaVM, "reloadPage", "reloadBrowserPage"); + lua_classfunction(luaVM, "toggleDevTools", "toggleBrowserDevTools"); + lua_classfunction(luaVM, "resize", "resizeBrowser"); + + lua_classfunction(luaVM, "requestDomains", "requestBrowserDomains"); + lua_classfunction(luaVM, "isDomainBlocked", "isBrowserDomainBlocked"); + + lua_classvariable(luaVM, "url", "loadBrowserURL", "getBrowserURL"); + lua_classvariable(luaVM, "loading", nullptr, "isBrowserLoading"); + lua_classvariable(luaVM, "title", nullptr, "getBrowserTitle"); + lua_classvariable(luaVM, "renderingPaused", "setBrowserRenderingPaused", "isBrowserRenderingPaused"); + lua_classvariable(luaVM, "volume", "setBrowserVolume", "getBrowserVolume"); + lua_classvariable(luaVM, "devTools", "toggleBrowserDevTools", nullptr); + lua_classvariable(luaVM, "gpuEnabled", nullptr, "isBrowserGPUEnabled"); + + lua_registerclass(luaVM, "Browser", "DxTexture"); + + // Add GUI browser class + lua_newclass(luaVM); + lua_classfunction(luaVM, "create", "guiCreateBrowser"); + lua_classfunction(luaVM, "getBrowser", "guiGetBrowser"); + lua_classvariable(luaVM, "browser", nullptr, "guiGetBrowser"); + lua_registerclass(luaVM, "GuiBrowser", "GuiElement"); +} + std::variant CLuaBrowserDefs::CreateBrowser(lua_State* luaVM, CVector2D size, bool isLocal, std::optional transparent) { // texture createBrowser ( int width, int height, bool isLocal [, bool transparent = false] ) @@ -30,20 +128,20 @@ std::variant CLuaBrowserDefs::CreateBrowser(lua_State* if (!isLocal && !webCore->GetRemotePagesEnabled()) return false; - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (pLuaMain) + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (luaMain) { - CResource* pParentResource = pLuaMain->GetResource(); + CResource* parentResource = luaMain->GetResource(); - CClientWebBrowser* browserTexture = - g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser((int)size.fX, (int)size.fY, isLocal, transparent.value_or(false)); + CClientWebBrowser* browserTexture = g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser( + static_cast(size.fX), static_cast(size.fY), isLocal, transparent.value_or(false)); if (browserTexture) { - // Make it a child of the resource's file root ** CHECK Should parent be pFileResource, and element added to pParentResource's ElementGroup? ** - browserTexture->SetParent(pParentResource->GetResourceDynamicEntity()); + // Make it a child of the resource's file root ** CHECK Should parent be fileResource, and element added to parentResource's ElementGroup? ** + browserTexture->SetParent(parentResource->GetResourceDynamicEntity()); // Set our owner resource - browserTexture->SetResource(pParentResource); + browserTexture->SetResource(parentResource); } return browserTexture; } @@ -83,8 +181,8 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vectorIsLuaVMValid(luaVM) && VERIFY_FUNCTION(functionRef)) { - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (!pLuaMain) + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (!luaMain) return; CLuaArguments arguments; @@ -98,7 +196,7 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vectorInjectMouseMove((int)position.fX, (int)position.fY); + browser->InjectMouseMove(static_cast(position.fX), static_cast(position.fY)); return true; } @@ -192,37 +290,23 @@ bool CLuaBrowserDefs::ExecuteBrowserJavascript(CClientWebBrowser* browser, const throw std::invalid_argument("This function does not work with remote browsers"); } -int CLuaBrowserDefs::GetBrowserVolume(lua_State* luaVM) +float CLuaBrowserDefs::GetBrowserVolume(CClientWebBrowser* browser) { // float getBrowserVolume ( browser webBrowser ) - CClientWebBrowser* pWebBrowser; - - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pWebBrowser); - - if (!argStream.HasErrors()) - { - lua_pushnumber(luaVM, pWebBrowser->GetAudioVolume()); - return 1; - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return browser->GetAudioVolume(); } bool CLuaBrowserDefs::SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume) { // bool setBrowserVolume ( float volume ) // bool setBrowserVolume ( browser webBrowser, float volume ) - if (std::holds_alternative(webBrowserOrVolume)) + if (auto* volumeValue = std::get_if(&webBrowserOrVolume)) { auto webCore = g_pCore->GetWebCore(); - return webCore ? webCore->SetGlobalAudioVolume(std::get(webBrowserOrVolume)) : false; + return webCore ? webCore->SetGlobalAudioVolume(*volumeValue) : false; } - return std::get(webBrowserOrVolume)->SetAudioVolume(volume.value_or(0.0f)); + return (*std::get_if(&webBrowserOrVolume))->SetAudioVolume(volume.value_or(0.0f)); } std::optional CLuaBrowserDefs::IsBrowserDomainBlocked(const std::string url, std::optional isURL) @@ -232,7 +316,7 @@ std::optional CLuaBrowserDefs::IsBrowserDomainBlocked(const std::string ur if (!webCore) return std::nullopt; - SString domain = SString(url); + SString domain = url; if (isURL.value_or(false)) domain = webCore->GetDomainFromURL(domain); @@ -281,7 +365,7 @@ std::optional CLuaBrowserDefs::GetBrowserProperty(CClientWebBrowser return std::nullopt; } -auto CLuaBrowserDefs::GetBrowserSettings() +std::unordered_map CLuaBrowserDefs::GetBrowserSettings() { // table getBrowserSettings () auto webCore = g_pCore->GetWebCore(); @@ -297,11 +381,11 @@ auto CLuaBrowserDefs::GetBrowserSettings() bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction) { // bool getBrowserSource ( browser webBrowser, function callback ) - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (pLuaMain && VERIFY_FUNCTION(callbackFunction)) + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (luaMain && VERIFY_FUNCTION(callbackFunction)) { browser->GetSourceCode( - [callbackFunction, pLuaMain, browser](const std::string& code) + [callbackFunction, luaMain, browser](const std::string& code) { /* This function should not be called when the resource is about to stop as @@ -314,7 +398,7 @@ bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* brow // TODO: Use SCharStringRef/direct string access instead of copying strings around arguments.PushString(code); arguments.PushElement(browser); - arguments.Call(pLuaMain, callbackFunction); + arguments.Call(luaMain, callbackFunction); } }); @@ -372,96 +456,61 @@ bool CLuaBrowserDefs::ReloadBrowserPage(CClientWebBrowser* browser, std::optiona return true; } -int CLuaBrowserDefs::GUICreateBrowser(lua_State* luaVM) +std::variant CLuaBrowserDefs::GUICreateBrowser(lua_State* luaVM, CVector2D position, CVector2D size, bool isLocal, bool isTransparent, + std::optional relative, std::optional parent) { // element guiCreateBrowser ( float x, float y, float width, float height, bool isLocal, bool isTransparent, bool relative, [element parent = nil] ) - CVector2D position; - CVector2D size; - bool bIsLocal; - bool bIsTransparent; - bool bIsRelative; - CClientGUIElement* parent; - - CScriptArgReader argStream(luaVM); - argStream.ReadVector2D(position); - argStream.ReadVector2D(size); - argStream.ReadBool(bIsLocal); - argStream.ReadBool(bIsTransparent); - argStream.ReadBool(bIsRelative, false); - argStream.ReadUserData(parent, nullptr); - - if (!argStream.HasErrors()) - { - if (size.fX < 0) - { - argStream.SetCustomError("Browser width is smaller than 0", "Invalid parameter"); - } - else if (size.fY < 0) - { - argStream.SetCustomError("Browser height is smaller than 0", "Invalid parameter"); - } - else if (size.fX == 0 || size.fY == 0) - { - argStream.SetCustomError("A browser must be at least 1x1 in size.", "Invalid parameter"); - } - } + if (size.fX < 0) + throw std::invalid_argument("Browser width is smaller than 0"); + else if (size.fY < 0) + throw std::invalid_argument("Browser height is smaller than 0"); + else if (size.fX == 0 || size.fY == 0) + throw std::invalid_argument("A browser must be at least 1x1 in size."); - if (!argStream.HasErrors()) - { - auto pWebCore = g_pCore->GetWebCore(); - if (!bIsLocal && (!pWebCore || !pWebCore->GetRemotePagesEnabled())) - { - lua_pushboolean(luaVM, false); - return 1; - } + auto webCore = g_pCore->GetWebCore(); + if (!isLocal && (!webCore || !webCore->GetRemotePagesEnabled())) + return false; - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (pLuaMain) - { - CClientGUIElement* pGUIElement = - CStaticFunctionDefinitions::GUICreateBrowser(*pLuaMain, position, size, bIsLocal, bIsTransparent, bIsRelative, parent); + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (!luaMain) + return false; - if (pGUIElement) - { - lua_pushelement(luaVM, pGUIElement); - return 1; - } - else - { - argStream.SetCustomError("Failed to create browser element", "Create browser"); - } - } + CGUI* gui = g_pCore->GetGUI(); + + CGUIWebBrowser* webBrowser = gui->CreateWebBrowser(parent.has_value() ? (*parent)->GetCGUIElement() : gui->GetScriptRoot()); + webBrowser->SetPosition(position, relative.value_or(false)); + webBrowser->SetSize(size, relative.value_or(false)); + + // Register to the gui manager + CVector2D absoluteSize; + webBrowser->GetSize(absoluteSize, false); + auto guiBrowser = new CClientGUIWebBrowser(isLocal, isTransparent, static_cast(absoluteSize.fX), static_cast(absoluteSize.fY), m_pManager, + luaMain, webBrowser); + + if (!guiBrowser->GetBrowser()) + { + delete guiBrowser; + return false; } - if (argStream.HasErrors()) - return luaL_error(luaVM, argStream.GetFullErrorMessage()); + guiBrowser->SetParent(parent.has_value() ? *parent : luaMain->GetResource()->GetResourceGUIEntity()); + + // Load CEGUI element texture from webview + webBrowser->LoadFromWebView(guiBrowser->GetBrowser()->GetWebView()); + + if (parent.has_value() && !(*parent)->IsCallPropagationEnabled()) + guiBrowser->GetCGUIElement()->SetInheritsAlpha(false); - lua_pushboolean(luaVM, false); - return 1; + return guiBrowser; } -int CLuaBrowserDefs::GUIGetBrowser(lua_State* luaVM) // Or rather guiGetBrowserBrowser? +std::variant CLuaBrowserDefs::GUIGetBrowser(CClientGUIElement* guiElement) // Or rather guiGetBrowserBrowser? { // webbrowser guiGetBrowser ( gui-webbrowser browser ) - CClientGUIElement* pGUIElement; + if (guiElement->GetCGUIType() == CGUI_WEBBROWSER) + return static_cast(guiElement)->GetBrowser(); - CScriptArgReader argStream(luaVM); - argStream.ReadUserData(pGUIElement); - - if (!argStream.HasErrors()) - { - if (IS_GUI(pGUIElement) && pGUIElement->GetCGUIType() == CGUI_WEBBROWSER) - { - CClientGUIWebBrowser* pGUIBrowser = static_cast(pGUIElement); - lua_pushelement(luaVM, pGUIBrowser->GetBrowser()); - return 1; - } - } - else - m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage()); - - lua_pushboolean(luaVM, false); - return 1; + return false; } bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* browser, const std::string url, @@ -471,56 +520,56 @@ bool CLuaBrowserDefs::SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* if (!callbackFunction.has_value()) return browser->RemoveAjaxHandler(url); - CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); - if (pLuaMain && VERIFY_FUNCTION(callbackFunction.value())) + CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (luaMain && VERIFY_FUNCTION(callbackFunction.value())) { - CResource* pResource = pLuaMain->GetResource(); - CResourceManager* pResourceManager = m_pResourceManager; - auto netId = pResource->GetNetID(); - - bool bResult = browser->AddAjaxHandler(url, - [=](std::vector& vecGet, std::vector& vecPost) -> const std::string - { - // Make sure the resource is still running - if (!pResourceManager->Exists(pResource) || pResource->GetNetID() != netId) - { - return ""; - } - - // Make sure the function is valid - if (VERIFY_FUNCTION(callbackFunction.value())) - { - CLuaArguments arguments; - CLuaArguments getArguments; - CLuaArguments postArguments; - - for (auto&& param : vecGet) - getArguments.PushString(param); - - for (auto&& param : vecPost) - postArguments.PushString(param); - - arguments.PushTable(&getArguments); - arguments.PushTable(&postArguments); - - CLuaArguments result; - - arguments.Call(pLuaMain, callbackFunction.value(), &result); - - if (result.IsEmpty()) - return ""; - - CLuaArgument* returnedValue = *result.begin(); - if (returnedValue->GetType() == LUA_TSTRING) - return std::string(returnedValue->GetString()); - else - return ""; - } - else - return ""; - }); - - return bResult; + CResource* resource = luaMain->GetResource(); + CResourceManager* resourceManager = m_pResourceManager; + auto netId = resource->GetNetID(); + + bool result = browser->AddAjaxHandler(url, + [=](std::vector& get, std::vector& post) -> const std::string + { + // Make sure the resource is still running + if (!resourceManager->Exists(resource) || resource->GetNetID() != netId) + { + return ""; + } + + // Make sure the function is valid + if (VERIFY_FUNCTION(callbackFunction.value())) + { + CLuaArguments arguments; + CLuaArguments getArguments; + CLuaArguments postArguments; + + for (auto&& param : get) + getArguments.PushString(param); + + for (auto&& param : post) + postArguments.PushString(param); + + arguments.PushTable(&getArguments); + arguments.PushTable(&postArguments); + + CLuaArguments result; + + arguments.Call(luaMain, callbackFunction.value(), &result); + + if (result.IsEmpty()) + return ""; + + CLuaArgument* returnedValue = *result.begin(); + if (returnedValue->GetType() == LUA_TSTRING) + return std::string(returnedValue->GetString()); + else + return ""; + } + else + return ""; + }); + + return result; } return false; @@ -531,100 +580,3 @@ bool CLuaBrowserDefs::IsBrowserGPUEnabled() noexcept auto webCore = g_pCore->GetWebCore(); return webCore ? webCore->GetGPUEnabled() : false; } - -void CLuaBrowserDefs::LoadFunctions() -{ - // Define browser functions - constexpr static const std::pair functions[]{ - {"createBrowser", ArgumentParserWarn}, - {"requestBrowserDomains", ArgumentParserWarn}, - {"loadBrowserURL", ArgumentParserWarn}, - {"isBrowserLoading", ArgumentParserWarn}, - {"injectBrowserMouseMove", ArgumentParserWarn}, - {"injectBrowserMouseDown", ArgumentParserWarn}, - {"injectBrowserMouseUp", ArgumentParserWarn}, - {"injectBrowserMouseWheel", ArgumentParserWarn}, - {"getBrowserTitle", ArgumentParserWarn}, - {"getBrowserURL", ArgumentParserWarn}, - {"setBrowserRenderingPaused", ArgumentParserWarn}, - {"isBrowserRenderingPaused", ArgumentParser}, - {"executeBrowserJavascript", ArgumentParserWarn}, - {"getBrowserVolume", GetBrowserVolume}, - {"setBrowserVolume", ArgumentParserWarn}, - {"isBrowserDomainBlocked", ArgumentParserWarn}, - {"focusBrowser", ArgumentParserWarn}, - {"isBrowserFocused", ArgumentParserWarn}, - {"setBrowserProperty", ArgumentParserWarn}, - {"getBrowserProperty", ArgumentParserWarn}, - {"getBrowserSettings", ArgumentParserWarn}, - {"getBrowserSource", ArgumentParserWarn}, - {"setBrowserAjaxHandler", ArgumentParserWarn}, - {"canBrowserNavigateBack", ArgumentParserWarn}, - {"canBrowserNavigateForward", ArgumentParserWarn}, - {"navigateBrowserBack", ArgumentParserWarn}, - {"navigateBrowserForward", ArgumentParserWarn}, - {"reloadBrowserPage", ArgumentParserWarn}, - {"toggleBrowserDevTools", ArgumentParserWarn}, - {"resizeBrowser", ArgumentParserWarn}, - {"guiCreateBrowser", GUICreateBrowser}, - {"guiGetBrowser", GUIGetBrowser}, - {"isBrowserGPUEnabled", ArgumentParser}, - }; - - // Add browser functions - for (const auto& [name, func] : functions) - CLuaCFunctions::AddFunction(name, func); -} - -void CLuaBrowserDefs::AddClass(lua_State* luaVM) -{ - lua_newclass(luaVM); - - lua_classfunction(luaVM, "create", "createBrowser"); - lua_classfunction(luaVM, "loadURL", "loadBrowserURL"); - lua_classfunction(luaVM, "isLoading", "isBrowserLoading"); - lua_classfunction(luaVM, "injectMouseMove", "injectBrowserMouseMove"); - lua_classfunction(luaVM, "injectMouseDown", "injectBrowserMouseDown"); - lua_classfunction(luaVM, "injectMouseUp", "injectBrowserMouseUp"); - lua_classfunction(luaVM, "injectMouseWheel", "injectBrowserMouseWheel"); - lua_classfunction(luaVM, "getTitle", "getBrowserTitle"); - lua_classfunction(luaVM, "getURL", "getBrowserURL"); - lua_classfunction(luaVM, "setRenderingPaused", "setBrowserRenderingPaused"); - lua_classfunction(luaVM, "isRenderingPaused", "isBrowserRenderingPaused"); - lua_classfunction(luaVM, "executeJavascript", "executeBrowserJavascript"); - lua_classfunction(luaVM, "getVolume", "getBrowserVolume"); - lua_classfunction(luaVM, "setVolume", "setBrowserVolume"); - lua_classfunction(luaVM, "focus", "focusBrowser"); - lua_classfunction(luaVM, "isFocused", "isBrowserFocused"); - lua_classfunction(luaVM, "setProperty", "setBrowserProperty"); - lua_classfunction(luaVM, "getProperty", "getBrowserProperty"); - lua_classfunction(luaVM, "getSource", "getBrowserSource"); - lua_classfunction(luaVM, "setAjaxHandler", "setBrowserAjaxHandler"); - lua_classfunction(luaVM, "canNavigateBack", "canBrowserNavigateBack"); - lua_classfunction(luaVM, "canNavigateForward", "canBrowserNavigateForward"); - lua_classfunction(luaVM, "navigateBack", "navigateBrowserBack"); - lua_classfunction(luaVM, "navigateForward", "navigateBrowserForward"); - lua_classfunction(luaVM, "reloadPage", "reloadBrowserPage"); - lua_classfunction(luaVM, "toggleDevTools", "toggleBrowserDevTools"); - lua_classfunction(luaVM, "resize", "resizeBrowser"); - - lua_classfunction(luaVM, "requestDomains", "requestBrowserDomains"); - lua_classfunction(luaVM, "isDomainBlocked", "isBrowserDomainBlocked"); - - lua_classvariable(luaVM, "url", "loadBrowserURL", "getBrowserURL"); - lua_classvariable(luaVM, "loading", nullptr, "isBrowserLoading"); - lua_classvariable(luaVM, "title", nullptr, "getBrowserTitle"); - lua_classvariable(luaVM, "renderingPaused", "setBrowserRenderingPaused", "isBrowserRenderingPaused"); - lua_classvariable(luaVM, "volume", "setBrowserVolume", "getBrowserVolume"); - lua_classvariable(luaVM, "devTools", "toggleBrowserDevTools", nullptr); - lua_classvariable(luaVM, "gpuEnabled", nullptr, "isBrowserGPUEnabled"); - - lua_registerclass(luaVM, "Browser", "DxTexture"); - - // Add GUI browser class - lua_newclass(luaVM); - lua_classfunction(luaVM, "create", "guiCreateBrowser"); - lua_classfunction(luaVM, "getBrowser", "guiGetBrowser"); - lua_classvariable(luaVM, "browser", nullptr, "guiGetBrowser"); - lua_registerclass(luaVM, "GuiBrowser", "GuiElement"); -} From 6b0e8a82081b5d057a4fa95276cdf5986763cb2b Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Wed, 9 Sep 2026 22:48:29 +0300 Subject: [PATCH 8/9] Refactor browser-related function declarations --- .../logic/luadefs/CLuaBrowserDefs.h | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h index f4f29123d26..c5ab61ec10c 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.h @@ -33,15 +33,15 @@ class CLuaBrowserDefs : public CLuaDefs static bool SetBrowserRenderingPaused(CClientWebBrowser* browser, bool paused); static bool IsBrowserRenderingPaused(CClientWebBrowser* browser); static bool ExecuteBrowserJavascript(CClientWebBrowser* browser, const std::string javascriptCode); - LUA_DECLARE(GetBrowserVolume); - static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume); - static std::optional IsBrowserDomainBlocked(const std::string url, std::optional isURL); - static bool FocusBrowser(std::optional browser); - static bool IsBrowserFocused(CClientWebBrowser* browser); - static bool SetBrowserProperty(CClientWebBrowser* browser, const std::string key, const std::string value); - static std::optional GetBrowserProperty(CClientWebBrowser* browser, const std::string key); - static auto GetBrowserSettings(); - static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction); + static float GetBrowserVolume(CClientWebBrowser* browser); + static bool SetBrowserVolume(std::variant webBrowserOrVolume, std::optional volume); + static std::optional IsBrowserDomainBlocked(const std::string url, std::optional isURL); + static bool FocusBrowser(std::optional browser); + static bool IsBrowserFocused(CClientWebBrowser* browser); + static bool SetBrowserProperty(CClientWebBrowser* browser, const std::string key, const std::string value); + static std::optional GetBrowserProperty(CClientWebBrowser* browser, const std::string key); + static std::unordered_map GetBrowserSettings(); + static bool GetBrowserSource(lua_State* luaVM, CClientWebBrowser* browser, CLuaFunctionRef callbackFunction); static bool SetBrowserAjaxHandler(lua_State* luaVM, CClientWebBrowser* browser, const std::string url, std::optional callbackFunction); static bool CanBrowserNavigateBack(CClientWebBrowser* browser); static bool CanBrowserNavigateForward(CClientWebBrowser* browser); @@ -50,7 +50,8 @@ class CLuaBrowserDefs : public CLuaDefs static bool ReloadBrowserPage(CClientWebBrowser* browser, std::optional ignoreCache); static bool ToggleBrowserDevTools(CClientWebBrowser* browser, bool visible); static bool ResizeBrowser(CClientWebBrowser* browser, CVector2D size); - LUA_DECLARE(GUICreateBrowser); - LUA_DECLARE(GUIGetBrowser); - static bool IsBrowserGPUEnabled() noexcept; + static std::variant GUICreateBrowser(lua_State* luaVM, CVector2D position, CVector2D size, bool isLocal, bool isTransparent, + std::optional relative, std::optional parent); + static std::variant GUIGetBrowser(CClientGUIElement* guiElement); + static bool IsBrowserGPUEnabled() noexcept; }; From 119a54dc9d74ffca279317beb54a304df179069a Mon Sep 17 00:00:00 2001 From: Youssef Maged <57957016+iManGaaX@users.noreply.github.com> Date: Thu, 10 Sep 2026 02:24:05 +0300 Subject: [PATCH 9/9] Add lines --- .../deathmatch/logic/luadefs/CLuaBrowserDefs.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp index b1505215ffb..97fb1b682a1 100644 --- a/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp +++ b/Client/mods/deathmatch/logic/luadefs/CLuaBrowserDefs.cpp @@ -135,6 +135,7 @@ std::variant CLuaBrowserDefs::CreateBrowser(lua_State* CClientWebBrowser* browserTexture = g_pClientGame->GetManager()->GetRenderElementManager()->CreateWebBrowser( static_cast(size.fX), static_cast(size.fY), isLocal, transparent.value_or(false)); + if (browserTexture) { // Make it a child of the resource's file root ** CHECK Should parent be fileResource, and element added to parentResource's ElementGroup? ** @@ -143,6 +144,7 @@ std::variant CLuaBrowserDefs::CreateBrowser(lua_State* // Set our owner resource browserTexture->SetResource(parentResource); } + return browserTexture; } @@ -182,6 +184,7 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vectorIsLuaVMValid(luaVM) && VERIFY_FUNCTION(functionRef)) { CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (!luaMain) return; @@ -190,20 +193,25 @@ bool CLuaBrowserDefs::RequestBrowserDomains(lua_State* luaVM, std::vectorGetWebCore(); + if (!webCore) return false; + webCore->RequestPages(urls, VERIFY_FUNCTION(functionRef) ? &callback : nullptr); + return true; } @@ -313,10 +321,12 @@ std::optional CLuaBrowserDefs::IsBrowserDomainBlocked(const std::string ur { // bool isBrowserDomainBlocked ( string domain, bool isURL ) auto webCore = g_pCore->GetWebCore(); + if (!webCore) return std::nullopt; SString domain = url; + if (isURL.value_or(false)) domain = webCore->GetDomainFromURL(domain); @@ -334,6 +344,7 @@ bool CLuaBrowserDefs::FocusBrowser(std::optional browser) auto webCore = g_pCore->GetWebCore(); if (webCore) webCore->SetFocusedWebView(nullptr); + return true; } @@ -375,6 +386,7 @@ std::unordered_map CLuaBrowserDefs::GetBrowserSettings() {"RemoteJavascript", webCore ? webCore->GetRemoteJavascriptEnabled() : false}, {"PluginsEnabled", false}, }; + return settings; } @@ -382,6 +394,7 @@ bool CLuaBrowserDefs::GetBrowserSource(lua_State* luaVM, CClientWebBrowser* brow { // bool getBrowserSource ( browser webBrowser, function callback ) CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaVM); + if (luaMain && VERIFY_FUNCTION(callbackFunction)) { browser->GetSourceCode(