* common function to do case insensitive compare

* some const + ref in different places
* some logs, comments & refactoring
This commit is contained in:
otavepto 2024-03-30 07:55:56 +02:00 committed by otavepto
parent 574e7a7781
commit f423b07eef
10 changed files with 53 additions and 62 deletions

View File

@ -133,7 +133,7 @@ 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

View File

@ -315,11 +315,11 @@ public:
std::set<PublishedFileId_t> modSet();
//leaderboards
void setLeaderboard(std::string leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type);
std::map<std::string, Leaderboard_config> getLeaderboards();
void setLeaderboard(const std::string &leaderboard, enum ELeaderboardSortMethod sort_method, enum ELeaderboardDisplayType display_type);
const std::map<std::string, Leaderboard_config>& getLeaderboards() const;
//stats
const std::map<std::string, Stat_config>& getStats();
const std::map<std::string, Stat_config>& getStats() const;
void setStatDefiniton(const std::string &name, const struct Stat_config &stat_config);
//images

View File

@ -100,12 +100,9 @@ google::protobuf::Map<std::string,std::string>::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;
}

View File

@ -216,7 +216,7 @@ message Steam_Messages {
}
message GameServerStats_Messages {
// --- baisc definitions
// --- basic definitions
message StatInfo {
enum Stat_Type {
STAT_TYPE_INT = 0;

View File

@ -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<IP_PORT>
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<char> 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) {

View File

@ -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<std::string, Leaderboard_config> Settings::getLeaderboards()
const std::map<std::string, Leaderboard_config>& Settings::getLeaderboards() const
{
return leaderboards;
}
const std::map<std::string, Stat_config>& Settings::getStats()
const std::map<std::string, Stat_config>& Settings::getStats() const
{
return stats;
}

View File

@ -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);
}
}
}

View File

@ -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<const std::string, CachedStat> &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<const std::string, CachedAchievement> &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);
}
);

View File

@ -3,6 +3,7 @@
#include <cwchar>
#include <algorithm>
#include <thread>
#include <cctype>
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<uint8_t
return result;
}
bool common_helpers::str_cmp_insensitive(const std::string &str1, const std::string &str2)
{
if (str1.size() != str2.size()) return false;
return std::equal(str1.begin(), str1.end(), str2.begin(), [](const char c1, const char c2){
return std::toupper(c1) == std::toupper(c2);
});
}
std::string common_helpers::ascii_to_lowercase(std::string data) {
std::transform(data.begin(), data.end(), data.begin(),
[](unsigned char c){ return std::tolower(c); });
[](char c){ return std::tolower(c); });
return data;
}

View File

@ -31,6 +31,8 @@ std::string string_strip(const std::string& str);
std::string uint8_vector_to_hex_string(const std::vector<uint8_t>& 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);