From 18040fe4abf5853b73ae4eaa763feedbe366186a 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 --- CHANGELOG.md | 3 ++ .../autoloads/snapshots_client.gd | 9 +++++ .../GodotAndroidPlugin.kt | 9 +++++ .../snapshots/SnapshotsProxy.kt | 36 ++++++++++++++++++- 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5969def..33299ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ When calling the `loadGame` method with a non existing file name, the app crashe ### Add new parameter to loadGame method Added the `createIfNotFound` parameter to the `loadGame` method, with a default value of `false` to not break backwards compatibility. This parameter creates a new snapshot if the file name does not exist. +### Add method to delete snapshots +Added new method to delete snapshots by snapshot id. + ## v1.5.0 ### Order of autoloads The autoloads where causing errors on first launch of the project, due to the load order and dependencies between them. The load order has now been fixed to avoid this errors. Also, the plugin is now disabled by default in the demo project. Look at the [demo project documentation](https://github.com/Iakobs/godot-play-game-services/tree/main/plugin/demo) for further info. 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