Skip to content

Commit c296690

Browse files
authored
Merge pull request #8 from Gh0stBlade/Stability
DRM/DDS<->PCD Stability improvements.
2 parents 33f18b6 + 69ac3b5 commit c296690

File tree

5 files changed

+62
-76
lines changed

5 files changed

+62
-76
lines changed

DRM/DRM.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
/*
2-
[DRM] Tomb Raider: Legend/Anniversary/Underworld DRM Unpacker
3-
Copyright (C) Gh0stBlade 2015 - gh0stblade@live[dot]co.uk
4-
5-
This program is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU General Public License
7-
as published by the Free Software Foundation; either version 2
8-
of the License, or (at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18-
*/
19-
201
#include "DRM.h"
212
#include "File.h"
223
#include "FileExtensions.h"

DRM/Main.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
/*
2-
[DRM] Tomb Raider: Legend/Anniversary/Underworld DRM Unpacker
3-
Copyright (C) Gh0stBlade 2015 - gh0stblade@live[dot]co.uk
4-
5-
This program is free software; you can redistribute it and/or
6-
modify it under the terms of the GNU General Public License
7-
as published by the Free Software Foundation; either version 2
8-
of the License, or (at your option) any later version.
9-
10-
This program is distributed in the hope that it will be useful,
11-
but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
GNU General Public License for more details.
14-
15-
You should have received a copy of the GNU General Public License
16-
along with this program; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18-
*/
19-
201
#include <stdio.h>
212
#include <iostream>
223

DRM/Repack.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,39 @@ void RepackSections(const char* sectionListPath, const char* basePath)
3030
std::ifstream ifs(sectionPath.str(), std::ios::binary);
3131
int sectHdr = ReadUInt(ifs);//Unused
3232

33+
//
34+
std::string path(sectionPath.str());
35+
std::string filename;
36+
37+
size_t pos = path.find_last_of("\\");
38+
if (pos != std::string::npos)
39+
{
40+
filename.assign(path.begin() + pos + 1, path.end());
41+
pos = path.find_last_of("_");
42+
filename.assign(path.begin() + pos + 1, path.end());
43+
filename.erase(filename.find_first_of("."), std::string::npos);
44+
}
45+
else
46+
{
47+
filename = path;
48+
}
49+
50+
unsigned int hash;
51+
sscanf(filename.c_str(), "%x", &hash);
52+
3353
WriteUInt(ofs, ReadUInt(ifs));
3454
WriteUByte(ofs, ReadUByte(ifs));
3555
WriteUByte(ofs, ReadUByte(ifs));
3656
WriteUShort(ofs, ReadUShort(ifs));
3757
WriteUInt(ofs, ReadUInt(ifs));
38-
WriteUInt(ofs, ReadUInt(ifs));
58+
unsigned int sectionHash = ReadUInt(ifs);
59+
if (sectionHash != hash)
60+
{
61+
std::cout << "Warning: Detected hash mis-match!" << std::endl;
62+
sectionHash = hash;
63+
}
64+
WriteUInt(ofs, sectionHash);
65+
3966
WriteUInt(ofs, ReadUInt(ifs));
4067
ifs.close();
4168
}

PCD2DDS/DDS.cpp

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "DDS.h"
2+
#include "File.h"
23

34
#include <iostream>
45
#include <fstream>
@@ -10,6 +11,7 @@ void ConvertDDSToPCD(const char* filePath)
1011
//If there was a failure to open the file we must exit
1112
if (!ifs.good())
1213
{
14+
std::cout << "Error: failed to open the file!" << std::endl;
1315
ifs.close();
1416
return;
1517
}
@@ -22,8 +24,17 @@ void ConvertDDSToPCD(const char* filePath)
2224
//Load texture data header
2325
char* fileBuffer = new char[sizeof(DDSHeader)];
2426
ifs.read(fileBuffer, sizeof(DDSHeader));
27+
2528
DDSHeader* ddsHeader = nullptr;
2629
ddsHeader = (DDSHeader*)fileBuffer;
30+
if (ddsHeader->m_magic != DDS_MAGIC)
31+
{
32+
std::cout << "Error: DDS magic mis-match!" << std::endl;
33+
ifs.close();
34+
delete[] fileBuffer;
35+
return;
36+
}
37+
2738
char* textureData = new char[fileSize-0x80];
2839
ifs.seekg(0x80, SEEK_SET);
2940
ifs.read(textureData, fileSize - 0x80);
@@ -35,22 +46,6 @@ void ConvertDDSToPCD(const char* filePath)
3546

