-
Notifications
You must be signed in to change notification settings - Fork 6
/
Steam.cpp
executable file
·1333 lines (1153 loc) · 45.1 KB
/
Steam.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
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
#include "StdAfx.h"
#include "Steam.h"
#if HAS_GOG
# include "../gog_galaxy/Include/galaxy/GalaxyApi.h"
#endif
static DEFINE_CVAR(bool, kSteamCloudEnable, true);
static DEFINE_CVAR(bool, kForceAppSandbox, false);
static DEFINE_CVAR(bool, kSteamEnable, true);
static DEFINE_CVAR(bool, kSteamBenchmark, false);
static DEFINE_CVAR(bool, kGOGEnable, true);
static DEFINE_CVAR(int, kSteamCallbackInterval, 50);
static DEFINE_CVAR(int, kGOGLoginWaitSecond, 2);
static const char* kGOGClientId = "52046877955667918";
static const uint64_t kGOGProductId = 1487278562;
static const uint64_t kGOGFieldsProductId = 2125664342;
enum GOGLoginState { GOG_DISCONNECTED, GOG_WAIT, GOG_SIGNED_IN, GOG_LOGIN_FAILED };
static OL_Thread s_steamThread;
static GOGLoginState s_gog_state = GOG_DISCONNECTED;
static bool s_gog_logged_on = false;
static bool s_steam_init = false;
static bool s_steam_subscribed = false;
void olSleep(int ms);
bool isOLExiting();
bool isAppSandboxed();
bool isAppSandboxed()
{
#if __APPLE__
return kForceAppSandbox || OL_IsSandboxed();
#else
return false;
#endif
}
#if HAS_GOG
std::mutex s_gog_mutex;
std::map<galaxy::api::GalaxyID, string> s_gog_friends;
int s_gog_friend_count = -1;
int s_gog_friend_info_count = 0;
static void finishGOGInit();
bool CheckGOGError(const char* func)
{
const galaxy::api::IError* error = galaxy::api::GetError();
if (!error)
return true;
DPRINT(GOG, ("%s -> %s: %s", func, error->GetName(), error->GetMsg()));
return false;
}
static const char* GOGAuthFailureReasonToString(galaxy::api::IAuthListener::FailureReason reason)
{
switch (reason)
{
default:
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_UNDEFINED);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_GALAXY_SERVICE_NOT_AVAILABLE);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_GALAXY_SERVICE_NOT_SIGNED_IN);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_CONNECTION_FAILURE);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_NO_LICENSE);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_INVALID_CREDENTIALS);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_GALAXY_NOT_INITIALIZED);
CASE_STR(galaxy::api::IAuthListener::FAILURE_REASON_EXTERNAL_SERVICE_FAILURE);
}
}
struct GOGAuthListener : public galaxy::api::GlobalAuthListener {
void OnAuthSuccess() override
{
DPRINT(GOG, ("OnAuthSuccess"));
finishGOGInit();
}
void OnAuthFailure(galaxy::api::IAuthListener::FailureReason reason) override
{
DPRINT(GOG, ("OnAuthFailure: %s", GOGAuthFailureReasonToString(reason)));
finishGOGInit();
}
void OnAuthLost() override
{
DPRINT(GOG, ("OnAuthLost"));
}
};
static const char* GOGStateToString(uint32_t state)
{
switch (state)
{
default: return "none";
case galaxy::api::IOperationalStateChangeListener::OperationalState::OPERATIONAL_STATE_SIGNED_IN:
return "signed in";
case galaxy::api::IOperationalStateChangeListener::OperationalState::OPERATIONAL_STATE_LOGGED_ON:
return "logged on";
case galaxy::api::IOperationalStateChangeListener::OperationalState::OPERATIONAL_STATE_SIGNED_IN|
galaxy::api::IOperationalStateChangeListener::OperationalState::OPERATIONAL_STATE_LOGGED_ON:
return "logged on and signed in";
}
}
static void finishGOGInit()
{
std::unique_lock<std::mutex> l(s_gog_mutex);
if (galaxy::api::Friends())
{
// authenticated with GOG client
const bool signed_in = galaxy::api::User()->SignedIn();
// also connected to the internet and authenticated with GOG server
const bool logged_on = galaxy::api::User()->IsLoggedOn();
const char* name = signed_in ? galaxy::api::Friends()->GetPersonaName() : "";
CheckGOGError("GetPersonaName");
uint64 uid = galaxy::api::User()->GetGalaxyID().GetRealID();
CheckGOGError("GetGalaxyID");
DPRINT(GOG, ("User '%s' %lld signed in: %s logged on: %s", name, uid,
signed_in ? "YES" : "NO",
logged_on ? "YES" : "NO"));
s_gog_state = signed_in ? GOG_SIGNED_IN : GOG_LOGIN_FAILED;
s_gog_logged_on = logged_on;
}
if (galaxy::api::Utils())
{
galaxy::api::OverlayState state = galaxy::api::Utils()->GetOverlayState();
DPRINT(GOG, ("OverlayState: %s",
(state == galaxy::api::OVERLAY_STATE_UNDEFINED) ? "UNDEFINED" :
(state == galaxy::api::OVERLAY_STATE_NOT_SUPPORTED) ? "NOT_SUPPORTED" :
(state == galaxy::api::OVERLAY_STATE_DISABLED) ? "DISABLED" :
(state == galaxy::api::OVERLAY_STATE_FAILED_TO_INITIALIZE) ? "FAILED_TO_INITIALIZE" :
(state == galaxy::api::OVERLAY_STATE_INITIALIZED) ? "INITIALIZED" : "UNKNOWN"));
}
}
struct GOGStateListener : public galaxy::api::GlobalOperationalStateChangeListener {
void OnOperationalStateChanged(uint32_t operationalState) override
{
DPRINT(GOG, ("OnOperationalStateChanged: %s", GOGStateToString(operationalState)));
}
};
struct GOGFriendListener : public galaxy::api::GlobalFriendListListener {
void OnFriendListRetrieveSuccess() override
{
std::lock_guard<std::mutex> l(s_gog_mutex);
uint32_t count = galaxy::api::Friends()->GetFriendCount();
CheckGOGError("GetFriendCount");
s_gog_friend_count = count;
char buf[128] = {};
for (uint32_t i=0; i<count; i++)
{
galaxy::api::GalaxyID fid = galaxy::api::Friends()->GetFriendByIndex(i);
CheckGOGError("GetFriendByIndex");
buf[0] = '\0';
if (GOG_CALL( galaxy::api::Friends()->GetFriendPersonaNameCopy(fid, buf, sizeof(buf)) )
&& buf[0])
{
s_gog_friends[fid] = string(buf, str_len(buf));
s_gog_friend_info_count++;
}
else
{
s_gog_friends[fid] = string();
}
}
// should call RequestUserInformation automatically
}
void OnFriendListRetrieveFailure(FailureReason failureReason) override
{
DPRINT(GOG, ("OnFriendListRetrieveFailure: %s",
(failureReason == FAILURE_REASON_UNDEFINED) ? "UNDEFINED" :
(failureReason == FAILURE_REASON_CONNECTION_FAILURE) ? "CONNECTION_FAILURE" :
"UNKNOWN"));
}
};
struct GOGUserListener : public galaxy::api::GlobalUserInformationRetrieveListener {
void OnUserInformationRetrieveSuccess(galaxy::api::GalaxyID userID) override
{
std::lock_guard<std::mutex> l(s_gog_mutex);
string &name = s_gog_friends[userID];
if (name.empty())
{
char buf[128] = {};
GOG_CALL( galaxy::api::Friends()->GetFriendPersonaNameCopy(userID, buf, sizeof(buf)) );
if (buf[0])
{
name = string(buf, str_len(buf));
s_gog_friend_info_count++;
}
}
}
void OnUserInformationRetrieveFailure(galaxy::api::GalaxyID userID, FailureReason failureReason) override
{
DPRINT(GOG, ("OnUserInformationRetrieveFailure: %s",
(failureReason == FAILURE_REASON_UNDEFINED) ? "UNDEFINED" :
(failureReason == FAILURE_REASON_CONNECTION_FAILURE) ? "CONNECTION_FAILURE" :
"UNKNOWN"));
s_gog_friend_info_count = -1;
}
};
string getGOGFriendList()
{
if (!isGOGInitialized())
return string();
if (s_gog_friend_count == -1)
{
if (!GOG_CALL( galaxy::api::Friends()->RequestFriendList() ))
return string();
std::unique_lock<std::mutex> l(s_gog_mutex);
while (s_gog_friend_count == -1 || s_gog_friend_count < s_gog_friend_info_count)
{
l.unlock();
olSleep(10);
if (isOLExiting() || s_gog_friend_info_count == -1)
return string();
l.lock();
}
}
string s;
for_ (it, s_gog_friends)
{
if (it.second.empty())
continue;
if (s.size())
s +=",";
s += it.second;
}
return s;
}
vector<galaxy::api::IGalaxyListener*> s_gog_listeners;
#endif
void registerGOGListener(void *it)
{
#if HAS_GOG
s_gog_listeners.push_back((galaxy::api::IGalaxyListener*)it);
#endif
}
bool isSteamInitialized()
{
return s_steam_subscribed;
}
bool isGOGInitialized()
{
return s_gog_state == GOG_SIGNED_IN;
}
bool isGOGLoggedOn()
{
return s_gog_logged_on;
}
// steam stats/achievements wrapper class
// moves steam API calls to another thread to avoid blocking
struct SteamStats {
private:
std::unordered_map<std::string, bool> m_achievements;
std::unordered_map<std::string, int> m_stats;
mutable std::mutex m_mutex;
volatile int m_pending = 0;
std::unordered_set< std::string > m_updates;
const char** m_steamStats = NULL; // null terminated array of stat names for this game
const AchievementStat * m_gog_achievements = NULL;
bool m_ready = false;
public:
std::mutex deleteMutex;
std::mutex indexMutex;
STEAM_CALLBACK_NAMED(SteamStats, UserStatsReceived);
SteamStats();
void Init(const char** stats, const AchievementStat *achievements)
{
m_steamStats = stats;
m_gog_achievements = achievements;
#if HAS_STEAM
if (SteamUserStats())
SteamUserStats()->RequestCurrentStats();
#endif
#if HAS_GOG
if (isGOGInitialized() && galaxy::api::Stats())
galaxy::api::Stats()->RequestUserStatsAndAchievements();
#endif
}
int GetStat(const char* name);
void SetStat(const char* name, int value)
{
std::lock_guard<std::mutex> l(m_mutex);
if (map_get(m_stats, name, -value) == value)
return;
m_stats[name] = value;
m_updates.insert(name);
}
void SetAchievement(const char* name);
void ReadStats();
// actually set the stats
// call periodically from steam thread
void processCallbacks();
static SteamStats &instance();
};
bool setupSteamStats(const char** stats, const AchievementStat *achievements)
{
#if HAS_GOG
{
std::unique_lock<std::mutex> l(s_gog_mutex);
if (s_gog_state == GOG_WAIT)
{
return false;
}
}
#endif
SteamStats::instance().Init(stats, achievements);
return true;
}
void steamAchievementSet(const char* statname)
{
SteamStats::instance().SetAchievement(statname);
}
void steamStatIncrement(const char* statname, int incr)
{
SteamStats::instance().SetStat(statname, SteamStats::instance().GetStat(statname) + incr);
}
void steamStatMax(const char* statname, int value)
{
if (value > SteamStats::instance().GetStat(statname))
SteamStats::instance().SetStat(statname, value);
}
void steamStatSet(const char* statname, int value)
{
SteamStats::instance().SetStat(statname, value);
}
#if HAS_STEAM
SteamStats::SteamStats() : STEAM_CALLBACK_CONS(SteamStats, UserStatsReceived)
#else
SteamStats::SteamStats()
#endif
{
}
void SteamStats::ReadStats()
{
std::lock_guard<std::mutex> l(m_mutex);
for (const char** ptr=m_steamStats; *ptr; ++ptr)
{
const char *name = *ptr;
int &val = m_stats[name];
#if HAS_STEAM
if (SteamUserStats())
{
bool success = SteamUserStats()->GetStat(name, &val);
DPRINT(STEAM, ("Read stat %s=%d: %s", name, val, success ? "OK" : "FAILED"));
}
#endif
#if HAS_GOG
if (isGOGInitialized() && galaxy::api::Stats())
{
val = galaxy::api::Stats()->GetStatInt(name);
bool success = CheckGOGError("GetStatInt");
DPRINT(GOG, ("Read stat %s=%d: %s", name, val, success ? "OK" : "FAILED"));
}
#endif
}
m_ready = true;
}
#if HAS_GOG
struct GOGStatsListener : public galaxy::api::GlobalUserStatsAndAchievementsRetrieveListener {
void OnUserStatsAndAchievementsRetrieveSuccess(galaxy::api::GalaxyID userID) override
{
DPRINT(GOG, ("OnUserStatsAndAchievementsRetrieveSuccess"));
SteamStats::instance().ReadStats();
}
void OnUserStatsAndAchievementsRetrieveFailure(galaxy::api::GalaxyID userID, FailureReason failureReason) override
{
DPRINT(GOG, ("OnUserStatsAndAchievementsRetrieveFailure: %s",
(failureReason == FAILURE_REASON_CONNECTION_FAILURE) ? "CONNECTION_FAILURE" :
(failureReason == FAILURE_REASON_UNDEFINED) ? "UNDEFINED" : "UNKNOWN"));
}
};
#endif
int SteamStats::GetStat(const char* name)
{
std::lock_guard<std::mutex> l(m_mutex);
auto it = m_stats.find(name);
if (it != m_stats.end())
return it->second;
int32_t val = 0;
#if HAS_STEAM
if (SteamUserStats())
{
bool success = SteamUserStats()->GetStat(name, &val);
m_stats[name] = val;
DPRINT(STEAM, ("Fallback Read stat %s=%d: %s", name, val, success ? "OK" : "FAILED"));
return val;
}
#endif
#if HAS_GOG
if (isGOGInitialized() && galaxy::api::Stats())
{
val = galaxy::api::Stats()->GetStatInt(name);
m_stats[name] = val;
bool success = CheckGOGError("GetStatInt");
DPRINT(GOG, ("Fallback Read stat %s=%d: %s", name, val, success ? "OK" : "FAILED"));
return val;
}
#endif
return val;
}
void SteamStats::SetAchievement(const char* name)
{
if (m_achievements[name])
return;
bool aok = false;
#if HAS_STEAM
if (SteamUserStats())
{
aok = SteamUserStats()->SetAchievement(name);
DPRINT(STEAM, ("Set Achievement %s: %s", name, aok ? "OK" : "FAILED"));
}
#endif
#if HAS_GOG
if (isGOGInitialized() && galaxy::api::Stats())
{
aok = GOG_CALL( galaxy::api::Stats()->SetAchievement(name) );
DPRINT(GOG, ("Set Achievement %s: %s", name, aok ? "OK" : "FAILED"));
}
#endif
std::lock_guard<std::mutex> l(m_mutex);
if (aok)
m_achievements[name] = true;
++m_pending;
}
void SteamStats::processCallbacks()
{
if (m_ready)
{
std::lock_guard<std::mutex> l(SteamStats::instance().deleteMutex);
#if HAS_STEAM
ISteamUserStats *ss = s_steam_subscribed ? SteamUserStats() : NULL;
#endif
#if HAS_GOG
galaxy::api::IStats *is = isGOGInitialized() ? galaxy::api::Stats() : NULL;
#endif
if (m_stats.size())
{
std::lock_guard<std::mutex> l0(m_mutex);
foreach (const string &name, m_updates)
{
const int value = m_stats[name];
#if HAS_STEAM
if (ss)
{
bool success = ss->SetStat(name.c_str(), value);
DPRINT(STEAM, ("Set Stat %s = %d: %s", name.c_str(), value, success ? "OK" : "FAILED"));
}
#endif
#if HAS_GOG
if (is)
{
bool success = GOG_CALL( is->SetStatInt(name.c_str(), value) );
DPRINT(GOG, ("Set Stat %s = %d: %s", name.c_str(), value, success ? "OK" : "FAILED"));
for (const AchievementStat *as=m_gog_achievements; as->minval; ++as)
{
if (as->stat == name && value >= as->minval)
{
// SetAchievement(as->achievement);
bool &has = m_achievements[as->achievement];
if (has)
continue;
bool aok = GOG_CALL( is->SetAchievement(as->achievement) );
DPRINT(GOG, ("Set Achievement %s: %s", as->achievement, aok ? "OK" : "FAILED"));
if (aok)
has = true;
m_pending++;
}
}
}
#endif
}
m_pending += m_updates.size();
m_updates.clear();
}
const int pending = m_pending;
if (pending)
{
#if HAS_STEAM
if (ss)
{
const bool sok = ss->StoreStats();
DPRINT(STEAM, ("Stored %d Stats: %s", pending, sok ? "OK" : "FAILED"));
}
#endif
#if HAS_GOG
if (is)
{
const bool sok = GOG_CALL( is->StoreStatsAndAchievements() );
DPRINT(GOG, ("Stored %d Stats: %s", pending, sok ? "OK" : "FAILED"));
}
#endif
std::lock_guard<std::mutex> l0(m_mutex);
m_pending -= pending;
}
}
#if HAS_STEAM
SteamAPI_RunCallbacks();
#endif
#if HAS_GOG
galaxy::api::ProcessData();
#endif
}
SteamStats & SteamStats::instance()
{
static SteamStats it;
return it;
}
#if HAS_STEAM
void SteamStats::OnUserStatsReceived(UserStatsReceived_t *callback)
{
DPRINT(STEAM, ("OnUserStatsRetrieved"));
SteamStats::instance().ReadStats();
}
const char* EResult2String(EResult res)
{
switch (res)
{
case k_EResultOK: return "OK // success";
case k_EResultFail: return "Fail // generic failure ";
case k_EResultNoConnection: return "NoConnection // no/failed network connection";
case k_EResultInvalidPassword: return "InvalidPassword // password/ticket is invalid";
case k_EResultLoggedInElsewhere: return "LoggedInElsewhere // same user logged in elsewhere";
case k_EResultInvalidProtocolVer: return "InvalidProtocolVer // protocol version is incorrect";
case k_EResultInvalidParam: return "InvalidParam // a parameter is incorrect";
case k_EResultFileNotFound: return "FileNotFound // file was not found";
case k_EResultBusy: return "Busy // called method busy - action not taken";
case k_EResultInvalidState: return "InvalidState // called object was in an invalid state";
case k_EResultInvalidName: return "InvalidName // name is invalid";
case k_EResultInvalidEmail: return "InvalidEmail // email is invalid";
case k_EResultDuplicateName: return "DuplicateName // name is not unique";
case k_EResultAccessDenied: return "AccessDenied // access is denied";
case k_EResultTimeout: return "Timeout // operation timed out";
case k_EResultBanned: return "Banned // VAC2 banned";
case k_EResultAccountNotFound: return "AccountNotFound // account not found";
case k_EResultInvalidSteamID: return "InvalidSteamID // steamID is invalid";
case k_EResultServiceUnavailable: return "ServiceUnavailable // The requested service is currently unavailable";
case k_EResultNotLoggedOn: return "NotLoggedOn // The user is not logged on";
case k_EResultPending: return "Pending // Request is pending (may be in process, or waiting on third party)";
case k_EResultEncryptionFailure: return "EncryptionFailure // Encryption or Decryption failed";
case k_EResultInsufficientPrivilege: return "InsufficientPrivilege // Insufficient privilege";
case k_EResultLimitExceeded: return "LimitExceeded // Too much of a good thing";
case k_EResultRevoked: return "Revoked // Access has been revoked (used for revoked guest passes)";
case k_EResultExpired: return "Expired // License/Guest pass the user is trying to access is expired";
case k_EResultAlreadyRedeemed: return "AlreadyRedeemed // Guest pass has already been redeemed by account, cannot be acked again";
case k_EResultDuplicateRequest: return "DuplicateRequest // The request is a duplicate and the action has already occurred in the past, ignored this time";
case k_EResultAlreadyOwned: return "AlreadyOwned // All the games in this guest pass redemption request are already owned by the user";
case k_EResultIPNotFound: return "IPNotFound // IP address not found";
case k_EResultPersistFailed: return "PersistFailed // failed to write change to the data store";
case k_EResultLockingFailed: return "LockingFailed // failed to acquire access lock for this operation";
case k_EResultLogonSessionReplaced: return "LogonSessionReplaced ";
case k_EResultConnectFailed: return "ConnectFailed ";
case k_EResultHandshakeFailed: return "HandshakeFailed ";
case k_EResultIOFailure: return "IOFailure ";
case k_EResultRemoteDisconnect: return "RemoteDisconnect ";
case k_EResultShoppingCartNotFound: return "ShoppingCartNotFound // failed to find the shopping cart requested";
case k_EResultBlocked: return "Blocked // a user didn't allow it";
case k_EResultIgnored: return "Ignored // target is ignoring sender";
case k_EResultNoMatch: return "NoMatch // nothing matching the request found";
case k_EResultAccountDisabled: return "AccountDisabled ";
case k_EResultServiceReadOnly: return "ServiceReadOnly // this service is not accepting content changes right now";
case k_EResultAccountNotFeatured: return "AccountNotFeatured // account doesn't have value, so this feature isn't available";
case k_EResultAdministratorOK: return "AdministratorOK // allowed to take this action, but only because requester is admin";
case k_EResultContentVersion: return "ContentVersion // A Version mismatch in content transmitted within the Steam protocol.";
case k_EResultTryAnotherCM: return "TryAnotherCM // The current CM can't service the user making a request, user should try another.";
case k_EResultPasswordRequiredToKickSession: return "PasswordRequiredToKickSession // You are already logged in elsewhere, this cached credential login has failed.";
case k_EResultAlreadyLoggedInElsewhere: return "AlreadyLoggedInElsewhere // You are already logged in elsewhere, you must wait";
case k_EResultSuspended: return "Suspended // Long running operation (content download) suspended/paused";
case k_EResultCancelled: return "Cancelled // Operation canceled (typically by user: content download)";
case k_EResultDataCorruption: return "DataCorruption // Operation canceled because data is ill formed or unrecoverable";
case k_EResultDiskFull: return "DiskFull // Operation canceled - not enough disk space.";
case k_EResultRemoteCallFailed: return "RemoteCallFailed // an remote call or IPC call failed";
case k_EResultPasswordUnset: return "PasswordUnset // Password could not be verified as it's unset server side";
case k_EResultExternalAccountUnlinked: return "ExternalAccountUnlinked // External account (PSN, Facebook...) is not linked to a Steam account";
case k_EResultPSNTicketInvalid: return "PSNTicketInvalid // PSN ticket was invalid";
case k_EResultExternalAccountAlreadyLinked: return "ExternalAccountAlreadyLinked // External account (PSN, Facebook...) is already linked to some other account, must explicitly request to replace/delete the link first";
case k_EResultRemoteFileConflict: return "RemoteFileConflict // The sync cannot resume due to a conflict between the local and remote files";
case k_EResultIllegalPassword: return "IllegalPassword // The requested new password is not legal";
case k_EResultSameAsPreviousValue: return "SameAsPreviousValue // new value is the same as the old one ( secret question and answer )";
case k_EResultAccountLogonDenied: return "AccountLogonDenied // account login denied due to 2nd factor authentication failure";
case k_EResultCannotUseOldPassword: return "CannotUseOldPassword // The requested new password is not legal";
case k_EResultInvalidLoginAuthCode: return "InvalidLoginAuthCode // account login denied due to auth code invalid";
case k_EResultAccountLogonDeniedNoMail: return "AccountLogonDeniedNoMail // account login denied due to 2nd factor auth failure - and no mail has been sent";
case k_EResultHardwareNotCapableOfIPT: return "HardwareNotCapableOfIPT // ";
case k_EResultIPTInitError: return "IPTInitError // ";
case k_EResultParentalControlRestricted: return "ParentalControlRestricted // operation failed due to parental control restrictions for current user";
case k_EResultFacebookQueryError: return "FacebookQueryError // Facebook query returned an error";
case k_EResultExpiredLoginAuthCode: return "ExpiredLoginAuthCode // account login denied due to auth code expired";
case k_EResultIPLoginRestrictionFailed: return "IPLoginRestrictionFailed ";
case k_EResultAccountLockedDown: return "AccountLockedDown ";
case k_EResultAccountLogonDeniedVerifiedEmailRequired: return "AccountLogonDeniedVerifiedEmailRequired ";
case k_EResultNoMatchingURL: return "NoMatchingURL ";
case k_EResultBadResponse: return "BadResponse // parse failure, missing field, etc.";
case k_EResultRequirePasswordReEntry: return "RequirePasswordReEntry // The user cannot complete the action until they re-enter their password";
case k_EResultValueOutOfRange: return "ValueOutOfRange // the value entered is outside the acceptable range";
case k_EResultUnexpectedError: return "UnexpectedError // something happened that we didn't expect to ever happen";
case k_EResultDisabled: return "Disabled // The requested service has been configured to be unavailable";
case k_EResultInvalidCEGSubmission: return "InvalidCEGSubmission // The set of files submitted to the CEG server are not valid !";
case k_EResultRestrictedDevice: return "RestrictedDevice // The device being used is not allowed to perform this action";
case k_EResultRegionLocked: return "RegionLocked // The action could not be complete because it is region restricted";
case k_EResultRateLimitExceeded: return "RateLimitExceeded // Temporary rate limit exceeded, try again later, different from k_EResultLimitExceeded which may be permanent";
case k_EResultAccountLoginDeniedNeedTwoFactor: return "AccountLoginDeniedNeedTwoFactor // Need two-factor code to login";
case k_EResultItemDeleted: return "ItemDeleted // The thing we're trying to access has been deleted";
case k_EResultAccountLoginDeniedThrottle: return "AccountLoginDeniedThrottle // login attempt failed, try to throttle response to possible attacker";
case k_EResultTwoFactorCodeMismatch: return "TwoFactorCodeMismatch // two factor code mismatch";
case k_EResultTwoFactorActivationCodeMismatch: return "TwoFactorActivationCodeMismatch // activation code for two-factor didn't match";
case k_EResultAccountAssociatedToMultiplePartners: return "AccountAssociatedToMultiplePartners // account has been associated with multiple partners";
case k_EResultNotModified: return "NotModified // data not modified";
default: return "Unknown EResult";
}
}
const char* EItemUpdateStatus2String(EItemUpdateStatus status)
{
switch (status)
{
case k_EItemUpdateStatusInvalid: return "handle was invalid, job might be finished, listen too SubmitItemUpdateResult_t";
case k_EItemUpdateStatusPreparingConfig: return "processing configuration data";
case k_EItemUpdateStatusPreparingContent: return "reading and processing content files";
case k_EItemUpdateStatusUploadingContent: return "uploading content changes to Steam";
case k_EItemUpdateStatusUploadingPreviewFile: return "uploading new preview file image";
case k_EItemUpdateStatusCommittingChanges: return "committing all changes";
default: return "Unkown EUpdateStatus";
}
}
const char* EPublishedFileVisibility2String(ERemoteStoragePublishedFileVisibility status)
{
switch (status)
{
case k_ERemoteStoragePublishedFileVisibilityPublic: return "Public";
case k_ERemoteStoragePublishedFileVisibilityFriendsOnly: return "FriendsOnly";
case k_ERemoteStoragePublishedFileVisibilityPrivate: return "Private";
default: return "Unknown Visibility";
}
}
#endif
static unordered_map<string, int> &cloudIndex()
{
static unordered_map<string, int> *files = NULL;
if (!files)
{
// std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
files = new unordered_map<string, int>();
#if HAS_STEAM
ISteamRemoteStorage *ss = SteamRemoteStorage();
if (ss)
{
const int32 count = ss->GetFileCount();
int totalBytes = 0;
for (int i=0; i<count; i++)
{
int32 size = 0;
const char* name = ss->GetFileNameAndSize(i, &size);
if (name)
{
(*files)[name] = size;
totalBytes += size;
DPRINT(SAVE, ("steam cloud/%s %d bytes", name, size));
}
}
DPRINT(STEAM, ("Created Steam Cloud file index with %d files, %s",
(int)files->size(), str_bytes_format(totalBytes).c_str()));
}
#endif
#if HAS_GOG
galaxy::api::IStorage *is = galaxy::api::Storage();
if (is)
{
const uint32_t count = is->GetFileCount();
if (count)
{
int totalBytes = 0;
for (int i=0; i<count; i++)
{
const char* name = is->GetFileNameByIndex(i);
CheckGOGError("GetFileNameByIndex");
if (name)
{
uint32_t size = is->GetFileSize(name);
CheckGOGError("GetFileSize()");
(*files)[name] = size;
totalBytes += size;
DPRINT(SAVE, ("gog cloud/%s %d bytes", name, size));
}
}
DPRINT(GOG, ("Created GOG Galaxy Cloud file index with %d files, %s",
(int)files->size(), str_bytes_format(totalBytes).c_str()));
}
}
#endif
}
return *files;
}
int SteamFileExists(const string &fname)
{
std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
return map_get(cloudIndex(), fname, 0);
}
bool SteamFileDelete(const char* fname)
{
if (!SteamFileExists(fname))
return false;
{
std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
cloudIndex().erase(fname);
}
#if HAS_STEAM
ISteamRemoteStorage *ss = SteamRemoteStorage();
if (ss)
{
const bool success = ss->FileDelete(fname);
ASSERTF(success, "FileDelete failed on '%s'", fname);
return success;
}
#endif
#if HAS_GOG
galaxy::api::IStorage *is = galaxy::api::Storage();
if (is)
{
return GOG_CALL( is->FileDelete(fname) );
}
#endif
return false;
}
bool SteamFileForget(const char* fname)
{
#if HAS_STEAM
ISteamRemoteStorage *ss = SteamRemoteStorage();
return ss && ss->FileForget(fname);
#else
return false;
#endif
}
int SteamFileCount()
{
std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
return cloudIndex().size();
}
void SteamForEachFile(std::function<void(const string&, int)> fun)
{
std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
foreach (const auto &it, cloudIndex())
{
fun(it.first, it.second);
}
}
bool isCloudEnabled()
{
return kSteamCloudEnable && !OLG_UseDevSavePath() &&
(
#if HAS_STEAM
SteamRemoteStorage() ||
#endif
#if HAS_GOG
galaxy::api::Storage() ||
#endif
false);
}
bool steamFileWrite(const char* fname, const char* data, int size, int ucsize)
{
#if HAS_STEAM || HAS_GOG
# if HAS_STEAM
ISteamRemoteStorage *ss = s_steam_subscribed ? SteamRemoteStorage(): NULL;
if (ss && !ss->FileWrite(fname, data, size))
{
int32 totalBytes=0, availableBytes=0;
ss->GetQuota(&totalBytes, &availableBytes);
const int32 count = ss->GetFileCount();
ASSERT_FAILED("FileWrite", "Failed to save '%s' (size=%s, uncompressed=%s) (%d/%d files used) (%s/%s available) (EnabledForAccount=%s, ForApp=%s)",
fname,
str_bytes_format(size).c_str(),
str_bytes_format(ucsize).c_str(),
count, 1000,
str_bytes_format(availableBytes).c_str(),
str_bytes_format(totalBytes).c_str(),
ss->IsCloudEnabledForAccount() ? "YES" : "NO",
ss->IsCloudEnabledForApp() ? "YES" : "NO");
ASSERT(size < k_unMaxCloudFileChunkSize);
OL_ScheduleUploadLog("Steam Write Failed");
return false;
}
# endif
# if HAS_GOG
galaxy::api::IStorage *is = galaxy::api::Storage();
if (is && !GOG_CALL( is->FileWrite(fname, data, size) ))
{
ASSERT_FAILED("FileWrite", "failed to save '%s' (size=%s, uncompressed=%s)",
fname, str_bytes_format(size).c_str(), str_bytes_format(ucsize).c_str());
return false;
}
# endif
{
std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
cloudIndex()[fname] = size;
}
DPRINT(SAVE, ("save cloud/%s %d bytes", fname, size));
return true;
#else
return false;
#endif
}
int g_steamReadsFailed;
string steamFileRead(int size, const char* fname)
{
string data;
#if HAS_STEAM || HAS_GOG
if (!size)
return data;
data.resize(size);
int32 bytes = 0;
# if HAS_STEAM
ISteamRemoteStorage *ss = s_steam_subscribed ? SteamRemoteStorage(): NULL;
if (ss)
bytes = ss->FileRead(fname, &data[0], size);
# endif
# if HAS_GOG
galaxy::api::IStorage *is = galaxy::api::Storage();
if (is && bytes == 0)
{
bytes = is->FileRead(fname, &data[0], size);
CheckGOGError("FileRead");
}
# endif
ASSERTF(bytes == size, "Expected %d, got %d: '%s'", size, bytes, fname);
if (bytes != size)
{
g_steamReadsFailed++;
data.resize(bytes);
}
if (bytes) {
DPRINT(SAVE, ("load cloud/%s %d bytes", fname, bytes));
}
#endif
return data;
}
int steamDeleteRecursive(const char *path)
{
int deleted = 0;
#if HAS_STEAM || HAS_GOG
# if HAS_STEAM
if (!SteamRemoteStorage())
return 0;
# endif
# if HAS_GOG
if (!galaxy::api::Storage())
return 0;
# endif
std::lock_guard<std::mutex> l(SteamStats::instance().deleteMutex);
if (SteamFileDelete(path))
{
DPRINT(STEAM, ("Deleted cloud file '%s'", path));
return 1;
}
vector<string> files;
const string base = str_path_standardize(path) + "/";
{
std::lock_guard<std::mutex> m(SteamStats::instance().indexMutex);
foreach(const auto &it, cloudIndex())
{
if (str_startswith(it.first, base))
files.push_back(it.first);
}
}
for_(name, files)
{
if (SteamFileDelete(name.c_str()))
{
deleted++;
DPRINT(SAVE, ("Deleted steam/%s", name.c_str()));
}
}
DPRINT(STEAM, ("Deleted %d/%d cloud files from '%s' (%s)",
deleted, (int)files.size(), path, (files.size() == 1 ? files[0].c_str() : "")));
#endif
return deleted;
}
static void runSteamTests()
{
#if HAS_STEAM
ISteamRemoteStorage *ss = SteamRemoteStorage();
if (!ss)
return;
const double kMicroSec = 1e6;