From 19737af4d08fcdc8609d71967f48d64ea24fe2ab Mon Sep 17 00:00:00 2001 From: IcePixelx <41352111+PixieCore@users.noreply.github.com> Date: Sat, 31 Jul 2021 16:09:48 +0200 Subject: [PATCH] Cleaned up the token implementation, fixed crash vectors. --- r5dev/include/id3dx.h | 2 +- r5dev/include/overlay.h | 13 ++++ r5dev/src/id3dx.cpp | 2 +- r5dev/src/overlay.cpp | 134 +++++++++++++++++----------------------- 4 files changed, 73 insertions(+), 78 deletions(-) diff --git a/r5dev/include/id3dx.h b/r5dev/include/id3dx.h index 88f5fe78..78ed097d 100644 --- a/r5dev/include/id3dx.h +++ b/r5dev/include/id3dx.h @@ -29,4 +29,4 @@ extern BOOL g_bShowBrowser; //################################################################################# // UTILS //################################################################################# -bool LoadTextureFromCharArray(unsigned char* image_data, const int& image_width, const int& image_height, ID3D11ShaderResourceView** out_srv); \ No newline at end of file +bool LoadTextureFromByteArray(unsigned char* image_data, const int& image_width, const int& image_height, ID3D11ShaderResourceView** out_srv); \ No newline at end of file diff --git a/r5dev/include/overlay.h b/r5dev/include/overlay.h index edc9913c..1dafde50 100644 --- a/r5dev/include/overlay.h +++ b/r5dev/include/overlay.h @@ -186,6 +186,19 @@ public: bool StartAsDedi = false; bool BroadCastServer = false; + //////////////////// + // Private Server // + //////////////////// + std::string PrivateServerToken = ""; + std::string PrivateServerPassword = ""; + std::string PrivateServerRequestMessage = ""; + ImVec4 PrivateServerMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); + + /* Texture */ + ID3D11ShaderResourceView* ApexLockIcon = nullptr; + int ApexLockIconWidth = 48; + int ApexLockIconHeight = 48; + void SetSection(ESection section) { CurrentSection = section; diff --git a/r5dev/src/id3dx.cpp b/r5dev/src/id3dx.cpp index 67893459..6e616430 100644 --- a/r5dev/src/id3dx.cpp +++ b/r5dev/src/id3dx.cpp @@ -492,7 +492,7 @@ void SetupDXSwapChain() //################################################################################# // UTILS //################################################################################# -bool LoadTextureFromCharArray(unsigned char* image_data, const int &image_width, const int &image_height, ID3D11ShaderResourceView** out_srv) +bool LoadTextureFromByteArray(unsigned char* image_data, const int &image_width, const int &image_height, ID3D11ShaderResourceView** out_srv) { // Load from disk into a raw RGBA buffer //int image_width = 0; diff --git a/r5dev/src/overlay.cpp b/r5dev/src/overlay.cpp index 21fd4a88..b200e797 100644 --- a/r5dev/src/overlay.cpp +++ b/r5dev/src/overlay.cpp @@ -410,7 +410,7 @@ void CCompanion::RefreshServerList() for (auto obj : root["servers"]) { ServerList.push_back( - new ServerListing(obj["name"], obj["map"], obj["ip"], obj["port"]) + new ServerListing(obj["name"].get(), obj["map"].get(), obj["ip"].get(), obj["port"].get()) ); } } @@ -434,8 +434,6 @@ void CCompanion::SendHostingPostRequest() body["port"] = hostport->m_pzsCurrentValue; //body["port"] = MyServer.port; body["password"] = MyServer.password; - - std::string body_str = body.dump(); #ifdef OVERLAY_DEBUG @@ -450,10 +448,10 @@ void CCompanion::SendHostingPostRequest() nlohmann::json res = nlohmann::json::parse(result->body); if (!res["success"] && !res["err"].is_null()) { - HostRequestMessage = res["err"]; + HostRequestMessage = res["err"].get(); HostRequestMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f); } - else if(res["success"] == true) + else if (res["success"].get() == true) { std::stringstream msg; msg << "Broadcasting! "; @@ -461,7 +459,7 @@ void CCompanion::SendHostingPostRequest() if (res["token"].is_string()) { msg << "Share the following token for people to connect: "; - HostToken = res["token"]; + HostToken = res["token"].get(); } HostRequestMessage = msg.str().c_str(); HostRequestMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); @@ -488,11 +486,12 @@ const nlohmann::json CCompanion::SendGetServerByTokenRequest(const std::string & std::string reqBody_str = reqBody.dump(); - auto res = client.Post("/server/byToken", reqBody_str.c_str(), reqBody_str.length(), "application/json"); + httplib::Result res = client.Post("/server/byToken", reqBody_str.c_str(), reqBody_str.length(), "application/json"); if (res && !res->body.empty()) { return nlohmann::json::parse(res->body); } + return nlohmann::json::object(); } @@ -527,8 +526,8 @@ void CCompanion::ServerBrowserSection() ImGui::EndGroup(); ImGui::Separator(); - const float footer_height_to_reserve = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); - ImGui::BeginChild("ServerListChild", { 0, -footer_height_to_reserve }, true, ImGuiWindowFlags_AlwaysVerticalScrollbar); + const float FooterHeight = ImGui::GetStyle().ItemSpacing.y + ImGui::GetFrameHeightWithSpacing(); + ImGui::BeginChild("ServerListChild", { 0, -FooterHeight }, true, ImGuiWindowFlags_AlwaysVerticalScrollbar); ImGui::BeginTable("##ServerBrowser_ServerList", 4, ImGuiWindowFlags_None); { ImGui::TableSetupColumn("Name", 0, 35); @@ -576,21 +575,22 @@ void CCompanion::ServerBrowserSection() ImGui::InputTextWithHint("##ServerBrowser_ServerConnString", "Enter an ip address or \"localhost\"", ServerConnStringBuffer, IM_ARRAYSIZE(ServerConnStringBuffer)); ImGui::SameLine(); + if (ImGui::Button("Connect", ImVec2(ImGui::GetWindowContentRegionWidth() * (1.f / 3.f /2.f), 19))) { //const char* replace = ""; // For history pos soon std::stringstream cmd; cmd << "connect " << ServerConnStringBuffer; ConnectToServer(ServerConnStringBuffer); - //strcpy_s(ServerConnStringBuffer, sizeof(replace), replace); // For history pos soon - // wut? } ImGui::SameLine(); if (ImGui::Button("Private Servers##ServerBrowser_PrivateServersButton", ImVec2(ImGui::GetWindowContentRegionWidth() * (1.f / 3.f / 2.f), 19))) + { ImGui::OpenPopup("Connect to a Private Server##PrivateServersConnectModal"); + } bool modalOpen = true; if (ImGui::BeginPopupModal("Connect to a Private Server##PrivateServersConnectModal", &modalOpen)) @@ -599,9 +599,7 @@ void CCompanion::ServerBrowserSection() ImGui::SetWindowSize(ImVec2(400.f, 200.f), ImGuiCond_Always); - int imgWidth = 48; - int imgHeight = 48; - static ID3D11ShaderResourceView* apex_private_servers_icon = NULL; + /* When removing this and adding the resource instead. Please initialize the texture in the CCompanion class constructor. - Pixie*/ static unsigned char lockedserver[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -655,87 +653,74 @@ void CCompanion::ServerBrowserSection() }; // ^^^^ // this definitely wont reach the final commit lmao - if (apex_private_servers_icon == nullptr) + // Nope it will for now -Pixie + if (!ApexLockIcon) { - bool ret = LoadTextureFromCharArray(lockedserver, imgWidth, imgHeight, &apex_private_servers_icon); - IM_ASSERT(ret); - + bool ret = LoadTextureFromByteArray(lockedserver, ApexLockIconWidth, ApexLockIconHeight, &ApexLockIcon); // Load texture from byte array. } - - // - - - static std::string token; - static std::string password; - - ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.00f, 0.00f, 0.00f, 0.00f)); // transparent bg - ImGui::BeginChild("##PrivateServersConnectModal_IconParent", ImVec2(imgWidth, imgHeight)); - ImGui::Image((void*)apex_private_servers_icon, ImVec2(imgWidth, imgHeight)); + ImGui::PushStyleColor(ImGuiCol_ChildBg, ImVec4(0.00f, 0.00f, 0.00f, 0.00f)); // Override the style color for child bg. + ImGui::BeginChild("##PrivateServersConnectModal_IconParent", ImVec2(ApexLockIconWidth, ApexLockIconHeight)); + { + ImGui::Image(ApexLockIcon, ImVec2(ApexLockIconWidth, ApexLockIconHeight)); // Display texture. + } ImGui::EndChild(); - ImGui::PopStyleColor(); - + ImGui::PopStyleColor(); // Pop the override for the child bg. ImGui::SameLine(); ImGui::Text("Enter the following details to continue"); - ImGui::PushItemWidth(ImGui::GetWindowContentRegionWidth()); - ImGui::InputTextWithHint("##PrivateServersConnectModal_TokenInput", "Token", &token); - ImGui::InputTextWithHint("##PrivateServersConnectModal_PasswordInput", "Password", &password, ImGuiInputTextFlags_Password); - ImGui::PopItemWidth(); + ImGui::PushItemWidth(ImGui::GetWindowContentRegionWidth()); // Override item width. + ImGui::InputTextWithHint("##PrivateServersConnectModal_TokenInput", "Token", &PrivateServerToken); + ImGui::InputTextWithHint("##PrivateServersConnectModal_PasswordInput", "Password", &PrivateServerPassword, ImGuiInputTextFlags_Password); + ImGui::PopItemWidth(); // Pop item width. - ImGui::Dummy(ImVec2(ImGui::GetWindowContentRegionWidth(), 19.f)); + ImGui::Dummy(ImVec2(ImGui::GetWindowContentRegionWidth(), 19.f)); // Place a dummy, basically making space inserting a blank element. - - static std::string reqMessage; - static ImVec4 reqMessageColor; - - ImGui::TextColored(reqMessageColor, reqMessage.c_str()); + ImGui::TextColored(PrivateServerMessageColor, PrivateServerRequestMessage.c_str()); ImGui::Separator(); if (ImGui::Button("Connect##PrivateServersConnectModal_ConnectButton", ImVec2(ImGui::GetWindowContentRegionWidth() / 2.f, 19))) { - nlohmann::json response = SendGetServerByTokenRequest(token, password); - if (response["success"]) + nlohmann::json response = SendGetServerByTokenRequest(PrivateServerToken, PrivateServerPassword); // Send token connect request. + if (response["success"].get()) // Was the response successful? { - auto server = response["server"]; + nlohmann::json server = response["server"].get(); // Get server field. - - if (server["ip"].is_string() && server["port"].is_string()) + if (server["ip"].is_string() && server["port"].is_string()) // Check if both field are a string. { - std::string name = server["name"]; - std::string ip = server["ip"]; - std::string port = server["port"]; + std::string name = server["name"].get(); // Please do this if you grab values from the response. + std::string ip = server["ip"].get(); + std::string port = server["port"].get(); - - ConnectToServer(ip, port); - reqMessage = "Found Server: " + name; - reqMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); - - + ConnectToServer(ip, port); // Connect to the server + PrivateServerRequestMessage = "Found Server: " + name; + PrivateServerMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); + ImGui::CloseCurrentPopup(); } } else { - std::string err = response["err"]; - reqMessage = "Error: " + err; - reqMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f); + std::string err = response["err"].get(); + PrivateServerRequestMessage = "Error: " + err; + PrivateServerMessageColor = ImVec4(1.00f, 0.00f, 0.00f, 1.00f); } } ImGui::SameLine(); + if (ImGui::Button("Close##PrivateServersConnectModal_CloseButton", ImVec2(ImGui::GetWindowContentRegionWidth() / 2.f, 19))) + { ImGui::CloseCurrentPopup(); + } ImGui::EndPopup(); } - } - void CCompanion::HostServerSection() { static std::string ServerNameErr = ""; @@ -866,6 +851,20 @@ void CCompanion::ExecCommand(const char* command_line) org_CommandExecute(NULL, command_line); } +void CCompanion::ConnectToServer(const std::string& ip, const std::string& port) +{ + std::stringstream cmd; + cmd << "connect " << ip << ":" << port; + g_ServerBrowser->ProcessCommand(cmd.str().c_str()); +} + +void CCompanion::ConnectToServer(const std::string& connString) +{ + std::stringstream cmd; + cmd << "connect " << connString; + g_ServerBrowser->ProcessCommand(cmd.str().c_str()); +} + //############################################################################# // INTERNALS //############################################################################# @@ -907,23 +906,6 @@ void Strtrim(char* s) } -void CCompanion::ConnectToServer(const std::string &ip, const std::string &port) -{ - - std::stringstream cmd; - cmd << "connect " << ip << ":" << port; - g_ServerBrowser->ProcessCommand(cmd.str().c_str()); -} - -void CCompanion::ConnectToServer(const std::string &connString) -{ - - std::stringstream cmd; - cmd << "connect " << connString; - g_ServerBrowser->ProcessCommand(cmd.str().c_str()); -} - - //############################################################################# // ENTRYPOINT //#############################################################################