From e7b7e2270d8bfcbcbd973d3c9d0271f5b3a96ffc Mon Sep 17 00:00:00 2001 From: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Date: Thu, 10 Oct 2024 17:35:35 +0300 Subject: [PATCH] Fix bounties(and potentially other things) running out of ids (#32700) * Make NameIdentifier Ids get refreshed after round restarts Before this commit the existing values would just get shuffled. This means that eventually the server would run out of ids to give to new entities for different groups. As a result everything would get id 0 * Comply with what seemingly is the convention for sawmills * Make it impossible to insert a bounty with a duplicate id * Reduce duplication * Remove unused sawmill * Fix rustbrain and skill issue * Aaaa * Apply suggestions from code review --------- Co-authored-by: Pieter-Jan Briers --- .../Cargo/Systems/CargoSystem.Bounty.cs | 7 +++ .../NameIdentifier/NameIdentifierSystem.cs | 44 +++++++++++++------ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Content.Server/Cargo/Systems/CargoSystem.Bounty.cs b/Content.Server/Cargo/Systems/CargoSystem.Bounty.cs index 37d7015fd9..373e8e243b 100644 --- a/Content.Server/Cargo/Systems/CargoSystem.Bounty.cs +++ b/Content.Server/Cargo/Systems/CargoSystem.Bounty.cs @@ -420,6 +420,13 @@ public sealed partial class CargoSystem return false; _nameIdentifier.GenerateUniqueName(uid, BountyNameIdentifierGroup, out var randomVal); + var newBounty = new CargoBountyData(bounty, randomVal); + // This bounty id already exists! Probably because NameIdentifierSystem ran out of ids. + if (component.Bounties.Any(b => b.Id == newBounty.Id)) + { + Log.Error("Failed to add bounty {ID} because another one with the same ID already existed!", newBounty.Id); + return false; + } component.Bounties.Add(new CargoBountyData(bounty, randomVal)); _adminLogger.Add(LogType.Action, LogImpact.Low, $"Added bounty \"{bounty.ID}\" (id:{component.TotalBounties}) to station {ToPrettyString(uid)}"); component.TotalBounties++; diff --git a/Content.Server/NameIdentifier/NameIdentifierSystem.cs b/Content.Server/NameIdentifier/NameIdentifierSystem.cs index eefd4357cb..6db6e0ff56 100644 --- a/Content.Server/NameIdentifier/NameIdentifierSystem.cs +++ b/Content.Server/NameIdentifier/NameIdentifierSystem.cs @@ -14,6 +14,7 @@ public sealed class NameIdentifierSystem : EntitySystem [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IRobustRandom _robustRandom = default!; [Dependency] private readonly MetaDataSystem _metaData = default!; + [Dependency] private readonly ILogManager _logManager = default!; /// /// Free IDs available per . @@ -118,23 +119,39 @@ public sealed class NameIdentifierSystem : EntitySystem private void InitialSetupPrototypes() { - foreach (var proto in _prototypeManager.EnumeratePrototypes()) - { - AddGroup(proto); - } + EnsureIds(); } - private void AddGroup(NameIdentifierGroupPrototype proto) + private void FillGroup(NameIdentifierGroupPrototype proto, List values) { - var values = new List(proto.MaxValue - proto.MinValue); - + values.Clear(); for (var i = proto.MinValue; i < proto.MaxValue; i++) { values.Add(i); } _robustRandom.Shuffle(values); - CurrentIds.Add(proto.ID, values); + } + + private List GetOrCreateIdList(NameIdentifierGroupPrototype proto) + { + if (!CurrentIds.TryGetValue(proto.ID, out var ids)) + { + ids = new List(proto.MaxValue - proto.MinValue); + CurrentIds.Add(proto.ID, ids); + } + + return ids; + } + + private void EnsureIds() + { + foreach (var proto in _prototypeManager.EnumeratePrototypes()) + { + var ids = GetOrCreateIdList(proto); + + FillGroup(proto, ids); + } } private void OnReloadPrototypes(PrototypesReloadedEventArgs ev) @@ -159,19 +176,20 @@ public sealed class NameIdentifierSystem : EntitySystem foreach (var proto in set.Modified.Values) { + var name_proto = (NameIdentifierGroupPrototype) proto; + // Only bother adding new ones. if (CurrentIds.ContainsKey(proto.ID)) continue; - AddGroup((NameIdentifierGroupPrototype) proto); + var ids = GetOrCreateIdList(name_proto); + FillGroup(name_proto, ids); } } + private void CleanupIds(RoundRestartCleanupEvent ev) { - foreach (var values in CurrentIds.Values) - { - _robustRandom.Shuffle(values); - } + EnsureIds(); } }