240 lines
6.5 KiB
C#
240 lines
6.5 KiB
C#
using PhxhSDK;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 屏蔽词工具
|
|
/// </summary>
|
|
public class MaskWindow : EditorWindow
|
|
{
|
|
// 25/2/24 隐藏此功能
|
|
|
|
/*
|
|
[MenuItem("Tools/Mask(屏蔽字)")]
|
|
public static void OpenWindow()
|
|
{
|
|
MaskWindow window = GetWindow<MaskWindow>("屏蔽字", true);
|
|
window.minSize = new Vector2(800, 600);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 屏蔽词文件资源路径
|
|
/// </summary>
|
|
private string maskPath;
|
|
private Rect rect;
|
|
/// <summary>
|
|
/// 检查到包含屏蔽词的屏蔽词集合
|
|
/// </summary>
|
|
private readonly HashSet<string> setCheckMask = new();
|
|
/// <summary>
|
|
/// 检查到包含屏蔽词的屏蔽词集合显示
|
|
/// </summary>
|
|
private readonly HashSet<string> 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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 去重
|
|
/// </summary>
|
|
private async void OnDistinct()
|
|
{
|
|
TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(maskPath);
|
|
if (null == textAsset)
|
|
{
|
|
DebugUtil.LogError("没有找到屏蔽词资源");
|
|
return;
|
|
}
|
|
|
|
int count = 0; // 去重个数
|
|
StringBuilder sb = new();
|
|
List<string> listMask = new(65536);
|
|
HashSet<string> 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}个");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查部分重叠的
|
|
/// </summary>
|
|
private async void OnCheckDistinctBySub()
|
|
{
|
|
TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(maskPath);
|
|
if (null == textAsset)
|
|
{
|
|
DebugUtil.LogError("没有找到屏蔽词资源");
|
|
return;
|
|
}
|
|
|
|
setCheckMask.Clear();
|
|
setCheckMaskDisplay.Clear();
|
|
HashSet<string> 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("检查含有屏蔽词的屏蔽词完成");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除包含屏蔽词的屏蔽词
|
|
/// </summary>
|
|
private async void OnDeleteDistinctBySub()
|
|
{
|
|
TextAsset textAsset = await AssetManager.Instance.LoadAssetAsync<TextAsset>(maskPath);
|
|
if (null == textAsset)
|
|
{
|
|
DebugUtil.LogError("没有找到屏蔽词资源");
|
|
return;
|
|
}
|
|
|
|
StringBuilder sb = new();
|
|
HashSet<string> 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($"已删除冗余屏蔽词");
|
|
}
|
|
|
|
*/
|
|
} |