Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EntityId filter #11

Merged
merged 1 commit into from
Jan 7, 2025
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
11 changes: 9 additions & 2 deletions PacketPeep/Systems/PacketDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

PacketPeepTool.Log.AddLogInfo(LogCategories.PacketDB, $"Loaded sift data from: {dir}");
}
catch (Exception e) {

Check warning on line 63 in PacketPeep/Systems/PacketDB.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used
PacketPeepTool.Log.AddLogWarn(LogCategories.PacketDB, $"Couldn't load Sift data, check it exists in Data/Sift");
}
}
Expand Down Expand Up @@ -245,6 +245,11 @@
fromCheck |= msg.FromServer && filter.FromServer;
fromCheck |= !msg.FromServer && filter.FromClient;

bool entityIdsCheck = !filter.EntityIdsEnabled;
if (msg.Server == Server.Game && msg is GameMessage gameMessage)
{
entityIdsCheck |= filter.EntityIds.Contains(Utils.GetEntityId(gameMessage));
}

// Channel filtering
bool channelCheck = false;
Expand Down Expand Up @@ -292,7 +297,7 @@
}
}

if (fromCheck && channelCheck && msgIdCheck) FilteredIndices.Add(msg.Id);
if (fromCheck && channelCheck && msgIdCheck && entityIdsCheck) FilteredIndices.Add(msg.Id);
} //);

sw.Stop();
Expand Down Expand Up @@ -458,7 +463,9 @@
public bool ChanRgss;

public List<MsgFilterData> MsgFilters = new();
public List<UInt64> EntityIds = new();

public List<ulong> EntityIds = new();
public bool EntityIdsEnabled = false;

public string SessionName = "";

Expand Down
15 changes: 15 additions & 0 deletions PacketPeep/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{
if (msg.Server == Server.Game) {
var gameMsg = msg as GameMessage;
var color = gameMsg.Channel switch

Check warning on line 59 in PacketPeep/Utils.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(FauCap.Channel)4' is not covered.
{
Channel.Control => Config.Inst.PColors[Config.Colors.Control],
Channel.Matrix => Config.Inst.PColors[Config.Colors.Matrix],
Expand Down Expand Up @@ -121,6 +121,21 @@

return header;
}

public static ulong GetEntityId(Message msg)
{
if (msg is SubMessage subMessage)
{
return subMessage.EntityId;
}

if (msg is GameMessage { Channel: Channel.ReliableGss or Channel.UnreliableGss })
{
return BitConverter.ToUInt64(msg.Data[..8]) >> 8;
}

return 0;
}
}

public struct MessageHeader
Expand Down
14 changes: 14 additions & 0 deletions PacketPeep/Widgets/MessageInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ private void DrawMenuBar()
PacketPeepTool.PcktDb.ApplyFilter(PacketPeepTool.Main.PacketExp.ActiveFilter);
}

if (ImGui.MenuItem("Add/remove filter for this EntityId")) {
var entId = Utils.GetEntityId(Msg);
var filter = PacketPeepTool.Main.PacketExp.ActiveFilter.EntityIds;
if (!filter.Contains(entId))
{
filter.Add(entId);
}
else
{
filter.Remove(entId);
}
PacketPeepTool.PcktDb.ApplyFilter(PacketPeepTool.Main.PacketExp.ActiveFilter);
}

ImGui.EndMenu();
}

Expand Down
104 changes: 102 additions & 2 deletions PacketPeep/Widgets/PacketExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public PacketFilter ActiveFilter = new();
private string channelsPreviewStr = "";
private string fromPreviewStr = "";
private string entityIdsPreviewStr = "";
private Dictionary<string, ControllerData> controllerList = new();
private MsgFilterData pendingMsgFilterData = MsgFilterData.Create();
private string pendingControllerName = "";
Expand Down Expand Up @@ -112,9 +113,23 @@
hasFiltersChanged = DrawFiltersSource(hasFiltersChanged);

// Message / Controllers
ImGui.SetNextItemWidth(-32);
ImGui.SetNextItemWidth(ImGui.GetWindowWidth()/2 - 40);
hasFiltersChanged = DrawFiltersMessages(hasFiltersChanged);

ImGui.SameLine();
ImGui.SetNextItemWidth(ImGui.GetWindowWidth()/2 - 40);
hasFiltersChanged = DrawFiltersEntityIds(hasFiltersChanged);

ImGui.SameLine();
FontManager.PushFont("FAS");
if (ImGui.Button(ActiveFilter.EntityIdsEnabled ? "\uf205" : "\uf204"))
{
ActiveFilter.EntityIdsEnabled = !ActiveFilter.EntityIdsEnabled;
hasFiltersChanged = true;
SetEntityIdsPreviewStr();
}
FontManager.PopFont();

ImGui.SameLine();
DrawMessageFinder();

Expand Down Expand Up @@ -444,6 +459,60 @@
return hasFiltersChanged;
}

private string inputEntityId = "";

