-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFStringTable.cs
More file actions
310 lines (267 loc) · 11.6 KB
/
Copy pathFStringTable.cs
File metadata and controls
310 lines (267 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Squared.Util.Text;
namespace Squared.FString {
public delegate void OnMissingString (FStringTable table, string key);
public class FStringTable {
public static FStringTable Default = new FStringTable("empty");
/// <summary>
/// If a string is not found in this table, it will be searched for in FallbackTable
/// </summary>
public FStringTable FallbackTable;
public event OnMissingString MissingString;
public readonly string Name;
private readonly Dictionary<string, FStringDefinition> Entries
= new Dictionary<string, FStringDefinition>(StringComparer.Ordinal);
public string Path { get; internal set; }
public string Language { get; internal set; }
public bool IsMissing { get; internal set; }
public FStringTable (string name) {
Name = name;
}
public FStringTable (string name, Stream input)
: this(name) {
PopulateFromXmlStream(input, false);
}
public void Clear () => Entries.Clear();
public int Count => Entries.Count;
public void PopulateFromXmlStream (Stream input, bool allowOverwrite) {
var xrs = new XmlReaderSettings {
CloseInput = false,
};
using (var xr = XmlReader.Create(input, xrs)) {
// Seek to first file and then read each file
while (xr.ReadToFollowing("File")) {
if (xr.IsEmptyElement)
continue;
try {
PopulateFromXmlNode(xr, allowOverwrite);
} catch (Exception exc) {
var fs = input as FileStream;
throw new Exception($"Parse error in file {fs?.Name ?? "unknown"}: {exc.Message}", exc);
}
}
}
}
private void PopulateFromXmlNode (XmlReader xr, bool allowOverwrite) {
while (xr.Read()) {
switch (xr.NodeType) {
case XmlNodeType.Element:
var isLiteral = xr.Name == "Literal";
if ((xr.Name != "String") && (xr.Name != "Literal"))
throw new Exception($"Unexpected element '{xr.Name}', expected String or Literal");
var name = xr.GetAttribute("Name");
var newline = xr.GetAttribute("TrailingNewline")?.Trim() == "true";
var formatString = xr.ReadElementContentAsString();
if (newline)
formatString += '\n';
if (isLiteral)
AddRaw(name, formatString, allowOverwrite);
else
Add(name, formatString, allowOverwrite);
break;
case XmlNodeType.Whitespace:
case XmlNodeType.Comment:
break;
case XmlNodeType.EndElement:
if ((xr.Name == "File") || (xr.Name == "FStringTable"))
return;
else if (xr.Name != "String")
throw new Exception($"Unexpected end of element '{xr.Name}', expected String");
break;
default:
return;
}
}
}
public FStringDefinition AddRaw (string name, string text, bool allowOverwrite = false) {
var definition = FStringDefinition.Parse(this, name, text, true);
if (allowOverwrite)
Entries[name] = definition;
else
Entries.Add(name, definition);
return definition;
}
public FStringDefinition Add (string name, string formatString, bool allowOverwrite = false) {
var definition = FStringDefinition.Parse(this, name, formatString, false);
if (allowOverwrite)
Entries[name] = definition;
else
Entries.Add(name, definition);
return definition;
}
// FIXME: Flow through caller information so it can be provided to the MissingString event handler
public FStringDefinition Get (string name, bool optional = true) {
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
if (!Entries.TryGetValue(name, out var entry)) {
if (MissingString != null)
MissingString(this, name);
if (!optional)
throw new KeyNotFoundException(name);
else
return FallbackTable?.Get(name, optional) ?? FStringDefinition.Missing(this, name);
} else
return entry;
}
}
public class FStringDefinition {
private static readonly Dictionary<string, FStringDefinition> MissingStringCache = new Dictionary<string, FStringDefinition>();
public readonly FStringTable Table;
public readonly string Name;
public readonly bool IsMissing;
public readonly List<(bool emit, string textOrId)> Opcodes =
new List<(bool emit, string textOrId)>();
protected FStringDefinition (FStringTable table, string name, bool isMissing) {
Table = table;
Name = name;
IsMissing = isMissing;
}
private static char GetChar (string s, int index) {
if ((index < 0) || (index >= s.Length))
return '\0';
else
return s[index];
}
public static FStringDefinition Raw (FStringTable table, string name, string text) {
var result = new FStringDefinition(table, name, false);
result.Opcodes.Add((false, text));
return result;
}
public static FStringDefinition Parse (FStringTable table, string name, string formatString, bool escapesOnly) {
var result = new FStringDefinition(table, name, false);
var buildingEmit = false;
int segmentStart = 0;
for (int i = 0; i < formatString.Length; i++) {
switch (formatString[i]) {
case '\\':
if (buildingEmit)
continue;
AddSegment(i);
i += AddEscape(GetChar(formatString, ++i), i);
segmentStart = i + 1;
break;
case '{':
if (!escapesOnly) {
if (buildingEmit)
throw new Exception($"Unexpected '{{' inside of expansion value in string {name}");
AddSegment(i);
if (GetChar(formatString, i + 1) == '{') {
i++;
AddString("{");
} else {
buildingEmit = true;
}
segmentStart = i + 1;
}
break;
case '}':
if (!escapesOnly) {
var wasBuildingEmit = buildingEmit;
AddSegment(i);
if (GetChar(formatString, i + 1) == '}') {
if (wasBuildingEmit)
throw new Exception($"Unexpected '}}' inside of expansion value in string {name}");
i++;
AddString("}");
}
segmentStart = i + 1;
}
break;
}
}
AddSegment(formatString.Length);
void AddString (string text) {
if (result.Opcodes.Count > 0) {
var previousOpcode = result.Opcodes[result.Opcodes.Count - 1];
// Merge the two strings. This will occur for escape sequences
if (!previousOpcode.emit) {
text = previousOpcode.textOrId + text;
result.Opcodes.RemoveAt(result.Opcodes.Count - 1);
}
}
result.Opcodes.Add((false, text));
}
int AddEscape (char ch, int offset) {
switch (ch) {
case 'u':
var hex = formatString.Substring(offset + 1, 4);
var ch2 = (char)int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
AddString(new string(ch2, 1));
return 5;
default:
throw new NotImplementedException($"Escape sequence \\{ch}");
case 't':
AddString("\t");
return 1;
case 'r':
AddString("\r");
return 1;
case 'n':
AddString("\n");
return 1;
case '0':
AddString("\0");
return 1;
}
}
void AddSegment (int end) {
if (end <= segmentStart)
return;
var text = formatString.Substring(segmentStart, end - segmentStart);
if (buildingEmit) {
text = string.Intern(text);
result.Opcodes.Add((buildingEmit, text));
} else
AddString(text);
buildingEmit = false;
}
return result;
}
public static FStringDefinition Missing (FStringTable table, string name) {
lock (MissingStringCache) {
if (!MissingStringCache.TryGetValue(name, out var result))
MissingStringCache[name] = result = new FStringDefinition(table, name, true);
return result;
}
}
public override string ToString () {
return $"String '{Name}'";
}
public bool IsLiteral => (Opcodes.Count <= 1) && (Opcodes.FirstOrDefault().emit != true);
public string GetStringLiteral () {
if (IsMissing)
return $"<MISSING: {Name}>";
else if (Opcodes.Count == 0)
return "";
else if ((Opcodes.Count != 1) || Opcodes[0].emit)
throw new InvalidOperationException($"{Name} is not a literal");
else
return Opcodes[0].textOrId;
}
public void AppendTo<TInstance> (ref TInstance instance, ref FStringBuilder output)
where TInstance : IFString
{
if (IsMissing) {
output.Append("<MISSING: ");
output.Append(Name);
output.Append('>');
} else {
foreach (var opcode in Opcodes) {
if (opcode.emit)
instance.EmitValue(ref output, opcode.textOrId);
else
output.Append(opcode.textOrId);
}
}
}
}
}