mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10a66b2298 | ||
|
|
509c0482bf |
No files matched your search
@@ -87,6 +87,14 @@ 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), "batch" (the older whole-cache native fetch, then native
|
||||
* assembly; needs about twice the game's size free) or "java" (the bounded one-chunk-at-a-time
|
||||
* Java writer). No UI; set it in the `session` preferences for benchmarks.
|
||||
*/
|
||||
fun storesEpicMode(context: Context): String = prefs(context).getString("storesEpicMode", "adaptive") ?: "adaptive"
|
||||
|
||||
/** Which tab a signed-in store opens on when its chip is picked: [STORES_OPEN_LIBRARY] or [STORES_OPEN_STORE]. */
|
||||
fun storesOpenTab(context: Context): String =
|
||||
prefs(context).getString("storesOpenTab", STORES_OPEN_LIBRARY)?.takeIf { it == STORES_OPEN_STORE } ?: STORES_OPEN_LIBRARY
|
||||
|
||||
@@ -6,6 +6,8 @@ import android.util.Log;
|
||||
import com.droiddeck.launcher.stores.StoreLog;
|
||||
import com.droiddeck.launcher.stores.StoreInstallRoot;
|
||||
import com.droiddeck.launcher.stores.StoreInstalls;
|
||||
import com.droiddeck.launcher.stores.StoresNative;
|
||||
import com.droiddeck.launcher.stores.download.StoreDownloadTier;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
@@ -32,15 +34,19 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
/**
|
||||
* The Epic install pipeline, ported from Bannerlator's EpicDownloadManager: the manifest API's CDN
|
||||
* list, the manifest binary (ChunksV4, or the older JSON form), the files narrowed to the base
|
||||
* set plus the device's language, a delta pass that skips files already on disk with the right
|
||||
* size and SHA-1, then the bounded-cache chunk-to-file writer. The native batch engine remains
|
||||
* available for other callers, but this install path intentionally uses the same serial writer
|
||||
* whether or not native code is installed: a whole-game chunk cache can exceed available space.
|
||||
* size and SHA-1, then the writer. When the native library loads, the writer is the adaptive
|
||||
* native install ({@link EpicNative#stream}: chunks fetched in parallel and written straight into
|
||||
* staged files, shared chunks in a budgeted cache); otherwise, or when its space plan does not
|
||||
* fit, the bounded-cache Java writer, one chunk at a time. Both replace each file the same safe
|
||||
* way. A hidden preference ({@code storesEpicMode}) can pick the older whole-cache native batch
|
||||
* engine or the Java writer instead, for side-by-side benchmarks.
|
||||
*
|
||||
* Chunk URLs carry no auth tokens; the subfolder is the decimal group number; the Cloudflare
|
||||
* mirror is skipped (it refuses chunks). Blocking; the download queue runs it on its own thread.
|
||||
@@ -51,6 +57,37 @@ public final class EpicDownloadManager {
|
||||
private static final String UA = EpicAuthClient.USER_AGENT;
|
||||
/** Keep reusable chunk data bounded even when a manifest shares chunks across distant files. */
|
||||
static final long MAX_RETAINED_CHUNK_CACHE_BYTES = 64L * 1024 * 1024;
|
||||
/** Most the adaptive engine caches of shared chunks; more only saves the occasional refetch. */
|
||||
static final long ADAPTIVE_CACHE_MAX_BYTES = 1024L * 1024 * 1024;
|
||||
/** Its budget when free space cannot be read. */
|
||||
static final long ADAPTIVE_CACHE_DEFAULT_BYTES = 256L * 1024 * 1024;
|
||||
/**
|
||||
* Below this, internal scratch is too small to be worth a cache of its own: the shared chunks
|
||||
* then go beside the game (still released at their last use) rather than failing the install.
|
||||
*/
|
||||
static final long ADAPTIVE_CACHE_MIN_SCRATCH_BYTES = MAX_RETAINED_CHUNK_CACHE_BYTES;
|
||||
/** Chunks being decoded, written or released at any moment, beyond the budget itself. */
|
||||
static final long ADAPTIVE_TRANSIENT_BYTES = 64L * 1024 * 1024;
|
||||
/**
|
||||
* Files the fetch window has open beyond the sequential plan: the parallel writer can finish
|
||||
* files a little out of order, so replaced files near the front of the window may not have
|
||||
* released their old copies yet.
|
||||
*/
|
||||
static final long ADAPTIVE_OPEN_WINDOW_BYTES = 256L * 1024 * 1024;
|
||||
|
||||
static final String MODE_ADAPTIVE = "adaptive";
|
||||
static final String MODE_BATCH = "batch";
|
||||
static final String MODE_JAVA = "java";
|
||||
|
||||
/** How {@link #install} may use the native engine; null = the bounded Java writer only. */
|
||||
static final class NativeOptions {
|
||||
final String mode;
|
||||
final int maxWorkers, processWorkers;
|
||||
final String tierId;
|
||||
NativeOptions(String mode, int maxWorkers, int processWorkers, String tierId) {
|
||||
this.mode = mode; this.maxWorkers = maxWorkers; this.processWorkers = processWorkers; this.tierId = tierId;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Callback {
|
||||
void onProgress(String message, int pct);
|
||||
@@ -114,6 +151,8 @@ public final class EpicDownloadManager {
|
||||
/** The prerequisite fields were read (an empty name and path then mean the build names none). */
|
||||
public boolean prereqRead = false;
|
||||
public List<CdnUrl> cdnUrls = new ArrayList<>();
|
||||
/** The manifest as downloaded, for the native engines (which parse it themselves); null = Java only. */
|
||||
byte[] rawBytes = null;
|
||||
}
|
||||
|
||||
/** What an install leaves for the sidecar. */
|
||||
@@ -154,8 +193,12 @@ public final class EpicDownloadManager {
|
||||
Manifest manifest = parseManifest(manifestBytes);
|
||||
if (manifest == null) throw new InstallException("The manifest could not be parsed");
|
||||
manifest.cdnUrls = cdnUrls;
|
||||
manifest.rawBytes = manifestBytes;
|
||||
cb.onLog("epic: chunkDir=" + manifest.chunkDir + " chunks=" + manifest.uniqueChunks.size() + " files=" + manifest.files.size() + " exe=" + manifest.launchExe);
|
||||
return installManifest(manifest, installDirPath, chunkCacheDirPath, installTags, cancel, cb);
|
||||
StoreDownloadTier tier = StoreDownloadTier.Companion.current(ctx);
|
||||
String mode = com.droiddeck.launcher.session.SessionPrefs.INSTANCE.storesEpicMode(ctx);
|
||||
NativeOptions options = new NativeOptions(mode, Math.max(1, Math.min(128, tier.getNetworkWindow())), Math.max(2, tier.getProcessWorkers()), tier.getId());
|
||||
return installManifest(manifest, installDirPath, chunkCacheDirPath, installTags, cancel, cb, options);
|
||||
} catch (InstallException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
@@ -166,6 +209,12 @@ public final class EpicDownloadManager {
|
||||
|
||||
static Result installManifest(Manifest manifest, String installDirPath, String chunkCacheDirPath,
|
||||
List<String> installTags, AtomicBoolean cancel, Callback cb) throws InstallException {
|
||||
return installManifest(manifest, installDirPath, chunkCacheDirPath, installTags, cancel, cb, null);
|
||||
}
|
||||
|
||||
static Result installManifest(Manifest manifest, String installDirPath, String chunkCacheDirPath,
|
||||
List<String> installTags, AtomicBoolean cancel, Callback cb,
|
||||
NativeOptions nativeOptions) throws InstallException {
|
||||
try {
|
||||
List<CdnUrl> cdnUrls = manifest.cdnUrls;
|
||||
File installDir = new File(installDirPath);
|
||||
@@ -207,7 +256,6 @@ public final class EpicDownloadManager {
|
||||
Map<String, ChunkInfo> chunksByGuid = new LinkedHashMap<>();
|
||||
for (ChunkInfo c : needed) chunksByGuid.put(c.guidStr(), c);
|
||||
if (!chunkCacheDir.isDirectory() && !chunkCacheDir.mkdirs()) throw new InstallException("Could not create Epic chunk cache");
|
||||
Map<String, Long> cachedChunkBytes = prepareBoundedCache(chunkCacheDir, needed);
|
||||
long pendingBytes = 0;
|
||||
Map<String, Long> oldFileBytes = new LinkedHashMap<>();
|
||||
for (FileInfo f : pending) {
|
||||
@@ -215,6 +263,15 @@ public final class EpicDownloadManager {
|
||||
File output = new File(installDir, f.filename.replace("\\", "/"));
|
||||
if (output.isFile()) oldFileBytes.put(f.filename, output.length());
|
||||
}
|
||||
if (nativeOptions != null && !MODE_JAVA.equals(nativeOptions.mode)) {
|
||||
NativeAttempt attempt = MODE_BATCH.equals(nativeOptions.mode)
|
||||
? installBatch(manifest, pending, needed, pendingBytes, planned, installDir, cachePath, chunkCacheDir, nativeOptions, cancel, cb)
|
||||
: installAdaptive(manifest, pending, pendingBytes, oldFileBytes, planned, installDir, cachePath, chunkCacheDir, nativeOptions, cancel, cb);
|
||||
if (attempt.handled) return attempt.result;
|
||||
} else if (nativeOptions != null) {
|
||||
cb.onLog("epic: mode=java-bounded chosen by storesEpicMode");
|
||||
}
|
||||
Map<String, Long> cachedChunkBytes = prepareBoundedCache(chunkCacheDir, needed);
|
||||
boolean cacheOnInstallVolume = cachePath.isEmpty();
|
||||
long peakGameAdditional = cacheOnInstallVolume
|
||||
? estimatePeakAdditionalBytes(pending, chunksByGuid, cachedChunkBytes, true, oldFileBytes)
|
||||
@@ -311,6 +368,8 @@ public final class EpicDownloadManager {
|
||||
cb.onStage("stream", writtenBytes, pendingBytes, doneFiles, totalFiles);
|
||||
}
|
||||
deleteDir(chunkCacheDir);
|
||||
long boundedMs = Math.max(1, (System.nanoTime() - transferStarted) / 1_000_000);
|
||||
cb.onLog("epic: summary mode=java-bounded files=" + doneFiles + " bytes_written=" + writtenBytes + " elapsed_ms=" + boundedMs + " avg_MBps=" + String.format(java.util.Locale.ROOT, "%.2f", writtenBytes * 1000.0 / boundedMs / 1048576.0));
|
||||
cb.onStage("install", writtenBytes, pendingBytes, doneFiles, totalFiles);
|
||||
cb.onProgress("Complete", 100);
|
||||
return new Result(manifest.launchExe, manifest.buildVersion, planned).withPrereq(manifest);
|
||||
@@ -322,6 +381,272 @@ public final class EpicDownloadManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** What a native attempt did: {@code handled} = the install ended there ({@code result} null when cancelled). */
|
||||
private static final class NativeAttempt {
|
||||
final boolean handled;
|
||||
final Result result;
|
||||
private NativeAttempt(boolean handled, Result result) { this.handled = handled; this.result = result; }
|
||||
static final NativeAttempt FALLBACK = new NativeAttempt(false, null);
|
||||
static NativeAttempt done(Result result) { return new NativeAttempt(true, result); }
|
||||
}
|
||||
|
||||
/** The pending files as indices into the manifest's file list, what the native runs take; null when one is not in it. */
|
||||
static int[] pendingIndices(Manifest manifest, List<FileInfo> pending) {
|
||||
java.util.IdentityHashMap<FileInfo, Integer> index = new java.util.IdentityHashMap<>();
|
||||
for (int i = 0; i < manifest.files.size(); i++) index.put(manifest.files.get(i), i);
|
||||
int[] pendingIdx = new int[pending.size()];
|
||||
for (int i = 0; i < pending.size(); i++) {
|
||||
Integer k = index.get(pending.get(i));
|
||||
if (k == null) return null;
|
||||
pendingIdx[i] = k;
|
||||
}
|
||||
return pendingIdx;
|
||||
}
|
||||
|
||||
private static String[] cdnPrefixes(List<CdnUrl> cdnUrls) {
|
||||
String[] prefixes = new String[cdnUrls.size()];
|
||||
for (int i = 0; i < cdnUrls.size(); i++) prefixes[i] = cdnUrls.get(i).baseUrl + cdnUrls.get(i).cloudDir;
|
||||
return prefixes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where the adaptive engine's shared-chunk cache goes and how big it may get, and what each
|
||||
* volume must hold. A card install keeps the cache on internal scratch (each byte hits the
|
||||
* card once); when internal storage cannot hold a useful cache, the cache goes beside the game
|
||||
* instead of failing. {@code error} names the storage that is short, and by how much.
|
||||
*/
|
||||
static final class AdaptiveSpacePlan {
|
||||
boolean cacheBesideGame;
|
||||
long cacheBudget;
|
||||
long gameNeed;
|
||||
long scratchNeed;
|
||||
String error;
|
||||
}
|
||||
|
||||
/** The part of free space the shared cache may use, after {@code need}: half the headroom, capped. */
|
||||
static long adaptiveCacheBudget(long free, long need) {
|
||||
if (free < 0) return ADAPTIVE_CACHE_DEFAULT_BYTES;
|
||||
long headroom = free - need - ADAPTIVE_TRANSIENT_BYTES;
|
||||
if (headroom <= 0) return 0;
|
||||
return Math.min(ADAPTIVE_CACHE_MAX_BYTES, headroom / 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Space the game volume needs beyond the sequential staged plan: none for a fresh install (a
|
||||
* staged file becomes the file itself); for replaced files, the largest old copy that may not
|
||||
* be released yet plus what the fetch window spans, never more than all old copies together.
|
||||
*/
|
||||
static long adaptiveOpenStagingAllowance(Map<String, Long> oldFileBytes) {
|
||||
long total = 0, largest = 0;
|
||||
for (long bytes : oldFileBytes.values()) { total += bytes; largest = Math.max(largest, bytes); }
|
||||
return Math.min(total, largest + ADAPTIVE_OPEN_WINDOW_BYTES);
|
||||
}
|
||||
|
||||
static AdaptiveSpacePlan planAdaptiveSpace(long stagedPeak, long openAllowance, long freeGame, boolean scratchRequested, long freeScratch) {
|
||||
AdaptiveSpacePlan plan = new AdaptiveSpacePlan();
|
||||
long files = stagedPeak + openAllowance;
|
||||
long scratchBudget = scratchRequested ? adaptiveCacheBudget(freeScratch, 0) : 0;
|
||||
plan.cacheBesideGame = !scratchRequested || (freeScratch >= 0 && scratchBudget < ADAPTIVE_CACHE_MIN_SCRATCH_BYTES);
|
||||
if (plan.cacheBesideGame) {
|
||||
plan.cacheBudget = adaptiveCacheBudget(freeGame, files);
|
||||
plan.gameNeed = files + plan.cacheBudget + ADAPTIVE_TRANSIENT_BYTES;
|
||||
plan.scratchNeed = 0;
|
||||
} else {
|
||||
plan.cacheBudget = scratchBudget;
|
||||
plan.gameNeed = files;
|
||||
plan.scratchNeed = plan.cacheBudget + ADAPTIVE_TRANSIENT_BYTES;
|
||||
}
|
||||
if (StoreInstallRoot.insufficientSpace(plan.gameNeed, freeGame)) {
|
||||
plan.error = "Not enough free space on the game's storage: need " + fmt(plan.gameNeed)
|
||||
+ (plan.cacheBesideGame && scratchRequested ? " (download cache moved beside the game: internal storage is full)" : "")
|
||||
+ ", only " + fmt(freeGame) + " free (short by " + fmt(plan.gameNeed - freeGame) + ")";
|
||||
} else if (!plan.cacheBesideGame && StoreInstallRoot.insufficientSpace(plan.scratchNeed, freeScratch)) {
|
||||
plan.error = "Not enough internal space for the download cache: need " + fmt(plan.scratchNeed) + ", only " + fmt(freeScratch)
|
||||
+ " free (short by " + fmt(plan.scratchNeed - freeScratch) + ")";
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
/**
|
||||
* The adaptive native install. Falls back (nothing fetched or written) when the engine is not
|
||||
* there, refuses the plan, or its space plan does not fit while the bounded writer's might.
|
||||
*/
|
||||
private static NativeAttempt installAdaptive(Manifest manifest, List<FileInfo> pending, long pendingBytes, Map<String, Long> oldFileBytes,
|
||||
long planned, File installDir, String cachePath, File chunkCacheDir, NativeOptions options,
|
||||
AtomicBoolean cancel, Callback cb) throws InstallException {
|
||||
int[] pendingIdx = pendingIndices(manifest, pending);
|
||||
if (manifest.rawBytes == null || pendingIdx == null) return NativeAttempt.FALLBACK;
|
||||
if (!StoresNative.INSTANCE.getAvailable()) {
|
||||
cb.onLog("epic: native engine unavailable; mode=java-bounded");
|
||||
return NativeAttempt.FALLBACK;
|
||||
}
|
||||
long stagedPeak = estimateStagedFilePeak(pending, oldFileBytes);
|
||||
long openAllowance = adaptiveOpenStagingAllowance(oldFileBytes);
|
||||
long freeGame = StoreInstallRoot.availableBytes(installDir);
|
||||
boolean scratchRequested = !cachePath.isEmpty();
|
||||
long freeScratch = scratchRequested ? StoreInstallRoot.availableBytes(chunkCacheDir) : -1;
|
||||
AdaptiveSpacePlan space = planAdaptiveSpace(stagedPeak, openAllowance, freeGame, scratchRequested, freeScratch);
|
||||
if (space.error != null) {
|
||||
// The bounded writer needs less (one chunk at a time, sequential swaps): let it decide.
|
||||
cb.onLog("epic: adaptive space plan does not fit (" + space.error + "); mode=java-bounded");
|
||||
return NativeAttempt.FALLBACK;
|
||||
}
|
||||
String engineCache = space.cacheBesideGame ? "" : cachePath;
|
||||
File engineCacheDir = engineCache.isEmpty() ? new File(installDir, ".chunks") : chunkCacheDir;
|
||||
if (space.cacheBesideGame && scratchRequested) {
|
||||
cb.onLog("epic: internal storage cannot hold the shared-chunk cache (" + fmt(freeScratch) + " free); cache beside the game");
|
||||
}
|
||||
if (cancel.get()) return NativeAttempt.done(null);
|
||||
cb.onLog("epic: mode=adaptive tier=" + options.tierId + " workers=" + options.maxWorkers + " process_workers=" + options.processWorkers
|
||||
+ " cache=" + (engineCache.isEmpty() ? "beside the game" : "scratch") + " budget=" + fmt(space.cacheBudget)
|
||||
+ " game need=" + fmt(space.gameNeed) + " of " + fmt(freeGame) + (space.cacheBesideGame ? "" : ", scratch need=" + fmt(space.scratchNeed) + " of " + fmt(freeScratch)));
|
||||
cb.onSizes(pendingBytes, planned);
|
||||
cb.onBytes(0, pendingBytes, 0);
|
||||
final int totalFiles = pending.size();
|
||||
final File fInstallDir = installDir;
|
||||
final Object lock = new Object();
|
||||
final long started = System.nanoTime();
|
||||
final long[] state = new long[] {0, 0, System.currentTimeMillis(), 0, 0}; // bytes, files, speed ms, speed bytes, speed bps
|
||||
EpicNative.Result res = EpicNative.stream(manifest.rawBytes, installDir.getPath(), engineCache, cdnPrefixes(manifest.cdnUrls), pendingIdx, pendingBytes,
|
||||
space.cacheBudget, "", options.maxWorkers, options.processWorkers, cancel, new EpicNative.StreamListener() {
|
||||
@Override public void onStreamProgress(long bytesWritten, long bytesTotal, int filesDone, int filesTotal, long filesBytes) {
|
||||
synchronized (lock) {
|
||||
if (bytesWritten < state[0] && filesDone <= state[1]) return; // a late, older sample
|
||||
state[0] = Math.max(state[0], bytesWritten);
|
||||
long nowMs = System.currentTimeMillis();
|
||||
if (nowMs - state[2] >= 500) {
|
||||
state[4] = (state[0] - state[3]) * 1000L / Math.max(1, nowMs - state[2]);
|
||||
state[2] = nowMs;
|
||||
state[3] = state[0];
|
||||
}
|
||||
cb.onBytes(state[0], pendingBytes, state[4]);
|
||||
cb.onProgress("Downloading chunks (" + filesDone + "/" + totalFiles + " files)" + speedSuffix(state[4]),
|
||||
(int) (state[0] * 80L / Math.max(1, pendingBytes)));
|
||||
if (filesDone > state[1]) {
|
||||
state[1] = filesDone;
|
||||
cb.onStage("stream", filesBytes, pendingBytes, filesDone, totalFiles);
|
||||
}
|
||||
}
|
||||
}
|
||||
@Override public void onLog(String line) { cb.onLog(line); }
|
||||
@Override public boolean onBeforeReplace() {
|
||||
// Once a swap can mix versions, the old installed record is no longer truthful.
|
||||
try {
|
||||
StoreInstalls.markUpdating(fInstallDir);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
cb.onLog("epic: could not mark the install as updating: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@Override public void onComplete(boolean success, String error, long bytes) {}
|
||||
});
|
||||
if (!res.started) {
|
||||
cb.onLog("epic: adaptive engine not started (" + res.error + "); mode=java-bounded");
|
||||
return NativeAttempt.FALLBACK;
|
||||
}
|
||||
long elapsedMs = Math.max(1, (System.nanoTime() - started) / 1_000_000);
|
||||
if (res.cancelled) return NativeAttempt.done(null);
|
||||
if (!res.success) throw new InstallException(res.error == null || res.error.isEmpty() ? "Epic download failed" : res.error);
|
||||
deleteDir(engineCacheDir);
|
||||
deleteDir(chunkCacheDir);
|
||||
cb.onLog("epic: summary mode=adaptive files=" + totalFiles + " bytes_written=" + pendingBytes + " elapsed_ms=" + elapsedMs
|
||||
+ " avg_MBps=" + String.format(java.util.Locale.ROOT, "%.2f", pendingBytes * 1000.0 / elapsedMs / 1048576.0));
|
||||
cb.onStage("install", pendingBytes, pendingBytes, totalFiles, totalFiles);
|
||||
cb.onProgress("Complete", 100);
|
||||
return NativeAttempt.done(new Result(manifest.launchExe, manifest.buildVersion, planned).withPrereq(manifest));
|
||||
}
|
||||
|
||||
/**
|
||||
* The older native batch install, kept for A/B benchmarks: every chunk fetched into the cache
|
||||
* in parallel, then the files assembled from it. It needs the whole cache and the files at
|
||||
* once (about twice the game), and it writes files in place rather than staged, so an
|
||||
* existing install is marked updating before anything is fetched.
|
||||
*/
|
||||
private static NativeAttempt installBatch(Manifest manifest, List<FileInfo> pending, List<ChunkInfo> needed, long pendingBytes,
|
||||
long planned, File installDir, String cachePath, File chunkCacheDir, NativeOptions options,
|
||||
AtomicBoolean cancel, Callback cb) throws InstallException {
|
||||
int[] pendingIdx = pendingIndices(manifest, pending);
|
||||
if (manifest.rawBytes == null || pendingIdx == null) return NativeAttempt.FALLBACK;
|
||||
if (!StoresNative.INSTANCE.getAvailable()) {
|
||||
cb.onLog("epic: native engine unavailable; mode=java-bounded");
|
||||
return NativeAttempt.FALLBACK;
|
||||
}
|
||||
long cacheMissing = 0, totalBytes = 0;
|
||||
for (ChunkInfo c : needed) {
|
||||
if (!new File(chunkCacheDir, c.guidStr()).isFile()) cacheMissing += chunkWindowSize(c);
|
||||
totalBytes += Math.max(c.fileSize, 1);
|
||||
}
|
||||
long freeGame = StoreInstallRoot.availableBytes(installDir);
|
||||
if (cachePath.isEmpty()) {
|
||||
long need = cacheMissing + pendingBytes;
|
||||
if (StoreInstallRoot.insufficientSpace(need, freeGame)) throw new InstallException("Not enough free space for the batch Epic download: need " + fmt(need) + " (" + fmt(pendingBytes) + " of game files + " + fmt(cacheMissing) + " of download cache), only " + fmt(freeGame) + " free (short by " + fmt(need - freeGame) + ")");
|
||||
} else {
|
||||
long freeCache = StoreInstallRoot.availableBytes(chunkCacheDir);
|
||||
if (StoreInstallRoot.insufficientSpace(cacheMissing, freeCache)) throw new InstallException("Not enough internal space for the batch download cache: need " + fmt(cacheMissing) + ", only " + fmt(freeCache) + " free (short by " + fmt(cacheMissing - freeCache) + ")");
|
||||
if (StoreInstallRoot.insufficientSpace(pendingBytes, freeGame)) throw new InstallException("Not enough free space on the game's storage: need " + fmt(pendingBytes) + ", only " + fmt(freeGame) + " free (short by " + fmt(pendingBytes - freeGame) + ")");
|
||||
}
|
||||
if (cancel.get()) return NativeAttempt.done(null);
|
||||
final long fTotal = totalBytes;
|
||||
final int totalChunks = needed.size();
|
||||
final int totalFiles = pending.size();
|
||||
cb.onLog("epic: mode=batch tier=" + options.tierId + " workers=" + options.maxWorkers + " process_workers=" + options.processWorkers + (cachePath.isEmpty() ? "" : " cache=scratch") + " cache_missing=" + fmt(cacheMissing));
|
||||
cb.onSizes(pendingBytes, planned);
|
||||
final AtomicLong lastMs = new AtomicLong(System.currentTimeMillis()), lastBytes = new AtomicLong(), speed = new AtomicLong();
|
||||
final long started = System.nanoTime();
|
||||
EpicNative.Result fetch = EpicNative.run(manifest.rawBytes, installDir.getPath(), cachePath, cdnPrefixes(manifest.cdnUrls), pendingIdx, totalChunks, fTotal, "",
|
||||
options.maxWorkers, options.processWorkers, cancel, new EpicNative.Listener() {
|
||||
@Override public void onPlan(int chunksTotal, long bytesTotal, String chunkDir) { cb.onLog("epic: plan chunks=" + chunksTotal + " bytes=" + bytesTotal + " dir=" + chunkDir); }
|
||||
@Override public void onProgress(long bytesDone, long bytesTotal, int chunksDone, int chunksTotal) {
|
||||
sampleSpeed(bytesDone, lastMs, lastBytes, speed);
|
||||
cb.onProgress("Downloading chunks (" + chunksDone + "/" + totalChunks + ")" + speedSuffix(speed.get()), (int) (bytesDone * 80L / Math.max(1, fTotal)));
|
||||
cb.onBytes(bytesDone, fTotal, speed.get());
|
||||
}
|
||||
@Override public void onLog(String line) { cb.onLog(line); }
|
||||
@Override public void onComplete(boolean success, String error, long bytes) {}
|
||||
});
|
||||
if (!fetch.started) {
|
||||
cb.onLog("epic: batch engine not started (" + fetch.error + "); mode=java-bounded");
|
||||
return NativeAttempt.FALLBACK;
|
||||
}
|
||||
if (fetch.cancelled) return NativeAttempt.done(null);
|
||||
if (!fetch.success) throw new InstallException("Epic batch download failed: " + fetch.error);
|
||||
// Assembly overwrites files in place: the old installed record stops being truthful now.
|
||||
try { StoreInstalls.markUpdating(installDir); } catch (Exception e) { throw new InstallException("Could not mark the install as updating: " + e.getMessage()); }
|
||||
EpicNative.Result asm = EpicNative.assemble(manifest.rawBytes, installDir.getPath(), cachePath, pendingIdx, cancel, new EpicNative.Listener() {
|
||||
@Override public void onPlan(int chunksTotal, long bytesTotal, String chunkDir) {}
|
||||
@Override public void onProgress(long bytesDone, long bytesTotal, int done, int total) {
|
||||
cb.onStage("stream", bytesDone, bytesTotal, done, Math.max(total, totalFiles));
|
||||
cb.onProgress("Writing files (" + done + "/" + Math.max(total, totalFiles) + ")", 80 + (int) (done * 20L / Math.max(1, Math.max(total, totalFiles))));
|
||||
}
|
||||
@Override public void onLog(String line) { cb.onLog(line); }
|
||||
@Override public void onComplete(boolean success, String error, long bytes) {}
|
||||
});
|
||||
if (asm.cancelled) return NativeAttempt.done(null);
|
||||
if (!asm.started || !asm.success) throw new InstallException(asm.error == null || asm.error.isEmpty() ? "Epic batch assembly failed" : asm.error);
|
||||
long elapsedMs = Math.max(1, (System.nanoTime() - started) / 1_000_000);
|
||||
cb.onLog("epic: summary mode=batch files=" + totalFiles + " bytes_written=" + pendingBytes + " elapsed_ms=" + elapsedMs
|
||||
+ " avg_MBps=" + String.format(java.util.Locale.ROOT, "%.2f", pendingBytes * 1000.0 / elapsedMs / 1048576.0));
|
||||
deleteDir(chunkCacheDir);
|
||||
cb.onStage("install", pendingBytes, pendingBytes, totalFiles, totalFiles);
|
||||
cb.onProgress("Complete", 100);
|
||||
return NativeAttempt.done(new Result(manifest.launchExe, manifest.buildVersion, planned).withPrereq(manifest));
|
||||
}
|
||||
|
||||
private static void sampleSpeed(long done, AtomicLong lastSpeedMs, AtomicLong lastSpeedBytes, AtomicLong speedBps) {
|
||||
long nowMs = System.currentTimeMillis(), prevMs = lastSpeedMs.get();
|
||||
if (nowMs - prevMs >= 500 && lastSpeedMs.compareAndSet(prevMs, nowMs)) {
|
||||
long prevB = lastSpeedBytes.getAndSet(done);
|
||||
long dt = nowMs - prevMs;
|
||||
if (dt > 0) speedBps.set((done - prevB) * 1000L / dt);
|
||||
}
|
||||
}
|
||||
|
||||
private static String speedSuffix(long bps) {
|
||||
if (bps <= 0) return "";
|
||||
return " " + (bps >= 1048576 ? String.format(java.util.Locale.ROOT, "%.1f MB/s", bps / 1048576.0) : (bps / 1024) + " KB/s");
|
||||
}
|
||||
|
||||
private static File createStagingFile(File output, Set<String> reservedPaths) throws IOException {
|
||||
File parent = output.getParentFile();
|
||||
if (!parent.isDirectory() && !parent.mkdirs()) throw new IOException("Could not create output directory");
|
||||
|
||||
@@ -9,18 +9,20 @@ import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
/**
|
||||
* The native batch APIs for Epic in `libdroiddeckstores.so`: [run] fills the chunk cache with
|
||||
* verified, decompressed chunks for the files the manager says are pending, [assemble] writes those
|
||||
* files from the cache. The production installer intentionally uses one serial Java
|
||||
* chunk-to-file path instead, so cache bounds and resume behavior do not depend on native
|
||||
* availability. These APIs remain for callers that explicitly need batch fetch/assembly.
|
||||
* The native Epic engines in `libdroiddeckstores.so`. [stream] is the install the manager uses
|
||||
* whenever the library loads: chunks fetched in parallel by the adaptive fetch core and written
|
||||
* straight into each file's staging file as they arrive, shared chunks kept in a cache of at most
|
||||
* the given budget, every file verified and swapped in with the same rules as the bounded Java
|
||||
* writer (see `stream.rs`). [run] and [assemble] are the older batch pair: [run] fills the chunk
|
||||
* cache with verified, decompressed chunks for the pending files, [assemble] writes the files from
|
||||
* it. They stay for callers that explicitly want a whole-game cache.
|
||||
*
|
||||
* The cache is `<installDir>/.chunks` when `chunkCacheDir` is `""`, else the scratch folder given -
|
||||
* the app's own cache when the game installs to a card, where the assembler drops each chunk right
|
||||
* after its last use and the folder at the end. Both calls take the same value for one install.
|
||||
* the app's own cache when the game installs to a card.
|
||||
*
|
||||
* Both block while polling [AtomicBoolean] `cancel` every 250 ms: on cancel they flip the native
|
||||
* flag, wait up to 5 s for the run to wind down, then return.
|
||||
* All block while polling [AtomicBoolean] `cancel` every 250 ms. [run] and [assemble] flip the
|
||||
* native flag on cancel and wait up to 5 s; [stream] waits for the run to finish, because its
|
||||
* cleanup (removing unfinished staging files) must be done before the manager returns.
|
||||
*/
|
||||
object EpicNative {
|
||||
private const val TAG = "EpicNative"
|
||||
@@ -37,6 +39,18 @@ object EpicNative {
|
||||
fun onComplete(success: Boolean, error: String, bytes: Long)
|
||||
}
|
||||
|
||||
/** 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)
|
||||
}
|
||||
|
||||
/** Outcome of a run. `started == false`: nothing happened on disk, the caller runs its own loop. */
|
||||
class Result(
|
||||
@JvmField val started: Boolean, @JvmField val success: Boolean, @JvmField val cancelled: Boolean,
|
||||
@@ -59,6 +73,59 @@ 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) }
|
||||
|
||||
/**
|
||||
* The adaptive install of the files [pendingFileIdx] (indices into the manifest's file list),
|
||||
* with at most [cacheBudget] bytes of shared chunks cached. `started == false` means nothing
|
||||
* was fetched or written and the caller runs the bounded Java writer.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun stream(
|
||||
manifest: ByteArray, installDir: String, chunkCacheDir: String, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedBytes: Long,
|
||||
cacheBudget: 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, cdnPrefixes, pendingFileIdx, expectedBytes, cacheBudget, 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}", 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 {
|
||||
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, start: (Listener) -> Long): Result {
|
||||
if (!StoresNative.ensureLoaded()) return Result(false, false, false, "engine not built", 0L, 0, 0)
|
||||
val latch = CountDownLatch(1)
|
||||
@@ -110,6 +177,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, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedBytes: Long,
|
||||
cacheBudget: Long, caBundlePath: String, maxWorkers: Int, processWorkers: Int, listener: StreamListener,
|
||||
): Long
|
||||
|
||||
@JvmStatic
|
||||
private external fun nativeCancel(handle: Long)
|
||||
|
||||
|
||||
@@ -112,7 +112,12 @@ 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`, below): the chunks
|
||||
are fetched in parallel and written straight into staged files as they arrive. The older batch
|
||||
pair below (fetch everything into a cache, then assemble) stays for A/B benchmarks (the hidden
|
||||
`storesEpicMode=batch` preference).
|
||||
|
||||
Two runs make up the native half of a batch Epic install:
|
||||
|
||||
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 +229,50 @@ 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;[IJJLjava/lang/String;IILjava/lang/Object;)J` (last parameter is the listener) | `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStream` |
|
||||
|
||||
```kotlin
|
||||
@JvmStatic private external fun nativeStream(
|
||||
manifest: ByteArray, installDir: String, chunkCacheDir: String, cdnPrefixes: Array<String>,
|
||||
pendingFileIdx: IntArray, expectedBytes: Long, cacheBudget: Long, caBundlePath: String,
|
||||
maxWorkers: Int, processWorkers: Int, listener: StreamListener,
|
||||
): Long
|
||||
```
|
||||
|
||||
Parameters as `nativeStart`, 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).
|
||||
`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 that file's unique sibling `.droiddeck-epic-*.stage` file; 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 runs
|
||||
its bounded Java writer.
|
||||
|
||||
| `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
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
//! - [`assemble`] — the files from the cache, one sequential write each, SHA-1 checked as they
|
||||
//! are written; with a scratch cache (one the caller placed apart from the install) each chunk
|
||||
//! is deleted as soon as the last file that uses it is done.
|
||||
//! - [`stream`] — the adaptive install the manager uses by default: the chunks fetched in
|
||||
//! parallel through the fetch core and written straight into staging files as they arrive,
|
||||
//! shared chunks kept in a budgeted cache, each file verified and swapped in safely.
|
||||
//! - [`jni`] — `Java_com_droiddeck_launcher_stores_epic_EpicNative_native*` exports.
|
||||
//!
|
||||
//! The selective-install-tag, delta/resume and chunk verification rules mirrored here were ported
|
||||
@@ -31,6 +34,7 @@ pub mod driver;
|
||||
pub mod jni;
|
||||
pub mod manifest;
|
||||
pub mod plan;
|
||||
pub mod stream;
|
||||
|
||||
/// User-Agent the Java manager sends on every chunk request (`EpicDownloadManager.UA`).
|
||||
pub const USER_AGENT: &str =
|
||||
|
||||
@@ -177,6 +177,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::*;
|
||||
@@ -361,6 +435,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");
|
||||
|
||||
@@ -7,7 +7,11 @@
|
||||
//! listener: EpicNative.Listener) -> long handle (0 = not started)
|
||||
//! nativeAssemble(manifest: byte[], installDir: String, chunkCacheDir: String, fileIdx: int[],
|
||||
//! listener: EpicNative.Listener) -> long handle (0 = not started)
|
||||
//! nativeCancel(handle) — flips the run's AtomicBool; fetch or assembly stops promptly
|
||||
//! nativeStream(manifest: byte[], installDir: String, chunkCacheDir: String, cdnPrefixes: String[],
|
||||
//! pendingFileIdx: int[], expectedBytes: long, cacheBudget: long, caBundlePath: String,
|
||||
//! maxWorkers: int, processWorkers: int, listener: EpicNative.StreamListener)
|
||||
//! -> long handle (0 = not started)
|
||||
//! nativeCancel(handle) — flips the run's AtomicBool; fetch, assembly or stream stops promptly
|
||||
//! nativeRelease(handle) — frees the handle (after onComplete)
|
||||
//! ```
|
||||
//! `chunkCacheDir` = "" keeps the cache at `<installDir>/.chunks`; any other directory is a
|
||||
@@ -17,6 +21,11 @@
|
||||
//! an assembly run, per file written), `onLog(String line)`,
|
||||
//! `onComplete(boolean success, String error, long bytes)`.
|
||||
//!
|
||||
//! The stream run (`stream.rs`) writes the files as the chunks arrive; its listener has
|
||||
//! `onStreamProgress(long bytesWritten, long bytesTotal, int filesDone, int filesTotal,
|
||||
//! long filesBytes)`, `onLog(String)`, `boolean onBeforeReplace()` (called once, before the first
|
||||
//! file replaces its destination; `false` fails the run) and the same `onComplete`.
|
||||
//!
|
||||
//! Planning (manifest parse + cross-check) runs synchronously on the calling thread so a plan
|
||||
//! failure returns 0 BEFORE any fetch and Java can fall back to its own pool; the fetch runs on
|
||||
//! a new thread. JavaVM attach / GlobalRef handling is the same shape as the GOG facade.
|
||||
@@ -31,6 +40,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")]
|
||||
@@ -458,6 +468,184 @@ pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_native
|
||||
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,
|
||||
cdn_prefixes: JObjectArray,
|
||||
pending_file_idx: JIntArray,
|
||||
expected_bytes: jlong,
|
||||
cache_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 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,
|
||||
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,
|
||||
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 handle = Box::new(EpicDownloadHandle { cancel });
|
||||
|
||||
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,
|
||||
);
|
||||
});
|
||||
|
||||
Box::into_raw(handle) as jlong
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeCancel(
|
||||
_env: JNIEnv,
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,57 @@
|
||||
package com.droiddeck.launcher.stores.epic
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33])
|
||||
class EpicAdaptivePlanTest {
|
||||
private val mib = 1024L * 1024
|
||||
private val gib = 1024 * mib
|
||||
|
||||
@Test fun freshInstallsNeedNoOpenStagingAllowance() {
|
||||
assertEquals(0L, EpicDownloadManager.adaptiveOpenStagingAllowance(emptyMap()))
|
||||
val old = mapOf("big.pak" to 10 * gib, "small.pak" to gib)
|
||||
assertEquals(10 * gib + EpicDownloadManager.ADAPTIVE_OPEN_WINDOW_BYTES, EpicDownloadManager.adaptiveOpenStagingAllowance(old))
|
||||
assertEquals(100 * mib, EpicDownloadManager.adaptiveOpenStagingAllowance(mapOf("a" to 60 * mib, "b" to 40 * mib)))
|
||||
}
|
||||
|
||||
@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 cardInstallKeepsTheCacheOnInternalScratchWhenItFits() {
|
||||
val plan = EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, 30 * gib, true, 10 * gib)
|
||||
assertNull(plan.error)
|
||||
assertFalse(plan.cacheBesideGame)
|
||||
assertEquals(EpicDownloadManager.ADAPTIVE_CACHE_MAX_BYTES, plan.cacheBudget)
|
||||
assertEquals(20 * gib, plan.gameNeed)
|
||||
assertEquals(plan.cacheBudget + EpicDownloadManager.ADAPTIVE_TRANSIENT_BYTES, plan.scratchNeed)
|
||||
}
|
||||
|
||||
@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)
|
||||
assertTrue(plan.gameNeed <= 30 * gib)
|
||||
}
|
||||
|
||||
@Test fun shortStorageIsNamedWithTheShortfall() {
|
||||
val card = EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, 10 * gib, true, 10 * gib)
|
||||
assertTrue(card.error!!.contains("game's storage"))
|
||||
assertTrue(card.error!!.contains("short by 10.0 GB"))
|
||||
val internal = EpicDownloadManager.planAdaptiveSpace(20 * gib, gib, 20 * gib, false, -1)
|
||||
assertTrue(internal.error!!.contains("game's storage"))
|
||||
assertNull(EpicDownloadManager.planAdaptiveSpace(20 * gib, 0, -1, true, -1).error)
|
||||
}
|
||||
}
|
||||
+47
-19
@@ -45,27 +45,55 @@ with a placeholder job. Failures are kept in the Downloads UI, notification and
|
||||
duplicated as transient failure toasts. Active queue persistence and ordinary backend pause/retry
|
||||
are unchanged.
|
||||
|
||||
Epic installs use the same bounded Java writer whether the native engine is available or not.
|
||||
It fetches one chunk at a time, verifies it, and writes the required parts into the current
|
||||
game file. Chunks are deleted after their last use; reusable chunks are retained up to 64 MiB,
|
||||
with at most one additional incoming chunk before eviction. Evicted shared chunks may be
|
||||
downloaded again. This trades parallel-download throughput and potentially extra network
|
||||
traffic for bounded temporary disk usage. The native batch APIs remain available but are not
|
||||
used by this install path.
|
||||
Epic installs have three writers, all with the same per-file safety rules (below):
|
||||
|
||||
The earlier batch design intentionally prioritizes parallel-download throughput. Native
|
||||
`epic/assemble.rs::build_assemble_plan` enables `scratch_cache` only for a nonempty cache path.
|
||||
`run_assemble` decrements per-file chunk references and removes chunks at their last use **only
|
||||
when that flag is set**; the default beside-game cache is removed at the end. The native tests
|
||||
`default_cache_assembles_like_the_kotlin_loop_and_removes_the_cache_at_the_end` and
|
||||
the scratch-cache assembly tests cover that distinction. Extending last-use reclamation to
|
||||
internal native assembly could keep parallel batch throughput and reduce the overlapping
|
||||
cache/output peak, but batch fetching still fills
|
||||
the cache before assembly. That is not equivalent to the bounded sequential writer's fixed
|
||||
retained-cache ceiling. This change keeps the bounded path and its explicit throughput/refetch
|
||||
tradeoff; it does not redesign or benchmark native download throughput.
|
||||
- **Adaptive (default when `libdroiddeckstores.so` loads)** - `EpicNative.stream`,
|
||||
`epic/stream.rs`. The chunks go through the shared fetch core in the order their files need
|
||||
them, with its full adaptivity (window ramp, per-host caps, in-flight byte budget, retries,
|
||||
host rotation, stall handoff). A chunk only one file part uses is verified and written straight
|
||||
into that file's staging file at the part's offset, in whatever order it arrives; no cache. A
|
||||
chunk several parts use is written into every part whose file the download has reached and kept,
|
||||
decompressed, in the cache until its last part is written. The cache has a byte budget; the plan
|
||||
simulates the install in file order and, when the budget would be exceeded, drops the cached
|
||||
chunk needed furthest ahead and downloads it again later. Files are opened only once the
|
||||
download reaches them, so the set of half-written files stays within the fetch window. All writes
|
||||
happen in the fetch core's process pool, whose completion frees the in-flight byte budget: a slow
|
||||
card throttles the download instead of growing memory. Each file's SHA-1 is computed as its
|
||||
parts are written (parts that arrived out of order are read back once the bytes before them are
|
||||
in). The run logs `stream-plan` (direct vs shared chunks, planned refetches, budget) and
|
||||
`stream-summary mode=adaptive` (peak cache, peak staging, refetches, bytes written from the
|
||||
download vs from the cache, average MB/s).
|
||||
- **Bounded Java** - when the library does not load or link, when the engine refuses the plan,
|
||||
or when the adaptive space plan does not fit while this one might. It fetches one chunk at a
|
||||
time, verifies it, and writes the required parts into the current game file. Chunks are deleted
|
||||
after their last use; reusable chunks are retained up to 64 MiB, with at most one additional
|
||||
incoming chunk before eviction. Evicted shared chunks may be downloaded again. It logs
|
||||
`epic: summary mode=java-bounded`.
|
||||
- **Batch** - the earlier native design, kept for A/B benchmarks: every chunk fetched into the
|
||||
cache in parallel, then the files assembled from it (`EpicNative.run` + `EpicNative.assemble`).
|
||||
It needs the cache and the files at once (about twice the game) and checks exactly that before
|
||||
starting; it writes files in place, so an existing install is marked `updating` before assembly.
|
||||
It logs `epic: summary mode=batch`.
|
||||
|
||||
The bounded Epic download bar, measured rate and ETA count pending file bytes written, not unique
|
||||
The writer is chosen by the hidden `storesEpicMode` string in the `session` preferences:
|
||||
`adaptive` (default), `batch` or `java`. There is no UI for it; set it on a test device to compare
|
||||
the modes on the same game.
|
||||
|
||||
The adaptive space plan (`EpicDownloadManager.planAdaptiveSpace`, unit-tested in
|
||||
`EpicAdaptivePlanTest`) checks each volume for what lands on it. The game volume: the sequential
|
||||
staged-file peak (remaining file bytes, each replaced file's old copy credited only after its
|
||||
swap), plus, for an update, the largest old file plus 256 MiB for files finishing out of order
|
||||
within the fetch window, plus the cache when it is beside the game. The cache volume: the budget
|
||||
plus 64 MiB of chunks in transit. The budget is half the cache volume's free space after those
|
||||
needs, at most 1 GiB (256 MiB when free space is unknown; 0 is allowed and means every reuse is a
|
||||
refetch). On a card install the cache stays on internal scratch unless internal storage cannot
|
||||
hold at least 64 MiB of it; then the cache goes beside the game (still released chunk by chunk)
|
||||
instead of failing. A plan that does not fit names the short storage and the shortfall in the log
|
||||
and hands over to the bounded writer, whose own checks decide. Running out of space in the middle
|
||||
of an adaptive run is not destructive: a failed write fails the run, unfinished staging files are
|
||||
removed, the old files stay.
|
||||
|
||||
The Epic download bar, measured rate and ETA (adaptive and bounded) count pending file bytes written, not unique
|
||||
compressed chunks fetched. Shared chunks therefore cannot complete the bar before later files
|
||||
are written, and an evicted chunk's refetch is reflected in the time spent doing that work.
|
||||
Streaming stays in the Download stage until all files are written and verified, then enters
|
||||
|
||||
@@ -14,6 +14,7 @@ EXPORTS = ['Java_com_droiddeck_launcher_stores_StoresNative_nativeVersion',
|
||||
'Java_com_droiddeck_launcher_stores_gog_GogNative_nativeStart',
|
||||
'Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStart',
|
||||
'Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeAssemble',
|
||||
'Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStream',
|
||||
'Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeStart']
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user