Refactored companion to use same serverlisting class used for external servers to keep its internal hosting state

This commit is contained in:
alexsandulescu 2021-07-24 19:55:23 +03:00
parent 53383fc5a9
commit b473d23f7d
8 changed files with 136 additions and 20 deletions

76
external/imgui/src/imgui_stdlib.cpp vendored Normal file
View File

@ -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);
}

View File

@ -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 <string>
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);
}

View File

@ -178,11 +178,10 @@ public:
////////////////////
// Host Server //
////////////////////
ServerListing MyServer;
std::vector<std::string> 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();

View File

@ -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"

View File

@ -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.

View File

@ -293,6 +293,7 @@
<ClInclude Include="include\hooks.h" />
<ClInclude Include="include\httplib.h" />
<ClInclude Include="include\id3dx.h" />
<ClInclude Include="include\imgui_stdlib.h" />
<ClInclude Include="include\input.h" />
<ClInclude Include="include\json.hpp" />
<ClInclude Include="include\opcptc.h" />
@ -320,6 +321,9 @@
<ClCompile Include="..\external\imgui\src\imgui_impl_win32.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\external\imgui\src\imgui_stdlib.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="..\external\imgui\src\imgui_tables.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>

View File

@ -108,6 +108,9 @@
<ClCompile Include="src\pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\external\imgui\src\imgui_stdlib.cpp">
<Filter>External Libraries\imgui\Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\console.h">
@ -476,6 +479,9 @@
<ClInclude Include="include\pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="include\imgui_stdlib.h">
<Filter>External Libraries\imgui\Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="r5dev.def">

View File

@ -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)