TrinityCore
wdtfile.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 "wdtfile.h"
20#include "adtfile.h"
21#include "Common.h"
22#include "Errors.h"
23#include "StringFormat.h"
24#include <cstdio>
25
26extern std::shared_ptr<CASC::Storage> CascStorage;
27
28WDTFile::WDTFile(uint32 fileDataId, std::string const& description, std::string mapName, bool cache)
29 : _file(CascStorage, fileDataId, description), _mapName(std::move(mapName))
30{
31 memset(&_header, 0, sizeof(WDT::MPHD));
32 memset(&_adtInfo, 0, sizeof(WDT::MAIN));
33 if (cache)
34 {
35 _adtCache = std::make_unique<ADTCache>();
36 memset(_adtCache->file, 0, sizeof(_adtCache->file));
37 }
38 else
39 _adtCache = nullptr;
40}
41
42WDTFile::~WDTFile() = default;
43
45{
46 if (_file.isEof())
47 return false;
48
49 char fourcc[5];
51
52 std::string dirname = std::string(szWorkDirWmo) + "/dir_bin";
53 FILE* dirfile = fopen(dirname.c_str(), "ab");
54 if (!dirfile)
55 {
56 printf("Can't open dirfile!'%s'\n", dirname.c_str());
57 return false;
58 }
59
60 while (!_file.isEof())
61 {
62 _file.read(fourcc,4);
63 _file.read(&size, 4);
64
65 flipcc(fourcc);
66 fourcc[4] = 0;
67
68 size_t nextpos = _file.getPos() + size;
69
70 if (!strcmp(fourcc, "MPHD"))
71 {
72 ASSERT(size == sizeof(WDT::MPHD));
73 _file.read(&_header, sizeof(WDT::MPHD));
74 }
75 else if (!strcmp(fourcc,"MAIN"))
76 {
77 ASSERT(size == sizeof(WDT::MAIN));
78 _file.read(&_adtInfo, sizeof(WDT::MAIN));
79 }
80 else if (!strcmp(fourcc, "MAID"))
81 {
82 ASSERT(size == sizeof(WDT::MAID));
83 _adtFileDataIds = std::make_unique<WDT::MAID>();
84 _file.read(_adtFileDataIds.get(), sizeof(WDT::MAID));
85 }
86 else if (!strcmp(fourcc,"MWMO"))
87 {
88 // global map objects
89 if (size)
90 {
91 char *buf = new char[size];
92 _file.read(buf, size);
93 char *p = buf;
94 while (p < buf + size)
95 {
96 std::string path(p);
97
98 char* s = GetPlainName(p);
99 NormalizeFileName(s, strlen(s));
100 p = p + strlen(p) + 1;
101 _wmoNames.emplace_back(s);
102
103 ExtractSingleWmo(path);
104 }
105 delete[] buf;
106 }
107 }
108 else if (!strcmp(fourcc, "MODF"))
109 {
110 // global wmo instance data
111 if (size)
112 {
113 uint32 mapObjectCount = size / sizeof(ADT::MODF);
114 for (uint32 i = 0; i < mapObjectCount; ++i)
115 {
116 ADT::MODF mapObjDef;
117 _file.read(&mapObjDef, sizeof(ADT::MODF));
118 if (!(mapObjDef.Flags & 0x8))
119 {
120 MapObject::Extract(mapObjDef, _wmoNames[mapObjDef.Id].c_str(), true, mapId, mapId, dirfile, nullptr);
121 Doodad::ExtractSet(WmoDoodads[_wmoNames[mapObjDef.Id]], mapObjDef, true, mapId, mapId, dirfile, nullptr);
122 }
123 else
124 {
125 std::string fileName = Trinity::StringFormat("FILE{:08X}.xxx", mapObjDef.Id);
126 ExtractSingleWmo(fileName);
127 MapObject::Extract(mapObjDef, fileName.c_str(), true, mapId, mapId, dirfile, nullptr);
128 Doodad::ExtractSet(WmoDoodads[fileName], mapObjDef, true, mapId, mapId, dirfile, nullptr);
129 }
130 }
131 }
132 }
133 _file.seek((int)nextpos);
134 }
135
136 _file.close();
137 fclose(dirfile);
138 return true;
139}
140
142{
143 if (!(x >= 0 && y >= 0 && x < 64 && y < 64))
144 return nullptr;
145
146 if (_adtCache && _adtCache->file[x][y])
147 return _adtCache->file[x][y].get();
148
149 if (!(_adtInfo.Data[y][x].Flag & 1))
150 return nullptr;
151
152 ADTFile* adt;
153 std::string name = Trinity::StringFormat(R"(World\Maps\{}\{}_{}_{}_obj0.adt)", _mapName, _mapName, x, y);
154 if (_header.Flags & 0x200)
155 adt = new ADTFile(_adtFileDataIds->Data[y][x].Obj0ADT, name, _adtCache != nullptr);
156 else
157 adt = new ADTFile(name, _adtCache != nullptr);
158
159 if (_adtCache)
160 _adtCache->file[x][y].reset(adt);
161
162 return adt;
163}
164
166{
167 if (_adtCache)
168 return;
169
170 delete adt;
171}
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
#define ASSERT
Definition: Errors.h:68
char const * GetPlainName(char const *FileName)
Definition: adtfile.cpp:24
void NormalizeFileName(char *name, size_t len)
Definition: adtfile.cpp:69
void flipcc(char *fcc)
Definition: cascfile.h:58
bool isEof()
Definition: cascfile.h:52
void seek(int offset)
Definition: cascfile.cpp:99
size_t read(void *dest, size_t bytes)
Definition: cascfile.cpp:80
size_t getPos()
Definition: cascfile.h:49
void close()
Definition: cascfile.cpp:111
void FreeADT(ADTFile *adt)
Definition: wdtfile.cpp:165
WDT::MAIN _adtInfo
Definition: wdtfile.h:82
WDT::MPHD _header
Definition: wdtfile.h:81
std::vector< std::string > _wmoNames
Definition: wdtfile.h:85
ADTFile * GetMap(int32 x, int32 y)
Definition: wdtfile.cpp:141
WDTFile(uint32 fileDataId, std::string const &description, std::string mapName, bool cache)
Definition: wdtfile.cpp:28
bool init(uint32 mapId)
Definition: wdtfile.cpp:44
CASCFile _file
Definition: wdtfile.h:80
std::string _mapName
Definition: wdtfile.h:84
std::unique_ptr< ADTCache > _adtCache
Definition: wdtfile.h:90
std::unique_ptr< WDT::MAID > _adtFileDataIds
Definition: wdtfile.h:83
void ExtractSet(WMODoodadData const &doodadData, ADT::MODF const &wmo, bool isGlobalWmo, uint32 mapID, uint32 originalMapId, FILE *pDirfile, std::vector< ADTOutputCache > *dirfileCache)
Definition: model.cpp:226
void Extract(ADT::MODF const &mapObjDef, char const *WmoInstName, bool isGlobalWmo, uint32 mapID, uint32 originalMapId, FILE *pDirfile, std::vector< ADTOutputCache > *dirfileCache)
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
constexpr std::size_t size()
Definition: UpdateField.h:796
STL namespace.
uint16 Flags
Definition: adtfile.h:45
uint32 Id
Definition: adtfile.h:40
struct WDT::MAIN::SMAreaInfo Data[64][64]
uint32 Flags
Definition: wdtfile.h:34
std::unordered_map< std::string, WMODoodadData > WmoDoodads
Definition: vmapexport.cpp:70
char const * szWorkDirWmo
Definition: vmapexport.cpp:74
bool ExtractSingleWmo(std::string &fname)
Definition: vmapexport.cpp:188
std::shared_ptr< CASC::Storage > CascStorage
Definition: System.cpp:49