[new] support preserve UnityEngine core types when GenerateLinkXml

main
walon 2025-01-08 16:20:39 +08:00
parent 208372e6af
commit 82163d2e50
6 changed files with 50 additions and 6 deletions

View File

@ -28,7 +28,7 @@ namespace HybridCLR.Editor.Commands
List<string> hotfixAssemblies = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved; List<string> hotfixAssemblies = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved;
var analyzer = new Analyzer(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotfixAssemblies)); var analyzer = new Analyzer(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotfixAssemblies));
var refTypes = analyzer.CollectRefs(hotfixAssemblies); var refTypes = analyzer.CollectRefs(hotfixAssemblies, !ls.dontPreserveUnityEngineCoreTypesInLinkXml);
Debug.Log($"[LinkGeneratorCommand] hotfix assembly count:{hotfixAssemblies.Count}, ref type count:{refTypes.Count} output:{Application.dataPath}/{ls.outputLinkFile}"); Debug.Log($"[LinkGeneratorCommand] hotfix assembly count:{hotfixAssemblies.Count}, ref type count:{refTypes.Count} output:{Application.dataPath}/{ls.outputLinkFile}");
var linkXmlWriter = new LinkXmlWriter(); var linkXmlWriter = new LinkXmlWriter();

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
@ -21,12 +22,43 @@ namespace HybridCLR.Editor.Link
_resolver = resolver; _resolver = resolver;
} }
public HashSet<TypeRef> CollectRefs(List<string> rootAssemblies)
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)
{ {
var assCollector = new AssemblyCache(_resolver); var assCollector = new AssemblyCache(_resolver);
var rootAssemblyNames = new HashSet<string>(rootAssemblies); var rootAssemblyNames = new HashSet<string>(rootAssemblies);
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
foreach (var rootAss in rootAssemblies) foreach (var rootAss in rootAssemblies)
{ {
var dnAss = assCollector.LoadModule(rootAss, false); var dnAss = assCollector.LoadModule(rootAss, false);
@ -39,11 +71,10 @@ namespace HybridCLR.Editor.Link
} }
if (!rootAssemblyNames.Contains(type.DefinitionAssembly.Name.ToString())) if (!rootAssemblyNames.Contains(type.DefinitionAssembly.Name.ToString()))
{ {
typeRefs.Add(type); preservedTypes.Add(type);
} }
} }
} }
return typeRefs;
} }
} }
} }

View File

@ -11,7 +11,7 @@ namespace HybridCLR.Editor.Link
{ {
internal class LinkXmlWriter internal class LinkXmlWriter
{ {
public void Write(string outputLinkXmlFile, HashSet<TypeRef> refTypes) public void Write(string outputLinkXmlFile, HashSet<ITypeDefOrRef> refTypes)
{ {
string parentDir = Directory.GetParent(outputLinkXmlFile).FullName; string parentDir = Directory.GetParent(outputLinkXmlFile).FullName;
Directory.CreateDirectory(parentDir); Directory.CreateDirectory(parentDir);

View File

@ -71,6 +71,13 @@ namespace HybridCLR.Editor.Meta
return mod; return mod;
} }
public ModuleDefMD LoadModuleFromFileWithoutCache(string dllPath)
{
ModuleDefMD mod = ModuleDefMD.Load(File.ReadAllBytes(dllPath), _modCtx);
mod.EnableTypeDefFindCache = true;
return mod;
}
private void LoadNetStandard() private void LoadNetStandard()
{ {
string netstandardDllPath = _assemblyPathResolver.ResolveAssembly("netstandard", false); string netstandardDllPath = _assemblyPathResolver.ResolveAssembly("netstandard", false);

View File

@ -21,6 +21,7 @@ namespace HybridCLR.Editor.Settings
private SerializedProperty _externalHotUpdateAssembliyDirs; private SerializedProperty _externalHotUpdateAssembliyDirs;
private SerializedProperty _strippedAOTDllOutputRootDir; private SerializedProperty _strippedAOTDllOutputRootDir;
private SerializedProperty _patchAOTAssemblies; private SerializedProperty _patchAOTAssemblies;
private SerializedProperty _dontPreserveUnityEngineCoreTypesInLinkXml;
private SerializedProperty _outputLinkFile; private SerializedProperty _outputLinkFile;
private SerializedProperty _outputAOTGenericReferenceFile; private SerializedProperty _outputAOTGenericReferenceFile;
private SerializedProperty _maxGenericReferenceIteration; private SerializedProperty _maxGenericReferenceIteration;
@ -51,6 +52,7 @@ namespace HybridCLR.Editor.Settings
_externalHotUpdateAssembliyDirs = _serializedObject.FindProperty("externalHotUpdateAssembliyDirs"); _externalHotUpdateAssembliyDirs = _serializedObject.FindProperty("externalHotUpdateAssembliyDirs");
_strippedAOTDllOutputRootDir = _serializedObject.FindProperty("strippedAOTDllOutputRootDir"); _strippedAOTDllOutputRootDir = _serializedObject.FindProperty("strippedAOTDllOutputRootDir");
_patchAOTAssemblies = _serializedObject.FindProperty("patchAOTAssemblies"); _patchAOTAssemblies = _serializedObject.FindProperty("patchAOTAssemblies");
_dontPreserveUnityEngineCoreTypesInLinkXml = _serializedObject.FindProperty("dontPreserveUnityEngineCoreTypesInLinkXml");
_outputLinkFile = _serializedObject.FindProperty("outputLinkFile"); _outputLinkFile = _serializedObject.FindProperty("outputLinkFile");
_outputAOTGenericReferenceFile = _serializedObject.FindProperty("outputAOTGenericReferenceFile"); _outputAOTGenericReferenceFile = _serializedObject.FindProperty("outputAOTGenericReferenceFile");
_maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration"); _maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration");
@ -140,6 +142,7 @@ namespace HybridCLR.Editor.Settings
EditorGUILayout.PropertyField(_externalHotUpdateAssembliyDirs); EditorGUILayout.PropertyField(_externalHotUpdateAssembliyDirs);
EditorGUILayout.PropertyField(_strippedAOTDllOutputRootDir); EditorGUILayout.PropertyField(_strippedAOTDllOutputRootDir);
EditorGUILayout.PropertyField(_patchAOTAssemblies); EditorGUILayout.PropertyField(_patchAOTAssemblies);
EditorGUILayout.PropertyField(_dontPreserveUnityEngineCoreTypesInLinkXml);
EditorGUILayout.PropertyField(_outputLinkFile); EditorGUILayout.PropertyField(_outputLinkFile);
EditorGUILayout.PropertyField(_outputAOTGenericReferenceFile); EditorGUILayout.PropertyField(_outputAOTGenericReferenceFile);
EditorGUILayout.PropertyField(_maxGenericReferenceIteration); EditorGUILayout.PropertyField(_maxGenericReferenceIteration);

View File

@ -39,6 +39,9 @@ namespace HybridCLR.Editor.Settings
[Tooltip("supplementary metadata assembly names(without .dll suffix)")] [Tooltip("supplementary metadata assembly names(without .dll suffix)")]
public string[] patchAOTAssemblies; public string[] patchAOTAssemblies;
[Tooltip("don't preserve UnityEngine core types in link.xml")]
public bool dontPreserveUnityEngineCoreTypesInLinkXml = false;
[Tooltip("output file of automatic generated link.xml by scanning hot update assemblies")] [Tooltip("output file of automatic generated link.xml by scanning hot update assemblies")]
public string outputLinkFile = "HybridCLRGenerate/link.xml"; public string outputLinkFile = "HybridCLRGenerate/link.xml";