Skip to content

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? ​

  1. Load-Order Independence: ModLoader may initialize add-on packs (CyberpunkMercV, RestoredUnusedNPCs) either before or after CustomNPCFramework.
  2. Static Scanner Compliance (ModScanner): reflecting into private members triggers security warnings, whereas System.AppDomain.CurrentDomain.GetData(string) and SetData(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 EventTrigger TimingFramework Handler & Behavior
OnGenerateCustomerVeryEarly .. VeryLateGenerateClient() Phases 0..4Runs HandleGenerationPhase(mgr, phase) and places any pending debug summon at clientStack[0] at the end of VeryLate.
OnShutterOpenedLateMorning shutter fully openedSyncs textures, drains pending queues, applies deferred save resets, and runs ConsumePendingDebugSummonOnShutterOpened() to verify clientStack[0] and call TryCallNextClient().
OnGameLoadedInitSave file loaded into the store sceneSyncs all mod textures, syncs holophone contacts to PlayerStore.PhoneClientDict, and restores pending debug state.
OnModAssetsLoadedAny mod's Textures/ assets finish loadingRuns RegisterModTexturesToSpriteDict(mod) to register 100 PPU, FilterMode.Point sprites into SpriteDict and refreshes debug dropdown options.
OnModConfigAppliedPlayer clicks Apply in Mod ConfigProcesses save resets and debug summon selections.
OnGoingSleepEarly / OnAfterhourLeaveEarlyBefore sleeping / leaving afterhour areaFires callbacks registered via RegisterNightlyHook and RegisterDumpingGroundWatcher.

Custom NPC Framework Documentation