Skip to content

Commit

Permalink
Fix items rarely being added twice to visible selections list
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Aug 29, 2024
1 parent 3ff3089 commit 01b29ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
- ModernLighting
- ConsoleDriver
workflow_dispatch:

concurrency:
Expand Down
26 changes: 15 additions & 11 deletions MCGalaxy/Network/Player.Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,9 @@ class VisibleSelection { public object data; public byte ID; }
VolatileArray<VisibleSelection> selections = new VolatileArray<VisibleSelection>();

public bool AddVisibleSelection(string label, Vec3U16 min, Vec3U16 max, ColorDesc color, object instance) {
VisibleSelection sel = new VisibleSelection();
sel.data = instance;

lock (selections.locker) {
sel.ID = GetSelectionID(selections.Items, instance);
selections.Add(sel);
return Session.SendAddSelection(sel.ID, label, min, max, color);
byte id = FindOrAddSelection(selections.Items, instance);
return Session.SendAddSelection(id, label, min, max, color);
}
}

Expand All @@ -258,23 +254,31 @@ public bool RemoveVisibleSelection(object instance) {
return false;
}

static unsafe byte GetSelectionID(VisibleSelection[] items, object instance) {
unsafe byte FindOrAddSelection(VisibleSelection[] items, object instance) {
byte* used = stackalloc byte[256];
for (int i = 0; i < 256; i++) used[i] = 0;
byte id;

for (int i = 0; i < items.Length; i++)
{
byte id = items[i].ID;
id = items[i].ID;
if (instance == items[i].data) return id;

used[id] = 1;
}

for (byte i = 0; i < 255; i++)
// find unused ID, or 255 if none unused
for (id = 0; id < 255; id++)
{
if (used[i] == 0) return i;
if (used[id] == 0) break;
}
return 255;

VisibleSelection sel = new VisibleSelection();
sel.data = instance;
sel.ID = id;

selections.Add(sel);
return id;
}
}
}

0 comments on commit 01b29ea

Please sign in to comment.