Skip to content

Commit c04a23e

Browse files
committed
add a few debug console commands for running scripts
We have debug commands for sexps, so here are a few for scripts. These will be very useful for script developers to perform diagnostics and troubleshooting.
1 parent 2cb76e8 commit c04a23e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

code/parse/sexp.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30752,6 +30752,73 @@ DCF(sexp, "Runs the given sexp")
3075230752
dc_printf("Result = %d\n", sexp_val);
3075330753
}
3075430754

30755+
DCF(script, "Runs the given script, without regard to its return value")
30756+
{
30757+
SCP_string script;
30758+
30759+
if (dc_optional_string_either("help", "--help")) {
30760+
dc_printf( "Usage: script <script code>\n. Runs the given script.\n");
30761+
return;
30762+
}
30763+
30764+
dc_stuff_string(script);
30765+
30766+
bool success = Script_system.EvalString(script.c_str());
30767+
dc_printf("Script %s\n", success ? "successful" : "unsuccessful");
30768+
}
30769+
30770+
template <typename T>
30771+
void script_eval_with_return(const char *help_text, const char *format, void (*to_scp_string)(T, SCP_string&))
30772+
{
30773+
SCP_string script;
30774+
30775+
if (dc_optional_string_either("help", "--help")) {
30776+
dc_printf("%s", help_text);
30777+
return;
30778+
}
30779+
30780+
dc_stuff_string(script);
30781+
30782+
T result{};
30783+
SCP_string result_string;
30784+
30785+
bool success = Script_system.EvalStringWithReturn(script.c_str(), format, &result);
30786+
to_scp_string(result, result_string);
30787+
dc_printf("Script %s. Result = %s\n", success ? "successful" : "unsuccessful", result_string.c_str());
30788+
}
30789+
30790+
DCF(script_eval_bool, "Evaluates the given script, returning a boolean result")
30791+
{
30792+
script_eval_with_return<bool>("Usage: script_eval_bool <script code>\n. Runs the given script, returning a boolean result.\n",
30793+
"|b",
30794+
[](bool b, SCP_string& str) { str = b ? "true" : "false"; }
30795+
);
30796+
}
30797+
30798+
DCF(script_eval_int, "Evaluates the given script, returning an integer result")
30799+
{
30800+
script_eval_with_return<int>("Usage: script_eval_int <script code>\n. Runs the given script, returning an integer result.\n",
30801+
"|i",
30802+
[](int i, SCP_string& str) { sprintf(str, "%d", i); }
30803+
);
30804+
}
30805+
30806+
DCF(script_eval_float, "Evaluates the given script, returning a float result")
30807+
{
30808+
script_eval_with_return<float>("Usage: script_eval_float <script code>\n. Runs the given script, returning a float result.\n",
30809+
"|f",
30810+
[](float f, SCP_string& str) { sprintf(str, "%f", f); }
30811+
);
30812+
}
30813+
30814+
DCF(script_eval_string, "Evaluates the given script, returning a string result")
30815+
{
30816+
script_eval_with_return<const char*>("Usage: script_eval_string <script code>\n. Runs the given script, returning a string result.\n",
30817+
"|s",
30818+
[](const char* s, SCP_string& str) { sprintf(str, "'%s'", s ? s : ""); }
30819+
);
30820+
}
30821+
3075530822
bool map_opf_to_opr(sexp_opf_t opf_type, sexp_opr_t &opr_type)
3075630823
{
3075730824
opr_type = OPR_NONE;

0 commit comments

Comments
 (0)