Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
LocalizedText.cs
Go to the documentation of this file.
3
5
6public class LocalizedText
7{
8 public static readonly LocalizedText Empty = new LocalizedText("", "");
9
10 private static Regex _substitutionRegex = new Regex("{(\\?(?:!)?)?([a-zA-Z][\\w\\.]*)}", RegexOptions.Compiled);
11
12 public readonly string Key;
13
14 public string Value { get; private set; }
15
16 internal LocalizedText(string key, string text)
17 {
18 Key = key;
19 Value = text;
20 }
21
22 internal void SetValue(string text)
23 {
24 Value = text;
25 }
26
27 public string FormatWith(object obj)
28 {
29 string value = Value;
31 return _substitutionRegex.Replace(value, delegate(Match match)
32 {
33 if (match.Groups[1].Length != 0)
34 {
35 return "";
36 }
37 string name = match.Groups[2].ToString();
38 PropertyDescriptor propertyDescriptor = properties.Find(name, ignoreCase: false);
39 return (propertyDescriptor == null) ? "" : (propertyDescriptor.GetValue(obj) ?? "").ToString();
40 });
41 }
42
43 public bool CanFormatWith(object obj)
44 {
46 foreach (Match item in _substitutionRegex.Matches(Value))
47 {
48 string name = item.Groups[2].ToString();
49 PropertyDescriptor propertyDescriptor = properties.Find(name, ignoreCase: false);
50 if (propertyDescriptor == null)
51 {
52 return false;
53 }
54 object value = propertyDescriptor.GetValue(obj);
55 if (value == null)
56 {
57 return false;
58 }
59 if (item.Groups[1].Length != 0 && (((value as bool?) ?? false) ^ (item.Groups[1].Length == 1)))
60 {
61 return false;
62 }
63 }
64 return true;
65 }
66
68 {
69 return NetworkText.FromKey(Key);
70 }
71
72 public NetworkText ToNetworkText(params object[] substitutions)
73 {
74 return NetworkText.FromKey(Key, substitutions);
75 }
76
77 public static explicit operator string(LocalizedText text)
78 {
79 return text.Value;
80 }
81
82 public string Format(object arg0)
83 {
84 return string.Format(Value, arg0);
85 }
86
87 public string Format(object arg0, object arg1)
88 {
89 return string.Format(Value, arg0, arg1);
90 }
91
92 public string Format(object arg0, object arg1, object arg2)
93 {
94 return string.Format(Value, arg0, arg1, arg2);
95 }
96
97 public string Format(params object[] args)
98 {
99 return string.Format(Value, args);
100 }
101
102 public override string ToString()
103 {
104 return Value;
105 }
106}
virtual ? PropertyDescriptor Find(string name, bool ignoreCase)
object? GetValue(object? component)
static PropertyDescriptorCollection GetProperties([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType)
NetworkText ToNetworkText(params object[] substitutions)
string Format(object arg0, object arg1)
LocalizedText(string key, string text)
string Format(object arg0, object arg1, object arg2)
string Format(params object[] args)
static NetworkText FromKey(string key, params object[] substitutions)