From 2efae3ffd720095222c800e016286a45c9fe1e5c Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 21 Jan 2017 15:02:08 +0100 Subject: [CSM] Client side modding * rename GameScripting to ServerScripting * Make getBuiltinLuaPath static serverside * Add on_shutdown callback * Add on_receiving_chat_message & on_sending_chat_message callbacks * ScriptApiBase: use IGameDef instead of Server This permits to share common attribute between client & server * Enable mod security in client side modding without conditions --- src/script/cpp_api/s_client.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/script/cpp_api/s_client.cpp (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 new file mode 100644 index 000000000..08af8ebdc --- /dev/null +++ b/src/script/cpp_api/s_client.cpp @@ -0,0 +1,61 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola +Copyright (C) 2017 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "s_client.h" +#include "s_internal.h" + +void ScriptApiClient::on_shutdown() +{ + SCRIPTAPI_PRECHECKHEADER + + // Get registered shutdown hooks + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_shutdown"); + // Call callbacks + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); +} + +bool ScriptApiClient::on_sending_message(const std::string &message) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_sending_chat_messages"); + // Call callbacks + lua_pushstring(L, message.c_str()); + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + bool ate = lua_toboolean(L, -1); + return ate; +} + +bool ScriptApiClient::on_receiving_message(const std::string &message) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_receiving_chat_messages"); + // Call callbacks + lua_pushstring(L, message.c_str()); + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); + bool ate = lua_toboolean(L, -1); + return ate; +} -- cgit v1.2.3 From 9978f5af828550d819890fed1fc56d65838a2c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Sun, 22 Jan 2017 00:20:55 +0100 Subject: [CSM] Add on_death, on_hp_modification & oh_damage_taken callbacks (#5093) * Add on_death callback * Add on_hp_modification & on_damage_taken callbacks * move preview code to preview.lua --- src/script/cpp_api/s_client.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (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 08af8ebdc..f0676f4c2 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -59,3 +59,38 @@ bool ScriptApiClient::on_receiving_message(const std::string &message) bool ate = lua_toboolean(L, -1); return ate; } + +void ScriptApiClient::on_damage_taken(int32_t damage_amount) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_damage_taken"); + // Call callbacks + lua_pushinteger(L, damage_amount); + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); +} + +void ScriptApiClient::on_hp_modification(int32_t newhp) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_hp_modification"); + // Call callbacks + lua_pushinteger(L, newhp); + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); +} + +void ScriptApiClient::on_death() +{ + SCRIPTAPI_PRECHECKHEADER + + // Get registered shutdown hooks + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_death"); + // Call callbacks + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); +} -- cgit v1.2.3 From 2c19d51409ca903021e0b508e5bc15299c4e51dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Sun, 22 Jan 2017 11:17:41 +0100 Subject: [CSM] sound_play & sound_stop support + client_lua_api doc (#5096) * squashed: CSM: Implement register_globalstep * Re-use fatal error mechanism from server to disconnect client on CSM error * Little client functions cleanups * squashed: CSM: add core.after function * core.after is shared code between client & server * ModApiUtil get_us_time feature enabled for client --- src/script/cpp_api/s_client.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (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 f0676f4c2..ce88d67e3 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "s_client.h" #include "s_internal.h" +#include "client.h" void ScriptApiClient::on_shutdown() { @@ -94,3 +95,20 @@ void ScriptApiClient::on_death() // Call callbacks runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); } + +void ScriptApiClient::environment_step(float dtime) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_globalsteps + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_globalsteps"); + // Call callbacks + lua_pushnumber(L, 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)); + } +} -- cgit v1.2.3 From c42c53fccf87a3819ca78de52f8f20c47c4fbb9f Mon Sep 17 00:00:00 2001 From: red-001 Date: Tue, 24 Jan 2017 16:26:15 +0000 Subject: [CSM] Add local formspecs. (#5094) --- src/script/cpp_api/s_client.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (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 ce88d67e3..1827d483b 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -112,3 +112,27 @@ void ScriptApiClient::environment_step(float dtime) + script_get_backtrace(L)); } } + +void ScriptApiClient::on_formspec_input(const std::string &formname, + const StringMap &fields) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_formspec_input"); + // Call callbacks + // param 1 + lua_pushstring(L, formname.c_str()); + // param 2 + lua_newtable(L); + StringMap::const_iterator it; + for (it = fields.begin(); it != fields.end(); ++it) { + const std::string &name = it->first; + const std::string &value = it->second; + lua_pushstring(L, name.c_str()); + lua_pushlstring(L, value.c_str(), value.size()); + lua_settable(L, -3); + } + runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC); +} -- cgit v1.2.3 From 073f5cf03d95ce1cdf04ce8a0adcaf1fc571d95f Mon Sep 17 00:00:00 2001 From: red-001 Date: Sun, 29 Jan 2017 17:43:44 +0000 Subject: [CSM] Add `on_dignode` callback (#5140) --- src/script/cpp_api/s_client.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (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 1827d483b..2c8fee334 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -21,6 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "s_client.h" #include "s_internal.h" #include "client.h" +#include "common/c_converter.h" +#include "common/c_content.h" void ScriptApiClient::on_shutdown() { @@ -136,3 +138,23 @@ void ScriptApiClient::on_formspec_input(const std::string &formname, } runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC); } + +bool ScriptApiClient::on_dignode(v3s16 p, MapNode node) +{ + SCRIPTAPI_PRECHECKHEADER + + INodeDefManager *ndef = getClient()->ndef(); + + // Get core.registered_on_dignode + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_dignode"); + + // Push data + push_v3s16(L, p); + pushnode(L, node, ndef); + + // Call functions + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + bool blocked = lua_toboolean(L, -1); + return blocked; +} \ No newline at end of file -- cgit v1.2.3 From 0727bb3ddd9c550ff962af4546bac8cc058bce73 Mon Sep 17 00:00:00 2001 From: red-001 Date: Sun, 29 Jan 2017 18:28:38 +0000 Subject: [CSM] Add `on_punchnode` callback --- src/script/cpp_api/s_client.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (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 2c8fee334..8c5e3796b 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -157,4 +157,24 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node) runCallbacks(2, RUN_CALLBACKS_MODE_OR); bool blocked = lua_toboolean(L, -1); return blocked; -} \ No newline at end of file +} + +bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node) +{ + SCRIPTAPI_PRECHECKHEADER + + INodeDefManager *ndef = getClient()->ndef(); + + // Get core.registered_on_punchgnode + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_punchnode"); + + // Push data + push_v3s16(L, p); + pushnode(L, node, ndef); + + // Call functions + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + bool blocked = lua_toboolean(L, -1); + return blocked; +} -- cgit v1.2.3 From 44ca9c9cb2079fa97068adb8ee894c5ae13a9975 Mon Sep 17 00:00:00 2001 From: nerzhul Date: Mon, 13 Mar 2017 15:55:30 +0100 Subject: [CSM] Add enable_client_modding param (default: false) --- src/script/cpp_api/s_client.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 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 8c5e3796b..154dd6194 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -155,8 +155,7 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node) // Call functions runCallbacks(2, RUN_CALLBACKS_MODE_OR); - bool blocked = lua_toboolean(L, -1); - return blocked; + return lua_toboolean(L, -1); } bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node) -- cgit v1.2.3 From 0891975ad6c8d6d3e15b20f33b22cf5baca7eb62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Fri, 17 Mar 2017 07:48:29 +0100 Subject: [CSM] Add core.get_timeofday & core.get_day_count env calls (#5401) * [CSM] Add core.get_timeofday & core.get_day_count env calls * [CSM] Add core.get_node_level, core.get_node_max_level, core.find_node_near --- src/script/cpp_api/s_client.cpp | 5 +++++ 1 file changed, 5 insertions(+) (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 154dd6194..666fd693d 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -177,3 +177,8 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node) bool blocked = lua_toboolean(L, -1); return blocked; } + +void ScriptApiClient::setEnv(ClientEnvironment *env) +{ + ScriptApiBase::setEnv(env); +} -- cgit v1.2.3 From f73534640a18709921519ee43110292cf658e67f Mon Sep 17 00:00:00 2001 From: Vincent Glize Date: Sat, 8 Apr 2017 08:20:30 +0200 Subject: [CSM] Add event on_connect player API lua (#5540) * Add event on_connect player API lua --- src/script/cpp_api/s_client.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (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 666fd693d..a8a7d5e26 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -35,6 +35,17 @@ void ScriptApiClient::on_shutdown() runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); } +void ScriptApiClient::on_connect() +{ + SCRIPTAPI_PRECHECKHEADER + + // get registered connect hooks + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_connect"); + // Call callback + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); +} + bool ScriptApiClient::on_sending_message(const std::string &message) { SCRIPTAPI_PRECHECKHEADER -- cgit v1.2.3 From dc5bc6cac7b81ab27e0064bc25b5fd1d8d617340 Mon Sep 17 00:00:00 2001 From: Vincent Glize Date: Sat, 29 Apr 2017 12:08:16 +0200 Subject: [CSM] Add event on_place_node API lua (#5548) * [CSM] Add event on_place_node API lua --- src/script/cpp_api/s_client.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (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 a8a7d5e26..4bc368d1d 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client.h" #include "common/c_converter.h" #include "common/c_content.h" +#include "s_item.h" void ScriptApiClient::on_shutdown() { @@ -189,6 +190,23 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node) return blocked; } +bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_placenode + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_placenode"); + + // Push data + push_pointed_thing(L, pointed); + push_item_definition(L, item); + + // Call functions + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + return lua_toboolean(L, -1); +} + void ScriptApiClient::setEnv(ClientEnvironment *env) { ScriptApiBase::setEnv(env); -- cgit v1.2.3 From 5ebf8f945050e9c74a3bb6784a0844d1fb68bdff Mon Sep 17 00:00:00 2001 From: red-001 Date: Sat, 6 May 2017 20:12:44 +0100 Subject: [CSM] add `on_item_use` (#5544) --- src/script/cpp_api/s_client.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (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 4bc368d1d..d5ec52407 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -207,6 +207,23 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini return lua_toboolean(L, -1); } +bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_item_use + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_item_use"); + + // Push data + LuaItemStack::create(L, item); + push_pointed_thing(L, pointed); + + // Call functions + runCallbacks(2, RUN_CALLBACKS_MODE_OR); + return lua_toboolean(L, -1); +} + void ScriptApiClient::setEnv(ClientEnvironment *env) { ScriptApiBase::setEnv(env); -- cgit v1.2.3 From 772944daf69ab463568fbbf35a9011e4c8e6c44a Mon Sep 17 00:00:00 2001 From: red-001 Date: Sat, 20 May 2017 15:45:49 +0100 Subject: Fix CSM crash (#5779) Caused by dc5bc6c and them made worse by 5ebf8f9 --- src/script/cpp_api/s_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 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 d5ec52407..55d309fda 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -199,7 +199,7 @@ bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefini lua_getfield(L, -1, "registered_on_placenode"); // Push data - push_pointed_thing(L, pointed); + push_pointed_thing(L, pointed, true); push_item_definition(L, item); // Call functions @@ -217,7 +217,7 @@ bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &poi // Push data LuaItemStack::create(L, item); - push_pointed_thing(L, pointed); + push_pointed_thing(L, pointed, true); // Call functions runCallbacks(2, RUN_CALLBACKS_MODE_OR); -- cgit v1.2.3