From ec336c39dbd154b43fb5f53274e86b0303dba314 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Wed, 17 Jan 2024 18:35:39 -0600 Subject: [PATCH] [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. --- Delta/Database/Model/Human/GameSave.swift | 30 +++++++++++++++++++++++ External/Harmony | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Delta/Database/Model/Human/GameSave.swift b/Delta/Database/Model/Human/GameSave.swift index 034f59c..99ef1d3 100644 --- a/Delta/Database/Model/Human/GameSave.swift +++ b/Delta/Database/Model/Human/GameSave.swift @@ -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 + } } diff --git a/External/Harmony b/External/Harmony index 1f6c00e..e190129 160000 --- a/External/Harmony +++ b/External/Harmony @@ -1 +1 @@ -Subproject commit 1f6c00ea96b6449cc3fee9900f5f397674d79171 +Subproject commit e1901297408ad220aa3c05e4c4fd9660c3420679