Browse Source

使用字典存储字段

master
coffee 6 months ago
parent
commit
92cfef42ff
  1. 28
      Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/ClassCodeData.cs
  2. 3
      Assets/Unity-Tools/Core/ExcelResolver/Editor/Data/FieldData.cs
  3. 9
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs
  4. 12
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteDataCode.cs
  5. 3
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteSOData.cs
  6. 2
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.WriteUtilCode.cs
  7. 18
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Cell.cs
  8. 10
      Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverUtil.Type.cs

28
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<int, FieldData> 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<int, FieldData> 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]);
// }
}
}
}

3
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;

9
Assets/Unity-Tools/Core/ExcelResolver/Editor/ExcelResolverEditorWindow.ReadExcel.cs

@ -121,10 +121,11 @@ namespace Tools.ExcelResolver.Editor
}
}
private FieldData[] GetFieldData(ExcelWorksheet worksheet)
private Dictionary<int, FieldData> GetFieldData(ExcelWorksheet worksheet)
{
List<FieldData> fieldDatas = new List<FieldData>();
var fieldDatas = new Dictionary<int, FieldData>();
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;
}
}
}

12
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("<summary>", true),
new CodeCommentStatement(field.info, true),
// new CodeCommentStatement($"<c>{field.description}</c>", true),
// new CodeCommentStatement("</summary>", true),
},
};
if (!string.IsNullOrEmpty(field.description))

3
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);
}
}
}

2
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,

18
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
{
/// <summary>
/// 读取指定行的数据
/// </summary>
public static List<string> ReadRow(ExcelWorksheet worksheet, int row, int colCount, string rowType)
internal static List<string> ReadRow(ExcelWorksheet worksheet, int row, int colCount, string rowType)
{
List<string> values = new(colCount);
for (int col = 1; col <= colCount; col++)
@ -36,11 +36,17 @@ namespace Tools.ExcelResolver.Editor
}
return values;
}
// internal static object ConvertCellValue<T>(ExcelRange cell, T type, string header, string className)
// {
// string cellValue = cell.Text.Trim();
//
// }
/// <summary>
/// 通用的单元格 -> C# 对象转换
/// </summary>
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
/// <summary>
/// 返回类型的默认值 (包含 List<...> 场景)
/// </summary>
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
/// <summary>
/// 转换逗号分隔的字符串到 List<...> (List<int>, List<string>, ...)
/// </summary>
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<string, Func<string, object>> TypeConverters = new(StringComparer.OrdinalIgnoreCase)
internal static readonly Dictionary<string, Func<string, object>> TypeConverters = new(StringComparer.OrdinalIgnoreCase)
{
{ "int", value => int.TryParse(value, out var intValue) ? intValue : 0 },
{ "float", value => float.TryParse(value, out var floatValue) ? floatValue : 0f },

10
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
{
/// <summary>
/// 类型缓存,避免重复反射查找
/// </summary>
public static readonly Dictionary<string, Type> TypeCache = new(StringComparer.OrdinalIgnoreCase);
internal static readonly Dictionary<string, Type> TypeCache = new(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// 通过类名(含命名空间)获取 Type,并缓存
/// </summary>
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();
}

Loading…
Cancel
Save