Skip to content

Commit

Permalink
Cache item comparer
Browse files Browse the repository at this point in the history
  • Loading branch information
whatston3 committed Feb 10, 2025
1 parent bc89ac1 commit e4a1322
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Content.Server/Silicons/Borgs/BorgSystem.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public bool CanInsertModule(EntityUid uid, EntityUid module, BorgChassisComponen

if (TryComp<ItemBorgModuleComponent>(module, out var itemModuleComp))
{
var droppableComparer = new DroppableBorgItemComparer(); // Frontier: cached comparer
foreach (var containedModuleUid in component.ModuleContainer.ContainedEntities)
{
if (!TryComp<ItemBorgModuleComponent>(containedModuleUid, out var containedItemModuleComp))
Expand All @@ -371,7 +372,7 @@ public bool CanInsertModule(EntityUid uid, EntityUid module, BorgChassisComponen
if (containedItemModuleComp.Items.Count == itemModuleComp.Items.Count &&
containedItemModuleComp.DroppableItems.Count == itemModuleComp.DroppableItems.Count && // Frontier
containedItemModuleComp.Items.All(itemModuleComp.Items.Contains) &&
containedItemModuleComp.DroppableItems.All(x => itemModuleComp.DroppableItems.Contains(x, new DroppableBorgItemComparer()))) // Frontier
containedItemModuleComp.DroppableItems.All(x => itemModuleComp.DroppableItems.Contains(x, droppableComparer))) // Frontier
{
if (user != null)
Popup.PopupEntity(Loc.GetString("borg-module-duplicate"), uid, user.Value);
Expand All @@ -388,19 +389,20 @@ private sealed class DroppableBorgItemComparer : IEqualityComparer<DroppableBorg
{
public bool Equals(DroppableBorgItem? x, DroppableBorgItem? y)
{
// Same object
// Same object (or both null)
if (ReferenceEquals(x, y))
return true;
// Comparisons vs. null
if (x is null || y is null)
// One-side null
if (x == null || y == null)
return false;
// Otherwise, use EntProtoId of item to keep
// Otherwise, use EntProtoId of item
return x.ID == y.ID;
}

public int GetHashCode(DroppableBorgItem obj)
{
if (obj is null) return 0;
if (obj is null)
return 0;
return obj.ID.GetHashCode();
}
}
Expand Down

0 comments on commit e4a1322

Please sign in to comment.