From 92cfef42fff227215414d6864130069b8eb75486 Mon Sep 17 00:00:00 2001 From: coffee <985942825@qq.com> Date: Sun, 5 Jan 2025 21:56:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=AD=97=E5=85=B8=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/Data/ClassCodeData.cs | 28 ++++++++----------- .../ExcelResolver/Editor/Data/FieldData.cs | 3 +- .../ExcelResolverEditorWindow.ReadExcel.cs | 9 +++--- ...ExcelResolverEditorWindow.WriteDataCode.cs | 12 ++++---- .../ExcelResolverEditorWindow.WriteSOData.cs | 3 +- ...ExcelResolverEditorWindow.WriteUtilCode.cs | 2 +- .../Editor/ExcelResolverUtil.Cell.cs | 18 ++++++++---- .../Editor/ExcelResolverUtil.Type.cs | 10 +++---- 8 files changed, 43 insertions(+), 42 deletions(-) diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/ClassCodeData.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/ClassCodeData.cs index 204fc84..e1dc586 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/ClassCodeData.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/ClassCodeData.cs @@ -1,34 +1,28 @@ -using System; -using System.Linq; -using Sirenix.Utilities; +using System.Collections.Generic; namespace Tools.ExcelResolver.Editor { - internal class ClassCodeData : IDisposable + internal class ClassCodeData { public readonly TableType tableType; public readonly string className; - public readonly FieldData[] fields; + public readonly Dictionary fields = new(); + // public readonly FieldData[] fields; public readonly int[] keyIndex; - public readonly FieldData[] keyField; + // public readonly FieldData[] keyField; - public ClassCodeData(TableType tableType, string className, FieldData[] fields, int[] keyIndex) + public ClassCodeData(TableType tableType, string className, Dictionary fields, int[] keyIndex) { this.tableType = tableType; this.className = $"{char.ToUpper(className[0])}{className.Substring(1)}"; this.fields = fields; this.keyIndex = keyIndex; - keyField = new FieldData[keyIndex.Length]; - for (int i = 0; i < keyIndex.Length; i++) - { - keyField[i] = fields.First(f => f.colIndex == keyIndex[i]); - } - } - - public void Dispose() - { - fields.ForEach(field => field.Dispose()); + // keyField = new FieldData[keyIndex.Length]; + // for (int i = 0; i < keyIndex.Length; i++) + // { + // keyField[i] = fields.First(f => f.colIndex == keyIndex[i]); + // } } } } \ No newline at end of file diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/FieldData.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/FieldData.cs index 29e9b0c..1b5e9fc 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/FieldData.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/FieldData.cs @@ -3,9 +3,10 @@ using System.CodeDom; namespace Tools.ExcelResolver.Editor { - internal class FieldData : IDisposable + internal class FieldData { public int colIndex; + public bool isKey; public string varName; public string typeString; diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs index 728397e..ce31bcb 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs @@ -121,10 +121,11 @@ namespace Tools.ExcelResolver.Editor } } - private FieldData[] GetFieldData(ExcelWorksheet worksheet) + private Dictionary GetFieldData(ExcelWorksheet worksheet) { - List fieldDatas = new List(); + var fieldDatas = new Dictionary(); var endColumn = worksheet.Dimension.End.Column; + for (int col = 2; col <= endColumn; col++) { FieldData fieldData = new FieldData @@ -137,10 +138,10 @@ namespace Tools.ExcelResolver.Editor description = worksheet.Cells[5, col].Text, path = worksheet.Cells[6, col].Text, }; - fieldDatas.Add(fieldData); + fieldDatas.Add(col, fieldData); } - return fieldDatas.ToArray(); + return fieldDatas; } } } \ No newline at end of file diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs index 2232f26..c91f8c8 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs @@ -44,10 +44,10 @@ namespace Tools.ExcelResolver.Editor { IsClass = true, TypeAttributes = System.Reflection.TypeAttributes.Public, - CustomAttributes = new CodeAttributeDeclarationCollection() - { - new CodeAttributeDeclaration("Serializable") - }, + // CustomAttributes = new CodeAttributeDeclarationCollection() + // { + // new CodeAttributeDeclaration("Serializable") + // }, BaseTypes = { new CodeTypeReference("ScriptableObject"), @@ -60,7 +60,7 @@ namespace Tools.ExcelResolver.Editor #region 字段 - foreach (var field in classCodeData.fields) + foreach (var field in classCodeData.fields.Values) { CodeMemberField codeField = new CodeMemberField { @@ -71,8 +71,6 @@ namespace Tools.ExcelResolver.Editor { new CodeCommentStatement("", true), new CodeCommentStatement(field.info, true), - // new CodeCommentStatement($"{field.description}", true), - // new CodeCommentStatement("", true), }, }; if (!string.IsNullOrEmpty(field.description)) diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteSOData.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteSOData.cs index b498415..e102d56 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteSOData.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteSOData.cs @@ -41,7 +41,8 @@ namespace Tools.ExcelResolver.Editor for (int col = 2; col < worksheet.Dimension.End.Column; col++) { - + var cell = worksheet.Cells[row, col]; + // object convertedValue = ExcelResolverUtil.ConvertCellValue(cell, classCodeData.fields[col], classCodeData.fieldNames[col - 2], classCodeData.className); } } } diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs index 07e76ad..fa7155b 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs @@ -61,7 +61,7 @@ namespace Tools.ExcelResolver.Editor switch (classCodeData.tableType) { case TableType.SingleKeyTable: - FieldData keyField = classCodeData.keyField[0]; + FieldData keyField = classCodeData.fields[classCodeData.keyIndex[0]]; CodeMemberField codeField = new CodeMemberField($"Dictionary<{keyField.typeString}, {classCodeData.className}>", "Data") { Attributes = MemberAttributes.Public, diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Cell.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Cell.cs index 700dec6..163349c 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Cell.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Cell.cs @@ -6,12 +6,12 @@ using UnityEngine; namespace Tools.ExcelResolver.Editor { - public static partial class ExcelResolverUtil + internal static partial class ExcelResolverUtil { /// /// 读取指定行的数据 /// - public static List ReadRow(ExcelWorksheet worksheet, int row, int colCount, string rowType) + internal static List ReadRow(ExcelWorksheet worksheet, int row, int colCount, string rowType) { List values = new(colCount); for (int col = 1; col <= colCount; col++) @@ -36,11 +36,17 @@ namespace Tools.ExcelResolver.Editor } return values; } + + // internal static object ConvertCellValue(ExcelRange cell, T type, string header, string className) + // { + // string cellValue = cell.Text.Trim(); + // + // } /// /// 通用的单元格 -> C# 对象转换 /// - public static object ConvertCellValue(ExcelRange cell, string type, string header, string className) + internal static object ConvertCellValue(ExcelRange cell, string type, string header, string className) { try { @@ -70,7 +76,7 @@ namespace Tools.ExcelResolver.Editor /// /// 返回类型的默认值 (包含 List<...> 场景) /// - public static object GetDefaultValue(string type) + internal static object GetDefaultValue(string type) { if (type.StartsWith("list<", StringComparison.OrdinalIgnoreCase) && type.EndsWith(">")) { @@ -100,7 +106,7 @@ namespace Tools.ExcelResolver.Editor /// /// 转换逗号分隔的字符串到 List<...> (List, List, ...) /// - public static object ConvertToList(string cellValue, string type, string header, string className) + internal static object ConvertToList(string cellValue, string type, string header, string className) { var insideType = type.Substring(5, type.Length - 6).Trim(); var splitted = string.IsNullOrEmpty(cellValue) @@ -126,7 +132,7 @@ namespace Tools.ExcelResolver.Editor }; } - public static readonly Dictionary> TypeConverters = new(StringComparer.OrdinalIgnoreCase) + internal static readonly Dictionary> TypeConverters = new(StringComparer.OrdinalIgnoreCase) { { "int", value => int.TryParse(value, out var intValue) ? intValue : 0 }, { "float", value => float.TryParse(value, out var floatValue) ? floatValue : 0f }, diff --git a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Type.cs b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Type.cs index a763e94..2fe71a8 100644 --- a/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Type.cs +++ b/Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Type.cs @@ -3,17 +3,17 @@ using System.Collections.Generic; namespace Tools.ExcelResolver.Editor { - public static partial class ExcelResolverUtil + internal static partial class ExcelResolverUtil { /// /// 类型缓存,避免重复反射查找 /// - public static readonly Dictionary TypeCache = new(StringComparer.OrdinalIgnoreCase); + internal static readonly Dictionary TypeCache = new(StringComparer.OrdinalIgnoreCase); /// /// 通过类名(含命名空间)获取 Type,并缓存 /// - public static Type GetOrCacheTypeByName(string typeName) + internal static Type GetOrCacheTypeByName(string typeName) { if (TypeCache.TryGetValue(typeName, out Type cachedType)) { @@ -31,7 +31,7 @@ namespace Tools.ExcelResolver.Editor return type; } - public static Type GetTypeFromAllAssemblies(string typeName) + internal static Type GetTypeFromAllAssemblies(string typeName) { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { @@ -46,7 +46,7 @@ namespace Tools.ExcelResolver.Editor throw new ArgumentException($"Unsupported type: {typeName}"); } - public static void Dispose() + internal static void Dispose() { TypeCache.Clear(); }