-
-
Notifications
You must be signed in to change notification settings - Fork 261
feat: sync offline achievements on reconnect #1268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xXJSONDeruloXx
wants to merge
8
commits into
utkarshdalal:master
Choose a base branch
from
xXJSONDeruloXx:feat/offline-achievement-reconnect-sync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c115753
feat: sync offline achievements on reconnect
xXJSONDeruloXx dc87d87
fix: cancel stale reconnect achievement sync
xXJSONDeruloXx 0b133fa
Merge remote-tracking branch 'upstream/master' into feat/offline-achi…
xXJSONDeruloXx e553b21
refactor: scope reconnect achievement sweep to offline-closed apps
xXJSONDeruloXx 040eae7
refactor: persist pending achievement sync via flat file
xXJSONDeruloXx 7a6059d
feat: record pending sync on offline achievement earn and clear on su…
xXJSONDeruloXx d3dd146
fix: atomic file writes and defer persistence wipe to logout
xXJSONDeruloXx bdeeaa4
Merge remote-tracking branch 'upstream/master' into feat/offline-achi…
xXJSONDeruloXx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -261,6 +261,10 @@ class SteamService : Service(), IChallengeUrlChanged { | |
|
|
||
| private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) | ||
| private var reconnectJob: Job? = null | ||
| private var offlineAchievementSyncJob: Job? = null | ||
| private val pendingSyncAppIds: MutableSet<Int> = java.util.concurrent.ConcurrentHashMap.newKeySet() | ||
| private val pendingSyncFileLock = Any() | ||
| private val pendingSyncFile by lazy { File(applicationContext.filesDir, "pending_achievement_sync.txt") } | ||
|
|
||
| private val onEndProcess: (AndroidEvent.EndProcess) -> Unit = { | ||
| Companion.stop() | ||
|
|
@@ -2289,6 +2293,7 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| suspend fun closeApp(context: Context, appId: Int, isOffline: Boolean, prefixToPath: (String) -> String) = withContext(Dispatchers.IO) { | ||
| async { | ||
| if (isOffline || !isConnected) { | ||
| instance?.addPendingSyncApp(appId) | ||
| return@async | ||
| } | ||
|
|
||
|
|
@@ -2342,6 +2347,7 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| } | ||
| } finally { | ||
| releaseSync(appId) | ||
| instance?.removePendingSyncApp(appId) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2631,7 +2637,7 @@ class SteamService : Service(), IChallengeUrlChanged { | |
|
|
||
| private fun clearUserData(clearCloudSyncState: Boolean = false) { | ||
| PrefManager.clearSteamSessionPreferences() | ||
|
|
||
| instance?.clearPendingSync() | ||
| clearDatabase(clearCloudSyncState = clearCloudSyncState) | ||
| } | ||
|
|
||
|
|
@@ -3060,6 +3066,13 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| super.onCreate() | ||
| instance = this | ||
|
|
||
| // Restore any app IDs that were pending achievement sync before the service was killed | ||
| pendingSyncAppIds.addAll( | ||
| runCatching { | ||
| pendingSyncFile.readLines().mapNotNull { it.trim().toIntOrNull() } | ||
| }.getOrDefault(emptyList()) | ||
| ) | ||
|
|
||
| // JavaSteam logger CME hot-fix | ||
| runCatching { | ||
| val clazz = Class.forName("in.dragonbra.javasteam.util.log.LogManager") | ||
|
|
@@ -3313,6 +3326,9 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| _unifiedFriends = null | ||
|
|
||
| reconnectJob?.cancel() | ||
| offlineAchievementSyncJob?.cancel() | ||
| offlineAchievementSyncJob = null | ||
| pendingSyncAppIds.clear() | ||
| isStopping = false | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| retryAttempt = 0 | ||
|
|
||
|
|
@@ -3362,6 +3378,8 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| Timber.i("Disconnected from Steam. User initiated: ${callback.isUserInitiated}") | ||
|
|
||
| isConnected = false | ||
| offlineAchievementSyncJob?.cancel() | ||
| offlineAchievementSyncJob = null | ||
|
|
||
| if (!isStopping && retryAttempt < MAX_RETRY_ATTEMPTS) { | ||
| retryAttempt++ | ||
|
|
@@ -3458,6 +3476,8 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| scope.launch { | ||
| resumePendingWorkshopDownloads() | ||
| } | ||
|
|
||
| syncPendingOfflineAchievements() | ||
| } | ||
|
|
||
| else -> { | ||
|
|
@@ -3510,6 +3530,101 @@ class SteamService : Service(), IChallengeUrlChanged { | |
| } | ||
| } | ||
|
|
||
| internal fun addPendingSyncApp(appId: Int) { | ||
| synchronized(pendingSyncFileLock) { | ||
| pendingSyncAppIds.add(appId) | ||
| runCatching { pendingSyncFile.writeText(pendingSyncAppIds.joinToString("\n")) } | ||
| } | ||
| Timber.tag("achievements").d("Recording appId=$appId for offline achievement sync on reconnect") | ||
| } | ||
|
|
||
| internal fun removePendingSyncApp(appId: Int) { | ||
| synchronized(pendingSyncFileLock) { | ||
| pendingSyncAppIds.remove(appId) | ||
| runCatching { | ||
| if (pendingSyncAppIds.isEmpty()) pendingSyncFile.delete() | ||
| else pendingSyncFile.writeText(pendingSyncAppIds.joinToString("\n")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal fun clearPendingSync() { | ||
| synchronized(pendingSyncFileLock) { | ||
| pendingSyncAppIds.clear() | ||
| runCatching { pendingSyncFile.delete() } | ||
| } | ||
| } | ||
|
|
||
| private fun syncPendingOfflineAchievements() { | ||
| offlineAchievementSyncJob?.cancel() | ||
| offlineAchievementSyncJob = scope.launch { | ||
| try { | ||
| delay(2_000) | ||
|
|
||
| if (!isConnected || !isLoggedIn) { | ||
| Timber.tag("achievements").d("Skipping reconnect achievement sync sweep — Steam no longer connected") | ||
| return@launch | ||
| } | ||
|
|
||
| val appsToSync = pendingSyncAppIds.toSet() | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| if (appsToSync.isEmpty()) { | ||
| Timber.tag("achievements").d("Skipping reconnect achievement sync sweep — no apps were closed while offline") | ||
| return@launch | ||
| } | ||
|
|
||
| Timber.tag("achievements").i("Syncing offline achievements for ${appsToSync.size} app(s) closed while disconnected") | ||
| for (appId in appsToSync) { | ||
| ensureActive() | ||
|
|
||
| if (!isConnected || !isLoggedIn) { | ||
| Timber.tag("achievements").d("Stopping reconnect achievement sync sweep — Steam no longer connected") | ||
| return@launch | ||
| } | ||
|
|
||
| val gseSaveDirs = getGseSaveDirs(applicationContext, appId).filter { it.isDirectory } | ||
| if (gseSaveDirs.isEmpty()) { | ||
| removePendingSyncApp(appId) | ||
| continue | ||
| } | ||
|
|
||
| val hasOfflineAchievementData = gseSaveDirs.any { dir -> | ||
| File(dir, "achievements.json").exists() || | ||
| (File(dir, "stats").isDirectory && (File(dir, "stats").listFiles()?.isNotEmpty() == true)) | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| } | ||
| if (!hasOfflineAchievementData) { | ||
| removePendingSyncApp(appId) | ||
| continue | ||
| } | ||
|
|
||
| if (!tryAcquireSync(appId)) { | ||
| Timber.tag("achievements").d("Skipping reconnect achievement sync for appId=$appId — sync already in progress") | ||
| continue | ||
| } | ||
|
|
||
| try { | ||
| Timber.tag("achievements").i("Attempting reconnect achievement sync for appId=$appId") | ||
| syncAchievementsFromGoldberg(applicationContext, appId) | ||
| removePendingSyncApp(appId) | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
|
Comment on lines
+3604
to
+3610
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a double-catch? Or is this due to typing difficulties?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coroutine cancelation requires cancelation exception, ik it looks weird |
||
| Timber.tag("achievements").e(e, "Reconnect achievement sync failed for appId=$appId") | ||
| } finally { | ||
| releaseSync(appId) | ||
| } | ||
| } | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
| Timber.tag("achievements").e(e, "Reconnect achievement sync sweep failed") | ||
|
xXJSONDeruloXx marked this conversation as resolved.
|
||
| } finally { | ||
| if (offlineAchievementSyncJob?.isActive != true) { | ||
| offlineAchievementSyncJob = null | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private fun onLoggedOff(callback: LoggedOffCallback) { | ||
| Timber.i("Logged off of Steam: ${callback.result}") | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Persisted pending achievement sync state is global and not reliably cleared on service teardown, allowing stale app IDs to be replayed in a later session.
Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a task queue that's the idea