-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
196 lines (181 loc) · 6.79 KB
/
Program.cs
File metadata and controls
196 lines (181 loc) · 6.79 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
using System.Buffers;
using System.IO.Compression;
#if DEBUG
using System.Runtime.ExceptionServices;
#endif
using System.Text.Json;
using System.Text.Json.Nodes;
namespace LEFontPatch {
public static class Program {
public static void Main(string[] args) {
if (args.Length != 2) {
Console.WriteLine("Usage: LEFontPatch <gamePath> <zipPath|folderPath>");
Console.WriteLine(" or: LEFontPatch --dump <gamePath>");
if (args.Length == 0)
goto pause;
return;
}
try {
if (args[0].Equals("--dump", StringComparison.OrdinalIgnoreCase))
DumpFallbacks(args[1] + "/Last Epoch_Data");
else {
Run(args[0] + "/Last Epoch_Data", args[1]);
return; // Do not pause if success
}
} catch (Exception ex) {
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error");
Console.Error.WriteLine(ex);
Console.ForegroundColor = tmp;
#if DEBUG
ExceptionDispatchInfo.Capture(ex).Throw(); // Throw to the debugger
#endif
}
pause:
Console.WriteLine();
Console.Write("Enter to exit . . .");
Console.ReadLine();
}
public static void Run(string gameDataPath, string zipOrFolderPath) {
using var zip = Directory.Exists(zipOrFolderPath) ? null : ZipFile.OpenRead(zipOrFolderPath);
var zipEntries = zip?.Entries.ToDictionary(e => e.FullName.ToLowerInvariant(), e => e);
byte[]? buffer = null;
try {
byte[] GetFile(string fileName) {
if (zip is null)
return File.ReadAllBytes(Path.Combine(zipOrFolderPath, fileName));
if (!zipEntries!.TryGetValue(fileName.ToLowerInvariant(), out var e))
throw new FileNotFoundException(fileName + " not found in the zip file");
Console.WriteLine("Zip: Getting " + fileName);
var len = (int)e.Length;
if (buffer is not null && buffer.Length < len) {
ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
buffer ??= ArrayPool<byte>.Shared.Rent(len);
using (var s = e.Open())
s.ReadExactly(buffer, 0, len);
return buffer.Length == len ? buffer : buffer[..len];
}
#nullable disable // Json DOM
var json = JsonNode.Parse(GetFile("manifest.json"), null, new() {
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip
}).AsObject();
Console.WriteLine("Reading TMP_FontAssets");
LibCpp2IL.Logging.LibLogger.ShowVerbose = false;
using var manager = new LEFontManager(gameDataPath);
Console.WriteLine();
var sourceFonts = json["sourceFontFiles"]?.AsArray().Select(n => {
manager.TryAddFontFile(GetFile((string)n["path"]), out var i);
return i;
}).ToArray() ?? [];
var atlases = json["atlases"]?.AsArray().Select(
n => manager.AddAtlas(GetFile((string)n["path"]))).ToArray() ?? [];
var materials = json["materials"]?.AsArray().Select(n => {
var atlas = atlases[(int)n["atlas"]];
return (manager.AddMaterial(GetFile((string)n["path"]), atlas), atlas);
}).ToArray() ?? [];
var fonts = json["fonts"]?.AsArray() ?? [];
var fontDic = new Dictionary<string, List<int>>(manager.TMPFonts.Count);
BuildFontDic();
void BuildFontDic() {
foreach (var kvp in manager.TMPFonts) {
var name = kvp.Value["m_Name"].AsString;
if (!fontDic.TryGetValue(name, out var list))
fontDic.Add(name, list = new(1));
list.Add(kvp.Key);
}
}
var excludes = new HashSet<int>();
if (json["fontReplacements"] is JsonObject replacement) {
Console.WriteLine("Replacing fonts:");
var rCache = new Dictionary<int, int>(replacement.Count);
var sCache = new Dictionary<int, int>();
foreach (var (fontName, fi) in replacement) {
var fontIndex = (int)fi;
if (!fontDic.TryGetValue(fontName, out var list)) {
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\tWarning: Font not found: " + fontName);
Console.ForegroundColor = tmp;
continue;
}
foreach (var i in list) {
if ((i < 0 ? rCache : sCache).TryGetValue(fontIndex, out var j)) {
var data = manager.TMPFonts[j];
manager.GetAsset(i).SetNewData(data);
manager.TMPFonts[i] = data;
} else {
var font = fonts[fontIndex].AsObject();
var (material, atlas) = materials[(int)font["material"]];
var sourceFont = font.TryGetPropertyValue("sourceFont", out var sf) ? sourceFonts[(int)sf] : -1;
var fontData = JsonNode.Parse(GetFile((string)font["path"])).AsObject();
manager.ReplaceTMPFont(i, fontData, atlas, material, sourceFont);
(i < 0 ? rCache : sCache)[fontIndex] = i;
}
excludes.Add(i);
Console.Write('\t');
Console.WriteLine("Replaced: " + fontName);
}
}
}
if (excludes.Count == 0 && json["cancelIfNoFontReplaced"] is JsonValue v && (bool)v) {
Console.WriteLine("No font replaced, cancelling . . .");
return;
}
if (json["removeCharacters"] is JsonObject rC) {
Console.WriteLine("Start removing characters . . .");
var characters = new HashSet<int>();
if (rC["fromCharacters"] is JsonArray from1) {
characters.EnsureCapacity(from1.Count);
foreach (var c in from1)
characters.Add((int)c);
}
BuildFontDic();
if (rC["fromFont"] is JsonArray from2) {
foreach (var f in from2) {
foreach (var fi in fontDic[(string)f]) {
var chars = manager.GetCharacters(fi, out var count);
characters.EnsureCapacity(characters.Count + count);
characters.UnionWith(chars);
}
}
}
if (rC["excludeReplaced"] is not JsonValue v2 || !(bool)v2)
excludes.Clear();
if (rC["excludeFonts"] is JsonArray efs)
foreach (var ef in efs) {
if (!fontDic.TryGetValue((string)ef, out var fs)) {
var tmp = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\tWarning: Font not found: " + (string)ef);
Console.ForegroundColor = tmp;
continue;
}
excludes.EnsureCapacity(excludes.Count + fs.Count);
excludes.UnionWith(fs);
}
var excludedFonts = manager.TMPFonts.Keys.Except(excludes).ToArray();
Console.WriteLine($"Removing {characters.Count} characters in {excludedFonts.Length} fonts");
if (characters.Count != 0)
manager.RemoveCharacters(excludedFonts, characters);
}
#nullable restore
Console.WriteLine("Saving . . . (may takes minutes)");
manager.Save();
Console.WriteLine("Done!");
} finally {
if (buffer is not null)
ArrayPool<byte>.Shared.Return(buffer);
}
}
public static void DumpFallbacks(string gameDataPath) {
Console.WriteLine("Reading TMP_FontAssets");
using var manager = new LEFontManager(gameDataPath);
Console.WriteLine();
Console.WriteLine(manager.DumpFontFallbacks());
}
}
}