Skip to content

Commit

Permalink
Add delete method to delete snapshots
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Ibáñez Sánchez <jacobibanez@jacobibanez.com>
  • Loading branch information
Iakobs committed Mar 2, 2024
1 parent 10634bc commit 1c06eb9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
9 changes: 9 additions & 0 deletions plugin/export_scripts_template/autoloads/snapshots_client.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
}
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1c06eb9

Please sign in to comment.