From 1c06eb9dcf7288b61f0ff39a6e47f80cc753c9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Ib=C3=A1=C3=B1ez=20S=C3=A1nchez?= Date: Sat, 2 Mar 2024 19:16:23 +0100 Subject: [PATCH] Add delete method to delete snapshots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jacob Ibáñez Sánchez --- .../autoloads/snapshots_client.gd | 9 +++++ .../GodotAndroidPlugin.kt | 9 +++++ .../snapshots/SnapshotsProxy.kt | 36 ++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/plugin/export_scripts_template/autoloads/snapshots_client.gd b/plugin/export_scripts_template/autoloads/snapshots_client.gd index f608b95..9b2446b 100644 --- a/plugin/export_scripts_template/autoloads/snapshots_client.gd +++ b/plugin/export_scripts_template/autoloads/snapshots_client.gd @@ -96,6 +96,8 @@ func load_game(file_name: String, create_if_not_found := false) -> void: ## Loads the list of [SnapshotMetadata] of the current signed in player.[br] ## [br] +## This method emits the [signal snapshots_loaded] signal.[br] +## [br] ## [param force_reload]: If true, this call will clear any locally cached ## data and attempt to fetch the latest data from the server. Send it set to [code]true[/code] ## the first time, and [code]false[/code] in subsequent calls, or when you want @@ -104,6 +106,13 @@ func load_snapshots(force_reload: bool) -> void: if GodotPlayGameServices.android_plugin: GodotPlayGameServices.android_plugin.loadSnapshots(force_reload) +## Deletes a snapshot. This will delete the data of the snapshot locally and on the server.[br] +## [br] +## [param snapshot_id]: The snapshot identifier. +func delete_snapshot(snapshot_id: String) -> void: + if GodotPlayGameServices.android_plugin: + GodotPlayGameServices.android_plugin.deleteSnapshot(snapshot_id) + ## A snapshot. class Snapshot: var content: PackedByteArray ## A [PackedByteArray] with the contents of the snapshot. diff --git a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt index 4d79248..a947c88 100644 --- a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt +++ b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt @@ -439,4 +439,13 @@ class GodotAndroidPlugin(godot: Godot) : GodotPlugin(godot) { */ @UsedByGodot fun loadSnapshots(forceReload: Boolean) = snapshotsProxy.loadSnapshots(forceReload) + + /** + * Deletes the given snapshot. This will delete the data of the snapshot locally and on the server. + * + * @param snapshotId The snapshot identifier. + */ + @UsedByGodot + fun deleteSnapshot(snapshotId: String) = + snapshotsProxy.deleteSnapshot(snapshotId) } diff --git a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt index 55f8035..1cf19ce 100644 --- a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt +++ b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt @@ -112,7 +112,10 @@ class SnapshotsProxy( ) } } else { - Log.e(tag, "Error while opening Snapshot $fileName for loading. Cause: ${task.exception}") + Log.e( + tag, + "Error while opening Snapshot $fileName for loading. Cause: ${task.exception}" + ) } } } @@ -152,6 +155,37 @@ class SnapshotsProxy( } } + fun deleteSnapshot(snapshotId: String) { + var isDeleted = false + snapshotsClient.load(true).addOnSuccessListener { annotatedData -> + annotatedData.get()?.let { buffer -> + buffer + .toList() + .firstOrNull { it.snapshotId == snapshotId }?.let { snapshotMetadata -> + Log.d(tag, "Deleting snapshot with id $snapshotId") + snapshotsClient.delete(snapshotMetadata).addOnCompleteListener { task -> + if (task.isSuccessful) { + Log.d( + tag, + "Snapshot with id $snapshotId deleted successfully." + ) + isDeleted = true + } else { + Log.e( + tag, + "Failed to delete snapshot with id $snapshotId. Cause: ${task.exception}", + task.exception + ) + } + } + } + } + } + if (!isDeleted) { + Log.d(tag, "Snapshot with id $snapshotId not found!") + } + } + private fun handleConflict(conflict: SnapshotConflict?) { conflict?.let { val snapshot = it.snapshot