Optimize class type search code

优化类型搜索方式,改为全域搜索类型。
pull/7/head^2
hevinci 2022-04-26 18:24:14 +08:00
parent e9d31bbf94
commit 5484b604a7
3 changed files with 10 additions and 141 deletions

View File

@ -199,7 +199,8 @@ namespace YooAsset.Editor
/// </summary> /// </summary>
private List<Type> GetEncryptionServicesClassTypes() private List<Type> GetEncryptionServicesClassTypes()
{ {
List<Type> classTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IEncryptionServices)); TypeCache.TypeCollection collection = TypeCache.GetTypesDerivedFrom<IEncryptionServices>();
List<Type> classTypes = collection.ToList();
return classTypes; return classTypes;
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
@ -110,7 +111,9 @@ namespace YooAsset.Editor
typeof(PackGrouper), typeof(PackGrouper),
typeof(PackRawFile), typeof(PackRawFile),
}; };
var customTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IPackRule));
TypeCache.TypeCollection collection = TypeCache.GetTypesDerivedFrom<IPackRule>();
var customTypes = collection.ToList();
types.AddRange(customTypes); types.AddRange(customTypes);
for (int i = 0; i < types.Count; i++) for (int i = 0; i < types.Count; i++)
{ {
@ -134,7 +137,9 @@ namespace YooAsset.Editor
typeof(CollectPrefab), typeof(CollectPrefab),
typeof(CollectSprite) typeof(CollectSprite)
}; };
var customTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IFilterRule));
TypeCache.TypeCollection collection = TypeCache.GetTypesDerivedFrom<IFilterRule>();
var customTypes = collection.ToList();
types.AddRange(customTypes); types.AddRange(customTypes);
for (int i = 0; i < types.Count; i++) for (int i = 0; i < types.Count; i++)
{ {

View File

@ -3,147 +3,10 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Diagnostics;
using System.Reflection;
namespace YooAsset namespace YooAsset
{ {
/// <summary>
/// 程序集工具类
/// </summary>
internal static class AssemblyUtility
{
public const string YooAssetAssemblyName = "YooAsset";
public const string YooAssetAssemblyEditorName = "YooAsset.Editor";
public const string UnityDefaultAssemblyName = "Assembly-CSharp";
public const string UnityDefaultAssemblyEditorName = "Assembly-CSharp-Editor";
private static readonly Dictionary<string, List<Type>> _cache = new Dictionary<string, List<Type>>();
static AssemblyUtility()
{
_cache.Clear();
}
/// <summary>
/// 获取程序集
/// </summary>
public static Assembly GetAssembly(string assemblyName)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
if (assembly.GetName().Name == assemblyName)
return assembly;
}
return null;
}
/// <summary>
/// 获取程序集里的所有类型
/// </summary>
private static List<Type> GetTypes(string assemblyName)
{
if (_cache.ContainsKey(assemblyName))
return _cache[assemblyName];
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
if (assembly.GetName().Name == assemblyName)
{
List<Type> types = assembly.GetTypes().ToList();
_cache.Add(assemblyName, types);
return types;
}
}
// 注意:如果没有找到程序集返回空列表
UnityEngine.Debug.LogWarning($"Not found assembly : {assemblyName}");
return new List<Type>();
}
/// <summary>
/// 获取带继承关系的所有类的类型
/// <param name="parentType">父类类型</param>
/// </summary>
public static List<Type> GetAssignableTypes(string assemblyName, System.Type parentType)
{
List<Type> result = new List<Type>();
List<Type> cacheTypes = GetTypes(assemblyName);
for (int i = 0; i < cacheTypes.Count; i++)
{
Type type = cacheTypes[i];
// 判断继承关系
if (parentType.IsAssignableFrom(type))
{
if (type.Name == parentType.Name)
continue;
result.Add(type);
}
}
return result;
}
/// <summary>
/// 获取带属性标签的所有类的类型
/// <param name="attributeType">属性类型</param>
/// </summary>
public static List<Type> GetAttributeTypes(string assemblyName, System.Type attributeType)
{
List<Type> result = new List<Type>();
List<Type> cacheTypes = GetTypes(assemblyName);
for (int i = 0; i < cacheTypes.Count; i++)
{
System.Type type = cacheTypes[i];
// 判断属性标签
if (Attribute.IsDefined(type, attributeType))
{
result.Add(type);
}
}
return result;
}
/// <summary>
/// 获取带继承关系和属性标签的所有类的类型
/// </summary>
/// <param name="parentType">父类类型</param>
/// <param name="attributeType">属性类型</param>
public static List<Type> GetAssignableAttributeTypes(string assemblyName, System.Type parentType, System.Type attributeType, bool checkError = true)
{
List<Type> result = new List<Type>();
List<Type> cacheTypes = GetTypes(assemblyName);
for (int i = 0; i < cacheTypes.Count; i++)
{
Type type = cacheTypes[i];
// 判断属性标签
if (Attribute.IsDefined(type, attributeType))
{
// 判断继承关系
if (parentType.IsAssignableFrom(type))
{
if (type.Name == parentType.Name)
continue;
result.Add(type);
}
else
{
if(checkError)
throw new Exception($"class {type} must inherit from {parentType}.");
}
}
}
return result;
}
}
/// <summary> /// <summary>
/// 字符串工具类 /// 字符串工具类
/// </summary> /// </summary>