Skip to content

Commit c8678b0

Browse files
committed
fix(inventory.getall<t>()): no longer crashes when used with a custom item entry type
Was crashing due to a failed LINQ cast that sometimes silently failed. fix #20
1 parent afb6b44 commit c8678b0

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

Assets/com.fluid.elastic-inventory/Runtime/Inventory/InventoryInstance.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,18 @@ public List<T> GetAll<T> (
182182
string category = null
183183
) where T : IItemEntryReadOnly {
184184
var allEntries = new List<T>();
185-
allEntries.AddRange(_uniqueEntries.Cast<T>());
186-
allEntries.AddRange(_entries.Values.Cast<T>());
187185

186+
// Find valid entries from both unique and non-unique lists
187+
foreach (var i in _uniqueEntries) {
188+
if (i is T valid) allEntries.Add(valid);
189+
}
190+
191+
foreach (var VARIABLE in _entries.Values) {
192+
if (VARIABLE is T valid) allEntries.Add(valid);
193+
194+
}
195+
196+
// Include additional filters
188197
if (definitionType != null) {
189198
allEntries = allEntries.Where(e => definitionType.IsInstanceOfType(e.Definition)).ToList();
190199
}

Assets/com.fluid.elastic-inventory/Tests/Editor/Scripts/InventoryInstanceTest.cs

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -424,42 +424,83 @@ public void It_should_trigger_a_remove_event_when_removing_a_unique_item () {
424424
}
425425
}
426426

427-
public class GetAll_Method : InventoryInstanceTest {
428-
[Test]
429-
public void It_should_return_all_added_item_entries () {
430-
var definition = A.ItemDefinition().Build();
431-
var inventory = Setup();
427+
public class GetAll_Method {
428+
public class Defaults : InventoryInstanceTest {
429+
[Test]
430+
public void It_should_return_all_added_item_entries () {
431+
var definition = A.ItemDefinition().Build();
432+
var inventory = Setup();
432433

433-
inventory.Add(definition);
434-
var entries = inventory.GetAll();
434+
inventory.Add(definition);
435+
var entries = inventory.GetAll();
435436

436-
Assert.AreEqual(1, entries.Count);
437-
}
437+
Assert.AreEqual(1, entries.Count);
438+
}
438439

439-
[Test]
440-
public void It_should_get_all_items_by_definition_type () {
441-
var definition = A.ItemDefinition().Build();
442-
var inventory = Setup();
440+
[Test]
441+
public void It_should_get_all_items_by_definition_type () {
442+
var definition = A.ItemDefinition().Build();
443+
var inventory = Setup();
443444

444-
inventory.Add(definition);
445-
var entries = inventory.GetAll(typeof(IItemDefinition));
445+
inventory.Add(definition);
446+
var entries = inventory.GetAll(typeof(IItemDefinition));
447+
448+
Assert.AreEqual(1, entries.Count);
449+
}
450+
451+
[Test]
452+
public void It_should_get_all_items_by_category () {
453+
var category = "Weapon";
454+
var definitionA = A.ItemDefinition().WithCategory(category).Build();
455+
var definitionB = A.ItemDefinition().Build();
456+
var inventory = Setup();
457+
458+
inventory.Add(definitionA);
459+
inventory.Add(definitionB);
460+
var entries = inventory.GetAll(category: category);
446461

447-
Assert.AreEqual(1, entries.Count);
462+
Assert.AreEqual(1, entries.Count);
463+
Assert.AreEqual(definitionA, entries[0].Definition);
464+
}
448465
}
449466

450-
[Test]
451-
public void It_should_get_all_items_by_category () {
452-
var category = "Weapon";
453-
var definitionA = A.ItemDefinition().WithCategory(category).Build();
454-
var definitionB = A.ItemDefinition().Build();
455-
var inventory = Setup();
467+
public class WithCustomData : InventoryInstanceTest {
468+
public class TEST_ItemEntryCustom : ItemEntryBase {
469+
}
470+
471+
public class TEST_ItemEntryDataResolverCustom : ItemEntryDataResolverBase<TEST_ItemEntryCustom> {
472+
}
473+
474+
public class TEST_ItemDefinitionCustom : ItemDefinitionBase {
475+
public override string DisplayName { get; }
476+
public override string Category { get; }
477+
478+
public override IItemEntryDataResolver DataResolver => new TEST_ItemEntryDataResolverCustom();
456479

457-
inventory.Add(definitionA);
458-
inventory.Add(definitionB);
459-
var entries = inventory.GetAll(category: category);
480+
public override IItemEntry CreateItemEntry (IItemDatabase database, int quantity = 1, string id = null, int? createdAt = null, int? updatedAt = null) {
481+
// We have to initialize a new implementation of the entry here
482+
// This is because the database doesn't know about our custom entry type
483+
var entry = new TEST_ItemEntryCustom();
484+
entry.Setup(database, this, quantity, id, createdAt, updatedAt);
460485

461-
Assert.AreEqual(1, entries.Count);
462-
Assert.AreEqual(definitionA, entries[0].Definition);
486+
return entry;
487+
}
488+
}
489+
490+
[Test]
491+
public void It_should_not_fail_on_a_custom_item_entry_type () {
492+
var definitionGeneric = A.ItemDefinition().Build();
493+
var definition = ScriptableObject.CreateInstance<TEST_ItemDefinitionCustom>();
494+
var inventory = Setup();
495+
496+
inventory.Add(definitionGeneric);
497+
inventory.Add(definition);
498+
499+
Assert.DoesNotThrow(() => {
500+
var entries = inventory.GetAll<TEST_ItemEntryCustom>();
501+
Assert.AreEqual(1, entries.Count);
502+
});
503+
}
463504
}
464505
}
465506

0 commit comments

Comments
 (0)