diff --git a/thcrap/src/jansson_ex.cpp b/thcrap/src/jansson_ex.cpp index d7ac9219..12f1120c 100644 --- a/thcrap/src/jansson_ex.cpp +++ b/thcrap/src/jansson_ex.cpp @@ -364,7 +364,7 @@ json_t *json5_loadb(const void *buffer, size_t size, char **error) json5pp::impl::imemstream istream(buffer, size); json5 = json5pp::parse5(istream, false); } - catch (json5pp::syntax_error e) { + catch (const json5pp::syntax_error& e) { if (error) { *error = strdup(e.what()); } diff --git a/thcrap/src/repo.cpp b/thcrap/src/repo.cpp index ea55b63f..dc505875 100644 --- a/thcrap/src/repo.cpp +++ b/thcrap/src/repo.cpp @@ -104,7 +104,7 @@ bool RepoWrite(const repo_t *repo) try { std::filesystem::create_directories(repo_dir); } - catch (std::filesystem::filesystem_error e) { + catch (const std::filesystem::filesystem_error& e) { log_printf("Failed to create repo folder %s.\nError %d: %s\n", (const char*)repo_dir.u8string().c_str(), e.code().value(), e.what()); return false; } diff --git a/thcrap/src/search.cpp b/thcrap/src/search.cpp index 8c11683c..7461dec1 100644 --- a/thcrap/src/search.cpp +++ b/thcrap/src/search.cpp @@ -190,9 +190,9 @@ static DWORD WINAPI SearchThread(void *param_) if (ent->path().extension() == L".exe") SearchCheckExe(*state, *ent); } - catch (std::system_error &) {} + catch (const std::system_error&) {} } - } catch (std::system_error &) {} + } catch (const std::system_error&) {} return 0; } diff --git a/thcrap_update/src/downloader.cpp b/thcrap_update/src/downloader.cpp index f9fe3a06..872eaf87 100644 --- a/thcrap_update/src/downloader.cpp +++ b/thcrap_update/src/downloader.cpp @@ -22,7 +22,9 @@ std::list Downloader::serversListToDownloadUrlList(const std::list< } void Downloader::addFile(const std::list& serversUrl, std::string filePath, - File::success_t successCallback, File::failure_t failureCallback, File::progress_t progressCallback) + File::SuccessCallback successCallback, + File::FailureCallback failureCallback, + File::ProgressCallback progressCallback) { std::scoped_lock lock(this->mutex); @@ -41,7 +43,9 @@ void Downloader::addFile(const std::list& serversUrl, std::string f } void Downloader::addFile(char** serversUrl, std::string filePath, - File::success_t successCallback, File::failure_t failureCallback, File::progress_t progressCallback) + File::SuccessCallback successCallback, + File::FailureCallback failureCallback, + File::ProgressCallback progressCallback) { std::list serversList; diff --git a/thcrap_update/src/downloader.h b/thcrap_update/src/downloader.h index fd440a29..34939d83 100644 --- a/thcrap_update/src/downloader.h +++ b/thcrap_update/src/downloader.h @@ -22,13 +22,13 @@ class Downloader Downloader(); ~Downloader(); void addFile(const std::list& servers, std::string filename, - File::success_t successCallback = File::defaultSuccessFunction, - File::failure_t failureCallback = File::defaultFailureFunction, - File::progress_t progressCallback = File::defaultProgressFunction); + File::SuccessCallback successCallback = File::defaultSuccessFunction, + File::FailureCallback failureCallback = File::defaultFailureFunction, + File::ProgressCallback progressCallback = File::defaultProgressFunction); void addFile(char** servers, std::string filename, - File::success_t successCallback = File::defaultSuccessFunction, - File::failure_t failureCallback = File::defaultFailureFunction, - File::progress_t progressCallback = File::defaultProgressFunction); + File::SuccessCallback successCallback = File::defaultSuccessFunction, + File::FailureCallback failureCallback = File::defaultFailureFunction, + File::ProgressCallback progressCallback = File::defaultProgressFunction); size_t current() const; size_t total() const; void wait(); diff --git a/thcrap_update/src/file.cpp b/thcrap_update/src/file.cpp index c7c7b2dc..c69d6018 100644 --- a/thcrap_update/src/file.cpp +++ b/thcrap_update/src/file.cpp @@ -8,14 +8,14 @@ using namespace std::string_literals; File::File(std::list&& urls, - success_t successCallback, - failure_t failureCallback, - progress_t progressCallback) + SuccessCallback successCallback, + FailureCallback failureCallback, + ProgressCallback progressCallback) : status(Status::Todo), urls(urls), userSuccessCallback(successCallback), userFailureCallback(failureCallback), userProgressCallback(progressCallback) { if (urls.empty()) { - throw new std::invalid_argument("Input URL list must not be empty"); + throw std::invalid_argument("Input URL list must not be empty"); } } @@ -62,7 +62,7 @@ void File::download(IHttpHandle& http, const DownloadUrl& url) } ); if (!status) { - if (status.get() == HttpStatus::ServerError || status.get() == HttpStatus::SystemError) { + if (status.get() == HttpStatus::Status::ServerError || status.get() == HttpStatus::Status::SystemError) { // If the server is dead, we don't want to continue using it. // If the library returned an error, future downloads to the // same server are also likely to fail. @@ -105,10 +105,10 @@ extern "C" int download_single_file(const char* url, const char* fn) { return 0; } else { - return res.second.get(); + return static_cast(res.second.get()); } } - catch (std::bad_alloc e) { - return HttpStatus::SystemError; + catch (const std::bad_alloc& e) { + return static_cast(HttpStatus::Status::SystemError); } } diff --git a/thcrap_update/src/file.h b/thcrap_update/src/file.h index 4226cad4..a10afff1 100644 --- a/thcrap_update/src/file.h +++ b/thcrap_update/src/file.h @@ -11,9 +11,9 @@ class File { public: - typedef std::function& data)> success_t; - typedef std::function failure_t; - typedef std::function progress_t; + using SuccessCallback = std::function& data)>; + using FailureCallback = std::function; + using ProgressCallback = std::function; static void defaultSuccessFunction(const DownloadUrl&, std::vector&) {} static void defaultFailureFunction(const DownloadUrl&, HttpStatus) {} static bool defaultProgressFunction(const DownloadUrl&, size_t, size_t) { return true; } @@ -35,9 +35,9 @@ class File std::list urls; // User-provided callbacks - success_t userSuccessCallback; - failure_t userFailureCallback; - progress_t userProgressCallback; + SuccessCallback userSuccessCallback; + FailureCallback userFailureCallback; + ProgressCallback userProgressCallback; size_t writeCallback(std::vector& buffer, const uint8_t *data, size_t size); bool progressCallback(const DownloadUrl& url, size_t dlnow, size_t dltotal); @@ -46,9 +46,9 @@ class File public: File(std::list&& urls, - success_t successCallback = defaultSuccessFunction, - failure_t failureCallback = defaultFailureFunction, - progress_t progressCallback = defaultProgressFunction); + SuccessCallback successCallback = defaultSuccessFunction, + FailureCallback failureCallback = defaultFailureFunction, + ProgressCallback progressCallback = defaultProgressFunction); File(const File&) = delete; File(File&&) = delete; File& operator=(const File&) = delete; diff --git a/thcrap_update/src/http_status.cpp b/thcrap_update/src/http_status.cpp index dea2134e..48357140 100644 --- a/thcrap_update/src/http_status.cpp +++ b/thcrap_update/src/http_status.cpp @@ -12,22 +12,22 @@ HttpStatus::~HttpStatus() HttpStatus HttpStatus::makeOk() { - return HttpStatus(Ok, 0, "success"); + return HttpStatus(Status::Ok, 0, "success"); } HttpStatus HttpStatus::makeCancelled() { - return HttpStatus(Cancelled, 0, "cancelled"); + return HttpStatus(Status::Cancelled, 0, "cancelled"); } HttpStatus HttpStatus::makeNetworkError(unsigned int httpCode) { HttpStatus::Status status; if (300 <= httpCode && httpCode < 499) { - status = ClientError; + status = Status::ClientError; } else { - status = ServerError; + status = Status::ServerError; } std::map messages = { @@ -67,7 +67,7 @@ HttpStatus HttpStatus::makeNetworkError(unsigned int httpCode) HttpStatus HttpStatus::makeSystemError(unsigned int systemCode, std::string text) { - return HttpStatus(SystemError, systemCode, text); + return HttpStatus(Status::SystemError, systemCode, text); } HttpStatus::Status HttpStatus::get() const @@ -77,7 +77,7 @@ HttpStatus::Status HttpStatus::get() const HttpStatus::operator bool() const { - return this->status == Ok; + return this->status == Status::Ok; } bool HttpStatus::operator==(Status status) const diff --git a/thcrap_update/src/http_status.h b/thcrap_update/src/http_status.h index 436a73d3..e0d1b8a3 100644 --- a/thcrap_update/src/http_status.h +++ b/thcrap_update/src/http_status.h @@ -6,7 +6,7 @@ class HttpStatus { public: - enum Status { + enum class Status { // 200 - success Ok, // Download cancelled by the progress callback, or another client diff --git a/thcrap_update/src/loader_update.cpp b/thcrap_update/src/loader_update.cpp index e2a96feb..8a1be926 100644 --- a/thcrap_update/src/loader_update.cpp +++ b/thcrap_update/src/loader_update.cpp @@ -677,7 +677,7 @@ BOOL loader_update_with_UI(const char *exe_fn, char *args) state.update_at_exit = globalconfig_get_boolean("update_at_exit", false); state.background_updates = globalconfig_get_boolean("background_updates", false); - state.time_between_updates = (int)globalconfig_get_integer("time_between_updates", 5); + state.time_between_updates = static_cast(globalconfig_get_integer("time_between_updates", 5)); state.update_others = globalconfig_get_boolean("update_others", true); SetLastError(0); diff --git a/thcrap_update/src/self.cpp b/thcrap_update/src/self.cpp index 591f5adf..2f15ad2b 100644 --- a/thcrap_update/src/self.cpp +++ b/thcrap_update/src/self.cpp @@ -81,7 +81,7 @@ LRESULT CALLBACK smartdlg_proc( if (state && state->hProgress) { std::lock_guard lock(state->progress_mutex); if (state->total_size > 0) { - int pos = (int)((state->current_progress * 100) / state->total_size); + int pos = static_cast((state->current_progress * 100) / state->total_size); SendMessage(state->hProgress, PBM_SETPOS, pos, 0); } } diff --git a/thcrap_update/src/server.cpp b/thcrap_update/src/server.cpp index fd34e2b5..efbb930e 100644 --- a/thcrap_update/src/server.cpp +++ b/thcrap_update/src/server.cpp @@ -53,7 +53,7 @@ const std::string& Server::getUrl() const return this->baseUrl; } -std::pair, HttpStatus> Server::downloadFile(const std::string& name, File::progress_t progress) +std::pair, HttpStatus> Server::downloadFile(const std::string& name, File::ProgressCallback progress) { std::list urls{ DownloadUrl(*this, name) @@ -75,7 +75,7 @@ std::pair, HttpStatus> Server::downloadFile(const std::stri return std::make_pair(std::move(ret), status); } -std::pair Server::downloadJsonFile(const std::string& name, File::progress_t progress) +std::pair Server::downloadJsonFile(const std::string& name, File::ProgressCallback progress) { auto [data, status] = this->downloadFile(name, progress); if (!status) { @@ -189,13 +189,13 @@ std::pair ServerCache::urlToServer(const std::string& url) } } -std::pair, HttpStatus> ServerCache::downloadFile(const std::string& url, File::progress_t progress) +std::pair, HttpStatus> ServerCache::downloadFile(const std::string& url, File::ProgressCallback progress) { auto [server, path] = this->urlToServer(url); return server.downloadFile(path, progress); } -std::pair ServerCache::downloadJsonFile(const std::string& url, File::progress_t progress) +std::pair ServerCache::downloadJsonFile(const std::string& url, File::ProgressCallback progress) { auto [server, path] = this->urlToServer(url); return server.downloadJsonFile(path, progress); diff --git a/thcrap_update/src/server.h b/thcrap_update/src/server.h index 2fd4f2ee..4f1281bb 100644 --- a/thcrap_update/src/server.h +++ b/thcrap_update/src/server.h @@ -30,7 +30,7 @@ class BorrowedHttpHandle class Server { public: - typedef std::function(void)> HttpHandleFactory; + using HttpHandleFactory = std::function(void)>; private: HttpHandleFactory handleFactory; @@ -54,10 +54,10 @@ class Server // Download a single file from this server. std::pair, HttpStatus> downloadFile( const std::string& name, - File::progress_t progress = File::defaultProgressFunction + File::ProgressCallback progress = File::defaultProgressFunction ); // Download a single json file from this server. - std::pair downloadJsonFile(const std::string& name, File::progress_t progress = File::defaultProgressFunction); + std::pair downloadJsonFile(const std::string& name, File::ProgressCallback progress = File::defaultProgressFunction); // Borrow a HttpHandle from the server. // You own it until the BorrowedHttpHandle is destroyed. @@ -102,8 +102,8 @@ class ServerCache // Find the server for url and call downloadFile on it std::pair, HttpStatus> downloadFile( const std::string& url, - File::progress_t progress = File::defaultProgressFunction + File::ProgressCallback progress = File::defaultProgressFunction ); // Find the server for url and call downloadJsonFile on it - std::pair downloadJsonFile(const std::string& url, File::progress_t progress = File::defaultProgressFunction); + std::pair downloadJsonFile(const std::string& url, File::ProgressCallback progress = File::defaultProgressFunction); }; diff --git a/thcrap_update/src/update.cpp b/thcrap_update/src/update.cpp index 13122870..dab7efe9 100644 --- a/thcrap_update/src/update.cpp +++ b/thcrap_update/src/update.cpp @@ -7,7 +7,7 @@ #include "strings_array.h" #include "3rdparty/crc32.h" -Update::Update(Update::filter_t filterCallback, +Update::Update(Update::FilterCallback filterCallback, progress_callback_t progressCallback, void *progressData) : filterCallback(filterCallback), progressCallback(progressCallback), progressData(progressData) { @@ -17,15 +17,15 @@ Update::Update(Update::filter_t filterCallback, get_status_t Update::httpStatusToGetStatus(HttpStatus status) { switch (status.get()) { - case HttpStatus::Ok: + case HttpStatus::Status::Ok: return GET_OK; - case HttpStatus::Cancelled: + case HttpStatus::Status::Cancelled: return GET_CANCELLED; - case HttpStatus::ClientError: + case HttpStatus::Status::ClientError: return GET_CLIENT_ERROR; - case HttpStatus::ServerError: + case HttpStatus::Status::ServerError: return GET_SERVER_ERROR; - case HttpStatus::SystemError: + case HttpStatus::Status::SystemError: return GET_SYSTEM_ERROR; default: throw std::invalid_argument("Invalid status"); @@ -236,7 +236,7 @@ void Update::startPatchUpdate(const patch_t *patch) this->onFilesJsComplete(patch, data); }, [patch_id = std::string(patch->id)](const DownloadUrl& url, HttpStatus httpStatus) { - if (httpStatus.get() == HttpStatus::Cancelled) { + if (httpStatus.get() == HttpStatus::Status::Cancelled) { // Another file finished before return ; } diff --git a/thcrap_update/src/update.h b/thcrap_update/src/update.h index 035bbf2a..e71d4e15 100644 --- a/thcrap_update/src/update.h +++ b/thcrap_update/src/update.h @@ -96,7 +96,7 @@ void thcrap_update_exit(void); class Update { private: - typedef std::function filter_t; + using FilterCallback = std::function; // Downloader used for the files.js downloads Downloader filesJsDownloader; @@ -106,7 +106,7 @@ class Update // files.js is downloaded. Downloader mainDownloader; - filter_t filterCallback; + FilterCallback filterCallback; progress_callback_t progressCallback; void *progressData; @@ -121,7 +121,7 @@ class Update std::string fnToUrl(const std::string& url, uint32_t crc32); public: - Update(filter_t filterCallback, progress_callback_t progressCallback, void *progressData); + Update(FilterCallback filterCallback, progress_callback_t progressCallback, void *progressData); void run(const std::list& patchs); }; /// ---------------------------------------