1 #include "core/command.hpp"
2 #include "core/rom.hpp"
3 #include "core/moviedata.hpp"
4 #include "interface/romtype.hpp"
8 command::fnptr
<const std::string
&> action(lsnes_cmd
, "action", "Execute core action",
9 "Syntax: action <name> [<params>...]\nExecutes core action.\n",
10 [](const std::string
& _args
) throw(std::bad_alloc
, std::runtime_error
) {
12 messages
<< "Action name required." << std::endl
;
15 token_iterator
<char> itr(_args
, {" ", "\t"});
16 token_iterator
<char> itre
;
17 std::string sym
= *itr
++;
18 const interface_action
* act
= NULL
;
19 for(auto i
: our_rom
.rtype
->get_actions())
20 if(i
->get_symbol() == sym
) {
25 messages
<< "No such action." << std::endl
;
28 if(!(our_rom
.rtype
->action_flags(act
->id
) & 1)) {
29 messages
<< "Action not enabled." << std::endl
;
32 std::vector
<interface_action_paramval
> params
;
33 for(auto i
: act
->params
) {
34 if(regex_match("toggle", i
.model
)) {
35 interface_action_paramval pv
;
40 messages
<< "Action needs more parameters." << std::endl
;
43 std::string p
= *itr
++;
45 interface_action_paramval pv
;
46 if(r
= regex("string(:(.*))?", i
.model
)) {
48 if(r
[2] != "" && !regex_match(r
[2], p
)) {
49 messages
<< "String does not satisfy constraints."
54 messages
<< "Internal error: Bad constraint in '" << i
.model
<< "'."
59 } else if(r
= regex("int:([0-9]+),([0-9]+)", i
.model
)) {
62 low
= parse_value
<int64_t>(r
[1]);
63 high
= parse_value
<int64_t>(r
[2]);
65 messages
<< "Internal error: Unknown limits in '" << i
.model
<< "'."
70 v
= parse_value
<int64_t>(p
);
72 messages
<< "Can't parse parameter as integer." << std::endl
;
75 if(v
< low
|| v
> high
) {
76 messages
<< "Parameter out of limits." << std::endl
;
80 } else if(r
= regex("enum:(.*)", i
.model
)) {
86 if(i
.type() == JSON::string
)
88 else if(i
.type() == JSON::array
)
89 n
= i
.index(0).as_string8();
91 throw std::runtime_error("Choice not array nor "
97 messages
<< "Invalid choice for enumeration." << std::endl
;
101 } catch(std::exception
& e
) {
102 messages
<< "JSON parse error parsing " << "model: "
103 << e
.what() << std::endl
;
106 } else if(regex_match("bool", i
.model
)) {
107 int r
= string_to_bool(p
);
109 messages
<< "Bad value for boolean parameter." << std::endl
;
113 } else if(regex_match("toggle", i
.model
)) {
115 messages
<< "Internal error: Unknown parameter model '" << i
.model
<< "'."
119 params
.push_back(pv
);
122 messages
<< "Excess parameters for action." << std::endl
;
125 our_rom
.rtype
->execute_action(act
->id
, params
);