-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFStringTableCollection.cs
More file actions
112 lines (97 loc) · 3.74 KB
/
Copy pathFStringTableCollection.cs
File metadata and controls
112 lines (97 loc) · 3.74 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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Squared.FString {
public class FStringTableCollection {
protected readonly Dictionary<(string folder, string name), FStringTable> Cache =
new Dictionary<(string folder, string name), FStringTable>();
public string Language = CultureInfo.CurrentCulture.Name;
public event OnMissingString MissingString;
private OnMissingString ForwardOnMissingString;
public FStringTableCollection () {
ForwardOnMissingString = (table, key) => {
if (MissingString != null)
MissingString(table, key);
};
}
private string BuildPath ((string folder, string name) key) =>
BuildPath(key.folder, key.name, Language);
private string BuildPath (string folder, string name, string language) =>
Path.Combine(folder, $"{name}_{language}.xml");
private void Reload ((string folder, string name) key, FStringTable table) {
// HACK
if (Language == "missing") {
table.IsMissing = true;
table.Language = Language;
table.Clear();
return;
}
var path = BuildPath(key);
if (!File.Exists(path)) {
table.IsMissing = true;
table.Language = Language;
table.Clear();
System.Diagnostics.Debug.WriteLine($"String table missing during reload: '{path}'");
return;
}
using (var stream = File.OpenRead(path)) {
table.Path = path;
table.Clear();
table.PopulateFromXmlStream(stream, true);
table.IsMissing = false;
table.Language = Language;
System.Diagnostics.Debug.WriteLine($"Reloaded string table '{path}'");
}
}
public void ReloadAll () {
lock (Cache) {
foreach (var kvp in Cache)
Reload(kvp.Key, kvp.Value);
}
}
public void ClearCache () {
lock (Cache)
Cache.Clear();
}
public FStringTable LoadFromPath (string folder, string name, bool optional, bool forceReload = false) {
FStringTable result;
var key = (folder, name);
lock (Cache)
if (Cache.TryGetValue(key, out result)) {
if (forceReload || (result.Language != Language))
Reload(key, result);
return result;
}
var path = BuildPath(key);
if (!File.Exists(path) && optional) {
result = new FStringTable(name) {
Path = path,
IsMissing = true,
Language = Language
};
result.MissingString += ForwardOnMissingString;
} else {
using (var stream = File.OpenRead(path)) {
result = new FStringTable(name, stream) {
Path = path,
Language = Language,
};
result.MissingString += ForwardOnMissingString;
}
}
lock (Cache) {
if (Cache.TryGetValue(key, out var lostARace)) {
if (forceReload || (result.Language != Language))
Reload(key, lostARace);
return lostARace;
}
Cache[key] = result;
return result;
}
}
}
}