using PhxhSDK; using System.Collections.Generic; using System.IO; using System.Text; using UnityEditor; using UnityEngine; /// /// 屏蔽词工具 /// public class MaskWindow : EditorWindow { // 25/2/24 隐藏此功能 /* [MenuItem("Tools/Mask(屏蔽字)")] public static void OpenWindow() { MaskWindow window = GetWindow("屏蔽字", true); window.minSize = new Vector2(800, 600); } /// /// 屏蔽词文件资源路径 /// private string maskPath; private Rect rect; /// /// 检查到包含屏蔽词的屏蔽词集合 /// private readonly HashSet setCheckMask = new(); /// /// 检查到包含屏蔽词的屏蔽词集合显示 /// private readonly HashSet setCheckMaskDisplay = new(); private string checkMask; private Vector2 scrollPos; public void OnGUI() { GUILayout.BeginVertical(); EditorGUILayout.LabelField("屏蔽字文件"); rect = EditorGUILayout.GetControlRect(GUILayout.Width(300f)); maskPath = EditorGUI.TextField(rect, maskPath); //如果鼠标正在拖拽中或拖拽结束时,并且鼠标所在位置在文本输入框内 if ((EventType.DragUpdated == Event.current.type || EventType.DragExited == Event.current.type) && rect.Contains(Event.current.mousePosition)) { //改变鼠标的外表 DragAndDrop.visualMode = DragAndDropVisualMode.Generic; if (null != DragAndDrop.paths && DragAndDrop.paths.Length > 0) { maskPath = DragAndDrop.paths[0]; EditorPrefs.SetString("Mask_Editor", maskPath); } } if (GUILayout.Button("去重")) OnDistinct(); if (GUILayout.Button("检查含有屏蔽词的屏蔽词")) OnCheckDistinctBySub(); if (setCheckMask.Count > 0) { scrollPos = GUILayout.BeginScrollView(scrollPos); GUILayout.TextArea(checkMask); GUILayout.EndScrollView(); if (GUILayout.Button("删除含有屏蔽词的屏蔽词")) OnDeleteDistinctBySub(); } GUILayout.EndVertical(); } private void OnEnable() { maskPath = EditorPrefs.GetString("Mask_Editor"); } /// /// 去重 /// private async void OnDistinct() { TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync(maskPath); if (null == textAsset) { DebugUtil.LogError("没有找到屏蔽词资源"); return; } int count = 0; // 去重个数 StringBuilder sb = new(); List listMask = new(65536); HashSet setMask = new(); #region 获取所有屏蔽词 using StringReader reader = new(textAsset.text); string line; while (null != (line = reader.ReadLine())) { if (string.Empty == line) continue; ++count; listMask.Add(line.Replace(",", string.Empty)); } #endregion #region 屏蔽词从短到长排序 listMask.Sort((a, b) => // 按从短到长排序 { return a.Length.CompareTo(b.Length); }); #endregion #region 去重 foreach (string mask in listMask) { setMask.Add(mask); } #endregion count -= setMask.Count; foreach (string mask in setMask) { sb.AppendLine(mask); } File.WriteAllText(maskPath, sb.ToString(), Encoding.UTF8); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); DebugUtil.Log($"去重完成,去重{count}个"); } /// /// 检查部分重叠的 /// private async void OnCheckDistinctBySub() { TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync(maskPath); if (null == textAsset) { DebugUtil.LogError("没有找到屏蔽词资源"); return; } setCheckMask.Clear(); setCheckMaskDisplay.Clear(); HashSet setMask = new(); using StringReader reader = new(textAsset.text); string line; while (null != (line = reader.ReadLine())) { if (string.Empty == line) continue; bool isExist = false; for (int i = 0; i < line.Length; ++i) { if (isExist) break; for (int j = i + 1; j < line.Length; ++j) { string checkMask = line[i..j]; if (setMask.Contains(checkMask)) { setCheckMask.Add(line); setCheckMaskDisplay.Add($"{line} 包含 {checkMask}"); isExist = true; break; } } } setMask.Add(line); } if (setCheckMask.Count > 0) { StringBuilder sb = new(); foreach (string mask in setCheckMaskDisplay) { sb.AppendLine(mask); } checkMask = sb.ToString(); } else checkMask = string.Empty; DebugUtil.Log("检查含有屏蔽词的屏蔽词完成"); } /// /// 删除包含屏蔽词的屏蔽词 /// private async void OnDeleteDistinctBySub() { TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync(maskPath); if (null == textAsset) { DebugUtil.LogError("没有找到屏蔽词资源"); return; } StringBuilder sb = new(); HashSet setMask = new(); using StringReader reader = new(textAsset.text); string line; while (null != (line = reader.ReadLine())) { if (string.Empty == line) continue; if (setCheckMask.Contains(line)) continue; setMask.Add(line); } foreach (string mask in setMask) { sb.AppendLine(mask); } File.WriteAllText(maskPath, sb.ToString(), Encoding.UTF8); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); OnCheckDistinctBySub(); DebugUtil.Log($"已删除冗余屏蔽词"); } */ }