Skip to content

Commit 82163d2

Browse files
committed
[new] support preserve UnityEngine core types when GenerateLinkXml
1 parent 208372e commit 82163d2

6 files changed

Lines changed: 50 additions & 6 deletions

File tree

Editor/Commands/LinkGeneratorCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void GenerateLinkXml(BuildTarget target)
2828
List<string> hotfixAssemblies = SettingsUtil.HotUpdateAssemblyNamesExcludePreserved;
2929

3030
var analyzer = new Analyzer(MetaUtil.CreateHotUpdateAndAOTAssemblyResolver(target, hotfixAssemblies));
31-
var refTypes = analyzer.CollectRefs(hotfixAssemblies);
31+
var refTypes = analyzer.CollectRefs(hotfixAssemblies, !ls.dontPreserveUnityEngineCoreTypesInLinkXml);
3232

3333
Debug.Log($"[LinkGeneratorCommand] hotfix assembly count:{hotfixAssemblies.Count}, ref type count:{refTypes.Count} output:{Application.dataPath}/{ls.outputLinkFile}");
3434
var linkXmlWriter = new LinkXmlWriter();

Editor/Link/Analyzer.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using System.Text;
@@ -21,12 +22,43 @@ public Analyzer(IAssemblyResolver resolver)
2122
_resolver = resolver;
2223
}
2324

24-
public HashSet<TypeRef> CollectRefs(List<string> rootAssemblies)
25+
26+
public HashSet<ITypeDefOrRef> CollectRefs(List<string> rootAssemblies, bool includeUnityEngineCoreTypes)
27+
{
28+
var typeRefs = new HashSet<ITypeDefOrRef>(TypeEqualityComparer.Instance);
29+
CollectHotUpdateRefs(rootAssemblies, typeRefs);
30+
31+
if (includeUnityEngineCoreTypes)
32+
{
33+
CollectUnityEngineCoreTypes(typeRefs);
34+
}
35+
return typeRefs;
36+
}
37+
38+
private void CollectUnityEngineCoreTypes(HashSet<ITypeDefOrRef> preservedTypes)
39+
{
40+
Debug.Log("CollectUnityEngineCoreTypes");
41+
string unityEngineDllPath = $"{EditorApplication.applicationContentsPath}/Managed/UnityEngine";
42+
43+
var assCollector = new AssemblyCache(_resolver);
44+
foreach (string unityEngineDll in Directory.GetFiles(unityEngineDllPath, "UnityEngine.*.dll", SearchOption.AllDirectories))
45+
{
46+
ModuleDefMD mod = assCollector.LoadModuleFromFileWithoutCache(unityEngineDll);
47+
foreach (TypeDef type in mod.GetTypes())
48+
{
49+
if (type.Methods.Any(m => m.IsInternalCall || m.IsPinvokeImpl))
50+
{
51+
preservedTypes.Add(type);
52+
}
53+
}
54+
}
55+
}
56+
57+
public void CollectHotUpdateRefs(List<string> rootAssemblies, HashSet<ITypeDefOrRef> preservedTypes)
2558
{
2659
var assCollector = new AssemblyCache(_resolver);
2760
var rootAssemblyNames = new HashSet<string>(rootAssemblies);
2861

29-
var typeRefs = new HashSet<TypeRef>(TypeEqualityComparer.Instance);
3062
foreach (var rootAss in rootAssemblies)
3163
{
3264
var dnAss = assCollector.LoadModule(rootAss, false);
@@ -39,11 +71,10 @@ public HashSet<TypeRef> CollectRefs(List<string> rootAssemblies)
3971
}
4072
if (!rootAssemblyNames.Contains(type.DefinitionAssembly.Name.ToString()))
4173
{
42-
typeRefs.Add(type);
74+
preservedTypes.Add(type);
4375
}
4476
}
4577
}
46-
return typeRefs;
4778
}
4879
}
4980
}

