forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheevos_client.c
1860 lines (1566 loc) · 57.7 KB
/
cheevos_client.c
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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2019-2021 - Brian Weiss
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "cheevos_client.h"
#include "cheevos.h"
#include "../configuration.h"
#include "../file_path_special.h"
#include "../paths.h"
#include "../retroarch.h"
#include "../version.h"
#include <features/features_cpu.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#include "../frontend/frontend_driver.h"
#include "../network/net_http_special.h"
#include "../tasks/tasks_internal.h"
#ifdef HAVE_PRESENCE
#include "../network/presence.h"
#endif
#include "../deps/rcheevos/include/rc_api_runtime.h"
#include "../deps/rcheevos/include/rc_api_user.h"
/* Define this macro to log URLs. */
#undef CHEEVOS_LOG_URLS
/* Define this macro to have the password and token logged.
* THIS WILL DISCLOSE THE USER'S PASSWORD, TAKE CARE! */
#undef CHEEVOS_LOG_PASSWORD
/* Define this macro to load a JSON file from disk instead of downloading
* from retroachievements.org. */
#undef CHEEVOS_JSON_OVERRIDE
/* Define this macro with a string to save the JSON file to disk with
* that name. */
#undef CHEEVOS_SAVE_JSON
/* Define this macro to log downloaded badge images. */
#undef CHEEVOS_LOG_BADGES
/* Number of usecs to wait between posting rich presence to the site. */
/* Keep consistent with SERVER_PING_FREQUENCY from RAIntegration. */
#define CHEEVOS_PING_FREQUENCY 2 * 60 * 1000000
/****************************
* data types *
****************************/
enum rcheevos_async_io_type
{
CHEEVOS_ASYNC_RICHPRESENCE = 0,
CHEEVOS_ASYNC_AWARD_ACHIEVEMENT,
CHEEVOS_ASYNC_SUBMIT_LBOARD,
CHEEVOS_ASYNC_LOGIN,
CHEEVOS_ASYNC_RESOLVE_HASH,
CHEEVOS_ASYNC_FETCH_GAME_DATA,
CHEEVOS_ASYNC_FETCH_USER_UNLOCKS,
CHEEVOS_ASYNC_FETCH_HARDCORE_USER_UNLOCKS,
CHEEVOS_ASYNC_START_SESSION,
CHEEVOS_ASYNC_FETCH_BADGE
};
struct rcheevos_async_io_request;
typedef void (*rcheevos_async_handler)(struct rcheevos_async_io_request *request,
http_transfer_data_t *data, char buffer[], size_t buffer_size);
typedef struct rcheevos_async_io_request
{
rc_api_request_t request;
rcheevos_async_handler handler;
int id;
rcheevos_client_callback callback;
void* callback_data;
int attempt_count;
const char* success_message;
const char* failure_message;
const char* user_agent;
char type;
} rcheevos_async_io_request;
#ifdef HAVE_THREADS
#define RCHEEVOS_CONCURRENT_BADGE_DOWNLOADS 2
#else
#define RCHEEVOS_CONCURRENT_BADGE_DOWNLOADS 1
#endif
typedef struct rcheevos_fetch_badge_state
{
unsigned badge_fetch_index;
unsigned locked_badge_fetch_index;
const char* badge_directory;
rcheevos_client_callback callback;
void* callback_data;
char requested_badges[
RCHEEVOS_CONCURRENT_BADGE_DOWNLOADS][32];
} rcheevos_fetch_badge_state;
typedef struct rcheevos_fetch_badge_data
{
rcheevos_fetch_badge_state* state;
int request_index;
void* data;
size_t data_len;
rcheevos_client_callback callback;
} rcheevos_fetch_badge_data;
/****************************
* forward declarations *
****************************/
static retro_time_t rcheevos_client_prepare_ping(rcheevos_async_io_request* request);
static void rcheevos_async_http_task_callback(
retro_task_t* task, void* task_data, void* user_data, const char* error);
static void rcheevos_async_end_request(rcheevos_async_io_request* request);
static void rcheevos_async_fetch_badge_callback(
struct rcheevos_async_io_request* request,
http_transfer_data_t* data, char buffer[], size_t buffer_size);
/****************************
* user agent construction *
****************************/
static int append_no_spaces(char* buffer, char* stop, const char* text)
{
char* ptr = buffer;
while (ptr < stop && *text)
{
if (*text == ' ')
{
*ptr++ = '_';
++text;
}
else
*ptr++ = *text++;
}
*ptr = '\0';
return (int)(ptr - buffer);
}
void rcheevos_get_user_agent(rcheevos_locals_t *locals,
char *buffer, size_t len)
{
char* ptr;
struct retro_system_info *sysinfo = &runloop_state_get_ptr()->system.info;
/* if we haven't calculated the non-changing portion yet, do so now
* [retroarch version + os version] */
if (!locals->user_agent_prefix[0])
{
const frontend_ctx_driver_t *frontend = frontend_get_ptr();
if (frontend && frontend->get_os)
{
char tmp[64];
int major, minor;
frontend->get_os(tmp, sizeof(tmp), &major, &minor);
snprintf(locals->user_agent_prefix, sizeof(locals->user_agent_prefix),
"RetroArch/%s (%s %d.%d)", PACKAGE_VERSION, tmp, major, minor);
}
else
snprintf(locals->user_agent_prefix, sizeof(locals->user_agent_prefix),
"RetroArch/%s", PACKAGE_VERSION);
}
/* append the non-changing portion */
ptr = buffer + strlcpy(buffer, locals->user_agent_prefix, len);
/* if a core is loaded, append its information */
if (sysinfo && !string_is_empty(sysinfo->library_name))
{
char* stop = buffer + len - 1;
const char* path = path_get(RARCH_PATH_CORE);
*ptr++ = ' ';
if (!string_is_empty(path))
{
append_no_spaces(ptr, stop, path_basename(path));
path_remove_extension(ptr);
ptr += strlen(ptr);
}
else
ptr += append_no_spaces(ptr, stop, sysinfo->library_name);
if (sysinfo->library_version)
{
*ptr++ = '/';
ptr += append_no_spaces(ptr, stop, sysinfo->library_version);
}
}
*ptr = '\0';
}
#ifdef CHEEVOS_LOG_URLS
#ifndef CHEEVOS_LOG_PASSWORD
static void rcheevos_filter_url_param(char* url, char* param)
{
char *next;
size_t param_len = strlen(param);
char *start = strchr(url, '?');
if (!start)
start = url;
else
++start;
do
{
next = strchr(start, '&');
if (start[param_len] == '=' && memcmp(start, param, param_len) == 0)
{
if (next)
strcpy_literal(start, next + 1);
else if (start > url)
start[-1] = '\0';
else
*start = '\0';
return;
}
if (!next)
return;
start = next + 1;
} while (1);
}
#endif
#endif
void rcheevos_log_url(const char* api, const char* url)
{
#ifdef CHEEVOS_LOG_URLS
#ifdef CHEEVOS_LOG_PASSWORD
CHEEVOS_LOG(RCHEEVOS_TAG "GET %s\n", url);
#else
char copy[256];
strlcpy(copy, url, sizeof(copy));
rcheevos_filter_url_param(copy, "p");
rcheevos_filter_url_param(copy, "t");
CHEEVOS_LOG(RCHEEVOS_TAG "GET %s\n", copy);
#endif
#else
(void)api;
(void)url;
#endif
}
static void rcheevos_log_post_url(const char* url, const char* post)
{
#ifdef CHEEVOS_LOG_URLS
#ifdef CHEEVOS_LOG_PASSWORD
if (post && post[0])
CHEEVOS_LOG(RCHEEVOS_TAG "POST %s %s\n", url, post);
else
CHEEVOS_LOG(RCHEEVOS_TAG "POST %s\n", url);
#else
if (post && post[0])
{
char post_copy[2048];
strlcpy(post_copy, post, sizeof(post_copy));
rcheevos_filter_url_param(post_copy, "p");
rcheevos_filter_url_param(post_copy, "t");
if (post_copy[0])
CHEEVOS_LOG(RCHEEVOS_TAG "POST %s %s\n", url, post_copy);
else
CHEEVOS_LOG(RCHEEVOS_TAG "POST %s\n", url);
}
else
{
CHEEVOS_LOG(RCHEEVOS_TAG "POST %s\n", url);
}
#endif
#else
(void)url;
(void)post;
#endif
}
/****************************
* dispatch *
****************************/
static void rcheevos_async_begin_http_request(rcheevos_async_io_request* request)
{
if (request->request.post_data)
task_push_http_post_transfer_with_user_agent(request->request.url,
request->request.post_data, true, "POST", request->user_agent,
rcheevos_async_http_task_callback, request);
else
task_push_http_transfer_with_user_agent(request->request.url,
true, "GET", request->user_agent,
rcheevos_async_http_task_callback, request);
}
static void rcheevos_async_retry_request(retro_task_t* task)
{
rcheevos_async_io_request* request = (rcheevos_async_io_request*)
task->user_data;
/* the timer task has done its job. let it dispose itself */
task_set_finished(task, 1);
/* start a new task for the HTTP call */
rcheevos_async_begin_http_request(request);
}
static void rcheevos_async_retry_request_after_delay(rcheevos_async_io_request* request, const char* error)
{
retro_task_t* task = task_init();
/* Double the wait between each attempt until we hit
* a maximum delay of two minutes.
* 250ms -> 500ms -> 1s -> 2s -> 4s -> 8s -> 16s -> 32s -> 64s -> 120s -> 120s... */
retro_time_t retry_delay = (request->attempt_count > 8)
? (120 * 1000 * 1000)
: ((250 * 1000) << request->attempt_count);
CHEEVOS_ERR(RCHEEVOS_TAG "%s %u: %s (automatic retry in %dms)\n", request->failure_message,
request->id, error, (int)retry_delay / 1000);
task->when = cpu_features_get_time_usec() + retry_delay;
task->handler = rcheevos_async_retry_request;
task->user_data = request;
task->progress = -1;
++request->attempt_count;
task_queue_push(task);
}
static bool rcheevos_async_request_failed(rcheevos_async_io_request* request, const char* error)
{
/* always retry any request once (attempt_count==0) in case of network hiccup */
if (request->attempt_count > 0)
{
/* retry failed, don't retry these requests */
switch (request->type)
{
/* timer will ping again */
case CHEEVOS_ASYNC_RICHPRESENCE:
/* fallback to the placeholder image */
case CHEEVOS_ASYNC_FETCH_BADGE:
return false;
case CHEEVOS_ASYNC_RESOLVE_HASH:
case CHEEVOS_ASYNC_LOGIN:
/* make a maximum of four attempts
(0ms -> 250ms -> 500ms -> 1s) */
if (request->attempt_count == 3)
return false;
break;
default:
break;
}
}
/* automatically retry the request */
rcheevos_async_retry_request_after_delay(request, error);
return true;
}
static void rcheevos_async_http_task_callback(
retro_task_t* task, void* task_data, void* user_data,
const char* error)
{
rcheevos_async_io_request *request = (rcheevos_async_io_request*)user_data;
http_transfer_data_t *data = (http_transfer_data_t*)task_data;
const bool aborted = rcheevos_load_aborted();
char buffer[224];
if (aborted)
{
/* load was aborted. don't process the response */
strlcpy(buffer, "Load aborted", sizeof(buffer));
}
else if (error)
{
/* there was a communication error */
/* if automatically requeued, don't process any further */
if (rcheevos_async_request_failed(request, error))
return;
strlcpy(buffer, error, sizeof(buffer));
}
else if (!data)
{
/* Server did not return HTTP headers */
strlcpy(buffer, "Server communication error", sizeof(buffer));
}
else if (!data->data || !data->len)
{
if (data->status <= 0)
{
/* something occurred which prevented the response from being processed.
* assume the server request hasn't happened and try again. */
snprintf(buffer, sizeof(buffer), "task status code %d", data->status);
rcheevos_async_request_failed(request, buffer);
return;
}
if (data->status != 200) /* Server returned error via status code. */
{
snprintf(buffer, sizeof(buffer), "HTTP error code %d", data->status);
if (request->type == CHEEVOS_ASYNC_FETCH_BADGE)
{
/* This isn't a JSON request. An empty response with a status code is a valid response. */
if (request->handler)
request->handler(request, data, buffer, sizeof(buffer));
}
}
else /* Server sent empty response without error status code */
strlcpy(buffer, "No response from server", sizeof(buffer));
}
else
{
/* indicate success unless handler provides error */
buffer[0] = '\0';
/* Call appropriate handler to process the response */
/* NOTE: data->data is not null-terminated. Most handlers assume the
* response is properly formatted or will encounter a parse failure
* before reading past the end of the data */
if (request->handler)
request->handler(request, data, buffer, sizeof(buffer));
}
if (!buffer[0])
{
/* success */
if (request->success_message)
{
if (request->id)
CHEEVOS_LOG(RCHEEVOS_TAG "%s %u\n", request->success_message, request->id);
else
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", request->success_message);
}
}
else
{
/* encountered an error */
char errbuf[256];
if (request->id)
snprintf(errbuf, sizeof(errbuf), "%s %u: %s",
request->failure_message, request->id, buffer);
else
snprintf(errbuf, sizeof(errbuf), "%s: %s",
request->failure_message, buffer);
if (!aborted)
{
switch (request->type)
{
case CHEEVOS_ASYNC_RICHPRESENCE:
case CHEEVOS_ASYNC_FETCH_BADGE:
/* Don't bother informing user when these fail */
break;
case CHEEVOS_ASYNC_LOGIN:
case CHEEVOS_ASYNC_RESOLVE_HASH:
if (error)
{
rcheevos_locals_t* rcheevos_locals = get_rcheevos_locals();
size_t len = 0;
char* ptr;
if (rcheevos_locals->load_info.state
== RCHEEVOS_LOAD_STATE_NETWORK_ERROR)
break;
rcheevos_locals->load_info.state =
RCHEEVOS_LOAD_STATE_NETWORK_ERROR;
while (
/* find the first single slash */
request->request.url[len] != '/'
|| request->request.url[len + 1] == '/'
|| request->request.url[len - 1] == '/')
++len;
ptr = errbuf + snprintf(errbuf, sizeof(errbuf),
"Could not communicate with ");
memcpy(ptr, request->request.url, len);
ptr[len] = '\0';
}
/* fallthrough to default */
default:
runloop_msg_queue_push(errbuf, 0, 5 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR);
break;
}
}
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", errbuf);
}
rcheevos_async_end_request(request);
}
static void rcheevos_async_end_request(rcheevos_async_io_request* request)
{
rc_api_destroy_request(&request->request);
if (request->callback && !rcheevos_load_aborted())
request->callback(request->callback_data);
/* rich presence request will be reused on next ping - reset the attempt
* counter. for all other request types, free the request object */
if (request->type == CHEEVOS_ASYNC_RICHPRESENCE)
request->attempt_count = 0;
else
free(request);
}
static void rcheevos_async_begin_request(
rcheevos_async_io_request* request, int init_result,
rcheevos_async_handler handler, char type, int id,
const char* success_message, const char* failure_message)
{
if (init_result != RC_OK)
{
char errbuf[256];
if (id)
snprintf(errbuf, sizeof(errbuf), "%s %u: %s",
failure_message, id, rc_error_str(init_result));
else
snprintf(errbuf, sizeof(errbuf), "%s: %s",
failure_message, rc_error_str(init_result));
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", errbuf);
runloop_msg_queue_push(errbuf, 0, 5 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR);
rcheevos_async_end_request(request);
return;
}
request->handler = handler;
request->type = type;
request->id = id;
request->success_message = success_message;
request->failure_message = failure_message;
request->attempt_count = 0;
if (!request->user_agent)
request->user_agent = get_rcheevos_locals()->user_agent_core;
rcheevos_log_post_url(request->request.url, request->request.post_data);
rcheevos_async_begin_http_request(request);
}
static bool rcheevos_async_succeeded(int result,
const rc_api_response_t* response, char buffer[],
size_t buffer_size)
{
if (result != RC_OK)
{
strlcpy(buffer, rc_error_str(result), buffer_size);
return false;
}
if (!response->succeeded)
{
strlcpy(buffer, response->error_message, buffer_size);
return false;
}
return true;
}
void rcheevos_client_initialize(void)
{
const settings_t* settings = config_get_ptr();
const char* host = settings->arrays.cheevos_custom_host;
if (!host[0])
{
#ifdef HAVE_SSL
host = "https://retroachievements.org";
#else
host = "http://retroachievements.org";
#endif
}
CHEEVOS_LOG(RCHEEVOS_TAG "Using host: %s\n", host);
if (!string_is_equal(host, "https://retroachievements.org"))
{
rc_api_set_host(host);
if (!string_is_equal(host, "http://retroachievements.org"))
rc_api_set_image_host(host);
}
}
/****************************
* login *
****************************/
static void rcheevos_async_login_callback(
struct rcheevos_async_io_request* request,
http_transfer_data_t* data, char buffer[], size_t buffer_size)
{
rcheevos_locals_t* rcheevos_locals = get_rcheevos_locals();
rc_api_login_response_t api_response;
int result = rc_api_process_login_response(&api_response, data->data);
if (rcheevos_async_succeeded(result, &api_response.response,
buffer, buffer_size))
{
/* save the token to the config and clear the password on success */
settings_t* settings = config_get_ptr();
strlcpy(settings->arrays.cheevos_token, api_response.api_token,
sizeof(settings->arrays.cheevos_token));
settings->arrays.cheevos_password[0] = '\0';
CHEEVOS_LOG(RCHEEVOS_TAG "%s logged in successfully\n",
api_response.display_name);
strlcpy(rcheevos_locals->displayname, api_response.display_name,
sizeof(rcheevos_locals->displayname));
strlcpy(rcheevos_locals->username, api_response.username,
sizeof(rcheevos_locals->username));
strlcpy(rcheevos_locals->token, api_response.api_token,
sizeof(rcheevos_locals->token));
}
else
rcheevos_locals->token[0] = '\0';
rc_api_destroy_login_response(&api_response);
}
static void rcheevos_client_login(const char* username,
const char* password, const char* token,
rcheevos_client_callback callback, void* userdata)
{
rcheevos_async_io_request* request = (rcheevos_async_io_request*)
calloc(1, sizeof(rcheevos_async_io_request));
if (!request)
{
CHEEVOS_LOG(RCHEEVOS_TAG "Failed to allocate login request\n");
}
else
{
rc_api_login_request_t api_params;
int result;
memset(&api_params, 0, sizeof(api_params));
api_params.username = username;
api_params.password = password;
api_params.api_token = token;
result = rc_api_init_login_request(&request->request, &api_params);
request->callback = callback;
request->callback_data = userdata;
rcheevos_async_begin_request(request, result,
rcheevos_async_login_callback,
CHEEVOS_ASYNC_LOGIN, 0,
NULL,
"Error logging in");
}
}
void rcheevos_client_login_with_password(const char* username,
const char* password,
rcheevos_client_callback callback, void* userdata)
{
rcheevos_client_login(username, password, NULL, callback, userdata);
}
void rcheevos_client_login_with_token(const char* username,
const char* token,
rcheevos_client_callback callback, void* userdata)
{
rcheevos_client_login(username, NULL, token, callback, userdata);
}
/****************************
* identify game *
****************************/
static void rcheevos_async_resolve_hash_callback(
struct rcheevos_async_io_request* request,
http_transfer_data_t* data, char buffer[], size_t buffer_size)
{
rc_api_resolve_hash_response_t api_response;
int result = rc_api_process_resolve_hash_response(&api_response,
data->data);
if (rcheevos_async_succeeded(result, &api_response.response,
buffer, buffer_size))
{
rcheevos_locals_t* rcheevos_locals = get_rcheevos_locals();
rcheevos_locals->game.id = api_response.game_id;
}
rc_api_destroy_resolve_hash_response(&api_response);
}
void rcheevos_client_identify_game(const char* hash,
rcheevos_client_callback callback, void* userdata)
{
rcheevos_async_io_request* request = (rcheevos_async_io_request*)
calloc(1, sizeof(rcheevos_async_io_request));
if (!request)
{
CHEEVOS_LOG(RCHEEVOS_TAG "Failed to allocate game identification request\n");
}
else
{
rc_api_resolve_hash_request_t api_params;
int result;
memset(&api_params, 0, sizeof(api_params));
api_params.game_hash = hash;
result = rc_api_init_resolve_hash_request(&request->request, &api_params);
request->callback = callback;
request->callback_data = userdata;
rcheevos_async_begin_request(request, result,
rcheevos_async_resolve_hash_callback,
CHEEVOS_ASYNC_RESOLVE_HASH, 0,
NULL,
"Error resolving hash");
}
}
/****************************
* initialize runtime *
****************************/
typedef struct rcheevos_async_initialize_runtime_data_t
{
rc_api_fetch_game_data_response_t game_data;
rc_api_fetch_user_unlocks_response_t hardcore_unlocks;
rc_api_fetch_user_unlocks_response_t non_hardcore_unlocks;
rcheevos_client_callback callback;
void* callback_data;
} rcheevos_async_initialize_runtime_data_t;
static void rcheevos_client_copy_achievements(
rcheevos_async_initialize_runtime_data_t* runtime_data)
{
unsigned i, j;
const rc_api_achievement_definition_t* definition;
rcheevos_racheevo_t *achievement;
rcheevos_locals_t *rcheevos_locals = get_rcheevos_locals();
const settings_t *settings = config_get_ptr();
rcheevos_locals->game.achievements = (rcheevos_racheevo_t*)
calloc(runtime_data->game_data.num_achievements,
sizeof(rcheevos_racheevo_t));
achievement = rcheevos_locals->game.achievements;
if (!achievement)
{
CHEEVOS_ERR(RCHEEVOS_TAG "Could not allocate achievements\n");
return;
}
definition = runtime_data->game_data.achievements;
for (i = 0; i < runtime_data->game_data.num_achievements;
++i, ++definition)
{
/* invalid definition, ignore */
if (
definition->category == 0
|| !definition->definition
|| !definition->definition[0]
|| !definition->title
|| !definition->title[0]
|| !definition->description
|| !definition->description[0])
continue;
if (definition->category != 3)
{
if (!settings->bools.cheevos_test_unofficial)
continue;
achievement->active = RCHEEVOS_ACTIVE_UNOFFICIAL
| RCHEEVOS_ACTIVE_SOFTCORE
| RCHEEVOS_ACTIVE_HARDCORE;
}
else
{
achievement->active = RCHEEVOS_ACTIVE_SOFTCORE
| RCHEEVOS_ACTIVE_HARDCORE;
for (j = 0; j <
runtime_data->hardcore_unlocks.num_achievement_ids; ++j)
{
if (runtime_data->hardcore_unlocks.achievement_ids[j]
== definition->id)
{
achievement->active &= ~(RCHEEVOS_ACTIVE_HARDCORE
| RCHEEVOS_ACTIVE_SOFTCORE);
break;
}
}
if ((achievement->active & RCHEEVOS_ACTIVE_SOFTCORE) != 0)
{
for (j = 0; j <
runtime_data->non_hardcore_unlocks.num_achievement_ids;
++j)
{
if (runtime_data->non_hardcore_unlocks.achievement_ids[j] == definition->id)
{
achievement->active &= ~RCHEEVOS_ACTIVE_SOFTCORE;
break;
}
}
}
}
achievement->id = definition->id;
achievement->title = strdup(definition->title);
achievement->description = strdup(definition->description);
achievement->badge = strdup(definition->badge_name);
achievement->points = definition->points;
/* If an achievement has been fully unlocked,
* we don't need to keep the definition around
* as it won't be reactivated. Otherwise,
* we do have to keep a copy of it. */
if ((achievement->active & (
RCHEEVOS_ACTIVE_HARDCORE
| RCHEEVOS_ACTIVE_SOFTCORE)) != 0)
achievement->memaddr = strdup(definition->definition);
++achievement;
}
rcheevos_locals->game.achievement_count = (unsigned)(achievement
- rcheevos_locals->game.achievements);
}
static void rcheevos_client_copy_leaderboards(
rcheevos_async_initialize_runtime_data_t* runtime_data)
{
unsigned i;
rcheevos_ralboard_t *leaderboard;
const rc_api_leaderboard_definition_t *definition;
rcheevos_locals_t* rcheevos_locals = get_rcheevos_locals();
rcheevos_locals->game.leaderboards = (rcheevos_ralboard_t*)
calloc(runtime_data->game_data.num_leaderboards,
sizeof(rcheevos_ralboard_t));
rcheevos_locals->game.leaderboard_count =
runtime_data->game_data.num_leaderboards;
leaderboard = rcheevos_locals->game.leaderboards;
if (!leaderboard)
{
CHEEVOS_ERR(RCHEEVOS_TAG "Could not allocate leaderboards\n");
return;
}
definition = runtime_data->game_data.leaderboards;
for (i = 0; i < runtime_data->game_data.num_leaderboards; ++i, ++definition, ++leaderboard)
{
leaderboard->id = definition->id;
leaderboard->title = strdup(definition->title);
leaderboard->description = strdup(definition->description);
leaderboard->mem = strdup(definition->definition);
leaderboard->format = definition->format;
}
}
static void rcheevos_client_initialize_runtime_rich_presence(
rcheevos_async_initialize_runtime_data_t* runtime_data)
{
if (runtime_data->game_data.rich_presence_script && *runtime_data->game_data.rich_presence_script)
{
rcheevos_locals_t* rcheevos_locals = get_rcheevos_locals();
/* Just activate the rich presence script now.
* It can't be toggled on or off,
* so there's no reason to keep the unparsed version
* around any longer than necessary, and we can avoid
* making a copy in the process. */
int result = rc_runtime_activate_richpresence(
&rcheevos_locals->runtime,
runtime_data->game_data.rich_presence_script, NULL, 0);
if (result != RC_OK)
{
const settings_t* settings = config_get_ptr();
char buffer[256];
snprintf(buffer, sizeof(buffer),
"Could not activate rich presence: %s",
rc_error_str(result));
if (settings->bools.cheevos_verbose_enable)
runloop_msg_queue_push(buffer, 0, 4 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
CHEEVOS_ERR(RCHEEVOS_TAG "%s\n", buffer);
}
}
}
static void rcheevos_client_initialize_runtime_callback(void* userdata)
{
rcheevos_async_initialize_runtime_data_t* runtime_data = (rcheevos_async_initialize_runtime_data_t*)userdata;
if (rcheevos_end_load_state() > 0)
return;
if (!rcheevos_load_aborted())
{
rcheevos_client_copy_achievements(runtime_data);
rcheevos_client_copy_leaderboards(runtime_data);
rcheevos_client_initialize_runtime_rich_presence(runtime_data);
}
rc_api_destroy_fetch_user_unlocks_response(&runtime_data->hardcore_unlocks);
rc_api_destroy_fetch_user_unlocks_response(&runtime_data->non_hardcore_unlocks);
rc_api_destroy_fetch_game_data_response(&runtime_data->game_data);
if (runtime_data->callback)
runtime_data->callback(runtime_data->callback_data);
free(runtime_data);
}
static void rcheevos_client_fetch_game_badge_callback(void* userdata)
{
rcheevos_fetch_badge_data* data = (rcheevos_fetch_badge_data*)userdata;
rcheevos_async_initialize_runtime_data_t* runtime_data =
(rcheevos_async_initialize_runtime_data_t*)data->state->callback_data;
free((void*)data->state->badge_directory);
free(data->state);
free(data);
rcheevos_client_initialize_runtime_callback(runtime_data);
}
static void rcheevos_client_fetch_game_badge(const char* badge_name, rcheevos_async_initialize_runtime_data_t* runtime_data)
{
#if defined(HAVE_GFX_WIDGETS) /* don't need game badge unless widgets are enabled */
char badge_fullpath[PATH_MAX_LENGTH] = "";
char *badge_fullname = NULL;
size_t badge_fullname_size = 0;
/* make sure the directory exists */
fill_pathname_application_special(badge_fullpath,
sizeof(badge_fullpath),
APPLICATION_SPECIAL_DIRECTORY_THUMBNAILS_CHEEVOS_BADGES);
if (!path_is_directory(badge_fullpath))
{
CHEEVOS_LOG(RCHEEVOS_TAG "Creating %s\n", badge_fullpath);
path_mkdir(badge_fullpath);
}
fill_pathname_slash(badge_fullpath, sizeof(badge_fullpath));
badge_fullname = badge_fullpath + strlen(badge_fullpath);
badge_fullname_size = sizeof(badge_fullpath) -
(badge_fullname - badge_fullpath);
snprintf(badge_fullname,
badge_fullname_size, "i%s" FILE_PATH_PNG_EXTENSION, badge_name);
/* check if it's already available */
if (path_is_valid(badge_fullpath))