导表修改

This commit is contained in:
2025-10-09 23:58:11 +08:00
parent 547d234ad3
commit 520d4f37bd
8 changed files with 95 additions and 305 deletions

View File

@@ -6,11 +6,14 @@ public class AppConfig
public string ExcelPath { get; set; } public string ExcelPath { get; set; }
public string ClientPath { get; set; } public string ClientPath { get; set; }
public string ClientJsonPath { get; set; } public string ClientJsonPath { get; set; }
public string ClientNamespace { get; set; }
public string ServerPath { get; set; } public string ServerPath { get; set; }
public string ServerJsonPath { get; set; } public string ServerJsonPath { get; set; }
public string ServerNamespace { get; set; }
public bool GenClient { get; set; } public bool GenClient { get; set; }
public bool GenServer { get; set; } public bool GenServer { get; set; }
public string ExcelVersionPath => Path.Combine(ExcelPath, "Version.txt"); public string ExcelVersionPath => Path.Combine(ExcelPath, "Version.txt");
} }

View File

@@ -1,169 +0,0 @@
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;
}
}

View File

@@ -1,35 +0,0 @@
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();
}

View File

@@ -1,69 +0,0 @@
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());
}
}
}

View File

@@ -635,6 +635,12 @@ public sealed class ExcelExporter
var colInfos = isServer ? table.ServerColInfos : table.ClientColInfos; var colInfos = isServer ? table.ServerColInfos : table.ClientColInfos;
var exportPath = isServer ? App.Config.ServerPath : App.Config.ClientPath; var exportPath = isServer ? App.Config.ServerPath : App.Config.ClientPath;
var csNamespace = isServer ? App.Config.ServerNamespace : App.Config.ClientNamespace;
if (string.IsNullOrEmpty(csNamespace))
{
csNamespace = "NB";
}
if (colInfos.Count <= 0) if (colInfos.Count <= 0)
{ {
return; return;
@@ -723,7 +729,7 @@ public sealed class ExcelExporter
FileHelper.CreateDirectory(exportPath); FileHelper.CreateDirectory(exportPath);
} }
var content = template.Replace("(namespace)", "Fantasy") var content = template.Replace("(namespace)", csNamespace)
.Replace("(ConfigName)", csName) .Replace("(ConfigName)", csName)
.Replace("(Fields)", fileBuilder.ToString()); .Replace("(Fields)", fileBuilder.ToString());
File.WriteAllText(Path.Combine(exportPath, $"{csName}.cs"), content); File.WriteAllText(Path.Combine(exportPath, $"{csName}.cs"), content);

View File

