1 #include "core/command.hpp"
2 #include "core/instance.hpp"
3 #include "core/messages.hpp"
4 #include "core/moviedata.hpp"
5 #include "core/rom.hpp"
9 command::fnptr
<const std::string
&> CMD_action(lsnes_cmds
, "action", "Execute core action",
10 "Syntax: action <name> [<params>...]\nExecutes core action.\n",
11 [](const std::string
& _args
) throw(std::bad_alloc
, std::runtime_error
) {
14 messages
<< "Action name required." << std::endl
;
17 token_iterator
<char> itr(_args
, {" ", "\t"});
18 token_iterator
<char> itre
;
19 std::string sym
= *itr
++;
20 const interface_action
* act
= NULL
;
21 for(auto i
: core
.rom
->rtype
->get_actions())
22 if(i
->get_symbol() == sym
) {
27 messages
<< "No such action." << std::endl
;
30 if(!(core
.rom
->rtype
->action_flags(act
->id
) & 1)) {
31 messages
<< "Action not enabled." << std::endl
;
34 std::vector
<interface_action_paramval
> params
;
35 for(auto i
: act
->params
) {
36 if(regex_match("toggle", i
.model
)) {
37 interface_action_paramval pv
;
42 messages
<< "Action needs more parameters." << std::endl
;
45 std::string p
= *itr
++;
47 interface_action_paramval pv
;
48 if(r
= regex("string(:(.*))?", i
.model
)) {
50 if(r
[2] != "" && !regex_match(r
[2], p
)) {
51 messages
<< "String does not satisfy constraints."
56 messages
<< "Internal error: Bad constraint in '" << i
.model
<< "'."
61 } else if(r
= regex("int:([0-9]+),([0-9]+)", i
.model
)) {
64 low
= parse_value
<int64_t>(r
[1]);
65 high
= parse_value
<int64_t>(r
[2]);
67 messages
<< "Internal error: Unknown limits in '" << i
.model
<< "'."
72 v
= parse_value
<int64_t>(p
);
74 messages
<< "Can't parse parameter as integer." << std::endl
;
77 if(v
< low
|| v
> high
) {
78 messages
<< "Parameter out of limits." << std::endl
;
82 } else if(r
= regex("enum:(.*)", i
.model
)) {
88 if(i
.type() == JSON::string
)
90 else if(i
.type() == JSON::array
)
91 n
= i
.index(0).as_string8();
93 throw std::runtime_error("Choice not array nor "
99 messages
<< "Invalid choice for enumeration." << std::endl
;
103 } catch(std::exception
& e
) {
104 messages
<< "JSON parse error parsing " << "model: "
105 << e
.what() << std::endl
;
108 } else if(regex_match("bool", i
.model
)) {
109 int r
= string_to_bool(p
);
111 messages
<< "Bad value for boolean parameter." << std::endl
;
115 } else if(regex_match("toggle", i
.model
)) {
117 messages
<< "Internal error: Unknown parameter model '" << i
.model
<< "'."
121 params
.push_back(pv
);
124 messages
<< "Excess parameters for action." << std::endl
;
127 core
.rom
->rtype
->execute_action(act
->id
, params
);