From 61fc863e6b1f5880cab061563366d1fb6c531974 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Mon, 14 Sep 2026 22:28:24 +0200 Subject: [PATCH] Stop loadouts from saving or trusting dangling weapon-group indices (Cut 10 fix) Entity.TryUnequip does not clear WeaponGroups, so Capture saved -1 for a grouped item that was no longer equipped, and Materialize later threw IndexOutOfRangeException after every other check passed. Capture now skips group items that are not equipped. Materialize reports every weapon-group index outside the loadout's slots as a failure before building anything, so a stale or hand-edited record builds nothing and charges nothing. Co-Authored-By: Claude Opus 5 --- Assets/Scripts/ServerShared/Loadout.cs | 4 ++- tests/Aetheria.Shared.Tests/LoadoutTests.cs | 32 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/ServerShared/Loadout.cs b/Assets/Scripts/ServerShared/Loadout.cs index 448f23688..e38033bfc 100644 --- a/Assets/Scripts/ServerShared/Loadout.cs +++ b/Assets/Scripts/ServerShared/Loadout.cs @@ -51,7 +51,7 @@ public static class Loadouts Design = cache.RefOf(itemManager.GetData(item.EquippableItem)) }).ToList(), WeaponGroups = entity.WeaponGroups - .Select(group => group.items.Select(item => items.IndexOf(item)).ToArray()) + .Select(group => group.items.Select(item => items.IndexOf(item)).Where(index => index >= 0).ToArray()) // skips unequipped items .ToArray() }; } @@ -92,6 +92,8 @@ public static class Loadouts var hullProduct = Resolve(loadout.Hull, "hull"); var slotProducts = loadout.Slots.Select(slot => Resolve(slot.Design, Cell(slot))).ToArray(); + foreach (var index in (loadout.WeaponGroups ?? new int[0][]).SelectMany(group => group)) + if (index < 0 || index >= loadout.Slots.Count) failures.Add($"weapon group: no slot {index}"); if (failures.Count > reported) return null; var hull = (EquippableItem) itemManager.CreateInstance(hullProduct); diff --git a/tests/Aetheria.Shared.Tests/LoadoutTests.cs b/tests/Aetheria.Shared.Tests/LoadoutTests.cs index 7ab696330..71fcc8d30 100644 --- a/tests/Aetheria.Shared.Tests/LoadoutTests.cs +++ b/tests/Aetheria.Shared.Tests/LoadoutTests.cs @@ -218,6 +218,38 @@ public sealed class LoadoutTests : IDisposable Assert.Contains("slot 0,0: no available product of Lamp", Assert.Single(failures)); } + // Entity.TryUnequip leaves a non-weapon item in its weapon group; Capture must not record it. + [Fact] + public void CaptureSkipsGroupedItemsNoLongerEquipped() + { + using var cache = Open(); + var items = new ItemManager(cache, RunSaveTests.TestSettings(), _ => { }); + var ship = Build(items, HandBuilt(cache), new List()); + Assert.NotNull(ship.TryUnequip(Assert.Single(ship.WeaponGroups[0].items))); + Assert.NotEmpty(ship.WeaponGroups[0].items); + + var captured = Loadouts.Capture(items, ship, "stripped"); + Assert.Single(captured.Slots); + Assert.All(captured.WeaponGroups, group => Assert.Empty(group)); + var failures = new List(); + Assert.NotNull(Build(items, captured, failures)); + Assert.Empty(failures); + } + + [Fact] + public void OutOfRangeWeaponGroupIndexBuildsNothing() + { + using var cache = Open(); + var items = new ItemManager(cache, RunSaveTests.TestSettings(), _ => { }); + var stale = HandBuilt(cache); + stale.WeaponGroups = new[] { new[] { 0, 2 }, new[] { -1 } }; + var failures = new List(); + var credits = 1000; + Assert.Null(Loadouts.Materialize(items, null, stale, _ => true, ref credits, failures)); + Assert.Equal(1000, credits); + Assert.Equal(new[] { "weapon group: no slot 2", "weapon group: no slot -1" }, failures); + } + // Everything available, credits discarded: for tests about placement and failure lists. private static Ship Build(ItemManager items, Loadout loadout, List failures) {