@@ -46,20 +46,24 @@
textBoxServerGenJsonPath = new TextBox(); textBoxServerGenJsonPath = new TextBox();
label5 = new Label(); label5 = new Label();
buttonSelectServerJsonPath = new Button(); buttonSelectServerJsonPath = new Button();
textBoxClientNamespce = new TextBox();
textBoxServerNamespace = new TextBox();
label6 = new Label();
label7 = new Label();
SuspendLayout(); SuspendLayout();
// //
// textBoxExcelPath // textBoxExcelPath
// //
textBoxExcelPath.Location = new Point(84, 13); textBoxExcelPath.Location = new Point(105, 13);
textBoxExcelPath.Name = "textBoxExcelPath"; textBoxExcelPath.Name = "textBoxExcelPath";
textBoxExcelPath.Size = new Size(322, 23); textBoxExcelPath.Size = new Size(301, 23);
textBoxExcelPath.TabIndex = 0; textBoxExcelPath.TabIndex = 0;
// //
// buttonSelectExcelPath // buttonSelectExcelPath
// //
buttonSelectExcelPath.Location = new Point(412, 13); buttonSelectExcelPath.Location = new Point(412, 13);
buttonSelectExcelPath.Name = "buttonSelectExcelPath"; buttonSelectExcelPath.Name = "buttonSelectExcelPath";
buttonSelectExcelPath.Size = new Size(75, 23); buttonSelectExcelPath.Size = new Size(84, 23);
buttonSelectExcelPath.TabIndex = 1; buttonSelectExcelPath.TabIndex = 1;
buttonSelectExcelPath.Text = "选择"; buttonSelectExcelPath.Text = "选择";
buttonSelectExcelPath.UseVisualStyleBackColor = true; buttonSelectExcelPath.UseVisualStyleBackColor = true;
@@ -76,16 +80,16 @@
// //
// textBoxClientGenPath // textBoxClientGenPath
// //
textBoxClientGenPath.Location = new Point(84, 42); textBoxClientGenPath.Location = new Point(105, 42);
textBoxClientGenPath.Name = "textBoxClientGenPath"; textBoxClientGenPath.Name = "textBoxClientGenPath";
textBoxClientGenPath.Size = new Size(318, 23); textBoxClientGenPath.Size = new Size(301, 23);
textBoxClientGenPath.TabIndex = 3; textBoxClientGenPath.TabIndex = 3;
// //
// buttonSelectClientPath // buttonSelectClientPath
// //
buttonSelectClientPath.Location = new Point(412, 42); buttonSelectClientPath.Location = new Point(412, 42);
buttonSelectClientPath.Name = "buttonSelectClientPath"; buttonSelectClientPath.Name = "buttonSelectClientPath";
buttonSelectClientPath.Size = new Size(75, 23); buttonSelectClientPath.Size = new Size(84, 23);
buttonSelectClientPath.TabIndex = 4; buttonSelectClientPath.TabIndex = 4;
buttonSelectClientPath.Text = "选择"; buttonSelectClientPath.Text = "选择";
buttonSelectClientPath.UseVisualStyleBackColor = true; buttonSelectClientPath.UseVisualStyleBackColor = true;
@@ -96,7 +100,7 @@
checkBoxGenClient.AutoSize = true; checkBoxGenClient.AutoSize = true;
checkBoxGenClient.Checked = true; checkBoxGenClient.Checked = true;
checkBoxGenClient.CheckState = CheckState.Checked; checkBoxGenClient.CheckState = CheckState.Checked;
checkBoxGenClient.Location = new Point(495, 44); checkBoxGenClient.Location = new Point(412, 160);
checkBoxGenClient.Name = "checkBoxGenClient"; checkBoxGenClient.Name = "checkBoxGenClient";
checkBoxGenClient.Size = new Size(87, 21); checkBoxGenClient.Size = new Size(87, 21);
checkBoxGenClient.TabIndex = 5; checkBoxGenClient.TabIndex = 5;
@@ -114,9 +118,9 @@
// //
// textBoxServerGenPath // textBoxServerGenPath
// //
textBoxServerGenPath.Location = new Point(84, 100); textBoxServerGenPath.Location = new Point(105, 100);
textBoxServerGenPath.Name = "textBoxServerGenPath"; textBoxServerGenPath.Name = "textBoxServerGenPath";
textBoxServerGenPath.Size = new Size(318, 23); textBoxServerGenPath.Size = new Size(301, 23);
textBoxServerGenPath.TabIndex = 7; textBoxServerGenPath.TabIndex = 7;
// //
// label3 // label3
@@ -133,18 +137,19 @@
checkBoxGenServer.AutoSize = true; checkBoxGenServer.AutoSize = true;
checkBoxGenServer.Checked = true; checkBoxGenServer.Checked = true;
checkBoxGenServer.CheckState = CheckState.Checked; checkBoxGenServer.CheckState = CheckState.Checked;
checkBoxGenServer.Location = new Point(495, 16); checkBoxGenServer.Location = new Point(412, 189);
checkBoxGenServer.Name = "checkBoxGenServer"; checkBoxGenServer.Name = "checkBoxGenServer";
checkBoxGenServer.Size = new Size(87, 21); checkBoxGenServer.Size = new Size(87, 21);
checkBoxGenServer.TabIndex = 9; checkBoxGenServer.TabIndex = 9;
checkBoxGenServer.Text = "生成服务端"; checkBoxGenServer.Text = "生成服务端";
checkBoxGenServer.UseVisualStyleBackColor = true; checkBoxGenServer.UseVisualStyleBackColor = true;
checkBoxGenServer.CheckedChanged += checkBoxGenServer_CheckedChanged;
// //
// buttonSelectServerPath // buttonSelectServerPath
// //
buttonSelectServerPath.Location = new Point(412, 100); buttonSelectServerPath.Location = new Point(412, 100);
buttonSelectServerPath.Name = "buttonSelectServerPath"; buttonSelectServerPath.Name = "buttonSelectServerPath";
buttonSelectServerPath.Size = new Size(75, 23); buttonSelectServerPath.Size = new Size(84, 23);
buttonSelectServerPath.TabIndex = 10; buttonSelectServerPath.TabIndex = 10;
buttonSelectServerPath.Text = "选择"; buttonSelectServerPath.Text = "选择";
buttonSelectServerPath.UseVisualStyleBackColor = true; buttonSelectServerPath.UseVisualStyleBackColor = true;
@@ -152,9 +157,9 @@
// //
// buttonRun // buttonRun
// //
buttonRun.Location = new Point(495, 71); buttonRun.Location = new Point(504, 13);
buttonRun.Name = "buttonRun"; buttonRun.Name = "buttonRun";
buttonRun.Size = new Size(80, 81); buttonRun.Size = new Size(82, 195);
buttonRun.TabIndex = 12; buttonRun.TabIndex = 12;
buttonRun.Text = "执行"; buttonRun.Text = "执行";
buttonRun.UseVisualStyleBackColor = true; buttonRun.UseVisualStyleBackColor = true;
@@ -162,9 +167,9 @@
// //
// textBoxClientGenJsonPath // textBoxClientGenJsonPath
// //
textBoxClientGenJsonPath.Location = new Point(84, 71); textBoxClientGenJsonPath.Location = new Point(105, 71);
textBoxClientGenJsonPath.Name = "textBoxClientGenJsonPath"; textBoxClientGenJsonPath.Name = "textBoxClientGenJsonPath";
textBoxClientGenJsonPath.Size = new Size(318, 23); textBoxClientGenJsonPath.Size = new Size(301, 23);
textBoxClientGenJsonPath.TabIndex = 13; textBoxClientGenJsonPath.TabIndex = 13;
// //
// label4 // label4
@@ -180,7 +185,7 @@
// //
buttonSelectClientJsonPath.Location = new Point(412, 71); buttonSelectClientJsonPath.Location = new Point(412, 71);
buttonSelectClientJsonPath.Name = "buttonSelectClientJsonPath"; buttonSelectClientJsonPath.Name = "buttonSelectClientJsonPath";
buttonSelectClientJsonPath.Size = new Size(75, 23); buttonSelectClientJsonPath.Size = new Size(84, 23);
buttonSelectClientJsonPath.TabIndex = 15; buttonSelectClientJsonPath.TabIndex = 15;
buttonSelectClientJsonPath.Text = "选择"; buttonSelectClientJsonPath.Text = "选择";
buttonSelectClientJsonPath.UseVisualStyleBackColor = true; buttonSelectClientJsonPath.UseVisualStyleBackColor = true;
@@ -188,9 +193,9 @@
// //
// textBoxServerGenJsonPath // textBoxServerGenJsonPath
// //
textBoxServerGenJsonPath.Location = new Point(85, 129); textBoxServerGenJsonPath.Location = new Point(105, 131);
textBoxServerGenJsonPath.Name = "textBoxServerGenJsonPath"; textBoxServerGenJsonPath.Name = "textBoxServerGenJsonPath";
textBoxServerGenJsonPath.Size = new Size(317, 23); textBoxServerGenJsonPath.Size = new Size(301, 23);
textBoxServerGenJsonPath.TabIndex = 16; textBoxServerGenJsonPath.TabIndex = 16;
// //
// label5 // label5
@@ -206,17 +211,53 @@
// //
buttonSelectServerJsonPath.Location = new Point(412, 129); buttonSelectServerJsonPath.Location = new Point(412, 129);
buttonSelectServerJsonPath.Name = "buttonSelectServerJsonPath"; buttonSelectServerJsonPath.Name = "buttonSelectServerJsonPath";
buttonSelectServerJsonPath.Size = new Size(75, 23); buttonSelectServerJsonPath.Size = new Size(84, 23);
buttonSelectServerJsonPath.TabIndex = 18; buttonSelectServerJsonPath.TabIndex = 18;
buttonSelectServerJsonPath.Text = "选择"; buttonSelectServerJsonPath.Text = "选择";
buttonSelectServerJsonPath.UseVisualStyleBackColor = true; buttonSelectServerJsonPath.UseVisualStyleBackColor = true;
buttonSelectServerJsonPath.Click += buttonSelectServerJsonPath_Click; buttonSelectServerJsonPath.Click += buttonSelectServerJsonPath_Click;
// //
// textBoxClientNamespce
//
textBoxClientNamespce.Location = new Point(105, 160);
textBoxClientNamespce.Name = "textBoxClientNamespce";
textBoxClientNamespce.Size = new Size(301, 23);
textBoxClientNamespce.TabIndex = 19;
//
// textBoxServerNamespace
//
textBoxServerNamespace.Location = new Point(105, 189);
textBoxServerNamespace.Name = "textBoxServerNamespace";
textBoxServerNamespace.Size = new Size(301, 23);
textBoxServerNamespace.TabIndex = 20;
//
// label6
//
label6.AutoSize = true;
label6.Location = new Point(7, 161);
label6.Name = "label6";
label6.Size = new Size(92, 17);
label6.TabIndex = 21;
label6.Text = "客户端命名空间";
//
// label7
//
label7.AutoSize = true;
label7.Location = new Point(7, 192);
label7.Name = "label7";
label7.Size = new Size(92, 17);
label7.TabIndex = 22;
label7.Text = "服务端命名空间";
//
// Form1 // Form1
// //
AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(585, 161); ClientSize = new Size(596, 216);
Controls.Add(label7);
Controls.Add(label6);
Controls.Add(textBoxServerNamespace);
Controls.Add(textBoxClientNamespce);
Controls.Add(buttonSelectServerJsonPath); Controls.Add(buttonSelectServerJsonPath);
Controls.Add(label5); Controls.Add(label5);
Controls.Add(textBoxServerGenJsonPath); Controls.Add(textBoxServerGenJsonPath);
@@ -261,5 +302,9 @@
private TextBox textBoxServerGenJsonPath; private TextBox textBoxServerGenJsonPath;
private Label label5; private Label label5;
private Button buttonSelectServerJsonPath; private Button buttonSelectServerJsonPath;
private TextBox textBoxClientNamespce;
private TextBox textBoxServerNamespace;
private Label label6;
private Label label7;
} }
} }

