|
|
@@ -1,8 +1,17 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
using System.IO;
|
|
|
+using System.Reflection;
|
|
|
using System.Text;
|
|
|
+using Microsoft.CodeAnalysis;
|
|
|
+using Microsoft.CodeAnalysis.CSharp;
|
|
|
+using Microsoft.CodeAnalysis.Emit;
|
|
|
+using MongoDB.Bson.Serialization;
|
|
|
+using MongoDB.Bson.Serialization.Attributes;
|
|
|
using OfficeOpenXml;
|
|
|
+using ProtoBuf;
|
|
|
+using LicenseContext = OfficeOpenXml.LicenseContext;
|
|
|
|
|
|
namespace ET
|
|
|
{
|
|
|
@@ -38,25 +47,39 @@ namespace ET
|
|
|
|
|
|
private const string jsonDir = "../../Generate/{0}/Json";
|
|
|
|
|
|
+ private const string protoDir = "../../Generate/{0}/Proto";
|
|
|
+
|
|
|
static void Main(string[] args)
|
|
|
{
|
|
|
- template = File.ReadAllText("Template.txt");
|
|
|
- ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
|
|
- foreach (string path in Directory.GetFiles(excelDir, "*.xlsx"))
|
|
|
+ try
|
|
|
{
|
|
|
- using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
- using ExcelPackage p = new ExcelPackage(stream);
|
|
|
- string name = Path.GetFileNameWithoutExtension(path);
|
|
|
+ template = File.ReadAllText("Template.txt");
|
|
|
+ ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
|
|
+ foreach (string path in Directory.GetFiles(excelDir, "*.xlsx"))
|
|
|
+ {
|
|
|
+ using Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
+ using ExcelPackage p = new ExcelPackage(stream);
|
|
|
+ string name = Path.GetFileNameWithoutExtension(path);
|
|
|
|
|
|
- ExportExcelClass(p, name, ConfigType.Client);
|
|
|
- ExportExcelClass(p, name, ConfigType.Server);
|
|
|
+ ExportExcelClass(p, name, ConfigType.Client);
|
|
|
+ ExportExcelClass(p, name, ConfigType.Server);
|
|
|
|
|
|
- ExportExcelJson(p, name, ConfigType.Client);
|
|
|
- ExportExcelJson(p, name, ConfigType.Server);
|
|
|
+ ExportExcelJson(p, name, ConfigType.Client);
|
|
|
+ ExportExcelJson(p, name, ConfigType.Server);
|
|
|
+ }
|
|
|
+
|
|
|
+ ExportExcelProtobuf(ConfigType.Client);
|
|
|
+ ExportExcelProtobuf(ConfigType.Server);
|
|
|
+
|
|
|
+ Console.WriteLine("导表成功!");
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ Console.WriteLine(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- #region 导出class
|
|
|
+#region 导出class
|
|
|
static void ExportExcelClass(ExcelPackage p, string name, ConfigType configType)
|
|
|
{
|
|
|
List<HeadInfo> classField = new List<HeadInfo>();
|
|
|
@@ -92,7 +115,12 @@ namespace ET
|
|
|
|
|
|
static void ExportClass(string protoName, List<HeadInfo> classField, ConfigType configType)
|
|
|
{
|
|
|
- string exportPath = Path.Combine(string.Format(classDir, configType.ToString()), $"{protoName}.cs");
|
|
|
+ string dir = string.Format(classDir, configType.ToString());
|
|
|
+ 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);
|
|
|
@@ -111,8 +139,9 @@ namespace ET
|
|
|
string content = template.Replace("(ConfigName)", protoName).Replace(("(Fields)"), sb.ToString());
|
|
|
sw.Write(content);
|
|
|
}
|
|
|
- #endregion
|
|
|
-
|
|
|
+#endregion
|
|
|
+
|
|
|
+#region 导出json
|
|
|
static void ExportExcelJson(ExcelPackage p, string name, ConfigType configType)
|
|
|
{
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
@@ -123,7 +152,13 @@ namespace ET
|
|
|
}
|
|
|
sb.AppendLine("]}");
|
|
|
|
|
|
- string jsonPath = Path.Combine(string.Format(jsonDir, configType.ToString()), $"{name}.txt");
|
|
|
+ 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());
|
|
|
@@ -132,22 +167,25 @@ namespace ET
|
|
|
static void ExportSheetJson(ExcelWorksheet worksheet, ConfigType configType, StringBuilder sb)
|
|
|
{
|
|
|
int infoRow = 2;
|
|
|
- List<HeadInfo> headInfos = new List<HeadInfo>();
|
|
|
- headInfos.Add(new HeadInfo());
|
|
|
- headInfos.Add(new HeadInfo());
|
|
|
- headInfos.Add(new HeadInfo());
|
|
|
+ HeadInfo[] headInfos = new HeadInfo[100];
|
|
|
for (int col = 3; col <= worksheet.Dimension.End.Column; ++col)
|
|
|
{
|
|
|
+ string fieldCS = worksheet.Cells[infoRow, col].Text.Trim();
|
|
|
+ if (fieldCS.Contains("#"))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
string fieldName = worksheet.Cells[infoRow + 2, col].Text.Trim();
|
|
|
if (fieldName == "")
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
- string fieldCS = worksheet.Cells[infoRow, col].Text.Trim();
|
|
|
+
|
|
|
string fieldDesc = worksheet.Cells[infoRow + 1, col].Text.Trim();
|
|
|
string fieldType = worksheet.Cells[infoRow + 3, col].Text.Trim();
|
|
|
|
|
|
- headInfos.Add(new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType));
|
|
|
+ headInfos[col] = new HeadInfo(fieldCS, fieldDesc, fieldName, fieldType);
|
|
|
}
|
|
|
|
|
|
for (int row = 6; row <= worksheet.Dimension.End.Row; ++row)
|
|
|
@@ -156,6 +194,10 @@ namespace ET
|
|
|
for (int col = 3; col < worksheet.Dimension.End.Column; ++col)
|
|
|
{
|
|
|
HeadInfo headInfo = headInfos[col];
|
|
|
+ if (headInfo.FieldAttribute == null)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
if (headInfo.FieldAttribute.Contains("#"))
|
|
|
{
|
|
|
continue;
|
|
|
@@ -198,5 +240,81 @@ namespace ET
|
|
|
throw new Exception($"不支持此类型: {type}");
|
|
|
}
|
|
|
}
|
|
|
+#endregion
|
|
|
+
|
|
|
+ // 根据生成的类,动态编译把json转成protobuf
|
|
|
+ private static void ExportExcelProtobuf(ConfigType configType)
|
|
|
+ {
|
|
|
+ string classPath = string.Format(classDir, 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>();
|
|
|
+
|
|
|
+ string assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(typeof(object).Assembly.Location).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(typeof(ProtoMemberAttribute).Assembly.Location).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(typeof(BsonDefaultValueAttribute).Assembly.Location).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(typeof(IConfig).Assembly.Location).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(typeof(Attribute).Assembly.Location).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "mscorlib.dll")).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "System.dll")).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "System.Core.dll")).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(Path.Combine(assemblyPath, "netstandard.dll")).GetReference());
|
|
|
+ references.Add(AssemblyMetadata.CreateFromFile(typeof(ISupportInitialize).Assembly.Location).GetReference());
|
|
|
+
|
|
|
+
|
|
|
+ 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 = string.Format(protoDir, configType.ToString());
|
|
|
+ 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}.bytes");
|
|
|
+
|
|
|
+ using FileStream file = File.Create(path);
|
|
|
+ Serializer.Serialize(file, deserialize);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|