提交导表相关功能
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
using System.Reflection;
|
||||
using Fantasy.Serialize;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using ProtoBuf;
|
||||
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 动态程序集类,用于加载动态生成的程序集并获取动态信息。
|
||||
/// </summary>
|
||||
public static class DynamicAssembly
|
||||
{
|
||||
private static void MetadataReference(out string assemblyName, out List<MetadataReference> metadataReferenceList)
|
||||
{
|
||||
AssemblyMetadata assemblyMetadata;
|
||||
MetadataReference metadataReference;
|
||||
var currentDomain = AppDomain.CurrentDomain;
|
||||
assemblyName = Path.GetRandomFileName();
|
||||
var assemblyArray = currentDomain.GetAssemblies();
|
||||
metadataReferenceList = new List<MetadataReference>();
|
||||
|
||||
// 注册引用
|
||||
|
||||
foreach (var domainAssembly in assemblyArray)
|
||||
{
|
||||
if (string.IsNullOrEmpty(domainAssembly.Location))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
assemblyMetadata = AssemblyMetadata.CreateFromFile(domainAssembly.Location);
|
||||
metadataReference = assemblyMetadata.GetReference();
|
||||
metadataReferenceList.Add(metadataReference);
|
||||
}
|
||||
|
||||
// 添加Proto支持
|
||||
|
||||
assemblyMetadata = AssemblyMetadata.CreateFromFile(typeof(ProtoMemberAttribute).Assembly.Location);
|
||||
metadataReference = assemblyMetadata.GetReference();
|
||||
metadataReferenceList.Add(metadataReference);
|
||||
|
||||
// 添加Fantasy支持
|
||||
|
||||
assemblyMetadata = AssemblyMetadata.CreateFromFile(typeof(ASerialize).Assembly.Location);
|
||||
metadataReference = assemblyMetadata.GetReference();
|
||||
metadataReferenceList.Add(metadataReference);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载指定路径下的动态程序集。
|
||||
/// </summary>
|
||||
/// <param name="path">程序集文件路径。</param>
|
||||
/// <returns>加载的动态程序集。</returns>
|
||||
public static Assembly Load(string path)
|
||||
{
|
||||
var fileList = new List<string>();
|
||||
|
||||
// 找到所有需要加载的CS文件
|
||||
|
||||
foreach (string file in Directory.GetFiles(path))
|
||||
{
|
||||
if (Path.GetExtension(file) != ".cs")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
fileList.Add(file);
|
||||
}
|
||||
|
||||
var syntaxTreeList = new List<SyntaxTree>();
|
||||
|
||||
foreach (var file in fileList)
|
||||
{
|
||||
using var fileStream = new StreamReader(file);
|
||||
var cSharp = CSharpSyntaxTree.ParseText(fileStream.ReadToEnd());
|
||||
syntaxTreeList.Add(cSharp);
|
||||
}
|
||||
|
||||
// 注册程序集
|
||||
MetadataReference(out var assemblyName, out var metadataReferenceList);
|
||||
var compilation = CSharpCompilation.Create(assemblyName, syntaxTreeList, metadataReferenceList, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
|
||||
using var ms = new MemoryStream();
|
||||
var result = compilation.Emit(ms);
|
||||
if (!result.Success)
|
||||
{
|
||||
foreach (var resultDiagnostic in result.Diagnostics)
|
||||
{
|
||||
Log.Error(resultDiagnostic.GetMessage());
|
||||
}
|
||||
|
||||
throw new Exception("failures");
|
||||
}
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
return Assembly.Load(ms.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取动态程序集中指定表格的动态信息。
|
||||
/// </summary>
|
||||
/// <param name="dynamicAssembly">动态程序集。</param>
|
||||
/// <param name="tableName">表格名称。</param>
|
||||
/// <returns>动态信息对象。</returns>
|
||||
public static DynamicConfigDataType GetDynamicInfo(Assembly dynamicAssembly, string tableName)
|
||||
{
|
||||
var dynamicConfigDataType = new DynamicConfigDataType
|
||||
{
|
||||
ConfigDataType = GetConfigType(dynamicAssembly, $"{tableName}Data"),
|
||||
ConfigType = GetConfigType(dynamicAssembly, $"{tableName}")
|
||||
};
|
||||
|
||||
dynamicConfigDataType.ConfigData = CreateInstance(dynamicConfigDataType.ConfigDataType);
|
||||
|
||||
var listPropertyType = dynamicConfigDataType.ConfigDataType.GetProperty("List");
|
||||
|
||||
if (listPropertyType == null)
|
||||
{
|
||||
throw new Exception("No Property named Add was found");
|
||||
}
|
||||
|
||||
dynamicConfigDataType.Obj = listPropertyType.GetValue(dynamicConfigDataType.ConfigData);
|
||||
dynamicConfigDataType.Method = listPropertyType.PropertyType.GetMethod("Add");
|
||||
|
||||
if (dynamicConfigDataType.Method == null)
|
||||
{
|
||||
throw new Exception("No method named Add was found");
|
||||
}
|
||||
|
||||
return dynamicConfigDataType;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据类型名称获取动态类型。
|
||||
/// </summary>
|
||||
/// <param name="dynamicAssembly">动态程序集。</param>
|
||||
/// <param name="typeName">类型名称。</param>
|
||||
/// <returns>动态类型。</returns>
|
||||
private static Type GetConfigType(Assembly dynamicAssembly, string typeName)
|
||||
{
|
||||
var configType = dynamicAssembly.GetType($"Fantasy.{typeName}");
|
||||
|
||||
if (configType == null)
|
||||
{
|
||||
throw new FileNotFoundException($"Fantasy.{typeName} not found");
|
||||
}
|
||||
|
||||
return configType;
|
||||
// return dynamicAssembly.GetType($"Fantasy.{typeName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建动态实例。
|
||||
/// </summary>
|
||||
/// <param name="configType">动态类型。</param>
|
||||
/// <returns>动态实例。</returns>
|
||||
public static object CreateInstance(Type configType)
|
||||
{
|
||||
var config = Activator.CreateInstance(configType);
|
||||
|
||||
if (config == null)
|
||||
{
|
||||
throw new Exception($"{configType.Name} is Activator.CreateInstance error");
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// 动态配置数据类型类,用于存储动态配置数据的相关信息。
|
||||
/// </summary>
|
||||
public class DynamicConfigDataType
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置数据对象,继承自 AProto 基类。
|
||||
/// </summary>
|
||||
public object ConfigData;
|
||||
/// <summary>
|
||||
/// 配置数据类型。
|
||||
/// </summary>
|
||||
public Type ConfigDataType;
|
||||
/// <summary>
|
||||
/// 配置类型。
|
||||
/// </summary>
|
||||
public Type ConfigType;
|
||||
/// <summary>
|
||||
/// 反射方法信息,用于调用特定方法。
|
||||
/// </summary>
|
||||
public MethodInfo Method;
|
||||
/// <summary>
|
||||
/// 配置数据对象实例。
|
||||
/// </summary>
|
||||
public object Obj;
|
||||
/// <summary>
|
||||
/// 用于生成 JSON 格式数据的字符串构建器。
|
||||
/// </summary>
|
||||
public StringBuilder Json = new StringBuilder();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System.Reflection;
|
||||
using Fantasy.Serialize;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using ProtoBuf;
|
||||
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
public class OneDynamicAssembly
|
||||
{
|
||||
private readonly List<SyntaxTree> _syntaxTreeList = new List<SyntaxTree>();
|
||||
|
||||
public void Load(string file)
|
||||
{
|
||||
using var fileStream = new StreamReader(file);
|
||||
var cSharp = CSharpSyntaxTree.ParseText(fileStream.ReadToEnd());
|
||||
_syntaxTreeList.Add(cSharp);
|
||||
}
|
||||
|
||||
public Assembly Assembly
|
||||
{
|
||||
get
|
||||
{
|
||||
AssemblyMetadata assemblyMetadata;
|
||||
MetadataReference metadataReference;
|
||||
var currentDomain = AppDomain.CurrentDomain;
|
||||
var assemblyName = Path.GetRandomFileName();
|
||||
var assemblyArray = currentDomain.GetAssemblies();
|
||||
var metadataReferenceList = new List<MetadataReference>();
|
||||
// 注册引用
|
||||
foreach (var domainAssembly in assemblyArray)
|
||||
{
|
||||
if (string.IsNullOrEmpty(domainAssembly.Location))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
assemblyMetadata = AssemblyMetadata.CreateFromFile(domainAssembly.Location);
|
||||
metadataReference = assemblyMetadata.GetReference();
|
||||
metadataReferenceList.Add(metadataReference);
|
||||
}
|
||||
// 添加ProtoEntity支持
|
||||
assemblyMetadata = AssemblyMetadata.CreateFromFile(typeof(ASerialize).Assembly.Location);
|
||||
metadataReference = assemblyMetadata.GetReference();
|
||||
metadataReferenceList.Add(metadataReference);
|
||||
// 添加MessagePack支持
|
||||
assemblyMetadata = AssemblyMetadata.CreateFromFile(typeof(ProtoMemberAttribute).Assembly.Location);
|
||||
metadataReference = assemblyMetadata.GetReference();
|
||||
metadataReferenceList.Add(metadataReference);
|
||||
CSharpCompilation compilation = CSharpCompilation.Create(assemblyName, _syntaxTreeList, metadataReferenceList, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
|
||||
|
||||
using var ms = new MemoryStream();
|
||||
var result = compilation.Emit(ms);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
foreach (var resultDiagnostic in result.Diagnostics)
|
||||
{
|
||||
Log.Error(resultDiagnostic.GetMessage());
|
||||
}
|
||||
|
||||
throw new Exception("failures");
|
||||
}
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
return Assembly.Load(ms.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
950
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExcelExporter.cs
Normal file
950
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExcelExporter.cs
Normal file
@@ -0,0 +1,950 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.VisualBasic.ApplicationServices;
|
||||
using Newtonsoft.Json;
|
||||
using OfficeOpenXml;
|
||||
using static System.String;
|
||||
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
using TableDictionary = SortedDictionary<string, List<int>>;
|
||||
|
||||
/// <summary>
|
||||
/// Excel 数据导出器,用于从 Excel 文件导出数据到二进制格式和生成 C# 类文件。
|
||||
/// </summary>
|
||||
public sealed class ExcelExporter
|
||||
{
|
||||
public ExportType ExportType;
|
||||
private readonly string _excelProgramPath;
|
||||
private readonly string _versionFilePath;
|
||||
|
||||
// private readonly string _excelClientFileDirectory;
|
||||
// private readonly string _excelServerFileDirectory;
|
||||
// private readonly string _excelServerJsonDirectory;
|
||||
// private readonly string _excelClientJsonDirectory;
|
||||
|
||||
public VersionInfo VersionInfo; // 存储 Excel 文件的版本信息。
|
||||
private readonly Regex _regexName = new Regex("^[a-zA-Z][a-zA-Z0-9_]*$"); // 用于验证 Excel 表名的正则表达式。
|
||||
private readonly HashSet<string> _loadFiles = [".xlsx", ".xlsm", ".csv"]; // 加载的支持文件扩展名。
|
||||
|
||||
private readonly OneToManyList<string, ExportInfo>
|
||||
_tables = new OneToManyList<string, ExportInfo>(); // 存储 Excel 表及其导出信息。
|
||||
|
||||
private readonly ConcurrentDictionary<string, ExcelTable> _excelTables =
|
||||
new ConcurrentDictionary<string, ExcelTable>(); // 存储解析后的 Excel 表。
|
||||
|
||||
public readonly ConcurrentDictionary<string, ExcelWorksheet> Worksheets =
|
||||
new ConcurrentDictionary<string, ExcelWorksheet>(); // 存储已加载的 Excel 工作表。
|
||||
|
||||
public readonly Dictionary<string, string> IgnoreTable = new Dictionary<string, string>(); // 存储以#开头的的表和路径
|
||||
|
||||
/// <summary>
|
||||
/// 导表支持的数据类型集合。
|
||||
/// </summary>
|
||||
public static readonly HashSet<string> ColTypeSet =
|
||||
[
|
||||
"", "0", "bool", "byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "string",
|
||||
"IntDictionaryConfig", "StringDictionaryConfig",
|
||||
"short[]", "int[]", "long[]", "float[]", "string[]", "uint[]"
|
||||
];
|
||||
|
||||
static ExcelExporter()
|
||||
{
|
||||
ExcelPackage.License.SetNonCommercialOrganization("Fantasy");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据指定的 exportType 初始化 ExcelExporter 类的新实例。
|
||||
/// </summary>
|
||||
/// <param name="exportType">要执行的导出类型(AllExcel 或 AllExcelIncrement)。</param>
|
||||
public ExcelExporter(ExportType exportType)
|
||||
{
|
||||
ExportType = exportType;
|
||||
|
||||
if (App.Config.ExcelVersionPath == null || App.Config.ExcelVersionPath.Trim() == "")
|
||||
{
|
||||
Log.Info($"ExcelVersionFile Can not be empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (App.Config.ExcelPath == null || App.Config.ExcelPath.Trim() == "")
|
||||
{
|
||||
Log.Info($"ExcelProgramPath Can not be empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
_excelProgramPath = FileHelper.GetFullPath(App.Config.ExcelPath);
|
||||
_versionFilePath = FileHelper.GetFullPath(App.Config.ExcelVersionPath);
|
||||
|
||||
if (App.Config.GenClient)
|
||||
{
|
||||
if (App.Config.ClientJsonPath == null ||
|
||||
App.Config.ClientJsonPath.Trim() == "")
|
||||
{
|
||||
Log.Info($"ExcelClientJsonDirectory Can not be empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (App.Config.ClientPath == null ||
|
||||
App.Config.ClientPath.Trim() == "")
|
||||
{
|
||||
Log.Info($"ExcelServerFileDirectory Can not be empty!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (App.Config.GenServer)
|
||||
{
|
||||
if (App.Config.ServerPath == null ||
|
||||
App.Config.ServerPath.Trim() == "")
|
||||
{
|
||||
Log.Info($"ExcelServerFileDirectory Can not be empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (App.Config.ServerJsonPath == null ||
|
||||
App.Config.ServerJsonPath.Trim() == "")
|
||||
{
|
||||
Log.Info($"ExcelServerJsonDirectory Can not be empty!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
switch (ExportType)
|
||||
{
|
||||
case ExportType.AllExcelIncrement:
|
||||
{
|
||||
break;
|
||||
}
|
||||
case ExportType.AllExcel:
|
||||
{
|
||||
if (File.Exists(_versionFilePath))
|
||||
{
|
||||
File.Delete(_versionFilePath);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// SerializerManager.Initialize();
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
Find();
|
||||
Parsing();
|
||||
ExportToBinary();
|
||||
File.WriteAllText(_versionFilePath, JsonConvert.SerializeObject(VersionInfo));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找配置文件
|
||||
/// </summary>
|
||||
private void Find()
|
||||
{
|
||||
VersionInfo = File.Exists(_versionFilePath)
|
||||
? JsonConvert.DeserializeObject<VersionInfo>(File.ReadAllText(_versionFilePath))
|
||||
: new VersionInfo();
|
||||
|
||||
var dir = new DirectoryInfo(_excelProgramPath);
|
||||
var excelFiles = dir.GetFiles("*", SearchOption.AllDirectories);
|
||||
|
||||
if (excelFiles.Length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var excelFile in excelFiles)
|
||||
{
|
||||
// 过滤掉非指定后缀的文件
|
||||
|
||||
if (!_loadFiles.Contains(excelFile.Extension))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var lastIndexOf = excelFile.Name.LastIndexOf(".", StringComparison.Ordinal);
|
||||
|
||||
if (lastIndexOf < 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var fullName = excelFile.FullName;
|
||||
var excelName = excelFile.Name.Substring(0, lastIndexOf);
|
||||
var path = fullName.Substring(0, fullName.Length - excelFile.Name.Length);
|
||||
|
||||
// 过滤~开头文件
|
||||
|
||||
if (excelName.StartsWith("~", StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果文件名以#开头,那么这个文件夹下的所有文件都不导出
|
||||
|
||||
if (excelName.StartsWith("#", StringComparison.Ordinal))
|
||||
{
|
||||
IgnoreTable.Add(excelName, fullName);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果文件夹名包含#,那么这个文件夹下的所有文件都不导出
|
||||
|
||||
if (path.Contains("#", StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_regexName.IsMatch(excelName))
|
||||
{
|
||||
Log.Info($"{excelName} 配置文件名非法");
|
||||
continue;
|
||||
}
|
||||
|
||||
var exportInfo = new ExportInfo()
|
||||
{
|
||||
Name = excelName, FileInfo = excelFile
|
||||
};
|
||||
|
||||
_tables.Add(excelName.Split('_')[0], exportInfo);
|
||||
}
|
||||
|
||||
var removeTables = new List<string>();
|
||||
|
||||
foreach (var (tableName, tableList) in _tables)
|
||||
{
|
||||
var isNeedExport = false;
|
||||
|
||||
foreach (var exportInfo in tableList)
|
||||
{
|
||||
var key = HashCodeHelper.ComputeHash64(exportInfo.Name);
|
||||
var timer = TimeHelper.Transition(exportInfo.FileInfo.LastWriteTime);
|
||||
|
||||
if (!isNeedExport)
|
||||
{
|
||||
if (VersionInfo.Tables.TryGetValue(key, out var lastWriteTime))
|
||||
{
|
||||
isNeedExport = lastWriteTime != timer;
|
||||
}
|
||||
else
|
||||
{
|
||||
isNeedExport = true;
|
||||
}
|
||||
}
|
||||
|
||||
VersionInfo.Tables[key] = timer;
|
||||
}
|
||||
|
||||
if (!isNeedExport)
|
||||
{
|
||||
removeTables.Add(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var removeTable in removeTables)
|
||||
{
|
||||
_tables.Remove(removeTable);
|
||||
}
|
||||
|
||||
foreach (var (_, exportInfo) in _tables)
|
||||
{
|
||||
exportInfo.Sort((x, y) => Compare(x.Name, y.Name, StringComparison.Ordinal));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成配置文件
|
||||
/// </summary>
|
||||
private void Parsing()
|
||||
{
|
||||
var generateTasks = new List<Task>();
|
||||
|
||||
foreach (var (tableName, tableList) in _tables)
|
||||
{
|
||||
var task = Task.Run(() =>
|
||||
{
|
||||
var writeToClassTask = new List<Task>();
|
||||
var excelTable = new ExcelTable(tableName);
|
||||
|
||||
// 筛选需要导出的列
|
||||
foreach (var exportInfo in tableList)
|
||||
{
|
||||
try
|
||||
{
|
||||
var serverColInfoList = new List<int>();
|
||||
var clientColInfoList = new List<int>();
|
||||
var worksheet = LoadExcel(exportInfo.FileInfo.FullName, true);
|
||||
|
||||
for (var col = 3; col <= worksheet.Columns.EndColumn; col++)
|
||||
{
|
||||
// 列名字第一个字符是#不参与导出
|
||||
|
||||
var colName = worksheet.GetCellValue(5, col);
|
||||
if (colName.StartsWith("#", StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 数值列不参与导出
|
||||
|
||||
var numericalCol = worksheet.GetCellValue(3, col);
|
||||
if (numericalCol != "" && numericalCol != "0")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var serverType = worksheet.GetCellValue(1, col);
|
||||
var clientType = worksheet.GetCellValue(2, col);
|
||||
var isExportServer = !IsNullOrEmpty(serverType) && serverType != "0";
|
||||
var isExportClient = !IsNullOrEmpty(clientType) && clientType != "0";
|
||||
|
||||
if (!isExportServer && !isExportClient)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isExportServer && isExportClient & serverType != clientType)
|
||||
{
|
||||
Log.Info(
|
||||
$"配置表 {exportInfo.Name} {col} 列 [{colName}] 客户端类型 {clientType} 和 服务端类型 {serverType} 不一致");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ColTypeSet.Contains(serverType) || !ColTypeSet.Contains(clientType))
|
||||
{
|
||||
Log.Info(
|
||||
$"配置表 {exportInfo.Name} {col} 列 [{colName}] 客户端类型 {clientType}, 服务端类型 {serverType} 不合法");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!_regexName.IsMatch(colName))
|
||||
{
|
||||
Log.Info($"配置表 {exportInfo.Name} {col} 列 [{colName}] 列名非法");
|
||||
continue;
|
||||
}
|
||||
|
||||
serverColInfoList.Add(col);
|
||||
|
||||
if (isExportClient)
|
||||
{
|
||||
clientColInfoList.Add(col);
|
||||
}
|
||||
}
|
||||
|
||||
if (clientColInfoList.Count > 0)
|
||||
{
|
||||
excelTable.ClientColInfos.Add(exportInfo.FileInfo.FullName, clientColInfoList);
|
||||
}
|
||||
|
||||
if (serverColInfoList.Count > 0)
|
||||
{
|
||||
excelTable.ServerColInfos.Add(exportInfo.FileInfo.FullName, serverColInfoList);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"Config : {tableName}, Name : {exportInfo.Name}, Error : {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// 生成cs文件
|
||||
|
||||
if (App.Config.GenServer)
|
||||
{
|
||||
writeToClassTask.Add(Task.Run(() =>
|
||||
{
|
||||
WriteToClass(excelTable.ServerColInfos, App.Config.ServerPath, true);
|
||||
}));
|
||||
}
|
||||
|
||||
if (App.Config.GenClient)
|
||||
{
|
||||
writeToClassTask.Add(Task.Run(() =>
|
||||
{
|
||||
WriteToClass(excelTable.ClientColInfos, App.Config.ClientPath, false);
|
||||
}));
|
||||
}
|
||||
|
||||
Task.WaitAll(writeToClassTask.ToArray());
|
||||
_excelTables.TryAdd(tableName, excelTable);
|
||||
});
|
||||
|
||||
generateTasks.Add(task);
|
||||
}
|
||||
|
||||
Task.WaitAll(generateTasks.ToArray());
|
||||
Console.WriteLine("build success===");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把数据和实体类转换二进制导出到文件中
|
||||
/// </summary>
|
||||
private void ExportToBinary()
|
||||
{
|
||||
System.Reflection.Assembly dynamicServerAssembly = null;
|
||||
System.Reflection.Assembly dynamicClientAssembly = null;
|
||||
var exportToBinaryTasks = new List<Task>();
|
||||
|
||||
if (App.Config.GenServer &&
|
||||
Directory.Exists(App.Config.ServerPath))
|
||||
{
|
||||
dynamicServerAssembly = DynamicAssembly.Load(App.Config.ServerPath);
|
||||
}
|
||||
|
||||
if (App.Config.GenClient &&
|
||||
Directory.Exists(App.Config.ClientPath))
|
||||
{
|
||||
dynamicClientAssembly = DynamicAssembly.Load(App.Config.ClientPath);
|
||||
}
|
||||
|
||||
foreach (var (tableName, tableList) in _tables)
|
||||
{
|
||||
var task = Task.Run(() =>
|
||||
{
|
||||
DynamicConfigDataType serverDynamicInfo = null;
|
||||
DynamicConfigDataType clientDynamicInfo = null;
|
||||
|
||||
var idCheck = new HashSet<string>();
|
||||
var excelTable = _excelTables[tableName];
|
||||
var csName = Path.GetFileNameWithoutExtension(tableName);
|
||||
|
||||
if (App.Config.GenServer)
|
||||
{
|
||||
var serverColInfoCount = excelTable.ServerColInfos.Sum(d => d.Value.Count);
|
||||
serverDynamicInfo = serverColInfoCount == 0
|
||||
? null
|
||||
: DynamicAssembly.GetDynamicInfo(dynamicServerAssembly, csName);
|
||||
}
|
||||
|
||||
if (App.Config.GenClient)
|
||||
{
|
||||
var clientColInfoCount = excelTable.ClientColInfos.Sum(d => d.Value.Count);
|
||||
clientDynamicInfo = clientColInfoCount == 0
|
||||
? null
|
||||
: DynamicAssembly.GetDynamicInfo(dynamicClientAssembly, csName);
|
||||
}
|
||||
|
||||
for (var i = 0; i < tableList.Count; i++)
|
||||
{
|
||||
var tableListName = tableList[i];
|
||||
|
||||
try
|
||||
{
|
||||
var fileInfoFullName = tableListName.FileInfo.FullName;
|
||||
var excelWorksheet = LoadExcel(fileInfoFullName, false);
|
||||
var rows = excelWorksheet.Dimension.Rows;
|
||||
excelTable.ServerColInfos.TryGetValue(fileInfoFullName, out var serverCols);
|
||||
excelTable.ClientColInfos.TryGetValue(fileInfoFullName, out var clientCols);
|
||||
|
||||
for (var row = 7; row <= rows; row++)
|
||||
{
|
||||
if (excelWorksheet.GetCellValue(row, 1).StartsWith("#", StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var id = excelWorksheet.GetCellValue(row, 3);
|
||||
|
||||
if (idCheck.Contains(id))
|
||||
{
|
||||
Log.Info($"{tableListName.Name} 存在重复Id {id} 行号 {row}");
|
||||
continue;
|
||||
}
|
||||
|
||||
idCheck.Add(id);
|
||||
var isLast = row == rows && (i == tableList.Count - 1);
|
||||
|
||||
if (App.Config.GenServer)
|
||||
{
|
||||
GenerateBinary(fileInfoFullName, excelWorksheet, serverDynamicInfo, serverCols, id, row,
|
||||
isLast, true);
|
||||
}
|
||||
|
||||
if (App.Config.GenClient)
|
||||
{
|
||||
GenerateBinary(fileInfoFullName, excelWorksheet, clientDynamicInfo, clientCols, id, row,
|
||||
isLast, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error($"Table:{tableListName} error! \n{e}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
if (serverDynamicInfo?.ConfigData != null)
|
||||
{
|
||||
// var memoryStream = new MemoryStreamBuffer();
|
||||
// SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf)
|
||||
// .Serialize(serverDynamicInfo.ConfigData, memoryStream);
|
||||
// if (!Directory.Exists(_excelServerBinaryDirectory))
|
||||
// {
|
||||
// Directory.CreateDirectory(_excelServerBinaryDirectory);
|
||||
// }
|
||||
|
||||
// var asSpan = memoryStream.GetBuffer().AsSpan(0, (int)memoryStream.Position);
|
||||
// File.WriteAllBytes(Path.Combine(_excelServerBinaryDirectory, $"{csName}Data.bytes"),
|
||||
// asSpan.ToArray());
|
||||
|
||||
if (serverDynamicInfo.Json.Length > 0)
|
||||
{
|
||||
if (!Directory.Exists(App.Config.ServerJsonPath))
|
||||
{
|
||||
Directory.CreateDirectory(App.Config.ServerJsonPath);
|
||||
}
|
||||
|
||||
using var sw = new StreamWriter(Path.Combine(App.Config.ServerJsonPath, $"{csName}Data.Json"));
|
||||
sw.WriteLine("{\"List\":[");
|
||||
sw.Write(serverDynamicInfo.Json.ToString());
|
||||
sw.WriteLine("]}");
|
||||
}
|
||||
}
|
||||
|
||||
if (clientDynamicInfo?.ConfigData != null)
|
||||
{
|
||||
// var memoryStream = new MemoryStreamBuffer();
|
||||
// SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf)
|
||||
// .Serialize(clientDynamicInfo.ConfigData, memoryStream);
|
||||
// if (!Directory.Exists(_excelClientBinaryDirectory))
|
||||
// {
|
||||
// Directory.CreateDirectory(_excelClientBinaryDirectory);
|
||||
// }
|
||||
//
|
||||
// var asSpan = memoryStream.GetBuffer().AsSpan(0, (int)memoryStream.Position);
|
||||
// File.WriteAllBytes(Path.Combine(_excelClientBinaryDirectory, $"{csName}Data.bytes"),
|
||||
// asSpan.ToArray());
|
||||
|
||||
if (clientDynamicInfo.Json.Length > 0)
|
||||
{
|
||||
if (!Directory.Exists(App.Config.ClientJsonPath))
|
||||
{
|
||||
Directory.CreateDirectory(App.Config.ClientJsonPath);
|
||||
}
|
||||
|
||||
using var sw = new StreamWriter(Path.Combine(App.Config.ClientJsonPath, $"{csName}Data.Json"));
|
||||
sw.WriteLine("{\"List\":[");
|
||||
sw.Write(clientDynamicInfo.Json.ToString());
|
||||
sw.WriteLine("]}");
|
||||
}
|
||||
}
|
||||
});
|
||||
exportToBinaryTasks.Add(task);
|
||||
}
|
||||
|
||||
Task.WaitAll(exportToBinaryTasks.ToArray());
|
||||
}
|
||||
|
||||
private void GenerateBinary(string fileInfoFullName, ExcelWorksheet excelWorksheet,
|
||||
DynamicConfigDataType dynamicInfo, List<int> cols, string id, int row, bool isLast, bool isServer)
|
||||
{
|
||||
if (cols == null || IsNullOrEmpty(id) || cols.Count <= 0 || dynamicInfo?.ConfigType == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var config = DynamicAssembly.CreateInstance(dynamicInfo.ConfigType);
|
||||
|
||||
for (var i = 0; i < cols.Count; i++)
|
||||
{
|
||||
string colType;
|
||||
var colIndex = cols[i];
|
||||
var colName = excelWorksheet.GetCellValue(5, colIndex);
|
||||
var value = excelWorksheet.GetCellValue(row, colIndex);
|
||||
|
||||
if (isServer)
|
||||
{
|
||||
colType = excelWorksheet.GetCellValue(1, colIndex);
|
||||
|
||||
if (IsNullOrEmpty(colType) || colType == "0")
|
||||
{
|
||||
colType = excelWorksheet.GetCellValue(2, colIndex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
colType = excelWorksheet.GetCellValue(2, colIndex);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SetNewValue(dynamicInfo.ConfigType.GetProperty(colName), config, colType, value);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(
|
||||
$"Error Table {fileInfoFullName} Col:{colName} colType:{colType} Row:{row} value:{value} {e}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
dynamicInfo.Method.Invoke(dynamicInfo.Obj, new object[] { config });
|
||||
|
||||
var json = JsonConvert.SerializeObject(config);
|
||||
|
||||
if (isLast)
|
||||
{
|
||||
dynamicInfo.Json.AppendLine(json);
|
||||
}
|
||||
else
|
||||
{
|
||||
dynamicInfo.Json.AppendLine($"{json},");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 从 Excel 文件加载工作表并返回 ExcelWorksheet 对象。
|
||||
/// </summary>
|
||||
/// <param name="name">工作表的名称或文件路径。</param>
|
||||
/// <param name="isAddToDic">是否将加载的工作表添加到缓存字典中。</param>
|
||||
/// <returns>表示 Excel 工作表的 ExcelWorksheet 对象。</returns>
|
||||
public ExcelWorksheet LoadExcel(string name, bool isAddToDic)
|
||||
{
|
||||
if (Worksheets.TryGetValue(name, out var worksheet))
|
||||
{
|
||||
return worksheet;
|
||||
}
|
||||
|
||||
var workbookWorksheets = ExcelHelper.LoadExcel(name).Workbook.Worksheets;
|
||||
worksheet = workbookWorksheets[0];
|
||||
|
||||
if (isAddToDic)
|
||||
{
|
||||
Worksheets.TryAdd(name, worksheet);
|
||||
|
||||
foreach (var workbookWorksheet in workbookWorksheets)
|
||||
{
|
||||
try
|
||||
{
|
||||
var hash = HashCodeHelper.ComputeHash64(workbookWorksheet.Name);
|
||||
VersionInfo.WorksheetNames.Add(hash);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
|
||||
Worksheets.TryAdd(workbookWorksheet.Name, workbookWorksheet);
|
||||
}
|
||||
}
|
||||
|
||||
Log.Info(name);
|
||||
return workbookWorksheets[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入到cs
|
||||
/// </summary>
|
||||
/// <param name="colInfos"></param>
|
||||
/// <param name="exportPath"></param>
|
||||
/// <param name="isServer"></param>
|
||||
private void WriteToClass(TableDictionary colInfos, string exportPath, bool isServer)
|
||||
{
|
||||
if (colInfos.Count <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var index = 0;
|
||||
var fileBuilder = new StringBuilder();
|
||||
var colNameSet = new HashSet<string>();
|
||||
|
||||
if (colInfos.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var csName = Path.GetFileNameWithoutExtension(colInfos.First().Key)?.Split('_')[0];
|
||||
|
||||
foreach (var (tableName, cols) in colInfos)
|
||||
{
|
||||
if (cols == null || cols.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var excelWorksheet = LoadExcel(tableName, false);
|
||||
|
||||
foreach (var colIndex in cols)
|
||||
{
|
||||
var colName = excelWorksheet.GetCellValue(5, colIndex);
|
||||
|
||||
if (colNameSet.Contains(colName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
colNameSet.Add(colName);
|
||||
|
||||
string colType;
|
||||
|
||||
if (isServer)
|
||||
{
|
||||
colType = excelWorksheet.GetCellValue(1, colIndex);
|
||||
|
||||
if (IsNullOrEmpty(colType) || colType == "0")
|
||||
{
|
||||
colType = excelWorksheet.GetCellValue(2, colIndex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
colType = excelWorksheet.GetCellValue(2, colIndex);
|
||||
}
|
||||
|
||||
var remarks = excelWorksheet.GetCellValue(4, colIndex);
|
||||
|
||||
// 解决不同平台换行符不一致的问题
|
||||
|
||||
switch (Environment.OSVersion.Platform)
|
||||
{
|
||||
case PlatformID.Win32NT:
|
||||
case PlatformID.Win32S:
|
||||
case PlatformID.Win32Windows:
|
||||
case PlatformID.WinCE:
|
||||
{
|
||||
fileBuilder.Append($"\r\n\t\t[ProtoMember({++index})]\r\n");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
fileBuilder.Append($"\n\t\t[ProtoMember({++index})]\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fileBuilder.Append(
|
||||
IsArray(colType, out var t)
|
||||
? $"\t\tpublic {colType} {colName} {{ get; set; }} = Array.Empty<{t}>(); // {remarks}"
|
||||
: $"\t\tpublic {colType} {colName} {{ get; set; }} // {remarks}");
|
||||
}
|
||||
}
|
||||
|
||||
var template = ExcelTemplate.Template;
|
||||
|
||||
if (fileBuilder.Length > 0)
|
||||
{
|
||||
if (!Directory.Exists(exportPath))
|
||||
{
|
||||
FileHelper.CreateDirectory(exportPath);
|
||||
}
|
||||
|
||||
var content = template.Replace("(namespace)", "Fantasy")
|
||||
.Replace("(ConfigName)", csName)
|
||||
.Replace("(Fields)", fileBuilder.ToString());
|
||||
File.WriteAllText(Path.Combine(exportPath, $"{csName}.cs"), content);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetNewValue(PropertyInfo propertyInfo, object config, string type, string value)
|
||||
{
|
||||
if (IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "short":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToInt16(value));
|
||||
return;
|
||||
}
|
||||
case "ushort":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToUInt16(value));
|
||||
return;
|
||||
}
|
||||
case "uint":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToUInt32(value));
|
||||
return;
|
||||
}
|
||||
case "int":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToInt32(value));
|
||||
return;
|
||||
}
|
||||
case "decimal":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToDecimal(value));
|
||||
return;
|
||||
}
|
||||
case "string":
|
||||
{
|
||||
try
|
||||
{
|
||||
propertyInfo.SetValue(config, value);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "bool":
|
||||
{
|
||||
// 空字符串
|
||||
|
||||
value = value.ToLower();
|
||||
|
||||
if (IsNullOrEmpty(value))
|
||||
{
|
||||
propertyInfo.SetValue(config, false);
|
||||
}
|
||||
else if (bool.TryParse(value, out bool b))
|
||||
{
|
||||
propertyInfo.SetValue(config, b);
|
||||
}
|
||||
else if (int.TryParse(value, out int v))
|
||||
{
|
||||
propertyInfo.SetValue(config, v != 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
propertyInfo.SetValue(config, false);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "ulong":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToUInt64(value));
|
||||
return;
|
||||
}
|
||||
case "long":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToInt64(value));
|
||||
return;
|
||||
}
|
||||
case "double":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToDouble(value));
|
||||
return;
|
||||
}
|
||||
case "float":
|
||||
{
|
||||
propertyInfo.SetValue(config, Convert.ToSingle(value));
|
||||
return;
|
||||
}
|
||||
case "int32[]":
|
||||
case "int[]":
|
||||
{
|
||||
if (value != "0")
|
||||
{
|
||||
propertyInfo.SetValue(config, value.Split(",").Select(d => Convert.ToInt32(d)).ToArray());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "uint[]":
|
||||
{
|
||||
if (value != "0")
|
||||
{
|
||||
propertyInfo.SetValue(config, value.Split(",").Select(d => Convert.ToUInt32(d)).ToArray());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "long[]":
|
||||
{
|
||||
if (value != "0")
|
||||
{
|
||||
propertyInfo.SetValue(config, value.Split(",").Select(d => Convert.ToInt64(d)).ToArray());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "double[]":
|
||||
{
|
||||
if (value != "0")
|
||||
{
|
||||
propertyInfo.SetValue(config, value.Split(",").Select(d => Convert.ToDouble(d)).ToArray());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "string[]":
|
||||
{
|
||||
if (value == "0")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var list = value.Split(",").ToArray();
|
||||
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
list[i] = list[i].Replace("\"", "");
|
||||
}
|
||||
|
||||
propertyInfo.SetValue(config, value.Split(",").ToArray());
|
||||
|
||||
return;
|
||||
}
|
||||
case "float[]":
|
||||
{
|
||||
if (value != "0")
|
||||
{
|
||||
propertyInfo.SetValue(config, value.Split(",").Select(d => Convert.ToSingle(d)).ToArray());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
case "IntDictionaryConfig":
|
||||
{
|
||||
if (value.Trim() == "" || value.Trim() == "{}")
|
||||
{
|
||||
propertyInfo.SetValue(config, null);
|
||||
return;
|
||||
}
|
||||
|
||||
var attr = new IntDictionaryConfig { Dic = JsonConvert.DeserializeObject<Dictionary<int, int>>(value) };
|
||||
|
||||
propertyInfo.SetValue(config, attr);
|
||||
|
||||
return;
|
||||
}
|
||||
case "StringDictionaryConfig":
|
||||
{
|
||||
if (value.Trim() == "" || value.Trim() == "{}")
|
||||
{
|
||||
propertyInfo.SetValue(config, null);
|
||||
return;
|
||||
}
|
||||
|
||||
var attr = new StringDictionaryConfig
|
||||
{ Dic = JsonConvert.DeserializeObject<Dictionary<int, string>>(value) };
|
||||
|
||||
propertyInfo.SetValue(config, attr);
|
||||
|
||||
return;
|
||||
}
|
||||
default:
|
||||
throw new NotSupportedException($"不支持此类型: {type}");
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsArray(string type, out string t)
|
||||
{
|
||||
t = null;
|
||||
var index = type.IndexOf("[]", StringComparison.Ordinal);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
t = type.Remove(index, 2);
|
||||
}
|
||||
|
||||
return index >= 0;
|
||||
}
|
||||
}
|
||||
47
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExcelHelper.cs
Normal file
47
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExcelHelper.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using OfficeOpenXml;
|
||||
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// 提供操作 Excel 文件的辅助方法。
|
||||
/// </summary>
|
||||
public static class ExcelHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 加载 Excel 文件并返回 ExcelPackage 实例。
|
||||
/// </summary>
|
||||
/// <param name="name">Excel 文件的路径。</param>
|
||||
/// <returns>ExcelPackage 实例。</returns>
|
||||
public static ExcelPackage LoadExcel(string name)
|
||||
{
|
||||
return new ExcelPackage(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定工作表中指定行列位置的单元格值。
|
||||
/// </summary>
|
||||
/// <param name="sheet">Excel 工作表。</param>
|
||||
/// <param name="row">行索引。</param>
|
||||
/// <param name="column">列索引。</param>
|
||||
/// <returns>单元格值。</returns>
|
||||
public static string GetCellValue(this ExcelWorksheet sheet, int row, int column)
|
||||
{
|
||||
ExcelRange cell = sheet.Cells[row, column];
|
||||
|
||||
try
|
||||
{
|
||||
if (cell.Value == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
string s = cell.GetValue<string>();
|
||||
|
||||
return s.Trim();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception($"Rows {row} Columns {column} Content {cell.Text} {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExcelTable.cs
Normal file
28
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExcelTable.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// Excel表格类,用于存储表格的名称和列信息。
|
||||
/// </summary>
|
||||
public sealed class ExcelTable
|
||||
{
|
||||
/// <summary>
|
||||
/// 表格的名称。
|
||||
/// </summary>
|
||||
public readonly string Name;
|
||||
/// <summary>
|
||||
/// 客户端列信息,使用排序字典存储列名和列索引列表。
|
||||
/// </summary>
|
||||
public readonly SortedDictionary<string, List<int>> ClientColInfos = new();
|
||||
/// <summary>
|
||||
/// 服务器端列信息,使用排序字典存储列名和列索引列表。
|
||||
/// </summary>
|
||||
public readonly SortedDictionary<string, List<int>> ServerColInfos = new();
|
||||
/// <summary>
|
||||
/// 构造函数,初始化Excel表格对象并设置表格名称。
|
||||
/// </summary>
|
||||
/// <param name="name">表格名称。</param>
|
||||
public ExcelTable(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
public static class ExcelTemplate
|
||||
{
|
||||
public static readonly string Template = """
|
||||
using System;
|
||||
using ProtoBuf;
|
||||
using Fantasy;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
using Fantasy.ConfigTable;
|
||||
using Fantasy.Serialize;
|
||||
// ReSharper disable CollectionNeverUpdated.Global
|
||||
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
#pragma warning disable CS0169
|
||||
#pragma warning disable CS8618
|
||||
#pragma warning disable CS8625
|
||||
#pragma warning disable CS8603
|
||||
|
||||
namespace (namespace)
|
||||
{
|
||||
[ProtoContract]
|
||||
public sealed partial class (ConfigName)Data : ASerialize, IConfigTable, IProto
|
||||
{
|
||||
[ProtoMember(1)]
|
||||
public List<(ConfigName)> List { get; set; } = new List<(ConfigName)>();
|
||||
#if FANTASY_NET
|
||||
[ProtoIgnore]
|
||||
private readonly ConcurrentDictionary<uint, (ConfigName)> _configs = new ConcurrentDictionary<uint, (ConfigName)>();
|
||||
#else
|
||||
[ProtoIgnore]
|
||||
private readonly Dictionary<uint, (ConfigName)> _configs = new Dictionary<uint, (ConfigName)>();
|
||||
#endif
|
||||
private static (ConfigName)Data _instance = null;
|
||||
|
||||
public static (ConfigName)Data Instance
|
||||
{
|
||||
get { return _instance ??= ConfigTableHelper.Load<(ConfigName)Data>(); }
|
||||
private set => _instance = value;
|
||||
}
|
||||
|
||||
public (ConfigName) Get(uint id, bool check = true)
|
||||
{
|
||||
if (_configs.ContainsKey(id))
|
||||
{
|
||||
return _configs[id];
|
||||
}
|
||||
|
||||
if (check)
|
||||
{
|
||||
throw new Exception($"(ConfigName) not find {id} Id");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
public bool TryGet(uint id, out (ConfigName) config)
|
||||
{
|
||||
config = null;
|
||||
|
||||
if (!_configs.ContainsKey(id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
config = _configs[id];
|
||||
return true;
|
||||
}
|
||||
public override void AfterDeserialization()
|
||||
{
|
||||
foreach (var config in List)
|
||||
{
|
||||
#if FANTASY_NET
|
||||
_configs.TryAdd(config.Id, config);
|
||||
#else
|
||||
_configs.Add(config.Id, config);
|
||||
#endif
|
||||
config.AfterDeserialization();
|
||||
}
|
||||
|
||||
EndInit();
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
}
|
||||
|
||||
[ProtoContract]
|
||||
public sealed partial class (ConfigName) : ASerialize, IProto
|
||||
{(Fields)
|
||||
}
|
||||
}
|
||||
""";
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using OfficeOpenXml;
|
||||
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
public sealed class ExcelWorksheets(ExcelExporter excelExporter)
|
||||
{
|
||||
public bool TryGetValue(string worksheetName, out ExcelWorksheet excelWorksheet)
|
||||
{
|
||||
if (excelExporter.Worksheets.TryGetValue(worksheetName, out excelWorksheet))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var computeHash64 = HashCodeHelper.ComputeHash64(worksheetName);
|
||||
if (!excelExporter.VersionInfo.WorksheetNames.Contains(computeHash64))
|
||||
{
|
||||
Log.Info($"{worksheetName} is not exist!");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
17
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExportInfo.cs
Normal file
17
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExportInfo.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// 导出信息类,用于存储导出操作的名称和文件信息。
|
||||
/// </summary>
|
||||
public class ExportInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 导出操作的名称。
|
||||
/// </summary>
|
||||
public string Name;
|
||||
/// <summary>
|
||||
/// 导出操作生成的文件信息。
|
||||
/// </summary>
|
||||
public FileInfo FileInfo;
|
||||
}
|
||||
24
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExportType.cs
Normal file
24
Tools/ConfigBuilder/NBConfigBuilder/Exporter/ExportType.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// 导出类型枚举,用于标识不同类型的导出操作。
|
||||
/// </summary>
|
||||
public enum ExportType
|
||||
{
|
||||
/// <summary>
|
||||
/// 无导出类型。
|
||||
/// </summary>
|
||||
None = 0,
|
||||
/// <summary>
|
||||
/// 所有数据的增量导出Excel类型。
|
||||
/// </summary>
|
||||
AllExcelIncrement = 1,
|
||||
/// <summary>
|
||||
/// 所有数据的全量导出Excel类型。
|
||||
/// </summary>
|
||||
AllExcel = 2,
|
||||
/// <summary>
|
||||
/// 导出类型枚举的最大值,一定要放在最后。
|
||||
/// </summary>
|
||||
Max,
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace NBConfigBuilder;
|
||||
|
||||
public class VersionInfo
|
||||
{
|
||||
public SortedSet<long> WorksheetNames = [];
|
||||
public SortedDictionary<long, long> Tables = new();
|
||||
}
|
||||
Reference in New Issue
Block a user