From 5cf9b0dd4c5e7b9f934014d7f86a054e17330193 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sun, 6 Feb 2022 11:11:17 +0000 Subject: [PATCH] Fix MSVC warnings The size_t to int conversion is unavoidable, but at least it's now explicit. --- src/api/game/game.cpp | 8 ++-- src/api/game/load_order_handler.cpp | 63 +++++++++++++---------------- src/api/helpers/text.cpp | 32 +++++++++++---- 3 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/api/game/game.cpp b/src/api/game/game.cpp index d6433344..8b566bf8 100644 --- a/src/api/game/game.cpp +++ b/src/api/game/game.cpp @@ -25,12 +25,11 @@ #include "api/game/game.h" #include +#include #include #include #include -#include - #include "api/api_database.h" #include "api/helpers/logging.h" #include "api/sorting/plugin_sort.h" @@ -128,9 +127,9 @@ void Game::LoadPlugins(const std::vector& plugins, threadsToUse = ::std::max(threadsToUse, (size_t)1); // Divide the plugins up by thread. - unsigned int pluginsPerThread = ceil((double)sizeMap.size() / threadsToUse); vector> pluginGroups(threadsToUse); if (logger) { + auto pluginsPerThread = sizeMap.size() / threadsToUse; logger->info( "Loading {} plugins using {} threads, with up to {} plugins per " "thread.", @@ -224,7 +223,8 @@ std::vector Game::SortPlugins( void Game::LoadCurrentLoadOrderState() { loadOrderHandler_->LoadCurrentState(); - conditionEvaluator_->RefreshActivePluginsState(loadOrderHandler_->GetActivePlugins()); + conditionEvaluator_->RefreshActivePluginsState( + loadOrderHandler_->GetActivePlugins()); } bool Game::IsPluginActive(const std::string& pluginName) const { diff --git a/src/api/game/load_order_handler.cpp b/src/api/game/load_order_handler.cpp index 9927138d..3bf0a1b8 100644 --- a/src/api/game/load_order_handler.cpp +++ b/src/api/game/load_order_handler.cpp @@ -32,26 +32,26 @@ using std::string; namespace loot { unsigned int mapGameId(GameType gameType) { switch (gameType) { - case GameType::tes3: - return LIBLO_GAME_TES3; - case GameType::tes4: - return LIBLO_GAME_TES4; - case GameType::tes5: - return LIBLO_GAME_TES5; - case GameType::tes5se: - return LIBLO_GAME_TES5SE; - case GameType::tes5vr: - return LIBLO_GAME_TES5VR; - case GameType::fo3: - return LIBLO_GAME_FO3; - case GameType::fonv: - return LIBLO_GAME_FNV; - case GameType::fo4: - return LIBLO_GAME_FO4; - case GameType::fo4vr: - return LIBLO_GAME_FO4VR; - default: - return (unsigned int)-1; + case GameType::tes3: + return LIBLO_GAME_TES3; + case GameType::tes4: + return LIBLO_GAME_TES4; + case GameType::tes5: + return LIBLO_GAME_TES5; + case GameType::tes5se: + return LIBLO_GAME_TES5SE; + case GameType::tes5vr: + return LIBLO_GAME_TES5VR; + case GameType::fo3: + return LIBLO_GAME_FO3; + case GameType::fonv: + return LIBLO_GAME_FNV; + case GameType::fo4: + return LIBLO_GAME_FO4; + case GameType::fo4vr: + return LIBLO_GAME_FO4VR; + default: + return (unsigned int)-1; } } @@ -77,8 +77,10 @@ void LoadOrderHandler::Init(const GameType& gameType, gh_ = nullptr; } - int ret = lo_create_handle( - &gh_, mapGameId(gameType), gamePath.u8string().c_str(), gameLocalDataPath); + int ret = lo_create_handle(&gh_, + mapGameId(gameType), + gamePath.u8string().c_str(), + gameLocalDataPath); HandleError("create a game handle", ret); } @@ -136,8 +138,7 @@ std::vector LoadOrderHandler::GetActivePlugins() const { char** pluginArr; size_t pluginArrSize; - unsigned int ret = - lo_get_active_plugins(gh_, &pluginArr, &pluginArrSize); + unsigned int ret = lo_get_active_plugins(gh_, &pluginArr, &pluginArrSize); HandleError("get active plugins", ret); @@ -174,23 +175,17 @@ void LoadOrderHandler::SetLoadOrder( logger->info("Setting load order."); } - size_t pluginArrSize = loadOrder.size(); - char** pluginArr = new char*[pluginArrSize]; - int i = 0; + std::vector plugins; + plugins.reserve(loadOrder.size()); for (const auto& plugin : loadOrder) { if (logger) { logger->info("\t\t{}", plugin); } - pluginArr[i] = new char[plugin.length() + 1]; - strcpy(pluginArr[i], plugin.c_str()); - ++i; + plugins.push_back(plugin.c_str()); } - unsigned int ret = lo_set_load_order(gh_, pluginArr, pluginArrSize); - - for (size_t i = 0; i < pluginArrSize; i++) delete[] pluginArr[i]; - delete[] pluginArr; + unsigned int ret = lo_set_load_order(gh_, plugins.data(), plugins.size()); HandleError("set the load order", ret); diff --git a/src/api/helpers/text.cpp b/src/api/helpers/text.cpp index 5228b0f3..a76800eb 100644 --- a/src/api/helpers/text.cpp +++ b/src/api/helpers/text.cpp @@ -126,28 +126,46 @@ std::optional ExtractVersion(const std::string& text) { #ifdef _WIN32 std::wstring ToWinWide(const std::string& str) { - size_t len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), 0, 0); + size_t len = MultiByteToWideChar( + CP_UTF8, 0, str.c_str(), static_cast(str.length()), 0, 0); if (len == 0) { return std::wstring(); } std::wstring wstr(len, 0); - MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &wstr[0], len); + MultiByteToWideChar(CP_UTF8, + 0, + str.c_str(), + static_cast(str.length()), + &wstr[0], + static_cast(len)); return wstr; } std::string FromWinWide(const std::wstring& wstr) { - size_t len = WideCharToMultiByte( - CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL); + size_t len = WideCharToMultiByte(CP_UTF8, + 0, + wstr.c_str(), + static_cast(wstr.length()), + NULL, + 0, + NULL, + NULL); if (len == 0) { return std::string(); } std::string str(len, 0); - WideCharToMultiByte( - CP_UTF8, 0, wstr.c_str(), wstr.length(), &str[0], len, NULL, NULL); + WideCharToMultiByte(CP_UTF8, + 0, + wstr.c_str(), + static_cast(wstr.length()), + &str[0], + static_cast(len), + NULL, + NULL); return str; } #endif @@ -185,7 +203,7 @@ std::string NormalizeFilename(const std::string& filename) { return std::string(); } - CharUpperBuffW(&wideString[0], wideString.length()); + CharUpperBuffW(&wideString[0], static_cast(wideString.length())); return FromWinWide(wideString); #else std::string normalizedFilename;