-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
560 lines (457 loc) · 21.7 KB
/
Copy pathProgram.cs
File metadata and controls
560 lines (457 loc) · 21.7 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Lumina.Data.Files;
using Lumina.Excel;
using Lumina.Excel.Sheets;
using Lumina.Text.ReadOnly;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
namespace Lumenstone;
class Program
{
private const int DefaultIconStart = 1;
private const int DefaultIconEnd = 250000;
static IJsonTypeInfoResolver CreateLuminaIgnoreExcelPageResolver()
{
var resolver = new DefaultJsonTypeInfoResolver();
resolver.Modifiers.Add(static typeInfo =>
{
if (typeInfo.Kind != JsonTypeInfoKind.Object)
return;
// Only touch Lumina Excel row/subrow structs (Action, Item, etc.)
var t = typeInfo.Type;
bool isLuminaRow =
t.GetInterfaces().Any(i =>
i.IsGenericType &&
(i.GetGenericTypeDefinition() == typeof(IExcelRow<>) ||
i.GetGenericTypeDefinition() == typeof(IExcelSubrow<>)));
if (!isLuminaRow)
return;
// Remove ExcelPage and RowOffset if present
for (int i = typeInfo.Properties.Count - 1; i >= 0; i--)
{
if (typeInfo.Properties[i].Name == "ExcelPage" || typeInfo.Properties[i].Name == "RowOffset")
typeInfo.Properties.RemoveAt(i);
}
});
return resolver;
}
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: dotnet run <sqpackPath> <patch> [full] [options]");
Console.WriteLine("Options:");
Console.WriteLine(" --full Force re-export for icons/maps/loading images.");
Console.WriteLine(" --icons-only Skip JSON/maps/loading and export icons only.");
Console.WriteLine($" --icon-start <id> First icon id to export (default: {DefaultIconStart}).");
Console.WriteLine($" --icon-end <id> Last icon id to export (default: {DefaultIconEnd}).");
Console.WriteLine(" --icon-range <start-end> Export a contiguous icon range.");
Console.WriteLine(" --icon-chunk-size <n> Chunk size for icon range generation.");
Console.WriteLine(" --icon-chunk-index <n> Zero-based chunk index when using --icon-chunk-size.");
Console.WriteLine(" --icons-full Force overwrite icons in selected range/chunk.");
return;
}
// Initialize Lumina with the sqpack path
string sqpackPath = args[0]; // Assuming the first argument is the sqpack path
var patch = args[1];
var optionArgs = args.Skip(2).ToArray();
bool full = HasFlag(optionArgs, "full") || HasFlag(optionArgs, "--full");
bool iconsOnly = HasFlag(optionArgs, "--icons-only");
bool hasIconRangeOption = HasFlag(optionArgs, "--icon-start")
|| HasFlag(optionArgs, "--icon-end")
|| HasFlag(optionArgs, "--icon-range")
|| HasFlag(optionArgs, "--icon-chunk-size")
|| HasFlag(optionArgs, "--icon-chunk-index");
bool iconsFull = full || HasFlag(optionArgs, "--icons-full");
int iconStart = DefaultIconStart;
int iconEnd = DefaultIconEnd;
if (TryGetOptionValue(optionArgs, "--icon-range", out var iconRange))
{
if (!TryParseRange(iconRange, out iconStart, out iconEnd))
{
Console.WriteLine($"Invalid --icon-range value '{iconRange}'. Expected format: <start-end>.");
return;
}
}
if (TryGetIntOption(optionArgs, "--icon-start", out var iconStartOverride))
iconStart = iconStartOverride;
if (TryGetIntOption(optionArgs, "--icon-end", out var iconEndOverride))
iconEnd = iconEndOverride;
if (TryGetIntOption(optionArgs, "--icon-chunk-size", out var chunkSize))
{
if (!TryGetIntOption(optionArgs, "--icon-chunk-index", out var chunkIndex))
{
Console.WriteLine("--icon-chunk-index is required when --icon-chunk-size is provided.");
return;
}
if (chunkSize <= 0 || chunkIndex < 0)
{
Console.WriteLine("--icon-chunk-size must be > 0 and --icon-chunk-index must be >= 0.");
return;
}
var chunkStart = iconStart + (chunkSize * chunkIndex);
var chunkEnd = Math.Min(iconEnd, chunkStart + chunkSize - 1);
if (chunkStart > iconEnd)
{
Console.WriteLine($"Chunk index {chunkIndex} is outside the requested icon range {iconStart}-{iconEnd}.");
return;
}
iconStart = chunkStart;
iconEnd = chunkEnd;
}
if (hasIconRangeOption)
{
// Explicit icon selection means "regenerate this icon slice only".
iconsOnly = true;
iconsFull = true;
}
if (iconStart < 1 || iconEnd < iconStart)
{
Console.WriteLine($"Invalid icon range {iconStart}-{iconEnd}. Ensure start >= 1 and end >= start.");
return;
}
var luminaEN = new Lumina.GameData(sqpackPath, new() { DefaultExcelLanguage = Lumina.Data.Language.English });
var luminaDE = new Lumina.GameData(sqpackPath, new() { DefaultExcelLanguage = Lumina.Data.Language.German });
var luminaFR = new Lumina.GameData(sqpackPath, new() { DefaultExcelLanguage = Lumina.Data.Language.French });
var luminaJA = new Lumina.GameData(sqpackPath, new() { DefaultExcelLanguage = Lumina.Data.Language.Japanese });
var uiColors = luminaEN.GetExcelSheet<UIColor>();
if (uiColors == null)
throw new InvalidOperationException("Could not load UIColor sheet.");
JsonSerializerOptions options = new() {
ReferenceHandler = ReferenceHandler.IgnoreCycles,
WriteIndented = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Converters = { new SeStringConverter(uiColors), new LazyRowConverterFactory(), new LazySubrowConverterFactory() },
TypeInfoResolver = CreateLuminaIgnoreExcelPageResolver(),
};
if (!iconsOnly)
{
// Get all types in the Lumina.Excel.GeneratedSheets namespace
var types = Assembly.GetAssembly(typeof(Lumina.Excel.Sheets.Action))!.GetTypes()
.Where(t => t.Namespace == "Lumina.Excel.Sheets"
&& !t.IsAbstract
&& t.GetInterfaces()
.Any(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(Lumina.Excel.IExcelRow<>)
&& i.GenericTypeArguments[0] == t));
MethodInfo? generic = typeof(Program).GetMethod(nameof(ExtractSheetForAllLanguages), BindingFlags.Static | BindingFlags.NonPublic);
if (generic == null)
return;
// Call ExtractSheetForAllLanguages for each type
foreach (var type in types)
{
Console.WriteLine(type.Name);
MethodInfo constructed = generic.MakeGenericMethod(type);
constructed.Invoke(null, new object[] { patch, luminaEN, luminaDE, luminaFR, luminaJA, options });
}
var subrowTypes = Assembly.GetAssembly(typeof(Lumina.Excel.Sheets.GilShopItem))!.GetTypes()
.Where(t => t.Namespace == "Lumina.Excel.Sheets"
&& !t.IsAbstract
&& t.GetInterfaces()
.Any(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(Lumina.Excel.IExcelSubrow<>)
&& i.GenericTypeArguments[0] == t));
generic = typeof(Program).GetMethod(nameof(ExtractSubrowSheetForAllLanguages), BindingFlags.Static | BindingFlags.NonPublic);
if (generic == null)
return;
// Call ExtractSheetForAllLanguages for each type
foreach (var type in subrowTypes)
{
Console.WriteLine(type.Name);
MethodInfo constructed = generic.MakeGenericMethod(type);
constructed.Invoke(null, new object[] { patch, luminaEN, luminaDE, luminaFR, luminaJA, options });
}
ExtractMaps(luminaEN, full);
ExtractLoadingImages(luminaEN, full);
}
Console.WriteLine($"Extracting icons from {iconStart} to {iconEnd} (full={iconsFull}).");
ExtractIcons(iconStart, iconEnd, luminaEN, iconsFull);
}
private static bool HasFlag(string[] args, string name) =>
args.Any(a => string.Equals(a, name, StringComparison.OrdinalIgnoreCase));
private static bool TryGetOptionValue(string[] args, string optionName, out string value)
{
value = string.Empty;
for (int i = 0; i < args.Length - 1; i++)
{
if (!string.Equals(args[i], optionName, StringComparison.OrdinalIgnoreCase))
continue;
value = args[i + 1];
return true;
}
return false;
}
private static bool TryGetIntOption(string[] args, string optionName, out int value)
{
value = 0;
if (!TryGetOptionValue(args, optionName, out var rawValue))
return false;
if (!int.TryParse(rawValue, out value))
{
Console.WriteLine($"Invalid integer for {optionName}: '{rawValue}'.");
return false;
}
return true;
}
private static bool TryParseRange(string range, out int start, out int end)
{
start = 0;
end = 0;
var parts = range.Split('-', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (parts.Length != 2)
return false;
return int.TryParse(parts[0], out start) && int.TryParse(parts[1], out end);
}
static void ExtractSheetForAllLanguages<T>(string patch, Lumina.GameData luminaEN, Lumina.GameData luminaDE, Lumina.GameData luminaFR, Lumina.GameData luminaJA,
JsonSerializerOptions options) where T : struct, IExcelRow<T>
{
ExtractSheet<T>(luminaEN, patch, "en", options);
ExtractSheet<T>(luminaDE, patch, "de", options);
ExtractSheet<T>(luminaFR, patch, "fr", options);
ExtractSheet<T>(luminaJA, patch, "ja", options);
}
static void ExtractSubrowSheetForAllLanguages<T>(string patch, Lumina.GameData luminaEN, Lumina.GameData luminaDE, Lumina.GameData luminaFR, Lumina.GameData luminaJA,
JsonSerializerOptions options) where T : struct, IExcelSubrow<T>
{
ExtractSubrowSheet<T>(luminaEN, patch, "en", options);
ExtractSubrowSheet<T>(luminaDE, patch, "de", options);
ExtractSubrowSheet<T>(luminaFR, patch, "fr", options);
ExtractSubrowSheet<T>(luminaJA, patch, "ja", options);
}
static void ExtractSheet<T>(Lumina.GameData lumina, string patch, string lang, JsonSerializerOptions options) where T: struct, Lumina.Excel.IExcelRow<T>
{
// Get the ClassJobs Excel sheet
var sheet = lumina.GetExcelSheet<T>();
if (sheet == null) {
return;
}
const int cPageSize = 500;
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), $"json/{patch}/{lang}/{typeof(T).Name}");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
// Extract data and save as JSON
var sheetData = new List<T>();
int currentPage = 1;
foreach (var row in sheet)
{
sheetData.Add(row);
if (sheetData.Count >= cPageSize) {
string jsonForPage = JsonSerializer.Serialize(sheetData, options);
string fileNameForPage = $"{currentPage}.json";
string filePathForPage = Path.Combine(directoryPath, fileNameForPage);
File.WriteAllText(filePathForPage, jsonForPage);
sheetData.Clear();
Console.WriteLine($"{typeof(T).Name} data extracted and saved to {fileNameForPage}");
currentPage++;
}
}
if (sheetData.Count == 0)
return;
// Serialize the object
string json = JsonSerializer.Serialize(sheetData, options);
string fileName = $"{currentPage}.json";
string filePath = Path.Combine(directoryPath, fileName);
File.WriteAllText(filePath, json);
Console.WriteLine($"{typeof(T).Name} data extracted and saved to {fileName}");
}
static void ExtractSubrowSheet<T>(Lumina.GameData lumina, string patch, string lang, JsonSerializerOptions options) where T: struct, Lumina.Excel.IExcelSubrow<T>
{
// Get the ClassJobs Excel sheet
var sheet = lumina.GetSubrowExcelSheet<T>();
if (sheet == null) {
return;
}
const int cPageSize = 500;
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), $"json/{patch}/{lang}/{typeof(T).Name}");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
// Extract data and save as JSON
var sheetData = new List<T>();
int currentPage = 1;
foreach (var rows in sheet)
{
foreach (var row in rows) {
sheetData.Add(row);
}
if (sheetData.Count >= cPageSize) {
string jsonForPage = JsonSerializer.Serialize(sheetData, options);
string fileNameForPage = $"{currentPage}.json";
string filePathForPage = Path.Combine(directoryPath, fileNameForPage);
File.WriteAllText(filePathForPage, jsonForPage);
sheetData.Clear();
Console.WriteLine($"{typeof(T).Name} data extracted and saved to {fileNameForPage}");
currentPage++;
}
}
if (sheetData.Count == 0)
return;
// Serialize the object
string json = JsonSerializer.Serialize(sheetData, options);
string fileName = $"{currentPage}.json";
string filePath = Path.Combine(directoryPath, fileName);
File.WriteAllText(filePath, json);
Console.WriteLine($"{typeof(T).Name} data extracted and saved to {fileName}");
}
private const string IconFileFormat = "ui/icon/{0:D3}000/{1}{2:D6}.tex";
private const string IconHDFileFormat = "ui/icon/{0:D3}000/{1}{2:D6}_hr1.tex";
private static TexFile? GetIcon(Lumina.GameData lumina, string type, int iconId, bool hd)
{
type ??= string.Empty;
if (type.Length > 0 && !type.EndsWith("/"))
type += "/";
var filePath = string.Format(hd ? IconHDFileFormat :IconFileFormat, iconId / 1000, type, iconId);
try {
var file = lumina.GetFile<TexFile>(filePath);
if (file != default(TexFile) || type.Length <= 0) return file;
// Couldn't get specific type, try for generic version.
filePath = string.Format(hd ? IconHDFileFormat : IconFileFormat, iconId / 1000, string.Empty, iconId);
file = lumina.GetFile<TexFile>(filePath);
return file;
} catch (FileNotFoundException) {
return null;
}
}
static void ExtractIcons(int first, int last, Lumina.GameData lumina, bool fullImport)
{
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "icons");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
for (int i = first; i <= last; ++i) {
var iconFilePath = Path.Combine(directoryPath, $"{i / 1000:D3}000", $"{i:D6}.png");
if (fullImport || !File.Exists(iconFilePath)) {
var icon = GetIcon(lumina, "en/", i, false);
if (icon != null && icon != default(TexFile)) {
Console.WriteLine($"-> {i:D6}");
var folderPath = Path.Combine(directoryPath, $"{i / 1000:D3}000");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var image = GetImage(icon);
if (image != null)
image.Save(iconFilePath);
}
}
var iconHDFilePath = Path.Combine(directoryPath, $"{i / 1000:D3}000", $"{i:D6}_hr1.png");
if (fullImport || !File.Exists(iconHDFilePath)) {
var iconHD = GetIcon(lumina, "en/", i, true);
if (iconHD != null && iconHD != default(TexFile)) {
Console.WriteLine($"-> HD {i:D6}");
var folderPath = Path.Combine(directoryPath, $"{i / 1000:D3}000");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var image = GetImage(iconHD);
if (image != null)
image.Save(iconHDFilePath);
}
}
}
}
private static Image<Bgra32>? GetImage(TexFile tex)
{
// Create a new image from the raw pixel data
try
{
var image = Image.LoadPixelData<Bgra32>(tex.ImageData, tex.Header.Width, tex.Header.Height);
return image;
}
catch (System.NotSupportedException)
{
Console.WriteLine("Failed to extract image!");
return null;
} }
private static void ExtractMaps(Lumina.GameData lumina, bool fullImport)
{
fullImport = true;
var sheet = lumina.GetExcelSheet<Map>();
if (sheet == null)
return;
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "maps");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
foreach (var map in sheet) {
var idProperty = typeof(Map).GetProperty("Id");
if (idProperty == null || idProperty.PropertyType != typeof(ReadOnlySeString))
continue;
var idValue = idProperty.GetValue(map);
if (idValue == null)
continue;
var idString = ((ReadOnlySeString)idValue).AsSpan().ToString();
// Assuming idString is in the format "xxx/yyy"
string[] parts = idString.Split('/');
if (parts.Length != 2)
continue; // Ensure idString is in the expected format
var outputFilePath = Path.Combine(directoryPath, parts[0] + "/" + parts[0] + "." + parts[1] + ".jpg");
if (fullImport || !File.Exists(outputFilePath)) {
// Directly concatenate "ui" and "maps" with the rest of the path
string filePath = "ui/map/" + idString + "/" + parts[0] + parts[1] + "_m.tex";
if (!lumina.FileExists(filePath))
filePath = "ui/map/" + idString + "/" + parts[0] + parts[1] + "m_m.tex";
if (!lumina.FileExists(filePath)) {
Console.WriteLine("Failed to extract map for " + idString);
continue;
}
Console.WriteLine("Extracting data for map: " + idString);
// Access the lumina data
var file = lumina.GetFile<TexFile>(filePath);
var folderPath = Path.Combine(directoryPath, parts[0]);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
if (file == null)
continue;
var image = GetImage(file);
if (image != null)
image.SaveAsJpeg(outputFilePath);
}
}
}
private static void ExtractLoadingImages(Lumina.GameData lumina, bool fullImport)
{
var sheet = lumina.GetExcelSheet<LoadingImage>();
if (sheet == null)
return;
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "loadingimages");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
foreach (var loadingImage in sheet) {
var fileName = typeof(LoadingImage).GetProperty("Unknown0");
if (fileName == null || fileName.PropertyType != typeof(ReadOnlySeString))
continue;
var fileValue = fileName.GetValue(loadingImage);
if (fileValue == null)
continue;
var fileString = ((ReadOnlySeString)fileValue).AsSpan().ToString();
var outputFilePath = Path.Combine(directoryPath, fileString + ".jpg");
if (fullImport || !File.Exists(outputFilePath)) {
string filePath = "ui/loadingimage/" + fileString + ".tex";
if (!lumina.FileExists(filePath))
continue;
Console.WriteLine("Extracting data for loading image: " + fileString);
// Access the lumina data
var file = lumina.GetFile<TexFile>(filePath);
if (file == null)
continue;
var image = GetImage(file);
if (image != null)
image.SaveAsJpeg(outputFilePath);
}
}
}
}