Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class DepotDownloader @JvmOverloads constructor(

private val listeners = CopyOnWriteArrayList<IDownloadListener>()

@Volatile
private var currentPhase: DownloadPhase = DownloadPhase.UNKNOWN

private val progressUpdateInterval = 500L // ms

private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob(parentJob))
Expand Down Expand Up @@ -984,6 +987,7 @@ class DepotDownloader @JvmOverloads constructor(
} else {
logger?.debug("Downloading depot ${depot.depotId} manifest")
notifyListeners { it.onStatusUpdate("Downloading manifest for depot ${depot.depotId}") }
transitionPhase(DownloadPhase.PREPARING)

var manifestRequestCode: ULong = 0U
var manifestRequestCodeExpiration = Instant.MIN
Expand Down Expand Up @@ -1270,6 +1274,7 @@ class DepotDownloader @JvmOverloads constructor(
if (!fileDidExist) {
logger?.debug("Pre-allocating: $fileFinalPath")
notifyListeners { it.onStatusUpdate("Allocating file: ${file.fileName}") }
transitionPhase(DownloadPhase.PREPARING)

// create new file. need all chunks
try {
Expand Down Expand Up @@ -1390,6 +1395,7 @@ class DepotDownloader @JvmOverloads constructor(
filesystem.openReadOnly(fileFinalPath).use { handle ->
logger?.debug("Validating $fileFinalPath")
notifyListeners { it.onStatusUpdate("Validating: ${file.fileName}") }
transitionPhase(DownloadPhase.VERIFYING)

neededChunks = Util.validateSteam3FileChecksums(
handle = handle,
Expand Down Expand Up @@ -1625,6 +1631,7 @@ class DepotDownloader @JvmOverloads constructor(
}

if (processingItemsMap.isEmpty()) {
transitionPhase(DownloadPhase.COMPLETE)
completionFuture.complete(null)
}
}
Expand All @@ -1647,6 +1654,13 @@ class DepotDownloader @JvmOverloads constructor(
}
}

// Notify listeners only when the phase actually changes
private fun transitionPhase(newPhase: DownloadPhase) {
if (currentPhase == newPhase) return
currentPhase = newPhase
notifyListeners { it.onPhaseChanged(newPhase) }
}

// endregion

// region [REGION] Queue Operations
Expand Down Expand Up @@ -1889,6 +1903,9 @@ class DepotDownloader @JvmOverloads constructor(

val depotPercentage = (sizeDownloaded.toFloat() / depotDownloadCounter.completeDownloadSize)

// Transition to DOWNLOADING once actual data chunks start flowing
transitionPhase(DownloadPhase.DOWNLOADING)

notifyListeners { listener ->
listener.onChunkCompleted(
depotId = depot.depotId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package `in`.dragonbra.javasteam.depotdownloader

// High level download phases reported by DepotDownloader
enum class DownloadPhase {
UNKNOWN,
PREPARING,
DOWNLOADING,
VERIFYING,
COMPLETE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ interface IDownloadListener {
* @param uncompressedBytes Actual data size (uncompressed)
*/
fun onDepotCompleted(depotId: Int, compressedBytes: Long, uncompressedBytes: Long) {}

// Called when the download transitions to a new phase
fun onPhaseChanged(phase: DownloadPhase) {}
}