Skip to content

Commit 6ddb57b

Browse files
authored
Merge pull request #6744 from Goober5000/script_debug_console_commands
add a few debug console commands for running scripts
2 parents 552f999 + c04a23e commit 6ddb57b

File tree

1 file changed

+76
-10
lines changed

1 file changed

+76
-10
lines changed

code/parse/sexp.cpp

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30719,38 +30719,104 @@ int run_sexp(const char* sexpression, bool run_eval_num, bool *is_nan_or_nan_for
3071930719
return sexp_val;
3072030720
}
3072130721

30722-
DCF(sexpc, "Always runs the given sexp command (Warning! There is no undo for this!)")
30722+
DCF(sexpc, "Runs the given sexp command, wrapped as an action of a when-true condition")
3072330723
{
3072430724
SCP_string sexp;
30725-
SCP_string sexp_always;
30725+
SCP_string sexp_wrapped;
3072630726

3072730727
if (dc_optional_string_either("help", "--help")) {
30728-
dc_printf( "Usage: sexpc sexpression\n. Always runs the given sexp as '( when ( true ) ( sexp ) )' .\n" );
30728+
dc_printf( "Usage: sexpc <sexpression>\n. Runs the given sexp as '( when ( true ) ( sexpression ) )' .\n" );
3072930729
return;
3073030730
}
3073130731

3073230732
dc_stuff_string(sexp);
3073330733

30734-
sexp_always = "( when ( true ) ( " + sexp + " ) )";
30734+
sexp_wrapped = "( when ( true ) ( " + sexp + " ) )";
3073530735

30736-
int sexp_val = run_sexp(sexp_always.c_str());
30737-
dc_printf("SEXP '%s' run, sexp_val = %d\n", sexp_always.c_str(), sexp_val);
30736+
int sexp_val = run_sexp(sexp_wrapped.c_str());
30737+
dc_printf("SEXP '%s' run; sexp_val = %d\n", sexp_wrapped.c_str(), sexp_val);
3073830738
}
3073930739

30740-
30741-
DCF(sexp,"Runs the given sexp")
30740+
DCF(sexp, "Runs the given sexp")
3074230741
{
3074330742
SCP_string sexp;
3074430743

3074530744
if (dc_optional_string_either("help", "--help")) {
30746-
dc_printf( "Usage: sexp 'sexpression'\n. Runs the given sexp.\n");
30745+
dc_printf( "Usage: sexp <sexpression>\n. Runs the given sexp.\n");
3074730746
return;
3074830747
}
3074930748

3075030749
dc_stuff_string(sexp);
3075130750

3075230751
int sexp_val = run_sexp(sexp.c_str());
30753-
dc_printf("SEXP '%s' run, sexp_val = %d\n", sexp.c_str(), sexp_val);
30752+
dc_printf("Result = %d\n", sexp_val);
30753+
}
30754+
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+
);
3075430820
}
3075530821

3075630822
bool map_opf_to_opr(sexp_opf_t opf_type, sexp_opr_t &opr_type)

0 commit comments

Comments
 (0)