diff --git a/dll/dll/network.h b/dll/dll/network.h index 74df2b67..9ea4382f 100644 --- a/dll/dll/network.h +++ b/dll/dll/network.h @@ -133,10 +133,10 @@ public: void setAppID(uint32 appid); void Run(); - // send to a specific user, if 0 was passed to set_dest_id() then this will be broadcasted to all users on the network + // send to a specific user, set_dest_id() must be called bool sendTo(Common_Message *msg, bool reliable, Connection *conn = NULL); - // send to all users whose account type is Individual, no need to call set_dest_id(), this is done automatically + // send to all users whose account type is Individual, no need to call set_dest_id(), this is done automatically bool sendToAllIndividuals(Common_Message *msg, bool reliable); // send to all active/current connections, no need to call set_dest_id(), this is done automatically diff --git a/dll/dll/settings.h b/dll/dll/settings.h index ba500081..1e4e1f83 100644 --- a/dll/dll/settings.h +++ b/dll/dll/settings.h @@ -315,11 +315,11 @@ public: std::set modSet(); //leaderboards - void setLeaderboard(std::string leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type); - std::map getLeaderboards(); + void setLeaderboard(const std::string &leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type); + const std::map& getLeaderboards() const; //stats - const std::map& getStats(); + const std::map& getStats() const; void setStatDefiniton(const std::string &name, const struct Stat_config &stat_config); //images diff --git a/dll/dll/steam_matchmaking.h b/dll/dll/steam_matchmaking.h index e6ae84ed..16e60d08 100644 --- a/dll/dll/steam_matchmaking.h +++ b/dll/dll/steam_matchmaking.h @@ -100,12 +100,9 @@ google::protobuf::Map::const_iterator caseinsensitive_f { auto x = map.begin(); while (x != map.end()) { - if (key.size() == x->first.size() && std::equal(x->first.begin(), x->first.end(), key.begin(), - [](char a, char b) { - return tolower(a) == tolower(b); - })) { - break; - } + if (common_helpers::str_cmp_insensitive(key, x->first)) { + break; + } ++x; } diff --git a/dll/net.proto b/dll/net.proto index 7e5b36f9..f83d7d77 100644 --- a/dll/net.proto +++ b/dll/net.proto @@ -216,7 +216,7 @@ message Steam_Messages { } message GameServerStats_Messages { - // --- baisc definitions + // --- basic definitions message StatInfo { enum Stat_Type { STAT_TYPE_INT = 0; diff --git a/dll/network.cpp b/dll/network.cpp index 20d531cc..9a350610 100644 --- a/dll/network.cpp +++ b/dll/network.cpp @@ -612,28 +612,11 @@ bool Networking::handle_tcp(Common_Message *msg, struct TCP_Socket &socket) struct Connection *Networking::find_connection(CSteamID search_id, uint32 appid) { - if (appid) { - auto conn = std::find_if(connections.begin(), connections.end(), [&search_id, &appid](struct Connection const& conn) { - if (conn.appid != appid) return false; + auto conn = std::find_if(connections.begin(), connections.end(), [&search_id, appid](struct Connection const& conn) { + if (appid && (conn.appid != appid)) return false; - for (auto &id: conn.ids) { - if (search_id == id) { - return true; - } - } - - return false; - }); - - if (connections.end() != conn) - return &(*conn); - } - - auto conn = std::find_if(connections.begin(), connections.end(), [&search_id](struct Connection const& conn) { - for (auto &id: conn.ids) { - if (search_id == id) { - return true; - } + for (const auto &id: conn.ids) { + if (search_id == id) return true; } return false; @@ -642,7 +625,7 @@ struct Connection *Networking::find_connection(CSteamID search_id, uint32 appid) if (connections.end() != conn) return &(*conn); - return NULL; + return nullptr; } bool Networking::add_id_connection(struct Connection *connection, CSteamID steam_id) @@ -653,6 +636,7 @@ bool Networking::add_id_connection(struct Connection *connection, CSteamID steam if (id != connection->ids.end()) return false; + PRINT_DEBUG("Networking::add_id_connection ADDED ID %llu\n", (uint64)steam_id.ConvertToUint64()); connection->ids.push_back(steam_id); if (connection->connected) { run_callback_user(steam_id, true, connection->appid); @@ -671,6 +655,7 @@ struct Connection *Networking::new_connection(CSteamID search_id, uint32 appid) connection.appid = appid; connection.last_received = std::chrono::high_resolution_clock::now(); + PRINT_DEBUG("Networking::new_connection ADDED ID %llu\n", (uint64)search_id.ConvertToUint64()); connections.push_back(connection); return &(connections[connections.size() - 1]); } @@ -681,7 +666,10 @@ bool Networking::handle_announce(Common_Message *msg, IP_PORT ip_port) if (!conn || conn->appid != msg->announce().appid()) { conn = new_connection((uint64)msg->source_id(), msg->announce().appid()); if (!conn) return false; - PRINT_DEBUG("New Connection Created\n"); + PRINT_DEBUG( + "Networking::handle_announce new connection created: user %llu, appid %lu\n", + (uint64)msg->source_id(), msg->announce().appid() + ); } PRINT_DEBUG("Handle Announce: %u, " "%" PRIu64 ", %u, %u\n", conn->appid, msg->source_id(), msg->announce().appid(), msg->announce().type()); @@ -860,6 +848,7 @@ Networking::Networking(CSteamID id, uint32 appid, uint16 port, std::set enabled = true; } + PRINT_DEBUG("Networking::Networking ADDED ID %llu\n", (uint64)id.ConvertToUint64()); ids.push_back(id); reset_last_error(); @@ -1172,7 +1161,7 @@ void Networking::addListenId(CSteamID id) return; } - PRINT_DEBUG("ADDED ID\n"); + PRINT_DEBUG("Networking::addListenId ADDED ID %llu\n", (uint64)id.ConvertToUint64()); ids.push_back(id); send_announce_broadcasts(); return; @@ -1243,10 +1232,9 @@ bool Networking::sendTo(Common_Message *msg, bool reliable, Connection *conn) ret = true; } } else { - char *buffer = new char[size]; - msg->SerializeToArray(buffer, size); - send_packet_to(udp_socket, conn->udp_ip_port, buffer, size); - delete[] buffer; + std::vector buffer(size, 0); + msg->SerializeToArray(&buffer[0], size); + send_packet_to(udp_socket, conn->udp_ip_port, &buffer[0], size); ret = true; } } @@ -1299,7 +1287,7 @@ void Networking::run_callback_user(CSteamID steam_id, bool online, uint32 appid) //only give callbacks for right game accounts if (steam_id.BIndividualAccount() && appid != this->appid && appid != LOBBY_CONNECT_APPID) return; - Common_Message msg; + Common_Message msg{}; msg.set_source_id(steam_id.ConvertToUint64()); msg.set_allocated_low_level(new Low_Level()); if (online) { diff --git a/dll/settings.cpp b/dll/settings.cpp index 91f73c9f..aeea66a4 100644 --- a/dll/settings.cpp +++ b/dll/settings.cpp @@ -79,11 +79,13 @@ Settings::Settings(CSteamID steam_id, CGameID game_id, const std::string &name, this->offline = offline; } +// user id CSteamID Settings::get_local_steam_id() { return steam_id; } +// game id CGameID Settings::get_local_game_id() { return game_id; @@ -278,21 +280,21 @@ std::string Settings::getAppInstallPath(AppId_t appID) return app_paths[appID]; } -void Settings::setLeaderboard(std::string leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type) +void Settings::setLeaderboard(const std::string &leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type) { - Leaderboard_config leader; + Leaderboard_config leader{}; leader.sort_method = sort_method; leader.display_type = display_type; leaderboards[leaderboard] = leader; } -std::map Settings::getLeaderboards() +const std::map& Settings::getLeaderboards() const { return leaderboards; } -const std::map& Settings::getStats() +const std::map& Settings::getStats() const { return stats; } diff --git a/dll/settings_parser.cpp b/dll/settings_parser.cpp index 7ea402ff..e5563132 100644 --- a/dll/settings_parser.cpp +++ b/dll/settings_parser.cpp @@ -599,7 +599,7 @@ static void parse_leaderboards(class Settings *settings_client, Settings *settin line.pop_back(); } - std::string leaderboard; + std::string leaderboard{}; unsigned int sort_method = 0; unsigned int display_type = 0; @@ -618,7 +618,7 @@ static void parse_leaderboards(class Settings *settings_client, Settings *settin settings_client->setLeaderboard(leaderboard, (ELeaderboardSortMethod)sort_method, (ELeaderboardDisplayType)display_type); settings_server->setLeaderboard(leaderboard, (ELeaderboardSortMethod)sort_method, (ELeaderboardDisplayType)display_type); } else { - PRINT_DEBUG("Error adding leaderboard for: %s, are sort method %u or display type %u valid?\n", leaderboard.c_str(), sort_method, display_type); + PRINT_DEBUG("Error adding leaderboard for: '%s', are sort method %u or display type %u valid?\n", leaderboard.c_str(), sort_method, display_type); } } } diff --git a/dll/steam_gameserverstats.cpp b/dll/steam_gameserverstats.cpp index 3585d292..3922dc3b 100644 --- a/dll/steam_gameserverstats.cpp +++ b/dll/steam_gameserverstats.cpp @@ -25,16 +25,16 @@ void Steam_GameServerStats::steam_gameserverstats_network_callback(void *object, { // PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_network_callback\n"); - auto steam_gameserverstats = (Steam_GameServerStats *)object; - steam_gameserverstats->network_callback(msg); + auto inst = (Steam_GameServerStats *)object; + inst->network_callback(msg); } void Steam_GameServerStats::steam_gameserverstats_run_every_runcb(void *object) { // PRINT_DEBUG("Steam_GameServerStats::steam_gameserverstats_run_every_runcb\n"); - auto steam_gameserverstats = (Steam_GameServerStats *)object; - steam_gameserverstats->steam_run_callback(); + auto inst = (Steam_GameServerStats *)object; + inst->steam_run_callback(); } Steam_GameServerStats::CachedStat* Steam_GameServerStats::find_stat(CSteamID steamIDUser, const std::string &key) @@ -46,11 +46,7 @@ Steam_GameServerStats::CachedStat* Steam_GameServerStats::find_stat(CSteamID ste it_data->second.stats.begin(), it_data->second.stats.end(), [&key](std::pair &item) { const std::string &name = item.first; - return key.size() == name.size() && - std::equal( - name.begin(), name.end(), key.begin(), - [](char a, char b) { return std::tolower(a) == std::tolower(b); } - ); + return common_helpers::str_cmp_insensitive(key, name); } ); @@ -68,11 +64,7 @@ Steam_GameServerStats::CachedAchievement* Steam_GameServerStats::find_ach(CSteam it_data->second.achievements.begin(), it_data->second.achievements.end(), [&key](std::pair &item) { const std::string &name = item.first; - return key.size() == name.size() && - std::equal( - name.begin(), name.end(), key.begin(), - [](char a, char b) { return std::tolower(a) == std::tolower(b); } - ); + return common_helpers::str_cmp_insensitive(key, name); } ); diff --git a/helpers/common_helpers.cpp b/helpers/common_helpers.cpp index 5d0140e5..1a66bf23 100644 --- a/helpers/common_helpers.cpp +++ b/helpers/common_helpers.cpp @@ -3,6 +3,7 @@ #include #include #include +#include static bool create_dir_impl(std::filesystem::path &dirpath) { @@ -142,9 +143,18 @@ std::string common_helpers::uint8_vector_to_hex_string(const std::vector& v); +bool str_cmp_insensitive(const std::string &str1, const std::string &str2); + std::string ascii_to_lowercase(std::string data); void thisThreadYieldFor(std::chrono::microseconds u);