diff --git a/external/imgui/src/imgui_stdlib.cpp b/external/imgui/src/imgui_stdlib.cpp new file mode 100644 index 00000000..cb1fe174 --- /dev/null +++ b/external/imgui/src/imgui_stdlib.cpp @@ -0,0 +1,76 @@ +// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) +// This is also an example of how you may wrap your own similar types. + +// Compatibility: +// - std::string support is only guaranteed to work from C++11. +// If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture) + +// Changelog: +// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string + +#include "imgui.h" +#include "imgui_stdlib.h" + +struct InputTextCallback_UserData +{ + std::string* Str; + ImGuiInputTextCallback ChainCallback; + void* ChainCallbackUserData; +}; + +static int InputTextCallback(ImGuiInputTextCallbackData* data) +{ + InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData; + if (data->EventFlag == ImGuiInputTextFlags_CallbackResize) + { + // Resize string callback + // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want. + std::string* str = user_data->Str; + IM_ASSERT(data->Buf == str->c_str()); + str->resize(data->BufTextLen); + data->Buf = (char*)str->c_str(); + } + else if (user_data->ChainCallback) + { + // Forward to user callback, if any + data->UserData = user_data->ChainCallbackUserData; + return user_data->ChainCallback(data); + } + return 0; +} + +bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + flags |= ImGuiInputTextFlags_CallbackResize; + + InputTextCallback_UserData cb_user_data; + cb_user_data.Str = str; + cb_user_data.ChainCallback = callback; + cb_user_data.ChainCallbackUserData = user_data; + return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data); +} + +bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + flags |= ImGuiInputTextFlags_CallbackResize; + + InputTextCallback_UserData cb_user_data; + cb_user_data.Str = str; + cb_user_data.ChainCallback = callback; + cb_user_data.ChainCallbackUserData = user_data; + return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data); +} + +bool ImGui::InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) +{ + IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0); + flags |= ImGuiInputTextFlags_CallbackResize; + + InputTextCallback_UserData cb_user_data; + cb_user_data.Str = str; + cb_user_data.ChainCallback = callback; + cb_user_data.ChainCallbackUserData = user_data; + return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data); +} diff --git a/r5dev/include/imgui_stdlib.h b/r5dev/include/imgui_stdlib.h new file mode 100644 index 00000000..f860b0c7 --- /dev/null +++ b/r5dev/include/imgui_stdlib.h @@ -0,0 +1,22 @@ +// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) +// This is also an example of how you may wrap your own similar types. + +// Compatibility: +// - std::string support is only guaranteed to work from C++11. +// If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture) + +// Changelog: +// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string + +#pragma once + +#include + +namespace ImGui +{ + // ImGui::InputText() with std::string + // Because text input needs dynamic resizing, we need to setup a callback to grow the capacity + IMGUI_API bool InputText(const char* label, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextMultiline(const char* label, std::string* str, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); + IMGUI_API bool InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); +} diff --git a/r5dev/include/overlay.h b/r5dev/include/overlay.h index bb366bdd..f948f332 100644 --- a/r5dev/include/overlay.h +++ b/r5dev/include/overlay.h @@ -178,11 +178,10 @@ public: //////////////////// // Host Server // //////////////////// + ServerListing MyServer; std::vector MapsList; - std::string* SelectedMap = nullptr; std::string HostRequestMessage = ""; ImVec4 HostRequestMessageColor = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); - char ServerNameBuffer[64] = { 0 }; bool StartAsDedi = false; bool BroadCastServer = false; @@ -254,7 +253,8 @@ public: } void RefreshServerList(); - void SendHostingPostRequest(char* mapName); + void UpdateMyServerInfo(); + void SendHostingPostRequest(); void CompMenu(); void ServerBrowserSection(); void SettingsSection(); diff --git a/r5dev/include/pch.h b/r5dev/include/pch.h index d05f4e31..3d46b534 100644 --- a/r5dev/include/pch.h +++ b/r5dev/include/pch.h @@ -26,6 +26,7 @@ // Our headers #include "imgui.h" +#include "imgui_stdlib.h" #include "imgui_impl_dx11.h" #include "imgui_impl_win32.h" #include "spdlog.h" diff --git a/r5dev/include/serverlisting.h b/r5dev/include/serverlisting.h index b49e7fa3..2979d812 100644 --- a/r5dev/include/serverlisting.h +++ b/r5dev/include/serverlisting.h @@ -3,6 +3,9 @@ class ServerListing { public: + + ServerListing() = default; + ServerListing(std::string name, std::string map, std::string ip, std::string port) : name(name), map(map), ip(ip), port(port) { // for future constructor use. diff --git a/r5dev/r5dev.vcxproj b/r5dev/r5dev.vcxproj index 271412f4..4725e485 100644 --- a/r5dev/r5dev.vcxproj +++ b/r5dev/r5dev.vcxproj @@ -293,6 +293,7 @@ + @@ -320,6 +321,9 @@ NotUsing + + NotUsing + NotUsing diff --git a/r5dev/r5dev.vcxproj.filters b/r5dev/r5dev.vcxproj.filters index 069110c7..88c4db0c 100644 --- a/r5dev/r5dev.vcxproj.filters +++ b/r5dev/r5dev.vcxproj.filters @@ -108,6 +108,9 @@ Source Files + + External Libraries\imgui\Source Files + @@ -476,6 +479,9 @@ Header Files + + External Libraries\imgui\Header Files + diff --git a/r5dev/src/overlay.cpp b/r5dev/src/overlay.cpp index 4deb27c9..ebe76fa9 100644 --- a/r5dev/src/overlay.cpp +++ b/r5dev/src/overlay.cpp @@ -325,7 +325,6 @@ int CGameConsole::TextEditCallback(ImGuiInputTextCallbackData* data) CCompanion::CCompanion() { memset(MatchmakingServerStringBuffer, 0, sizeof(MatchmakingServerStringBuffer)); - memset(ServerNameBuffer, 0, sizeof(ServerNameBuffer)); memset(ServerConnStringBuffer, 0, sizeof(ServerConnStringBuffer)); strcpy_s(MatchmakingServerStringBuffer, "r5a-comp-sv.herokuapp.com"); @@ -338,8 +337,9 @@ CCompanion::CCompanion() filename = filename.substr(0, filename.size() - 6); MapsList.push_back(filename); } - - SelectedMap = &MapsList[0]; + + // copy assignment kjek + MyServer.map = MapsList[0]; static std::thread HostingServerRequestThread([this]() { @@ -353,6 +353,12 @@ CCompanion::CCompanion() HostingServerRequestThread.detach(); } +void CCompanion::UpdateMyServerInfo() +{ + MyServer.map = GameGlobals::HostState->m_levelName; + MyServer.port = ((CVValue_t*)(0x141734DD0 + 0x58))->m_pszString; + +} void CCompanion::UpdateHostingStatus() { if (!GameGlobals::HostState) // Is HostState valid? @@ -373,7 +379,8 @@ void CCompanion::UpdateHostingStatus() if (!BroadCastServer) // Do we wanna broadcast server to the browser? break; - SendHostingPostRequest(GameGlobals::HostState->m_levelName); + UpdateMyServerInfo(); + SendHostingPostRequest(); break; } default: @@ -415,19 +422,16 @@ void CCompanion::RefreshServerList() } } -void CCompanion::SendHostingPostRequest(char* mapName) +void CCompanion::SendHostingPostRequest() { httplib::Client client(MatchmakingServerStringBuffer); client.set_connection_timeout(10); // send a post request to "/servers/add" with a json body nlohmann::json body = nlohmann::json::object(); - body["name"] = ServerNameBuffer; - body["map"] = mapName; - - CVValue_t* hostport_value = (CVValue_t*)(0x141734DD0 + 0x58); - - body["port"] = hostport_value->m_pszString; + body["name"] = MyServer.name; + body["map"] = MyServer.map; + body["port"] = MyServer.port; std::string body_str = body.dump(); @@ -555,15 +559,15 @@ void CCompanion::HostServerSection() { static std::string ServerNameErr = ""; - ImGui::InputTextWithHint("Server Name##ServerHost_ServerName", "Required Field", ServerNameBuffer, IM_ARRAYSIZE(ServerNameBuffer)); + ImGui::InputTextWithHint("Server Name##ServerHost_ServerName", "Required Field", &MyServer.name); ImGui::Spacing(); - if (ImGui::BeginCombo("Map##ServerHost_MapListBox", SelectedMap->c_str())) + if (ImGui::BeginCombo("Map##ServerHost_MapListBox", MyServer.map.c_str())) { for (auto& item : MapsList) { - if (ImGui::Selectable(item.c_str(), &item == SelectedMap)) + if (ImGui::Selectable(item.c_str(), item == MyServer.map)) { - SelectedMap = &item; + MyServer.map = item; } } ImGui::EndCombo(); @@ -580,13 +584,13 @@ void CCompanion::HostServerSection() if (ImGui::Button("Start The Server##ServerHost_StartServerButton", ImVec2(ImGui::GetWindowSize().x, 32))) { - if (strlen(ServerNameBuffer) != 0) + if (!MyServer.name.empty()) { ServerNameErr = std::string(); UpdateHostingStatus(); std::stringstream cmd; - cmd << "map " << SelectedMap->c_str(); + cmd << "map " << MyServer.map; ProcessCommand(cmd.str().c_str()); if (StartAsDedi)