From 8f917a2590df83f3f83bcbc84db291aed78e9edd Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 13 Aug 2019 18:32:32 +0100 Subject: [PATCH 1/2] Show the object download source when downloading object files --- data/language/en-GB.txt | 1 + src/openrct2-ui/windows/ObjectLoadError.cpp | 85 +++++++++++++++++---- src/openrct2/localisation/StringIds.h | 1 + 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/data/language/en-GB.txt b/data/language/en-GB.txt index 29e247ed8c..f11b6cd3a0 100644 --- a/data/language/en-GB.txt +++ b/data/language/en-GB.txt @@ -3777,6 +3777,7 @@ STR_6326 :Can't simulate {POP16}{POP16}{POP16}{STRINGID}... STR_6327 :Transparent background for giant screenshots STR_6328 :{SMALLFONT}{BLACK}With this option enabled, giant screenshots will have a transparent background instead of the default black colour. STR_6329 :{STRING}{STRINGID} +STR_6330 :Downloading [{STRING}] from {STRING} ({COMMA16} / {COMMA16}) ############# # Scenarios # diff --git a/src/openrct2-ui/windows/ObjectLoadError.cpp b/src/openrct2-ui/windows/ObjectLoadError.cpp index 7649e2ddf7..415f456c41 100644 --- a/src/openrct2-ui/windows/ObjectLoadError.cpp +++ b/src/openrct2-ui/windows/ObjectLoadError.cpp @@ -30,6 +30,23 @@ class ObjectDownloader private: static constexpr auto OPENRCT2_API_LEGACY_OBJECT_URL = "https://api.openrct2.io/objects/legacy/"; + struct DownloadStatusInfo + { + std::string Name; + std::string Source; + size_t Count{}; + size_t Total{}; + + bool operator==(const DownloadStatusInfo& rhs) + { + return Name == rhs.Name && Source == rhs.Source && Count == rhs.Count && Total == rhs.Total; + } + bool operator!=(const DownloadStatusInfo& rhs) + { + return !(*this == rhs); + } + }; + std::vector _entries; std::vector _downloadedEntries; size_t _currentDownloadIndex{}; @@ -37,12 +54,20 @@ private: std::mutex _queueMutex; bool _nextDownloadQueued{}; + DownloadStatusInfo _lastDownloadStatusInfo; + DownloadStatusInfo _downloadStatusInfo; + std::mutex _downloadStatusInfoMutex; + std::string _lastDownloadSource; + // TODO static due to INTENT_EXTRA_CALLBACK not allowing a std::function inline static bool _downloadingObjects; public: void Begin(const std::vector& entries) { + _lastDownloadStatusInfo = {}; + _downloadStatusInfo = {}; + _lastDownloadSource = {}; _entries = entries; _currentDownloadIndex = 0; _downloadingObjects = true; @@ -68,23 +93,54 @@ public: _nextDownloadQueued = false; NextDownload(); } + UpdateStatusBox(); } private: - void UpdateProgress(const std::string& name, size_t count, size_t total) + void UpdateStatusBox() { - char str_downloading_objects[256]{}; - uint8_t args[32]{}; - set_format_arg_on(args, 0, int16_t, count); - set_format_arg_on(args, 2, int16_t, total); - set_format_arg_on(args, 4, char*, name.c_str()); + std::lock_guard guard(_downloadStatusInfoMutex); + if (_lastDownloadStatusInfo != _downloadStatusInfo) + { + _lastDownloadStatusInfo = _downloadStatusInfo; - format_string(str_downloading_objects, sizeof(str_downloading_objects), STR_DOWNLOADING_OBJECTS, args); + if (_downloadStatusInfo == DownloadStatusInfo()) + { + context_force_close_window_by_class(WC_NETWORK_STATUS); + } + else + { + char str_downloading_objects[256]{}; + uint8_t args[32]{}; + if (_downloadStatusInfo.Source.empty()) + { + set_format_arg_on(args, 0, int16_t, (int16_t)_downloadStatusInfo.Count); + set_format_arg_on(args, 2, int16_t, (int16_t)_downloadStatusInfo.Total); + set_format_arg_on(args, 4, char*, _downloadStatusInfo.Name.c_str()); + format_string(str_downloading_objects, sizeof(str_downloading_objects), STR_DOWNLOADING_OBJECTS, args); + } + else + { + set_format_arg_on(args, 0, char*, _downloadStatusInfo.Name.c_str()); + set_format_arg_on(args, sizeof(char*), char*, _downloadStatusInfo.Source.c_str()); + set_format_arg_on(args, sizeof(char*) + sizeof(char*), int16_t, (int16_t)_downloadStatusInfo.Count); + set_format_arg_on( + args, sizeof(char*) + sizeof(char*) + sizeof(int16_t), int16_t, (int16_t)_downloadStatusInfo.Total); + format_string(str_downloading_objects, sizeof(str_downloading_objects), STR_DOWNLOADING_OBJECTS_FROM, args); + } - auto intent = Intent(WC_NETWORK_STATUS); - intent.putExtra(INTENT_EXTRA_MESSAGE, std::string(str_downloading_objects)); - intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { _downloadingObjects = false; }); - context_open_intent(&intent); + auto intent = Intent(WC_NETWORK_STATUS); + intent.putExtra(INTENT_EXTRA_MESSAGE, std::string(str_downloading_objects)); + intent.putExtra(INTENT_EXTRA_CALLBACK, []() -> void { _downloadingObjects = false; }); + context_open_intent(&intent); + } + } + } + + void UpdateProgress(const DownloadStatusInfo& info) + { + std::lock_guard guard(_downloadStatusInfoMutex); + _downloadStatusInfo = info; } void QueueNextDownload() @@ -139,15 +195,15 @@ private: { // Finished... _downloadingObjects = false; - context_force_close_window_by_class(WC_NETWORK_STATUS); + UpdateProgress({}); return; } auto& entry = _entries[_currentDownloadIndex]; auto name = String::Trim(std::string(entry.name, sizeof(entry.name))); - UpdateProgress(name, _currentDownloadIndex + 1, (int32_t)_entries.size()); std::printf("Downloading %s...\n", name.c_str()); _currentDownloadIndex++; + UpdateProgress({ name, _lastDownloadSource, _currentDownloadIndex, _entries.size() }); try { Http::Request req; @@ -160,9 +216,12 @@ private: if (jresponse != nullptr) { auto objName = json_string_value(json_object_get(jresponse, "name")); + auto source = json_string_value(json_object_get(jresponse, "source")); auto downloadLink = json_string_value(json_object_get(jresponse, "download")); if (downloadLink != nullptr) { + _lastDownloadSource = source; + UpdateProgress({ name, source, _currentDownloadIndex, _entries.size() }); DownloadObject(entry, objName, downloadLink); } json_decref(jresponse); diff --git a/src/openrct2/localisation/StringIds.h b/src/openrct2/localisation/StringIds.h index 06a74a877f..0cbaca5234 100644 --- a/src/openrct2/localisation/StringIds.h +++ b/src/openrct2/localisation/StringIds.h @@ -3966,6 +3966,7 @@ enum STR_TRANSPARENT_SCREENSHOT_TIP = 6328, STR_STRING_STRINGID = 6329, + STR_DOWNLOADING_OBJECTS_FROM = 6330, // Have to include resource strings (from scenarios and objects) for the time being now that language is partially working STR_COUNT = 32768 From 634b25444751876a818846caf33231d9eab6fec7 Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 13 Aug 2019 22:38:21 +0100 Subject: [PATCH 2/2] Print the URL of the object that it is downloading --- src/openrct2-ui/windows/ObjectLoadError.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/openrct2-ui/windows/ObjectLoadError.cpp b/src/openrct2-ui/windows/ObjectLoadError.cpp index 415f456c41..b3575523ce 100644 --- a/src/openrct2-ui/windows/ObjectLoadError.cpp +++ b/src/openrct2-ui/windows/ObjectLoadError.cpp @@ -149,11 +149,12 @@ private: _nextDownloadQueued = true; } - void DownloadObject(const rct_object_entry& entry, const std::string name, const std::string_view url) + void DownloadObject(const rct_object_entry& entry, const std::string name, const std::string url) { using namespace OpenRCT2::Network; try { + std::printf("Downloading %s\n", url.c_str()); Http::Request req; req.method = Http::Method::GET; req.url = url; @@ -201,7 +202,7 @@ private: auto& entry = _entries[_currentDownloadIndex]; auto name = String::Trim(std::string(entry.name, sizeof(entry.name))); - std::printf("Downloading %s...\n", name.c_str()); + log_verbose("Downloading object: [%s]:", name.c_str()); _currentDownloadIndex++; UpdateProgress({ name, _lastDownloadSource, _currentDownloadIndex, _entries.size() }); try