Add active event check to task serializer

This commit is contained in:
Tyfon
2024-07-08 18:03:58 -07:00
parent 9c30f8d3a5
commit 3b933f6536
3 changed files with 17 additions and 5 deletions

View File

@@ -37,10 +37,11 @@ namespace UIFixes
foreach (ItemContextClass itemContext in MultiSelect.SortedItemContexts()) foreach (ItemContextClass itemContext in MultiSelect.SortedItemContexts())
{ {
LocationInGrid location = itemContext.ItemAddress is ItemAddressClass gridAddress ? MultiGrid.GetGridLocation(gridAddress) : null; LocationInGrid location = itemContext.ItemAddress is ItemAddressClass gridAddress ? MultiGrid.GetGridLocation(gridAddress) : null;
builder.AppendFormat("x{0} {1} {2}\n", builder.AppendFormat("x{0} {1} {2} {3}\n",
itemContext.Item.StackObjectsCount, itemContext.Item.StackObjectsCount,
itemContext.ItemAddress.ContainerName,
location != null ? $"({location.x}, {location.y})" : "slot", location != null ? $"({location.x}, {location.y})" : "slot",
itemContext.Item.ToString()); itemContext.Item.Name.Localized());
} }
if (MultiSelect.SecondaryContexts.Any()) if (MultiSelect.SecondaryContexts.Any())

View File

@@ -681,7 +681,8 @@ namespace UIFixes
FindOrigin = GetTargetGridAddress(itemContext, ic, hoveredAddress); FindOrigin = GetTargetGridAddress(itemContext, ic, hoveredAddress);
FindVerticalFirst = ic.ItemRotation == ItemRotation.Vertical; FindVerticalFirst = ic.ItemRotation == ItemRotation.Vertical;
return __instance.AcceptItem(ic, targetItemContext); return __instance.AcceptItem(ic, targetItemContext);
}); },
itemContext => __instance.Grid.ParentItem.Owner.SelectEvents(itemContext.Item).All(args => args.Status == CommandStatus.Succeed));
// Setting the fallback after initializing means it only applies after the first item is already moved // Setting the fallback after initializing means it only applies after the first item is already moved
GridViewPickTargetPatch.FallbackResult = __instance.Grid.ParentItem; GridViewPickTargetPatch.FallbackResult = __instance.Grid.ParentItem;
@@ -906,7 +907,10 @@ namespace UIFixes
InPatch = true; InPatch = true;
var serializer = __instance.gameObject.AddComponent<MultiSelectItemContextTaskSerializer>(); var serializer = __instance.gameObject.AddComponent<MultiSelectItemContextTaskSerializer>();
__result = serializer.Initialize(MultiSelect.SortedItemContexts(), itemContext => __instance.AcceptItem(itemContext, targetItemContext)); __result = serializer.Initialize(
MultiSelect.SortedItemContexts(),
itemContext => __instance.AcceptItem(itemContext, targetItemContext),
itemContext => __instance.Slot.ParentItem.Owner.SelectEvents(itemContext.Item).All(args => args.Status == CommandStatus.Succeed));
__result.ContinueWith(_ => { InPatch = false; }); __result.ContinueWith(_ => { InPatch = false; });

View File

@@ -8,14 +8,16 @@ namespace UIFixes
public class TaskSerializer<T> : MonoBehaviour public class TaskSerializer<T> : MonoBehaviour
{ {
private Func<T, Task> func; private Func<T, Task> func;
private Func<T, bool> canContinue;
private IEnumerator<T> enumerator; private IEnumerator<T> enumerator;
private Task currentTask; private Task currentTask;
private TaskCompletionSource totalTask; private TaskCompletionSource totalTask;
public Task Initialize(IEnumerable<T> items, Func<T, Task> func) public Task Initialize(IEnumerable<T> items, Func<T, Task> func, Func<T, bool> canContinue = null)
{ {
this.enumerator = items.GetEnumerator(); this.enumerator = items.GetEnumerator();
this.func = func; this.func = func;
this.canContinue = canContinue;
currentTask = Task.CompletedTask; currentTask = Task.CompletedTask;
totalTask = new TaskCompletionSource(); totalTask = new TaskCompletionSource();
@@ -52,6 +54,11 @@ namespace UIFixes
return; return;
} }
if (canContinue != null && enumerator.Current != null && !canContinue(enumerator.Current))
{
return;
}
if (enumerator.MoveNext()) if (enumerator.MoveNext())
{ {
currentTask = func(enumerator.Current); currentTask = func(enumerator.Current);