-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSteam.hpp
More file actions
1984 lines (1590 loc) · 63.8 KB
/
Steam.hpp
File metadata and controls
1984 lines (1590 loc) · 63.8 KB
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "Friends/Friends.hpp"
#include "Party/Party.hpp"
#include "Users/Users.hpp"
#include "stormancer/Configuration.h"
#include "stormancer/IPlugin.h"
#include "stormancer/IScheduler.h"
#include "stormancer/StormancerTypes.h"
#include "stormancer/Utilities/PointerUtilities.h"
#include "stormancer/Utilities/TaskUtilities.h"
#include "stormancer/cpprestsdk/cpprest/asyncrt_utils.h"
#pragma warning(disable: 4265) // Disable virtual destructor requirement warnings
#ifndef STORM_NOINCLUDE_STEAM
#include "steam_api.h"
#endif
#pragma warning(default: 4265)
// https://partner.steamgames.com/doc/sdk/api
namespace Stormancer
{
namespace Steam
{
static constexpr const char* platformName = "steam";
/// <summary>
/// Keys to use in Configuration::additionalParameters map to customize the Steam plugin behavior.
/// </summary>
namespace ConfigurationKeys
{
/// <summary>
/// Enable Steam authentication.
/// If disabled, the Steam plugin will not be considered for authentication.
/// Default is "true".
/// Use "false" to disable.
/// </summary>
constexpr const char* AuthenticationEnabled = "steam.authentication.enabled";
/// <summary>
/// The lobbyID the client should connect on authentication.
/// Automatic connection to a Steam lobby on successful authentication should occur when the game has been launched by a lobby invitation.
/// You can get the LobbyID by searching the "+connect_lobby" parameter in the command line arguments (argv).
/// </summary>
constexpr const char* ConnectLobby = "steam.connectLobby";
/// <summary>
/// Should Stormancer initialize the Steam API library.
/// Default is "true".
/// Use "false" to disable.
/// </summary>
constexpr const char* SteamApiInitialize = "steam.steamApi.initialize";
/// <summary>
/// Should Stormancer run Steam Api callbacks.
/// Default is "true".
/// Use "false" to disable.
/// </summary>
constexpr const char* SteamApiRunCallbacks = "steam.steamApi.runCallbacks";
}
constexpr const char* PARTY_TYPE_STEAMIDLOBBY = "steamIDLobby";
using SteamID = uint64;
using SteamIDLobby = uint64;
using SteamIDFriend = uint64;
using SteamIDApp = uint64;
struct LobbyMember
{
SteamID steamID;
std::string personaname;
std::unordered_map<std::string, std::string> data;
};
struct Lobby
{
SteamIDLobby steamIDLobby = 0;
int numLobbyMembers = 0;
int lobbyMemberLimit = 0;
SteamID lobbyOwner = 0;
std::unordered_map<SteamID, LobbyMember> lobbyMembers;
std::unordered_map<std::string, std::string> data;
};
struct LobbyFilter
{
ELobbyDistanceFilter distanceFilter = ELobbyDistanceFilter::k_ELobbyDistanceFilterDefault;
int slotsAvailable = 0;
int resultCountFilter = 0;
std::vector<std::pair<std::string, int>> nearValueFilter;
std::unordered_map<std::string, std::pair<int, ELobbyComparison>> numericalFilter;
std::unordered_map<std::string, std::pair<std::string, ELobbyComparison>> stringFilter;
};
struct PartyDataDto
{
std::string partyId;
std::string leaderUserId;
SteamID leaderSteamId = 0;
MSGPACK_DEFINE(partyId, leaderUserId, leaderSteamId);
};
struct SteamFriend
{
std::string steamId;
std::string relationship;
uint64 friend_since = 0;
MSGPACK_DEFINE(steamId, relationship, friend_since);
};
class SteamApi
{
public:
static constexpr const char* METADATA_KEY = "stormancer.plugins.steam";
virtual ~SteamApi() = default;
virtual void initialize() = 0;
// Stormancer Api
virtual pplx::task<std::unordered_map<std::string, PartyDataDto>> decodePartyDataBearerTokens(const std::unordered_map<std::string, std::string>& partyDataBearerToken, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<std::unordered_map<SteamID, std::string>> queryUserIds(const std::vector<SteamID>& steamIDs, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<bool> inLobby(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<bool> isOwner(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<std::vector<SteamFriend>> getFriends(int friendsFlag = k_EFriendFlagImmediate, uint32 maxFriendsCount = UINT32_MAX, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
// Steam Api
virtual SteamID getSteamID() = 0;
virtual pplx::task<SteamIDLobby> createLobby(ELobbyType lobbyType = ELobbyType::k_ELobbyTypeFriendsOnly, int maxMembers = 5, bool joinable = true, const std::unordered_map<std::string, std::string> metadata = std::unordered_map<std::string, std::string>(), pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<void> joinLobby(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<void> leaveLobby(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<Lobby> requestLobbyData(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<std::vector<Lobby>> requestLobbyList(LobbyFilter lobbyFilter = LobbyFilter(), pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<void> setLobbyJoinable(SteamIDLobby steamIDLobby, bool joinable, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<void> setLobbyData(SteamIDLobby steamIDLobby, const std::string& key, const std::string& value, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
virtual pplx::task<void> setLobbyMemberData(SteamIDLobby steamIDLobby, const std::string& key, const std::string& value, pplx::cancellation_token ct = pplx::cancellation_token::none()) = 0;
// Steam Utils
virtual SteamIDApp getAppId() = 0;
};
namespace details
{
class SteamPlatformUserId : public Users::PlatformUserId
{
public:
std::string type() const override
{
return platformName;
}
static std::shared_ptr<SteamPlatformUserId> create(SteamID steamID)
{
// No make_shared because this class constructor is private
return std::shared_ptr<SteamPlatformUserId>(new SteamPlatformUserId(steamID));
}
static std::shared_ptr<SteamPlatformUserId> tryCast(std::shared_ptr<Users::PlatformUserId> id)
{
if (id != nullptr && id->type() == platformName)
{
return std::static_pointer_cast<SteamPlatformUserId>(id);
}
return nullptr;
}
SteamID getSteamID()
{
return _steamID;
}
bool operator==(const SteamPlatformUserId& right)
{
return _steamID == right._steamID;
}
bool operator!=(const SteamPlatformUserId& right)
{
return _steamID != right._steamID;
}
private:
SteamPlatformUserId(SteamID steamID)
: PlatformUserId(std::to_string(steamID))
, _steamID(steamID)
{
}
const SteamID _steamID;
};
class SteamState
{
public:
SteamState(std::shared_ptr<Configuration> config)
{
_authenticationEnabled = config->additionalParameters.find(ConfigurationKeys::AuthenticationEnabled) != config->additionalParameters.end() ? (config->additionalParameters.at(ConfigurationKeys::AuthenticationEnabled) != "false") : true;
_connectLobby = config->additionalParameters.find(ConfigurationKeys::ConnectLobby) != config->additionalParameters.end() ? config->additionalParameters.at(ConfigurationKeys::ConnectLobby) : "";
_steamApiInitialize = config->additionalParameters.find(ConfigurationKeys::SteamApiInitialize) != config->additionalParameters.end() ? (config->additionalParameters.at(ConfigurationKeys::SteamApiInitialize) != "false") : true;
_steamApiRunCallbacks = config->additionalParameters.find(ConfigurationKeys::SteamApiRunCallbacks) != config->additionalParameters.end() ? (config->additionalParameters.at(ConfigurationKeys::SteamApiRunCallbacks) != "false") : true;
if (_connectLobby.empty() && config->processLaunchArguments.size() > 0)
{
for (auto argi = 0; argi < config->processLaunchArguments.size(); argi++)
{
if (config->processLaunchArguments[argi] == "+connect_lobby" && (argi + 1) < config->processLaunchArguments.size())
{
std::string steamIDLobby = config->processLaunchArguments[argi + 1];
_connectLobby = steamIDLobby;
}
}
}
}
bool getAuthenticationEnabled() const
{
return _authenticationEnabled;
}
std::string getConnectLobby() const
{
return _connectLobby;
}
bool getSteamApiInitialize() const
{
return _steamApiInitialize;
}
bool getSteamApiRunCallbacks() const
{
return _steamApiRunCallbacks;
}
void resetConnectLobby()
{
_connectLobby = "";
}
private:
bool _authenticationEnabled = true;
std::string _connectLobby;
bool _steamApiInitialize = true;
bool _steamApiRunCallbacks = true;
};
struct CreateLobbyDto
{
ELobbyType lobbyType = ELobbyType::k_ELobbyTypePrivate;
int maxMembers = 0;
bool joinable = false;
std::unordered_map<std::string, std::string> metadata;
MSGPACK_DEFINE(lobbyType, maxMembers, joinable, metadata)
};
struct JoinLobbyDto
{
SteamIDLobby steamIDLobby;
MSGPACK_DEFINE(steamIDLobby)
};
class SteamService : public std::enable_shared_from_this<SteamService>
{
public:
SteamService(std::shared_ptr<Scene> scene)
: _rpcService(scene->dependencyResolver().resolve<RpcService>())
{
}
pplx::task<std::unordered_map<std::string, PartyDataDto>> decodePartyDataBearerTokens(const std::unordered_map<std::string, std::string>& partyDataBearerTokens, pplx::cancellation_token ct = pplx::cancellation_token::none())
{
return _rpcService->rpc<std::unordered_map<std::string, PartyDataDto>>("Steam.DecodePartyDataBearerTokens", ct, partyDataBearerTokens);
}
pplx::task<std::unordered_map<SteamID, std::string>> queryUserIds(const std::vector<SteamID>& steamIDs, pplx::cancellation_token ct = pplx::cancellation_token::none())
{
return _rpcService->rpc<std::unordered_map<SteamID, std::string>>("Steam.QueryUserIds", ct, steamIDs);
}
private:
std::shared_ptr<RpcService> _rpcService;
};
class SteamPartyService : public std::enable_shared_from_this<SteamPartyService>
{
public:
SteamPartyService(std::shared_ptr<Scene> scene)
: _rpcService(scene->dependencyResolver().resolve<RpcService>())
{
}
pplx::task<std::string> createPartyDataBearerToken(pplx::cancellation_token ct = pplx::cancellation_token::none())
{
return _rpcService->rpc<std::string>("SteamParty.CreatePartyDataBearerToken", ct);
}
private:
std::shared_ptr<RpcService> _rpcService;
};
class SteamPartyInvitation : public Party::Platform::IPlatformInvitation
{
public:
SteamPartyInvitation(const Party::PartyId& partyId, const std::string& senderSteamID = "")
: _partyId(partyId)
, _senderSteamID(senderSteamID)
{
}
pplx::task<Party::PartyId> accept(std::shared_ptr<Party::PartyApi> partyApi) override
{
return pplx::task_from_result(_partyId);
}
pplx::task<void> decline(std::shared_ptr<Party::PartyApi>) override
{
return pplx::task_from_result();
}
std::string getSenderId() override
{
return _senderSteamID;
}
std::string getSenderPlatformId() override
{
return platformName;
}
Party::PartyId getPartyId()
{
return _partyId;
}
private:
Party::PartyId _partyId;
std::string _senderSteamID;
};
class SteamPartyProvider;
class SteamImpl : public ClientAPI<SteamImpl, SteamService>, public SteamApi
{
friend class SteamPartyProvider;
public:
#pragma region public_methods
SteamImpl(std::shared_ptr<Users::UsersApi> usersApi, std::shared_ptr<SteamState> steamConfig, std::shared_ptr<Configuration> config, std::shared_ptr<IScheduler> scheduler, std::shared_ptr<ILogger> logger, std::shared_ptr<Party::PartyApi> partyApi, std::shared_ptr<Party::Platform::InvitationMessenger> invitationMessenger)
: ClientAPI(usersApi, "stormancer.steam")
, _wSteamConfig(steamConfig)
, _wScheduler(scheduler)
, _wActionDispatcher(config->actionDispatcher)
, _logger(logger)
, _wUsersApi(usersApi)
, _wPartyApi(partyApi)
, _wInvitationMessenger(invitationMessenger)
{
}
~SteamImpl()
{
_cts.cancel();
}
void initialize() override
{
if (auto steamConfig = _wSteamConfig.lock())
{
if (steamConfig->getSteamApiInitialize())
{
if (!SteamAPI_Init())
{
_logger->log(LogLevel::Error, "Steam", "SteamAPI_Init failed");
return;
}
}
if (steamConfig->getSteamApiRunCallbacks())
{
scheduleRunSteamAPiCallbacks();
}
auto connectLobbyArgument = steamConfig->getConnectLobby();
if (!connectLobbyArgument.empty())
{
if (auto invitationMessenger = _wInvitationMessenger.lock())
{
_logger->log(LogLevel::Trace, "Steam", "Process launch argument +connect_lobby", connectLobbyArgument);
SteamIDLobby steamIDLobby = std::stoull(connectLobbyArgument);
Party::PartyId partyId;
partyId.id = std::to_string(steamIDLobby);
partyId.type = PARTY_TYPE_STEAMIDLOBBY;
partyId.platform = platformName;
auto steamPartyInvitation = std::make_shared<SteamPartyInvitation>(partyId);
invitationMessenger->notifyInvitationReceived(steamPartyInvitation);
}
}
}
auto usersApi = _wUsersApi.lock();
if (!usersApi)
{
_logger->log(LogLevel::Error, "Steam", "UsersApi deleted");
return;
}
auto wSteamImpl = STORM_WEAK_FROM_THIS();
usersApi->setOperationHandler("Steam.GetFriends", [wSteamApi = wSteamImpl, wUsersApi = _wUsersApi, logger = _logger](Stormancer::Users::OperationCtx& ctx)
{
auto steamApi = wSteamApi.lock();
if (!steamApi)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("SteamApi"), void);
}
uint32 maxFriendsCount = ctx.request->readObject<uint32>();
return steamApi->getFriends(k_EFriendFlagImmediate, maxFriendsCount, ctx.request->cancellationToken())
.then([ctx](std::vector<SteamFriend> friends)
{
ctx.request->sendValueTemplated(friends);
});
});
usersApi->setOperationHandler("Steam.CreateLobby", [wSteamImpl, wUsersApi = _wUsersApi, logger = _logger](Stormancer::Users::OperationCtx& ctx)
{
auto steamImpl = wSteamImpl.lock();
if (!steamImpl)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("SteamApi"), void);
}
auto createLobbyDto = ctx.request->readObject<CreateLobbyDto>();
// Create lobby
return steamImpl->createLobby(createLobbyDto.lobbyType, createLobbyDto.maxMembers, createLobbyDto.joinable, createLobbyDto.metadata, ctx.request->cancellationToken())
.then([wSteamImpl, wUsersApi, ctx](SteamIDLobby steamIDLobby)
{
auto steamImpl = wSteamImpl.lock();
if (!steamImpl)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("SteamApi"), void);
}
auto usersApi = wUsersApi.lock();
if (!usersApi)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("UsersApi"), void);
}
{
std::lock_guard<std::recursive_mutex> lg(steamImpl->_mutex);
// Keep steamIDLobby to leave on party leave
steamImpl->_partySteamIDLobby = steamIDLobby;
}
auto myUserId = usersApi->userId();
return steamImpl->setLobbyMemberData(steamIDLobby, "stormancer.userId", myUserId, ctx.request->cancellationToken())
.then([steamIDLobby, ctx]()
{
// Send back steamIDLobby to server
ctx.request->sendValue([steamIDLobby](obytestream& stream)
{
Serializer serializer;
serializer.serialize(stream, steamIDLobby);
});
});
});
});
usersApi->setOperationHandler("Steam.JoinLobby", [wSteamImpl, wUsersApi = _wUsersApi, logger = _logger](Stormancer::Users::OperationCtx& ctx)
{
auto steamImpl = wSteamImpl.lock();
if (!steamImpl)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("SteamApi"), void);
}
auto joinLobbyDto = ctx.request->readObject<JoinLobbyDto>();
auto steamIDLobby = joinLobbyDto.steamIDLobby;
std::lock_guard<std::recursive_mutex> lg(steamImpl->_mutex);
// Keep steamIDLobby to leave on party leave
steamImpl->_partySteamIDLobby = steamIDLobby;
return steamImpl->inLobby(steamIDLobby, ctx.request->cancellationToken())
.then([steamIDLobby, wSteamImpl, ctx](bool inLobby)
{
if (inLobby)
{
// We already are in the lobby, do nothing
return pplx::task_from_result();
}
else
{
// Join lobby
auto steamImpl = wSteamImpl.lock();
if (!steamImpl)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("SteamApi"), void);
}
return steamImpl->joinLobby(steamIDLobby, ctx.request->cancellationToken());
}
})
.then([wSteamImpl, wUsersApi, steamIDLobby, ctx]()
{
auto steamImpl = wSteamImpl.lock();
if (!steamImpl)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("SteamApi"), void);
}
auto usersApi = wUsersApi.lock();
if (!usersApi)
{
STORM_RETURN_TASK_FROM_EXCEPTION(ObjectDeletedException("UsersApi"), void);
}
auto myUserId = usersApi->userId();
return steamImpl->setLobbyMemberData(steamIDLobby, "stormancer.userId", myUserId, ctx.request->cancellationToken());
});
});
}
void scheduleRunSteamAPiCallbacks()
{
if (!_cts.get_token().is_canceled())
{
SteamAPI_RunCallbacks();
if (auto actionDispatcher = _wActionDispatcher.lock())
{
auto wSteamImpl = STORM_WEAK_FROM_THIS();
actionDispatcher->post([wSteamImpl]()
{
if (auto steamImpl = wSteamImpl.lock())
{
steamImpl->scheduleRunSteamAPiCallbacks();
}
});
}
}
}
SteamID getSteamID() override
{
auto steamUser = SteamUser();
auto steamID = steamUser->GetSteamID();
return steamID.ConvertToUint64();
}
pplx::task<SteamIDLobby> createLobby(ELobbyType lobbyType = ELobbyType::k_ELobbyTypeFriendsOnly, int maxMembers = 5, bool joinable = true, const std::unordered_map<std::string, std::string> metadata = std::unordered_map<std::string, std::string>(), pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
if (maxMembers < 1 || maxMembers > 250)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("maxMembers must be between 1 and 250"), taskOptions, SteamIDLobby);
}
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), taskOptions, SteamIDLobby);
}
_logger->log(LogLevel::Trace, "Steam", "Create lobby");
std::lock_guard<std::recursive_mutex> lg(_mutex);
// Cancel
if (_lobbyCreatedTce)
{
_lobbyCreatedCallResult.Cancel();
_lobbyCreatedTce->set_exception(pplx::task_canceled());
}
// Prepare
_lobbyCreatedTce = std::make_shared<pplx::task_completion_event<SteamIDLobby>>();
// Timeout
timeout(10s, ct)
.register_callback([tce = _lobbyCreatedTce]()
{
tce->set_exception(pplx::task_canceled());
});
// Call SteamAPI and register call result
SteamAPICall_t hSteamAPICall = steamMatchmaking->CreateLobby(lobbyType, maxMembers);
_lobbyCreatedCallResult.Set(hSteamAPICall, this, &SteamImpl::onLobbyCreatedCallResult);
return pplx::create_task(*_lobbyCreatedTce, taskOptions)
.then([steamMatchmaking, joinable, metadata, wSteamApi = STORM_WEAK_FROM_THIS(), logger = _logger, ct](SteamIDLobby steamIDLobby)
{
auto steamApi = wSteamApi.lock();
auto task = pplx::task_from_result();
if (!joinable)
{
steamApi->setLobbyJoinable(steamIDLobby, joinable, ct)
.then([logger](pplx::task<void> task)
{
try
{
return task.get();
}
catch (const std::exception& ex)
{
logger->log(LogLevel::Warn, "Steam", "setLobbyJoinable failed", ex);
}
});
}
if (metadata.size() > 0)
{
for (auto& md : metadata)
{
steamApi->setLobbyData(steamIDLobby, md.first, md.second, ct)
.then([logger](pplx::task<void> task)
{
try
{
return task.get();
}
catch (const std::exception& ex)
{
logger->log(LogLevel::Warn, "Steam", "setLobbyData failed, metadata ignored", ex);
}
});
}
}
return steamIDLobby;
});
}
pplx::task<void> joinLobby(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), actionDispatcher, void);
}
_logger->log(LogLevel::Trace, "Steam", "Join lobby", std::to_string(steamIDLobby));
std::lock_guard<std::recursive_mutex> lg(_mutex);
// Cancel
auto it = _lobbyEnterEventData.find(steamIDLobby);
if (it != _lobbyEnterEventData.end())
{
it->second.callResult.Cancel();
it->second.tce.set_exception(pplx::task_canceled());
_lobbyEnterEventData.erase(it);
}
// Prepare
auto& lobbyEnterEventData = _lobbyEnterEventData[steamIDLobby];
// Timeout
timeout(10s, ct)
.register_callback([tce = lobbyEnterEventData.tce]()
{
tce.set_exception(pplx::task_canceled());
});
// Call SteamAPI and register call result
SteamAPICall_t hSteamAPICall = steamMatchmaking->JoinLobby(CSteamID(steamIDLobby));
lobbyEnterEventData.callResult.Set(hSteamAPICall, this, &SteamImpl::onLobbyEnterCallResult);
return pplx::create_task(lobbyEnterEventData.tce, taskOptions);
}
pplx::task<void> leaveLobby(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), actionDispatcher, void);
}
if (ct.is_cancelable() && ct.is_canceled())
{
STORM_RETURN_TASK_CANCELED_OPT(actionDispatcher, void);
}
_logger->log(LogLevel::Trace, "Steam", "Leave lobby", std::to_string(steamIDLobby));
steamMatchmaking->LeaveLobby(CSteamID(steamIDLobby));
_logger->log(LogLevel::Trace, "Steam", "Lobby left", std::to_string(steamIDLobby));
return pplx::task_from_result(taskOptions);
}
pplx::task<std::vector<Lobby>> requestLobbyList(LobbyFilter lobbyFilter = LobbyFilter(), pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), taskOptions, std::vector<Lobby>);
}
_logger->log(LogLevel::Trace, "Steam", "requestLobbyList");
if (lobbyFilter.distanceFilter != ELobbyDistanceFilter::k_ELobbyDistanceFilterDefault)
{
steamMatchmaking->AddRequestLobbyListDistanceFilter(lobbyFilter.distanceFilter);
}
if (lobbyFilter.slotsAvailable > 0)
{
steamMatchmaking->AddRequestLobbyListFilterSlotsAvailable(lobbyFilter.slotsAvailable);
}
if (lobbyFilter.resultCountFilter > 0)
{
steamMatchmaking->AddRequestLobbyListResultCountFilter(lobbyFilter.distanceFilter);
}
for (auto& nearValueFilter : lobbyFilter.nearValueFilter)
{
steamMatchmaking->AddRequestLobbyListNearValueFilter(nearValueFilter.first.c_str(), nearValueFilter.second);
}
for (auto& numericalFilter : lobbyFilter.numericalFilter)
{
steamMatchmaking->AddRequestLobbyListNumericalFilter(numericalFilter.first.c_str(), numericalFilter.second.first, numericalFilter.second.second);
}
for (auto& stringFilter : lobbyFilter.stringFilter)
{
steamMatchmaking->AddRequestLobbyListStringFilter(stringFilter.first.c_str(), stringFilter.second.first.c_str(), stringFilter.second.second);
}
std::lock_guard<std::recursive_mutex> lg(_mutex);
// Cancel
if (_requestLobbyListTce)
{
_requestLobbyListCallResult.Cancel();
_requestLobbyListTce->set_exception(pplx::task_canceled());
}
// Prepare
_requestLobbyListTce = std::make_shared<pplx::task_completion_event<std::vector<Lobby>>>();
// Timeout
timeout(10s, ct)
.register_callback([tce = _requestLobbyListTce]()
{
tce->set_exception(pplx::task_canceled());
});
// Call SteamAPI and register call result
SteamAPICall_t hSteamAPICall = steamMatchmaking->RequestLobbyList();
_requestLobbyListCallResult.Set(hSteamAPICall, this, &SteamImpl::onRequestLobbyListCallResult);
return pplx::create_task(*_requestLobbyListTce, taskOptions);
}
pplx::task<void> setLobbyJoinable(SteamIDLobby steamIDLobby, bool joinable, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), taskOptions, void);
}
if (ct.is_cancelable() && ct.is_canceled())
{
STORM_RETURN_TASK_CANCELED_OPT(taskOptions, void);
}
auto res = steamMatchmaking->SetLobbyJoinable(CSteamID(steamIDLobby), joinable);
if (!res)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("Steam::SetLobbyJoinable Api call failed"), taskOptions, void);
}
return pplx::task_from_result(taskOptions);
}
pplx::task<void> setLobbyData(SteamIDLobby steamIDLobby, const std::string& key, const std::string& value, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), taskOptions, void);
}
if (key.size() > k_nMaxLobbyKeyLength)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::invalid_argument("Steam.SetLobbyData failed: key size too long."), taskOptions, void);
}
if (value.size() > k_cubChatMetadataMax)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::invalid_argument("Steam.SetLobbyData failed: value size too long."), taskOptions, void);
}
if (ct.is_cancelable() && ct.is_canceled())
{
STORM_RETURN_TASK_CANCELED_OPT(taskOptions, void);
}
bool res = steamMatchmaking->SetLobbyData(CSteamID(steamIDLobby), key.c_str(), value.c_str());
if (!res)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("steamMatchmaking::SetLobbyData call returned failed."), taskOptions, void);
}
return pplx::task_from_result(taskOptions);
}
pplx::task<void> setLobbyMemberData(SteamIDLobby steamIDLobby, const std::string& key, const std::string& value, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), taskOptions, void);
}
if (key.size() > k_nMaxLobbyKeyLength)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::invalid_argument("Steam.SetLobbyData failed: key size too long."), taskOptions, void);
}
if (value.size() > k_cubChatMetadataMax)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::invalid_argument("Steam.SetLobbyData failed: value size too long."), taskOptions, void);
}
if (ct.is_cancelable() && ct.is_canceled())
{
STORM_RETURN_TASK_CANCELED_OPT(taskOptions, void);
}
steamMatchmaking->SetLobbyMemberData(CSteamID(steamIDLobby), key.c_str(), value.c_str());
return pplx::task_from_result(taskOptions);
}
pplx::task<Lobby> requestLobbyData(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
auto actionDispatcher = _wActionDispatcher.lock();
auto taskOptions = actionDispatcher ? pplx::task_options(actionDispatcher) : pplx::task_options();
auto steamMatchmaking = SteamMatchmaking();
if (!steamMatchmaking)
{
STORM_RETURN_TASK_FROM_EXCEPTION_OPT(std::runtime_error("SteamMatchmaking() returned null"), taskOptions, Lobby);
}
pplx::task_completion_event<Lobby> requestLobbyDataTce;
auto res = steamMatchmaking->RequestLobbyData(CSteamID(steamIDLobby));
if (res)
{
std::lock_guard<std::recursive_mutex> lg(_mutex);
_requestLobbyDataTces[steamIDLobby] = requestLobbyDataTce;
timeout(10s, ct)
.register_callback([steamIDLobby, wSteamImpl = STORM_WEAK_FROM_THIS()]()
{
if (auto steamImpl = wSteamImpl.lock())
{
std::lock_guard<std::recursive_mutex> lg(steamImpl->_mutex);
auto it = steamImpl->_requestLobbyDataTces.find(steamIDLobby);
if (it != steamImpl->_requestLobbyDataTces.end())
{
it->second.set_exception(pplx::task_canceled());
steamImpl->_requestLobbyDataTces.erase(it);
}
}
});
}
else
{
requestLobbyDataTce.set_exception(std::runtime_error("Steam request lobby data failed"));
}
return pplx::create_task(requestLobbyDataTce, taskOptions);
}
SteamIDApp getAppId() override
{
auto steamUtils = SteamUtils();
if (!steamUtils)
{
return 0;
}
return steamUtils->GetAppID();
}
pplx::task<std::unordered_map<SteamID, std::string>> queryUserIds(const std::vector<SteamID>& steamIDs, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
return getService([](auto, auto, auto) {}, [](auto, auto) {}, ct)
.then([steamIDs, ct](std::shared_ptr<SteamService> service)
{
return service->queryUserIds(steamIDs, ct);
});
}
pplx::task<std::unordered_map<std::string, PartyDataDto>> decodePartyDataBearerTokens(const std::unordered_map<std::string, std::string>& partyDataBearerTokens, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
return getService([](auto, auto, auto) {}, [](auto, auto) {}, ct)
.then([partyDataBearerTokens, ct](std::shared_ptr<SteamService> service)
{
return service->decodePartyDataBearerTokens(partyDataBearerTokens, ct);
});
}
pplx::task<bool> inLobby(SteamIDLobby steamIDLobby, pplx::cancellation_token ct = pplx::cancellation_token::none()) override
{
return requestLobbyData(steamIDLobby, ct)
.then([steamIDLobby](Lobby lobby)
{
auto steamUser = SteamUser();
if (!steamUser)
{
return false;
}
SteamID steamID = steamUser->GetSteamID().ConvertToUint64();