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 18040fe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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 18040fe

Please sign in to comment.