2.2 五阶段生成管线与调试强制置顶算法
本节结合从《深空当铺:可能是偷的》游戏本体 Assembly-CSharp.dll 反编译出的 PlayerStore.OnShutterOpened 与 StoreClientManager.GenerateClient IL 字节码,完整拆解顾客队列 clientStack 的生成顺序、7 步筛选逻辑以及调试召唤的双阶段置顶实现。
1. 原版开店与顾客生成底层 IL 执行流
当玩家在清晨点击店铺左上角的卷帘门开关(StoreShutterButton.OnClick → StoreShutterButton.OpenShutter)时,原版代码的精确调用链如下:
text
StoreShutterButton.OpenShutter()
├── 1. PlayerStore.Instance.OnShutterOpened()
│ ├── CheckCrime() / SoldItem.HandleSoldItem()
│ ├── this.storeClientManager.GenerateClient()
│ │ ├── ModHook.FireOnGenerateCustomerVeryEarly(this)
│ │ ├── [原版固定天数剧情顾客生成]
│ │ ├── ModHook.FireOnGenerateCustomerEarly(this)
│ │ ├── [原版常规顾客池抽样]
│ │ ├── ModHook.FireOnGenerateCustomerNormal(this)
│ │ ├── [原版特殊职业与展示柜顾客生成]
│ │ ├── ModHook.FireOnGenerateCustomerLate(this)
│ │ ├── this.ClientGenCheck()
│ │ ├── this.HandleJohnSmithVandalVisit() // <-- 注意:内部调用 AddNextClient 抢占 index 0!
│ │ ├── LouBombHelper.QueueVisit(this) // <-- 注意:内部调用 AddNextClient 抢占 index 0!
│ │ └── ModHook.FireOnGenerateCustomerVeryLate(this)
│ ├── this.HandleFutureClientQueue() // 将跨日预约队列追加到 clientStack
│ ├── this.storeClientManager.clientStack.RemoveAll(c => c == null)
│ └── this.LogQueuedClient() // 在控制台打印当日排队的顾客名单
│
├── 2. PlayerStore.Instance.OpenShutter()
│ └── this.storeState = PlayerStore.StoreState.OPEN (值为 1)
│
├── 3. StoreShutterButton.OnShutterDoorOpenMorning()
│ ├── ModHook.FireOnShutterOpenedEarly()
│ ├── [发放早报 newspaper、教程手册、枪证、分红等]
│ └── ModHook.FireOnShutterOpenedLate() // <-- 框架在此处执行二次置顶校验与自动唤入!
│
└── 4. PlayerStore.Instance.OnLateShutterOpen()
└── [检查是否有清晨堵门事件 SetupWaitingClient]2. HandleGenerationPhase 完整源码与 7 步筛选剖析
在 NpcFrameworkRegistry.cs 中,每个 NpcSpawnPhase 触发时都会运行 HandleGenerationPhase:
csharp
internal static void HandleGenerationPhase(StoreClientManager manager, NpcSpawnPhase phase)
{
if (manager == null) return;
DrainPendingQueue();
NpcPhoneRegistry.SyncPhoneContactsToPlayerStore();
int day = 1;
try { day = StoreStation.Instance != null ? StoreStation.Instance.dayCounter : 1; } catch { }
if (CustomNPCFrameworkMod.FrameworkEnabled)
{
float chanceScale = CustomNPCFrameworkMod.SpawnChanceMultiplier;
for (int i = 0; i < Entries.Count; i++)
{
var entry = Entries[i];
// Step 1: 阶段匹配
if (entry.SpawnPhase != phase) continue;
// Step 2: 天数窗口 [MinDay, MaxDay]
if (day < entry.MinDay || day > entry.MaxDay) continue;
// Step 3: 单档唯一性检查 (OncePerSave)
if (entry.OncePerSave && NpcSaveStore.GetInt($"visits_{entry.Id}", 0) > 0) continue;
// Step 4: 冷却天数检查 (CooldownDays)
if (entry.CooldownDays > 0)
{
int lastDay = NpcSaveStore.GetInt($"last_day_{entry.Id}", -9999);
if (day - lastDay < entry.CooldownDays) continue;
}
// Step 5: 已逮捕黑名单排除
try
{
if (manager.arrestedClients != null && manager.arrestedClients.Contains(entry.Id))
continue;
}
catch { }
// Step 6: 自定义前置条件谓词 (CanSpawnPredicate)
try
{
if (entry.CanSpawnPredicate != null && !entry.CanSpawnPredicate())
continue;
}
catch (Exception ex)
{
CustomNPCFrameworkMod.Log?.Warning($"CanSpawnPredicate error for '{entry.Id}': " + ex.Message);
continue;
}
// Step 7: 概率倍率检定 (SpawnChancePercent * SpawnChanceMultiplier)
int effectiveChance = Mathf.Clamp(Mathf.RoundToInt(entry.SpawnChancePercent * chanceScale), 0, 100);
if (effectiveChance <= 0) continue;
if (effectiveChance < 100 && !RNG.Roll(effectiveChance)) continue;
// 通过全部检定:构造 StoreClient 实例并入队
try
{
var client = BuildSafeClient(entry);
if (client == null) continue;
if (entry.NoDuplicateInQueue)
manager.TryAddClient(client, true);
else
manager.AddClient(client);
RecordSpawn(entry, day);
entry.OnSpawned?.Invoke();
CustomNPCFrameworkMod.Log?.Log($"Queued NPC '{entry.Id}' on Day {day} ({phase}).");
}
catch (Exception ex)
{
CustomNPCFrameworkMod.Log?.Warning($"Failed to create/queue NPC '{entry.Id}': " + ex.Message);
}
}
}
// 阶段 4 (VeryLate) 最末尾:若存在挂起的调试召唤 NPC,立即置顶到 clientStack[0]
if (phase == NpcSpawnPhase.VeryLate)
{
string targetNpcId = CustomNPCFrameworkMod.GetConfiguredSummonNpcId();
if (!string.IsNullOrEmpty(targetNpcId))
{
var debugEntry = Find(targetNpcId);
if (debugEntry != null)
ForceInsertClientAtFront(manager, debugEntry, autoCallIfDoorEmpty: false, delayedCall: false);
}
}
}3. 即时召唤与队首置顶机制(ForceInsertClientAtFront)
反编译 StoreClientManager.AddNextClient(StoreClient) 的 IL 代码可知,原版方法开头包含两道拦截:
csharp
// 原版 StoreClientManager.AddNextClient 反编译等价逻辑:
public void AddNextClient(StoreClient client)
{
if (client == null) return;
if (this.IsClientBlockedByKnownSnitch(client)) return; // 拦截点 1:告密者惩罚状态拦截
if (this.IsClientBlockedByBannedCommissary(client)) return; // 拦截点 2:禁售开局天赋拦截
this.clientStack.Insert(0, client);
this.TrackClient(client);
}此外,如果同一个 NPC 在当日的 Early 或 Normal 阶段已经自然生成过一次,后续再插队会导致当日队列里出现两个完全相同的 NPC。
因此,NpcFrameworkRegistry.ForceInsertClientAtFront 直接操作底层公开列表 mgr.clientStack 与公开方法 mgr.TrackClient(client):
csharp
internal static bool ForceInsertClientAtFront(StoreClientManager mgr, NpcRegistrationEntry entry, bool autoCallIfDoorEmpty, bool delayedCall)
{
if (mgr == null || entry == null) return false;
var client = BuildSafeClient(entry);
if (client == null) return false;
if (mgr.clientStack == null)
mgr.clientStack = new List<StoreClient>();
// 1. 先移除队列中可能已经自然刷出的同名重复实例,保证全队唯一且位于 index 0
string targetId = client.identifier ?? entry.Id;
mgr.clientStack.RemoveAll(c => c != null && string.Equals(c.identifier, targetId, StringComparison.OrdinalIgnoreCase));
// 2. 直接插入 index 0 并登记冷却追踪,绕过 KnownSnitch / BannedCommissary 拦截
mgr.clientStack.Insert(0, client);
mgr.TrackClient(client);
int day = StoreStation.Instance != null ? StoreStation.Instance.dayCounter : 1;
RecordSpawn(entry, day);
entry.OnSpawned?.Invoke();
// 3. 若需要自动唤入店门口(卷帘门拉开时 delayedCall=true 延迟 0.35s;营业中途 delayedCall=false 立即唤入)
if (autoCallIfDoorEmpty)
ScheduleAutoCallFirstClient(delayedCall);
return true;
}