Skip to content

Commit 03e52ed

Browse files
committed
PS3 Texture support.
1 parent 4fb1766 commit 03e52ed

5 files changed

Lines changed: 227 additions & 44 deletions

File tree

DRM/DRM.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//Constants
88
const unsigned int DRM_MAX_SECTIONS = 16777215;
99

10-
#define REPACK_MODE (0)
10+
#define REPACK_MODE (1)//Must be on for PCD->DDS tool
1111

1212
#if (TR7 || TRAE)
1313
#define DRM_VERSION (14)

PCD2DDS/File.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
#include <iostream>
33
#include <Shlwapi.h>
44

5-
65
#include "File.h"
76

7+
#include "PCD.h"
8+
#define ENDIAN_BIG PS3
9+
810
unsigned int getFileMagic(const char* filePath)
911
{
1012
std::ifstream ifs(filePath, std::ios::binary);
@@ -58,7 +60,7 @@ short ReadShort(std::ifstream& ifs)
5860
short val;
5961
ifs.read((char*)&val, sizeof(short));
6062
#if ENDIAN_BIG
61-
ReverseShort(val);
63+
val = ReverseShort(val);
6264
#endif
6365
return val;
6466
}
@@ -68,7 +70,7 @@ unsigned short ReadUShort(std::ifstream& ifs)
6870
unsigned short val;
6971
ifs.read((char*)&val, sizeof(unsigned short));
7072
#if ENDIAN_BIG
71-
ReverseUShort(val);
73+
val = ReverseUShort(val);
7274
#endif
7375
return val;
7476
}
@@ -78,7 +80,7 @@ int ReadInt(std::ifstream& ifs)
7880
int val;
7981
ifs.read((char*)&val, sizeof(int));
8082
#if ENDIAN_BIG
81-
ReverseInt(val);
83+
val = ReverseInt(val);
8284
#endif
8385
return val;
8486
}
@@ -88,7 +90,7 @@ unsigned int ReadUInt(std::ifstream& ifs)
8890
unsigned int val;
8991
ifs.read((char*)&val, sizeof(unsigned int));
9092
#if ENDIAN_BIG
91-
ReverseUInt(val);
93+
val = ReverseUInt(val);
9294
#endif
9395
return val;
9496
}

PCD2DDS/Main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ int main(int argc, char* argv[])
1818
{
1919
unsigned int fileMagic = getFileMagic(argv[1]);
2020

21+
#if PS3
22+
fileMagic = ReverseUInt(fileMagic);
23+
#endif
2124
switch (fileMagic)
2225
{
2326
case SECTION_MAGIC:
@@ -26,6 +29,9 @@ int main(int argc, char* argv[])
2629
case DDS_MAGIC:
2730
ConvertDDSToPCD(argv[1]);
2831
break;
32+
default:
33+
std::cout << "Failed to detect file type!" << std::endl;
34+
break;
2935
}
3036
}
3137
else

PCD2DDS/PCD.cpp

