-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcommand.c
2258 lines (1942 loc) · 66.1 KB
/
command.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) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2021 - David G.F.
*
* 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 <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <locale.h>
#ifdef HAVE_NETWORKING
#include <net/net_compat.h>
#include <net/net_socket.h>
#endif
#include <lists/dir_list.h>
#include <file/file_path.h>
#include <streams/stdin_stream.h>
#include <streams/file_stream.h>
#include <string/stdstring.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_CHEEVOS
#include "cheevos/cheevos.h"
#endif
#ifdef HAVE_GFX_WIDGETS
#include "gfx/gfx_widgets.h"
#endif
#ifdef HAVE_MENU
#include "menu/menu_driver.h"
#endif
#ifdef HAVE_NETWORKING
#include "network/netplay/netplay.h"
#endif
#include "audio/audio_driver.h"
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
#include "gfx/video_shader_parse.h"
#endif
#include "autosave.h"
#include "command.h"
#include "core_info.h"
#include "cheat_manager.h"
#include "content.h"
#include "dynamic.h"
#include "list_special.h"
#include "paths.h"
#include "retroarch.h"
#include "runloop.h"
#include "verbosity.h"
#include "version.h"
#include "version_git.h"
#define CMD_BUF_SIZE 4096
static void command_post_state_loaded(void)
{
#ifdef HAVE_CHEEVOS
if (rcheevos_hardcore_active())
{
const char *_msg = msg_hash_to_str(MSG_CHEEVOS_HARDCORE_MODE_DISABLED);
rcheevos_pause_hardcore();
runloop_msg_queue_push(_msg, strlen(_msg), 0, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
#endif
#ifdef HAVE_NETWORKING
netplay_driver_ctl(RARCH_NETPLAY_CTL_LOAD_SAVESTATE, NULL);
#endif
{
settings_t *settings = config_get_ptr();
video_driver_state_t *video_st =
video_state_get_ptr();
bool frame_time_counter_reset_after_load_state =
settings->bools.frame_time_counter_reset_after_load_state;
if (frame_time_counter_reset_after_load_state)
video_st->frame_time_count = 0;
}
}
#if defined(HAVE_COMMAND)
/* Generic command parse utilities */
static bool command_get_arg(const char *tok,
const char **arg, unsigned *index)
{
unsigned i;
for (i = 0; i < ARRAY_SIZE(map); i++)
{
if (string_is_equal(tok, map[i].str))
{
if (arg)
*arg = NULL;
if (index)
*index = i;
return true;
}
}
for (i = 0; i < ARRAY_SIZE(action_map); i++)
{
const char *str = strstr(tok, action_map[i].str);
if (str == tok)
{
const char *argument = str + strlen(action_map[i].str);
if (!argument)
return false;
if (*argument != ' ' && *argument != '\0')
return false;
if (arg)
*arg = argument + 1;
if (index)
*index = i;
return true;
}
}
return false;
}
static void command_parse_sub_msg(command_t *handle, const char *tok)
{
const char *arg = NULL;
unsigned index = 0;
if (command_get_arg(tok, &arg, &index))
{
if (arg)
{
if (!action_map[index].action(handle, arg))
RARCH_ERR("Command \"%s\" failed.\n", arg);
}
else
handle->state[map[index].id] = true;
}
else
RARCH_WARN(msg_hash_to_str(MSG_UNRECOGNIZED_COMMAND), tok);
}
static void command_parse_msg(
command_t *handle, char *buf)
{
char *save = NULL;
const char *tok = strtok_r(buf, "\n", &save);
while (tok)
{
command_parse_sub_msg(handle, tok);
tok = strtok_r(NULL, "\n", &save);
}
}
#if defined(HAVE_NETWORK_CMD)
typedef struct
{
/* Network socket FD */
int net_fd;
/* Source address for the command received */
struct sockaddr_storage cmd_source;
/* Size of the previous structure in use */
socklen_t cmd_source_len;
} command_network_t;
static void network_command_reply(
command_t *cmd,
const char * data, size_t len)
{
command_network_t *netcmd = (command_network_t*)cmd->userptr;
/* Respond (fire and forget since it's UDP) */
sendto(netcmd->net_fd, data, len, 0,
(struct sockaddr*)&netcmd->cmd_source, netcmd->cmd_source_len);
}
static void network_command_free(command_t *handle)
{
command_network_t *netcmd = (command_network_t*)handle->userptr;
if (netcmd->net_fd >= 0)
socket_close(netcmd->net_fd);
free(netcmd);
free(handle);
}
static void command_network_poll(command_t *handle)
{
ssize_t ret;
char buf[2048];
command_network_t *netcmd = (command_network_t*)handle->userptr;
if (netcmd->net_fd < 0)
return;
for (;;)
{
netcmd->cmd_source_len = sizeof(netcmd->cmd_source);
if ((ret = recvfrom(netcmd->net_fd, buf, sizeof(buf) - 1, 0,
(struct sockaddr*)&netcmd->cmd_source,
&netcmd->cmd_source_len)) <= 0)
return;
buf[ret] = '\0';
command_parse_msg(handle, buf);
}
}
command_t* command_network_new(uint16_t port)
{
struct addrinfo *res = NULL;
command_t *cmd = (command_t*)calloc(1, sizeof(*cmd));
command_network_t *netcmd = (command_network_t*)calloc(
1, sizeof(command_network_t));
int fd = socket_init(
(void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM, AF_INET);
RARCH_LOG("[NetCMD]: %s %hu.\n",
msg_hash_to_str(MSG_BRINGING_UP_COMMAND_INTERFACE_ON_PORT),
(unsigned short)port);
if (fd < 0)
goto error;
netcmd->net_fd = fd;
cmd->userptr = netcmd;
cmd->poll = command_network_poll;
cmd->replier = network_command_reply;
cmd->destroy = network_command_free;
if (!socket_nonblock(netcmd->net_fd))
goto error;
if (!socket_bind(netcmd->net_fd, (void*)res))
{
RARCH_ERR("[NetCMD]: %s.\n",
msg_hash_to_str(MSG_FAILED_TO_BIND_SOCKET));
goto error;
}
freeaddrinfo_retro(res);
return cmd;
error:
if (res)
freeaddrinfo_retro(res);
free(netcmd);
free(cmd);
return NULL;
}
#endif
#if defined(HAVE_STDIN_CMD)
typedef struct
{
/* Buffer and pointer for stdin reads */
size_t stdin_buf_ptr;
char stdin_buf[CMD_BUF_SIZE];
} command_stdin_t;
static void stdin_command_reply(
command_t *cmd,
const char * data, size_t len)
{
/* Just write to stdout! */
fwrite(data, 1, len, stdout);
fflush(stdout);
}
static void stdin_command_free(command_t *handle)
{
free(handle->userptr);
free(handle);
}
static void command_stdin_poll(command_t *handle)
{
ptrdiff_t msg_len;
char *last_newline = NULL;
command_stdin_t *stdincmd = (command_stdin_t*)handle->userptr;
ssize_t ret = read_stdin(
stdincmd->stdin_buf + stdincmd->stdin_buf_ptr,
CMD_BUF_SIZE - stdincmd->stdin_buf_ptr - 1);
if (ret == 0)
return;
stdincmd->stdin_buf_ptr += ret;
stdincmd->stdin_buf[stdincmd->stdin_buf_ptr] = '\0';
last_newline = strrchr(stdincmd->stdin_buf, '\n');
if (!last_newline)
{
/* We're receiving bogus data in pipe
* (no terminating newline), flush out the buffer. */
if (stdincmd->stdin_buf_ptr + 1 >= CMD_BUF_SIZE)
{
stdincmd->stdin_buf_ptr = 0;
stdincmd->stdin_buf[0] = '\0';
}
return;
}
*last_newline++ = '\0';
msg_len = last_newline - stdincmd->stdin_buf;
command_parse_msg(handle, stdincmd->stdin_buf);
memmove(stdincmd->stdin_buf, last_newline,
stdincmd->stdin_buf_ptr - msg_len);
stdincmd->stdin_buf_ptr -= msg_len;
}
command_t* command_stdin_new(void)
{
command_t *cmd;
command_stdin_t *stdincmd;
#ifndef _WIN32
#ifdef HAVE_NETWORKING
if (!socket_nonblock(STDIN_FILENO))
return NULL;
#endif
#endif
cmd = (command_t*)calloc(1, sizeof(command_t));
stdincmd = (command_stdin_t*)calloc(1, sizeof(command_stdin_t));
if (!cmd)
return NULL;
if (!stdincmd)
{
free(cmd);
return NULL;
}
cmd->userptr = stdincmd;
cmd->poll = command_stdin_poll;
cmd->replier = stdin_command_reply;
cmd->destroy = stdin_command_free;
return cmd;
}
#endif
bool command_get_config_param(command_t *cmd, const char* arg)
{
size_t _len;
char reply[8192];
#ifdef HAVE_BSV_MOVIE
char value_dynamic[256];
#endif
const char *value = "unsupported";
settings_t *settings = config_get_ptr();
bool video_fullscreen = settings->bools.video_fullscreen;
const char *dir_runtime_log = settings->paths.directory_runtime_log;
const char *log_dir = settings->paths.log_dir;
const char *directory_cache = settings->paths.directory_cache;
const char *directory_system = settings->paths.directory_system;
const char *path_username = settings->paths.username;
if (string_is_equal(arg, "video_fullscreen"))
{
if (video_fullscreen)
value = "true";
else
value = "false";
}
else if (string_is_equal(arg, "savefile_directory"))
value = dir_get_ptr(RARCH_DIR_SAVEFILE);
else if (string_is_equal(arg, "savestate_directory"))
value = dir_get_ptr(RARCH_DIR_SAVESTATE);
else if (string_is_equal(arg, "runtime_log_directory"))
value = dir_runtime_log;
else if (string_is_equal(arg, "log_dir"))
value = log_dir;
else if (string_is_equal(arg, "cache_directory"))
value = directory_cache;
else if (string_is_equal(arg, "system_directory"))
value = directory_system;
else if (string_is_equal(arg, "netplay_nickname"))
value = path_username;
#ifdef HAVE_BSV_MOVIE
else if (string_is_equal(arg, "active_replay"))
{
input_driver_state_t *input_st = input_state_get_ptr();
value = value_dynamic;
value_dynamic[0] = '\0';
if (input_st->bsv_movie_state_handle)
{
bsv_movie_t *movie = input_st->bsv_movie_state_handle;
snprintf(value_dynamic, sizeof(value_dynamic), "%lld %u %lld",
(long long)(movie->identifier),
input_st->bsv_movie_state.flags,
(long long)(movie->frame_counter));
}
else
strlcpy(value_dynamic, "0 0 0", sizeof(value_dynamic));
}
#endif
/* TODO: query any string */
_len = strlcpy(reply, "GET_CONFIG_PARAM ", sizeof(reply));
_len += strlcpy(reply + _len, arg, sizeof(reply) - _len);
reply[ _len] = ' ';
reply[++_len] = '\0';
_len += strlcpy(reply + _len, value, sizeof(reply) - _len);
cmd->replier(cmd, reply, _len);
return true;
}
#if defined(HAVE_LAKKA)
#include <sys/un.h>
#define MAX_USER_CONNECTIONS 4
typedef struct
{
/* File descriptor for the domain socket */
int sfd;
/* Client sockets */
int userfd[MAX_USER_CONNECTIONS];
/* Last received user socket */
int last_fd;
} command_uds_t;
static void uds_command_reply(
command_t *cmd,
const char * data, size_t len)
{
command_uds_t *subcmd = (command_uds_t*)cmd->userptr;
write(subcmd->last_fd, data, len);
}
static void uds_command_free(command_t *handle)
{
int i;
command_uds_t *udscmd = (command_uds_t*)handle->userptr;
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
if (udscmd->userfd[i] >= 0)
socket_close(udscmd->userfd[i]);
socket_close(udscmd->sfd);
free(handle->userptr);
free(handle);
}
static void command_uds_poll(command_t *handle)
{
int i;
int fd;
ssize_t ret;
char buf[2048];
command_uds_t *udscmd = (command_uds_t*)handle->userptr;
if (udscmd->sfd < 0)
return;
/* Read data from clients and process commands */
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
{
bool err = false;
fd = udscmd->userfd[i];
if (fd < 0)
continue;
ret = socket_receive_all_nonblocking(fd, &err, buf, sizeof(buf) - 1);
if (!ret)
continue;
if (!err)
{
buf[ret] = '\0';
udscmd->last_fd = fd;
command_parse_msg(handle, buf);
}
else
{
socket_close(fd);
udscmd->userfd[i] = -1;
}
}
/* Accepts new connections from clients */
fd = accept(udscmd->sfd, NULL, NULL);
if (fd >= 0)
{
if (socket_nonblock(fd))
{
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
{
if (udscmd->userfd[i] < 0)
{
udscmd->userfd[i] = fd;
return;
}
}
}
socket_close(fd);
}
}
command_t* command_uds_new(void)
{
int i;
command_t *cmd;
command_uds_t *subcmd;
struct sockaddr_un addr;
socklen_t addrsz = offsetof(struct sockaddr_un, sun_path) + STRLEN_CONST("retroarch/cmd") + 1;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return NULL;
/* use an abstract socket for simplicity */
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(&addr.sun_path[1], "retroarch/cmd");
if (bind(fd, (struct sockaddr*)&addr, addrsz) < 0 ||
listen(fd, MAX_USER_CONNECTIONS) < 0)
{
socket_close(fd);
return NULL;
}
if (!socket_nonblock(fd))
{
socket_close(fd);
return NULL;
}
cmd = (command_t*)calloc(1, sizeof(command_t));
subcmd = (command_uds_t*)calloc(1, sizeof(command_uds_t));
subcmd->sfd = fd;
subcmd->last_fd = -1;
for (i = 0; i < MAX_USER_CONNECTIONS; i++)
subcmd->userfd[i] = -1;
cmd->userptr = subcmd;
cmd->poll = command_uds_poll;
cmd->replier = uds_command_reply;
cmd->destroy = uds_command_free;
return cmd;
}
#endif
/* Routines used to invoke retroarch command ... */
#ifdef HAVE_NETWORK_CMD
static bool command_verify(const char *cmd)
{
unsigned i;
if (command_get_arg(cmd, NULL, NULL))
return true;
RARCH_ERR("[NetCMD]: Command \"%s\" is not recognized by the program.\n", cmd);
RARCH_ERR("[NetCMD]: \tValid commands:\n");
for (i = 0; i < ARRAY_SIZE(map); i++)
RARCH_ERR("\t\t%s\n", map[i].str);
for (i = 0; i < ARRAY_SIZE(action_map); i++)
RARCH_ERR("\t\t%s %s\n", action_map[i].str, action_map[i].arg_desc);
return false;
}
static bool udp_send_packet(const char *host, uint16_t port, const char *msg)
{
char port_buf[6];
const struct addrinfo *tmp_info;
struct addrinfo *addr = NULL;
struct addrinfo hints = {0};
size_t len = strlen(msg);
bool ret = false;
snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port);
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_NUMERICSERV;
if (getaddrinfo_retro(host, port_buf, &hints, &addr))
return false;
if (!addr)
return false;
/* Send to all possible targets. */
tmp_info = addr;
do
{
int fd = socket(tmp_info->ai_family,
tmp_info->ai_socktype, tmp_info->ai_protocol);
if (fd < 0)
continue;
if (sendto(fd, msg, len, 0, tmp_info->ai_addr, tmp_info->ai_addrlen) ==
(ssize_t)len)
ret = true;
socket_close(fd);
} while ((tmp_info = tmp_info->ai_next));
freeaddrinfo_retro(addr);
return ret;
}
bool command_network_send(const char *cmd_)
{
char *command = NULL;
char *save = NULL;
const char *cmd = NULL;
if (!network_init())
return false;
if (!(command = strdup(cmd_)))
return false;
cmd = strtok_r(command, ";", &save);
if (cmd)
{
uint16_t port = DEFAULT_NETWORK_CMD_PORT;
const char *port_ = NULL;
const char *host = strtok_r(NULL, ";", &save);
if (host)
port_ = strtok_r(NULL, ";", &save);
else
{
#ifdef _WIN32
host = "127.0.0.1";
#else
host = "localhost";
#endif
}
if (port_)
port = strtoul(port_, NULL, 0);
RARCH_LOG("[NetCMD]: %s: \"%s\" to %s:%hu\n",
msg_hash_to_str(MSG_SENDING_COMMAND),
cmd, host, (unsigned short)port);
if (command_verify(cmd) && udp_send_packet(host, port, cmd))
{
free(command);
return true;
}
}
free(command);
return false;
}
#endif
bool command_show_osd_msg(command_t *cmd, const char* arg)
{
runloop_msg_queue_push(arg, strlen(arg), 1, 180, false, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
return true;
}
bool command_load_state_slot(command_t *cmd, const char *arg)
{
char state_path[16384];
size_t _len = 0;
char reply[128] = "";
unsigned int slot = (unsigned int)strtoul(arg, NULL, 10);
bool savestates_enabled = core_info_current_supports_savestate();
bool ret = false;
state_path[0] = '\0';
_len = strlcpy(reply, "LOAD_STATE_SLOT ", sizeof(reply));
_len += snprintf(reply + _len, sizeof(reply) - _len, "%d", slot);
if (savestates_enabled)
{
size_t info_size;
runloop_get_savestate_path(state_path, sizeof(state_path), slot);
info_size = core_serialize_size();
savestates_enabled = (info_size > 0);
}
if (savestates_enabled)
{
if ((ret = content_load_state(state_path, false, false)))
command_post_state_loaded();
}
else
ret = false;
cmd->replier(cmd, reply, _len);
return ret;
}
bool command_play_replay_slot(command_t *cmd, const char *arg)
{
#ifdef HAVE_BSV_MOVIE
char replay_path[16384];
char reply[128] = "";
unsigned int slot = (unsigned int)strtoul(arg, NULL, 10);
bool savestates_enabled = core_info_current_supports_savestate();
bool ret = false;
replay_path[0] = '\0';
if (savestates_enabled)
{
size_t info_size;
runloop_get_replay_path(replay_path, sizeof(replay_path), slot);
info_size = core_serialize_size();
savestates_enabled = (info_size > 0);
}
if (savestates_enabled)
{
ret = movie_start_playback(input_state_get_ptr(), replay_path);
if (ret)
{
input_driver_state_t *input_st = input_state_get_ptr();
task_queue_wait(NULL, NULL);
if (input_st->bsv_movie_state_next_handle)
snprintf(reply, sizeof(reply) - 1, "PLAY_REPLAY_SLOT %lld", (long long)(input_st->bsv_movie_state_next_handle->identifier));
else
snprintf(reply, sizeof(reply) - 1, "PLAY_REPLAY_SLOT 0");
command_post_state_loaded();
}
}
else
ret = false;
cmd->replier(cmd, reply, strlen(reply));
return ret;
#else
return false;
#endif
}
#if defined(HAVE_CHEEVOS)
bool command_read_ram(command_t *cmd, const char *arg)
{
unsigned i;
char *reply = NULL;
const uint8_t *data = NULL;
char *reply_at = NULL;
unsigned int nbytes = 0;
unsigned int alloc_size = 0;
unsigned int addr = -1;
size_t _len = 0;
if (sscanf(arg, "%x %u", &addr, &nbytes) != 2)
return true;
/* We allocate more than needed, saving 20 bytes is not really relevant */
alloc_size = 40 + nbytes * 3;
reply = (char*)malloc(alloc_size);
reply[0] = '\0';
reply_at = reply + snprintf(
reply, alloc_size - 1, "READ_CORE_RAM" " %x", addr);
if ((data = rcheevos_patch_address(addr)))
{
for (i = 0; i < nbytes; i++)
snprintf(reply_at + 3 * i, 4, " %.2X", data[i]);
reply_at[3 * nbytes] = '\n';
_len = reply_at + 3 * nbytes + 1 - reply;
}
else
{
strlcpy(reply_at, " -1\n", sizeof(reply) - strlen(reply));
_len = reply_at + STRLEN_CONST(" -1\n") - reply;
}
cmd->replier(cmd, reply, _len);
free(reply);
return true;
}
bool command_write_ram(command_t *cmd, const char *arg)
{
unsigned int addr = (unsigned int)strtoul(arg, (char**)&arg, 16);
uint8_t *data = (uint8_t *)rcheevos_patch_address(addr);
if (!data)
return false;
if (rcheevos_hardcore_active())
{
RARCH_LOG("[Command]: Achievements hardcore mode disabled by WRITE_CORE_RAM.\n");
rcheevos_pause_hardcore();
}
while (*arg)
{
*data = strtoul(arg, (char**)&arg, 16);
data++;
}
return true;
}
#endif
bool command_version(command_t *cmd, const char* arg)
{
char reply[256];
size_t _len = strlcpy(reply, PACKAGE_VERSION, sizeof(reply));
reply[ _len] = '\n';
reply[++_len] = '\0';
cmd->replier(cmd, reply, _len);
return true;
}
static const rarch_memory_descriptor_t* command_memory_get_descriptor(const rarch_memory_map_t* mmap, unsigned address, size_t* offset)
{
const rarch_memory_descriptor_t* desc = mmap->descriptors;
const rarch_memory_descriptor_t* end = desc + mmap->num_descriptors;
for (; desc < end; desc++)
{
if (desc->core.select == 0)
{
/* if select is 0, attempt to explicitly match the address */
if (address >= desc->core.start && address < desc->core.start + desc->core.len)
{
*offset = address - desc->core.start;
return desc;
}
}
else
{
/* otherwise, attempt to match the address by matching the select bits */
if (((desc->core.start ^ address) & desc->core.select) == 0)
{
/* adjust the address to the start of the descriptor */
unsigned desc_offset = address - (unsigned)desc->core.start;
/* address is unsigned. we only need that much of the disconnect mask */
unsigned mask = (unsigned)desc->core.disconnect;
/* this magic logic is copied from mmap_reduce. it removes any bits from
* address that are non-zero in the disconnect field. bits above the
* removed bits are shifted down to fill the gap. */
while (mask)
{
const unsigned tmp = (mask - 1) & ~mask;
desc_offset = (desc_offset & tmp) | ((desc_offset >> 1) & ~tmp);
mask = (mask & (mask - 1)) >> 1;
}
/* we've calculated the actual offset of the data within the descriptor */
*offset = desc_offset;
/* sanity check - make sure the descriptor is large enough to hold the target address */
if (desc_offset < desc->core.len)
return desc;
}
}
}
return NULL;
}
static uint8_t *command_memory_get_pointer(
const rarch_system_info_t* sys_info,
unsigned address, unsigned int* max_bytes,
int for_write, char *s, size_t len)
{
if (!sys_info || sys_info->mmaps.num_descriptors == 0)
strlcpy(s, " -1 no memory map defined\n", len);
else
{
size_t offset;
const rarch_memory_descriptor_t* desc = command_memory_get_descriptor(&sys_info->mmaps, address, &offset);
if (!desc)
strlcpy(s, " -1 no descriptor for address\n", len);
else if (!desc->core.ptr)
strlcpy(s, " -1 no data for descriptor\n", len);
else if (for_write && (desc->core.flags & RETRO_MEMDESC_CONST))
strlcpy(s, " -1 descriptor data is readonly\n", len);
else
{
*max_bytes = (unsigned int)(desc->core.len - offset);
return (uint8_t*)desc->core.ptr + desc->core.offset + offset;
}
}
*max_bytes = 0;
return NULL;
}
bool command_get_status(command_t *cmd, const char* arg)
{
size_t _len;
char reply[4096];
uint8_t flags = content_get_flags();
if (flags & CONTENT_ST_FLAG_IS_INITED)
{
/* add some content info */
core_info_t *core_info = NULL;
runloop_state_t *runloop_st = runloop_state_get_ptr();
core_info_get_current_core(&core_info);
_len = strlcpy(reply, "GET_STATUS ", sizeof(reply));
if (runloop_st->flags & RUNLOOP_FLAG_PAUSED)
_len += strlcpy(reply + _len, "PAUSED", sizeof(reply) - _len);
else
_len += strlcpy(reply + _len, "PLAYING", sizeof(reply) - _len);
_len += strlcpy(reply + _len, " ", sizeof(reply) - _len);
if (core_info)
_len += strlcpy(reply + _len, core_info->system_id,
sizeof(reply) - _len);
else
_len += strlcpy(reply + _len, runloop_st->system.info.library_name,
sizeof(reply) - _len);
_len += strlcpy(reply + _len, ",", sizeof(reply) - _len);
_len += strlcpy(reply + _len,
path_basename(path_get(RARCH_PATH_BASENAME)), sizeof(reply) - _len);
_len += snprintf(reply + _len, sizeof(reply) - _len,
",crc32=%x\n", content_get_crc());
}
else
_len = strlcpy(reply, "GET_STATUS CONTENTLESS", sizeof(reply));
cmd->replier(cmd, reply, _len);
return true;
}
bool command_read_memory(command_t *cmd, const char *arg)
{
unsigned i;
char* reply = NULL;
char* reply_at = NULL;
const uint8_t* data = NULL;
unsigned int nbytes = 0;
unsigned int alloc_size = 0;
unsigned int address = -1;
size_t _len = 0;
unsigned int max_bytes = 0;
runloop_state_t *runloop_st = runloop_state_get_ptr();
const rarch_system_info_t* sys_info= &runloop_st->system;
if (sscanf(arg, "%x %u", &address, &nbytes) != 2)
return false;
/* Ensure large enough to return all requested bytes or an error message */
alloc_size = 64 + nbytes * 3;
reply = (char*)malloc(alloc_size);
reply_at = reply + snprintf(reply, alloc_size - 1, "READ_CORE_MEMORY %x", address);
if ((data = command_memory_get_pointer(
sys_info, address, &max_bytes,
0, reply_at, alloc_size - strlen(reply))))
{
if (nbytes > max_bytes)
nbytes = max_bytes;
for (i = 0; i < nbytes; i++)
snprintf(reply_at + 3 * i, 4, " %02X", data[i]);
reply_at[3 * nbytes] = '\n';
_len = reply_at + 3 * nbytes + 1 - reply;
}
else
_len = strlen(reply);
cmd->replier(cmd, reply, _len);
free(reply);
return true;
}
bool command_write_memory(command_t *cmd, const char *arg)
{