| | |
| | | |
| | | } |
| | | |
| | | public class RemoveUnUsedIconKey |
| | | { |
| | | static List<string> ignoreKeyList = new List<string>() |
| | | { |
| | | "Remark_","GemTypeMini_","AllianceBossRank_","GemTypeMini_","RealmSelectBottom_","XT_TJ_","Fb_", |
| | | "MultipleExp_Icon_","MapNPC_Colour_","LocalMapTaskState_","EquipDefaultIcon_" |
| | | }; |
| | | |
| | | static int taskCount = 1; |
| | | static int completedTaskCount = 0; |
| | | static Dictionary<string, string> iconKeyMap = new Dictionary<string, string>(); |
| | | |
| | | static List<Column> iconKeyRefrences = new List<Column>(); |
| | | static List<string> csharpFileContents = new List<string>(); |
| | | static List<string> usedIconKeys = new List<string>(); |
| | | static List<AnalyzeTask> tasks = new List<AnalyzeTask>(); |
| | | |
| | | [MenuItem("程序/UI Assets/移除无用的IconKey")] |
| | | public static void Remove() |
| | | { |
| | | tasks.Clear(); |
| | | var iconKeys = GetOriginalIconKeys(); |
| | | |
| | | taskCount = iconKeys.Count; |
| | | completedTaskCount = 0; |
| | | |
| | | var count = 0; |
| | | var task = new AnalyzeTask(); |
| | | tasks.Add(task); |
| | | foreach (var key in iconKeys) |
| | | { |
| | | task.Add(new IconKeyInfo() |
| | | { |
| | | key = key, |
| | | unused = false |
| | | }); |
| | | |
| | | if (count >= 200) |
| | | { |
| | | count = 0; |
| | | task = new AnalyzeTask(); |
| | | tasks.Add(task); |
| | | } |
| | | count++; |
| | | } |
| | | |
| | | iconKeyMap = GetIconKeyMap(); |
| | | iconKeyRefrences = GetAllIconKeyRefrences(); |
| | | csharpFileContents = GetAllCSharpFileContents(); |
| | | |
| | | try |
| | | { |
| | | EditorApplication.update += OnUpdate; |
| | | Analyze(); |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | EditorUtility.ClearProgressBar(); |
| | | EditorApplication.update -= OnUpdate; |
| | | Debug.Log(e); |
| | | } |
| | | |
| | | } |
| | | |
| | | static void Analyze() |
| | | { |
| | | foreach (var task in tasks) |
| | | { |
| | | ThreadPool.QueueUserWorkItem(x => |
| | | { |
| | | for (int i = task.iconKeys.Count - 1; i >= 0; i--) |
| | | { |
| | | task.completed++; |
| | | var info = task.iconKeys[i]; |
| | | if (ContainByTables(info.key, ref iconKeyRefrences)) |
| | | { |
| | | //Debug.LogFormat("找到一个被其他配置配件引用的 icon key:{0}", info.key); |
| | | continue; |
| | | } |
| | | |
| | | if (ContainByCSharpFile(info.key, ref csharpFileContents)) |
| | | { |
| | | // Debug.LogFormat("找到一个写在C#文件中的 icon key:{0}", info.key); |
| | | continue; |
| | | } |
| | | |
| | | info.unused = true; |
| | | task.iconKeys[i] = info; |
| | | } |
| | | |
| | | task.done = true; |
| | | }); |
| | | } |
| | | } |
| | | |
| | | static void ProcessUnUsedIconKeys() |
| | | { |
| | | var lines = new List<string>(File.ReadAllLines(Application.dataPath + "/ResourcesOut/Refdata/Config/Icon.txt")); |
| | | var unUsedIconKeys = new List<string>(); |
| | | foreach (var task in tasks) |
| | | { |
| | | foreach (var iconkey in task.iconKeys) |
| | | { |
| | | if (iconkey.unused) |
| | | { |
| | | unUsedIconKeys.Add(iconkey.key); |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (int i = lines.Count - 1; i >= 3; i--) |
| | | { |
| | | var contents = lines[i].Split('\t'); |
| | | if (!contents.IsNullOrEmpty()) |
| | | { |
| | | if (unUsedIconKeys.Contains(contents[0])) |
| | | { |
| | | lines.RemoveAt(i); |
| | | } |
| | | } |
| | | } |
| | | |
| | | File.WriteAllLines(Application.dataPath + "/ResourcesOut/Refdata/Config/Icon.txt", lines.ToArray()); |
| | | } |
| | | |
| | | static Dictionary<string, string> GetIconKeyMap() |
| | | { |
| | | var lines = File.ReadAllLines(Application.dataPath + "/Editor/Config/TxtIconKeys.txt"); |
| | | var map = new Dictionary<string, string>(); |
| | | |
| | | for (int i = 1; i < lines.Length; i++) |
| | | { |
| | | var line = lines[i]; |
| | | var contents = line.Split('\t'); |
| | | if (!contents.IsNullOrEmpty() && contents.Length >= 2) |
| | | { |
| | | map[contents[0]] = contents[1]; |
| | | } |
| | | } |
| | | |
| | | return map; |
| | | } |
| | | |
| | | static List<string> GetOriginalIconKeys() |
| | | { |
| | | var lines = File.ReadAllLines(Application.dataPath + "/ResourcesOut/Refdata/Config/Icon.txt"); |
| | | var iconKeys = new List<string>(); |
| | | for (int i = 3; i < lines.Length; i++) |
| | | { |
| | | var contents = lines[i].Split('\t'); |
| | | if (!contents.IsNullOrEmpty()) |
| | | { |
| | | iconKeys.Add(contents[0]); |
| | | } |
| | | } |
| | | |
| | | return iconKeys; |
| | | } |
| | | |
| | | static List<Column> GetAllIconKeyRefrences() |
| | | { |
| | | var files = FileExtersion.GetFileInfos(Application.dataPath + "/ResourcesOut/Refdata/Config", new string[] { "*.txt", "*.TXT" }); |
| | | |
| | | var columns = new List<Column>(); |
| | | foreach (var file in files) |
| | | { |
| | | var nameWithoutExtension = Path.GetFileNameWithoutExtension(file.FullName); |
| | | if (!iconKeyMap.ContainsKey(nameWithoutExtension)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | var iconKeyField = iconKeyMap[nameWithoutExtension]; |
| | | var lines = File.ReadAllLines(file.FullName); |
| | | var fields = new List<string>(lines[1].Split('\t')); |
| | | var index = fields.IndexOf(iconKeyField); |
| | | |
| | | if (index == -1) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | var column = new Column() |
| | | { |
| | | name = fields[index], |
| | | values = new List<string>() |
| | | }; |
| | | |
| | | columns.Add(column); |
| | | for (int i = 1; i < lines.Length; i++) |
| | | { |
| | | var line = lines[i]; |
| | | var contents = line.Split('\t'); |
| | | column.values.Add(contents[index]); |
| | | } |
| | | } |
| | | |
| | | return columns; |
| | | } |
| | | |
| | | static List<string> GetAllCSharpFileContents() |
| | | { |
| | | var files = FileExtersion.GetFileInfos(Application.dataPath + "/Scripts", new string[] { "*.cs" }); |
| | | var contents = new List<string>(); |
| | | |
| | | foreach (var file in files) |
| | | { |
| | | contents.Add(File.ReadAllText(file.FullName)); |
| | | } |
| | | |
| | | return contents; |
| | | } |
| | | |
| | | static bool ContainByTables(string key, ref List<Column> columns) |
| | | { |
| | | foreach (var column in columns) |
| | | { |
| | | foreach (var value in column.values) |
| | | { |
| | | if (key == value) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | static bool ContainByCSharpFile(string key, ref List<string> csharpContents) |
| | | { |
| | | foreach (var ignore in ignoreKeyList) |
| | | { |
| | | if (Regex.IsMatch(key, ignore)) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | var pattern = string.Format(@"\.SetSprite.*{0}.*\);", key); |
| | | foreach (var content in csharpContents) |
| | | { |
| | | if (Regex.IsMatch(content, pattern)) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | static void OnUpdate() |
| | | { |
| | | var done = true; |
| | | completedTaskCount = 0; |
| | | foreach (var task in tasks) |
| | | { |
| | | completedTaskCount += task.completed; |
| | | if (!task.done) |
| | | { |
| | | done = false; |
| | | } |
| | | } |
| | | |
| | | EditorUtility.DisplayProgressBar("分析无用IconKey", |
| | | string.Format("正在分析第{0}个IconKey,共计{1}个", completedTaskCount + 1, taskCount), |
| | | (float)completedTaskCount / taskCount); |
| | | |
| | | if (done) |
| | | { |
| | | EditorUtility.ClearProgressBar(); |
| | | ProcessUnUsedIconKeys(); |
| | | EditorApplication.update -= OnUpdate; |
| | | } |
| | | } |
| | | |
| | | struct Column |
| | | { |
| | | public string name; |
| | | public List<string> values; |
| | | } |
| | | |
| | | struct IconKeyInfo |
| | | { |
| | | public string key; |
| | | public bool unused; |
| | | } |
| | | |
| | | class AnalyzeTask |
| | | { |
| | | public int total; |
| | | public int completed; |
| | | public bool done; |
| | | public List<IconKeyInfo> iconKeys; |
| | | |
| | | public AnalyzeTask() |
| | | { |
| | | iconKeys = new List<IconKeyInfo>(); |
| | | } |
| | | |
| | | public void Add(IconKeyInfo key) |
| | | { |
| | | iconKeys.Add(key); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |