TrinityCore
M2Stores.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 "M2Stores.h"
19#include "Containers.h"
20#include "DB2Stores.h"
21#include "Log.h"
22#include "M2Structure.h"
23#include "Timer.h"
24#include <boost/filesystem/path.hpp>
25#include <G3D/Vector4.h>
26#include <fstream>
27
28typedef std::vector<FlyByCamera> FlyByCameraCollection;
29std::unordered_map<uint32, FlyByCameraCollection> sFlyByCameraStore;
30
31// Convert the geomoetry from a spline value, to an actual WoW XYZ
32G3D::Vector3 translateLocation(G3D::Vector4 const* dbcLocation, G3D::Vector3 const* basePosition, G3D::Vector3 const* splineVector)
33{
34 G3D::Vector3 work;
35 float x = basePosition->x + splineVector->x;
36 float y = basePosition->y + splineVector->y;
37 float z = basePosition->z + splineVector->z;
38 float const distance = sqrt((x * x) + (y * y));
39 float angle = std::atan2(x, y) - dbcLocation->w;
40
41 if (angle < 0)
42 angle += 2 * float(M_PI);
43
44 work.x = dbcLocation->x + (distance * sin(angle));
45 work.y = dbcLocation->y + (distance * cos(angle));
46 work.z = dbcLocation->z + z;
47 return work;
48}
49
50// Number of cameras not used. Multiple cameras never used in 7.1.5
51bool readCamera(M2Camera const* cam, uint32 buffSize, M2Header const* header, CinematicCameraEntry const* dbcentry)
52{
53 char const* buffer = reinterpret_cast<char const*>(header);
54
56 FlyByCameraCollection targetcam;
57
58 G3D::Vector4 dbcData;
59 dbcData.x = dbcentry->Origin.X;
60 dbcData.y = dbcentry->Origin.Y;
61 dbcData.z = dbcentry->Origin.Z;
62 dbcData.w = dbcentry->OriginFacing;
63
64 // Read target locations, only so that we can calculate orientation
65 for (uint32 k = 0; k < cam->target_positions.timestamps.number; ++k)
66 {
67 // Extract Target positions
68 if (cam->target_positions.timestamps.offset_elements + sizeof(M2Array) > buffSize)
69 return false;
70 M2Array const* targTsArray = reinterpret_cast<M2Array const*>(buffer + cam->target_positions.timestamps.offset_elements);
71 if (targTsArray->offset_elements + sizeof(uint32) > buffSize || cam->target_positions.values.offset_elements + sizeof(M2Array) > buffSize)
72 return false;
73 uint32 const* targTimestamps = reinterpret_cast<uint32 const*>(buffer + targTsArray->offset_elements);
74 M2Array const* targArray = reinterpret_cast<M2Array const*>(buffer + cam->target_positions.values.offset_elements);
75
76 if (targArray->offset_elements + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
77 return false;
78 M2SplineKey<G3D::Vector3> const* targPositions = reinterpret_cast<M2SplineKey<G3D::Vector3> const*>(buffer + targArray->offset_elements);
79
80 // Read the data for this set
81 uint32 currPos = targArray->offset_elements;
82 for (uint32 i = 0; i < targTsArray->number; ++i)
83 {
84 if (currPos + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
85 return false;
86 // Translate co-ordinates
87 G3D::Vector3 newPos = translateLocation(&dbcData, &cam->target_position_base, &targPositions->p0);
88
89 // Add to vector
90 FlyByCamera thisCam;
91 thisCam.timeStamp = targTimestamps[i];
92 thisCam.locations.Relocate(newPos.x, newPos.y, newPos.z, 0.0f);
93 targetcam.push_back(thisCam);
94 targPositions++;
95 currPos += sizeof(M2SplineKey<G3D::Vector3>);
96 }
97 }
98
99 // Read camera positions and timestamps (translating first position of 3 only, we don't need to translate the whole spline)
100 for (uint32 k = 0; k < cam->positions.timestamps.number; ++k)
101 {
102 // Extract Camera positions for this set
103 if (cam->positions.timestamps.offset_elements + sizeof(M2Array) > buffSize)
104 return false;
105 M2Array const* posTsArray = reinterpret_cast<M2Array const*>(buffer + cam->positions.timestamps.offset_elements);
106 if (posTsArray->offset_elements + sizeof(uint32) > buffSize || cam->positions.values.offset_elements + sizeof(M2Array) > buffSize)
107 return false;
108 uint32 const* posTimestamps = reinterpret_cast<uint32 const*>(buffer + posTsArray->offset_elements);
109 M2Array const* posArray = reinterpret_cast<M2Array const*>(buffer + cam->positions.values.offset_elements);
110 if (posArray->offset_elements + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
111 return false;
112 M2SplineKey<G3D::Vector3> const* positions = reinterpret_cast<M2SplineKey<G3D::Vector3> const*>(buffer + posArray->offset_elements);
113
114 // Read the data for this set
115 uint32 currPos = posArray->offset_elements;
116 for (uint32 i = 0; i < posTsArray->number; ++i)
117 {
118 if (currPos + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
119 return false;
120 // Translate co-ordinates
121 G3D::Vector3 newPos = translateLocation(&dbcData, &cam->position_base, &positions->p0);
122
123 // Add to vector
124 FlyByCamera thisCam;
125 thisCam.timeStamp = posTimestamps[i];
126 thisCam.locations.Relocate(newPos.x, newPos.y, newPos.z);
127
128 if (targetcam.size() > 0)
129 {
130 // Find the target camera before and after this camera
131 FlyByCamera lastTarget;
132 FlyByCamera nextTarget;
133
134 // Pre-load first item
135 lastTarget = targetcam[0];
136 nextTarget = targetcam[0];
137 for (uint32 j = 0; j < targetcam.size(); ++j)
138 {
139 nextTarget = targetcam[j];
140 if (targetcam[j].timeStamp > posTimestamps[i])
141 break;
142
143 lastTarget = targetcam[j];
144 }
145
146 float x = lastTarget.locations.GetPositionX();
147 float y = lastTarget.locations.GetPositionY();
148
149 // Now, the timestamps for target cam and position can be different. So, if they differ we interpolate
150 if (lastTarget.timeStamp != posTimestamps[i])
151 {
152 uint32 timeDiffTarget = nextTarget.timeStamp - lastTarget.timeStamp;
153 uint32 timeDiffThis = posTimestamps[i] - lastTarget.timeStamp;
154 float xDiff = nextTarget.locations.GetPositionX() - lastTarget.locations.GetPositionX();
155 float yDiff = nextTarget.locations.GetPositionY() - lastTarget.locations.GetPositionY();
156 x = lastTarget.locations.GetPositionX() + (xDiff * (float(timeDiffThis) / float(timeDiffTarget)));
157 y = lastTarget.locations.GetPositionY() + (yDiff * (float(timeDiffThis) / float(timeDiffTarget)));
158 }
159 float xDiff = x - thisCam.locations.GetPositionX();
160 float yDiff = y - thisCam.locations.GetPositionY();
161 thisCam.locations.SetOrientation(std::atan2(yDiff, xDiff));
162 }
163
164 cameras.push_back(thisCam);
165 positions++;
166 currPos += sizeof(M2SplineKey<G3D::Vector3>);
167 }
168 }
169
170 sFlyByCameraStore[dbcentry->ID] = cameras;
171 return true;
172}
173
174TC_GAME_API void LoadM2Cameras(std::string const& dataPath)
175{
176 sFlyByCameraStore.clear();
177 TC_LOG_INFO("server.loading", ">> Loading Cinematic Camera files");
178
179 boost::filesystem::path camerasPath = boost::filesystem::path(dataPath) / "cameras";
180
181 uint32 oldMSTime = getMSTime();
182 for (CinematicCameraEntry const* cameraEntry : sCinematicCameraStore)
183 {
184 boost::filesystem::path filename = camerasPath / Trinity::StringFormat("FILE{:08X}.xxx", cameraEntry->FileDataID);
185
186 // Convert to native format
187 filename.make_preferred();
188
189 std::ifstream m2file(filename.string().c_str(), std::ios::in | std::ios::binary);
190 if (!m2file.is_open())
191 continue;
192
193 // Get file size
194 m2file.seekg(0, std::ios::end);
195 std::streamoff fileSize = m2file.tellg();
196
197 // Reject if not at least the size of the header
198 if (static_cast<uint32>(fileSize) < sizeof(M2Header) + 4)
199 {
200 TC_LOG_ERROR("server.loading", "Camera file {} is damaged. File is smaller than header size", filename.string());
201 m2file.close();
202 continue;
203 }
204
205 // Read 4 bytes (signature)
206 m2file.seekg(0, std::ios::beg);
207 char fileCheck[5];
208 m2file.read(fileCheck, 4);
209 fileCheck[4] = '\0';
210
211 // Check file has correct magic (MD21)
212 if (strcmp(fileCheck, "MD21"))
213 {
214 TC_LOG_ERROR("server.loading", "Camera file {} is damaged. File identifier not found.", filename.string());
215 m2file.close();
216 continue;
217 }
218
219 // Now we have a good file, read it all into a vector of char's, then close the file.
220 std::vector<char> buffer(fileSize);
221 m2file.seekg(0, std::ios::beg);
222 if (!m2file.read(buffer.data(), fileSize))
223 {
224 m2file.close();
225 continue;
226 }
227 m2file.close();
228
229 bool fileValid = true;
230 uint32 m2start = 0;
231 char const* ptr = buffer.data();
232 while (m2start + 4 < buffer.size() && memcmp(ptr, "MD20", 4) != 0)
233 {
234 ++m2start;
235 ++ptr;
236 if (m2start + sizeof(M2Header) > buffer.size())
237 {
238 fileValid = false;
239 break;
240 }
241 }
242
243 if (!fileValid)
244 {
245 TC_LOG_ERROR("server.loading", "Camera file {} is damaged. File is smaller than header size.", filename.string());
246 continue;
247 }
248
249 // Read header
250 M2Header const* header = reinterpret_cast<M2Header const*>(buffer.data() + m2start);
251
252 if (m2start + header->ofsCameras + sizeof(M2Camera) > static_cast<uint32>(fileSize))
253 {
254 TC_LOG_ERROR("server.loading", "Camera file {} is damaged. Camera references position beyond file end", filename.string());
255 continue;
256 }
257
258 // Get camera(s) - Main header, then dump them.
259 M2Camera const* cam = reinterpret_cast<M2Camera const*>(buffer.data() + m2start + header->ofsCameras);
260 if (!readCamera(cam, fileSize - m2start, header, cameraEntry))
261 TC_LOG_ERROR("server.loading", "Camera file {} is damaged. Camera references position beyond file end", filename.string());
262 }
263
264 TC_LOG_INFO("server.loading", ">> Loaded {} cinematic waypoint sets in {} ms", sFlyByCameraStore.size(), GetMSTimeDiffToNow(oldMSTime));
265}
266
267std::vector<FlyByCamera> const* GetFlyByCameras(uint32 cinematicCameraId)
268{
270}
#define M_PI
Definition: Common.h:115
DB2Storage< CinematicCameraEntry > sCinematicCameraStore("CinematicCamera.db2", &CinematicCameraLoadInfo::Instance)
#define TC_GAME_API
Definition: Define.h:123
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
std::vector< FlyByCamera > FlyByCameraCollection
Definition: M2Stores.cpp:28
std::unordered_map< uint32, FlyByCameraCollection > sFlyByCameraStore
Definition: M2Stores.cpp:29
G3D::Vector3 translateLocation(G3D::Vector4 const *dbcLocation, G3D::Vector3 const *basePosition, G3D::Vector3 const *splineVector)
Definition: M2Stores.cpp:32
bool readCamera(M2Camera const *cam, uint32 buffSize, M2Header const *header, CinematicCameraEntry const *dbcentry)
Definition: M2Stores.cpp:51
std::vector< FlyByCamera > const * GetFlyByCameras(uint32 cinematicCameraId)
Definition: M2Stores.cpp:267
TC_GAME_API void LoadM2Cameras(std::string const &dataPath)
Definition: M2Stores.cpp:174
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
Definition: StringFormat.h:38
DBCPosition3D Origin
Definition: DB2Structure.h:884
uint32 timeStamp
Definition: M2Stores.h:27
Position locations
Definition: M2Stores.h:28
uint32 offset_elements
Definition: M2Structure.h:112
uint32_t number
Definition: M2Structure.h:111
M2Track target_positions
Definition: M2Structure.h:129
G3D::Vector3 target_position_base
Definition: M2Structure.h:130
M2Track positions
Definition: M2Structure.h:127
G3D::Vector3 position_base
Definition: M2Structure.h:128
uint32 ofsCameras
Definition: M2Structure.h:98
M2Array timestamps
Definition: M2Structure.h:118
M2Array values
Definition: M2Structure.h:119
constexpr void SetOrientation(float orientation)
Definition: Position.h:71
constexpr float GetPositionX() const
Definition: Position.h:76
constexpr float GetPositionY() const
Definition: Position.h:77
constexpr void Relocate(float x, float y)
Definition: Position.h:63