forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheevos.c
More file actions
1895 lines (1624 loc) · 61.4 KB
/
cheevos.c
File metadata and controls
1895 lines (1624 loc) · 61.4 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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2015-2018 - Andre Leiradella
* Copyright (C) 2019-2023 - 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 <string.h>
#include <ctype.h>
#include <file/file_path.h>
#include <string/stdstring.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h>
#include <features/features_cpu.h>
#include <formats/cdfs.h>
#include <formats/m3u_file.h>
#include <compat/strl.h>
#include <retro_miscellaneous.h>
#include <retro_math.h>
#include <retro_timers.h>
#include <net/net_http.h>
#include <libretro.h>
#include <lrc_hash.h>
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#ifdef HAVE_GFX_WIDGETS
#include "../gfx/gfx_widgets.h"
#endif
#ifdef HAVE_THREADS
#include <rthreads/rthreads.h>
#endif
#ifdef HAVE_CHEATS
#include "../cheat_manager.h"
#endif
#ifdef HAVE_CHD
#include "streams/chd_stream.h"
#endif
#ifdef HAVE_CHEEVOS_RVZ
#include "cheevos_rvz.h"
#endif
#include "cheevos.h"
#include "cheevos_client.h"
#include "cheevos_menu.h"
#include "cheevos_locals.h"
#include "../network/netplay/netplay.h"
#include "../audio/audio_driver.h"
#include "../file_path_special.h"
#include "../paths.h"
#include "../command.h"
#include "../configuration.h"
#include "../performance_counters.h"
#include "../msg_hash.h"
#include "../retroarch.h"
#include "../runtime_file.h"
#include "../core.h"
#include "../core_option_manager.h"
#include "../tasks/tasks_internal.h"
#include "../deps/rcheevos/include/rc_runtime.h"
#include "../deps/rcheevos/include/rc_runtime_types.h"
#include "../deps/rcheevos/include/rc_hash.h"
#include "../deps/rcheevos/src/rc_libretro.h"
/* Define this macro to prevent cheevos from being deactivated when they trigger. */
#undef CHEEVOS_DONT_DEACTIVATE
static rcheevos_locals_t rcheevos_locals =
{
NULL, /* client */
{{0}},/* memory */
#ifdef HAVE_THREADS
CMD_EVENT_NONE, /* queued_command */
#endif
"", /* user_agent_prefix */
"", /* user_agent_core */
#ifdef HAVE_MENU
NULL, /* menuitems */
0, /* menuitem_capacity */
0, /* menuitem_count */
#endif
true, /* hardcore_allowed */
false,/* hardcore_requires_reload */
false,/* hardcore_being_enabled */
true, /* core_supports */
false,/* badges_loaded */
false /* badges_loading */
};
rcheevos_locals_t* get_rcheevos_locals(void)
{
return &rcheevos_locals;
}
#define CHEEVOS_MB(x) ((x) * 1024 * 1024)
/*****************************************************************************
Supporting functions.
*****************************************************************************/
#define CMD_CHEEVOS_FINALIZE_LOAD -1
static void rcheevos_finalize_game_load_on_ui_thread(void);
#ifndef CHEEVOS_VERBOSE
void rcheevos_log(const char* fmt, ...)
{
(void)fmt;
}
#endif
static void rcheevos_handle_log_message(const char* message)
{
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", message);
}
static void rcheevos_get_core_memory_info(uint32_t id,
rc_libretro_core_memory_info_t* info)
{
retro_ctx_memory_info_t ctx_info;
if (!info)
return;
ctx_info.id = id;
if (core_get_memory(&ctx_info))
{
info->data = (unsigned char*)ctx_info.data;
info->size = ctx_info.size;
}
else
{
info->data = NULL;
info->size = 0;
}
}
static int rcheevos_init_memory(rcheevos_locals_t* locals)
{
unsigned i;
int result;
struct retro_memory_map mmap;
const rc_client_game_t* game;
rarch_system_info_t *sys_info = &runloop_state_get_ptr()->system;
rarch_memory_map_t *mmaps = &sys_info->mmaps;
struct retro_memory_descriptor* descriptors;
unsigned console_id;
/* if no game is loaded, fallback to a default mapping (SYSTEM RAM followed by SAVE RAM) */
game = rc_client_get_game_info(locals->client);
console_id = game ? game->console_id : 0;
descriptors = (struct retro_memory_descriptor*)malloc(mmaps->num_descriptors * sizeof(*descriptors));
if (!descriptors)
return 0;
mmap.descriptors = &descriptors[0];
mmap.num_descriptors = mmaps->num_descriptors;
/* RetroArch wraps the retro_memory_descriptor's
* in rarch_memory_descriptor_t's, pull them back out */
for (i = 0; i < mmap.num_descriptors; ++i)
memcpy(&descriptors[i], &mmaps->descriptors[i].core,
sizeof(descriptors[0]));
rc_libretro_init_verbose_message_callback(rcheevos_handle_log_message);
result = rc_libretro_memory_init(&locals->memory, &mmap,
rcheevos_get_core_memory_info, console_id);
free(descriptors);
return result;
}
uint8_t* rcheevos_patch_address(unsigned address)
{
/* Memory map was not previously initialized
* (no achievements for this game?), try now */
if (rcheevos_locals.memory.count == 0)
rcheevos_init_memory(&rcheevos_locals);
return rc_libretro_memory_find(&rcheevos_locals.memory, address);
}
static bool rcheevos_is_game_loaded(void)
{
return rc_client_is_game_loaded(rcheevos_locals.client);
}
static bool rcheevos_is_player_active(void)
{
if (netplay_is_spectating())
return false;
/* TODO: disallow player slots other than player one unless it's a [Multi] set */
return true;
}
void rcheevos_spectating_changed(void)
{
/* don't update spectator mode while a game is loading - it prevents being able to change it later */
if (rcheevos_is_game_loaded())
{
const bool spectating = !rcheevos_is_player_active();
if (spectating != rc_client_get_spectator_mode_enabled(rcheevos_locals.client))
rc_client_set_spectator_mode_enabled(rcheevos_locals.client, !rcheevos_is_player_active());
}
}
bool rcheevos_is_pause_allowed(void)
{
return rc_client_can_pause(rcheevos_locals.client, NULL);
}
static void rcheevos_show_completion_placard(const char* title, const char* badge_name)
{
const settings_t* settings = config_get_ptr();
if (settings->bools.cheevos_visibility_mastery)
{
char message[256];
size_t _len = snprintf(message, sizeof(message),
msg_hash_to_str(rc_client_get_hardcore_enabled(rcheevos_locals.client)
? MSG_CHEEVOS_MASTERED_GAME
: MSG_CHEEVOS_COMPLETED_GAME),
title);
message[sizeof(message) - 1] = '\0';
#if defined (HAVE_GFX_WIDGETS)
if (gfx_widgets_ready())
{
char msg[128];
char badge[32];
const char* displayname = rc_client_get_user_info(rcheevos_locals.client)->display_name;
const bool content_runtime_log = settings->bools.content_runtime_log;
const bool content_runtime_log_aggr = settings->bools.content_runtime_log_aggregate;
size_t __len = strlcpy(msg, displayname, sizeof(msg));
if (__len < sizeof(msg) - 12
&& (content_runtime_log || content_runtime_log_aggr))
{
const char* content_path = path_get(RARCH_PATH_CONTENT);
const char* core_path = path_get(RARCH_PATH_CORE);
runtime_log_t* runtime_log = runtime_log_init(
content_path, core_path,
settings->paths.directory_runtime_log,
settings->paths.directory_playlist,
!content_runtime_log_aggr);
if (runtime_log)
{
const runloop_state_t* runloop_state = runloop_state_get_ptr();
runtime_log_add_runtime_usec(runtime_log,
runloop_state->core_runtime_usec);
__len += strlcpy(msg + __len, " | ", sizeof(msg) - __len);
runtime_log_get_runtime_str(runtime_log, msg + __len, sizeof(msg) - __len);
msg[sizeof(msg) - 1] = '\0';
free(runtime_log);
}
}
/* badge image = "iBADGENAME" */
badge[0] = 'i';
strlcpy(&badge[1], badge_name, sizeof(badge) - 1);
gfx_widgets_push_achievement(title, msg, badge);
}
else
#endif
runloop_msg_queue_push(title, _len, 0, 3 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
static void rcheevos_show_mastery_placard(void)
{
const rc_client_game_t* game = rc_client_get_game_info(rcheevos_locals.client);
rcheevos_show_completion_placard(game->title, game->badge_name);
}
static void rcheevos_show_subset_completion_placard(const rc_client_subset_t* subset)
{
char badge[32];
badge[0] = 'i';
strlcpy(&badge[1], subset->badge_name, sizeof(badge) - 1);
rcheevos_client_download_badge_from_url(subset->badge_url, badge);
rcheevos_show_completion_placard(subset->title, subset->badge_name);
}
#if defined(HAVE_GFX_WIDGETS)
static void rcheevos_show_achievement_popup(const rc_client_achievement_t* cheevo, float rarity)
{
char title[128], subtitle[96];
if (rarity >= 10.0)
snprintf(title, sizeof(title), "%s - %0.2f%%",
msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED), rarity);
else if (rarity > 0.0)
snprintf(title, sizeof(title), "%s - %0.2f%%",
msg_hash_to_str(MSG_RARE_ACHIEVEMENT_UNLOCKED), rarity);
else
strlcpy(title,
msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED), sizeof(title));
snprintf(subtitle, sizeof(subtitle), "%s (%lu)", cheevo->title, (unsigned long)cheevo->points);
gfx_widgets_push_achievement(title, subtitle, cheevo->badge_name);
/* if all badges haven't been loaded, preload the next one assuming it will be the next needed */
if (!rcheevos_locals.badges_loaded)
{
const rc_client_achievement_t* next_locked_achievement =
rc_client_get_next_achievement_info(rcheevos_locals.client, cheevo, RC_CLIENT_ACHIEVEMENT_BUCKET_LOCKED);
if (next_locked_achievement)
rcheevos_client_download_badge_from_url(next_locked_achievement->badge_url, next_locked_achievement->badge_name);
}
}
struct rcheevos_retry_achievement_info_t
{
uint32_t achievement_id;
float rarity;
};
static void rcheevos_retry_achievement_popup(retro_task_t* task)
{
struct rcheevos_retry_achievement_info_t* info = (struct rcheevos_retry_achievement_info_t*)task->user_data;
const rc_client_achievement_t* cheevo = rc_client_get_achievement_info(rcheevos_locals.client, info->achievement_id);
if (!cheevo)
{
/* achievement not found, assume game unloaded and don't show the popup */
}
else if (task->progress > 4 || rcheevos_is_badge_available(cheevo->badge_name, false))
{
/* badge is available now, or we've reached the retry limit. show the popup */
rcheevos_show_achievement_popup(cheevo, info->rarity);
}
else
{
/* second retry in 200ms, third is 400ms, fourth in 800ms. if not available after 1500ms
* (100+200+400+800), then just show the popup with the placeholder. */
task->progress <<= 1;
task->when = cpu_features_get_time_usec() + 100000 * task->progress; /* first retry in 100ms */
return;
}
/* cleanup the user data */
task->user_data = NULL;
free(info);
/* mark task as complete so it will get cleaned up */
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
}
#endif /* HAVE_GFX_WIDGETS */
static void rcheevos_award_achievement(const rc_client_achievement_t* cheevo)
{
const settings_t* settings = config_get_ptr();
if (!cheevo)
return;
/* Show the on screen message. */
if (settings->bools.cheevos_visibility_unlock)
{
#if defined(HAVE_GFX_WIDGETS)
if (gfx_widgets_ready())
{
float rarity = rc_client_get_hardcore_enabled(rcheevos_locals.client) ?
cheevo->rarity_hardcore : cheevo->rarity;
if (rcheevos_locals.badges_loaded || rcheevos_is_badge_available(cheevo->badge_name, false))
{
rcheevos_show_achievement_popup(cheevo, rarity);
}
else
{
retro_task_t* task;
rcheevos_client_download_badge_from_url(cheevo->badge_url, cheevo->badge_name);
task = task_init();
if (!task)
{
rcheevos_show_achievement_popup(cheevo, rarity);
}
else
{
struct rcheevos_retry_achievement_info_t* info;
info = (struct rcheevos_retry_achievement_info_t*)malloc(sizeof(*info));
info->achievement_id = cheevo->id;
info->rarity = rarity;
task->handler = rcheevos_retry_achievement_popup;
task->user_data = info;
task->progress = 1;
task->when = cpu_features_get_time_usec() + 100000; /* first retry in 100ms */
task_queue_push(task);
}
}
}
else
#endif
{
char buffer[256];
size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_ACHIEVEMENT_UNLOCKED),
sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, cheevo->title, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, _len, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
runloop_msg_queue_push(cheevo->description, strlen(cheevo->description), 0, 3 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
#ifdef HAVE_AUDIOMIXER
/* Play the unlock sound */
if (settings->bools.cheevos_unlock_sound_enable)
audio_driver_mixer_play_menu_sound(
AUDIO_MIXER_SYSTEM_SLOT_ACHIEVEMENT_UNLOCK);
#endif
#ifdef HAVE_SCREENSHOTS
/* Take a screenshot of the achievement. */
if (settings->bools.cheevos_auto_screenshot)
{
size_t shotname_len = sizeof(char) * 8192;
char* shotname = (char*)malloc(shotname_len);
if (shotname)
{
const char *path_directory_screenshot = settings->paths.directory_screenshot;
video_driver_state_t* video_st = video_state_get_ptr();;
snprintf(shotname, shotname_len, "%s/%s-cheevo-%u",
path_directory_screenshot,
path_basename(path_get(RARCH_PATH_BASENAME)),
(unsigned)cheevo->id);
shotname[shotname_len - 1] = '\0';
if (take_screenshot(path_directory_screenshot,
shotname,
true,
video_st->frame_cache_data
&& (video_st->frame_cache_data == RETRO_HW_FRAME_BUFFER_VALID),
false,
true))
CHEEVOS_LOG(RCHEEVOS_TAG
"Captured screenshot for achievement %u\n",
cheevo->id);
else
CHEEVOS_LOG(RCHEEVOS_TAG
"Failed to capture screenshot for achievement %u\n",
cheevo->id);
free(shotname);
}
}
#endif
}
static void rcheevos_lboard_submitted(const rc_client_leaderboard_t* lboard,
const rc_client_leaderboard_scoreboard_t* scoreboard)
{
const settings_t* settings = config_get_ptr();
if (lboard && settings->bools.cheevos_visibility_lboard_submit)
{
size_t _len;
char buffer[256];
if (scoreboard)
{
_len = snprintf(buffer, sizeof(buffer), msg_hash_to_str(MSG_LEADERBOARD_SUBMISSION),
scoreboard->submitted_score, lboard->title);
_len += strlcpy(buffer + _len, " (", sizeof(buffer) - _len);
if (strcmp(scoreboard->best_score, scoreboard->submitted_score) == 0)
_len += snprintf(buffer + _len, sizeof(buffer) - _len,
msg_hash_to_str(MSG_LEADERBOARD_RANK), scoreboard->new_rank);
else
_len += snprintf(buffer + _len, sizeof(buffer) - _len,
msg_hash_to_str(MSG_LEADERBOARD_BEST), scoreboard->best_score);
_len += strlcpy(buffer + _len, ")", sizeof(buffer) - _len);
}
else
_len = snprintf(buffer, sizeof(buffer), msg_hash_to_str(MSG_LEADERBOARD_SUBMISSION),
lboard->tracker_value, lboard->title);
runloop_msg_queue_push(buffer, _len, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
static void rcheevos_lboard_canceled(const rc_client_leaderboard_t* lboard)
{
const settings_t* settings = config_get_ptr();
bool cheevos_visibility_lboard_cancel = settings->bools.cheevos_visibility_lboard_cancel;
if (cheevos_visibility_lboard_cancel)
{
char buffer[256];
size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_LEADERBOARD_FAILED), sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, lboard->title, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, _len, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
static void rcheevos_lboard_started(const rc_client_leaderboard_t* lboard)
{
const settings_t *settings = config_get_ptr();
bool cheevos_visibility_lboard_start = settings->bools.cheevos_visibility_lboard_start;
if (cheevos_visibility_lboard_start)
{
char buffer[256];
size_t _len = strlcpy(buffer, msg_hash_to_str(MSG_LEADERBOARD_STARTED),
sizeof(buffer));
_len += strlcpy(buffer + _len, ": ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, lboard->title, sizeof(buffer) - _len);
if (lboard->description && *lboard->description)
_len += snprintf(buffer + _len, sizeof(buffer) - _len, "- %s",
lboard->description);
runloop_msg_queue_push(buffer, _len, 0, 2 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
#if defined(HAVE_GFX_WIDGETS)
static void rcheevos_lboard_update_tracker(const rc_client_leaderboard_tracker_t* tracker)
{
const settings_t* settings = config_get_ptr();
if (tracker && gfx_widgets_ready() && settings->bools.cheevos_visibility_lboard_trackers)
gfx_widgets_set_leaderboard_display(tracker->id, tracker->display);
}
static void rcheevos_lboard_hide_tracker(const rc_client_leaderboard_tracker_t* tracker)
{
const settings_t* settings = config_get_ptr();
if (tracker && gfx_widgets_ready() && settings->bools.cheevos_visibility_lboard_trackers)
gfx_widgets_set_leaderboard_display(tracker->id, NULL);
}
static void rcheevos_challenge_started(const rc_client_achievement_t* cheevo)
{
settings_t* settings = config_get_ptr();
if (cheevo && gfx_widgets_ready() && settings->bools.cheevos_challenge_indicators)
gfx_widgets_set_challenge_display(cheevo->id, cheevo->badge_name);
}
static void rcheevos_challenge_ended(const rc_client_achievement_t* cheevo)
{
if (cheevo && gfx_widgets_ready())
gfx_widgets_set_challenge_display(cheevo->id, NULL);
}
static void rcheevos_progress_updated(rcheevos_locals_t* locals,
const rc_client_achievement_t* cheevo)
{
settings_t* settings = config_get_ptr();
if ( cheevo
&& gfx_widgets_ready()
&& settings->bools.cheevos_visibility_progress_tracker)
gfx_widget_set_achievement_progress(cheevo->badge_name, cheevo->measured_progress);
}
static void rcheevos_progress_hide(rcheevos_locals_t* locals)
{
settings_t* settings = config_get_ptr();
if (gfx_widgets_ready() && settings->bools.cheevos_visibility_progress_tracker)
gfx_widget_set_achievement_progress(NULL, NULL);
}
#endif
static void rcheevos_client_log_message(const char* message, const rc_client_t* client)
{
CHEEVOS_LOG(RCHEEVOS_TAG "%s\n", message);
}
static void rcheevos_server_error(const char* api_name, const char* message)
{
char buffer[256];
size_t _len = strlcpy(buffer, api_name, sizeof(buffer));
_len += strlcpy(buffer + _len, " failed: ", sizeof(buffer) - _len);
_len += strlcpy(buffer + _len, message, sizeof(buffer) - _len);
runloop_msg_queue_push(buffer, _len, 0, 4 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR);
}
static void rcheevos_server_disconnected(void)
{
CHEEVOS_LOG(RCHEEVOS_TAG "Unable to communicate with RetroAchievements server\n");
/* always show message - even with widget. it helps the user understand what the widget is for */
{
const char *_msg = msg_hash_to_str(MENU_ENUM_LABEL_CHEEVOS_SERVER_DISCONNECTED);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_WARNING);
}
#if defined(HAVE_GFX_WIDGETS)
if (gfx_widgets_ready())
gfx_widget_set_cheevos_disconnect(true);
#endif
}
static void rcheevos_server_reconnected(void)
{
CHEEVOS_LOG(RCHEEVOS_TAG "All pending requests synced to RetroAchievements server\n");
{
const char *_msg = msg_hash_to_str(MENU_ENUM_LABEL_CHEEVOS_SERVER_RECONNECTED);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_SUCCESS);
}
#if defined(HAVE_GFX_WIDGETS)
if (gfx_widgets_ready())
gfx_widget_set_cheevos_disconnect(false);
#endif
}
static void rcheevos_client_event_handler(const rc_client_event_t* event, rc_client_t* client)
{
switch (event->type)
{
#ifdef HAVE_GFX_WIDGETS
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_UPDATE:
rcheevos_lboard_update_tracker(event->leaderboard_tracker);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_SHOW:
rcheevos_challenge_started(event->achievement);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_HIDE:
rcheevos_challenge_ended(event->achievement);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_SHOW:
rcheevos_progress_updated(&rcheevos_locals, event->achievement);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_UPDATE:
rcheevos_progress_updated(&rcheevos_locals, event->achievement);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_HIDE:
rcheevos_progress_hide(&rcheevos_locals);
break;
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_SHOW:
rcheevos_lboard_update_tracker(event->leaderboard_tracker);
break;
case RC_CLIENT_EVENT_LEADERBOARD_TRACKER_HIDE:
rcheevos_lboard_hide_tracker(event->leaderboard_tracker);
break;
#endif
case RC_CLIENT_EVENT_ACHIEVEMENT_TRIGGERED:
rcheevos_award_achievement(event->achievement);
break;
case RC_CLIENT_EVENT_LEADERBOARD_STARTED:
rcheevos_lboard_started(event->leaderboard);
break;
case RC_CLIENT_EVENT_LEADERBOARD_FAILED:
rcheevos_lboard_canceled(event->leaderboard);
break;
case RC_CLIENT_EVENT_LEADERBOARD_SUBMITTED:
/* don't notify on submission - report new rank/best score after submission via SCOREBOARD event */
break;
case RC_CLIENT_EVENT_LEADERBOARD_SCOREBOARD:
rcheevos_lboard_submitted(event->leaderboard, event->leaderboard_scoreboard);
break;
case RC_CLIENT_EVENT_RESET:
command_event(CMD_EVENT_RESET, NULL); /* reset the game */
break;
case RC_CLIENT_EVENT_GAME_COMPLETED:
rcheevos_show_mastery_placard();
break;
case RC_CLIENT_EVENT_SUBSET_COMPLETED:
rcheevos_show_subset_completion_placard(event->subset);
break;
case RC_CLIENT_EVENT_SERVER_ERROR:
rcheevos_server_error(event->server_error->api, event->server_error->error_message);
break;
case RC_CLIENT_EVENT_DISCONNECTED:
rcheevos_server_disconnected();
break;
case RC_CLIENT_EVENT_RECONNECTED:
rcheevos_server_reconnected();
break;
default:
#ifndef NDEBUG
CHEEVOS_LOG(RCHEEVOS_TAG "Unsupported rc_client event %u\n", event->type);
#endif
break;
}
}
int rcheevos_get_richpresence(char* s, size_t len)
{
if (!rcheevos_is_player_active())
{
if (!rcheevos_is_game_loaded())
return 0;
return snprintf(s, len, msg_hash_to_str(MSG_CHEEVOS_RICH_PRESENCE_SPECTATING),
rc_client_get_game_info(rcheevos_locals.client)->title);
}
return (int)rc_client_get_rich_presence_message(rcheevos_locals.client, s, (size_t)len);
}
int rcheevos_get_game_badge_url(char* s, size_t len)
{
const rc_client_game_t* game = rc_client_get_game_info(rcheevos_locals.client);
if (!game || !game->id || !game->badge_name || !game->badge_name[0])
return 0;
return (rc_client_game_get_image_url(game, s, len) == RC_OK);
}
#ifdef HAVE_GFX_WIDGETS
static void rcheevos_hide_widgets(bool widgets_ready)
{
/* Hide any visible trackers */
if (widgets_ready)
{
gfx_widgets_clear_leaderboard_displays();
gfx_widgets_clear_challenge_displays();
gfx_widget_set_achievement_progress(NULL, NULL);
}
}
#endif
void rcheevos_reset_game(bool widgets_ready)
{
#if defined(HAVE_GFX_WIDGETS)
/* Hide any visible trackers */
rcheevos_hide_widgets(widgets_ready);
#endif
rc_client_reset(rcheevos_locals.client);
/* Some cores reallocate memory on reset,
* make sure we update our pointers */
if (rcheevos_locals.memory.total_size > 0)
rcheevos_init_memory(&rcheevos_locals);
}
void rcheevos_refresh_memory(void)
{
if (rcheevos_locals.memory.total_size > 0)
rcheevos_init_memory(&rcheevos_locals);
}
bool rcheevos_hardcore_active(void)
{
/* normal hardcore check */
if (rcheevos_locals.client && rc_client_get_hardcore_enabled(rcheevos_locals.client))
return true;
/* if we're trying to enable hardcore, pretend it's on so the caller can decide to disable
* it (by calling rcheevos_pause_hardcore) before we actually turn it on. */
return rcheevos_locals.hardcore_being_enabled;
}
void rcheevos_pause_hardcore(void)
{
rcheevos_locals.hardcore_allowed = false;
if (rcheevos_hardcore_active())
rcheevos_toggle_hardcore_paused();
}
bool rcheevos_unload(void)
{
const bool was_loaded = rcheevos_is_game_loaded();
#ifdef HAVE_GFX_WIDGETS
rcheevos_hide_widgets(gfx_widgets_ready());
gfx_widget_set_cheevos_set_loading(false);
#endif
rc_client_unload_game(rcheevos_locals.client);
#ifdef HAVE_THREADS
rcheevos_locals.queued_command = CMD_EVENT_NONE;
#endif
if (rcheevos_locals.memory.count > 0)
rc_libretro_memory_destroy(&rcheevos_locals.memory);
if (was_loaded)
{
rcheevos_locals.badges_loaded = rcheevos_locals.badges_loading = false;
#ifdef HAVE_MENU
rcheevos_menu_reset_badges();
if (rcheevos_locals.menuitems)
{
CHEEVOS_FREE(rcheevos_locals.menuitems);
rcheevos_locals.menuitems = NULL;
rcheevos_locals.menuitem_capacity =
rcheevos_locals.menuitem_count = 0;
}
#endif
}
#ifdef HAVE_THREADS
rcheevos_locals.queued_command = CMD_EVENT_NONE;
#endif
if (!config_get_ptr()->arrays.cheevos_token[0])
{
/* If the config-level token has been cleared, we need to re-login on
* loading the next game. Easiest way to do that is to destroy the client */
rc_client_t* client = rcheevos_locals.client;
rcheevos_locals.client = NULL;
rc_client_destroy(client);
}
return true;
}
void rcheevos_leaderboard_trackers_visibility_changed(void)
{
#if defined(HAVE_GFX_WIDGETS)
bool cheevos_visibility_lboard_trackers = config_get_ptr()->bools.cheevos_visibility_lboard_trackers;
/* Hide any visible trackers */
if (!cheevos_visibility_lboard_trackers)
gfx_widgets_clear_leaderboard_displays();
#endif
}
/* Disable slowdown */
static void rcheevos_enforce_hardcore_settings(void)
{
runloop_state_get_ptr()->flags &= ~RUNLOOP_FLAG_SLOWMOTION;
}
static void rcheevos_toggle_hardcore_active(rcheevos_locals_t* locals)
{
settings_t* settings = config_get_ptr();
bool rewind_enable = settings->bools.rewind_enable;
const bool was_enabled = rcheevos_hardcore_active();
bool notification_show_cheats_applied =
settings->bools.notification_show_cheats_applied;
if (!was_enabled)
{
locals->hardcore_being_enabled = true;
locals->hardcore_allowed = true;
/* If one or more invalid settings is enabled, abort*/
rcheevos_validate_config_settings();
if (!locals->hardcore_allowed)
{
locals->hardcore_being_enabled = false;
return;
}
#ifdef HAVE_CHEATS
/* If one or more emulator managed cheats is active, abort */
cheat_manager_apply_cheats(notification_show_cheats_applied);
if (!locals->hardcore_allowed)
{
locals->hardcore_being_enabled = false;
return;
}
#endif
if (rcheevos_is_game_loaded())
{
const char *_msg = msg_hash_to_str(
MSG_CHEEVOS_HARDCORE_MODE_ENABLE);
CHEEVOS_LOG("%s\n", _msg);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
rcheevos_enforce_hardcore_settings();
}
/* deinit rewind */
if (rewind_enable)
{
#ifdef HAVE_THREADS
if (!task_is_on_main_thread())
{
/* have to "schedule" this.
* CMD_EVENT_REWIND_DEINIT should
* only be called on the main thread */
rcheevos_locals.queued_command = CMD_EVENT_REWIND_DEINIT;
}
else
#endif
command_event(CMD_EVENT_REWIND_DEINIT, NULL);
}
locals->hardcore_being_enabled = false;
rc_client_set_hardcore_enabled(locals->client, 1);
}
else
{
/* pause hardcore */
rc_client_set_hardcore_enabled(locals->client, 0);
/* re-init rewind */
if (rewind_enable)
{
#ifdef HAVE_THREADS
if (!task_is_on_main_thread())
{
/* have to "schedule" this.
* CMD_EVENT_REWIND_INIT should
* only be called on the main thread */
rcheevos_locals.queued_command = CMD_EVENT_REWIND_INIT;
}
else
#endif
command_event(CMD_EVENT_REWIND_INIT, NULL);
}
}
#ifdef HAVE_NETWORKING
/* force sending a savestate to clients so they'll drop out of hardcore too */
netplay_force_send_savestate();
#endif
}
void rcheevos_toggle_hardcore_paused(void)
{
/* if hardcore mode is not enabled, we can't toggle whether its active */
if (config_get_ptr()->bools.cheevos_hardcore_mode_enable)
rcheevos_toggle_hardcore_active(&rcheevos_locals);
}
void rcheevos_hardcore_enabled_changed(void)
{
/* called whenever a setting that could potentially affect hardcore enabledness changes
* (i.e. cheevos_enable, hardcore_mode_enable) to synchronize the internal state to the configs.
* also called when a game is first loaded to synchronize the internal state to the configs. */
const settings_t* settings = config_get_ptr();
const bool enabled = settings
&& settings->bools.cheevos_enable
&& settings->bools.cheevos_hardcore_mode_enable;
const bool was_enabled = rcheevos_hardcore_active();
if (enabled != was_enabled)
{
rcheevos_toggle_hardcore_active(&rcheevos_locals);
}
else if (was_enabled && rcheevos_is_game_loaded())
{
/* hardcore enabledness didn't change, but hardcore is active, so make
* sure to enforce the restrictions. */
rcheevos_enforce_hardcore_settings();
}
}
void rcheevos_validate_config_settings(void)
{
int i;
unsigned console_id;
const rc_disallowed_setting_t
*disallowed_settings = NULL;
core_option_manager_t* coreopts = NULL;
struct retro_system_info *sysinfo =
&runloop_state_get_ptr()->system.info;
const settings_t* settings = config_get_ptr();
if (!rcheevos_hardcore_active())
return;
/* this adds a sleep to every frame. if the value is high enough that a
* single frame takes more than 1/60th of a second to evaluate, render,
* and sleep, then the real framerate is less than 60fps. with vsync on,
* it'll wait for the next vsync event, effectively halfing the fps. the
* auto setting should achieve the most accurate frame rate anyway, so
* disallow any manual values */
if ( !settings->bools.video_frame_delay_auto
&& settings->uints.video_frame_delay != 0)
{
const char* error = msg_hash_to_str(