少年修仙传客户端基础资源
client_Wu Xijin
2019-01-16 d53a782e2fd91ffc47dad0dd5a080ee451a351b7
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using System;
using Assets.Editor.Treemap;
using UnityEditor;
using UnityEngine;
using UnityEditor.MemoryProfiler;
using System.Linq;
using System.Collections.Generic;
 
namespace MemoryProfilerWindow
{
    public class Inspector
    {
        ThingInMemory _selectedThing;
        private ThingInMemory[] _shortestPath;
        private ShortestPathToRootFinder _shortestPathToRootFinder;
        private static int s_InspectorWidth = 400;
        Vector2 _scrollPosition;
        MemoryProfilerWindow _hostWindow;
        CrawledMemorySnapshot _unpackedCrawl;
        PrimitiveValueReader _primitiveValueReader;
        Dictionary<ulong, ThingInMemory> objectCache = new Dictionary<ulong, ThingInMemory>();
        private Texture2D _textureObject;
        private int _prevInstance;
        private float _textureSize = 128.0f;
 
 
        static class Styles
        {
            public static GUIStyle entryEven = "OL EntryBackEven";
            public static GUIStyle entryOdd = "OL EntryBackOdd";
        }
 
        GUILayoutOption labelWidth = GUILayout.Width(150);
 
        public Inspector(MemoryProfilerWindow hostWindow, CrawledMemorySnapshot unpackedCrawl, PackedMemorySnapshot snapshot)
        {
            _unpackedCrawl = unpackedCrawl;
            _hostWindow = hostWindow;
            _shortestPathToRootFinder = new ShortestPathToRootFinder(unpackedCrawl);
            _primitiveValueReader = new PrimitiveValueReader(_unpackedCrawl.virtualMachineInformation, _unpackedCrawl.managedHeap);
        }
 
        public float width
        {
            get { return s_InspectorWidth; }
        }
 
        public void SelectThing(ThingInMemory thing)
        {
            _selectedThing = thing;
            _shortestPath = _shortestPathToRootFinder.FindFor(thing);
        }
 
