-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpakman.cpp
217 lines (168 loc) · 7.27 KB
/
pakman.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
#include <iostream>
#include <filesystem>
#include <string_view>
#include <algorithm>
#include <fstream>
#include <vector>
#include <string>
using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;
#pragma pack(push, 1)
struct pak_header_t {
char magic[4];
uint32_t initial_file_header_offset;
uint32_t file_count;
};
struct pak_file_header_t {
char filename[52];
uint32_t data_offset;
uint32_t data_size;
uint32_t data_size_2;
};
#pragma pack(pop)
void print_usage(char** argv) {
std::cout << "pakman: Pack and unpack .PAK and .BIN files from Garfield (2004)" << std::endl;
std::cout << "pakman: Usage: " << argv[0] << " [pack/unpack] [file type (pak/bin)] [input/output file]";
}
int main(int argc, char** argv) {
if (argc != 4) {
print_usage(argv);
return 1;
}
std::string_view cmd = argv[1];
std::string_view type = argv[2];
std::string file = argv[3];
if (type != "pak" && type != "bin") {
print_usage(argv);
return 1;
}
if (cmd == "pack") {
if (type == "pak") {
std::vector<pak_file_header_t> headers;
std::string pak_name = file + ".pak";
std::ofstream pak(pak_name, std::ios::binary);
pak_header_t pak_header{ 0 };
pak.write((char*)&pak_header, sizeof(pak_header_t));
std::cout << "pakman: Generating file table and writing files..." << std::endl;
int offset = sizeof(pak_header_t);
for (const auto& dir_entry : recursive_directory_iterator(file)) {
if (dir_entry.is_directory())
continue;
std::string rel_path = dir_entry.path().lexically_relative(file).string();
std::replace(rel_path.begin(), rel_path.end(), '\\', '/');
uint32_t size = std::filesystem::file_size(dir_entry);
pak_file_header_t& header = headers.emplace_back();
memset(header.filename, 0, sizeof(header.filename));
memcpy(header.filename, rel_path.c_str(), std::min<uint32_t>(rel_path.length(), 52));
header.data_offset = offset;
header.data_size = size;
header.data_size_2 = size;
std::ifstream file(dir_entry.path(), std::ios::binary);
pak << file.rdbuf();
file.close();
offset += size + 16;
int pad = offset % 8;
offset -= pad;
for (int i = 0; i < 16 - pad; i++)
pak << '\0';
}
std::cout << "pakman: Writing file table..." << std::endl;
pak.write((char*)headers.data(), headers.size() * sizeof(pak_file_header_t));
memcpy(pak_header.magic, "PACK", 4);
pak_header.file_count = headers.size();
pak_header.initial_file_header_offset = offset;
std::cout << "pakman: Writing pak header..." << std::endl;
pak.seekp(0);
pak.write((char*)&pak_header, sizeof(pak_header_t));
pak.close();
std::cout << "pakman: Done! Saved to " << pak_name << "." << std::endl;
}
else if (type == "bin") {
std::string bin_name = file + ".bin";
std::ofstream bin(bin_name, std::ios::binary);
std::cout << "pakman: Writing files..." << std::endl;
for (const auto& dir_entry : recursive_directory_iterator(file)) {
if (dir_entry.is_directory())
continue;
std::string rel_path = dir_entry.path().lexically_relative(file).string();
std::replace(rel_path.begin(), rel_path.end(), '\\', '/');
uint32_t size = std::filesystem::file_size(dir_entry);
std::ifstream file(dir_entry.path(), std::ios::binary);
bin << file.rdbuf();
file.close();
}
bin.close();
std::cout << "pakman: Done! Saved to " << bin_name << "." << std::endl;
}
}
else if (cmd == "unpack") {
std::cout << "pakman: Unpacking " << file << "..." << std::endl;
if (type == "pak") {
std::ifstream pak(file.data(), std::ios::binary | std::ios::ate);
size_t size = pak.tellg();
pak.seekg(0);
if (!pak.is_open()) {
std::cout << "pakman: Failed to open " << file << "." << std::endl;
return 1;
}
pak_header_t pak_header;
pak.read((char*)&pak_header, sizeof(pak_header_t));
for (uint32_t i = 0; i < pak_header.file_count; i++) {
pak_file_header_t file_header;
pak.seekg(pak_header.initial_file_header_offset + (i * sizeof(pak_file_header_t)));
pak.read((char*)&file_header, sizeof(pak_file_header_t));
std::cout << "pakman: Wrote " << file_header.filename << " (" << i + 1 << "/" << pak_header.file_count << ")" << std::endl;
std::filesystem::path path = file.substr(0, file.find_last_of("."));
path /= file_header.filename;
try {
std::filesystem::path dir = path;
std::filesystem::create_directories(dir.remove_filename());
}
catch (...) {}
std::ofstream file(path, std::ios::binary);
std::vector<char> data(file_header.data_size);
pak.seekg(file_header.data_offset);
pak.read(data.data(), file_header.data_size);
file.write(data.data(), file_header.data_size);
}
pak.close();
std::cout << "pakman: Done!" << std::endl;
}
else if (type == "bin") {
std::ifstream bin(file, std::ios::binary);
if (!bin.is_open()) {
std::cout << "pakman: Failed to open " << file << "." << std::endl;
return 1;
}
// This is slow, but the files will be relatively small anyway.
std::string str((std::istreambuf_iterator<char>(bin)), std::istreambuf_iterator<char>());
size_t pos;
while ((pos = str.find("VAGp")) != std::string::npos) {
size_t end_pos = str.find("VAGp", 4);
if (end_pos == std::string::npos)
end_pos = str.size();
size_t size = end_pos - pos;
std::string data = str.substr(pos, end_pos);
const char* filename = data.data() + 0x20;
std::filesystem::path path = file.substr(0, file.find_last_of("."));
path /= filename;
try {
std::filesystem::path dir = path;
std::filesystem::create_directories(dir.remove_filename());
}
catch (...) {}
std::ofstream out(path, std::ios::binary);
out.write(data.data(), size);
out.close();
std::cout << "pakman: Wrote " << filename << "..." << std::endl;
str = str.substr(end_pos);
}
bin.close();
std::cout << "pakman: Done!" << std::endl;
}
}
else {
print_usage(argv);
return 1;
}
return 0;
}