Editor/Link/LinkXmlWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace HybridCLR.Editor.Link
1111
{
1212
internal class LinkXmlWriter
1313
{
14-
public void Write(string outputLinkXmlFile, HashSet<TypeRef> refTypes)
14+
public void Write(string outputLinkXmlFile, HashSet<ITypeDefOrRef> refTypes)
1515
{
1616
string parentDir = Directory.GetParent(outputLinkXmlFile).FullName;
1717
Directory.CreateDirectory(parentDir);

Editor/Meta/AssemblyCacheBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public ModuleDefMD LoadModule(string moduleName, bool loadReferenceAssemblies =
7171
return mod;
7272
}
7373

74+
public ModuleDefMD LoadModuleFromFileWithoutCache(string dllPath)
75+
{
76+
ModuleDefMD mod = ModuleDefMD.Load(File.ReadAllBytes(dllPath), _modCtx);
77+
mod.EnableTypeDefFindCache = true;
78+
return mod;
79+
}
80+
7481
private void LoadNetStandard()
7582
{
7683
string netstandardDllPath = _assemblyPathResolver.ResolveAssembly("netstandard", false);

Editor/Settings/HybridCLRSettingProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class HybridCLRSettingsProvider : SettingsProvider
2121
private SerializedProperty _externalHotUpdateAssembliyDirs;
2222
private SerializedProperty _strippedAOTDllOutputRootDir;
2323
private SerializedProperty _patchAOTAssemblies;
24+
private SerializedProperty _dontPreserveUnityEngineCoreTypesInLinkXml;
2425
private SerializedProperty _outputLinkFile;
2526
private SerializedProperty _outputAOTGenericReferenceFile;
2627
private SerializedProperty _maxGenericReferenceIteration;
@@ -51,6 +52,7 @@ private void InitGUI()
5152
_externalHotUpdateAssembliyDirs = _serializedObject.FindProperty("externalHotUpdateAssembliyDirs");
5253
_strippedAOTDllOutputRootDir = _serializedObject.FindProperty("strippedAOTDllOutputRootDir");
5354
_patchAOTAssemblies = _serializedObject.FindProperty("patchAOTAssemblies");
55+
_dontPreserveUnityEngineCoreTypesInLinkXml = _serializedObject.FindProperty("dontPreserveUnityEngineCoreTypesInLinkXml");
5456
_outputLinkFile = _serializedObject.FindProperty("outputLinkFile");
5557
_outputAOTGenericReferenceFile = _serializedObject.FindProperty("outputAOTGenericReferenceFile");
5658
_maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration");
@@ -140,6 +142,7 @@ public override void OnGUI(string searchContext)
140142
EditorGUILayout.PropertyField(_externalHotUpdateAssembliyDirs);
141143
EditorGUILayout.PropertyField(_strippedAOTDllOutputRootDir);
142144
EditorGUILayout.PropertyField(_patchAOTAssemblies);
145+
EditorGUILayout.PropertyField(_dontPreserveUnityEngineCoreTypesInLinkXml);
143146
EditorGUILayout.PropertyField(_outputLinkFile);
144147
EditorGUILayout.PropertyField(_outputAOTGenericReferenceFile);
145148
EditorGUILayout.PropertyField(_maxGenericReferenceIteration);

Editor/Settings/HybridCLRSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class HybridCLRSettings : ScriptableSingleton<HybridCLRSettings>
3939
[Tooltip("supplementary metadata assembly names(without .dll suffix)")]
4040
public string[] patchAOTAssemblies;
4141

42+
[Tooltip("don't preserve UnityEngine core types in link.xml")]
43+
public bool dontPreserveUnityEngineCoreTypesInLinkXml = false;
44+
4245
[Tooltip("output file of automatic generated link.xml by scanning hot update assemblies")]
4346
public string outputLinkFile = "HybridCLRGenerate/link.xml";
4447

0 commit comments

Comments
 (0)