hybridclr_unity/Editor/Link/Analyzer.cs

54 lines
1.7 KiB
C#
Raw Normal View History

2022-09-22 08:56:07 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using UnityEditor;
using UnityEngine;
2022-09-22 08:56:07 +08:00
using IAssemblyResolver = HybridCLR.Editor.Meta.IAssemblyResolver;
namespace HybridCLR.Editor.Link
2022-09-22 08:56:07 +08:00
{
public class Analyzer
2022-09-22 08:56:07 +08:00
{
private readonly IAssemblyResolver _resolver;
public Analyzer(IAssemblyResolver resolver)
2022-09-22 08:56:07 +08:00
{
_resolver = resolver;
}
public HashSet<TypeRef> CollectRefs(List<string> rootAssemblies)
2022-09-22 08:56:07 +08:00
{
using (var assCollector = new AssemblyCache(_resolver))
2022-09-22 08:56:07 +08:00
{
var rootAssemblyNames = new HashSet<string>(rootAssemblies);
2022-09-22 08:56:07 +08:00
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
foreach (var rootAss in rootAssemblies)
2022-09-22 08:56:07 +08:00
{
var dnAss = assCollector.LoadModule(rootAss, false);
foreach (var type in dnAss.GetTypeRefs())
2022-09-22 08:56:07 +08:00
{
if (type.DefinitionAssembly == null)
{
Debug.LogWarning($"assembly:{dnAss.Name} TypeRef {type.FullName} has no DefinitionAssembly");
continue;
}
if (!rootAssemblyNames.Contains(type.DefinitionAssembly.Name.ToString()))
{
typeRefs.Add(type);
}
2022-09-22 08:56:07 +08:00
}
}
assCollector.Dispose();
return typeRefs;
2022-09-22 08:56:07 +08:00
}
}
}
}