-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathsteam_timeline.cpp
535 lines (413 loc) · 19.2 KB
/
steam_timeline.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* Copyright (C) 2019 Mr Goldberg
This file is part of the Goldberg Emulator
The Goldberg Emulator is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
The Goldberg Emulator 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the Goldberg Emulator; if not, see
<http://www.gnu.org/licenses/>. */
#include "dll/steam_timeline.h"
const std::chrono::system_clock::time_point& Steam_Timeline::TimelineEvent_t::get_time_added() const
{
return time_added;
}
const std::chrono::system_clock::time_point& Steam_Timeline::TimelineState_t::get_time_added() const
{
return time_added;
}
const std::chrono::system_clock::time_point& Steam_Timeline::TimelineGamePhase_t::get_time_added() const
{
return time_added;
}
void Steam_Timeline::steam_callback(void *object, Common_Message *msg)
{
// PRINT_DEBUG_ENTRY();
auto instance = (Steam_Timeline *)object;
instance->Callback(msg);
}
void Steam_Timeline::steam_run_every_runcb(void *object)
{
// PRINT_DEBUG_ENTRY();
auto instance = (Steam_Timeline *)object;
instance->RunCallbacks();
}
Steam_Timeline::Steam_Timeline(class Settings *settings, class Networking *network, class SteamCallResults *callback_results, class SteamCallBacks *callbacks, class RunEveryRunCB *run_every_runcb)
{
this->settings = settings;
this->network = network;
this->callback_results = callback_results;
this->callbacks = callbacks;
this->run_every_runcb = run_every_runcb;
// this->network->setCallback(CALLBACK_ID_USER_STATUS, settings->get_local_steam_id(), &Steam_Timeline::steam_callback, this);
this->run_every_runcb->add(&Steam_Timeline::steam_run_every_runcb, this);
// timeline starts with a default event as seen here: https://www.youtube.com/watch?v=YwBD0E4-EsI
PRINT_DEBUG("adding an initial game mode");
SetTimelineGameMode(ETimelineGameMode::k_ETimelineGameMode_Invalid);
}
Steam_Timeline::~Steam_Timeline()
{
// this->network->rmCallback(CALLBACK_ID_USER_STATUS, settings->get_local_steam_id(), &Steam_Timeline::steam_callback, this);
this->run_every_runcb->remove(&Steam_Timeline::steam_run_every_runcb, this);
}
// Sets a description for the current game state in the timeline. These help the user to find specific
// moments in the timeline when saving clips. Setting a new state description replaces any previous
// description.
//
// Examples could include:
// * Where the user is in the world in a single player game
// * Which round is happening in a multiplayer game
// * The current score for a sports game
//
// Parameters:
// - pchDescription: provide a localized string in the language returned by SteamUtils()->GetSteamUILanguage()
// - flTimeDelta: The time offset in seconds to apply to this event. Negative times indicate an
// event that happened in the past.
void Steam_Timeline::SetTimelineTooltip( const char *pchDescription, float flTimeDelta )
{
PRINT_DEBUG("'%s' %f", pchDescription, flTimeDelta);
std::lock_guard lock(timeline_mutex);
const auto target_timepoint = std::chrono::system_clock::now() + std::chrono::milliseconds(static_cast<long long>(flTimeDelta * 1000));
// reverse iterators to find last/nearest match in recent time
auto event_it = std::find_if(timeline_states.rbegin(), timeline_states.rend(), [this, &target_timepoint](const TimelineState_t &item) {
return target_timepoint >= item.get_time_added();
});
if (timeline_states.rend() != event_it) {
PRINT_DEBUG("setting timeline state description");
if (pchDescription) {
event_it->description = pchDescription;
} else {
event_it->description.clear();
}
}
}
void Steam_Timeline::ClearTimelineTooltip( float flTimeDelta )
{
PRINT_DEBUG("%f", flTimeDelta);
std::lock_guard lock(timeline_mutex);
const auto target_timepoint = std::chrono::system_clock::now() + std::chrono::milliseconds(static_cast<long long>(flTimeDelta * 1000));
// reverse iterators to find last/nearest match in recent time
auto event_it = std::find_if(timeline_states.rbegin(), timeline_states.rend(), [this, &target_timepoint](const TimelineState_t &item) {
return target_timepoint >= item.get_time_added();
});
if (timeline_states.rend() != event_it) {
PRINT_DEBUG("clearing timeline state description");
event_it->description.clear();
}
}
void Steam_Timeline::SetTimelineStateDescription( const char *pchDescription, float flTimeDelta )
{
PRINT_DEBUG("old v1");
SetTimelineTooltip(pchDescription, flTimeDelta);
}
void Steam_Timeline::ClearTimelineStateDescription( float flTimeDelta )
{
PRINT_DEBUG("old v1");
ClearTimelineTooltip(flTimeDelta);
}
void Steam_Timeline::SetTimelineGameMode( ETimelineGameMode eMode )
{
PRINT_DEBUG("%i", (int)eMode);
std::lock_guard lock(timeline_mutex);
auto &new_timeline_state = timeline_states.emplace_back(TimelineState_t{});
new_timeline_state.bar_color = eMode;
}
TimelineEventHandle_t Steam_Timeline::AddInstantaneousTimelineEvent( const char *pchTitle, const char *pchDescription, const char *pchIcon, uint32 unIconPriority, float flStartOffsetSeconds, ETimelineEventClipPriority ePossibleClip )
{
PRINT_DEBUG_TODO();
return AddRangeTimelineEvent(pchTitle, pchDescription, pchIcon, unIconPriority, flStartOffsetSeconds, 0, ePossibleClip);
}
TimelineEventHandle_t Steam_Timeline::AddRangeTimelineEvent( const char *pchTitle, const char *pchDescription, const char *pchIcon, uint32 unIconPriority, float flStartOffsetSeconds, float flDuration, ETimelineEventClipPriority ePossibleClip)
{
PRINT_DEBUG("'%s' ('%s') icon='%s', %u, [%f, %f) %i", pchTitle, pchDescription, pchIcon, unIconPriority, flStartOffsetSeconds, flDuration, (int)ePossibleClip);
std::lock_guard lock(timeline_mutex);
auto event_id = StartRangeTimelineEvent(pchTitle, pchDescription, pchIcon, unIconPriority, flStartOffsetSeconds, ePossibleClip);
if (!event_id || event_id > timeline_events.size()) return 0;
auto& my_event = timeline_events[static_cast<size_t>(event_id - 1)];
my_event.ended = true; // ranged and instantaneous events are ended/closed events, they can't be modified later according to docs
// make events last at least 1 sec
if (static_cast<long long>(flDuration * 1000) < 1000LL) { // < 1000ms
flDuration = 1;
}
// for events with priority=ETimelineEventClipPriority::k_ETimelineEventClipPriority_Featured steam creates ~30 sec clip
if (flDuration < PRIORITY_CLIP_MIN_SEC && ePossibleClip == ETimelineEventClipPriority::k_ETimelineEventClipPriority_Featured) {
flDuration = PRIORITY_CLIP_MIN_SEC;
}
if (flDuration > k_flMaxTimelineEventDuration) {
flDuration = k_flMaxTimelineEventDuration;
}
my_event.flDurationSeconds = flDuration;
return event_id;
}
TimelineEventHandle_t Steam_Timeline::AddTimelineEvent( const char *pchTitle, const char *pchDescription, const char *pchIcon, uint32 unIconPriority, float flStartOffsetSeconds, float flDurationSeconds, ETimelineEventClipPriority ePossibleClip )
{
PRINT_DEBUG("undocumented v2/v3");
// this is how actual steamclient64.dll implements it
if (flDurationSeconds > 0) {
return AddRangeTimelineEvent(pchTitle, pchDescription, pchIcon, unIconPriority, flStartOffsetSeconds, flDurationSeconds, ePossibleClip);
} else {
return AddInstantaneousTimelineEvent(pchTitle, pchDescription, pchIcon, unIconPriority, flStartOffsetSeconds, ePossibleClip);
}
}
// Use this to mark an event on the Timeline. The event can be instantaneous or take some amount of time
// to complete, depending on the value passed in flDurationSeconds
//
// Examples could include:
// * a boss battle
// * a cut scene
// * a large team fight
// * picking up a new weapon or ammunition
// * scoring a goal
//
// Parameters:
//
// - pchIcon: specify the name of the icon uploaded through the Steamworks Partner Site for your title
// or one of the provided icons that start with steam_
// - pchTitle & pchDescription: provide a localized string in the language returned by
// SteamUtils()->GetSteamUILanguage()
// - unPriority: specify how important this range is compared to other markers provided by the game.
// Ranges with larger priority values will be displayed more prominently in the UI. This value
// may be between 0 and k_unMaxTimelinePriority.
// - flStartOffsetSeconds: The time that this range started relative to now. Negative times
// indicate an event that happened in the past.
// - flDurationSeconds: How long the time range should be in seconds. For instantaneous events, this
// should be 0
// - ePossibleClip: By setting this parameter to Featured or Standard, the game indicates to Steam that it
// would be appropriate to offer this range as a clip to the user. For instantaneous events, the
// suggested clip will be for a short time before and after the event itself.
void Steam_Timeline::AddTimelineEvent_old( const char *pchIcon, const char *pchTitle, const char *pchDescription, uint32 unPriority, float flStartOffsetSeconds, float flDurationSeconds, ETimelineEventClipPriority ePossibleClip )
{
PRINT_DEBUG("old v1");
// this is how actual steamclient64.dll implements it
if (flDurationSeconds > 0) {
AddRangeTimelineEvent(pchTitle, pchDescription, pchIcon, unPriority, flStartOffsetSeconds, flDurationSeconds, ePossibleClip);
} else {
AddInstantaneousTimelineEvent(pchTitle, pchDescription, pchIcon, unPriority, flStartOffsetSeconds, ePossibleClip);
}
}
TimelineEventHandle_t Steam_Timeline::StartRangeTimelineEvent( const char *pchTitle, const char *pchDescription, const char *pchIcon, uint32 unPriority, float flStartOffsetSeconds, ETimelineEventClipPriority ePossibleClip )
{
PRINT_DEBUG("'%s' ('%s') icon='%s', %u, @[%f]sec %i", pchTitle, pchDescription, pchIcon, unPriority, flStartOffsetSeconds, (int)ePossibleClip);
std::lock_guard lock(timeline_mutex);
// this adds a new event, but the duration is set once EndRangeTimelineEvent is called
// also its "ended" flag is not set
auto &new_event = timeline_events.emplace_back(TimelineEvent_t{});
new_event.pchTitle = pchTitle ? pchTitle : "";
new_event.pchDescription = pchDescription ? pchDescription : "";
new_event.pchIcon = pchIcon ? pchIcon : "";
new_event.unPriority = unPriority;
new_event.flStartOffsetSeconds = flStartOffsetSeconds;
new_event.ePossibleClip = ePossibleClip;
auto new_event_id = timeline_events.size(); // never return 0, most APIs in other interfaces use it for invalid IDs
PRINT_DEBUG(" new event ID = [%zu]", new_event_id);
return static_cast<TimelineEventHandle_t>(new_event_id);
}
void Steam_Timeline::UpdateRangeTimelineEvent( TimelineEventHandle_t ulEvent, const char *pchTitle, const char *pchDescription, const char *pchIcon, uint32 unPriority, ETimelineEventClipPriority ePossibleClip )
{
PRINT_DEBUG("[%llu] '%s' ('%s') | icon='%s', %u, %i", ulEvent, pchTitle, pchDescription, pchIcon, unPriority, (int)ePossibleClip);
std::lock_guard lock(timeline_mutex);
if (!ulEvent || ulEvent > timeline_events.size()) return;
auto& my_event = timeline_events[static_cast<size_t>(ulEvent - 1)];
if (my_event.ended) return;
if (pchTitle) {
my_event.pchTitle = pchTitle;
} else {
my_event.pchTitle.clear();
}
if (pchDescription) {
my_event.pchDescription = pchDescription;
} else {
my_event.pchDescription.clear();
}
if (pchIcon) {
my_event.pchIcon = pchIcon;
} else {
my_event.pchIcon.clear();
}
my_event.unPriority = unPriority;
my_event.ePossibleClip = ePossibleClip;
PRINT_DEBUG(" updated event");
}
void Steam_Timeline::EndRangeTimelineEvent( TimelineEventHandle_t ulEvent, float flEndOffsetSeconds )
{
PRINT_DEBUG("[%llu] %f", ulEvent, flEndOffsetSeconds);
std::lock_guard lock(timeline_mutex);
if (!ulEvent || ulEvent > timeline_events.size()) return;
auto& my_event = timeline_events[static_cast<size_t>(ulEvent - 1)];
if (my_event.ended) return;
my_event.ended = true;
auto end_timepoint = std::chrono::system_clock::now();
auto start_timepoint = my_event.get_time_added() + std::chrono::milliseconds(static_cast<long long>(my_event.flStartOffsetSeconds * 1000));
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_timepoint - start_timepoint);
my_event.flDurationSeconds = duration_ms.count() / 1000.0f;
PRINT_DEBUG(" ended event // TODO show in the UI");
}
void Steam_Timeline::RemoveTimelineEvent( TimelineEventHandle_t ulEvent )
{
PRINT_DEBUG("[%llu]", ulEvent);
std::lock_guard lock(timeline_mutex);
if (!ulEvent || ulEvent > timeline_events.size()) return;
timeline_events.erase(timeline_events.begin() + static_cast<size_t>(ulEvent - 1));
PRINT_DEBUG(" removed event // TODO remove from the UI");
}
STEAM_CALL_RESULT( SteamTimelineEventRecordingExists_t )
SteamAPICall_t Steam_Timeline::DoesEventRecordingExist(TimelineEventHandle_t ulEvent)
{
PRINT_DEBUG("[%llu] // TODO", ulEvent);
std::lock_guard lock(timeline_mutex);
if (!ulEvent || ulEvent > timeline_events.size()) {
SteamTimelineEventRecordingExists_t data_invalid{};
data_invalid.m_bRecordingExists = false;
data_invalid.m_ulEventID = ulEvent;
auto ret = callback_results->addCallResult(data_invalid.k_iCallback, &data_invalid, sizeof(data_invalid));
callbacks->addCBResult(data_invalid.k_iCallback, &data_invalid, sizeof(data_invalid));
return ret;
}
auto& my_event = timeline_events[static_cast<size_t>(ulEvent - 1)];
auto recordings_count = my_event.recordings.size();
SteamTimelineEventRecordingExists_t data{};
data.m_bRecordingExists = !my_event.recordings.empty();
data.m_ulEventID = ulEvent;
auto ret = callback_results->addCallResult(data.k_iCallback, &data, sizeof(data));
callbacks->addCBResult(data.k_iCallback, &data, sizeof(data));
return ret;
}
void Steam_Timeline::StartGamePhase()
{
PRINT_DEBUG_ENTRY();
std::lock_guard lock(timeline_mutex);
timeline_game_phases.emplace_back(TimelineGamePhase_t{});
}
void Steam_Timeline::EndGamePhase()
{
PRINT_DEBUG_ENTRY();
std::lock_guard lock(timeline_mutex);
if (timeline_game_phases.empty()) return;
auto &last_game_phase = timeline_game_phases.back();
last_game_phase.ended = true;
}
void Steam_Timeline::SetGamePhaseID( const char *pchPhaseID )
{
PRINT_DEBUG("['%s']", pchPhaseID);
std::lock_guard lock(timeline_mutex);
if (timeline_game_phases.empty()) return;
auto &last_game_phase = timeline_game_phases.back();
if (last_game_phase.ended) return;
last_game_phase.pchPhaseID = pchPhaseID ? pchPhaseID : "";
PRINT_DEBUG(" changed phase ID");
}
STEAM_CALL_RESULT( SteamTimelineGamePhaseRecordingExists_t )
SteamAPICall_t Steam_Timeline::DoesGamePhaseRecordingExist( const char *pchPhaseID )
{
PRINT_DEBUG("'%s' // TODO", pchPhaseID);
std::lock_guard lock(timeline_mutex);
if (!pchPhaseID) pchPhaseID = "";
std::string_view game_phase_id_view(pchPhaseID);
const auto trigger_failure = [game_phase_id_view, this]() {
SteamTimelineGamePhaseRecordingExists_t data_invalid{};
auto chars_copied = game_phase_id_view.copy(data_invalid.m_rgchPhaseID, sizeof(data_invalid.m_rgchPhaseID) - 1);
data_invalid.m_rgchPhaseID[chars_copied] = 0;
data_invalid.m_ulLongestClipMS = 0;
data_invalid.m_ulRecordingMS = 0;
data_invalid.m_unClipCount = 0;
data_invalid.m_unScreenshotCount = 0;
auto ret = callback_results->addCallResult(data_invalid.k_iCallback, &data_invalid, sizeof(data_invalid));
callbacks->addCBResult(data_invalid.k_iCallback, &data_invalid, sizeof(data_invalid));
return ret;
};
if (timeline_game_phases.empty()) {
return trigger_failure();
}
auto phase_it = std::find_if(timeline_game_phases.begin(), timeline_game_phases.end(), [game_phase_id_view](const TimelineGamePhase_t &item){
return game_phase_id_view == item.pchPhaseID;
});
if (timeline_game_phases.end() == phase_it) {
return trigger_failure();
}
// TODO return actual count ?
auto recordings_count = phase_it->recordings.size();
return trigger_failure();
}
void Steam_Timeline::AddGamePhaseTag( const char *pchTagName, const char *pchTagIcon, const char *pchTagGroup, uint32 unPriority )
{
PRINT_DEBUG("['%s']: '%s' '%s' <%u>", pchTagGroup, pchTagName, pchTagIcon, unPriority);
std::lock_guard lock(timeline_mutex);
if (timeline_game_phases.empty()) return;
auto &last_game_phase = timeline_game_phases.back();
if (last_game_phase.ended) return;
if (!pchTagGroup) pchTagGroup = "";
auto &phase_tag = last_game_phase.tags[pchTagGroup].emplace_back(Steam_Timeline::TimelineGamePhase_t::Tag_t{});
phase_tag.pchTagName = pchTagName ? pchTagName : "";
phase_tag.pchTagIcon = pchTagIcon ? pchTagIcon : "";
phase_tag.unPriority = unPriority;
PRINT_DEBUG(" added phase tag");
}
void Steam_Timeline::SetGamePhaseAttribute( const char *pchAttributeGroup, const char *pchAttributeValue, uint32 unPriority )
{
PRINT_DEBUG("['%s']: '%s' <%u>", pchAttributeGroup, pchAttributeValue, unPriority);
std::lock_guard lock(timeline_mutex);
if (timeline_game_phases.empty()) return;
auto &last_game_phase = timeline_game_phases.back();
if (last_game_phase.ended) return;
if (!pchAttributeGroup) pchAttributeGroup = "";
auto &phase_att = last_game_phase.attributes[pchAttributeGroup];
phase_att.pchAttributeValue = pchAttributeValue ? pchAttributeValue : "";
phase_att.unPriority = unPriority;
PRINT_DEBUG(" changed phase attribute");
}
void Steam_Timeline::OpenOverlayToGamePhase( const char *pchPhaseID )
{
PRINT_DEBUG("['%s'] // TODO", pchPhaseID);
std::lock_guard lock(timeline_mutex);
}
void Steam_Timeline::OpenOverlayToTimelineEvent( const TimelineEventHandle_t ulEvent )
{
PRINT_DEBUG("[%llu] // TODO", ulEvent);
std::lock_guard lock(timeline_mutex);
}
uint32 Steam_Timeline::unknown_ret0_1()
{
PRINT_DEBUG_TODO();
return 0;
}
uint32 Steam_Timeline::unknown_ret0_2()
{
PRINT_DEBUG_TODO();
return 0;
}
void Steam_Timeline::unknown_nop_3()
{
PRINT_DEBUG_TODO();
}
void Steam_Timeline::unknown_nop_4()
{
PRINT_DEBUG_TODO();
}
void Steam_Timeline::unknown_nop_5()
{
PRINT_DEBUG_TODO();
}
void Steam_Timeline::unknown_nop_6()
{
PRINT_DEBUG_TODO();
}
void Steam_Timeline::unknown_nop_7()
{
PRINT_DEBUG_TODO();
}
void Steam_Timeline::RunCallbacks()
{
}
void Steam_Timeline::Callback(Common_Message *msg)
{
if (msg->has_low_level()) {
if (msg->low_level().type() == Low_Level::CONNECT) {
}
if (msg->low_level().type() == Low_Level::DISCONNECT) {
}
}
}