diff --git a/ExcelTool/ExcelHelper.cs b/ExcelTool/ExcelHelper.cs index c9cc6ce..8879b0d 100644 --- a/ExcelTool/ExcelHelper.cs +++ b/ExcelTool/ExcelHelper.cs @@ -9,9 +9,10 @@ namespace ExcelTool { public static class ExcelHelper { - public static List ParseAllSheets(string fileName) + public static List ParseAllSheets(string fileName, out List enumSheets) { List result = []; + enumSheets = []; try { using FileStream fs = File.OpenRead(fileName); @@ -27,7 +28,13 @@ namespace ExcelTool if (string.IsNullOrEmpty(sheetName) || sheetName.StartsWith('#')) continue; - var parsed = ParseSheet(sheet, sheetName); + if (sheetName.Equals("__enums__", StringComparison.OrdinalIgnoreCase)) + { + enumSheets = ParseEnumSheet(sheet); + continue; + } + + ParsedSheet parsed = ParseSheet(sheet, sheetName); if (parsed != null) result.Add(parsed); } @@ -47,7 +54,7 @@ namespace ExcelTool IRow typeRow = sheet.GetRow(1); IRow descRow = sheet.GetRow(2); - var headers = new List(); + List headers = []; for (int j = 0; j < nameRow.LastCellNum; j++) { string fieldName = nameRow.GetCell(j)?.ToString() ?? ""; @@ -69,13 +76,13 @@ namespace ExcelTool } // 数据从第 6 行开始(0-indexed) - var tableRows = new List(); + List tableRows = []; for (int i = 5; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row == null) continue; - var tableExcelRow = new TableExcelRow(); + TableExcelRow tableExcelRow = new(); for (int j = 0; j < headers.Count; j++) tableExcelRow.Add(GetCellValue(row.GetCell(j))); @@ -95,8 +102,52 @@ namespace ExcelTool return null; } } - - private static string GetCellValue(ICell cell) + + static List ParseEnumSheet(ISheet sheet) + { + List result = []; + // 列索引约定:A=0 Id, B=1 EnumName, C=2 items.Name, D=3 items.Value, E=4 items.Desc + // 数据从第 6 行开始(0-indexed = 5) + ParsedEnum currentEnum = null; + + for (int i = 5; i <= sheet.LastRowNum; i++) + { + IRow row = sheet.GetRow(i); + if (row == null) continue; + + string idStr = GetCellValue(row.GetCell(0)); + string enumName = GetCellValue(row.GetCell(1)); + string memberKey = GetCellValue(row.GetCell(2)); + string memberVal = GetCellValue(row.GetCell(3)); + string memberDesc = GetCellValue(row.GetCell(4)); + + // 成员名为空则跳过此行 + if (string.IsNullOrEmpty(memberKey)) continue; + + // Id / EnumName 非空时,开启新枚举 + if (!string.IsNullOrEmpty(enumName)) + { + currentEnum = new ParsedEnum + { + Id = string.IsNullOrEmpty(idStr) ? 0 : Convert.ToUInt32(double.Parse(idStr)), + EnumName = enumName, + }; + result.Add(currentEnum); + } + + // 如果没有任何枚举上下文就跳过 + currentEnum?.Members.Add(new ParsedEnumMember + { + Key = memberKey, + Value = string.IsNullOrEmpty(memberVal) ? null : Convert.ToInt32(double.Parse(memberVal)), + Desc = memberDesc, + }); + } + + return result; + } + + static string GetCellValue(ICell cell) { if (cell == null) return ""; diff --git a/ExcelTool/Parser/GenEnums.cs b/ExcelTool/Parser/GenEnums.cs new file mode 100644 index 0000000..be403c9 --- /dev/null +++ b/ExcelTool/Parser/GenEnums.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace ExcelTool.Parser +{ + public static class GenEnums + { + /// + /// 所有枚举写入同一个文件 AudioEnums.cs + /// + public static bool GenCSharpEnum(List enumSheet, string outputDir, string nameSpace = "") + { + try + { + StringBuilder sb = new(); + sb.Append("/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * AudioEnums\n */\n\n"); + + if (!string.IsNullOrEmpty(nameSpace)) + sb.Append($"namespace {nameSpace}\n{{\n"); + + foreach (ParsedEnum parsedEnum in enumSheet) + { + sb.Append($"\tpublic enum {parsedEnum.EnumName}\n\t{{\n"); + + foreach (ParsedEnumMember member in parsedEnum.Members) + { + string valuePart = member.Value.HasValue ? $" = {member.Value.Value}" : ""; + string descPart = string.IsNullOrEmpty(member.Desc) ? "" : $" // {member.Desc}"; + sb.Append($"\t\t{member.Key}{valuePart},{descPart}\n"); + } + + sb.Append("\t}\n\n"); + } + + if (!string.IsNullOrEmpty(nameSpace)) + sb.Append("}\n"); + + FileManager.WriteToFile(Path.Combine(outputDir, "AudioEnums.cs"), sb.ToString()); + return true; + } + catch (Exception ex) + { + ex.ToString().WriteErrorLine(); + return false; + } + } + + /// + /// 将所有枚举的 Id 汇总,生成 EnumIds.cs 静态常量类 + /// + public static bool GenEnumIds(List enumSheets, string outputDir, string nameSpace = "") + { + try + { + StringBuilder sb = new(); + sb.Append("/*\n * auto generated by tools(注意:千万不要手动修改本文件)\n * EnumIds\n */\n\n"); + + if (!string.IsNullOrEmpty(nameSpace)) + sb.Append($"namespace {nameSpace}\n{{\n"); + + sb.Append("\tpublic static class EnumIds\n\t{\n"); + + foreach (ParsedEnum enumSheet in enumSheets) + sb.Append($"\t\tpublic const uint {enumSheet.EnumName} = {enumSheet.Id};\n"); + + sb.Append("\t}\n"); + + if (!string.IsNullOrEmpty(nameSpace)) + sb.Append("}\n"); + + FileManager.WriteToFile(Path.Combine(outputDir, "AudioEnumIds.cs"), sb.ToString()); + return true; + } + catch (Exception ex) + { + ex.ToString().WriteErrorLine(); + return false; + } + } + } +} diff --git a/ExcelTool/Parser/ParsedEnum.cs b/ExcelTool/Parser/ParsedEnum.cs new file mode 100644 index 0000000..c6f4699 --- /dev/null +++ b/ExcelTool/Parser/ParsedEnum.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; + +namespace ExcelTool.Parser +{ + public class ParsedEnumMember + { + /// 成员名,如 "Home" + public string Key { get; set; } + + /// 显式值,为 null 时代码生成时自动递增(不写值) + public int? Value { get; set; } + + /// 行内注释,如 "主界面" + public string Desc { get; set; } + } + + public class ParsedEnum + { + /// A 列 Id,供外部系统通过 EnumIds 常量类引用 + public uint Id { get; set; } + + /// 枚举类型名,如 "GameState" + public string EnumName { get; set; } + + public List Members { get; set; } = new(); + } +} diff --git a/ExcelTool/Program.cs b/ExcelTool/Program.cs index 856cae3..d71e752 100644 --- a/ExcelTool/Program.cs +++ b/ExcelTool/Program.cs @@ -76,7 +76,7 @@ namespace ExcelTool { if (file.Name.StartsWith("~$")) continue; - List sheets = ExcelHelper.ParseAllSheets(file.FullName); + List sheets = ExcelHelper.ParseAllSheets(file.FullName, out List? enumSheets); //生成CS文件 bool res = GenModels.GenCSharpModel(sheets, codeDir, nameSpace); @@ -88,6 +88,22 @@ namespace ExcelTool { $"{file.Name}CS模板生成失败".WriteErrorLine(); } + + // 生成CS枚举文件 + if (enumSheets.Count > 0) + { + bool enumRes = GenEnums.GenCSharpEnum(enumSheets, codeDir, nameSpace); + if (enumRes) + $"{file.Name} 枚举代码生成成功".WriteSuccessLine(); + else + $"{file.Name} 枚举代码生成失败".WriteErrorLine(); + + bool enumIdsRes = GenEnums.GenEnumIds(enumSheets, codeDir, nameSpace); + if (enumIdsRes) + $"{file.Name} EnumIds 生成成功".WriteSuccessLine(); + else + $"{file.Name} EnumIds 生成失败".WriteErrorLine(); + } //生成二进制文件,如果list或者vector数据为空则写入0,要根据类型来读取csv的字段数据强转成对应的数据类型然后写入 res = TableExcelExportBytes.ExportToFile(sheets, dataDir);