2022-09-22 08:56:07 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-01-08 16:20:39 +08:00
|
|
|
|
using System.IO;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
|
using HybridCLR.Editor.Meta;
|
2022-10-14 18:51:19 +08:00
|
|
|
|
using UnityEditor;
|
2024-05-17 21:42:30 +08:00
|
|
|
|
using UnityEngine;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
using IAssemblyResolver = HybridCLR.Editor.Meta.IAssemblyResolver;
|
|
|
|
|
|
|
2022-09-23 09:40:06 +08:00
|
|
|
|
namespace HybridCLR.Editor.Link
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-09-23 09:40:06 +08:00
|
|
|
|
public class Analyzer
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IAssemblyResolver _resolver;
|
|
|
|
|
|
|
2023-02-07 12:42:16 +08:00
|
|
|
|
public Analyzer(IAssemblyResolver resolver)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
|
_resolver = resolver;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-08 16:20:39 +08:00
|
|
|
|
|
|
|
|
|
|
public HashSet<ITypeDefOrRef> CollectRefs(List<string> rootAssemblies, bool includeUnityEngineCoreTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
var typeRefs = new HashSet<ITypeDefOrRef>(TypeEqualityComparer.Instance);
|
|
|
|
|
|
CollectHotUpdateRefs(rootAssemblies, typeRefs);
|
|
|
|
|
|
|
|
|
|
|
|
if (includeUnityEngineCoreTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
CollectUnityEngineCoreTypes(typeRefs);
|
|
|
|
|
|
}
|
|
|
|
|
|
return typeRefs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CollectUnityEngineCoreTypes(HashSet<ITypeDefOrRef> preservedTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log("CollectUnityEngineCoreTypes");
|
|
|
|
|
|
string unityEngineDllPath = $"{EditorApplication.applicationContentsPath}/Managed/UnityEngine";
|
|
|
|
|
|
|
|
|
|
|
|
var assCollector = new AssemblyCache(_resolver);
|
|
|
|
|
|
foreach (string unityEngineDll in Directory.GetFiles(unityEngineDllPath, "UnityEngine.*.dll", SearchOption.AllDirectories))
|
|
|
|
|
|
{
|
|
|
|
|
|
ModuleDefMD mod = assCollector.LoadModuleFromFileWithoutCache(unityEngineDll);
|
|
|
|
|
|
foreach (TypeDef type in mod.GetTypes())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (type.Methods.Any(m => m.IsInternalCall || m.IsPinvokeImpl))
|
|
|
|
|
|
{
|
|
|
|
|
|
preservedTypes.Add(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CollectHotUpdateRefs(List<string> rootAssemblies, HashSet<ITypeDefOrRef> preservedTypes)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2024-05-26 11:59:54 +08:00
|
|
|
|
var assCollector = new AssemblyCache(_resolver);
|
|
|
|
|
|
var rootAssemblyNames = new HashSet<string>(rootAssemblies);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
2024-05-26 11:59:54 +08:00
|
|
|
|
foreach (var rootAss in rootAssemblies)
|
|
|
|
|
|
{
|
|
|
|
|
|
var dnAss = assCollector.LoadModule(rootAss, false);
|
|
|
|
|
|
foreach (var type in dnAss.GetTypeRefs())
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2024-05-26 11:59:54 +08:00
|
|
|
|
if (type.DefinitionAssembly == null)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2024-05-26 11:59:54 +08:00
|
|
|
|
Debug.LogWarning($"assembly:{dnAss.Name} TypeRef {type.FullName} has no DefinitionAssembly");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!rootAssemblyNames.Contains(type.DefinitionAssembly.Name.ToString()))
|
|
|
|
|
|
{
|
2025-01-08 16:20:39 +08:00
|
|
|
|
preservedTypes.Add(type);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|