-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathiwd.cpp
More file actions
126 lines (109 loc) · 2.75 KB
/
Copy pathiwd.cpp
File metadata and controls
126 lines (109 loc) · 2.75 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
#include "iwd.h"
#include "../cvar.h"
#include <Windows.h>
#include "io.h"
#include "../shared/miniz/miniz.h"
#include "str.h"
#include "../sys/AppInfo.h"
#define IWD_DIR_SOUND "sound/"
#define IWD_DIR_IMAGE "images/"
int __cdecl IWD_IWDHandler(const char* iwdPath, const char* iwdName)
{
if (!g_useLocalized.ValueBool() && stristr(iwdName, "localized"))
{
return 0;
}
char sub[MAX_PATH];
if (g_extractAll.ValueBool())
{
sprintf_s(sub, "ALL");
}
else if (g_extractImages.ValueBool())
{
char subdst[MAX_PATH];
sprintf_s(subdst, "IMAGE");
if (g_extractSounds.ValueBool())
{
sprintf_s(sub, "%s and %s", subdst, "AUDIO");
}
}
else
{
sprintf_s(sub, "AUDIO");
}
Con_Print("Extracting %s files from: \"%s\"...\n", sub, iwdName);
IWD_IWDExtract(iwdPath, iwdName);
return 0;
}
int __cdecl IWD_IWDExtractFile(mz_zip_archive* iwd, const char* filepath)
{
char outPath[MAX_PATH];
sprintf_s(outPath, "%s/%s", AppInfo_OutDir(), filepath);
FS_SanitizePath(outPath);
// Skip any irrelevant files
if (!fs_overwrite.ValueBool() && FS_FileExists(outPath))
{
Con_Print_v("Skipping file: \"%s\"\n", filepath);
return 1;
}
Con_Print_v("Extracting file: \"%s\"... ", filepath);
#if DRY_RUN
Con_Print_v("(DRY RUN)\n");
#else
// Ensure that the destination path exists
// Otherwise mz_zip_reader_extract_file_to_file will fail
int err = FS_CreatePath(filepath);
if (err != 0)
{
Con_Error_v("DIR ERROR (%d)\n", err);
return 2;
}
// Attempt to actually extract the file
if (!mz_zip_reader_extract_file_to_file(iwd, filepath, outPath, 0))
{
Con_Print_v("ERROR\n");
return 3;
}
Con_Print_v("SUCCESS\n");
#endif
return 0;
}
int __cdecl IWD_IWDExtract(const char* iwdPath, const char* iwdName)
{
mz_zip_archive iwd;
memset(&iwd, 0, sizeof(iwd));
if (!mz_zip_reader_init_file(&iwd, iwdPath, 0))
{
Con_Error_v("ERROR: Could not open %s\n", iwdPath);
return 2;
}
int extractedCount = 0;
size_t lenDirImage = strlen(IWD_DIR_IMAGE);
size_t lenDirSound = strlen(IWD_DIR_SOUND);
for (int f = 0; f < (int)mz_zip_reader_get_num_files(&iwd); f++)
{
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&iwd, f, &file_stat))
{
Con_Error_v("ERROR: Could Get Info for Compressed File\n");
mz_zip_reader_end(&iwd);
return -1;
}
if (g_extractImages.ValueBool() && _strnicmp(IWD_DIR_IMAGE, file_stat.m_filename, lenDirImage) == 0)
{
IWD_IWDExtractFile(&iwd, file_stat.m_filename);
continue;
}
else if (g_extractSounds.ValueBool() && _strnicmp(IWD_DIR_SOUND, file_stat.m_filename, lenDirSound) == 0)
{
IWD_IWDExtractFile(&iwd, file_stat.m_filename);
continue;
}
else if (g_extractMisc.ValueBool()) {
IWD_IWDExtractFile(&iwd, file_stat.m_filename);
continue;
}
}
mz_zip_reader_end(&iwd);
return 0;
}