-
Notifications
You must be signed in to change notification settings - Fork 1
/
KXRUnpacker.cpp
173 lines (136 loc) · 4.27 KB
/
KXRUnpacker.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
#include <fstream>
#include <iostream>
#include "KXRUnpacker.h"
void
KXRUnpacker::ExtractKXRFile( std::string filename )
{
if( std::filesystem::path( filename ).extension() != ".kxr" )
{
std::cout << "Skip file " << filename << std::endl;
return;
}
_kxrFileName = std::filesystem::path( filename ).stem().string();
// Remove the versioning from the file name
size_t versionPos = _kxrFileName.find_last_of( "-" );
if( versionPos != std::string::npos )
{
_kxrFileName.erase( _kxrFileName.begin() + versionPos, _kxrFileName.end() );
}
// Load the file
std::fstream file_stream( filename.data(), std::ios::binary | std::ios::in );
if( !file_stream.is_open() )
{
std::cerr << "[Failed to load file]" << filename << std::endl;
return;
}
// Get the file size
file_stream.seekg( 0, file_stream.end );
size_t fileSize = static_cast< uint32_t >( file_stream.tellg() );
file_stream.seekg( 0, file_stream.beg );
// Allocate space on the buffer and read the contents
std::vector< unsigned char > fileBuffer( fileSize, 0 );
file_stream.read( reinterpret_cast< char* >( fileBuffer.data() ), fileSize );
// Close the file stream
file_stream.close();
_kxrContentBuffer = KBuffer( fileBuffer.data(), 0, fileBuffer.size());
uint32_t file_identity = _kxrContentBuffer.u32();
if( file_identity != KXRF_Identifier )
{
std::cerr << "Invalid KXR File header for input file: " << filename << std::endl;
return;
}
_kxrContentBuffer.u32();
uint32_t _kxrContentTableOffset = _kxrContentBuffer.u32();
uint32_t _kxrContentTableSize = _kxrContentBuffer.u32();
_kxrContentTable = KBuffer( fileBuffer.data(), _kxrContentTableOffset, _kxrContentTableSize);
_kxrContentTable.crypt( _kxrContentTableOffset );
RecursiveKXRExtract();
}
void
KXRUnpacker::RecursiveKXRExtract()
{
std::string entryName = _kxrContentTable.string();
uint8_t unknown_a = _kxrContentTable.u8();
uint32_t unknown_b = _kxrContentTable.u32();
uint32_t entryType = _kxrContentTable.u32();
switch( entryType )
{
case TypeDirectory: UnpackFolder( entryName ); break;
case TypeFileEncrypted: UnpackEncryptedFile( entryName ); break;
case TypeFileCompressed: UnpackCompressedFile( entryName ); break;
}
}
void
KXRUnpacker::UnpackFolder( std::filesystem::path folder_name )
{
_currentDirectory += folder_name;
uint16_t nbFilesInFolder = _kxrContentTable.u16();
for( int i = 0; i < nbFilesInFolder; i++ )
{
RecursiveKXRExtract();
}
_currentDirectory = _currentDirectory.parent_path();
}
void
KXRUnpacker::UnpackEncryptedFile( std::filesystem::path file_name )
{
uint32_t offset = _kxrContentTable.u32();
uint32_t size = _kxrContentTable.u32();
KBuffer fileBuffer( _kxrContentBuffer.data(), offset, size);
fileBuffer.crypt( offset );
std::filesystem::path lookup_name;
if( false == file_name.has_extension() )
{
lookup_name = Package.FindFileName( _kxrFileName, std::atoi( file_name.stem().generic_string().c_str() ) );
}
else
{
lookup_name = _currentDirectory / file_name;
}
SaveToDisk( lookup_name, fileBuffer );
std::cout << "\t" << lookup_name << std::endl;
}
void
KXRUnpacker::UnpackCompressedFile( std::filesystem::path file_name )
{
uint32_t offset = _kxrContentTable.u32();
uint32_t size = _kxrContentTable.u32();
KBuffer fileBuffer( _kxrContentBuffer.data(), offset, size );
fileBuffer.decompress();
std::filesystem::path lookup_name;
if( false == file_name.has_extension() )
{
lookup_name = Package.FindFileName( _kxrFileName, std::atoi( file_name.stem().generic_string().c_str() ) );
}
else
{
lookup_name = _currentDirectory / file_name;
}
SaveToDisk( lookup_name, fileBuffer );
std::cout << "\t" << lookup_name << std::endl;
}
bool
KXRUnpacker::SetOutputDirectory( std::filesystem::path path )
{
if( path.is_absolute() )
{
_outputDirectory = path;
}
else
{
_outputDirectory = std::filesystem::current_path() / path;
}
return std::filesystem::create_directories( _outputDirectory );
}
void
KXRUnpacker::SaveToDisk( std::filesystem::path file_path, KBuffer buffer )
{
std::filesystem::path save_path = _outputDirectory / file_path;
std::filesystem::create_directories( save_path.parent_path() );
std::fstream save_file( save_path, std::ios::out | std::ios::binary );
if( save_file.good() )
{
save_file.write( ( char* )&buffer.data()[0], buffer.size());
save_file.close();
}
}