Skip to content

Commit afb6b44

Browse files
committed
fix(inventory add): no longer crashes when read only items are added
Using ItemEntryData to pass in items to InventoryInstance.AddEntry would crash due to an edge case where it thought the read only item could be runtime manipulated. fix #17
1 parent 489ec64 commit afb6b44

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public IItemEntryReadOnly Add(IItemDefinition item, int quantity = 1) {
3535
IItemEntryReadOnly entry;
3636
if (item.Unique) {
3737
entry = item.CreateItemEntry(_database);
38-
AddEntry((IItemEntry)entry);
38+
AddEntryInternal((IItemEntry)entry);
3939
} else if (_entries.TryGetValue(item, out var existingEntry)) {
4040
existingEntry.SetQuantity(existingEntry.Quantity + quantity);
4141
entry = existingEntry;
4242
} else {
4343
entry = item.CreateItemEntry(_database, quantity);
44-
AddEntry((IItemEntry)entry);
44+
AddEntryInternal((IItemEntry)entry);
4545
}
4646

4747
Events.ItemAdded.Invoke(entry);
@@ -58,17 +58,17 @@ public IItemEntryReadOnly Add (string definitionId, int quantity = 1) {
5858
public IItemEntryReadOnly AddEntry(IItemEntryReadOnly entry) {
5959
if (entry == null) return null;
6060

61-
// Unique items and items without existing quantities can be added directly
62-
if (entry.Definition.Unique || !Has(entry.Definition)) {
63-
AddEntry(entry as IItemEntry);
64-
entry.UpdateTimeLogs();
65-
return entry;
61+
// Unique items and items without existing quantities can be added directly (unless they are read only)
62+
if (entry is IItemEntry existingEntry && (existingEntry.Definition.Unique || !Has(existingEntry.Definition))) {
63+
AddEntryInternal(existingEntry);
64+
existingEntry.UpdateTimeLogs();
65+
return existingEntry;
6666
}
6767

6868
return Add(entry.Definition, entry.Quantity);
6969
}
7070

71-
private void AddEntry (IItemEntry entry) {
71+
private void AddEntryInternal (IItemEntry entry) {
7272
if (entry.Definition.Unique) {
7373
_uniqueEntries.Add(entry);
7474
} else {
@@ -160,7 +160,7 @@ public void Load (string save) {
160160
var tmpEntry = resolver.Load(json, _database);
161161
var entry = tmpEntry.Definition.DataResolver.Load(json, _database);
162162

163-
AddEntry(entry);
163+
AddEntryInternal(entry);
164164
});
165165
}
166166

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ public void It_should_add_an_item_entry_object () {
152152
}
153153
}
154154

155+
public class ReadOnly_Items : InventoryInstanceTest {
156+
[Test]
157+
public void It_should_not_error_when_a_read_only_item_is_passed () {
158+
var inventory = Setup();
159+
var item = A.ItemEntryReadOnly().Build();
160+
161+
Assert.DoesNotThrow(() => {
162+
inventory.AddEntry(item);
163+
});
164+
}
165+
}
166+
155167
public class Unique_Items : InventoryInstanceTest {
156168
[Test]
157169
public void It_should_add_two_unique_items_as_individual_entries () {

0 commit comments

Comments
 (0)