From 76d3259fa5c0ca42f308164d4ca32405e6fdb67e Mon Sep 17 00:00:00 2001 From: Erfa Date: Tue, 2 Jan 2024 15:42:17 +0100 Subject: [PATCH 1/2] Ensure all task exceptions are handled, to prevent crashes. --- .../com/abedalkareem/games_services/Achievements.kt | 7 ++++--- .../com/abedalkareem/games_services/Leaderboards.kt | 7 ++++--- .../com/abedalkareem/games_services/SaveGame.kt | 12 ++++++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Achievements.kt b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Achievements.kt index 6497b97..d70adec 100644 --- a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Achievements.kt +++ b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Achievements.kt @@ -66,15 +66,16 @@ class Achievements(private var activityPluginBinding: ActivityPluginBinding) { activity ?: return achievementClient .load(true) - .addOnCompleteListener { task -> - val data = task.result.get() + .addOnSuccessListener { annotatedData -> + val data = annotatedData.get() + if (data == null) { result.error( PluginError.FailedToLoadAchievements.errorCode(), PluginError.FailedToLoadAchievements.errorMessage(), null ) - return@addOnCompleteListener + return@addOnSuccessListener } val handler = CoroutineExceptionHandler { _, exception -> result.error( diff --git a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt index 7888c21..6012dfc 100644 --- a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt +++ b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt @@ -56,15 +56,16 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) { ) { activity ?: return leaderboardsClient.loadTopScores(leaderboardID, span, leaderboardCollection, maxResults) - .addOnCompleteListener { task -> - val data = task.result.get() + .addOnSuccessListener { annotatedData -> + val data = annotatedData.get() + if (data == null) { result.error( PluginError.FailedToLoadLeaderboardScores.errorCode(), PluginError.FailedToLoadLeaderboardScores.errorMessage(), null ) - return@addOnCompleteListener + return@addOnSuccessListener } val handler = CoroutineExceptionHandler { _, exception -> result.error( diff --git a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/SaveGame.kt b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/SaveGame.kt index dd1e267..24ca035 100644 --- a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/SaveGame.kt +++ b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/SaveGame.kt @@ -55,8 +55,9 @@ class SaveGame(private var activityPluginBinding: ActivityPluginBinding) { .setDescription(desc) .build() snapshotsClient.open(name, true, SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED) - .addOnCompleteListener { task -> - val snapshot = task.result.data + .addOnSuccessListener { annotatedData -> + val snapshot = annotatedData.data + if (snapshot != null) { // Set the data payload for the snapshot snapshot.snapshotContents.writeBytes(data.toByteArray()) @@ -77,6 +78,13 @@ class SaveGame(private var activityPluginBinding: ActivityPluginBinding) { ) } } + .addOnFailureListener { + result.error( + PluginError.FailedToSaveGame.errorCode(), + it.localizedMessage, + null + ) + } } fun deleteGame(name: String, result: MethodChannel.Result) { From 143d7e2f0765fad7654275016c0f16edfa5e7998 Mon Sep 17 00:00:00 2001 From: Erfa Date: Sun, 28 Jan 2024 11:26:49 +0100 Subject: [PATCH 2/2] Revert Leaderboards.kt to allow for a proper fix. --- .../kotlin/com/abedalkareem/games_services/Leaderboards.kt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt index 6012dfc..7888c21 100644 --- a/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt +++ b/games_services/android/src/main/kotlin/com/abedalkareem/games_services/Leaderboards.kt @@ -56,16 +56,15 @@ class Leaderboards(private var activityPluginBinding: ActivityPluginBinding) { ) { activity ?: return leaderboardsClient.loadTopScores(leaderboardID, span, leaderboardCollection, maxResults) - .addOnSuccessListener { annotatedData -> - val data = annotatedData.get() - + .addOnCompleteListener { task -> + val data = task.result.get() if (data == null) { result.error( PluginError.FailedToLoadLeaderboardScores.errorCode(), PluginError.FailedToLoadLeaderboardScores.errorMessage(), null ) - return@addOnSuccessListener + return@addOnCompleteListener } val handler = CoroutineExceptionHandler { _, exception -> result.error(