-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathReplay.cpp
281 lines (224 loc) · 5.45 KB
/
Replay.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
/*
** RLReplayManager
**
** Copyright (C) 2015 Tobias Taschner <[email protected]>
**
** Licensed under GPL v3 or later
*/
#include <wx/wxprec.h>
#include "Replay.h"
#include <wx/log.h>
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/zipstrm.h>
#include <wx/fileconf.h>
#include <wx/stdpaths.h>
//
// ReplayProperties
//
ReplayProperties::ReplayProperties()
{
}
ReplayProperties::ReplayProperties(wxInputStream& istr)
{
Load(istr);
}
void ReplayProperties::Load(wxInputStream& istr)
{
while (istr.IsOk())
{
wxString propertyName = ReadString(istr);
if (propertyName.IsSameAs("None"))
break;
wxString propertyType = ReadString(istr);
wxUint64 propertySize;
istr.Read(&propertySize, sizeof(propertySize));
if (propertyType.IsSameAs("IntProperty"))
{
wxUint32 value;
istr.Read(&value, sizeof(value));
(*this)[propertyName] = value;
}
else if (propertyType.IsSameAs("StrProperty")
|| propertyType.IsSameAs("NameProperty"))
{
(*this)[propertyName] = ReadString(istr);
}
else if (propertyType.IsSameAs("FloatProperty"))
{
wxFloat32 value;
istr.Read(&value, sizeof(value));
(*this)[propertyName] = value;
}
else if (propertyType.IsSameAs("ArrayProperty"))
{
wxUint32 entryCount;
istr.Read(&entryCount, sizeof(entryCount));
List arrayValues;
for (wxUint32 i = 0; i < entryCount; i++)
{
ReplayProperties subProps;
subProps.Load(istr);
arrayValues.push_back(subProps);
}
(*this)[propertyName] = arrayValues;
}
else if (propertyType.IsSameAs("ByteProperty"))
{
// Description
wxString desc = ReadString(istr);
// Skip length
istr.SeekI(propertySize, wxFromCurrent);
}
else if (propertyType.IsSameAs("QWordProperty"))
{
wxUint64 value;
istr.Read(&value, sizeof(value));
(*this)[propertyName] = value;
}
else if (propertyType.IsSameAs("BoolProperty"))
{
wxByte boolVal;
istr.Read(&boolVal, sizeof(boolVal));
(*this)[propertyName] = (bool) boolVal;
}
else
{
wxLogError(_("Unknown property type: %s"), propertyType);
break;
}
}
}
wxString ReplayProperties::ReadString(wxInputStream& istr)
{
wxInt32 len;
istr.Read(&len, sizeof(len));
if (len == 0)
return wxString();
if (len < 0)
{
// Unicode string
len = abs(len);
wxWCharBuffer chBuf(len - 1);
istr.Read(chBuf.data(), (len - 1) * 2);
istr.SeekI(2, wxFromCurrent); // Skip trailing zero
return chBuf;
}
else
{
wxCharBuffer chBuf(len - 1);
istr.Read(chBuf.data(), len - 1);
istr.SeekI(1, wxFromCurrent); // Skip trailing zero
return chBuf;
}
}
//
// Replay
//
std::map<wxString, wxString> Replay::ms_mapNames;
Replay::Replay(const wxString& filename)
{
Load(filename);
}
void Replay::Load(const wxString& filename)
{
m_filename = filename;
wxFileName fn(filename);
wxFileInputStream istr(filename);
wxUint32 val1;
istr.Read(&val1, sizeof(val1));
wxUint32 val2;
istr.Read(&val2, sizeof(val2));
wxUint32 val3;
istr.Read(&val3, sizeof(val3));
wxUint32 val4;
istr.Read(&val4, sizeof(val4));
wxString gameId = ReadString(istr);
if (!gameId.IsSameAs("TAGame.Replay_Soccar_TA"))
{
wxLogError(_("Unsupported game type: %s"), gameId);
return;
}
ReplayProperties::Load(istr);
m_description = (*this)["Id"].As<wxString>();
m_date.ParseFormat((*this)["Date"].As<wxString>(), wxS("%Y-%m-%d:%H-%M"));
m_fps = (*this)["RecordFPS"].As<wxFloat32>();
if (find("NumFrames") != end())
m_length = ConvertFrames((*this)["NumFrames"].As<wxUint32>());
else
m_length = 0;
}
wxDateTime Replay::ConvertFrames(wxUint32 frames) const
{
wxDouble seconds = frames / m_fps;
wxUint32 minutes = seconds / 60;
wxUint32 hours = 0;
if (minutes > 60)
{
hours = minutes / 60;
minutes %= 60;
}
return wxDateTime(hours, minutes, (int)seconds % 60, 0);
}
wxString Replay::GetExportBaseName()
{
wxString str = "Replay_";
str += (*this)["PlayerName"].As<wxString>();
str += "_";
int teamSize = (*this)["TeamSize"].As<wxUint32>();
if (teamSize == 1)
str += "Duel";
else
str += wxString::Format("%don%d", teamSize, teamSize);
str += "_";
str += m_date.Format("%Y-%m-%d_%H-%M");
return str;
}
wxString Replay::GetExportFileName()
{
return GetExportBaseName() + ".zip";
}
int Replay::GetGoalCount()
{
int goals = 0;
if (find("Team0Score") != end())
goals += (*this)["Team0Score"].As<wxUint32>();
if (find("Team1Score") != end())
goals += (*this)["Team1Score"].As<wxUint32>();
return goals;
}
void Replay::Export(const wxString& filename)
{
wxFileInputStream istr(m_filename);
wxFileOutputStream ostr(filename);
wxZipOutputStream zipstr(ostr);
wxString replayPath = "My Games/Rocket League/TAGame/Demos/";
zipstr.PutNextEntry(replayPath + GetExportBaseName() + ".replay", m_date);
istr.Read(zipstr);
zipstr.CloseEntry();
}
wxString Replay::GetMapDisplayName()
{
if (find("MapName") == end())
return _("Unknown");
wxString mapId = (*this)["MapName"].As<wxString>();
if (ms_mapNames.empty())
{
wxFileName cfgName(wxStandardPaths::Get().GetResourcesDir(), "RLReplayManager.cfg");
wxFileConfig cfg(wxEmptyString, wxEmptyString, wxEmptyString, cfgName.GetFullPath());
cfg.SetPath("/MapNames");
wxString entry;
long entryIndex = 0;
bool moreEntries = cfg.GetFirstEntry(entry, entryIndex);
while (moreEntries)
{
ms_mapNames[entry] = cfg.Read(entry);
moreEntries = cfg.GetNextEntry(entry, entryIndex);
}
}
auto mapDesc = ms_mapNames.find(mapId.Lower());
if (mapDesc != ms_mapNames.end())
return mapDesc->second;
else
return mapId;
}