3647
std::ofstream ofs(nameBuff, std::ios::binary);
3748

38-
unsigned int pcdMagic = 0x39444350;
39-
unsigned int pcdFormat = ddsHeader->m_format;
40-
unsigned int pcdTextureDataSize = fileSize - 0x80;
41-
unsigned int pcdPaletteDataSize = 0;
42-
unsigned short pcdWidth = ddsHeader->m_width;///@TODO assert max unsigned short
43-
unsigned short pcdHeight = ddsHeader->m_height;///@TODO assert max unsigned short
44-
unsigned char pcdDepth = ddsHeader->m_depth;///@TODO assert max unsigned char
45-
unsigned char pcdMipCount = ddsHeader->m_mipCount;///@TODO assert max unsigned char
46-
unsigned short pcdFlags = 0x3; //??FILTER_ANISOTROPIC_1X = 0x3,
47-
48-
//Write section header
49-
unsigned int sectionMagic = 0x54434553;
50-
unsigned int sectionFileSize = pcdTextureDataSize + 0x18;
51-
unsigned int Type = 0x5;
52-
unsigned int headerSize = 0;
53-
5449
//
5550
std::string path(filePath);
5651
std::string filename;
@@ -71,27 +66,24 @@ void ConvertDDSToPCD(const char* filePath)
7166
unsigned int hash;
7267
sscanf(filename.c_str(), "%x", &hash);
7368

74-
unsigned int lang = 0xFFFFFFFF;
75-
ofs.write((char*)&sectionMagic, sizeof(unsigned int));
76-
ofs.write((char*)&sectionFileSize, sizeof(unsigned int));
77-
ofs.write((char*)&Type, sizeof(unsigned int));
78-
ofs.write((char*)&headerSize, sizeof(unsigned int));
79-
ofs.write((char*)&hash, sizeof(unsigned int));
80-
ofs.write((char*)&lang, sizeof(unsigned int));
81-
82-
ofs.write((char*)&pcdMagic, sizeof(unsigned int));
83-
ofs.write((char*)&pcdFormat, sizeof(unsigned int));
84-
ofs.write((char*)&pcdTextureDataSize, sizeof(unsigned int));
85-
ofs.write((char*)&pcdPaletteDataSize, sizeof(unsigned int));
86-
87-
ofs.write((char*)&pcdWidth, sizeof(unsigned short));
88-
ofs.write((char*)&pcdHeight, sizeof(unsigned short));
89-
90-
ofs.write((char*)&pcdDepth, sizeof(unsigned char));
91-
ofs.write((char*)&pcdMipCount, sizeof(unsigned char));
92-
ofs.write((char*)&pcdFlags, sizeof(unsigned short));
93-
94-
ofs.write(textureData, fileSize - 0x80);
69+
WriteUInt(ofs, SECTION_MAGIC);
70+
WriteUInt(ofs, ((fileSize-0x80) + 0x18));
71+
WriteUInt(ofs, (TEXTURE_SECTION_TYPE));
72+
WriteUInt(ofs, 0);
73+
WriteUInt(ofs, hash);
74+
WriteUInt(ofs, 0xFFFFFFFF);
75+
76+
WriteUInt(ofs, PCD_MAGIC);
77+
WriteUInt(ofs, ddsHeader->m_format);
78+
WriteUInt(ofs, (fileSize - 0x80));
79+
WriteUInt(ofs, 0);
80+
WriteUShort(ofs, ddsHeader->m_width);
81+
WriteUShort(ofs, ddsHeader->m_height);
82+
WriteUByte(ofs, ddsHeader->m_depth);
83+
WriteUByte(ofs, ddsHeader->m_mipCount);
84+
WriteUShort(ofs, 3);
85+
86+
ofs.write(textureData, (fileSize - 0x80));
9587

9688
ofs.flush();
9789
ofs.close();

PCD2DDS/DDS.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef DDS_H
22
#define DDS_H
33

4+
#define SECTION_MAGIC (0x54434553)
5+
#define PCD_MAGIC (0x39444350)
6+
#define DDS_MAGIC (0x20534444)
7+
#define TEXTURE_SECTION_TYPE (5)
8+
49
/*enum DDSFormat
510
{
611
DXT1 = 0x31545844,

0 commit comments

Comments
 (0)