Skip to content

Commit

Permalink
fix: some sonar checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Oct 10, 2024
1 parent c55498c commit 8d5afaf
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 142 deletions.
196 changes: 98 additions & 98 deletions src/creatures/players/livestream/livestream.cpp

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions src/creatures/players/livestream/livestream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,25 @@ struct ViewerInfo {

class Livestream {
public:
Livestream(std::shared_ptr<ProtocolGame> client);
virtual ~Livestream();
explicit Livestream(std::shared_ptr<ProtocolGame> client);

void clear(bool clearAll);
bool checkPassword(const std::string &_password);
void handle(const std::shared_ptr<ProtocolGame> &client, const std::string &text, uint16_t channelId);
void handle(const std::shared_ptr<ProtocolGame> &client, const std::string &text);
size_t getLivestreamViewerCount();
std::vector<std::string> getLivestreamViewers() const;
std::vector<std::shared_ptr<Player>> getLivestreamViewersByIP(uint32_t ip) const;
void setKickViewer(const std::vector<std::string> &list);
const std::vector<std::string> &getLivestreamMutes() const;
void setMuteViewer(std::vector<std::string> mutes);
const std::map<std::string, uint32_t> &getLivestreamBans() const;
const std::map<std::string, uint32_t, std::less<>> &getLivestreamBans() const;
void setBanViewer(const std::vector<std::string> &bans);
bool checkBannedIP(uint32_t ip) const;
std::shared_ptr<ProtocolGame> getLivestreamOwner() const;
void setLivestreamOwner(std::shared_ptr<ProtocolGame> client);
void resetLivestreamOwner();
std::string getLivestreamPassword() const;
void setLivestreamPassword(const std::string &value);
void setLivestreamPassword(std::string_view value);
bool isLivestreamBroadcasting() const;
void setLivestreamBroadcasting(bool value);
std::string getLivestreamBroadcastTimeString() const;
Expand All @@ -68,7 +67,7 @@ class Livestream {
uint32_t getLivestreamLiveRecord() const;
void setLivestreamLiveRecord(uint32_t value);
std::string getLivestreamDescription() const;
void setLivestreamDescription(const std::string &desc);
void setLivestreamDescription(std::string_view description);
uint32_t getViewerId(const std::shared_ptr<ProtocolGame> &client) const;
// inherited
void insertLivestreamCaster();
Expand Down Expand Up @@ -257,7 +256,7 @@ class Livestream {
bool isLivestreamViewer() const;

private:
void processCommand(const std::shared_ptr<ProtocolGame> &client, const std::string &text, std::map<std::shared_ptr<ProtocolGame>, ViewerInfo>::iterator sit);
void processCommand(const std::shared_ptr<ProtocolGame> &client, std::string_view text, std::map<std::shared_ptr<ProtocolGame>, ViewerInfo>::iterator sit);
void showViewers(const std::shared_ptr<ProtocolGame> &client);
void changeViewerName(const std::shared_ptr<ProtocolGame> &client, const std::vector<std::string> &CommandParam, std::map<std::shared_ptr<ProtocolGame>, ViewerInfo>::iterator sit);
bool isNameAvailable(const std::string &name) const;
Expand All @@ -267,7 +266,7 @@ class Livestream {
friend class Player;
std::map<std::shared_ptr<ProtocolGame>, ViewerInfo> m_viewers;
std::vector<std::string> m_mutes;
std::map<std::string, uint32_t> m_bans;
std::map<std::string, uint32_t, std::less<>> m_bans;
std::shared_ptr<ProtocolGame> m_owner = nullptr;
std::string m_livestreamCasterPassword;
std::string m_livestreamCasterDescription;
Expand Down
28 changes: 11 additions & 17 deletions src/lua/functions/lua_functions_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,25 @@ class LuaFunctionsLoader {
static void setCreatureMetatable(lua_State* L, int32_t index, std::shared_ptr<Creature> creature);

template <typename T>
static typename std::enable_if<std::is_enum<T>::value, T>::type
getNumber(lua_State* L, int32_t arg) {
return static_cast<T>(static_cast<int64_t>(lua_tonumber(L, arg)));
}
template <typename T>
static typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value, T>::type getNumber(lua_State* L, int32_t arg) {
static T getNumber(lua_State* L, int32_t arg, T defaultValue = T {}) {
const auto parameters = lua_gettop(L);
if (parameters == 0 || arg > parameters) {
return defaultValue;
}

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 constexpr (std::is_enum_v<T>) {
return static_cast<T>(static_cast<int64_t>(number));
} else 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);
return T { 0 };
}
}

return static_cast<T>(number);
}
template <typename T>
static T getNumber(lua_State* L, int32_t arg, T defaultValue) {
const auto parameters = lua_gettop(L);
if (parameters == 0 || arg > parameters) {
return defaultValue;
}
return getNumber<T>(L, arg);
}
template <class T>
static T* getUserdata(lua_State* L, int32_t arg) {
T** userdata = getRawUserdata<T>(L, arg);
Expand Down
16 changes: 7 additions & 9 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,7 @@ void ProtocolGame::parseSay(NetworkMessage &msg) {
bool isLivestreamChannel = channelId == CHANNEL_LIVESTREAM;
if (isLivestreamChannel) {
g_dispatcher().addEvent(
[client = player->client, self = getThis(), text, channelId] { client->handle(self, text, channelId); }, "Livestream::handle"
[client = player->client, self = getThis(), text, channelId] { client->handle(self, text); }, "Livestream::handle"
);
return;
}
Expand Down Expand Up @@ -9351,12 +9351,11 @@ std::unordered_map<std::shared_ptr<Player>, ProtocolGame*> &ProtocolGame::getLiv
}

void ProtocolGame::insertLivestreamCaster() {
const auto &cast = getLivestreamCasters().find(player);
if (cast != getLivestreamCasters().end()) {
if (getLivestreamCasters().contains(player)) {
return;
}

getLivestreamCasters().insert(std::make_pair(player, this));
getLivestreamCasters().try_emplace(player, this);
}

void ProtocolGame::removeLivestreamCaster() {
Expand Down Expand Up @@ -9419,20 +9418,19 @@ void ProtocolGame::syncLivestreamViewerOpenContainers(const std::shared_ptr<Play

const auto &openContainers = foundPlayer->getOpenContainers();
if (!openContainers.empty()) {
for (const auto &it : openContainers) {
auto openContainer = it.second;
for (const auto &[cid, openContainer] : openContainers) {
auto opcontainer = openContainer.container;
bool hasParent = (opcontainer->getParent() != nullptr);
sendContainer(it.first, opcontainer, hasParent, openContainer.index);
sendContainer(cid, opcontainer, hasParent, openContainer.index);
}
}
}

void ProtocolGame::syncLivestreamViewerCloseContainers() {
const auto &openContainers = player->getOpenContainers();
if (!openContainers.empty()) {
for (const auto &it : openContainers) {
sendCloseContainer(it.first);
for (const auto &[cid, openContainer] : openContainers) {
sendCloseContainer(cid);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ class ProtocolGame final : public Protocol {
// Since it is false by default, we can leave it enabled to avoid directives of "FEATURE_LIVESTREAM" in the code
bool m_isLivestreamViewer = false;
#if FEATURE_LIVESTREAM == 0
bool isOldProtocol() {
bool isOldProtocol() const {
return oldProtocol;
}

Expand Down
5 changes: 2 additions & 3 deletions src/server/network/protocol/protocollogin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ void ProtocolLogin::getLivestreamViewersList(const std::string &password) {

std::vector<std::shared_ptr<Player>> players;

for (const auto &it : ProtocolGame::getLivestreamCasters()) {
std::shared_ptr<Player> player = it.first;
for (const auto &[player, _] : ProtocolGame::getLivestreamCasters()) {
if (!password.empty() && password != player->client->getLivestreamPassword()) {
continue;
}
Expand All @@ -237,7 +236,7 @@ void ProtocolLogin::getLivestreamViewersList(const std::string &password) {

uint8_t size = std::min<size_t>(std::numeric_limits<uint8_t>::max(), players.size());
output->addByte(size);
std::sort(players.begin(), players.end(), Player::sortByLivestreamViewerCount);
std::ranges::sort(players.begin(), players.end(), Player::sortByLivestreamViewerCount);

for (const auto &player : players) {
output->addByte(uint8_t());
Expand Down
10 changes: 5 additions & 5 deletions src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,16 @@ std::string toStartCaseWithSpace(const std::string &str) {
return result;
}

StringVector explodeString(const std::string &inString, const std::string &separator, int32_t limit /* = -1*/) {
StringVector explodeString(std::string_view inString, std::string_view separator, int32_t limit /*= -1*/) {
StringVector returnVector;
std::string::size_type start = 0, end = 0;
std::string_view::size_type start = 0, end = 0;

while (--limit != -1 && (end = inString.find(separator, start)) != std::string::npos) {
returnVector.push_back(inString.substr(start, end - start));
while (--limit != -1 && (end = inString.find(separator, start)) != std::string_view::npos) {
returnVector.emplace_back(inString.substr(start, end - start));
start = end + separator.size();
}

returnVector.push_back(inString.substr(start));
returnVector.emplace_back(inString.substr(start));
return returnVector;
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ std::string toStartCaseWithSpace(const std::string &str);
using StringVector = std::vector<std::string>;
using IntegerVector = std::vector<int32_t>;

StringVector explodeString(const std::string &inString, const std::string &separator, int32_t limit = -1);
StringVector explodeString(std::string_view inString, std::string_view separator, int32_t limit = -1);
IntegerVector vectorAtoi(const StringVector &stringVector);
constexpr bool hasBitSet(uint32_t flag, uint32_t flags) {
return (flags & flag) != 0;
Expand Down

0 comments on commit 8d5afaf

Please sign in to comment.