-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nier Shader Extractor.c
571 lines (459 loc) · 26.8 KB
/
Nier Shader Extractor.c
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
561
562
563
564
565
566
567
568
569
570
571
// Nier Shader Extractor.cpp : Defines the exported functions for the DLL application.
//
#include <Windows.h>
#include <d3dcompiler.h>
#include <time.h>
#include <stdio.h>
#pragma comment(lib, "d3dcompiler.lib")
#define FOURCC(a, b, c, d) ( (DWORD)(((DWORD)((BYTE)a)) | (((DWORD)((BYTE)b)) << 8) | (((DWORD)((BYTE)c)) << 16) | (((DWORD)((BYTE)d)) << 24)) )
#define SHADER_COMPILED_MAGIC FOURCC('D', 'X', 'B', 'C')
#define SHADER_COMPILED_RDEF_CHUNK_MAGIC FOURCC('R', 'D', 'E', 'F')
#define SHADER_COMPILED_ISGN_CHUNK_MAGIC FOURCC('I', 'S', 'G', 'N')
typedef enum _SHADER_TYPE
{
SHADER_TYPE_COMPUTE = 0x4353,
SHADER_TYPE_VERTEX = 0xFFFE,
SHADER_TYPE_PIXEL = 0xFFFF,
} SHADER_TYPE;
//resource definition chunk
typedef struct _SHADER_COMPILED_HEADER
{
union
{
char signature[4];
DWORD magic;
};
BYTE checksum[16];
DWORD one;
DWORD dwSize;
DWORD dwChunkCount;
DWORD* pdwChunkOffsets; // not sure if it will aways be five
} SHADER_COMPILED_HEADER, *PSHADER_COMPILED_HEADER;
typedef struct _SHADER_COMPILED_RDEF_CHUNK
{
union
{
char signature[4];
DWORD magic;
};
DWORD dwSize;
DWORD dwConstantBufferCount;
DWORD dwConstantBufferDescriptionsOffset;
DWORD dwResourceBindingCount;
DWORD dwResourceBindingDescriptionsOffset;
BYTE MajorVersion;
BYTE MinorVersion;
WORD wProgramType;
DWORD dwCompilerStringOffset;
} SHADER_COMPILED_RDEF_CHUNK, *PSHADER_COMPILED_RDEF_CHUNK;
typedef struct _SHADER_LIST_ENTRY
{
SHADER_COMPILED_HEADER hdr;
fpos_t fileoffset;
const char* szShaderName;
SHADER_TYPE type;
void* pShader;
ID3D10Blob* pDisassembly;
struct _SCRIPT_LIST_ENTRY* pNext;
} SHADER_LIST_ENTRY, *PSHADER_LIST_ENTRY;
typedef struct DATAFILE_ENUM
{
const char* szFileName;
fpos_t rva;
} DATAFILE_ENUM, *PDATAFILE_ENUM;
size_t get_list_size(PSHADER_LIST_ENTRY pHead)
{
size_t index = 0;
if (!pHead)
return 0;
for (PSHADER_LIST_ENTRY it = pHead; it; it = it->pNext, ++index);
return index;
}
void free_list(PSHADER_LIST_ENTRY pHead)
{
PSHADER_LIST_ENTRY pEntry = pHead;
PSHADER_LIST_ENTRY pNext;
if (!pEntry)
return;
while (pEntry->pNext)
{
pNext = pEntry->pNext;
if (pEntry)
{
if (pEntry->pDisassembly)
pEntry->pDisassembly->lpVtbl->Release(pEntry->pDisassembly);
if (pEntry->szShaderName)
free(pEntry->szShaderName);
if (pEntry->hdr.pdwChunkOffsets)
free(pEntry->hdr.pdwChunkOffsets);
if (pEntry->pShader)
free(pEntry->pShader);
free(pEntry);
}
pEntry = pNext;
}
}
errno_t open_file(const char* szFilename, FILE** ppFile, size_t* pSize)
{
if (!szFilename || !ppFile || !pSize)
return -1;
errno_t err = fopen_s(ppFile, szFilename, "rb");
if (err != 0)
return 1;
if (fseek(*ppFile, 0, SEEK_END))
return 2;
*pSize = ftell(*ppFile);
if (*pSize == 0xFFFFFFFF)
return errno;
rewind(*ppFile);
return 0;
}
size_t fstr(char** ppszStr, int nByteAdvance, FILE* pStream)
{
size_t len = 0;
fpos_t pos;
fpos_t new_pos;
char c;
fgetpos(pStream, &pos);
new_pos = pos + nByteAdvance;
fsetpos(pStream, &new_pos);
do
{
fread(&c, sizeof(char), 1, pStream);
++len;
} while (c);
fsetpos(pStream, &new_pos);
*ppszStr = malloc(len + 1);
fprobe(*ppszStr, sizeof(char), len, 0, pStream);
(*ppszStr)[len] = 0;
fsetpos(pStream, &pos);
return len;
}
inline size_t freadrva(void* pBuffer, size_t size, size_t count, int rva, FILE* pStream)
{
fpos_t pos;
fpos_t new_pos;
size_t nElementsRead;
fgetpos(pStream, &pos);
new_pos = rva;
fsetpos(pStream, &new_pos);
nElementsRead = fread(pBuffer, size, count, pStream);
fsetpos(pStream, &pos);
return nElementsRead;
}
inline size_t fprobe(void* pBuffer, size_t size, size_t count, int nBytesAdvance, FILE* pStream)
{
fpos_t pos;
fpos_t new_pos;
size_t nElementsRead;
fgetpos(pStream, &pos);
new_pos = pos + nBytesAdvance;
nElementsRead = fread(pBuffer, size, count, pStream);
fsetpos(pStream, &new_pos);
return nElementsRead;
}
inline void fsetposrel(int nBytesAdvance, FILE* pStream)
{
fpos_t pos;
fpos_t new_pos;
fgetpos(pStream, &pos);
new_pos = pos + nBytesAdvance;
fsetpos(pStream, &new_pos);
}
inline HRESULT disassemble_shader(PSHADER_LIST_ENTRY pEntry)
{
return (pEntry) ? D3DDisassemble(pEntry->pShader, pEntry->hdr.dwSize, D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS, NULL, &pEntry->pDisassembly) : E_INVALIDARG;
}
fpos_t get_chunk_offset(FILE* pFile, PSHADER_LIST_ENTRY pEntry, DWORD magic)
{
DWORD chunk_magic;
fpos_t offset = -1;
fpos_t orignal_offset;
if (pEntry)
{
fgetpos(pFile, &orignal_offset);
for (size_t index = 0; index < pEntry->hdr.dwChunkCount; ++index)
{
offset = orignal_offset + pEntry->hdr.pdwChunkOffsets[index];
fsetpos(pFile, &offset);
fread(&chunk_magic, sizeof(DWORD), 1, pFile);
if (chunk_magic == magic)
break;
else
offset = -1;
}
fsetpos(pFile, &orignal_offset);
}
return offset;
}
PSHADER_LIST_ENTRY parse_shader_binary(FILE* pFile, PDATAFILE_ENUM pEnumerated, PSHADER_LIST_ENTRY pLastEntry)
{
PSHADER_LIST_ENTRY pEntry = NULL;
size_t nBytesRead = 0;
fpos_t offset;
fpos_t resource_def_chunk_offset;
SHADER_COMPILED_RDEF_CHUNK rdef_chunk;
if (!pFile || !pEnumerated)
return NULL;
pEntry = malloc(sizeof(SHADER_LIST_ENTRY));
if (!pEntry)
return NULL;
pEntry->pNext = NULL;
if (pLastEntry)
pLastEntry->pNext = pEntry;
pEntry->szShaderName = pEnumerated->szFileName;
pEntry->fileoffset = pEnumerated->rva;
fsetpos(pFile, &pEnumerated->rva);
nBytesRead = fread(&pEntry->hdr, 1, UFIELD_OFFSET(SHADER_COMPILED_HEADER, pdwChunkOffsets), pFile);
if (nBytesRead < UFIELD_OFFSET(SHADER_COMPILED_HEADER, pdwChunkOffsets))
{
printf("Partial Read! Read %x bytes out of %x\n", nBytesRead, pEntry->hdr.dwSize);
}
pEntry->hdr.pdwChunkOffsets = malloc(sizeof(DWORD) * pEntry->hdr.dwChunkCount);
fprobe(pEntry->hdr.pdwChunkOffsets, sizeof(DWORD), pEntry->hdr.dwChunkCount, -FIELD_OFFSET(SHADER_COMPILED_HEADER, pdwChunkOffsets), pFile);
if (nBytesRead < pEntry->hdr.dwChunkCount)
{
printf("Partial Read! Read %x bytes out of %x\n", nBytesRead, pEntry->hdr.dwSize);
}
resource_def_chunk_offset = get_chunk_offset(pFile, pEntry, SHADER_COMPILED_RDEF_CHUNK_MAGIC);
if (resource_def_chunk_offset != -1)
{
fgetpos(pFile, &offset);
fsetpos(pFile, &resource_def_chunk_offset);
fprobe(&rdef_chunk, 1, sizeof(SHADER_COMPILED_RDEF_CHUNK), 0, pFile);
pEntry->type = rdef_chunk.wProgramType;
fsetpos(pFile, &offset);
}
else
{
printf("[WARNING]: Couldn't determine shader type!\n");
pEntry->type = 0;
}
pEntry->pShader = malloc(pEntry->hdr.dwSize);
nBytesRead = fread(pEntry->pShader, 1, pEntry->hdr.dwSize, pFile);
if (nBytesRead < pEntry->hdr.dwSize)
{
printf("Partial Read! Read %x bytes out of %x\n", nBytesRead, pEntry->hdr.dwSize);
}
if (FAILED(disassemble_shader(pEntry)))
pEntry->pDisassembly = NULL;
return pEntry;
}
void data_file_enum_contents(FILE* pFile, struct DATAFILE_ENUM** ppEnum, DWORD* pdwFileCount)
{
if (!pdwFileCount)
return;
DWORD dwOffset;
DWORD dwNextNameOffset;
DWORD offset_tbl_rva;
fpos_t pos;
fgetpos(pFile, &pos);
freadrva(pdwFileCount, sizeof(DWORD), 1, 4, pFile);
freadrva(&offset_tbl_rva, sizeof(DWORD), 1, 8, pFile);
freadrva(&dwOffset, sizeof(DWORD), 1, 16, pFile);
freadrva(&dwNextNameOffset, sizeof(DWORD), 1, dwOffset, pFile);
*ppEnum = calloc(*pdwFileCount, sizeof(struct DATAFILE_ENUM));
for (DWORD i = 0; i < (*pdwFileCount); ++i)
{
fstr(&(*ppEnum)[i].szFileName, dwOffset + 4 + i * dwNextNameOffset, pFile);
freadrva(&(*ppEnum)[i].rva, sizeof(DWORD), 1, offset_tbl_rva + 4 * i, pFile);
}
fsetpos(pFile, &pos);
}
PSHADER_LIST_ENTRY extract_shaders(FILE* pFile, size_t file_size)
{
DWORD magic;
PSHADER_LIST_ENTRY pHead = NULL;
PSHADER_LIST_ENTRY pLast = NULL;
BOOLEAN bHeadMade = FALSE;
DWORD file_count;
PDATAFILE_ENUM pEnumerated;
if (!pFile)
return NULL;
data_file_enum_contents(pFile, &pEnumerated, &file_count);
for (DWORD i = 0; i < file_count; ++i)
{
if (strstr(pEnumerated[i].szFileName, ".pso") || strstr(pEnumerated[i].szFileName, ".vso") || strstr(pEnumerated[i].szFileName, ".cso"))
{
freadrva(&magic, sizeof(DWORD), 1, pEnumerated[i].rva, pFile);
if (magic == SHADER_COMPILED_MAGIC)
{
if (bHeadMade)
{
pLast = parse_shader_binary(pFile, &pEnumerated[i], pLast);
}
else
{
pHead = parse_shader_binary(pFile, &pEnumerated[i], NULL);
pLast = pHead;
bHeadMade = TRUE;
}
}
}
}
return pHead;
}
void save_shaders(const char* szSavePath, const char* szPrefix, PSHADER_LIST_ENTRY pHead, BOOLEAN bDumpDissasmbly, BOOLEAN bDumpDecompiled)
{
char szFilePath[MAX_PATH];
char szDecompile[MAX_PATH];
char szCmdline[512];
char* szShaderFormat;
char* szShaderDissasemblyFormat;
char* szTruncate;
FILE* pFile;
errno_t err;
STARTUPINFOA si;
PROCESS_INFORMATION pi;
if (bDumpDecompiled)
{
GetModuleFileNameA(NULL, szDecompile, MAX_PATH);
szTruncate = strrchr(szDecompile, '\\');
strcpy_s(++szTruncate, MAX_PATH - (strlen(szDecompile) + strlen(szTruncate)), "decompile.exe");
}
for (PSHADER_LIST_ENTRY it = pHead; it; it = it->pNext)
{
if (it->type != SHADER_TYPE_COMPUTE && it->type != SHADER_TYPE_VERTEX && it->type != SHADER_TYPE_PIXEL)
continue;
if (szPrefix)
{
szShaderFormat = "%s\\%s_%s";
szShaderDissasemblyFormat = "%s\\disassembly_%s_%s.hlsl";
}
else
{
szShaderFormat = "%s\\%s";
szShaderDissasemblyFormat = "%s\\disassembly_%s.hlsl";
}
if (szPrefix)
sprintf_s(szFilePath, MAX_PATH, szShaderFormat, (szSavePath) ? szSavePath : "", szPrefix, it->szShaderName);
else
sprintf_s(szFilePath, MAX_PATH, szShaderFormat, (szSavePath) ? szSavePath : "", it->szShaderName);
err = fopen_s(&pFile, szFilePath, "wb");
if (!err)
{
fwrite(it->pShader, 1, it->hdr.dwSize, pFile);
fclose(pFile);
if (bDumpDecompiled)
{
memset(&si, 0, sizeof(STARTUPINFOA));
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFOA);
sprintf_s(szCmdline, 512, "decompile.exe -D \"%s\"", szFilePath);
if (!CreateProcessA(szDecompile, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
printf("CreateProcessA Failed with error code %x\n", GetLastError());
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
if (bDumpDissasmbly)
{
if (szPrefix)
sprintf_s(szFilePath, MAX_PATH, szShaderDissasemblyFormat, (szSavePath) ? szSavePath : "", szPrefix, it->szShaderName);
else
sprintf_s(szFilePath, MAX_PATH, szShaderDissasemblyFormat, (szSavePath) ? szSavePath : "", it->szShaderName);
if (it->pDisassembly)
{
err = fopen_s(&pFile, szFilePath, "wb");
if (!err)
{
fwrite(it->pDisassembly->lpVtbl->GetBufferPointer(it->pDisassembly), 1, it->pDisassembly->lpVtbl->GetBufferSize(it->pDisassembly), pFile);
fclose(pFile);
}
}
}
}
}
void print_welcome()
{
#if WIN32
printf("Nier Automata Shader Extractor v0.04 by Martino\n\n");
#else
printf("Nier Automata Shader Extractor (x64) v0.04 by Martino\n\n");
#endif
}
void print_help()
{
printf("Usage: -d [filename] [dumppath] | -help\n"
"-d Dumps all compiled ruby srcipts in the file designated [filename] to the [dumppath].\n"
" [dumppath] is optional and if ommited the scripts will be dumped to the extractor path\n\n"
"-p Appends the filename as a prefix to the dumpfile. To be used with -d\n");
}
int main(int argc, char** argv)
{
FILE* pFile;
size_t file_size;
size_t disasm_index;
char* szFilename = NULL;
char* szFilenameCopy = NULL;
char* szDumpPath = NULL;
char* szRelativeFilename = NULL;
BOOLEAN bPrefix = FALSE;
print_welcome();
if (argc > 1)
{
if (!_stricmp(argv[1], "-help"))
{
print_help();
return 1;
}
else if (!_stricmp(argv[1], "-d"))
{
if (!_stricmp(argv[2], "-p"))
{
szFilename = argv[3];
if (argc >= 5)
szDumpPath = argv[4];
bPrefix = TRUE;
}
else
{
szFilename = argv[2];
if (argc >= 4)
szDumpPath = argv[3];
}
}
}
else
{
print_help();
return 2;
}
if (szFilename && bPrefix)
{
size_t size = strlen(szFilename) + 1;
szFilenameCopy = malloc(size);
if (szFilenameCopy)
{
strcpy_s(szFilenameCopy, size, szFilename);
szRelativeFilename = strrchr(szFilenameCopy, '\\');
char* szDumpFilePrefix = strstr(szRelativeFilename++, ".");
*szDumpFilePrefix = 0;
}
else
{
printf("Could not allocate the required memory!\n");
return 3;
}
}
if (open_file(szFilename, &pFile, &file_size))
{
if (szFilenameCopy)
free(szFilenameCopy);
printf("Could not open the file: %s\nPlease check the file path and try again\n", szFilename);
return 4;
}
clock_t start = clock();
PSHADER_LIST_ENTRY pHead = extract_shaders(pFile, file_size);
fclose(pFile);
if (pHead)
{
printf("Extracted %d shaders in %.3f seconds\n", get_list_size(pHead), ((float)(clock() - start) / (float)CLOCKS_PER_SEC));
save_shaders(szDumpPath, szRelativeFilename, pHead, TRUE, TRUE);
free_list(pHead);
}
if (szFilenameCopy)
free(szFilenameCopy);
return 0;
}