Kaynağa Gözat

ExcelExporter工具生成proto class成功

tanghai 5 yıl önce
ebeveyn
işleme
c79738cfa2

BIN
Excel/StartMachineConfig.xlsx


BIN
Excel/StartProcessConfig.xlsx


BIN
Excel/StartSceneConfig.xlsx


BIN
Excel/StartZoneConfig.xlsx


BIN
Excel/UnitConfig.xlsx


+ 6 - 0
Tools/ExcelExporter/ExcelExporter.csproj

@@ -13,5 +13,11 @@
     <ItemGroup>
       <PackageReference Include="EPPlus" Version="5.5.2" />
     </ItemGroup>
+    
+    <ItemGroup>
+      <None Update="Template.txt">
+        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      </None>
+    </ItemGroup>
 
 </Project>

+ 30 - 45
Tools/ExcelExporter/Program.cs

@@ -24,44 +24,47 @@ namespace ET
     
     class Program
     {
+        private static string template;
+        
         static void Main(string[] args)
         {
+            template = File.ReadAllText("Template.txt");
             ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
-            ExportExcel("/Users/tanghai/Documents/ET/Excel/StartMachineConfig.xlsx", new StringBuilder());
+            ExportExcel("/Users/tanghai/Documents/ET/Excel/StartZoneConfig.xlsx", new StringBuilder());
         }
 
         static void ExportExcel(string path, StringBuilder stringBuilder)
         {
-            FileInfo fileInfo = new FileInfo(path);
-            using ExcelPackage p = new ExcelPackage(fileInfo);
+            using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+            using ExcelPackage p = new ExcelPackage(stream);
 
             List<HeadInfo> classField = new List<HeadInfo>();
+            HashSet<string> uniqeField = new HashSet<string>();
             foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
             {
-                ExportSheet(worksheet, classField);
+                ExportSheet(worksheet, classField, uniqeField);
             }
 
-            ExportClass(Path.GetFileNameWithoutExtension(path), classField, "");
-
-            foreach (HeadInfo headInfo in classField)
-            {
-                Console.WriteLine($"{headInfo.FieldName} {headInfo.FieldType} {headInfo.FieldDesc}");
-            }
+            ExportClass(Path.GetFileNameWithoutExtension(path), classField, "./");
         }
         
-        static void ExportSheet(ExcelWorksheet worksheet, List<HeadInfo> classField)
+        static void ExportSheet(ExcelWorksheet worksheet, List<HeadInfo> classField, HashSet<string> uniqeField)
         {
             const int row = 2;
             for (int col = 3; col < worksheet.Dimension.Columns; ++col)
             {
-                string fieldCS = worksheet.Cells[row, col].Text.Trim();
-                string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
                 string fieldName = worksheet.Cells[row + 2, col].Text.Trim();
-                string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
                 if (fieldName == "")
                 {
                     continue;
                 }
+                if (!uniqeField.Add(fieldName))
+                {
+                    continue;
+                }
+                string fieldCS = worksheet.Cells[row, col].Text.Trim();
+                string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
+                string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
 
                 classField.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
             }
@@ -70,41 +73,23 @@ namespace ET
         static void ExportClass(string protoName, List<HeadInfo> classField, string exportDir)
         {
             string exportPath = Path.Combine(exportDir, $"{protoName}.cs");
-            using (FileStream txt = new FileStream(exportPath, FileMode.Create))
-            using (StreamWriter sw = new StreamWriter(txt))
+            
+            using FileStream txt = new FileStream(exportPath, FileMode.Create);
+            using StreamWriter sw = new StreamWriter(txt);
+            
+            StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < classField.Count; i++)
             {
-                StringBuilder sb = new StringBuilder();
-                sb.Append("namespace ET\n{");
-                sb.Append($"\t[Config]\n");
-                sb.Append($"\tpublic partial class {protoName}Category : ACategory<{protoName}>\n");
-                sb.Append("\t{\n");
-                sb.Append($"\t\tpublic static {protoName}Category Instance;\n");
-                sb.Append($"\t\tpublic {protoName}Category()\n");
-                sb.Append("\t\t{\n");
-                sb.Append($"\t\t\tInstance = this;\n");
-                sb.Append("\t\t}\n");
-                sb.Append("\t}\n\n");
-
-                sb.Append($"\tpublic partial class {protoName}: IConfig\n");
-                sb.Append("\t{\n");
-                sb.Append("\t\t[BsonId]\n");
-                sb.Append("\t\tpublic long Id { get; set; }\n");
-
-                for (int i = 0; i < classField.Count; i++)
+                HeadInfo headInfo = classField[i];
+                if (headInfo.ClientServer.StartsWith("#"))
                 {
-                    HeadInfo headInfo = classField[i];
-                    if (headInfo.ClientServer.StartsWith("#"))
-                    {
-                        continue;
-                    }
-                    sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set;}};\n");
+                    continue;
                 }
-
-                sb.Append("\t}\n");
-                sb.Append("}\n");
-
-                sw.Write(sb.ToString());
+                sb.Append($"\t\t[ProtoMember({i + 1}, IsRequired  = true)]\n");
+                sb.Append($"\t\tpublic {headInfo.FieldType} {headInfo.FieldName} {{ get; set; }}\n");
             }
+            string content = template.Replace("(ConfigName)", protoName).Replace(("(Fields)"), sb.ToString());
+            sw.Write(content);
         }
     }
 }

+ 73 - 0
Tools/ExcelExporter/Template.txt

@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using MongoDB.Bson.Serialization.Attributes;
+using ProtoBuf;
+
+namespace ET
+{
+    [Config]
+    public partial class (ConfigName)Category : ProtoObject
+    {
+        public static (ConfigName)Category Instance;
+		
+        [ProtoIgnore]
+        [BsonIgnore]
+        private Dictionary<int, (ConfigName)> dict = new Dictionary<int, (ConfigName)>();
+		
+        [BsonElement]
+        [ProtoMember(1)]
+        private List<(ConfigName)> list = new List<(ConfigName)>();
+		
+        public (ConfigName)Category()
+        {
+            Instance = this;
+        }
+		
+        public override void AfterDeserialization()
+        {
+            foreach (StartMachineConfig config in list)
+            {
+                this.dict.Add(config.Id, config);
+            }
+            list.Clear();
+            base.AfterDeserialization();
+        }
+		
+        public (ConfigName) Get(int id)
+        {
+            this.dict.TryGetValue(id, out (ConfigName) item);
+
+            if (item == null)
+            {
+                throw new Exception($"配置找不到,配置表名: {nameof ((ConfigName))},配置id: {id}");
+            }
+
+            return item;
+        }
+		
+        public bool Contain(int id)
+        {
+            return this.dict.ContainsKey(id);
+        }
+
+        public Dictionary<int, (ConfigName)> GetAll()
+        {
+            return this.dict;
+        }
+
+        public (ConfigName) GetOne()
+        {
+            if (this.dict == null || this.dict.Count <= 0)
+            {
+                return null;
+            }
+            return this.dict.Values.First();
+        }
+    }
+
+	public partial class (ConfigName): IConfig
+	{
+(Fields)
+	}
+}