1 #include "core/command.hpp"
2 #include "core/messages.hpp"
3 #include "core/moviedata.hpp"
7 command::fnptr
<const std::string
&> CMD_action(lsnes_cmds
, "action", "Execute core action",
8 "Syntax: action <name> [<params>...]\nExecutes core action.\n",
9 [](const std::string
& _args
) throw(std::bad_alloc
, std::runtime_error
) {
11 messages
<< "Action name required." << std::endl
;
14 token_iterator
<char> itr(_args
, {" ", "\t"});
15 token_iterator
<char> itre
;
16 std::string sym
= *itr
++;
17 const interface_action
* act
= NULL
;
18 for(auto i
: our_rom
.rtype
->get_actions())
19 if(i
->get_symbol() == sym
) {
24 messages
<< "No such action." << std::endl
;
27 if(!(our_rom
.rtype
->action_flags(act
->id
) & 1)) {
28 messages
<< "Action not enabled." << std::endl
;
31 std::vector
<interface_action_paramval
> params
;
32 for(auto i
: act
->params
) {
33 if(regex_match("toggle", i
.model
)) {
34 interface_action_paramval pv
;
39 messages
<< "Action needs more parameters." << std::endl
;
42 std::string p
= *itr
++;
44 interface_action_paramval pv
;
45 if(r
= regex("string(:(.*))?", i
.model
)) {
47 if(r
[2] != "" && !regex_match(r
[2], p
)) {
48 messages
<< "String does not satisfy constraints."
53 messages
<< "Internal error: Bad constraint in '" << i
.model
<< "'."
58 } else if(r
= regex("int:([0-9]+),([0-9]+)", i
.model
)) {
61 low
= parse_value
<int64_t>(r
[1]);
62 high
= parse_value
<int64_t>(r
[2]);
64 messages
<< "Internal error: Unknown limits in '" << i
.model
<< "'."
69 v
= parse_value
<int64_t>(p
);
71 messages
<< "Can't parse parameter as integer." << std::endl
;
74 if(v
< low
|| v
> high
) {
75 messages
<< "Parameter out of limits." << std::endl
;
79 } else if(r
= regex("enum:(.*)", i
.model
)) {
85 if(i
.type() == JSON::string
)
87 else if(i
.type() == JSON::array
)
88 n
= i
.index(0).as_string8();
90 throw std::runtime_error("Choice not array nor "
96 messages
<< "Invalid choice for enumeration." << std::endl
;
100 } catch(std::exception
& e
) {
101 messages
<< "JSON parse error parsing " << "model: "
102 << e
.what() << std::endl
;
105 } else if(regex_match("bool", i
.model
)) {
106 int r
= string_to_bool(p
);
108 messages
<< "Bad value for boolean parameter." << std::endl
;
112 } else if(regex_match("toggle", i
.model
)) {
114 messages
<< "Internal error: Unknown parameter model '" << i
.model
<< "'."
118 params
.push_back(pv
);
121 messages
<< "Excess parameters for action." << std::endl
;
124 our_rom
.rtype
->execute_action(act
->id
, params
);