Skip to content

2.3 跨日存档状态机(NpcSaveStore)与 ModConfig 动态设置系统 ​

本节解析 NpcSaveStore.cs 如何利用游戏原生存档字典实现多存档隔离的持久化存储,以及 CustomNPCFrameworkMod.cs 中 ModConfig 设置项与开发者调试面板的完整实现。


1. NpcSaveStore:基于 PlayerStore.ModData() 的命名空间隔离存储 ​

在《深空当铺:可能是偷的》中,PlayerStore.Instance.ModData() 返回一个随当前存档(runID)一起序列化到存档 JSON 文件中的 Dictionary<string, string>。

为了防止不同 Mod 的键名冲突,并确保在主菜单(PlayerStore.Instance == null)调用时不抛异常,NpcSaveStore.cs 实现了带 "cnpcf_" 前缀与内存回退池 MemoryFallback 的双重存储层:

csharp
namespace CustomNPCFramework
{
    public static class NpcSaveStore
    {
        private const string Prefix = "cnpcf_";
        private static readonly Dictionary<string, string> MemoryFallback = new Dictionary<string, string>(StringComparer.Ordinal);

        private static Dictionary<string, string> GetActiveDict()
        {
            try
            {
                var ps = PlayerStore.Instance;
                if (ps != null)
                {
                    var dict = ps.ModData();
                    if (dict != null) return dict;
                }
            }
            catch { }
            return MemoryFallback;
        }

        private static string FullKey(string key) => Prefix + (key ?? "");

        public static string GetString(string key, string defaultValue = "")
        {
            var dict = GetActiveDict();
            return dict.TryGetValue(FullKey(key), out var val) && val != null ? val : defaultValue;
        }

        public static void SetString(string key, string value)
        {
            var dict = GetActiveDict();
            dict[FullKey(key)] = value ?? "";
        }

        public static int GetInt(string key, int defaultValue = 0)
        {
            string raw = GetString(key, null);
            if (!string.IsNullOrEmpty(raw) && int.TryParse(raw, out int parsed))
                return parsed;
            return defaultValue;
        }

        public static void SetInt(string key, int value) => SetString(key, value.ToString());

        public static int AddInt(string key, int delta, int defaultValue = 0)
        {
            int next = GetInt(key, defaultValue) + delta;
            SetInt(key, next);
            return next;
        }

        public static void ResetAllFrameworkKeys()
        {
            var dict = GetActiveDict();
            var toRemove = new List<string>();
            foreach (var kv in dict)
            {
                if (kv.Key != null && kv.Key.StartsWith(Prefix, StringComparison.Ordinal))
                    toRemove.Add(kv.Key);
            }
            for (int i = 0; i < toRemove.Count; i++)
                dict.Remove(toRemove[i]);
            MemoryFallback.Clear();
        }
    }
}

框架自动维护的内置存档键 ​

完整存档键名写入时机用途
cnpcf_last_day_<npcId>每次该 NPC 被排入队列或强制召唤时记录该 NPC 最近一次生成的游戏天数(dayCounter),用于计算 CooldownDays。
cnpcf_visits_<npcId>每次该 NPC 被排入队列或强制召唤时累计该 NPC 在当前存档中的总造访次数,用于判断 OncePerSave。
cnpcf_billy_stage / cnpcf_billy_fedRestoredUnusedNPCs 剧情推进时控制比利四幕剧情阶段(0..5)与第 1 幕是否给过食物(0/1)。
cnpcf_v_stage / cnpcf_v_trustCyberpunkMercV 剧情推进时控制赛博朋克 V 五阶段剧情(0..5)与信任度等级。

2. ModConfig 设置面板与动态下拉选项刷新 ​

在 CustomNPCFrameworkMod.InitModConfig() 中,利用游戏原生的 ModConfig.For("CustomNPCFramework") 注册了两大分组共 5 个配置项:

  1. 常规与生成设置 / General & Spawn Settings:
    • language(下拉框):"自动跟随游戏 / Auto (Follow Game)"、"简体中文 / Simplified Chinese"、"英文 / English"。通过委托 label: () => T(...) 实现界面语言热切换;
    • enabled(布尔开关):是否启用扩展包 NPC 的每日自动随机生成;
    • spawn_rate_percent(整型滑块 25 ~ 300,默认 100):全局缩放所有概率型自定义 NPC 的出现权重。
  2. 开发者调试面板 / Developer Debug Panel:
    • debug_summon_id(动态下拉框):列出 "不召唤 / None"、置顶的智能剧情入口 "v (赛博朋克V - 按当前剧情阶段自动推进)"、"billy (比利 - 按当前剧情阶段自动推进)"、16 个预设阶段 ID,以及所有第三方 Mod 通过 RegisterNpc 动态注册的额外 NPC ID;
    • debug_reset_save(布尔开关):开启并点击应用后,清空当前存档内所有 cnpcf_ 开头的剧情进度与冷却记录,并自动恢复为关闭状态(若在主菜单开启,则挂起至进入存档时自动执行)。

Custom NPC Framework Documentation