Add proper lua api deprecated handling
authorsapier <Sapier at GMX dot net>
Tue, 29 Apr 2014 15:47:34 +0000 (17:47 +0200)
committersapier <Sapier at GMX dot net>
Tue, 29 Apr 2014 20:49:04 +0000 (22:49 +0200)
builtin/misc.lua
minetest.conf.example
src/defaultsettings.cpp
src/guiFormSpecMenu.cpp
src/script/common/c_internal.cpp
src/script/common/c_internal.h
src/script/lua_api/l_object.cpp
src/script/lua_api/l_particles.cpp
src/script/lua_api/l_util.cpp
src/script/scripting_game.cpp
src/script/scripting_game.h

index f91f5e095d7c3a04728463dac19a9557cf9fd891..82cc527cd821d71d0f04387e864e88f8f5d4fd9b 100644 (file)
@@ -85,6 +85,7 @@ function minetest.get_item_group(name, group)
 end
 
 function minetest.get_node_group(name, group)
+       minetest.log("deprecated", "Deprecated usage of get_node_group, use get_item_group instead")
        return minetest.get_item_group(name, group)
 end
 
index dfebe0102bfafd473a055abfd21e4b7831207864..c134446579e23c2b74a65d7e122865b8f7d03db3 100644 (file)
 #disable_anticheat = false
 # If true, actions are recorded for rollback
 #enable_rollback_recording = false
+# handling for deprecated lua api calls
+#    "legacy" = (try to) mimic old behaviour (default for release)
+#    "log"    = mimic and log backtrace of deprecated call (default for debug)
+#    "error"  = abort on usage of deprecated call (suggested for mod developers)
+#deprecated_lua_api_handling = legacy
 
 # Profiler data print interval. #0 = disable.
 #profiler_print_interval = 0
index ff0da4b2c76058c4f64de8c2f01cca2b5b30caf5..3c17e650e76e4eb2b2c9f631a1760070a814a6da 100644 (file)
@@ -201,6 +201,11 @@ void set_default_settings(Settings *settings)
        settings->setDefault("disallow_empty_password", "false");
        settings->setDefault("disable_anticheat", "false");
        settings->setDefault("enable_rollback_recording", "false");
+#ifdef NDEBUG
+       settings->setDefault("deprecated_lua_api_handling", "legacy");
+#else
+       settings->setDefault("deprecated_lua_api_handling", "log");
+#endif
 
        settings->setDefault("profiler_print_interval", "0");
        settings->setDefault("enable_mapgen_debug_info", "false");
index 962d151888f3bb2cffd5430c6e8df318a9d5795a..129ab02593eabc7db272e86d8d6186849e579632 100644 (file)
@@ -45,6 +45,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "filesys.h"
 #include "gettime.h"
 #include "gettext.h"
