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
|
/*
* This file is part of the TrinityCore 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, see <http://www.gnu.org/licenses/>.
*/
#include "M2Stores.h"
#include "DB2Stores.h"
#include "Log.h"
#include "M2Structure.h"
#include "MapUtils.h"
#include "Timer.h"
#include <G3D/Vector4.h>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <fstream>
typedef std::vector<FlyByCamera> FlyByCameraCollection;
std::unordered_map<uint32, FlyByCameraCollection> sFlyByCameraStore;
// Convert the geomoetry from a spline value, to an actual WoW XYZ
G3D::Vector3 translateLocation(G3D::Vector4 const* dbcLocation, G3D::Vector3 const* basePosition, G3D::Vector3 const* splineVector)
{
G3D::Vector3 work;
float x = basePosition->x + splineVector->x;
float y = basePosition->y + splineVector->y;
float z = basePosition->z + splineVector->z;
float const distance = sqrt((x * x) + (y * y));
float angle = std::atan2(x, y) - dbcLocation->w;
if (angle < 0)
angle += 2 * float(M_PI);
work.x = dbcLocation->x + (distance * sin(angle));
work.y = dbcLocation->y + (distance * cos(angle));
work.z = dbcLocation->z + z;
return work;
}
// Number of cameras not used. Multiple cameras never used in 7.1.5
bool readCamera(M2Camera const* cam, uint32 buffSize, M2Header const* header, CinematicCameraEntry const* dbcentry)
{
char const* buffer = reinterpret_cast<char const*>(header);
FlyByCameraCollection cameras;
FlyByCameraCollection targetcam;
G3D::Vector4 dbcData;
dbcData.x = dbcentry->Origin.X;
dbcData.y = dbcentry->Origin.Y;
dbcData.z = dbcentry->Origin.Z;
dbcData.w = dbcentry->OriginFacing;
// Read target locations, only so that we can calculate orientation
for (uint32 k = 0; k < cam->target_positions.timestamps.number; ++k)
{
// Extract Target positions
if (cam->target_positions.timestamps.offset_elements + sizeof(M2Array) > buffSize)
return false;
M2Array const* targTsArray = reinterpret_cast<M2Array const*>(buffer + cam->target_positions.timestamps.offset_elements);
if (targTsArray->offset_elements + sizeof(uint32) > buffSize || cam->target_positions.values.offset_elements + sizeof(M2Array) > buffSize)
return false;
uint32 const* targTimestamps = reinterpret_cast<uint32 const*>(buffer + targTsArray->offset_elements);
M2Array const* targArray = reinterpret_cast<M2Array const*>(buffer + cam->target_positions.values.offset_elements);
if (targArray->offset_elements + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
return false;
M2SplineKey<G3D::Vector3> const* targPositions = reinterpret_cast<M2SplineKey<G3D::Vector3> const*>(buffer + targArray->offset_elements);
// Read the data for this set
uint32 currPos = targArray->offset_elements;
for (uint32 i = 0; i < targTsArray->number; ++i)
{
if (currPos + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
return false;
// Translate co-ordinates
G3D::Vector3 newPos = translateLocation(&dbcData, &cam->target_position_base, &targPositions->p0);
// Add to vector
FlyByCamera thisCam;
thisCam.timeStamp = targTimestamps[i];
thisCam.locations.Relocate(newPos.x, newPos.y, newPos.z, 0.0f);
targetcam.push_back(thisCam);
targPositions++;
currPos += sizeof(M2SplineKey<G3D::Vector3>);
}
}
// Read camera positions and timestamps (translating first position of 3 only, we don't need to translate the whole spline)
for (uint32 k = 0; k < cam->positions.timestamps.number; ++k)
{
// Extract Camera positions for this set
if (cam->positions.timestamps.offset_elements + sizeof(M2Array) > buffSize)
return false;
M2Array const* posTsArray = reinterpret_cast<M2Array const*>(buffer + cam->positions.timestamps.offset_elements);
if (posTsArray->offset_elements + sizeof(uint32) > buffSize || cam->positions.values.offset_elements + sizeof(M2Array) > buffSize)
return false;
uint32 const* posTimestamps = reinterpret_cast<uint32 const*>(buffer + posTsArray->offset_elements);
M2Array const* posArray = reinterpret_cast<M2Array const*>(buffer + cam->positions.values.offset_elements);
if (posArray->offset_elements + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
return false;
M2SplineKey<G3D::Vector3> const* positions = reinterpret_cast<M2SplineKey<G3D::Vector3> const*>(buffer + posArray->offset_elements);
// Read the data for this set
uint32 currPos = posArray->offset_elements;
for (uint32 i = 0; i < posTsArray->number; ++i)
{
if (currPos + sizeof(M2SplineKey<G3D::Vector3>) > buffSize)
return false;
// Translate co-ordinates
G3D::Vector3 newPos = translateLocation(&dbcData, &cam->position_base, &positions->p0);
// Add to vector
FlyByCamera thisCam;
thisCam.timeStamp = posTimestamps[i];
thisCam.locations.Relocate(newPos.x, newPos.y, newPos.z);
if (targetcam.size() > 0)
{
// Find the target camera before and after this camera
FlyByCamera lastTarget;
FlyByCamera nextTarget;
// Pre-load first item
lastTarget = targetcam[0];
nextTarget = targetcam[0];
for (uint32 j = 0; j < targetcam.size(); ++j)
{
nextTarget = targetcam[j];
if (targetcam[j].timeStamp > posTimestamps[i])
break;
lastTarget = targetcam[j];
}
float x = lastTarget.locations.GetPositionX();
float y = lastTarget.locations.GetPositionY();
// Now, the timestamps for target cam and position can be different. So, if they differ we interpolate
if (lastTarget.timeStamp != posTimestamps[i])
{
uint32 timeDiffTarget = nextTarget.timeStamp - lastTarget.timeStamp;
uint32 timeDiffThis = posTimestamps[i] - lastTarget.timeStamp;
float xDiff = nextTarget.locations.GetPositionX() - lastTarget.locations.GetPositionX();
float yDiff = nextTarget.locations.GetPositionY() - lastTarget.locations.GetPositionY();
x = lastTarget.locations.GetPositionX() + (xDiff * (float(timeDiffThis) / float(timeDiffTarget)));
y = lastTarget.locations.GetPositionY() + (yDiff * (float(timeDiffThis) / float(timeDiffTarget)));
}
float xDiff = x - thisCam.locations.GetPositionX();
float yDiff = y - thisCam.locations.GetPositionY();
thisCam.locations.SetOrientation(std::atan2(yDiff, xDiff));
}
cameras.push_back(thisCam);
positions++;
currPos += sizeof(M2SplineKey<G3D::Vector3>);
}
}
sFlyByCameraStore[dbcentry->ID] = cameras;
return true;
}
TC_GAME_API void LoadM2Cameras(std::string const& dataPath)
{
sFlyByCameraStore.clear();
TC_LOG_INFO("server.loading", ">> Loading Cinematic Camera files");
boost::filesystem::path camerasPath = boost::filesystem::path(dataPath) / "cameras";
uint32 oldMSTime = getMSTime();
for (CinematicCameraEntry const* cameraEntry : sCinematicCameraStore)
{
boost::filesystem::path filename = camerasPath / Trinity::StringFormat("FILE{:08X}.xxx", cameraEntry->FileDataID);
// Convert to native format
filename.make_preferred();
std::ifstream m2file(filename.string().c_str(), std::ios::in | std::ios::binary);
if (!m2file.is_open())
continue;
// Get file size
std::streamoff fileSize = boost::filesystem::file_size(filename);
// Reject if not at least the size of the header
if (static_cast<uint32>(fileSize) < sizeof(M2Header) + 4)
{
TC_LOG_ERROR("server.loading", "Camera file {} is damaged. File is smaller than header size", filename.string());
m2file.close();
continue;
}
// Read 4 bytes (signature)
char fileCheck[5];
m2file.read(fileCheck, 4);
fileCheck[4] = '\0';
// Check file has correct magic (MD21)
if (strcmp(fileCheck, "MD21"))
{
TC_LOG_ERROR("server.loading", "Camera file {} is damaged. File identifier not found.", filename.string());
m2file.close();
continue;
}
// Now we have a good file, read it all into a vector of char's, then close the file.
std::vector<char> buffer(fileSize);
m2file.seekg(0, std::ios::beg);
if (!m2file.read(buffer.data(), fileSize))
{
m2file.close();
continue;
}
m2file.close();
bool fileValid = true;
uint32 m2start = 0;
char const* ptr = buffer.data();
while (m2start + 4 < buffer.size() && memcmp(ptr, "MD20", 4) != 0)
{
++m2start;
++ptr;
if (m2start + sizeof(M2Header) > buffer.size())
{
fileValid = false;
break;
}
}
if (!fileValid)
{
TC_LOG_ERROR("server.loading", "Camera file {} is damaged. File is smaller than header size.", filename.string());
continue;
}
// Read header
M2Header const* header = reinterpret_cast<M2Header const*>(buffer.data() + m2start);
if (m2start + header->ofsCameras + sizeof(M2Camera) > static_cast<uint32>(fileSize))
{
TC_LOG_ERROR("server.loading", "Camera file {} is damaged. Camera references position beyond file end", filename.string());
continue;
}
// Get camera(s) - Main header, then dump them.
M2Camera const* cam = reinterpret_cast<M2Camera const*>(buffer.data() + m2start + header->ofsCameras);
if (!readCamera(cam, fileSize - m2start, header, cameraEntry))
TC_LOG_ERROR("server.loading", "Camera file {} is damaged. Camera references position beyond file end", filename.string());
}
TC_LOG_INFO("server.loading", ">> Loaded {} cinematic waypoint sets in {} ms", sFlyByCameraStore.size(), GetMSTimeDiffToNow(oldMSTime));
}
std::vector<FlyByCamera> const* GetFlyByCameras(uint32 cinematicCameraId)
{
return Trinity::Containers::MapGetValuePtr(sFlyByCameraStore, cinematicCameraId);
}
|