Lines changed: 133 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
void ConvertPCDToDDS(const char* filePath)
88
{
9+
#ifndef PS3
910
std::ifstream ifs(filePath, std::ios::binary);
1011

1112
//If there was a failure to open the file we must exit
@@ -45,18 +46,18 @@ void ConvertPCDToDDS(const char* filePath)
4546
ifs.seekg(24, SEEK_SET);
4647

4748
//Load texture data header
48-
char* fileBuffer = new char[sizeof(PCTextureDataHeader)];
49-
ifs.read(fileBuffer, sizeof(PCTextureDataHeader));
50-
51-
PCTextureDataHeader* pcdHeader = nullptr;
52-
pcdHeader = (PCTextureDataHeader*)fileBuffer;
49+
char* fileBuffer = new char[sizeof(cdc::PC::Texture::Header)];
50+
ifs.read(fileBuffer, sizeof(cdc::PC::Texture::Header));
51+
52+
cdc::PC::Texture::Header* pcdHeader = reinterpret_cast<cdc::PC::Texture::Header*>(fileBuffer);
53+
5354
if (pcdHeader->m_magic != PCD_MAGIC)
5455
{
5556
std::cout << "Error PCD magic mis-match!" << std::endl;
5657
ifs.close();
5758
return;
5859
}
59-
60+
6061
char* textureData = new char[pcdHeader->m_textureDataSize];
6162
ifs.read(textureData, pcdHeader->m_textureDataSize);
6263
ifs.close();
@@ -104,4 +105,129 @@ void ConvertPCDToDDS(const char* filePath)
104105
ofs.close();
105106
delete[] fileBuffer;
106107
delete[] textureData;
107-
}
108+
#else
109+
std::ifstream ifs(filePath, std::ios::binary);
110+
111+
//If there was a failure to open the file we must exit
112+
if (!ifs.good())
113+
{
114+
std::cout << "Error: failed to open the file!" << std::endl;
115+
ifs.close();
116+
return;
117+
}
118+
119+
//Check to see if file has valid section header
120+
unsigned int sectionMagic = ReadUInt(ifs);
121+
122+
if (sectionMagic != SECTION_MAGIC)
123+
{
124+
std::cout << "Error: section magic mis-match!" << std::endl;
125+
ifs.close();
126+
return;
127+
}
128+
129+
unsigned int sectionSize = ReadUInt(ifs);
130+
if (sectionSize <= 0)
131+
{
132+
std::cout << "Error: section corrupt, size is <= 0!" << std::endl;
133+
ifs.close();
134+
return;
135+
}
136+
137+
unsigned char sectionType = ReadUByte(ifs);
138+
if (sectionType != TEXTURE_SECTION_TYPE)
139+
{
140+
std::cout << "Error: invalid section type!" << std::endl;
141+
ifs.close();
142+
return;
143+
}
144+
145+
//Skip past section header
146+
ifs.seekg(24, SEEK_SET);
147+
148+
//Load texture data header
149+
char* fileBuffer = new char[sizeof(cdc::ps3::Texture::Header)];
150+
ifs.read(fileBuffer, sizeof(cdc::ps3::Texture::Header));
151+
152+
cdc::ps3::Texture::Header* ps3tHeader = reinterpret_cast<cdc::ps3::Texture::Header*>(fileBuffer);
153+
154+
//Endian swap
155+
*&ps3tHeader->m_magic = ReverseUInt(*&ps3tHeader->m_magic);
156+
*&ps3tHeader->m_textureDataSize = ReverseUInt(*&ps3tHeader->m_textureDataSize);
157+
*&ps3tHeader->m_width = ReverseUShort(*&ps3tHeader->m_width);
158+
*&ps3tHeader->m_height = ReverseUShort(*&ps3tHeader->m_height);
159+
160+
//Swap
161+
if (ps3tHeader->m_magic != PS3T_MAGIC)
162+
{
163+
std::cout << "Error PS3T magic mis-match!" << std::endl;
164+
ifs.close();
165+
return;
166+
}
167+
168+
char* textureData = new char[ps3tHeader->m_textureDataSize];
169+
ifs.read(textureData, ps3tHeader->m_textureDataSize);
170+
ifs.close();
171+
172+
char nameBuff[128];
173+
memset(nameBuff, 0, 128);
174+
sprintf_s(nameBuff, "%s%s", filePath, ".dds");
175+
176+
std::ofstream ofs(nameBuff, std::ios::binary);
177+
178+
unsigned int ddsSize = 0x7C;
179+
unsigned int ddsFlags = 0x000A1007;
180+
unsigned int ddsPitchOrLinearSize = 0x00010000;
181+
unsigned int ddsDummy = 0;
182+
unsigned int ddsHeight = ps3tHeader->m_height;
183+
unsigned int ddsWidth = ps3tHeader->m_width;
184+
unsigned int ddsDepth = 0;//;ps3tHeader->m_depth;
185+
unsigned int ddsMipCount = 0;// ps3tHeader->m_numMipMaps;
186+
unsigned int ddsFormat = cdc::ps3::Texture::getFormat(ps3tHeader->m_format);
187+
unsigned int ddsUnk00 = 0x00401008;
188+
189+
WriteUInt(ofs, DDS_MAGIC);
190+
WriteUInt(ofs, ddsSize);
191+
WriteUInt(ofs, ddsFlags);
192+
WriteUInt(ofs, ddsHeight);
193+
194+
WriteUInt(ofs, ddsWidth);
195+
WriteUInt(ofs, ddsPitchOrLinearSize);
196+
WriteUInt(ofs, ddsDummy);
197+
WriteUInt(ofs, ddsDepth);
198+
199+
WriteUInt(ofs, ddsMipCount);
200+
201+
//Reserved
202+
ofs.seekp(12 * sizeof(unsigned int), SEEK_CUR);
203+
204+
WriteUInt(ofs, ddsFormat);
205+
ofs.seekp(0x14, SEEK_CUR);
206+
WriteUInt(ofs, ddsUnk00);
207+
ofs.seekp(0x10, SEEK_CUR);
208+
209+
ofs.write(textureData, ps3tHeader->m_textureDataSize);
210+
211+
ofs.flush();
212+
ofs.close();
213+
delete[] fileBuffer;
214+
delete[] textureData;
215+
#endif
216+
}
217+
218+
unsigned int cdc::ps3::Texture::getFormat(unsigned int format)
219+
{
220+
switch (format)
221+
{
222+
case cdc::ps3::Texture::kFormat::DXT1:
223+
return 0x31545844;
224+
break;
225+
case cdc::ps3::Texture::kFormat::DXT5:
226+
return 0x35545844;
227+
default:
228+
assert(false);
229+
break;
230+
}
231+
232+
return -1;
233+
}

