Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions Source/Client/Desyncs/LogGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,14 @@ private static string NormalizeLineEndings(string log)

private static string TrimExcessLines(string log)
{
var indexOfLastNewline = IndexOfOccurrence(log, '\n', MaxLogLineCount);
var indexOfLastNewline = log.IndexOfOccurrence('\n', MaxLogLineCount);
if (indexOfLastNewline >= 0)
{
log = $"{log.Substring(0, indexOfLastNewline + 1)}(log trimmed to {MaxLogLineCount:N0} lines.)";
}
return log;
}

private static int IndexOfOccurrence(string s, char match, int occurrence)
{
var currentOccurrence = 1;
var currentIndex = 0;
while (currentOccurrence <= occurrence && (currentIndex = s.IndexOf(match, currentIndex + 1)) != -1)
{
if (currentOccurrence == occurrence) return currentIndex;
currentOccurrence++;
}
return -1;
}

private static string RedactUselessLines(string log)
{
log = Regex.Replace(log, "Non platform assembly:.+\n", "");
Expand Down
13 changes: 12 additions & 1 deletion Source/Client/Util/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ public static float Range(this Random rand, float start, float end)
{
return (float)(start + rand.NextDouble() * (end - start));
}
}

public static int IndexOfOccurrence(this string s, char match, int occurrence)
{
var currentOccurrence = 1;
var currentIndex = 0;
while (currentOccurrence <= occurrence && (currentIndex = s.IndexOf(match, currentIndex + 1)) != -1)
{
if (currentOccurrence == occurrence) return currentIndex;
currentOccurrence++;
}
return -1;
}
}
}
55 changes: 38 additions & 17 deletions Source/Client/Windows/PacketLogWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Verse;

Expand All @@ -9,7 +10,34 @@ public class PacketLogWindow : Window
{
public override Vector2 InitialSize => new(UI.screenWidth / 2f, UI.screenHeight / 2f);

private List<LogNode> nodes = new();
private class UINode(string text)
{
public string text = text;
public List<UINode> children = [];
public bool expanded;

public static UINode FromLogNode(LogNode node)
{
return new UINode(node.text)
{
children = node.children.Select(FromLogNode).ToList()
};
}

public void AttachStackTrace()
{
var stackTrace = StackTraceUtility.ExtractStackTrace();
// Removes the first 3 frames - ExtractStackTrace, AttachStackTrace and AddCurrentNode
var index = stackTrace.IndexOfOccurrence('\n', 3) + 1;
stackTrace = stackTrace[index..];
children.Add(new UINode("Stack trace")
{
children = [new UINode(stackTrace)]
});
}
}

private List<UINode> nodes = new();
private int logHeight;
private Vector2 scrollPos;
private bool storeStackTrace;
Expand All @@ -34,7 +62,7 @@ public override void DoWindowContents(Rect rect)
Widgets.BeginScrollView(outRect, ref scrollPos, viewRect);

Rect nodeRect = new Rect(0f, 0f, viewRect.width, 20f);
foreach (LogNode node in nodes)
foreach (var node in nodes)
Draw(node, 0, ref nodeRect);

if (Event.current.type == EventType.Layout)
Expand All @@ -45,7 +73,7 @@ public override void DoWindowContents(Rect rect)
GUI.EndGroup();
}

public void Draw(LogNode node, int depth, ref Rect rect)
private void Draw(UINode node, int depth, ref Rect rect)
{
string text = node.text;
if (depth == 0 && text == SyncLogger.RootNodeName)
Expand All @@ -54,7 +82,7 @@ public void Draw(LogNode node, int depth, ref Rect rect)
rect.x = depth * 15;
if (node.children.Count > 0)
{
Widgets.Label(rect, node.expand ? "[-]" : "[+]");
Widgets.Label(rect, node.expanded ? "[-]" : "[+]");
rect.x += 15;
}

Expand All @@ -64,27 +92,20 @@ public void Draw(LogNode node, int depth, ref Rect rect)

Widgets.Label(rect, text);
if (Widgets.ButtonInvisible(rect))
node.expand = !node.expand;
node.expanded = !node.expanded;

rect.y += (int)rect.height;

if (node.expand)
foreach (LogNode child in node.children)
if (node.expanded)
foreach (var child in node.children)
Draw(child, depth + 1, ref rect);
}

public void AddCurrentNode(IHasLogger hasLogger)
{
if (storeStackTrace)
hasLogger.Log.current.children.Add(new LogNode("Stack trace")
{
children =
{
new LogNode(StackTraceUtility.ExtractStackTrace())
}
});

nodes.Add(hasLogger.Log.current);
var node = UINode.FromLogNode(hasLogger.Log.current);
if (storeStackTrace) node.AttachStackTrace();
nodes.Add(node);
}
}

Expand Down
13 changes: 3 additions & 10 deletions Source/Common/Syncing/Logger/LogNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

namespace Multiplayer.Client
{
public class LogNode
public class LogNode(string text, LogNode? parent = null)
{
public LogNode? parent;
public LogNode? parent = parent;
public string text = text;
public List<LogNode> children = new();
public string text;
public bool expand;

public LogNode(string text, LogNode? parent = null)
{
this.text = text;
this.parent = parent;
}
}

}
Loading