From 24d26a96c8dc3daccdcd4a875839c40e49c8adef Mon Sep 17 00:00:00 2001 From: henry3218 Date: Wed, 22 Oct 2025 13:41:19 +0800 Subject: [PATCH] Guard custom stats and word libraries against duplicate registration --- mod/CHEVALIER/code/stats.cs | 89 ++++++----- mod/WARRIOR/code/stats.cs | 145 ++++++++++-------- .../Code/WordLibraryManager.cs" | 25 ++- 3 files changed, 153 insertions(+), 106 deletions(-) diff --git a/mod/CHEVALIER/code/stats.cs b/mod/CHEVALIER/code/stats.cs index aecccbeb..a183ce1c 100644 --- a/mod/CHEVALIER/code/stats.cs +++ b/mod/CHEVALIER/code/stats.cs @@ -7,48 +7,63 @@ internal class stats public static void Init() { - BaseStatAsset Chevalier = new BaseStatAsset(); - Chevalier.id = "Chevalier"; - Chevalier.normalize = true; - Chevalier.normalize_min = -9999999; - Chevalier.normalize_max = 9999999; - //Chevalier.multiplier = true; - Chevalier.used_only_for_civs = false; - AssetManager.base_stats_library.add(Chevalier); + BaseStatAsset Chevalier = EnsureBaseStat("Chevalier", stat => + { + stat.normalize = true; + stat.normalize_min = -9999999; + stat.normalize_max = 9999999; + //Chevalier.multiplier = true; + stat.used_only_for_civs = false; + }); // 定义 Resist 属性 - Resist = new BaseStatAsset(); - Resist.id = "Resist"; - Resist.normalize = true; - Resist.normalize_min = 0; - Resist.normalize_max = 999999; - Resist.used_only_for_civs = false; - AssetManager.base_stats_library.add(Resist); + Resist = EnsureBaseStat("Resist", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 999999; + stat.used_only_for_civs = false; + }); - BaseStatAsset Dodge = new BaseStatAsset(); - Dodge.id = "Dodge";// 闪避率 - Dodge.normalize = true; - Dodge.normalize_min = 0; - Dodge.normalize_max = 99999; - Dodge.used_only_for_civs = false; - AssetManager.base_stats_library.add(Dodge); + EnsureBaseStat("Dodge", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 99999; + stat.used_only_for_civs = false; + }); - BaseStatAsset Accuracy = new BaseStatAsset(); - Accuracy.id = "Accuracy";// 命中率 - Accuracy.normalize = true; - Accuracy.normalize_min = 0; - Accuracy.normalize_max = 99999; - Accuracy.used_only_for_civs = false; - AssetManager.base_stats_library.add(Accuracy); + EnsureBaseStat("Accuracy", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 99999; + stat.used_only_for_civs = false; + }); // 定义领悟度属性 - Comprehension = new BaseStatAsset(); - Comprehension.id = "Comprehension"; - Comprehension.normalize = true; - Comprehension.normalize_min = 0; - Comprehension.normalize_max = 99999; - Comprehension.used_only_for_civs = false; - AssetManager.base_stats_library.add(Comprehension); + Comprehension = EnsureBaseStat("Comprehension", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 99999; + stat.used_only_for_civs = false; + }); + } + + private static BaseStatAsset EnsureBaseStat(string id, System.Action initializer) + { + BaseStatAsset existing = AssetManager.base_stats_library.get(id); + if (existing != null) + { + return existing; + } + + BaseStatAsset stat = new BaseStatAsset(); + stat.id = id; + initializer?.Invoke(stat); + AssetManager.base_stats_library.add(stat); + return stat; } } -} \ No newline at end of file +} diff --git a/mod/WARRIOR/code/stats.cs b/mod/WARRIOR/code/stats.cs index e192474c..b0ccdf08 100644 --- a/mod/WARRIOR/code/stats.cs +++ b/mod/WARRIOR/code/stats.cs @@ -12,84 +12,99 @@ internal class stats public static void Init() { - BaseStatAsset Warrior = new BaseStatAsset(); - Warrior.id = "Warrior"; - Warrior.normalize = true; - Warrior.normalize_min = -9999999; - Warrior.normalize_max = 9999999; - //Warrior.multiplier = true; - Warrior.used_only_for_civs = false; - AssetManager.base_stats_library.add(Warrior); + BaseStatAsset Warrior = EnsureBaseStat("Warrior", stat => + { + stat.normalize = true; + stat.normalize_min = -9999999; + stat.normalize_max = 9999999; + //Warrior.multiplier = true; + stat.used_only_for_civs = false; + }); // 定义阵纹属性 - Pattern = new BaseStatAsset(); - Pattern.id = "Pattern"; - Pattern.normalize = true; - Pattern.normalize_min = -9999999; - Pattern.normalize_max = 9999999; - Pattern.used_only_for_civs = false; - AssetManager.base_stats_library.add(Pattern); + Pattern = EnsureBaseStat("Pattern", stat => + { + stat.normalize = true; + stat.normalize_min = -9999999; + stat.normalize_max = 9999999; + stat.used_only_for_civs = false; + }); // 定义真罡属性,与武道气血使用相同的上下限 - TrueGang = new BaseStatAsset(); - TrueGang.id = "TrueGang"; - TrueGang.normalize = true; - TrueGang.normalize_min = -9999999; - TrueGang.normalize_max = 9999999; - TrueGang.used_only_for_civs = false; - AssetManager.base_stats_library.add(TrueGang); + TrueGang = EnsureBaseStat("TrueGang", stat => + { + stat.normalize = true; + stat.normalize_min = -9999999; + stat.normalize_max = 9999999; + stat.used_only_for_civs = false; + }); // 定义真罡真伤倍数属性 - TrueGangTrueDamageMultiplier = new BaseStatAsset(); - TrueGangTrueDamageMultiplier.id = "TrueGangTrueDamageMultiplier"; - TrueGangTrueDamageMultiplier.normalize = true; - TrueGangTrueDamageMultiplier.normalize_min = 0; - TrueGangTrueDamageMultiplier.normalize_max = 999999; - TrueGangTrueDamageMultiplier.used_only_for_civs = false; - AssetManager.base_stats_library.add(TrueGangTrueDamageMultiplier); + TrueGangTrueDamageMultiplier = EnsureBaseStat("TrueGangTrueDamageMultiplier", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 999999; + stat.used_only_for_civs = false; + }); // 定义真罡回血倍数属性 - TrueGangHealMultiplier = new BaseStatAsset(); - TrueGangHealMultiplier.id = "TrueGangHealMultiplier"; - TrueGangHealMultiplier.normalize = true; - TrueGangHealMultiplier.normalize_min = 0; - TrueGangHealMultiplier.normalize_max = 999999; - TrueGangHealMultiplier.used_only_for_civs = false; - AssetManager.base_stats_library.add(TrueGangHealMultiplier); + TrueGangHealMultiplier = EnsureBaseStat("TrueGangHealMultiplier", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 999999; + stat.used_only_for_civs = false; + }); // 定义真罡伤害稀释倍数属性 - TrueGangDamageReductionMultiplier = new BaseStatAsset(); - TrueGangDamageReductionMultiplier.id = "TrueGangDamageReductionMultiplier"; - TrueGangDamageReductionMultiplier.normalize = true; - TrueGangDamageReductionMultiplier.normalize_min = 0; - TrueGangDamageReductionMultiplier.normalize_max = 999999; - TrueGangDamageReductionMultiplier.used_only_for_civs = false; - AssetManager.base_stats_library.add(TrueGangDamageReductionMultiplier); + TrueGangDamageReductionMultiplier = EnsureBaseStat("TrueGangDamageReductionMultiplier", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 999999; + stat.used_only_for_civs = false; + }); // 定义 Resist 属性 - Resist = new BaseStatAsset(); - Resist.id = "Resist"; - Resist.normalize = true; - Resist.normalize_min = 0; - Resist.normalize_max = 999999; - Resist.used_only_for_civs = false; - AssetManager.base_stats_library.add(Resist); + Resist = EnsureBaseStat("Resist", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 999999; + stat.used_only_for_civs = false; + }); - BaseStatAsset Dodge = new BaseStatAsset(); - Dodge.id = "Dodge";// 闪避率 - Dodge.normalize = true; - Dodge.normalize_min = 0; - Dodge.normalize_max = 99999; - Dodge.used_only_for_civs = false; - AssetManager.base_stats_library.add(Dodge); + EnsureBaseStat("Dodge", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 99999; + stat.used_only_for_civs = false; + }); - BaseStatAsset Accuracy = new BaseStatAsset(); - Accuracy.id = "Accuracy";// 命中率 - Accuracy.normalize = true; - Accuracy.normalize_min = 0; - Accuracy.normalize_max = 99999; - Accuracy.used_only_for_civs = false; - AssetManager.base_stats_library.add(Accuracy); + EnsureBaseStat("Accuracy", stat => + { + stat.normalize = true; + stat.normalize_min = 0; + stat.normalize_max = 99999; + stat.used_only_for_civs = false; + }); + } + + private static BaseStatAsset EnsureBaseStat(string id, System.Action initializer) + { + BaseStatAsset existing = AssetManager.base_stats_library.get(id); + if (existing != null) + { + return existing; + } + + BaseStatAsset stat = new BaseStatAsset(); + stat.id = id; + initializer?.Invoke(stat); + AssetManager.base_stats_library.add(stat); + return stat; } } -} \ No newline at end of file +} diff --git "a/mod/\350\245\277\345\271\273\344\270\255\346\226\207\345\220\215/Code/WordLibraryManager.cs" "b/mod/\350\245\277\345\271\273\344\270\255\346\226\207\345\220\215/Code/WordLibraryManager.cs" index fba866e3..da9627f6 100644 --- "a/mod/\350\245\277\345\271\273\344\270\255\346\226\207\345\220\215/Code/WordLibraryManager.cs" +++ "b/mod/\350\245\277\345\271\273\344\270\255\346\226\207\345\220\215/Code/WordLibraryManager.cs" @@ -26,10 +26,27 @@ internal void Reload() } } /// - /// ָĴʿȡһ - /// - /// ָʿid, ΪӦļļȥ׺ - /// ָʿһ, ʿⲻ򷵻ؿմ + List words = text_asset.text.Replace("\r", "").Split('\n').ToList(); + if (Instance.dict.TryGetValue(text_asset.name, out WordLibraryAsset existingAsset)) + { + existingAsset.words.Clear(); + existingAsset.words.AddRange(words); + } + else + { + Instance.add(new WordLibraryAsset(text_asset.name, words)); + } + if (Instance.dict.TryGetValue(pId, out WordLibraryAsset existingAsset)) + { + existingAsset.words.Clear(); + existingAsset.words.AddRange(pWords); + } + else + { + Instance.add(new WordLibraryAsset(pId, pWords)); + } + /// 指定词库的id, 为对应文件文件名去除后缀 + /// 指定词库中随机一个词, 如果词库不存在则返回空串 public static string GetRandomWord(string pId) { if (Instance.dict.TryGetValue(pId, out WordLibraryAsset asset) && asset.words.Count > 0)