PCD2DDS/PCD.h

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,93 @@
66
#define DDS_MAGIC (0x20534444)
77
#define TEXTURE_SECTION_TYPE (5)
88

9-
enum PCDFormat
10-
{
11-
DXT1 = 0x31545844,
12-
DXT3 = 0x33545844,
13-
DTX5 = 0x35545844
14-
};
9+
#define PS3 1
10+
11+
#define PS3T_MAGIC (0x50533354)
12+
13+
#include <cassert>
1514

16-
enum PCDFlags
15+
namespace cdc
1716
{
18-
FILTER_POINT = 0x0,
19-
FILTER_BILINEAR = 0x1,
20-
FILTER_TRILINEAR = 0x2,
21-
FILTER_ANISOTROPIC_1X = 0x3,
22-
FILTER_ANISOTROPIC_2X = 0x4,
23-
FILTER_ANISOTROPIC_4X = 0x6,
24-
FILTER_ANISOTROPIC_8X = 0xa,
25-
FILTER_ANISOTROPIC_16X = 0x12,
26-
FILTER_BEST = 0x100,
27-
FILTER_DEFAULT = 0x12,
28-
FILTER_INVALID = 0x200,
29-
};
17+
namespace PC
18+
{
19+
namespace Texture
20+
{
21+
enum kFormat
22+
{
23+
DXT1 = 0x31545844,
24+
DXT3 = 0x33545844,
25+
DTX5 = 0x35545844
26+
};
27+
28+
enum kFlags//Taken from TR7.pdb
29+
{
30+
FILTER_POINT = 0x0,
31+
FILTER_BILINEAR = 0x1,
32+
FILTER_TRILINEAR = 0x2,
33+
FILTER_ANISOTROPIC_1X = 0x3,
34+
FILTER_ANISOTROPIC_2X = 0x4,
35+
FILTER_ANISOTROPIC_4X = 0x6,
36+
FILTER_ANISOTROPIC_8X = 0xa,
37+
FILTER_ANISOTROPIC_16X = 0x12,
38+
FILTER_BEST = 0x100,
39+
FILTER_DEFAULT = 0x12,
40+
FILTER_INVALID = 0x200,
41+
};
3042

3143
#pragma pack(push, 1)
32-
struct PCTextureDataHeader
33-
{
34-
unsigned int m_magic;
35-
enum PCDFMT m_format;
36-
unsigned int m_textureDataSize;
37-
unsigned int m_paletteDataSize;
38-
unsigned short m_width;
39-
unsigned short m_height;
40-
unsigned char m_depth;
41-
unsigned char m_numMipMaps;
42-
unsigned short m_flags;
43-
};
44+
struct Header
45+
{
46+
unsigned int m_magic;
47+
enum PCDFMT m_format;
48+
unsigned int m_textureDataSize;
49+
unsigned int m_paletteDataSize;
50+
unsigned short m_width;
51+
unsigned short m_height;
52+
unsigned char m_depth;
53+
unsigned char m_numMipMaps;
54+
unsigned short m_flags;
55+
};
56+
#pragma pack(pop)
57+
}
58+
}
59+
60+
namespace ps3
61+
{
62+
namespace Texture
63+
{
64+
enum kFormat
65+
{
66+
DXT1 = 0x86,
67+
//DXT3 = 0x33545844,
68+
DXT5 = 0x88
69+
};
4470

71+
#pragma pack(push, 1)
72+
struct Header
73+
{
74+
unsigned int m_magic;
75+
unsigned int m_textureDataSize;
76+
unsigned short m_unk00;
77+
unsigned short m_unk01;
78+
unsigned char m_format;
79+
unsigned char m_unk02;
80+
unsigned short m_unk03;
81+
82+
unsigned int m_unk04;
83+
unsigned short m_width;
84+
unsigned short m_height;
85+
unsigned int m_unk05;
86+
unsigned int m_unk06;
87+
unsigned int m_unk07;
88+
};
4589
#pragma pack(pop)
4690

91+
unsigned int getFormat(unsigned int format);
92+
}
93+
}
94+
}
95+
4796
void ConvertPCDToDDS(const char* filePath);
4897

4998
#endif

0 commit comments

Comments
 (0)