TrinityCore
gameobject_extract.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "adtfile.h"
19#include "DB2CascFileSource.h"
20#include "Errors.h"
22#include "model.h"
23#include "StringFormat.h"
24#include "vmapexport.h"
25#include "VMapDefinitions.h"
26#include <CascLib.h>
27#include <algorithm>
28#include <cstdio>
29
30bool ExtractSingleModel(std::string& fname)
31{
32 if (fname.length() < 4)
33 return false;
34
35 std::string extension = fname.substr(fname.length() - 4, 4);
36 if (extension == ".mdx" || extension == ".MDX" || extension == ".mdl" || extension == ".MDL")
37 {
38 fname.erase(fname.length() - 2, 2);
39 fname.append("2");
40 }
41
42 std::string originalName = fname;
43
44 char* name = GetPlainName((char*)fname.c_str());
45 NormalizeFileName(name, strlen(name));
46
47 std::string output(szWorkDirWmo);
48 output += "/";
49 output += name;
50
51 if (FileExists(output.c_str()))
52 return true;
53
54 Model mdl(originalName);
55 if (!mdl.open())
56 return false;
57
58 return mdl.ConvertToVMAPModel(output.c_str());
59}
60
61extern std::shared_ptr<CASC::Storage> CascStorage;
62
63bool GetHeaderMagic(std::string const& fileName, uint32* magic)
64{
65 *magic = 0;
66 std::unique_ptr<CASC::File> file(CascStorage->OpenFile(fileName.c_str(), CASC_LOCALE_ALL_WOW));
67 if (!file)
68 return false;
69
70 uint32 bytesRead = 0;
71 if (!file->ReadFile(magic, 4, &bytesRead) || bytesRead != 4)
72 return false;
73
74 return true;
75}
76
78{
79 printf("Extracting GameObject models...\n");
80
82 DB2FileLoader db2;
83 try
84 {
86 }
87 catch (std::exception const& e)
88 {
89 printf("Fatal error: Invalid GameObjectDisplayInfo.db2 file format!\n%s\n", e.what());
90 exit(1);
91 }
92
93 std::string basepath = szWorkDirWmo;
94 basepath += "/";
95
96 std::string modelListPath = basepath + "temp_gameobject_models";
97 FILE* model_list = fopen(modelListPath.c_str(), "wb");
98 if (!model_list)
99 {
100 printf("Fatal error: Could not open file %s\n", modelListPath.c_str());
101 return;
102 }
103
104 fwrite(VMAP::RAW_VMAP_MAGIC, 1, 8, model_list);
105
106 for (uint32 rec = 0; rec < db2.GetRecordCount(); ++rec)
107 {
108 DB2Record record = db2.GetRecord(rec);
109 if (!record)
110 continue;
111
112 uint32 fileId = record.GetUInt32("FileDataID");
113 if (!fileId)
114 continue;
115
116 std::string fileName = Trinity::StringFormat("FILE{:08X}.xxx", fileId);
117 bool result = false;
118 uint32 header;
119 if (!GetHeaderMagic(fileName, &header))
120 continue;
121
122 if (!memcmp(&header, "REVM", 4))
123 result = ExtractSingleWmo(fileName);
124 else if (!memcmp(&header, "MD20", 4) || !memcmp(&header, "MD21", 4))
125 result = ExtractSingleModel(fileName);
126 else
127 ABORT_MSG("%s header: %d - %c%c%c%c", fileName.c_str(), header, (header >> 24) & 0xFF, (header >> 16) & 0xFF, (header >> 8) & 0xFF, header & 0xFF);
128
129 if (result)
130 {
131 uint32 displayId = record.GetId();
132 uint32 path_length = fileName.length();
133 fwrite(&displayId, sizeof(uint32), 1, model_list);
134 fwrite(&path_length, sizeof(uint32), 1, model_list);
135 fwrite(fileName.c_str(), sizeof(char), path_length, model_list);
136 }
137 }
138
139 fclose(model_list);
140
141 printf("Done!\n");
142}
uint32_t uint32
Definition: Define.h:142
#define ABORT_MSG
Definition: Errors.h:75
ModelList model_list
char const * GetPlainName(char const *FileName)
Definition: adtfile.cpp:24
void NormalizeFileName(char *name, size_t len)
Definition: adtfile.cpp:69
void Load(DB2FileSource *source, DB2FileLoadInfo const *loadInfo)
DB2Record GetRecord(uint32 recordNumber) const
uint32 GetRecordCount() const
uint32 GetUInt32(uint32 field, uint32 arrayIndex) const
uint32 GetId() const
Definition: model.h:33
bool open()
Definition: model.cpp:37
bool ConvertToVMAPModel(char const *outfilename)
Definition: model.cpp:88
std::shared_ptr< CASC::Storage > CascStorage
Definition: System.cpp:49
bool GetHeaderMagic(std::string const &fileName, uint32 *magic)
bool ExtractSingleModel(std::string &fname)
void ExtractGameobjectModels()
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
const char RAW_VMAP_MAGIC[]
static constexpr DB2LoadInfo Instance
Definition: DB2LoadInfo.h:1963
bool FileExists(char const *file)
Definition: vmapexport.cpp:178
char const * szWorkDirWmo
Definition: vmapexport.cpp:74
bool ExtractSingleWmo(std::string &fname)
Definition: vmapexport.cpp:188