Support NetImgui on dedicated server

Support the creation of ImGui context on dedicated server (remove the requirements on the game viewport and slate application)
Add an option to auto connect to the netimgui server when running on a dedicated game server. On by default.
This commit is contained in:
Arnaud Jamin
2025-01-02 22:44:19 -05:00
parent 123ae5b68f
commit 9a0c32c4b8
6 changed files with 260 additions and 103 deletions
@@ -1,8 +1,13 @@
#include "CogEngineWindow_NetImGui.h"
#include "CogImguiContext.h"
#include "CogImguiHelper.h"
#include "CogWindowConsoleCommandManager.h"
#include "CogWindowManager.h"
#include "CogWindowWidgets.h"
#include "imgui.h"
#include "imgui_internal.h"
#include "Misc/CoreMisc.h"
#include "NetImgui_Api.h"
//--------------------------------------------------------------------------------------------------------------------------
@@ -11,11 +16,54 @@ void FCogEngineWindow_NetImGui::Initialize()
Super::Initialize();
bHasMenu = true;
Config = GetConfig<UCogEngineConfig_NetImGui>();
FCogWindowConsoleCommandManager::RegisterWorldConsoleCommand(
TEXT("Cog.NetImgui.ConnectTo"),
TEXT("Connect to NetImgui server"),
GetWorld(),
FCogWindowConsoleCommandDelegate::CreateLambda([this](const TArray<FString>& InArgs, UWorld* InWorld)
{
ConnectTo();
}));
FCogWindowConsoleCommandManager::RegisterWorldConsoleCommand(
TEXT("Cog.NetImgui.ConnectFrom"),
TEXT("Listen for NetImgui server connection"),
GetWorld(),
FCogWindowConsoleCommandDelegate::CreateLambda([this](const TArray<FString>& InArgs, UWorld* InWorld)
{
ConnectFrom();
}));
FCogWindowConsoleCommandManager::RegisterWorldConsoleCommand(
TEXT("Cog.NetImgui.Disconnect"),
TEXT("Disconnect from NetImgui server"),
GetWorld(),
FCogWindowConsoleCommandDelegate::CreateLambda([this](const TArray<FString>& InArgs, UWorld* InWorld)
{
Disconnect();
}));
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineWindow_NetImGui::Shutdown()
{
if (NetImgui::IsConnected())
{
ImGui::TextUnformatted("Status: Connected");
if (ImGui::Button("Disconnect"))
{
NetImgui::Disconnect();
}
}
else if (NetImgui::IsConnectionPending())
{
ImGui::TextUnformatted("Status: Waiting Server");
if (ImGui::Button("Cancel"))
{
NetImgui::Disconnect();
}
}
}
//--------------------------------------------------------------------------------------------------------------------------
@@ -31,6 +79,23 @@ void FCogEngineWindow_NetImGui::RenderHelp()
ImGui::Text("");
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineWindow_NetImGui::RenderTick(float DeltaTime)
{
//------------------------------------------------------
// Before auto connecting, wait for NetImgui startup,
// which require imgui context to be initialized
//------------------------------------------------------
if (HasAlreadyTriedToConnectOnDedicatedServer == false
&& Config->AutoConnectOnDedicatedServer
&& UCogWindowManager::GetIsNetImguiInitialized()
&& IsRunningDedicatedServer())
{
ConnectFrom();
HasAlreadyTriedToConnectOnDedicatedServer = true;
}
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineWindow_NetImGui::RenderContent()
{
@@ -52,6 +117,8 @@ void FCogEngineWindow_NetImGui::RenderContent()
}
ImGui::InputInt("Server Port", &Config->ServerPort);
ImGui::Separator();
{
static char Buffer[256] = "";
ImStrncpy(Buffer, TCHAR_TO_ANSI(*Config->ClientName), IM_ARRAYSIZE(Buffer));
@@ -62,6 +129,9 @@ void FCogEngineWindow_NetImGui::RenderContent()
}
ImGui::InputInt("Client Port", &Config->ClientPort);
ImGui::Separator();
ImGui::Checkbox("Auto Listen On Dedicated Server", &Config->AutoConnectOnDedicatedServer);
ImGui::EndMenu();
}
@@ -90,9 +160,7 @@ void FCogEngineWindow_NetImGui::RenderContent()
if (ImGui::Button("Connect", ImVec2(ImGui::GetContentRegionAvail().x, 0)))
{
const auto clientName = StringCast<ANSICHAR>(*Config->ClientName);
const auto serverName = StringCast<ANSICHAR>(*Config->ServerName);
NetImgui::ConnectToApp(clientName.Get(), serverName.Get(), Config->ServerPort, nullptr, nullptr);
ConnectTo();
}
if (ImGui::IsItemHovered())
{
@@ -101,8 +169,7 @@ void FCogEngineWindow_NetImGui::RenderContent()
if (ImGui::Button("Listen", ImVec2(ImGui::GetContentRegionAvail().x, 0)))
{
const auto clientName = StringCast<ANSICHAR>(*Config->ClientName);
NetImgui::ConnectFromApp(clientName.Get(), Config->ClientPort, nullptr, nullptr);
ConnectFrom();
}
if (ImGui::IsItemHovered())
{
@@ -113,6 +180,44 @@ void FCogEngineWindow_NetImGui::RenderContent()
#endif // #if NETIMGUI_ENABLED
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineWindow_NetImGui::ConnectTo()
{
FCogImGuiContextScope ImGuiContextScope(GetOwner()->GetContext());
UE_LOG(LogCogImGui, Verbose, TEXT("FCogEngineWindow_NetImGui::ConnectTo | Client:%s | Server:%s | ServerPort:%d"),
*Config->ClientName,
*Config->ServerName,
Config->ServerPort);
const auto clientName = StringCast<ANSICHAR>(*Config->ClientName);
const auto serverName = StringCast<ANSICHAR>(*Config->ServerName);
NetImgui::ConnectToApp(clientName.Get(), serverName.Get(), Config->ServerPort, nullptr, nullptr);
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineWindow_NetImGui::ConnectFrom()
{
FCogImGuiContextScope ImGuiContextScope(GetOwner()->GetContext());
UE_LOG(LogCogImGui, Verbose, TEXT("FCogEngineWindow_NetImGui::ConnectFrom | Client:%s | ClientPort:%d"),
*Config->ClientName,
Config->ClientPort);
const auto clientName = StringCast<ANSICHAR>(*Config->ClientName);
NetImgui::ConnectFromApp(clientName.Get(), Config->ClientPort, nullptr, nullptr);
}
//--------------------------------------------------------------------------------------------------------------------------
void FCogEngineWindow_NetImGui::Disconnect()
{
FCogImGuiContextScope ImGuiContextScope(GetOwner()->GetContext());
UE_LOG(LogCogImGui, Verbose, TEXT("FCogEngineWindow_NetImGui::Disconnect"));
NetImgui::Disconnect();
}
//--------------------------------------------------------------------------------------------------------------------------
void UCogEngineConfig_NetImGui::Reset()
{
Super::Reset();