-
Notifications
You must be signed in to change notification settings - Fork 294
/
vmapexport.cpp
547 lines (475 loc) · 14.8 KB
/
vmapexport.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
/*
* This file is part of the CMaNGOS Project. See AUTHORS file for Copyright information
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _CRT_SECURE_NO_DEPRECATE
#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <errno.h>
#if defined WIN32
#include <Windows.h>
#include <sys/stat.h>
#include <direct.h>
#define mkdir _mkdir
#else
#include <sys/stat.h>
#endif
#undef min
#undef max
//#pragma warning(disable : 4505)
//#pragma comment(lib, "Winmm.lib")
#include <map>
//From Extractor
#include "adtfile.h"
#include "wdtfile.h"
#include "dbcfile.h"
#include "wmo.h"
#include "mpq_libmpq04.h"
#include "vmapexport.h"
//------------------------------------------------------------------------------
// Defines
#define MPQ_BLOCK_SIZE 0x1000
//-----------------------------------------------------------------------------
extern ArchiveSet gOpenArchives;
typedef struct
{
char name[64];
unsigned int id;
} map_id;
map_id* map_ids;
uint16* LiqType = 0;
uint32 map_count;
char output_path[128] = ".";
char input_path[1024] = ".";
bool hasInputPathParam = false;
bool preciseVectorData = false;
// Constants
//static const char * szWorkDirMaps = ".\\Maps";
const char* szWorkDirWmo = "./Buildings";
const char* szRawVMAPMagic = "VMAP005";
// Local testing functions
bool FileExists(const char* file)
{
if (FILE* n = fopen(file, "rb"))
{
fclose(n);
return true;
}
return false;
}
void strToLower(char* str)
{
while (*str)
{
*str = tolower(*str);
++str;
}
}
// copied from contrib/extractor/System.cpp
void ReadLiquidTypeTableDBC()
{
printf("Read LiquidType.dbc file...");
DBCFile dbc("DBFilesClient\\LiquidType.dbc");
if (!dbc.open())
{
printf("Fatal error: Invalid LiquidType.dbc file format!\n");
exit(1);
}
size_t LiqType_count = dbc.getRecordCount();
size_t LiqType_maxid = dbc.getRecord(LiqType_count - 1).getUInt(0);
LiqType = new uint16[LiqType_maxid + 1];
memset(LiqType, 0xff, (LiqType_maxid + 1) * sizeof(uint16));
for (uint32 x = 0; x < LiqType_count; ++x)
LiqType[dbc.getRecord(x).getUInt(0)] = dbc.getRecord(x).getUInt(3);
printf("Done! (%u LiqTypes loaded)\n", (unsigned int)LiqType_count);
}
bool ExtractWmo()
{
bool success = true;
//const char* ParsArchiveNames[] = {"patch-2.MPQ", "patch.MPQ", "common.MPQ", "expansion.MPQ"};
for (ArchiveSet::const_iterator ar_itr = gOpenArchives.begin(); ar_itr != gOpenArchives.end() && success; ++ar_itr)
{
vector<string> filelist;
(*ar_itr)->GetFileListTo(filelist);
for (vector<string>::iterator fname = filelist.begin(); fname != filelist.end() && success; ++fname)
{
if (fname->find(".wmo") != string::npos)
success = ExtractSingleWmo(*fname);
}
}
if (success)
printf("\nExtract wmo complete (No (fatal) errors)\n");
return success;
}
bool ExtractSingleWmo(std::string& fname)
{
// Copy files from archive
char szLocalFile[1024];
const char* plain_name = GetPlainName(fname.c_str());
sprintf(szLocalFile, "%s/%s", szWorkDirWmo, plain_name);
fixnamen(szLocalFile, strlen(szLocalFile));
if (FileExists(szLocalFile))
return true;
int p = 0;
//Select root wmo files
const char* rchr = strrchr(plain_name, '_');
if (rchr != NULL)
{
char cpy[4];
strncpy((char*)cpy, rchr, 4);
for (int i = 0; i < 4; ++i)
{
int m = cpy[i];
if (isdigit(m))
p++;
}
}
if (p == 3)
return true;
bool file_ok = true;
std::cout << "Extracting " << fname << std::endl;
WMORoot froot(fname);
if (!froot.open())
{
printf("Couldn't open RootWmo!!!\n");
return true;
}
FILE* output = fopen(szLocalFile, "wb");
if (!output)
{
printf("couldn't open %s for writing!\n", szLocalFile);
return false;
}
froot.ConvertToVMAPRootWmo(output);
int Wmo_nVertices = 0;
//printf("root has %d groups\n", froot->nGroups);
if (froot.nGroups != 0)
{
for (uint32 i = 0; i < froot.nGroups; ++i)
{
char temp[1024];
strcpy(temp, fname.c_str());
temp[fname.length() - 4] = 0;
char groupFileName[1024];
sprintf(groupFileName, "%s_%03d.wmo", temp, i);
//printf("Trying to open groupfile %s\n",groupFileName);
string s = groupFileName;
WMOGroup fgroup(s);
if (!fgroup.open())
{
printf("Could not open all Group file for: %s\n", plain_name);
file_ok = false;
break;
}
Wmo_nVertices += fgroup.ConvertToVMAPGroupWmo(output, &froot, preciseVectorData);
}
}
fseek(output, 8, SEEK_SET); // store the correct no of vertices
fwrite(&Wmo_nVertices, sizeof(int), 1, output);
fclose(output);
// Delete the extracted file in the case of an error
if (!file_ok)
remove(szLocalFile);
return true;
}
void ParsMapFiles()
{
char fn[512];
//char id_filename[64];
char id[10];
StringSet failedPaths;
for (unsigned int i = 0; i < map_count; ++i)
{
sprintf(id, "%03u", map_ids[i].id);
sprintf(fn, "World\\Maps\\%s\\%s.wdt", map_ids[i].name, map_ids[i].name);
WDTFile WDT(fn, map_ids[i].name);
if (WDT.init(id, map_ids[i].id))
{
printf("Processing Map %u\n[", map_ids[i].id);
for (int x = 0; x < 64; ++x)
{
for (int y = 0; y < 64; ++y)
{
if (ADTFile* ADT = WDT.GetMap(x, y))
{
//sprintf(id_filename,"%02u %02u %03u",x,y,map_ids[i].id);//!!!!!!!!!
ADT->init(map_ids[i].id, x, y, failedPaths);
delete ADT;
}
}
printf("#");
fflush(stdout);
}
printf("]\n");
}
}
if (!failedPaths.empty())
{
printf("Warning: Some models could not be extracted, see below\n");
for (StringSet::const_iterator itr = failedPaths.begin(); itr != failedPaths.end(); ++itr)
printf("Could not find file of model %s\n", itr->c_str());
printf("A few not found models can be expected and are not alarming.\n");
}
}
void getGamePath()
{
#ifdef _WIN32
strcpy(input_path, "Data\\");
#else
strcpy(input_path, "Data/");
#endif
}
bool scan_patches(char* scanmatch, std::vector<std::string>& pArchiveNames)
{
int i;
char path[512];
for (i = 1; i <= 99; i++)
{
if (i != 1)
{
sprintf(path, "%s-%d.MPQ", scanmatch, i);
}
else
{
sprintf(path, "%s.MPQ", scanmatch);
}
#ifndef _WIN32
if (FILE* h = fopen64(path, "rb"))
#else
if (FILE* h = fopen(path, "rb"))
#endif
{
fclose(h);
//matches.push_back(path);
pArchiveNames.push_back(path);
}
}
return (true);
}
bool fillArchiveNameVector(std::vector<std::string>& pArchiveNames)
{
if (!hasInputPathParam)
getGamePath();
printf("\nGame path: %s\n", input_path);
char path[512];
string in_path(input_path);
std::vector<std::string> locales, searchLocales;
searchLocales.push_back("enGB");
searchLocales.push_back("enUS");
searchLocales.push_back("deDE");
searchLocales.push_back("esES");
searchLocales.push_back("frFR");
searchLocales.push_back("koKR");
searchLocales.push_back("zhCN");
searchLocales.push_back("zhTW");
searchLocales.push_back("enCN");
searchLocales.push_back("enTW");
searchLocales.push_back("esMX");
searchLocales.push_back("ruRU");
for (std::vector<std::string>::iterator i = searchLocales.begin(); i != searchLocales.end(); ++i)
{
std::string localePath = in_path + *i;
// check if locale exists:
struct stat status;
if (stat(localePath.c_str(), &status))
continue;
if ((status.st_mode & S_IFDIR) == 0)
continue;
printf("Found locale '%s'\n", i->c_str());
locales.push_back(*i);
}
printf("\n");
// open locale expansion and common files
printf("Adding data files from locale directories.\n");
for (std::vector<std::string>::iterator i = locales.begin(); i != locales.end(); ++i)
{
pArchiveNames.push_back(in_path + *i + "/locale-" + *i + ".MPQ");
pArchiveNames.push_back(in_path + *i + "/expansion-locale-" + *i + ".MPQ");
pArchiveNames.push_back(in_path + *i + "/lichking-locale-" + *i + ".MPQ");
}
// open expansion and common files
pArchiveNames.push_back(input_path + string("common.MPQ"));
pArchiveNames.push_back(input_path + string("common-2.MPQ"));
pArchiveNames.push_back(input_path + string("expansion.MPQ"));
pArchiveNames.push_back(input_path + string("lichking.MPQ"));
// now, scan for the patch levels in the core dir
printf("Scanning patch levels from data directory.\n");
sprintf(path, "%spatch", input_path);
if (!scan_patches(path, pArchiveNames))
return (false);
// now, scan for the patch levels in locale dirs
printf("Scanning patch levels from locale directories.\n");
bool foundOne = false;
for (std::vector<std::string>::iterator i = locales.begin(); i != locales.end(); ++i)
{
printf("Locale: %s\n", i->c_str());
sprintf(path, "%s%s/patch-%s", input_path, i->c_str(), i->c_str());
if (scan_patches(path, pArchiveNames))
foundOne = true;
}
printf("\n");
if (!foundOne)
{
printf("no locale found\n");
return false;
}
return true;
}
bool processArgv(int argc, char** argv)
{
bool result = true;
hasInputPathParam = false;
bool preciseVectorData = false;
for (int i = 1; i < argc; ++i)
{
if (strcmp("-s", argv[i]) == 0)
{
preciseVectorData = false;
}
else if (strcmp("-d", argv[i]) == 0)
{
if ((i + 1) < argc)
{
hasInputPathParam = true;
strcpy(input_path, argv[i + 1]);
if (input_path[strlen(input_path) - 1] != '\\' || input_path[strlen(input_path) - 1] != '/')
strcat(input_path, "/");
++i;
}
else
{
result = false;
}
}
else if (strcmp("-?", argv[1]) == 0)
{
result = false;
}
else if (strcmp("-l", argv[i]) == 0)
{
preciseVectorData = true;
}
else
{
result = false;
break;
}
}
if (!result)
{
printf("Extract for %s.\n", szRawVMAPMagic);
printf("%s [-?][-s][-l][-d <path>]\n", argv[0]);
printf(" -s : (default) small size (data size optimization), ~500MB less vmap data.\n");
printf(" -l : large size, ~500MB more vmap data. (might contain more details)\n");
printf(" -d <path>: Path to the vector data source folder.\n");
printf(" -? : This message.\n");
}
return result;
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Main
//
// The program must be run with two command line arguments
//
// Arg1 - The source MPQ name (for testing reading and file find)
// Arg2 - Listfile name
//
int main(int argc, char** argv)
{
bool success = true;
// Use command line arguments, when some
if (!processArgv(argc, argv))
return 1;
// some simple check if working dir is dirty
else
{
std::string sdir = std::string(szWorkDirWmo) + "/dir";
std::string sdir_bin = std::string(szWorkDirWmo) + "/dir_bin";
struct stat status;
if (!stat(sdir.c_str(), &status) || !stat(sdir_bin.c_str(), &status))
{
printf("Your output directory seems to be polluted, please use an empty directory!\n");
printf("<press return to exit>");
char garbage[2];
scanf("%c", garbage);
return 1;
}
}
printf("Extract for %s. Beginning work ....\n", szRawVMAPMagic);
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// Create the working directory
if (mkdir(szWorkDirWmo
#ifndef _WIN32
, 0711
#endif
))
success = (errno == EEXIST);
// prepare archive name list
std::vector<std::string> archiveNames;
fillArchiveNameVector(archiveNames);
for (size_t i = 0; i < archiveNames.size(); ++i)
{
MPQArchive* archive = new MPQArchive(archiveNames[i].c_str());
if (!gOpenArchives.size() || gOpenArchives.front() != archive)
delete archive;
}
if (gOpenArchives.empty())
{
printf("FATAL ERROR: None MPQ archive found by path '%s'. Use -d option with proper path.\n", input_path);
return 1;
}
ReadLiquidTypeTableDBC();
// extract data
if (success)
success = ExtractWmo();
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//map.dbc
if (success)
{
DBCFile* dbc = new DBCFile("DBFilesClient\\Map.dbc");
if (!dbc->open())
{
delete dbc;
printf("FATAL ERROR: Map.dbc not found in data file.\n");
return 1;
}
map_count = dbc->getRecordCount();
map_ids = new map_id[map_count];
for (unsigned int x = 0; x < map_count; ++x)
{
map_ids[x].id = dbc->getRecord(x).getUInt(0);
strcpy(map_ids[x].name, dbc->getRecord(x).getString(1));
printf("Map - %s\n", map_ids[x].name);
}
delete dbc;
ParsMapFiles();
delete [] map_ids;
//nError = ERROR_SUCCESS;
// Extract models, listed in DameObjectDisplayInfo.dbc
ExtractGameobjectModels();
}
printf("\n");
if (!success)
{
printf("ERROR: Extract for %s. Work NOT complete.\n Precise vector data=%d.\nPress any key.\n", szRawVMAPMagic, preciseVectorData);
getchar();
}
printf("Extract for %s. Work complete. No errors.\n", szRawVMAPMagic);
delete [] LiqType;
return 0;
}