Skip to content

Commit

Permalink
fix: some sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Nov 9, 2024
1 parent c8caa7f commit 7606882
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 49 deletions.
6 changes: 3 additions & 3 deletions docs/python-scripts/modify_lua_script_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def move_init_function_to_cpp(hpp_content, cpp_content, class_name):
init_function = match.group()
init_function = re.sub(r'static void\s+', f'void {class_name}::', init_function)
# Remove extra indentation
init_function = re.sub(r'\n\t\t', r'\n\t', init_function)
init_function = init_function.replace(' \n\t\t', '\n\t')
# Remove the extra tab from the function closure
init_function = re.sub(r'\n\t\}', r'\n}', init_function)
init_function = init_function.replace('\n\t}', '\n}')

# Add a blank line before and after the function
init_function = f"\n{init_function}\n"
Expand Down Expand Up @@ -179,7 +179,7 @@ def convert_to_static(file_path):
for function in functions_to_convert:
# Regex to capture function calls and replace them with Lua::function
# Skip calls that are part of g_configManager() and handle both regular and template functions
pattern = rf'(?<!\w)(?<!g_configManager\(\)\.)' + re.escape(function) + r'(<.*?>)?\('
pattern = r'(?<!\w)(?<!g_configManager\(\)\.)' + re.escape(function) + r'(<.*?>)?\('
replacement = rf'Lua::{function}\1('
content = re.sub(pattern, replacement, content)

