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;
|
2022-10-14 18:51:19 +08:00
|
|
|
|
using UnityEditor;
|
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;
|
2022-10-14 23:20:35 +08:00
|
|
|
|
private readonly bool _analyzeAssetType;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
2022-10-14 23:20:35 +08:00
|
|
|
|
public Analyzer(IAssemblyResolver resolver, bool analyzeAssetType)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
_resolver = resolver;
|
2022-10-14 23:20:35 +08:00
|
|
|
|
_analyzeAssetType = analyzeAssetType;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HashSet<TypeRef> CollectRefs(List<Assembly> rootAssemblies)
|
|
|
|
|
{
|
2022-09-26 12:12:57 +08:00
|
|
|
|
using (var assCollector = new AssemblyCache(_resolver))
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-09-26 12:12:57 +08:00
|
|
|
|
var rootAssemblyName = new HashSet<string>();
|
|
|
|
|
foreach (var ass in rootAssemblies)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-09-26 12:12:57 +08:00
|
|
|
|
if (!rootAssemblyName.Add(ass.GetName().Name))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"assembly:{ass.GetName().Name} 重复");
|
|
|
|
|
}
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 12:12:57 +08:00
|
|
|
|
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
|
|
|
|
|
foreach (var rootAss in rootAssemblies)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-09-26 12:12:57 +08:00
|
|
|
|
var dnAss = assCollector.LoadModule(rootAss.GetName().Name);
|
|
|
|
|
foreach (var type in dnAss.GetTypeRefs())
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2022-09-26 12:12:57 +08:00
|
|
|
|
if (!rootAssemblyName.Contains(type.DefinitionAssembly.Name))
|
|
|
|
|
{
|
|
|
|
|
typeRefs.Add(type);
|
|
|
|
|
}
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-14 18:51:19 +08:00
|
|
|
|
|
2022-10-14 23:20:35 +08:00
|
|
|
|
if (_analyzeAssetType)
|
|
|
|
|
{
|
|
|
|
|
var modsExludeRoots = assCollector.LoadedModules
|
|
|
|
|
.Where(e => !rootAssemblyName.Contains(e.Key))
|
|
|
|
|
.ToDictionary(e => e.Key, e => e.Value);
|
|
|
|
|
CollectObjectTypeInAssets(modsExludeRoots, typeRefs);
|
|
|
|
|
}
|
2022-10-14 18:51:19 +08:00
|
|
|
|
|
2022-09-26 12:12:57 +08:00
|
|
|
|
assCollector.Dispose();
|
|
|
|
|
return typeRefs;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-14 18:51:19 +08:00
|
|
|
|
|
|
|
|
|
public void CollectObjectTypeInAssets(Dictionary<string, ModuleDefMD> mods, HashSet<TypeRef> typeRefs)
|
|
|
|
|
{
|
|
|
|
|
var objTypes = new HashSet<Type>();
|
|
|
|
|
string[] guids = AssetDatabase.FindAssets("t:Object", new[] { "Assets" });
|
|
|
|
|
foreach (string guid in guids)
|
|
|
|
|
{
|
|
|
|
|
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
|
|
|
Type mainAssetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
|
|
|
|
|
if (mainAssetType == typeof(SceneAsset))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetsAtPath(assetPath);
|
|
|
|
|
foreach (UnityEngine.Object obj in objs)
|
|
|
|
|
{
|
2022-10-14 23:20:35 +08:00
|
|
|
|
if (obj != null)
|
|
|
|
|
{
|
|
|
|
|
objTypes.Add(obj.GetType());
|
|
|
|
|
}
|
2022-10-14 18:51:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var importers = mods.ToDictionary(e => e.Key, e => new Importer(e.Value));
|
|
|
|
|
//Debug.Log($"importers count:{importers.Count} importers:{string.Join(",", importers.Keys)}");
|
|
|
|
|
foreach (var type in objTypes)
|
|
|
|
|
{
|
|
|
|
|
if (importers.TryGetValue(type.Assembly.GetName().Name, out var im))
|
|
|
|
|
{
|
|
|
|
|
typeRefs.Add((TypeRef)im.Import(type));
|
|
|
|
|
//Debug.Log($"== add asset type:{type}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Debug.Log($"== ignore asset type:{type} {type.Assembly.GetName().Name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|