[Delta Sync] Fixes incorrectly marking some GameSaves as conflicted during initial sync

Fixes an edge case where updating from Delta 1.4.x to Delta 1.5 could result in false-conflicts if user had previously updated to Delta 1.5 in the past (including betas). This is because GameSaves updated on 1.5 have a new `sha1` field, which changes the hash for the whole record.
This commit is contained in:
Riley Testut 2024-01-17 18:35:39 -06:00
parent 1d37a1dae8
commit ec336c39db
2 changed files with 31 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import Foundation
import GBCDeltaCore
import Harmony
import Roxas
@objc(GameSave)
public class GameSave: _GameSave
@ -113,4 +114,33 @@ extension GameSave: Syncable
}
}
}
public func resolveConflict(_ record: AnyRecord) -> ConflictResolution
{
// Only attempt to resolve conflicts for older GameSaves without SHA1 hash (i.e. pre-Delta 1.5)
guard let game = self.game, self.sha1 == nil else { return .conflict }
do
{
let sha1Hash = try RSTHasher.sha1HashOfFile(at: game.gameSaveURL)
// resolveConflict() is called from self.managedObjectContext, so we can update `self` directly
// and it will be automatically saved once finished conflicting records.
self.sha1 = sha1Hash
// Don't update localRecord's hash here or else GameSave won't be repaired during initial sync.
// try localRecord.updateSHA1Hash()
}
catch CocoaError.fileNoSuchFile
{
// Ignore
}
catch
{
Logger.sync.error("Failed to update GameSave SHA1 hash when resolving conflict. \(error.localizedDescription, privacy: .public)")
}
// Conflict for now, but we'll "repair" this record to hopefully resolve conflict.
return .conflict
}
}

2
External/Harmony vendored

@ -1 +1 @@
Subproject commit 1f6c00ea96b6449cc3fee9900f5f397674d79171
Subproject commit e1901297408ad220aa3c05e4c4fd9660c3420679