Client LuaCsForBarotrauma
RichString.cs
1 #nullable enable
2 using System;
3 using System.Collections.Immutable;
4 using System.Diagnostics;
5 
6 namespace Barotrauma
7 {
8  public class RichString
9  {
10  protected bool loaded = false;
12  private int languageVersion = 0;
13 
14  protected string cachedSanitizedValue = "";
15  public string SanitizedValue
16  {
17  get
18  {
19  if (MustRetrieveValue()) { RetrieveValue(); }
20  return cachedSanitizedValue;
21  }
22  }
23 
24  public int Length => SanitizedValue.Length;
25 
26  private readonly Func<string, string>? postProcess;
27  private readonly bool shouldParseRichTextData;
28 
29  private readonly LocalizedString originalStr;
30  public LocalizedString NestedStr { get; private set; }
32 
33 #if CLIENT
34  private readonly GUIFont? font;
35  private readonly GUIComponentStyle? componentStyle;
36  private bool forceUpperCase = false;
37 
38  private bool FontOrStyleForceUpperCase
39  => font is { ForceUpperCase: true } || componentStyle is { ForceUpperCase: true };
40 #endif
41 
42  public ImmutableArray<RichTextData>? RichTextData { get; private set; }
43 
44 #if CLIENT
45  private RichString(
46  LocalizedString nestedStr, bool shouldParseRichTextData, Func<string, string>? postProcess = null,
47  GUIFont? font = null, GUIComponentStyle? componentStyle = null) : this(nestedStr, shouldParseRichTextData, postProcess)
48  {
49  this.font = font;
50  this.componentStyle = componentStyle;
51  }
52 #endif
53 
54  private RichString(LocalizedString nestedStr, bool shouldParseRichTextData, Func<string,string>? postProcess = null)
55  {
56  originalStr = nestedStr;
57  NestedStr = originalStr;
58  this.shouldParseRichTextData = shouldParseRichTextData;
59  this.postProcess = postProcess;
60  SanitizedString = new StripRichTagsLString(this);
61 #if CLIENT
62  this.font = null;
63  this.componentStyle = null;
64 #endif
65  }
66 
67  public static RichString Rich(LocalizedString str, Func<string, string>? postProcess = null)
68  {
69  return new RichString(str, true, postProcess);
70  }
71 
72  public static RichString Plain(LocalizedString str)
73  {
74  return new RichString(str, false, postProcess: null);
75  }
76 
77  public static implicit operator LocalizedString(RichString richStr) => richStr.NestedStr;
78 
79  public static implicit operator RichString(LocalizedString lStr)
80  {
81 #if DEBUG
82  if (!lStr.IsNullOrEmpty() && lStr.Contains("‖"))
83  {
84  //if (Debugger.IsAttached) { Debugger.Break(); }
85  }
86 #endif
87  return Plain(lStr ?? string.Empty);
88  }
89  public static implicit operator RichString(string str) => (LocalizedString)str;
90 
91  protected virtual bool MustRetrieveValue()
92  {
93  return NestedStr.Loaded != loaded
94  || language != GameSettings.CurrentConfig.Language
95  || languageVersion != TextManager.LanguageVersion
96 #if CLIENT
97  || (FontOrStyleForceUpperCase != forceUpperCase)
98 #endif
99  ;
100  }
101 
102  public void RetrieveValue()
103  {
104 #if CLIENT
105  NestedStr = FontOrStyleForceUpperCase ? originalStr.ToUpper() : originalStr;
106  forceUpperCase = FontOrStyleForceUpperCase;
107 #endif
108  if (shouldParseRichTextData)
109  {
111  }
112  else
113  {
115  }
116  if (postProcess != null) { cachedSanitizedValue = postProcess(cachedSanitizedValue); }
117  language = GameSettings.CurrentConfig.Language;
118  languageVersion = TextManager.LanguageVersion;
120  }
121 
122 #if CLIENT
124  {
125  return new RichString(originalStr, shouldParseRichTextData, postProcess, font, componentStyle);
126  }
127 #endif
128 
130  {
131  return new RichString(NestedStr.ToUpper(), shouldParseRichTextData, postProcess);
132  }
133 
135  {
136  return new RichString(NestedStr.ToLower(), shouldParseRichTextData, postProcess);
137  }
138 
139  public RichString Replace(string from, string to, StringComparison stringComparison = StringComparison.Ordinal)
140  {
141  return new RichString(NestedStr.Replace(from, to, stringComparison), shouldParseRichTextData, postProcess);
142  }
143 
144  public override string ToString()
145  {
146  return SanitizedValue;
147  }
148 
149  public bool Contains(string str, StringComparison stringComparison = StringComparison.Ordinal) =>
150  SanitizedValue.Contains(str, stringComparison);
151 
152  public bool Contains(char chr, StringComparison stringComparison = StringComparison.Ordinal) =>
153  SanitizedValue.Contains(chr, stringComparison);
154 
155 
156  public static bool operator ==(RichString? a, RichString? b)
157  => a?.SanitizedValue == b?.SanitizedValue
158 #if CLIENT
159  && a?.font == b?.font
160  && a?.componentStyle == b?.componentStyle
161 #endif
162  ;
163 
164  public static bool operator !=(RichString? a, RichString? b) => !(a == b);
165 
166  public static bool operator ==(RichString? a, LocalizedString? b)
167  => a?.SanitizedValue == b?.Value;
168 
169  public static bool operator !=(RichString? a, LocalizedString? b) => !(a == b);
170 
171  public static bool operator ==(LocalizedString? a, RichString? b)
172  => a?.Value == b?.SanitizedValue;
173 
174  public static bool operator !=(LocalizedString? a, RichString? b) => !(a == b);
175 
176  public static bool operator ==(RichString? a, string? b)
177  => a?.SanitizedValue == b;
178 
179  public static bool operator !=(RichString? a, string? b) => !(a == b);
180 
181  public static bool operator ==(string? a, RichString? b)
182  => a == b?.SanitizedValue;
183 
184  public static bool operator !=(string? a, RichString? b) => !(a == b);
185  }
186 
188  {
189  public readonly RichString RichStr;
190 
192  {
193  RichStr = richStr;
194  }
195 
196  public override bool Loaded => RichStr.NestedStr.Loaded;
197  public override void RetrieveValue()
198  {
200  }
201  }
202 }
bool Contains(string subStr, StringComparison comparison=StringComparison.Ordinal)
virtual LocalizedString ToUpper()
LocalizedString Replace(Identifier find, LocalizedString replace, StringComparison stringComparison=StringComparison.Ordinal)
override string ToString()
Definition: RichString.cs:144
bool Contains(string str, StringComparison stringComparison=StringComparison.Ordinal)
RichString Replace(string from, string to, StringComparison stringComparison=StringComparison.Ordinal)
Definition: RichString.cs:139
LocalizedString NestedStr
Definition: RichString.cs:30
RichString ToLower()
Definition: RichString.cs:134
virtual bool MustRetrieveValue()
Definition: RichString.cs:91
string cachedSanitizedValue
Definition: RichString.cs:14
static RichString Plain(LocalizedString str)
Definition: RichString.cs:72
bool Contains(char chr, StringComparison stringComparison=StringComparison.Ordinal)
static RichString Rich(LocalizedString str, Func< string, string >? postProcess=null)
Definition: RichString.cs:67
LanguageIdentifier language
Definition: RichString.cs:11
readonly LocalizedString SanitizedString
Definition: RichString.cs:31
RichString ToUpper()
Definition: RichString.cs:129
static bool operator==(RichString? a, RichString? b)
static bool operator!=(RichString? a, RichString? b)
RichString CaseTiedToFontAndStyle(GUIFont? font, GUIComponentStyle? componentStyle)
Definition: RichString.cs:123
static ? ImmutableArray< RichTextData > GetRichTextData(string text, out string sanitizedText)
readonly RichString RichStr
Definition: RichString.cs:189
StripRichTagsLString(RichString richStr)
Definition: RichString.cs:191
static readonly LanguageIdentifier None
Definition: TextPack.cs:12