private void Clear()
 {
     m_snaps.Clear();
     compSnap1    = null;
     compSnap2    = null;
     m_selectSnap = null;
 }
    private void CalculateDiff(LuaMemory lm1, LuaMemory lm2)
    {
        if (lm1 == null || lm2 == null)
        {
            return;
        }

        CalculateDiffByID(lm1, lm2);
    }
    private void SnapShot()
    {
        if (!Application.isPlaying)
        {
            ShowNotification(new GUIContent("请先运行游戏"));
            return;
        }

        LuaEnv      luaEnv = MemoryProfileTest.GetLuaEnv();
        LuaFunction lf     = luaEnv.Global.GetInPath <LuaFunction>("memory.snapshot");

        if (lf == null)
        {
            Debug.LogError("不存在lua函数");
        }
        string text = lf.Func <string, string>("");


        LuaMemory memory = new LuaMemory(++m_snapCount);

        string[] lines = text.Split(new string[] { "\n" }, StringSplitOptions.None);
        for (int i = 1; i < lines.Length; i++)
        {
            string line = lines[i];
            if (string.IsNullOrEmpty(line))
            {
                continue;
            }

            string[]      fields = line.Split(new char[] { ':' }, 5);
            LuaMemoryItem item   = new LuaMemoryItem();
            item.RawText = line;
            item.Name    = fields[0].Trim();
            item.Size    = long.Parse(fields[1].Trim());
            item.Type    = fields[2].Trim();
            item.ID      = fields[3].Trim();
            item.Info    = fields[4].Trim();

            //Debug.Log(line);
            //Debug.Log(item.Name + "  " + item.Size + "  " + item.Type + "  " + item.ID + "  " + item.Info);
            //Debug.Log(item.Name + ":" + item.Size + ":" + item.Type + ":" + item.ID + ":" + item.Info);

            memory.MemoryList.Add(item);
        }

        m_selectSnap = memory;
        m_snaps.Add(memory);
        SetSortType(m_sortType);
    }
 private void LeftView(float height)
 {
     GUI.Box(new Rect(30, 74, 140, height), "");
     GUILayout.BeginArea(new Rect(30, 75, 140, height));
     m_snapScroll = GUILayout.BeginScrollView(m_snapScroll);
     for (int i = 0; i < m_snaps.Count; i++)
     {
         if (!m_showChange && m_snaps[i].Id == m_selectSnap.Id)
         {
             GUI.contentColor = Color.green;
         }
         if (GUILayout.Button(m_snaps[i].Name + "(" + m_snaps[i].Time + ")"))
         {
             m_showChange = false;
             m_selectSnap = m_snaps[i];
         }
         GUI.contentColor = Color.white;
     }
     GUILayout.EndScrollView();
     GUILayout.EndArea();
 }
    private void CalculateDiffByID(LuaMemory lm1, LuaMemory lm2)
    {
        ItemSortType rawSort = m_sortType;

        if (ItemSortType.ID == m_sortType)
        {
            m_sortDir = !m_sortDir;
        }
        SetSortType(ItemSortType.ID, lm1.MemoryList);

        if (ItemSortType.ID == m_sortType)
        {
            m_sortDir = !m_sortDir;
        }
        SetSortType(ItemSortType.ID, lm2.MemoryList);

        changeList.Clear();
        for (int i = 0, j = 0; i < lm1.MemoryList.Count || j < lm2.MemoryList.Count;)
        {
            if (i >= lm1.MemoryList.Count)
            {
                ChangeItem addItem = new ChangeItem();
                addItem.CType      = ChangeType.Add;
                addItem.Item       = lm2.MemoryList[j];
                addItem.SizeChange = lm2.MemoryList[j].Size;
                j++;
                changeList.Add(addItem);
                continue;
            }

            if (j >= lm2.MemoryList.Count)
            {
                ChangeItem removeItem = new ChangeItem();
                removeItem.CType      = ChangeType.Remove;
                removeItem.Item       = lm1.MemoryList[i];
                removeItem.SizeChange = -lm1.MemoryList[i].Size;
                i++;
                changeList.Add(removeItem);
                continue;
            }

            LuaMemoryItem item1 = lm1.MemoryList[i];
            LuaMemoryItem item2 = lm2.MemoryList[j];

            ChangeItem changeItem = new ChangeItem();
            changeList.Add(changeItem);
            if (string.Compare(item1.ID, item2.ID) > 0)//add
            {
                changeItem.CType      = ChangeType.Add;
                changeItem.Item       = item2;
                changeItem.SizeChange = item2.Size;
                j++;
            }
            else if (string.Compare(item1.ID, item2.ID) < 0)//remove
            {
                changeItem.CType      = ChangeType.Remove;
                changeItem.Item       = item1;
                changeItem.SizeChange = -item1.Size;
                i++;
            }
            else if (string.Compare(item1.ID, item2.ID) == 0)
            {
                if (item1.Size > item2.Size)
                {
                    changeItem.CType         = ChangeType.SizeSub;
                    changeItem.SizeChangeStr = item1.Size + "->" + item2.Size;
                }
                else if (item1.Size < item2.Size)
                {
                    changeItem.CType         = ChangeType.SizeAdd;
                    changeItem.SizeChangeStr = item1.Size + "->" + item2.Size;
                }
                else
                {
                    changeItem.CType = ChangeType.None;
                }
                changeItem.SizeChange = item2.Size - item1.Size;
                changeItem.Item       = item1;
                i++;
                j++;
            }
        }

        m_sortType = rawSort;
        SortChangeList();
    }
    private void UpBtnView()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("SnapShot", GUILayout.Height(20), GUILayout.Width(80)))
        {
            SnapShot();
        }

        LuaMemory[] otherSnaps = null;
        if (m_selectSnap != null && m_snaps.Count > 1)
        {
            string[] otherSnapNames = new string[m_snaps.Count - 1];
            otherSnaps = new LuaMemory[m_snaps.Count - 1];
            int j = 0;
            for (int i = 0; i < m_snaps.Count; i++)
            {
                if (m_snaps[i].Id == m_selectSnap.Id)
                {
                    continue;
                }
                otherSnapNames[j] = m_snaps[i].Name;
                otherSnaps[j]     = m_snaps[i];
                j++;
            }
            difSelectIndex = EditorGUI.Popup(new Rect(82, 2, 100, 20), difSelectIndex, otherSnapNames);
            GUILayout.Space(100);
        }
        else
        {
            difSelectIndex = EditorGUI.Popup(new Rect(82, 2, 100, 20), difSelectIndex, new string[] { });
            GUILayout.Space(100);
        }

        if (GUILayout.Button("Diff", GUILayout.Height(20), GUILayout.Width(80)))
        {
            if (m_snaps.Count < 2)
            {
                Debug.LogError("请至少获得两个快照");
                return;
            }
            LuaMemory lm = otherSnaps[difSelectIndex];

            m_showChange = true;
            int count = m_snaps.Count;
            compSnap1 = lm;
            compSnap2 = m_selectSnap;
            CalculateDiff(compSnap1, compSnap2);
        }

        if (GUILayout.Button("Clear", GUILayout.Height(20), GUILayout.Width(80)))
        {
            Clear();
        }

        if (GUILayout.Button("Load", GUILayout.Height(20), GUILayout.Width(80)))
        {
            string filePath = EditorUtility.OpenFilePanel("加载Lua内存分析文件", "", "txt");
            string json     = File.ReadAllText(filePath);
            if (!string.IsNullOrEmpty(json))
            {
                LuaMemory lm = JsonUtility.FromJson <LuaMemory>(json);
                if (lm != null)
                {
                    for (int i = 0; i < m_snaps.Count; i++)
                    {
                        if (m_snaps[i].Id == lm.Id)
                        {
                            m_selectSnap = m_snaps[i];

                            Debug.LogError("已经存在该文件");
                            return;
                        }
                    }
                    m_selectSnap = lm;
                    m_snaps.Add(lm);
                    SetSortType(m_sortType);
                }
            }
        }

        if (GUILayout.Button("Save", GUILayout.Height(20), GUILayout.Width(80)))
        {
            Debug.Log("Save");
            if (m_selectSnap != null)
            {
                string filePath = EditorUtility.SaveFilePanel("保存Lua内存分析文件", "", m_selectSnap.Name, "txt");
                string json     = JsonUtility.ToJson(m_selectSnap);
                File.WriteAllText(filePath, json);
            }
        }
        GUILayout.EndHorizontal();
    }