        public void Draw()
        {
            GUILayout.BeginArea(new Rect(_hostWindow.position.width - s_InspectorWidth, 25, s_InspectorWidth, _hostWindow.position.height - 25f));
            _scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
 
            if (_selectedThing == null)
                GUILayout.Label("Select an object to see more info");
            else
            {
                var nativeObject = _selectedThing as NativeUnityEngineObject;
                if (nativeObject != null)
                {
                    GUILayout.Label("NativeUnityEngineObject", EditorStyles.boldLabel);
                    GUILayout.Space(5);
                    EditorGUILayout.LabelField("Name", nativeObject.name);
                    EditorGUILayout.LabelField("ClassName", nativeObject.className);
                    EditorGUILayout.LabelField("instanceID", nativeObject.instanceID.ToString());
                    EditorGUILayout.LabelField("isDontDestroyOnLoad", nativeObject.isDontDestroyOnLoad.ToString());
                    EditorGUILayout.LabelField("isPersistent", nativeObject.isPersistent.ToString());
                    EditorGUILayout.LabelField("isManager", nativeObject.isManager.ToString());
                    EditorGUILayout.LabelField("hideFlags", nativeObject.hideFlags.ToString());
                    EditorGUILayout.LabelField("hideFlags", nativeObject.size.ToString());
                    DrawSpecificTexture2D(nativeObject);
                }
 
                var managedObject = _selectedThing as ManagedObject;
                if (managedObject != null)
                {
                    GUILayout.Label("ManagedObject");
                    EditorGUILayout.LabelField("Type", managedObject.typeDescription.name);
                    EditorGUILayout.LabelField("Address", managedObject.address.ToString("X"));
                    EditorGUILayout.LabelField("size", managedObject.size.ToString());
 
                    if (managedObject.typeDescription.name == "System.String")
                        EditorGUILayout.LabelField("value", StringTools.ReadString(_unpackedCrawl.managedHeap.Find(managedObject.address, _unpackedCrawl.virtualMachineInformation), _unpackedCrawl.virtualMachineInformation));
                    DrawFields(managedObject);
 
                    if (managedObject.typeDescription.isArray)
                    {
                        DrawArray(managedObject);
                    }
                }
 
                if (_selectedThing is GCHandle)
                {
                    GUILayout.Label("GCHandle");
                    EditorGUILayout.LabelField("size", _selectedThing.size.ToString());
                }
 
                var staticFields = _selectedThing as StaticFields;
                if (staticFields != null)
                {
                    GUILayout.Label("Static Fields");
                    GUILayout.Label("Of type: " + staticFields.typeDescription.name);
                    GUILayout.Label("size: " + staticFields.size);
 
                    DrawFields(staticFields.typeDescription, new BytesAndOffset() { bytes = staticFields.typeDescription.staticFieldBytes, offset = 0, pointerSize = _unpackedCrawl.virtualMachineInformation.pointerSize}, true);
                }
 
                if (managedObject == null)
                {
                    GUILayout.Space(10);
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("References:", labelWidth);
                    GUILayout.BeginVertical();
                    DrawLinks(_selectedThing.references);
                    GUILayout.EndVertical();
                    GUILayout.EndHorizontal();
                }
 
                GUILayout.Space(10);
                GUILayout.Label("Referenced by:");
                DrawLinks(_selectedThing.referencedBy);
 
                GUILayout.Space(10);
                if (_shortestPath != null)
                {
                    if (_shortestPath.Length > 1)
                    {
                        GUILayout.Label("ShortestPathToRoot");
                        DrawLinks(_shortestPath);
                    }
                    string reason;
                    _shortestPathToRootFinder.IsRoot(_shortestPath.Last(), out reason);
                    GUILayout.Label("This is a root because:");
                    GUILayout.TextArea(reason);
                }
                else
                {
                    GUILayout.TextArea("No root is keeping this object alive. It will be collected next UnloadUnusedAssets() or scene load");
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndArea();
        }
 
        private Texture2D GetTexture(NativeUnityEngineObject nativeObject)
        {
            // If the texture has a name, try to match it to a loaded object
            if(!string.IsNullOrEmpty(nativeObject.name))
            {
                Texture2D[] loadedTextures = Resources.FindObjectsOfTypeAll<Texture2D>();
 
                for (int i = 0; i < loadedTextures.Length; i++) 
                {
                    if(loadedTextures[i].name == nativeObject.name)
                    {
                        return loadedTextures[i];
                    }
                }
            }
            // None matched
            return null;
        }
 
        private void DrawSpecificTexture2D(NativeUnityEngineObject nativeObject)
        {
            if (nativeObject.className != "Texture2D")
            {
                _textureObject = null;
                return;
            }
            EditorGUILayout.HelpBox("Watching Texture Detail Data is only for Editor.", MessageType.Warning, true);
            if (_prevInstance != nativeObject.instanceID)
            {
                // Attempt to match the texture to one in editor
                _textureObject = GetTexture(nativeObject);
                _prevInstance = nativeObject.instanceID;
            }
            if (_textureObject != null)
            {
                EditorGUILayout.LabelField("textureInfo: " + _textureObject.width + "x" + _textureObject.height + " " + _textureObject.format);
                EditorGUILayout.ObjectField(_textureObject, typeof(Texture2D), false);
                _textureSize = EditorGUILayout.Slider(_textureSize, 100.0f, 1024.0f);
                GUILayout.Label(_textureObject, GUILayout.Width(_textureSize), GUILayout.Height(_textureSize * _textureObject.height / _textureObject.width));
            }
            else
            {
                EditorGUILayout.LabelField("Can't instance texture,maybe it was already released.");
            }
        }
 
        private void DrawArray(ManagedObject managedObject)
        {
            var typeDescription = managedObject.typeDescription;
            int elementCount = ArrayTools.ReadArrayLength(_unpackedCrawl.managedHeap, managedObject.address, typeDescription, _unpackedCrawl.virtualMachineInformation);
            GUILayout.Label("element count: " + elementCount);
            int rank = typeDescription.arrayRank;
            GUILayout.Label("arrayRank: " + rank);
            if (_unpackedCrawl.typeDescriptions[typeDescription.baseOrElementTypeIndex].isValueType)
            {
                GUILayout.Label("Cannot yet display elements of value type arrays");
                return;
            }
            if (rank != 1)
            {
                GUILayout.Label("Cannot display non rank=1 arrays yet.");
                return;
            }
 
            var pointers = new List<UInt64>();
            for (int i = 0; i != elementCount; i++)
            {
                pointers.Add(_primitiveValueReader.ReadPointer(managedObject.address + (UInt64)_unpackedCrawl.virtualMachineInformation.arrayHeaderSize + (UInt64)(i * _unpackedCrawl.virtualMachineInformation.pointerSize)));
            }
            GUILayout.Label("elements:");
            DrawLinks(pointers);
        }
 
        private void DrawFields(TypeDescription typeDescription, BytesAndOffset bytesAndOffset, bool useStatics = false)
        {
            int counter = 0;
        
            foreach (var field in TypeTools.AllFieldsOf(typeDescription, _unpackedCrawl.typeDescriptions, useStatics ? TypeTools.FieldFindOptions.OnlyStatic : TypeTools.FieldFindOptions.OnlyInstance))
            {
                counter++;
                var gUIStyle = counter % 2 == 0 ? Styles.entryEven : Styles.entryOdd;
                gUIStyle.margin = new RectOffset(0, 0, 0, 0);
                gUIStyle.overflow = new RectOffset(0, 0, 0, 0);
                gUIStyle.padding = EditorStyles.label.padding;
                GUILayout.BeginHorizontal(gUIStyle);
                GUILayout.Label(field.name, labelWidth);
                GUILayout.BeginVertical();
                DrawValueFor(field, bytesAndOffset.Add(field.offset));
                GUILayout.EndVertical();
                GUILayout.EndHorizontal();
            }
        }
 
        private void DrawFields(ManagedObject managedObject)
        {
            if (managedObject.typeDescription.isArray)
                return;
            GUILayout.Space(10);
            GUILayout.Label("Fields:");
            DrawFields(managedObject.typeDescription, _unpackedCrawl.managedHeap.Find(managedObject.address, _unpackedCrawl.virtualMachineInformation));
        }
 
        private void DrawValueFor(FieldDescription field, BytesAndOffset bytesAndOffset)
        {
            var typeDescription = _unpackedCrawl.typeDescriptions[field.typeIndex];
 
            switch (typeDescription.name)
            {
                case "System.Int32":
                    GUILayout.Label(_primitiveValueReader.ReadInt32(bytesAndOffset).ToString());
                    break;
                case "System.Int64":
                    GUILayout.Label(_primitiveValueReader.ReadInt64(bytesAndOffset).ToString());
                    break;
                case "System.UInt32":
                    GUILayout.Label(_primitiveValueReader.ReadUInt32(bytesAndOffset).ToString());
                    break;
                case "System.UInt64":
                    GUILayout.Label(_primitiveValueReader.ReadUInt64(bytesAndOffset).ToString());
                    break;
                case "System.Int16":
                    GUILayout.Label(_primitiveValueReader.ReadInt16(bytesAndOffset).ToString());
                    break;
                case "System.UInt16":
                    GUILayout.Label(_primitiveValueReader.ReadUInt16(bytesAndOffset).ToString());
                    break;
                case "System.Byte":
                    GUILayout.Label(_primitiveValueReader.ReadByte(bytesAndOffset).ToString());
                    break;
                case "System.SByte":
                    GUILayout.Label(_primitiveValueReader.ReadSByte(bytesAndOffset).ToString());
                    break;
                case "System.Char":
                    GUILayout.Label(_primitiveValueReader.ReadChar(bytesAndOffset).ToString());
                    break;
                case "System.Boolean":
                    GUILayout.Label(_primitiveValueReader.ReadBool(bytesAndOffset).ToString());
                    break;
                case "System.Single":
                    GUILayout.Label(_primitiveValueReader.ReadSingle(bytesAndOffset).ToString());
                    break;
                case "System.Double":
                    GUILayout.Label(_primitiveValueReader.ReadDouble(bytesAndOffset).ToString());
                    break;
                case "System.IntPtr":
                    GUILayout.Label(_primitiveValueReader.ReadPointer(bytesAndOffset).ToString("X"));
                    break;
                default:
 
                    if (!typeDescription.isValueType)
                    {
                        ThingInMemory item = GetThingAt(bytesAndOffset.ReadPointer());
                        if (item == null)
                        {
                            EditorGUI.BeginDisabledGroup(true);
                            GUILayout.Button("Null");
                            EditorGUI.EndDisabledGroup();
                        }
                        else
                        {
                            DrawLinks(new ThingInMemory[] { item });
                        }
                    }
                    else
                    {
                        DrawFields(typeDescription, bytesAndOffset);
                    }
                    break;
            }
        }
 
        private ThingInMemory GetThingAt(ulong address)
        {
            if (!objectCache.ContainsKey(address))
            {
                objectCache[address] = _unpackedCrawl.allObjects.OfType<ManagedObject>().FirstOrDefault(mo => mo.address == address);
            }
 
            return objectCache[address];
        }
 
        /*
        private Item FindItemPointedToByManagedFieldAt(BytesAndOffset bytesAndOffset)
        {
            var stringAddress = _primitiveValueReader.ReadPointer(bytesAndOffset);
            return
                _items.FirstOrDefault(i =>
                    {
                        var m = i._thingInMemory as ManagedObject;
                        if (m != null)
                        {
                            return m.address == stringAddress;
                        }
                        return false;
                    });
        }*/
 
        private void DrawLinks(IEnumerable<UInt64> pointers)
        {
            DrawLinks(pointers.Select(p => GetThingAt(p)));
        }
 
        private void DrawLinks(IEnumerable<ThingInMemory> thingInMemories)
        {
            var c = GUI.backgroundColor;
            GUI.skin.button.alignment = TextAnchor.UpperLeft;
            foreach (var rb in thingInMemories)
            {
                EditorGUI.BeginDisabledGroup(rb == _selectedThing || rb == null);
 
                GUI.backgroundColor = ColorFor(rb);
 
                var caption = rb == null ? "null" : rb.caption;
 
                var managedObject = rb as ManagedObject;
                if (managedObject != null && managedObject.typeDescription.name == "System.String")
                    caption = StringTools.ReadString(_unpackedCrawl.managedHeap.Find(managedObject.address, _unpackedCrawl.virtualMachineInformation), _unpackedCrawl.virtualMachineInformation);
 
                if (GUILayout.Button(caption))
                    _hostWindow.SelectThing(rb);
                EditorGUI.EndDisabledGroup();
            }
            GUI.backgroundColor = c;
        }
 
        private Color ColorFor(ThingInMemory rb)
        {
            if (rb == null)
                return Color.gray;
            if (rb is NativeUnityEngineObject)
                return Color.red;
            if (rb is ManagedObject)
                return Color.Lerp(Color.blue, Color.white, 0.5f);
            if (rb is GCHandle)
                return Color.magenta;
            if (rb is StaticFields)
                return Color.yellow;
 
            throw new ArgumentException("Unexpected type: " + rb.GetType());
        }
    }
}