private bool DrawFiltersEntityIds(bool hasFiltersChanged)
{
if (ImGui.BeginCombo("###Entity Ids", entityIdsPreviewStr)) {
ImGui.InputText("###Entity Id", ref inputEntityId, 20, 0);

ImGui.SameLine();
if (ImGui.Button("Add") || ImGui.IsKeyPressedMap(ImGuiKey.Enter))
{
if (ulong.TryParse(inputEntityId, out var entityId) && !ActiveFilter.EntityIds.Contains(entityId))
{
ActiveFilter.EntityIds.Add(entityId);
hasFiltersChanged = true;
inputEntityId = "";
}
}

ImGui.NewLine();
if (ImGui.Button("Remove all"))
{
ActiveFilter.EntityIds.Clear();
hasFiltersChanged = true;
}

if (ImGui.BeginTable("Entity Ids", 2, ImGuiTableFlags.SizingStretchProp | ImGuiTableFlags.Borders)) {
var entityIdsFiltersToRemove = new List<int>();
int idx = 0;
foreach (var eid in ActiveFilter.EntityIds) {
ImGui.TableNextColumn();
ImGui.Text($"{eid}");
ImGui.TableNextColumn();
if (ImGui.Button($"X###{idx}")) {
entityIdsFiltersToRemove.Add(idx);
hasFiltersChanged = true;
}

idx++;
}

foreach (var index in entityIdsFiltersToRemove) {
ActiveFilter.EntityIds.RemoveAt(index);
}

ImGui.EndTable();
}

ImGui.EndCombo();
}

if (hasFiltersChanged || entityIdsPreviewStr == "") SetEntityIdsPreviewStr();
return hasFiltersChanged;
}

private bool DrawFilterChannels(bool hasFiltersChanged)
{
ImGui.SetNextItemWidth(300);
Expand All @@ -465,6 +534,7 @@
{
if (ImGui.BeginChild("###PacketListArea", new Vector2(-1, -40))) {
var numColumns = Config.Inst.PacketList.GetNumColumsNeeded() + 3; // 3 fixed ones
if (ActiveFilter.EntityIds.Count > 0) numColumns++;
if (ImGui.BeginTable("Packet List", numColumns, ImGuiTableFlags.Borders | ImGuiTableFlags.RowBg | ImGuiTableFlags.ScrollY | ImGuiTableFlags.ContextMenuInBody)) {
DrawPacketListHeader(numColumns);

Expand Down Expand Up @@ -496,14 +566,15 @@
}
}

private static void DrawPacketListHeader(int numColumns)
private void DrawPacketListHeader(int numColumns)
{
ImGui.TableSetupScrollFreeze(numColumns, 1);
if (Config.Inst.PacketList.ShowPacketIdx) ImGui.TableSetupColumn("Pkt Idx", ImGuiTableColumnFlags.WidthFixed, 45);
ImGui.TableSetupColumn("Src", ImGuiTableColumnFlags.WidthFixed, 20);
ImGui.TableSetupColumn("Ch", ImGuiTableColumnFlags.WidthFixed, 20);
if (Config.Inst.PacketList.ShowPacketSeqNum) ImGui.TableSetupColumn("Seq", ImGuiTableColumnFlags.WidthFixed, 45);
if (Config.Inst.PacketList.ShowPacketIds) ImGui.TableSetupColumn("Id", ImGuiTableColumnFlags.WidthFixed, 45);
if (ActiveFilter.EntityIds.Count > 0) ImGui.TableSetupColumn("Ent Idx", ImGuiTableColumnFlags.WidthFixed, 45);
ImGui.TableSetupColumn("Name");
ImGui.TableHeadersRow();
}
Expand Down Expand Up @@ -571,6 +642,14 @@
}
}

if (ActiveFilter.EntityIds.Count > 0)
{
ImGui.TableNextColumn();
var headerData = Utils.GetGssMessageHeader(msg);
var entityIdIdx = ActiveFilter.EntityIds.FindIndex(e => e == headerData.EntityId);
ImGui.Text(entityIdIdx == -1 ? "" : $"{entityIdIdx}");
}

// Name
ImGui.TableNextColumn();
DrawPacketListName(msg, gameMsg);
Expand Down Expand Up @@ -644,7 +723,7 @@
ImGui.Spacing();
ImGui.SameLine();

var chanColor = gameMsg.Channel switch

Check warning on line 726 in PacketPeep/Widgets/PacketExplorer.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(FauCap.Channel)4' is not covered.
{
Channel.Control => Config.Inst.PColors[Config.Colors.Control],
Channel.Matrix => Config.Inst.PColors[Config.Colors.Matrix],
Expand All @@ -661,6 +740,22 @@
shouldApplyFilter = true;
}

if (ImGui.MenuItem("Add/remove filter for this EntityId")) {
var entId = Utils.GetEntityId(gameMsg);
var filter = ActiveFilter.EntityIds;
if (!filter.Contains(entId))
{
filter.Add(entId);
}
else
{
filter.Remove(entId);
}

shouldApplyFilter = true;
SetEntityIdsPreviewStr();
}

OnMessageContextMenuDraw?.Invoke(i);
}

Expand Down Expand Up @@ -766,6 +861,11 @@
}
}

private void SetEntityIdsPreviewStr()
{
entityIdsPreviewStr = $"EntityIds: {ActiveFilter.EntityIds.Count} selected";
}

private void ToggleMessageSelect(int idx)
{
if (!SelectedIdxs.ContainsKey(ActiveFilter.SessionName)) SelectedIdxs.Add(ActiveFilter.SessionName, new List<int>(20));
Expand Down
Loading