| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine; | 
| using UnityEditor; | 
| using System.Reflection; | 
| using System; | 
| using System.IO; | 
| using System.Text.RegularExpressions; | 
| using System.Linq; | 
|   | 
| public class ConfusionEditorWindow : EditorWindow | 
| { | 
|     [MenuItem("Tools/Confusion")] | 
|     static void Init() | 
|     { | 
|         var _win = CreateInstance<ConfusionEditorWindow>(); | 
|         _win.minSize = new Vector2(420, 400); | 
|         _win.maxSize = _win.minSize; | 
|         _win.Show(); | 
|     } | 
|   | 
|     private string LS_KEY_CONFIG_PATH; | 
|   | 
|     private string m_MapFilePath = ""; | 
|     private List<string> removePathList = new List<string>(); | 
|   | 
|     private Vector2 m_ScrollVec2; | 
|     private Color m_LabelColor = new Color(.9f, .9f, .8f, 1); | 
|   | 
|     public static ConfusionSetting Setting { get; private set; } | 
|   | 
|     void OnEnable() | 
|     { | 
|         ConfusionController.Reset(); | 
|         LS_KEY_CONFIG_PATH = Application.dataPath + "LS_KEY_CONFIG_PATH"; | 
|         BuildConfig(); | 
|     } | 
|   | 
|     private void BuildConfig() | 
|     { | 
|         // 确认配置位置 | 
|         string _configPath = EditorPrefs.GetString(LS_KEY_CONFIG_PATH, ""); | 
|   | 
|         // 有配置地址,读取配置地址 | 
|         if (string.IsNullOrEmpty(_configPath)) | 
|         { | 
|             var _files = Directory.GetFiles(Application.dataPath, "*.cs", SearchOption.AllDirectories); | 
|             foreach (var _file in _files) | 
|             { | 
|                 if (_file.Contains("ConfusionEditorWindow")) | 
|                 { | 
|                     _configPath = Path.GetDirectoryName(_file).Replace("\\", "/"); | 
|                     break; | 
|                 } | 
|             } | 
|   | 
|             _configPath += "/config.json"; | 
|         } | 
|   | 
|         // 判断配置地址下是否有配置文件 | 
|         if (File.Exists(_configPath)) | 
|         { | 
|             string _json = null; | 
|             using (StreamReader _sr = new StreamReader(_configPath)) | 
|             { | 
|                 _json = _sr.ReadToEnd(); | 
|             } | 
|   | 
|             if (!string.IsNullOrEmpty(_json)) | 
|             { | 
|                 Setting = JsonUtility.FromJson<ConfusionSetting>(_json); | 
|             } | 
|         } | 
|   | 
|         if (Setting == null) | 
|         { | 
|             Setting = new ConfusionSetting(); | 
|             EditorPrefs.SetString(LS_KEY_CONFIG_PATH, _configPath); | 
|             File.Create(_configPath); | 
|         } | 
|   | 
|         if (Setting.confusionDirectorys != null) | 
|         { | 
|             foreach (var _info in Setting.confusionDirectorys) | 
|             { | 
|                 ConfusionController.SelectDirectory(_info.path, _info.includeChild); | 
|             } | 
|         } | 
|     } | 
|   | 
|     private void SaveConfig() | 
|     { | 
|         ConfusionSetting _setting = new ConfusionSetting(); | 
|         _setting.confuseField = Setting.confuseField; | 
|         _setting.confuseMethod = Setting.confuseMethod; | 
|   | 
|         int _count = ConfusionController.SelectDirectoryCount(); | 
|         _setting.confusionDirectorys = new ConfusionDirectoryInfo[_count]; | 
|         int _index = 0; | 
|         ConfusionController.ForeachSelectDirectory((_path, _info) => | 
|         { | 
|             _setting.confusionDirectorys[_index] = new ConfusionDirectoryInfo(); | 
|             _setting.confusionDirectorys[_index].path = _path; | 
|             _setting.confusionDirectorys[_index].includeChild = _info.includeChild; | 
|             _index++; | 
|         }); | 
|   | 
|         string _json = JsonUtility.ToJson(_setting); | 
|         Debug.Log(_json); | 
|   | 
|         string _configPath = EditorPrefs.GetString(LS_KEY_CONFIG_PATH, ""); | 
|         if (string.IsNullOrEmpty(_configPath)) | 
|         { | 
|             Debug.LogErrorFormat("找不到配置地址!"); | 
|             return; | 
|         } | 
|   | 
|         using (StreamWriter _sw = new StreamWriter(_configPath)) | 
|         { | 
|             _sw.Write(_json); | 
|         } | 
|     } | 
|   | 
|     void OnGUI() | 
|     { | 
|         Color _defaultColor = GUI.color; | 
|         EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); | 
|         GUI.color = m_LabelColor; | 
|         GUILayout.Label("ConfusionDiretory", EditorStyles.toolbarButton, GUILayout.Width(110)); | 
|         GUI.color = _defaultColor; | 
|         if (GUILayout.Button("ADD +", EditorStyles.toolbarButton, GUILayout.ExpandWidth(true))) | 
|         { | 
|             var _path = EditorUtility.OpenFolderPanel("选择要被混淆的脚本文件夹", | 
|                                                       Application.dataPath, ""); | 
|             if (!string.IsNullOrEmpty(_path)) | 
|             { | 
|                 ConfusionController.SelectDirectory(_path); | 
|             } | 
|         } | 
|   | 
|         EditorGUILayout.EndHorizontal(); | 
|   | 
|         if (ConfusionController.SelectDirectoryCount() == 0) | 
|         { | 
|             EditorGUILayout.HelpBox("点击[ADD+]按钮添加一个想要被混淆的脚本文件夹,可以多次添加\r\n" | 
|                                   + "添加完毕以后点击Confusion按钮进行混淆\r\n" | 
|                                   + "混淆后会生成一个混淆列表文件,可以将混淆的文件恢复回来.", MessageType.Info); | 
|         } | 
|   | 
|         removePathList.Clear(); | 
|   | 
|         m_ScrollVec2 = EditorGUILayout.BeginScrollView(m_ScrollVec2); | 
|   | 
|         int _i = 1; | 
|   | 
|         ConfusionController.ForeachSelectDirectory((key, info) => | 
|         { | 
|             EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); | 
|             GUILayout.Label("└", GUILayout.Width(18)); | 
|             GUILayout.Label(_i.ToString(), EditorStyles.toolbarButton, GUILayout.Width(20)); | 
|             GUILayout.Label(key, EditorStyles.textField, GUILayout.ExpandWidth(true)); | 
|             info.includeChild = GUILayout.Toggle(info.includeChild, | 
|                                                 "IncludeChild", | 
|                                                  EditorStyles.toolbarButton, | 
|                                                  GUILayout.Width(80)); | 
|             if (GUILayout.Button("DEL -", EditorStyles.toolbarButton, GUILayout.Width(70))) | 
|             { | 
|                 removePathList.Add(key); | 
|             } | 
|             EditorGUILayout.EndHorizontal(); | 
|             _i++; | 
|         }); | 
|   | 
|         EditorGUILayout.EndScrollView(); | 
|   | 
|         for (int i = 0; i < removePathList.Count; ++i) | 
|         { | 
|             ConfusionController.RemoveSelectDirectory(removePathList[i]); | 
|         } | 
|   | 
|         EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); | 
|         GUI.color = m_LabelColor; | 
|         GUILayout.Label("Confuse Field", EditorStyles.toolbarButton); | 
|         GUI.color = _defaultColor; | 
|         bool _toggle = Setting.IsConfusePrivateField; | 
|         var _newToggle = GUILayout.Toggle(_toggle, "Private", EditorStyles.toolbarButton, GUILayout.Width(100)); | 
|         if (_toggle != _newToggle) | 
|         { | 
|             Setting.IsConfusePrivateField = _newToggle; | 
|         } | 
|   | 
|         _toggle = Setting.IsConfuseProtectedField; | 
|         _newToggle = GUILayout.Toggle(_toggle, "Protected", EditorStyles.toolbarButton, GUILayout.Width(100)); | 
|         if (_toggle != _newToggle) | 
|         { | 
|             Setting.IsConfuseProtectedField = _newToggle; | 
|         } | 
|   | 
|         _toggle = Setting.IsConfusePublicField; | 
|         _toggle = GUILayout.Toggle(_toggle, "Public", EditorStyles.toolbarButton, GUILayout.Width(100)); | 
|         Setting.IsConfusePublicField = _toggle; | 
|         EditorGUILayout.EndHorizontal(); | 
|   | 
|         EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); | 
|         GUI.color = m_LabelColor; | 
|         GUILayout.Label("Confuse Method", EditorStyles.toolbarButton); | 
|         GUI.color = _defaultColor; | 
|         _toggle = Setting.IsConfusePrivateMethod; | 
|         _toggle = GUILayout.Toggle(_toggle, "Private", EditorStyles.toolbarButton, GUILayout.Width(100)); | 
|         Setting.IsConfusePrivateMethod = _toggle; | 
|   | 
|         _toggle = Setting.IsConfuseProtectedMethod; | 
|         _toggle = GUILayout.Toggle(_toggle, "Protected", EditorStyles.toolbarButton, GUILayout.Width(100)); | 
|         Setting.IsConfuseProtectedMethod = _toggle; | 
|   | 
|         _toggle = Setting.IsConfusePublicMethod; | 
|         _toggle = GUILayout.Toggle(_toggle, "Public", EditorStyles.toolbarButton, GUILayout.Width(100)); | 
|         Setting.IsConfusePublicMethod = _toggle; | 
|         EditorGUILayout.EndHorizontal(); | 
|   | 
|         if (GUILayout.Button("Confusion", GUILayout.Height(100))) | 
|         { | 
|             SaveConfig(); | 
|             Confusion(); | 
|         } | 
|   | 
|         EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); | 
|         GUI.color = m_LabelColor; | 
|         GUILayout.Label("MapFileSavePath", EditorStyles.toolbarButton, GUILayout.Width(110)); | 
|         GUI.color = _defaultColor; | 
|         GUILayout.TextField(ConfusionController.DataModel.ReConfuseMapFileDir, EditorStyles.toolbarTextField, GUILayout.Width(222)); | 
|         if (GUILayout.Button("SELECT", EditorStyles.toolbarButton, GUILayout.Width(70))) | 
|         { | 
|             var _path = EditorUtility.OpenFolderPanel("选择混淆替换对应文件存放路径", | 
|                                                       Application.dataPath, ""); | 
|             if (!string.IsNullOrEmpty(_path)) | 
|             { | 
|                 ConfusionController.DataModel.ReConfuseMapFileDir = _path.Replace("\\", "/"); | 
|             } | 
|         } | 
|         EditorGUILayout.EndHorizontal(); | 
|   | 
|         EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); | 
|         GUI.color = m_LabelColor; | 
|         GUILayout.Label("ReConfusionMapFile", EditorStyles.toolbarButton, GUILayout.Width(110)); | 
|         GUI.color = _defaultColor; | 
|         GUILayout.TextField(m_MapFilePath, EditorStyles.toolbarTextField, GUILayout.Width(222)); | 
|         if (GUILayout.Button("SELECT", EditorStyles.toolbarButton, GUILayout.Width(70))) | 
|         { | 
|             m_MapFilePath = EditorUtility.OpenFilePanel("选择要反混淆的文件", | 
|                                                         ConfusionController.DataModel.ReConfuseMapFileDir, "json"); | 
|         } | 
|         GUILayout.FlexibleSpace(); | 
|         EditorGUILayout.EndHorizontal(); | 
|   | 
|         if (GUILayout.Button("ReConfusion", GUILayout.Height(100))) | 
|         { | 
|             ConfusionController.ReConfusion(m_MapFilePath); | 
|         } | 
|     } | 
|   | 
|     private void Confusion() | 
|     { | 
|         if (ConfusionController.SelectDirectoryCount() == 0) | 
|         { | 
|             EditorUtility.DisplayDialog("警告", "未指定要混淆的文件夹\r\n请点击 [ADD +] 按钮进行添加", "返回"); | 
|             return; | 
|         } | 
|   | 
|         ConfusionController.CollectConfusionDirectory(); | 
|         ConfusionController.DecideClassType(); | 
|         ConfusionController.BuildAssembleRelation(); | 
|         //ConfusionController.ReplaceFieldAndMethod(); | 
|         //ConfusionController.BuildConfusionRelation(); | 
|   | 
|         EditorUtility.ClearProgressBar(); | 
|     } | 
|   | 
| } |