Client LuaCsForBarotrauma
ForbiddenWordFilter.cs
1 using System.Collections.Generic;
2 using Barotrauma.IO;
3 using System.Linq;
4 
5 namespace Barotrauma
6 {
7  static class ForbiddenWordFilter
8  {
9  static readonly string fileListPath = Path.Combine("Data", "forbiddenwordlist.txt");
10 
11  private static readonly HashSet<string> forbiddenWords;
12 
13  static ForbiddenWordFilter()
14  {
15  try
16  {
17  forbiddenWords = File.ReadAllLines(fileListPath).Select(s => s.ToLowerInvariant()).ToHashSet();
18  }
19  catch (System.IO.IOException e)
20  {
21  DebugConsole.ThrowError($"Failed to load the list of forbidden words from {fileListPath}.", e);
22  }
23  }
24 
25  public static bool IsForbidden(string text)
26  {
27  return IsForbidden(text, out _);
28  }
29 
30  public static bool IsForbidden(string text, out string forbiddenWord)
31  {
32  forbiddenWord = string.Empty;
33  if (forbiddenWords == null)
34  {
35  return false;
36  }
37 
38  char[] delimiters = new char[] { ' ', '-', '.', '_', ':', ';', '\'' };
39 
40  HashSet<string> words = new HashSet<string>();
41  foreach (char delimiter in delimiters)
42  {
43  foreach (string word in text.Split(delimiter))
44  {
45  words.Add(word.ToLowerInvariant());
46  }
47  }
48 
49  text = text.ToLowerInvariant();
50  foreach (string forbidden in forbiddenWords)
51  {
52  if (forbidden.Contains(' '))
53  {
54  if (words.Contains(forbidden.Trim()))
55  {
56  forbiddenWord = forbidden.Trim();
57  return true;
58  }
59  }
60  else
61  {
62  if (text.Contains(forbidden))
63  {
64  forbiddenWord = forbidden.Trim();
65  return true;
66  }
67  }
68  }
69  return false;
70  }
71  }
72 }