-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCD_CPKTool.cpp
More file actions
376 lines (331 loc) · 10.9 KB
/
CD_CPKTool.cpp
File metadata and controls
376 lines (331 loc) · 10.9 KB
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
#include <iostream>
#include <windows.h>
#include <set>
#include <fstream>
#include <direct.h>
#include "filesystem"
#include "vector"
#define red "\033[31m"
#define green "\033[32m"
#define yellow "\033[33m"
#define bold "\033[1m"
#define underline "\033[4m"
#define reset "\033[0m"
#define ok green << "OK!" << reset
using namespace std;
struct stat sb;
set<string> validArgs = {"list","l","ls","unpack","extract","ex","e","x","u","up","pack","archive","ar","a","p","help","h","version","v"};
int total_files;
int fileoffset;
int cpkfilebytessize = 260;
char* cpkfile = new char[cpkfilebytessize];
char* outfilecontent = new char[1073741824];
int cpkfilesize;
bool EnableVirtualTerminalProcessing() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) {
return false;
}
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) {
return false;
}
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode)) {
return false;
}
return true;
}
void help()
{
cout << "| ================================= |\n";
cout << "| |\n";
cout << "| Crashday Package Tool |\n";
cout << "| by St1ngLeR |\n";
cout << "| |\n";
cout << "| ================================= |\n";
cout << "\n\nA tool for CPK (Crashday Package) files, which were originally used to install mods for \"classic\" Crashday from 2006.\n";
cout << bold << "The tool is not made for the Redline Edition!\n\n" << reset;
cout << bold << underline << "Usage:" << reset << bold << " CD_CPKTool.exe" << reset << " <COMMAND>\n\n";
cout << bold << underline << "Commands:" << reset;
cout << "\n list\t\tList all files in the archive [aliases: l, ls]";
cout << "\n unpack\tUnpack / extract files from the archive [aliases: extract, ex, e, x, u, up]";
cout << "\n pack\t\tPack / archive files to the archive [aliases: archive, ar, a, p]";
cout << "\n help\t\tPrint help information [aliases: h]";
cout << "\n version\tPrint version information [aliases: v]\n";
}
void version()
{
cout << "CPKTool 1.0.1" << endl;
}
void errmsg_args(string subcommand, string arguments[], int length)
{
cout << red << "ERROR: " << reset << "The following required arguments were not provided:\n";
for (int i = 0; i <= length; i++)
{
cout << green << " " << arguments[i] << "\n";
}
cout << reset << bold << underline << "\nUsage:" << reset << bold << " CD_CPKTool.exe " << reset << subcommand;
for (int i = 0; i <= length; i++)
{
cout << " " << arguments[i];
}
cout << "\n\nFor more information try '" << bold << "help" << reset << "' or '" << bold << "h" << reset << "'.\n";
}
void errmsg_path(const char* path)
{
cout << red << "ERROR: " << reset << "The specified file \'" << yellow << path << reset << "' was not found!\n";
cout << reset "Make sure the file actually exists and provide a valid path to the file.\n";
}
void errmsg_dir(const char* dir)
{
cout << red << "ERROR: " << reset << "The specified directory \'" << yellow << dir << reset << "' was not found!\n";
cout << reset "Make sure the directory actually exists and provide a valid path to the directory.\n";
}
void list(const char* target_file)
{
if (stat(target_file, &sb) == 0 && !(sb.st_mode & S_IFDIR))
{
ifstream file(target_file, ios::binary);
file.seekg(0, ios::beg);
file.read(reinterpret_cast<char*>(&total_files), 4);
cout << "Total files: " << total_files << endl;
cout << setw(10) << left << "\nNum" << setw(40) << " File name" << setw(40) << " File size (in bytes)" << endl;
for (int i = 0; i < total_files; i++)
{
file.seekg(4+(cpkfilebytessize * i) + (12 * i), ios::beg);
file.read(cpkfile, cpkfilebytessize);
file.seekg(4 + (cpkfilebytessize * (i+1)) + (12*i)+4, ios::beg);
file.read(reinterpret_cast<char*>(&cpkfilesize), 4);
cout << setw(10) << left << i+1 << setw(40) << cpkfile << setw(40) << cpkfilesize << endl;
}
}
else
{
errmsg_path(target_file);
}
}
void unpack(const char* target_file, const char* output_dir = NULL, const char* trkdata_dir = NULL)
{
string output_dir2;
if (output_dir == NULL)
{
output_dir2 = (string(target_file).substr(0, string(target_file).find_last_of(".")));
}
else
{
output_dir2 = output_dir;
}
string fullpath;
if (stat(target_file, &sb) == 0 && !(sb.st_mode & S_IFDIR))
{
filesystem::create_directories(output_dir2); //_mkdir(output_dir2.c_str());
ifstream file(target_file, ios::binary);
file.seekg(0, ios::beg);
file.read(reinterpret_cast<char*>(&total_files), 4);
for (int i = 0; i < total_files; i++)
{
file.seekg(4 + (cpkfilebytessize * i) + (12 * i), ios::beg);
file.read(cpkfile, cpkfilebytessize);
filesystem::path filePath(cpkfile);
filesystem::path dirPath = filePath.parent_path();
filesystem::path filePart = filePath.filename();
filesystem::create_directories(output_dir2 + string("\\") + dirPath.string());
cout << "Creating file '";
if (trkdata_dir != NULL)
{
if (dirPath.empty())
{
filesystem::create_directories(output_dir2 + string("\\trkdata\\cars\\" + string(trkdata_dir)));
fullpath = output_dir2 + string("\\trkdata\\cars\\") + trkdata_dir + "\\" + string(cpkfile);
}
else
{
fullpath = output_dir2 + string("\\") + string(cpkfile);
}
}
else
{
fullpath = output_dir2 + string("\\") + string(cpkfile);
}
cout << fullpath;
ofstream outfile(fullpath, ios::binary);
cout << "'." << endl;
cout << "Determining the file size - ";
file.seekg(4 + (cpkfilebytessize * (i + 1)) + (12 * i)+4, ios::beg);
file.read(reinterpret_cast<char*>(&cpkfilesize), 4);
cout << cpkfilesize << " bytes. " << endl;
cout << "Determining the file offset in CPK - ";
file.seekg(8 + (cpkfilebytessize * (i + 1)) + (12 * i)+4, ios::beg);
file.read(reinterpret_cast<char*>(&fileoffset), 4);
cout << fileoffset << " bytes." << endl;
cout << "Setting the pointer to the found offset... ";
file.seekg(fileoffset, ios::beg);
cout << ok << endl;
cout << "Reading the file contents... ";
file.read(outfilecontent, cpkfilesize);
cout << ok << endl;
cout << "Writing the file contents to the output file... ";
outfile.write(outfilecontent, cpkfilesize);
cout << ok << endl;
cout << endl;
}
cout << bold << green << "The file has been successfully unpacked!" << reset;
}
else
{
errmsg_path(target_file);
}
}
void pack(const char* target_dir, const char* output_file = NULL)
{
string output_file2;
if (output_file == NULL)
{
output_file2 = target_dir + (string)".cpk";
}
else
{
output_file2 = output_file;
}
if (stat(target_dir, &sb) == 0)
{
cout << "Counting the total number of files... ";
vector<string> filePaths;
int i = 0;
for (const auto& entry : filesystem::recursive_directory_iterator(target_dir))
{
if (filesystem::is_regular_file(entry))
{
i++;
string relativePath = filesystem::relative(entry.path(), target_dir).string();
filePaths.push_back(relativePath);
}
}
cout << ok << endl;
cout << "Creating the CPK file... ";
filesystem::path filePathOutput(output_file2);
filesystem::path dirPath = filePathOutput.parent_path();
filesystem::path filePart = filePathOutput.filename();
if (!dirPath.empty())
{
filesystem::create_directories(dirPath);
}
cout << ok << endl;
ofstream cpkfile(output_file2, ios::binary);
cpkfile.write(reinterpret_cast<char*>(&i), 4);
cout << "Entering a list of files... ";
for (const auto& filePath : filePaths)
{
cpkfile.write(filePath.c_str(), filePath.length());
cpkfile.seekp(cpkfilebytessize + 12 - filePath.length(), ios::cur);
}
cout << ok << endl;
cout << "Entering a files informations... ";
cpkfile.seekp(4 + cpkfilebytessize, ios::beg);
int size = 0;
int fileoffset = 4 + ((cpkfilebytessize + 12) * i);
for (const auto& filePath : filePaths)
{
int dir;
if (filePath.find_first_of("\\") != string::npos)
{
dir = 1;
}
else
{
dir = 0;
}
cpkfile.write(reinterpret_cast<char*>(&dir), 4);
fileoffset += size;
size = filesystem::file_size(target_dir + string("\\") + filePath);
cpkfile.write(reinterpret_cast<char*>(&size), 4);
cpkfile.write(reinterpret_cast<char*>(&fileoffset), 4);
cpkfile.seekp(cpkfilebytessize, ios::cur);
}
cout << ok << endl;
cpkfile.seekp(4 + ((cpkfilebytessize + 12) * i), ios::beg);
for (const auto& filePath : filePaths)
{
cout << "Adding file '" << filePath << "'... ";
size = filesystem::file_size(target_dir + string("\\") + filePath);
ifstream file(target_dir + string("\\") + filePath, ios::binary);
char buffer[4096];
while (file.good())
{
file.read(buffer, sizeof(buffer));
cpkfile.write(buffer, file.gcount());
}
cout << ok << endl;
}
cout << endl;
cout << bold << green << "The file has been successfully packed!" << reset;
}
else
{
errmsg_dir(target_dir);
}
}
int main(int argc, char** argv)
{
if(EnableVirtualTerminalProcessing())
{
for (int i = 1; i < argc; i++) {
if (argv[i] == "help"sv || argv[i] == "h"sv)
{
help();
}
else if (argv[i] == "version"sv || argv[i] == "v"sv)
{
version();
}
else if (argv[i] == "list"sv || argv[i] == "l"sv || argv[i] == "ls"sv)
{
if (i + 1 >= argc)
{
string arglist[] = { "<CPK_NAME>" };
errmsg_args("list", arglist, 0);
}
else
{
list(argv[i + 1]);
}
}
else if (argv[i] == "unpack"sv || argv[i] == "extract"sv || argv[i] == "ex"sv || argv[i] == "e"sv || argv[i] == "x"sv || argv[i] == "u"sv || argv[i] == "up"sv)
{
if (i + 1 >= argc)
{
string arglist[] = { "<CPK_NAME>", "[OUTPUT_DIR]", "[TRKDATA_DIR]" };
errmsg_args("unpack", arglist, 2);
}
if ((argv[i + 3] == NULL) || (argv[i + 2] == NULL))
{
unpack(argv[i + 1], argv[i + 2]);
}
else
{
unpack(argv[i + 1], argv[i + 2], argv[i + 3]);
}
}
else if (argv[i] == "pack"sv || argv[i] == "archive"sv || argv[i] == "ar"sv || argv[i] == "a"sv || argv[i] == "p"sv)
{
if (i + 1 >= argc)
{
string arglist[] = { "<INPUT_DIR>", "[CPK_NAME]" };
errmsg_args("pack", arglist, 1);
}
else
{
pack(argv[i + 1], argv[i + 2]);
}
}
else if (validArgs.find(argv[1]) == validArgs.end())
{
cout << red << "ERROR: " << reset << "The subcommand '" << yellow << argv[i] << reset << "' wasn't recognized!\n\n";
cout << bold << underline << "Usage:" << reset << bold << " CD_CPKTool.exe" << reset << " <COMMAND>\n\n";
cout << "For more information try '" << bold << "help" << reset << "' or '" << bold << "h" << reset << "'.\n";
}
}
}
return 0;
}