diff --git a/submodules/mh_stuff b/submodules/mh_stuff index bf53d519..f3f6b989 160000 --- a/submodules/mh_stuff +++ b/submodules/mh_stuff @@ -1 +1 @@ -Subproject commit bf53d519f5a12bda5f30ebb7848e2c15b891667a +Subproject commit f3f6b9890cd177a0c33d2d034a3d91e78c09cf60 diff --git a/tf2_bot_detector/Config/ChatWrappers.cpp b/tf2_bot_detector/Config/ChatWrappers.cpp index dcea6ae0..4beb99da 100644 --- a/tf2_bot_detector/Config/ChatWrappers.cpp +++ b/tf2_bot_detector/Config/ChatWrappers.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -19,6 +20,9 @@ #include #include +#undef min +#undef max + using namespace std::string_literals; using namespace std::string_view_literals; using namespace tf2_bot_detector; @@ -411,12 +415,8 @@ static void PrintChatWrappers(const ChatWrappers& wrappers) mh::strwrapperstream os(str); os << std::quoted(wrapper.m_Narrow) << " ("; - char buf[16]; for (auto c : wrapper.m_Wide) - { - sprintf_s(buf, "%04X", +c); - os << "\\x" << buf; - } + os << mh::pfstr<64>("\\x%04X", +c); os << ')'; }; diff --git a/tf2_bot_detector/Config/DRPInfo.cpp b/tf2_bot_detector/Config/DRPInfo.cpp index 5b9e5bf0..f84ab498 100644 --- a/tf2_bot_detector/Config/DRPInfo.cpp +++ b/tf2_bot_detector/Config/DRPInfo.cpp @@ -3,6 +3,7 @@ #include "RegexHelpers.h" #include +#include #include #include #include @@ -126,9 +127,8 @@ std::string DRPInfo::Map::GetFriendlyName() const bool DRPInfo::Map::Matches(const std::string_view& mapName) const { - char buf[512]; - sprintf_s(buf, "%s%s", m_MapNames.at(0).c_str(), R"regex(?(?:_(?!.*_)(?:(?:rc)|(?:final)|(?:[abv]))\d*[a-zA-Z]?)?)regex"); - const std::regex s_MainRegex(buf, std::regex::icase); + mh::fmtstr<512> buf("{}{}", m_MapNames.at(0).c_str(), R"regex(?(?:_(?!.*_)(?:(?:rc)|(?:final)|(?:[abv]))\d*[a-zA-Z]?)?)regex"); + const std::regex s_MainRegex(buf.c_str(), std::regex::icase); if (std::regex_match(mapName.begin(), mapName.end(), s_MainRegex)) return true; diff --git a/tf2_bot_detector/ConsoleLog/ConsoleLines.cpp b/tf2_bot_detector/ConsoleLog/ConsoleLines.cpp index 5ca9dcf2..4d2d044b 100644 --- a/tf2_bot_detector/ConsoleLog/ConsoleLines.cpp +++ b/tf2_bot_detector/ConsoleLog/ConsoleLines.cpp @@ -7,6 +7,7 @@ #include "WorldState.h" #include +#include #include #include #include @@ -142,9 +143,8 @@ static void ProcessChatMessage(const ChatConsoleLine& msgLine, const IConsoleLin if (newlineEnd > nonNewlineEnd) { - char buf[64]; - sprintf_s(buf, "(\\n x %zu)", (newlineEnd - nonNewlineEnd)); - textFunc(ImVec4(1, 0.5f, 0.5f, 1.0f), buf); + textFunc(ImVec4(1, 0.5f, 0.5f, 1.0f), + mh::fmtstr<64>("(\\n x {})", (newlineEnd - nonNewlineEnd)).c_str()); sameLineFunc(); } diff --git a/tf2_bot_detector/MainWindow.cpp b/tf2_bot_detector/MainWindow.cpp index e6445a25..01a6b2f7 100644 --- a/tf2_bot_detector/MainWindow.cpp +++ b/tf2_bot_detector/MainWindow.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -918,19 +919,17 @@ void MainWindow::OnDrawServerStats() ImGui::SameLine(0, 4); auto& lastSample = m_EdictUsageSamples.back(); - char buf[32]; const float percent = float(lastSample.m_UsedEdicts) / lastSample.m_MaxEdicts; - sprintf_s(buf, "%i (%1.0f%%)", lastSample.m_UsedEdicts, percent * 100); - ImGui::ProgressBar(percent, { -1, 0 }, buf); + ImGui::ProgressBar(percent, { -1, 0 }, + mh::pfstr<64>("%i (%1.0f%%)", lastSample.m_UsedEdicts, percent * 100).c_str()); ImGui::SetHoverTooltip("%i of %i (%1.1f%%)", lastSample.m_UsedEdicts, lastSample.m_MaxEdicts, percent * 100); } if (!m_ServerPingSamples.empty()) { - char buf[64]; - sprintf_s(buf, "Average ping: %u", m_ServerPingSamples.back().m_Ping); - ImGui::PlotLines(buf, [&](int idx) + ImGui::PlotLines(mh::fmtstr<64>("Average ping: {}", m_ServerPingSamples.back().m_Ping).c_str(), + [&](int idx) { return m_ServerPingSamples[idx].m_Ping; }, (int)m_ServerPingSamples.size(), 0, nullptr, 0); @@ -1015,9 +1014,7 @@ void MainWindow::OnDraw() if (parseProgress < 0.95f) { - char overlayStr[64]; - sprintf_s(overlayStr, "%1.2f %%", parseProgress * 100); - ImGui::ProgressBar(parseProgress, { 0, 0 }, overlayStr); + ImGui::ProgressBar(parseProgress, { 0, 0 }, mh::pfstr<64>("%1.2f %%", parseProgress * 100).c_str()); ImGui::SameLine(0, 4); } @@ -1112,9 +1109,8 @@ void MainWindow::OnDrawMenuBar() ImGui::Separator(); - char buf[128]; - sprintf_s(buf, "Version: %s", VERSION_STRING); - ImGui::MenuItem(buf, nullptr, false, false); + static const mh::fmtstr<128> VERSION_STRING_LABEL("Version: {}", VERSION_STRING); + ImGui::MenuItem(VERSION_STRING_LABEL.c_str(), nullptr, false, false); if (m_Settings.m_AllowInternetUsage.value_or(false)) { diff --git a/tf2_bot_detector/ModeratorLogic.cpp b/tf2_bot_detector/ModeratorLogic.cpp index e20a3a47..6ddd70f1 100644 --- a/tf2_bot_detector/ModeratorLogic.cpp +++ b/tf2_bot_detector/ModeratorLogic.cpp @@ -8,6 +8,7 @@ #include "WorldState.h" #include +#include #include #include @@ -248,8 +249,8 @@ void ModeratorLogic::HandleConnectedEnemyCheaters(const std::vector& en if (chatMsgCheaterNames.size() > 0) { - constexpr char FMT_ONE_CHEATER[] = "Attention! There is a cheater on the other team named %s. Please kick them!"; - constexpr char FMT_MULTIPLE_CHEATERS[] = "Attention! There are %u cheaters on the other team named %s. Please kick them!"; + constexpr char FMT_ONE_CHEATER[] = "Attention! There is a cheater on the other team named \"{}\". Please kick them!"; + constexpr char FMT_MULTIPLE_CHEATERS[] = "Attention! There are {} cheaters on the other team named {}. Please kick them!"; //constexpr char FMT_MANY_CHEATERS[] = "Attention! There are %u cheaters on the other team including %s. Please kick them!"; constexpr size_t MAX_CHATMSG_LENGTH = 127; constexpr size_t MAX_NAMES_LENGTH_ONE = MAX_CHATMSG_LENGTH - std::size(FMT_ONE_CHEATER) - 1 - 2; @@ -257,11 +258,10 @@ void ModeratorLogic::HandleConnectedEnemyCheaters(const std::vector& en //constexpr size_t MAX_NAMES_LENGTH_MANY = MAX_CHATMSG_LENGTH - std::size(FMT_MANY_CHEATERS) - 1 - 1 - 2; static_assert(MAX_NAMES_LENGTH_ONE >= 32); - std::string chatMsg; + mh::fmtstr chatMsg; if (chatMsgCheaterNames.size() == 1) { - chatMsg.resize(256); - chatMsg.resize(sprintf_s(chatMsg.data(), 256, FMT_ONE_CHEATER, chatMsgCheaterNames.front().c_str())); + chatMsg.fmt(FMT_ONE_CHEATER, chatMsgCheaterNames.front()); } else { @@ -276,8 +276,7 @@ void ModeratorLogic::HandleConnectedEnemyCheaters(const std::vector& en break; } - chatMsg.resize(256); - chatMsg.resize(sprintf_s(chatMsg.data(), 256, FMT_MULTIPLE_CHEATERS, chatMsgCheaterNames.size(), cheaters.c_str())); + chatMsg.fmt(FMT_MULTIPLE_CHEATERS, chatMsgCheaterNames.size(), cheaters); } assert(chatMsg.size() <= 127); @@ -352,14 +351,14 @@ void ModeratorLogic::HandleConnectingEnemyCheaters(const std::vector& c if (!needsWarning || !m_Settings->m_AutoChatWarnings || !m_Settings->m_AutoChatWarningsConnecting) return; - char chatMsg[128]; + mh::fmtstr<128> chatMsg; if (connectingEnemyCheaters.size() == 1) { - strcpy_s(chatMsg, "Heads up! There is a known cheater joining the other team! Name unknown until they fully join."); + chatMsg.puts("Heads up! There is a known cheater joining the other team! Name unknown until they fully join."); } else { - sprintf_s(chatMsg, "Heads up! There are %zu known cheaters joining the other team! Names unknown until they fully join.", + chatMsg.fmt("Heads up! There are {} known cheaters joining the other team! Names unknown until they fully join.", connectingEnemyCheaters.size()); } diff --git a/tf2_bot_detector/TextUtils.cpp b/tf2_bot_detector/TextUtils.cpp index cbb43f1e..2fe88b97 100644 --- a/tf2_bot_detector/TextUtils.cpp +++ b/tf2_bot_detector/TextUtils.cpp @@ -4,6 +4,7 @@ #include "TextUtils.h" #include "Log.h" +#include #include #include @@ -131,12 +132,10 @@ std::string tf2_bot_detector::CollapseNewlines(const std::string_view& input) const auto smallGroupMsgLength = newlineCount * (std::size("\\n") - 1); - char buf[64]; - const auto largeGroupMsgLength = sprintf_s(buf, "(\\n x %zu)", newlineCount); - - if (smallGroupMsgLength >= largeGroupMsgLength) + const mh::fmtstr<64> newlineGroupStr("(\\n x %zu)", newlineCount); + if (smallGroupMsgLength >= newlineGroupStr.size()) { - retVal.append(buf); + retVal.append(newlineGroupStr); } else {