From 0f328605266a6082c281d2978f8fcdfe546e064c Mon Sep 17 00:00:00 2001 From: Oliver Wong Date: Thu, 2 Apr 2026 14:28:23 +0800 Subject: [PATCH] refactor: improve SharedIdNames generation logic in GenModels.cs - Refactored SharedIdNames generation to use a HashSet for collecting shared names - Updated loop to iterate over KeyValuePair for clarity - Added inline comments for better code understanding - Simplified list initialization syntax - Improved type annotations in foreach loops --- ExcelTool/Parser/GenModels.cs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/ExcelTool/Parser/GenModels.cs b/ExcelTool/Parser/GenModels.cs index dbc909f..7389635 100644 --- a/ExcelTool/Parser/GenModels.cs +++ b/ExcelTool/Parser/GenModels.cs @@ -193,8 +193,7 @@ namespace ExcelTool.Parser { ParsedSheet audioSheet = parsedSheets.Find(s => s.SheetName == "AudioObject"); if (audioSheet == null) return; - - // 假设字段名:Id(uint), Names(list) + Dictionary> idToNames = new(); Dictionary> nameToIds = new(); @@ -214,13 +213,13 @@ namespace ExcelTool.Parser string rawNames = row.StrList[namesIndex]; List names = new(rawNames.Split([','], StringSplitOptions.RemoveEmptyEntries)); - idToNames[id] = names; + idToNames[id] = names; // 同一个ID对应了多少个不同的名字(Container) - foreach (var name in names) + foreach (string name in names) { - if (!nameToIds.TryGetValue(name, out var list)) + if (!nameToIds.TryGetValue(name, out List list)) { - list = new List(); + list = []; nameToIds[name] = list; } list.Add(id); @@ -258,15 +257,22 @@ namespace ExcelTool.Parser sb.Append("\t};\n\n"); // SharedIdNames - sb.Append("\tpublic static readonly HashSet SharedIdNames = new()\n\t{\n"); - foreach (var kv in idToNames) + HashSet sharedNames = []; + foreach (KeyValuePair> keyValuePair in idToNames) { - var names = kv.Value; - for (int i = 1; i < names.Count; i++) + if (keyValuePair.Value.Count > 1) { - sb.Append($"\t\t\"{names[i]}\",\n"); + foreach (string name in keyValuePair.Value) + sharedNames.Add(name); } } + + sb.Append("\tpublic static readonly HashSet SharedIdNames = new()\n\t{\n"); + + foreach (string name in sharedNames) + { + sb.Append($"\t\t\"{name}\",\n"); + } sb.Append("\t};\n"); sb.Append("}\n");