Improve store install storage checks and recovery; adaptive parallel Epic installs (#710)

Space checks before Epic, GOG and Amazon installs (measured zero vs unknown), clear failure reasons with retry/clear, resume-aware checks, and safe per-file replacement so a failed or cancelled update never breaks an installed game.

Epic installs stream chunks straight into staged files in parallel (most bytes written once, a small budgeted cache for shared chunks, about 1x game size instead of 2x), pace updates by free space, and keep the lookahead, batch and bounded Java writers as hidden alternatives.

Co-authored-by: Juan Marti Mercado <juanma@microsoft.com>
Co-authored-by: The412Banner <the412banner@users.noreply.github.com>
This commit is contained in:
Juan MartíandThe412Banner authored and GitHub committed 2026-10-13 22:29:23 -04:00
1 parent d310e59fce
commit a074ba994b
34 files changed
+6777 -233

No files matched your search

@@ -82,6 +82,15 @@ object SessionPrefs {
prefs(context).edit().putString("gameStoresSpeedTier", tier).apply()
}
/**
* Hidden A/B switch for the Epic installer: "adaptive" (default: native parallel fetch written
* straight into staged files), "lookahead" (the native ordered-lookahead engine that caches each
* chunk and copies it into staged files), "batch" (the older whole-cache native fetch, then
* native assembly; needs about twice the game's size free) or "java" (the bounded Java writer).
* No UI; set it in the `session` preferences for benchmarks.
*/
fun storesEpicMode(context: Context): String = prefs(context).getString("storesEpicMode", "adaptive") ?: "adaptive"
fun storesOpenTab(context: Context): String =
prefs(context).getString("storesOpenTab", STORES_OPEN_LIBRARY)?.takeIf { it == STORES_OPEN_STORE } ?: STORES_OPEN_LIBRARY
@@ -68,6 +68,7 @@ class StoreGameSidecar(
const val VERSION = 1
const val STATE_INSTALLED = "installed"
const val STATE_INSTALLING = "installing"
const val STATE_UPDATING = "updating"
fun file(folder: File): File = File(folder, FILE_NAME)
@@ -9,12 +9,67 @@ import java.io.File
import java.util.zip.CRC32
object StoreInstallRoot {
const val SPACE_UNKNOWN = -1L
private const val INTERNAL = "root/Games/Stores"
private const val ON_VOLUME = "Games"
const val GUEST_EXTERNAL = "/mnt/droiddeck-stores"
class Target(val root: File, val label: String, val removable: Boolean) {
val freeBytes: Long get() = try { StatFs((if (root.isDirectory) root else root.parentFile ?: root).path).availableBytes } catch (e: Exception) { 0L }
val freeBytes: Long get() = availableBytes(root)
}
data class StorageCheck(
val enough: Boolean,
val requiredBytes: Long,
val availableBytes: Long,
val errorMessage: String? = null
)
@JvmStatic
fun availableBytes(file: File): Long = probeAvailableBytes(file, { StatFs(it.path).availableBytes }, { it.usableSpace })
internal fun probeAvailableBytes(file: File, statBytes: (File) -> Long, usableBytes: (File) -> Long): Long {
var dir: File? = if (file.isDirectory) file else file.parentFile
if (dir == null) dir = file
while (dir != null && !dir.exists()) dir = dir.parentFile
val existing = dir ?: return SPACE_UNKNOWN
return try {
statBytes(existing).coerceAtLeast(SPACE_UNKNOWN)
} catch (e: Exception) {
StoresState.logLine("storage: StatFs failed for store install target (${e.javaClass.simpleName})")
try {
// File's zero also means an unavailable measurement; only StatFs establishes zero.
usableBytes(existing).takeIf { it > 0L } ?: SPACE_UNKNOWN
} catch (fallback: Exception) {
StoresState.logLine("storage: usable-space fallback failed for store install target (${fallback.javaClass.simpleName})")
SPACE_UNKNOWN
}
}
}
@JvmStatic
fun insufficientSpace(required: Long, available: Long): Boolean = required > 0L && available >= 0L && available < required
/**
* The catalog size is the known minimum for an install. Epic's actual chunk-cache requirement
* is determined from its manifest before chunk downloads begin.
*/
fun estimatedBytesNeeded(item: CatalogItem): Long = StoreSizes.size(item).coerceAtLeast(0L)
/** Pre-install storage check to detect insufficient space before starting. */
fun checkStorage(context: Context, item: CatalogItem, targetRoot: File): StorageCheck {
val size = estimatedBytesNeeded(item)
val existing = existingFolder(context, item.store, item.id, item.title)
val freeTarget = availableBytes(existing ?: targetRoot)
// Backends account for completed files on resume; the catalog cannot describe remaining work.
// Comparing the full catalog size to remaining free space would reject valid resumes.
if (item.store == Store.EPIC || existing != null) return StorageCheck(enough = true, requiredBytes = size, availableBytes = freeTarget)
if (size <= 0L) return StorageCheck(enough = true, requiredBytes = 0L, availableBytes = freeTarget)
if (insufficientSpace(size, freeTarget)) {
val msg = context.getString(com.droiddeck.launcher.R.string.stores_storage_insufficient, formatBytes(size), formatBytes(freeTarget))
return StorageCheck(enough = false, requiredBytes = size, availableBytes = freeTarget, errorMessage = msg)
}
return StorageCheck(enough = true, requiredBytes = size, availableBytes = freeTarget)
}
fun internalRoot(context: Context): File = File(LinuxRuntime.rootDir(context), INTERNAL)
@@ -7,15 +7,35 @@ import com.droiddeck.launcher.frontend.AddedGames
import com.droiddeck.launcher.frontend.LibraryCache
import com.droiddeck.launcher.frontend.Library
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption
object StoreInstalls {
private const val TAG = "StoreInstalls"
fun begin(folder: File, store: Store, id: String, title: String, cover: String?, hero: String?) {
if (StoreGameSidecar.read(folder)?.isInstalled == true) return
val before = StoreGameSidecar.read(folder)
if (before?.isInstalled == true || before?.state == StoreGameSidecar.STATE_UPDATING) return
StoreGameSidecar(store, id, title, exe = "", cover = cover, hero = hero, state = StoreGameSidecar.STATE_INSTALLING).write(folder)
}
/** Epic commits verified files individually, not a whole-game transaction. Keep updates resumable. */
@JvmStatic
fun markUpdating(folder: File) {
val before = StoreGameSidecar.read(folder) ?: return
if (!before.isInstalled) return
val stage = File.createTempFile(".droiddeck-update-", ".stage", folder)
try {
java.io.FileOutputStream(stage).use {
it.write(before.copy(state = StoreGameSidecar.STATE_UPDATING, installVersion = "").toJson().toString(2).toByteArray(Charsets.UTF_8))
it.fd.sync()
}
Files.move(stage.toPath(), StoreGameSidecar.file(folder).toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING)
} finally {
stage.delete()
}
}
fun complete(context: Context, folder: File, sidecar: StoreGameSidecar, onStep: ((Int, Int) -> Unit)? = null) {
val app = context.applicationContext
onStep?.invoke(0, STEPS)
@@ -66,10 +86,11 @@ object StoreInstalls {
fun discard(context: Context, folder: File, caches: List<File>) {
val app = context.applicationContext
Thread({
val keep = StoreGameSidecar.read(folder)?.isInstalled == true
val sidecar = StoreGameSidecar.read(folder)
val keep = sidecar?.isInstalled == true || sidecar?.state == StoreGameSidecar.STATE_UPDATING
caches.forEach { deleteTree(it) }
if (!keep) deleteTree(folder)
StoresState.logLine(if (keep) "removed the partial update of ${folder.name}" else "removed ${folder.name}")
StoresState.logLine(if (keep) "removed update caches for ${folder.name}; verified file swaps are retained" else "removed ${folder.name}")
StoresState.notifyLibraryChanged(app)
}, "store-cancel-clean").start()
}
@@ -170,7 +170,25 @@ object StoresState {
fun install(context: Context, item: CatalogItem, root: java.io.File? = null) {
val app = context.applicationContext
val target = root ?: StoreInstallRoot.installRoot(app)
val folder = StoreInstallRoot.folderFor(app, item.store, item.id, item.title, root ?: StoreInstallRoot.installRoot(app))
val target = folder.parentFile!!.parentFile!!
val check = StoreInstallRoot.checkStorage(app, item, target)
if (!check.enough) {
val errorMsg = check.errorMessage ?: app.getString(com.droiddeck.launcher.R.string.stores_storage_insufficient, formatBytes(check.requiredBytes), formatBytes(check.availableBytes))
val entry = com.droiddeck.launcher.stores.download.DownloadEntry(
item.store,
item.id,
item.title,
cover = item.imageUrl,
bytesTotal = item.sizeBytes,
location = StoreInstallRoot.labelFor(app, folder)
)
com.droiddeck.launcher.stores.download.SavedQueue.remember(app, item, target)
com.droiddeck.launcher.stores.download.DownloadQueue.recordFailed(app, entry, errorMsg) {
install(app, item, target)
}
return
}
com.droiddeck.launcher.stores.download.SavedQueue.remember(app, item, target)
backends[item.store]?.install(app, item, target) ?: logLine("${item.store.label}: this build cannot install yet")
}
@@ -69,9 +69,10 @@ public final class AmazonDownloadManager {
if (manifestBytes == null) throw new InstallException("The manifest could not be downloaded");
AmazonManifest.ParsedManifest manifest = AmazonManifest.parse(manifestBytes);
cb.onLog("amazon: " + manifest.allFiles.size() + " files, " + fmt(manifest.totalInstallSize));
long usable = installDir.getUsableSpace();
if (manifest.totalInstallSize > 0 && usable > 0 && manifest.totalInstallSize > usable)
throw new InstallException("Not enough free space: need " + fmt(manifest.totalInstallSize) + ", only " + fmt(usable) + " free");
long remainingBytes = remainingInstallBytes(manifest.allFiles, installDir);
long usable = com.droiddeck.launcher.stores.StoreInstallRoot.availableBytes(installDir);
if (com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(remainingBytes, usable))
throw new InstallException("Not enough free space: need " + fmt(remainingBytes) + ", only " + fmt(usable) + " free");
cb.onSizes(manifest.totalInstallSize, manifest.totalInstallSize);
cb.onBytes(0, manifest.totalInstallSize, 0);
if (cancel.get()) return null;
@@ -175,7 +176,7 @@ public final class AmazonDownloadManager {
AtomicLong lastSpeedMs, AtomicLong lastSpeedBytes, AtomicLong speedBps, Callback cb, AtomicBoolean cancel) {
File destFile = new File(installDir, file.unixPath());
File tmpFile = new File(installDir, file.unixPath() + ".tmp");
if (destFile.exists() && destFile.length() == file.size) { totalDownloaded.addAndGet(file.size); return true; }
if (fileComplete(file, destFile)) { totalDownloaded.addAndGet(file.size); return true; }
String fileUrl = AmazonApiClient.appendPath(baseUrl, "files/" + file.hashHex());
for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
destFile.getParentFile().mkdirs();
@@ -245,6 +246,21 @@ public final class AmazonDownloadManager {
public static boolean isInstalled(File installDir) { return new File(installDir, COMPLETE_MARKER).exists(); }
static long remainingInstallBytes(List<AmazonManifest.ManifestFile> files, File installDir) {
long remaining = 0;
for (AmazonManifest.ManifestFile file : files) {
if (!fileComplete(file, new File(installDir, file.unixPath()))) remaining += file.size;
}
return remaining;
}
private static boolean fileComplete(AmazonManifest.ManifestFile file, File output) {
if (!output.isFile() || output.length() != file.size) return false;
if (file.hashAlgorithm != 0 || file.hashBytes.length == 0) return true;
try { return Arrays.equals(sha256(output), file.hashBytes); }
catch (Exception e) { return false; }
}
private static byte[] sha256(File file) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
try (FileInputStream fis = new FileInputStream(file)) {
@@ -88,7 +88,7 @@ object DownloadQueue {
}
}
private class Item(var entry: DownloadEntry, val factory: () -> DownloadJob, var job: DownloadJob? = null, val cancelled: AtomicBoolean = AtomicBoolean(false), var pauseRequested: Boolean = false) {
private class Item(var entry: DownloadEntry, val factory: (() -> DownloadJob)? = null, val retry: (() -> Unit)? = null, var job: DownloadJob? = null, val cancelled: AtomicBoolean = AtomicBoolean(false), var pauseRequested: Boolean = false) {
var speedAt = 0L
var speedBytes = 0L
var speedEwma = 0.0
@@ -137,6 +137,32 @@ object DownloadQueue {
advance(app)
}
/** Immediately records a failed download (e.g. from pre-flight storage check). */
fun recordFailed(context: Context, entry: DownloadEntry, error: String, retry: (() -> Unit)? = null) {
val app = context.applicationContext
val now = System.currentTimeMillis()
synchronized(lock) {
if (items[entry.key]?.entry?.isActive == true) return
items.remove(entry.key)
heldPaused.remove(entry.key)
items[entry.key] = Item(
entry.copy(
state = DownloadState.FAILED,
error = error,
speedBps = 0,
etaSeconds = -1,
startedAt = now,
finishedAt = now
),
retry = retry
)
renumber()
publishLocked()
}
StoresState.logLine("${entry.store.label}: \"${entry.name}\" failed: $error")
StoreDownloadService.ended(app, entry.copy(state = DownloadState.FAILED, error = error), installed = false, gamesKey = null)
}
fun pause(key: String) {
val item = synchronized(lock) { items[key] } ?: return
synchronized(lock) {
@@ -151,14 +177,21 @@ object DownloadQueue {
fun resume(context: Context, key: String) {
val app = context.applicationContext
val retry: (() -> Unit)?
synchronized(lock) {
val item = items[key] ?: return
if (item.entry.state != DownloadState.PAUSED && item.entry.state != DownloadState.FAILED) return
retry = item.retry
if (retry == null && item.factory == null) return
items.remove(key)
items[key] = Item(item.entry.copy(state = DownloadState.QUEUED, speedBps = 0, etaSeconds = -1, error = null, startedAt = System.currentTimeMillis()), item.factory)
if (retry == null) {
items[key] = Item(item.entry.copy(state = DownloadState.QUEUED, speedBps = 0, etaSeconds = -1, error = null, startedAt = System.currentTimeMillis()), item.factory)
}
renumber()
publishLocked()
}
// A preflight retry rechecks storage and enqueues the real backend, never a running placeholder.
if (retry != null) { retry(); return }
StoresState.logLine("resumed \"${entry(key)?.name}\"")
advance(app)
}
@@ -182,7 +215,7 @@ object DownloadQueue {
item.cancelled.set(true)
item.job?.cancel(deleteFiles = true)
} else {
Thread({ runCatching { item.factory().cancel(deleteFiles = true) } }, "store-dl-cancel").start()
Thread({ runCatching { item.factory?.invoke()?.cancel(deleteFiles = true) } }, "store-dl-cancel").start()
}
StoresState.logLine("cancelled \"${item.entry.name}\"")
StoreDownloadService.finish(app, key)
@@ -194,7 +227,7 @@ object DownloadQueue {
val item = synchronized(lock) { items[key] } ?: return
if (item.entry.state == DownloadState.RUNNING || item.entry.state == DownloadState.QUEUED) return
if (item.entry.state == DownloadState.FAILED || item.entry.state == DownloadState.PAUSED) {
Thread({ runCatching { item.factory().cancel(deleteFiles = true) } }, "store-dl-clear").start()
Thread({ runCatching { item.factory?.invoke()?.cancel(deleteFiles = true) } }, "store-dl-clear").start()
}
synchronized(lock) { items.remove(key); publishLocked() }
StoreDownloadService.finish(context.applicationContext, key)
@@ -276,7 +309,7 @@ object DownloadQueue {
var result: String? = null
var error: String? = null
try {
val job = item.factory()
val job = checkNotNull(item.factory).invoke()
synchronized(lock) { item.job = job }
StoresState.logLine("${item.entry.store.label}: fetching manifest for \"${item.entry.name}\"")
result = job.run(handle)
@@ -239,12 +239,16 @@ class StoreDownloadService : Service() {
val app = context.applicationContext
channels(app)
val code = entry.key.hashCode()
val n = Notification.Builder(app, DONE_CHANNEL)
val builder = Notification.Builder(app, DONE_CHANNEL)
.setSmallIcon(if (installed) android.R.drawable.stat_sys_download_done else android.R.drawable.stat_notify_error)
.setContentTitle(app.getString(if (installed) R.string.stores_notification_installed else R.string.stores_notification_failed, entry.name))
.setAutoCancel(true)
.setContentIntent(if (installed && gamesKey != null) openPage(app, gamesKey, code + 10) else openDownloads(app, code + 11))
.build()
if (!installed && !entry.error.isNullOrBlank()) {
builder.setContentText(entry.error)
builder.style = Notification.BigTextStyle().bigText(entry.error)
}
val n = builder.build()
runCatching { app.getSystemService(NotificationManager::class.java)?.notify(ID + 1 + (code and 0xFFFF), n) }
}
}
@@ -136,6 +136,8 @@ object EpicBackend : StoreBackend, EpicLaunchSupport {
EpicLibrary.rememberSize(app, item.id, diskBytes)
}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
// Assembly overlaps chunk fetching; only the final install event advances the UI.
if (stage == "stream") return
handle.stage(when (stage) { "check" -> DownloadStage.MANIFEST; "verify" -> DownloadStage.VERIFY; else -> DownloadStage.INSTALL })
handle.stageProgress(done, total, items, itemsTotal, bytes = stage == "install")
}
File diff suppressed because it is too large. Load diff
@@ -17,6 +17,30 @@ object EpicNative {
fun onComplete(success: Boolean, error: String, bytes: Long)
}
interface InstallListener : Listener {
/** Synchronous approval before the first verified atomic replacement. False aborts. */
fun beforeSwap(): Boolean
}
/** One native budgeted fetch/assembly run. A loaded-engine failure is never a Java retry. */
@JvmStatic
fun installAdaptive(request: String, cancel: AtomicBoolean, listener: InstallListener): Result =
drive("nativeInstallAdaptive", cancel, listener, strict = true) { inner ->
nativeInstallAdaptive(request, inner)
}
/** Callbacks from a [stream] run; every method is called on a native thread. */
interface StreamListener {
/** File bytes written so far (staging included), and the files verified and swapped in so far. */
fun onStreamProgress(bytesWritten: Long, bytesTotal: Long, filesDone: Int, filesTotal: Int, filesBytes: Long)
/** Engine log line (already in logcat under `EpicNative`). */
fun onLog(line: String)
/** Once, before the first file replaces its destination; false (or a throw) fails the run with nothing replaced. */
fun onBeforeReplace(): Boolean
/** Terminal; `bytes` = file bytes written. */
fun onComplete(success: Boolean, error: String, bytes: Long)
}
class Result(
@JvmField val started: Boolean, @JvmField val success: Boolean, @JvmField val cancelled: Boolean,
@JvmField val error: String, @JvmField val bytes: Long, @JvmField val done: Int, @JvmField val total: Int,
@@ -36,13 +60,69 @@ object EpicNative {
fun assemble(manifest: ByteArray, installDir: String, chunkCacheDir: String, fileIdx: IntArray, cancel: AtomicBoolean?, listener: Listener): Result =
drive("nativeAssemble", cancel, listener) { inner -> nativeAssemble(manifest, installDir, chunkCacheDir, fileIdx, inner) }
private fun drive(what: String, cancel: AtomicBoolean?, listener: Listener, start: (Listener) -> Long): Result {
/**
* The adaptive install of the files [pendingFileIdx] (indices into the manifest's file list),
* with at most [cacheBudget] bytes of shared chunks cached and at most [overlapBudget] planned
* bytes of replaced files staged at once (`-1` = no limit), staged in [stagingDir] (the
* manager's owned staging directory). `started == false` means nothing was fetched or written.
*/
@JvmStatic
fun stream(
manifest: ByteArray, installDir: String, chunkCacheDir: String, stagingDir: String, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedBytes: Long,
cacheBudget: Long, overlapBudget: Long, caBundlePath: String, maxWorkers: Int, processWorkers: Int, cancel: AtomicBoolean?, listener: StreamListener,
): Result {
if (!StoresNative.ensureLoaded()) return Result(false, false, false, "engine not built", 0L, 0, 0)
val latch = CountDownLatch(1)
val completion = AtomicReference<Completion?>(null)
val doneRef = AtomicReference(0)
val totalRef = AtomicReference(pendingFileIdx.size)
val inner = object : StreamListener {
override fun onStreamProgress(bytesWritten: Long, bytesTotal: Long, filesDone: Int, filesTotal: Int, filesBytes: Long) {
doneRef.set(filesDone); totalRef.set(filesTotal)
listener.onStreamProgress(bytesWritten, bytesTotal, filesDone, filesTotal, filesBytes)
}
override fun onLog(line: String) = listener.onLog(line)
override fun onBeforeReplace(): Boolean = runCatching { listener.onBeforeReplace() }.getOrDefault(false)
override fun onComplete(success: Boolean, error: String, bytes: Long) {
completion.set(Completion(success, error, bytes))
latch.countDown()
runCatching { listener.onComplete(success, error, bytes) }
}
}
val handle: Long = try {
nativeStream(manifest, installDir, chunkCacheDir, stagingDir, cdnPrefixes, pendingFileIdx, expectedBytes, cacheBudget, overlapBudget, caBundlePath, maxWorkers, processWorkers, inner)
} catch (t: Throwable) {
Log.w(TAG, "engine unavailable: ${t.javaClass.simpleName}: ${t.message}")
return Result(false, false, false, "nativeStream: ${t.javaClass.simpleName}: ${t.message}", 0L, 0, 0)
}
if (handle == 0L) return Result(false, false, false, completion.get()?.error ?: "not started", 0L, 0, totalRef.get())
var cancelSent = false
try {
while (!latch.await(250, TimeUnit.MILLISECONDS)) {
if (!cancelSent && cancel != null && cancel.get()) {
nativeCancel(handle)
cancelSent = true
}
}
} finally {
if (completion.get() == null) nativeCancel(handle)
nativeRelease(handle)
}
val c = completion.get()
val cancelled = cancelSent || (c != null && !c.success && c.error == "cancelled")
val success = !cancelled && c != null && c.success
val error = when { cancelled -> "cancelled"; c == null -> "no completion"; else -> c.error }
return Result(true, success, cancelled, error, c?.bytes ?: 0L, doneRef.get(), totalRef.get())
}
private fun drive(what: String, cancel: AtomicBoolean?, listener: Listener, strict: Boolean = false, start: (Listener) -> Long): Result {
if (!StoresNative.ensureLoaded()) return Result(false, false, false, "engine not built", 0L, 0, 0)
val latch = CountDownLatch(1)
val completion = AtomicReference<Completion?>(null)
val doneRef = AtomicReference(0)
val totalRef = AtomicReference(0)
val inner = object : Listener {
val inner = object : InstallListener {
override fun beforeSwap(): Boolean = (listener as? InstallListener)?.beforeSwap() ?: false
override fun onPlan(chunksTotal: Int, bytesTotal: Long, chunkDir: String) { totalRef.set(chunksTotal); listener.onPlan(chunksTotal, bytesTotal, chunkDir) }
override fun onProgress(bytesDone: Long, bytesTotal: Long, done: Int, total: Int) { doneRef.set(done); totalRef.set(total); listener.onProgress(bytesDone, bytesTotal, done, total) }
override fun onLog(line: String) = listener.onLog(line)
@@ -55,21 +135,26 @@ object EpicNative {
val handle: Long = try {
start(inner)
} catch (t: Throwable) {
Log.w(TAG, "engine unavailable: ${t.javaClass.simpleName}: ${t.message}")
return Result(false, false, false, "$what: ${t.javaClass.simpleName}", 0L, 0, 0)
Log.w(TAG, "${if (strict) "engine start failed (no fallback)" else "engine unavailable"}: ${t.javaClass.simpleName}: ${t.message}")
return Result(strict, false, false, "$what: ${t.javaClass.simpleName}: ${t.message}", 0L, 0, 0)
}
if (handle == 0L) return Result(false, false, false, completion.get()?.error ?: "not started", 0L, 0, totalRef.get())
if (handle == 0L) return Result(strict, false, false, completion.get()?.error ?: "not started", 0L, 0, totalRef.get())
var cancelSent = false
try {
while (!latch.await(250, TimeUnit.MILLISECONDS)) {
if (cancel != null && cancel.get()) {
nativeCancel(handle)
cancelSent = true
latch.await(5, TimeUnit.SECONDS)
if (strict) {
while (!latch.await(250, TimeUnit.MILLISECONDS)) { }
} else {
latch.await(5, TimeUnit.SECONDS)
}
break
}
}
} finally {
if (completion.get() == null) nativeCancel(handle)
nativeRelease(handle)
}
val c = completion.get()
@@ -78,6 +163,9 @@ object EpicNative {
return Result(true, success, cancelSent, error, c?.bytes ?: 0L, doneRef.get(), totalRef.get())
}
@JvmStatic
private external fun nativeInstallAdaptive(request: String, listener: Listener): Long
@JvmStatic
private external fun nativeStart(
manifest: ByteArray, installDir: String, chunkCacheDir: String, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedChunks: Int, expectedBytes: Long,
@@ -87,6 +175,12 @@ object EpicNative {
@JvmStatic
private external fun nativeAssemble(manifest: ByteArray, installDir: String, chunkCacheDir: String, fileIdx: IntArray, listener: Listener): Long
@JvmStatic
private external fun nativeStream(
manifest: ByteArray, installDir: String, chunkCacheDir: String, stagingDir: String, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedBytes: Long,
cacheBudget: Long, overlapBudget: Long, caBundlePath: String, maxWorkers: Int, processWorkers: Int, listener: StreamListener,
): Long
@JvmStatic
private external fun nativeCancel(handle: Long)
@@ -152,11 +152,12 @@ public final class GogDownloadManager {
installPath.mkdirs();
long plannedBytes = 0;
for (DepotFile df : files) plannedBytes += df.totalSize;
if (plannedBytes > 0) {
long usable = installPath.getUsableSpace();
if (usable > 0 && plannedBytes > usable) {
long remainingBytes = remainingInstallBytes(files, installPath);
if (remainingBytes > 0) {
long usable = com.droiddeck.launcher.stores.StoreInstallRoot.availableBytes(installPath);
if (com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(remainingBytes, usable)) {
out.hardStop = true;
out.error = "Not enough free space: need " + formatBytes(plannedBytes) + ", only " + formatBytes(usable) + " free";
out.error = "Not enough free space: need " + formatBytes(remainingBytes) + ", only " + formatBytes(usable) + " free";
return out;
}
}
@@ -946,7 +947,16 @@ public final class GogDownloadManager {
for (File f : files) if (f.isDirectory()) collectExeRecursive(f, out);
}
private static final class DepotFile {
static long remainingInstallBytes(List<DepotFile> files, File installPath) {
long remaining = 0;
for (DepotFile file : files) {
File output = new File(installPath, file.relativePath);
if (!fileVerified(output, file.totalSize, file.md5)) remaining += file.totalSize;
}
return remaining;
}
static final class DepotFile {
final String relativePath;
final List<ChunkRef> chunks = new ArrayList<>();
String md5;
@@ -51,7 +51,9 @@ internal fun StoreGameDetail(store: Store, key: String, s: FrontEndState, a: Fro
val id = key.substringAfter(':')
val item = (StoresState.library[store].orEmpty() + StoresState.shelves[store]?.all.orEmpty()).firstOrNull { it.id == id }
val installed = StoresState.installedGame(store, id)
val download = StoresState.download("${store.id}:$id")?.takeIf { it.isActive }
val rawDownload = StoresState.download("${store.id}:$id")
val download = rawDownload?.takeIf { it.isActive }
val failedDownload = rawDownload?.takeIf { it.state == DownloadState.FAILED }
val title = item?.title ?: installed?.sidecar?.title ?: id
Rise(0) {
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.padding(bottom = 12.dp)) {
@@ -73,12 +75,24 @@ internal fun StoreGameDetail(store: Store, key: String, s: FrontEndState, a: Fro
val state = when {
installed != null -> stringResource(R.string.stores_eyebrow_installed) + " · " + com.droiddeck.launcher.stores.StoreInstallRoot.labelFor(ctx, installed.folder)
download != null && download.location.isNotBlank() -> stringResource(R.string.stores_eyebrow_library) + " · " + download.location
failedDownload != null && failedDownload.location.isNotBlank() -> stringResource(R.string.stores_eyebrow_library) + " · " + failedDownload.location
item?.owned == true -> stringResource(R.string.stores_eyebrow_library)
else -> stringResource(R.string.stores_eyebrow_store)
}
val size = (item?.sizeBytes ?: 0L).takeIf { it > 0 }?.let { " · " + formatBytes(it) } ?: ""
Text((state + size).uppercase(), fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.2.sp, color = pal.signal)
Text(title, fontSize = if (narrow) 24.sp else 30.sp, lineHeight = if (narrow) 27.sp else 33.sp, fontWeight = FontWeight.Black, color = colors.onBackground, maxLines = 2, overflow = TextOverflow.Ellipsis)
if (!failedDownload?.error.isNullOrBlank()) {
Text(
failedDownload!!.error!!,
fontSize = 13.sp,
lineHeight = 17.sp,
fontWeight = FontWeight.Medium,
color = colors.error,
maxLines = 3,
overflow = TextOverflow.Ellipsis
)
}
Actions {
when {
installed != null -> {
@@ -95,6 +109,16 @@ internal fun StoreGameDetail(store: Store, key: String, s: FrontEndState, a: Fro
else PrimaryButton(stringResource(R.string.stores_dl_resume), main = true) { com.droiddeck.launcher.stores.download.DownloadQueue.resume(ctx, download.key) }
ConfirmButton(stringResource(R.string.stores_dl_cancel), stringResource(R.string.stores_dl_cancel_confirm)) { com.droiddeck.launcher.stores.download.DownloadQueue.cancel(ctx, download.key) }
}
failedDownload != null -> {
ActionChip(stringResource(R.string.stores_dl_failed), ok = false)
PrimaryButton(stringResource(R.string.stores_dl_resume), main = true) {
if (item != null) StoresState.requestInstall(ctx, item)
else com.droiddeck.launcher.stores.download.DownloadQueue.retry(ctx, failedDownload.key)
}
ConfirmButton(stringResource(R.string.stores_dl_clear), stringResource(R.string.stores_dl_cancel_confirm), compact = true) {
com.droiddeck.launcher.stores.download.DownloadQueue.clear(ctx, failedDownload.key)
}
}
item?.owned == true && StoresState.isUnfinished(item) -> {
InstallButton(stringResource(R.string.stores_resume_install)) { StoresState.requestInstall(ctx, item) }
ConfirmButton(stringResource(R.string.stores_dl_clear), stringResource(R.string.stores_dl_cancel_confirm), compact = true) { StoresState.clearUnfinished(ctx, item) }
@@ -305,6 +305,7 @@ private fun Modifier.timeRim(left: () -> Float, color: Color): Modifier = drawWi
@Composable
private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) {
val ctx = LocalContext.current
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val narrow = LocalNarrowPane.current
val ink = Color.White
@@ -326,7 +327,11 @@ private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions)
val meta = when (d.state) {
DownloadState.INSTALLED -> stringResource(R.string.stores_installed_chip)
DownloadState.CANCELLED -> stringResource(R.string.stores_dl_cancelled)
DownloadState.FAILED -> if (d.bytesDone > 0) stringResource(R.string.stores_dl_failed_kept, formatBytes(d.bytesDone)) else stringResource(R.string.stores_dl_failed)
DownloadState.FAILED -> {
if (!d.error.isNullOrBlank()) stringResource(R.string.stores_dl_failed_reason, d.error)
else if (d.bytesDone > 0) stringResource(R.string.stores_dl_failed_kept, formatBytes(d.bytesDone))
else stringResource(R.string.stores_dl_failed)
}
DownloadState.PAUSED -> stringResource(R.string.stores_dl_paused)
DownloadState.QUEUED -> if (d.queuePosition > 0) stringResource(R.string.stores_dl_queued_at, d.queuePosition) else stringResource(R.string.stores_dl_queued)
DownloadState.RUNNING -> {
@@ -338,7 +343,8 @@ private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions)
}
}
}
Text(meta, fontSize = 12.sp, color = dim, maxLines = 2, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false))
val metaColor = if (d.state == DownloadState.FAILED) colors.error else dim
Text(meta, fontSize = 12.sp, color = metaColor, maxLines = 2, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false))
if (d.state == DownloadState.RUNNING && (d.stage == DownloadStage.DOWNLOAD || d.stage == DownloadStage.INSTALL) && d.speedBps > 0) {
Text(formatSpeed(d.speedBps), fontSize = 12.sp, color = dim, maxLines = 1)
if (d.etaSeconds >= 0) Text(eta(d.etaSeconds), fontSize = 12.sp, color = dim, maxLines = 1)
@@ -355,7 +361,10 @@ private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions)
RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_dl_resume), primary = true) { DownloadQueue.resume(ctx, d.key) }
CancelAction { DownloadQueue.cancel(ctx, d.key) }
}
DownloadState.FAILED -> RoundAction(Icons.Filled.Refresh, stringResource(R.string.stores_dl_resume), primary = true) { DownloadQueue.retry(ctx, d.key) }
DownloadState.FAILED -> {
RoundAction(Icons.Filled.Refresh, stringResource(R.string.stores_dl_resume), primary = true) { DownloadQueue.retry(ctx, d.key) }
CancelAction { DownloadQueue.clear(ctx, d.key) }
}
DownloadState.INSTALLED -> Box(Modifier.bloomIn(true, 420)) {
RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_play), primary = true) { launchStoreGame(ctx, d.store, d.id, s, a) }
}
@@ -411,6 +420,7 @@ private fun StageBar(d: DownloadEntry) {
val x = i * (segW + gap)
drawRoundRect(colors.surfaceVariant, Offset(x, cy - h / 2f), Size(segW, h), CornerRadius(h / 2f))
val base = when {
d.state == DownloadState.FAILED -> colors.error
done[i] -> pal.good
actives[i] && paused -> pausedColor.copy(alpha = breath)
actives[i] && stages[i] == DownloadStage.VERIFY -> verifyColor
@@ -67,10 +67,21 @@ internal fun InstallWhereDialog(item: CatalogItem, onDismiss: () -> Unit) {
Rise(1) {
Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) {
targets.forEachIndexed { i, t ->
val needed = com.droiddeck.launcher.stores.StoreInstallRoot.estimatedBytesNeeded(item)
val freeBytes = t.freeBytes
val isLow = com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(needed, freeBytes)
val freeText = if (freeBytes < 0L) {
stringResource(R.string.stores_target_free_unknown)
} else if (isLow) {
stringResource(R.string.stores_storage_low, formatBytes(freeBytes), formatBytes(needed))
} else {
stringResource(R.string.stores_target_free, formatBytes(freeBytes))
}
TargetRow(
label = if (t.removable) t.label else stringResource(R.string.stores_target_internal),
free = stringResource(R.string.stores_target_free, formatBytes(t.freeBytes)),
free = freeText,
removable = t.removable, focus = focus[i],
warning = isLow,
) {
SessionPrefs.setStoresInstallTarget(ctx, t.root.absolutePath)
StoresState.install(ctx, item, t.root)
@@ -83,7 +94,7 @@ internal fun InstallWhereDialog(item: CatalogItem, onDismiss: () -> Unit) {
}
@Composable
private fun TargetRow(label: String, free: String, removable: Boolean, focus: FocusRequester, onPick: () -> Unit) {
private fun TargetRow(label: String, free: String, removable: Boolean, focus: FocusRequester, warning: Boolean = false, onPick: () -> Unit) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val src = remember { MutableInteractionSource() }
@@ -99,6 +110,6 @@ private fun TargetRow(label: String, free: String, removable: Boolean, focus: Fo
) {
Icon(if (removable) Icons.Outlined.SdCard else Icons.Outlined.Smartphone, null, tint = if (hot) pal.signal else colors.onBackground, modifier = Modifier.size(22.dp))
Text(label, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f))
Text(free, fontSize = 12.sp, color = colors.onSurfaceVariant, maxLines = 1)
Text(free, fontSize = 12.sp, color = if (warning) colors.error else colors.onSurfaceVariant, maxLines = 1)
}
}
@@ -438,10 +438,20 @@ private fun InstallWhere(item: CatalogItem) {
StepTitle(title)
StepList {
targets.forEachIndexed { i, t ->
val needed = com.droiddeck.launcher.stores.StoreInstallRoot.estimatedBytesNeeded(item)
val freeBytes = t.freeBytes
val isLow = com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(needed, freeBytes)
val trailingText = if (freeBytes < 0L) {
stringResource(R.string.stores_target_free_unknown)
} else if (isLow) {
stringResource(R.string.stores_storage_low, formatBytes(freeBytes), formatBytes(needed))
} else {
stringResource(R.string.stores_target_free, formatBytes(freeBytes))
}
StepChoice(
label = if (t.removable) t.label else internalLabel,
icon = if (t.removable) androidx.compose.material.icons.Icons.Outlined.SdCard else androidx.compose.material.icons.Icons.Outlined.Smartphone,
trailing = stringResource(R.string.stores_target_free, formatBytes(t.freeBytes)),
trailing = trailingText,
enabled = open,
modifier = Modifier.fillMaxWidth().stepItem(1 + i).then(if (i == defaultIndex) Modifier.focusRequester(first) else Modifier),
) {
+3
View File
@@ -2084,6 +2084,9 @@
<string name="stores_install_title">Install %1$s:</string>
<string name="stores_target_internal">Internal</string>
<string name="stores_target_free">%1$s free</string>
<string name="stores_target_free_unknown">Free space unknown</string>
<string name="stores_storage_insufficient">Not enough free space: need %1$s, only %2$s free</string>
<string name="stores_storage_low">Insufficient space (%1$s free, need %2$s)</string>
<string name="stores_notification_channel">Store downloads</string>
<string name="stores_notification_channel_hint">Shows game download progress and keeps downloads running in the background</string>
<string name="stores_notification_one">Downloading</string>
+90 -1
View File
@@ -112,7 +112,45 @@ On-disk protocol (what a resumed run, by either engine, relies on): a file is as
## `com.droiddeck.launcher.stores.epic.EpicNative`
Two runs make up the native half of an Epic install:
The manager's default Epic install is the **stream** run (`nativeStream`, mode `adaptive`,
below): the chunks are fetched in parallel and written straight into staged files as they arrive;
on device it measured about 83 MB/s on a 71 GB install. The **lookahead** install
(`nativeInstallAdaptive`, mode `lookahead`) is its fallback when the stream engine's space plan
does not fit, and is selectable directly, as is the legacy batch pair (`nativeStart` +
`nativeAssemble`, mode `batch`), through the hidden `storesEpicMode` preference.
### Lookahead install (snapshot schema v1)
`nativeInstallAdaptive(request: String, listener: EpicNative.Listener): Long`
exports `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeInstallAdaptive`.
Its exact Kotlin descriptor is
`(Ljava/lang/String;Lcom/droiddeck/launcher/stores/epic/EpicNative$Listener;)J`.
The listener must also implement `EpicNative.InstallListener.beforeSwap(): Boolean` (`()Z`).
Rust invokes it synchronously after staging/hash/sync and before the first atomic replacement;
false or a Java exception aborts without replacing that output. Java marks the sidecar updating
there. Later verified swaps preserve mixed-update/resume semantics.
The UTF-8 JSON request contains:
- `schema_version: 1`, absolute `install_dir`, `cache_dir`, owned `staging_dir`,
`same_volume`, signed `free_game`/`free_cache` (`-1` unknown), `max_workers`, CDN `hosts`.
- `chunks`: distinct `guid`, relative CDN `path`, decoded `size`, `exact_size`, `wire_size`,
SHA-1 hex `sha1` (empty/all-zero means absent), `cached` verified length (`-1` absent).
- `files`: pending manifest-order relative `path`, `sha1`, current `old_size`, ordered `parts`
containing `guid`, unsigned `offset` and `size`.
Parse, owned-namespace validation and adaptive storage planning happen synchronously. Errors
return zero after `onComplete(false, error, 0)` and are **terminal**, not fallback signals.
`onPlan` reports the selected lookahead/retention/peaks; `onProgress` counts staged file bytes
and verified swapped file counts, not fetched wire bytes. Fetch and staged assembly overlap
in one native run, using the same pure reservation/release policy as the peak planner.
Cancellation/release joins native workers; no worker survives the terminal callback/handle
release. Only `StoresNative.ensureLoaded() == false` selects bounded parallel Java; the same rule
holds for `nativeStream`.
### Legacy batch APIs (mode `batch`, for A/B runs)
Two explicit calls provide the legacy batch interface:
1. **Fetch** (`nativeStart`): fills the chunk cache with verified, decompressed chunks for the
files the manager says are pending. The Kotlin manager still fetches and parses the manifest
@@ -224,6 +262,57 @@ Bannerlator's `BlEpicDownload.run(...)` wraps `nativeStart` in a blocking call t
`onComplete`, then `nativeRelease` in `finally`. The same wrapper shape fits `nativeAssemble`
(cancel is honoured between files; a multi-GB file finishes before the run stops).
### `nativeStream` — the adaptive install
| Method | JNI signature | Symbol |
|---|---|---|
| `nativeStream(...)`: Long | `([BLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[IJJJLjava/lang/String;IILcom/droiddeck/launcher/stores/epic/EpicNative$StreamListener;)J` | `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStream` |
```kotlin
@JvmStatic private external fun nativeStream(
manifest: ByteArray, installDir: String, chunkCacheDir: String, stagingDir: String,
cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedBytes: Long, cacheBudget: Long,
overlapBudget: Long, caBundlePath: String,
maxWorkers: Int, processWorkers: Int, listener: StreamListener,
): Long
```
Parameters as `nativeStart`, plus `stagingDir` (the manager's owned
`<installDir>/.droiddeck-epic-staging`, which must exist; `""` = stage beside each file), and
except `expectedBytes` (the manager's `Σ fileSize` of the pending
files, `-1` = no cross-check; no chunk count) and `cacheBudget` (the most bytes of shared chunks
the cache may hold; the manager sizes it from free space, `0` = never cache, refetch instead) and
`overlapBudget` (the most planned bytes of *replaced* files - destination already holds data -
staged at once, since each sits beside its old copy until its swap; one is always allowed so the
run progresses; `-1` = no limit). Files are admitted in order and a chunk is dispatched only once
the file of its first use is admitted; fresh files are never held back.
`chunkCacheDir` is `""` (`<installDir>/.chunks`) or a scratch directory; the manager falls back to
`""` for a card install when internal storage cannot hold the budget.
What the run does (`epic/stream.rs`): a chunk one file part uses is decompressed, verified and
written at the part's offset into a unique `stream-*.stage` file in `stagingDir`; a
chunk several parts use is also kept in the cache, decompressed, as `<GUID>` (a later copy after
an eviction as `<GUID>.<n>`) and deleted after its last covered part. The plan evicts by furthest
next use when the budget would be exceeded and fetches the evicted chunk again later. Each file,
once complete: synced, size and SHA-1 checked, `onBeforeReplace()` once for the whole run, then
renamed over its destination. Cancel or failure removes every unfinished stage file before
`onComplete`, leaves the destinations and the cache; success removes the cache directory.
Planning runs on the calling thread and also prepares the cache (keeps verified chunks this
install needs, deletes everything else, as the Java writer does). A refusal returns `0` after a
synchronous `onComplete(false, reason, 0)`; nothing was fetched or written, and the manager fails
the install with that reason (it does not switch engines once the library is loaded).
| `StreamListener` method | JNI signature | When |
|---|---|---|
| `onStreamProgress(bytesWritten: Long, bytesTotal: Long, filesDone: Int, filesTotal: Int, filesBytes: Long)` | `(JJIIJ)V` | after written parts (at most every 50 ms) and after every finished file; `filesBytes` = sizes of the files swapped in |
| `onLog(line: String)` | `(Ljava/lang/String;)V` | engine lines: `stream-plan …`, the fetch core's lines, `stream-summary mode=adaptive …` (peak cache, peak staging, refetches, direct vs cached bytes) |
| `onBeforeReplace(): Boolean` | `()Z` | once, before the first file replaces its destination; `false` or a throw fails the run with nothing replaced |
| `onComplete(success: Boolean, error: String, bytes: Long)` | `(ZLjava/lang/String;J)V` | terminal, exactly once; `bytes` = file bytes written; `error` = `"cancelled"` on cancel |
Unlike the batch runs, the Kotlin wrapper (`EpicNative.stream`) waits for `onComplete` after a
cancel without a time limit: the stage-file cleanup must be finished before the manager returns.
## `com.droiddeck.launcher.stores.amazon.AmazonNative`
The file-fetch pool of an Amazon Games install: one streamed HTTP GET per manifest file, SHA-256
+2
View File
@@ -1,11 +1,13 @@
//! Derived from GameNative's Epic manager (itself derived from Legendary); both are GPL-3.0 and this crate carries the same licence (see Cargo.toml).
pub mod assemble;
pub mod adaptive;
pub mod chunk;
pub mod driver;
pub mod jni;
pub mod manifest;
pub mod plan;
pub mod stream;
pub const USER_AGENT: &str =
"UELauncher/11.0.1-14907503+++Portal+Release-Live Windows/10.0.19041.1.256.64bit";
File diff suppressed because it is too large. Load diff
+28 -1
View File
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{BufWriter, Write};
use std::io::{BufWriter, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
@@ -10,6 +10,33 @@ use sha1::{Digest, Sha1};
use super::manifest::{parse_manifest, FileInfo, Manifest};
use super::plan::resolve_cache_dir;
/// Streaming assembly primitive used by the budgeted installer; never holds a decoded chunk
/// in memory and closes the reader before the caller releases the cache reservation.
pub fn copy_chunk_part(
cache_path: &Path, offset: u64, size: u64, output: &mut impl Write,
sha: &mut Sha1, cancel: &AtomicBool,
) -> Result<u64, String> {
let mut input = File::open(cache_path).map_err(|e| format!("read chunk: {e}"))?;
let length = input.metadata().map_err(|e| e.to_string())?.len();
if offset.checked_add(size).filter(|end| *end <= length).is_none() {
return Err("chunk part exceeds cached data".into());
}
input.seek(SeekFrom::Start(offset)).map_err(|e| e.to_string())?;
let mut left = size;
let mut buffer = [0u8; 64 * 1024];
while left > 0 {
if cancel.load(Ordering::Acquire) { return Err("cancelled".into()); }
let take = left.min(buffer.len() as u64) as usize;
let count = input.read(&mut buffer[..take])
.map_err(|e| e.to_string())?;
if count == 0 { return Err("short cached chunk".into()); }
output.write_all(&buffer[..count]).map_err(|e| e.to_string())?;
sha.update(&buffer[..count]);
left -= count as u64;
}
Ok(size)
}
pub struct AssembleRequest {
pub manifest_bytes: Vec<u8>,
pub install_dir: String,
+141 -3
View File
@@ -71,10 +71,14 @@ struct HashingWriter<W: Write> {
inner: W,
sha: Option<Sha1>,
written: u64,
limit: u64,
}
impl<W: Write> Write for HashingWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.written.saturating_add(buf.len() as u64) > self.limit {
return Err(io::Error::other("chunk exceeds reserved decoded window"));
}
let n = self.inner.write(buf)?;
if let Some(sha) = self.sha.as_mut() {
sha.update(&buf[..n]);
@@ -102,10 +106,21 @@ pub fn write_verified_chunk(
let tmp = part_path(final_path);
let _ = fs::remove_file(&tmp);
let result = write_part(body, expected_sha1, &tmp, final_path);
let result = write_part(body, expected_sha1, &tmp, final_path, u64::MAX, false);
if result.is_err() {
let _ = fs::remove_file(&tmp);
}
result
}
pub fn write_verified_chunk_bounded(
body: &[u8], expected_sha1: Option<&[u8; 20]>, final_path: &Path,
decoded_limit: u64, exact_size: bool,
) -> Result<u64, String> {
let tmp = part_path(final_path);
let result = write_part(body, expected_sha1, &tmp, final_path, decoded_limit, exact_size);
if result.is_err() { let _ = fs::remove_file(tmp); }
result
}
@@ -114,20 +129,43 @@ fn write_part(
expected_sha1: Option<&[u8; 20]>,
tmp: &Path,
final_path: &Path,
limit: u64,
exact_size: bool,
) -> Result<u64, String> {
let hdr = parse_chunk_header(body)?;
let payload = chunk_payload(body, &hdr)?;
if limit != u64::MAX && (hdr.compressed_size < 0 || payload.len() != hdr.compressed_size as usize) {
return Err("truncated chunk body".into());
}
let file = File::create(tmp).map_err(|e| format!("open {}: {e}", tmp.display()))?;
let mut out = HashingWriter {
inner: BufWriter::with_capacity(128 * 1024, file),
sha: expected_sha1.map(|_| Sha1::new()),
written: 0,
limit,
};
if hdr.is_compressed() {
let mut dec = flate2::read::ZlibDecoder::new(payload);
io::copy(&mut dec, &mut out).map_err(|e| format!("inflate: {e}"))?;
if limit == u64::MAX {
let mut dec = flate2::read::ZlibDecoder::new(payload);
io::copy(&mut dec, &mut out).map_err(|e| format!("inflate: {e}"))?;
} else {
let mut dec = flate2::Decompress::new(true);
let mut buffer = [0u8; 128 * 1024];
loop {
let input = dec.total_in() as usize;
let before = dec.total_out();
let status = dec.decompress(&payload[input..], &mut buffer, flate2::FlushDecompress::None)
.map_err(|e| format!("inflate: {e}"))?;
let count = (dec.total_out() - before) as usize;
out.write_all(&buffer[..count]).map_err(|e| format!("inflate write: {e}"))?;
if status == flate2::Status::StreamEnd { break; }
if count == 0 && dec.total_in() as usize == input {
return Err("truncated compressed Epic chunk".into());
}
}
}
} else {
out.write_all(payload).map_err(|e| format!("write: {e}"))?;
}
@@ -135,6 +173,9 @@ fn write_part(
let written = out.written;
let digest = out.sha.take().map(|s| s.finalize());
drop(out);
if exact_size && written != limit {
return Err("chunk decoded size mismatch".into());
}
if let (Some(expected), Some(actual)) = (expected_sha1, digest) {
if actual.as_slice() != &expected[..] {
@@ -146,6 +187,80 @@ fn write_part(
Ok(written)
}
/// The window a chunk decompresses to: the manifest's, or 1 MiB for a legacy JSON manifest that
/// names none (`EpicDownloadManager.chunkWindowSize`). The streaming installer also plans its
/// shared-chunk cache with this size.
pub fn chunk_window_bytes(window_size: i32) -> u64 {
if window_size > 0 {
window_size as u64
} else {
1024 * 1024
}
}
/// One downloaded body → the verified, decompressed chunk in memory, for the streaming installer
/// (`stream.rs`), which writes most chunks straight into the files that use them and never puts
/// them in a cache. The checks are the bounded Java writer's (`downloadChunkStreaming`), which is
/// stricter than [`write_verified_chunk`]: a stored payload shorter than `compressedSize` is
/// truncated, a zlib stream must reach its end, the output may not grow past the chunk's window
/// and must fill it exactly when the manifest names one, and the SHA-1 must match when the
/// manifest has one. `Err` = "try another CDN", exactly like a cache write that failed.
pub fn decode_verified_chunk(
body: &[u8],
window_size: i32,
expected_sha1: Option<&[u8; 20]>,
) -> Result<Vec<u8>, String> {
let hdr = parse_chunk_header(body)?;
let start = hdr.payload_start();
if start > body.len() {
return Err("Stream ended during skip".to_string());
}
let want = if hdr.compressed_size <= 0 {
0
} else {
hdr.compressed_size as usize
};
let avail = body.len() - start;
let cap = chunk_window_bytes(window_size) as usize;
let out = if hdr.is_compressed() {
// Java stops reading once the inflater finishes, so a body may carry less than
// `compressedSize` as long as the stream itself is complete.
let payload = &body[start..start + want.min(avail)];
let mut out: Vec<u8> = Vec::with_capacity(cap + 1);
let mut inflater = flate2::Decompress::new(true);
let status = inflater
.decompress_vec(payload, &mut out, flate2::FlushDecompress::Finish)
.map_err(|e| format!("inflate: {e}"))?;
if out.len() > cap {
return Err("chunk exceeds expected window size".to_string());
}
if status != flate2::Status::StreamEnd {
return Err("truncated compressed Epic chunk".to_string());
}
out
} else {
if avail < want {
return Err("truncated Epic chunk".to_string());
}
if want > cap {
return Err("chunk exceeds expected window size".to_string());
}
body[start..start + want].to_vec()
};
if window_size > 0 && out.len() != window_size as usize {
return Err(format!(
"chunk is {} bytes, window is {window_size}",
out.len()
));
}
if let Some(expected) = expected_sha1 {
if Sha1::digest(&out).as_slice() != &expected[..] {
return Err("Chunk SHA-1 mismatch (streaming)".to_string());
}
}
Ok(out)
}
#[cfg(test)]
pub(crate) mod test_support {
use super::*;
@@ -326,6 +441,29 @@ mod tests {
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn decode_in_memory_applies_the_bounded_writer_checks() {
let data = sample_data();
let window = data.len() as i32;
let sha = sha1_of(&data);
let zipped = build_chunk_body(&data, true, 2, None);
assert_eq!(decode_verified_chunk(&zipped, window, Some(&sha)).unwrap(), data);
let stored = build_chunk_body(&data, false, 0, None);
assert_eq!(decode_verified_chunk(&stored, window, Some(&sha)).unwrap(), data);
// No window and no hash (legacy JSON manifest): accepted up to 1 MiB.
assert_eq!(decode_verified_chunk(&zipped, 0, None).unwrap(), data);
// Window mismatch, SHA-1 mismatch, truncation, oversize.
assert!(decode_verified_chunk(&zipped, window + 1, None).is_err());
let err = decode_verified_chunk(&zipped, window, Some(&[7; 20])).unwrap_err();
assert!(err.contains("SHA-1 mismatch"), "{err}");
let cut = zipped.len() - 500;
assert!(decode_verified_chunk(&zipped[..cut], window, None).is_err());
assert!(decode_verified_chunk(&stored[..stored.len() - 1], 0, None)
.unwrap_err()
.contains("truncated"));
assert!(decode_verified_chunk(&zipped, window - 1, None).is_err());
}
#[test]
fn stale_part_file_is_cleared_before_the_attempt() {
let dir = temp_dir("stale");
+262 -7
View File
@@ -9,6 +9,7 @@ use jni::{JNIEnv, JavaVM};
use super::assemble::{build_assemble_plan, run_assemble, AssembleRequest};
use super::driver::{build_plan, run_plan, EpicRequest};
use super::stream::{build_stream_plan, run_stream, Hooks, StreamProgress, StreamRequest};
#[cfg(target_os = "android")]
#[link(name = "log")]
@@ -39,6 +40,7 @@ fn android_log(message: &str) {
pub struct EpicDownloadHandle {
cancel: Arc<AtomicBool>,
worker: Option<thread::JoinHandle<()>>,
}
fn clear_pending_exception(env: &mut JNIEnv) {
@@ -289,12 +291,11 @@ pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_native
let cancel = Arc::new(AtomicBool::new(false));
let thread_cancel = Arc::clone(&cancel);
let handle = Box::new(EpicDownloadHandle { cancel });
thread::spawn(move || {
let worker = thread::spawn(move || {
crate::priority::background();
run_on_thread(vm, listener, plan, req, thread_cancel);
});
let handle = Box::new(EpicDownloadHandle { cancel, worker: Some(worker) });
Box::into_raw(handle) as jlong
}
@@ -393,9 +394,7 @@ pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_native
let cancel = Arc::new(AtomicBool::new(false));
let thread_cancel = Arc::clone(&cancel);
let handle = Box::new(EpicDownloadHandle { cancel });
thread::spawn(move || {
let worker = thread::spawn(move || {
crate::priority::background();
let progress = |bytes_done: u64, bytes_total: u64, files_done: u64, files_total: u64| {
let Ok(mut env) = vm.attach_current_thread_as_daemon() else {
@@ -422,6 +421,194 @@ pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_native
outcome.bytes_written,
);
});
let handle = Box::new(EpicDownloadHandle { cancel, worker: Some(worker) });
Box::into_raw(handle) as jlong
}
fn call_on_stream_progress(env: &mut JNIEnv, listener: &JObject, p: &StreamProgress) {
if listener.is_null() {
return;
}
let _ = env.call_method(
listener,
"onStreamProgress",
"(JJIIJ)V",
&[
JValue::Long(p.bytes_written as jlong),
JValue::Long(p.bytes_total as jlong),
JValue::Int(p.files_done.min(i32::MAX as u64) as jint),
JValue::Int(p.files_total.min(i32::MAX as u64) as jint),
JValue::Long(p.files_bytes as jlong),
],
);
clear_pending_exception(env);
}
/// `boolean onBeforeReplace()`: the Java side marks an existing install `updating`. A thrown
/// exception or `false` is a refusal.
fn call_on_before_replace(env: &mut JNIEnv, listener: &JObject) -> Result<(), String> {
if listener.is_null() {
return Ok(());
}
let result = env
.call_method(listener, "onBeforeReplace", "()Z", &[])
.and_then(|v| v.z());
let threw = env.exception_check().unwrap_or(false);
clear_pending_exception(env);
match result {
Ok(true) if !threw => Ok(()),
_ => Err("could not mark the install as updating".to_string()),
}
}
/// `EpicNative.nativeStream(...)`: the adaptive install (see `stream.rs`). Parse, plan and cache
/// preparation run on the calling thread, so a refusal returns 0 (after a synchronous
/// `onComplete(false, reason, 0)`) before anything is fetched or written, and the manager runs
/// the bounded Java writer instead. The run itself is on a new thread; `onComplete` fires after
/// every unfinished staging file of a cancelled or failed run is gone.
#[no_mangle]
pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStream(
mut env: JNIEnv,
_class: JClass,
manifest: JByteArray,
install_dir: JString,
chunk_cache_dir: JString,
staging_dir: JString,
cdn_prefixes: JObjectArray,
pending_file_idx: JIntArray,
expected_bytes: jlong,
cache_budget: jlong,
overlap_budget: jlong,
ca_bundle_path: JString,
max_workers: jint,
process_workers: jint,
listener: JObject,
) -> jlong {
if listener.is_null() {
android_log("nativeStream: null listener");
return 0;
}
let Ok(manifest_bytes) = env.convert_byte_array(manifest) else {
clear_pending_exception(&mut env);
call_on_complete(&mut env, &listener, false, "manifest bytes unreadable", 0);
return 0;
};
let install_dir = jstring_to_string(&mut env, &install_dir).unwrap_or_default();
let chunk_cache_dir = jstring_to_string(&mut env, &chunk_cache_dir).unwrap_or_default();
let staging_dir = jstring_to_string(&mut env, &staging_dir).unwrap_or_default();
let cdn_prefixes = string_array_to_vec(&mut env, &cdn_prefixes);
let pending_file_indices: Vec<usize> = int_array_to_vec(&env, &pending_file_idx)
.into_iter()
.map(|i| i.max(0) as usize)
.collect();
let ca_bundle_path = jstring_to_string(&mut env, &ca_bundle_path).unwrap_or_default();
let max_workers = max_workers.max(1) as usize;
let process_workers = process_workers.max(1) as usize;
let label = {
let base = std::path::Path::new(&install_dir)
.file_name()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default();
format!("epic app={base}")
};
log_both(
&mut env,
&listener,
&format!(
"engine=rust-stream label=\"{label}\" install_dir={install_dir} cache_dir={} cdns={} pending_files={} cache_budget={} workers={max_workers} process_workers={process_workers} manifest_bytes={}",
if chunk_cache_dir.is_empty() { "<installDir>/.chunks" } else { chunk_cache_dir.as_str() },
cdn_prefixes.len(),
pending_file_indices.len(),
cache_budget.max(0),
manifest_bytes.len()
),
);
let req = StreamRequest {
manifest_bytes,
install_dir,
chunk_cache_dir,
staging_dir,
cdn_prefixes,
pending_file_indices,
expected_bytes: if expected_bytes >= 0 {
Some(expected_bytes as u64)
} else {
None
},
cache_budget: cache_budget.max(0) as u64,
// Negative = no limit (nothing the manager knows of is being replaced).
replace_overlap_budget: if overlap_budget < 0 {
u64::MAX
} else {
overlap_budget as u64
},
ca_bundle_path,
max_workers,
process_workers,
label,
};
let plan = match build_stream_plan(&req) {
Ok(plan) => plan,
Err(err) => {
log_both(&mut env, &listener, &format!("not started: {err}"));
call_on_complete(&mut env, &listener, false, &err, 0);
return 0;
}
};
let Ok(vm) = env.get_java_vm() else {
clear_pending_exception(&mut env);
call_on_complete(&mut env, &listener, false, "JavaVM unavailable", 0);
return 0;
};
let Ok(listener) = env.new_global_ref(&listener) else {
clear_pending_exception(&mut env);
call_on_complete(&mut env, &listener, false, "listener ref failed", 0);
return 0;
};
let cancel = Arc::new(AtomicBool::new(false));
let thread_cancel = Arc::clone(&cancel);
let worker = thread::spawn(move || {
crate::priority::background();
let progress = |p: &StreamProgress| {
let Ok(mut env) = vm.attach_current_thread_as_daemon() else {
return;
};
call_on_stream_progress(&mut env, listener.as_obj(), p);
};
let before_replace = || {
let Ok(mut env) = vm.attach_current_thread_as_daemon() else {
return Err("JavaVM attach failed".to_string());
};
call_on_before_replace(&mut env, listener.as_obj())
};
let log = |line: &str| {
android_log(line);
let Ok(mut env) = vm.attach_current_thread_as_daemon() else {
return;
};
call_on_log(&mut env, listener.as_obj(), line);
};
let hooks = Hooks {
progress: &progress,
before_replace: &before_replace,
log: &log,
};
let outcome = run_stream(&plan, &req, thread_cancel.as_ref(), &hooks);
let Ok(mut env) = vm.attach_current_thread_as_daemon() else {
return;
};
call_on_complete(
&mut env,
listener.as_obj(),
outcome.success,
&outcome.error,
outcome.bytes_written,
);
});
let handle = Box::new(EpicDownloadHandle { cancel, worker: Some(worker) });
Box::into_raw(handle) as jlong
}
@@ -449,5 +636,73 @@ pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_native
if handle == 0 {
return;
}
drop(unsafe { Box::from_raw(handle as *mut EpicDownloadHandle) });
let mut handle = unsafe { Box::from_raw(handle as *mut EpicDownloadHandle) };
if let Some(worker) = handle.worker.take() { let _ = worker.join(); }
}
/// ABI v1 snapshot: selected chunks/files, verified cache lengths, old output lengths and both
/// measured free-space probes. Parse/budget failures are terminal, never fallback requests.
#[no_mangle]
pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeInstallAdaptive(
mut env: JNIEnv, _class: JClass, request: JString, listener: JObject,
) -> jlong {
if listener.is_null() { return 0; }
let parsed = jstring_to_string(&mut env, &request).ok_or("unreadable adaptive request".to_string())
.and_then(|s| super::adaptive::Request::parse(&s))
.and_then(|req| super::adaptive::choose_budget(&req).map(|budget| (req, budget)));
let (req, budget) = match parsed {
Ok(plan) => plan,
Err(error) => {
call_on_complete(&mut env, &listener, false, &error, 0);
return 0;
}
};
call_on_plan(&mut env, &listener, req.chunks.len() as u64, 0,
&format!("lookahead={} retained={} game_peak={} cache_peak={}",
budget.lookahead, budget.retained, budget.game_peak, budget.cache_peak));
let Ok(vm) = env.get_java_vm() else {
call_on_complete(&mut env, &listener, false, "JavaVM unavailable", 0);
return 0;
};
let Ok(listener) = env.new_global_ref(&listener) else {
call_on_complete(&mut env, &listener, false, "listener ref failed", 0);
return 0;
};
let cancel = Arc::new(AtomicBool::new(false));
let thread_cancel = cancel.clone();
let worker = thread::spawn(move || {
crate::priority::background();
let progress = |done, total, files, files_total| {
if let Ok(mut env) = vm.attach_current_thread_as_daemon() {
call_on_progress(&mut env, listener.as_obj(), done, total, files, files_total);
}
};
let before_swap = || -> Result<(), String> {
let mut env = vm.attach_current_thread_as_daemon().map_err(|e| e.to_string())?;
match env.call_method(listener.as_obj(), "beforeSwap", "()Z", &[]) {
Ok(value) => if value.z().unwrap_or(false) { Ok(()) } else { Err("Could not mark installed record updating before replacement".into()) },
_ => {
clear_pending_exception(&mut env);
Err("Could not mark installed record updating before replacement".into())
}
}
};
let log = |line: &str| {
android_log(line);
if let Ok(mut env) = vm.attach_current_thread_as_daemon() {
call_on_log(&mut env, listener.as_obj(), line);
}
};
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(||
super::adaptive::run(&req, budget, &thread_cancel, &before_swap, &progress, &log)))
.unwrap_or_else(|_| Err("Native Epic install panicked".into()));
let (success, error, bytes) = match outcome {
Ok(bytes) => (true, String::new(), bytes),
Err(error) => (false, error, 0),
};
if let Ok(mut env) = vm.attach_current_thread_as_daemon() {
call_on_complete(&mut env, listener.as_obj(), success, &error, bytes);
}
});
Box::into_raw(Box::new(EpicDownloadHandle { cancel, worker: Some(worker) })) as jlong
}
File diff suppressed because it is too large. Load diff
+23 -1
View File
@@ -117,6 +117,25 @@ pub fn run_fetch(
cancel: &AtomicBool,
progress: &(dyn Fn(u64, u64) + Sync),
log: &(dyn Fn(&str) + Sync),
) -> FetchOutcome {
run_fetch_gated(items, hosts, opts, sink, cancel, progress, log, &|_: usize| true)
}
/// [`run_fetch`] with an admission gate: the next fresh item (by index) is only dispatched once
/// `gate(index)` says so. Items are still taken strictly in order, and retries are never held
/// back. While the gate is closed the driver idles as it does on a full window, so a gate that
/// opens as the sink makes progress (e.g. as files finish) paces the run without stalling the
/// requests already in flight. The adaptivity is unchanged.
#[allow(clippy::too_many_arguments)]
pub fn run_fetch_gated(
items: Vec<FetchItem>,
hosts: &[String],
opts: &FetchOptions,
sink: &dyn FetchSink,
cancel: &AtomicBool,
progress: &(dyn Fn(u64, u64) + Sync),
log: &(dyn Fn(&str) + Sync),
gate: &(dyn Fn(usize) -> bool + Sync),
) -> FetchOutcome {
let started = Instant::now();
let label = opts.label.as_str();
@@ -359,6 +378,7 @@ mode={}",
Ok(rt) => {
rt.block_on(run_driver(DriverCtx {
items: items.as_slice(),
gate,
hosts: hosts.as_slice(),
opts,
label,
@@ -832,6 +852,7 @@ struct FetchDone {
struct DriverCtx<'a> {
items: &'a [FetchItem],
gate: &'a (dyn Fn(usize) -> bool + Sync),
hosts: &'a [String],
opts: &'a FetchOptions,
label: &'a str,
@@ -853,6 +874,7 @@ struct DriverCtx<'a> {
async fn run_driver(ctx: DriverCtx<'_>) {
let DriverCtx {
items,
gate,
hosts,
opts,
label,
@@ -997,7 +1019,7 @@ async fn run_driver(ctx: DriverCtx<'_>) {
let (idx, attempts) = if use_retry {
let p = *retry.front().expect("retry nonempty");
(p.idx, p.attempts)
} else if next_item < items.len() {
} else if next_item < items.len() && gate(next_item) {
(next_item, 0u32)
} else {
break;
@@ -11,6 +11,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
@@ -55,4 +56,87 @@ class StoreInstallRootTest {
assertTrue(dir.absolutePath.startsWith(app.cacheDir.absolutePath))
assertEquals("a_b_c", dir.name)
}
@Test fun estimatedBytesNeededUsesKnownInstallSizeForAllStores() {
val item = CatalogItem(Store.EPIC, "game1", "Game 1", imageUrl = null, owned = true, sizeBytes = 10_000_000L)
assertEquals(10_000_000L, StoreInstallRoot.estimatedBytesNeeded(item))
val gogItem = CatalogItem(Store.GOG, "game2", "Game 2", imageUrl = null, owned = true, sizeBytes = 10_000_000L)
assertEquals(10_000_000L, StoreInstallRoot.estimatedBytesNeeded(gogItem))
}
@Test fun checkStorageReportsEnoughWhenSpaceIsUnknownOrAvailable() {
val zeroItem = CatalogItem(Store.EPIC, "game1", "Game 1", imageUrl = null, owned = true, sizeBytes = 0L)
val check = StoreInstallRoot.checkStorage(app, zeroItem, StoreInstallRoot.internalRoot(app))
assertTrue(check.enough)
}
@Test fun availableBytesUsesNearestExistingAncestorWithoutCreatingDirectories() {
val existing = File(app.filesDir, "storage-probe").apply { mkdirs() }
val missing = File(existing, "not-created/another-level/game")
ShadowStatFs.registerStats(existing.path, 1024, 100, 100)
assertTrue(StoreInstallRoot.availableBytes(missing) > 0L)
assertFalse(File(existing, "not-created").exists())
}
@Test fun epicDefersFullSizeChecksToItsManifestDeltaForResume() {
val item = CatalogItem(Store.EPIC, "large", "Large", imageUrl = null, owned = true, sizeBytes = Long.MAX_VALUE)
val check = StoreInstallRoot.checkStorage(app, item, StoreInstallRoot.internalRoot(app))
assertTrue(check.enough)
assertEquals(Long.MAX_VALUE, check.requiredBytes)
}
@Test fun gogAndAmazonResumeChecksUseTheExistingVolumeAndDeferRemainingWork() {
val card = File(app.filesDir, "resume-card").apply { mkdirs() }
SessionPrefs.setGameStorage(app, card.path, "Card")
for (store in listOf(Store.GOG, Store.AMAZON)) {
val item = CatalogItem(store, "resume-${store.id}", "Partial ${store.id}", imageUrl = null, sizeBytes = Long.MAX_VALUE)
val folder = File(StoreInstallRoot.storeDir(File(card, "Games"), store), StoreInstallRoot.folderName(item.title, item.id)).apply { mkdirs() }
StoreInstalls.begin(folder, store, item.id, item.title, null, null)
val check = StoreInstallRoot.checkStorage(app, item, StoreInstallRoot.internalRoot(app))
assertTrue(check.enough)
assertEquals(StoreInstallRoot.availableBytes(folder), check.availableBytes)
assertEquals(folder, StoreInstallRoot.folderFor(app, store, item.id, item.title))
}
}
@Test fun freshGogAndAmazonStillRejectInsufficientCatalogSpace() {
StoreInstallRoot.internalRoot(app).mkdirs()
ShadowStatFs.registerStats(StoreInstallRoot.internalRoot(app).path, 1024, 100, 100)
for (store in listOf(Store.GOG, Store.AMAZON)) {
val item = CatalogItem(store, "too-large-${store.id}", "Too large ${store.id}", imageUrl = null, sizeBytes = Long.MAX_VALUE)
assertFalse(StoreInstallRoot.checkStorage(app, item, StoreInstallRoot.internalRoot(app)).enough)
}
}
@Test fun measuredZeroRejectsFreshGogAndAmazonRatherThanBeingUnknown() {
val root = File(app.filesDir, "zero-space").apply { mkdirs() }
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
val missing = File(root, "not-created/game")
assertEquals(0L, StoreInstallRoot.availableBytes(missing))
assertFalse(File(root, "not-created").exists())
for (store in listOf(Store.GOG, Store.AMAZON)) {
val item = CatalogItem(store, "zero-${store.id}", "Zero ${store.id}", imageUrl = null, sizeBytes = 1L)
val check = StoreInstallRoot.checkStorage(app, item, missing)
assertFalse(check.enough)
assertEquals(0L, check.availableBytes)
}
}
@Test fun failedProbesAreUnknownAndDoNotCreateOrWriteToTargets() {
val root = File(app.filesDir, "read-only-probe").apply { mkdirs() }
val missing = File(root, "not-created/game")
val fail: (File) -> Long = { throw java.io.IOException("unavailable") }
assertEquals(-1L, StoreInstallRoot.probeAvailableBytes(missing, fail, fail))
assertEquals(-1L, StoreInstallRoot.probeAvailableBytes(missing, fail, { 0L }))
assertEquals(42L, StoreInstallRoot.probeAvailableBytes(missing, fail, { 42L }))
assertEquals(0L, StoreInstallRoot.probeAvailableBytes(missing, { 0L }, fail))
assertFalse(File(root, "not-created").exists())
assertTrue(root.listFiles().orEmpty().isEmpty())
assertFalse(StoreInstallRoot.insufficientSpace(1L, -1L))
assertTrue(StoreInstallRoot.insufficientSpace(1L, 0L))
assertFalse(StoreInstallRoot.insufficientSpace(1L, 1L))
assertFalse(StoreInstallRoot.insufficientSpace(0L, 0L))
}
}
@@ -0,0 +1,43 @@
package com.droiddeck.launcher.stores.amazon
import java.io.File
import java.security.MessageDigest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
import com.droiddeck.launcher.stores.StoreInstallRoot
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class AmazonResumeSpaceTest {
@Test fun remainingWorkRejectsMeasuredZeroButNotUnknownSpace() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "amazon-zero").apply { mkdirs() }
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
val files = listOf(AmazonManifest.ManifestFile("missing", 4, 0, null))
val needed = AmazonDownloadManager.remainingInstallBytes(files, root)
assertEquals(4L, needed)
assertTrue(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.availableBytes(root)))
assertFalse(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.SPACE_UNKNOWN))
}
@Test fun remainingSpaceExcludesVerifiedFilesButIncludesCorruptAndPartialFiles() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "amazon-resume").apply { mkdirs() }
val data = byteArrayOf(1, 2, 3, 4)
val hash = MessageDigest.getInstance("SHA-256").digest(data)
val files = listOf("good", "corrupt", "partial", "missing").map {
AmazonManifest.ManifestFile(it, 4, 0, hash)
}
File(root, "good").writeBytes(data)
File(root, "corrupt").writeBytes(ByteArray(4))
File(root, "partial").writeBytes(byteArrayOf(1))
assertEquals(12L, AmazonDownloadManager.remainingInstallBytes(files, root))
for (file in files) File(root, file.unixPath()).writeBytes(data)
assertEquals(0L, AmazonDownloadManager.remainingInstallBytes(files, root))
}
}
@@ -0,0 +1,140 @@
package com.droiddeck.launcher.stores.download
import android.content.Context
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.stores.*
import java.io.File
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import org.junit.After
import org.junit.Assert.*
import org.junit.Test
import org.junit.runner.RunWith
import org.json.JSONArray
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class DownloadQueueRetryTest {
private val app get() = RuntimeEnvironment.getApplication()
@After fun cleanRows() { DownloadQueue.dismissFinished(); StoreSizes.known.clear() }
private fun awaitFailure(key: String, error: String) {
val until = System.nanoTime() + TimeUnit.SECONDS.toNanos(5)
while (System.nanoTime() < until) {
val entry = DownloadQueue.entry(key)
if (entry?.state == DownloadState.FAILED && entry.error == error) return
Thread.sleep(10)
}
fail("Expected failed real job: ${DownloadQueue.entry(key)}")
}
@Test fun storageRetryRechecksThenRunsTheRealBackendAndNormalRetryStillWorks() {
StoreInstallRoot.internalRoot(app).mkdirs()
ShadowStatFs.registerStats(StoreInstallRoot.internalRoot(app).path, 1024, 100, 100)
val item = CatalogItem(Store.GOG, "preflight-retry", "Preflight retry", imageUrl = null, sizeBytes = Long.MAX_VALUE)
val runs = AtomicInteger()
var lastTarget: File? = null
val previous = StoresState.backend(Store.GOG)
val backend = object : StoreBackend {
override val store = Store.GOG
override fun signIn(context: Context) {}
override fun signOut(context: Context) {}
override fun syncLibrary(context: Context, force: Boolean) {}
override fun loadShelves(context: Context, force: Boolean) {}
override fun uninstall(context: Context, game: InstalledStoreGame) {}
override fun install(context: Context, item: CatalogItem, root: File) {
lastTarget = root
DownloadQueue.enqueue(context, DownloadEntry(item.store, item.id, item.title)) {
object : DownloadQueue.DownloadJob {
override fun run(handle: DownloadQueue.JobHandle): String? {
throw IllegalStateException("backend-${runs.incrementAndGet()}")
}
override fun cancel(deleteFiles: Boolean) {}
}
}
}
}
StoresState.register(backend)
try {
StoresState.install(app, item)
assertEquals(DownloadState.FAILED, DownloadQueue.entry(item.key)?.state)
DownloadQueue.retry(app, item.key)
assertEquals(DownloadState.FAILED, DownloadQueue.entry(item.key)?.state)
assertEquals(0, runs.get())
StoreSizes.known[item.key] = 1
DownloadQueue.retry(app, item.key)
awaitFailure(item.key, "backend-1")
DownloadQueue.retry(app, item.key)
awaitFailure(item.key, "backend-2")
DownloadQueue.clear(app, item.key)
assertNull(DownloadQueue.entry(item.key))
// A card resume (including the restored paused path) must retain its real target.
val card = File(app.filesDir, "retry-card").apply { mkdirs() }
SessionPrefs.setGameStorage(app, card.path, "Card")
val target = File(card, "Games")
val folder = File(StoreInstallRoot.storeDir(target, item.store), StoreInstallRoot.folderName(item.title, item.id)).apply { mkdirs() }
StoreInstalls.begin(folder, item.store, item.id, item.title, null, null)
StoreSizes.known[item.key] = Long.MAX_VALUE
StoresState.unfinished = StoreInstallRoot.unfinished(app)
DownloadQueue.holdPaused(item.key)
StoresState.requestInstall(app, item)
assertEquals(target, lastTarget)
assertEquals(DownloadState.PAUSED, DownloadQueue.entry(item.key)?.state)
assertEquals(2, runs.get())
val saved = File(app.filesDir, "store-queue.json")
val until = System.nanoTime() + TimeUnit.SECONDS.toNanos(5)
var correctSavedRoot = false
while (!correctSavedRoot && System.nanoTime() < until) {
correctSavedRoot = runCatching {
val rows = JSONArray(saved.readText())
(0 until rows.length()).any { i ->
val row = rows.getJSONObject(i)
row.getJSONObject("item").getString("id") == item.id &&
row.getString("root") == target.absolutePath && row.getBoolean("paused")
}
}.getOrDefault(false)
if (!correctSavedRoot) Thread.sleep(10)
}
assertTrue("Paused saved queue must remember the existing card root", correctSavedRoot)
DownloadQueue.clear(app, item.key)
} finally {
if (previous != null) StoresState.register(previous)
SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "")
StoresState.unfinished = emptyList()
}
}
@Test fun preflightClearRemovesTheRowWithoutRunningItsRetry() {
val entry = DownloadEntry(Store.AMAZON, "clear-preflight", "Clear")
DownloadQueue.recordFailed(app, entry, "storage") { fail("Clear must not retry") }
DownloadQueue.clear(app, entry.key)
assertNull(DownloadQueue.entry(entry.key))
}
@Test fun heldPausedBackendRestoresAndResumesWithoutAPlaceholder() {
val entry = DownloadEntry(Store.GOG, "restored-paused", "Paused")
val ran = CountDownLatch(1)
DownloadQueue.holdPaused(entry.key)
DownloadQueue.enqueue(app, entry) {
object : DownloadQueue.DownloadJob {
override fun run(handle: DownloadQueue.JobHandle): String? {
ran.countDown()
throw IllegalStateException("restored")
}
override fun cancel(deleteFiles: Boolean) {}
}
}
assertEquals(DownloadState.PAUSED, DownloadQueue.entry(entry.key)?.state)
assertEquals(1L, ran.count)
DownloadQueue.resume(app, entry.key)
assertTrue(ran.await(5, TimeUnit.SECONDS))
awaitFailure(entry.key, "restored")
}
}
@@ -0,0 +1,107 @@
package com.droiddeck.launcher.stores.epic
import java.io.File
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicAdaptivePlanTest {
@get:Rule val temp = TemporaryFolder()
private val mib = 1024L * 1024
private val gib = 1024 * mib
private fun file(name: String, size: Long) = EpicDownloadManager.FileInfo().apply {
filename = name
var left = size
while (left > 0) {
val part = minOf(left, Int.MAX_VALUE.toLong())
parts.add(EpicDownloadManager.ChunkPart().apply { this.size = part.toInt() })
left -= part
}
}
@Test fun netPeakCreditsEachOldCopyAtItsSwapAndLargestReplacedIgnoresFreshFiles() {
val files = listOf(file("a", 10 * gib), file("b", 2 * gib), file("c", 4 * gib))
val old = mapOf("a" to 9 * gib, "c" to 5 * gib)
// Running Σ(new − old): +1, +3, +2 GiB.
assertEquals(3 * gib, EpicDownloadManager.settledNetPeak(files, old))
assertEquals(10 * gib, EpicDownloadManager.largestReplacedFile(files, old))
assertEquals(16 * gib, EpicDownloadManager.settledNetPeak(files, emptyMap()))
assertEquals(0L, EpicDownloadManager.largestReplacedFile(files, emptyMap()))
assertEquals(0L, EpicDownloadManager.largestReplacedFile(files, mapOf("b" to 0L)))
}
@Test fun cacheBudgetIsHalfTheHeadroomCappedAndDefaultsWhenSpaceIsUnknown() {
assertEquals(EpicDownloadManager.ADAPTIVE_CACHE_DEFAULT_BYTES, EpicDownloadManager.adaptiveCacheBudget(-1, 0))
assertEquals(EpicDownloadManager.ADAPTIVE_CACHE_MAX_BYTES, EpicDownloadManager.adaptiveCacheBudget(100 * gib, 10 * gib))
assertEquals(0L, EpicDownloadManager.adaptiveCacheBudget(gib, gib))
val free = 1000 * mib
assertEquals((free - 400 * mib - EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES) / 2, EpicDownloadManager.adaptiveCacheBudget(free, 400 * mib))
}
@Test fun freshInstallsHaveNoOverlapLimit() {
val plan = EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, 30 * gib, true, 10 * gib)
assertNull(plan.error)
assertEquals(-1L, plan.overlapBudget)
assertEquals(20 * gib, plan.gameNeed)
}
@Test fun updateOverlapBudgetIsTheLeftoverAfterNetGrowthAndCache() {
// Card install, cache on internal: the card's leftover after the net growth is the budget.
val roomy = EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, 20 * gib, true, 10 * gib)
assertNull(roomy.error)
assertFalse(roomy.cacheBesideGame)
assertEquals(19 * gib, roomy.overlapBudget)
assertEquals(20 * gib, roomy.gameNeed)
// Tight: floored at the largest replaced file, one at a time.
val tight = EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, 5 * gib, true, 10 * gib)
assertNull(tight.error)
assertEquals(4 * gib, tight.overlapBudget)
// Not even one replaced file fits: the shortfall is named.
val short = EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, 4 * gib, true, 10 * gib)
assertTrue(short.error!!.contains("game's storage"))
assertTrue(short.error!!.contains("short by 1.0 GB"))
// Unknown free space: one replaced file at a time.
assertEquals(4 * gib, EpicDownloadManager.planAdaptiveSpace(gib, 4 * gib, -1, true, -1).overlapBudget)
}
@Test fun cacheBesideTheGameComesOutOfTheOverlapBudget() {
val plan = EpicDownloadManager.planAdaptiveSpace(gib, 2 * gib, 10 * gib, false, -1)
assertNull(plan.error)
assertTrue(plan.cacheBesideGame)
assertTrue(plan.cacheBudget > 0)
assertEquals(10 * gib - gib - plan.cacheBudget - EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES, plan.overlapBudget)
assertEquals(10 * gib, plan.gameNeed)
}
@Test fun fullInternalStorageMovesTheCacheBesideTheGameInsteadOfFailing() {
val plan = EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, 30 * gib, true, 100 * mib)
assertNull(plan.error)
assertTrue(plan.cacheBesideGame)
assertEquals(0L, plan.scratchNeed)
assertTrue(plan.cacheBudget > 0)
assertEquals(20 * gib + plan.cacheBudget + EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES, plan.gameNeed)
}
@Test fun ownedStagingIsRemovedOnlyWhenItHoldsJustItsMarker() {
val root = temp.newFolder("game")
val staging = EpicDownloadManager.recoverOwnedStaging(root, emptyList())
File(staging, "keep.stage").writeText("x")
EpicDownloadManager.releaseOwnedStaging(root)
assertTrue(File(staging, "owner").exists())
File(staging, "keep.stage").delete()
EpicDownloadManager.releaseOwnedStaging(root)
assertFalse(staging.exists())
// A directory without the marker is not ours.
staging.mkdir()
File(staging, "owner").writeText("someone else")
EpicDownloadManager.releaseOwnedStaging(root)
assertTrue(staging.exists())
}
}
@@ -0,0 +1,646 @@
package com.droiddeck.launcher.stores.epic
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreInstalls
import java.io.File
import java.net.InetAddress
import java.net.ServerSocket
import java.net.SocketException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.security.MessageDigest
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.net.Socket
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicBoundedInstallTest {
@get:Rule val temp = TemporaryFolder()
private lateinit var server: ServerSocket
private lateinit var worker: Thread
private lateinit var manifest: EpicDownloadManager.Manifest
private lateinit var root: File
private val requests = AtomicInteger()
private val reject = AtomicBoolean()
private val rejectGuid = AtomicInteger()
private val handlers = Executors.newCachedThreadPool()
private val sockets = ConcurrentHashMap.newKeySet<Socket>()
private var requestBarrier: CountDownLatch? = null
private var deliveryGate: CountDownLatch? = null
private var bytes = ByteArray(256 * 1024) { (it % 251).toByte() }
@Before fun setUp() {
root = temp.newFolder("game")
ShadowStatFs.registerStats(root.path, 1_000_000_000, 1_000_000_000, 1_000_000_000)
server = ServerSocket(0, 8, InetAddress.getByName("127.0.0.1"))
worker = Thread {
while (!server.isClosed) {
val socket = try { server.accept() } catch (e: SocketException) {
if (server.isClosed) break else throw e
}
sockets.add(socket)
handlers.submit {
try {
socket.use {
it.soTimeout = 5000
val reader = it.getInputStream().bufferedReader()
val request = reader.readLine()
while (!reader.readLine().isNullOrEmpty()) { }
requests.incrementAndGet()
requestBarrier?.let { barrier ->
barrier.countDown()
check(barrier.await(5, TimeUnit.SECONDS)) { "Requests did not overlap" }
}
val out = it.getOutputStream()
val rejectedGuid = rejectGuid.get()
if (reject.get() || (rejectedGuid != 0 && request.contains("_%08X".format(rejectedGuid)))) {
out.write("HTTP/1.1 503 Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray())
} else {
val header = ByteBuffer.allocate(41).order(ByteOrder.LITTLE_ENDIAN)
.putInt(0xB1FE3AA2.toInt()).putInt(1).putInt(41).putInt(bytes.size)
.put(ByteArray(24)).put(0).array()
out.write("HTTP/1.1 200 OK\r\nContent-Length: ${header.size + bytes.size}\r\nConnection: close\r\n\r\n".toByteArray())
out.write(header)
out.flush()
deliveryGate?.await(5, TimeUnit.SECONDS)
out.write(bytes)
}
out.flush()
}
} catch (_: java.io.IOException) {
// Cancellation disconnects an owned request while this fixture serves it.
} finally { sockets.remove(socket) }
}
}
}.apply { start() }
val chunk = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(1, 0, 0, 0)
windowSize = bytes.size
fileSize = bytes.size.toLong()
sha1 = sha(bytes)
}
manifest = EpicDownloadManager.Manifest().apply {
chunkDir = "chunks"
uniqueChunks = arrayListOf(chunk)
cdnUrls = listOf(EpicDownloadManager.CdnUrl("http://127.0.0.1:${server.localPort}", "", ""))
files = arrayListOf(gameFile("game.bin", 0, bytes.size / 2), gameFile("game.bin.part", bytes.size / 2, bytes.size / 2))
}
}
@After fun tearDown() {
deliveryGate?.countDown()
server.close()
worker.join(6000)
sockets.forEach { it.close() }
handlers.shutdownNow()
assertTrue(handlers.awaitTermination(6, TimeUnit.SECONDS))
assertFalse(worker.isAlive)
}
private fun sha(data: ByteArray) = MessageDigest.getInstance("SHA-1").digest(data)
private fun gameFile(name: String, offset: Int, size: Int) = EpicDownloadManager.FileInfo().apply {
filename = name
sha1 = sha(bytes.copyOfRange(offset, offset + size))
parts.add(EpicDownloadManager.ChunkPart().apply {
guid = intArrayOf(1, 0, 0, 0)
this.offset = offset
this.size = size
})
}
private fun install(cancel: AtomicBoolean = AtomicBoolean(), callback: EpicDownloadManager.Callback =
EpicDownloadManager.Callback { _, _ -> }, cache: String = "") =
EpicDownloadManager.installManifest(manifest, root.path, cache, null, cancel, callback)
private fun oldInstall() {
File(root, "game.bin").writeBytes(byteArrayOf(9, 8, 7))
File(root, "game.bin.part").writeBytes(byteArrayOf(6, 5, 4))
StoreGameSidecar(Store.EPIC, "game", "Game", "game.bin", installVersion = "old").write(root)
}
private fun assertOldInstallIntact() {
assertArrayEquals(byteArrayOf(9, 8, 7), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
assertEquals("old", StoreGameSidecar.read(root)?.installVersion)
assertTrue(StoreGameSidecar.read(root)?.isInstalled == true)
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun networkFailureAfterAcceptedPreflightPreservesInstalledFilesAndRecord() {
oldInstall()
reject.set(true)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(requests.get() > 0)
assertOldInstallIntact()
}
@Test fun hashFailureAfterAcceptedPreflightPreservesInstalledFilesAndRecord() {
oldInstall()
manifest.files.first().sha1 = ByteArray(20) { 1 }
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertOldInstallIntact()
}
@Test fun cancellationWhileStagingPreservesInstalledFilesAndRecord() {
oldInstall()
val cancel = AtomicBoolean()
assertNull(install(cancel, EpicDownloadManager.Callback { message, _ ->
if (message.startsWith("Downloading chunks (")) cancel.set(true)
}))
assertOldInstallIntact()
}
@Test fun laterFailureRetainsVerifiedSwapsAndMarksMixedUpdateResumable() {
oldInstall()
manifest.files.last().sha1 = ByteArray(20) { 1 }
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
val record = StoreGameSidecar.read(root)!!
assertEquals(StoreGameSidecar.STATE_UPDATING, record.state)
assertFalse(record.isInstalled)
assertEquals("", record.installVersion)
StoreInstalls.begin(root, Store.EPIC, "game", "Game", null, null)
assertEquals("game.bin", StoreGameSidecar.read(root)?.exe)
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
}
@Test fun laterNetworkFailurePreservesTheUnreplacedOldFile() {
oldInstall()
val secondChunk = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(2, 0, 0, 0)
windowSize = bytes.size
fileSize = bytes.size.toLong()
sha1 = sha(bytes)
}
manifest.uniqueChunks.add(secondChunk)
manifest.files.last().parts.single().guid = secondChunk.guid
// Prefetch may fail early, but replacement is still ordered by the writer.
rejectGuid.set(2)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun cancellationAfterFirstSwapLeavesAResumableMixedUpdate() {
oldInstall()
val cancel = AtomicBoolean()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "stream" && items == 1) cancel.set(true)
}
}
assertNull(install(cancel, callback))
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(byteArrayOf(6, 5, 4), File(root, "game.bin.part").readBytes())
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(bytes.size / 2, bytes.size), File(root, "game.bin.part").readBytes())
// Registration, not an individual file swap, restores the installed state.
assertFalse(StoreGameSidecar.read(root)!!.isInstalled)
}
@Test fun atomicRenameFailurePreservesDestinationAndCleansStagingWithoutDeleteFallback() {
val destination = File(root, "game.bin").apply { mkdir() }
File(destination, "old-content").writeText("keep")
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("atomically replace"))
assertEquals("keep", File(destination, "old-content").readText())
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun safeStagingSpaceBoundaryRejectsWithoutReclaimingAllOldOutputs() {
// The first staged half-file must coexist with the shared chunk and BOTH old outputs.
val old = ByteArray(bytes.size / 2) { 9 }
File(root, "game.bin").writeBytes(old)
File(root, "game.bin.part").writeBytes(old)
val peak = bytes.size + old.size
ShadowStatFs.registerStats(root.path, 1024, peak / 4096 - 1, peak / 4096 - 1)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals(0, requests.get())
assertArrayEquals(old, File(root, "game.bin").readBytes())
assertArrayEquals(old, File(root, "game.bin.part").readBytes())
ShadowStatFs.registerStats(root.path, 1024, peak / 4096, peak / 4096)
assertNotNull(install())
}
@Test fun zeroSpaceRejectsPendingFilesWhileUnknownSpaceIsNotTreatedAsZero() {
oldInstall()
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertOldInstallIntact()
assertEquals(0, requests.get())
assertTrue(com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(1, 0))
assertFalse(com.droiddeck.launcher.stores.StoreInstallRoot.insufficientSpace(1, -1))
}
@Test fun zeroScratchSpaceRejectsBeforeFetchWithoutTouchingInstalledOutputs() {
oldInstall()
val cache = temp.newFolder("full-scratch")
ShadowStatFs.registerStats(cache.path, 1024, 0, 0)
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install(cache = cache.path) }
assertTrue(error.message.orEmpty().contains("Not enough internal space"))
assertOldInstallIntact()
assertEquals(0, requests.get())
}
@Test fun installsSharedChunksAndPreservesLegitimatePartFilename() {
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(bytes.copyOfRange(bytes.size / 2, bytes.size), File(root, "game.bin.part").readBytes())
assertEquals(1, requests.get())
assertFalse(File(root, ".chunks").exists())
assertNotNull(install())
assertEquals("Verified files must not be downloaded again", 1, requests.get())
}
private fun oversizedUpdate() {
val file = manifest.files[0]
val part = file.parts[0]
file.parts.clear()
repeat(100_000) {
file.parts.add(EpicDownloadManager.ChunkPart().apply {
guid = part.guid
size = Int.MAX_VALUE
})
}
}
@Test fun insufficientInstallSpacePreservesExistingFilesOnBothCacheLayouts() {
val oldGame = byteArrayOf(9, 8, 7)
val oldPart = byteArrayOf(6, 5, 4)
File(root, "game.bin").writeBytes(oldGame)
File(root, "game.bin.part").writeBytes(oldPart)
oversizedUpdate()
assertTrue(manifest.files[0].fileSize() > root.usableSpace)
for (cache in listOf("", temp.newFolder("scratch-rejected").path)) {
if (cache.isNotEmpty()) ShadowStatFs.registerStats(cache, 1_000_000_000, 1_000_000_000, 1_000_000_000)
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install(cache = cache) }
assertTrue(error.message.orEmpty().contains("Not enough free space"))
assertArrayEquals(oldGame, File(root, "game.bin").readBytes())
assertArrayEquals(oldPart, File(root, "game.bin.part").readBytes())
}
assertEquals(0, requests.get())
}
@Test fun acceptedUpdateReplacesOutdatedFilesWithVerifiedOutputs() {
oldInstall()
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
assertArrayEquals(bytes.copyOfRange(bytes.size / 2, bytes.size), File(root, "game.bin.part").readBytes())
assertEquals(StoreGameSidecar.STATE_UPDATING, StoreGameSidecar.read(root)?.state)
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun pauseKeepsCompletedFilesAndResumeReusesSharedCache() {
val cancel = AtomicBoolean()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "stream" && items == 1) cancel.set(true)
}
}
assertNull(install(cancel, callback))
assertTrue(File(root, "game.bin").exists())
assertFalse(File(root, "game.bin.part").exists())
assertEquals(1, requests.get())
assertNotNull(install())
assertEquals(1, requests.get())
}
@Test fun cancellationDuringChunkDeliveryRemovesIncompleteOutput() {
val cancel = AtomicBoolean()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {
if (message.startsWith("Downloading chunks (")) cancel.set(true)
}
}
assertNull(install(cancel, callback))
assertFalse(File(root, "game.bin").exists())
assertNotNull(install())
assertArrayEquals(bytes.copyOfRange(0, bytes.size / 2), File(root, "game.bin").readBytes())
}
@Test fun fileHashFailureIsExplicitAndRetryRepairsIt() {
val expected = manifest.files[0].sha1
manifest.files[0].sha1 = ByteArray(20) { 1 }
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("SHA-1 mismatch"))
assertFalse(File(root, "game.bin").exists())
manifest.files[0].sha1 = expected
assertNotNull(install())
}
@Test fun corruptCacheIsRefetchedAndScratchCacheIsRemovedOnSuccess() {
val cache = temp.newFolder("scratch")
ShadowStatFs.registerStats(cache.path, 1_000_000_000, 1_000_000_000, 1_000_000_000)
File(cache, manifest.uniqueChunks[0].guidStr()).writeBytes(ByteArray(bytes.size))
assertNotNull(install(cache = cache.path))
assertEquals(1, requests.get())
assertFalse(cache.exists())
}
@Test fun networkFailureLeavesNoCompletedFileAndCanBeRetried() {
reject.set(true)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertFalse(File(root, "game.bin").exists())
reject.set(false)
assertNotNull(install())
}
@Test fun boundedProgressNeverReentersDownloadAfterEnteringInstall() {
val events = mutableListOf<String>()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {
if (message.startsWith("Downloading chunks")) events.add("download")
if (message.startsWith("Writing")) events.add("install")
}
override fun onBytes(done: Long, total: Long, speedBps: Long) { events.add("download") }
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "install") {
assertEquals(total, done)
assertEquals(itemsTotal, items)
events.add("install")
}
}
}
assertNotNull(install(callback = callback))
val firstInstall = events.indexOf("install")
assertTrue(firstInstall > 0)
assertFalse(events.drop(firstInstall).contains("download"))
}
@Test fun sharedChunksDoNotCompleteProgressBeforeTheRemainingFilesAreWritten() {
val samples = mutableListOf<Pair<Long, Long>>()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onBytes(done: Long, total: Long, speedBps: Long) { samples.add(done to total) }
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
if (stage == "stream" && items < itemsTotal) assertTrue(samples.last().first < samples.last().second)
}
}
assertNotNull(install(callback = callback))
assertEquals(listOf(0L, bytes.size / 2L, bytes.size.toLong()), samples.map { it.first })
assertTrue(samples.all { it.second == bytes.size.toLong() })
}
@Test fun oversizedSharedChunkStaysPinnedAcrossPrefetchedFilesWithoutChangingStages() {
bytes = ByteArray((EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 1024).toInt()) { (it % 251).toByte() }
manifest.uniqueChunks.single().apply {
windowSize = bytes.size
fileSize = bytes.size.toLong()
sha1 = sha(bytes)
}
manifest.files = (0..2).map { gameFile("part-$it.bin", it * 1024, 1024) }
val chunk = manifest.uniqueChunks.single()
val estimatedPeak = EpicDownloadManager.estimatePeakAdditionalBytes(
manifest.files,
mapOf(chunk.guidStr() to chunk),
emptyMap(),
true,
)
assertEquals(EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 4 * 1024L, estimatedPeak)
val samples = mutableListOf<Long>()
val stages = mutableListOf<String>()
val callback = object : EpicDownloadManager.Callback {
override fun onProgress(message: String, pct: Int) {}
override fun onBytes(done: Long, total: Long, speedBps: Long) {
assertEquals(3072L, total)
assertFalse(stages.contains("install"))
samples.add(done)
}
override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) {
stages.add(stage)
if (stage == "stream" && items < itemsTotal) assertTrue(samples.last() < total)
}
}
assertNotNull(install(callback = callback))
assertEquals(1, requests.get())
assertEquals(listOf(0L, 1024L, 2048L, 3072L), samples)
assertEquals("install", stages.last())
repeat(3) { assertArrayEquals(bytes.copyOfRange(it * 1024, (it + 1) * 1024), File(root, "part-$it.bin").readBytes()) }
}
@Test fun hogwartsSizedSequentialPlanFitsAvailableSpaceWithoutFullChunkCache() {
val gib = 1024L * 1024 * 1024
val file = EpicDownloadManager.FileInfo()
val chunks = LinkedHashMap<String, EpicDownloadManager.ChunkInfo>()
repeat(72) { index ->
val chunk = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(index + 1, 0, 0, 0)
windowSize = gib.toInt()
}
chunks[chunk.guidStr()] = chunk
file.parts.add(EpicDownloadManager.ChunkPart().apply {
guid = chunk.guid
size = chunk.windowSize
})
}
val peak = EpicDownloadManager.estimatePeakAdditionalBytes(listOf(file), chunks, emptyMap(), true)
assertEquals(73 * gib, peak)
assertTrue(peak < 96 * gib)
}
@Test fun plannersReclaimEachOldFileOnlyAfterItsOwnVerifiedSwap() {
val chunks = manifest.uniqueChunks.associateBy { it.guidStr() }
val half = bytes.size / 2L
val old = mapOf("game.bin" to half, "game.bin.part" to 100 * half)
assertEquals(bytes.size + half, EpicDownloadManager.estimatePeakAdditionalBytes(manifest.files, chunks, emptyMap(), true, old))
assertEquals(half, EpicDownloadManager.estimateStagedFilePeak(manifest.files, old))
assertEquals(2 * half, EpicDownloadManager.estimateStagedFilePeak(manifest.files, emptyMap()))
assertEquals(bytes.size.toLong(), EpicDownloadManager.estimatePeakAdditionalBytes(manifest.files, chunks, emptyMap(), false, old))
}
private fun distinctFiles(count: Int) {
val template = manifest.uniqueChunks.single()
manifest.uniqueChunks = (1..count).map { id ->
EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(id, 0, 0, 0)
windowSize = template.windowSize
fileSize = template.fileSize
sha1 = template.sha1
}
}
manifest.files = manifest.uniqueChunks.mapIndexed { index, chunk ->
gameFile("file-$index.bin", 0, bytes.size).apply { parts.single().guid = chunk.guid }
}
}
@Test fun prefetchOverlapsEightSinglePartFilesButNeverFetchesTheWholeManifest() {
distinctFiles(20)
requestBarrier = CountDownLatch(8)
val cancel = AtomicBoolean()
var sawWindow = false
assertNull(install(cancel, EpicDownloadManager.Callback { message, _ ->
if (message.startsWith("Downloading chunks (")) {
assertEquals(8, requests.get())
val cacheBytes = File(root, ".chunks").listFiles()!!.sumOf { it.length() }
assertTrue(cacheBytes <= 8L * bytes.size)
sawWindow = true
cancel.set(true)
}
}))
assertTrue("An eight-request barrier cannot pass with a sequential downloader", sawWindow)
assertEquals(8, requests.get())
assertTrue(root.walk().none { it.name.endsWith(".stage") || it.name.endsWith(".part") })
}
@Test fun parallelWindowSpaceThresholdIsCheckedBeforeStartingAnyRequest() {
distinctFiles(20)
val cache = temp.newFolder("parallel-scratch")
val peak = EpicDownloadManager.estimatePeakAdditionalBytes(
manifest.files, manifest.uniqueChunks.associateBy { it.guidStr() }, emptyMap(), false,
)
assertEquals(8L * bytes.size, peak)
ShadowStatFs.registerStats(cache.path, 1024, peak.toInt() / 4096 - 1, peak.toInt() / 4096 - 1)
assertThrows(EpicDownloadManager.InstallException::class.java) { install(cache = cache.path) }
assertEquals(0, requests.get())
ShadowStatFs.registerStats(cache.path, 1024, peak.toInt() / 4096, peak.toInt() / 4096)
requestBarrier = CountDownLatch(8)
assertNotNull(install(cache = cache.path))
assertEquals(20, requests.get())
manifest.files.forEach { assertArrayEquals(bytes, File(root, it.filename).readBytes()) }
}
@Test fun cancellationDuringParallelNetworkReadsJoinsWorkersBeforeReturning() {
distinctFiles(20)
requestBarrier = CountDownLatch(8)
deliveryGate = CountDownLatch(1)
val cancel = AtomicBoolean()
val outcome = java.util.concurrent.atomic.AtomicReference<Any?>()
val installer = Thread {
try { outcome.set(install(cancel)) } catch (e: Throwable) { outcome.set(e) }
}.apply { start() }
try {
assertTrue(requestBarrier!!.await(5, TimeUnit.SECONDS))
cancel.set(true)
installer.join(10_000)
assertFalse("Installer must disconnect and join all download workers", installer.isAlive)
assertNull(outcome.get())
val cache = File(root, ".chunks")
val snapshot = cache.listFiles()?.associate { it.name to it.length() }.orEmpty()
deliveryGate!!.countDown()
Thread.sleep(150)
assertEquals(snapshot, cache.listFiles()?.associate { it.name to it.length() }.orEmpty())
assertTrue(snapshot.keys.none { it.endsWith(".part") })
assertTrue(root.walk().none { it.name.endsWith(".stage") })
assertEquals(8, requests.get())
} finally {
cancel.set(true)
deliveryGate!!.countDown()
installer.join(10_000)
}
}
@Test fun failedParallelFetchJoinsOtherNetworkReadersBeforeReturning() {
distinctFiles(20)
requestBarrier = CountDownLatch(8)
deliveryGate = CountDownLatch(1)
rejectGuid.set(1)
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals(8, requests.get())
val cache = File(root, ".chunks")
val snapshot = cache.listFiles()?.associate { it.name to it.length() }.orEmpty()
deliveryGate!!.countDown()
Thread.sleep(150)
assertEquals(snapshot, cache.listFiles()?.associate { it.name to it.length() }.orEmpty())
assertTrue(snapshot.keys.none { it.endsWith(".part") })
assertTrue(root.walk().none { it.name.endsWith(".stage") })
}
@Test fun ownedOrphanStagingIsRecoveredBeforeTheSpaceProbeWithoutTouchingManifestFiles() {
val staging = EpicDownloadManager.recoverOwnedStaging(root, manifest.files)
File(staging, "native-123-0.stage").writeBytes(ByteArray(bytes.size))
val legitimate = File(root, ".droiddeck-epic-old.stage").apply { writeText("keep") }
oldInstall()
val peak = 2 * bytes.size
ShadowStatFs.registerStats(root.path, 1024, peak / 4096, peak / 4096)
assertNotNull(install())
assertEquals("keep", legitimate.readText())
// The orphan was recovered, and a finished install removes the then-empty owned namespace.
assertFalse(staging.exists())
}
@Test fun stagingRecoveryRefusesUnownedAndManifestOwnedNamespaces() {
val staging = File(root, EpicDownloadManager.STAGING_DIRECTORY).apply { mkdir() }
val untouched = File(staging, "legitimate.stage").apply { writeText("keep") }
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals("keep", untouched.readText())
File(staging, "owner").writeText("DroidDeck Epic staging v1\n")
manifest.files.first().filename = "${EpicDownloadManager.STAGING_DIRECTORY}/legitimate.stage"
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals("keep", untouched.readText())
assertEquals(0, requests.get())
}
@Test fun loadedNativeAbiFailureIsExplicitAndNeverFallsBackToJavaDownloads() {
val field = com.droiddeck.launcher.stores.StoresNative::class.java.getDeclaredField("loaded")
field.isAccessible = true
field.set(null, true)
try {
oldInstall()
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("Native Epic install failed"))
assertEquals(0, requests.get())
assertOldInstallIntact()
} finally {
com.droiddeck.launcher.stores.StoresNative.forget()
}
}
@Test fun stagingRecoveryRejectsSymlinkNamespaceOwnerAndEntriesWithoutDeletingOutsideData() {
val outside = temp.newFolder("outside-staging")
val externalStage = File(outside, "keep.stage").apply { writeText("keep") }
val externalOwner = File(outside, "owner").apply { writeText("DroidDeck Epic staging v1\n") }
val staging = File(root, EpicDownloadManager.STAGING_DIRECTORY)
try {
java.nio.file.Files.createSymbolicLink(staging.toPath(), outside.toPath())
} catch (e: java.nio.file.FileSystemException) {
org.junit.Assume.assumeNoException("Host does not permit symbolic links", e)
}
try {
val error = assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertTrue(error.message.orEmpty().contains("Unsafe Epic staging"))
assertEquals("keep", externalStage.readText())
java.nio.file.Files.delete(staging.toPath())
staging.mkdir()
java.nio.file.Files.createSymbolicLink(File(staging, "owner").toPath(), externalOwner.toPath())
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
java.nio.file.Files.delete(File(staging, "owner").toPath())
File(staging, "owner").writeText("DroidDeck Epic staging v1\n")
val localStage = File(staging, "local.stage").apply { writeText("local") }
java.nio.file.Files.createSymbolicLink(File(staging, "linked.stage").toPath(), externalStage.toPath())
assertThrows(EpicDownloadManager.InstallException::class.java) { install() }
assertEquals("local", localStage.readText())
assertEquals("keep", externalStage.readText())
assertEquals(0, requests.get())
} finally {
if (java.nio.file.Files.isSymbolicLink(staging.toPath())) java.nio.file.Files.delete(staging.toPath())
for (name in listOf("owner", "linked.stage")) {
val entry = File(staging, name).toPath()
if (java.nio.file.Files.isSymbolicLink(entry)) java.nio.file.Files.delete(entry)
}
}
}
}
@@ -1,8 +1,10 @@
package com.droiddeck.launcher.stores.epic
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -11,6 +13,140 @@ import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class EpicJsonManifestTest {
@Test
fun boundedInstallPeakReservesParallelIncomingChunksBeforeWriting() {
val first = chunk(1)
val second = chunk(2)
val file = file(part(1, 0, 100), part(2, 0, 100))
val chunks = mapOf(first.guidStr() to first, second.guidStr() to second)
assertEquals(300L, EpicDownloadManager.estimatePeakAdditionalBytes(listOf(file), chunks, emptyMap(), true))
assertEquals(200L, EpicDownloadManager.estimatePeakAdditionalBytes(listOf(file), chunks, emptyMap(), false))
}
@Test
fun sharedCachedChunkIsReclaimedOnlyAfterItsLastPart() {
val shared = chunk(1)
val files = listOf(file(part(1, 0, 60)), file(part(1, 60, 40)))
assertEquals(
100L,
EpicDownloadManager.estimatePeakAdditionalBytes(
files, mapOf(shared.guidStr() to shared), mapOf(shared.guidStr() to 100L), true,
),
)
assertEquals(
200L,
EpicDownloadManager.estimatePeakAdditionalBytes(
files, mapOf(shared.guidStr() to shared), emptyMap(), true,
),
)
}
@Test
fun sharedChunksWithinTheWindowStayPinnedWithinTheCacheCeiling() {
val first = chunk(1).apply { windowSize = 40 * 1024 * 1024 }
val second = chunk(2).apply { windowSize = 40 * 1024 * 1024 }
val files = listOf(
file(part(1, 0, 40 * 1024 * 1024)),
file(part(2, 0, 40 * 1024 * 1024)),
file(part(1, 0, 40 * 1024 * 1024)),
file(part(2, 0, 40 * 1024 * 1024)),
)
val chunks = mapOf(first.guidStr() to first, second.guidStr() to second)
val gamePeak = EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), true)
val cachePeak = EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), false)
assertEquals(200L * 1024 * 1024, gamePeak)
assertTrue(cachePeak <= EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 40L * 1024 * 1024)
}
@Test fun scheduleDeduplicatesWindowAndEvictsOnlyUnpinnedReusableChunks() {
val size = 40L * 1024 * 1024
val chunks = (1..12).map { chunk(it).apply { windowSize = size.toInt() } }
.associateBy { it.guidStr() }
val files = (1..12).map { file(part(it, 0, 1)) } + (1..12).map { file(part(it, 0, 1)) }
val schedule = EpicDownloadManager.PrefetchSchedule(files, chunks, emptyMap())
assertEquals(chunks.keys.take(8), schedule.fill())
assertEquals(emptyList<String>(), schedule.fill())
assertEquals(8 * size, schedule.bytes())
assertEquals(emptyList<String>(), schedule.consume())
assertEquals(listOf(chunks.keys.elementAt(8)), schedule.fill())
assertEquals(9 * size, schedule.bytes())
assertEquals(listOf(chunks.keys.first()), schedule.consume())
var refetchedFirst = false
repeat(22) {
val fetched = schedule.fill()
if (chunks.keys.first() in fetched) refetchedFirst = true
assertTrue(schedule.bytes() <= EpicDownloadManager.MAX_RETAINED_CHUNK_CACHE_BYTES + 8 * size)
val released = schedule.consume()
assertTrue(released.none { it in schedule.window() })
}
assertTrue(refetchedFirst)
assertEquals(0L, schedule.bytes())
assertEquals(9 * size, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), false))
}
@Test fun cachedAndOldOutputBytesAreCreditedOnlyWhenTheScheduleReleasesThem() {
val chunks = (1..8).map { chunk(it) }.associateBy { it.guidStr() }
val files = (1..8).map { id -> file(part(id, 0, 100)).apply { filename = "file-$id" } }
val existing = chunks.keys.associateWith { 100L }
val old = files.associate { it.filename to 100L }
assertEquals(800L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), false))
assertEquals(0L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, existing, false))
assertEquals(900L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, emptyMap(), true, old))
assertEquals(100L, EpicDownloadManager.estimatePeakAdditionalBytes(files, chunks, existing, true, old))
}
@Test
fun resumeCacheDropsCorruptAndUnneededEntriesButKeepsVerifiedChunks() {
val dir = java.io.File(System.getProperty("user.dir"), "build/epic-cache-test-${System.nanoTime()}")
assertTrue(dir.mkdirs())
try {
val bytes = ByteArray(100) { (it % 251).toByte() }
val expectedSha = java.security.MessageDigest.getInstance("SHA-1").digest(bytes)
val valid = chunk(1).apply { sha1 = expectedSha }
val corrupt = chunk(2).apply { sha1 = expectedSha }
java.io.File(dir, valid.guidStr()).writeBytes(bytes)
java.io.File(dir, corrupt.guidStr()).writeBytes(ByteArray(100) { 1 })
java.io.File(dir, "orphan").writeBytes(bytes)
val retained = EpicDownloadManager.prepareBoundedCache(dir, listOf(valid, corrupt))
assertEquals(mapOf(valid.guidStr() to 100L), retained)
assertTrue(java.io.File(dir, valid.guidStr()).exists())
assertFalse(java.io.File(dir, corrupt.guidStr()).exists())
assertFalse(java.io.File(dir, "orphan").exists())
} finally {
EpicDownloadManager.deleteDir(dir)
}
}
@Test
fun referenceCountsIncludeRepeatedPartsSoCancelledFilesCanBeReplanned() {
val pending = listOf(file(part(1, 0, 20), part(1, 20, 20)), file(part(1, 40, 60)))
assertEquals(3, EpicDownloadManager.chunkReferences(pending)["00000001000000000000000000000000"])
}
private fun chunk(id: Int) = EpicDownloadManager.ChunkInfo().apply {
guid = intArrayOf(id, 0, 0, 0)
windowSize = 100
fileSize = 100
}
private fun part(id: Int, offset: Int, size: Int) = EpicDownloadManager.ChunkPart().apply {
guid = intArrayOf(id, 0, 0, 0)
this.offset = offset
this.size = size
}
private fun file(vararg parts: EpicDownloadManager.ChunkPart) = EpicDownloadManager.FileInfo().apply {
filename = "test.bin"
this.parts.addAll(parts)
}
@Test
fun blobNumbersAreDecimalBytesLeastSignificantFirst() {
assertEquals(1L, EpicDownloadManager.blobToNum("001000000000"))
@@ -0,0 +1,43 @@
package com.droiddeck.launcher.stores.gog
import java.io.File
import java.security.MessageDigest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowStatFs
import com.droiddeck.launcher.stores.StoreInstallRoot
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33])
class GogResumeSpaceTest {
@Test fun remainingWorkRejectsMeasuredZeroButNotUnknownSpace() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "gog-zero").apply { mkdirs() }
ShadowStatFs.registerStats(root.path, 1024, 0, 0)
val files = listOf(GogDownloadManager.DepotFile("missing").apply { totalSize = 4 })
val needed = GogDownloadManager.remainingInstallBytes(files, root)
assertEquals(4L, needed)
assertTrue(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.availableBytes(root)))
assertFalse(StoreInstallRoot.insufficientSpace(needed, StoreInstallRoot.SPACE_UNKNOWN))
}
@Test fun remainingSpaceExcludesVerifiedFilesButIncludesCorruptAndPartialFiles() {
val root = File(RuntimeEnvironment.getApplication().filesDir, "gog-resume").apply { mkdirs() }
val data = byteArrayOf(1, 2, 3, 4)
val hash = MessageDigest.getInstance("MD5").digest(data).joinToString("") { "%02x".format(it) }
val files = listOf("good", "corrupt", "partial", "missing").map {
GogDownloadManager.DepotFile(it).apply { totalSize = 4; md5 = hash }
}
File(root, "good").writeBytes(data)
File(root, "corrupt").writeBytes(ByteArray(4))
File(root, "partial").writeBytes(byteArrayOf(1))
assertEquals(12L, GogDownloadManager.remainingInstallBytes(files, root))
for (file in files) File(root, file.relativePath).writeBytes(data)
assertEquals(0L, GogDownloadManager.remainingInstallBytes(files, root))
}
}