forked from MontagueM/MontevenDynamicExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.cpp
298 lines (278 loc) · 9.14 KB
/
texture.cpp
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
#include "texture.h"
//#include <shlwapi.h>
//#pragma comment(lib, "shlwapi.lib")
void Texture::getHeader(std::string x)
{
memcpy((char*)&textureFormat, data + 4, 2);
memcpy((char*)&width, data + 0x22, 2);
memcpy((char*)&height, data + 0x24, 2);
memcpy((char*)&arraySize, data + 0x28, 2);
uint32_t val;
memcpy((char*)&val, data + 0x3C, 4);
largeHash = uint32ToHexStr(val);
}
void Texture::tex2DDS(std::string fullSavePath)
{
if (largeHash != "ffffffff" && largeHash != "")
dataFile = new File(largeHash, packagesPath);
else
dataFile = new File(getReferenceFromHash(hash, packagesPath), packagesPath);
writeTexture(fullSavePath);
}
void Texture::tex2Other(std::string fullSavePath, std::string saveFormat)
{
tex2DDS(fullSavePath);
std::string dxgiFormat;
dxgiFormat = DXGI_FORMAT[textureFormat];
/// Code to try and fix texconv not always working from command line, couldn't get it to work
//wchar_t exePath[MAX_PATH];
//DWORD nSize = 0;
//GetModuleFileName(NULL, exePath, MAX_PATH);
//std::wstring wPath(exePath);
//wPath.erase(wPath.rfind('\\'));
//std::wstring wFSP(fullSavePath.begin(), fullSavePath.end());
//std::wstring wsaveFormat(saveFormat.begin(), saveFormat.end());
//std::wstring wdxgiFormat(dxgiFormat.begin(), dxgiFormat.end());
//std::wstring wPathNoBackslashes = L"";
//for (auto& c : wPath)
//{
// if (c == '\\') wPathNoBackslashes += '/';
// else wPathNoBackslashes += c;
//}
//std::wstring str = L'"' + wPathNoBackslashes + L"/texconv.exe" + L'"' + L" " + wFSP + L"\" -y -ft " + wsaveFormat + L" -f " + wdxgiFormat;
//wprintf(str.c_str());
//_wsystem(str.c_str());
std::string str = "texconv.exe \"" + fullSavePath + "\" -y -ft " + saveFormat + " -f " + dxgiFormat;
printf(str.c_str());
system(str.c_str());
// Delete dds file if it exists
std::string newPath = fullSavePath.substr(0, fullSavePath.size() - 3) + saveFormat;
std::ifstream f(newPath);
if (f) std::remove(fullSavePath.c_str());
}
void Texture::writeTexture(std::string fullSavePath)
{
bool bCompressed = false;
if (70 < textureFormat < 99) bCompressed = true;
DDSHeader dds;
DXT10Header dxt;
dds.MagicNumber = 542327876;
dds.dwSize = 124;
dds.dwFlags = (0x1 + 0x2 + 0x4 + 0x1000) + 0x8;
dds.dwHeight = height;
dds.dwWidth = width;
dds.dwPitchOrLinearSize = 0;
dds.dwDepth = 0;
dds.dwMipMapCount = 0;
dds.dwReserved1 = std::array<uint32_t, 11>();
dds.dwPFSize = 32;
dds.dwPFRGBBitCount = 0;
dds.dwPFRGBBitCount = 32;
dds.dwPFRBitMask = 0xFF;
dds.dwPFGBitMask = 0xFF00;
dds.dwPFBBitMask = 0xFF0000;
dds.dwPFABitMask = 0xFF000000;
dds.dwCaps = 0x1000;
dds.dwCaps2 = 0;
dds.dwCaps3 = 0;
dds.dwCaps4 = 0;
dds.dwReserved2 = 0;
if (bCompressed)
{
dds.dwPFFlags = 0x1 + 0x4; // contains alpha data + contains compressed RGB data
dds.dwPFFourCC = 808540228;
dxt.dxgiFormat = textureFormat;
dxt.resourceDimension = 3; // DDS_DIMENSION_TEXTURE2D
if (arraySize % 6 == 0)
{
// Compressed cubemap
dxt.miscFlag = 4;
dxt.arraySize = arraySize / 6;
}
else
{
// Compressed BCn
dxt.miscFlag = 0;
dxt.arraySize = 1;
}
}
else
{
// Uncompressed
dds.dwPFFlags = 0x1 + 0x40; // contains alpha data + contains uncompressed RGB data
dds.dwPFFourCC = 0;
dxt.miscFlag = 0;
dxt.arraySize = 1;
dxt.miscFlags2 = 0x1;
}
writeFile(dds, dxt, fullSavePath);
}
void Texture::writeFile(DDSHeader dds, DXT10Header dxt, std::string fullSavePath)
{
FILE* outputFile;
fopen_s(&outputFile, fullSavePath.c_str(), "wb");
if (outputFile != NULL) {
fwrite(&dds, sizeof(struct DDSHeader), 1, outputFile);
fwrite(&dxt, sizeof(struct DXT10Header), 1, outputFile);
int fileSize = dataFile->getData();
fwrite(dataFile->data, fileSize, 1, outputFile);
fclose(outputFile);
}
}
void Material::parseMaterial(std::unordered_map<uint64_t, uint32_t> hash64Table)
{
getData();
uint32_t textureCount;
uint32_t textureOffset;
// Pixel shader textures
memcpy((char*)&textureCount, data + 0x2A0, 4);
memcpy((char*)&textureOffset, data + 0x2A8, 4);
textureOffset += 0x2A8 + 0x10;
uint64_t h64Val;
for (int i = textureOffset; i < textureOffset + textureCount * 0x18; i += 0x18)
{
uint8_t textureIndex;
memcpy((char*)&textureIndex, data + i, 1);
uint32_t val;
memcpy((char*)&val, data + i + 8, 4);
std::string h64Check = uint32ToHexStr(val);
if (h64Check == "ffffffff")
{
memcpy((char*)&h64Val, data + i + 0x10, 8);
if (h64Val == 0) continue;
std::string textureHash = getHash64(h64Val, hash64Table);
if (textureHash != "ffffffff")
{
Texture* texture = new Texture(textureHash, packagesPath);
textures[textureIndex] = texture;
}
}
else
{
printf("Support old texture format");
return;
}
}
}
void Material::exportTextures(std::string fullSavePath, std::string saveFormat)
{
std::string actualSavePath;
std::string newPath;
for (auto& element : textures)
{
uint8_t texID = element.first;
Texture* tex = element.second;
actualSavePath = fullSavePath + "/" + tex->hash + ".dds";
newPath = fullSavePath + "/" + tex->hash + "." + saveFormat;
std::ifstream f(newPath);
std::ifstream q(actualSavePath);
if (f || q)
{
free(tex);
continue;
}
if (saveFormat == "dds") tex->tex2DDS(actualSavePath);
else tex->tex2Other(actualSavePath, saveFormat);
free(tex);
}
}
void Material::parseCBuffers()
{
/*
90008080 stride 16
09008080 stride 1
3F018080 stride 16 external cbuffers
---
vertex 0x68 vertex shader texture offset
vertex 0x80 09008080 internal offset
vertex 0x90 90008080 internal offset
vertex 0xA0 3F018080 internal offset
vertex 0xB0 90008080 internal offset
pixel 0x2C0 09008080 internal offset
pixel 0x2E0 3F018080 internal offset
pixel 0x2D0 90008080 internal offset
pixel 0x2F0 90008080 internal offset
pixel 0x30C external cbuffer hash
*/
std::vector<int> pixelOffsets = { 0x2C0, 0x2D0, 0x2F0 };
uint32_t val;
uint32_t count;
getData();
int i = 0;
for (auto& offset : pixelOffsets)
{
memcpy((char*)&val, data + offset, 4);
if (val == 0) continue;
offset += val;
memcpy((char*)&count, data + offset, 4);
uint32_t cbType;
memcpy((char*)&cbType, data + offset + 8, 4);
std::string floats = getCBufferFromOffset(data, offset + 0x10, count, cbType, std::to_string(i));
cbuffers.push_back(floats);
i++;
}
memcpy((char*)&val, data + 0x30C, 4);
if (val != 4294967295)
{
File buffer = File(getReferenceFromHash(uint32ToHexStr(val), packagesPath), packagesPath);
int fileSize = buffer.getData();
std::string pixelExternalFloats = getCBufferFromOffset(buffer.data, 0, fileSize / 16, 1, "pixelExt");
cbuffers.push_back(pixelExternalFloats);
}
}
void Material::writeCBuffers(std::string fullSavePath)
{
parseCBuffers();
FILE* cbFile;
std::string path = fullSavePath + "/cbuffers.txt";
fopen_s(&cbFile, path.c_str(), "a");
std::string header = "\n--------\nCBUFFERS FOR MATERIAL " + hash + ":\n--------\n";
fwrite(header.c_str(), header.size(), 1, cbFile);
for (auto& cbuffer : cbuffers)
{
fwrite(cbuffer.c_str(), cbuffer.size(), 1, cbFile);
}
fclose(cbFile);
}
std::string getCBufferFromOffset(unsigned char* data, int offset, int count, uint32_t cbType, std::string name)
{
if (cbType == 2155872265)
{
std::string allFloat = "static float cb" + name + '[' + std::to_string(count) + "] = \n{\n ";
allFloat.reserve(count);
int8_t val;
for (int i = 0; i < count; i++)
{
memcpy((char*)&val, data + offset + i, 1);
std::string floats = std::to_string((float)val/128) + ",";
allFloat += floats;
if (i % 8 == 0 && i != 0) allFloat += "\n ";
}
allFloat += "\n};\n";
return allFloat;
}
else if (cbType == 2155872400 || offset == 0)
{
std::string allFloat4 = "static float4 cb" + name + '[' + std::to_string(count) + "] = \n{\n";
float_t val;
for (int i = 0; i < count; i++)
{
std::string float4 = " float4(";
for (int j = 0; j < 4; j++)
{
memcpy((char*)&val, data + offset + i * 16 + j * 4, 4);
float4 += std::to_string(val);
if (j < 3) float4 += ", ";
else float4 += "),\n";
}
allFloat4 += float4;
}
allFloat4 += "};\n";
return allFloat4;
}
else
{
std::cerr << "\nUnknown cbuffer class found, exiting...\n";
exit(1);
}
}