Files
2026-03-04 09:37:33 +08:00

136 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
namespace DebuggingEssentials
{
public class MemorySnapshot
{
public FastSortedDictionary<string, MemoryInstanceType> memoryTypesLookup = new FastSortedDictionary<string, MemoryInstanceType>(1024);
public float tStamp;
public GUIChangeBool selected = new GUIChangeBool(value: false);
public CompareResult compareResult;
public bool hasCleanedDifCompare;
public void Reset()
{
memoryTypesLookup.Clear();
hasCleanedDifCompare = false;
}
public void ScanMemory(CompareMode compareMode)
{
tStamp = Time.realtimeSinceStartup;
UnityEngine.Object[] array = Resources.FindObjectsOfTypeAll<UnityEngine.Object>();
foreach (UnityEngine.Object obj in array)
{
Type type = obj.GetType();
string name = type.Name;
if (!memoryTypesLookup.lookup.TryGetValue(name, out var value))
{
value = new MemoryInstanceType(type);
memoryTypesLookup.Add(name, value);
}
int instanceID = obj.GetInstanceID();
value.instances.Add(instanceID, new MemoryObject(obj.name, instanceID, obj.hideFlags, Helper.IsPrefab(obj)));
}
memoryTypesLookup.Sort(compareMode);
}
public void CleanDifSnapshot(CompareMode compareMode)
{
if (hasCleanedDifCompare)
{
return;
}
hasCleanedDifCompare = true;
FastList<ItemHolder<string, MemoryInstanceType>> list = memoryTypesLookup.list;
for (int i = 0; i < list.Count; i++)
{
FastList<MemoryObject> list2 = list.items[i].value.instances.list;
for (int j = 0; j < list2.Count; j++)
{
MemoryObject memoryObject = list2.items[j];
CompareResult compareResult = memoryObject.compareResult;
for (int k = j + 1; k < list2.Count; k++)
{
MemoryObject memoryObject2 = list2.items[k];
if (memoryObject2.compareResult != compareResult && memoryObject2.name == memoryObject.name)
{
list2.RemoveAt(k);
list2.RemoveAt(j--);
break;
}
}
}
if (list2.Count == 0)
{
list.RemoveAt(i--);
}
}
memoryTypesLookup.Sort(compareMode);
}
public static void CompareMemorySnapshots(MemorySnapshot oldSnapshot, MemorySnapshot newSnapshot, MemorySnapshot difSnapshot, CompareMode compareMode)
{
difSnapshot.Reset();
CompareMemorySnapshots(oldSnapshot, newSnapshot, difSnapshot, CompareResult.Removed);
CompareMemorySnapshots(newSnapshot, oldSnapshot, difSnapshot, CompareResult.New);
difSnapshot.memoryTypesLookup.Sort(compareMode);
}
public static void CompareMemorySnapshots(MemorySnapshot s, MemorySnapshot d, MemorySnapshot difSnapshot, CompareResult compareResult)
{
FastSortedDictionary<string, MemoryInstanceType> fastSortedDictionary = s.memoryTypesLookup;
FastSortedDictionary<string, MemoryInstanceType> fastSortedDictionary2 = d.memoryTypesLookup;
FastList<ItemHolder<string, MemoryInstanceType>> list = fastSortedDictionary.list;
for (int i = 0; i < list.Count; i++)
{
ItemHolder<string, MemoryInstanceType> itemHolder = list.items[i];
MemoryInstanceType value = itemHolder.value;
MemoryInstanceType value2 = null;
string key = itemHolder.key;
bool flag = true;
if (fastSortedDictionary2.lookup.TryGetValue(key, out var value3))
{
FastSortedHashSet<int, MemoryObject> instances = value.instances;
HashSet<int> lookup = value3.instances.lookup;
FastList<MemoryObject> list2 = instances.list;
for (int j = 0; j < list2.Count; j++)
{
MemoryObject memoryObject = list2.items[j];
if (lookup.Contains(memoryObject.instanceId))
{
continue;
}
if (flag)
{
flag = false;
if (!difSnapshot.memoryTypesLookup.lookup.TryGetValue(key, out value2))
{
value2 = new MemoryInstanceType(value.type);
difSnapshot.memoryTypesLookup.Add(key, value2);
}
}
value2.instances.Add(memoryObject.instanceId, new MemoryObject(memoryObject, compareResult));
}
}
else
{
value2 = new MemoryInstanceType(value.type);
difSnapshot.memoryTypesLookup.Add(key, value2);
FastList<MemoryObject> list3 = value.instances.list;
for (int k = 0; k < list3.Count; k++)
{
value2.instances.Add(list3.items[k].instanceId, new MemoryObject(list3.items[k], compareResult));
}
}
}
}
}
}