-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
209 lines (152 loc) · 4.91 KB
/
main.cc
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
//------------------------------------------------------------------------
// QSAVETEX : Main program
//------------------------------------------------------------------------
//
// Copyright (C) 2021-2022 The OBSIDIAN Team
// Copyright (c) 2009-2017 Andrew J Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
#include "main.h"
#include <filesystem>
#include "im_tex.h"
#include "pakfile.h"
#define VERSION "0.75"
#define LOG_FILE "qsavetex_log.txt"
#define QUAKE1_OUTPUT "quake_tex.wd2"
#define HEXEN2_OUTPUT "hexen2_tex.wd2"
static FILE *log_file;
static const char *output_name = QUAKE1_OUTPUT;
static bool explicit_output = false;
void LogInit(const char *filename) {
log_file = fopen(filename, "w");
LogPrintf("\n");
LogPrintf("QSAVETEX LOG\n");
LogPrintf("============\n\n");
}
void LogClose(void) {
LogPrintf("\n");
if (log_file) {
fflush(log_file);
fclose(log_file);
log_file = NULL;
}
}
void LogPrintf(const char *str, ...) {
if (log_file) {
va_list args;
va_start(args, str);
vfprintf(log_file, str, args);
va_end(args);
fflush(log_file);
}
}
void FatalError(const char *message, ...) {
LogPrintf("\nFATAL ERROR OCCURRED:\n\n");
if (log_file) {
va_list argptr;
va_start(argptr, message);
vfprintf(log_file, message, argptr);
va_end(argptr);
fprintf(log_file, "\n\n");
}
// delete output file (need to close it first)
WAD2_CloseWrite();
if (FileDelete(output_name)) {
LogPrintf("\n(deleted incomplete output file)\n");
}
LogClose();
exit(9);
}
void ShowHelp(void) {
printf("\n");
printf("**** QSAVETEX v" VERSION " (C) 2009-2010 Andrew Apted ****\n");
printf("\n");
printf("USAGE: qsavetex\n");
printf("\n");
printf("OPTIONS:\n");
printf(" -h --help show this help text\n");
printf(" -o --output <name> set the output filename\n");
printf("\n");
printf(
"This program is free software, under the terms of the GNU General\n");
printf("Public License, and comes with ABSOLUTELY NO WARRANTY. See the\n");
printf("accompanying documentation for more details. USE AT OWN RISK.\n");
printf("\n");
}
/* returns number of arguments used, at least 1 */
int HandleOption(int argc, char **argv) {
const char *opt = argv[0];
// GNU style options begin with two dashes
if (opt[0] == '-' && opt[1] == '-') opt++;
if (StringCaseCmp(opt, "-o") == 0 || StringCaseCmp(opt, "-output") == 0) {
if (argc <= 1 || argv[1][0] == '-')
FatalError("Missing output filename after %s\n", argv[0]);
output_name = StringDup(argv[1]);
explicit_output = true;
return 2;
}
FatalError("Unknown option: %s\n", argv[0]);
return 0; // NOT REACHED
}
void DetectGame(void) {
if (explicit_output) return;
if (!PAK_OpenRead("pak0.pak")) return;
bool is_hexen2 = false;
if (PAK_FindEntry("puzzles.txt") >= 0) {
is_hexen2 = true;
output_name = HEXEN2_OUTPUT;
}
PAK_CloseRead();
LogPrintf("\nGame detected: %s\n\n", is_hexen2 ? "Hexen II" : "Quake");
}
int main(int argc, char **argv) {
// skip program name itself
argv++, argc--;
if (argc >= 1 && (StringCaseCmp(argv[0], "/?") == 0 ||
StringCaseCmp(argv[0], "-h") == 0 ||
StringCaseCmp(argv[0], "-help") == 0 ||
StringCaseCmp(argv[0], "--help") == 0)) {
ShowHelp();
return 1;
}
LogInit(LOG_FILE);
std::filesystem::path working_path = std::filesystem::current_path();
if (working_path.empty()) working_path = ".";
FileChangeDir(working_path);
// handle command-line arguments
while (argc > 0) {
if (argv[0][0] == '-') {
int num = HandleOption(argc, argv);
argv += num;
argc -= num;
continue;
}
argv++;
argc--;
}
DetectGame();
if (!WAD2_OpenWrite(output_name)) {
FatalError("Could not create file: %s\n", output_name);
}
LogPrintf("\n");
TEX_ExtractStart();
TEX_ExtractFromPAK("pak0.pak");
TEX_ExtractFromPAK("pak1.pak");
TEX_ExtractDone();
WAD2_CloseWrite();
LogPrintf("\nSuccess!");
LogClose();
return 0;
}
//--- editor settings ---
// vi:ts=2:sw=2:expandtab