From 286aab0cf19381dcfdebf0742896b38b7856858b Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 20 Jul 2025 16:23:59 +0300 Subject: [PATCH] chore: Got rid of fake Github error messages when attempting to download mod preview images --- core/src/com/unciv/logic/github/Github.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/com/unciv/logic/github/Github.kt b/core/src/com/unciv/logic/github/Github.kt index 247b92a391..757c0db983 100644 --- a/core/src/com/unciv/logic/github/Github.kt +++ b/core/src/com/unciv/logic/github/Github.kt @@ -49,11 +49,12 @@ object Github { /** * Helper opens an url and accesses its input stream, logging errors to the console * @param url String representing a [URI] to download. + * @param shouldLogError Some downloads are just us guessing where an image could be, an exception is acceptable in these circumstances * @param preDownloadAction Optional callback that will be executed between opening the connection and * accessing its data - passes the [connection][HttpURLConnection] and allows e.g. reading the response headers. * @return The [InputStream] if successful, `null` otherwise. */ - fun download(url: String, preDownloadAction: (HttpURLConnection) -> Unit = {}): InputStream? { + fun download(url: String, shouldLogError: Boolean = true, preDownloadAction: (HttpURLConnection) -> Unit = {}): InputStream? { try { // Problem type 1 - opening the URL connection // URL(string) is deprecated, URI.toUrl(string) API level 36, see [Android Doc](https://developer.android.com/reference/java/net/URI#toURL()): @@ -75,7 +76,7 @@ object Github { } catch (_: RedirectionException) { return null } catch (ex: Exception) { - Log.error("Exception during GitHub download", ex) + if (shouldLogError) Log.error("Exception during GitHub download", ex) return null } } @@ -374,12 +375,12 @@ object Github { // Thus we ask for a "preview" file as part of the repo contents instead. val fileLocation = GithubAPI.getUrlForPreview(modUrl, defaultBranch) try { - val file = download("$fileLocation.jpg") - ?: download("$fileLocation.png") + val file = download("$fileLocation.jpg", shouldLogError = false) + ?: download("$fileLocation.png", shouldLogError = false) // Note: avatar urls look like: https://avatars.githubusercontent.com/u/?v=4 // So the image format is only recognizable from the response "Content-Type" header // or by looking for magic markers in the bits - which the Pixmap constructor below does. - ?: avatarUrl?.let { download(it) } + ?: avatarUrl?.let { download(it, shouldLogError = false) } ?: return null val byteArray = file.readBytes() val buffer = ByteBuffer.allocateDirect(byteArray.size).put(byteArray).position(0)