From 766e885a1b1c5afb7a62f11b427b6d135adeab87 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 10 Sep 2021 23:16:46 +0200 Subject: Clean up/improve some scriptapi error handling code --- src/script/cpp_api/s_client.cpp | 88 ++++++++++++++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 15 deletions(-) (limited to 'src/script/cpp_api/s_client.cpp') diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index f2cc9730b..c889fffa0 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -33,7 +33,11 @@ void ScriptApiClient::on_mods_loaded() lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_mods_loaded"); // Call callbacks - runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); + try { + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); + } catch (LuaError &e) { + getClient()->setFatalError(e); + } } void ScriptApiClient::on_shutdown() @@ -44,7 +48,11 @@ void ScriptApiClient::on_shutdown() lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_shutdown"); // Call callbacks - runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); + try { + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); + } catch (LuaError &e) { + getClient()->setFatalError(e); + } } bool ScriptApiClient::on_sending_message(const std::string &message) @@ -56,7 +64,12 @@ bool ScriptApiClient::on_sending_message(const std::string &message) lua_getfield(L, -1, "registered_on_sending_chat_message"); // Call callbacks lua_pushstring(L, message.c_str()); - runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + try { + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return readParam(L, -1); } @@ -69,7 +82,12 @@ bool ScriptApiClient::on_receiving_message(const std::string &message) lua_getfield(L, -1, "registered_on_receiving_chat_message"); // Call callbacks lua_pushstring(L, message.c_str()); - runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + try { + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return readParam(L, -1); } @@ -82,7 +100,11 @@ void ScriptApiClient::on_damage_taken(int32_t damage_amount) lua_getfield(L, -1, "registered_on_damage_taken"); // Call callbacks lua_pushinteger(L, damage_amount); - runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + try { + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + } catch (LuaError &e) { + getClient()->setFatalError(e); + } } void ScriptApiClient::on_hp_modification(int32_t newhp) @@ -94,7 +116,11 @@ void ScriptApiClient::on_hp_modification(int32_t newhp) lua_getfield(L, -1, "registered_on_hp_modification"); // Call callbacks lua_pushinteger(L, newhp); - runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + try { + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + } catch (LuaError &e) { + getClient()->setFatalError(e); + } } void ScriptApiClient::on_death() @@ -105,7 +131,11 @@ void ScriptApiClient::on_death() lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_death"); // Call callbacks - runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); + try { + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); + } catch (LuaError &e) { + getClient()->setFatalError(e); + } } void ScriptApiClient::environment_step(float dtime) @@ -120,8 +150,7 @@ void ScriptApiClient::environment_step(float dtime) try { runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } catch (LuaError &e) { - getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n" - + script_get_backtrace(L)); + getClient()->setFatalError(e); } } @@ -146,7 +175,11 @@ void ScriptApiClient::on_formspec_input(const std::string &formname, lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); } - runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC); + try { + runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC); + } catch (LuaError &e) { + getClient()->setFatalError(e); + } } bool ScriptApiClient::on_dignode(v3s16 p, MapNode node) @@ -164,7 +197,12 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node) pushnode(L, node, ndef); // Call functions - runCallbacks(2, RUN_CALLBACKS_MODE_OR); + try { + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return lua_toboolean(L, -1); } @@ -183,7 +221,12 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node) pushnode(L, node, ndef); // Call functions - runCallbacks(2, RUN_CALLBACKS_MODE_OR); + try { + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return readParam(L, -1); } @@ -200,7 +243,12 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini push_item_definition(L, item); // Call functions - runCallbacks(2, RUN_CALLBACKS_MODE_OR); + try { + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return readParam(L, -1); } @@ -217,7 +265,12 @@ bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &poi push_pointed_thing(L, pointed, true); // Call functions - runCallbacks(2, RUN_CALLBACKS_MODE_OR); + try { + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return readParam(L, -1); } @@ -238,7 +291,12 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory) lua_rawset(L, -3); } - runCallbacks(1, RUN_CALLBACKS_MODE_OR); + try { + runCallbacks(1, RUN_CALLBACKS_MODE_OR); + } catch (LuaError &e) { + getClient()->setFatalError(e); + return true; + } return readParam(L, -1); } -- cgit v1.2.3