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 f52a37c4..1e233970 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 8cd018a9..c1ccfd44 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::lock_guard 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; if (serversUrl) { 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 8ef61907..dec6d310 100644 --- a/thcrap_update/src/file.cpp +++ b/thcrap_update/src/file.cpp @@ -5,15 +5,17 @@ #include "server.h" #include "file.h" +using HttpStatusOf = HttpStatus::Status; + 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 unexpected(urls.empty()) { - throw new std::invalid_argument("Input URL list must not be empty"); + if (urls.empty()) { + throw std::invalid_argument("Input URL list must not be empty"); } } @@ -60,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() == HttpStatusOf::ServerError || status.get() == HttpStatusOf::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. @@ -103,10 +105,10 @@ extern "C" int download_single_file(const char* url, const char* fn) { return 0; } else { - return res.second.get(); + return (int)res.second.get(); } } - catch (std::bad_alloc e) { - return HttpStatus::SystemError; + catch (const std::bad_alloc& e) { + return (int)HttpStatusOf::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 f0b7c18f..31b30604 100644 --- a/thcrap_update/src/http_status.cpp +++ b/thcrap_update/src/http_status.cpp @@ -12,12 +12,12 @@ HttpStatus::~HttpStatus() HttpStatus HttpStatus::makeOk() { - return HttpStatus(Ok, 0, "success"s); + return HttpStatus(Status::Ok, 0, "success"); } HttpStatus HttpStatus::makeCancelled() { - return HttpStatus(Cancelled, 0, "cancelled"s); + return HttpStatus(Status::Cancelled, 0, "cancelled"); } static const std::map messages = { @@ -46,12 +46,12 @@ static const std::map messages = { HttpStatus HttpStatus::makeNetworkError(unsigned int httpCode) { - HttpStatus::Status status; + Status status; if (300 <= httpCode && httpCode < 499) { - status = ClientError; + status = Status::ClientError; } else { - status = ServerError; + status = Status::ServerError; } @@ -62,17 +62,17 @@ HttpStatus HttpStatus::makeNetworkError(unsigned int httpCode) HttpStatus HttpStatus::makeSystemError(unsigned int systemCode, const std::string& text) { - return HttpStatus(SystemError, systemCode, text); + return HttpStatus(Status::SystemError, systemCode, text); } -HttpStatus::Status HttpStatus::get() const +Status HttpStatus::get() const { return this->status; } 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 cbee9af6..8010a3d2 100644 --- a/thcrap_update/src/http_status.h +++ b/thcrap_update/src/http_status.h @@ -8,7 +8,7 @@ using namespace std::literals::string_literals; 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 42949094..e21a09b5 100644 --- a/thcrap_update/src/loader_update.cpp +++ b/thcrap_update/src/loader_update.cpp @@ -1007,9 +1007,9 @@ BOOL loader_update_with_UI(const char *exe_fn, char *args) } } - UPDATE_STATE.event_wrapper_request_update = CreateEventW(NULL, false, false, NULL); - UPDATE_STATE.event_update_finished = CreateEventW(NULL, false, false, NULL); - UPDATE_STATE.event_update_wrapper_terminate = CreateEventW(NULL, false, false, NULL); + UPDATE_STATE.event_wrapper_request_update = CreateEventW(nullptr, false, false, nullptr); + UPDATE_STATE.event_update_finished = CreateEventW(nullptr, false, false, nullptr); + UPDATE_STATE.event_update_wrapper_terminate = CreateEventW(nullptr, false, false, nullptr); UPDATE_STATE.exe_fn = exe_fn; UPDATE_STATE.args = args; UPDATE_STATE.state = STATE_SELF; @@ -1030,14 +1030,14 @@ BOOL loader_update_with_UI(const char *exe_fn, char *args) if (WINDOW_STATE.enabled()) { log_print("Creating UI thread and main window... "); // Reusing the request update event to signal for window creation - WINDOW_STATE.event_request_update = CreateEventW(NULL, FALSE, FALSE, NULL); - WINDOW_STATE.hThread = CreateThread(NULL, 0, loader_update_window_create_and_run, NULL, 0, NULL); + WINDOW_STATE.event_request_update = CreateEventW(nullptr, false, false, nullptr); + WINDOW_STATE.hThread = CreateThread(nullptr, 0, loader_update_window_create_and_run, nullptr, 0, nullptr); WaitForSingleObject(WINDOW_STATE.event_request_update, INFINITE); log_set_hook(log_callback, log_ncallback); log_print("done.\n"); } - HANDLE hWrapperUpdateThread = CreateThread(NULL, 0, update_wrapper_patch, NULL, 0, NULL); + HANDLE hWrapperUpdateThread = CreateThread(nullptr, 0, update_wrapper_patch, nullptr, 0, nullptr); if (UPDATE_STATE.update_at_exit) { // We didn't enable the "start game" button yet, the game can't be running. UPDATE_STATE.game_started = true; @@ -1054,7 +1054,7 @@ BOOL loader_update_with_UI(const char *exe_fn, char *args) HANDLE hMutex = CreateMutexW(nullptr, false, (L"thcrap loader process list mutex "sv + pid_wstr).c_str()); HANDLE hProcess1; - ret = thcrap_inject_into_new(exe_fn, args, &hProcess1, NULL); + ret = thcrap_inject_into_new(exe_fn, args, &hProcess1, nullptr); log_printf("Waiting until %s is finished... ", exe_fn); WaitForSingleObject(hProcess1, INFINITE); CloseHandle(hProcess1); diff --git a/thcrap_update/src/self.cpp b/thcrap_update/src/self.cpp index f7da4f3e..6c4890e6 100644 --- a/thcrap_update/src/self.cpp +++ b/thcrap_update/src/self.cpp @@ -80,7 +80,7 @@ LRESULT CALLBACK smartdlg_proc( std::lock_guard lock(state->progress_mutex); if (state->total_size > 0) { int pos = (int)((state->current_progress * 100) / state->total_size); - SendMessageW(state->hProgress, PBM_SETPOS, pos, 0); + SendMessage(state->hProgress, PBM_SETPOS, pos, 0); } } break; diff --git a/thcrap_update/src/server.cpp b/thcrap_update/src/server.cpp index 200c97d0..f0928dc7 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 e5bf5af9..20778278 100644 --- a/thcrap_update/src/update.cpp +++ b/thcrap_update/src/update.cpp @@ -7,7 +7,9 @@ #include "strings_array.h" #include "3rdparty/crc32.h" -Update::Update(Update::filter_t filterCallback, +using HttpStatusOf = HttpStatus::Status; + +Update::Update(Update::FilterCallback filterCallback, progress_callback_t progressCallback, void *progressData) : filterCallback(filterCallback), progressCallback(progressCallback), progressData(progressData) { @@ -17,19 +19,18 @@ Update::Update(Update::filter_t filterCallback, get_status_t Update::httpStatusToGetStatus(HttpStatus status) { switch (status.get()) { - case HttpStatus::Ok: + case HttpStatusOf::Ok: return GET_OK; - case HttpStatus::Cancelled: + case HttpStatusOf::Cancelled: return GET_CANCELLED; - case HttpStatus::ClientError: + case HttpStatusOf::ClientError: return GET_CLIENT_ERROR; - case HttpStatus::ServerError: + case HttpStatusOf::ServerError: return GET_SERVER_ERROR; - case HttpStatus::SystemError: + case HttpStatusOf::SystemError: return GET_SYSTEM_ERROR; - default: - throw std::invalid_argument("Invalid status"); } + throw std::invalid_argument("Invalid status"); } bool Update::callProgressCallback(const patch_t *patch, const std::string& fn, const DownloadUrl& url, get_status_t getStatus, std::string error, @@ -236,7 +237,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() == HttpStatusOf::Cancelled) { // Another file finished before return ; } diff --git a/thcrap_update/src/update.h b/thcrap_update/src/update.h index 11fc228a..82b31f7a 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); }; /// ---------------------------------------