Expand Down
20 changes: 10 additions & 10 deletions src/creatures/combat/spells.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ LuaScriptInterface* BaseSpell::getScriptInterface() const {

bool BaseSpell::loadScriptId() {
LuaScriptInterface &luaInterface = g_scripts().getScriptInterface();
m_scriptId = luaInterface.getEvent();
if (m_scriptId == -1) {
m_spellScriptId = luaInterface.getEvent();
if (m_spellScriptId == -1) {
g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName());
return false;
}
Expand All @@ -294,15 +294,15 @@ bool BaseSpell::loadScriptId() {
}

int32_t BaseSpell::getScriptId() const {
return m_scriptId;
return m_spellScriptId;
}

void BaseSpell::setScriptId(int32_t newScriptId) {
m_scriptId = newScriptId;
m_spellScriptId = newScriptId;
}

bool BaseSpell::isLoadedScriptId() const {
return m_scriptId != 0;
return m_spellScriptId != 0;
}

CombatSpell::CombatSpell(const std::shared_ptr<Combat> &newCombat, bool newNeedTarget, bool newNeedDirection) :
Expand Down Expand Up @@ -1317,8 +1317,8 @@ LuaScriptInterface* RuneSpell::getScriptInterface() const {

bool RuneSpell::loadScriptId() {
LuaScriptInterface &luaInterface = g_scripts().getScriptInterface();
m_scriptId = luaInterface.getEvent();
if (m_scriptId == -1) {
m_spellScriptId = luaInterface.getEvent();
if (m_spellScriptId == -1) {
g_logger().error("[MoveEvent::loadScriptId] Failed to load event. Script name: '{}', Module: '{}'", luaInterface.getLoadingScriptName(), luaInterface.getInterfaceName());
return false;
}
Expand All @@ -1327,15 +1327,15 @@ bool RuneSpell::loadScriptId() {
}

int32_t RuneSpell::getScriptId() const {
return m_scriptId;
return m_spellScriptId;
}

void RuneSpell::setScriptId(int32_t newScriptId) {
m_scriptId = newScriptId;
m_spellScriptId = newScriptId;
}

bool RuneSpell::isLoadedScriptId() const {
return m_scriptId != 0;
return m_spellScriptId != 0;
}

ReturnValue RuneSpell::canExecuteAction(const std::shared_ptr<Player> &player, const Position &toPos) {
Expand Down
27 changes: 13 additions & 14 deletions src/creatures/combat/spells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,17 @@ class BaseSpell {
virtual bool castSpell(const std::shared_ptr<Creature> &creature) = 0;
virtual bool castSpell(const std::shared_ptr<Creature> &creature, const std::shared_ptr<Creature> &target) = 0;

LuaScriptInterface* getScriptInterface() const;
bool loadScriptId();
int32_t getScriptId() const;
void setScriptId(int32_t newScriptId);
bool isLoadedScriptId() const;

int32_t m_scriptId {};
virtual LuaScriptInterface* getScriptInterface() const;
virtual bool loadScriptId();
virtual int32_t getScriptId() const;
virtual void setScriptId(int32_t newScriptId);
virtual bool isLoadedScriptId() const;

SoundEffect_t soundImpactEffect = SoundEffect_t::SILENCE;
SoundEffect_t soundCastEffect = SoundEffect_t::SPELL_OR_RUNE;

protected:
int32_t m_spellScriptId {};
};

class CombatSpell final : public BaseSpell, public std::enable_shared_from_this<CombatSpell> {
Expand Down Expand Up @@ -313,11 +314,11 @@ class RuneSpell final : public Action, public Spell {
public:
using Action::Action;

LuaScriptInterface* getScriptInterface() const;
bool loadScriptId();
int32_t getScriptId() const;
void setScriptId(int32_t newScriptId);
bool isLoadedScriptId() const;
LuaScriptInterface* getScriptInterface() const override;
bool loadScriptId() override;
int32_t getScriptId() const override;
void setScriptId(int32_t newScriptId) override;
bool isLoadedScriptId() const override;

ReturnValue canExecuteAction(const std::shared_ptr<Player> &player, const Position &toPos) override;
bool hasOwnErrorHandler() override;
Expand All @@ -340,8 +341,6 @@ class RuneSpell final : public Action, public Spell {
private:
bool internalCastSpell(const std::shared_ptr<Creature> &creature, const LuaVariant &var, bool isHotkey) const;

int32_t m_scriptId {};

uint16_t runeId = 0;
uint32_t charges = 0;
bool hasCharges = false;
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5027,8 +5027,8 @@ ItemsTierCountList Player::getDepotChestItemsId() const {
ItemsTierCountList Player::getDepotInboxItemsId() const {
ItemsTierCountList itemMap;

const auto &inbox = getInbox();
const auto &container = inbox->getContainer();
const auto &inboxPtr = getInbox();
const auto &container = inboxPtr->getContainer();
if (container) {
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
const auto &item = *it;
Expand Down
4 changes: 2 additions & 2 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8251,8 +8251,8 @@ void Game::checkPlayersRecord() {
uint32_t previousRecord = playersRecord;
playersRecord = playersOnline;

for (auto &[key, it] : g_globalEvents().getEventMap(GLOBALEVENT_RECORD)) {
it->executeRecord(playersRecord, previousRecord);
for (const auto &[key, globalEvent] : g_globalEvents().getEventMap(GLOBALEVENT_RECORD)) {
globalEvent->executeRecord(playersRecord, previousRecord);
}
updatePlayersRecord();
}
Expand Down
2 changes: 1 addition & 1 deletion src/lua/creature/creatureevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool CreatureEvents::playerAdvance(
=======================
*/

CreatureEvent::CreatureEvent() { }
CreatureEvent::CreatureEvent() = default;

LuaScriptInterface* CreatureEvent::getScriptInterface() const {
return &g_scripts().getScriptInterface();
Expand Down
5 changes: 3 additions & 2 deletions src/lua/functions/creatures/combat/combat_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void CombatFunctions::init(lua_State* L) {

int CombatFunctions::luaCombatCreate(lua_State* L) {
// Combat()
std::shared_ptr<Combat> combat = std::make_shared<Combat>();
auto combat = std::make_shared<Combat>();
Lua::pushUserdata<Combat>(L, combat);
Lua::setMetatable(L, -1, "Combat");
return 1;
Expand Down Expand Up @@ -168,8 +168,9 @@ int CombatFunctions::luaCombatExecute(lua_State* L) {
}

if (Lua::isUserdata(L, 2)) {
using enum LuaData_t;
const LuaData_t type = Lua::getUserdataType(L, 2);
if (type != LuaData_t::Player && type != LuaData_t::Monster && type != LuaData_t::Npc) {
if (type != Player && type != Monster && type != Npc) {
Lua::pushBoolean(L, false);
return 1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/lua/functions/creatures/creature_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ int CreatureFunctions::luaCreatureCreate(lua_State* L) {
} else if (Lua::isString(L, 2)) {
creature = g_game().getCreatureByName(Lua::getString(L, 2));
} else if (Lua::isUserdata(L, 2)) {
using enum LuaData_t;
const LuaData_t type = Lua::getUserdataType(L, 2);
if (type != LuaData_t::Player && type != LuaData_t::Monster && type != LuaData_t::Npc) {
if (type != Player && type != Monster && type != Npc) {
lua_pushnil(L);
return 1;
}
Expand Down
32 changes: 19 additions & 13 deletions src/lua/functions/lua_functions_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Guild;
class Zone;
class KV;

typedef double lua_Number;
using lua_Number = double;

struct LuaVariant;

Expand Down Expand Up @@ -63,23 +63,29 @@ class Lua {
static void setCreatureMetatable(lua_State* L, int32_t index, const std::shared_ptr<Creature> &creature);

template <typename T>
static std::enable_if_t<std::is_enum_v<T>, T>
getNumber(lua_State* L, int32_t arg) {
return static_cast<T>(static_cast<int64_t>(lua_tonumber(L, arg)));
}
template <typename T>
static std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>, T> getNumber(lua_State* L, int32_t arg) {
static T getNumber(lua_State* L, int32_t arg) {
auto number = lua_tonumber(L, arg);
// If there is overflow, we return the value 0
if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>) {
if (number < 0) {
g_logger().debug("[{}] overflow, setting to default signed value (0)", __FUNCTION__);
number = T(0);

if constexpr (std::is_enum_v<T>) {
return static_cast<T>(static_cast<int64_t>(number));
}

if constexpr (std::is_integral_v<T>) {
if constexpr (std::is_unsigned_v<T>) {
if (number < 0) {
g_logger().debug("[{}] overflow, setting to default unsigned value (0)", __FUNCTION__);
return T(0);
}
}
return static_cast<T>(number);
}
if constexpr (std::is_floating_point_v<T>) {
return static_cast<T>(number);
}

return static_cast<T>(number);
return T{};
}

template <typename T>
static T getNumber(lua_State* L, int32_t arg, T defaultValue) {
const auto parameters = lua_gettop(L);
Expand Down
3 changes: 2 additions & 1 deletion src/lua/global/globalevent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class GlobalEvents {

private:
GlobalEventMap thinkMap, serverMap, timerMap;
uint64_t thinkEventId = 0, timerEventId = 0;
uint64_t thinkEventId = 0;
uint64_t timerEventId = 0;
};

constexpr auto g_globalEvents = GlobalEvents::getInstance;
Expand Down

0 comments on commit 7606882

Please sign in to comment.