handle [args] also in [lua] tags outside events.

This commit is contained in:
gfgtdf 2016-07-09 21:40:02 +02:00
parent 9ee327f417
commit bbc32856f1
3 changed files with 25 additions and 9 deletions

View File

@ -4949,10 +4949,10 @@ void game_lua_kernel::initialize(const config& level)
game_config::load_config(game_lua_kernel::preload_config);
for (const config &cfg : game_lua_kernel::preload_scripts) {
run(cfg["code"].str().c_str());
run_lua_tag(cfg);
}
for (const config &cfg : level_lua_.child_range("lua")) {
run(cfg["code"].str().c_str());
run_lua_tag(cfg);
}
load_game(level);

View File

@ -476,19 +476,31 @@ bool lua_kernel_base::load_string(char const * prog, error_handler e_h)
return true;
}
void lua_kernel_base::run_lua_tag(const config& cfg)
{
int nArgs = 0;
if (const config& args = cfg.child("args")) {
luaW_pushconfig(this->mState, args);
++nArgs;
}
run(cfg["code"].str().c_str(), nArgs);
}
// Call load_string and protected call. Make them throw exceptions.
//
void lua_kernel_base::throwing_run(const char * prog) {
void lua_kernel_base::throwing_run(const char * prog, int nArgs)
{
cmd_log_ << "$ " << prog << "\n";
error_handler eh = std::bind(&lua_kernel_base::throw_exception, this, _1, _2 );
load_string(prog, eh);
protected_call(0, 0, eh);
lua_insert(mState, -nArgs - 1);
protected_call(nArgs, 0, eh);
}
// Do a throwing run, but if we catch a lua_error, reformat it with signature for this function and log it.
void lua_kernel_base::run(const char * prog) {
void lua_kernel_base::run(const char * prog, int nArgs)
{
try {
throwing_run(prog);
throwing_run(prog, nArgs);
} catch (game::lua_error & e) {
cmd_log_ << e.what() << "\n";
lua_kernel_base::log_error(e.what(), "In function lua_kernel::run()");
@ -507,7 +519,7 @@ void lua_kernel_base::interactive_run(char const * prog) {
// Try to load the experiment without syntax errors
load_string(experiment.c_str(), eh);
} catch (game::lua_error &) {
throwing_run(prog); // Since it failed, fall back to the usual throwing_run, on the original input.
throwing_run(prog, 0); // Since it failed, fall back to the usual throwing_run, on the original input.
return;
}
// experiment succeeded, now run but log normally.

View File

@ -23,17 +23,21 @@
struct lua_State;
class CVideo;
class config;
class lua_kernel_base {
public:
lua_kernel_base(CVideo * ptr);
virtual ~lua_kernel_base();
/** Runs a [lua] tag. Doesn't throw lua_error.*/
void run_lua_tag(const config& cfg);
/** Runs a plain script. Doesn't throw lua_error.*/
void run(char const *prog);
void run(char const *prog, int nArgs = 0);
/** Runs a plain script, but reports errors by throwing lua_error.*/
void throwing_run(char const * prog);
void throwing_run(char const * prog, int nArgs);
/** Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally. Throws exceptions.*/
void interactive_run(char const * prog);