Repository navigation
Expose item-slot lock state and lifecycle events #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| using S1API.Items; | ||
| #if MONOMELON | ||
| using System.Reflection; | ||
| using NativeItemSlot = ScheduleOne.ItemFramework.ItemSlot; | ||
| using NativeItemSlotLock = ScheduleOne.ItemFramework.ItemSlotLock; | ||
| #endif | ||
|
|
||
| namespace S1API.Tests.Items; | ||
|
|
||
| public sealed class ItemSlotInstanceTests | ||
| { | ||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" Station task ")] | ||
| public void LockSnapshotPreservesReasonWithoutNormalization(string? reason) | ||
| { | ||
| Assert.Equal(reason, new ItemSlotLockInfo(reason).LockReason); | ||
| } | ||
|
|
||
| #if MONOMELON | ||
| [Fact] | ||
| public void EmptySlotRetainsLegacyDefaultsAndZeroQuantityMutation() | ||
| { | ||
| var slot = new ItemSlotInstance(new NativeItemSlot()); | ||
|
|
||
| Assert.Equal(0, slot.Quantity); | ||
| Assert.Null(slot.ItemInstance); | ||
| slot.AddQuantity(amount: 0); | ||
| Assert.Equal(0, slot.Quantity); | ||
| Assert.Null(slot.ItemInstance); | ||
| Assert.False(slot.IsLocked); | ||
| Assert.False(slot.IsAddLocked); | ||
| Assert.False(slot.IsRemovalLocked); | ||
| Assert.Null(slot.ActiveLock); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NullNativeSlotStillFailsOnAccessRatherThanConstruction() | ||
| { | ||
| var slot = new ItemSlotInstance(null!); | ||
|
|
||
| Assert.Throws<NullReferenceException>(() => slot.Quantity); | ||
| Assert.Throws<NullReferenceException>(() => slot.ItemInstance); | ||
| Assert.Throws<NullReferenceException>(() => slot.AddQuantity(amount: -1)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData("Station task")] | ||
| public void ActiveLockIsASnapshotAndIndependentFlagsRemainIndependent(string? reason) | ||
| { | ||
| var nativeSlot = new SlotFixture(); | ||
| var slot = new ItemSlotInstance(nativeSlot); | ||
| int lockEvents = 0; | ||
| slot.Locked += () => lockEvents++; | ||
| slot.Unlocked += () => lockEvents++; | ||
|
|
||
| nativeSlot.SetIsAddLocked(true); | ||
| nativeSlot.SetIsRemovalLocked(true); | ||
| Assert.True(slot.IsAddLocked); | ||
| Assert.True(slot.IsRemovalLocked); | ||
| Assert.False(slot.IsLocked); | ||
| Assert.Null(slot.ActiveLock); | ||
| Assert.Equal(0, lockEvents); | ||
|
|
||
| nativeSlot.SetLock(reason); | ||
| Assert.True(slot.IsLocked); | ||
| ItemSlotLockInfo snapshot = slot.ActiveLock!.Value; | ||
| Assert.Equal(reason, snapshot.LockReason); | ||
|
|
||
| nativeSlot.SetLock("Replacement"); | ||
| Assert.Equal("Replacement", slot.ActiveLock!.Value.LockReason); | ||
| Assert.Equal(reason, snapshot.LockReason); | ||
|
|
||
| nativeSlot.ClearLock(); | ||
| Assert.False(slot.IsLocked); | ||
| Assert.Null(slot.ActiveLock); | ||
| Assert.True(slot.IsAddLocked); | ||
| Assert.True(slot.IsRemovalLocked); | ||
| Assert.Equal(reason, snapshot.LockReason); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(nameof(ItemSlotInstance.Locked), "onLocked")] | ||
| [InlineData(nameof(ItemSlotInstance.Unlocked), "onUnlocked")] | ||
| [InlineData(nameof(ItemSlotInstance.ItemInstanceChanged), "onItemInstanceChanged")] | ||
| [InlineData(nameof(ItemSlotInstance.ItemDataChanged), "onItemDataChanged")] | ||
| [InlineData(nameof(ItemSlotInstance.FilterChanged), "onFilterChange")] | ||
| public void EventsPreserveNativeListenersAndRemoveLastDuplicate(string eventName, string callbackName) | ||
| { | ||
| var nativeSlot = new NativeItemSlot(); | ||
| var slot = new ItemSlotInstance(nativeSlot); | ||
| EventInfo managedEvent = typeof(ItemSlotInstance).GetEvent(eventName)!; | ||
| FieldInfo nativeCallback = typeof(NativeItemSlot).GetField(callbackName)!; | ||
| var calls = new List<string>(); | ||
| Action handler = () => calls.Add("shared"); | ||
| Action otherHandler = () => calls.Add("other"); | ||
| nativeCallback.SetValue(nativeSlot, handler); | ||
|
|
||
| managedEvent.AddEventHandler(slot, null); | ||
| managedEvent.RemoveEventHandler(slot, null); | ||
| managedEvent.RemoveEventHandler(slot, otherHandler); | ||
| Assert.Same(handler, nativeCallback.GetValue(nativeSlot)); | ||
|
|
||
| managedEvent.AddEventHandler(slot, handler); | ||
| managedEvent.AddEventHandler(slot, otherHandler); | ||
| managedEvent.AddEventHandler(slot, handler); | ||
| InvokeAndAssert("shared", "shared", "other", "shared"); | ||
|
|
||
| managedEvent.RemoveEventHandler(slot, handler); | ||
| InvokeAndAssert("shared", "shared", "other"); | ||
| managedEvent.RemoveEventHandler(slot, handler); | ||
| InvokeAndAssert("shared", "other"); | ||
| managedEvent.RemoveEventHandler(slot, handler); | ||
| InvokeAndAssert("shared", "other"); | ||
| managedEvent.RemoveEventHandler(slot, otherHandler); | ||
| Assert.Same(handler, nativeCallback.GetValue(nativeSlot)); | ||
|
|
||
| // A fresh wrapper can subscribe after cleanup without retaining old registrations. | ||
| var reusedSlot = new ItemSlotInstance(nativeSlot); | ||
| managedEvent.AddEventHandler(reusedSlot, otherHandler); | ||
| InvokeAndAssert("shared", "other"); | ||
| managedEvent.RemoveEventHandler(reusedSlot, otherHandler); | ||
| Assert.Same(handler, nativeCallback.GetValue(nativeSlot)); | ||
|
|
||
| void InvokeAndAssert(params string[] expected) | ||
| { | ||
| calls.Clear(); | ||
| ((Action?)nativeCallback.GetValue(nativeSlot))?.Invoke(); | ||
| Assert.Equal(expected, calls); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RemovingHandlersFromOneWrapperLeavesOtherWrappersAndEventsIntact() | ||
| { | ||
| var nativeSlot = new NativeItemSlot(); | ||
| var first = new ItemSlotInstance(nativeSlot); | ||
| var second = new ItemSlotInstance(nativeSlot); | ||
| var replacementNativeSlot = new NativeItemSlot(); | ||
| var replacement = new ItemSlotInstance(replacementNativeSlot); | ||
| int calls = 0; | ||
| Action handler = () => calls++; | ||
|
|
||
| first.Locked += handler; | ||
| second.Locked += handler; | ||
| first.Unlocked += handler; | ||
| second.Locked -= handler; | ||
| replacement.Locked -= handler; | ||
| Assert.Null(replacementNativeSlot.onLocked); | ||
|
|
||
| nativeSlot.onLocked(); | ||
| nativeSlot.onUnlocked(); | ||
| Assert.Equal(2, calls); | ||
| first.Locked -= handler; | ||
| first.Unlocked -= handler; | ||
| Assert.Null(nativeSlot.onLocked); | ||
| Assert.Null(nativeSlot.onUnlocked); | ||
| } | ||
|
|
||
| private sealed class SlotFixture : NativeItemSlot | ||
| { | ||
| internal void SetLock(string? reason) => | ||
| ActiveLock = new NativeItemSlotLock(this, null!, reason!); | ||
|
|
||
| internal void ClearLock() => | ||
| ActiveLock = null!; | ||
| } | ||
| #endif | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| namespace S1API.Items | ||
| { | ||
| /// <summary> | ||
| /// Contains an immutable managed snapshot of an item-slot lock reason. | ||
| /// </summary> | ||
| public readonly struct ItemSlotLockInfo | ||
| { | ||
| internal ItemSlotLockInfo(string? lockReason) | ||
| { | ||
| LockReason = lockReason; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the native lock reason, preserving empty strings and <c>null</c>. | ||
| /// </summary> | ||
| public string? LockReason { get; } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.