using System; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; using System.Threading; public partial class DirtyNameConfig : ConfigBase { private static WordGroup[] DIRTYWORLD = new WordGroup[(int)char.MaxValue]; private static List memoryList = new List(); private static bool dirtyNameInited = false; static int cursor = 0; static int wordlenght = 0; static int nextCursor = 0; protected override void OnConfigParseCompleted() { if (string.IsNullOrEmpty(word)) { Debug.LogErrorFormat("屏蔽名字 id = {0} 的内容为空 需要修改", id); return; } string key = ToDBC(word); memoryList.Add(key); } private static string ToDBC(string input) { char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) { if (c[i] == 12288) { c[i] = (char)32; continue; } if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); } return new string(c).ToLower(); } public static bool IsDirtName(string source) { if (source != string.Empty) { cursor = 0; nextCursor = 0; for (int i = 0; i < source.Length; i++) { //查询以该字为首字符的词组 WordGroup group = DIRTYWORLD[(int)ToDBC(source)[i]]; if (group != null) { for (int z = 0; z < group.Count(); z++) { string word = group.GetWord(z); if (word.Length == 0 || Check(word, source)) { return true; } } } cursor++; } } return false; } private static bool Check(string blackWord, string source) { wordlenght = 0; int wordCnt = 0; //检测源下一位游标 nextCursor = cursor + 1; bool found = false; //遍历词的每一位做匹配 for (int i = 0; i < blackWord.Length; i++) { //特殊字符偏移游标 int offset = 0; if (nextCursor >= source.Length) { break; } else { //检测下位字符如果不是汉字 数字 字符 偏移量加1,,过滤特殊字符 for (int y = nextCursor; y < source.Length; y++) { //if (!IsCHS(source[y]) && !IsNum(source[y]) && !IsAlphabet(source[y])) { if (IsSpecial(source[y])) { offset++; //避让特殊字符,下位游标如果>=字符串长度 跳出 if (nextCursor + offset >= source.Length) break; wordlenght++; } else break; } if (nextCursor + offset >= source.Length) { break; } if ((int)blackWord[i] == (int)source[nextCursor + offset]) { wordCnt++; found = true; } else { found = false; break; } } nextCursor = nextCursor + 1 + offset; wordlenght++; } if (blackWord.Length != wordCnt) return false; return found; } private static bool IsSpecial(char character) { if (character == ' ') { return true; } return false; } static public void DirtyNameInit() { if (dirtyNameInited) { return; } dirtyNameInited = true; ThreadPool.QueueUserWorkItem((object aaa) => { memoryList.Sort((string x, string y) => { return x.CompareTo(y); }); for (int i = memoryList.Count - 1; i > 0; i--) { if (memoryList[i].ToString() == memoryList[i - 1].ToString()) { memoryList.RemoveAt(i); } } foreach (string word in memoryList) { WordGroup group = DIRTYWORLD[(int)word[0]]; if (group == null) { group = new WordGroup(); DIRTYWORLD[(int)word[0]] = group; } group.Add(word.Substring(1)); } }); } public static bool IsCHS(char character) { // 中文表意字符的范围 4E00-9FA5 int charVal = (int)character; return (charVal >= 0x4e00 && charVal <= 0x9fa5); } private static bool IsNum(char character) { int charVal = (int)character; return (charVal >= 48 && charVal <= 57); } private static bool IsAlphabet(char character) { int charVal = (int)character; return ((charVal >= 97 && charVal <= 122) || (charVal >= 65 && charVal <= 90)); } private const string FACE_REPLACE = @"#~[0-9a-zA-Z]{1,3}"; private static Regex FaceRegex = new Regex(FACE_REPLACE, RegexOptions.Singleline); public static string FitterSpecial(string source) { return FaceRegex.Replace(source, " "); } }