2.1 Zero-Reference AppDomain Bridge & IMod Lifecycle
This section explains how CustomNPCFrameworkMod.cs, NpcFrameworkRegistry.cs, and add-on packs (NpcFrameworkSdk.cs) achieve zero-DLL-reference coupling, load-order independence, and 100% ModScanner compliance.
1. Why Use AppDomain.CurrentDomain Delegate Bridging?
- Load-Order Independence:
ModLoadermay initialize add-on packs (CyberpunkMercV,RestoredUnusedNPCs) either before or afterCustomNPCFramework. - Static Scanner Compliance (
ModScanner): reflecting into private members triggers security warnings, whereasSystem.AppDomain.CurrentDomain.GetData(string)andSetData(string, object)use standard BCL types (Dictionary<string, object>,Action<...>,Func<...>) and require zero declared<Capabilities>.
2. The Three Global Data Slots
csharp
public const string BridgeKey = "CustomNPCFramework.Bridge.v1";
public const string PendingQueueKey = "CustomNPCFramework.PendingQueue.v1";
public const string PendingPhoneQueueKey = "CustomNPCFramework.PendingPhoneQueue.v1";2.1 InitializeBridge() Full Delegate Table
csharp
public static void InitializeBridge()
{
var bridge = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{
["RegisterNpc"] = new Action<Dictionary<string, object>>(RegisterFromDictionary),
["SummonNpcNow"] = new Func<string, bool>(SummonNpcImmediate),
["ResolveSprite"] = new Func<string, string>(NpcTraitHelper.ResolveValidSpriteName),
["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),
["RegisterPhoneContact"] = new Action<Dictionary<string, object>>(NpcPhoneRegistry.RegisterFromDictionary),
["SyncPhoneContacts"] = new Action(NpcPhoneRegistry.SyncPhoneContactsToPlayerStore),
["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();
}3. All 11 Subscribed ModHook Events
Subscribed ModHook Event | Trigger Timing | Framework Handler & Behavior |
|---|---|---|
OnGenerateCustomerVeryEarly .. VeryLate | GenerateClient() Phases 0..4 | Runs HandleGenerationPhase(mgr, phase) and places any pending debug summon at clientStack[0] at the end of VeryLate. |
OnShutterOpenedLate | Morning shutter fully opened | Syncs textures, drains pending queues, applies deferred save resets, and runs ConsumePendingDebugSummonOnShutterOpened() to verify clientStack[0] and call TryCallNextClient(). |
OnGameLoadedInit | Save file loaded into the store scene | Syncs all mod textures, syncs holophone contacts to PlayerStore.PhoneClientDict, and restores pending debug state. |
OnModAssetsLoaded | Any mod's Textures/ assets finish loading | Runs RegisterModTexturesToSpriteDict(mod) to register 100 PPU, FilterMode.Point sprites into SpriteDict and refreshes debug dropdown options. |
OnModConfigApplied | Player clicks Apply in Mod Config | Processes save resets and debug summon selections. |
OnGoingSleepEarly / OnAfterhourLeaveEarly | Before sleeping / leaving afterhour area | Fires callbacks registered via RegisterNightlyHook and RegisterDumpingGroundWatcher. |