From 7828966ef5add1989e098e932b34dc4d8c697467 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Thu, 12 Sep 2024 10:19:09 -0300 Subject: [PATCH 1/3] fix: luaConfigManagerGetFloat correct pushnumber --- src/config/configmanager.hpp | 28 +++++++++++++++++++ .../functions/core/game/config_functions.cpp | 13 +++++++-- .../functions/core/game/config_functions.hpp | 14 ++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/config/configmanager.hpp b/src/config/configmanager.hpp index 2a0c0861955..0da69e15fc7 100644 --- a/src/config/configmanager.hpp +++ b/src/config/configmanager.hpp @@ -39,6 +39,34 @@ class ConfigManager { [[nodiscard]] const std::string &getString(const ConfigKey_t &key, std::string_view context) const; [[nodiscard]] int32_t getNumber(const ConfigKey_t &key, std::string_view context) const; [[nodiscard]] bool getBoolean(const ConfigKey_t &key, std::string_view context) const; + + /** + * @brief Retrieves a float configuration value from the configuration manager, with an optional rounding. + * + * This function is a Lua binding used to get a float value from the configuration manager. It requires + * a key as the first argument, which should be a valid enumeration. An optional second boolean argument + * specifies whether the retrieved float should be rounded to two decimal places. + * + * @param L Pointer to the Lua state. The first argument must be a valid enum key, and the second argument (optional) + * can be a boolean indicating whether to round the result. + * + * @return Returns 1 after pushing the result onto the Lua stack, indicating the number of return values. + * + * @exception reportErrorFunc Throws an error if the first argument is not a valid enum. + * + * Usage: + * local result = ConfigManager.getFloat(ConfigKey.SomeKey) + * local result_rounded = ConfigManager.getFloat(ConfigKey.SomeKey, false) + * + * Detailed behavior: + * 1. Extracts the key from the first Lua stack argument as an enumeration of type `ConfigKey_t`. + * 2. Checks if the second argument is provided; if not, defaults to true for rounding. + * 3. Retrieves the float value associated with the key from the configuration manager. + * 4. If rounding is requested, rounds the value to two decimal places. + * 5. Logs the method call and the obtained value using the debug logger. + * 6. Pushes the final value (rounded or original) back onto the Lua stack. + * 7. Returns 1 to indicate a single return value. + */ [[nodiscard]] float getFloat(const ConfigKey_t &key, std::string_view context) const; private: diff --git a/src/lua/functions/core/game/config_functions.cpp b/src/lua/functions/core/game/config_functions.cpp index d0e77a69352..b839f720057 100644 --- a/src/lua/functions/core/game/config_functions.cpp +++ b/src/lua/functions/core/game/config_functions.cpp @@ -70,12 +70,21 @@ int ConfigFunctions::luaConfigManagerGetBoolean(lua_State* L) { } int ConfigFunctions::luaConfigManagerGetFloat(lua_State* L) { - auto key = getNumber(L, -1); + // configManager.getFloat(key, shouldRound = true) + + // Ensure the first argument (key) is provided and is a valid enum + auto key = getNumber(L, 1); if (!key) { reportErrorFunc("Wrong enum"); return 1; } - lua_pushnumber(L, g_configManager().getFloat(key, __FUNCTION__)); + // Check if the second argument (shouldRound) is provided and is a boolean; default to true if not provided + bool shouldRound = getBoolean(L, 2, true); + float value = g_configManager().getFloat(key, __FUNCTION__); + double finalValue = shouldRound ? static_cast(std::round(value * 100.0) / 100.0) : value; + + g_logger().debug("[{}] key: {}, finalValue: {}, shouldRound: {}", __METHOD_NAME__, magic_enum::enum_name(key), finalValue, shouldRound); + lua_pushnumber(L, finalValue); return 1; } diff --git a/src/lua/functions/core/game/config_functions.hpp b/src/lua/functions/core/game/config_functions.hpp index ae4952e9643..e7dbd973208 100644 --- a/src/lua/functions/core/game/config_functions.hpp +++ b/src/lua/functions/core/game/config_functions.hpp @@ -17,6 +17,20 @@ class ConfigFunctions final : LuaScriptInterface { static void init(lua_State* L); private: + /** + * @brief Pushes a potentially rounded floating-point configuration value to Lua. + * + * This function retrieves a configuration value based on the specified key, + * optionally rounds it to two decimal places to address floating-point precision issues, + * and then pushes this value to the Lua stack depending on the rounding flag provided. + * + * @param L Pointer to the Lua state. + * @param roundFlag A boolean value to determine if rounding should be applied. + * @return Returns 1, the number of values pushed onto the Lua stack. + * + * @note If roundFlag is true, the function rounds the float to two decimal places, + * reducing typical floating-point representation errors. + */ static int luaConfigManagerGetFloat(lua_State* L); static int luaConfigManagerGetBoolean(lua_State* L); static int luaConfigManagerGetNumber(lua_State* L); From 89537405cd5b10ae162326d6deb010f6e66b7e3f Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Thu, 12 Sep 2024 12:09:04 -0300 Subject: [PATCH 2/3] fix: remove wrong documentation --- src/config/configmanager.hpp | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/config/configmanager.hpp b/src/config/configmanager.hpp index 0da69e15fc7..2a0c0861955 100644 --- a/src/config/configmanager.hpp +++ b/src/config/configmanager.hpp @@ -39,34 +39,6 @@ class ConfigManager { [[nodiscard]] const std::string &getString(const ConfigKey_t &key, std::string_view context) const; [[nodiscard]] int32_t getNumber(const ConfigKey_t &key, std::string_view context) const; [[nodiscard]] bool getBoolean(const ConfigKey_t &key, std::string_view context) const; - - /** - * @brief Retrieves a float configuration value from the configuration manager, with an optional rounding. - * - * This function is a Lua binding used to get a float value from the configuration manager. It requires - * a key as the first argument, which should be a valid enumeration. An optional second boolean argument - * specifies whether the retrieved float should be rounded to two decimal places. - * - * @param L Pointer to the Lua state. The first argument must be a valid enum key, and the second argument (optional) - * can be a boolean indicating whether to round the result. - * - * @return Returns 1 after pushing the result onto the Lua stack, indicating the number of return values. - * - * @exception reportErrorFunc Throws an error if the first argument is not a valid enum. - * - * Usage: - * local result = ConfigManager.getFloat(ConfigKey.SomeKey) - * local result_rounded = ConfigManager.getFloat(ConfigKey.SomeKey, false) - * - * Detailed behavior: - * 1. Extracts the key from the first Lua stack argument as an enumeration of type `ConfigKey_t`. - * 2. Checks if the second argument is provided; if not, defaults to true for rounding. - * 3. Retrieves the float value associated with the key from the configuration manager. - * 4. If rounding is requested, rounds the value to two decimal places. - * 5. Logs the method call and the obtained value using the debug logger. - * 6. Pushes the final value (rounded or original) back onto the Lua stack. - * 7. Returns 1 to indicate a single return value. - */ [[nodiscard]] float getFloat(const ConfigKey_t &key, std::string_view context) const; private: From c8e7072db30959a3d5403186e822303e1cd48602 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Thu, 12 Sep 2024 12:09:23 -0300 Subject: [PATCH 3/3] fix: add correct documentation --- .../functions/core/game/config_functions.hpp | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/lua/functions/core/game/config_functions.hpp b/src/lua/functions/core/game/config_functions.hpp index e7dbd973208..9806a35f426 100644 --- a/src/lua/functions/core/game/config_functions.hpp +++ b/src/lua/functions/core/game/config_functions.hpp @@ -18,18 +18,31 @@ class ConfigFunctions final : LuaScriptInterface { private: /** - * @brief Pushes a potentially rounded floating-point configuration value to Lua. + * @brief Retrieves a float configuration value from the configuration manager, with an optional rounding. * - * This function retrieves a configuration value based on the specified key, - * optionally rounds it to two decimal places to address floating-point precision issues, - * and then pushes this value to the Lua stack depending on the rounding flag provided. + * This function is a Lua binding used to get a float value from the configuration manager. It requires + * a key as the first argument, which should be a valid enumeration. An optional second boolean argument + * specifies whether the retrieved float should be rounded to two decimal places. * - * @param L Pointer to the Lua state. - * @param roundFlag A boolean value to determine if rounding should be applied. - * @return Returns 1, the number of values pushed onto the Lua stack. + * @param L Pointer to the Lua state. The first argument must be a valid enum key, and the second argument (optional) + * can be a boolean indicating whether to round the result. * - * @note If roundFlag is true, the function rounds the float to two decimal places, - * reducing typical floating-point representation errors. + * @return Returns 1 after pushing the result onto the Lua stack, indicating the number of return values. + * + * @exception reportErrorFunc Throws an error if the first argument is not a valid enum. + * + * Usage: + * local result = ConfigManager.getFloat(ConfigKey.SomeKey) + * local result_rounded = ConfigManager.getFloat(ConfigKey.SomeKey, false) + * + * Detailed behavior: + * 1. Extracts the key from the first Lua stack argument as an enumeration of type `ConfigKey_t`. + * 2. Checks if the second argument is provided; if not, defaults to true for rounding. + * 3. Retrieves the float value associated with the key from the configuration manager. + * 4. If rounding is requested, rounds the value to two decimal places. + * 5. Logs the method call and the obtained value using the debug logger. + * 6. Pushes the final value (rounded or original) back onto the Lua stack. + * 7. Returns 1 to indicate a single return value. */ static int luaConfigManagerGetFloat(lua_State* L); static int luaConfigManagerGetBoolean(lua_State* L);