| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp;
- using Microsoft.CodeAnalysis.Emit;
- using MongoDB.Bson;
- using MongoDB.Bson.Serialization;
- using OfficeOpenXml;
- using ProtoBuf;
- using LicenseContext = OfficeOpenXml.LicenseContext;
- namespace ET
- {
- public enum ConfigType
- {
- c,
- s,
- }
-
- class HeadInfo
- {
- public string FieldAttribute;
- public string FieldDesc;
- public string FieldName;
- public string FieldType;
- public int FieldIndex;
- public HeadInfo(string cs, string desc, string name, string type, int index)
- {
- this.FieldAttribute = cs;
- this.FieldDesc = desc;
- this.FieldName = name;
- this.FieldType = type;
- this.FieldIndex = index;
- }
- }
-
- public static class ExcelExporter
- {
- private static string template;
- private const string clientClassDir = "../Unity/Codes/Model/Generate/Config";
- private const string serverClassDir = "../Apps/Model/Generate/Config";
-
- private const string excelDir = "../Excel";
-
- private const string jsonDir = "./Json/{0}";
-
- private const string clientProtoDir = "../Unity/Assets/Bundles/Config";
- private const string serverProtoDir = "../Config";
-
- public static void Export()
- {
- try
- {
- template = File.ReadAllText("Template.txt");
- ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
- if (Directory.Exists(clientClassDir))
- {
- Directory.Delete(clientClassDir, true);
- }
- if (Directory.Exists(serverClassDir))
- {
- Directory.Delete(serverClassDir, true);
- }
- foreach (string path in Directory.GetFiles(excelDir))
- {
- string fileName = Path.GetFileName(path);
- if (!fileName.EndsWith(".xlsx") || fileName.StartsWith("~$"))
- {
- continue;
- }
- using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
- using ExcelPackage p = new ExcelPackage(stream);
- string name = Path.GetFileNameWithoutExtension(path);
- string cs = p.Workbook.Worksheets[0].Cells[1, 1].Text.Trim();
- if (cs.Contains("#"))
- {
- continue;
- }
- if (cs == "")
- {
- cs = "cs";
- }
- if (cs.Contains("c"))
- {
- ExportExcelClass(p, name, ConfigType.c);
- }
- if (cs.Contains("s"))
- {
- ExportExcelClass(p, name, ConfigType.s);
- }
- }
-
- ExportExcelProtobuf(ConfigType.c);
- ExportExcelProtobuf(ConfigType.s);
-
- Log.Console("Export Excel Sucess!");
- }
- catch (Exception e)
- {
- Log.Console(e.ToString());
- }
- }
- private static string GetProtoDir(ConfigType configType)
- {
- if (configType == ConfigType.c)
- {
- return clientProtoDir;
- }
- return serverProtoDir;
- }
-
- private static string GetClassDir(ConfigType configType)
- {
- if (configType == ConfigType.c)
- {
- return clientClassDir;
- }
- return serverClassDir;
- }
-
-
- #region 导出class
- static void ExportExcelClass(ExcelPackage p, string name, ConfigType configType)
- {
- Dictionary<string, HeadInfo> classField = new Dictionary<string, HeadInfo>();
- foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
- {
- ExportSheetClass(worksheet, classField, configType);
- }
- ExportClass(name, classField, configType);
- ExportExcelJson(p, name, classField, configType);
- }
-
- static void ExportSheetClass(ExcelWorksheet worksheet, Dictionary<string, HeadInfo> classField, ConfigType configType)
- {
- string configTypeStr = configType.ToString();
- const int row = 2;
- for (int col = 3; col <= worksheet.Dimension.End.Column; ++col)
- {
- if (worksheet.Name.StartsWith("#"))
- {
- continue;
- }
-
- string fieldName = worksheet.Cells[row + 2, col].Text.Trim();
- if (fieldName == "")
- {
- continue;
- }
- if (classField.ContainsKey(fieldName))
- {
- continue;
- }
-
- string fieldCS = worksheet.Cells[row, col].Text.Trim().ToLower();
- if (fieldCS.Contains("#"))
- {
- classField[fieldName] = null;
- continue;
- }
- if (fieldCS == "")
- {
- fieldCS = "cs";
- }
- if (!fieldCS.Contains(configTypeStr))
- {
- classField[fieldName] = null;
- continue;
- }
-
- string fieldDesc = worksheet.Cells[row + 1, col].Text.Trim();
- string fieldType = worksheet.Cells[row + 3, col].Text.Trim();
- classField[fieldName] = new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType, col);
- }
- }
- static void ExportClass(string protoName, Dictionary<string, HeadInfo> classField, ConfigType configType)
- {
- string dir = GetClassDir(configType);
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
- string exportPath = Path.Combine(dir, $"{protoName}.cs");
-
- using FileStream txt = new FileStream(exportPath, FileMode.Create);
- using StreamWriter sw = new StreamWriter(txt);
-
- StringBuilder sb = new StringBuilder();
- foreach ((string _, HeadInfo headInfo) in classField)
- {
- if (headInfo == null)
- {
- continue;
- }
- sb.Append($"\t\t[ProtoMember({headInfo.FieldIndex - 2})]\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);
- }
- #endregion
- #region 导出json
- static void ExportExcelJson(ExcelPackage p, string name, Dictionary<string, HeadInfo> classField, ConfigType configType)
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("{\"list\":[");
- foreach (ExcelWorksheet worksheet in p.Workbook.Worksheets)
- {
- if (worksheet.Name.StartsWith("#"))
- {
- continue;
- }
- ExportSheetJson(worksheet, name, classField, configType, sb);
- }
- sb.AppendLine("]}");
-
- string dir = string.Format(jsonDir, configType.ToString());
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
-
- string jsonPath = Path.Combine(dir, $"{name}.txt");
- using FileStream txt = new FileStream(jsonPath, FileMode.Create);
- using StreamWriter sw = new StreamWriter(txt);
- sw.Write(sb.ToString());
- }
-
- static void ExportSheetJson(ExcelWorksheet worksheet, string name, Dictionary<string, HeadInfo> classField, ConfigType configType, StringBuilder sb)
- {
- string configTypeStr = configType.ToString();
- for (int row = 6; row <= worksheet.Dimension.End.Row; ++row)
- {
- string prefix = worksheet.Cells[row, 2].Text.Trim();
- if (prefix.Contains("#"))
- {
- continue;
- }
- if (prefix == "")
- {
- prefix = "cs";
- }
- if (!prefix.Contains(configTypeStr))
- {
- continue;
- }
-
- if (worksheet.Cells[row, 3].Text.Trim() == "")
- {
- continue;
- }
- sb.Append("{");
- sb.Append($"\"_t\":\"{name}\"");
- for (int col = 3; col <= worksheet.Dimension.End.Column; ++col)
- {
- string fieldName = worksheet.Cells[4, col].Text.Trim();
- if (!classField.ContainsKey(fieldName))
- {
- continue;
- }
- HeadInfo headInfo = classField[fieldName];
- if (headInfo == null)
- {
- continue;
- }
-
- string fieldN = headInfo.FieldName;
- if (fieldN == "Id")
- {
- fieldN = "_id";
- }
- sb.Append($",\"{fieldN}\":{Convert(headInfo.FieldType, worksheet.Cells[row, col].Text.Trim())}");
- }
- sb.Append("},\n");
- }
- }
-
- private static string Convert(string type, string value)
- {
- switch (type)
- {
- case "int[]":
- case "int32[]":
- case "long[]":
- return $"[{value}]";
- case "string[]":
- return $"[{value}]";
- case "int":
- case "int32":
- case "int64":
- case "long":
- case "float":
- case "double":
- if (value == "")
- {
- return "0";
- }
- return value;
- case "string":
- return $"\"{value}\"";
- default:
- throw new Exception($"不支持此类型: {type}");
- }
- }
- #endregion
- // 根据生成的类,动态编译把json转成protobuf
- private static void ExportExcelProtobuf(ConfigType configType)
- {
- string classPath = GetClassDir(configType);
- List<SyntaxTree> syntaxTrees = new List<SyntaxTree>();
- List<string> protoNames = new List<string>();
- foreach (string classFile in Directory.GetFiles(classPath, "*.cs"))
- {
- protoNames.Add(Path.GetFileNameWithoutExtension(classFile));
- syntaxTrees.Add(CSharpSyntaxTree.ParseText(File.ReadAllText(classFile)));
- }
-
- List<PortableExecutableReference> references = new List<PortableExecutableReference>();
- Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
- foreach (Assembly assembly in assemblies)
- {
- try
- {
- if (assembly.IsDynamic)
- {
- continue;
- }
- if (assembly.Location == "")
- {
- continue;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- throw;
- }
- PortableExecutableReference reference = MetadataReference.CreateFromFile(assembly.Location);
- references.Add(reference);
- }
- CSharpCompilation compilation = CSharpCompilation.Create(
- null,
- syntaxTrees.ToArray(),
- references.ToArray(),
- new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
- using MemoryStream memSteam = new MemoryStream();
-
- EmitResult emitResult = compilation.Emit(memSteam);
- if (!emitResult.Success)
- {
- StringBuilder stringBuilder = new StringBuilder();
- foreach (Diagnostic t in emitResult.Diagnostics)
- {
- stringBuilder.AppendLine(t.GetMessage());
- }
- throw new Exception($"动态编译失败:\n{stringBuilder}");
- }
-
- memSteam.Seek(0, SeekOrigin.Begin);
- Assembly ass = Assembly.Load(memSteam.ToArray());
- string dir = GetProtoDir(configType);
- if (!Directory.Exists(dir))
- {
- Directory.CreateDirectory(dir);
- }
-
- foreach (string protoName in protoNames)
- {
- Type type = ass.GetType($"ET.{protoName}Category");
- Type subType = ass.GetType($"ET.{protoName}");
- Serializer.NonGeneric.PrepareSerializer(type);
- Serializer.NonGeneric.PrepareSerializer(subType);
-
-
- string json = File.ReadAllText(Path.Combine(string.Format(jsonDir, configType), $"{protoName}.txt"));
- object deserialize = BsonSerializer.Deserialize(json, type);
- string path = Path.Combine(dir, $"{protoName}Category.bytes");
- using FileStream file = File.Create(path);
- Serializer.Serialize(file, deserialize);
- }
- }
- }
- }
|