Skip to content

Commit 725b8fb

Browse files
authored
Expose net info debug view to users with dev mode (#828)
Also reduce verbosity of the messages
1 parent 979b2df commit 725b8fb

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

Source/Client/Windows/ChatWindow.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ private void DrawInfo(Rect inRect)
120120
Widgets.Label(inRect, Multiplayer.session.gameName);
121121
inRect.yMin += Text.CalcHeight(Multiplayer.session.gameName, inRect.width) + 10f;
122122

123+
if (Prefs.DevMode)
124+
{
125+
var rect = new Rect(inRect).Height(25f).Width(80f);
126+
if (Widgets.ButtonText(rect, "Net info"))
127+
Find.WindowStack.Add(new DebugTextWindow(NetInfoText()));
128+
129+
inRect.yMin += rect.height + 10f;
130+
}
131+
123132
DrawList(
124133
"MpChatPlayers".Translate(Multiplayer.session.players.Count),
125134
Multiplayer.session.players,
@@ -350,9 +359,7 @@ public void SendMsg()
350359

351360
if (currentMsg.NullOrEmpty()) return;
352361

353-
if (MpVersion.IsDebug && currentMsg == "/netinfo")
354-
Find.WindowStack.Add(new DebugTextWindow(NetInfoText()));
355-
else if (Multiplayer.Client == null)
362+
if (Multiplayer.Client == null)
356363
Multiplayer.session.AddMsg(Multiplayer.username + ": " + currentMsg);
357364
else
358365
Multiplayer.Client.Send(ClientChatPacket.Create(currentMsg));
@@ -366,21 +373,13 @@ private string NetInfoText()
366373

367374
var text = new StringBuilder();
368375

369-
void LogNetData(string name, NetStatistics stats)
376+
if (Multiplayer.Client is ClientLiteNetConnection conn)
370377
{
371-
text.AppendLine(name);
372-
text.AppendLine($"Bytes received: {stats.BytesReceived}");
373-
text.AppendLine($"Bytes sent: {stats.BytesSent}");
374-
text.AppendLine($"Packets received: {stats.PacketsReceived}");
375-
text.AppendLine($"Packets sent: {stats.PacketsSent}");
376-
text.AppendLine($"Packet loss: {stats.PacketLoss}");
377-
text.AppendLine($"Packet loss percent: {stats.PacketLossPercent}");
378+
text.AppendLine("Client");
379+
text.AppendLine(conn.peer.Statistics.ToDebugString());
378380
text.AppendLine();
379381
}
380382

381-
if (Multiplayer.Client is ClientLiteNetConnection conn)
382-
LogNetData("Client", conn.peer.Statistics);
383-
384383
if (Multiplayer.LocalServer != null)
385384
{
386385
foreach (var man in Multiplayer.LocalServer.netManagers)
@@ -406,16 +405,19 @@ void LogNetData(string name, NetStatistics stats)
406405
text.AppendLine($"Connecting: {state.m_bConnecting}");
407406
text.AppendLine($"Error: {state.m_eP2PSessionError}");
408407
text.AppendLine($"Using relay: {state.m_bUsingRelay}");
409-
text.AppendLine($"Bytes to send: {state.m_nBytesQueuedForSend}");
410-
text.AppendLine($"Packets to send: {state.m_nPacketsQueuedForSend}");
411-
text.AppendLine($"Remote IP: {state.m_nRemoteIP}");
412-
text.AppendLine($"Remote port: {state.m_nRemotePort}");
408+
text.AppendLine($"Send queue: {state.m_nBytesQueuedForSend}B {state.m_nPacketsQueuedForSend} packets");
409+
text.AppendLine($"Remote IP: {state.m_nRemoteIP}:{state.m_nRemotePort}");
413410
}
414411
else
415412
{
416413
text.AppendLine("No connection");
417414
}
418415

416+
foreach (var pending in Multiplayer.session.pendingSteam)
417+
{
418+
text.AppendLine($"Steam pending {pending}:{SteamFriends.GetFriendPersonaName(remote)}");
419+
}
420+
419421
text.AppendLine();
420422
}
421423

Source/Client/Windows/DebugTextWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void DoWindowContents(Rect inRect)
3232

3333
Text.Font = GameFont.Tiny;
3434

35-
if (Widgets.ButtonText(new Rect(0, 0, 55f, 20f), "Copy all"))
35+
if (Widgets.ButtonText(new Rect(0, 0, 65f, 20f), "Copy all"))
3636
GUIUtility.systemCopyBuffer = text;
3737

3838
Text.Font = GameFont.Small;

Source/Common/LiteNetManager.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public string GetDiagnosticsInfo()
7070
foreach (var (endpoint, man) in netManagers)
7171
{
7272
text.AppendLine($"{endpoint}");
73-
text.AppendLine($"{man.Statistics}");
73+
text.AppendLine(man.Statistics.ToDebugString());
7474
}
7575
return text.ToString();
7676
}
@@ -132,7 +132,7 @@ public void Tick()
132132
public void Stop() => lanManager.Stop();
133133

134134
public string GetDiagnosticsName() => $"Lan (LiteNet) {lanAddress}:{lanManager.LocalPort}";
135-
public string GetDiagnosticsInfo() => lanManager.Statistics.ToString();
135+
public string GetDiagnosticsInfo() => lanManager.Statistics.ToDebugString();
136136
}
137137

138138
public class LiteNetEndpoint
@@ -149,4 +149,16 @@ public override string ToString()
149149
$"{ipv4}:{port} / {ipv6}:{port}";
150150
}
151151
}
152+
153+
public static class LiteNetExtensions
154+
{
155+
public static string ToDebugString(this NetStatistics stats)
156+
{
157+
var text = new StringBuilder();
158+
text.AppendLine($"Recv: {stats.BytesReceived}B {stats.PacketsReceived} packets");
159+
text.AppendLine($"Sent: {stats.BytesSent}B {stats.PacketsSent} packets");
160+
text.AppendLine($"Loss: {stats.PacketLoss} packets ({stats.PacketLossPercent}%)");
161+
return text.ToString();
162+
}
163+
}
152164
}

0 commit comments

Comments
 (0)