acdream/src/AcDream.Core/Selection/SelectionState.cs
Erik 97cf873870
Some checks are pending
Headless portability / portable-headless (ubuntu-latest) (push) Waiting to run
Headless portability / portable-headless (windows-latest) (push) Waiting to run
Headless portability / linux-graphical (push) Waiting to run
Headless portability / linux-vulkan (push) Waiting to run
feat(vendor): Slice 6 buy arc — shop items are real objects, vendor selection is THE selection, and Buy works (0x005F)
Three ordered pieces in one landing (the shared controller/composition
files carry all three; the internal order was 6.1 -> 6.2 -> 6.3):

6.1 VendorShopItemMaterializer diff-merges the shop list into the live
ClientObjectTable on VendorState transitions (so client-local close and
session teardown retire the entries too) and never claims a guid it did
not add — ACE's UniqueItemsForSale can re-list a guid a player once
held (AP-163 files the collision-skip; no retail counterpart traced).
Right-click examine on shop items now routes through the ordinary
appraisal path — the 5.4 F7c blocker dissolves with the table entries.

6.2 SelectionChangeSource.Vendor: row clicks, auto-select, and examine
all flow through the canonical SelectionState; the status bar and the
existing byte-faithful StackSplitQuantityState slider light up
unmodified. VendorSplitPolicy is the single 0xDC41CB0 mask owner; the
slider VALUE seeds to 1 for exempt items while maxSplitSize keeps the
stack (the splitSize/maxSplitSize distinction, research §B.3).
Selection clears at retail's actual site — VendorItemsUI::RemoveFromShop
(pc:202848), not a CloseVendor-level clear that does not exist.

6.3 BuildBuy (0x005F): vendorGuid, count, (i32 amount, u32 guid) pairs,
and the trailing alternateCurrencyId the REAL client sends
(CM_Vendor::Event_Buy pc:689288) though ACE's reader ignores it.
TryBuy rides the EXISTING J5.2 one-request-at-a-time reservation and
completes on UseDone; the Buy button disables while a request is in
flight. The reconciliation round-trip (money property update, inventory
CreateObject, ApproachVendor refresh -> panel rebuild) is proven by a
synthetic-inbound test against existing machinery — no new owner.

Register: AP-161 narrowed (selection + examine residuals close;
staging/Sell remain; double-click-to-buy confirmed ABSENT from retail
with negative evidence cited — we match retail). AP-162 files the
conscious no-client-side-affordability-precheck deferral.

Clean-room complete solution: 11,368 passed / 4 skipped / 0 failed.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-08-07 20:28:26 +02:00

158 lines
5.1 KiB
C#

using AcDream.Plugin.Abstractions;
namespace AcDream.Core.Selection;
public enum SelectionChangeSource
{
System,
World,
Radar,
Inventory,
ExternalContainer,
Paperdoll,
Toolbar,
Keyboard,
Plugin,
// Slice 6.2: a vendor shop-list row click — research doc §B.4 confirms
// vendor-context selections flow through the SAME global
// ACCWeenieObject::SetSelectedObject primitive as every other origin
// (VendorSellUI::AddItemToSell, pc:203558), never a vendor-local one.
Vendor,
}
public enum SelectionChangeReason
{
Selected,
Cleared,
SelectedObjectRemoved,
CombatTargetDied,
PreviousSelection,
SessionReset,
}
public readonly record struct SelectionTransition(
uint? PreviousObjectId,
uint? SelectedObjectId,
SelectionChangeSource Source,
SelectionChangeReason Reason);
/// <summary>
/// Single owner for the client's selected object. This ports the state behavior
/// of retail <c>ACCWeenieObject::SetSelectedObject @ 0x0058C2E0</c>: changes are
/// deduplicated, the old selection becomes the previous selection, and plugin/UI
/// listeners receive one transition after state is committed.
/// </summary>
public sealed class SelectionState : ISelectionService
{
private event Action<SelectionChangedEvent>? PluginChanged;
public uint? SelectedObjectId { get; private set; }
public uint? PreviousObjectId { get; private set; }
public uint? PreviousValidObjectId { get; private set; }
public event Action<SelectionTransition>? Changed;
event Action<SelectionChangedEvent> ISelectionService.Changed
{
add => PluginChanged += value;
remove => PluginChanged -= value;
}
public bool Select(
uint objectId,
SelectionChangeSource source,
SelectionChangeReason reason = SelectionChangeReason.Selected)
=> objectId == 0
? Clear(source)
: Set(objectId, source, reason);
public bool Clear(
SelectionChangeSource source,
SelectionChangeReason reason = SelectionChangeReason.Cleared)
=> Set(null, source, reason);
public bool SelectPrevious(SelectionChangeSource source = SelectionChangeSource.Keyboard)
=> PreviousObjectId is uint previous
&& Set(previous, source, SelectionChangeReason.PreviousSelection);
/// <summary>
/// Ends a world session. Unlike an ordinary selection clear, no previous
/// object survives for the next session: server GUIDs may be reused by a
/// different logical incarnation after reconnect.
/// </summary>
public bool Reset(SelectionChangeSource source = SelectionChangeSource.System)
{
uint? old = SelectedObjectId;
bool changed = old is not null
|| PreviousObjectId is not null
|| PreviousValidObjectId is not null;
SelectedObjectId = null;
PreviousObjectId = null;
PreviousValidObjectId = null;
var transition = new SelectionTransition(
old,
null,
source,
SelectionChangeReason.SessionReset);
List<Exception>? failures = DispatchChanged(transition);
DispatchPluginChanged(new SelectionChangedEvent(old, null));
if (failures is not null)
throw new AggregateException(
"One or more selection reset observers failed.",
failures);
return changed;
}
bool ISelectionService.Select(uint objectId)
=> Select(objectId, SelectionChangeSource.Plugin);
bool ISelectionService.Clear()
=> Clear(SelectionChangeSource.Plugin);
private bool Set(
uint? selectedObjectId,
SelectionChangeSource source,
SelectionChangeReason reason)
{
if (SelectedObjectId == selectedObjectId)
return false;
uint? old = SelectedObjectId;
SelectedObjectId = selectedObjectId;
PreviousObjectId = old;
if (old is not null)
PreviousValidObjectId = old;
var transition = new SelectionTransition(old, selectedObjectId, source, reason);
Changed?.Invoke(transition);
DispatchPluginChanged(new SelectionChangedEvent(old, selectedObjectId));
return true;
}
private List<Exception>? DispatchChanged(SelectionTransition transition)
{
Action<SelectionTransition>? listeners = Changed;
if (listeners is null)
return null;
List<Exception>? failures = null;
foreach (Action<SelectionTransition> listener in listeners.GetInvocationList())
{
try { listener(transition); }
catch (Exception error) { (failures ??= []).Add(error); }
}
return failures;
}
private void DispatchPluginChanged(SelectionChangedEvent change)
{
Action<SelectionChangedEvent>? listeners = PluginChanged;
if (listeners is null) return;
foreach (Action<SelectionChangedEvent> listener in listeners.GetInvocationList())
{
try { listener(change); }
catch { /* plugin exceptions never escape into client state changes */ }
}
}
}