View File

@@ -150,6 +150,8 @@ namespace NBConfigBuilder
ServerJsonPath = textBoxServerGenJsonPath.Text, ServerJsonPath = textBoxServerGenJsonPath.Text,
GenClient = checkBoxGenClient.Checked, GenClient = checkBoxGenClient.Checked,
GenServer = checkBoxGenServer.Checked, GenServer = checkBoxGenServer.Checked,
ClientNamespace = textBoxClientNamespce.Text,
ServerNamespace = textBoxServerNamespace.Text
}; };
App.Config = config; App.Config = config;
try try
@@ -182,6 +184,8 @@ namespace NBConfigBuilder
checkBoxGenServer.Checked = config.GenServer; checkBoxGenServer.Checked = config.GenServer;
textBoxClientGenJsonPath.Text = config.ClientJsonPath ?? ""; textBoxClientGenJsonPath.Text = config.ClientJsonPath ?? "";
textBoxServerGenJsonPath.Text = config.ServerJsonPath ?? ""; textBoxServerGenJsonPath.Text = config.ServerJsonPath ?? "";
textBoxClientNamespce.Text = config.ClientNamespace ?? "NB";
textBoxServerNamespace.Text = config.ServerNamespace ?? "NB";
} }
} }
catch (Exception ex) catch (Exception ex)
@@ -189,5 +193,10 @@ namespace NBConfigBuilder
MessageBox.Show($"加载配置失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"加载配置失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
private void checkBoxGenServer_CheckedChanged(object sender, EventArgs e)
{
}
} }
} }