-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnityHeapDumper.cs
111 lines (99 loc) · 3.33 KB
/
UnityHeapDumper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace UnityHeapDumper
{
public class UnityHeapDumper : IHeapDumper, IDumpContext
{
[MenuItem("Tools/Dump")]
private static void Dump()
{
IHeapDumper dumper = new UnityHeapDumper();
dumper.Dump(@"C:\Users\stefa\Downloads\heap.xml");
}
private IFactory<ITypeData, Type> typeDataFactory;
private IFactory<IInstanceData, object> instanceDataFactory;
private IFieldDataFactory fieldDataFactory;
private IDumpWriter dumpWriter;
public UnityHeapDumper()
{
typeDataFactory = new TypeDataFactory(this);
instanceDataFactory = new InstanceDataFactory(this);
fieldDataFactory = new FieldDataFactory(this);
dumpWriter = new XMLDumpWriter();
}
IFactory<ITypeData, Type> IDumpContext.TypeDataFactory
{
get
{
return typeDataFactory;
}
}
IFactory<IInstanceData, object> IDumpContext.InstanceDataFactory
{
get
{
return instanceDataFactory;
}
}
IFieldDataFactory IDumpContext.FieldDataFactory
{
get
{
return fieldDataFactory;
}
}
void IHeapDumper.Dump(string path)
{
List<IFieldData> staticFields = GetStaticFields();
HashSet<int> seenInstances = new HashSet<int>();
dumpWriter.Open(path);
foreach (var staticField in staticFields)
{
Debug.LogFormat("type={0} field={1} size={2}", staticField.DeclaringType, staticField.Name, staticField.InstanceData.GetSize());
seenInstances.Clear();
dumpWriter.WriteField(staticField, seenInstances);
}
dumpWriter.Close();
}
private List<IFieldData> GetStaticFields()
{
var thisNamespace = typeof(UnityHeapDumper).Namespace;
var staticFields = new List<IFieldData>();
var assemblies = GetAppAssemblies();
foreach (var assembly in assemblies)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.Namespace == thisNamespace)
{
continue;
}
var typeData = typeDataFactory.Create(type);
IList<IFieldData> typeStaticFields = typeData.StaticFields;
if (typeStaticFields.Count > 0)
{
staticFields.AddRange(typeStaticFields);
}
}
}
return staticFields;
}
private List<Assembly> GetAppAssemblies()
{
var appAssemblies = new List<Assembly>(3);
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
if (assembly.FullName.Contains("Assembly-CSharp"))
{
appAssemblies.Add(assembly);
}
}
return appAssemblies;
}
}
}