Skip to content

Commit

Permalink
Merge pull request #11 from SzymonKaminski/entityid
Browse files Browse the repository at this point in the history
Add EntityId filter
  • Loading branch information
GoomiiV2 authored Jan 7, 2025
2 parents 5cd054b + d696b3a commit aae263c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
11 changes: 9 additions & 2 deletions PacketPeep/Systems/PacketDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ public void ApplyFilter(PacketFilter filter)
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 @@ public void ApplyFilter(PacketFilter filter)
}
}

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 class PacketFilter
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 @@ -121,6 +121,21 @@ public static MessageHeader GetGssMessageHeader(Message msg)

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 class PacketExplorer
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 @@ private void DrawFilters()
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 @@ private bool DrawFiltersSource(bool hasFiltersChanged)
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 @@ private unsafe void DrawPacketList()
{
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 unsafe void DrawPacketList()
}
}

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 @@ private void DrawPacketListItem(int i)
}
}

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 @@ -661,6 +740,22 @@ private void DrawPacketListItemContextMenu(int i)
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 SetFromPreviewStr()
}
}

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

0 comments on commit aae263c

Please sign in to comment.