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 <noreply@anthropic.com>
This commit is contained in:
parent
5e5c304e37
commit
61fc863e6b
2 changed files with 35 additions and 1 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<string>());
|
||||
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<string>();
|
||||
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<string>();
|
||||
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<string> failures)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue