2.1 零引用 AppDomain 桥接协议与 IMod 生命周期
本节深入分析 CustomNPCFrameworkMod.cs、NpcFrameworkRegistry.cs 与扩展包 NpcFrameworkSdk.cs 之间如何实现 “零 DLL 引用耦合、与加载顺序完全无关、且 100% 通过 ModScanner 静态安全扫描” 的跨程序集通信机制。
1. 为什么采用 AppDomain.CurrentDomain 委托桥接?
在《深空当铺:可能是偷的》的模组加载器(ModLoader)中,存在两个核心工程约束:
- 加载顺序不确定性:玩家安装的多个 Mod 按目录名或创意工坊订阅顺序加载,扩展包(如
CyberpunkMercV)可能在底座框架(CustomNPCFramework)之前执行Init(),也可能在其之后执行; ModScanner反射限制:游戏内置静态字节码审计器ModScanner,若模组使用System.Reflection调用私有方法或跨程序集动态反射,会触发权限告警。而System.AppDomain.CurrentDomain.GetData(string)与SetData(string, object)属于标准公开托管内存槽,所有委托(Action<...>/Func<...>)与基础容器(Dictionary<string, object>/List<...>) 均在mscorlib/netstandard标准类型范围内,无需任何反射或程序集直接引用。
2. 三大全局内存槽(Data Slots)定义
在 NpcFrameworkRegistry.cs 中定义了三个全局常量键名:
csharp
public const string BridgeKey = "CustomNPCFramework.Bridge.v1";
public const string PendingQueueKey = "CustomNPCFramework.PendingQueue.v1";
public const string PendingPhoneQueueKey = "CustomNPCFramework.PendingPhoneQueue.v1";2.1 CustomNPCFramework.Bridge.v1 完整委托注册表
当 NpcFrameworkRegistry.InitializeBridge() 执行时,会将 22 个核心操作委托一次性挂载到 AppDomain.CurrentDomain:
csharp
public static void InitializeBridge()
{
var bridge = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{
// 1. 核心 NPC 注册与即时召唤
["RegisterNpc"] = new Action<Dictionary<string, object>>(RegisterFromDictionary),
["SummonNpcNow"] = new Func<string, bool>(SummonNpcImmediate),
["ResolveSprite"] = new Func<string, string>(NpcTraitHelper.ResolveValidSpriteName),
// 2. 跨日剧情存档状态机 (cnpcf_ 命名空间)
["GetSaveInt"] = new Func<string, int, int>(NpcSaveStore.GetInt),
["SetSaveInt"] = new Action<string, int>(NpcSaveStore.SetInt),
["AddSaveInt"] = new Func<string, int, int, int>(NpcSaveStore.AddInt),
["GetSaveBool"] = new Func<string, bool, bool>(NpcSaveStore.GetBool),
["SetSaveBool"] = new Action<string, bool>(NpcSaveStore.SetBool),
["GetSaveString"] = new Func<string, string, string>(NpcSaveStore.GetString),
["SetSaveString"] = new Action<string, string>(NpcSaveStore.SetString),
// 3. 全息电话簿注册与实时同步
["RegisterPhoneContact"] = new Action<Dictionary<string, object>>(NpcPhoneRegistry.RegisterFromDictionary),
["SyncPhoneContacts"] = new Action(NpcPhoneRegistry.SyncPhoneContactsToPlayerStore),
// 4. 声明式属性映射与 顾客塑造子系统高阶工具箱
["ApplyClientProfile"] = new Action<StoreClient, Dictionary<string, object>>(NpcFullFeatureKit.ApplyClientProfile),
["CreateHandnoteItem"] = new Func<string, string, string, int, GameItem>(NpcFullFeatureKit.CreateHandnoteItem),
["CreatePaperNoteItem"] = new Func<string, string, GameItem>(NpcFullFeatureKit.CreatePaperNoteItem),
["SpawnItemOnCounterOrStore"] = new Func<GameItem, bool, bool, bool>(NpcFullFeatureKit.SpawnItemOnCounterOrStore),
["GiveHandnoteToPlayer"] = new Func<string, string, string, int, bool>(NpcFullFeatureKit.GiveHandnoteToPlayer),
["PostStoreRequest"] = new Func<Dictionary<string, object>, bool>(NpcFullFeatureKit.PostStoreRequestFromDictionary),
["AttachProcurementOffer"] = new Action<StoreClient, string, string, string, int, int, int, Func<GameItem, bool>>(NpcFullFeatureKit.AttachProcurementOffer),
["BuildCustomGunOrder"] = new Func<int, int, string, bool, IList<(string, int, int, bool)>, GunOrderData>(NpcFullFeatureKit.BuildCustomGunOrder),
["RegisterAfterhourEvent"] = new Func<Dictionary<string, object>, bool>(NpcFullFeatureKit.RegisterAfterhourEventFromDictionary),
["RegisterNightlyHook"] = new Action<Action>(NpcFullFeatureKit.RegisterNightlyHook),
["RegisterDumpingGroundWatcher"] = new Action<Action<GameInventory>>(NpcFullFeatureKit.RegisterDumpingGroundWatcher),
["TriggerNews"] = new Action<string, string, string, int>(NpcTraitHelper.TriggerCustomNewspaper)
};
AppDomain.CurrentDomain.SetData(BridgeKey, bridge);
DrainPendingQueue();
NpcPhoneRegistry.SyncPhoneContactsToPlayerStore();
}2.2 双缓冲挂起队列(DrainPendingQueue)消费机制
当扩展包比框架更早加载时,NpcFrameworkSdk.RegisterNpc 与 RegisterPhoneContact 会自动将构造好的参数字典推入 PendingQueue.v1 和 PendingPhoneQueue.v1。随后框架在以下 5 个时机 均会主动调用 DrainPendingQueue(),确保没有任何注册请求遗漏:
NpcFrameworkRegistry.InitializeBridge()(框架Init与OnEnable时);CustomNPCFrameworkMod.OnModAssetsLoaded(LoadedMod mod)(任意 Mod 资产加载完成时);CustomNPCFrameworkMod.OnGameLoadedInit()(玩家加载存档进入游戏时);CustomNPCFrameworkMod.OnShutterOpenedLate()(玩家每天拉开卷帘门时);NpcFrameworkRegistry.Find(string id)/HandleGenerationPhase(...)(每次查询或生成顾客前)。
3. CustomNPCFrameworkMod 完整 ModHook 事件挂载表
框架在 OnEnable() 与 NpcFullFeatureKit.InitHooks() 中挂载了游戏原生的 11 个核心生命周期钩子,在 OnDisable() 中对称解绑:
挂载的 ModHook 事件 | 触发时机 | 框架绑定的处理函数与核心行为 |
|---|---|---|
OnGenerateCustomerVeryEarly | GenerateClient() 阶段 0(最早期) | 调用 HandleGenerationPhase(mgr, NpcSpawnPhase.VeryEarly)。 |
OnGenerateCustomerEarly | GenerateClient() 阶段 1(原版固定剧情后) | 调用 HandleGenerationPhase(mgr, NpcSpawnPhase.Early),推荐主线剧情 NPC 使用。 |
OnGenerateCustomerNormal | GenerateClient() 阶段 2(原版常规抽样后) | 调用 HandleGenerationPhase(mgr, NpcSpawnPhase.Normal),推荐日常散客使用。 |
OnGenerateCustomerLate | GenerateClient() 阶段 3 | 调用 HandleGenerationPhase(mgr, NpcSpawnPhase.Late)。 |
OnGenerateCustomerVeryLate | GenerateClient() 阶段 4(原版全部逻辑末尾) | 调用 HandleGenerationPhase(mgr, NpcSpawnPhase.VeryLate),并在最末尾执行第一次调试召唤置顶 ForceInsertClientAtFront。 |
OnShutterOpenedLate | 早上卷帘门拉开且 HandleFutureClientQueue 完毕后 | 同步贴图、消费挂起队列、执行延迟存档重置,并调用 ConsumePendingDebugSummonOnShutterOpened() 二次确认置顶并自动调用 TryCallNextClient() 唤入店门。 |
OnGameLoadedInit | 存档加载进入店铺场景时 | 同步所有 Mod 贴图、同步全息电话簿联系人至 PlayerStore.PhoneClientDict、恢复调试挂起状态。 |
OnModAssetsLoaded | 任一 Mod 的 Textures/ 贴图加载完成时 | 调用 RegisterModTexturesToSpriteDict(mod) 将 PNG 转为 100 PPU、FilterMode.Point 的 Sprite 注入 SpriteDict,并刷新调试面板下拉选项。 |
OnModConfigApplied | 玩家在设置界面点击“应用 (Apply)”时 | 处理存档进度重置与调试召唤指令(若已开店则立即插队进门;若在主菜单或未开店则持久化挂起)。 |
OnGoingSleepEarly | 玩家夜晚点击床铺入睡前 | 触发 NpcFullFeatureKit 中通过 RegisterNightlyHook 注册的全部夜间结算回调。 |
OnAfterhourLeaveEarly | 玩家夜间离开垃圾场/夜间地点时 | 触发 RegisterDumpingGroundWatcher 注册的背包/地面扫描回调。 |