From 83b2823a9f52486a933bd08800246cacdc63d279 Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Fri, 25 Nov 2022 09:43:24 +0100 Subject: [PATCH] Add /pal test-connection --- Pal.Client/Pal.Client.csproj | 3 +- Pal.Client/Plugin.cs | 10 +++++ Pal.Client/Windows/ConfigWindow.cs | 64 ++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/Pal.Client/Pal.Client.csproj b/Pal.Client/Pal.Client.csproj index f540498..fbe8882 100644 --- a/Pal.Client/Pal.Client.csproj +++ b/Pal.Client/Pal.Client.csproj @@ -3,7 +3,7 @@ net6.0-windows 9.0 - 1.18.0.0 + 1.19.0.0 enable @@ -13,6 +13,7 @@ Palace Pal true false + true diff --git a/Pal.Client/Plugin.cs b/Pal.Client/Plugin.cs index c95ab86..df29722 100644 --- a/Pal.Client/Plugin.cs +++ b/Pal.Client/Plugin.cs @@ -122,6 +122,16 @@ namespace Pal.Client Task.Run(async () => await FetchFloorStatistics()); break; + case "test-connection": + case "tc": + var configWindow = Service.WindowSystem.GetWindow(); + if (configWindow == null) + return; + + configWindow.IsOpen = true; + configWindow.TestConnection(); + break; + #if DEBUG case "update-saves": LocalState.UpdateAll(); diff --git a/Pal.Client/Windows/ConfigWindow.cs b/Pal.Client/Windows/ConfigWindow.cs index 8dd4306..1ae7f47 100644 --- a/Pal.Client/Windows/ConfigWindow.cs +++ b/Pal.Client/Windows/ConfigWindow.cs @@ -10,6 +10,8 @@ using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Reflection; +using System.Runtime.InteropServices; +using System.Text; using System.Threading; using System.Threading.Tasks; @@ -29,6 +31,7 @@ namespace Pal.Client.Windows private bool _fillSilverCoffers; private string? _connectionText; + private bool _switchToCommunityTab; public ConfigWindow() : base("Palace Pal###PalPalaceConfig") { @@ -109,8 +112,10 @@ namespace Pal.Client.Windows ImGui.EndTabItem(); } - if (ImGui.BeginTabItem("Community")) + if (BeginTabItemEx("Community", _switchToCommunityTab ? ImGuiTabItemFlags.SetSelected : ImGuiTabItemFlags.None)) { + _switchToCommunityTab = false; + ImGui.TextWrapped("Ideally, we want to discover every potential trap and chest location in the game, but doing this alone is very tedious. Floor 51-60 has over 100 trap locations and over 50 coffer locations, the last of which took over 50 runs to find - and we don't know if that map is complete. Higher floors naturally see fewer runs, making solo attempts to map the place much harder."); ImGui.TextWrapped("You can decide whether you want to share traps and chests you find with the community, which likewise also will let you see chests and coffers found by other players. This can be changed at any time. No data regarding your FFXIV character or account is ever sent to our server."); @@ -122,25 +127,7 @@ namespace Pal.Client.Windows ImGui.BeginDisabled(Service.Configuration.Mode != Configuration.EMode.Online); if (ImGui.Button("Test Connection")) - { - Task.Run(async () => - { - _connectionText = "Testing..."; - - CancellationTokenSource cts = new CancellationTokenSource(); - cts.CancelAfter(TimeSpan.FromSeconds(60)); - - try - { - _connectionText = await Service.RemoteApi.VerifyConnection(cts.Token); - } - catch (Exception e) - { - PluginLog.Error(e, "Could not establish remote connection"); - _connectionText = e.ToString(); - } - }); - } + TestConnection(); if (_connectionText != null) ImGui.Text(_connectionText); @@ -262,5 +249,42 @@ namespace Pal.Client.Windows Service.Chat.PrintError("Could not draw markers, is Splatoon installed and enabled?"); } } + + /// + /// None of the default BeginTabItem methods allow using flags without making the tab have a close button for some reason. + /// + private unsafe static bool BeginTabItemEx(string label, ImGuiTabItemFlags flags) + { + int labelLength = Encoding.UTF8.GetByteCount(label); + byte* labelPtr = stackalloc byte[labelLength + 1]; + byte[] labelBytes = Encoding.UTF8.GetBytes(label); + + Marshal.Copy(labelBytes, 0, (IntPtr)labelPtr, labelLength); + labelPtr[labelLength] = 0; + + return ImGuiNative.igBeginTabItem(labelPtr, null, flags) != 0; + } + + internal void TestConnection() + { + Task.Run(async () => + { + _connectionText = "Testing..."; + _switchToCommunityTab = true; + + CancellationTokenSource cts = new CancellationTokenSource(); + cts.CancelAfter(TimeSpan.FromSeconds(60)); + + try + { + _connectionText = await Service.RemoteApi.VerifyConnection(cts.Token); + } + catch (Exception e) + { + PluginLog.Error(e, "Could not establish remote connection"); + _connectionText = e.ToString(); + } + }); + } } }