Skip to content

Commit

Permalink
Small refactor of snapshot code
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 Dec 27, 2023
1 parent 3916792 commit 330fff2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ class GodotAndroidPlugin(godot: Godot) : GodotPlugin(godot) {
*
* @param fileName The name of the save file. Must be between 1 and 100 non-URL-reserved
* characters (a-z, A-Z, 0-9, or the symbols "-", ".", "_", or "~").
* @param saveData A String with the saved data of the game.
* @param description The description of the save file.
* @param saveData A ByteArray with the saved data of the game.
*/
@UsedByGodot
fun saveGame(
fileName: String,
saveData: String,
description: String
) = snapshotsProxy.saveGame(fileName, saveData, description)
description: String,
saveData: ByteArray
) = snapshotsProxy.saveGame(fileName, description, saveData)

/**
* Loads game data from the Google Cloud. This method emits the [com.jacobibanez.plugin.android.godotplaygameservices.signals.SnapshotSignals.gameLoaded]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.google.android.gms.games.SnapshotsClient
import com.google.android.gms.games.SnapshotsClient.EXTRA_SNAPSHOT_METADATA
import com.google.android.gms.games.SnapshotsClient.RESOLUTION_POLICY_HIGHEST_PROGRESS
import com.google.android.gms.games.SnapshotsClient.RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED
import com.google.android.gms.games.SnapshotsClient.SnapshotConflict
import com.google.android.gms.games.snapshot.SnapshotMetadata
import com.google.android.gms.games.snapshot.SnapshotMetadataChange
import com.jacobibanez.plugin.android.godotplaygameservices.BuildConfig.GODOT_PLUGIN_NAME
Expand Down Expand Up @@ -55,27 +56,16 @@ class SnapshotsProxy(
}
}

fun saveGame(fileName: String, saveData: String, description: String) {
fun saveGame(fileName: String, description: String, saveData: ByteArray) {
Log.d(tag, "Saving game data with name $fileName and description ${description}.")
snapshotsClient.open(fileName, true, RESOLUTION_POLICY_HIGHEST_PROGRESS)
.addOnSuccessListener { dataOrConflict ->
if (dataOrConflict.isConflict) {
Log.e(
tag,
"Conflict during saving of data with name $fileName and description ${description}."
)
emitSignal(
godot,
GODOT_PLUGIN_NAME,
gameSaved,
false,
fileName,
description
)
handleConflict(dataOrConflict.conflict)
return@addOnSuccessListener
}
dataOrConflict.data?.let { snapshot ->
snapshot.snapshotContents.writeBytes(saveData.toByteArray())
snapshot.snapshotContents.writeBytes(saveData)
val metadata = SnapshotMetadataChange.Builder()
.setDescription(description)
.build()
Expand All @@ -94,21 +84,40 @@ class SnapshotsProxy(
}

fun loadGame(fileName: String) {
Log.d(tag, "Loading game.")
Log.d(tag, "Loading snapshot with name $fileName.")
snapshotsClient.open(fileName, false, RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED)
.addOnFailureListener { exception ->
Log.e(tag, "Error while opening Snapshot.", exception);
}.continueWith { dataOrConflictTask ->
dataOrConflictTask.result.data?.let { snapshot ->
Log.e(tag, "Error while opening Snapshot $fileName for loading.", exception);
}.continueWith { task ->
val dataOrConflict = task.result
if (dataOrConflict.isConflict) {
handleConflict(dataOrConflict.conflict)
return@continueWith null
}
dataOrConflict.data?.let { snapshot ->
return@continueWith snapshot
}
}.addOnCompleteListener { task ->
emitSignal(
godot,
GODOT_PLUGIN_NAME,
gameLoaded,
fromSnapshot(godot, task.result)
)
task.result?.let { snapshot ->
emitSignal(
godot,
GODOT_PLUGIN_NAME,
gameLoaded,
fromSnapshot(godot, snapshot)
)
}
}
}

private fun handleConflict(conflict: SnapshotConflict?) {
conflict?.let {
val snapshot = it.snapshot
val fileName = snapshot.metadata.uniqueName
val description = snapshot.metadata.description
Log.e(
tag, "Conflict with id ${conflict.conflictId} during saving of data with " +
"name $fileName and description ${description}."
)
}
}
}

0 comments on commit 330fff2

Please sign in to comment.