+#include "scripting_game.h"
 
 #define MY_CHECKPOS(a,b)                                                                                                       \
        if (v_pos.size() != 2) {                                                                                                \
@@ -1478,7 +1479,13 @@ void GUIFormSpecMenu::parseElement(parserData* data,std::string element)
        std::string type = trim(parts[0]);
        std::string description = trim(parts[1]);
 
-       if ((type == "size") || (type == "invsize")){
+       if (type == "size") {
+               parseSize(data,description);
+               return;
+       }
+
+       if (type == "invsize") {
+               log_deprecated("Deprecated formspec element \"invsize\" is used");
                parseSize(data,description);
                return;
        }
index 4263dec90d4e0e1dad6f2b600c24df7e2d9d0c56..4c6604f65d43b01303dde6f1495cff53002522c6 100644 (file)
@@ -19,6 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "common/c_internal.h"
 #include "debug.h"
+#include "log.h"
+#include "main.h"
+#include "settings.h"
 
 std::string script_get_backtrace(lua_State *L)
 {
@@ -109,4 +112,40 @@ void script_run_callbacks(lua_State *L, int nargs, RunCallbacksMode mode)
        lua_remove(L, -2); // Remove error handler
 }
 
+void log_deprecated(lua_State *L, std::string message)
+{
+       static bool configured = false;
+       static bool dolog      = false;
+       static bool doerror    = false;
+
+       // performance optimization to not have to read and compare setting for every logline
+       if (!configured) {
+               std::string value = g_settings->get("deprecated_lua_api_handling");
+               if (value == "log") {
+                       dolog = true;
+               }
+               if (value == "error") {
+                       dolog = true;
+                       doerror = true;
+               }
+       }
+
+       if (doerror) {
+               if (L != NULL) {
+                       script_error(L);
+               } else {
+                       /* As of april 2014 assert is not optimized to nop in release builds
+                        * therefore this is correct. */
+                       assert("Can't do a scripterror for this deprecated message, so exit completely!");
+               }
+       }
+
+       if (dolog) {
+               /* abusing actionstream because of lack of file-only-logged loglevel */
+               actionstream << message << std::endl;
+               if (L != NULL) {
+                       actionstream << script_get_backtrace(L) << std::endl;
+               }
+       }
+}
 
index f3ef18d70e3e9d081b3acc62e008f6f32932d2ed..eb9181b09cfe2801da1a1eea556f4e8c9543826f 100644 (file)
@@ -68,7 +68,7 @@ std::string script_get_backtrace(lua_State *L);
 int script_error_handler(lua_State *L);
 int script_exception_wrapper(lua_State *L, lua_CFunction f);
 void script_error(lua_State *L);
-void script_run_callbacks(lua_State *L, int nargs,
-               RunCallbacksMode mode);
+void script_run_callbacks(lua_State *L, int nargs, RunCallbacksMode mode);
+void log_deprecated(lua_State *L, std::string message);
 
 #endif /* C_INTERNAL_H_ */
index 5e3ddd2358c30eb16e20b2f24ec1e5a649bb7c8f..30d423e6ae41b6b6d8121f841fd1ab41e6a4f8c5 100644 (file)
@@ -659,6 +659,7 @@ int ObjectRef::l_get_entity_name(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
        ObjectRef *ref = checkobject(L, 1);
        LuaEntitySAO *co = getluaobject(ref);
+       log_deprecated(L,"Deprecated call to \"get_entity_name");
        if(co == NULL) return 0;
        // Do it
        std::string name = co->getName();
index 088eba06e7a6d53d0140754047d4939d578a10fe..6769f5c230d86b6d0eef226dbda0eccf0cf379c6 100644 (file)
@@ -44,6 +44,7 @@ int ModApiParticles::l_add_particle(lua_State *L)
 
        if (lua_gettop(L) > 1) // deprecated
        {
+               log_deprecated(L,"Deprecated add_particle call with individual parameters instead of definition");
                pos = check_v3f(L, 1);
                vel = check_v3f(L, 2);
                acc = check_v3f(L, 3);
@@ -128,6 +129,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
 
        if (lua_gettop(L) > 1) //deprecated
        {
+               log_deprecated(L,"Deprecated add_particlespawner call with individual parameters instead of definition");
                amount = luaL_checknumber(L, 1);
                time = luaL_checknumber(L, 2);
                minpos = check_v3f(L, 3);
index 90a1d77ab2c12c5cf5260d67e4735847dd910a03..7babaaf7160faee85e29c7c6004b676dc6913f06 100644 (file)
@@ -78,6 +78,11 @@ int ModApiUtil::l_log(lua_State *L)
                        level = LMT_ACTION;
                else if(levelname == "verbose")
                        level = LMT_VERBOSE;
+               else if (levelname == "deprecated") {
+                       log_deprecated(L,text);
+                       return 0;
+               }
+
        }
        log_printline(level, text);
        return 0;
index 12baac032b94f3efc6c1c18c4782ddf82cc19700..b2c2150c66eebb062d0018fa75795ee9e90fbe61 100644 (file)
@@ -98,3 +98,8 @@ void GameScripting::InitializeModApi(lua_State *L, int top)
        ObjectRef::Register(L);
        LuaSettings::Register(L);
 }
+
+void log_deprecated(std::string message)
+{
+       log_deprecated(NULL,message);
+}
index ed65679223507d8e956ba44f69576c4fa1fc72ed..14dbd9170d2df4fb98280dc8f2f1b8d987705077 100644 (file)
@@ -50,4 +50,6 @@ private:
        void InitializeModApi(lua_State *L, int top);
 };
 
+void log_deprecated(std::string message);
+
 #endif /* SCRIPTING_GAME_H_ */