TrinityCore
Loading...
Searching...
No Matches
adtfile.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 "vmapexport.h"
19#include "adtfile.h"
20#include "StringFormat.h"
21#include <algorithm>
22#include <cstdio>
23#include "Errors.h"
24
25char const* GetPlainName(char const* FileName)
26{
27 const char * szTemp;
28
29 if((szTemp = strrchr(FileName, '\\')) != nullptr)
30 FileName = szTemp + 1;
31 return FileName;
32}
33
34char* GetPlainName(char* FileName)
35{
36 char * szTemp;
37
38 if((szTemp = strrchr(FileName, '\\')) != nullptr)
39 FileName = szTemp + 1;
40 return FileName;
41}
42
43void FixNameCase(char* name, size_t len)
44{
45 char* ptr = name + len - 1;
46
47 //extension in lowercase
48 for (; *ptr != '.' && ptr >= name; --ptr)
49 *ptr |= 0x20;
50
51 for (; ptr >= name; --ptr)
52 {
53 if (ptr > name && *ptr >= 'A' && *ptr <= 'Z' && isalpha(*(ptr - 1)))
54 *ptr |= 0x20;
55 else if ((ptr == name || !isalpha(*(ptr - 1))) && *ptr >= 'a' && *ptr <= 'z')
56 *ptr &= ~0x20;
57 }
58}
59
60void FixNameSpaces(char* name, size_t len)
61{
62 if (len < 3)
63 return;
64
65 for (size_t i = 0; i < len - 3; i++)
66 if (name[i] == ' ')
67 name[i] = '_';
68}
69
70char* GetExtension(char* FileName)
71{
72 if (char* szTemp = strrchr(FileName, '.'))
73 return szTemp;
74 return nullptr;
75}
76
77ADTFile::ADTFile(char const* filename) : _file(filename)
78{
79 Adtfilename.append(filename);
80}
81
82bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY)
83{
84 if (_file.isEof())
85 return false;
86
87 uint32 size;
88 std::string dirname = std::string(szWorkDirWmo) + "/dir_bin";
89 FILE *dirfile;
90 dirfile = fopen(dirname.c_str(), "ab");
91 if(!dirfile)
92 {
93 printf("Can't open dirfile!'%s'\n", dirname.c_str());
94 return false;
95 }
96
97 while (!_file.isEof())
98 {
99 char fourcc[5];
100 _file.read(&fourcc,4);
101 _file.read(&size, 4);
102 flipcc(fourcc);
103 fourcc[4] = 0;
104
105 size_t nextpos = _file.getPos() + size;
106
107 if (!strcmp(fourcc,"MCIN"))
108 {
109 }
110 else if (!strcmp(fourcc,"MTEX"))
111 {
112 }
113 else if (!strcmp(fourcc,"MMDX"))
114 {
115 if (size)
116 {
117 char* buf = new char[size];
118 _file.read(buf, size);
119 char *p = buf;
120 while (p < buf + size)
121 {
122 std::string path(p);
123
124 char* s = GetPlainName(p);
125 FixNameCase(s, strlen(s));
126 FixNameSpaces(s, strlen(s));
127
128 ModelInstanceNames.emplace_back(s);
129
130 ExtractSingleModel(path);
131
132 p += strlen(p) + 1;
133 }
134 delete[] buf;
135 }
136 }
137 else if (!strcmp(fourcc,"MWMO"))
138 {
139 if (size)
140 {
141 char* buf = new char[size];
142 _file.read(buf, size);
143 char* p = buf;
144 while (p < buf + size)
145 {
146 std::string path(p);
147
148 char* s = GetPlainName(p);
149 FixNameCase(s, strlen(s));
150 FixNameSpaces(s, strlen(s));
151
152 WmoInstanceNames.emplace_back(s);
153
154 ExtractSingleWmo(path);
155
156 p += strlen(p) + 1;
157 }
158 delete[] buf;
159 }
160 }
161 //======================
162 else if (!strcmp(fourcc, "MDDF"))
163 {
164 if (size)
165 {
166 uint32 doodadCount = size / sizeof(ADT::MDDF);
167 for (uint32 i = 0; i < doodadCount; ++i)
168 {
169 ADT::MDDF doodadDef;
170 _file.read(&doodadDef, sizeof(ADT::MDDF));
171 Doodad::Extract(doodadDef, ModelInstanceNames[doodadDef.Id].c_str(), map_num, tileX, tileY, dirfile);
172 }
173 }
174 }
175 else if (!strcmp(fourcc,"MODF"))
176 {
177 if (size)
178 {
179 uint32 mapObjectCount = size / sizeof(ADT::MODF);
180 for (uint32 i = 0; i < mapObjectCount; ++i)
181 {
182 ADT::MODF mapObjDef;
183 _file.read(&mapObjDef, sizeof(ADT::MODF));
184 MapObject::Extract(mapObjDef, WmoInstanceNames[mapObjDef.Id].c_str(), map_num, tileX, tileY, dirfile);
185 Doodad::ExtractSet(WmoDoodads[WmoInstanceNames[mapObjDef.Id]], mapObjDef, map_num, tileX, tileY, dirfile);
186 }
187 }
188 }
189
190 //======================
191 _file.seek(nextpos);
192 }
193
194 _file.close();
195 fclose(dirfile);
196 return true;
197}
198
200{
201 _file.close();
202}
uint32_t uint32
Definition Define.h:133
char const * GetPlainName(char const *FileName)
Definition adtfile.cpp:25
void FixNameCase(char *name, size_t len)
Definition adtfile.cpp:43
char * GetExtension(char *FileName)
Definition adtfile.cpp:70
void FixNameSpaces(char *name, size_t len)
Definition adtfile.cpp:60
char const * GetPlainName(char const *FileName)
Definition adtfile.cpp:25
void FixNameCase(char *name, size_t len)
Definition adtfile.cpp:43
void FixNameSpaces(char *name, size_t len)
Definition adtfile.cpp:60
std::string Adtfilename
Definition adtfile.h:57
std::vector< std::string > ModelInstanceNames
Definition adtfile.h:62
MPQFile _file
Definition adtfile.h:56
bool init(uint32 map_num, uint32 tileX, uint32 tileY)
Definition adtfile.cpp:82
std::vector< std::string > WmoInstanceNames
Definition adtfile.h:61
ADTFile(char const *filename)
Definition adtfile.cpp:77
size_t read(void *dest, size_t bytes)
bool isEof()
Definition mpq_libmpq.h:91
void close()
size_t getPos()
Definition mpq_libmpq.h:88
void seek(int offset)
bool ExtractSingleModel(std::string &fname)
void flipcc(char *fcc)
Definition mpq_libmpq.h:97
void Extract(ADT::MDDF const &doodadDef, char const *ModelInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE *pDirfile)
Definition model.cpp:142
void ExtractSet(WMODoodadData const &doodadData, ADT::MODF const &wmo, uint32 mapID, uint32 tileX, uint32 tileY, FILE *pDirfile)
Definition model.cpp:184
void Extract(ADT::MODF const &mapObjDef, char const *WmoInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE *pDirfile)
uint32 Id
Definition adtfile.h:30
uint32 Id
Definition adtfile.h:40
std::unordered_map< std::string, WMODoodadData > WmoDoodads
char const * szWorkDirWmo
bool ExtractSingleWmo(std::string &fname)