-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
631 lines (527 loc) · 18.4 KB
/
main.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
This file is part of the Paintball 2 Map Release Tool.
The Paintball 2 Map Release Tool 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 3 of the License, or (at your option)
any later version.
The Paintball 2 Map Release Tool 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.
You should have received a copy of the GNU General Public License
along with this file. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2023 Richard Ebeling
*/
#include "main.h"
#include "Files.h"
#include "DefaultFiles.h"
#include "resource.h"
//Use ComCtl.dll V6
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
HWND g_hWinMain;
HWND g_hStatus;
HWND g_hStatic;
HFONT g_hFont;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX wincl;
CHAR szClassName[ ] = "MapReleaseTool\0";
INITCOMMONCONTROLSEX icex;
wincl.hInstance = hInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WndProc;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_APP));
wincl.hIconSm = (HICON) LoadImage(hInstance, MAKEINTRESOURCE(IDI_APP), IMAGE_ICON, 16, 16, LR_SHARED);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) (COLOR_WINDOW);
if (!RegisterClassEx (&wincl))
{
MessageBox(NULL, "Could not register window class. Will now exit.", NULL, MB_OK | MB_ICONERROR);
exit(-1);
}
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_BAR_CLASSES | ICC_STANDARD_CLASSES;
InitCommonControlsEx(&icex);
g_hWinMain = CreateWindowEx (WS_EX_ACCEPTFILES,
szClassName,
"DP:PB2 Map Release Tool\0",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
300,
150,
HWND_DESKTOP,
NULL,
hInstance,
NULL);
//Other windows will be created in "OnCreate".
if (g_hWinMain == NULL)
{
MessageBox(NULL, "Could create main window. Will now exit.", NULL, MB_OK | MB_ICONERROR);
exit(-1);
}
ShowWindow(g_hWinMain, nCmdShow);
UpdateWindow(g_hWinMain);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
HANDLE_MSG(hWnd, WM_CREATE, OnMainWindowCreate);
HANDLE_MSG(hWnd, WM_SIZE, OnMainWindowSize);
HANDLE_MSG(hWnd, WM_DROPFILES, OnMainWindowDropFiles);
HANDLE_MSG(hWnd, WM_DESTROY, OnMainWindowDestroy);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
static BOOL OnMainWindowCreate (HWND hWnd, LPCREATESTRUCT lpCreateStruct)
{
g_hStatus = CreateWindowEx(0,
STATUSCLASSNAME,
NULL,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hWnd,
NULL,
GetModuleHandle(NULL),
NULL);
ChangeStatusText("Waiting for user input...");
g_hStatic = CreateWindowEx(0,
"STATIC",
"Drop your .bsp file here",
WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_CENTER,
0, 0, 0, 0,
hWnd,
NULL,
GetModuleHandle(NULL),
NULL);
HDC hdc = GetDC(NULL);
LONG lfHeight = -MulDiv(9, GetDeviceCaps(hdc, LOGPIXELSY), 72);
ReleaseDC(NULL, hdc);
g_hFont = CreateFont(lfHeight, 0, 0, 0, 0, FALSE, 0, 0, 0, 0, 0, 0, 0, "MS Shell Dlg\0");
SendMessage(g_hStatic, WM_SETFONT, WPARAM(g_hFont), true);
return TRUE;
}
static void OnMainWindowSize (HWND hWnd, UINT state, int cx, int cy)
{
SendMessage(g_hStatus, WM_SIZE, 0, 0);
RECT rc;
GetWindowRect(g_hStatus, &rc);
int iStatusHeight = rc.bottom - rc.top;
GetClientRect(hWnd, &rc);
MoveWindow(g_hStatic, 0, 0, rc.right, rc.bottom - iStatusHeight, FALSE);
RedrawWindow(g_hStatic, NULL, NULL, RDW_INVALIDATE | RDW_ERASE);
}
static void OnMainWindowDropFiles (HWND hWnd, HDROP hDrop)
{
if (DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0) != 1)
{
MessageBox(g_hWinMain, "This tool can only process one file at the same time.", "Too many files", MB_OK | MB_ICONERROR);
return;
}
UINT size = DragQueryFile(hDrop, 0, NULL, 0) + 1;
std::vector<char> path_buffer(size);
if (!DragQueryFile(hDrop, 0, path_buffer.data(), size))
{
MessageBox(g_hWinMain, "Error when trying to get the dropped files.", "Error", MB_OK | MB_ICONERROR);
return;
}
DragFinish(hDrop);
GenerateArchiveFromFile(std::string(path_buffer.data()), ChangeStatusText);
}
static void OnMainWindowDestroy (HWND hWnd)
{
DeleteObject(g_hFont);
PostQuitMessage(0);
}
static void ChangeStatusText(std::string str)
{
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)str.c_str());
}
static void GenerateArchiveFromFile(const std::string &file, void (*ChangeStatusText)(std::string))
{
std::vector<std::string> requiredFiles;
MistakesClass mistakes;
(*ChangeStatusText)("Checking file location...");
BSPFilePath bspPath(file);
if (!bspPath.IsOk())
{
mistakes.wrongFilePlacing = true;
mistakes.displayMistakes();
(*ChangeStatusText)("Waiting for user input...");
return;
}
(*ChangeStatusText)("Reading file...");
auto bsp = std::make_unique<BSPFile>(bspPath.GetPath());
if (!bsp->IsOk())
{
MessageBox(g_hWinMain, "Error while reading the map file.", "Error", MB_OK | MB_ICONERROR);
(*ChangeStatusText)("Waiting for user input...");
return;
}
(*ChangeStatusText)("Analyzing compilation...");
mistakes.noVisingOrLighting = (!bsp->IsVised() || !bsp->IsLighted());
(*ChangeStatusText)("Analyzing textures...");
ConcatStringVectorsWithoutDuplicates(&requiredFiles, bsp->GetUsedTextures());
(*ChangeStatusText)("Analyzing required files in entities...");
ConcatStringVectorsWithoutDuplicates(&requiredFiles, bsp->RequiredFilesInEntities());
//requiredfiles in worldspawn; model in func_* / model; sky texture
(*ChangeStatusText)("Analyzing dependencies in required files...");
AddDependencies(&requiredFiles, bspPath.GetPb2BasePath());
//adds dependencies in the above, recursively, without a depth limit
(*ChangeStatusText)("Comparing required files with default files...");
RemoveDefaultFiles(&requiredFiles);
(*ChangeStatusText)("Looking for r_script, mapinfo and mapshot...");
std::string name = bspPath.GetFilename();
std::string tmp = GetRealFilename(bspPath.GetPb2BasePath(), "pics/mapshots/beta/" + name.substr(0, name.length() - 4) + ".img");
if (tmp.length() > 0)
requiredFiles.push_back(tmp);
tmp = "maps/mapinfo/" + name.substr(0, name.length() - 4) + ".txt";
if (FileExists(bspPath.GetPb2BasePath() + tmp))
requiredFiles.push_back(tmp);
tmp = "scripts/" + name.substr(0, name.length() - 4) + ".txt";
if (FileExists(bspPath.GetPb2BasePath() + tmp))
requiredFiles.push_back(tmp);
(*ChangeStatusText)("Generating real filenames...");
ReplaceWithRealFilenames(&requiredFiles, &mistakes, bspPath.GetPb2BasePath());
//replace generic extensiosn (.img and .mdl) with the real ones (and adds a .skp file for every .skm)
(*ChangeStatusText)("Checking textures...");
CheckTextures(&requiredFiles, &mistakes, bspPath.GetPb2BasePath());
//power of two? Non-hr4 version linked?
requiredFiles.insert(requiredFiles.end(), bspPath.GetNonBasePath());
(*ChangeStatusText)("Creating the archive...");
std::string out = GetOutputPath(bspPath.GetFilename());
if (out.length() == 0)
{
(*ChangeStatusText)("Aborted...");
return;
}
if (CreateZip(out, requiredFiles, bspPath.GetPb2BasePath()))
{
if (mistakes.isAnythingWrong())
{
mistakes.displayMistakes();
}
MessageBox(g_hWinMain, "The archive has been created.\n\n"
"Credits to the developers of the zlib and minizip libraries for providing "
"a free solution for creating zip files.",
"Archive successfully created", MB_ICONINFORMATION | MB_OK);
}
else
{
MessageBox(g_hWinMain, "Fatal error: could not create the archive.", "Fatal Error", MB_ICONERROR | MB_OK);
}
(*ChangeStatusText)("Waiting for user input...");
}
static void AddDependencies(std::vector<std::string> * requiredFiles, const std::string &pb2path)
{
// iterating with index because the vector is extended as we iterate.
for (size_t i = 0; i < requiredFiles->size(); i++)
{
//Dependencies from r_scripts
if (requiredFiles->at(i).length() > 4 && !requiredFiles->at(i).compare(requiredFiles->at(i).length() - 4, 4, ".txt"))
{
RScriptFile script((pb2path + requiredFiles->at(i)).c_str());
if (script.IsOk())
{
std::vector<std::string> tmp = script.GetUsedTextures();
for (size_t x = 0; x < tmp.size(); x++)
{
if (std::find(requiredFiles->begin(), requiredFiles->end(), tmp[x]) == requiredFiles->end())
requiredFiles->insert(requiredFiles->end(), tmp[x]);
}
}
}
//Dependencies from model files
else if (requiredFiles->at(i).length() > 4 && !requiredFiles->at(i).compare(requiredFiles->at(i).length() - 4, 4, ".mdl"))
{
//If available, use skm model
std::string path = pb2path + requiredFiles->at(i).substr(0, requiredFiles->at(i).length() - 4) + ".skm";
auto skm = std::make_unique<SKMFile>(path.c_str());
if (skm->IsOk())
{
std::vector<std::string> tmp = skm->GetUsedTextures(); // will only return texture names, so we need to prepend "models/mymodels/"
size_t end = requiredFiles->at(i).find_last_of("/");
for (size_t x = 0; x < tmp.size(); x++)
{
tmp[x] = requiredFiles->at(i).substr(0, end + 1) + tmp[x];
if (std::find(requiredFiles->begin(), requiredFiles->end(), tmp[x]) == requiredFiles->end())
requiredFiles->insert(requiredFiles->end(), tmp[x]);
}
}
else //use md2 model
{
path = pb2path + requiredFiles->at(i).substr(0, requiredFiles->at(i).length() - 4) + ".md2";
auto md2 = std::make_unique<MD2File>(path.c_str());
if (md2->IsOk())
{
std::vector<std::string> tmp = md2->GetUsedTextures();
for (size_t x = 0; x < tmp.size(); x++)
{
if (std::find(requiredFiles->begin(), requiredFiles->end(), tmp[x]) == requiredFiles->end())
requiredFiles->insert(requiredFiles->end(), tmp[x]);
}
}
}
}
}
}
static void CheckTextures(std::vector<std::string> * files, MistakesClass * mistakes, const std::string &pb2path)
{
for (size_t i = 0; i < files->size(); )
{
std::string extension = files->at(i).substr(files->at(i).size() - 4);
if ( extension.compare(".jpg")
&& extension.compare(".tga")
&& extension.compare(".wal")
&& extension.compare(".png")
&& extension.compare(".pcx") )
{
i++;
continue; //not a texture file
}
if (files->at(i).find("hr4") != std::string::npos)
{
mistakes->missingFiles.push_back(files->at(i) + " - always link to the low-res version. File not added.");
files->erase(files->begin() + i);
continue; //mapper should link to the low res texture and leave the choice of usign hr4s to the user
}
ImageFile img ((pb2path + files->at(i)).c_str());
if (!img.IsOk()) {
mistakes->missingFiles.push_back(files->at(i) + " - could not read -- corrupted file? File added.");
i++;
continue;
}
if (!img.IsHeightPowerOfTwo() || !img.IsWidthPowerOfTwo())
{
mistakes->missingFiles.push_back(files->at(i) + " - resolution is not power of two. File added.");
i++;
continue;
}
size_t pos = files->at(i).find_last_of("/");
std::string hr4file = files->at(i).substr(0, pos)
+ "/hr4"
+ files->at(i).substr(pos, files->at(i).length() - pos - 4)
+".img";
hr4file = GetRealFilename(pb2path, hr4file);
if (hr4file.length() > 0)
{
files->insert(files->begin() + i, hr4file);
i++; //jump over the created entry to the check for hr4 won't be triggered.
}
i++;
}
}
static bool CreateZip(const std::string &path, const std::vector<std::string> &files, const std::string &pb2BasePath)
{
std::ifstream input;
std::vector<char> buffer;
bool ok = true;
zipFile zf = zipOpen(path.c_str(), APPEND_STATUS_CREATE);
if (zf == NULL)
return false;
for (const auto& file : files)
{
input.open(pb2BasePath + file, std::ios::binary | std::ios::ate);
if (!input.is_open())
{
ok = false;
input.close();
break;
}
std::streamoff size = input.tellg();
buffer.resize(static_cast<unsigned int>(size));
input.seekg(0, std::ios::beg);
input.read((char*)&buffer[0], size);
input.close();
zip_fileinfo zfi = { 0 };
if (S_OK == zipOpenNewFileInZip(zf, file.c_str(), &zfi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION))
{
if (0 != zipWriteInFileInZip(zf, &buffer[0], static_cast<unsigned int>(buffer.size())))
ok = false;
if (0 != zipCloseFileInZip(zf))
ok = false;
}
else
{
ok = false;
}
}
if (0 != zipClose(zf, NULL))
{
return false;
}
else
{
return ok;
}
}
static void ConcatStringVectorsWithoutDuplicates(std::vector<std::string> * vecDest, const std::vector<std::string> &vecSource)
{
vecDest->insert(vecDest->end(), vecSource.begin(), vecSource.end());
RemoveMultipleEntries(vecDest);
}
static bool FileExists(const std::string &path)
{
std::ifstream f(path);
if (f.good())
{
f.close();
return true;
}
else
{
f.close();
return false;
}
}
static std::string GetOutputPath(const std::string &bspFilename)
{
char file[MAX_PATH];
memset(file, 0, sizeof(file));
memcpy (file, bspFilename.substr(0, bspFilename.length() - 4).append(".zip").c_str(), bspFilename.length());
OPENFILENAME ofn;
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = g_hWinMain;
ofn.hInstance = GetModuleHandle(NULL);
ofn.lpstrFilter = "ZIP file (*.zip)\0*.zip\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFile = &file[0];
ofn.nMaxFile = sizeof(file)/sizeof(file[0]);
ofn.nFileExtension = static_cast<WORD>(bspFilename.length() - 3);
ofn.lpstrTitle = "Select a location to create the archive";
ofn.lpstrDefExt = "zip";
ofn.Flags = OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT;
if (!GetSaveFileName(&ofn))
{
return std::string();
}
else
{
return std::string(ofn.lpstrFile);
}
}
static std::string GetRealFilename(const std::string &pb2BasePath, const std::string &file)
{
std::string result;
std::vector<std::string> validExtensions;
size_t exStart = file.find_last_of('.');
if (exStart == std::string::npos)
{
return result;
}
std::string extension = file.substr(exStart);
if (!extension.compare(".img"))
{
validExtensions.reserve(5);
validExtensions.push_back(".png");
validExtensions.push_back(".jpg");
validExtensions.push_back(".tga");
validExtensions.push_back(".pcx");
validExtensions.push_back(".wal");
}
else if (!extension.compare(".mdl"))
{
validExtensions.reserve(2);
validExtensions.push_back(".skm");
validExtensions.push_back(".md2");
}
else
{
validExtensions.push_back(extension);
}
for (const auto& validExtension : validExtensions)
{
if ( FileExists(pb2BasePath + file.substr(0, exStart) + validExtension) )
{
result = file.substr(0, exStart) + validExtension;
return result;
}
}
return result;
}
static void RemoveDefaultFiles(std::vector<std::string> * files)
{
std::erase_if(*files, [](const auto& file) {
return defaultFiles.contains(file) || defaultFilesWithoutExtensions.contains(file);
});
}
static void RemoveMultipleEntries(std::vector<std::string> * vec)
{
std::sort(vec->begin(), vec->end());
std::vector<std::string>::iterator last = std::unique(vec->begin(), vec->end());
vec->erase(last, vec->end());
}
static void ReplaceWithRealFilenames(std::vector<std::string> * requiredFiles, MistakesClass * mistakes, const std::string &pb2BasePath)
{
for (size_t i = 0; i < requiredFiles->size(); ) //dont increment i every loop in case the element is deleted
{
std::string path = GetRealFilename(pb2BasePath, requiredFiles->at(i));
if (path.length() == 0)
{
std::string tmp = requiredFiles->at(i) + " - no such file found. File not added.";
if (std::find(mistakes->missingFiles.begin(), mistakes->missingFiles.end(), tmp) == mistakes->missingFiles.end())
mistakes->missingFiles.insert(mistakes->missingFiles.end(), tmp);
requiredFiles->erase(requiredFiles->begin() + i);
}
else
{
requiredFiles->at(i) = path;
if (!path.substr(path.length() - 4).compare(".skm"))
{
requiredFiles->insert(requiredFiles->begin() + i, path = path.substr(0, path.length() - 4).append(".skp"));
i++;
}
i++;
}
}
}
void MistakesClass::displayMistakes(void) const
{
std::string msg = "Some things are not correct. Please fix the following errors:";
if (wrongFilePlacing)
msg.append("\n\nErr1: Your fileplacing or naming is wrong. Make sure the bsp file is in pball/maps/beta and has "
"a '_bX' or '_betaX' extension. The filename should only consist of lower case letters or underscores. "
"Example: pball/maps/beta/yourmap_beta1.bsp\n"
"There may be more errors that can't be detected due to missing information about given files likes custom textures.\n");
if (noVisingOrLighting)
msg.append("\n\n\nErr2: The mapfile you selected does not have vis-information or lightmap information. "
"Make sure you perform a final compile with a vising and lighting process.\n");
if (missingFiles.size() > 0)
{
msg.append("\n\nErr3: There are some files who either are not accessable or whose specifications are incorrect. "
"Make sure you put your own files in additional folders and all image files are power of 2 resolutions. "
"\"File added\" means that the file was added to the archive although it has incorrect specifications\n"
"Correct example: pball/textures/yourtextures/yourtexture1.jpg with 128x128.\n\n"
"Please check these files (*.img = any kind of image; *.mdl = any kind of model):\n");
for (const auto& missingFile : missingFiles)
msg.append(missingFile + "\n");
}
msg.append("\n\nYou may use CTRL+C to copy the content of this message and paste it somewhere else so reading it becomes easier.");
MessageBox(g_hWinMain, msg.c_str(), "Errors while checking files...", MB_OK | MB_ICONWARNING);
}
bool MistakesClass::isAnythingWrong(void) const
{
return (wrongFilePlacing || noVisingOrLighting || missingFiles.size() != 0);
}
MistakesClass::MistakesClass()
{
noVisingOrLighting = false;
wrongFilePlacing = false;
}