This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Expand.cpp
459 lines (417 loc) · 12.5 KB
/
Expand.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <cinttypes>
#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include <map>
#include <thread>
#include <future>
#include <regex>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#ifdef __unix__
#include <unistd.h>
#ifdef _POSIX_VERSION
#include <pthread.h>
#endif
#endif
#include <clara.hpp>
#include <mio/mmap.hpp>
#include <Maple2/Maple2.hpp>
#include <Util/File.hpp>
bool DumpPackFile(const fs::path& HeaderPath, fs::path DestPath, std::size_t TaskIndex);
void HexDump(const char* Description, const void* Data, std::size_t Size);
int main(int argc, char* argv[])
{
std::puts(
"MapleStory2 Filesystem expander:\n"
"\t\"Flattens\" a filesystem, expanding all m2h/m2d files it encounters\n"
"\tinto a folder of the same name\n"
"Build Date: " __TIMESTAMP__ "\n"
"\t- wunkolo <[email protected]>"
);
std::string ShadowOption = "sym";
// Workaround for Clara parsing an fs::path string with a space in it:
// https://github.com/catchorg/Clara/issues/55
std::string SourcePath;
std::string DestPath;
bool ShowHelp = false;
auto CommandParser =
clara::Help( ShowHelp ) |
clara::Arg( SourcePath, "Source path" )
("Source path to expand").required() |
clara::Arg( DestPath, "Destination path" )
("Destination path that will contain the dumped source folder").required() |
clara::Opt( [&]
(std::string String)
{
if( std::regex_match(String,std::regex("^(none|sym|hard|copy)$")) )
{
ShadowOption = String;
return clara::ParserResult::ok(clara::ParseResultType::Matched);
}
else
{
return clara::ParserResult::runtimeError("Invalid shadow option");
}
}, "none|sym|hard|copy")
["-s"]["--shadow"]
(
"Determines how the original files will be recreated in the dump\n\n"
"none - No shadowing\n"
"sym - Creates symbolic links in the dump folder(default)\n"
"hard - Creates hard links in the dump folder\n"
"copy - Copies the original file into the dump folder\n"
);
auto Result = CommandParser.parse( clara::Args( argc, argv ) );
if( !Result )
{
std::printf(
"Error parsing command line: %s\n",
Result.errorMessage().c_str()
);
CommandParser.writeToStream(std::cout);
return EXIT_FAILURE;
}
else if( ShowHelp || argc < 3 )
{
CommandParser.writeToStream(std::cout);
return EXIT_SUCCESS;
}
fs::create_directory(DestPath);
if( !fs::exists(SourcePath) || !fs::exists(DestPath) )
{
std::puts("Invalid source/dest paths");
return EXIT_FAILURE;
}
std::vector< std::future<bool> > Tasks;
std::size_t TaskIndex = 1;
for( const auto& CurEntry : fs::recursive_directory_iterator(SourcePath) )
{
if( fs::is_regular_file(CurEntry) )
{
const fs::path CurEntryRelative = CurEntry.path().string().substr(
fs::path(SourcePath).parent_path().string().length()
);
const fs::path& CurSource = CurEntry.path();
const fs::path CurDest = DestPath / CurEntryRelative;
// Create shadow original files
try
{
// TODO: Enum this, or something
// Allow overwrite
fs::remove(CurDest);
switch( ShadowOption[0] )
{
case 's': // sym
{
fs::create_directories(CurDest.parent_path());
fs::create_symlink(fs::absolute(CurSource), CurDest);
break;
}
case 'h': // hard
{
fs::create_directories(CurDest.parent_path());
fs::create_hard_link(fs::absolute(CurSource), CurDest);
break;
}
case 'c': // copy
{
fs::create_directories(CurDest.parent_path());
fs::copy_file(
fs::absolute(CurSource), CurDest,
fs::copy_options::overwrite_existing
);
break;
}
default: // none
{
break;
}
}
}
catch( fs::filesystem_error& Exception)
{
std::printf(
"Failed to create shadow file (%s):\n%s\n",
CurSource.c_str(), Exception.what()
);
continue;
}
// Process Header files
if( CurSource.extension() == ".m2h" )
{
const fs::path CurExpansion = CurDest.parent_path() / CurDest.stem();
fs::create_directories(CurDest.parent_path());
// Process .m2h into new folder of the same name
fs::create_directory(CurExpansion);
const auto ThreadProc = []
(
const fs::path& Source, const fs::path& Expansion,
std::size_t TaskIndex
) -> bool
{
#ifdef _POSIX_VERSION
char ThreadName[16] = {0};
const auto PackName = Source.stem().string();
std::sprintf(
ThreadName,
PackName.length() > 7 ? "Expand: %.4s...":"Expand: %.7s",
PackName.c_str()
);
pthread_setname_np(pthread_self(), ThreadName);
#endif
return DumpPackFile(Source,Expansion, TaskIndex);
};
Tasks.emplace_back(
std::async(ThreadProc, CurSource, CurExpansion, TaskIndex++)
);
}
}
}
for( auto& CurTasks : Tasks ) CurTasks.get();
std::printf("\033[%zuB\n", TaskIndex + 1);
return EXIT_SUCCESS;
}
template< typename PackTraits >
bool DumpPackStream(const fs::path& HeaderPath, fs::path DestPath,std::size_t TaskIndex)
{
mio::ummap_source HeaderFile(HeaderPath.c_str(), 0, mio::map_entire_file);
if( !HeaderFile.is_open() )
{
// Error opening file
std::printf(
"Error opening file for reading: %s\n",
HeaderPath.string().c_str()
);
return false;
}
auto HeaderReadPoint = HeaderFile.cbegin();
const Maple2::Identifier Magic
= *reinterpret_cast<const typename Maple2::Identifier*>(HeaderReadPoint);
HeaderReadPoint += sizeof(Maple2::Identifier);
if( Magic != PackTraits::Magic )
{
// Invalid magic
return false;
}
std::printf(
"\033[%zuB" // Move Down
"\033[2K" // Clear line
"\r" // Return to left
"[ %-20.20s ] | \033[0;33mParsing file list... \033[0m" // Printed string
"\033[%zuA", // Move up
TaskIndex,
HeaderPath.stem().c_str(),
TaskIndex
);
typename PackTraits::StreamType StreamHeader
= *reinterpret_cast<const typename PackTraits::StreamType*>(HeaderReadPoint);
HeaderReadPoint += sizeof(typename PackTraits::StreamType);
////////////////////////////////////////////////////////////////////////////
// FileList
std::string FileList;
FileList.resize(StreamHeader.FileListSize);
Maple2::Util::DecryptStream(
HeaderReadPoint,
StreamHeader.FileListEncodedSize,
PackTraits::IV_LUT[StreamHeader.FileListCompressedSize % std::extent<typename std::remove_reference<decltype(PackTraits::IV_LUT)>::type, 0u>::value],
PackTraits::Key_LUT[StreamHeader.FileListCompressedSize % std::extent<typename std::remove_reference<decltype(PackTraits::Key_LUT)>::type, 0u>::value],
FileList.data(),
StreamHeader.FileListSize,
StreamHeader.FileListSize != StreamHeader.FileListCompressedSize
);
HeaderReadPoint += StreamHeader.FileListEncodedSize;
// Generate list of File list entries
const std::map<std::size_t, fs::path> FileListEntries
= Maple2::Util::ParseFileList(FileList);
////////////////////////////////////////////////////////////////////////////
// File allocation Table
std::vector<typename PackTraits::FileHeaderType> FATable;
FATable.resize(StreamHeader.TotalFiles, typename PackTraits::FileHeaderType{});
Maple2::Util::DecryptStream(
HeaderReadPoint,
StreamHeader.FATEncodedSize,
PackTraits::IV_LUT[StreamHeader.FATCompressedSize % std::extent<typename std::remove_reference<decltype(PackTraits::IV_LUT)>::type, 0u>::value],
PackTraits::Key_LUT[StreamHeader.FATCompressedSize % std::extent<typename std::remove_reference<decltype(PackTraits::Key_LUT)>::type, 0u>::value],
FATable.data(),
StreamHeader.TotalFiles * sizeof(typename PackTraits::FileHeaderType),
StreamHeader.FATSize != StreamHeader.FATCompressedSize
);
HeaderFile.unmap();
////////////////////////////////////////////////////////////////////////////
// Process data file
const fs::path DataPath = fs::path(HeaderPath).replace_extension(".m2d");
mio::ummap_source DataFile(DataPath.c_str(), 0, mio::map_entire_file);
if( !DataFile.is_open() )
{
// Error opening file
std::printf(
"Error opening file for reading: %s\n", DataPath.string().c_str()
);
return false;
}
for( std::size_t i = 0; i < StreamHeader.TotalFiles; ++i )
{
fs::create_directories(
DestPath / FileListEntries.at(i + 1).parent_path()
);
std::printf(
"\033[%zuB" // Move Down
"\033[2K" // Clear line
"\r" // Return to left
"[ %-20.20s ] | \033[1;37m%6.2f%%\033[0m \033[1;34m%-60.60s\033[0m" // Printed string
"\033[%zuA", // Move up
TaskIndex,
HeaderPath.stem().string().c_str(), // PackFile
(static_cast<float>(i + 1) / FATable.size()) * 100.0f, // Percentage
fs::path(FileListEntries.at(i + 1)).string().c_str(), // Current file
TaskIndex
);
std::ofstream DumpFile;
DumpFile.open(
DestPath / FileListEntries.at(i + 1), std::ios::binary
);
if( !DumpFile )
{
std::printf(
"Error opening file \"%s\" for writing\n",
(DestPath / FileListEntries.at(i + 1)).c_str()
);
return false;
}
Maple2::Util::DecryptStreamToStream(
DataFile.cbegin() + FATable[i].Offset,
FATable[i].EncodedSize,
PackTraits::IV_LUT[FATable[i].CompressedSize % std::extent<typename std::remove_reference<decltype(PackTraits::IV_LUT)>::type, 0u>::value],
PackTraits::Key_LUT[FATable[i].CompressedSize % std::extent<typename std::remove_reference<decltype(PackTraits::Key_LUT)>::type, 0u>::value],
DumpFile,
FATable[i].Size != FATable[i].CompressedSize
);
DumpFile.close();
}
DataFile.unmap();
return true;
}
bool DumpPackFile(const fs::path& HeaderPath, fs::path DestPath, std::size_t TaskIndex)
{
std::ifstream FileIn;
FileIn.open(HeaderPath, std::ios::binary);
if( !FileIn.good() )
{
// Error opening file
std::printf(
"Error opening file for reading: %s\n",
HeaderPath.string().c_str()
);
return false;
}
const Maple2::Identifier Magic = Util::Read<Maple2::Identifier>(FileIn);
FileIn.close();
bool Result = false;
try
{
switch( Magic )
{
case Maple2::Identifier::MS2F:
{
Result = DumpPackStream<Maple2::PackTraits::MS2F>(
HeaderPath, DestPath, TaskIndex
);
break;
}
case Maple2::Identifier::NS2F:
{
Result = DumpPackStream<Maple2::PackTraits::NS2F>(
HeaderPath, DestPath, TaskIndex
);
break;
}
case Maple2::Identifier::OS2F:
{
Result = DumpPackStream<Maple2::PackTraits::OS2F>(
HeaderPath, DestPath, TaskIndex
);
break;
}
case Maple2::Identifier::PS2F:
{
Result = DumpPackStream<Maple2::PackTraits::PS2F>(
HeaderPath, DestPath, TaskIndex
);
break;
}
}
}
catch(const std::exception& Exception)
{
std::printf(
"\033[%zuB" // Move Down
"\033[2K" // Clear line
"\r" // Return to left
"[ %-20.20s ] | \033[0;31mERROR: %s\033[0m" // Printed string
"\033[%zuA", // Move up
TaskIndex,
HeaderPath.stem().string().c_str(),
Exception.what(),
TaskIndex
);
return false;
}
std::printf(
"\033[%zuB" // Move Down
"\033[2K" // Clear line
"\r" // Return to left
"[ %-20.20s ] | \033[0;32mDONE \033[0m" // Printed string
"\033[%zuA", // Move up
TaskIndex,
HeaderPath.stem().string().c_str(),
TaskIndex
);
return Result;
}
void HexDump(const char* Description, const void* Data, std::size_t Size)
{
std::size_t i;
std::uint8_t Buffer[17];
const std::uint8_t* CurByte = reinterpret_cast<const std::uint8_t*>(Data);
if( Description != nullptr )
{
std::printf("\e[5m%s\e[0m:\n", Description);
}
for( i = 0; i < Size; i++ )
{
if( (i % 16) == 0 )
{
if( i != 0 )
{
std::printf(" \e[0;35m%s\e[0m\n", Buffer);
}
std::printf(" \e[0;33m%04zx\e[0m ", i);
}
std::printf(
" \e[0;36m%02x\e[0m",
CurByte[i]
);
if( (CurByte[i] < ' ') || (CurByte[i] > 0x7e) )
{
Buffer[i % 16] = '.';
}
else
{
Buffer[i % 16] = CurByte[i];
}
Buffer[(i % 16) + 1] = '\0';
}
while( (i % 16) != 0 )
{
std::printf(" ");
i++;
}
std::printf(" \e[0;35m%s\e[0m\n", Buffer);
}