From 7c758c817f3f2c6281e2957d6fc9854fd1b2f3a1 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Thu, 8 Oct 2026 23:14:29 -0400 Subject: [PATCH 01/54] perf: fast path answers Steam's pre-2.33 stat calls and stays off without the patched proot (#466) --- .../launcher/runtime/ProotFastPath.kt | 9 ++++++- tools/linuxfs/preload/storage.c | 5 +++- tools/proot/fastpath/fastpath.c | 26 +++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt b/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt index 9b78180466..10538c5bc6 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/ProotFastPath.kt @@ -24,8 +24,15 @@ object ProotFastPath { /** MAX_BINDS in fastpath.c: past it the library turns itself off, so the session keeps it off. */ const val MAX_BINDS = 256 + /** + * Only the app's own proot (libproot.so, built with patches/0014) lets the trampoline through. + * The runtime's fallback copy (LinuxRuntime.prootBinary) is Termux's, which traps those calls + * too and translates their host paths a second time: every lookup then fails inside the + * process and goes to proot anyway, paying for both. + */ fun enabled(context: Context): Boolean = - SessionPrefs.prootFastPath(context) && !SessionPrefs.prootNoSeccomp(context) + SessionPrefs.prootFastPath(context) && !SessionPrefs.prootNoSeccomp(context) && + LinuxRuntime.prootBinary(context).parent == context.applicationInfo.nativeLibraryDir /** The key for this rootfs and these binds, or null when the library cannot be told them. */ fun key(root: File, binds: List): String? { diff --git a/tools/linuxfs/preload/storage.c b/tools/linuxfs/preload/storage.c index fb85d1b6f6..201f93cd40 100644 --- a/tools/linuxfs/preload/storage.c +++ b/tools/linuxfs/preload/storage.c @@ -262,7 +262,10 @@ int ftruncate(int fd, off_t length) { int known = 0; if (steam_client()) { if (next_fstat == NULL) next_fstat = dlsym(RTLD_NEXT, "fstat"); - known = next_fstat != NULL && next_fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && staging_file(fd); + /* staging_file() reads /proc/self/fd, a round trip through proot: skip it for a shrink, which + * is passed through untouched either way. */ + known = next_fstat != NULL && next_fstat(fd, &st) == 0 && S_ISREG(st.st_mode) && + length >= st.st_size && staging_file(fd); } int result = 0, call_errno = entry_errno; if (!known || length <= st.st_size || !cannot_reserve((long long)st.st_dev)) { diff --git a/tools/proot/fastpath/fastpath.c b/tools/proot/fastpath/fastpath.c index a48b201f89..3a3a10c14c 100644 --- a/tools/proot/fastpath/fastpath.c +++ b/tools/proot/fastpath/fastpath.c @@ -464,6 +464,26 @@ int lstat(const char *path, struct stat *st) { } int lstat64(const char *p, struct stat64 *st) { return lstat(p, (struct stat *)st); } +/* + * The pre-2.33 entry points. Programs linked against an older glibc - the Steam client, + * steamclient.so and filesystem_stdio.so among them - call these rather than stat(), so without + * them every stat the client makes goes to proot: a download's preallocation stats each file it + * creates. On aarch64 the only version is 0 (_STAT_VER), whose struct stat is the one above; + * glibc refuses any other with EINVAL, and so does this. + */ +#define FP_STAT_VER 0 +static int xstat_common(int ver, int dirfd, const char *path, struct stat *st, int flags) { + REAL(fstatat, int (*)(int, const char *, struct stat *, int)); + if (ver != FP_STAT_VER) { errno = EINVAL; return -1; } + return stat_common(dirfd, path, st, flags, real_fstatat); +} +int __xstat(int ver, const char *p, struct stat *st) { return xstat_common(ver, AT_FDCWD, p, st, 0); } +int __xstat64(int ver, const char *p, struct stat64 *st) { return xstat_common(ver, AT_FDCWD, p, (struct stat *)st, 0); } +int __lxstat(int ver, const char *p, struct stat *st) { return xstat_common(ver, AT_FDCWD, p, st, AT_SYMLINK_NOFOLLOW); } +int __lxstat64(int ver, const char *p, struct stat64 *st) { return xstat_common(ver, AT_FDCWD, p, (struct stat *)st, AT_SYMLINK_NOFOLLOW); } +int __fxstatat(int ver, int d, const char *p, struct stat *st, int f) { return xstat_common(ver, d, p, st, f); } +int __fxstatat64(int ver, int d, const char *p, struct stat64 *st, int f) { return xstat_common(ver, d, p, (struct stat *)st, f); } + int statx(int dirfd, const char *path, int flags, unsigned mask, struct statx *out) { REAL(statx, int (*)(int, const char *, int, unsigned, struct statx *)); char host[PATH_MAX]; @@ -483,8 +503,10 @@ static int access_common(int dirfd, const char *path, int mode, int flags) { REAL(faccessat, int (*)(int, const char *, int, int)); char host[PATH_MAX]; struct stat st; - /* faccessat(2) has no flags: AT_EACCESS and AT_SYMLINK_NOFOLLOW stay with glibc and proot. */ - long rs = flags == 0 ? resolve(dirfd, path, host) : FP_SLOW; + /* faccessat(2) has no flags. AT_EACCESS stays with glibc and proot; AT_SYMLINK_NOFOLLOW (the + * Steam client passes it for every file it reserves) only differs on a symlink, which goes to + * proot as well. */ + long rs = (flags & ~AT_SYMLINK_NOFOLLOW) == 0 ? resolve(dirfd, path, host) : FP_SLOW; if (rs == -ENOENT) { hits++; return ret(rs); } if (rs == 0) { long k = kind(host, &st); From 416c1b9efde09b8c66222f32e8da488bfd8311bc Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 9 Oct 2026 07:11:09 -0400 Subject: [PATCH 02/54] fix: proot fast path hands opendir of a directory symlink to proot (#454) --- tools/proot/fastpath/fastpath.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/proot/fastpath/fastpath.c b/tools/proot/fastpath/fastpath.c index 3a3a10c14c..8720f4d57f 100644 --- a/tools/proot/fastpath/fastpath.c +++ b/tools/proot/fastpath/fastpath.c @@ -391,6 +391,12 @@ static long fp_open(int dirfd, const char *path, int flags, mode_t mode) { /* The last component is never followed here: a symlink there is proot's to resolve. */ long fd = sc(SYS_openat, AT_FDCWD, (long)host, flags | O_NOFOLLOW, (flags & O_CREAT) ? mode : 0, 0); if (fd == -ELOOP && !(flags & O_NOFOLLOW)) return FP_SLOW; + /* With O_DIRECTORY the kernel checks for a directory before it gets to the symlink, so a link to + * a directory (opendir() of it) fails ENOTDIR rather than ELOOP. A real non-directory stays here. */ + if (fd == -ENOTDIR && (flags & O_DIRECTORY) && !(flags & O_NOFOLLOW)) { + struct stat st; + if (kind(host, &st) == 1) return FP_SLOW; + } if (fd >= 0 && (flags & O_PATH) && !(flags & O_NOFOLLOW)) { struct stat st; if (sc(SYS_fstat, fd, (long)&st, 0, 0, 0) != 0 || S_ISLNK(st.st_mode)) { From e93397011d839283d62fb029accb70a6a37362af Mon Sep 17 00:00:00 2001 From: SG3xHERO Date: Fri, 9 Oct 2026 12:13:06 +0100 Subject: [PATCH 03/54] Add a Follow screen resolution for foldables (#403) --- app/src/main/cpp/waylandcomp/src/compositor.c | 48 ++++++++++++-- app/src/main/cpp/waylandcomp/src/vk_present.c | 17 ++++- .../cpp/waylandcomp/src/waylandcomp_jni.c | 12 ++++ .../com/droiddeck/launcher/SessionActivity.kt | 65 +++++++++++++++++- .../launcher/session/SessionDisplay.kt | 17 ++++- .../launcher/session/SessionPrefs.kt | 6 +- .../launcher/session/SessionService.kt | 3 + .../launcher/session/SessionState.kt | 4 ++ .../droiddeck/launcher/ui/ResolutionRow.kt | 4 +- .../launcher/wayland/WaylandCompositor.java | 5 ++ app/src/main/res/values/strings.xml | 1 + .../launcher/session/SessionDisplayTest.kt | 17 +++-- .../overlay/usr/local/bin/droiddeck-session | 7 +- tools/linuxfs/preload/xmode.c | 66 +++++++++++++++++++ 14 files changed, 251 insertions(+), 21 deletions(-) create mode 100644 tools/linuxfs/preload/xmode.c diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index fbbd09886e..faa764565e 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -1661,15 +1661,24 @@ static void bind_desktop(struct wl_client *c, void *data, uint32_t ver, uint32_t static void output_release(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } static const struct wl_output_interface output_impl = { .release = output_release }; -static void bind_output(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { - struct wl_resource *r = wl_resource_create(c, &wl_output_interface, ver, id); - if (!r) { wl_client_post_no_memory(c); return; } - wl_resource_set_implementation(r, &output_impl, NULL, NULL); - wl_output_send_geometry(r, 0, 0, 340, 190, WL_OUTPUT_SUBPIXEL_UNKNOWN, - "DroidDeck", "Wayland", WL_OUTPUT_TRANSFORM_NORMAL); +/* Every bound wl_output, so a resized output (a foldable opening) can send its new mode. */ +static struct wl_list g_outputs; +static void output_resource_destroy(struct wl_resource *r) { wl_list_remove(wl_resource_get_link(r)); } + +static void output_send_mode(struct wl_resource *r) { wl_output_send_mode(r, WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED, g_output_w > 0 ? g_output_w : 1920, g_output_h > 0 ? g_output_h : 1080, g_output_refresh_mhz > 0 ? g_output_refresh_mhz : 60000); +} + +static void bind_output(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { + struct wl_resource *r = wl_resource_create(c, &wl_output_interface, ver, id); + if (!r) { wl_client_post_no_memory(c); return; } + wl_resource_set_implementation(r, &output_impl, NULL, output_resource_destroy); + wl_list_insert(&g_outputs, wl_resource_get_link(r)); + wl_output_send_geometry(r, 0, 0, 340, 190, WL_OUTPUT_SUBPIXEL_UNKNOWN, + "DroidDeck", "Wayland", WL_OUTPUT_TRANSFORM_NORMAL); + output_send_mode(r); if (ver >= WL_OUTPUT_NAME_SINCE_VERSION) { wl_output_send_name(r, "DroidDeck-1"); wl_output_send_description(r, "DroidDeck display"); @@ -3200,6 +3209,23 @@ static void key_event(uint32_t evdev, int pressed) { wl_display_flush_clients(g_display); } +/* The output changed size under a running session (a foldable opened or closed, with the + * session following the screen). Every client that has had its first configure is told the new + * size: gamescope's Wayland backend and the desktop's labwc both resize their output to it, and + * the next buffers they commit fill the new panel instead of being letterboxed onto it. */ +static void output_resized(void) { + struct surface *s; + struct wl_resource *r; + wl_resource_for_each(r, &g_outputs) { + output_send_mode(r); + if (wl_resource_get_version(r) >= 2) wl_output_send_done(r); + } + wl_list_for_each(s, &g_surfaces, link) + if (s->xdg_toplevel && s->toplevel_committed) send_toplevel_configure(s); + droiddeck_log("display", "output resized to %dx%d", g_output_w, g_output_h); + wl_display_flush_clients(g_display); +} + /* wl event-loop callback: drain queued input events written by the Android UI thread. */ static int on_input_readable(int fd, uint32_t mask, void *data) { struct input_msg m; @@ -3212,6 +3238,7 @@ static int on_input_readable(int fd, uint32_t mask, void *data) { case 5: on_vsync(((int64_t)m.p1 << 32) | (uint32_t)m.p2); break; case 6: pointer_delta(m.p1 / 256.0, m.p2 / 256.0); break; /* relative motion, 1/256 px */ case 7: deliver_touch(&m); break; + case 8: output_resized(); break; default: deliver_pointer(&m); break; } } @@ -3242,6 +3269,14 @@ void droiddeck_wayland_send_key(int evdev, int state) { (void)n; } +/* Called from JNI after g_output_w/h changed: the compositor thread re-sends the size. */ +void droiddeck_wayland_output_resized(void) { + if (g_input_pipe[1] < 0) return; + struct input_msg m = { 8, 0, 0, 0, 0 }; + ssize_t n = write(g_input_pipe[1], &m, sizeof(m)); + (void)n; +} + /* Called from JNI on every screen refresh (Choreographer). */ void droiddeck_wayland_vsync(int64_t frame_time_ns) { if (g_input_pipe[1] < 0) return; @@ -3439,6 +3474,7 @@ static struct wl_listener g_client_created = { .notify = on_client_created }; int droiddeck_wayland_run(void) { wl_list_init(&g_surfaces); wl_list_init(&g_toplevels); + wl_list_init(&g_outputs); wl_list_init(&g_constraints); wl_list_init(&g_relative_pointers); open_session_log(); diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.c b/app/src/main/cpp/waylandcomp/src/vk_present.c index 3af9a8c254..4551c4806f 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.c +++ b/app/src/main/cpp/waylandcomp/src/vk_present.c @@ -160,6 +160,11 @@ static const char *vk_result_name(VkResult r); static pthread_mutex_t g_req_lock = PTHREAD_MUTEX_INITIALIZER; static ANativeWindow *g_req_window; static int g_req_pending; +/* A detach was asked for since the compositor thread last looked. A rebind (null, then the same + * window) to rebuild the swapchain at a new size can land in full before the thread gets to it, + * and would otherwise read as the same window handed over again - nothing rebuilt, and the old + * size's buffers scaled onto the rotated or refolded panel. */ +static int g_req_detached; /* Scale mode + alignment (app values, see vk_present.h); written from any thread, read per frame. */ static volatile int g_mode = VKP_MODE_STRETCH, g_align = VKP_ALIGN_CENTER; @@ -252,6 +257,7 @@ void vk_present_set_window(ANativeWindow *window) { if (g_req_pending && g_req_window && g_req_window != window) superseded = g_req_window; g_req_window = window; g_req_pending = 1; + if (!window) g_req_detached = 1; pthread_mutex_unlock(&g_req_lock); if (superseded) ANativeWindow_release(superseded); } @@ -261,13 +267,20 @@ int vkp_apply_window_request(void) { pthread_mutex_lock(&g_req_lock); if (!g_req_pending) { pthread_mutex_unlock(&g_req_lock); return 0; } w = g_req_window; + const int detached = g_req_detached; g_req_window = NULL; g_req_pending = 0; + g_req_detached = 0; pthread_mutex_unlock(&g_req_lock); if (w == g_window) { /* The same window handed over again (ANativeWindow_fromSurface adds a reference each time). */ if (w) ANativeWindow_release(w); - return 0; + if (!w || !detached) return 0; + /* ... after a detach: a rebind for a resized surface. Rebuilt at its size on the next frame. */ + sc_layer_window_gone(); + destroy_swapchain(); + droiddeck_log("gpu", "screen surface rebound: rebuilding the swapchain at its new size"); + return 1; } sc_layer_window_gone(); /* the layer (if any) belongs to the old window */ destroy_swapchain(); /* recreated against the new window on the next frame */ @@ -1516,6 +1529,7 @@ static int render_impl(int scene_w, int scene_h, const struct vkp_draw *draws, i const struct vkp_hdr_frame *hf, int *how) { if (how) *how = 0; vkp_apply_window_request(); + check_surface_changed(); /* a rotation or a fold the rebind did not cover: rebuild at the new size */ if (g_dev_state == -2) return -1; if (dev_init() != 0 || !g_window || scene_w <= 0 || scene_h <= 0) return -1; @@ -2099,6 +2113,7 @@ static int pass_begin_impl(int scene_w, int scene_h, const struct vkp_draw *draw const struct vkp_hdr_frame *hf, int *rw, int *rh) { if (g_pass.active) vkp_pass_abort(); if (g_dev_state == -2 || dev_init() != 0 || !g_window || scene_w <= 0 || scene_h <= 0) return -1; + check_surface_changed(); /* as in render_impl: the effects pass maps onto the output's size */ if (!g_swapchain && swap_init() != 0) return -1; /* the mapping is in output pixels */ update_map(scene_w, scene_h); if (!g_map.valid) return -1; diff --git a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c index 4d11a54192..8ec4b96b64 100644 --- a/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c +++ b/app/src/main/cpp/waylandcomp/src/waylandcomp_jni.c @@ -30,6 +30,7 @@ extern int droiddeck_cursor_serial(void); extern void droiddeck_wayland_send_key(int evdev, int state); extern void droiddeck_wayland_send_scene_input(int type, int a, int b); extern void droiddeck_wayland_vsync(int64_t frame_time_ns); +extern void droiddeck_wayland_output_resized(void); extern volatile int g_fps_limit; extern volatile int g_hide_shell; extern volatile int g_zero_copy; @@ -401,6 +402,17 @@ Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeSetOutputSize(JNIEnv g_output_h = h > 0 ? h : 0; } +/* The output's size changed while the compositor runs (a foldable opening or closing with the + * session following the screen): store it and have the compositor thread tell the clients. */ +JNIEXPORT void JNICALL +Java_com_droiddeck_launcher_wayland_WaylandCompositor_nativeResizeOutput(JNIEnv *env, jclass clazz, jint w, jint h) { + (void)env; (void)clazz; + if (w <= 0 || h <= 0) return; + g_output_w = w; + g_output_h = h; + droiddeck_wayland_output_resized(); +} + /* Fullscreen mode + screen alignment (Container.FULLSCREEN_* / ALIGN_* values): how the scene is * fitted onto the output. Any thread, any time; the next frame uses it. */ JNIEXPORT void JNICALL diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 2e9be51934..d706b0622a 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -336,6 +336,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { ) } Log.i(TAG, "perf: " + PerfMode.apply(this, SessionState.fpsLimit)) + if (!SessionState.running) SessionState.followScreen = followChoice() + applyFollowOrientation(resources.configuration) goFullscreen() // The device's volume keys change the stream the session plays on (the relay and // PulseAudio are media playback); they are never forwarded to the guest. @@ -962,7 +964,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { // sized once when the session starts and cannot change. A foldable recreates the Surface // on the other panel, and recomputing the size there told the compositor a 21:9 buffer // was 16:9 - the picture came back squashed sideways. While a session runs, keep its size. - val size = if (SessionState.running) SessionState.outputSize else outputSize() + // Follow screen is the exception: there the compositor tells gamescope the new size, so the + // session takes the shape of the Surface it now has. + val previous = SessionState.outputSize + val live = if (SessionState.followScreen && !isInPictureInPictureMode) + com.droiddeck.launcher.session.SessionDisplay.followSize(holder.surfaceFrame.width(), holder.surfaceFrame.height()) + else null + val size = live ?: if (SessionState.running) previous else outputSize() SessionState.outputSize = size if (!SessionState.running) SessionState.refreshHz = refreshHz() // How far the panel enlarges the session, for texture sharpness "Auto" (TextureFiltering): @@ -1021,6 +1029,10 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { driverId?.let { turnip.driverPath(it) }, driverId?.let { turnip.libraryName(it) }, applicationInfo.nativeLibraryDir, size.first, size.second, refreshHz(), SessionState.fpsLimit, ) + if (SessionState.running && size != previous) { + Log.i(TAG, "follow screen: ${previous.first}x${previous.second} -> ${size.first}x${size.second} on a new surface") + WaylandCompositor.nativeResizeOutput(size.first, size.second) + } if (!SessionState.running) SessionEvents.record("compositor.started") // The service owns everything below the compositor. It is started whenever no session is // running - NOT only when the compositor was just started: the compositor lives for the @@ -1049,9 +1061,51 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (resized) { Log.i(TAG, "surface resized to ${width}x$height - rebinding the compositor") CompositorHost.resize(holder.surface) { applyFrameGen() } + followWindow(width, height) } } + /** + * Follow screen: the session's display takes the window's new size (a Fold opening onto its + * inner panel, or closing onto the cover), so the picture fills the panel instead of being + * letterboxed onto it. Not in picture-in-picture: that window is a preview of the session, + * and resizing the guest to it would leave Steam laid out for a thumbnail. + */ + private fun followWindow(width: Int, height: Int) { + if (!SessionState.followScreen || isInPictureInPictureMode || !CompositorHost.isStarted) return + val size = com.droiddeck.launcher.session.SessionDisplay.followSize(width, height) ?: return + if (size == SessionState.outputSize) return + Log.i(TAG, "follow screen: ${SessionState.outputSize.first}x${SessionState.outputSize.second} -> ${size.first}x${size.second}") + SessionState.outputSize = size + val panel = panelBounds() + SessionState.upscaleRatio = maxOf(panel.width(), panel.height()).toFloat() / maxOf(size.first, size.second).coerceAtLeast(1) + WaylandCompositor.nativeResizeOutput(size.first, size.second) + } + + /** Whether this session's resolution is Follow screen; decided once, when it starts. */ + private fun followChoice(): Boolean { + val mode = SessionPrefs.prefMode(intent.getStringExtra(SessionService.EXTRA_MODE) ?: SessionService.MODE_STEAM) + return SessionPrefs.resolutionChoice(this, mode, com.droiddeck.launcher.session.SessionDisplay.panelSize(this)) == + com.droiddeck.launcher.session.SessionDisplay.FOLLOW_SCREEN + } + + /** + * Follow screen on a large panel (a Fold's inner screen) may turn with the device: a window + * locked to landscape on a squarer panel is letterboxed by the system itself, which no output + * size can undo. A phone-sized panel (the cover screen) stays landscape as before. + */ + private fun applyFollowOrientation(config: android.content.res.Configuration) { + if (!SessionState.followScreen) return + val wanted = if (config.smallestScreenWidthDp >= 600) android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER + else android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE + if (requestedOrientation != wanted) requestedOrientation = wanted + } + + override fun onConfigurationChanged(newConfig: android.content.res.Configuration) { + super.onConfigurationChanged(newConfig) + applyFollowOrientation(newConfig) + } + override fun surfaceDestroyed(holder: SurfaceHolder) { surfaceW = 0 surfaceH = 0 @@ -1891,6 +1945,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onPictureInPictureModeChanged(inPip: Boolean, newConfig: android.content.res.Configuration) { super.onPictureInPictureModeChanged(inPip, newConfig) if (::pip.isInitialized) pip.changed(inPip) + // The panel may have changed while the session was a thumbnail (folded or unfolded in PiP). + if (!inPip && surfaceW > 0) followWindow(surfaceW, surfaceH) } override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) { @@ -2018,10 +2074,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) + // ALWAYS from Android 11: SHORT_EDGES left a black strip along a camera on the long edge, + // which is where a Fold's inner-screen camera sits with the device held in landscape. if (Build.VERSION.SDK_INT >= 28) { window.attributes = window.attributes.apply { - layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES + layoutInDisplayCutoutMode = if (Build.VERSION.SDK_INT >= 30) + WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS + else WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES } } } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt index 7963875eb3..bae8d79db6 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionDisplay.kt @@ -6,6 +6,11 @@ import android.view.WindowManager /** The virtual display advertised to the guest, before it is fitted onto the Android panel. */ object SessionDisplay { const val MATCH_SCREEN = "screen" + /** The window's size, kept in step while the session runs: a foldable opening or closing, a + * split screen or a freeform window resizes the guest's display instead of letterboxing it. */ + const val FOLLOW_SCREEN = "follow" + /** Below this a side is a picture-in-picture or a sliver of split screen: not worth a resize. */ + const val FOLLOW_MIN_SIDE = 480 /** * Presets are heights, not sizes: "720p" is the panel's own shape at 720 lines, never narrower @@ -25,7 +30,7 @@ object SessionDisplay { (maxOf(panel.first, panel.second) and 1.inv()) to (minOf(panel.first, panel.second) and 1.inv()) fun resolveChoice(panel: Pair, choice: String): Pair { - if (choice == MATCH_SCREEN) return screenSize(panel) + if (choice == MATCH_SCREEN || choice == FOLLOW_SCREEN) return screenSize(panel) presetHeight(choice)?.let { return resolve(panel, it, SessionPrefs.SHAPE_AUTO) } // Fixed sizes (custom, or a preset saved before presets were heights) keep their exact size, // including legacy ones past the custom dialog's limit. @@ -34,13 +39,19 @@ object SessionDisplay { else resolve(panel, 720, SessionPrefs.SHAPE_AUTO) } - /** The height presets the panel is tall enough for, then the panel itself, with no duplicate sizes. */ + /** The height presets the panel is tall enough for, then the panel itself, with no duplicate + * sizes, then Follow screen. */ fun resolutionOptions(panel: Pair): List { val panelHeight = minOf(panel.first, panel.second) return PRESET_HEIGHTS.filter { it <= panelHeight }.map { "${it}p" } - .filter { resolveChoice(panel, it) != screenSize(panel) } + MATCH_SCREEN + .filter { resolveChoice(panel, it) != screenSize(panel) } + MATCH_SCREEN + FOLLOW_SCREEN } + /** A Follow screen session's display for a window of this size: its own shape, square or + * portrait included, with even sides; null when it is too small to follow (PiP). */ + fun followSize(width: Int, height: Int): Pair? = + if (minOf(width, height) < FOLLOW_MIN_SIDE) null else (width and 1.inv()) to (height and 1.inv()) + fun panelSize(context: Context): Pair { val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager @Suppress("DEPRECATION") diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 87cd57b444..da7dd04f90 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -487,7 +487,8 @@ object SessionPrefs { fun resolutionChoice(context: Context, mode: String, panel: Pair): String { val saved = prefs(context) saved.getString("displayResolution.$mode", null)?.let { value -> - if (value == SessionDisplay.MATCH_SCREEN || SessionDisplay.presetHeight(value) != null) return value + if (value == SessionDisplay.MATCH_SCREEN || value == SessionDisplay.FOLLOW_SCREEN || + SessionDisplay.presetHeight(value) != null) return value parseResolution(value)?.let { return "${it.first}x${it.second}" } } if (!resolutionChosen(context, mode) && !saved.contains("shape")) return SessionDisplay.DEFAULT_RESOLUTION @@ -497,7 +498,8 @@ object SessionPrefs { } fun setResolutionChoice(context: Context, mode: String, choice: String) { - val value = if (choice == SessionDisplay.MATCH_SCREEN || SessionDisplay.presetHeight(choice) != null) choice else { + val value = if (choice == SessionDisplay.MATCH_SCREEN || choice == SessionDisplay.FOLLOW_SCREEN || + SessionDisplay.presetHeight(choice) != null) choice else { val size = requireNotNull(parseResolution(choice)) { "Invalid resolution" } "${size.first}x${size.second}" } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 69d4ea65ee..71fd002c6e 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -352,6 +352,9 @@ class SessionService : Service() { guest.add("BL_WIDTH=" + size.first) guest.add("BL_HEIGHT=" + size.second) + // Follow screen: the output is resized under gamescope when a foldable opens or closes, + // and the session script asks gamescope to resize Steam's X screen along with it. + if (SessionState.followScreen) guest.add("BL_FOLLOW_OUTPUT=1") if (SessionState.hdr) { // The activity opened the compositor's HDR gate: gamescope offers HDR to its clients // and DXVK takes the HDR10 swapchain when a game asks for one. diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt index af47468ccb..57c377bcbd 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -96,6 +96,10 @@ object SessionState { @Volatile var outputSize: Pair = Pair(1920, 1080) + /** The session's resolution is "Follow screen": outputSize tracks the window while it runs. */ + @Volatile + var followScreen = false + @Volatile var refreshHz: Float = 60f diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt b/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt index 429ba1bb98..2af914e4dc 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ResolutionRow.kt @@ -20,11 +20,13 @@ fun ResolutionRow( val size = SessionDisplay.resolveChoice(panel, choice) val screen = SessionDisplay.screenSize(panel) val choices = SessionDisplay.resolutionOptions(panel) - val selected = if (size == screen) SessionDisplay.MATCH_SCREEN else choice + val selected = if (choice == SessionDisplay.FOLLOW_SCREEN) choice + else if (size == screen) SessionDisplay.MATCH_SCREEN else choice var editCustom by remember { mutableStateOf(false) } val options = choices.map { value -> val dimensions = SessionDisplay.resolveChoice(panel, value) value to if (value == SessionDisplay.MATCH_SCREEN) stringResource(R.string.display_match_screen, screen.first, screen.second) + else if (value == SessionDisplay.FOLLOW_SCREEN) stringResource(R.string.display_follow_screen) else if (value == SessionDisplay.DEFAULT_RESOLUTION) stringResource(R.string.mode_res_default, "${dimensions.first}×${dimensions.second}") else "${dimensions.first}×${dimensions.second}" } + (custom to if (selected in choices) stringResource(R.string.mode_res_custom) diff --git a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java index 84330c98b4..c40fb1b527 100644 --- a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java +++ b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java @@ -312,6 +312,11 @@ public final class WaylandCompositor { * list stops at the desktop size, as the X server's does on X11. Set before the compositor starts. */ public static native void nativeSetOutputSize(int width, int height); + /** A new output size for a running compositor ("Follow screen": a foldable opening or closing). + * The compositor thread re-sends the wl_output mode and an xdg_toplevel configure to every + * client, and gamescope / labwc resize their output to it. Any thread. */ + public static native void nativeResizeOutput(int width, int height); + /** Re-arms the one-shot first-frame notice, for a new session under a compositor that has * already presented a previous one. */ public static native void nativeResetFirstFrame(); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 50f9c637ed..e21c36943a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1948,6 +1948,7 @@ Frame rate %1$s Sets scaling to %2$s. Match screen (%1$d×%2$d) + Follow screen (resizes when folded or unfolded) Steam Show device details Hide device details diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt index 373af869ca..6093ea80cb 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionDisplayTest.kt @@ -27,15 +27,24 @@ class SessionDisplayTest { @Test fun optionsListTheHeightsThePanelCanShowThenThePanel() { val thor = SessionDisplay.resolutionOptions(1920 to 1080) - assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN), thor) - assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN), SessionDisplay.resolutionOptions(2400 to 1080)) - assertEquals(listOf("720p", "900p", "1080p", SessionDisplay.MATCH_SCREEN), SessionDisplay.resolutionOptions(3120 to 1440)) + assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), thor) + assertEquals(listOf("720p", "900p", SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), + SessionDisplay.resolutionOptions(2400 to 1080)) + assertEquals(listOf("720p", "900p", "1080p", SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), + SessionDisplay.resolutionOptions(3120 to 1440)) val small = SessionDisplay.resolutionOptions(1280 to 720) - assertEquals(listOf(SessionDisplay.MATCH_SCREEN), small) + assertEquals(listOf(SessionDisplay.MATCH_SCREEN, SessionDisplay.FOLLOW_SCREEN), small) assertEquals(1280 to 720, SessionDisplay.resolveChoice(1280 to 720, SessionDisplay.MATCH_SCREEN)) assertEquals(2560 to 1440, SessionDisplay.resolveChoice(2560 to 1440, SessionDisplay.MATCH_SCREEN)) } + @Test fun followScreenKeepsTheWindowShapeAndSkipsTinyWindows() { + assertEquals(2520 to 1080, SessionDisplay.resolveChoice(1080 to 2520, SessionDisplay.FOLLOW_SCREEN)) + assertEquals(1968 to 2184, SessionDisplay.followSize(1968, 2184)) + assertEquals(2184 to 1968, SessionDisplay.followSize(2185, 1969)) + assertNull(SessionDisplay.followSize(640, 360)) + } + @Test fun resolutionCapsPreserveTheSelectedShapeWithoutExceedingPanelHeight() { assertEquals(1280 to 720, SessionDisplay.resolve(1920 to 1080, 720, SessionPrefs.SHAPE_AUTO)) assertEquals(960 to 720, SessionDisplay.resolve(1440 to 1080, 720, SessionPrefs.SHAPE_EXACT)) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 3dbeaef1ab..4220b0ae2c 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -189,7 +189,12 @@ if [ -z "${BL_INSIDE:-}" ]; then width=${BL_WIDTH:-1280} height=${BL_HEIGHT:-720} nested=$width - if [ "$mode" = steam ] && [ "${BL_GAMESCOPE_STRETCH_16X9:-0}" = 1 ]; then + # Follow screen (BL_FOLLOW_OUTPUT=1): the app resizes gamescope's output when a foldable opens or + # closes, and with Steam on its own Xwayland gamescope resizes Steam's X screen with it, as on a + # Steam Deck docking. A stretched 16:9 nested size would pin it, so the two are exclusive. + if [ "${BL_FOLLOW_OUTPUT:-0}" = 1 ]; then + echo "== gamescope: the output follows the screen (${width}x${height} to start)" + elif [ "$mode" = steam ] && [ "${BL_GAMESCOPE_STRETCH_16X9:-0}" = 1 ]; then wide=$(( (height * 16 + 4) / 9 / 2 * 2 )) if [ "$wide" -gt "$width" ]; then nested=$wide diff --git a/tools/linuxfs/preload/xmode.c b/tools/linuxfs/preload/xmode.c new file mode 100644 index 0000000000..8579f70bcc --- /dev/null +++ b/tools/linuxfs/preload/xmode.c @@ -0,0 +1,66 @@ +/* + * The Steam client's own screen, in a Follow screen session. + * + * With two Xwayland servers the client sizes them through GAMESCOPE_XWAYLAND_MODE_CONTROL + * ({server, width, height, allow_super_res} on the root window): the games' server (1) to a game's + * Game Resolution, and its own (0) to what it recommends for the display - 1920x1080 on a Fold's + * 2448x1848 inner panel. From then on it reads that 16:9 size back as the display's "native" one, + * so its interface and every game it starts are 16:9 with bars on a near-square panel, and stay so + * when the phone folds onto the cover screen. + * + * gamescope already keeps server 0 at the output's size and resizes it when the output changes (a + * Steam Deck docking; here, the app resizing the output as the phone folds and unfolds). So in a + * Follow screen session (BL_FOLLOW_OUTPUT=1) a request for server 0 is dropped and the client's + * screen stays the panel's; requests for the games' servers pass through untouched. Any other + * session, and any other property, is passed straight on. + */ +#define _GNU_SOURCE +#include +#include +#include +#include + +/* Xlib's types, without needing its headers in the build image. */ +typedef struct _XDisplay Display; +typedef unsigned long Atom; +typedef unsigned long Window; +typedef int Bool; + +typedef int (*change_property_fn)(Display *, Window, Atom, Atom, int, int, const unsigned char *, int); +typedef Atom (*intern_atom_fn)(Display *, const char *, Bool); + +static int following(void) { + static int cached = -1; + if (cached < 0) { + const char *v = getenv("BL_FOLLOW_OUTPUT"); + cached = v && v[0] == '1'; + } + return cached; +} + +int XChangeProperty(Display *dpy, Window w, Atom property, Atom type, int format, int mode, + const unsigned char *data, int nelements) { + static change_property_fn real; + if (!real) real = (change_property_fn)dlsym(RTLD_NEXT, "XChangeProperty"); + if (!real) return 0; + if (following() && format == 32 && nelements == 4 && data) { + static intern_atom_fn intern; + static Display *atom_dpy; + static Atom mode_control; + if (!intern) intern = (intern_atom_fn)dlsym(RTLD_NEXT, "XInternAtom"); + /* Atoms belong to a connection; the client's is the one that writes this. */ + if (intern && atom_dpy != dpy) { + mode_control = intern(dpy, "GAMESCOPE_XWAYLAND_MODE_CONTROL", 1); + atom_dpy = dpy; + } + /* Format-32 property data is an array of long in Xlib. */ + const long *v = (const long *)data; + if (mode_control && property == mode_control && v[0] == 0) { + static int said; + if (said++ < 3) + fprintf(stderr, "== steam: kept its screen at the panel's size (it asked for %ldx%ld)\n", v[1], v[2]); + return 1; + } + } + return real(dpy, w, property, type, format, mode, data, nelements); +} From 79880d15026254a46d7daf67b56cc2a6fd9895fe Mon Sep 17 00:00:00 2001 From: Magarto Date: Fri, 9 Oct 2026 13:13:57 +0200 Subject: [PATCH 04/54] Desktop: keep the session when the big cores cannot be pinned (#394) --- tools/linuxfs/overlay/usr/local/bin/droiddeck-session | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 4220b0ae2c..8bf8812f7a 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -182,7 +182,10 @@ if [ -z "${BL_INSIDE:-}" ]; then cores=$(program_cores) if [ -n "$cores" ]; then echo "== desktop cores: $cores (the desktop and every program started from it)" - exec taskset -c "$cores" /usr/local/bin/droiddeck-desktop + # Pin this shell and let exec carry the mask. Android keeps the app on a smaller cpuset + # until its activity is on top (foreground 0-5, background fewer), and a mask with no core + # of that set is EINVAL: taskset -c then exits 1 and takes the whole session with it. + taskset -cp "$cores" $$ >/dev/null 2>&1 || echo "== desktop cores: not allowed right now, every core" fi exec /usr/local/bin/droiddeck-desktop fi From fe71084f23c7e2fb009a2f754950e9166f99107a Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 9 Oct 2026 10:39:50 -0400 Subject: [PATCH 05/54] feat: replace the LXQt desktop with SteamOS-style KDE Plasma (#473) * feat: replace the LXQt desktop with SteamOS-style KDE Plasma * feat: leaving the KDE desktop logs out through Plasma and returns to gaming mode * feat: DroidDeck logo wallpaper as the KDE desktop default * feat: build the KDE wallpaper from the DroidDeck mark artwork * ci: keep the built desktop package as a short-lived artifact for device testing * fix: the desktop loading screen no longer promises Steam on the desktop * docs: the output resize comments name the desktop's KWin * fix: the desktop package verifies every package's Arch Linux ARM signature and never replaces a published tag * fix: programs sent to the GPU from the desktop drop its software rendering overrides * fix: an interrupted desktop install no longer counts as installed * feat: pin the desktop package to its DroidDeck-Components release instead of the catalog --- .github/workflows/build-desktop-kde.yml | 80 +++ .github/workflows/build-wlroots.yml | 49 -- .github/workflows/build.yml | 12 +- app/build.gradle | 10 + app/src/main/cpp/waylandcomp/CMakeLists.txt | 2 + .../single-pixel-buffer-v1-protocol.c | 61 ++ .../single-pixel-buffer-v1-server-protocol.h | 142 +++++ .../protocols/single-pixel-buffer-v1.xml | 76 +++ app/src/main/cpp/waylandcomp/src/compositor.c | 21 +- .../main/cpp/waylandcomp/src/droiddeck_ext.c | 1 + .../main/cpp/waylandcomp/src/droiddeck_ext.h | 3 + .../cpp/waylandcomp/src/wl_single_pixel.c | 51 ++ .../com/droiddeck/launcher/MainActivity.kt | 4 - .../com/droiddeck/launcher/SessionActivity.kt | 9 +- .../droiddeck/launcher/core/DeviceReport.kt | 4 +- .../gpu/LinuxVulkanDriverManager.java | 2 +- .../launcher/runtime/AppImageManager.kt | 2 +- .../launcher/runtime/BwrapSpawner.kt | 8 +- .../launcher/runtime/DesktopCatalog.kt | 38 +- .../launcher/session/SessionFiles.kt | 16 +- .../launcher/session/SessionPaths.kt | 2 +- .../launcher/session/SessionPrefs.kt | 15 - .../launcher/session/SessionService.kt | 15 +- .../launcher/ui/ModeSettingsDialog.kt | 10 - .../launcher/wayland/WaylandCompositor.java | 2 +- app/src/main/res/values-es/strings.xml | 11 +- app/src/main/res/values-fr/strings.xml | 9 +- app/src/main/res/values-ja/strings.xml | 11 +- app/src/main/res/values-ko/strings.xml | 11 +- app/src/main/res/values-ru/strings.xml | 9 +- app/src/main/res/values-zh-rCN/strings.xml | 11 +- app/src/main/res/values-zh-rHK/strings.xml | 11 +- app/src/main/res/values-zh-rTW/strings.xml | 11 +- app/src/main/res/values/strings.xml | 11 +- docs/development/flatpak.md | 8 +- tools/build_local.sh | 16 +- tools/desktop-kde/README.md | 23 + tools/desktop-kde/archlinuxarm-builder.gpg | Bin 0 -> 2228 bytes tools/desktop-kde/build.sh | 73 +++ tools/desktop-kde/closure.py | 153 +++++ tools/desktop-kde/overlay/etc/xdg/baloofilerc | 2 + .../overlay/etc/xdg/gtk-3.0/settings.ini | 5 + tools/desktop-kde/overlay/etc/xdg/kcminputrc | 5 + tools/desktop-kde/overlay/etc/xdg/kdeglobals | 24 + tools/desktop-kde/overlay/etc/xdg/konsolerc | 2 + .../overlay/etc/xdg/kscreenlockerrc | 3 + tools/desktop-kde/overlay/etc/xdg/ksmserverrc | 2 + tools/desktop-kde/overlay/etc/xdg/ksplashrc | 3 + tools/desktop-kde/overlay/etc/xdg/kwalletrc | 3 + tools/desktop-kde/overlay/etc/xdg/kwinrc | 12 + tools/desktop-kde/overlay/etc/xdg/kwinrulesrc | 32 + tools/desktop-kde/overlay/etc/xdg/plasmarc | 2 + tools/desktop-kde/overlay/etc/xdg/startkderc | 2 + .../droiddeck-return-gaming.desktop | 9 + .../applications/steamdeck-bigpicture.desktop | 8 + .../applications/steamdeck-steam.desktop | 8 + .../usr/share/color-schemes/Vapor.colors | 119 ++++ .../actions/steamdeck-gaming-return.svg | 7 + .../icons/hicolor/scalable/apps/steam.svg | 11 + .../places/distributor-logo-steamdeck.svg | 13 + .../usr/share/konsole/Vapor.colorscheme | 94 +++ .../overlay/usr/share/konsole/Vapor.profile | 6 + .../share/licenses/droiddeck-desktop/NOTICE | 12 + .../share/plasma/desktoptheme/Vapor/colors | 147 +++++ .../plasma/desktoptheme/Vapor/metadata.json | 13 + .../Vapor/widgets/plasmoidheading.svg | 71 +++ .../com.valve.vapor.desktop/contents/defaults | 27 + .../contents/icons/deck_icon.png | Bin 0 -> 4540 bytes .../layouts/org.kde.plasma.desktop-layout.js | 43 ++ .../org.kde.plasma.folder.js | 4 + .../org.kde.plasma.kickoff.js | 3 + .../org.kde.plasma.systemtray.js | 6 + .../contents/previews/preview.png | Bin 0 -> 45238 bytes .../com.valve.vapor.desktop/metadata.json | 22 + .../DroidDeck/contents/images/1920x1080.svg | 26 + .../share/wallpapers/DroidDeck/metadata.json | 8 + tools/desktop-kde/release.env | 4 + tools/desktop-kde/runtime-packages.txt | 165 ++++++ tools/desktop-kde/seeds.txt | 31 + tools/linuxfs/desktop/autostart | 57 -- tools/linuxfs/desktop/droiddeck-desktop | 231 +++----- tools/linuxfs/desktop/droiddeck-desktop-gpu | 9 +- tools/linuxfs/desktop/droiddeck-gpu | 11 +- tools/linuxfs/desktop/kwin_wayland_wrapper | 11 + tools/linuxfs/desktop/panel.conf | 55 -- tools/linuxfs/desktop/rc.xml | 31 - .../overlay/usr/local/bin/droiddeck-session | 4 +- .../usr/local/bin/droiddeck-steam-launch | 37 +- tools/linuxfs/preload/dircache.c | 16 +- tools/linuxfs/preload/links.c | 56 ++ tools/release/update_catalog.py | 2 +- tools/wlroots/build-in-arch.sh | 58 -- ...lkan-allocator-for-non-drm-gpu-nodes.patch | 549 ------------------ tools/wlroots/release.env | 2 - 94 files changed, 1904 insertions(+), 1182 deletions(-) create mode 100644 .github/workflows/build-desktop-kde.yml delete mode 100644 .github/workflows/build-wlroots.yml create mode 100644 app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c create mode 100644 app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h create mode 100644 app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml create mode 100644 app/src/main/cpp/waylandcomp/src/wl_single_pixel.c create mode 100644 tools/desktop-kde/README.md create mode 100644 tools/desktop-kde/archlinuxarm-builder.gpg create mode 100755 tools/desktop-kde/build.sh create mode 100755 tools/desktop-kde/closure.py create mode 100644 tools/desktop-kde/overlay/etc/xdg/baloofilerc create mode 100644 tools/desktop-kde/overlay/etc/xdg/gtk-3.0/settings.ini create mode 100644 tools/desktop-kde/overlay/etc/xdg/kcminputrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/kdeglobals create mode 100644 tools/desktop-kde/overlay/etc/xdg/konsolerc create mode 100644 tools/desktop-kde/overlay/etc/xdg/kscreenlockerrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/ksmserverrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/ksplashrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/kwalletrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/kwinrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/kwinrulesrc create mode 100644 tools/desktop-kde/overlay/etc/xdg/plasmarc create mode 100644 tools/desktop-kde/overlay/etc/xdg/startkderc create mode 100644 tools/desktop-kde/overlay/usr/share/applications/droiddeck-return-gaming.desktop create mode 100644 tools/desktop-kde/overlay/usr/share/applications/steamdeck-bigpicture.desktop create mode 100644 tools/desktop-kde/overlay/usr/share/applications/steamdeck-steam.desktop create mode 100644 tools/desktop-kde/overlay/usr/share/color-schemes/Vapor.colors create mode 100644 tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/actions/steamdeck-gaming-return.svg create mode 100644 tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/apps/steam.svg create mode 100644 tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/places/distributor-logo-steamdeck.svg create mode 100644 tools/desktop-kde/overlay/usr/share/konsole/Vapor.colorscheme create mode 100644 tools/desktop-kde/overlay/usr/share/konsole/Vapor.profile create mode 100644 tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/colors create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/metadata.json create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/widgets/plasmoidheading.svg create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/defaults create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/icons/deck_icon.png create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/layouts/org.kde.plasma.desktop-layout.js create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.folder.js create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.kickoff.js create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.systemtray.js create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/previews/preview.png create mode 100644 tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/metadata.json create mode 100644 tools/desktop-kde/overlay/usr/share/wallpapers/DroidDeck/contents/images/1920x1080.svg create mode 100644 tools/desktop-kde/overlay/usr/share/wallpapers/DroidDeck/metadata.json create mode 100644 tools/desktop-kde/release.env create mode 100644 tools/desktop-kde/runtime-packages.txt create mode 100644 tools/desktop-kde/seeds.txt delete mode 100644 tools/linuxfs/desktop/autostart create mode 100755 tools/linuxfs/desktop/kwin_wayland_wrapper delete mode 100644 tools/linuxfs/desktop/panel.conf delete mode 100644 tools/linuxfs/desktop/rc.xml create mode 100644 tools/linuxfs/preload/links.c delete mode 100755 tools/wlroots/build-in-arch.sh delete mode 100644 tools/wlroots/patches/0001-vulkan-allocator-for-non-drm-gpu-nodes.patch delete mode 100644 tools/wlroots/release.env diff --git a/.github/workflows/build-desktop-kde.yml b/.github/workflows/build-desktop-kde.yml new file mode 100644 index 0000000000..6566d92bd8 --- /dev/null +++ b/.github/workflows/build-desktop-kde.yml @@ -0,0 +1,80 @@ +name: Build desktop package (KDE Plasma) + +# The desktop the app installs into its Linux runtime on first use: KDE Plasma as SteamOS's desktop +# mode has it, the closure of tools/desktop-kde/seeds.txt over Arch Linux ARM minus what the runtime +# already carries, plus tools/desktop-kde/overlay (tools/desktop-kde/build.sh), every package +# signature-checked. Published as a release in Droid-Deck/DroidDeck-Components with DroidDeck's +# other components; the apk pins it in tools/desktop-kde/release.env (tag, sha256, size). A +# published tag is never rebuilt: the packages are Arch Linux ARM's current ones, so a rebuild +# would not match the pinned sha256. A changed package gets a new tag (desktop-kde-r2, ...) and a +# release.env update. +on: + push: + paths: + - 'tools/desktop-kde/**' + - '.github/workflows/build-desktop-kde.yml' + workflow_dispatch: + inputs: + tag: + description: 'Release tag to publish as (e.g. desktop-kde-r1); empty = artifact only' + required: false + default: '' + +permissions: + contents: write + +jobs: + build: + runs-on: ubuntu-24.04 + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + - uses: actions/create-github-app-token@v2 + id: components-token + if: github.event_name == 'workflow_dispatch' && inputs.tag != '' + with: + app-id: ${{ vars.DROIDDECK_CI_APP_ID }} + private-key: ${{ secrets.DROIDDECK_CI_APP_KEY }} + owner: Droid-Deck + repositories: DroidDeck-Components + - name: Refuse a published tag + if: github.event_name == 'workflow_dispatch' && inputs.tag != '' + env: + GH_TOKEN: ${{ steps.components-token.outputs.token }} + run: | + if gh release view '${{ inputs.tag }}' -R Droid-Deck/DroidDeck-Components >/dev/null 2>&1; then + echo "::error::${{ inputs.tag }} is already published, and published tags are never replaced: use a new tag" + exit 1 + fi + - name: Build + run: | + set -euo pipefail + sudo apt-get update -qq && sudo apt-get install -y -qq zstd xz-utils gpgv >/dev/null + tools/desktop-kde/build.sh out + echo "size $(stat -c %s out/desktop-kde.tar.zst) sha256 $(cat out/desktop-kde.sha256)" + # The package itself too, briefly: enough to try a build on a device before publishing it. + - uses: actions/upload-artifact@v4 + with: + name: desktop-kde + retention-days: 7 + compression-level: 0 + path: | + out/desktop-kde.tar.zst + out/desktop-kde.packages.txt + out/desktop-kde.sha256 + - name: Publish + if: github.event_name == 'workflow_dispatch' && inputs.tag != '' + env: + GH_TOKEN: ${{ steps.components-token.outputs.token }} + GH_REPO: Droid-Deck/DroidDeck-Components + run: | + set -euo pipefail + TAG='${{ inputs.tag }}' + if gh release view "$TAG" >/dev/null 2>&1; then + echo "::error::$TAG is already published in DroidDeck-Components, and published tags are never replaced: use a new tag" + exit 1 + fi + gh release create "$TAG" --latest=false --prerelease=false --title "$TAG" \ + --notes "KDE Plasma desktop package for DroidDeck's Linux runtime ($GITHUB_REPOSITORY tools/desktop-kde, $GITHUB_SHA). size $(stat -c %s out/desktop-kde.tar.zst), sha256 $(cat out/desktop-kde.sha256). desktop-kde.packages.txt lists the signature-checked Arch Linux ARM packages it was built from." \ + out/desktop-kde.tar.zst out/desktop-kde.sha256 out/desktop-kde.packages.txt + gh release edit "$TAG" --latest=false --prerelease=false diff --git a/.github/workflows/build-wlroots.yml b/.github/workflows/build-wlroots.yml deleted file mode 100644 index 92780a2a49..0000000000 --- a/.github/workflows/build-wlroots.yml +++ /dev/null @@ -1,49 +0,0 @@ -name: Build wlroots (patched) - -# The desktop's wlroots (Arch Linux ARM's wlroots0.20, what the desktop package's labwc links) -# rebuilt with tools/wlroots/patches: a Vulkan DMA-BUF allocator for the Adreno node, which is not -# a DRM device, so labwc's vulkan and gles2 renderers can start. Built in an Arch Linux ARM -# container on an arm64 runner and published as a release asset the apk build fetches -# (tools/wlroots/release.env names the tag and sha). The hosted desktop package is never changed. -on: - push: - paths: - - 'tools/wlroots/**' - - '.github/workflows/build-wlroots.yml' - workflow_dispatch: - inputs: - tag: - description: 'Release tag to publish as (e.g. wlroots-0.20.2-p1); empty = artifact only' - required: false - default: '' - -permissions: - contents: write - -jobs: - build: - runs-on: ubuntu-24.04-arm - timeout-minutes: 90 - steps: - - uses: actions/checkout@v4 - - name: Build in Arch Linux ARM - run: | - set -euo pipefail - docker run --rm -v "$PWD:/work" -w /work menci/archlinuxarm:base-devel bash tools/wlroots/build-in-arch.sh - sudo chown -R "$(id -u):$(id -g)" . - - uses: actions/upload-artifact@v4 - with: - name: wlroots-patched - path: | - wlroots.tzst - wlroots.tzst.sha256 - - name: Publish - if: github.event_name == 'workflow_dispatch' && inputs.tag != '' - env: - GH_TOKEN: ${{ github.token }} - run: | - set -euo pipefail - gh release create "${{ inputs.tag }}" --latest=false --prerelease=false --title "${{ inputs.tag }}" --notes "wlroots 0.20.2 with tools/wlroots/patches (Vulkan DMA-BUF allocator for non-DRM GPU nodes); staged by the apk at /usr/local/lib/droiddeck-wlroots for the desktop's GPU renderers. sha256 $(cut -d' ' -f1 wlroots.tzst.sha256)" wlroots.tzst wlroots.tzst.sha256 || gh release upload "${{ inputs.tag }}" wlroots.tzst wlroots.tzst.sha256 --clobber - # A component the apk fetches by tag, never something users install: it must not - # become the repo's Latest release or appear as a pre-release, also when re-uploaded. - gh release edit "${{ inputs.tag }}" --latest=false --prerelease=false diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d409218765..ac5144c95b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -216,14 +216,6 @@ jobs: test -f "$D/usr/local/bin/gamescope" rm gamescope.tzst fi - if [ -f tools/wlroots/release.env ]; then - . tools/wlroots/release.env - gh release download "${WLROOTS_TAG}" -R "${GITHUB_REPOSITORY}" -p wlroots.tzst -O wlroots.tzst - echo "${WLROOTS_SHA256} wlroots.tzst" | sha256sum -c - - tar --use-compress-program=unzstd -xf wlroots.tzst -C "$D" - test -f "$D/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" - rm wlroots.tzst - fi . tools/linuxfs/uruntime.env curl -fsSL --retry 3 -o uruntime "https://github.com/VHSgunzo/uruntime/releases/download/${URUNTIME_VERSION}/${URUNTIME_ASSET}" echo "${URUNTIME_SHA256} uruntime" | sha256sum -c - @@ -273,9 +265,7 @@ jobs: install -Dm644 tools/linuxfs/desktop/droiddeck-desktop "$D/usr/local/bin/droiddeck-desktop" install -Dm644 tools/linuxfs/desktop/droiddeck-gpu "$D/usr/local/bin/droiddeck-gpu" install -Dm644 tools/linuxfs/desktop/droiddeck-desktop-gpu "$D/usr/local/bin/droiddeck-desktop-gpu" - install -Dm644 tools/linuxfs/desktop/autostart "$D/etc/xdg/labwc/autostart" - install -Dm644 tools/linuxfs/desktop/rc.xml "$D/etc/xdg/labwc/rc.xml" - install -Dm644 tools/linuxfs/desktop/panel.conf "$D/etc/xdg/lxqt/panel.conf" + install -Dm644 tools/linuxfs/desktop/kwin_wayland_wrapper "$D/usr/local/bin/kwin_wayland_wrapper" install -Dm644 tools/linuxfs/desktop/firefox-droiddeck.js "$D/usr/lib/firefox/defaults/pref/droiddeck.js" ls -lR "$D" diff --git a/app/build.gradle b/app/build.gradle index 98a37d7e1e..bb03f680a9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,6 +59,12 @@ def ciBuild = providers.environmentVariable('GITHUB_ACTIONS').getOrNull() == 'tr def releaseSigner = rootProject.file('keystore/release-signer.sha256').text.trim() // When the source was committed, for "built 3 hours ago": fixed per commit, so it costs no rebuilds. def commitTime = (gitValue(['show', '-s', '--format=%ct', 'HEAD']) ?: '0').with { it.isLong() ? it : '0' } +// The desktop package this apk installs (tools/desktop-kde/release.env): published once per tag in +// DroidDeck-Components, pinned here with its checksum like gamescope and the esync packs, so the +// desktop scripts in the apk always get the package they were made for. +def desktopKde = rootProject.file('tools/desktop-kde/release.env').readLines() + .findAll { it.contains('=') && !it.startsWith('#') } + .collectEntries { def kv = it.split('=', 2); [(kv[0].trim()): kv[1].trim()] } // tools/release/variants.txt as "package=suffix;...", so the updater picks the apk of the package it runs as. def variantSuffixes = rootProject.file('tools/release/variants.txt').readLines() .findAll { it.trim() && !it.startsWith('#') } @@ -88,6 +94,10 @@ android { buildConfigField "String", "RELEASE_SIGNER", "\"${releaseSigner}\"" buildConfigField "long", "COMMIT_TIME", "${commitTime}L * 1000L" buildConfigField "String", "VARIANT_SUFFIXES", "\"${variantSuffixes}\"" + buildConfigField "String", "DESKTOP_KDE_TAG", "\"${desktopKde.DESKTOP_KDE_TAG}\"" + buildConfigField "String", "DESKTOP_KDE_URL", "\"https://github.com/${desktopKde.DESKTOP_KDE_REPO}/releases/download/${desktopKde.DESKTOP_KDE_TAG}/desktop-kde.tar.zst\"" + buildConfigField "String", "DESKTOP_KDE_SHA256", "\"${desktopKde.DESKTOP_KDE_SHA256}\"" + buildConfigField "long", "DESKTOP_KDE_SIZE", "${desktopKde.DESKTOP_KDE_SIZE}L" ndk { abiFilters 'arm64-v8a' diff --git a/app/src/main/cpp/waylandcomp/CMakeLists.txt b/app/src/main/cpp/waylandcomp/CMakeLists.txt index ddf19d05f7..c2f9cb6543 100644 --- a/app/src/main/cpp/waylandcomp/CMakeLists.txt +++ b/app/src/main/cpp/waylandcomp/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(droiddeckwayland SHARED src/wl_clipboard.c src/wl_text_input.c src/wl_toplevel_icon.c + src/wl_single_pixel.c src/wl_color_mgmt.c src/hdr_compose.c src/blend_pass.c @@ -31,6 +32,7 @@ add_library(droiddeckwayland SHARED generated/wlr-data-control-unstable-v1-protocol.c generated/text-input-unstable-v3-protocol.c generated/xdg-toplevel-icon-v1-protocol.c + generated/single-pixel-buffer-v1-protocol.c generated/banner-ahb-v1-protocol.c src/framegen_bridge.c src/framegen_jni.c diff --git a/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c new file mode 100644 index 0000000000..01434221c6 --- /dev/null +++ b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-protocol.c @@ -0,0 +1,61 @@ +/* Generated by wayland-scanner 1.26.0 */ + +/* + * Copyright © 2022 Simon Ser + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "wayland-util.h" + +#ifndef __has_attribute +# define __has_attribute(x) 0 /* Compatibility with non-clang compilers. */ +#endif + +#if (__has_attribute(visibility) || defined(__GNUC__) && __GNUC__ >= 4) +#define WL_PRIVATE __attribute__ ((visibility("hidden"))) +#else +#define WL_PRIVATE +#endif + +extern const struct wl_interface wl_buffer_interface; + +static const struct wl_interface *single_pixel_buffer_v1_types[] = { + &wl_buffer_interface, + NULL, + NULL, + NULL, + NULL, +}; + +static const struct wl_message wp_single_pixel_buffer_manager_v1_requests[] = { + { "destroy", "", single_pixel_buffer_v1_types + 0 }, + { "create_u32_rgba_buffer", "nuuuu", single_pixel_buffer_v1_types + 0 }, +}; + +WL_PRIVATE const struct wl_interface wp_single_pixel_buffer_manager_v1_interface = { + "wp_single_pixel_buffer_manager_v1", 1, + 2, wp_single_pixel_buffer_manager_v1_requests, + 0, NULL, +}; + diff --git a/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h new file mode 100644 index 0000000000..a030d47757 --- /dev/null +++ b/app/src/main/cpp/waylandcomp/generated/single-pixel-buffer-v1-server-protocol.h @@ -0,0 +1,142 @@ +/* Generated by wayland-scanner 1.26.0 */ + +#ifndef SINGLE_PIXEL_BUFFER_V1_SERVER_PROTOCOL_H +#define SINGLE_PIXEL_BUFFER_V1_SERVER_PROTOCOL_H + +#include +#include +#include "wayland-server.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct wl_client; +struct wl_resource; + +/** + * @page page_single_pixel_buffer_v1 The single_pixel_buffer_v1 protocol + * single pixel buffer factory + * + * @section page_desc_single_pixel_buffer_v1 Description + * + * This protocol extension allows clients to create single-pixel buffers. + * + * Compositors supporting this protocol extension should also support the + * viewporter protocol extension. Clients may use viewporter to scale a + * single-pixel buffer to a desired size. + * + * Warning! The protocol described in this file is currently in the testing + * phase. Backward compatible changes may be added together with the + * corresponding interface version bump. Backward incompatible changes can + * only be done by creating a new major version of the extension. + * + * @section page_ifaces_single_pixel_buffer_v1 Interfaces + * - @subpage page_iface_wp_single_pixel_buffer_manager_v1 - global factory for single-pixel buffers + * @section page_copyright_single_pixel_buffer_v1 Copyright + *
+ *
+ * Copyright © 2022 Simon Ser
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ * 
+ */ +struct wl_buffer; +struct wp_single_pixel_buffer_manager_v1; + +#ifndef WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_INTERFACE +#define WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_INTERFACE +/** + * @page page_iface_wp_single_pixel_buffer_manager_v1 wp_single_pixel_buffer_manager_v1 + * @section page_iface_wp_single_pixel_buffer_manager_v1_desc Description + * + * The wp_single_pixel_buffer_manager_v1 interface is a factory for + * single-pixel buffers. + * @section page_iface_wp_single_pixel_buffer_manager_v1_api API + * See @ref iface_wp_single_pixel_buffer_manager_v1. + */ +/** + * @defgroup iface_wp_single_pixel_buffer_manager_v1 The wp_single_pixel_buffer_manager_v1 interface + * + * The wp_single_pixel_buffer_manager_v1 interface is a factory for + * single-pixel buffers. + */ +extern const struct wl_interface wp_single_pixel_buffer_manager_v1_interface; +#endif + +/** + * @ingroup iface_wp_single_pixel_buffer_manager_v1 + * @struct wp_single_pixel_buffer_manager_v1_interface + */ +struct wp_single_pixel_buffer_manager_v1_interface { + /** + * destroy the manager + * + * Destroy the wp_single_pixel_buffer_manager_v1 object. + * + * The child objects created via this interface are unaffected. + */ + void (*destroy)(struct wl_client *client, + struct wl_resource *resource); + /** + * create a 1×1 buffer from 32-bit RGBA values + * + * Create a single-pixel buffer from four 32-bit RGBA values. + * + * Unless specified in another protocol extension, the RGBA values + * use pre-multiplied alpha. + * + * The width and height of the buffer are 1. + * + * The r, g, b and a arguments valid range is from UINT32_MIN (0) + * to UINT32_MAX (0xffffffff). + * + * These arguments should be interpreted as a percentage, i.e. - + * UINT32_MIN = 0% of the given color component - UINT32_MAX = 100% + * of the given color component + * @param r value of the buffer's red channel + * @param g value of the buffer's green channel + * @param b value of the buffer's blue channel + * @param a value of the buffer's alpha channel + */ + void (*create_u32_rgba_buffer)(struct wl_client *client, + struct wl_resource *resource, + uint32_t id, + uint32_t r, + uint32_t g, + uint32_t b, + uint32_t a); +}; + + +/** + * @ingroup iface_wp_single_pixel_buffer_manager_v1 + */ +#define WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_DESTROY_SINCE_VERSION 1 +/** + * @ingroup iface_wp_single_pixel_buffer_manager_v1 + */ +#define WP_SINGLE_PIXEL_BUFFER_MANAGER_V1_CREATE_U32_RGBA_BUFFER_SINCE_VERSION 1 + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml b/app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml new file mode 100644 index 0000000000..bcbd0382eb --- /dev/null +++ b/app/src/main/cpp/waylandcomp/protocols/single-pixel-buffer-v1.xml @@ -0,0 +1,76 @@ + + + + Copyright © 2022 Simon Ser + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + This protocol extension allows clients to create single-pixel buffers. + + Compositors supporting this protocol extension should also support the + viewporter protocol extension. Clients may use viewporter to scale a + single-pixel buffer to a desired size. + + Warning! The protocol described in this file is currently in the testing + phase. Backward compatible changes may be added together with the + corresponding interface version bump. Backward incompatible changes can + only be done by creating a new major version of the extension. + + + + + The wp_single_pixel_buffer_manager_v1 interface is a factory for + single-pixel buffers. + + + + + Destroy the wp_single_pixel_buffer_manager_v1 object. + + The child objects created via this interface are unaffected. + + + + + + Create a single-pixel buffer from four 32-bit RGBA values. + + Unless specified in another protocol extension, the RGBA values use + pre-multiplied alpha. + + The width and height of the buffer are 1. + + The r, g, b and a arguments valid range is from UINT32_MIN (0) + to UINT32_MAX (0xffffffff). + + These arguments should be interpreted as a percentage, i.e. + - UINT32_MIN = 0% of the given color component + - UINT32_MAX = 100% of the given color component + + + + + + + + + diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index faa764565e..805eff9fa1 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -745,6 +745,21 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res if (ci && ci->asked_feedback) ci->shm_frames++; /* since it asked for GPU buffers */ } +/* A single-pixel buffer (wl_single_pixel.c) as a 1x1 image, which the surface's viewport stretches. */ +static void take_pixel(struct surface *s, const uint8_t bgra[4], struct wl_resource *buffer) { + if (s->shm_img && (vkp_image_width(s->shm_img) != 1 || vkp_image_height(s->shm_img) != 1)) { + vkp_image_destroy(s->shm_img); + s->shm_img = NULL; + } + if (!s->shm_img) s->shm_img = vkp_image_create_shm(1, 1); + if (s->shm_img) vkp_image_upload_shm(s->shm_img, bgra, 4); + wl_buffer_send_release(buffer); + s->buf_w = 1; + s->buf_h = 1; + s->buf_alpha = bgra[3] != 0xff; + s->has_content = s->shm_img != NULL; +} + /* The window the app's performance HUD follows: the latest one at least as big as the last to * start presenting GPU frames (take_dmabuf) (X11 binds the HUD to the _MESA_DRV window and counts X presents instead). JNI upcalls. */ static struct surface *g_hud_surface; @@ -1084,6 +1099,7 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) { struct wl_resource *buffer = s->pending_buffer; struct dmabuf_buffer *db = get_dmabuf(buffer); struct wl_shm_buffer *shm = buffer && !db ? wl_shm_buffer_get(buffer) : NULL; + uint8_t pixel[4]; /* The previous content is replaced before reaching the screen. */ feedback_discard_all(&s->feedback); @@ -1113,6 +1129,9 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) { } else if (shm) { drop_dmabuf(s, 1); take_shm(s, shm, buffer); + } else if (single_pixel_buffer_get(buffer, pixel)) { + drop_dmabuf(s, 1); + take_pixel(s, pixel, buffer); } else { drop_dmabuf(s, 1); s->has_content = 0; @@ -3211,7 +3230,7 @@ static void key_event(uint32_t evdev, int pressed) { /* The output changed size under a running session (a foldable opened or closed, with the * session following the screen). Every client that has had its first configure is told the new - * size: gamescope's Wayland backend and the desktop's labwc both resize their output to it, and + * size: gamescope's Wayland backend and the desktop's KWin both resize their output to it, and * the next buffers they commit fill the new panel instead of being letterboxed onto it. */ static void output_resized(void) { struct surface *s; diff --git a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c index 74ddc6adaf..913f713d38 100644 --- a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c +++ b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.c @@ -122,4 +122,5 @@ void droiddeck_ext_init(struct wl_display *display) { clipboard_init(display); text_input_init(display); toplevel_icon_init(display); + single_pixel_init(display); } diff --git a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h index e21014fbf5..0dc8ae41aa 100644 --- a/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h +++ b/app/src/main/cpp/waylandcomp/src/droiddeck_ext.h @@ -48,3 +48,6 @@ void text_input_host_commit(const char *utf8, size_t len); void text_input_host_preedit(const char *utf8, size_t len, int cursor_begin, int cursor_end); void text_input_host_delete(int before, int after); void toplevel_icon_init(struct wl_display *display); +void single_pixel_init(struct wl_display *display); +/* A wp_single_pixel_buffer_manager_v1 buffer: 1 and its pixel (B, G, R, A, premultiplied), else 0. */ +int single_pixel_buffer_get(struct wl_resource *buffer, uint8_t bgra[4]); diff --git a/app/src/main/cpp/waylandcomp/src/wl_single_pixel.c b/app/src/main/cpp/waylandcomp/src/wl_single_pixel.c new file mode 100644 index 0000000000..9dfe78233c --- /dev/null +++ b/app/src/main/cpp/waylandcomp/src/wl_single_pixel.c @@ -0,0 +1,51 @@ +/* + * wp_single_pixel_buffer_manager_v1 (version 1): a wl_buffer of one pixel of one colour, which a + * client stretches over a surface with wp_viewporter. KWin's Wayland backend - the desktop's KWin + * nested in this compositor - refuses to start without it. The surface takes the pixel as a 1x1 + * image, the way it takes a wl_shm buffer (compositor.c, take_pixel). + */ +#include +#include "droiddeck_ext.h" +#include "single-pixel-buffer-v1-server-protocol.h" + +struct single_pixel { + uint8_t bgra[4]; /* premultiplied, as wl_shm's ARGB8888 lies in memory */ +}; + +static void buffer_destroy(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } +static const struct wl_buffer_interface buffer_impl = { .destroy = buffer_destroy }; + +static void buffer_free(struct wl_resource *r) { free(wl_resource_get_user_data(r)); } + +int single_pixel_buffer_get(struct wl_resource *buffer, uint8_t bgra[4]) { + if (!buffer || !wl_resource_instance_of(buffer, &wl_buffer_interface, &buffer_impl)) return 0; + struct single_pixel *p = wl_resource_get_user_data(buffer); + for (int i = 0; i < 4; i++) bgra[i] = p->bgra[i]; + return 1; +} + +static void mgr_destroy(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); } +static void mgr_create_buffer(struct wl_client *c, struct wl_resource *r, uint32_t id, + uint32_t red, uint32_t green, uint32_t blue, uint32_t alpha) { + struct single_pixel *p = calloc(1, sizeof(*p)); + struct wl_resource *buffer = p ? wl_resource_create(c, &wl_buffer_interface, 1, id) : NULL; + if (!buffer) { free(p); wl_client_post_no_memory(c); return; } + /* The protocol's values span the whole u32 range; the top byte is the 8-bit channel. */ + p->bgra[0] = blue >> 24; + p->bgra[1] = green >> 24; + p->bgra[2] = red >> 24; + p->bgra[3] = alpha >> 24; + wl_resource_set_implementation(buffer, &buffer_impl, p, buffer_free); +} +static const struct wp_single_pixel_buffer_manager_v1_interface mgr_impl = { + .destroy = mgr_destroy, .create_u32_rgba_buffer = mgr_create_buffer, +}; +static void bind_manager(struct wl_client *c, void *data, uint32_t ver, uint32_t id) { + struct wl_resource *r = wl_resource_create(c, &wp_single_pixel_buffer_manager_v1_interface, ver, id); + if (!r) { wl_client_post_no_memory(c); return; } + wl_resource_set_implementation(r, &mgr_impl, NULL, NULL); +} + +void single_pixel_init(struct wl_display *display) { + wl_global_create(display, &wp_single_pixel_buffer_manager_v1_interface, 1, NULL, bind_manager); +} diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 0c101a51a5..21b21b8f44 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -343,7 +343,6 @@ class MainActivity : ComponentActivity() { private var steamDownloadsInBackground by mutableStateOf(false) private var oscMode by mutableStateOf(SessionPrefs.OSC_AUTO) private var backActionsInverted by mutableStateOf(false) - private var renderer by mutableStateOf("vulkan") private var gameStorage by mutableStateOf("") private var storageDiagnostics by mutableStateOf(false) private var storageOptions by mutableStateOf>(emptyList()) @@ -1194,7 +1193,6 @@ class MainActivity : ComponentActivity() { directAudio = if (mode == SessionService.MODE_STEAM) directAudio else null, stretch16x9 = if (mode == SessionService.MODE_STEAM) stretch16x9 else null, mic = if (mode == SessionService.MODE_STEAM) mic else null, - renderer = if (mode == SessionService.MODE_DESKTOP) renderer else null, gameStorage = if (mode == SessionService.MODE_STEAM) gameStorage else null, storageOptions = storageOptions, storageDiagnostics = mode == SessionService.MODE_STEAM && storageDiagnostics, @@ -1266,7 +1264,6 @@ class MainActivity : ComponentActivity() { requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), 2) } }, - onRenderer = { r -> SessionPrefs.setDesktopRenderer(this, r); renderer = r }, onGameStorage = { path, label -> setGameStorage(path, label) }, onPickGameStorageFolder = { pickGameStorage.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_pick_game_storage), gameStorage.ifEmpty { null })) @@ -1423,7 +1420,6 @@ class MainActivity : ComponentActivity() { stretch16x9 = SessionPrefs.stretch16x9(this) mic = SessionPrefs.micEnabled(this) refreshWifiDiscovery() - renderer = SessionPrefs.desktopRenderer(this) gameStorage = SessionPrefs.gameStorage(this) storageDiagnostics = SessionPrefs.storageDiagnosticsEnabled(this) settingsMode = mode diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index d706b0622a..c54e26dafb 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -745,9 +745,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } private fun loadingTitle(): String = when (loadingMode()) { - SessionService.MODE_DESKTOP -> - if (!SessionState.running && intent.getStringExtra(SessionService.EXTRA_STEAM_UI) != null) getString(R.string.session_starting_steam_desktop) - else getString(R.string.session_starting_desktop) + // Also when the desktop was asked for with Steam (Big Picture's Switch to Desktop): the client + // does not open on it. + SessionService.MODE_DESKTOP -> getString(R.string.session_starting_desktop) SessionService.MODE_RUN -> { val program = if (SessionState.running) SessionState.program else intent.getStringExtra(SessionService.EXTRA_PROGRAM) com.droiddeck.launcher.frontend.Library.nameForProgram(program)?.let { getString(R.string.session_starting_named, it) } @@ -929,8 +929,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { /** Null when the desktop package is in, else the loading screen's closing line. */ private fun installDesktop(): String? { uiHandler.post { loading.percent = -1; loading.say(getString(DESKTOP_LINES.downloading), DESKTOP_LINES.topic, readable = true) } - val entry = com.droiddeck.launcher.runtime.DesktopCatalog.fetch()?.firstOrNull { it.id == "desktop" } - ?: return getString(R.string.session_desktop_catalog_unreachable) + val entry = com.droiddeck.launcher.runtime.DesktopCatalog.desktopEntry(this) val problem = com.droiddeck.launcher.runtime.DesktopCatalog.install(this, entry, progressFor(DESKTOP_LINES, entry.size / 1_000_000)) return problem?.let { getString(R.string.session_desktop_install_failed, it) } diff --git a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt index c16ad51f85..d23021295d 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/DeviceReport.kt @@ -53,7 +53,7 @@ object DeviceReport { b.append("DroidDeck session report\n") b.append("========================\n") k("Written", SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz", Locale.US).format(Date())) - k("Session mode", when (mode) { SessionService.MODE_DESKTOP -> "desktop (labwc/LXQt)"; SessionService.MODE_RUN -> "a program under gamescope"; else -> "Steam client (gamescope)" }) + k("Session mode", when (mode) { SessionService.MODE_DESKTOP -> "desktop (KDE Plasma)"; SessionService.MODE_RUN -> "a program under gamescope"; else -> "Steam client (gamescope)" }) h("App") runCatching { @@ -189,7 +189,7 @@ object DeviceReport { } for (name in listOf("droiddeck-tu-debug", "droiddeck-driver", "droiddeck-osc", "droiddeck-no-pad", - "droiddeck-no-hud", "droiddeck-wlr-renderer")) { + "droiddeck-no-hud")) { val f = File(Environment.getExternalStorageDirectory(), "Download/$name") if (f.isFile) k(name, FileUtils.readString(f)?.trim()?.replace('\n', ' ')?.ifEmpty { "(present, empty)" } ?: "(present)") } diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/LinuxVulkanDriverManager.java b/app/src/main/java/com/droiddeck/launcher/gpu/LinuxVulkanDriverManager.java index 2b7a5aa252..e5c3d585c0 100644 --- a/app/src/main/java/com/droiddeck/launcher/gpu/LinuxVulkanDriverManager.java +++ b/app/src/main/java/com/droiddeck/launcher/gpu/LinuxVulkanDriverManager.java @@ -25,7 +25,7 @@ import java.util.zip.ZipInputStream; /** * Imported LINUX Vulkan drivers: glibc Turnip ICDs ({@code Turnip-[-variant]-Linux.zip} from * Banners-Turnip) for the runtime - the gamescope session that runs Valve's native ARM64 Steam - * client, and the labwc desktop beside it. This is the driver that DRAWS there: the client's own UI + * client, and the KDE Plasma desktop beside it. This is the driver that DRAWS there: the client's own UI * (OpenGL through the runtime's Zink), every game the client launches (D3D through Proton's * DXVK/VKD3D) and everything on the desktop. Putting the frame on the screen stays the Android * driver's job, in the app's compositor ({@link TurnipDriver}). diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/AppImageManager.kt b/app/src/main/java/com/droiddeck/launcher/runtime/AppImageManager.kt index 59f4f78044..5c63a08599 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/AppImageManager.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/AppImageManager.kt @@ -242,7 +242,7 @@ object AppImageManager { private fun menuEntry(context: Context, id: String) = // A system directory, not ~/.local/share/applications: the desktop copies game entries from - // here into the user's with a GPU launcher (droiddeck-desktop-gpu) while it runs on pixman. + // here into the user's with a GPU launcher (droiddeck-desktop-gpu), since it composites in software. File(LinuxRuntime.rootDir(context), "usr/local/share/applications/droiddeck-appimage-$id.desktop") /** The Linux desktop's menu entry: the desktop's own Exec rules (and droiddeck-gpu) apply to it. */ diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt index 3d70002288..a5d3152970 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BwrapSpawner.kt @@ -232,10 +232,10 @@ object BwrapSpawner { val driver = File(lib, "libvulkan_freedreno.so") if (!driver.isFile || !File("/dev/kgsl-3d0").exists()) return null if (env.containsKey("VK_ICD_FILENAMES") || env.containsKey("VK_DRIVER_FILES")) return null - // A window on the desktop while labwc composites with pixman gets shared-memory buffers - // only; Zink cannot present there, so such an app keeps Flathub's own Mesa (llvmpipe). - // From the desktop, games go through droiddeck-gpu's gamescope and do get the GPU. - if (env["WLR_RENDERER"] == "pixman" && env["GAMESCOPE_WAYLAND_DISPLAY"] == null) return null + // A window on the desktop gets shared-memory buffers only (KWin composites in software); + // Zink cannot present there, so such an app keeps Flathub's own Mesa (llvmpipe). From the + // desktop, games go through droiddeck-gpu's gamescope and do get the GPU. + if (env["BL_DESKTOP"] == "1" && env["GAMESCOPE_WAYLAND_DISPLAY"] == null) return null val binds = ArrayList() for (name in listOf("libvulkan_freedreno.so", "libdisplay-info.so.3", "libSPIRV-Tools.so", "libSPIRV-Tools-opt.so")) { val f = File(lib, name) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt index b077ec7109..5c48fc31ca 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -3,6 +3,7 @@ package com.droiddeck.launcher.runtime import com.droiddeck.launcher.core.Hashes import android.content.Context import android.util.Log +import com.droiddeck.launcher.BuildConfig import com.droiddeck.launcher.R import com.droiddeck.launcher.core.Downloader import com.droiddeck.launcher.core.FileUtils @@ -74,10 +75,32 @@ object DesktopCatalog { installed(context, PROTON_SEED_ID) == null && !File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps/appmanifest_4427310.acf").isFile - // labwc comes with the hosted desktop package itself (its launcher is staged by the app at - // every session, so it cannot tell whether the package is there); SessionFiles uses the same test. - fun desktopInstalled(context: Context): Boolean = - File(LinuxRuntime.rootDir(context), "usr/bin/labwc").isFile + /** + * The desktop: KDE Plasma (tools/desktop-kde). It replaced an LXQt/labwc package of id + * "desktop", which a runtime may still carry; that one does not count, so the first desktop + * after the update installs this one over it. + */ + const val DESKTOP_ID = "desktop-kde" + + /** + * The desktop package this apk installs: pinned in tools/desktop-kde/release.env and published + * in DroidDeck-Components, not read from the catalog, so the desktop scripts staged by this + * apk always get the package they were made for. + */ + fun desktopEntry(context: Context) = Entry( + DESKTOP_ID, context.getString(R.string.content_linux_desktop), 1, BuildConfig.DESKTOP_KDE_TAG, "tar", + BuildConfig.DESKTOP_KDE_URL, BuildConfig.DESKTOP_KDE_SHA256, BuildConfig.DESKTOP_KDE_SIZE, "", + "", "", + ) + + // The desktop package installed to the end: KWin is there and the marker install() writes once + // the whole package is extracted. An install cut short (the app killed, storage full) leaves + // KWin without the marker, and the next desktop installs it again. The launcher is staged by + // the app at every session, so it cannot tell. SessionFiles uses the same test. + fun desktopInstalled(context: Context): Boolean = desktopInstalled(LinuxRuntime.rootDir(context)) + + fun desktopInstalled(root: File): Boolean = + File(root, "usr/bin/kwin_wayland").isFile && File(root, ".droiddeck-pkg-$DESKTOP_ID").isFile /** Downloads, verifies and installs one package. Returns null on success, else a message. */ fun install(context: Context, entry: Entry, listener: LinuxRuntimeInstaller.ProgressListener?): String? { @@ -111,7 +134,12 @@ object DesktopCatalog { "Exec=env APPIMAGE_EXTRACT_AND_RUN=1 /opt/appimages/${entry.id}.AppImage\n" + "Icon=${entry.icon}\nTerminal=false\nCategories=${entry.category};\n") } - else -> if (!LinuxRuntimeInstaller.extract(context, download, root, listener)) return context.getString(R.string.deskpkg_extract_failed) + else -> { + // Not complete until written again at the end: a reinstall cut short must not + // leave the last install's marker saying it is. + marker(context, entry.id).delete() + if (!LinuxRuntimeInstaller.extract(context, download, root, listener)) return context.getString(R.string.deskpkg_extract_failed) + } } FileUtils.writeString(marker(context, entry.id), entry.version) return null diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index a6478cd109..25f24f8300 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -5,6 +5,7 @@ import android.os.Environment import android.util.Log import com.droiddeck.launcher.R import com.droiddeck.launcher.core.FileUtils +import com.droiddeck.launcher.runtime.DesktopCatalog import com.droiddeck.launcher.runtime.LegacyNames import com.droiddeck.launcher.runtime.LinuxRuntime import com.droiddeck.launcher.runtime.ProotFastPath @@ -86,16 +87,15 @@ object SessionFiles { "usr/bin/steamos-polkit-helpers/jupiter-biosupdate" to "usr/bin/steamos-polkit-helpers/jupiter-biosupdate", "usr/bin/steamos-polkit-helpers/jupiter-dock-updater" to "usr/bin/steamos-polkit-helpers/jupiter-dock-updater", ) - // The desktop's launcher and labwc defaults, only where the desktop package is installed: + // The desktop's launchers, only where the desktop package is installed: // staging them into a runtime without it would make the desktop look present when it is not. val desktop = arrayOf( "usr/local/bin/droiddeck-desktop" to "usr/local/bin/droiddeck-desktop", // Games and emulators from the menu, full screen in a gamescope of their own. "usr/local/bin/droiddeck-gpu" to "usr/local/bin/droiddeck-gpu", "usr/local/bin/droiddeck-desktop-gpu" to "usr/local/bin/droiddeck-desktop-gpu", - "etc/xdg/labwc/autostart" to "etc/xdg/labwc/autostart", - "etc/xdg/labwc/rc.xml" to "etc/xdg/labwc/rc.xml", - "etc/xdg/lxqt/panel.conf" to "etc/xdg/lxqt/panel.conf", + // KWin at the app's surface size, for Plasma's session (it starts KWin through this name). + "usr/local/bin/kwin_wayland_wrapper" to "usr/local/bin/kwin_wayland_wrapper", "usr/lib/firefox/defaults/pref/droiddeck.js" to "usr/lib/firefox/defaults/pref/droiddeck.js", ) // The patched gamescope (tools/gamescope): the runtime's own version rebuilt with the ARM64 @@ -116,10 +116,6 @@ object SessionFiles { val msitools = listOf("msiinfo", "cabextract", "libmsi-1.0.so.0", "libgsf-1.so.114", "libgcab-1.0.so.0") .map { "usr/local/lib/droiddeck-msitools/$it" } + listOf("NOTICE", "GPL-2", "GPL-3", "LGPL-2.1").map { "usr/local/share/licenses/droiddeck-msitools/$it" } - // The patched wlroots (tools/wlroots) the desktop loads for its vulkan / gles2 renderers. - val wlroots = if (File(root, "usr/bin/labwc").isFile) { - arrayOf("usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" to "usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so") - } else emptyArray() val fexPreloads = listOf("x86_64", "i386").flatMap { arch -> listOf("libblsession.so", "libfakeinput.so").map { "$arch/$it" to "usr/local/lib/droiddeck-fex/$arch/$it" } } + listOf("libfaultreport.so", "libthunkaudit.so", "libvulkan-thunk.so").map { "x86_64/$it" to "usr/local/lib/droiddeck-fex/x86_64/$it" } @@ -127,11 +123,11 @@ object SessionFiles { "usr/local/bin/gamescope" to "usr/local/bin/gamescope", "usr/local/lib/droiddeck/uruntime" to "usr/local/lib/droiddeck/uruntime", "usr/local/share/licenses/uruntime/LICENSE" to "usr/local/share/licenses/uruntime/LICENSE", - ) + wlroots + mangoapp + msitools.map { it to it } + fexPreloads).filter { (asset, _) -> + ) + mangoapp + msitools.map { it to it } + fexPreloads).filter { (asset, _) -> val dir = asset.substringBeforeLast('/') runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false) } - val all = (if (File(root, "usr/bin/labwc").isFile) files + desktop else files) + optional + val all = (if (DesktopCatalog.desktopInstalled(root)) files + desktop else files) + optional for ((asset, relative) in all) { val target = File(root, relative) val staged = File(target.parentFile, target.name + ".staged") diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt index 896d08a19d..2b8f38eec4 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt @@ -19,7 +19,7 @@ import java.util.Locale * wayland.log the app's compositor * steam.log the Steam client's own log, scrubbed (Steam mode) * steam/ the rest of the client's logs, scrubbed (Steam mode) - * desktop.log labwc, the panel and the programs on it (desktop mode) + * desktop.log KWin, Plasma and the programs on it (desktop mode) * Download/DroidDeck/tools/ one-off commands: Flatpak installs, AppImage imports * ``` * diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index da7dd04f90..f4dcc6df09 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -634,21 +634,6 @@ object SessionPrefs { return Pair(w and 1.inv(), h and 1.inv()) } - /** - * What the desktop shell composites with: vulkan (the default) or gles2 on the GPU, through the - * app's patched wlroots (tools/wlroots) - the Adreno stand-in is not a DRM device and stock - * wlroots cannot allocate on it - or pixman in software. A GPU renderer a device cannot start - * falls back to pixman by itself (droiddeck-desktop). `Download/droiddeck-wlr-renderer` still - * overrides it. - */ - fun desktopRenderer(context: Context): String = prefs(context).getString("desktopRenderer", "vulkan") ?: "vulkan" - - fun setDesktopRenderer(context: Context, renderer: String) { - prefs(context).edit().putString("desktopRenderer", renderer).apply() - // A choice made again is a retry: forget that a renderer failed to start here before. - java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.droiddeck-renderer-failed").delete() - } - /** * HDR10 output for MODE_STEAM / MODE_DESKTOP. Off by default. Honoured only when the panel * lists HDR10 (HdrSupport), and decided when the compositor starts, which is once per app diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 71fd002c6e..da80bf6f71 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -372,16 +372,6 @@ class SessionService : Service() { SessionState.deckPad = false deckBinds = emptyList() if (controllersOn) addControllerEnvironment(guest, fakeInputDir, sessionDir) - // The desktop is wlroots (labwc). Stock wlroots allocates through gbm on a real DRM render - // node, and ours is a KGSL stand-in - labwc died at "unable to create allocator" - so the - // default is pixman (software, shm), where a Vulkan program cannot draw at all; those run - // through droiddeck-gpu instead. vulkan / gles2 use the app's patched wlroots and fall back - // to pixman by themselves. droiddeck-wlr-renderer in Downloads overrides the choice. - if (SessionState.mode == MODE_DESKTOP) { - val override = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-wlr-renderer") - .takeIf { it.isFile }?.let { FileUtils.readString(it)?.trim() } - guest.add("BL_WLR_RENDERER=" + (override?.takeIf { it.isNotEmpty() } ?: SessionPrefs.desktopRenderer(this))) - } // The user's own games, for the runtime's shortcuts writer to put in the client's library // before the client starts (see frontend/AddedGames and droiddeck-steam-shortcuts). if (steamHere) { @@ -397,9 +387,6 @@ class SessionService : Service() { // The second library's name, for droiddeck-steam-library; the bind itself is made below. GameStorage.effective(this)?.let { guest.add("BL_LIBRARY_LABEL=" + it.label.replace('"', ' ')) } if (SessionState.mode == MODE_STEAM && SessionState.steamUi == "desktop") guest.add("BL_STEAM_UI=desktop") - // The desktop asked for with Steam in it (the front end's "Steam Desktop UI", Big Picture's - // "Switch to Desktop"): the desktop's autostart opens the client's desktop UI there. - if (SessionState.mode == MODE_DESKTOP && SessionState.steamUi == "desktop") guest.add("BL_DESKTOP_STEAM=1") val shellGuest = ArrayList(guest).apply { add("SHELL=/bin/bash") add("TERM=xterm-256color") @@ -415,7 +402,7 @@ class SessionService : Service() { } // A program under gamescope: the script's run mode takes the path (an AppImage, a script // or a binary inside the runtime). This is how an emulator gets the GPU - the desktop's - // labwc composites in software and offers no dma-buf, so a Vulkan swapchain cannot exist + // KWin composites in software and offers no dma-buf, so a Vulkan swapchain cannot exist // there (RPCS3 died with VK_ERROR_SURFACE_LOST); gamescope's Xwayland is the path the // Steam games already render through. if (SessionState.mode == MODE_RUN) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt index c2ae73bd1b..8f3f29ec08 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ModeSettingsDialog.kt @@ -76,7 +76,6 @@ class ModeSettings( val backActionsInverted: Boolean = false, val directAudio: Boolean?, val mic: Boolean?, - val renderer: String?, val gameStorage: String? = null, val storageOptions: List = emptyList(), /** Steam only: record allocation and sampled storage timings in the next session's Share logs. */ @@ -140,7 +139,6 @@ class ModeSettingsActions( val onBackActionsInverted: (Boolean) -> Unit = {}, val onDirectAudio: (Boolean) -> Unit, val onMic: (Boolean) -> Unit, - val onRenderer: (String) -> Unit, val onGameStorage: (path: String, label: String) -> Unit = { _, _ -> }, val onPickGameStorageFolder: () -> Unit = {}, val onStorageDiagnostics: (Boolean) -> Unit = {}, @@ -528,14 +526,6 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) { onChange = a.onStorageDiagnostics, ) } - if (!steam && tab == ModeSettingsTab.DISPLAY && s.renderer != null) SettingsGroup(stringResource(R.string.mode_renderer)) { - ChoiceRow( - host, "renderer", stringResource(R.string.mode_desktop_renderer), stringResource(R.string.mode_renderer_hint), - listOf("vulkan" to stringResource(R.string.mode_renderer_vulkan), "gles2" to stringResource(R.string.mode_renderer_gles2), "pixman" to stringResource(R.string.mode_renderer_pixman)), s.renderer, - note = stringResource(R.string.mode_renderer_note), - onPick = a.onRenderer, - ) - } } if (explainWifiDiscovery) AlertDialog( onDismissRequest = { explainWifiDiscovery = false }, diff --git a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java index c40fb1b527..f5b369d069 100644 --- a/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java +++ b/app/src/main/java/com/droiddeck/launcher/wayland/WaylandCompositor.java @@ -314,7 +314,7 @@ public final class WaylandCompositor { /** A new output size for a running compositor ("Follow screen": a foldable opening or closing). * The compositor thread re-sends the wl_output mode and an xdg_toplevel configure to every - * client, and gamescope / labwc resize their output to it. Any thread. */ + * client, and gamescope / the desktop's KWin resize their output to it. Any thread. */ public static native void nativeResizeOutput(int width, int height); /** Re-arms the one-shot first-frame notice, for a new session under a compositor that has diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 391bf585a6..5d391727e1 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -358,13 +358,6 @@ Diagnóstico de almacenamiento Registra los resultados de asignación y muestras de tiempos de almacenamiento en Compartir registros. Puede ralentizar las instalaciones mientras esté activado. Se aplica en la próxima sesión. Elegir una carpeta… - Renderizador - Renderizador del escritorio - Dibuja el escritorio. - vulkan - GPU - gles2 - GPU (experimental) - pixman - software - Vulkan ejecuta los programas del escritorio en la GPU y recurre a pixman si falla. Con pixman, los juegos se abren a pantalla completa en la GPU. Resolución personalizada Sustituye el límite y la relación de aspecto. Un tamaño que no coincide con la pantalla muestra barras. Ancho @@ -894,7 +887,7 @@ Abrir escritorio Instalar y abrir escritorio Escritorio de Linux - LXQt, Firefox y tus emuladores + KDE Plasma, Firefox y tus emuladores Los juegos que instales en Steam aparecen aquí después de la sesión. Jugar en Steam Ajustes de inicio @@ -1259,7 +1252,6 @@ %1$s está en pausa El programa está en pausa - Iniciando Steam en el escritorio Iniciando el escritorio @@ -1285,7 +1277,6 @@ descomprimiendo Proton Experimental (ARM64) No se pudo acceder al catálogo del runtime. Revisa la conexión y vuelve a pulsar Jugar. El runtime de Linux no se instaló. Revisa el runtime en Configuración y vuelve a pulsar Jugar. - No se pudo acceder al catálogo del escritorio. Revisa la conexión y pulsa Escritorio de nuevo. El escritorio no se instaló (%1$s). Revisa la conexión y pulsa Escritorio de nuevo. diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 436b3cc16b..994372ea05 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -303,13 +303,6 @@ Bibliothèque secondaire Les jeux lisant des ressources depuis une carte SD ou un stockage partagé peuvent saccader. Choisir un dossier… - Rendu - Moteur de rendu du bureau - Responsable du dessin du bureau. - vulkan - GPU - gles2 - GPU (expérimental) - pixman - Logiciel - Vulkan utilise le GPU pour exécuter les programmes de bureau, et bascule vers pixman en cas d\'échec. Avec pixman, les jeux s\'exécutent toujours en plein écran sur le GPU. Résolution personnalisée Remplace la limite de résolution et le format d\'écran. Les dimensions ne correspondant pas à l\'écran afficheront des bandes noires. Largeur @@ -785,7 +778,7 @@ Ouvrir le bureau Installer et ouvrir le bureau Bureau Linux - LXQt, Firefox et vos émulateurs + KDE Plasma, Firefox et vos émulateurs Les jeux installés sur Steam apparaîtront ici après la fin de la session. Lancer Steam Paramètres de lancement diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index c10e9c9df4..eb417a2ebc 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -358,13 +358,6 @@ ストレージ診断 割り当て結果とサンプリングしたストレージの処理時間を「ログを共有」に記録します。有効中はインストールが遅くなる場合があります。次回のセッションで有効になります。 フォルダを選択… - レンダラー - デスクトップレンダラー - デスクトップの描画を担当します。 - vulkan - GPU - gles2 - GPU(実験的) - pixman - ソフトウェア - VulkanはGPUを使用してデスクトッププログラムを実行し、失敗した場合はpixmanにフォールバックします。pixmanを使用する場合でも、ゲームはGPU上で全画面実行されます。 カスタム解像度 解像度の上限と画面比率を置き換えます。画面に合わないサイズは黒帯で表示されます。 幅 @@ -886,7 +879,7 @@ デスクトップを開く インストールしてデスクトップを開く Linuxデスクトップ - LXQt、Firefox、エミュレーター + KDE Plasma、Firefox、エミュレーター Steamでインストールされたゲームは、セッション終了後にここに表示されます。 Steamを実行 起動設定 @@ -1237,7 +1230,6 @@ %1$sは一時停止中です プログラムは一時停止中です - デスクトップでSteamを起動中 デスクトップを起動中 @@ -1263,7 +1255,6 @@ Proton Experimental(ARM64)を展開中 実行環境のカタログに接続できませんでした。接続を確認して、もう一度Playをタップしてください。 Linux実行環境をインストールできませんでした。「設定」で実行環境を確認して、もう一度Playをタップしてください。 - デスクトップのカタログに接続できませんでした。接続を確認して、もう一度「デスクトップ」をタップしてください。 デスクトップをインストールできませんでした(%1$s)。接続を確認して、もう一度「デスクトップ」をタップしてください。 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 45a58e066f..768d09d280 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -358,13 +358,6 @@ 저장 공간 진단 할당 결과와 샘플링한 저장 공간 응답 시간을 \"로그 공유\"에 기록합니다. 켜져 있는 동안 설치가 느려질 수 있습니다. 다음 세션에 적용됩니다. 폴더 선택… - 렌더러 - 데스크톱 렌더러 - 데스크톱을 그리는 역할을 합니다. - vulkan - GPU - gles2 - GPU(실험적) - pixman - 소프트웨어 - Vulkan은 GPU를 사용하여 데스크톱 프로그램을 실행하고, 실패하면 pixman으로 대체합니다. pixman을 사용할 때도 게임은 GPU에서 전체 화면으로 실행됩니다. 사용자 정의 해상도 해상도 상한과 화면 비율을 대체합니다. 화면과 일치하지 않는 크기는 검은색 여백으로 표시됩니다. 너비 @@ -886,7 +879,7 @@ 데스크톱 열기 설치 및 데스크톱 열기 Linux 데스크톱 - LXQt, Firefox 및 에뮬레이터 + KDE Plasma, Firefox 및 에뮬레이터 Steam에서 설치한 게임은 세션 종료 후 여기에 표시됩니다. Steam 실행 실행 설정 @@ -1237,7 +1230,6 @@ %1$s 일시 중지됨 프로그램이 일시 중지됨 - 데스크톱에서 Steam 시작 중 데스크톱 시작 중 @@ -1263,7 +1255,6 @@ Proton Experimental(ARM64) 압축 해제 중 런타임 카탈로그에 연결할 수 없습니다. 연결을 확인한 후 \"Steam 실행\"을 다시 누르세요. Linux 런타임이 설치되지 않았습니다. \"설정\"에서 런타임을 확인한 후 \"Steam 실행\"을 다시 누르세요. - 데스크톱 카탈로그에 연결할 수 없습니다. 연결을 확인한 후 \"데스크톱\"을 다시 누르세요. 데스크톱이 설치되지 않았습니다(%1$s). 연결을 확인한 후 \"데스크톱\"을 다시 누르세요. diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 38024f75fd..595673f172 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -333,13 +333,6 @@ Диагностика хранилища Записывает результаты выделения места и выборочные замеры скорости хранилища в журналы. Пока включено, установка может идти медленнее. Вступит в силу в следующей сессии. Выбрать папку… - Рендерер - Рендерер рабочего стола - Отвечает за отрисовку рабочего стола. - vulkan — GPU - gles2 — GPU (экспериментально) - pixman — программный - Vulkan использует GPU для запуска программ рабочего стола, при сбое переключается на pixman. При использовании pixman игры по-прежнему запускаются в полноэкранном режиме на GPU. Пользовательское разрешение Заменяет предел разрешения и соотношение сторон. Размеры, не соответствующие экрану, будут отображаться с черными полосами. Ширина @@ -869,7 +862,7 @@ Открыть рабочий стол Установить и открыть рабочий стол Рабочий стол Linux - LXQt, Firefox и ваши эмуляторы + KDE Plasma, Firefox и ваши эмуляторы Игры, установленные в Steam, появятся здесь после завершения сессии. Запустить Steam Настройки запуска diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 3c130a3b40..0bb996a21c 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -358,13 +358,6 @@ 存储诊断 在分享日志中记录分配结果和存储采样耗时。启用期间可能减慢安装。将在下次会话生效。 选择文件夹… - 渲染器 - 桌面渲染器 - 绘制桌面。 - vulkan - GPU - gles2 - GPU(实验性) - pixman - 软件 - Vulkan 让桌面程序运行在 GPU 上,失败时回退到 pixman。桌面改用 pixman 时,游戏仍会全屏运行在 GPU 上。 自定义分辨率 取代上限与比例。与屏幕不匹配的尺寸会出现黑边。 宽度 @@ -886,7 +879,7 @@ 打开桌面 安装并打开桌面 Linux 桌面 - LXQt、Firefox 和你的模拟器 + KDE Plasma、Firefox 和你的模拟器 你在 Steam 安装的游戏会在会话结束后显示在这里。 游玩 Steam 启动设置 @@ -1237,7 +1230,6 @@ %1$s 已暂停 程序已暂停 - 正在桌面上启动 Steam 正在启动桌面 @@ -1263,7 +1255,6 @@ 正在解包 Proton Experimental(ARM64) 无法连接运行时目录。请检查网络连接,然后再次点“游玩”。 Linux 运行时未能安装。请在“配置”中检查运行时,然后再次点“游玩”。 - 无法连接桌面目录。请检查网络连接,然后再次点“桌面”。 桌面未能安装(%1$s)。请检查网络连接,然后再次点“桌面”。 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index ec76509a7e..cfbd40670a 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -358,13 +358,6 @@ 儲存空間診斷 在分享的記錄中加入空間分配結果和抽樣的儲存時間數據。啟用時可能會減慢安裝速度。下次會話生效。 選擇資料夾… - 渲染器 - 桌面渲染器 - 負責繪製桌面。 - vulkan - GPU - gles2 - GPU(實驗性) - pixman - 軟件 - Vulkan 會用 GPU 執行桌面程式,失敗時改用 pixman。使用 pixman 時,遊戲仍會以全螢幕在 GPU 上執行。 自訂解像度 取代解像度上限和螢幕比例。尺寸與螢幕不符時會顯示黑邊。 寬度 @@ -886,7 +879,7 @@ 開啟桌面 安裝並開啟桌面 Linux 桌面 - LXQt、Firefox 和你的模擬器 + KDE Plasma、Firefox 和你的模擬器 你在 Steam 安裝的遊戲會在會話結束後顯示在這裡。 開啟 Steam 啟動設定 @@ -1237,7 +1230,6 @@ %1$s 已暫停 程式已暫停 - 正在桌面上啟動 Steam 正在啟動桌面 @@ -1263,7 +1255,6 @@ 正在解壓 Proton Experimental (ARM64) 無法連接運行環境目錄。請檢查網絡連線,然後再按「開啟 Steam」。 Linux 運行環境未能安裝。請在「設定」中檢查運行環境,然後再按「開啟 Steam」。 - 無法連接桌面目錄。請檢查網絡連線,然後再按「桌面」。 桌面未能安裝(%1$s)。請檢查網絡連線,然後再按「桌面」。 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 6b715943ee..da2eaeb393 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -358,13 +358,6 @@ 儲存空間診斷 在「分享日誌」中記錄空間配置結果和取樣的儲存存取時間。啟用期間可能會減慢安裝速度。下次工作階段生效。 選擇資料夾… - 渲染器 - 桌面渲染器 - 負責繪製桌面。 - vulkan - GPU - gles2 - GPU(實驗性) - pixman - 軟體 - Vulkan 使用 GPU 執行桌面程式,失敗時退回 pixman。使用 pixman 時,遊戲仍會在 GPU 上全螢幕執行。 自訂解析度 取代解析度上限和螢幕比例。不符合螢幕的尺寸會顯示黑邊。 寬度 @@ -886,7 +879,7 @@ 開啟桌面 安裝並開啟桌面 Linux 桌面 - LXQt、Firefox 和你的模擬器 + KDE Plasma、Firefox 和你的模擬器 在 Steam 中安裝的遊戲會在工作階段結束後顯示在這裡。 執行 Steam 啟動設定 @@ -1237,7 +1230,6 @@ %1$s 已暫停 程式已暫停 - 正在桌面上啟動 Steam 正在啟動桌面 @@ -1263,7 +1255,6 @@ 正在解壓縮 Proton Experimental(ARM64) 無法連線到執行環境目錄。請檢查網路連線,然後再按一次「執行」。 Linux 執行環境未能安裝。請在「設定」中檢查執行環境,然後再按一次「執行」。 - 無法連線到桌面目錄。請檢查網路連線,然後再按一次「桌面」。 桌面未能安裝(%1$s)。請檢查網路連線,然後再按一次「桌面」。 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e21c36943a..34725bd440 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -364,13 +364,6 @@ Storage diagnostics Record allocation results and sampled storage timings in Share logs. May slow installs while enabled. Applies next session. Choose a folder… - Renderer - Desktop renderer - Draws the desktop. - vulkan - GPU - gles2 - GPU (experimental) - pixman - software - Vulkan runs desktop programs on the GPU, falling back to pixman if it fails. On pixman, games open full screen on the GPU. Custom resolution Replaces the cap and ratio. A size that doesn\'t match the screen gets bars. Width @@ -896,7 +889,7 @@ Open desktop Install & open desktop Linux desktop - LXQt, Firefox and your emulators + KDE Plasma, Firefox and your emulators Games you install in Steam show up here after the session. Play Steam Launch settings @@ -1227,7 +1220,6 @@ %1$s is paused The program is paused - Starting Steam on the desktop Starting the desktop Starting %1$s @@ -1251,7 +1243,6 @@ unpacking Proton Experimental (ARM64) Could not reach the runtime catalog. Check the connection and press Play again. The Linux runtime did not install. Check the runtime in Setup and press Play again. - Could not reach the desktop catalog. Check the connection and press Desktop again. The desktop did not install (%1$s). Check the connection and press Desktop again. diff --git a/docs/development/flatpak.md b/docs/development/flatpak.md index 939b8c6be5..7ed3ae7257 100644 --- a/docs/development/flatpak.md +++ b/docs/development/flatpak.md @@ -51,8 +51,8 @@ The spawner also adds what the rootfs gives its own programs: - **GPU.** Flathub's Mesa has Turnip only for DRM, and Adreno on Android is KGSL. The runtime's own `libvulkan_freedreno.so` is bound in with the three libraries the Freedesktop runtime lacks (`libdisplay-info`, SPIRV-Tools). Vulkan uses it, and Mesa's GL runs on it through Zink - (`MESA_LOADER_DRIVER_OVERRIDE=zink`). This is skipped for an app drawing into a desktop - composited by pixman, where Zink cannot present. + (`MESA_LOADER_DRIVER_OVERRIDE=zink`). This is skipped for an app drawing into a window on the + desktop, which KWin composites in software and where Zink cannot present. - **Controllers.** The libraries in the rootfs's `/etc/ld.so.preload` (the session shim and the fake evdev reader) go in through `LD_PRELOAD`, with the session's `dev` directory. - **Browsers.** Firefox's child sandboxes are switched off (`MOZ_DISABLE_*_SANDBOX`), and @@ -70,8 +70,8 @@ screen under gamescope. Under gamescope it passes `--nosocket=wayland --socket=x opens behind gamescope. gamescope's own Wayland socket is no alternative: Chromium on Wayland asks the render node it names for a DRM version, which KGSL cannot give, and aborts. The launcher also starts a session bus when there is none. On the Linux desktop, the apps' exported -menu entries appear in the LXQt menu. While the desktop is composited by pixman, games are -wrapped through `droiddeck-gpu` like the rootfs's own. +menu entries appear in Plasma's launcher. Games are wrapped through `droiddeck-gpu` like the +rootfs's own, since the desktop composites in software. ## The store diff --git a/tools/build_local.sh b/tools/build_local.sh index 3e7767c14f..c446de5ade 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -39,9 +39,9 @@ for tool in curl tar zstd shasum unzip; do exit 1 fi done -if [[ -f "${repo_root}/tools/gamescope/release.env" || -f "${repo_root}/tools/wlroots/release.env" \ +if [[ -f "${repo_root}/tools/gamescope/release.env" \ || -f "${repo_root}/tools/droiddeck-esync/release.env" ]] && ! command -v gh >/dev/null 2>&1; then - echo "GitHub CLI is required to download the pinned Gamescope, wlroots and droiddeck-esync release assets." >&2 + echo "GitHub CLI is required to download the pinned Gamescope and droiddeck-esync release assets." >&2 exit 1 fi @@ -138,9 +138,7 @@ docker run --rm --platform linux/amd64 \ install -Dm644 tools/linuxfs/desktop/droiddeck-desktop "$d/usr/local/bin/droiddeck-desktop" install -Dm644 tools/linuxfs/desktop/droiddeck-gpu "$d/usr/local/bin/droiddeck-gpu" install -Dm644 tools/linuxfs/desktop/droiddeck-desktop-gpu "$d/usr/local/bin/droiddeck-desktop-gpu" - install -Dm644 tools/linuxfs/desktop/autostart "$d/etc/xdg/labwc/autostart" - install -Dm644 tools/linuxfs/desktop/rc.xml "$d/etc/xdg/labwc/rc.xml" - install -Dm644 tools/linuxfs/desktop/panel.conf "$d/etc/xdg/lxqt/panel.conf" + install -Dm644 tools/linuxfs/desktop/kwin_wayland_wrapper "$d/usr/local/bin/kwin_wayland_wrapper" install -Dm644 tools/linuxfs/desktop/firefox-droiddeck.js \ "$d/usr/lib/firefox/defaults/pref/droiddeck.js" @@ -225,14 +223,6 @@ if [[ -f "${repo_root}/tools/gamescope/release.env" ]]; then test -f "${linuxfs_dir}/usr/local/bin/gamescope" fi -if [[ -f "${repo_root}/tools/wlroots/release.env" ]]; then - . "${repo_root}/tools/wlroots/release.env" - wlroots_archive=$(cached "${WLROOTS_SHA256}" wlroots.tzst \ - bash -c 'gh release download "$0" -R "$1" -p wlroots.tzst -O "$out"' "${WLROOTS_TAG}" "${github_repo}") - zstd -dc "${wlroots_archive}" | tar -xf - -C "${linuxfs_dir}" - test -f "${linuxfs_dir}/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so" -fi - . "${repo_root}/tools/linuxfs/uruntime.env" uruntime_binary=$(cached "${URUNTIME_SHA256}" "${URUNTIME_ASSET}" \ bash -c 'curl -fsSL --retry 3 -o "$out" "$0"' "https://github.com/VHSgunzo/uruntime/releases/download/${URUNTIME_VERSION}/${URUNTIME_ASSET}") diff --git a/tools/desktop-kde/README.md b/tools/desktop-kde/README.md new file mode 100644 index 0000000000..4daa7cffef --- /dev/null +++ b/tools/desktop-kde/README.md @@ -0,0 +1,23 @@ +# The desktop package (KDE Plasma) + +The desktop the app installs into its Linux runtime the first time the desktop is opened: KDE Plasma +set up as SteamOS's desktop mode, with Valve's Vapor theme, the Steam Deck wallpaper and launcher +icon, Plasma's default bottom panel, and "Return to Gaming Mode" and Steam on the desktop. + +- `seeds.txt`: the packages asked for. `build.sh` takes their closure over Arch Linux ARM's + repositories (`closure.py`) and drops what the runtime already has. +- `runtime-packages.txt`: the runtime's own packages, `name version` from its pacman database + (`ls /var/lib/pacman/local` in the rootfs). Refresh it when the runtime is rebuilt. +- `overlay/`: our files on top - system-wide KDE defaults in `/etc/xdg`, the Vapor theme and its + layout script, the menu entries Steam and Return to Gaming Mode use. + +`.github/workflows/build-desktop-kde.yml` builds it and, run with a new tag, publishes it as a +release in Droid-Deck/DroidDeck-Components. `release.env` pins the tag, sha256 and size the apk +installs (`DesktopCatalog.desktopEntry`); a published tag is never replaced, so a changed package +is a new tag and a `release.env` update. + +At run time the desktop is started by `tools/linuxfs/desktop/droiddeck-desktop` (the app stages it +at every session): Plasma's own `startplasma-wayland`, with KWin nested in the app's compositor +through `kwin_wayland_wrapper`. KWin composites in software (QPainter) - the Adreno node is not a +DRM device - so games and emulators from the menu go through `droiddeck-gpu`, and Steam goes back +to the app as a Steam session. diff --git a/tools/desktop-kde/archlinuxarm-builder.gpg b/tools/desktop-kde/archlinuxarm-builder.gpg new file mode 100644 index 0000000000000000000000000000000000000000..a4a9a7b55e0ac27051d9c5afe18f7d64fdd5cb27 GIT binary patch literal 2228 zcmV;l2ut^w0u2OG+XV9g5CF{wWx|A%DU}Qx0m*#ANNhcmsLW__QG>n(Q3dAmqs7B> zc^-RmIQ6~cu;HU768$$aKu3$xvdT@cDMM^VCSyaZ;_SdYkPuyk3?+%Q8J z3I0=zOcL~a61_vFbzwS1Xq%>Xkf-0m3uDKO{ZO5D^4+~{1BuO>awI>&>cW?Zts?h@ zbcQI&_piYJN-1@b^)*wO<(EvP))8m{^-ACcguTVbt1ZF6HX7C}qKcuhnACEJuDlVM zT-!^AjBm!U(sBFyK&fQTTgwS1x_)3}|I@pN+9Xek_|^gIV2U2GmS)?M$qJi@P$TOo z&0$V$49|Pib(GTIoxzvK6PA)o#+A2ORdy#mBA@Beejn*sE;O%&9Oq74{*2GgyZ>98 zH}sP7ZRzuf;iPmFP}U&fF$1=jIuxX)6vW3!Ggp!J-l$QVz0RRECHbHV@Xdp~!ZgqGdK~ha1LUn0uWFS*{b97~GAUtAqX>4R= zazJ5nV`yw?ZgqHJa&0bea%Vn?0yhK`0SW*i1p-pr1oHwL0|g5S2nPcN6$%Lm3k4Pe z0|5X69svRufB*^!5O*0r6)W53ripA1|3d%kPoIYs0uIbF@G`aj1H1hGi0c1sM$dHe z^WhQ7_HVWg4rzWuVc7evQ(&^35;aI5VD!q$;Jc!M7o%jbEZ|HUOTCk{;x}8CbVR!*Avrb_) z{m6e4n4m1~_LM9IcUZena!8eF-?c{sWfG%kz3iT*F@(46MLGLXJ?z~Oea#7$^tF?Q zjQnHNxGkz&S)4ZnMG^Vd6^vOHimm}^{NWnd#8JTi*9H#ZW+CGQMG_HQ z7*A$%n#{XTOP5~nOAgz(s&VQ1Gf8QK=tPKTl>_MNP-wgkD}z4dInxw922n8ZppT#y z@(JC}E_VJcjWHZMMg85MyVu63f&GmUUsMe4iyFk z%IzfsVijp-mUR~Q1)vGnB>)CaqF7YYI-E8okSeL3PxHtV89Ma6TS}C5x0K?ra^Z0) z4iRgB`Te*Gt?~@6!g#3TKVuxVM?QHkT z3-VMv0IYbnoJ{ybL?}NUQX9W=NN`S`9@)p1Sd(J|+1Ch)sZ&hnS9mwD4cMqijf387y8Y1z=-I0&*EbH%$*PGp&g~8$5#S`D2=isZ zYF^2f4M93=%Hyh8?rXv5O`@%7d8@}FnSPi&yW2O=kpn0Vrk>|jyRqNiWVfYMGat$f zZ6`+v3*vPR`m}ieYus5*)2%o{-8$BMsn27?pq2n1o5nl4~iC{`vg-J zJ|JKnt!W{y-LYPkxXj<%iWE#g01*KI0f_=11Q-Dd00{*GQriUc0vikf3JDN*89x;( z+vcWM{ty3pnya?w>`Ug9a64M>uF^68kL;i)W-Cz&Oj}a_eUAw~5|$+e$naunaVT;b ze!keXC#G`Jg77f6GqU74te>4g3ix7O!2w!Zyx852qVLdS_4BWCVX{(p(62eeXx&J& zTdR&8fii%HDrvrGxYXyzPitPb;A(UFX*Rc4oB@P&Kd&$t;9Lh3>gIih2$trOXK++G zEe8gtI5KtYae1j+LHIu|#-EbgxLiu}87tjP_cZ!@Zx(PwbdiYRl-%3u7=6`S1z^nE zv}JsvcpS<=*{t;U{L~;JKD%=>dNuBVjP}!%nOf={%fzAtkVb&Z_VQ_x=sx6l z*cbGZO$eF%Q{lc^YqdYu0a^7RlZNK_*4Vy9bc?vlkpgZLnhSep;f_QwiHHs%p0*WX zy46|N5QBvSBk21~MWM|5x2y_3l2?flUG5_ItmyNfR?s>-Zy8iI@q+;~ddeztZR4d% z8s5SrytR=ITz;F2MX7fGjS=J0Q1*#C+#mp>0Kn&e9{mY0%sW0RX@6x7kGP0 CyEc6Q literal 0 HcmV?d00001 diff --git a/tools/desktop-kde/build.sh b/tools/desktop-kde/build.sh new file mode 100755 index 0000000000..fccccd4e5a --- /dev/null +++ b/tools/desktop-kde/build.sh @@ -0,0 +1,73 @@ +#!/bin/bash +# Builds the KDE Plasma desktop package for the Linux runtime: the closure of seeds.txt over Arch +# Linux ARM's aarch64 repositories, minus what the runtime image already carries, plus overlay/, +# as a zstd tarball that extracts over the rootfs (DesktopCatalog's "tar" kind). +# +# tools/desktop-kde/build.sh +# +# The runtime's own packages (runtime-packages.txt, "name version" from its pacman db) are not +# shipped again, except where something in the closure asks for a version of one the runtime does +# not have (a versioned dependency on it, or on a library it provides): then the repository's +# build comes along and replaces it. Nothing is deleted on extraction, so a library whose soname +# moved keeps its old file for what the runtime linked against it. +# +# Every package is fetched over HTTPS and must carry a valid signature by the Arch Linux ARM Build +# System key (archlinuxarm-builder.gpg; primary fingerprint pinned below, as archlinuxarm.org +# publishes it and archlinuxarm-keyring trusts it) and match the sha256 the repository database +# gives for it; anything else stops the build. The repository databases themselves are not signed +# by Arch Linux ARM, so they decide only which signed packages are taken. desktop-kde.packages.txt +# lists every package taken, with its sha256: what a release was built from. +# +# Needs curl, gpgv, tar, xz, zstd, python3. +set -euo pipefail +out=${1:?out dir} +here=$(cd "$(dirname "$0")" && pwd) +mirror=${ALARM_MIRROR:-https://fl.us.mirror.archlinuxarm.org/aarch64} +case $mirror in https://*) ;; *) echo "ALARM_MIRROR must be https" >&2; exit 1 ;; esac +builder=68B3537F39A313B3E574D06777193F152BDBE6A6 +fetch() { curl -fsSL --proto '=https' --proto-redir '=https' --retry 6 --retry-delay 5 --retry-all-errors -o "$1" "$2"; } +work=$(mktemp -d); mkdir -p "$out" "$work/db" "$work/root" +# PKG_CACHE keeps the downloaded packages between builds (a local rebuild after an overlay change). +pkgs=${PKG_CACHE:-$work/pkgs}; mkdir -p "$pkgs" +trap 'rm -rf "$work"' EXIT + +for repo in core extra alarm; do + fetch "$work/db/$repo.db" "$mirror/$repo/$repo.db" + mkdir -p "$work/db/x_$repo" && tar -xzf "$work/db/$repo.db" -C "$work/db/x_$repo" +done + +python3 "$here/closure.py" "$work/db" "$here/runtime-packages.txt" "$here/seeds.txt" > "$work/pkglist.txt" +echo "kde: $(wc -l < "$work/pkglist.txt") packages beyond the runtime's own" + +while read -r entry sum; do + file=${entry#*/} + [ -s "$pkgs/$file" ] || fetch "$pkgs/$file" "$mirror/$entry" + fetch "$work/pkg.sig" "$mirror/$entry.sig" + if ! gpgv --status-fd 1 --keyring "$here/archlinuxarm-builder.gpg" "$work/pkg.sig" "$pkgs/$file" 2>/dev/null \ + | grep -Eq "^\[GNUPG:\] VALIDSIG .* $builder\$"; then + echo "$file: no valid Arch Linux ARM Build System signature" >&2; rm -f "$pkgs/$file"; exit 1 + fi + if [ "$(sha256sum "$pkgs/$file" | cut -d' ' -f1)" != "$sum" ]; then + echo "$file: sha256 differs from the repository database's" >&2; rm -f "$pkgs/$file"; exit 1 + fi + tar -xf "$pkgs/$file" -C "$work/root" --no-same-owner --no-same-permissions \ + --exclude=.PKGINFO --exclude=.MTREE --exclude=.INSTALL --exclude=.BUILDINFO --exclude=.CHANGELOG +done < "$work/pkglist.txt" + +cp -a "$here/overlay/." "$work/root/" + +# Weight that no desktop on a phone reads. +rm -rf "$work/root/usr/share/doc" "$work/root/usr/share/man" "$work/root/usr/share/info" \ + "$work/root/usr/share/gtk-doc" "$work/root/usr/include" "$work/root/usr/lib/pkgconfig" \ + "$work/root/usr/lib/cmake" "$work/root/usr/share/help" +# English only, on purpose: the desktop session runs in C.UTF-8 and nothing hands the app's language +# to KDE, so other translations would be weight no one sees. They come back with localizing the +# desktop itself (LANGUAGE from the app's language at session start). +find "$work/root/usr/share/locale" -mindepth 1 -maxdepth 1 -type d ! -name 'en*' -exec rm -rf {} + 2>/dev/null || true +find "$work/root/usr/lib" -name '*.a' -delete +chmod -R u+rwX "$work/root" + +sed 's#^[a-z]*/##' "$work/pkglist.txt" > "$out/desktop-kde.packages.txt" # "file sha256", each verified above +tar -C "$work/root" --zstd -cf "$out/desktop-kde.tar.zst" . +sha256sum "$out/desktop-kde.tar.zst" | awk '{print $1}' > "$out/desktop-kde.sha256" +ls -l "$out/desktop-kde.tar.zst" diff --git a/tools/desktop-kde/closure.py b/tools/desktop-kde/closure.py new file mode 100755 index 0000000000..b0475cf6e6 --- /dev/null +++ b/tools/desktop-kde/closure.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python3 +"""The packages the KDE desktop package ships: the dependency closure of the seeds over Arch Linux +ARM's extracted repository databases, minus what the runtime already has at a version that serves. + + closure.py + +Prints one "repo/filename sha256" per package to ship (the sha256 the repository database gives). +""" +import collections +import os +import re +import sys + +REPOS = ("core", "extra", "alarm") + + +def vercmp_part(a, b): + # rpmvercmp, as pacman compares version strings. + if a == b: + return 0 + sa = re.findall(r"\d+|[a-zA-Z]+", a) + sb = re.findall(r"\d+|[a-zA-Z]+", b) + for x, y in zip(sa, sb): + if x.isdigit() and y.isdigit(): + x, y = int(x), int(y) + if x != y: + return 1 if x > y else -1 + elif x.isdigit(): + return 1 + elif y.isdigit(): + return -1 + elif x != y: + return 1 if x > y else -1 + if len(sa) == len(sb): + return 0 + return 1 if len(sa) > len(sb) else -1 + + +def vercmp(a, b): + def split(v): + epoch, _, rest = v.rpartition(":") if ":" in v else ("0", "", v) + ver, _, rel = rest.partition("-") + return int(epoch or 0), ver, rel + ea, va, ra = split(a) + eb, vb, rb = split(b) + if ea != eb: + return 1 if ea > eb else -1 + c = vercmp_part(va, vb) + if c or not ra or not rb: + return c + return vercmp_part(ra, rb) + + +def parse_dep(d): + m = re.match(r"^([^<>=]+)(>=|<=|=|>|<)?(.*)$", d) + return m.group(1), m.group(2), m.group(3) + + +def satisfies(version, op, want): + if not op: + return True + c = vercmp(version, want) + return {"=": c == 0, ">=": c >= 0, "<=": c <= 0, ">": c > 0, "<": c < 0}[op] + + +def main(): + dbdir, runtime_file, seeds_file = sys.argv[1:4] + runtime = {} + for line in open(runtime_file): + parts = line.split() + if len(parts) == 2: + runtime[parts[0]] = parts[1] + seeds = [l.strip() for l in open(seeds_file) if l.strip() and not l.lstrip().startswith("#")] + + pkgs, providers = {}, collections.defaultdict(list) + for repo in REPOS: + root = os.path.join(dbdir, "x_" + repo) + for entry in sorted(os.listdir(root)): + fields, key = {}, None + for fname in ("desc", "depends"): + path = os.path.join(root, entry, fname) + if not os.path.exists(path): + continue + for line in open(path, encoding="utf-8", errors="replace"): + line = line.rstrip("\n") + if line.startswith("%") and line.endswith("%"): + key = line.strip("%") + fields[key] = [] + elif line == "": + key = None + elif key: + fields[key].append(line) + name = fields.get("NAME", [None])[0] + if not name or name in pkgs: + continue + provides = [parse_dep(p) for p in fields.get("PROVIDES", [])] + pkgs[name] = {"repo": repo, "file": fields["FILENAME"][0], "version": fields["VERSION"][0], + "sha256": fields.get("SHA256SUM", [""])[0], + "depends": fields.get("DEPENDS", []), "provides": provides} + providers[name].append((name, pkgs[name]["version"])) + for pname, _, pver in provides: + providers[pname].append((name, pver or pkgs[name]["version"])) + + def resolve(dep): + """The package to take for a dependency string, and whether the runtime's copy serves.""" + name, op, want = parse_dep(dep) + # A runtime package that is the dependency or provides it, at a version that serves. + if name in runtime and satisfies(runtime[name], op, want): + return name, True + candidates = providers.get(name, []) + for real, _ in candidates: + if real in runtime: + # A provided name (a soname, mostly): the runtime's build serves as long as it is + # the repository's own, or nothing asks for a particular version. + if not op or runtime[real] == pkgs[real]["version"]: + return real, True + if satisfies_provider(real, name, op, want): + return real, False + for real, pver in candidates: + if satisfies(pver, op, want): + return real, False + return None, False + + def satisfies_provider(real, name, op, want): + if real == name: + return satisfies(pkgs[real]["version"], op, want) + return any(p == name and satisfies(v or pkgs[real]["version"], op, want) for p, _, v in pkgs[real]["provides"]) + + ship, seen, missing = [], set(), [] + queue = list(seeds) + while queue: + dep = queue.pop() + real, served = resolve(dep) + if real is None: + missing.append(dep) + continue + if served or real in seen: + continue + seen.add(real) + ship.append(real) + queue.extend(pkgs[real]["depends"]) + if missing: + sys.exit("unresolved: " + " ".join(sorted(set(missing)))) + upgraded = sorted(n for n in ship if n in runtime) + if upgraded: + print("replacing runtime packages: " + " ".join(f"{n} {runtime[n]} -> {pkgs[n]['version']}" for n in upgraded), + file=sys.stderr) + for name in sorted(ship): + print(pkgs[name]["repo"] + "/" + pkgs[name]["file"], pkgs[name]["sha256"]) + + +if __name__ == "__main__": + main() diff --git a/tools/desktop-kde/overlay/etc/xdg/baloofilerc b/tools/desktop-kde/overlay/etc/xdg/baloofilerc new file mode 100644 index 0000000000..1735f7a8d4 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/baloofilerc @@ -0,0 +1,2 @@ +[Basic Settings] +Indexing-Enabled=false diff --git a/tools/desktop-kde/overlay/etc/xdg/gtk-3.0/settings.ini b/tools/desktop-kde/overlay/etc/xdg/gtk-3.0/settings.ini new file mode 100644 index 0000000000..3d33a8a939 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/gtk-3.0/settings.ini @@ -0,0 +1,5 @@ +[Settings] +gtk-theme-name=Breeze-Dark +gtk-icon-theme-name=breeze-dark +gtk-font-name=Noto Sans 11 +gtk-application-prefer-dark-theme=true diff --git a/tools/desktop-kde/overlay/etc/xdg/kcminputrc b/tools/desktop-kde/overlay/etc/xdg/kcminputrc new file mode 100644 index 0000000000..bdac20ee38 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/kcminputrc @@ -0,0 +1,5 @@ +[Mouse] +cursorTheme=breeze_cursors + +[Libinput][Defaults] +PointerAccelerationProfile=1 diff --git a/tools/desktop-kde/overlay/etc/xdg/kdeglobals b/tools/desktop-kde/overlay/etc/xdg/kdeglobals new file mode 100644 index 0000000000..d8ba273a48 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/kdeglobals @@ -0,0 +1,24 @@ +[KDE] +LookAndFeelPackage=com.valve.vapor.desktop +SingleClick=false +widgetStyle=Breeze + +[General] +ColorScheme=Vapor +font=Noto Sans,11,-1,5,50,0,0,0,0,0 +menuFont=Noto Sans,11,-1,5,50,0,0,0,0,0 +smallestReadableFont=Noto Sans,9,-1,5,50,0,0,0,0,0 +toolBarFont=Noto Sans,11,-1,5,50,0,0,0,0,0 +fixed=Noto Sans Mono,11,-1,5,50,0,0,0,0,0 + +[Icons] +Theme=breeze-dark + +[KDE Action Restrictions][$i] +action/switch_user=false +action/start_new_session=false +action/lock_screen=false + +[KDE Control Module Restrictions][$i] +kcm_sddm.desktop=false +kcm_plymouth.desktop=false diff --git a/tools/desktop-kde/overlay/etc/xdg/konsolerc b/tools/desktop-kde/overlay/etc/xdg/konsolerc new file mode 100644 index 0000000000..f93c61af6f --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/konsolerc @@ -0,0 +1,2 @@ +[Desktop Entry] +DefaultProfile=Vapor.profile diff --git a/tools/desktop-kde/overlay/etc/xdg/kscreenlockerrc b/tools/desktop-kde/overlay/etc/xdg/kscreenlockerrc new file mode 100644 index 0000000000..8a52bc54ce --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/kscreenlockerrc @@ -0,0 +1,3 @@ +[Daemon] +Autolock=false +LockOnResume=false diff --git a/tools/desktop-kde/overlay/etc/xdg/ksmserverrc b/tools/desktop-kde/overlay/etc/xdg/ksmserverrc new file mode 100644 index 0000000000..b47cc8917c --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/ksmserverrc @@ -0,0 +1,2 @@ +[General] +loginMode=emptySession diff --git a/tools/desktop-kde/overlay/etc/xdg/ksplashrc b/tools/desktop-kde/overlay/etc/xdg/ksplashrc new file mode 100644 index 0000000000..c252676bf0 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/ksplashrc @@ -0,0 +1,3 @@ +[KSplash] +Engine=none +Theme=None diff --git a/tools/desktop-kde/overlay/etc/xdg/kwalletrc b/tools/desktop-kde/overlay/etc/xdg/kwalletrc new file mode 100644 index 0000000000..3f28331ef6 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/kwalletrc @@ -0,0 +1,3 @@ +[Wallet] +Enabled=false +First Use=false diff --git a/tools/desktop-kde/overlay/etc/xdg/kwinrc b/tools/desktop-kde/overlay/etc/xdg/kwinrc new file mode 100644 index 0000000000..f9656dd00e --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/kwinrc @@ -0,0 +1,12 @@ +[org.kde.kdecoration2] +library=org.kde.breeze + +[WindowSwitcher] +LayoutName=org.kde.breeze.desktop + +[DesktopSwitcher] +LayoutName=org.kde.breeze.desktop + +[Plugins] +blurEnabled=false +contrastEnabled=false diff --git a/tools/desktop-kde/overlay/etc/xdg/kwinrulesrc b/tools/desktop-kde/overlay/etc/xdg/kwinrulesrc new file mode 100644 index 0000000000..80b8acdbd4 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/kwinrulesrc @@ -0,0 +1,32 @@ +[$Version] +update_info=kwinrules.upd:replace-placement-string-to-enum,kwinrules.upd:use-virtual-desktop-ids + +[1] +Description=Window settings for Steam Keyboard +above=true +aboverule=2 +skiptaskbar=true +skiptaskbarrule=2 +title=Steam Keyboard +titlematch=2 +type=16 +typerule=2 +wmclass=steam +wmclasscomplete=true +wmclassmatch=2 + +[9bd6e503-ac60-4e7b-9131-0b10c84b37eb] +Description=Window settings for Steam Keyboard +above=true +aboverule=2 +title=Steam Keyboard +titlematch=2 +type=16 +typerule=2 +wmclass=steam +wmclasscomplete=true +wmclassmatch=2 + +[General] +count=1 +rules=1 diff --git a/tools/desktop-kde/overlay/etc/xdg/plasmarc b/tools/desktop-kde/overlay/etc/xdg/plasmarc new file mode 100644 index 0000000000..c2a661e5d6 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/plasmarc @@ -0,0 +1,2 @@ +[Theme] +name=Vapor diff --git a/tools/desktop-kde/overlay/etc/xdg/startkderc b/tools/desktop-kde/overlay/etc/xdg/startkderc new file mode 100644 index 0000000000..2ebf976ad5 --- /dev/null +++ b/tools/desktop-kde/overlay/etc/xdg/startkderc @@ -0,0 +1,2 @@ +[General] +systemdBoot=false diff --git a/tools/desktop-kde/overlay/usr/share/applications/droiddeck-return-gaming.desktop b/tools/desktop-kde/overlay/usr/share/applications/droiddeck-return-gaming.desktop new file mode 100644 index 0000000000..67f1d7f218 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/applications/droiddeck-return-gaming.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Name=Return to Gaming Mode +Comment=Log out of the desktop and go back to Steam +Exec=dbus-send --session --type=method_call --dest=org.kde.Shutdown /Shutdown org.kde.Shutdown.logout +Icon=steamdeck-gaming-return +Terminal=false +Type=Application +StartupNotify=false +NoDisplay=true diff --git a/tools/desktop-kde/overlay/usr/share/applications/steamdeck-bigpicture.desktop b/tools/desktop-kde/overlay/usr/share/applications/steamdeck-bigpicture.desktop new file mode 100644 index 0000000000..98c3b27b10 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/applications/steamdeck-bigpicture.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Type=Application +Name=Steam Big Picture +Comment=The Steam client, full screen with a controller +Exec=/usr/local/bin/steamdeck-steam bigpicture +Icon=steam +Terminal=false +Categories=Game; diff --git a/tools/desktop-kde/overlay/usr/share/applications/steamdeck-steam.desktop b/tools/desktop-kde/overlay/usr/share/applications/steamdeck-steam.desktop new file mode 100644 index 0000000000..794c1e4482 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/applications/steamdeck-steam.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Type=Application +Name=Steam +Comment=The Steam client, in a window +Exec=/usr/local/bin/steamdeck-steam desktop +Icon=steam +Terminal=false +Categories=Game; diff --git a/tools/desktop-kde/overlay/usr/share/color-schemes/Vapor.colors b/tools/desktop-kde/overlay/usr/share/color-schemes/Vapor.colors new file mode 100644 index 0000000000..820af1df32 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/color-schemes/Vapor.colors @@ -0,0 +1,119 @@ +[ColorEffects:Disabled] +Color=56,56,56 +ColorAmount=0 +ColorEffect=0 +ContrastAmount=0.65 +ContrastEffect=1 +IntensityAmount=0.1 +IntensityEffect=2 + +[ColorEffects:Inactive] +ChangeSelectionColor=true +Color=112,111,110 +ColorAmount=0.025 +ColorEffect=2 +ContrastAmount=0.1 +ContrastEffect=2 +Enable=false +IntensityAmount=0 +IntensityEffect=0 + +[Colors:Button] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=239,240,241 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:Complementary] +BackgroundAlternate=59,64,69 +BackgroundNormal=49,54,59 +DecorationFocus=30,146,255 +DecorationHover=61,174,230 +ForegroundActive=246,116,0 +ForegroundInactive=175,176,179 +ForegroundLink=61,174,230 +ForegroundNegative=237,21,21 +ForegroundNeutral=201,206,59 +ForegroundNormal=239,240,241 +ForegroundPositive=17,209,22 +ForegroundVisited=61,174,230 + +[Colors:Selection] +BackgroundAlternate=29,153,243 +BackgroundNormal=86,159,204 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=252,252,252 +ForegroundInactive=239,240,241 +ForegroundLink=253,188,75 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=31,31,38 +ForegroundPositive=126,166,75 +ForegroundVisited=189,195,199 + +[Colors:Tooltip] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=239,240,241 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:View] +BackgroundAlternate=26,33,42 +BackgroundNormal=21,28,35 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=239,240,241 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:Window] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=241,241,242 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[General] +ColorScheme=Breeze Dark +Name=Vapor +shadeSortColumn=true + +[KDE] +contrast=4 + +[WM] +activeBackground=36,39,47 +activeBlend=255,255,255 +activeForeground=239,240,241 +inactiveBackground=36,39,44 +inactiveBlend=74,83,101 +inactiveForeground=127,140,141 diff --git a/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/actions/steamdeck-gaming-return.svg b/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/actions/steamdeck-gaming-return.svg new file mode 100644 index 0000000000..5cff1ce9d8 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/actions/steamdeck-gaming-return.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/apps/steam.svg b/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/apps/steam.svg new file mode 100644 index 0000000000..d5926b262e --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/apps/steam.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/places/distributor-logo-steamdeck.svg b/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/places/distributor-logo-steamdeck.svg new file mode 100644 index 0000000000..082485bf1a --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/icons/hicolor/scalable/places/distributor-logo-steamdeck.svg @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/tools/desktop-kde/overlay/usr/share/konsole/Vapor.colorscheme b/tools/desktop-kde/overlay/usr/share/konsole/Vapor.colorscheme new file mode 100644 index 0000000000..91f6921c8f --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/konsole/Vapor.colorscheme @@ -0,0 +1,94 @@ +[Background] +Color=21,27,33 + +[BackgroundFaint] +Color=22,32,45 + +[BackgroundIntense] +Color=19,46,69 + +[Color0] +Color=21,27,33 + +[Color0Faint] +Color=49,54,59 + +[Color0Intense] +Color=127,140,141 + +[Color1] +Color=237,21,21 + +[Color1Faint] +Color=120,50,40 + +[Color1Intense] +Color=192,57,43 + +[Color2] +Color=71,146,176 + +[Color2Faint] +Color=52,108,146 + +[Color2Intense] +Color=171,207,228 + +[Color3] +Color=246,116,0 + +[Color3Faint] +Color=182,86,25 + +[Color3Intense] +Color=253,188,75 + +[Color4] +Color=29,153,243 + +[Color4Faint] +Color=27,102,143 + +[Color4Intense] +Color=61,174,233 + +[Color5] +Color=155,89,182 + +[Color5Faint] +Color=97,74,115 + +[Color5Intense] +Color=142,68,173 + +[Color6] +Color=227,255,194 + +[Color6Faint] +Color=91,118,56 + +[Color6Intense] +Color=141,189,85 + +[Color7] +Color=239,240,241 + +[Color7Faint] +Color=99,104,109 + +[Color7Intense] +Color=252,252,252 + +[Foreground] +Color=239,240,241 + +[ForegroundFaint] +Color=220,230,231 + +[ForegroundIntense] +Color=252,252,252 + +[General] +Description=SteamOS +Opacity=0.9 +Wallpaper= diff --git a/tools/desktop-kde/overlay/usr/share/konsole/Vapor.profile b/tools/desktop-kde/overlay/usr/share/konsole/Vapor.profile new file mode 100644 index 0000000000..b4557e15e1 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/konsole/Vapor.profile @@ -0,0 +1,6 @@ +[Appearance] +ColorScheme=Vapor + +[General] +Name=Vapor +Parent=FALLBACK/ diff --git a/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE b/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE new file mode 100644 index 0000000000..c13011cd1b --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE @@ -0,0 +1,12 @@ +Third-party files in DroidDeck's KDE desktop package (tools/desktop-kde/overlay): + +- Vapor (color scheme, Plasma theme, look-and-feel package, Konsole profile and color scheme) and the + steamdeck-gaming-return / distributor-logo-steamdeck icons: Valve's steamdeck-kde-presets + (https://github.com/Jovian-Experiments/steamdeck-kde-presets), GPL-2.0-or-later (look-and-feel) + and GPL-3.0 (Plasma theme), as their metadata states. The look-and-feel package's defaults, setup + scripts and layout were adapted for DroidDeck; its Plasma 5 splash screen was left out. +- steam.svg: Papirus icon theme (https://github.com/PapirusDevelopmentTeam/papirus-icon-theme), + GPL-3.0. + +Everything else in the package is Arch Linux ARM's own packages, under their own licenses +(/usr/share/licenses). diff --git a/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/colors b/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/colors new file mode 100644 index 0000000000..673d46a284 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/colors @@ -0,0 +1,147 @@ +[ColorEffects:Disabled] +Color=56,56,56 +ColorAmount=0 +ColorEffect=0 +ContrastAmount=0.65 +ContrastEffect=1 +IntensityAmount=0.1 +IntensityEffect=2 + +[ColorEffects:Inactive] +ChangeSelectionColor=true +Color=112,111,110 +ColorAmount=0.025 +ColorEffect=2 +ContrastAmount=0.1 +ContrastEffect=2 +Enable=false +IntensityAmount=0 +IntensityEffect=0 + +[Colors:Button] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=239,240,241 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:Complementary] +BackgroundAlternate=59,64,69 +BackgroundNormal=49,54,59 +DecorationFocus=30,146,255 +DecorationHover=61,174,230 +ForegroundActive=246,116,0 +ForegroundInactive=175,176,179 +ForegroundLink=61,174,230 +ForegroundNegative=237,21,21 +ForegroundNeutral=201,206,59 +ForegroundNormal=239,240,241 +ForegroundPositive=17,209,22 +ForegroundVisited=61,174,230 + +[Colors:Selection] +BackgroundAlternate=29,153,243 +BackgroundNormal=86,159,204 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=252,252,252 +ForegroundInactive=239,240,241 +ForegroundLink=253,188,75 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=31,31,38 +ForegroundPositive=126,166,75 +ForegroundVisited=189,195,199 + +[Colors:Tooltip] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=239,240,241 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:View] +BackgroundAlternate=26,33,42 +BackgroundNormal=21,28,35 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=239,240,241 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:Window] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=241,241,242 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:Header] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=241,241,242 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[Colors:Header][Inactive] +BackgroundAlternate=77,77,77 +BackgroundNormal=36,39,44 +DecorationFocus=52,108,146 +DecorationHover=85,161,205 +ForegroundActive=61,174,233 +ForegroundInactive=189,195,199 +ForegroundLink=41,128,185 +ForegroundNegative=218,68,83 +ForegroundNeutral=199,130,61 +ForegroundNormal=241,241,242 +ForegroundPositive=126,166,75 +ForegroundVisited=127,140,141 + +[General] +ColorScheme=Vapor +Name=Vapor +shadeSortColumn=true + +[KDE] +contrast=4 + +[WM] +activeBackground=36,39,47 +activeBlend=255,255,255 +activeForeground=239,240,241 +inactiveBackground=36,39,44 +inactiveBlend=74,83,101 +inactiveForeground=127,140,141 diff --git a/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/metadata.json b/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/metadata.json new file mode 100644 index 0000000000..53e9f86420 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/metadata.json @@ -0,0 +1,13 @@ +{ + "KPlugin": { + "Authors": [{ "Name": "Valve" }], + "Category": "Plasma Theme", + "Description": "Vapor - SteamOS Theme based on KDE Breeze and Breath", + "EnabledByDefault": true, + "Id": "Vapor", + "License": "GPL-3.0", + "Name": "Vapor", + "Version": "0.0.17" + }, + "X-Plasma-API": "5.0" +} diff --git a/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/widgets/plasmoidheading.svg b/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/widgets/plasmoidheading.svg new file mode 100644 index 0000000000..4d92847799 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/desktoptheme/Vapor/widgets/plasmoidheading.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/defaults b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/defaults new file mode 100644 index 0000000000..2e46cd938f --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/defaults @@ -0,0 +1,27 @@ +[kdeglobals][KDE] +widgetStyle=Breeze + +[kdeglobals][General] +ColorScheme=Vapor + +[kdeglobals][Icons] +Theme=breeze-dark + +[plasmarc][Theme] +name=Vapor + +[kcminputrc][Mouse] +cursorTheme=breeze_cursors + +[kwinrc][WindowSwitcher] +LayoutName=org.kde.breeze.desktop + +[kwinrc][DesktopSwitcher] +LayoutName=org.kde.breeze.desktop + +[kwinrc][org.kde.kdecoration2] +library=org.kde.breeze + +[KSplash] +Engine=none +Theme=None diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/icons/deck_icon.png b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/icons/deck_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..7e596c00cab8fc71373669e37cbd76cec403d85a GIT binary patch literal 4540 zcmZWtc{~%~8z=XO(HyZd%rW;-m?+yUoBL>yqhqc@WV3RF%uO*vawNy3oKZPKmpMk| zmLo)yv)orXe(U%9^Y_Q|yzl4vJkRs}<9*)eeZFskjg{$9fFyv8jqNBFV`9fzJN^kT zH%nJIR_3v>vERCZLfPDKb7NyuN+qQlnYY-0Vkc|8(JCjDm8K~Li70+F0$faXEk!wh zC^aYKyh2iAb3m|4auVtTB5>7bMS4!TUYVCH8?V31vFAji6LXMBuM~50}I48xDPZ0wk8c6*4iy?X=`9do?lc5#|} z6%;Vgqf#C$y(%v==)$l&Q3Q^AAYg|DfZ0blKH8-T}o+jy<NqZ+`i$X(?79cBQh688t>+V)C&#xRQ?TJc(bVkSvlg8qZX01 zb!yu(q@oy|61gIHE4aTeo#VT=xI|d3ySg5Yt@uj+y%hedNTBO^)v4xfp+=hz!= z{z2b)KP9Y*-Jg1Ju2s`(*;5vM|dB@i%xsc33Rh9T@X!#bN+`= zapGt3cm+B!{(MJG_Q+ER2zpKmm?WG8RDoEWd1;MM!W;x!a7+uwuSuTc0w57X+btqLH~%%5fP5yn*Ky*haqD@lPrb|R@q#zE*` z8U^z$_8$%`bHnkdAW>?r34)hl84+=r!2T~EkBk_Kx@-?buGKY%VAfEqh;UC7Rr;T0 zoxhA~1H@ygIjrd21|h0N2@A?Auh{uLDB2M`s9Lk8nMwvG%@L_3YO3Z15W4ao`o6WT z#w})B05_s&PJRUU;?Kuk2jD(1O4w93RxTLdynHUJb5jB5tkCXNJ zd&`hzp(@aZ6fy-FjoqzriLU1ixWI+ec(9`Y4Nf3a@O(OgnPor+NQi@R#*kcFdg#|+ zfZc8(_en>55I0|Yn7G@VFjt>+_I!xG# zp2O;9{zzGcV~!wxgq*Lr^WIbTZ1Sa5(qskCE{sapR0tCmYQ6Og_ptdMoG$y<1kRJW zOC9&p6ZAC!Cr#YqiF2fZTPoj8YXer@EfULR57Ui!6AJYj*u}#Lu5L}wLVnRj-0x8J;WT;_^Tdu+(rW+5 z$eVpTqZaHkZ#=0D6NA zVU@ca`0UqB>tgM$(@$keABfp2uJiZZXxK0EX;wp1nOrR`6)zzSJAjD%(aMk>s|H5G zx5&f9uGRLjRm9}#HK8omxf7ZZR(;TsI?0M^PI9g_LKgr2u3llOnb=h&lAWr`cSh;f z?X22e@8yak^2ZaqRPgI!D*+T*gX5vZ6WSe;$k|EPD>DqA>I}L5ni*(C)KD>`+i*FI z@%D9n`&Bi6ypY}bGj(#%;#kzd`XZ>RdlaK_dIIrVp17f)Ao3sZum zLMhr7^x_=VvQzMy2T~;DxHd^(GHp*R6Wvz&R8ORml?%VIbu;v}VTyFzg&^ElhhG^l zTP6gzx{+7pYA>=^#WuCS?H~m0N$80uux{Q-U1+n9@iJoo!sN>`FsBiu@6uV8Z zta;$&JX$Ew$*Uq1gAIJXch`;d=VY9t{39aGE^90kC6Z0voPTwnf8EA*(tUe+cOn1!JVdL6d$lg<4$Mqhpa zRhD|ZOTN*A+aqV?vcO!X{%Lwr&s!V7_?Y+aivK3ly%-@Ee634n77_mY_(!A%=We8A zYXHMEKLIu;!ttTNLl4EyUbxY|3)f5lU6*6#YgDY&PoN6h!Lc7VeE+(7I`xz+?X(Ty zUZ+UH4mklLw7*pkFX6XOUrM_07tY_=a=uggkLVQrGeWyDWuPPClkW?lc%WRo5y8D@ z+)h*cl47qkPu+2&QR6L$>+btNpOc-!vH{e!y3>HfnC5UE;{kRM?O33_9^wFKwyao?^9f$8f3p_Q~vnaKWg2#r|-={3SUUW_E?GLwgqJ7Gd;pcbU6049o@Z z`Dz+f_IWA89Pqf@x|69b(RDQat^^mH?M+t*Lyv0J=Dts{P>u@_vF>$<2UNLS;eI&vRc zbIhE?AP64CxLE$lT_uI_{ao&;Nf2ErL;*4u$b`H4=MTr(Qe51AmUetSqE#X*EunN< z8bHk{@a;>T-EafoH^;1K(qlv>eB3*Kxao5azfoMSrNLURQ&1zt2=hfKt!l`rV?IxR zNC$>0cha4|PFx6{xXpSzVp7v(E=rwSU>R~3(TF8Mp4yjK)4YBVSM{H3A!~d&Ecjn; z`NL1_Dc@QmQ$}cV-Opp9NN@8Y4Z%yx=XoF~^kf0d*4Af3$2H}dc- zS%uDSU^rSQ%n*d0KBIMAXO$dM@AXZhWb_nwXLA;ri6{J|^=J5gMY|P0>4$*brW&ba zrj$--4f&Trwu`~+1~0kjC}^JpKwUUC9-3BKHfyA7;V>=oDeJ9KIs;$3as(l$(;(0`&uw-f$TgoE<2yC;S>~to8}3C_ z{>fBUiE!6q<5?Q@VZs~>aX3GY$~zR#*+mIE}^9VB3f1b_%7 z(8*8yZQ__{?cWAK=S=(rcW0Fbwt@y>-<@;vPf{fCj74}6i47>C=!@<5RgBN)OM5_` z3D{{ahQ)AbhwrOBniW?`@S6w?IIFX!iKBXjjc}UAtHoY%J^>`_o7KkX*M}oA^<16U4I$P1+OXT zOm#)NT(?4Y6#SZ3N8w;W0m@t>+f%bRrVA+takTN zpQ?wm$27k=RT-J~6hluidyMYj$_-E?Ck}X`w)iwqMQHSDpyB1-V|NBD7bPwVJ_&AP z+WZt-lrDrk{)Ba}%e76YyQjl?Dgcj&*OvpLhJz-H6pDkyC>t5cCN5r#o_`rS6gXa9AZu+4Zc*Prlu7f<$0&Z^4UFe9mKu5*cDFm)vCNfD*tJP&+B{+T%N$Ah753*MUhWC^4{PosBM%r4`CK(lXd zONo@Kez41xG_@7M!2^P!n(kJ8XRA5o$G~(6733vy?(1Mf`kd?I`S=52QB-I`$aolZ zj+gytnh%41W3UqZ4ylq@Sch&mo2pCLoT5FRZJEkH>GPl-d0QXKrudb&gkDj8bHkh!#28Z$>JXlkJKp( zoKwnV0Z?UP)sJN*)}xnvc_beyLfX>_2MB;&13p)EWF1}!l4>j$@8qVh0+}xf2eyLM z%BH;gs+;dEjE>n{i83lBmzJ3Rw1m}xLC^^#IJ!$BT77kh*g-13Sp}&0eUtK|Sm{7B z&QVt?!kVGJ-c!(fjXF-b`NF*pVjvlyimx9Ivn_ix$9*dkz1a?nSJT>9zwV{*mU)y> zIWT@4$G!s{7kh2-A#<+l%4D*gAON4@{S*?FrxGN?90rmQO7Uv?%+G>a8Zlsn+plO4 zy5v{mWUq2iEE#A%2N<7pRt9p(T-@Pf)=xSWDDhUywvUVPw<*p;z5OD=?`oa&0r({7 zcxzuHP6uMzJcG0Y3I-P$KOwq!&-;mTT@%7{g(1TWGG@VhZH17BJ+E3e?AFh2T@pF9JBz!c2wg0~^bF%l8a=m0`I`eUIKX7b{~ zI(#^aQeQAjS>$+^q~glDVo+Yo_$R2uHYhHeO!?Ad?-#+R^-!UD4HV79|09dO@x8fB z6B}S(>R^0ue4I?1?$anTFjb79&L7Na*YqXO21qsXajSOE%-2KQZ?@p$!fO9XTuh=G zF=~a8<=4zuJhyk|WYtbJcvYEp-OWh}(>EOL7}vvd+ebL0eg3nVvk)55adyKw91(Xo yG&9|}U)FXp!mgrPSa!*?^Z#k}f2p?PY{Wf>m9S;ur=zSd0X8h!%A^u`mHZ$1a%SNG literal 0 HcmV?d00001 diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/layouts/org.kde.plasma.desktop-layout.js b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/layouts/org.kde.plasma.desktop-layout.js new file mode 100644 index 0000000000..485bf74688 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/layouts/org.kde.plasma.desktop-layout.js @@ -0,0 +1,43 @@ +// The first desktop: DroidDeck's wallpaper, and SteamOS's Plasma's default bottom panel with the Steam Deck +// launcher icon, Steam, the file manager, the browser and Konsole pinned to the task manager. +var desktops = desktopsForActivity(currentActivity()); +for (var i = 0; i < desktops.length; i++) { + desktops[i].wallpaperPlugin = "org.kde.image"; + desktops[i].currentConfigGroup = ["Wallpaper", "org.kde.image", "General"]; + desktops[i].writeConfig("Image", "file:///usr/share/wallpapers/DroidDeck"); +} + +var panel = new Panel; +panel.location = "bottom"; +panel.height = 2 * Math.ceil(gridUnit * 2.5 / 2); + +var kickoff = panel.addWidget("org.kde.plasma.kickoff"); +kickoff.currentConfigGroup = ["Shortcuts"]; +kickoff.writeConfig("global", "Alt+F1"); +kickoff.currentConfigGroup = ["General"]; +kickoff.writeConfig("icon", "distributor-logo-steamdeck"); + +panel.addWidget("org.kde.plasma.pager"); + +var tasks = panel.addWidget("org.kde.plasma.icontasks"); +tasks.currentConfigGroup = ["General"]; +tasks.writeConfig("launchers", [ + "applications:steamdeck-steam.desktop", + "preferred://filemanager", + "preferred://browser", + "applications:org.kde.konsole.desktop", +]); + +panel.addWidget("org.kde.plasma.marginsseparator"); +var tray = panel.addWidget("org.kde.plasma.systemtray"); +var trayId = tray.readConfig("SystrayContainmentId"); +if (trayId) { + var trayContainment = desktopById(trayId); + trayContainment.currentConfigGroup = ["General"]; + trayContainment.writeConfig("scaleIconsToFit", true); +} +// The runtime has no locale of its own (C.UTF-8), whose short date reads "9 10 2026". +var clock = panel.addWidget("org.kde.plasma.digitalclock"); +clock.currentConfigGroup = ["Appearance"]; +clock.writeConfig("dateFormat", "isoDate"); +panel.addWidget("org.kde.plasma.showdesktop"); diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.folder.js b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.folder.js new file mode 100644 index 0000000000..4baaa9a591 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.folder.js @@ -0,0 +1,4 @@ +applet.wallpaperPlugin = 'org.kde.image' +applet.currentConfigGroup = ["Wallpaper", "org.kde.image", "General"] +applet.writeConfig("Image", "file:///usr/share/wallpapers/DroidDeck") +applet.reloadConfig() diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.kickoff.js b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.kickoff.js new file mode 100644 index 0000000000..2ef00cb570 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.kickoff.js @@ -0,0 +1,3 @@ +applet.currentConfigGroup = ["General"] +applet.writeConfig("icon", "distributor-logo-steamdeck") +applet.reloadConfig() diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.systemtray.js b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.systemtray.js new file mode 100644 index 0000000000..7eaae7a1c7 --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/plasmoidsetupscripts/org.kde.plasma.systemtray.js @@ -0,0 +1,6 @@ +systemtrayId = applet.readConfig("SystrayContainmentId"); +if (systemtrayId) { + const systrayContainer = desktopById(systemtrayId); + systrayContainer.currentConfigGroup = ["General"]; + systrayContainer.writeConfig("scaleIconsToFit", true); +} diff --git a/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/previews/preview.png b/tools/desktop-kde/overlay/usr/share/plasma/look-and-feel/com.valve.vapor.desktop/contents/previews/preview.png new file mode 100644 index 0000000000000000000000000000000000000000..708bcf3126488ffca8bbd55dd84b15ceb71260ab GIT binary patch literal 45238 zcmV*7Kytr{P)ApO-jw~ zRu4vNHXs|eU<>j9K{f<`_``+(8v+E`25i88hV^BC_``+)f5;F#sAagN?p8}~sb>|t zNfwJ_%~_dQIlh-~xKl)&v$sFaiMaO-nM_u*$zpYFpepaXH--}@&OUqZwI>d$0&7Il z%vOJ!066hYe+)q>;Y%|!Z7sGiC4dvh0#riM1xNx08cCc-W+#4Hzc2HJ4FJ<83Mdp$ zDOi+Jj$AcjITDfBEW+$i>thQ`YE)AIVtV$<%5?2?Q|$*J04)8Qj!)wk5dlC5s|^`! zLL4;q&unI<(M(~M&Pg|-shL^~&|ubIHT`Poyfh9NjOlr4q^rgdugj_B6Sbd7f}~i! zwf3_F0CaQ(>C)qv87S$7FkPEe#ff))@=x+F{)x-K@{J=DIl)@4WWFRs-n&W%n;-Fd1C=m!fZTM>B1{<-S9liJJ{;U5KA(#eOG>w2l zwY!+5_r1P0PA$l_AsboHRis$=x=$@Ckf6wLvzT0vVh9ZyT8qJAtIZ;|OK3ohrmbm! z0i;1hoROeV1fWp>{`8B(jY08iuP>NyV3ywD$>EtsYio;5H@$Su4MxNK4pjZA$2iG> zk9^wh>J5THd^cO&DAIKZcIyh=;L_T8%uV~x-G}~7FfSiXBPn27yJn_@GnlD@LMXsjt}iHUzqSo)i-xDU(fSPv0I|Kbb>*om^V$6Rd)GZ4 z@m4IajdND2ndS9jfCgk9%NzKLcMJwggXw-eXP-EI<@DzAvHfKQbmE-BM7)$ER}Q5d zNm0@lltRjZD~A*p7RRxzp^+jeDrjqAN%R|4-WQEri5wtFlAiNOrVYjH#QV#vlIFxz z>nR{C2h4INpGXjt#D{4U?>SA$w6$fWAWL^+zcOe9UA5siE=Vx|MPNojQ2|43YYUCV z)-1xJC2@okFOW%0o_Tj<;@$km6K0v1n~ClxzT75^nNh*;|ozE>m<@O&llad zZ_Hs4jtL*l+{Au+OH>%Hk_k~o)LZNk%Sad z;5@~FPKt`o0{}&wcY}!=jKx=UPNa~sqAS93s`VTi6Vp0hst#4vv`Nb?txH>*+x0z& zYZHF<$xrD*1k6izARk zNml|;3dNg7Sg;t)0!@)s!z4)r5OOu2-Rn=U^u!wN^aG?-bn2}cjZ(PbRya{6GAIX@BRmWx0*r;qJBJ#cSo{qAN!lYKyIu13KSM_m}ss z>*6@yKCKZF5K2&7C4XlQhCWMPqxPrrQr*~zW_<>9=^ z>!9Cufo?#8w2|eFuG@dHwt&u#E5H2omjF0AI$kW^jxoaEEpN*!118ODw?{u}0#4q~ zR-3(9vQxSjta(j#2imC}`phjzDWn`i9L0&R=u0|J=fssF4$`UnW>S<=4#bsoKGyTN zIM$?2kQ5JqUJ+W;pccWT7n6B}Nq^S0+2h(%oO$4rQ=D~d3tGPmqXa}T4Q7$-Z2&nb4NgCW zAx)qrdm{}d?bz#&MsZRO{cy9~dAi)ZNFpqz76UYb-K*^0J3!LG4Ajg@>9W2mH!oE? zSF7!(=nIRDsnQiv49&DYda!(O-7Jz~wwifD3o~a<{-)<)- z2tqtaBzaEhv_6>O-}_gd`e)xf_|-qSo8Do!Dmuk3U*UvUT)olG>gn{jUb9m(&1yCQ zAO<}?JhY?~nR=`?e(h)BgkjjR&52dGJ|X=++Rezx?LX~|E6ZoKXP=o;Q0PimjV0-L z;-#ouIS}U!q%WkX#1&A}*qCZ;mLSDf+ScvtFg6P_g$UunXfQfY=MlZB_V0tplhtd1 zwtCZ#T)XFiNd^Ggpq?7QvSKG^vcM6s7}u`?W4GY6oB5>F3B|J-Nl}W3YCwz#4PpaE zfEH>qg)1oD0E#g0Bm@a%;;U`|>}UJLBI!1E7y(+=Kdt{CvbG}hm5jEE@lG+>Ehf9p z7qOnldJ&e>ezbsPr4&L&WTUj3Vlx`0+AKFN`0+M<2@R%d6kn7=hUK(5xYHcnM+Yvg znf)LgH1Fc)JTgfFj_*_Ok?6mqh*Vf_}_oy?yU!lwd2nwgpz(K8{f+eXlnQzdn5hZ!%AX3N;_X$si(FhZP3hAM10wwO%M=> zK>>?QRw!vC$t3?s14JYtAEomZNsTSl0JYdcTXr@Kv`KSFah;J(&`}HkEt(M&(x*3h zdNU!Ilr#fGV1U|c15M|Mh^yRS!;Ln}&CA7ji>{!!*w)STVLRPpY^|GNz<|(6ECI}5 zOb=`jk|M>%wjXT?OzRma6#BBFE1(+YNA=zfv`dnpbc`$gRziV+rV$oxwYu_ipVZ;g zKaLqBGnm1mK?%ub9($=sic4mcWSlPvIVJFo>qlA71OVyyaQ)uB(g*ScixT#^XDS1~ zc4NL`9Wz)&x6$`tb*C;W48Geu`jxhBi&}Sc?esY724ZK_U}m!-vnkEAb0%caE?+f& zo0mS4?I%v0mvSW4kiKO8o-no0s47TRwb+6}5Is5)n&on8p)pl)p1w>s$=X>GaY?(2 z2E;UrJxi@?L60G>>6$o4R+_~I^;9ttU*sJtTQ`z+(5#ndbx~;oM2I6DML;LMfQV@% z1*EBJsPh&Dz{Ft4*c~7~4~hmzekSo0Pv`U6jcFCGwo4`@Dp`h>mVV4DN=RQxIdX%s zA8xwg#E-UIIixGX;;1>i*Bl~KJysT@HFOp)x~HOA{z#I;qp&Jqwv|=Ecv{x=>o+e5VV1qgpfCmgmtWsITIfkXP}Vw6p*6_nrJ2#MaFKjOy3y(y zy?(Z~Ue~1D`pw`@2$4=|Q@RVM4UZHiDdFYETFPAqh>|WkCv^ zPX-SOjcTDr45uHa-^q>LWO(?}Rbw|8yJ{>&>3mrfl^<@vtT~v4={{mZ5tla<0EB}j z#a79*?DDpzgphQ@3B_4xteKlHSd9={)d1B{&zpyLY1u%WAfEwZCJ<-I z8M`i#PJSc^k>nc95NkFq5{D->(m(|Ts>$06=YXUdbl%UD?yZaPXv_A zioF$-eBUCLcH?xNG)589^%BKk8I;p#v85~MJm{n-DNZ$7NZ!?6Wi(h!7D8f34UL7` zvl0gPs9M%a~|$$9qV$Mhpl z&ifbwfT((J?;tJs&J0M}kinwGV7l^VoEBzgXPosXtU+{_J1r@Uq#IB?oCjTolClA~ z!rZ#$o_DF$zQU}9vfJKCk}k={G(?CP1Xv3<$?UD#)>s@;1mdk6u^golv(q1vX%0FN zw5S>V000DmwwCO}M{^(mQ!JKsr=Hk4K-FQYG-KX2LGhIv@A}C_iyfibsRhS*^KX~Pr|F0){!#^TKkqmmB(2@XPV=}4hx^0@WDx10~t5Tze5FT-*9}ViC z@hd<3r(XKZ%Wr+>joDn`4oAOjAoK>1+r4zd2*bmxC9$gylE@G($K_uZm~5pi*0v%14v(5 zY%Mm3A+|LZ$2o>3iAPsZoMklBrSLXG%y494BMoLTJCu-v!A{AmHtHX|`rbM`p8nD7 zR!i2tz*Q_qZm>~q>=u&?X4)R_=jZBNQ{`~u#jpIOHy^Y|ckci{@=UmU>7sMad0$mk zUDu=Wcrl+j?^ShrbR0q`igGcVl~uK|IjO2@xmb)hHkzh!&MlYArmi3E?f@Z@{^zb)N5b+#%I6yV_$#e_h++Nc48znrc*!x%@C-a zQYU*=83vq|gjkn>YgSV_kFIj%Sjs{2!T~GZ?cBu*GidCLUJfrkS8Y87lBOC1#reUw za~O!3l9E))*e>xH4!FCZT5KmuE|(%g^7!xwz+yhHmo>?vEZU}>&t~bc;b?Sx zc+?ID%jI$~96q>rzZz6cU9aBchn||IIe2&w+BW@p|L(o?puFM@xvU8Wtv>|bOl^V=OIqS8HM6Vnnu%Tv9%b|2gG33LiJ?0<*BJipM+A- zmGor-5@j$h2Ai%LN->DdeEINBoF65WfWpLt7bk-!f9oI9j|>sxjg9$qW@b_K-h=%l z^wyP6GlRwywZDe`+1meM$tFCd%6A8nfSORm4JJ~JlJv{WX0eN1ilzz;rje#FwH&g* zq@Tv4yT@-$QwXg(iwX+ zadd@hpkN>-03?UQ!4nV9=JUmq9r?$G471~D{};RLne#&;0i368CGw|^HW-aYiZ5L?^21FH z4PtBUQZ*n3SYqp*v)6nSy@#dqH)f!|o=tDOI=uA4uzJBX!b}5tBhX^7wze2Fw&J`i zhwXCK9zRG|G^02NKx0VWzl__6gDa>3vFR+u?44U9fvJUvP{(#bajqChIbcy)3>t)> ziC9d|BK8ng&((QqKuRHli5qPd;|s;cwjWMFv9=ENyqz7GX3|2?^jw9fSEEsbdNQ zNIQWrq)+k)KI96RK?X|-7DHDK{9xh+V*u^(ewf__v`Zp<^e}q28&0`IU}3qq`=+T5 zuY6L95;1f^DhAV7&k!R?y3%3{4{sy1NFF2rsuU+lWfA#PD7yo6N;h+AgpKO|DZ-4*#_4fc=xpL*j7e7&!#r^vaUVH6Z{fSquT-n$d z*L8FA=FQEm?a6p}>;2mRCX>nW@o~D;r(SyL!tRBeH*c!i!Qs)cD)t{9q|+1Va_SBL z2Y>yqeesK5Fx9{PcmAvK#w65>_kqd;XGb{UHz$)V#OB_E z{ES6eX=tJvAd|`P_&8gSKl3wxYBU^9r_$R!K?JP~Uq~ zo5k?*^KQ6FaUu?6tQUyU06Gr~^}+qHI7+MEpinh3flJwqnjHop#l%y5mdxkh*#Jd0 z7o#p7YJD6B6F1n*i5lo^z7_1#&lCZ&7|L+Vk9UgErWlJAI(d zQm6#9or^1DF=8>GI0};v+Yjc`FTeZ;U;Fy{@Q#m;FYN5>KYW<}{M4sjx^w%^%b$Ab z^*7$^kGXvLa^d}N{Kju;hi82G%U^osm9MsKmNSQkho-u-y>sgQj)uei{fEy!^YqP| zH=ld%n)mLhD^KlS+C4rz{H@>ly>!g7ULGGE7e&#w?Nd)(0p+EaUv{Q#j3$Ku;L>;yzU z1S!Ap;NjaoQtQT=Pi_I)9 zj$=Kuc4=n4?#U!?-lQ+k4B^M zc+@nFs?KJ!`D}jo-ksZbZ^yMb^?I?mUoV&Qd5rOR|Nb|dsV}Q~J_}K+;V9ON!N!DU zM@K8YdVR(0KRnPFj*gCQyxWj6U)Gl|?%uoq;Pj&UkWms3mO==-NKB-VUUWHMX_=j2 z5RNXtkmJf+;i{4IIe^AgH3n@K+AQMoSesdjLO$pA=hYo}WQ>Tx05{wkKl9mg=PD9b zX(qmEXAj%`ThL%&3Y|%5&SY{vs>Ya03;;0Gbgo8gYs3cGEMBi+aY8~$A4lFh8l<2r zjV|M*8i3w~>ckIN45S=bjHa!&OA8HR=pqcWUzUI{Y00o0GE4|TFTQf+@bbmoCwq^l zsVvJEKk>r%Y7-XTw@+Y78(l;v(GT? z@9SC{&ESO|F!;h(BXLDPbB2g64=P0lTeLn>BwgT?&^rYxo=*gUxHz2Ncy-h^<<=z@ z1I8AnadBj^&7~OtSTqd;jgsITm?5@sB`B7|mkCN2pWSrn@=QG1`#E!@%j&ti4;nOO zB-5wh7NiIm5TI$aX0FSrY1`?ieen4znpBvx5K_DXl0!9l^<$&$_x?&dYTNd$x8GS` z$8G!FFBjsYDr=me`F#G?+wVN_SgdKjw(ycN;$Zh#H`s{FsV=4v zXJ)VfD>A^Cvn4WXkj`Rbu4Lvz0RcJ_2X)qYDApdbgF1OmoM_S~v*JnzrugKfVXDPt znk1Uwk(3Kn*y@gbO{FyC4~~wS*^_YH*|VPT$fjw&*NcBl=Mn)((Lw5cH5HhqSee!G z==djziYyWxtN6~zpM zhB$ku8pW5*!5xbsSynL6mxP(doWYQN29~|;rbb~dLz5!rtt$%?0fTlbdcK{SK~{{B zn1efP0*plOp52qok0>QM3ab?9*x?zopiqf7>!1!HjBsqx_)P?&B}lCQaF)!l zXq8<}gPbd+5wY#_3fSRK?5JO3IhBb3LLCm@kBh@%V^_*iR$e<8BxMk#oUu-Tq}#86 zkTjlC&wn<9V-?#9nridl-F9|RjJCuTniHH;j9cuVkU~p?0Z4%cvl=ON5J-uw<`fnY z*%iOYJh9p-%m8LAB?pVUB>8i?MobK>z>}~F0h3k4E;{aBiC}b5u+7X`c(7Yjk<|{{xAILhxhJn?`$oW?Yz;eyW?B;j_%!j^Y;CNPkiEq zZ+zp`r=NXxSe83G+k1zz7oXnw-LHM;bDw&lWfkW8;s+ukz9<}r!{I;rrGFGc`0Qss z{f$?@`Nc1OVfVs?gM)*`Vi{t%xVwAl(#7}QyK&{pl{epd>(Zr*q+Hn9zIE&UD_1Tb z9UWIywg2$pWMkvj`|m&f^wT>#+i$=9PH4ma!-v!9v}u~Uu0Qv=&wlfpucev!>7V(T zLip6xs}Jtoz4GkyTprA9xZ&Gbt^e;o{DE)n5%Tk<$sX*@M@W$2qGn3B`?fi>)g; z3MZLoF<59)NL)fzQ*8jV2v9?vqtcR(Mx(}Hp|-Yxww)+j(?9d=6Yu#11vB%ZSmHp7 zh^>bH3g=AqnlT7KeHW0BvDl171CUg%)A{nLtIvQkDE(kG_RQ|1HiYGT*0euNclKnm zaeO@8m~1-XXf&*<>fqtSaxf~L-F*M(L+u)oy=LkCQG^%rM|Ka{*G6CqF3o$w;1gEpva=A3qx~@ZrRaLfa z+Y7o+y!c7)D8%00{`T%Ak8m)pw43?7FunW)-FxkggTt(wa)_YlsDw@RtiZW4fNwn ze)FQZl#@z}F}BM!3oYd2^c0mAgT@xFq%X{pJ3eX*)+{V6Qx&5%JXnyj&c)~Y7Zix= z(>PQWDeOq2g+{}&OXWUEE1e>ZTwS*lsamg!D*=d6M4Whk_3E>adHj#*BS31lKMxv$ zhDO_^wo40jQuR6$@hq&6VNN@nPl!&BGY#?hZd?rfct?t%<>f_fiP&blDF=C&k$xBR8^$tO4}-3(Umuj)JRoouo$Em8VnjO1`*Mek00AyO)S-O z6agKbPZ(y$fi6FT8JW_rlD9$0b^K~I(!#qo#BP%@cG>R97n$r81JqVHpnZ zupGJ3wv+>iqc62xqOB2|&MI8_EY9BPF&x;%XRp2p$M@*UV!Uk{Q^H}+fY;FtRR91W z07*naR0LrzhiFI_ciAKuo`z#?K0i^c@P^Z^Q8A?A{t^e*7po4t!!=^+hqQi4xwib2WS>w*@IcVGX3-t;3%qf1YFindE@7TPXhF+uLgmREL8pW(n% z+s3~1!oIH?c(G*6DC;K>KR1D z59oX@zbBnO&E%61`cj;yct70G<+MHC53@b1r`FCb)QAl&rW(~yB_cI2^>WCe=TS8v zw$v6ez?Br2MY0(*8niFX)>8!j`$v(uv!cY{Getwx0hh z|LI_J$L-ySF%AcVE4$kd9v)nK_E~4~?%u(7dDY)vY8C*N)1&X>CCj34-t`pYcMl@} zXizblFf68r`xfgiiDDfkSu4NY^C8zFv{t#f`KPm9cv8F+Bk@I;KBNeX%9lechq0}s zsI{%p)}}Q~jj3|t=?gwe5}txgJGmXgbWi-i`3WJZ4170iftfg=F93@3rXjXZm#t~hu#Lf+Q3JgkP zP-~ah|JVPf^M9=9`t|GQPR<`GSq!O)VOe&vHZNq@A+kW?oEc4E!2v!TvN!qm`$?(R|ay}EdQ9E>>n0YnfZ zs0Kl!Mv^Ij3Tb0I09yx>zpIPVr-OcL+8CO zQI2A0Vm;GlVWGBEw!ItHqlDpkH%-Gc0$NYoW5=CJvJbtNnSD5{(`Jz}i~*x7AcdJ3 zV#0%Ar?0&B@E}%gd;l=iMq#;;d^JSwyVeV?HFzn{wvn2BvS-t~&Kdv!aOE%mo1eP< z8xIaIZf#D?;02(xYRlt;2F1lm`J2D<)gRi5CBHB!;-m^ixV%+8d*!)6X^gER8jo*W z`|ZavA+nQZHT2j)JXUR@ri^byYE*bK|g$p}7JC`qA`lBEFhk{mH z`V+jx&Lfa&DC3Mxn4>g>Ql@h2lGVXg{D&nmkn6w-tThcuaV^K&u^h$aact(+*3kCU zsQM6=M?Op!!DFgnwhy{u^AcRq*}F`oV6|PE#=h7dD0B|+W}2$SWc9q$9F>qxHo6n6 zp_%I~TDw3&q>d}8OTo!h=+!>bd*oMseo)^*T&tXU{`r823LmL8$Aie6`tt~%K!O!AGf}YaC+r;U1X5XEm-Fa5B**$TdfmGa1E30lsx}J? z`z=szUoE$v3d`fTObY4(+GoD6-RQBD91GAGrw>4uTTg+xuMG*!JtY;$k)7xXRq9eK~ zUPV72e++1Sm0%5tSy%nK)Pl9ZQ4+Mi#!R0rA*3UHnmw1&g|ZVuR;w8S`!l2-lbd!9)GEPXI-sj4?Ry_|J$yhKy-nQS8yxR_>W#hk*Fu?N+r}r$OCniS70D1nUDstS zk)@O;acViOW^Glk6KUTEkjgF?azmJ|+XhLx+#n%^O{2@oICV?aTwb1}6wVKfbVViN zEe11X*SHhn*c29q8X9XBOd0wq@h|gWMn6|PL!xVO0uq!|AI%RY7Gv)Ht(tnKq;OM< z0jbO(jH(HtQJ8eNCA$Xn-Mp;Bo}GqlAfVbpm97+a8PR8q=Jk!O`;0R}GS6xM)#AO^ z;=?;P?!Lb@8QqKa#+z?dgNkOq_N%`dntJQK_YWTKzxMi@lhJVh;QQ&pdor?T-oE}u z)T7eh`t;Vn|5UL%FXbn<-v8yr-G`H<8$Zb<27|$m{n(G;anScim#iBsd+TDuGR!40VKX#X+jZk-eLgBs1^f=STA5EbXIur zl@$XGH4^KBDM|&T6lDrhF;FBI!xwS%g_I*#jeRwuxaIyWZ5FxvhDDgAQj36P0LjcO zb`>iWfSUCkJ!smSy{DdR{YhysMAL?J$t<)-9GPdudNp)iq6^O01hZFuIenPVrZ_{h z`7HpLFAmoVUi`qwz<2-3zx|2$ui0{D&EoZXE@4r`h5NI$Yu)um@oT^MU9NC4ukFdx z8b4BGr~X*;f3N)q5E)H~rZM#oV%MJ9t$43<40MYuR|6Ii<9Y~71`?$xJI7jgRz0IH z3_|BABCgcfvMrzrfJG_3w9rCQS1?DAt`J`&zn>ICS5~eZx^ig1{Pr8MSwK@2U^O*p z(1aFbq1x4)BPkAQu$XFJ^~Ubnq^$6v?wh{vaaW)%)spH`_0LZD$px37+m*)7P86=* zNjYM>eCC;FE^JTk-rtK&4PTB1<;Hll_wcA~mLl$j=dazqbNBuCZ-1~g@PSF2*T3=VdWqCT4<78Dd;CX{_?Z`WzV+6A#w+{g+~+SG$w}m9NOec^ z>Y+pK)MXHlE@w9>ggJ+Ot!djDkVB+y@){`4R80s~g|RjG%0SYAaK!)=ivci-7hkd* z(dDjs^riSxd?BuovZ55OsH7;>Y;o&5^}(H7q)_`(H+0Se7Nfz8W+^k3;w&dW^%}Tk zsHK~R^<>Buo&v6H7^5+Dl{zcZ&U*u^3IP4wcfGzkdj3ED@0+*2I)3B-8;-`;o*7(y z=GkF^S6}_s^UuF980}rYv|Cn#*}?tE%P-%4|Mm}Bj+idq`058fdp4W>)xY*PzK_HC zQKWzM&%OAaw;!sVc8{Nq7)im&QqZ}KH4Y2Wl(Hv~ zmfneY?+*_TCtKUlFrChaG6kpIezYQpPB4j9@wl^DIK;iT|GEB zxVSZW@NoaNZ@%`C)w^KmC=BuYKpnU;Hya_vX#LXD^L!@6UZ)eB_xh zk#$Xwd$|7*Nqo6oOfFlY2JL!Lxcqg};@VgTZM_r&DXKm(u{Rb0TZute5YSjB2X|h? zeX~*;KnpD?^n=d-7ik81I%hG2#c>RcZC$P|Ty@nTxhPYy)5U`u$G5%%4Ov}OH4Q1* z&l1p*T=QxaNmU26YfNjoV>ST5Rc^RdZa-B_c8l@OgQC29ykG9zNkTQ154O-yqoVB^ zvcp11ZW|(X`#j$=#7g(*eL0#{4xBjS6SW){-1*F>u3X%Dcr=?&k1t%j_?>UQI$eAZ zr3C-z=+>S4_rLL)nx$?QsiU&xMsnXJ!1e22{|5OgNmc#Y@4o`zw^VaS=06^k8kZYW zMZ=YY=WN)fAyUm~v@GXb;qpNWimLz+_mgAG7IX^-w7z553U4Ww`Vi9DK&g+fDv zTxvB1xMoebD{-X;qpSS*Lb-LR+E7Hmo63_tV z1pm>Wd*-WeAANTt&3ZY^Zqp(~HYQu?_rE)TYE$L4O)Qqp*3OQH`Esy#|K1O!$OWs> zsZ^%pqeBg?#gPA>D>bwlT02t)>Rhr;N$J*{JzjVCXizblNV}XKAEpwaE64F;VlxxN zL4jzdUOO%oS5iFTS4&7-Zl;^i$W#_-jk(=m9GCM>BB2WzgV8}Xc@E5srZlx?$y6gW z>yc`q&CW&xM4F@fVL7XJpB-L&7OIEueobQokyM&Sgcbu+!_VZ5u?U#Kgf72owftk& zLWR!z$wfcfc7u&#vg;?i#bE5p0bSvSlg-VGp`Oq0yjdUbX$&R;7*t^v+onTr_|CmK z=*ej4caU?Z)_M<7zZloc-28`=+W>I$0Ki*sz4gIg`^WSlr6=8SIlR~H(2}NQ~@ghF72T8?Tm1c9LK-D54DWVLTxCXjBmf;pfI&d;c?WAfN zL}0qO^G3aQ6C&Cy@^2LB;>a`p8(WP5rW9vtYAUH|bMBbkA)%CFa;ez5?1!6vIH@*w z-C#qCp}5j478^TK4*ht?%!(h<9eaLeKh^@mw$F=dwcuB#Sp^PD?7jVi~s14OBVV^ne5buUSaIa zu1CsAZ=e)I>GezsxvHR&ea;=?0MlGdDh93REJ_0A9O;0_6uFY}ZaXJFtn-slKvJX5 z4`i?j&`h)8*+cw>1VV}wMxiSxVyTv4inJC-BDcS$GgH;bTqj=>$eZ|Iq$)Qr`SF&k zHl(avHIj0WWVMKoXaTf~!}{T^c6Okl*0zbw0zxTHhztJ}J@oWz8#*OQL*th>NeF(^USpuN*4L&A(Z~c2u zSBfi>=*aj7R)R9;7pR$r*vvIH$wqhLEzG1S-Duk^nwbToIE$L2nwfHrQt3fS2~nmB z5~0=QRE85`%@I!0^_7T|1a+n=u3%Z2wy@+pkET&I3ZNEJ5NXn_O=Hqs6WbJ9m;GqF z81J}pEM+OaL}+6OQdDty9OlPiIStFHhK9Z}gA}DU3(^&%O*$W&nS~ZefjFe8Dah=H z>3ZC+Lor?CkSm-kK$wP%V|DaDZ%otCs|==(=_5tFyfeDDKee^p`AMKDFMhBVH@W^V zK|n)nYYmO9bv9v+`ec|N(w8#a5>pm~9O`Va*rx8m%w5|+I*WNtQ>g7iMOX||Pi>OP z&a~JjSSSINBx76Cg`CjnL?BRy(3-ZUmTsX@kt^gYX`r~_q};h$3@3iDksOlE?7%Fh zVoVyF3=LOfKini>8bUp57sntCkfNkeL$J`~MgW$=<&0e&a3I;hY0Hw-u7i?nX_%#4 zs*|aUAJa#e3^4qKKl{1A_kaFg8@>k)1n5bzWa@FH%$ljTwZ>qv$xE$Ik4^D3NgSBA z$M*}08*Zk?x{?jE1YbI6Oz9X>R2o`smkIpMqE5O280H5tk}?!wgW1)=nSx zU3-%|MLbQ!LZhLz*tMe&&{lSAYB&LLV5%=H89r4-C%e<6^#!dzrjIV!|MvG^`QCjp z=5rS{W_Q2ER%4@~T{#^4uu1@_WIdQYeL_=fkMGkL-ftzFF=bSw_#FVuSrI~t_=41lS7>NGS&;Ea!jd>Tn;v8bRE0DdqoCKoA=&hU6wrqUOEOZBfrAl~`XU=~_h>Ic$f zz*ghyZ@guO5Zbm4LkR71KKmZu^vCpJr5r>FJb}CLl=s4GSZGWk5$A^{uOqE% z;W8-zlsSeow#&U6gA3Q_JjJDuODQTdOG$V9dV+k^of(!_%)%VPT1$Tg4Rpoi>6f9QIliBu#e}%9&&5rUPi7$sdjdfE zvfRAr2AKot3OXkVDd~FLTZ|eSi-D5TCM-0|_V|7d8^|-Ls{qvWu-OZOE+mK`E!3vL zS7TQU#1%qhNJhZ=`LQ_-o9{gV>Ej1 z+O_%Yxcx!Jk$%jC-!JNz@W`xF-_9o@hd~R-@u*xZ>-oF|x{`;dl+?4(YWDgA=*qeE z+>xB@1OTzcVZHZWb@6#g?Km}BQYj^+3sZ`6^OJwll>?+e;m{bADbv>%lNWy6jkgZp zdL=B5le04K1*Qa^-I7~}UR7fooUhWTj6&y%$wfcdkRq{PLFa@H?BS;LZ-k|c{3ZpC zH%EKgE_1+fa`(rWb%cCwis?%sI7~{dcV1l)7e`@!=ms0E7%)LNG>8ek5ehRAnyd%Z zKl|7J>v;Ve{Q56Vwsy8Qx6E{Bch{LNqPQ@%Vq6u)3?Cz?Kcdvdmr<@>p4_^7rn$(; z!ej5;c$o2Z^wri*mIFQO=I=fK$q|{fMM<5&Rt|SHW0)P#Rn^W@Ou0O%U1oBoi`#nn zrv^JuCmfYfjV+z;kf9c_9bJB5uzh9!+rPbda6P9Z!9ZRdyFdMM^^dCXT%rn(SsguD#TvcExvQNsUt{9K6G(KF}YA~ zY&%~hJ1|RKz)Usf@S4=V*i4DeTYtRQ&JUqc+uCAG4*ewB6O;l9lK|`_BE=*180}Io z#d$h!v9TDO_sQVY7%bm0q$5{0p@v79-#>WzSRMU-QBp_ev*R4rthTp5Js%=v=T|$a zMLO?zLa{vWMCOd96%%#_<{5#R*DS)~*omZCY4inRT}&=s_{`5W$NLr=KuBu41T5Bb zUycZ945lGAON+r^)z+1A^KvuYvvvux4tVQ+Gi$UPlLO!e<8rc_>O`exy`~C{T`Zdk zkfXgV*CaI6X1X7y4>Rzi(PCrT=AhZ$s_ue@h^_>dq$X`hEyN8F=O`k@kg(X+DR)U@ zNL|a(bz&P2KL%z#%2bRd5|-2Hw6g}Si>W%xhbKZwJH~@^PCAS45tt6?L=yj{;O^vE z@64qW7qx6m0YIo{elU@02#ZE}>QDZqP|vlV#b)Ws0Yy^Ay{kqfVvH%grsD@_Xv>YA z@s$@tXu@3%ca>w{w?g}R!h zcHF7vYCb(XEmETkmorODp#fbFQ<#Z3x&mZuYfJsTH6o)sN?<&!DE{S_KGC$HEQ`Tl zP?pukcr@OaOeW*D4VSN6F^$9F0F;gKXtI4lv}wW<<1t)JE%y54jaePx$?c>bn?GP>erNN~Sl`QZ9MBZ_M0Q$J~7yEt@y zPz*P;U8<@$M^c=RZ7sr}LE9R1&}bBKg_+X%otM5M!2JJb?@ePY%aZf3h&bom!mH8n%Ct06Wp`wHWJZ-F+cu6ZLzj0vl7oN1B58n z<)t{Qq7<|Nz)^&z>d+LRJ~Fb#fomIqNP#vs%f;p7Gmupv7XT2c)oh+ScMilArBf!d zgHFygKi_8Ol#b4yJF9f$m`N8ytCbYZHnuWP6 zM)^~p-gL!}UU~KK@2%UJ>vcNPluy9JN9idqG^7Mb`*0t# zt7|f_Zs{ioV`f#ns!~8OBN@~YMX8FLroEIboFAZ9 zS3KYbeGt->h{O)^{z24Q1esfi8vXlMi_V5PCnCZYg?wx4&>u}}&Px#?Qr5#tbwoNQ z6N$6M{swr^wvS{6TxYdbCJL6^m5#0GNJpvX0*5LfEWqF_Qs5~UW?>76AinHFSOiG} zIc6*DJDDCQ-R_A)_=S;*R3&7WvF;m0K-5$ zzfyr96kGvF6g5b*D|7HTP+PjZUk*A%N}P4Ypi;Uz=Lrai+8}8_WLx-zJ0P)Sr7Kn3 zBoiZu_;{@6jv@qckIt*Lx%hkU4G~bpVW2w>6yQo3W-Hc0q^5pXbMp($G`e?xQ%5mz zS=i4?y!1pqviM*pRSXMK1=j0HhqVTZhgN}r%{4?#WnxqjkggOpbeKU>qBQClT*)F) z>REbKD31uur4dy`CPp3UC_x>oq+yadiV{#tsR&JkA}T@Mn0Ld@VC!bOauKy5GT^lu zkdz9}FJLIA5D}JQ|DMvZ2%s<`l8%LKbL}x?SM>Hk1W^lE00d<~Se*6HAqiC6BtoPW z5iufRgUj46Ry`pN00<&D46vTy#`|>~i7hNU#Z48Zp=uEUv;qNP_lwW8pF9=4bhYD+ zdEkKb!C|E#6nu?Gl;OE&pWWWsxxcmZvtRl0sZ(o!9+5UMncW6}myB(5YRBt^=&d?4VEbOlWn z5Uz;&eU&hZ2sj4-z-;q@2$M;4(nLf+PrHvV-;eI}fZ_(Q^QjL&RnfkQu;+RdE=O4H zTeoj93$VNL&U;4Du&^#4^sM6Y1SRu@kg5=Nd<%z^0(d~kbh4f_kkXuWDQ*G)Dh0mC z)pxqB+R#?v9YLktuq)1{ryfyJf=Z(@qzsu@nFMtNq;wQ}=q8Bhv@L-ZA|yDxTB@kQ z#o*xfTZoXZUNUA8DrdPEc1dZ{hRdSpZd2TF&Y~}ya17vI7Dy&)EMGvt!PZS3r9xT+ zLjMbI^cv=?qx>ThL?q%O9 z081kE#umS^=||-JLqbF$6)Go)kcjg>j5Ye}bh`e--Me?keswn=?o2q5PeBAg7z5oj zPGrzWXqJggd-#$FkyKR~IwPN!|(xkZG5h=@c$X-zsNrG!~r>52>yO>-W=cMujLLegT(;r1;RCBQDw z^81hzFfi9di4>)zbiTgpMuMJb5$9X|)V>qY!iObQ6~(@=W43I5xsvk$|p*QbCA}c6VeeNq0&-jWRj()zoDB;h-mYHPFmth zm9zjA`6RWDLXD#^Dutv35v+6NkaVPs5kL?w`uhM7x0Z#d>}=CezF`Yg zSkM4cbIz5+vVSn#ypip1B=f5(X#)~zUySP?gkxKx(mrwn2mpjsWjB$;m2S`}dOM`F zX)OaHuoFP6+JiPU7A%$#0YPb{jVm(%P*I9X1G9fANEKE>KrSCCQ?=V!76P*G|GlEb1{@+2(BQdRFtx_{d-qUqixzNI&QMfwTVf` zo^%YA4v`OFTMU(nxy(r$Kp+*NHc)YuDV=~wez3)5X0w5gVzy<#g}G9I#d%0{>0HUS zU}wcSaaNr3Ate+SEU|IV7ct6qg0NQkbR4#9CCLP7>>iEfJgbB*3+~)d(cq z)>`Mn3$857vMil*Wm$$Ft?hL?rTc_37AKbQVacedS2!fzJTJs3)IW8Ehkpd7K@i`S z2}HOct3;{LO9%;oJ=!e<9uNT)k@Cp{+udWgfkbGMo!9@My}vR4*v}#olO~rtTL7Tg zQrNOB*cRg4&=z8IaiuE@u{k@-WiHNhnU(BH5MXw?F>e|RK5Gv|NE(#^s(7N5rm$EY zrCXGVm5xn&r3oO;x_nR$J4zcQjbMbCj0*{Rs1i6VJ}(I#<=~xIKmjS!ebtyNx;vQn zQAd%&=P#t4K_ToI1ljjI4E#1?b7o6Qla5g-0um4|hwKWdcKZb9!-;%)B8QZ1q7lgq z!K$c2`?>)X5t{=k&?!>g%)xgLtzhAZ5Rh>ckYmnD))%(>@4VEx^;)|6XqDpgX*!R4 zLyw9Mn&ZR#h(59+kg9-j1Bx4Bi{AR1DoW=rdcUNJu~eDv1{Vh&Jz?H59r*a)PAxc4Xw)fe01^!CY}V7#ao07RUWLqtM~tH3G%2##uZ#dBtRjwe!v1&|B~ zlh&p=U-otuE`J51rZ|fTzQSE>A>bEjA=V)w@FU`^S0@kwB4rW)LZy1Q-`v=~z4GkO z$19gYHc!G)Sfe+ED<~NP27)L6VR3%_sZ2~t+kC+6h!l`QQmz=F(iqa9AgDq(*{^Z| z5eNv7BJm)Ev<0^O$3NaFhXo>V;4iKKt3bdEj3NxKDEkFwT}0wC5AUlg`-+Hj?e_VL zk978TmzS2dcXttBer^s(8Le1QY<_F~?)2~IL_Swihw(=yaWZQ-TLe^OOqf(5mMTCR zEBIIixe0VH9?&czT1UQm4v0{j_JwE6!WnS3zj-}sEt&aMQkqN*NX$YeCT-CFs|+fo z$Tuggf|*LAGN!o*2&jxoT9Wk+-ur$z=)?$POsZNIOqTUND5h7T5^@k*fG?>Q@J$G9XUa`tD94PU91Lq?IzLbifoLiYt-AO4y%A zX-!(I#ylcOSxChJg+!zy(h(qlE!o+sW~^cdMj^yjs|_GU;bSqSb&j30)=CiepC8hc zcu+Y}!iOcHvfeid^{uHN_*lQEGTPLef-bTPyqb0J1F+J-x?0c^-LsiL-M;uNdcY|= zr46NX#lijjU@JX!NtqZ4Q5mWNDhR$Iu7o~Nq>xlJzoMcPl~Fnh45fgSG|?n(*y-JV zE#KdW=2lS0=!s%jd;$|aK-4ozh^tIgRYw%w8AOUm8Pc(@?5&%hlFvh$NJ5dW1_Y%e zQd(>oNKcW-!~y`Wq$pKUf`HEE9_v6R0uXVQ%X}P=k#81PHzZO*ZnG^jZ_`(gNZ`{$EZ#9Mwd{Q7!Dd`|H;I2m!9{NJH${( zDIM$Pf|ObB*6X^pj8V$PfK*gn(Z4Jzi%LpGsY&Pjx532)Af*9OnTS*@&J}xidh2h3 zuxYQ>?GPcr&BS;71<%C|z`micKb7Y`h43XQP0ED#4-p`Ge2>TF_=psX_zrNMV}?kR zGU7@kCC&knPFgx{vMbq@!XnHr?@vEy0RTo2ByR};0FvjwX`=YCv#XCh`p8_Xb@Aeb zh55EI@mIe3)rD5$xfj3EN|R@ve{QioH|Tfs;^WO0o+#nNk}<^0Skv1Fk!r_a3F{Kn z%J?yeppKwg7kL}zzXL=72Ef+E=aS`ffZiKqWnw@gtw|XbH&N;S`WxU1(_ZnUfFh17 zg48N~BJunw58Gr{iU|4yqxXAfm-Cn=5zgJqfy}eah?shsv*z141EZrwc z_^_nr9R)c)C2>ehwE3?*ixCfBy)zJ7h%33wJrlOrQrLkwsVD+Gp*`Eu4!dj% zDGM?&qNb#&lO_-Wl1f@CnNx8CO-v>Q6{C(w#i$gb_6~!Jn-sTPf8R6TAgZc#GZ=c` zRvQtg)`ac(1#qSNL{Qqt$cZI@$R@j85m+Swufw2@u)Y5a(sb zwzT<>^F9|tW{XNEtBdx`?vzO(+_}}pQn+y9lmKqn@%WLIee{NxEXM5+|#S0tvH-G$t?+=Slhy(vb z9=dp9J{Aj5f&P8)<0!2h(>D#s05p470bnaxPogG_){2}golBO_ySy*L0LoVkahU@m z2#aHv4-tI}C!aGRm9`_&y*pRp#WTtBc_bAttfA6MnP9kzjEkYOIor}BQ4mQPW70LB zMfCV@GO5yrU8%EvJiqFRMmtjeXecjt_Egkko3XXNwjF#T(U((&m73V$fgN<}^{waX zXJ%%H!hS{S8>jm0pNJ?@LWF=~3%?pf6T318Ku{4WE3Ocixj~Lfqe&oSLC4}>001z& z{;J#Gkb~_G*z(5u(qjAG*8U^s*M9WF9~Wim_!B~np2))&MAV58qb=BG01Ox!szb0b zGY%PjO5!M>W9ocF{TV`@5ThoTM4SU)H0jbae@2kTf0QoX}@{Mu< zP#Yp$bhn4wxAXlCoAn5h!|jTQ*ulQdGZ02#0aQBjbZI`zPozj|P==I-ki%n-4M3$t z1gvG3BPnKa`2Y}vS!@C2P=xDP6OsaDh!g@4(c0RnhyPpu$a1nKd|1+0Ije^ITRVFq zAz1~kV75Nh05GY9weHoyLJ@uH{N>7FJFKu{2Y4D>K++NIT?G-ULaOy7hdOFe)iwqt z^uS2Z4b?D6L%RQ}C(H#v71G*cR~!3BJo*$65M9V(FiyI8ZhWMG%p8G4;4Ec*E)gR5@`2P8|#mmpU zi2bc(@$`H%+3$4Q^NUH6KJ)bBTid(a8~09>@bfB&K)wi0fB>q{I#y;80D=bkoxLR- z86KH6+aY{8?xR*brd9fWx<$n?2Tu~+Kz-4w>Z3M@^$edn<;cd?bq7*|yrEJ*A4I8t z3?^%0K)xKpfd|Tn06PE_1Ym2k0hvUZ2pB<}2a$%}-E6~Zus$0C0D&^#N>By_J(nX@ zFQ0nso4@(@WZ2(2*nIDu3i?;UB>WDq{^S)RIw`04+zA2zJ7g9C1VJ4+n^)$JYc6+y z>Rk^$aPEKAsB1kyM0VD*9?i8|Y1;79e0O)(d4lZG%#%tFgYBKIy*Qyy6B;<$zdk&K zlBDWqG5Qw9cCJ;ga&)2Q?eOXc;~#ibgyBbkSYOCr6jcm_VoPs^iBtfB+uX4Y75V_e z49ud?6&ZSDk_dP>JBx@QX?7N!h0qgf#`CcEuaEAe+G9%P_#`~@`4p@iIRONv9s3ei ztP*bl_aAmf->E3DPx~mMfFN^m@7A?6jsM%f^*5I^fA2frzQ479>eAz1`-^|>U;n#r zU%7S{bu@lepY5A%L#Nemv}ybV)ebUYrAF2*q}^ab@t{(wk%^Pq)=7oLskes^#^2e7E2Tgn=mZabgzde5XQ00Hr)st%es#RX`OUC~0;D3Dv;9>Bn;BNj==Z zxZKuSw;G9wlVP_rC|soR`t|F>teE}lJ(16aU`_E$;sf%+6_Y)9!V3wjxZnhZqhzd} zFwb)$`tSeF-}%w+|JK{rZuJI3>+C!0_rLSq@BPZJ{%S7K&D(bY@cgC6o`2%}t$VxL z4K_PjXK!b5HcW!aX*-{hEFP}QSyf7T}+lwoA*KfZ5*1MB~tt?*nJoQx=7fA4O zM|L9U6$E9gv~Hx3<3S*QHkrDFKPT3&52;ES5eapSL|o>6?I&bNDR8B=rBWKS0RUIz zRs5+Q)R^HSL?&UMAgP^&k(~SGzn))zb^W{l@`*<;#Bn^xip7OF*U2J{i}Q2CZ2Q4} z+b8l_6a?}W+ybLHBJ8UEVBYogu&Np!T6utvKl6gu&urNH<~P39qVlEh{t=5<2WM2i zlEAwIwNuj4xy!@c0YYnT@tc48Yu&8eTEFqFU;6X!UA=ZNC|2g1%EZQ)Zns-n_dEag z+u!>0zqq-xA0fZ=@{f*OYaVk)Gh#8*`c~;-_^vXNa7ecgpE*2)?QSj<372eb)otA4 z!WHU>i=lY`9FepEBv%Z_tg5e@W*99_oiKwdJ*~L+Pn6atu(dC?G*-^);r{mCfry}p z06G!d+Ya@psE|fY=>fd4RKko1f+QnLh(L&6``TCk(f{%HG=X*UD{K8<{kqdeZ61_= z_YckOPDdwA0EifG*CBr`{y~(JU4f6N5kQZ zi;Mk!*Exxa&PnzwKPyYix)I^2)AC>JmVfu{aIu~3t>6EHKd3JK3IJTcaeaIki15~f zj9QNVH2&Yj=c=g6Z<$uSW9GX|=wZk=f-tiaAOu@@|FOCSaz!R=!)lwTed)^!Pkapk z*_C3;GV7JS-Lkh=_IB*B@A4kllAQ>!XTAs+9mEqY{3(%&h(VAPNHGcl`lz1ge*k0* zW#yhg|?eZAyx3}|4;cJd=<_vVGCslM@0Pq#uF72yBxXy zq3y@%kWp(2fl?9 z`3y@HfCn<-I^lCLhEi9YBOn5W%9~1b6)g0{@jyx^2DBlauukDWc@dY-s9xzJf}tFq z{o`)l^q{8f+T?+U|DU<@Oh5C;gC|BbLniH^pd0LBx{Rg`W$r115zyHJYz_cOB!H;& z;#1$y>6{1?DI^WPx)PDZD8rI8OnW6+xdd#5o$c=(T>D}7`YT-abuv#iae+v?-Oh?K zX(%N&MP;ouicXa93nSiZ0wM?vAj0gdG7~%&9IebCq9Euh-3RM;?)?1MUzl57NjZOK z*Y54_HQVx~Ur@s>_eVd({lZ~7OVf@X(uC_cbI2oS?LiEh%=|d}v=8*P2(#UrL_WXb9Z&(J&>CzEj{zZL#!&G&i9kgW#-lZ1=ineo8`ilYL$lrd z-~M>`*Nbea$#1UX|NROSDMlgy&}_CA=9>5JZxWHuET?IjWjQmGA|EmF!!b!iao!&m zM=H&6s`nudc|aN0Llky)e_bz}7vET$t+1J>!r^zTQ@`^8HivebGg0szk1{X2T$9O)RUHwXZtGK)`r zL&wdaexXY?BB8MBtzYS0d%1V}bvNh;P8syDn54f%FSDMmKNu_{p`%z z%6@maGT*#?f6FM!hXYTBzp$`iqF4dn-`q};D9iGP{vb}|VTo^i0)Tb5NvZJJ)sFxm zzBvrCLu6qQc4CW>5-M%pxq1TtKy=|iKtM$YA`n3m0aU4Q0Jwke?tHu1Z1J^s?}>1y z*VkJ2y4|d>);dIhjjiEu*lIT4cxNk45~Hxw?fH>>z&rc!59Prm#E97y_TXN$e91c! z9@*D8`($J7G7z%k;DQPdH*a>Yz1+R|njLsLZ3GbXc+s&@&jj-Qmw)-st>54N?LU0w z@h6|Ue0J&9`rYN_m9^8R$n)o3cxu7eo~37>e5AL(cmC3)J9q9mXS>~=G0}y~kFU%% zC`vc(-1@sHPkONa)HA2+AO6 zakl1|sh2Y|VBu&>kkxgoFsSe-G9h; zd?KHS1mq%yu9gS_;slg0!5!PN3Py;aFp@&0b!8&Q{Lf09PJ4?_5tq%~D-0(AOJ~3K~xX@C?ZY}fg+63sJ)t= zeq3zXz4^-U#`k@o3&vsZSRaW#!aVk4&{YvYnyrR^6CuLsyHK6 zIJ2kuTrO|0!#+MfmB6?VTXzRJ^zfJP>(TqikRkq_n&?1 znKR9UezrIFM1A<)&ere$-nYXKfAqc4sj&hqA64V~;PTNGc0vtCSwX0f30{SCdj+DV zO4?*%r6Uz3{k!jq%>~8xrSX2fjM+ngfG(=e$s;u!4SVeJWB0Px%|6gg)S1{5`JBnv z>dga3LxJxRqQ?Ra6M{wmJ3oA#zy9L?{?d-R_g-9WpP!#klXx)9m|1J3wdouj6s3hx zKw`2Se!>cV^4{sR23D0^2!OgVubL}B3PDty4!3UG!GSN&03ZO%RYelbc!DwtVucR~ zf&a?a0Z+$ge&&VeHa0ht)?zEstyb&Zn|B{Qv$(ZCSYBvD+qc*LT~QCuLKDZCS#sQl}A(v-LOEMdLGZ6PoJ2d|NSd_MlG2< z6PxcH^t#>t;#{MPE!lsJ9oSpRb(KV5=jgfpXU;Jd{0_-_6K!Lc*{ zQmoe^Rh5$cHIY*n{zer~m@#INQ3x z#h^Bo)uNGVtL}YwK9-7*#m8qxTIypppPl^7iF`65M@3n{uTb2mJw>Gx75l}kT*lv8 ziT-S&S>*riyMMeNxWF!7e(ag2o){Kx(AjA&o&J;W{O&W)z3}35Pp;qFUS3`v_WMa| zZrJZ4z%U#9$;&@}AKm&`Cuz8=8cP5GHG>oY04g)syk7M7kQ4xb$YcXu@@r@>z6zzb zk5aF#E3O*_p+zD92rvjC28ASLzV_8GU%htI7>!8ISyFn~>zX)ewps+z8)OH2I~Olq z%CbyYy1l_%I~@#j6Gek=r_<|yoR8^59+r&7$48E6y#;*4PMC3mhZ^+w3C0hy7n^TB zHGl6*|KO3;8-IJ{fB08_v+NE80N%TCI~xunt@?w(siEx-vh~|H^Zr4npKtB#EzY-Z z+`7?d&UHJTET0*X|5S2@lX@7kC^)$NdXZ#ByWP~9 z(v++%+jH#`CH(BmrSjQWs-Isjeqm-tSzy z-u093*7{u{dgaGIg36J*&OUE;XEF%%smg>(R46q80I~gDa4gn38lsL+DG>w?l&n5L ziW6BuZ{X_8V7)Os+$!#RXb5bt-?&x(xWB*eKiry(jNG_!<0IegiF`UT_NuNCGh^@Q zFk_^$0)j~FEQql1kxHo72g57d|Llps{cGoT{{Ful_A0W!Q>RX)aipT881|z$Zlvk3 zuv?p33#}-bTPX(}M~2v@jYe+y?(Lh|$J~4Ufu!;h$L%3_LRKzCMF2|5jB^J7KpJYU za`DW-6T(uhyX(^n5RNbkWAC3Mpxh_&Qzv88n5Zafl8#)~0WLw{NF_vp_W!pYEk=I> z?8e?m&-V{H(r!YQmu1;RM3CM6POsOAqs5)|+X~nuX*L){aZdn?*?`g@hrLof`YU)i zhP5_}qu?#tjE$neSmDHGEDpwW_OLh>O}-S->!@JmTU50afCwxzqB-;P^Gc~xYo`aj zt`pHHTRVGMmieQIU~y@skr)#t*RNguwEOv<$OjYwUnBwOAuA)(=jRBi2%{#M7>Q6v zl9vFWkD1vBQ0|o*lN*aX+o~3OwOa)MTiZL~Q?CyI{lVZvXw4@w#xs|1**8>Gty0`i;4@3&rl8&4YoC8%cC?ke~GK{X9tkX#}HJe*o}lL7kvU zQ3r}c(upgEaLk(UVK3-rrR_dV8F{DxfUXoW;A5l#`9lDqwbQ6ju-+e0M4X87){w#u zfe;uG{GUm8^sQH3f#KfOz59f?xV-#ZzjL+SYF@v2D}VP|l0*R1IoJn!Q!9$Jiqpo4 z27bQ88@-UKhni_d5DCekj!`G56ku?oP-##GY<{#70?49q|IrE#9esNT^RP6!@W^E; zv$Ja}uU~n0X=$k_O06hKn~l~SX9ow}!Tv#KVR6Aa+1j{2%bWVNweJWKI|SXFnNd9Q z5CI!FY1mdMB^bTFrjN(YZ2<78o_lxi0>Gdj!k(SZp|hW~DExeh094vH3XOUid=-;U zK>ME4A?q#xAWWnkWO}UpsS>Zhnu@=6hWCbeK2lFyF#qdK|79yNPdxvnI7!pA*=Wz@ ztnR({lXPKWd%yS9r!QO8D6-+D3u{08(Q6y`?~P7(XuzK}ArW>Ns3;T#0wYMRk%v9f z5s%$CKqhSWfg4m&)3g>osXI84Pg5kQAp7vB--ybfN>CY4$`fw`0Emby#b#nNVTO<= z#Hm;X0DxRb;A{e&T<#C}XIAQf#@p|_`}RBUBBIs`1WIeQR`$j{X1>3EJ#92wiTQ)? z>|1L`!~47lTkt&tKx&_UrX12q9RJG0yU8T+smsayDs*ptJi|Sa&z2yfgvNM`h)5CY z7?cK7K;$Xg#T8Iy;tB{g;s}W2?&yTc!n-*j;n@b9NioB^t!A1w2&F775+!M)mByP} zTfXeAD2k#e-p`BrWRvwr%5+{mIP|UnGNYK*5knm_gVPDwa57Z7wH&vXP1=SNVwj%- z!4V<^0X32V6@k)z1t_*;mxIkg*f-FuV|f6_q$qMWzkb7g>l9>8|K@r|r7ws?yYl@CC*b1s|WxjtwDK!O#&C zKu|@IX$rNmlZ`3fp$~|_y0H+=pQ5;x@89p=dUbVi{-NE-iF`%{fi!?ZapDSbg|Msb zO9g|hKLbFO%m;vDl+fC;7(-wuX_JTmKq)06ei4|YX%a<)ey^~{v)oq$4I&d;yk-7a zRdKc>I~{r%r%4_+(c=rhW9yj!Ku{%S@vJgY_xg{D-F4GA(ZHVyL1A{8xB^)4a0l5* z@#M4u4vI*! zYd3G*xwm=D2xt8$6I<#)$*19-&QvV~6d7A@KY$>hZY@cf?Z5L9=_oyQIchFIx%2)< zp2+7~M8svpmMh52vD0PZoqlW;1qcBA*Shh$F8>~v^;vhfH-HF>!S?>!!glD?+I(_s z#H2QNW0p{0mMVgu`7<_uobrCg4e{vFjDu+q5y{wL*9|+-;@M>RLe!imLfAf0!Y{s{ zgu?CzspIy(Rd~T8>Z4!t2mrgn7eeYR$lZJQrwD3~&4L})KnQlP?reHk5B)uT{CX(I zYGB~E4+w1;3xxtyE5oT2g$V1ok*r*b7f+kCO*+Dpz|2pFNCndlp^h3qHAA~5CQ@w) zkE~w!4VPV-RKm5j)2&8KIvNfJ=g*zFedqq5-`U$g47-9OG;lPMF+DtdX{@5S$I3@k z)I*0)w3Gk@00MmUbh_8KyWL`HOzjF~$fFP)9&mQ7<`B~23MW9vvxHS%Hi|8UtaJb`L5$hzwz)4fIM-o`B%Srp*tx4!Eas9?d+u^2mqmOE}6L%)0|W3 zyh>Wi#9Zde#D@<#kuRhmz@#b&cxHx~vpA@Rz!A|=1b`%MCh3esbAK@C4+ak~p2-ij z0miA&V`cFKj?lvgxX?#a#l!iA@zXzjuB8<$wxhMh=+@SBo6bn9r_MEZyXD3pGOZ=k znA1tyBuy11q$AQ=iovIz2tAQcSZX5!#{oV@vz?CEqu%dBU{rAG|LOYj84l`rRr-g$ zA|CpCXhetPDBRgdEQ1IzgYb-Z#F@otv8gYuwW7Ht-I!BpTg5Fj37JHhm{g3D&isjd zfu(loMytCcA`VL}^I!eaB~>*JI#LO>R&%XptC=>_G>zkWBA-5YacOb!iOZMLG+kLc zcj@e@X0r)M?N;mT*|W`N%NWyaHRCvnqc{v|w#?GQ2~|A)@9}4wJk9K_$EE(Hk~eR4 z3(I$Qhxhif!^KIBNC}eG&9u3wlD3MQ6eTK(Rg|F8NHqNdI*~7^jLoMR-%idkIlGvC z{i%gWRXtIT3d%%A&$rUGw0C!Bc#x?&;k`K5cleX}TOKXfH~atR zzrNAWZ0Y2XLdn6^CM?fQ+IH*W#be5>)!^;`Y!!Q6W{ zncW+&{p7W`Z(O}`V`XLa`mNisrru!qk-XX2o5z=b6FNM$(0yD9%#2OZ9dd~Rg|(lKcu zFwpje&4O4m{NzSAL@7n|7^JIS%LxED=i<2eFWx)T?G6?fqt0*?peLn=gMN`jJ3Cv5 z*zFGh;H|5-i)?u3?%?kI%`{1RoeZn?h#x%VS4URIZV_bit6Ccopz68^02Kl6tVn28GN1o*N$6VMmGqutQS1F@NHY z{xr%_9w|=GX)ikbI5AZa0GxAs2i<){YXPyWPta(#&zwHp?d)a!?wK=ZTCMidVk^sX z)JfJm*xlQE^2x_HxAz-qVjXL+d!7Ex+w1Q;#EGlpv=&xmX5$Bsv=OHc3!ruH6^z~3 zA$34fp!}YKA|i|;C@jLBDj7rs7zB|BP@z&l29-k8o~i&zao)}PyWmP;=gC(wY9cAp zS~nJWI5UHKB0r51GGQ=XP*J&~-h7OINh-766c7Xz6ad)U-RT_cX{A`i?ss8hgNU$- zfLbTzn^!u$L1c_kYM2+!O=hcQ#-}kE)4)*>)$7!_D#EljLNMS}2IP<}o$2M05?}-- zA^}8UVPD5IvW?)o=a2$YNCc!v8Bzw7CQ=|Q02B!df;b6Hd=-F@iG?lF@$u&q`9jKM zx-bAg0_n`*d9M%K!J{Rpgmcb~$|l6s-DD8}aBsf@fJ5hwZcBS~<^;z-;N*5ee*-m* z??m1o#+=kyQJdKpM5VQWfMWm#BvRx_vKRz~0SS>%X;PY^MCpiBgj$D2bDmaI7=)P} zFnRYFfdP@C)b(~ha@XP$`FsdgI7gVE$@Hru98J!E?Ld$bgORs{m4(3vQOMx)5h-SO zls&6XI`vL~2hOS2_H7K^Hlc-8SBS~8A3Nsh9T5?T2$c|{usADh0SQPGASy*V)^S2n zilmVgh@kHz;Q8vruOhwLdQt=bY!nhnnLh+9&xw4V1Z$IqfE7|^0-FNj$uzxj45j0G z^d;bwQ50ol={&d+_wUz0M#3pIYH6wcG*2bf!7y1P> z64UDsS5{XB-TlJK>uEogxfK+)9!Gje<0;+5y0&3%WyGP&br!QKo#S4#p+N1a) zh)G++{*C|&&}=(XQ9Ahb znP(aiTz~J~q_uGA(aTHSF0MM_Txi8|)tZazHV)^fCPn&MVC5wq`Gh=9WE zFOEnejDi2x_W~xdW%!n6-o>Xc<=)2o@APR%k|e$TP9bU)EYelSL zhljDFV@_#l&L?L6v3dRKwY56DaInAk(o5f~e!BVEYp>Mjf9nkZfFJH{*9vmYw;B3C zUg+3L5a3YabLJt{HI9@+K5-CN0y7E$01$#`0R$BRH~<9*MQ_JuJ+pAeH0DCe6#;;% zlp^~gLSU?#hzf|HR@|=|1blYegyN~y7v`EkIvWhKY%sU5#J0F|_x`b$I9AjQ-10)Z)$jKf7ZxTdR6dM2=d86M zoOZMjqMr4lEX$I#Idf`tIP5bhS7a)RHBpqrw#f66hy6~LkD48gnTG0BKl4{#*Ab4* zjHw49B< zb7xApwAzF>uDlz?jfKU9h2^!ke)96l^11f>l1ZE8ptHIEzG(hglC*Gc<o?|_X|t8( z^1^eE-oAaWaFWJm`P5mP^{!lb>+1De!O&FEmT3(fgAXMEVt`%;g8>C1-(5Q39D ze_2oi0+v1M-SyusE-uEACKbhz8V)n1jaFDX*}i|ba36ZcRV}`&e~yrm1|GW7#D*#q zb(94R`)uC3V;v&^2#Wv<3n?8pIUj@-wC|p+43Z)pk&e*Bq+>t?W)KIcKm>r98LNid zF6)6S{h}KYm*>(C{5(DpNz+6Ci#$Jb`c$`{%{5a-I@sGw(xg8aWWy{?Qys;LQN!VI zesLMu3My;Mez((JSTaW4y?xt0)W%WmcH4jGbUMca>pvM;SzSc{qgAihLuJx9LPa*u zdxIeW%r7juvKS2doo;XQ{#|jFnLWm1N(rl!nNZcX1ksbH0E--l+hRi6h7g8z~^d+>ZOwBbINN{GFjPBI1iM5EYWDtDl9fr<5IgHq2!%uAIpCB8IjH z^`&IG)#-F@9Cy*w z(6g7?uUtFe@$&I->LZDs1c}8W6rWr&pN-b*i6jb`uo8_xg(mc)lW`ql=%vDrs?E9E zfj|@`q)nZZbT$VOVTVLWTG*Any|S~%u7D76t46INBwgJmgMILLqj#r{_AK|s_o=?F=&E5(-K@q&+7 z;`9MWk6_~ZI0|wK0HB&kG)qVji!d-(8d(3GKJO!Ijjun}y1O&HzaR7{#1ll|7~WB> z)WvgW`UC%Gq)t&BUwGuv7oYnQnxr?#pLp!TyYF0EK6CD+-}{}#rR87#)-N>@v$J67O}e6<4rHNXJMhs03Nc3Te15FA@ z6tw_>;s%mJRDdY#0tmu6|8$D(ZoaoJE~Idgs;;Dhz&RQW$R0ou0v_YYne}YO&Fjdj z%Gmz?U*}LKKng3p`7=+oFRmsa^4qT*RHV-08?uKtny~Ps64sw_Y-Ug5?q>{e6jckW z0RTW1DbfNc0_-Y>kR4Qg&ap}v2W~E?@2$EkpW2mRo$4LN`d5MgLtUR8R(&j$`iX}b z*BF>7UlgD-D02tB%vLAxYr^pD(UgZGeK1`!|D`WK_40SWQ*STwd~4&L&F1zx-9dkF z=g#fT%`JW9-E2_o?QY+^aRWrOR#{PAvBiZ)AK%&9xO;zd!daS<8JmyvKOe+7#`-zR z`{^MnAOL3(fRZK@gX$eGzzn1ygtAcBL3p+d%;HJ}U~9qkS>-B9yo2h>Oq>&20GGf{ z#G$aHTF<2jpumc&UWBzC2nc}BIf_WsS{5V~C7_glS)z`S6o7DH0Z@Phn9E^tuvzTi z9l3!31Zwnv7z6-KV51cO5~(a)9 z_))PP>#V~fJZ{uxom&o9p#|L^pVk=k3jvS}kQN{SBvNdx$KQ)Eu&d7y)OGBf(^vLq z8++9peoqOqRB%NA5#|cFI8pvKeFF#paAzm~jqlv4n!aMa3IPB(j(3!~*B?B6>Ff`` z_lM(;V9@Uu+3?!+n?%ISu;FvQ-MMz_%9VGBl-5)fA%C*D^YYlJ>X+(anRNX=jP6Z5 zvk#z$0DuI*qzw#*Kn6WL&U>RZ2v7u3#C!Cv^sh4iJ3vI#aWubH?5?~1uGkE!qACIu zz<$QoMOs6rUjn=8DTLR)NHE9V2R8F-ocDEO9+d_lB&4Vz!oZeUP-z4yx(C_zO&%VM zKPp57K^eDUwjflXjK4=gEjIiAdwbJgTe9mu?6=n5RcE;4o0>g6Hk&h>A_+|dX>!Q`x?)mjXZ_ zIp=a8t`Oh{4_BXkW%|y;bx*<);RU2cBEn>;+H(AOwiZ!*4WwqzF(NU=VX`ATUzrH( zL`(o0NK}Xn0A-RqBVD!oCKJSDeUD08tXNNgfSi(}7_69pXk=aKiE&DAWP|Iblls)Z z8o(uzP*l$zy!U(e-T^3gcg|+BmtMN9st0@X#cIt2xborqKRG%&eDf0@U$s<(g%7b!!D$R=Qa6DDGQkyjpGKMvj zc8P{`v_x@nvLj6tpb*w)G}(tM|Icrqefz!T{gbxmpe8G9Nkx+C3&7G&dNI(8p1AxY zAvA)g)}WdYB=Ycu8Lo&~+9sujM)8!Jqk3(56XtgCAp2{l6-OpaHZ!8}oKM4|(o5+y zL0cuE0-23r$P8$AwKb|vvlRdUAOJ~3K~z;#O^8OeY%FzCKYM;k(ma0rn26f8ZG)-= z(Amk^dDFDsaos*RK02!EXN%=3HFzZ*?jXMbjzC&J?QggQ=;9i}PyrP;nag?s_Rs)u z#|ryq$bz>UD5)#_Y>%f0<^D}SJM`1Jh}5T#RRQ+oXWA|aAqqSK;E>;=%W%S@$zxBP0TM6VtPENYJ(-KLvq^;Yw*& z$uc(pUmFuV-jdl=s}VV=YIV-#e&Z+aKX1F+H4e#^j)68x3KJ}83r2a?0cJ0m(7&tS zWZF+rDwNqL)7c-Hxgb{%IS_+bf|yCh6t<|~^+HKg&FjtOkB2LFv|)mm)1N!tm_R~8 zp^17*>`cZ)a~LwLH#!K*9%SSJfZwu%Y3DBh*6Z~+v`+!xv>8aeN8|9nU8+FdO8>4Q z`3+Fhz|23O!+t+)ykn7 zsIw!#e1fJ~a`V5@Mg{E~>1u^-;P3Gh$1a;E`KRa;K zeRiIF!M<$Qi|X`o$^cd{!xc~sp|YUT^eWBMvWyDj@oOpo6?@0U{76CUOBEpyB%t)r zP@Ozly#Jl*^nSi$(k8LVqLIOsC~|a#pj{L1fym;1wAtc18s@)^|G4G;)D=;s`kbLC zW;WNHqb&<(*EY`ffNu^&RcbXG!tm`3B@>{;R7|;?axqc%L}=GbE+zyeQXvN=M2m3* zLfNb0Vdj^prO_S+NIDXzyVB-p6$LQyX6YLn#gmPhi-%#P5$>8{AT(%e_!1!askRlFFibQ1(}btklu32LYIK;sj`EO{7+}rH zwhq+GK}$P#PLf;-5QxD}EsRO(db#xTLsv|ja*bAO7HTZ@qB&g{t#N&bXYhFS(eg2s=l`#FcYDnMJ#$bL4!xIBCwFbn{NC%8r%A2^or& z+Vbs%qSB@bf#v_QpH7ZmQgg&hPzee!@#@3(E`Iz+p&A^+IwU_OVxrxN0=5C#H8~G< z#E$(`nnlOGthMO2QtIrJJLh+3-t54M)C&UOC*49C&xz_N<(QnheXkK^4qm$5f@*t# z*c_)s6zZjhw%EVP#Z*HReG;SyEiuDS6hOPCB%8X~&WtpfOMdvLjtsRo7Nrj`3E4Uz zn6aR%w7qrfQ9hy$6AO~!YLyyvL^;;6rmmkqdv>_DfB*EXYYwXa=pX(g`~1D{{{R5~ z?1uop{e!p1Z@!FX`Y_Sng|kV1X~YBk1t`%aZEt@uZ2`=-FI81UL114n7vwxS? zNKREyUyusiB(i%-E)ybTn|;7@x2@OKSEQ{#L=J>@dB)|;AHGDU?k2AwB2f#7P55hA zqvu)KTQcN5CVPV9hVe_UNKvtoEfdqV(c4iwga-wQr$~fdx1w&TKvUJi^yvPBe3Fii z4xMvQ@kLqH6+oqTLFC!4hL^Uv@z`lv`X1NBS8qlG=mo?NV zCXO6=PqCDfcje5Lb1tK?!4(s$^^|;fOwKd=>hxiI{?sz-2YkekDKk-Fe-LSCJRK=I zmjFxEhLqFDNU0)+BLd;61#SW z4Mt|r^@jmcMYB)|C}to|CK2`Dcst?WSgaRfr@jSflf3c^#<@nTuOE=}H~dM{fec-`P@)Y(*{Bg^M;fN9$+ z02O`u+ow>WnCyjF)3#+<&L;lZvlDE&HkLJ4{wUkH@qd&EG?&by zIoX0LH(#=S7JE9h4ev;aiV2r9b_Iyo6(Htf;>s!cf{2+N*b@`T5j!G)nAuBPubasO$3S$)mc4kBqZ6i-XPggeURL+XMBO(T=Lfh5F`H%mf_UvIs6j2gl+jMMY zKwB8U3N>q@JtcyhAXFml;K|Fe%ZLjgE?=)u+0axG){qwEp82<;(RhqVvo>kto6+#P z2Z=?6r1i67H#uPMjV1}&w9V#Ic5F3V$wbnwRok4MZPkRYlzWKNJ@R2`>SJdurF>m> zHmgvD&oc?)Vn+e0b(7-#vh>_1Po6w^^28o^u`2^034*3H!cA&K_l5+{lS~}7wc|~f zqSd}13?dC}STEEUR7}`;_9gp*d_m;Nb;S-H#nc31W)N3rABN?rH4@!Y)=^s=%8uG5 z278DmnW=-h0Q~$0TjHsrdV2r-NB<(M&ulvS0q+Pj*(L^c=lq05LQQ@OW_D8vi$MWC z9aX0tzjvUqWE(3jUs9eF(3Yxm_$hpM9?9+{%lf-(m`nta2*?#~y6+})a^@jST-8F@ zGcwoCx2Y}IIX{!I)>tY@fld?dvHb>G*>P<{V%;=!x}V)ik%wrSAsTN0%3Kr)Cfep9 zf}k_G_6#KJFAr~nowW7p z$xjyV|CzR3d}-g!U95DALbzk(cBi%#*n=IM2YZz|(@3 zO1mIwsF;Inm08i3XxM6%-7W@GfGqn0e&Qy3JzE5e)Q+8jwb9eDhk%MG5xJS7f;9QN zaA+?+Rf2;+Etsb3hM&6WJ|tY+|L*$HyQ(Q{;2K&k#U!fHojV^?D5OQZM!5%a zMw@lBsm=&Y(H`T-7jdNk zkRvFuleRJY)>eRt2r2|1b`u3u8awF@+I1GHdacbB-MtzOaGQpZ48LWc6af6p&kx(> zspXLaRxwolV9DZmVLwa^T1k~$Bx>I7B^|$v0^>4d6o{MxQ-5+XI2zpLSL*6z_YjB) z40aTi7AB(Z7-Pf0(p1`1&EkXr_|i}J{d8~r;d|OPrUDpz=ZyiU!J5<8O5HIrV3jn~ zqM4Z|^H42UkKe6N9%h#Mt0^$uHl~vt%x_R9DO5v4Z4Flt7es|>MMJqXvQlINx>m0` z9}HPq7gX96QvvoiSpysh*}6&t7;GABq(x0BB(4Md|&ECXMA|E@nyGP)2I-0e9dj>3e`r( zA#5+yhd~E=hf@MGQU8(}8koHj8{uV6!i`RLz72G&frva8rMiMTdym3SfYf?5d~j2@ z>?lK3LrixLHTnp`>kU~u$&=fy2Qz@gt!xhyF|j8LvP&jA08zWKM1UR_Wpj}BjTv06 z@U>b*Fb40>)t1=H&E|SVwv&IzLFr&4rIB`NTK)GgBnI<-4OgRon@Xc0)-8mpT45Ob zm6&1)a|SUvZ>mMHccWdMaWM(?TAGS{$<8IQ2{1zf5H0CRZ6FXM1h7B_!em)6(t=3V z3kemlV?SPcnUabmc1DtFM{`5~B%rO~CvcvmK&TX8sA|d}`3RTl2;jluK`G)Wft&Z!)&4sx6U&o2WF1Ces~ssAH@_ zWTx=V(PvAL%sfh-*qigYqk=xVJ<-wjR3>9VK=c_>GXGI$O(WfY{Me?Bu={V_z{h5} zP6~En@C9Vyu1;;6Vv!DH5dm=INDVbQ7p={d6jxO)=U|q4MXq!usk9OrfiU|-$Wckl zpcDYM(#)z1mADTikXX#1GC@Lpc^Q~Zvtcy-?+!W}ab+4Vg|<~X=%x^9prRWaZnMEl z5UGOpN;YmZEIXR_>PE) zAQFfeZUSNrZA`1)a`$LM?c9hXFi*rF$Lxqa*!6cs*YKbncl(u(U((?rTui9++AKR^ zPRf592*Z(_?6pl3=9+#TU6%kS+VS6(yVikFPF*>VqK_RJE;X16CTE6I5D}Qm!&~k0 zRGNxlksAfEQxVma^A)Mq%1rbsjd2-bCgxPbo5^`%hZuCm8EeE&#+;9Uh~V;Hx89RF z0b7PliBM8iSH#881asnYXkym==DvKF7mj}8U;E_0@fTis=fUE`vpRpIlM)85jIC1S zTlwyf_GFMob3ueo7AaA(pRg~%F2%uth%_`|c}hcm-%TQt}1pl&|EgS4h8vo2NoM`9$C@-X<6?>p${1aP<;r#6%=)m)5D~31JAvr+Xzv>Ok!pZC zX`5uQRS<|dl3!+R5Wu^uWF{O`;RSTd{T%NLkUU zFgd4U(t=ee)hXI350moEvp$!4orDe}AGW!0r3tlYTZQg#rVXk>MMI!I?wmU# zvvzMEW@u-h7^-$E9)ZZSh5%Jn$pV-YGXhM30l*-!YI65Ll+FZTE{R-M{Q%jw zR}!J-KXv?Zz?c^?CgSdmvUIIj~ce#MfXMk$lO19}4%r?7IdYzeRfHf4B6bQQ3J^nB3~E){l%v?sLk0|E zIYhMqZub(gKTl>ZL-oLPYo2yBg z!7$A&5xYsTcgM|-o2qhNqgG5kERP7vn6ij!9e>T}$m+^$UG(gw+y0vVRSlGaE65Rq zG?k??q~1*<4XW8kDXB)x(Z$6k06~QO3=GQUZq6T`QjA2 z8JBymoGLr17f~Cx8>!#Zv1()T;--z>-vf(VqS07$TX8Z@3oDW#j-(clJ5 zkYiwHR7(LD0|}xSG$Q0tD+FR+N@zL@e8Ty?B-}=UNj=k!0taGh3j^Nl_n;f&zialF z-9bY;x!6y6skD{432{`&($_YzjL@3n%gv9Zt)b$IiKh)hQKAeS-JS zIce*7bYa|fFjX*m7uA54X~_guOP-;~)lIarISvM35>aK++%do78p@w+(CjH!^YhO|2|an>3U0rH)D7vH(Jpd|dex7b}kG!hdr`HAyWa)m-u9w31o z1YN<7+*#iby&G9-(^f3g#zCp}M28nPIs;Yhvfa(-C>lU6DGe5>uc{J#bJ@!!4w*H1 zvw(~>Fh{%5a48T$pLCi@;qQPskPV)RzQ1qo+8|C6iLjnIH#JV$UwY-Hyrol3W3( z)p#)xCnBn%K`bhOhyXU@p$dSYu~bT`3YKEdQM~*>YCO2U}g)hv)9GWUFh(U%JxQ7uPj=OD|UVxezAaq!1p)T`7V`=|rB{2gT6ESpB&JoxNH@JuQ`Oc5h1nCcidBdpvZO>y>vT4^Vk$u_W~ZvLloONlR>K<-w5gNoC5RZ9m90LfSZ!Hi zQYDh0kW}(^LgAcF(S`s`Uog$Q80Yi?_rS_3Le?NMq*dC03{iF`F3d?#lJjyu%5cRIM_s&0h!-8kI&QY$L)BKMYjCD`#f+X20^H zekS@ob*WK!aOdUMB}y!_^F;_0np)b1ec@b+<73A~$>rP?)B5ajYDpkM&a3%}VeHRo z3kemGQ&&XhuS!+|HyZa^BIl-isXn);20&uXOk{vx6%tbXrTLIanapCjIl~5xTy&^i zJ!cz}6KBG(tsxth# z-hcJ8(l+htyj`A#^&+$t8Q{^*q$&g`JEJ!e>Xt6{Sw-_Aut}@DQXY;j?OjdDk8C{D zct^7&RVwCijsP{67ZKW8V}xC2aw2R}AQ7o@Qu3`RDv={D*cEV|TmfdV6@cpElF_q- zUdz!zhes=D(sPgrAxTDEF@EDC14X-UR+7F43%l2-mK)M`h#sb-_(edJOFw%_Pz?wv z(v!f%p;{0GDh%pOcRq52!5BA;F=JJ5*EL|(S1I%~`nhEv5sf+Gi;~|?#*x}(HupTS zleU(&meBfQ%Eg5IgowiG91@fySzlDu%D);u5EW5rSjogC3zN0`I&|yCVCZd@O1>j;2$dI-#L$mHV zU(6?rnEkHkA#z{>xR^TU%VKs= z*2}P7w$&n3i%>14sUWQ>im~<&X*8vpk!}b`du5CuYB6kAuM=f?dc$ey(2xQ|?OIi- z+=DBL2n>a%2-#|ApQ3ILae|-H?Ze1QJh8U|1#spgjS3a{GF&|C#*)!tVvz}SZW(eS zi;)_nv10w$LWBGks8UknwoXxJ28)Q4Oe2g@7G2;D^XNfUHNU?SDbxx*T5ULESL{k6 zC+#XoOR)|QdF@Oziu!@f|4)U6(O{y zku$LpNxN>BEtgYSFNqRvQBoE1&RXiZz9e5?XzO8YcY5mb#}M#F9i@Ptpb(%a3zT0 zC=QcYTRVxdmjOdcFn!r7gLtbHYNVK88$lK9v~4>g8fk9PMi%|!yShF6-drUTxIde5 zQw*k3-W~JYK2HMTE;&$P;h?^w!2Ey~qD&)DD1upum>tNa?WfU}&W?*c4YjswD|_hG z%m`-fUSy zP_5gh)_NHY@gf@52rXz@KYyU1agzfsN@6BeQ6UM*AY&^f_Mq)8vyH|88e`MMt|-c* zJ0O)HQK?JEiNKUbEAmYsX7PoTGifVrDrwdlS`Dp&2(2omn5?dh&6pa)4m#{`*9HyC z6H^-c;0YyVMkL35o-urlW{h(ZIhCi z3@zz4LGpf#nXm>ThwYcTQ`o6A3S{c3qSX> zzxN0K{O;?okBD#+lrp>Zy1MuN2LPD)?%mfHi{<6Jfk2=7mJ7Hu0|e)(e$9zNWd z+p*}obNjXt%E#}%{?5DahBl1WO4etgZ6OU}jF@zqB0y-C6q+Ed%7H6q#Lg_tAO%v8 zp|lduu`r+pRXZCrayBI{_Kw+?+S*Y~q=2R~^kN2MIyMXd*t>$5Sjdqt5n5>~X;#uG zLPNv~CPhkp+?WGER5{yK`Nqw$+{XHByP`O1QJ<3vidl@wGC}8jkGxf(!7L4w6dco? zh?pGrN)*V7kE?T5oe4wMkdZ{@WKYm=>yT-q%!CfzhNFupv)k^pG9*hCu@a}E9i)Ki zoS7?>07VEvh1ijWObu)u<5$FQTw(D*K6<5g;Ih_xM+3~4n4+Z+^4mk9|cZpp-x z6xO61Fr`KJzP+eEN!+0*-z9?MJFb(W`?~@xMkcGD3#d>z2YZSk);;`+%8QvhlR+VdU#hZNF)asT^9F*5UD>QoqkX3X zsHuA+d#@&y2|Z8QISM0P1EpL16FLyIJ0xNDT7$zNVz#jR4zH;>phBY%a)omxB7UZx zJq>y`?kL#772Y%r?Masi%0wT(`})nBHy%8Ev}>q&PbZVf@BRKi|LQOP{O|nkKmEiT zAAj=f+1A^-KJ3lsf8($JwTp{|P3Q0Z!T-ID)(78REEad~zW%-M|KQfGn;fG$hYx7~ zr2X~m?5r%KHo7zb01zxmL_t)F*>rOI){XD~;D_5x5otEE?+mpgG!afJph4E>TC|`; zcBM%zO8nm%FheOgiiO1T)y?+H{hQ<%B5?7%K2O`T6-3 zY*PW?=;-j~%^T?|*Q=X~xB20epDxown&R3F9-u1#zkEid15jSSM79xtktvF}0d#5<=1j z`*E5}A{ND9p1=H=*`2#eOlpz8YILR2Y6vPpC5W^L&BtfqE4PD$rft{%`=6eys|v9e zximfG%o=r^NvW8}?8c6Wky$8Mac7A>a(An8p>d@9v_8(7A zPft%z0X%u~bob4h7R}8i%(}60g*N=;C+}_RM9vSpf1)sqqqt%OK-}L1E}@}|#po*O z5uoiltW+e#!Vu$&@>Yq!<*e90hJqk5F;t{e2u)^+3PhxBr@EGU1zEGSrLI=dxI-!; z^-5#f6p(OBEPX4QyUQ{q3X*x^`ymfKdt0wSbWc2TpXl6O?xS7ZGMo%+cHoT{-*o6JUrpXk7}cSR@DmiW~zau`UuH3)=qGMP`xle05J;OvUh)4FPQ(cxrL zR#gRHHk+x)x~iDDEQ)niZM$8e^V@13s>nv zLm{m;>rgG*<=I78=-jHQbJa*vkO&gcZTJ3VDSjjR@p}N7^%X*Ptlwo8ugxnLmHlpp z9d9_Nat7yheXgOgVdq#YO=Kp|Foro6;zFvTl`Cc-Z-kbfPvjS;6REOtOyZ-|;$?ba zHkufApP4Z7r#coh`T#&x6d*91XJ3*lNCh+}-ptMIpt}X$j}`LaWDX!PWl6mX;8UM` z6RKbP`q!81wXDwC$&FiOqtnCF=THCH?~m2Cy~E>Q{l-@p!7rXY+B-N9@cZw7@Z~Rl z{_Xc3?z?(+^X0RrkN@xw|3w`JL;k|&K6|lTe)7#X-+c4#lhYNgp0|?&S}ktezIpHc z2Y>u0-`+ZrZ@u-_e73*0=Mcg&c;7C#oPX#G~CHWH0i{!DLIqgOB@8DGlqEUAu8b~cD)=HB48O-yMhSTe@_8Kep zm+LNe>Rbfoz0y0{{i98p7MRVq5v~GtEGMLFv*p=iQv1eyzB6wmKs)elAus)HC zOo1!`pzC?}TBAt|A?V85XLwDOKK1!8Ax+Y3HYeigq!6Xqv^-x{dsFZHL{%O>dOTXe z`D_N@^Pl@{RW}blc;A(i!m)_1*X!AAvZ|WdqiUi)I#5F^L|jl&vY(JIoiEv!Ne8gZYz@oP=HfX) zAvL%lKY=S^rep@htl1>sdv2v|4KZJiBz_?Dsn36D=Tvrk%h2jjCTvXfi!E^e<|K?Yw zH(rG*G^hj(t+Y*BEtIqP!R3ss0v)qSIaSfn5|OC17tdt9 z0Fg=yvEeg0Pi_J$1J{jNH3gRsCMi_s2^sZU)^DM}oZ#{XwtURL>LJke2=bD9Xv;5G zG2J+R!nTy3D;_4Ipdjb1R>BCf_sRqYSPv3_8m%gnT!Ew=DNyVk7xQCMsI;mY>Z)B| zG>hl;*@x}fhtjM8vg}>}DYSz6_9=n8$n3Y8D~A>o-M%nUXG;+28j_1NriXiuMHy3F zMrI{w5bf#|c5feA{LJv(>6X{n4I<5w%Q?Yq^szHIQ$OTRv+sOM7l zeQzS56u#I=TqS}XwMDI}>67scFPA%>=1-T4(3q9)s}l_!Qkg978K$d6Hb%`R7mtIj15K zo=DJy^JTqg7SAbqo&Zofw+B7imKsf??HYc{uC&S``MwNABB3|EKZt>=k@7>=In_y>vSxEM_&Ti z6bS{KW>8E45F>ikHey7#*g`FBbbD3wTw3a8@Ru5Ije(I?=qZL=gDvd?#Khi2y;V2s zq-wTZ2J620qD)TiAVm$*F4@m9jC9#bxzusRts~98>Qsm*O7lD>R~iOZ)pSNuJm12O zZ4#BRJZ+b!Acix49>pcw*jcIyZBbtcd-w?%DW(^vzA^byn^lJt0MKXN`ihOon%~>o zn@lH2mgmJ{QAK}43^ppuBA;=}W?G?DRd-A$pRrMI2Xln$P~g_4MsFF%4_?{U`Nvgc<9(l()9hV?>1owjSL1(cqEMNq~6Q>={F zaVypCFug68X8-TUKG2{xC!=S3!x#hf=Jk{?PIe|VvT{a%YOEJ#MLKkdJ9c+;&?&;U z)#=d|aWPC%8{8i~9C25VVrX@Acb2Rk`9|Jfaub@&V}+(-E~YPcohNRt5^go<6YeHH0<>XF1yK`{%Hig%T4^3%2(Eif6BN z(fD(>g}N_$Xy2{Z4pRSf=-5VoM&G?4pY68Ud_KQ*>*m9UkD9i5^Nlxt_GiEHoB!ln zr)TG{yz=tj`kVjufBoP7_vh#50A9Lt=kenYyC7A}O6Y<2{u7`0_}g#43t)eLZ$6(j zP4oQu383Ej>2!LrxX9o6=BGdR3qSLj-}>D@_~u{w`uRFMy7$g1aDnhMU;Z)_|KtDk z_W_;H=XUei=_&i7m`$nol20zxiu- ze*HK8_FpXlC}{Re|NejepZ%u?U;N8<|CfK}i@);A-~7VoK6~|yer)?RdFe<~vS zX&$DyLB|vbj0Zs*AFg+OL<+qAwL|f^(g^nd)3sBce}qQv+_}BKzyIp1uR!tZ|L)&< z`t;d1f9Wp*xOwBo&6_t44-Nnv9Uc9vzxpfl`3$OeUw`eJ-}p<%$HxHPdh7FVzVXK4 z;Q@f#w{QLGulyD7J%E4jZ~yvl{GI>k)1Uq%fa!GhD}Uve?fZN0z4!Fl$>GtBs;Vwd zpS7X~dwcu)`)7;l>4y)jKR0jOu>Sz?%O88`H~!=Q@OS^&@Bf$o)qi7u{MbV4KUYkIG60IQFBqp}My>uj?AX zKm0%b@xS?N|9Vw708UTO{{H{+zcoz*1yoN?PV9Xjd~m;Q+mq)f0DkE&e*M4rdw=(< zzwq+_R;$%-{?>07z5wuled~9=^WE<)mn#6?yWjk+-!97%KwGbVeD8y%T7UbyKU|)l zoSrW~eDbtus&D_xKmOhi-nI6tbOE5xf9dCrkB=6Mg^1R5{o1Rq?A?C7aWfGuo3p16 z-h2A&SvW2}f2covul3&V@9%+}Ob&Q`Dk9Fgz1tuE@?ZZufASCh-r~u<=cnU9%^c#4 zrv*b$+COtOC(drjmC%uY + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tools/desktop-kde/overlay/usr/share/wallpapers/DroidDeck/metadata.json b/tools/desktop-kde/overlay/usr/share/wallpapers/DroidDeck/metadata.json new file mode 100644 index 0000000000..c4431e01ca --- /dev/null +++ b/tools/desktop-kde/overlay/usr/share/wallpapers/DroidDeck/metadata.json @@ -0,0 +1,8 @@ +{ + "KPlugin": { + "Authors": [{ "Name": "DroidDeck" }], + "Id": "DroidDeck", + "License": "GPL-3.0-or-later", + "Name": "DroidDeck" + } +} diff --git a/tools/desktop-kde/release.env b/tools/desktop-kde/release.env new file mode 100644 index 0000000000..22a241e4be --- /dev/null +++ b/tools/desktop-kde/release.env @@ -0,0 +1,4 @@ +DESKTOP_KDE_REPO=Droid-Deck/DroidDeck-Components +DESKTOP_KDE_TAG=desktop-kde-r1 +DESKTOP_KDE_SHA256=55747e75d0ec055ab7ea1e92a757f3be227cc3205bb9101f5b53cadef6318579 +DESKTOP_KDE_SIZE=846370256 diff --git a/tools/desktop-kde/runtime-packages.txt b/tools/desktop-kde/runtime-packages.txt new file mode 100644 index 0000000000..928b17f3ac --- /dev/null +++ b/tools/desktop-kde/runtime-packages.txt @@ -0,0 +1,165 @@ +acl 2.4.0-1 +archlinux-keyring 20260727-1 +archlinuxarm-keyring 20240419-2 +attr 2.6.0-1 +audit 4.2.1-1 +base 3-3 +bash 5.3.15-1 +binutils 2.46+r70+g155188ea10a7-1 +brotli 1.2.0-1 +bzip2 1.0.8-6 +ca-certificates 20240618-1 +ca-certificates-mozilla 3.126-1 +ca-certificates-utils 20240618-1 +coreutils 9.11-2 +cryptsetup 2.8.7-1 +curl 8.21.0-1 +dbus 1.16.2-1 +dbus-broker 37-3 +dbus-broker-units 37-3 +dbus-units 37-3 +device-mapper 2.03.41-1 +dhcpcd 10.3.2-1 +diffutils 3.12-2 +e2fsprogs 1.47.4-1 +ex-vi-compat 3-1 +expat 2.8.2-1 +file 5.48-1 +filesystem 2025.10.12-1 +findutils 4.11.0-1 +gawk 5.4.1-1 +gcc-libs 16.1.1+r12+g301eb08fa2c5-1 +gdbm 1.26-2 +gettext 1.0-2 +glib2 2.88.3-1 +glibc 2.43+r22+g8362e8ce10b2-2 +gmp 6.3.0-3 +gnulib-l10n 20241231-1 +gnupg 2.4.9-2 +gnutls 3.8.13-2 +gpgme 2.1.2-1 +gpm 1.20.7.r38.ge82d1a6-6 +grep 3.12-2 +gzip 1.14-2 +hwdata 0.410-1 +iana-etc 20260530-1 +icu 78.3-1 +iproute2 7.1.0-1 +iptables 1:1.8.13-1 +iputils 20250605-1 +jansson 2.15.0-1 +json-c 0.19-1 +kbd 2.10.0-1 +keyutils 1.6.3-4 +kmod 34.2-1 +krb5 1.22.2-1 +leancrypto 1.8.0-1 +libarchive 3.8.9-1 +libasan 16.1.1+r12+g301eb08fa2c5-1 +libassuan 3.0.0-1 +libatomic 16.1.1+r12+g301eb08fa2c5-1 +libbpf 1.7.0-1 +libcap 2.78-1 +libcap-ng 0.9.3-1 +libedit 20260512_3.1-1 +libelf 0.195-1 +libevent 2.1.13-2 +libffi 3.7.1-1 +libgcc 16.1.1+r12+g301eb08fa2c5-1 +libgcrypt 1.12.2-1 +libgfortran 16.1.1+r12+g301eb08fa2c5-1 +libgomp 16.1.1+r12+g301eb08fa2c5-1 +libgpg-error 1.61-1 +libidn2 2.3.8-1 +libksba 1.8.0-1 +libldap 2.6.13-1 +liblsan 16.1.1+r12+g301eb08fa2c5-1 +libmakepkg-dropins 20-1 +libmnl 1.0.5-2 +libnetfilter_conntrack 1.1.1-1 +libnfnetlink 1.0.2-2 +libnftnl 1.3.1-1 +libnghttp2 1.70.0-1 +libnghttp3 1.18.0-1 +libngtcp2 1.25.0-1 +libnl 3.12.0-1 +libnsl 2.0.1-2 +libobjc 16.1.1+r12+g301eb08fa2c5-1 +libp11-kit 0.26.4-1 +libpcap 1.10.6-1 +libpsl 0.21.5-2 +libquadmath 16.1.1+r12+g301eb08fa2c5-1 +libsasl 2.1.28-5 +libseccomp 2.6.0-1 +libsecret 0.21.7-1 +libssh2 1.11.1-7 +libstdc++ 16.1.1+r12+g301eb08fa2c5-1 +libsysprof-capture 50.0-4 +libtasn1 4.21.0-1 +libtirpc 1.3.7-1 +libtsan 16.1.1+r12+g301eb08fa2c5-1 +libubsan 16.1.1+r12+g301eb08fa2c5-1 +libunistring 1.4.2-1 +libusb 1.0.30-1 +libverto 0.3.2-6 +libxcrypt 4.5.2-1 +libxml2 2.15.3-1 +licenses 20240728-1 +linux-aarch64 7.1.6-1 +linux-api-headers 7.1-1 +linux-firmware 20260622-1 +linux-firmware-amdgpu 20260622-1 +linux-firmware-atheros 20260622-1 +linux-firmware-broadcom 20260622-1 +linux-firmware-cirrus 20260622-1 +linux-firmware-intel 20260622-1 +linux-firmware-mediatek 20260622-1 +linux-firmware-nvidia 20260622-1 +linux-firmware-other 20260622-1 +linux-firmware-radeon 20260622-1 +linux-firmware-realtek 20260622-1 +linux-firmware-whence 20260622-1 +lmdb 0.9.35-1 +lz4 1:1.10.0-2 +mkinitcpio 41-4 +mkinitcpio-busybox 1.36.1-1 +mpfr 4.2.2-1 +nano 9.1-1 +ncurses 6.6-2 +net-tools 2.10-3 +netctl 1.29-2 +nettle 4.0-1 +nftables 1:1.1.6-3 +npth 1.8-1 +openssh 10.4p1-3 +openssl 3.6.3-1 +p11-kit 0.26.4-1 +pacman 7.1.0.r9.g54d9411-2 +pacman-mirrorlist 20260411-2 +pam 1.7.2-2 +pambase 20260616-1 +pciutils 3.15.0-1 +pcre2 10.47-1 +pinentry 1.3.3-1 +popt 1.19-2 +procps-ng 4.0.6-3 +psmisc 23.7-2 +readline 8.3.003-1 +sed 4.10-1 +shadow 4.20.0.arch1-1 +sqlite 3.53.4-1 +systemd 261.2-1 +systemd-libs 261.2-1 +systemd-resolvconf 261.2-1 +systemd-sysvcompat 261.2-1 +tar 1.35-2 +tpm2-tss 4.1.3-1 +tzdata 2026c-1 +util-linux 2.42.2-1 +util-linux-libs 2.42.2-1 +vim 9.2.0849-1 +vim-runtime 9.2.0849-1 +which 2.25-1 +xz 5.8.3-1 +zlib 1:1.3.2-3 +zstd 1.5.7-3 diff --git a/tools/desktop-kde/seeds.txt b/tools/desktop-kde/seeds.txt new file mode 100644 index 0000000000..816285de56 --- /dev/null +++ b/tools/desktop-kde/seeds.txt @@ -0,0 +1,31 @@ +# The desktop: KDE Plasma, as SteamOS's desktop mode has it - KWin nested as a client of the app's +# compositor, Plasma's shell on it, and the apps SteamOS puts on its desktop. One package name per +# line; build.sh takes the closure over Arch Linux ARM at build time, minus what the runtime has. +kwin +plasma-workspace +plasma-desktop +plasma-integration +plasma-pa +kscreen +breeze +breeze-gtk +kde-gtk-config +breeze-icons +systemsettings +kde-cli-tools +kinfocenter +dolphin +konsole +kate +ark +gwenview +spectacle +xdg-desktop-portal-kde +xorg-xwayland +qt6-wayland +wayland-utils +noto-fonts +noto-fonts-emoji +xdg-utils +firefox +mpv diff --git a/tools/linuxfs/desktop/autostart b/tools/linuxfs/desktop/autostart deleted file mode 100644 index bcc9b95b97..0000000000 --- a/tools/linuxfs/desktop/autostart +++ /dev/null @@ -1,57 +0,0 @@ -# What the desktop is made of, started once labwc is up. Everything logs into the session log. -# With ~/.droiddeck-desktop-debug present, the panel and the file manager also log what they -# say to the compositor about their windows (shell, layer, output), capped so it stays readable. -run() { - local tag=$1; shift - if [ -f "$HOME/.droiddeck-desktop-debug" ]; then - # Everything it says goes to ~/.cache/droiddeck/.log; the window-related protocol - # traffic also goes into the session log, capped. - mkdir -p "$HOME/.cache/droiddeck" - WAYLAND_DEBUG=1 "$@" 2>&1 | tee "$HOME/.cache/droiddeck/$tag.log" | awk -v t="[$tag]" '!/^\[[0-9:.]+\] +\{/ || (c<120 && /layer_s|xdg_toplevel|fullscreen|wl_output|xdg_output|configure\(/) {print t, $0; c++; fflush()}' & - else - "$@" 2>&1 | sed "s/^/[$tag] /" & - fi -} -# The output at the app's real size, in case rc.xml's mode did not take. -[ -n "${BL_WIDTH:-}" ] && wlr-randr --output WL-1 --custom-mode "${BL_WIDTH}x${BL_HEIGHT}" 2>&1 | sed 's/^/[wlr-randr] /' -# In debug runs, ground truth on the network from inside the session: name lookup, then a -# fetch over IPv4 and over IPv6, so a browser's "no internet" can be placed. -if [ -f "$HOME/.droiddeck-desktop-debug" ]; then - ( - echo "[net] getent: $(getent hosts example.com 2>&1 | head -2 | tr '\n' ' ')" - for f in 4 6; do - echo "[net] curl -$f: $(curl -$f -sS -m 8 -o /dev/null -w '%{http_code} %{remote_ip}' https://example.com 2>&1)" - done - ) & -fi -# In debug runs Firefox (started from the panel or the menu, so it inherits this) writes its -# own network log: name lookups, connects, HTTP transactions, the link/offline decisions. -if [ -f "$HOME/.droiddeck-desktop-debug" ]; then - mkdir -p "$HOME/.cache/droiddeck" - export MOZ_LOG="nsHttp:3,nsSocketTransport:3,nsHostResolver:4,NetworkLinkService:5,NetworkConnectivityService:5,nsIOService:5" - export MOZ_LOG_FILE="$HOME/.cache/droiddeck/firefox-net.log" -fi -swaybg -m fill -i /usr/share/backgrounds/steamdeck.png & -# Desktop icons (pcmanfm-qt manages the desktop surface) and the panel with the app menu. -run pcmanfm-qt pcmanfm-qt --desktop --profile lxqt -run lxqt-panel lxqt-panel -/usr/lib/xdg-desktop-portal-wlr >/dev/null 2>&1 & -# The desktop was asked for with Steam in it (the front end's "Steam Desktop UI", Big Picture's -# "Switch to Desktop"): the client's desktop UI, as the menu's Steam entry opens it. A running -# client just gets the request; on a software desktop it goes back to the app as a Steam session. -[ "${BL_DESKTOP_STEAM:-0}" = 1 ] && run steam /usr/local/bin/droiddeck-steam-launch desktop -# Remote runs, for driving the desktop from a debugger: with ~/.droiddeck-remote-run present (only -# the app or root can create it), each script dropped into ~/.cache/droiddeck/run/ is run once, -# inside the session with its environment, its output tagged into the desktop log. -if [ -f "$HOME/.droiddeck-remote-run" ]; then - mkdir -p "$HOME/.cache/droiddeck/run" - ( - while sleep 1; do - for f in "$HOME"/.cache/droiddeck/run/*.sh; do - [ -f "$f" ] || continue - mv -f "$f" "$f.running" || continue - ( echo "[run] $(basename "$f") started"; bash "$f.running" 2>&1 | sed "s/^/[run] /"; echo "[run] $(basename "$f") exited"; rm -f "$f.running" ) & - done - done - ) & -fi diff --git a/tools/linuxfs/desktop/droiddeck-desktop b/tools/linuxfs/desktop/droiddeck-desktop index fe4d0725d5..c955251843 100644 --- a/tools/linuxfs/desktop/droiddeck-desktop +++ b/tools/linuxfs/desktop/droiddeck-desktop @@ -1,15 +1,24 @@ #!/bin/bash -# The desktop session: labwc nested as a client of the app's compositor, with LXQt's panel and -# file manager started from its autostart, and Xwayland for the X11 programs (the Steam client, -# most emulators). Run by droiddeck-session in place of gamescope for mode "lxqt". +# The desktop session: KDE Plasma, set up as SteamOS's desktop mode - KWin nested as a client of the +# app's compositor, Plasma's shell on it, and Xwayland for the X11 programs (the Steam client, most +# emulators). Run by droiddeck-session in place of gamescope for mode "lxqt" (the mode's name from +# the LXQt desktop this replaced; it is what the app and its preferences use). # Staged by the app over the desktop package's copy on every launch, so a fix here reaches an # installed desktop without re-downloading the package. set -u -export XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=LXQt XDG_SESSION_DESKTOP=lxqt +export XDG_SESSION_TYPE=wayland XDG_CURRENT_DESKTOP=KDE XDG_SESSION_DESKTOP=KDE DESKTOP_SESSION=plasma +export KDE_FULL_SESSION=true KDE_SESSION_VERSION=6 KDE_APPLICATIONS_AS_SCOPE=0 # Qt tries Wayland and falls back to X11 (Xwayland): an AppImage that bundles only Qt's xcb # plugin - melonDS - exits at once on a plain "wayland" ("Could not find the Qt platform plugin"). export QT_QPA_PLATFORM="wayland;xcb" GDK_BACKEND=wayland,x11 SDL_VIDEODRIVER=wayland,x11 -export QT_QPA_PLATFORMTHEME=lxqt +unset QT_QPA_PLATFORMTHEME +# KDE's programs log through Qt, which writes to the journal when stderr is not a terminal; there is +# no journal here, so their messages go to the desktop log with everything else. +export QT_FORCE_STDERR_LOGGING=1 +# SteamOS's /etc/profile.d/kde.sh. +export MOZ_ENABLE_WAYLAND=1 MOZ_USE_XINPUT2=1 +# Konsole's profile starts the login shell, which it finds here. +export SHELL=/bin/bash # Firefox's child processes sandbox themselves with seccomp and namespaces, neither of which # behaves under proot: the content process ends up unable to read the font files its parent can, # reports "no fonts" and dies - the tab crash, with the chrome around it still drawing text. @@ -21,77 +30,34 @@ export MOZ_DISABLE_SOCKET_PROCESS_SANDBOX=1 MOZ_DISABLE_UTILITY_SANDBOX=1 # ("Shared memory PlatformHandle is not safe to map") and dies on signal 11 - every page. Firefox's # own switch skips the seals. (#280; the line went missing in the fontconfig move of #252.) export MOZ_SHM_NO_SEALS=1 -# pixman composites in software and gives programs shm buffers only, so a Vulkan program on it -# fails at its swapchain. vulkan (or gles2) composite on the GPU and give programs DMA-BUFs, but -# stock wlroots allocates through gbm on a real DRM node and the KGSL stand-in is not one ("unable -# to create allocator"): they need the app's patched wlroots (tools/wlroots), whose Vulkan -# allocator exports the buffers instead. BL_WLR_RENDERER picks; see the fallback at the end. -export WLR_RENDERER=${BL_WLR_RENDERER:-vulkan} -WLROOTS_DIR=/usr/local/lib/droiddeck-wlroots -BASE_LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-} -use_renderer() { - export WLR_RENDERER=$1 - if [ "$1" != pixman ] && [ -f "$WLROOTS_DIR/libwlroots-0.20.so" ]; then - export LD_LIBRARY_PATH=$WLROOTS_DIR${BASE_LD_LIBRARY_PATH:+:$BASE_LD_LIBRARY_PATH} - elif [ -n "$BASE_LD_LIBRARY_PATH" ]; then - export LD_LIBRARY_PATH=$BASE_LD_LIBRARY_PATH - else - unset LD_LIBRARY_PATH - fi -} -use_renderer "$WLR_RENDERER" -# A GPU renderer that failed to start here is not tried again on every desktop start (each try costs -# up to 20 s): the failure is kept with what it failed on - the renderer, the patched wlroots and the -# Linux driver - so an app update or a different driver retries, and so does picking a renderer -# again in the app's settings (which deletes the file). -FAILED_FILE=$HOME/.droiddeck-renderer-failed -renderer_key() { - echo "$WLR_RENDERER $(md5sum "$WLROOTS_DIR/libwlroots-0.20.so" 2>/dev/null | cut -d' ' -f1) ${BL_VK_DRIVER:-runtime}" -} -if [ "$WLR_RENDERER" != pixman ] && [ "$(cat "$FAILED_FILE" 2>/dev/null)" = "$(renderer_key)" ]; then - echo "== droiddeck-desktop: $WLR_RENDERER failed to start here before; using pixman (choose a renderer in the settings to retry)" - use_renderer pixman -fi -# The desktop's own log, beside the session's, so labwc and the programs on it can be read apart +# KWin composites in software (QPainter): its GPU path allocates through gbm on a DRM render node, +# and the KGSL stand-in is not one. Its windows get shm buffers only, so GL programs on it draw with +# Mesa's software rasterizer, Plasma's own Qt Quick draws with Qt's software renderer (plasmashell +# crashes on llvmpipe here), and a Vulkan program cannot draw at all: games and emulators from the +# menu run through droiddeck-gpu (their own gamescope on the app's compositor), and Steam goes back +# to the app as a Steam session - SteamOS's "Return to Gaming Mode". The session's own GL choice +# (zink on Turnip, for the Steam client) is put back by droiddeck-gpu. +export KWIN_COMPOSE=Q QT_QUICK_BACKEND=software LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe +export DROIDDECK_GLTHREAD=${mesa_glthread:-} +unset MESA_LOADER_DRIVER_OVERRIDE LIBGL_KOPPER_DRI2 mesa_glthread +# The desktop's own log, beside the session's, so KWin and the programs on it can be read apart # from the session that started them. The session log keeps the milestones either way. if [ -n "${BL_DEBUG_DIR:-}" ]; then mkdir -p "$BL_DEBUG_DIR" 2>/dev/null || true exec >>"$BL_DEBUG_DIR/desktop.log" 2>&1 - echo "== droiddeck-desktop $(date -u '+%Y-%m-%d %H:%M:%S UTC') renderer=$WLR_RENDERER" + echo "== droiddeck-desktop $(date -u '+%Y-%m-%d %H:%M:%S UTC') ${BL_WIDTH:-1280}x${BL_HEIGHT:-720}" fi -export WLR_BACKENDS=wayland -# The cursor goes to the app's compositor as a wl_pointer cursor surface ("hardware" cursor on the -# wayland backend), which the app draws as its pointer. Drawn into the desktop's own frames instead -# (WLR_NO_HARDWARE_CURSORS=1), it cost every frame a full-screen composite: a software cursor -# rules out direct scan-out, so a full-screen game was never handed to the app as it is. -mkdir -p "$HOME/.config/labwc" "$HOME/.config/lxqt" -# Our defaults for labwc and the panel, refreshed every session. What a user changes in the -# panel's own settings lands in panel.conf and is kept; only rc.xml and autostart are ours. -# The nested output takes the app's surface size; left alone, wlroots' Wayland backend opens -# a 1280x720 window and the whole desktop is upscaled from that. -sed "s/@MODE@/${BL_WIDTH:-1280}x${BL_HEIGHT:-720}/" /etc/xdg/labwc/rc.xml > "$HOME/.config/labwc/rc.xml" -cp -f /etc/xdg/labwc/autostart "$HOME/.config/labwc/autostart" -# panel.conf is the user's once the panel has written it, except the one our first package -# shipped: it never named its panel ([General] panels=), so lxqt-panel built an empty default -# one. That file is replaced; one that names its panels is kept. -if ! grep -q '^panels=' "$HOME/.config/lxqt/panel.conf" 2>/dev/null; then - cp -f /etc/xdg/lxqt/panel.conf "$HOME/.config/lxqt/panel.conf" -fi -[ -f "$HOME/.config/lxqt/lxqt.conf" ] || cp /etc/xdg/lxqt/lxqt.conf "$HOME/.config/lxqt/lxqt.conf" -# pcmanfm-qt paints the desktop; left unconfigured it paints it black, over swaybg's wallpaper. -if [ ! -f "$HOME/.config/pcmanfm-qt/lxqt/settings.conf" ]; then - mkdir -p "$HOME/.config/pcmanfm-qt/lxqt" - printf '[Desktop]\nWallpaper=/usr/share/backgrounds/steamdeck.png\nWallpaperMode=zoom\nBgColor=#1b1030\nShowHidden=false\n' \ - > "$HOME/.config/pcmanfm-qt/lxqt/settings.conf" -fi -# The runtime directory outlives a session. lxqt-panel and pcmanfm-qt are single-instance -# programs that leave a lock and a socket there; finding last session's, they decide they are -# already running and exit without a word - a desktop with a wallpaper and nothing on it. -# Only the app's own compositor socket (wayland-0) is kept. +# The runtime directory outlives a session. KDE's daemons are single-instance programs that leave a +# lock and a socket there; finding last session's, they decide they are already running and exit +# without a word. Only the app's own compositor socket (wayland-0) is kept. find "$XDG_RUNTIME_DIR" -mindepth 1 -maxdepth 1 ! -name 'wayland-0*' ! -name 'keymap.xkb' -exec rm -rf {} + 2>/dev/null +chmod 700 "$XDG_RUNTIME_DIR" 2>/dev/null +# KWin will not start Xwayland when its socket directory is not sticky, as a shared /tmp would be, +# and Plasma's session manager cannot listen at all without the ICE directory, which only root may +# create for it. +mkdir -p /tmp/.X11-unix /tmp/.ICE-unix && chmod 1777 /tmp/.X11-unix /tmp/.ICE-unix 2>/dev/null # Fontconfig defaults and Android language fallbacks are set up by droiddeck-session before this -# script or any desktop program starts. The generated defaults live in conf.d, leaving local.conf -# available for the user's own final overrides. +# script or any desktop program starts. # The menu's "Steam" / "Steam Big Picture" entries and one entry per installed game: all of them # hand off to the app (droiddeck-steam-launch), since the client cannot draw on this desktop. export BL_DESKTOP=1 @@ -103,81 +69,62 @@ python3 /usr/local/bin/droiddeck-desktop-games 2>&1 | sed 's/^/[games] /' # given the controller for its next start without waiting for the next desktop. python3 /usr/local/bin/droiddeck-pad-defaults 2>&1 | sed 's/^/[pads] /' ( while sleep 30; do python3 /usr/local/bin/droiddeck-pad-defaults 2>&1 | sed 's/^/[pads] /'; done ) & -# Games and emulators from the menu run through droiddeck-gpu (their own gamescope on the app's -# compositor) while the desktop is on pixman, where they could not draw with the GPU. -# BL_DESKTOP_GPU=on/off overrides; each entry keeps "Run in a desktop window" either way. -gpu_entries() { python3 /usr/local/bin/droiddeck-desktop-gpu 2>&1 | sed 's/^/[gpu] /'; } -gpu_entries - -# ~/.droiddeck-desktop-debug: labwc's own debug log (views and layer surfaces mapping) too. The GPU -# renderers are experimental and always log in full, so a desktop that does not come up says why. -DEBUG=; { [ -f "$HOME/.droiddeck-desktop-debug" ] || [ "$WLR_RENDERER" != pixman ]; } && DEBUG=-d -run_labwc() { - echo "== droiddeck-desktop: labwc ($WLR_RENDERER${LD_LIBRARY_PATH:+, wlroots from $WLROOTS_DIR}) on $WAYLAND_DISPLAY" - # A session bus: the panel's menus, the file manager and the portal all expect one. - if command -v dbus-run-session >/dev/null 2>&1; then - dbus-run-session -- labwc $DEBUG -C "$HOME/.config/labwc" - else - labwc $DEBUG -C "$HOME/.config/labwc" - fi -} -pids_named() { - local p - for p in /proc/[0-9]*; do - [ "$(cat "$p/comm" 2>/dev/null)" = "$1" ] && echo "${p#/proc/}" - done -} -# labwc answers on its own socket (the one beside the app's wayland-0) once its event loop runs; a -# labwc stuck on the GPU never does. -labwc_answers() { - local sock - for sock in "$XDG_RUNTIME_DIR"/wayland-*; do - case $sock in *.lock|*/wayland-0) continue ;; esac - [ -S "$sock" ] || continue - WAYLAND_DISPLAY=${sock##*/} timeout 3 wayland-info >/dev/null 2>&1 && return 0 - done - return 1 -} -if [ "$WLR_RENDERER" = pixman ]; then - run_labwc - exit $? +# Games and emulators from the menu run through droiddeck-gpu; BL_DESKTOP_GPU=on/off overrides, +# and each entry keeps "Run in a desktop window" either way. +python3 /usr/local/bin/droiddeck-desktop-gpu 2>&1 | sed 's/^/[gpu] /' +# SteamOS's desktop icons: Return to Gaming Mode and Steam, put there once - what the user moves +# or deletes stays that way. +desktop_dir=$(xdg-user-dir DESKTOP 2>/dev/null); [ -n "$desktop_dir" ] && [ "$desktop_dir" != "$HOME" ] || desktop_dir=$HOME/Desktop +if [ ! -e "$HOME/.config/droiddeck-desktop-icons" ]; then + mkdir -p "$desktop_dir" "$HOME/.config" + cp -f /usr/share/applications/droiddeck-return-gaming.desktop "$desktop_dir/Return.desktop" 2>/dev/null + cp -f /usr/share/applications/steamdeck-steam.desktop "$desktop_dir/steam.desktop" 2>/dev/null + chmod +x "$desktop_dir"/*.desktop 2>/dev/null + touch "$HOME/.config/droiddeck-desktop-icons" fi -# A GPU renderer this device cannot start either ends the desktop at once or leaves it hanging on -# the loading screen. Either way the desktop comes up on pixman instead, and the log says why. -started=$(date +%s) -run_labwc & -labwc_job=$! -answered= -while kill -0 "$labwc_job" 2>/dev/null; do - if labwc_answers; then answered=1; break; fi - [ $(( $(date +%s) - started )) -ge 20 ] && break - sleep 1 -done -if [ -n "$answered" ]; then - echo "== droiddeck-desktop: labwc on $WLR_RENDERER is up after $(( $(date +%s) - started )) s" - wait "$labwc_job" - exit $? +# A Return icon still there is kept current with the package's entry (what it runs has changed). +[ -f "$desktop_dir/Return.desktop" ] && cp -f /usr/share/applications/droiddeck-return-gaming.desktop "$desktop_dir/Return.desktop" 2>/dev/null +# Plasma's caches of the menu, the icons and the MIME types, which a package install never ran. +update-mime-database /usr/share/mime >/dev/null 2>&1 & +[ -f /usr/share/icons/hicolor/icon-theme.cache ] || gtk-update-icon-cache -q -t /usr/share/icons/hicolor 2>/dev/null & +[ -f /usr/share/glib-2.0/schemas/gschemas.compiled ] || glib-compile-schemas /usr/share/glib-2.0/schemas 2>/dev/null +# In debug runs Firefox (started from the panel or the menu, so it inherits this) writes its +# own network log: name lookups, connects, HTTP transactions, the link/offline decisions. +if [ -f "$HOME/.droiddeck-desktop-debug" ]; then + mkdir -p "$HOME/.cache/droiddeck" + export MOZ_LOG="nsHttp:3,nsSocketTransport:3,nsHostResolver:4,NetworkLinkService:5,NetworkConnectivityService:5,nsIOService:5" + export MOZ_LOG_FILE="$HOME/.cache/droiddeck/firefox-net.log" + export QT_LOGGING_RULES="kwin_*.debug=true" fi -if kill -0 "$labwc_job" 2>/dev/null; then - echo "== droiddeck-desktop: labwc on $WLR_RENDERER did not answer in 20 s; stopping it" - kill -TERM $(pids_named labwc) 2>/dev/null - sleep 2 - kill -KILL $(pids_named labwc) 2>/dev/null - for prog in lxqt-panel pcmanfm-qt swaybg Xwayland; do kill -KILL $(pids_named "$prog") 2>/dev/null; done - wait "$labwc_job" 2>/dev/null - rc=124 -else - wait "$labwc_job" - rc=$? +# Remote runs, for driving the desktop from a debugger: with ~/.droiddeck-remote-run present (only +# the app or root can create it), each script dropped into ~/.cache/droiddeck/run/ is run once, +# inside the session with its environment, its output tagged into the desktop log. +if [ -f "$HOME/.droiddeck-remote-run" ]; then + mkdir -p "$HOME/.cache/droiddeck/run" + ( + while sleep 1; do + for f in "$HOME"/.cache/droiddeck/run/*.sh; do + [ -f "$f" ] || continue + mv -f "$f" "$f.running" || continue + ( echo "[run] $(basename "$f") started"; WAYLAND_DISPLAY=wayland-1 DISPLAY=:1 bash "$f.running" 2>&1 | sed "s/^/[run] /"; echo "[run] $(basename "$f") exited"; rm -f "$f.running" ) & + done + done + ) & fi -if [ "$rc" -ne 0 ] && [ $(( $(date +%s) - started )) -lt 40 ]; then - echo "== droiddeck-desktop: labwc on $WLR_RENDERER failed at start (exit $rc); starting on pixman instead" - renderer_key > "$FAILED_FILE" - use_renderer pixman - DEBUG=; [ -f "$HOME/.droiddeck-desktop-debug" ] && DEBUG=-d - find "$XDG_RUNTIME_DIR" -mindepth 1 -maxdepth 1 ! -name 'wayland-0*' ! -name 'keymap.xkb' -exec rm -rf {} + 2>/dev/null - gpu_entries - run_labwc - rc=$? +# Plasma as its own login starts it: startplasma-wayland sets up the session's environment and runs +# plasma_session, which starts KWin (through kwin_wayland_wrapper, ours first on PATH) as a client of +# the app's compositor with its own Xwayland, then Plasma's daemons, shell and autostart entries on +# it. The session bus is ours: Plasma, the file manager and the portal all expect one. +echo "== droiddeck-desktop: Plasma on $WAYLAND_DISPLAY" +dbus-run-session -- startplasma-wayland +rc=$? +echo "== droiddeck-desktop: Plasma ended ($rc)" +# Plasma logged out - Log Out, or Return to Gaming Mode, which logs out the same way, so open windows +# are asked to close (and to save) first. As on SteamOS, leaving the desktop goes to Gaming Mode: the +# app is asked for a Steam session, and it ends this one to start it. A desktop the app stops never +# gets here; a Plasma that failed does not count as a logout. +if [ "$rc" -eq 0 ]; then + /usr/local/bin/droiddeck-steam-launch bigpicture + sleep 30 fi exit $rc diff --git a/tools/linuxfs/desktop/droiddeck-desktop-gpu b/tools/linuxfs/desktop/droiddeck-desktop-gpu index 5b0a533fa2..af9e112092 100644 --- a/tools/linuxfs/desktop/droiddeck-desktop-gpu +++ b/tools/linuxfs/desktop/droiddeck-desktop-gpu @@ -6,7 +6,7 @@ # a desktop window". Run at every desktop start; copies whose original is gone, or all of them when # the switch is off, are removed. # -# BL_DESKTOP_GPU=auto (default: on while the desktop is composited by pixman) | on | off +# BL_DESKTOP_GPU=on (default: the desktop composites in software) | off import os import re @@ -23,12 +23,7 @@ SKIP = re.compile(r"^(steam|droiddeck-game-|droiddeck-steam|steamdeck-)", re.I) def enabled(): - mode = os.environ.get("BL_DESKTOP_GPU", "auto").strip().lower() - if mode in ("on", "1", "yes", "true"): - return True - if mode in ("off", "0", "no", "false"): - return False - return os.environ.get("WLR_RENDERER", "pixman") == "pixman" + return os.environ.get("BL_DESKTOP_GPU", "on").strip().lower() not in ("off", "0", "no", "false") def main_group(text): diff --git a/tools/linuxfs/desktop/droiddeck-gpu b/tools/linuxfs/desktop/droiddeck-gpu index ff6fab7f0f..7f5b29432b 100644 --- a/tools/linuxfs/desktop/droiddeck-gpu +++ b/tools/linuxfs/desktop/droiddeck-gpu @@ -1,7 +1,7 @@ #!/bin/bash # Runs a program from the desktop on the GPU: in a gamescope of its own that draws straight into -# the app's compositor (wayland-0), not inside labwc. The desktop is composited by pixman, and -# pixman gives its windows no DMA-BUFs - a Vulkan program there fails at its swapchain +# the app's compositor (wayland-0), not inside the desktop's KWin. KWin composites in software, and +# gives its windows no DMA-BUFs - a Vulkan program there fails at its swapchain # (VK_ERROR_SURFACE_LOST, RPCS3's error). Here it gets the same path as a program started from the # app's rail: full screen over the desktop until it exits, then the desktop is back. # droiddeck-gpu [args...] @@ -20,10 +20,15 @@ elif [ "${BL_REFRESH:-0}" -gt 0 ]; then fi [ "${BL_HDR:-0}" = 1 ] && args+=(--hdr-enabled) echo "== droiddeck-gpu $(date -u +%H:%M:%S): $* (gamescope ${width}x${height} on wayland-0)" -# gamescope is a client of the app's compositor, beside labwc; the program inside it reaches it +# gamescope is a client of the app's compositor, beside the desktop's KWin; the program inside it reaches it # through gamescope's own Xwayland, as in a Steam session. export WAYLAND_DISPLAY=wayland-0 export GDK_BACKEND=x11 QT_QPA_PLATFORM=xcb SDL_VIDEODRIVER=x11 +# GL on the GPU again (zink on Turnip, as the session sets it): the desktop itself draws GL in software. +# Qt Quick back on its GPU renderer and KWin's software compositing not passed on, too. +unset LIBGL_ALWAYS_SOFTWARE QT_QUICK_BACKEND KWIN_COMPOSE +export MESA_LOADER_DRIVER_OVERRIDE=zink GALLIUM_DRIVER=zink LIBGL_KOPPER_DRI2=true +[ -n "${DROIDDECK_GLTHREAD:-}" ] && export mesa_glthread=$DROIDDECK_GLTHREAD # AppImages cannot mount under proot; the menu entries already say so, a hand-typed one may not. export APPIMAGE_EXTRACT_AND_RUN=1 gamescope "${args[@]}" -- "$@" diff --git a/tools/linuxfs/desktop/kwin_wayland_wrapper b/tools/linuxfs/desktop/kwin_wayland_wrapper new file mode 100755 index 0000000000..020c3d9c4d --- /dev/null +++ b/tools/linuxfs/desktop/kwin_wayland_wrapper @@ -0,0 +1,11 @@ +#!/bin/bash +# KWin for the desktop. Plasma's session (plasma_session) starts KWin through kwin_wayland_wrapper as +# found on PATH; this one, first on it, has KWin draw into the app's compositor (wayland-0) and serve +# its own clients on wayland-1 - by default it takes wayland-0 and finds the app's lock on it - with +# its nested output at the app's surface size (left alone, a 1024x768 window the desktop is scaled +# from), and without the lock screen, which the app's own suspend stands in for. Anything else (a KWin +# started outside DroidDeck's desktop) gets the real wrapper as it is. +if [ "${BL_DESKTOP:-}" = 1 ]; then + set -- --wayland-display wayland-0 --socket wayland-1 --width "${BL_WIDTH:-1280}" --height "${BL_HEIGHT:-720}" --no-lockscreen "$@" +fi +exec /usr/bin/kwin_wayland_wrapper "$@" diff --git a/tools/linuxfs/desktop/panel.conf b/tools/linuxfs/desktop/panel.conf deleted file mode 100644 index 06c940f749..0000000000 --- a/tools/linuxfs/desktop/panel.conf +++ /dev/null @@ -1,55 +0,0 @@ -[General] -__userfile__=true -iconTheme=Papirus-Dark -panels=panel1 -panels=panel1 - -[panel1] -alignment=-1 -animation-duration=0 -background-color=@Variant(\0\0\0\x43\x1\xff\xff\x1c\x1c\x1c\x1c\x1c\x1c\0\0) -background-image= -font-color=@Variant(\0\0\0\x43\x1\xff\xff\xff\xff\xff\xff\xff\xff\0\0) -hidable=false -iconSize=28 -lineCount=1 -lockPanel=false -opacity=90 -panelSize=40 -plugins=mainmenu, quicklaunch, taskbar, spacer, tray, worldclock, showdesktop -position=Bottom -reserve-space=true -show-delay=0 -width=100 -width-percent=true - -[mainmenu] -type=mainmenu -icon=/usr/share/icons/Papirus-Dark/32x32/apps/steam.svg -showText=false - -[quicklaunch] -type=quicklaunch -apps\1\desktop=/usr/share/applications/steamdeck-steam.desktop -apps\2\desktop=/usr/share/applications/steamdeck-bigpicture.desktop -apps\3\desktop=/usr/share/applications/pcmanfm-qt.desktop -apps\4\desktop=/usr/share/applications/qterminal.desktop -apps\size=4 - -[taskbar] -type=taskbar -buttonStyle=IconAndText -groupingEnabled=false -showGroupOnHover=true - -[spacer] -type=spacer - -[tray] -type=tray - -[worldclock] -type=worldclock - -[showdesktop] -type=showdesktop diff --git a/tools/linuxfs/desktop/rc.xml b/tools/linuxfs/desktop/rc.xml deleted file mode 100644 index cf5ab8a1f8..0000000000 --- a/tools/linuxfs/desktop/rc.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - 0yes - - Clearlooks - 6 - DejaVu Sans11 - - - center - - - - - qterminal - pcmanfm-qt - - - 500 - - - - - - - - diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 8bf8812f7a..6c9a8b12b2 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -4,7 +4,7 @@ # droiddeck-session desktop the file manager # droiddeck-session run a Linux program (AppImage, script or ELF) # droiddeck-session steam [steam-url] the native Steam client -# droiddeck-session lxqt the LXQt/labwc desktop (DroidDeck's desktop package) +# droiddeck-session lxqt the KDE Plasma desktop (DroidDeck's desktop package) # BL_WIDTH / BL_HEIGHT size the output, BL_FPS limits it, BL_LOG collects everything said here. set -u export HOME=${HOME:-/root} @@ -173,7 +173,7 @@ if [ -z "${BL_INSIDE:-}" ]; then wayland-info >/dev/null 2>&1 && break sleep 0.1 done - # The desktop (DroidDeck's hosted package) is its own nested compositor, labwc, drawing straight + # The desktop (DroidDeck's hosted package) is its own nested compositor, KWin, drawing straight # into the app's: no gamescope in between. if [ "$mode" = lxqt ]; then step "starting the desktop" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-launch b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-launch index 121c77d907..a04d7980c0 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-launch +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-launch @@ -4,15 +4,10 @@ # droiddeck-steam-launch desktop [appid|steam-url] Steam's desktop UI, optionally a game # droiddeck-steam-launch bigpicture [appid|steam-url] Big Picture # -# On a desktop that draws with the GPU (labwc on vulkan or gles2 - the default since the GPU -# desktop) the client runs right there: a window on the desktop, through the desktop's Xwayland, -# with the same set-up a Steam session gives it (droiddeck-session steam, minus gamescope), and -# its games open in windows of their own. A client already running there gets the request instead -# of a second one being started. -# -# On a software desktop (pixman: the GPU renderers failed on this device) there is no dma-buf, so a -# game - or the client's own Vulkan window - would die at its first frame. There the request goes -# to the app, which ends the desktop and starts a Steam session under gamescope in its place. +# The desktop (KDE Plasma) composites in software and has no dma-buf, so the client's own Vulkan +# window - and any game it starts - would die at its first frame there. The request goes to the app +# instead, which ends the desktop and starts a Steam session under gamescope in its place, as +# SteamOS's desktop hands Steam back to Gaming Mode. set -u ui=${1:-desktop} target=${2:-} @@ -25,29 +20,7 @@ case $target in *) url=steam://rungameid/$target ;; esac -steam_root=${XDG_DATA_HOME:-${HOME:-/root}/.local/share}/Steam -client=$steam_root/steamrtarm64/steam - -if [ "${BL_DESKTOP:-}" = 1 ] && [ -n "${WLR_RENDERER:-}" ] && [ "$WLR_RENDERER" != pixman ]; then - log=${BL_DEBUG_DIR:-$HOME/.cache/droiddeck}/steam-desktop.log - mkdir -p "$(dirname "$log")" 2>/dev/null - if pgrep -f 'steamrtarm64/steam( |$)' >/dev/null 2>&1 && [ -x "$client" ]; then - # One client at a time: the running one takes the request (a second start hands its command - # line over and exits). No game asked for = switch it to the UI that was clicked. - [ -n "$url" ] || { [ "$ui" = bigpicture ] && url=steam://open/bigpicture || url=steam://open/main; } - echo "droiddeck-steam-launch: the client is running on the desktop; handing it $url" | tee -a "$log" - exec "$client" "$url" - fi - echo "droiddeck-steam-launch: starting the Steam client on the desktop ($ui${url:+, $url}); log: $log" - # BL_INSIDE: the session script's Steam start-up without gamescope in front of it. The desktop UI - # unless Big Picture was asked for; never Deck mode (its Quick Access Menu needs gamescope). - export BL_INSIDE=1 BL_STEAMDECK=0 - if [ "$ui" = desktop ]; then export BL_STEAM_UI=desktop; else unset BL_STEAM_UI; fi - setsid /usr/local/bin/droiddeck-session steam ${url:+"$url"} >>"$log" 2>&1 < /dev/null & - exit 0 -fi - -# The software desktop, or outside the desktop: ask the app for a Steam session. +# Ask the app for a Steam session. dir=${BL_LAUNCH_DIR:-} if [ -z "$dir" ] || [ ! -d "$dir" ]; then echo "droiddeck-steam-launch: not inside a DroidDeck session (BL_LAUNCH_DIR unset)" >&2 diff --git a/tools/linuxfs/preload/dircache.c b/tools/linuxfs/preload/dircache.c index 0759913662..baf0ada12e 100644 --- a/tools/linuxfs/preload/dircache.c +++ b/tools/linuxfs/preload/dircache.c @@ -768,17 +768,17 @@ ONE_PATH(rmdir, REMOVED, IN_DELETE, next_rmdir(path)) ONE_PATH(unlink, REMOVED, IN_DELETE, next_unlink(path)) ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path)) -#define TWO_PATHS(name, gone, event) \ +#define TWO_PATHS(name, gone, event, call) \ int name(const char *from, const char *to) { \ NEXT(name); \ struct side a, b; \ int track_a = gone && watching(from) && split(from, &a), track_b = watching(to) && split(to, &b); \ - if (!track_a && !track_b) return next_##name(from, to); \ + if (!track_a && !track_b) return call(from, to); \ pthread_mutex_lock(&oplock); \ begin(); \ if (track_a) before(&a); \ if (track_b) before(&b); \ - int r = next_##name(from, to); \ + int r = call(from, to); \ int saved = errno; \ struct expect x[2]; \ int nx = 0; \ @@ -795,9 +795,13 @@ ONE_PATH(remove, REMOVED, IN_DELETE, next_remove(path)) return r; \ } -TWO_PATHS(rename, 1, IN_MOVED_TO) -TWO_PATHS(link, 0, IN_CREATE) -TWO_PATHS(symlink, 0, IN_CREATE) +/* A refused hard link to a lock file is taken as a symlink (links.c). */ +int bl_link_lock(const char *oldpath, const char *newpath, int result) __attribute__((visibility("hidden"))); +static int link_or_lock(const char *from, const char *to) { return bl_link_lock(from, to, next_link(from, to)); } + +TWO_PATHS(rename, 1, IN_MOVED_TO, next_rename) +TWO_PATHS(link, 0, IN_CREATE, link_or_lock) +TWO_PATHS(symlink, 0, IN_CREATE, next_symlink) #else diff --git a/tools/linuxfs/preload/links.c b/tools/linuxfs/preload/links.c new file mode 100644 index 0000000000..c1304577f0 --- /dev/null +++ b/tools/linuxfs/preload/links.c @@ -0,0 +1,56 @@ +/* + * Lock files taken by hard link, which Android denies apps. + * + * libICE and libXau lock an authority file the classic way: create "-c", then link() it to + * "-l" - the link either appears or fails because the other holder's is there. Android's + * SELinux policy refuses hard links in an app's data, so the link fails for everyone, the lock is + * never taken, and Plasma's session manager gives up at "Could not lock ICEAuthority file". With + * no session manager there is no Plasma desktop. + * + * Only that convention is answered here: when the real link is refused and the new name ends in + * "-l", a symlink stands in. Creating a symlink fails on an existing name just as a link does, so + * the lock excludes as before, and the holder removes both names when it is done. Every other + * link keeps the real answer: a program that links a file into place and then deletes the + * original would be left with a dangling symlink. + */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +static int refused(int error) { return error == EPERM || error == EACCES; } + +static int lock_name(const char *path) { + size_t n = path ? strlen(path) : 0; + return n > 2 && strcmp(path + n - 2, "-l") == 0; +} + +/* The answer to link(oldpath, newpath), given the real call's: a refused lock link becomes a symlink. */ +__attribute__((visibility("hidden"))) int bl_link_lock(const char *oldpath, const char *newpath, int result) { + /* libc's own symlink: dircache.c's wrapper may be holding its lock around this call. */ + static int (*real_symlink)(const char *, const char *); + if (result == 0 || !refused(errno) || !lock_name(newpath)) return result; + if (real_symlink == NULL) real_symlink = dlsym(RTLD_NEXT, "symlink"); + return real_symlink(oldpath, newpath); +} + +/* 64-bit builds wrap link() in dircache.c, which hands its result to bl_link_lock. */ +#if __SIZEOF_POINTER__ != 8 +int link(const char *oldpath, const char *newpath) { + static int (*real_link)(const char *, const char *); + if (real_link == NULL) real_link = dlsym(RTLD_NEXT, "link"); + return bl_link_lock(oldpath, newpath, real_link(oldpath, newpath)); +} +#endif + +int linkat(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags) { + static int (*real_linkat)(int, const char *, int, const char *, int); + if (real_linkat == NULL) real_linkat = dlsym(RTLD_NEXT, "linkat"); + int result = real_linkat(olddirfd, oldpath, newdirfd, newpath, flags); + /* A relative old name would resolve against the new name's directory as a symlink's target. */ + if (result != 0 && refused(errno) && lock_name(newpath) && oldpath[0] == '/' && flags == 0) + return symlinkat(oldpath, newdirfd, newpath); + return result; +} diff --git a/tools/release/update_catalog.py b/tools/release/update_catalog.py index e7c71f89bd..b16e4eab3d 100755 --- a/tools/release/update_catalog.py +++ b/tools/release/update_catalog.py @@ -288,7 +288,7 @@ def main_commit(release: dict) -> str | None: def stable_app_release(releases: list[dict]) -> dict | None: - """The newest published app release. The repo also publishes gamescope and wlroots + """The newest published app release. The repo also publishes gamescope and desktop-package releases that GitHub may mark latest, so an app release is one shipping DroidDeck-.apk.""" apps = [ r for r in releases diff --git a/tools/wlroots/build-in-arch.sh b/tools/wlroots/build-in-arch.sh deleted file mode 100755 index 8d39c5dfad..0000000000 --- a/tools/wlroots/build-in-arch.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/bash -# Runs INSIDE an Arch Linux ARM container (menci/archlinuxarm:base-devel) on an arm64 runner. -# Builds Arch's own wlroots0.20 package - the wlroots the desktop package's labwc links - with the -# patches in ./patches added, keeps only the library, and packs it as wlroots.tzst -# (usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so) for the apk to stage. The desktop launcher -# puts that directory first on labwc's library path for the GPU renderers only. -set -euxo pipefail -VERSION=${WLROOTS_VERSION:-0.20.2} -WORK=/work -cd "$WORK" -# Same container workarounds as tools/gamescope/build-in-arch.sh. -grep -q '^DisableSandbox' /etc/pacman.conf || sed -i 's/^\[options\]/[options]\nDisableSandbox/' /etc/pacman.conf -{ for m in https://ca.us.mirror.archlinuxarm.org https://fl.us.mirror.archlinuxarm.org https://de3.mirror.archlinuxarm.org https://nl.mirror.archlinuxarm.org; do echo "Server = $m/\$arch/\$repo"; done; cat /etc/pacman.d/mirrorlist; } > /etc/pacman.d/mirrorlist.new -mv /etc/pacman.d/mirrorlist.new /etc/pacman.d/mirrorlist -pacman -Syu --noconfirm --needed git sudo zstd binutils -id builder >/dev/null 2>&1 || useradd -m builder -echo 'builder ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/builder -rm -rf pkg && mkdir pkg && cp -r tools/wlroots/patches pkg/ && chown -R builder pkg -sudo -u builder bash -c " -set -euxo pipefail -cd pkg -for rel in 1 2 3 4; do - if git clone -q --depth 1 --branch ${VERSION}-\$rel https://gitlab.archlinux.org/archlinux/packaging/packages/wlroots0.20.git arch 2>/dev/null; then break; fi -done -test -f arch/PKGBUILD -cd arch -grep -q '^pkgver=${VERSION}$' PKGBUILD -cp ../patches/*.patch . -PATCHES=\$(ls *.patch | sort) -{ echo; echo 'source+=('; for p in \$PATCHES; do echo \" \$p\"; done; echo ')'; for p in \$PATCHES; do echo \"sha256sums+=('SKIP')\"; done; } >> PKGBUILD -# Arch's PKGBUILD has no prepare(); ours applies the patches to the checkout. -cat >> PKGBUILD <<'PREP' - -prepare() { - cd \"\$srcdir/\$pkgname\" - for p in \$(ls \"\$srcdir\"/*.patch | sort); do - echo \"applying \$(basename \"\$p\")\" - patch -p1 --no-backup-if-mismatch < \"\$p\" - done -} -PREP -makepkg -A -s --noconfirm --skipchecksums --skippgpcheck -ls -l *.pkg.tar.* -" -cd "$WORK" -rm -rf out && mkdir -p out/usr/local/lib/droiddeck-wlroots -PKG=$(ls pkg/arch/wlroots0.20-*.pkg.tar.* | grep -v -- '-debug-' | head -1) -mkdir -p x && tar --use-compress-program=unzstd -xf "$PKG" -C x -SO=x/usr/lib/libwlroots-0.20.so -test -f "$SO" -cp -L "$SO" out/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so -strip --strip-unneeded out/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so || true -# The patch is in: the allocator's symbol and its log line. -grep -q 'Created Vulkan DMA-BUF allocator' out/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so -readelf -d out/usr/local/lib/droiddeck-wlroots/libwlroots-0.20.so | grep -E 'NEEDED|SONAME' -(cd out && tar --use-compress-program='zstd -19' -cf ../wlroots.tzst usr) -sha256sum wlroots.tzst | tee wlroots.tzst.sha256 -ls -l wlroots.tzst out/usr/local/lib/droiddeck-wlroots/ diff --git a/tools/wlroots/patches/0001-vulkan-allocator-for-non-drm-gpu-nodes.patch b/tools/wlroots/patches/0001-vulkan-allocator-for-non-drm-gpu-nodes.patch deleted file mode 100644 index 658233f4fa..0000000000 --- a/tools/wlroots/patches/0001-vulkan-allocator-for-non-drm-gpu-nodes.patch +++ /dev/null @@ -1,549 +0,0 @@ -diff --git a/include/render/vulkan.h b/include/render/vulkan.h -index f96619f2..9f945f87 100644 ---- a/include/render/vulkan.h -+++ b/include/render/vulkan.h -@@ -461,6 +461,10 @@ struct wlr_vk_descriptor_pool *vulkan_alloc_blend_ds( - // Frees the given descriptor set from the pool its pool. - void vulkan_free_ds(struct wlr_vk_renderer *renderer, - struct wlr_vk_descriptor_pool *pool, VkDescriptorSet ds); -+// A DMA-BUF allocator on a Vulkan device of its own, for a DRM fd gbm cannot use (see -+// render/vulkan/allocator.c). -+struct wlr_allocator *vulkan_allocator_create(int drm_fd); -+ - struct wlr_vk_format_props *vulkan_format_props_from_drm( - struct wlr_vk_device *dev, uint32_t drm_format); - struct wlr_vk_renderer *vulkan_get_renderer(struct wlr_renderer *r); -diff --git a/render/allocator/allocator.c b/render/allocator/allocator.c -index b7dbf3d6..f3162d47 100644 ---- a/render/allocator/allocator.c -+++ b/render/allocator/allocator.c -@@ -1,6 +1,7 @@ - #include - #include - #include -+#include - #include - #include - #include -@@ -13,6 +14,10 @@ - #include "render/allocator/shm.h" - #include "render/wlr_renderer.h" - -+#if WLR_HAS_VULKAN_RENDERER -+#include "render/vulkan.h" -+#endif -+ - #if WLR_HAS_GBM_ALLOCATOR - #include "render/allocator/gbm.h" - #endif -@@ -32,6 +37,29 @@ void wlr_allocator_init(struct wlr_allocator *alloc, - wl_signal_init(&alloc->events.destroy); - } - -+/* Whether the fd is a DRM device at all. The GPU node on Android (KGSL, for Adreno) stands in for -+ * a render node so that Vulkan and libdrm's device lookup can name the GPU, but it answers no DRM -+ * ioctl: drmIsMaster() takes that for mastership, and gbm cannot allocate on it. */ -+static bool is_drm_device(int fd) { -+ drmVersion *version = drmGetVersion(fd); -+ if (version == NULL) { -+ return false; -+ } -+ drmFreeVersion(version); -+ return true; -+} -+ -+#if WLR_HAS_VULKAN_RENDERER -+static struct wlr_allocator *try_vulkan_allocator(int drm_fd) { -+ wlr_log(WLR_DEBUG, "Trying to create Vulkan allocator"); -+ struct wlr_allocator *alloc = vulkan_allocator_create(drm_fd); -+ if (alloc == NULL) { -+ wlr_log(WLR_DEBUG, "Failed to create Vulkan allocator"); -+ } -+ return alloc; -+} -+#endif -+ - /* Re-open the DRM node to avoid GEM handle ref'counting issues. See: - * https://gitlab.freedesktop.org/mesa/drm/-/merge_requests/110 - */ -@@ -109,8 +137,20 @@ struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend, - struct wlr_allocator *alloc = NULL; - - uint32_t gbm_caps = WLR_BUFFER_CAP_DMABUF; -+ bool drm_device = drm_fd >= 0 && is_drm_device(drm_fd); -+ const char *forced = getenv("WLR_ALLOCATOR"); -+ bool vk_first = forced != NULL && strcmp(forced, "vulkan") == 0; -+#if WLR_HAS_VULKAN_RENDERER -+ // A GPU node that is not a DRM device (and WLR_ALLOCATOR=vulkan): buffers come from Vulkan. -+ if ((backend_caps & gbm_caps) && (renderer_caps & gbm_caps) && drm_fd >= 0 -+ && (vk_first || !drm_device)) { -+ if ((alloc = try_vulkan_allocator(drm_fd)) != NULL) { -+ return alloc; -+ } -+ } -+#endif - if ((backend_caps & gbm_caps) && (renderer_caps & gbm_caps) -- && drm_fd >= 0) { -+ && drm_fd >= 0 && drm_device) { - #if WLR_HAS_GBM_ALLOCATOR - wlr_log(WLR_DEBUG, "Trying to create gbm allocator"); - int gbm_fd = reopen_drm_node(drm_fd, true); -@@ -124,6 +164,11 @@ struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend, - wlr_log(WLR_DEBUG, "Failed to create gbm allocator"); - #else - wlr_log(WLR_DEBUG, "Skipping gbm allocator: disabled at compile-time"); -+#endif -+#if WLR_HAS_VULKAN_RENDERER -+ if (!vk_first && (alloc = try_vulkan_allocator(drm_fd)) != NULL) { -+ return alloc; -+ } - #endif - } - -@@ -138,7 +183,7 @@ struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend, - - uint32_t drm_caps = WLR_BUFFER_CAP_DMABUF | WLR_BUFFER_CAP_DATA_PTR; - if ((backend_caps & drm_caps) && (renderer_caps & drm_caps) -- && drm_fd >= 0 && drmIsMaster(drm_fd)) { -+ && drm_fd >= 0 && drm_device && drmIsMaster(drm_fd)) { - wlr_log(WLR_DEBUG, "Trying to create drm dumb allocator"); - int dumb_fd = reopen_drm_node(drm_fd, false); - if (dumb_fd < 0) { -diff --git a/render/vulkan/allocator.c b/render/vulkan/allocator.c -new file mode 100644 -index 00000000..4c00d361 ---- /dev/null -+++ b/render/vulkan/allocator.c -@@ -0,0 +1,392 @@ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include "render/vulkan.h" -+ -+/* -+ * DMA-BUFs allocated by Vulkan, for a GPU whose device node is not a DRM device and so cannot be -+ * given to gbm - Adreno through KGSL on Android, where the node Turnip opens stands in for a -+ * render node. Each buffer is an exportable image with an explicit DRM format modifier, created -+ * the way vulkan_import_dmabuf() imports one for rendering, so the renderer and the parent -+ * compositor see the same layout. The device is our own, on the physical device the DRM fd names: -+ * this works under the gles2 renderer too, as long as its EGL can import the result. -+ */ -+ -+struct wlr_vk_allocator { -+ struct wlr_allocator base; -+ struct wlr_vk_device *dev; -+ PFN_vkGetMemoryFdKHR get_memory_fd; -+ PFN_vkGetImageDrmFormatModifierPropertiesEXT get_modifier_props; -+ struct wl_list buffers; // wlr_vk_allocator_buffer.link -+}; -+ -+struct wlr_vk_allocator_buffer { -+ struct wlr_buffer base; -+ struct wlr_vk_allocator *alloc; // NULL once the allocator is gone -+ struct wl_list link; -+ VkImage image; -+ VkDeviceMemory memory; -+ struct wlr_dmabuf_attributes dmabuf; -+}; -+ -+static const struct wlr_buffer_impl buffer_impl; -+ -+static struct wlr_vk_allocator_buffer *buffer_from_buffer(struct wlr_buffer *wlr_buffer) { -+ assert(wlr_buffer->impl == &buffer_impl); -+ struct wlr_vk_allocator_buffer *buffer = wl_container_of(wlr_buffer, buffer, base); -+ return buffer; -+} -+ -+static void buffer_release_vk(struct wlr_vk_allocator_buffer *buffer) { -+ if (buffer->alloc == NULL) { -+ return; -+ } -+ VkDevice dev = buffer->alloc->dev->dev; -+ // The exported DMA-BUF keeps the memory alive for whoever imported it. -+ if (buffer->image != VK_NULL_HANDLE) { -+ vkDestroyImage(dev, buffer->image, NULL); -+ } -+ if (buffer->memory != VK_NULL_HANDLE) { -+ vkFreeMemory(dev, buffer->memory, NULL); -+ } -+ buffer->image = VK_NULL_HANDLE; -+ buffer->memory = VK_NULL_HANDLE; -+ wl_list_remove(&buffer->link); -+ buffer->alloc = NULL; -+} -+ -+static void buffer_destroy(struct wlr_buffer *wlr_buffer) { -+ struct wlr_vk_allocator_buffer *buffer = buffer_from_buffer(wlr_buffer); -+ wlr_buffer_finish(wlr_buffer); -+ buffer_release_vk(buffer); -+ wlr_dmabuf_attributes_finish(&buffer->dmabuf); -+ free(buffer); -+} -+ -+static bool buffer_get_dmabuf(struct wlr_buffer *wlr_buffer, -+ struct wlr_dmabuf_attributes *dmabuf) { -+ struct wlr_vk_allocator_buffer *buffer = buffer_from_buffer(wlr_buffer); -+ *dmabuf = buffer->dmabuf; -+ return true; -+} -+ -+static const struct wlr_buffer_impl buffer_impl = { -+ .destroy = buffer_destroy, -+ .get_dmabuf = buffer_get_dmabuf, -+}; -+ -+// One vkCreateImage over the modifiers that share the same mutable-sRGB setting, the one thing -+// in the import that depends on the modifier. The driver picks the modifier. -+static VkImage create_image(struct wlr_vk_allocator *alloc, -+ const struct wlr_vk_format_props *props, int width, int height, -+ const uint64_t *mods, uint32_t mods_len, bool mutable_srgb) { -+ VkImageCreateInfo img_info = { -+ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, -+ .imageType = VK_IMAGE_TYPE_2D, -+ .format = props->format.vk, -+ .mipLevels = 1, -+ .arrayLayers = 1, -+ .samples = VK_SAMPLE_COUNT_1_BIT, -+ .sharingMode = VK_SHARING_MODE_EXCLUSIVE, -+ .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, -+ .extent = (VkExtent3D){ width, height, 1 }, -+ .usage = vulkan_render_usage, -+ .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT, -+ }; -+ if (mutable_srgb) { -+ img_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; -+ } -+ -+ VkExternalMemoryImageCreateInfo eimg = { -+ .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO, -+ .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, -+ }; -+ img_info.pNext = &eimg; -+ -+ VkImageDrmFormatModifierListCreateInfoEXT mod_list = { -+ .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT, -+ .drmFormatModifierCount = mods_len, -+ .pDrmFormatModifiers = mods, -+ }; -+ eimg.pNext = &mod_list; -+ -+ VkFormat view_formats[] = { -+ props->format.vk, -+ props->format.vk_srgb, -+ }; -+ VkImageFormatListCreateInfoKHR list_info = { -+ .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR, -+ .pViewFormats = view_formats, -+ .viewFormatCount = sizeof(view_formats) / sizeof(view_formats[0]), -+ }; -+ if (mutable_srgb) { -+ mod_list.pNext = &list_info; -+ } -+ -+ VkImage image = VK_NULL_HANDLE; -+ VkResult res = vkCreateImage(alloc->dev->dev, &img_info, NULL, &image); -+ if (res != VK_SUCCESS) { -+ wlr_vk_error("vkCreateImage", res); -+ return VK_NULL_HANDLE; -+ } -+ return image; -+} -+ -+static struct wlr_buffer *allocator_create_buffer(struct wlr_allocator *wlr_allocator, -+ int width, int height, const struct wlr_drm_format *format) { -+ struct wlr_vk_allocator *alloc = wl_container_of(wlr_allocator, alloc, base); -+ VkDevice dev = alloc->dev->dev; -+ -+ const struct wlr_vk_format_props *props = -+ vulkan_format_props_from_drm(alloc->dev, format->format); -+ if (props == NULL) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: format 0x%08"PRIX32" not supported", -+ format->format); -+ return NULL; -+ } -+ -+ // The modifiers asked for that this device can render to at this size, split by whether the -+ // import will make the image mutable-format. INVALID (implicit) is not something Vulkan can -+ // allocate; LINEAR stands in for it when it is all there is. -+ uint64_t *mutable_mods = calloc(format->len + 1, sizeof(uint64_t)); -+ uint64_t *plain_mods = calloc(format->len + 1, sizeof(uint64_t)); -+ if (mutable_mods == NULL || plain_mods == NULL) { -+ free(mutable_mods); -+ free(plain_mods); -+ return NULL; -+ } -+ uint32_t mutable_len = 0, plain_len = 0; -+ bool implicit_only = format->len > 0; -+ for (size_t i = 0; i <= format->len; i++) { -+ uint64_t mod; -+ if (i < format->len) { -+ mod = format->modifiers[i]; -+ if (mod == DRM_FORMAT_MOD_INVALID) { -+ continue; -+ } -+ implicit_only = false; -+ } else if (implicit_only) { -+ mod = DRM_FORMAT_MOD_LINEAR; -+ } else { -+ break; -+ } -+ const struct wlr_vk_format_modifier_props *mp = -+ vulkan_format_props_find_modifier(props, mod, true); -+ if (mp == NULL || (uint32_t)width > mp->max_extent.width || -+ (uint32_t)height > mp->max_extent.height) { -+ continue; -+ } -+ if (mp->has_mutable_srgb) { -+ mutable_mods[mutable_len++] = mod; -+ } else { -+ plain_mods[plain_len++] = mod; -+ } -+ } -+ -+ VkImage image = VK_NULL_HANDLE; -+ if (mutable_len > 0) { -+ image = create_image(alloc, props, width, height, mutable_mods, mutable_len, true); -+ } -+ if (image == VK_NULL_HANDLE && plain_len > 0) { -+ image = create_image(alloc, props, width, height, plain_mods, plain_len, false); -+ } -+ free(mutable_mods); -+ free(plain_mods); -+ if (image == VK_NULL_HANDLE) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: no usable modifier for %dx%d format 0x%08"PRIX32, -+ width, height, format->format); -+ return NULL; -+ } -+ -+ struct wlr_vk_allocator_buffer *buffer = calloc(1, sizeof(*buffer)); -+ if (buffer == NULL) { -+ vkDestroyImage(dev, image, NULL); -+ return NULL; -+ } -+ wlr_buffer_init(&buffer->base, &buffer_impl, width, height); -+ buffer->alloc = alloc; -+ buffer->image = image; -+ wl_list_insert(&alloc->buffers, &buffer->link); -+ for (int i = 0; i < WLR_DMABUF_MAX_PLANES; i++) { -+ buffer->dmabuf.fd[i] = -1; -+ } -+ -+ VkMemoryRequirements reqs; -+ vkGetImageMemoryRequirements(dev, image, &reqs); -+ int mem_type = vulkan_find_mem_type(alloc->dev, -+ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, reqs.memoryTypeBits); -+ if (mem_type < 0) { -+ mem_type = vulkan_find_mem_type(alloc->dev, 0, reqs.memoryTypeBits); -+ } -+ if (mem_type < 0) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: no memory type for the image"); -+ goto error; -+ } -+ -+ VkMemoryDedicatedAllocateInfo dedicated = { -+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, -+ .image = image, -+ }; -+ VkExportMemoryAllocateInfo export = { -+ .sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO, -+ .pNext = &dedicated, -+ .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, -+ }; -+ VkMemoryAllocateInfo mem_info = { -+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, -+ .pNext = &export, -+ .allocationSize = reqs.size, -+ .memoryTypeIndex = mem_type, -+ }; -+ VkResult res = vkAllocateMemory(dev, &mem_info, NULL, &buffer->memory); -+ if (res != VK_SUCCESS) { -+ wlr_vk_error("vkAllocateMemory", res); -+ goto error; -+ } -+ res = vkBindImageMemory(dev, image, buffer->memory, 0); -+ if (res != VK_SUCCESS) { -+ wlr_vk_error("vkBindImageMemory", res); -+ goto error; -+ } -+ -+ VkImageDrmFormatModifierPropertiesEXT mod_props = { -+ .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT, -+ }; -+ res = alloc->get_modifier_props(dev, image, &mod_props); -+ if (res != VK_SUCCESS) { -+ wlr_vk_error("vkGetImageDrmFormatModifierPropertiesEXT", res); -+ goto error; -+ } -+ const struct wlr_vk_format_modifier_props *mp = -+ vulkan_format_props_find_modifier(props, mod_props.drmFormatModifier, true); -+ if (mp == NULL) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: driver chose modifier 0x%016"PRIX64 -+ " which it cannot render to", mod_props.drmFormatModifier); -+ goto error; -+ } -+ uint32_t n_planes = mp->props.drmFormatModifierPlaneCount; -+ if (n_planes == 0 || n_planes > WLR_DMABUF_MAX_PLANES) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: %"PRIu32" memory planes", n_planes); -+ goto error; -+ } -+ -+ VkMemoryGetFdInfoKHR fd_info = { -+ .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR, -+ .memory = buffer->memory, -+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, -+ }; -+ int fd = -1; -+ res = alloc->get_memory_fd(dev, &fd_info, &fd); -+ if (res != VK_SUCCESS || fd < 0) { -+ wlr_vk_error("vkGetMemoryFdKHR", res); -+ goto error; -+ } -+ -+ buffer->dmabuf = (struct wlr_dmabuf_attributes){ -+ .width = width, -+ .height = height, -+ .format = format->format, -+ .modifier = mod_props.drmFormatModifier, -+ .n_planes = n_planes, -+ }; -+ for (int i = 0; i < WLR_DMABUF_MAX_PLANES; i++) { -+ buffer->dmabuf.fd[i] = -1; -+ } -+ static const VkImageAspectFlagBits plane_aspects[] = { -+ VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT, -+ VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT, -+ VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT, -+ VK_IMAGE_ASPECT_MEMORY_PLANE_3_BIT_EXT, -+ }; -+ for (uint32_t i = 0; i < n_planes; i++) { -+ VkImageSubresource sub = { .aspectMask = plane_aspects[i] }; -+ VkSubresourceLayout layout; -+ vkGetImageSubresourceLayout(dev, image, &sub, &layout); -+ buffer->dmabuf.offset[i] = layout.offset; -+ buffer->dmabuf.stride[i] = layout.rowPitch; -+ // Every plane lives in the one allocation; each gets its own fd, as -+ // wlr_dmabuf_attributes_finish() closes them one by one. -+ buffer->dmabuf.fd[i] = i == 0 ? fd : fcntl(fd, F_DUPFD_CLOEXEC, 0); -+ if (buffer->dmabuf.fd[i] < 0) { -+ wlr_log_errno(WLR_ERROR, "Vulkan allocator: dup failed"); -+ goto error; -+ } -+ } -+ -+ wlr_log(WLR_DEBUG, "Vulkan allocator: %dx%d format 0x%08"PRIX32" modifier 0x%016"PRIX64 -+ ", %"PRIu32" plane(s)", width, height, format->format, mod_props.drmFormatModifier, n_planes); -+ return &buffer->base; -+ -+error: -+ buffer_destroy(&buffer->base); -+ return NULL; -+} -+ -+static void allocator_destroy(struct wlr_allocator *wlr_allocator) { -+ struct wlr_vk_allocator *alloc = wl_container_of(wlr_allocator, alloc, base); -+ // Buffers may outlive their allocator; their DMA-BUF fds stay valid without the device. -+ struct wlr_vk_allocator_buffer *buffer, *tmp; -+ wl_list_for_each_safe(buffer, tmp, &alloc->buffers, link) { -+ buffer_release_vk(buffer); -+ } -+ struct wlr_vk_instance *ini = alloc->dev->instance; -+ vulkan_device_destroy(alloc->dev); -+ vulkan_instance_destroy(ini); -+ free(alloc); -+} -+ -+static const struct wlr_allocator_interface allocator_impl = { -+ .destroy = allocator_destroy, -+ .create_buffer = allocator_create_buffer, -+}; -+ -+struct wlr_allocator *vulkan_allocator_create(int drm_fd) { -+ struct wlr_vk_instance *ini = vulkan_instance_create(false); -+ if (ini == NULL) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: could not create an instance"); -+ return NULL; -+ } -+ VkPhysicalDevice phdev = vulkan_find_drm_phdev(ini, drm_fd); -+ if (phdev == VK_NULL_HANDLE) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: no Vulkan device for the DRM fd"); -+ vulkan_instance_destroy(ini); -+ return NULL; -+ } -+ struct wlr_vk_device *dev = vulkan_device_create(ini, phdev); -+ if (dev == NULL) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: could not create a device"); -+ vulkan_instance_destroy(ini); -+ return NULL; -+ } -+ -+ struct wlr_vk_allocator *alloc = calloc(1, sizeof(*alloc)); -+ if (alloc == NULL) { -+ vulkan_device_destroy(dev); -+ vulkan_instance_destroy(ini); -+ return NULL; -+ } -+ alloc->dev = dev; -+ wl_list_init(&alloc->buffers); -+ alloc->get_memory_fd = (PFN_vkGetMemoryFdKHR) -+ vkGetDeviceProcAddr(dev->dev, "vkGetMemoryFdKHR"); -+ alloc->get_modifier_props = (PFN_vkGetImageDrmFormatModifierPropertiesEXT) -+ vkGetDeviceProcAddr(dev->dev, "vkGetImageDrmFormatModifierPropertiesEXT"); -+ if (alloc->get_memory_fd == NULL || alloc->get_modifier_props == NULL) { -+ wlr_log(WLR_ERROR, "Vulkan allocator: the device lacks DMA-BUF export"); -+ free(alloc); -+ vulkan_device_destroy(dev); -+ vulkan_instance_destroy(ini); -+ return NULL; -+ } -+ -+ wlr_allocator_init(&alloc->base, &allocator_impl, WLR_BUFFER_CAP_DMABUF); -+ wlr_log(WLR_INFO, "Created Vulkan DMA-BUF allocator"); -+ return &alloc->base; -+} -diff --git a/render/vulkan/meson.build b/render/vulkan/meson.build -index 36a822f9..abeb5337 100644 ---- a/render/vulkan/meson.build -+++ b/render/vulkan/meson.build -@@ -37,6 +37,7 @@ glslang_version_info = run_command(glslang, '--version', check: true).stdout() - glslang_version = glslang_version_info.split('\n')[0].split(':')[-1] - - wlr_files += files( -+ 'allocator.c', - 'pass.c', - 'renderer.c', - 'texture.c', -diff --git a/types/wlr_linux_dmabuf_v1.c b/types/wlr_linux_dmabuf_v1.c -index 3165e880..a7fe060c 100644 ---- a/types/wlr_linux_dmabuf_v1.c -+++ b/types/wlr_linux_dmabuf_v1.c -@@ -901,11 +901,22 @@ static bool set_default_feedback(struct wlr_linux_dmabuf_v1 *linux_dmabuf, - if (device->available_nodes & (1 << DRM_NODE_RENDER)) { - const char *name = device->nodes[DRM_NODE_RENDER]; - main_device_fd = open(name, O_RDWR | O_CLOEXEC); -- drmFreeDevice(&device); - if (main_device_fd < 0) { - wlr_log_errno(WLR_ERROR, "Failed to open DRM device %s", name); -+ drmFreeDevice(&device); - goto error_compiled; - } -+ // A GPU node that stands in for a render node without being a DRM device (KGSL on -+ // Android) cannot import a buffer as a GEM handle; every client's would be refused. -+ drmVersion *version = drmGetVersion(main_device_fd); -+ if (version == NULL) { -+ wlr_log(WLR_INFO, "%s is not a DRM device, skipping DMA-BUF import checks", name); -+ close(main_device_fd); -+ main_device_fd = -1; -+ } else { -+ drmFreeVersion(version); -+ } -+ drmFreeDevice(&device); - } else { - // Likely a split display/render setup. Unfortunately we have no way to - // get back the proper render node used by the renderer under-the-hood. diff --git a/tools/wlroots/release.env b/tools/wlroots/release.env deleted file mode 100644 index f9e6fcaebd..0000000000 --- a/tools/wlroots/release.env +++ /dev/null @@ -1,2 +0,0 @@ -WLROOTS_TAG=wlroots-0.20.2-p1 -WLROOTS_SHA256=b4dcb08b46e69dbfaa004597844e843cdf938591ba5dbc7079b91626cdd4591d From e99d5f95055f4c830fe5077fbac6369ebacd00ba Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 9 Oct 2026 11:08:17 -0400 Subject: [PATCH 06/54] Stores: GOG, Epic Games and Amazon Games libraries, installs and cloud saves (#476) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A Stores section (rail, under Games; Setup › Stores gate) with each store's sign-in, library, storefront shelves and a download queue. Each store has two tabs, Store and Library, and Library filters to All or Installed; libraries open from a saved copy and refresh in the background, with install sizes on every card. Games install to internal storage, or a card chosen at Install time, and are registered in the Steam client as non-Steam games, so they launch through Steam and droiddeck-proton from either the front end or the client. - Downloads: GOG, Epic and Amazon engines in Rust (app/src/main/rust/stores, built by cargo-ndk, nothing compiled in the tree). One adaptive fetch core with a latency brake, stalled-host handoff and every CDN a store offers. Per-stage progress, pause/resume, two-step cancel that deletes, failed downloads kept for retry, a foreground service with Wi-Fi and wake locks and notification controls. - Games tab: store source chips (Steam, GOG, Epic, Amazon, Custom) and store art. - Epic: per-launch exchange code from the app and from Steam's Play button, "Epic sign-in (EOS)", "Launch offline", and a game's web links opened in Android's browser for EOS consent. The EOS overlay crashed games and is left out. - Cloud saves (GOG, Epic): pulled before launch, pushed after the game or the session ends, no upload before a first download, conflicts never overwritten, backups both ways. - Logs: every store line redacted; the engine log goes to daily files. --- .github/workflows/build.yml | 79 + .gitignore | 3 + README.md | 2 +- app/build.gradle | 33 + app/src/main/AndroidManifest.xml | 14 + .../main/java/com/droiddeck/launcher/App.kt | 5 + .../com/droiddeck/launcher/MainActivity.kt | 32 +- .../droiddeck/launcher/frontend/AddedGames.kt | 43 +- .../droiddeck/launcher/frontend/Library.kt | 9 +- .../launcher/frontend/LibraryCache.kt | 5 +- .../launcher/session/SessionFiles.kt | 4 + .../launcher/session/SessionLogShare.kt | 5 +- .../launcher/session/SessionPrefs.kt | 46 + .../launcher/session/SessionService.kt | 15 + .../droiddeck/launcher/stores/CloudPlan.kt | 87 + .../launcher/stores/CloudSavePaths.kt | 84 + .../droiddeck/launcher/stores/CloudSaves.kt | 387 +++ .../launcher/stores/CloudTransport.kt | 191 ++ .../droiddeck/launcher/stores/LoginFlows.kt | 55 + .../droiddeck/launcher/stores/SizeCache.kt | 46 + .../launcher/stores/SteamLiveShortcuts.kt | 187 ++ .../com/droiddeck/launcher/stores/Store.kt | 15 + .../launcher/stores/StoreAccounts.kt | 48 + .../com/droiddeck/launcher/stores/StoreArt.kt | 63 + .../droiddeck/launcher/stores/StoreCatalog.kt | 135 + .../com/droiddeck/launcher/stores/StoreExe.kt | 44 + .../launcher/stores/StoreGameSidecar.kt | 165 + .../launcher/stores/StoreInstallRoot.kt | 158 + .../launcher/stores/StoreInstalls.kt | 110 + .../droiddeck/launcher/stores/StoreLaunch.kt | 160 + .../launcher/stores/StoreLaunchRequests.kt | 108 + .../launcher/stores/StoreLogFiles.kt | 79 + .../launcher/stores/StoreLoginActivity.kt | 308 ++ .../com/droiddeck/launcher/stores/StoreNet.kt | 164 + .../droiddeck/launcher/stores/StoreSizes.kt | 96 + .../droiddeck/launcher/stores/StoresNative.kt | 54 + .../droiddeck/launcher/stores/StoresState.kt | 271 ++ .../stores/amazon/AmazonApiClient.java | 206 ++ .../stores/amazon/AmazonAuthClient.java | 174 ++ .../launcher/stores/amazon/AmazonBackend.kt | 163 + .../stores/amazon/AmazonCredentialStore.kt | 52 + .../stores/amazon/AmazonDownloadManager.java | 288 ++ .../launcher/stores/amazon/AmazonGame.java | 28 + .../stores/amazon/AmazonLaunchHelper.java | 140 + .../launcher/stores/amazon/AmazonLibrary.kt | 128 + .../stores/amazon/AmazonManifest.java | 158 + .../launcher/stores/amazon/AmazonNative.kt | 84 + .../launcher/stores/amazon/AmazonPkce.java | 53 + .../launcher/stores/amazon/AmazonPrefs.kt | 10 + .../stores/download/DownloadModels.kt | 75 + .../launcher/stores/download/DownloadQueue.kt | 379 +++ .../stores/download/StoreDownloadService.kt | 299 ++ .../stores/download/StoreDownloadTier.kt | 33 + .../launcher/stores/epic/EpicApiClient.java | 196 ++ .../launcher/stores/epic/EpicAuthClient.java | 181 ++ .../launcher/stores/epic/EpicBackend.kt | 191 ++ .../stores/epic/EpicCredentialStore.kt | 61 + .../stores/epic/EpicDownloadManager.java | 810 +++++ .../launcher/stores/epic/EpicGame.java | 25 + .../launcher/stores/epic/EpicInstallTags.kt | 44 + .../launcher/stores/epic/EpicLaunchData.kt | 55 + .../launcher/stores/epic/EpicLibrary.kt | 119 + .../launcher/stores/epic/EpicNative.kt | 117 + .../launcher/stores/epic/EpicPrefs.kt | 14 + .../launcher/stores/epic/EpicStoreCatalog.kt | 225 ++ .../droiddeck/launcher/stores/gog/GogAuth.kt | 101 + .../launcher/stores/gog/GogBackend.kt | 121 + .../stores/gog/GogDownloadManager.java | 1079 +++++++ .../launcher/stores/gog/GogGame.java | 27 + .../launcher/stores/gog/GogLibrary.kt | 211 ++ .../launcher/stores/gog/GogNative.kt | 109 + .../droiddeck/launcher/stores/gog/GogPrefs.kt | 15 + .../launcher/stores/gog/GogStoreCatalog.kt | 209 ++ .../com/droiddeck/launcher/ui/AddAppDialog.kt | 5 +- .../droiddeck/launcher/ui/FrontEndContent.kt | 5 + .../droiddeck/launcher/ui/FrontEndGames.kt | 108 +- .../com/droiddeck/launcher/ui/FrontEndRail.kt | 18 +- .../droiddeck/launcher/ui/FrontEndScreen.kt | 26 +- .../droiddeck/launcher/ui/FrontEndSetup.kt | 75 + .../droiddeck/launcher/ui/SettingsWidgets.kt | 28 +- .../com/droiddeck/launcher/ui/StoreChips.kt | 71 + .../com/droiddeck/launcher/ui/StoresDetail.kt | 118 + .../droiddeck/launcher/ui/StoresDownloads.kt | 328 ++ .../launcher/ui/StoresInstallDialog.kt | 109 + .../com/droiddeck/launcher/ui/StoresPage.kt | 686 +++++ .../droiddeck/launcher/ui/StoresWidgets.kt | 20 + app/src/main/res/values-es/strings.xml | 3 - app/src/main/res/values-fr/strings.xml | 3 - app/src/main/res/values-ja/strings.xml | 3 - app/src/main/res/values-ko/strings.xml | 3 - app/src/main/res/values-ru/strings.xml | 3 - app/src/main/res/values-zh-rCN/strings.xml | 3 - app/src/main/res/values-zh-rHK/strings.xml | 3 - app/src/main/res/values-zh-rTW/strings.xml | 3 - app/src/main/res/values/strings.xml | 163 +- app/src/main/rust/stores/Cargo.lock | 1729 +++++++++++ app/src/main/rust/stores/Cargo.toml | 41 + app/src/main/rust/stores/JNI.md | 272 ++ app/src/main/rust/stores/src/amazon.rs | 990 ++++++ app/src/main/rust/stores/src/amazon/jni.rs | 229 ++ app/src/main/rust/stores/src/epic.rs | 46 + app/src/main/rust/stores/src/epic/assemble.rs | 525 ++++ app/src/main/rust/stores/src/epic/chunk.rs | 375 +++ app/src/main/rust/stores/src/epic/driver.rs | 480 +++ app/src/main/rust/stores/src/epic/jni.rs | 487 +++ app/src/main/rust/stores/src/epic/manifest.rs | 987 ++++++ app/src/main/rust/stores/src/epic/plan.rs | 289 ++ app/src/main/rust/stores/src/fetch_core.rs | 2656 +++++++++++++++++ app/src/main/rust/stores/src/fetch_tuning.rs | 211 ++ app/src/main/rust/stores/src/gog.rs | 121 + app/src/main/rust/stores/src/gog/engine.rs | 1800 +++++++++++ app/src/main/rust/stores/src/gog/jni.rs | 321 ++ app/src/main/rust/stores/src/gog/plan.rs | 370 +++ app/src/main/rust/stores/src/lib.rs | 29 + app/src/main/rust/stores/src/md5_small.rs | 197 ++ app/src/main/rust/stores/src/priority.rs | 55 + app/src/main/rust/stores/src/stores_jni.rs | 26 + .../frontend/AddedGamesStoreScanTest.kt | 68 + .../launcher/stores/CloudPlanTest.kt | 66 + .../launcher/stores/CloudSavePathsTest.kt | 52 + .../launcher/stores/CloudSavesTest.kt | 96 + .../launcher/stores/InstalledCardsTest.kt | 28 + .../launcher/stores/MatureContentTest.kt | 31 + .../launcher/stores/StoreCacheTest.kt | 50 + .../launcher/stores/StoreGameSidecarTest.kt | 127 + .../launcher/stores/StoreInstallRootTest.kt | 62 + .../droiddeck/launcher/stores/StoreLogTest.kt | 30 + .../stores/download/DownloadEntryTest.kt | 31 + .../download/DownloadNotificationTextTest.kt | 21 + .../launcher/stores/gog/GogCdnTest.kt | 31 + .../droiddeck/launcher/ui/StoreTabsTest.kt | 18 + docs/development/stores.md | 193 ++ docs/progress-log.md | 377 +++ tools/build_local.sh | 23 +- .../overlay/usr/local/bin/droiddeck-open-url | 61 + .../overlay/usr/local/bin/droiddeck-session | 28 + .../usr/local/bin/droiddeck-store-launch | 317 ++ .../overlay/usr/local/bin/steam-compatibility | 52 +- tools/tests/test_game_environment.py | 4 +- tools/tests/test_store_launch.py | 262 ++ tools/tests/test_store_shortcuts.py | 74 + 141 files changed, 25539 insertions(+), 73 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/CloudPlan.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/SizeCache.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/SteamLiveShortcuts.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/Store.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreArt.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreInstallRoot.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreLogFiles.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoresNative.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonApiClient.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonAuthClient.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonCredentialStore.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonDownloadManager.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonGame.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLaunchHelper.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonManifest.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonNative.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPkce.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPrefs.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/download/DownloadModels.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadTier.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicApiClient.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicAuthClient.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicCredentialStore.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicDownloadManager.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicGame.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicInstallTags.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLaunchData.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLibrary.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicNative.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicPrefs.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/epic/EpicStoreCatalog.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogGame.java create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogPrefs.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogStoreCatalog.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoresInstallDialog.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoresWidgets.kt create mode 100644 app/src/main/rust/stores/Cargo.lock create mode 100644 app/src/main/rust/stores/Cargo.toml create mode 100644 app/src/main/rust/stores/JNI.md create mode 100644 app/src/main/rust/stores/src/amazon.rs create mode 100644 app/src/main/rust/stores/src/amazon/jni.rs create mode 100644 app/src/main/rust/stores/src/epic.rs create mode 100644 app/src/main/rust/stores/src/epic/assemble.rs create mode 100644 app/src/main/rust/stores/src/epic/chunk.rs create mode 100644 app/src/main/rust/stores/src/epic/driver.rs create mode 100644 app/src/main/rust/stores/src/epic/jni.rs create mode 100644 app/src/main/rust/stores/src/epic/manifest.rs create mode 100644 app/src/main/rust/stores/src/epic/plan.rs create mode 100644 app/src/main/rust/stores/src/fetch_core.rs create mode 100644 app/src/main/rust/stores/src/fetch_tuning.rs create mode 100644 app/src/main/rust/stores/src/gog.rs create mode 100644 app/src/main/rust/stores/src/gog/engine.rs create mode 100644 app/src/main/rust/stores/src/gog/jni.rs create mode 100644 app/src/main/rust/stores/src/gog/plan.rs create mode 100644 app/src/main/rust/stores/src/lib.rs create mode 100644 app/src/main/rust/stores/src/md5_small.rs create mode 100644 app/src/main/rust/stores/src/priority.rs create mode 100644 app/src/main/rust/stores/src/stores_jni.rs create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesStoreScanTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/InstalledCardsTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/MatureContentTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/StoreCacheTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/StoreInstallRootTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/StoreLogTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/download/DownloadEntryTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt create mode 100644 docs/development/stores.md create mode 100755 tools/linuxfs/overlay/usr/local/bin/droiddeck-open-url create mode 100755 tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch create mode 100644 tools/tests/test_store_launch.py create mode 100644 tools/tests/test_store_shortcuts.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ac5144c95b..80e3497d38 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -395,6 +395,39 @@ jobs: test -f "$D/usr/local/lib/droiddeck-msitools/$f" || { echo "::error::droiddeck-msitools/$f missing"; exit 1; } done + # The store download engines (app/src/main/rust/stores) are Rust, built by Gradle's + # buildRustStores task through cargo-ndk. rustup and cargo are on the runner image; the Android + # target and cargo-ndk are added here. Cache = cargo registry + cargo-ndk + the crate's target + # tree, keyed on the rustc version and Cargo.lock, so a source-only change rebuilds this crate + # on top of the cached dependency objects, and a lockfile change still starts from the + # previous objects (restore-keys). + - name: Rust toolchain for the store engines (aarch64-linux-android) + id: rust + run: | + set -euo pipefail + rustup target add aarch64-linux-android + echo "rustc=$(rustc -V | awk '{print $2}')" >> "$GITHUB_OUTPUT" + rustc -V; cargo -V + + - name: Cache cargo registry and the store engines' target + uses: actions/cache@v4 + with: + path: | + ~/.cargo/registry/index + ~/.cargo/registry/cache + ~/.cargo/git/db + ~/.cargo/bin/cargo-ndk + app/src/main/rust/stores/target + key: stores-${{ steps.rust.outputs.rustc }}-${{ hashFiles('app/src/main/rust/stores/Cargo.lock') }} + restore-keys: | + stores-${{ steps.rust.outputs.rustc }}- + + - name: cargo-ndk + run: | + set -euo pipefail + command -v cargo-ndk >/dev/null || cargo install cargo-ndk --locked + cargo ndk --version + - name: Cache the CMake build tree uses: actions/cache@v4 with: @@ -414,6 +447,52 @@ jobs: ./gradlew assembleRelease --console=plain -PndkVersion="$NDK_VERSION" 2>&1 | tee /tmp/build.log python3 tools/release/check_session_assets.py app/build/outputs/apk/release/app-release.apk + - name: Check the store engines' library + # The exports Kotlin binds must be in the apk's libdroiddeckstores.so, or the stores fail on + # the device with an UnsatisfiedLinkError instead of here. The lockfile check is the + # reproducibility guard: cargo must not have had to re-resolve anything. + run: | + set -euo pipefail + NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}" + NM="$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-nm" + SO=app/build/rustJniLibs/arm64-v8a/libdroiddeckstores.so + test -s "$SO" + ls -la "$SO" + unzip -l app/build/outputs/apk/release/app-release.apk | grep -q 'lib/arm64-v8a/libdroiddeckstores.so' \ + || { echo "::error::libdroiddeckstores.so is not in the apk"; exit 1; } + for sym in \ + 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_amazon_AmazonNative_nativeStart; do + "$NM" -D --defined-only "$SO" | grep -q " ${sym}\$" \ + || { echo "::error::libdroiddeckstores.so does not export $sym"; exit 1; } + done + git diff --stat -- app/src/main/rust/stores/Cargo.lock + git diff --exit-code -- app/src/main/rust/stores/Cargo.lock \ + || { echo "::error::Cargo.lock changed during the build; commit the regenerated lockfile (uploaded as stores-cargo-lock)"; exit 1; } + + - name: Store engines host unit tests + # The crate's #[test] functions are pure host-side logic (manifest parsing, chunk plans, + # assembly from a cache, hashes), so a plain `cargo test` on the runner's own architecture + # runs them; the Android build above compiles none of them. Informational on this first + # run - the point is to learn whether they pass - and bounded so a hang can never hold + # the apk. Make it blocking once it is known green. + timeout-minutes: 10 + continue-on-error: true + run: | + cd app/src/main/rust/stores + cargo test --locked 2>&1 | tail -60 + + - name: Upload the regenerated Cargo.lock + if: failure() + uses: actions/upload-artifact@v4 + with: + name: stores-cargo-lock + path: app/src/main/rust/stores/Cargo.lock + if-no-files-found: ignore + - name: Steam import and game environment tests run: python3 -m unittest discover -s tools/tests -p 'test_*.py' diff --git a/.gitignore b/.gitignore index 71f997c628..f1c4fc665c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,9 @@ app/src/main/jniLibs/arm64-v8a/libproot.so app/src/main/jniLibs/arm64-v8a/.proot-inputs app/src/main/jniLibs/arm64-v8a/libproot-loader.so app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so +# The store download engines (app/src/main/rust/stores) are cross-compiled by Gradle into +# app/build/rustJniLibs; cargo's own build tree stays next to the crate. Neither is source. +app/src/main/rust/stores/target/ app/src/main/assets/droiddeck-esync/ # Kotlin compiler session files (build-tool state, never source). diff --git a/README.md b/README.md index e9b495d1a0..1473653c81 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ DroidDeck brings the SteamOS experience to Android: Valve's Steam client in Big ## Requirements and install -Use Android 9 or newer on a supported Adreno device (730 or newer, or 8xx). Adreno 6xx is experimental: DirectX 11 uses DXVK 2 and may run, and DirectX 12 games can still crash. Mali, Xclipse, PowerVR, and Adreno 710 are unsupported. No root is required. Allow about 3 GB for the runtime and 1.1 GB more for the desktop and emulators. Install the APK from [Releases](https://github.com/Droid-Deck/DroidDeck/releases), install the Linux runtime, then press **Play** and sign in. Steam downloads on first launch. Install **Desktop & apps** to use the desktop and emulators. The **Store** installs Linux apps and games from Flathub (ARM64 builds) with Flatpak; with additional options to install Appimages and set up scripts. +Use Android 9 or newer on a supported Adreno device (730 or newer, or 8xx). Adreno 6xx is experimental: DirectX 11 uses DXVK 2 and may run, and DirectX 12 games can still crash. Mali, Xclipse, PowerVR, and Adreno 710 are unsupported. No root is required. Allow about 3 GB for the runtime and 1.1 GB more for the desktop and emulators. Install the APK from [Releases](https://github.com/Droid-Deck/DroidDeck/releases), install the Linux runtime, then press **Play** and sign in. Steam downloads on first launch. Install **Desktop & apps** to use the desktop and emulators. The **Store** installs Linux apps and games from Flathub (ARM64 builds) with Flatpak; with additional options to install Appimages and set up scripts. **Stores** (Setup › Stores › "Show Stores in the rail") signs in to GOG, Epic Games and Amazon Games, browses their catalogs and libraries, downloads games into the Games storage and adds each one to the Steam client as a non-Steam game, so it launches through Proton like everything else; the cog on its chip row sets which tab a store opens on and the download speed tier. Before Steam launches, you must turn off **Restrict child processes** in Developer options. If this option is not available in developer settings (Android 12 and 13 devices), first launch of Steam will present a "Fix it for me" button, which will help automate the setup process. diff --git a/app/build.gradle b/app/build.gradle index bb03f680a9..4a1e808c9d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -198,6 +198,39 @@ def stageSessionScripts = tasks.register('stageSessionScripts', Copy) { } tasks.named('preBuild').configure { dependsOn(stageSessionScripts) } +// The GOG / Epic / Amazon download engines are Rust (app/src/main/rust/stores, ported from +// Bannerlator's bl-steam-client crate). cargo-ndk cross-compiles them for arm64-v8a into the build +// directory, which is one more jniLibs source below: nothing compiled is committed, the same rule +// the preloads, proot and the DirectAudio relay follow. cargo-ndk exports the NDK clang/llvm-ar as +// the C compiler for the one C-compiling crate (ring, under rustls) - the step Bannerlator's CI +// does by hand - and reads the NDK from ANDROID_NDK_HOME, which is set to the NDK this build pins. +// -PskipRust=true builds without the library, for a checkout with no Rust toolchain: the stores +// then report themselves unavailable instead of the build failing. +def rustCrateDir = layout.projectDirectory.dir('src/main/rust/stores') +def rustJniLibsDir = layout.buildDirectory.dir('rustJniLibs') +def skipRust = (project.findProperty('skipRust') ?: 'false').toString().toBoolean() +def buildRustStores = tasks.register('buildRustStores', Exec) { + description = 'Cross-compiles the store download engines (libdroiddeckstores.so) with cargo-ndk.' + onlyIf { !skipRust } + inputs.dir(rustCrateDir.dir('src')) + inputs.files(rustCrateDir.file('Cargo.toml'), rustCrateDir.file('Cargo.lock')) + outputs.dir(rustJniLibsDir) + workingDir rustCrateDir + // The configuration cache serialises the task, so everything it needs is resolved here, not + // in doFirst. ~/.cargo/bin is where rustup puts cargo and cargo-ndk; an IDE-launched Gradle + // does not always have it on PATH. + def cargoBin = "${System.getProperty('user.home')}/.cargo/bin" + environment 'PATH', "${cargoBin}${File.pathSeparator}${System.getenv('PATH')}" + environment 'ANDROID_NDK_HOME', android.ndkDirectory.absolutePath + environment 'CARGO_TERM_COLOR', 'never' + // --platform 26 = minSdkVersion: the engines link against the API 26 libc, like the C++ code. + commandLine 'cargo', 'ndk', '--target', 'arm64-v8a', '--platform', '26', + '--output-dir', rustJniLibsDir.get().asFile.absolutePath, + 'build', '--release' +} +android.sourceSets.main.jniLibs.srcDir(rustJniLibsDir) +tasks.named('preBuild').configure { dependsOn(buildRustStores) } + dependencies { // The runtime tarball is a zstd-compressed tar. implementation 'com.github.luben:zstd-jni:1.5.2-3@aar' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7fa9d195aa..e0f1038414 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,8 @@ + + @@ -127,6 +129,13 @@ android:screenOrientation="sensorLandscape" android:theme="@style/AppTheme" /> + + + + + "), until the front end has gone there. */ + private var navRequest by mutableStateOf(null) + private var storesShowMature by mutableStateOf(false) private var mic by mutableStateOf(false) private var wifiDiscovery by mutableStateOf(false) private var wifiDiscoveryPermission by mutableStateOf(false) @@ -436,6 +442,7 @@ class MainActivity : ComponentActivity() { } private fun readGameIntent(request: Intent) { + request.getStringExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV)?.let { navRequest = it; request.removeExtra(com.droiddeck.launcher.stores.download.StoreDownloadService.EXTRA_NAV) } shortcutPicker = request.action == Intent.ACTION_CREATE_SHORTCUT if (shortcutPicker) { // The shortcut picker must only offer games from the scan started for this request. @@ -522,6 +529,10 @@ class MainActivity : ComponentActivity() { launcherFullscreen = SessionPrefs.launcherFullscreen(this) animationsEnabled = SessionPrefs.animationsEnabled(this) storeEnabled = SessionPrefs.storeEnabled(this) + gameStoresEnabled = SessionPrefs.gameStoresEnabled(this) + gameStoresSpeedTier = SessionPrefs.gameStoresSpeedTier(this) + storesOpenTab = SessionPrefs.storesOpenTab(this) + storesShowMature = SessionPrefs.storesShowMature(this) applyLauncherFullscreen() updates.start() setContent { @@ -575,6 +586,12 @@ class MainActivity : ComponentActivity() { launcherFullscreen = launcherFullscreen, animationsEnabled = animationsEnabled, storeEnabled = storeEnabled, + gameStoresEnabled = gameStoresEnabled, + gameStoresSpeedTier = gameStoresSpeedTier, + storesOpenTab = storesOpenTab, + navigate = navRequest, + storesShowMature = storesShowMature, + storeDownloadsActive = com.droiddeck.launcher.stores.StoresState.activeDownloads, ), FrontEndActions( onPlay = { startSteamSession() }, @@ -585,7 +602,8 @@ class MainActivity : ComponentActivity() { .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_DESKTOP) .putExtra(SessionService.EXTRA_STEAM_UI, "desktop"), steamSession = true) }, - onSteamGame = { g -> if (shortcutPicker) chooseGameShortcut(g) else launchGame(g) }, + // A store game may need something done first (an Epic game its sign-in code). + onSteamGame = { g -> if (shortcutPicker) chooseGameShortcut(g) else com.droiddeck.launcher.stores.StoreLaunch.prepare(this, g) { launchGame(g) } }, onGameShortcut = { g -> com.droiddeck.launcher.frontend.GameShortcuts.pin(this, g) }, onExportGameFile = { g -> pickGameExport(g) }, onSyncGameFiles = { pickGameExport(null) }, @@ -701,6 +719,12 @@ class MainActivity : ComponentActivity() { applyLauncherFullscreen() }, onStoreEnabled = { on -> SessionPrefs.setStoreEnabled(this, on); storeEnabled = on }, + onGameStoresEnabled = { on -> SessionPrefs.setGameStoresEnabled(this, on); gameStoresEnabled = on; if (on) com.droiddeck.launcher.stores.StoresState.refresh(this) }, + onGameStoresSpeedTier = { tier -> SessionPrefs.setGameStoresSpeedTier(this, tier); gameStoresSpeedTier = tier }, + onStoresOpenTab = { tab -> SessionPrefs.setStoresOpenTab(this, tab); storesOpenTab = tab }, + onNavigated = { navRequest = null }, + onStoresShowMature = { show -> SessionPrefs.setStoresShowMature(this, show); storesShowMature = show }, + onLibraryChanged = { refreshAddedGames(); refresh() }, onAnimationsEnabled = { on -> SessionPrefs.setAnimationsEnabled(this, on) animationsEnabled = on @@ -902,6 +926,10 @@ class MainActivity : ComponentActivity() { override fun onResume() { super.onResume() refreshWifiDiscovery() + // Back from a store's sign-in page: the Stores chips show the account at once. A store + // install finishing while this screen is up rebuilds the Games list through the listener. + if (gameStoresEnabled) com.droiddeck.launcher.stores.StoresState.refresh(this) + com.droiddeck.launcher.stores.StoresState.libraryListener = { refreshAddedGames(); refresh() } com.droiddeck.launcher.ui.Motion.refresh(this) // Back from a session stopped behind a flood: open on its blue, before the first frame. com.droiddeck.launcher.ui.QuitFlood.take()?.let { c -> @@ -959,6 +987,8 @@ class MainActivity : ComponentActivity() { unregisterReceiver(wifiLocationReceiver) unregisterReceiver(mediaReceiver) ui.removeCallbacks(mediaRefresh) + // An install finishing while this screen is away is picked up by onResume's refresh. + com.droiddeck.launcher.stores.StoresState.libraryListener = null displayManager.unregisterDisplayListener(secondScreenDisplayListener) // The session covers the page by now; coming back finds it as it was. flood = null diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt index 4b9e5875ee..bc4386a477 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt @@ -6,6 +6,8 @@ import com.droiddeck.launcher.runtime.LinuxRuntime import org.json.JSONObject import com.droiddeck.launcher.session.GameStorage import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot import java.io.File import java.util.zip.CRC32 @@ -16,13 +18,21 @@ object AddedGames { private const val LEGACY_LIBRARY = "/mnt/bannerlator-sd" class Game( - val folder: File, val name: String, val exe: File, val guestExe: String, val guestDir: String, + val folder: File, val name: String, + /** The game's own .exe: what the icon is read from and what the settings page lists. */ + val exe: File, + /** The guest path the shortcut runs: [exe], or a store install's launcher .bat in front of it. */ + val guestExe: String, val guestDir: String, /** The client's 32-bit appid for this shortcut, as an unsigned value. */ val appId: Long, /** What steam://rungameid/ takes for a shortcut. */ val gameId: Long, val candidates: List, val steamAppId: Int? = null, + /** Where the game came from: a store's id, or [Library.ADDED] for a folder the user added. */ + val source: String = Library.ADDED, + /** The store's own id for the game, from its sidecar; null for a plain added folder. */ + val storeId: String? = null, ) { fun folderName(): String = folder.name } @@ -66,9 +76,16 @@ object AddedGames { SessionPrefs.romsDir(context).takeIf { it.isNotEmpty() }?.let { roms -> if (path.startsWith("$roms/")) return "/root/ROMs/" + path.removePrefix("$roms/") } + // A store root on a card has a bind of its own, before the library rule: the same folder + // keeps the same guest path (and appid) whether or not that card is also the Steam library. + StoreInstallRoot.externalGuestPath(context, host)?.let { return it } GameStorage.effective(context)?.let { lib -> if (path.startsWith("${lib.path}/")) return "$LIBRARY/" + path.removePrefix("${lib.path}/") } + // Inside the runtime's own tree (store installs on internal storage live at + // root/Games/Stores): the tree is the session's / and needs no bind. + val runtime = LinuxRuntime.rootDir(context).absolutePath + if (path.startsWith("$runtime/")) return "/" + path.removePrefix("$runtime/") return null } @@ -99,9 +116,14 @@ object AddedGames { val steamInstalls = steamInstallDirs(dir) for (folder in dir.listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() } ?: emptyList()) { if (folder.name.lowercase() in steamInstalls || folder.name.equals("steamapps", ignoreCase = true)) continue + // The store install root sits inside the Games storage; its games are scanned below, per store. + if (folder.name == "Games" && StoreInstallRoot.roots(context).any { it.absolutePath == folder.absolutePath }) continue scanGame(context, folder, out) } } + // Games the Stores section installed: one folder each under //, with a sidecar. + // An unfinished one (sidecar still `installing`, or none at all) is neither a game nor Custom. + for (folder in StoreInstallRoot.gameFolders(context)) if (StoreInstallRoot.isFinished(folder)) scanGame(context, folder, out) return out.distinctBy { it.folder.canonicalPath } } @@ -125,13 +147,19 @@ object AddedGames { private fun scanGame(context: Context, folder: File, out: MutableList) { run { + // A store install carries a sidecar; every one of them is a shortcut. + val sidecar = StoreGameSidecar.read(folder) val candidates = candidates(folder) val picked = SessionPrefs.addedGameExe(context, folder.path) val chosen = picked.takeIf { it.isNotEmpty() }?.let { File(it) }?.takeIf { it.isFile } - val found = chosen ?: candidates.firstOrNull() ?: return - val foundGuest = guestPath(context, found) - if (foundGuest == null) { Log.w(TAG, "${folder.name}: the session cannot see ${found.path}"); return } - val name = folder.name + // A store install says which exe it is; the folder's guess only when that file is gone. + val found = chosen ?: sidecar?.exeFile(folder)?.takeIf { it.isFile } ?: candidates.firstOrNull() ?: return + // The store's launcher .bat stands in front of the exe when the game needs arguments or + // environment; it is what the shortcut runs. Dropped when the user picked another exe. + val launch = if (chosen == null) sidecar?.launcherFile(folder)?.takeIf { it.isFile } ?: found else found + val foundGuest = guestPath(context, launch) + if (foundGuest == null) { Log.w(TAG, "${folder.name}: the session cannot see ${launch.path}"); return } + val name = sidecar?.title ?: folder.name // Keyed by the pre-rename path so shortcut ids, and the prefixes and saves under them, stay put. val crc = CRC32().apply { update(("\"${foundGuest.replaceFirst(Regex("^$LIBRARY/"), "$LEGACY_LIBRARY/")}\"" + name).toByteArray()) }.value val appId = SessionPrefs.addedGameAppId(context, folder.path, crc or 0x80000000L) @@ -143,10 +171,11 @@ object AddedGames { ?.takeIf { SessionPrefs.adoptAddedGameExe(context, folder.path, picked, it.path, rev) } val exe = adopted ?: found val guestExe = if (adopted != null) guestPath(context, adopted) ?: return else foundGuest - val guestDir = guestPath(context, exe.parentFile ?: folder) ?: return + val guestDir = guestPath(context, (if (adopted != null) adopted else launch).parentFile ?: folder) ?: return val steamId = config?.let { steamRoute(it, appId) } out.add(Game(folder, name, exe, guestExe, guestDir, appId, - steamId?.toLong() ?: ((appId shl 32) or 0x02000000L), candidates, steamId)) + steamId?.toLong() ?: ((appId shl 32) or 0x02000000L), candidates, steamId, + source = sidecar?.store?.id ?: Library.ADDED, storeId = sidecar?.id)) } } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index 82f7065c7f..56e115914b 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -24,12 +24,18 @@ object Library { val hero: File? = null, val lastPlayed: Long = 0L, val gameFiles: File? = null, val protonPrefix: File? = null, val icon: File? = null, + /** Where the game came from: [SOURCE_STEAM], a store's id (gog, epic, amazon) or [ADDED] for a folder the user added. */ + val source: String = SOURCE_STEAM, + /** The store's own id for a store install; null otherwise. */ + val storeId: String? = null, ) { /** Decimal form used by Steam links and Android shortcuts, including unsigned shortcut ids. */ val gameIdString: String get() = java.lang.Long.toUnsignedString(gameId) } - /** The [SteamGame.library] of a game added to the library rather than installed by Steam. */ + /** The [SteamGame.library] of a game added to the library rather than installed by Steam, and the [SteamGame.source] of a plain added folder. */ const val ADDED = "added" + /** The [SteamGame.source] of a title Steam itself installed. */ + const val SOURCE_STEAM = "steam" class Rom(val name: String, val hostPath: File, val guestPath: String, val emulatorId: String, val art: File? = null) class Emulator(val id: String, val name: String, val system: String, val program: String, val installed: Boolean, val games: List) { @@ -155,6 +161,7 @@ object Library { hero = art.hero ?: art.header, gameFiles = g.folder, protonPrefix = protonPrefix(context, g.steamAppId?.toLong() ?: g.appId, libraries = roots), icon = art.icon?.takeIf { it.extension.lowercase() != "ico" }, + source = g.source, storeId = g.storeId, ) } }).distinctBy { it.gameId } diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt index 2add24685c..cd946724ad 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/LibraryCache.kt @@ -26,6 +26,8 @@ object LibraryCache { library = o.getString("library"), gameId = o.getLong("gameId"), hero = o.file("hero"), lastPlayed = o.optLong("lastPlayed"), gameFiles = o.file("gameFiles"), protonPrefix = o.file("protonPrefix"), icon = o.file("icon"), + source = o.optString("source").ifEmpty { if (o.getString("library") == Library.ADDED) Library.ADDED else Library.SOURCE_STEAM }, + storeId = o.optString("storeId").ifEmpty { null }, ) } }.getOrElse { e -> Log.w(TAG, "unreadable, ignored: ${e.message}"); emptyList() } @@ -36,7 +38,8 @@ object LibraryCache { for (g in games) array.put( JSONObject().put("appId", g.appId).put("name", g.name).put("library", g.library).put("gameId", g.gameId) .put("lastPlayed", g.lastPlayed).putFile("art", g.art).putFile("hero", g.hero) - .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon), + .putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon) + .put("source", g.source).apply { g.storeId?.let { put("storeId", it) } }, ) val f = file(context) val tmp = File(f.path + ".tmp") diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 25f24f8300..e61a83413b 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -61,6 +61,10 @@ object SessionFiles { "usr/local/bin/droiddeck-desktop-bookmarks" to "usr/local/bin/droiddeck-desktop-bookmarks", "usr/local/bin/droiddeck-steam-shim" to "usr/local/bin/droiddeck-steam-shim", "usr/local/bin/droiddeck-steam-shortcuts" to "usr/local/bin/droiddeck-steam-shortcuts", + // Asks the app for an Epic game's sign-in code at launch, from the compat tool (StoreLaunchRequests). + "usr/local/bin/droiddeck-store-launch" to "usr/local/bin/droiddeck-store-launch", + // A game's web links to Android's browser (Wine's winebrowser starts it). + "usr/local/bin/droiddeck-open-url" to "usr/local/bin/droiddeck-open-url", "usr/local/bin/droiddeck-steam-games" to "usr/local/bin/droiddeck-steam-games", "usr/local/bin/droiddeck-pad-defaults" to "usr/local/bin/droiddeck-pad-defaults", // Flatpak: the bwrap stand-in, the store's helper and setup, and the front end's launcher. diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt index 10bfd3b041..c12a69f39d 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt @@ -73,7 +73,9 @@ object SessionLogShare { // every share kept the share sheet from appearing for ten seconds or more. val scrubbed = SessionArtifacts.scrubbedFiles(folder) val live = liveSteamLogs(context, folder) - val total = (files + live).sumOf { it.length() }.coerceAtLeast(1L) + // The stores' own log (last 7 days, app-private, already redacted), under stores/. + val storeLogs = com.droiddeck.launcher.stores.StoreLogFiles.files(context) + val total = (files + live + storeLogs).sumOf { it.length() }.coerceAtLeast(1L) var done = 0L var shown = -1 fun advance(f: File) { @@ -84,6 +86,7 @@ object SessionLogShare { ZipOutputStream(zip.outputStream().buffered()).use { z -> files.forEach { f -> addEntry(z, folder.name + "/" + f.relativeTo(folder).path, f, f.relativeTo(folder).path in scrubbed); advance(f) } live.forEach { f -> addEntry(z, folder.name + "/steam/" + f.name, f, false); advance(f) } + storeLogs.forEach { f -> addEntry(z, folder.name + "/stores/" + f.name, f, false); advance(f) } } return zip } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index f4dcc6df09..2fd7b05c59 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -53,6 +53,52 @@ object SessionPrefs { prefs(context).edit().putBoolean("storeEnabled", on).apply() } + /** The Stores section (GOG, Epic Games, Amazon Games): its rail item. Off by default. */ + fun gameStoresEnabled(context: Context): Boolean = prefs(context).getBoolean("gameStoresEnabled", false) + + fun setGameStoresEnabled(context: Context, on: Boolean) { + prefs(context).edit().putBoolean("gameStoresEnabled", on).apply() + } + + /** The store download engine's speed tier (stores/download/StoreDownloadTier ids); Fast unless chosen otherwise. */ + fun gameStoresSpeedTier(context: Context): String = prefs(context).getString("gameStoresSpeedTier", "fast") ?: "fast" + + fun setGameStoresSpeedTier(context: Context, tier: String) { + prefs(context).edit().putString("gameStoresSpeedTier", tier).apply() + } + + /** 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 + + fun setStoresOpenTab(context: Context, tab: String) { + prefs(context).edit().putString("storesOpenTab", if (tab == STORES_OPEN_STORE) STORES_OPEN_STORE else STORES_OPEN_LIBRARY).apply() + } + + /** Mature titles (the stores' own 17+/18 ratings or adult tags) in the storefront's shelves and search; off by default. Owned games always show. */ + fun storesShowMature(context: Context): Boolean = prefs(context).getBoolean("storesShowMature", false) + + fun setStoresShowMature(context: Context, show: Boolean) { + prefs(context).edit().putBoolean("storesShowMature", show).apply() + } + + const val STORES_OPEN_LIBRARY = "library" + const val STORES_OPEN_STORE = "store" + + /** The install root picked last time the Stores asked where (a StoreInstallRoot target's path); only the dialog's default, never a silent choice. */ + fun storesInstallTarget(context: Context): String = prefs(context).getString("storesInstallTarget", "") ?: "" + + fun setStoresInstallTarget(context: Context, path: String) { + prefs(context).edit().putString("storesInstallTarget", path).apply() + } + + /** How many store downloads run at once, 1..3; one by default (a single download already fills the link). */ + fun gameStoresParallel(context: Context): Int = prefs(context).getInt("gameStoresParallel", 1).coerceIn(1, 3) + + fun setGameStoresParallel(context: Context, count: Int) { + prefs(context).edit().putInt("gameStoresParallel", count.coerceIn(1, 3)).apply() + } + /** * The session's performance HUD (the fps box). A Deck-mode Steam session with the performance * overlay has Steam's own (mangoapp, from the QAM), so there the HUD is off unless turned on diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index da80bf6f71..d563ffcf1f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -325,6 +325,8 @@ class SessionService : Service() { killStragglers() SessionFiles.stage(this, root) com.droiddeck.launcher.agent.AgentGuest.reset(this) + // The compat tool asks for an Epic game's sign-in code at every launch, the client's Play button included. + com.droiddeck.launcher.stores.StoreLaunchRequests.start(this) val sessionDir = openSessionFolder() val sessionLog = File(sessionDir, "session.log") @@ -884,6 +886,15 @@ class SessionService : Service() { Log.w(TAG, "added games: ${root.host} is not a readable folder this session") } } + // Store games installed on a card: each card's Games root at its own fixed place, so the + // shortcuts point somewhere whether or not the card is also the Steam library. + for ((host, guest) in com.droiddeck.launcher.stores.StoreInstallRoot.externalRoots(this)) { + if (host.isDirectory && host.canRead()) { + File(LinuxRuntime.rootDir(this), guest.removePrefix("/")).mkdirs() + binds.add(host.path + ":" + guest) + Log.i(TAG, "store games: $host -> $guest") + } + } // Folders of added scripts outside internal storage, where their links point. binds.addAll(com.droiddeck.launcher.runtime.UserApps.binds(this)) val roms = SessionPrefs.romsDir(this).takeIf { it.isNotEmpty() }?.let { File(it) } @@ -1395,6 +1406,10 @@ class SessionService : Service() { pipTask = false SessionState.guestPid = -1 com.droiddeck.launcher.agent.AgentGuest.stop() + com.droiddeck.launcher.stores.StoreLaunchRequests.stop() + // Cloud saves of a store game played this session, if the compat tool's exit request did + // not upload them (a session closed from the drawer never reaches it). + Thread({ com.droiddeck.launcher.stores.CloudSaves.uploadAllDirty(applicationContext, "session-end", running = false) }, "cloud-session-end").start() runCatching { com.droiddeck.launcher.agent.AgentEnv.endSession(this) } .onFailure { Log.w(TAG, "clearing the agent's session environment", it) } releaseLocks() diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudPlan.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudPlan.kt new file mode 100644 index 0000000000..720b462d69 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudPlan.kt @@ -0,0 +1,87 @@ +package com.droiddeck.launcher.stores + +import org.json.JSONObject + +/** + * What a cloud sync may do, decided from three views of each save file: the local copy, the cloud + * copy, and the baseline - both as they were when this device last synced the game. Pure, so the + * rules are tested on their own: + * + * - No upload before a download has completed once on this device (no baseline): a first launch + * writes new profile files that must never replace the real saves in the cloud. + * - The first download (no baseline) lets the cloud win where the two differ - the local folder is + * backed up first. + * - A file changed on both sides since the baseline is a conflict: neither side is overwritten; the + * user picks Keep cloud or Keep local in Manage saves. + * - Otherwise the side that changed since the baseline wins; one that changed on neither is left. + */ +object CloudPlan { + /** One file as this device last saw it in sync: the local MD5, and the cloud's MD5 (when known) and time. */ + data class Entry(val localMd5: String, val cloudMd5: String?, val cloudModified: Long) + + class Local(val md5: String) + class Remote(val md5: String?, val modified: Long) + + class Plan(val transfer: List, val conflicts: List, val same: List, val refused: String? = null) + + /** The cloud copy differs from what the baseline recorded of it. */ + private fun cloudChanged(r: Remote, b: Entry?): Boolean { + if (b == null) return true + if (r.md5 != null && b.cloudMd5 != null) return !r.md5.equals(b.cloudMd5, ignoreCase = true) + // Times only (Epic's listing): the service's own clock, so a couple of seconds of slack. + return r.modified > 0 && b.cloudModified > 0 && kotlin.math.abs(r.modified - b.cloudModified) > 2000 + } + + private fun localChanged(l: Local, b: Entry?): Boolean = b == null || !l.md5.equals(b.localMd5, ignoreCase = true) + + private fun same(l: Local, r: Remote): Boolean = r.md5 != null && r.md5.equals(l.md5, ignoreCase = true) + + /** Cloud → device. [baseline] null = this device never synced the game. */ + fun down(local: Map, remote: Map, baseline: Map?): Plan { + val transfer = ArrayList(); val conflicts = ArrayList(); val same = ArrayList() + for ((name, r) in remote) { + val l = local[name] + val b = baseline?.get(name) + when { + l == null -> transfer += name + same(l, r) -> same += name + baseline == null -> transfer += name + cloudChanged(r, b) && localChanged(l, b) -> conflicts += name + cloudChanged(r, b) -> transfer += name + } + } + return Plan(transfer, conflicts, same) + } + + /** Device → cloud. Refused outright without a baseline. */ + fun up(local: Map, remote: Map, baseline: Map?): Plan { + if (baseline == null) return Plan(emptyList(), emptyList(), emptyList(), refused = "no-baseline") + val transfer = ArrayList(); val conflicts = ArrayList(); val same = ArrayList() + for ((name, l) in local) { + val r = remote[name] + val b = baseline[name] + when { + r == null -> transfer += name + same(l, r) -> same += name + localChanged(l, b) && cloudChanged(r, b) -> conflicts += name + localChanged(l, b) -> transfer += name + } + } + return Plan(transfer, conflicts, same) + } + + fun toJson(baseline: Map): String = JSONObject().apply { + for ((k, e) in baseline) put(k, JSONObject().put("l", e.localMd5).put("c", e.cloudMd5 ?: "").put("t", e.cloudModified)) + }.toString() + + fun fromJson(text: String?): Map? { + if (text == null) return null + return runCatching { + val o = JSONObject(text) + o.keys().asSequence().associateWith { k -> + val e = o.getJSONObject(k) + Entry(e.optString("l"), e.optString("c").ifEmpty { null }, e.optLong("t", -1L)) + } + }.getOrNull() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt new file mode 100644 index 0000000000..c54297794d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSavePaths.kt @@ -0,0 +1,84 @@ +/* + * Cloud-save folder resolution for GOG and Epic games, after Bannerlator's GogCloudSavePaths and + * EpicCloudSavePaths (device-proven there, August 2026): a store's save-location template expanded + * against the game's Proton prefix and its install folder. + */ +package com.droiddeck.launcher.stores + +import java.io.File + +/** + * Where a store keeps a game's saves on Windows, made concrete inside this game's Proton prefix. + * + * GOG gives a template per game in its public remote-config (`cloudStorage.locations[].location`): + * a leading `` (or `%TOKEN%`) and a path, e.g. ELDERBORN's + * `/Hyperstrange/ELDERBORN`. Epic gives `CloudSaveFolder` in the + * game's catalog attributes: a leading `{Token}`, with `{EpicId}` / `{AppName}` inside, e.g. + * `{AppData}/Game/Saved/SaveGames`. Profile tokens map under the prefix's Wine user + * (`drive_c/users/steamuser`), the install tokens to the game's folder. The path is walked against + * what is on disk case-insensitively; a result outside its boundary is refused, never guessed. + */ +object CloudSavePaths { + /** The Wine user every prefix has (Proton's). */ + fun profile(prefix: File): File = File(prefix, "drive_c/users/steamuser") + + /** Expands a GOG location template; null for an unknown token or an escape. */ + fun gog(template: String, prefix: File, install: File): File? { + val norm = template.replace('\\', '/').trim().trimStart('/') + val match = Regex("^(<\\?[^?>]+\\?>|%[^%]+%)").find(norm) ?: return null + val token = match.groupValues[1].trim('<', '>', '?', '%').uppercase() + val profile = profile(prefix) + val (base, boundary) = when (token) { + "INSTALL" -> install to install + "SAVED_GAMES" -> File(profile, "Saved Games") to profile + "DOCUMENTS" -> File(profile, "Documents") to profile + "APPLICATION_DATA_LOCAL", "LOCALAPPDATA" -> File(profile, "AppData/Local") to profile + "APPLICATION_DATA_LOCAL_LOW" -> File(profile, "AppData/LocalLow") to profile + "APPLICATION_DATA_ROAMING", "APPDATA" -> File(profile, "AppData/Roaming") to profile + "USERPROFILE" -> profile to profile + "PROGRAMDATA" -> File(prefix, "drive_c/ProgramData").let { it to it } + else -> return null + } + return walk(base, boundary, norm.substring(match.value.length), fuzzy = false) + } + + /** Expands an Epic CloudSaveFolder; null for an unknown token or an escape. */ + fun epic(template: String, prefix: File, install: File, accountId: String, appName: String): File? { + val norm = template.replace('\\', '/').trim().trimStart('/') + val match = Regex("^\\{([^}]+)\\}").find(norm) ?: return null + val profile = profile(prefix) + val (base, boundary) = when (match.groupValues[1].lowercase()) { + "installdir" -> install to install + "userprofile" -> profile to profile + "userdir" -> File(profile, "Documents") to profile + "usersavedgames" -> File(profile, "Saved Games") to profile + "appdata", "localappdata" -> File(profile, "AppData/Local") to profile + "roamingappdata" -> File(profile, "AppData/Roaming") to profile + else -> return null + } + val rest = norm.substring(match.value.length) + .replace("{epicid}", accountId, ignoreCase = true) + .replace("{appname}", appName, ignoreCase = true) + return walk(base, boundary, rest, fuzzy = true) + } + + /** + * Each segment matched against the folder's entries case-insensitively (and, for Epic, by letters + * and digits alone when that names exactly one), else taken as written - a download creates it. + */ + private fun walk(base: File, boundary: File, rest: String, fuzzy: Boolean): File? { + var dir = base + for (seg in rest.trimStart('/').split('/').filter { it.isNotEmpty() && it != "." }) { + if (seg == "..") { dir = dir.parentFile ?: return null; continue } + val kids = dir.listFiles() + val existing = kids?.firstOrNull { it.name.equals(seg, ignoreCase = true) } + ?: if (fuzzy) kids?.filter { squash(it.name) == squash(seg) && squash(seg).isNotEmpty() }?.singleOrNull() else null + dir = existing ?: File(dir, seg) + } + val bound = runCatching { boundary.canonicalPath }.getOrNull() ?: return null + val dest = runCatching { dir.canonicalPath }.getOrNull() ?: return null + return if (dest == bound || dest.startsWith(bound + File.separator)) dir else null + } + + private fun squash(s: String) = s.lowercase().filter { it.isLetterOrDigit() } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt new file mode 100644 index 0000000000..3d877784fd --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudSaves.kt @@ -0,0 +1,387 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.frontend.AddedGames +import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.stores.epic.EpicApiClient +import com.droiddeck.launcher.stores.epic.EpicCredentialStore +import com.droiddeck.launcher.stores.epic.EpicPrefs +import com.droiddeck.launcher.stores.gog.GogAuth +import com.droiddeck.launcher.stores.gog.GogPrefs +import java.io.File +import java.net.HttpURLConnection +import java.net.URL +import java.security.MessageDigest +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit +import org.json.JSONObject + +/** + * GOG and Epic cloud saves for an installed store game: before a launch the cloud copy is brought + * down where it is newer, after the game exits what changed goes up - newest wins, file by file. + * Before a download overwrites anything, the folder is copied to the app's storage (the last + * [BACKUPS] per game). Both launch paths reach it: the compat tool's request from + * droiddeck-store-launch ([StoreLaunchRequests]) for every launch, and the Games tab's Manage saves. + * + * The save folder: GOG's remote-config location template (by the game's Galaxy client id) or + * Epic's CloudSaveFolder catalog attribute, cached per game, made concrete by [CloudSavePaths] + * inside the game's Proton prefix. A game whose store gives none has no cloud saves. + */ +object CloudSaves { + private const val TAG = "CloudSaves" + private const val BACKUPS = 3 + private const val PARALLEL = 6 + + /** What a sync did, for the log and the Manage saves row. */ + /** + * What a sync did, for the log and the Manage saves row. [result]: ok, skipped (nothing to do or + * not possible: [reason] says why), deferred (no prefix yet; it runs once Proton makes one), + * failed. [conflicts]: files changed on both sides, left alone. + */ + class Result(val result: String, val files: Int, val bytes: Long, val reason: String, val conflicts: Int = 0) { + val ok: Boolean get() = result != "failed" + val line: String get() = "result=$result files=$files bytes=$bytes reason=$reason" + if (conflicts > 0) " conflicts=$conflicts" else "" + } + + /** A game's cloud state for the Manage saves row. */ + class Status(val supported: Boolean, val folder: File?, val lastSync: Long, val reason: String, val conflicts: List = emptyList()) + + private fun game(context: Context, store: Store, id: String): Pair? = + StoreInstallRoot.gameFolders(context).firstNotNullOfOrNull { f -> + StoreGameSidecar.read(f)?.takeIf { it.store == store && it.id == id && it.isInstalled }?.let { f to it } + } + + /** The game's prefix: compatdata//pfx, wherever that library is. */ + fun prefix(context: Context, folder: File): File? { + val appId = AddedGames.scan(context).firstOrNull { it.folder.absolutePath == folder.absolutePath }?.appId ?: return null + return Library.protonPrefix(context, appId and 0xFFFFFFFFL) + } + + private fun prefs(context: Context, store: Store) = if (store == Store.GOG) GogPrefs.get(context) else EpicPrefs.get(context) + + fun lastSync(context: Context, store: Store, id: String): Long = prefs(context, store).getLong("cloud_sync_$id", 0L) + + private fun markSynced(context: Context, store: Store, id: String) = prefs(context, store).edit().putLong("cloud_sync_$id", System.currentTimeMillis()).apply() + + // ---- the save folder ------------------------------------------------------------------------ + + /** The store's template for this game: cached, else fetched; "" when the store gives none, null when it could not be asked. */ + fun template(context: Context, store: Store, id: String, folder: File, sidecar: StoreGameSidecar): String? { + val p = prefs(context, store) + if (p.contains("cloud_template_$id")) return p.getString("cloud_template_$id", "") + val fetched = when (store) { + Store.GOG -> gogTemplate(context, id, folder) + Store.EPIC -> { + val token = EpicCredentialStore.getValidAccessToken(context) ?: return null + val ns = sidecar.extra["namespace"].orEmpty() + val item = sidecar.extra["catalogItemId"].orEmpty() + if (ns.isEmpty() || item.isEmpty()) "" else EpicApiClient.getCustomAttribute(token, ns, item, "CloudSaveFolder") + } + Store.AMAZON -> "" + } ?: return null + p.edit().putString("cloud_template_$id", fetched).apply() + Log.i(TAG, "cloud ${store.id} $id template ${if (fetched.isEmpty()) "none" else "found"}") + return fetched + } + + private fun gogClientId(context: Context, id: String, folder: File): String? = + GogPrefs.get(context).getString("client_id_$id", null)?.ifEmpty { null } + ?: runCatching { JSONObject(File(folder, "goggame-$id.info").readText()).optString("clientId").ifEmpty { null } }.getOrNull() + + /** GOG's public remote-config: `content.Windows.cloudStorage.locations`, the one named "saves" first. */ + private fun gogTemplate(context: Context, id: String, folder: File): String? { + val clientId = gogClientId(context, id, folder) ?: return "" + return try { + val c = URL("https://remote-config.gog.com/components/galaxy_client/clients/$clientId?component_version=2.0.45").openConnection() as HttpURLConnection + c.connectTimeout = 15_000; c.readTimeout = 15_000 + c.setRequestProperty("User-Agent", "GOG Galaxy") + if (c.responseCode !in 200..299) { c.disconnect(); return null } + val cloud = JSONObject(c.inputStream.bufferedReader().readText()).optJSONObject("content")?.optJSONObject("Windows")?.optJSONObject("cloudStorage") + c.disconnect() + if (cloud?.optBoolean("enabled", false) != true) return "" + val locations = cloud.optJSONArray("locations") ?: return "" + val all = (0 until locations.length()).mapNotNull { locations.optJSONObject(it) } + (all.firstOrNull { it.optString("name").equals("saves", true) } ?: all.firstOrNull())?.optString("location").orEmpty() + } catch (e: Exception) { + Log.w(TAG, "gog remote-config: ${e.javaClass.simpleName}") + null + } + } + + /** The concrete save folder in the prefix, or null with why. */ + private fun saveFolder(context: Context, store: Store, id: String): Pair { + val (folder, sidecar) = game(context, store, id) ?: return null to "not-installed" + val template = template(context, store, id, folder, sidecar) ?: return null to "store-unreachable" + if (template.isEmpty()) return null to "no-cloud-saves" + val pfx = prefix(context, folder) ?: return null to "no-prefix" + val dir = when (store) { + Store.GOG -> CloudSavePaths.gog(template, pfx, folder) + Store.EPIC -> CloudSavePaths.epic(template, pfx, folder, EpicCredentialStore.load(context)?.accountId.orEmpty(), id) + Store.AMAZON -> null + } + return dir to (if (dir == null) "unresolved" else "ok") + } + + fun status(context: Context, store: Store, id: String): Status { + val (dir, reason) = saveFolder(context, store, id) + return Status(reason != "no-cloud-saves" && reason != "not-installed", dir, lastSync(context, store, id), reason, conflicts(context, store, id)) + } + + // ---- transport ------------------------------------------------------------------------------ + + private fun transport(context: Context, store: Store, id: String, folder: File): CloudTransport? = when (store) { + Store.GOG -> { + val account = StoreAccounts.read(context, Store.GOG) + val userId = account?.optString("user_id").orEmpty() + val galaxy = GogAuth.validToken(context) + val clientId = gogClientId(context, id, folder) + if (userId.isEmpty() || galaxy == null || clientId == null) null + else GogCloud(userId, clientId, gogScopedToken(context, id, clientId, account?.optString("refresh_token").orEmpty()) ?: galaxy) + } + Store.EPIC -> { + val token = EpicCredentialStore.getValidAccessToken(context) + val account = EpicCredentialStore.load(context)?.accountId.orEmpty() + if (token == null || account.isEmpty()) null else EpicCloud(account, id, token) + } + Store.AMAZON -> null + } + + /** GOG cloud storage wants a token issued to the game's own client: the Galaxy refresh token, exchanged with the game's id and secret. */ + private fun gogScopedToken(context: Context, id: String, clientId: String, refresh: String): String? { + val secret = GogPrefs.get(context).getString("client_secret_$id", null)?.ifEmpty { null } ?: return null + if (refresh.isEmpty()) return null + return try { + val enc = { s: String -> java.net.URLEncoder.encode(s, "UTF-8") } + val c = URL("https://auth.gog.com/token?client_id=${enc(clientId)}&client_secret=${enc(secret)}&grant_type=refresh_token&refresh_token=${enc(refresh)}").openConnection() as HttpURLConnection + c.connectTimeout = 15_000; c.readTimeout = 15_000 + c.setRequestProperty("User-Agent", "GOG Galaxy") + if (c.responseCode != 200) { c.disconnect(); return null } + JSONObject(c.inputStream.bufferedReader().readText()).optString("access_token").ifEmpty { null }.also { c.disconnect() } + } catch (e: Exception) { null } + } + + // ---- sync ----------------------------------------------------------------------------------- + + private fun baseline(context: Context, store: Store, id: String): Map? = + CloudPlan.fromJson(prefs(context, store).getString("cloud_baseline_$id", null)) + + private fun saveBaseline(context: Context, store: Store, id: String, b: Map) = + prefs(context, store).edit().putString("cloud_baseline_$id", CloudPlan.toJson(b)).apply() + + /** Files changed on both sides since the last sync, waiting for Keep cloud / Keep local. */ + fun conflicts(context: Context, store: Store, id: String): List = + prefs(context, store).getString("cloud_conflicts_$id", "").orEmpty().split('\n').filter { it.isNotEmpty() } + + private fun saveConflicts(context: Context, store: Store, id: String, names: Collection) = + prefs(context, store).edit().putString("cloud_conflicts_$id", names.joinToString("\n")).apply() + + /** Cloud → device where the cloud copy changed (the first time: wherever it differs). [force]: conflicts too (Keep cloud). Blocking. */ + fun download(context: Context, store: Store, id: String, force: Boolean = false): Result = + sync(context, store, id, "down") { dir, cloud -> downWith(context, store, id, dir, cloud, force) } + + /** Device → cloud where the local copy changed, once a download has set the baseline. [force]: conflicts too (Keep local). Blocking. */ + fun upload(context: Context, store: Store, id: String, force: Boolean = false): Result = + sync(context, store, id, "up") { dir, cloud -> upWith(context, store, id, dir, cloud, force) } + + fun keepCloud(context: Context, store: Store, id: String): Result = download(context, store, id, force = true) + fun keepLocal(context: Context, store: Store, id: String): Result = upload(context, store, id, force = true) + + /** + * The pre-launch download, or - when Proton has not made the game's prefix yet (a first launch) + * - a deferred one: answered at once, then run in the background as soon as the prefix's user + * folder appears (within [PREFIX_WAIT_MS]), so the launch is not held up for it. + */ + fun downloadOrDefer(context: Context, store: Store, id: String): Result { + val app = context.applicationContext + val g = game(app, store, id) ?: return done(store, id, "down", Result("skipped", 0, 0, "not-installed")) + if (!g.second.cloud) return done(store, id, "down", Result("skipped", 0, 0, "off")) + if (prefix(app, g.first)?.let { CloudSavePaths.profile(it).isDirectory } == true) return download(app, store, id) + Thread({ + val end = System.currentTimeMillis() + PREFIX_WAIT_MS + while (System.currentTimeMillis() < end) { + if (prefix(app, g.first)?.let { CloudSavePaths.profile(it).isDirectory } == true) { download(app, store, id); return@Thread } + Thread.sleep(2000) + } + done(store, id, "down", Result("skipped", 0, 0, "no-prefix")) + }, "cloud-deferred").start() + return done(store, id, "down", Result("deferred", 0, 0, "no-prefix")) + } + + private const val PREFIX_WAIT_MS = 60_000L + + private fun sync(context: Context, store: Store, id: String, way: String, body: (File, CloudTransport) -> Result): Result { + val result = try { + val g = game(context, store, id) + when { + g == null -> Result("skipped", 0, 0, "not-installed") + !g.second.cloud -> Result("skipped", 0, 0, "off") + else -> { + val (dir, reason) = saveFolder(context, store, id) + val cloud = if (dir != null) transport(context, store, id, g.first) else null + when { + dir == null -> Result("skipped", 0, 0, reason) + cloud == null -> Result("skipped", 0, 0, "not-signed-in") + else -> body(dir, cloud).also { if (it.result == "ok") markSynced(context, store, id) } + } + } + } + } catch (e: NoCloudSaves) { + prefs(context, store).edit().putString("cloud_template_$id", "").apply() + Result("skipped", 0, 0, "no-cloud-saves") + } catch (e: Exception) { + Result("failed", 0, 0, e.javaClass.simpleName) + } + return done(store, id, way, result) + } + + private fun done(store: Store, id: String, way: String, r: Result): Result { + Log.i(TAG, "cloud ${store.id} $id $way ${r.line}") + StoresState.logLine("cloud ${store.id} $id $way ${r.line}") + return r + } + + private fun localFiles(dir: File): Map = + if (!dir.isDirectory) emptyMap() + else dir.walkTopDown().filter { it.isFile }.associateBy { it.relativeTo(dir).path.replace(File.separatorChar, '/') } + + internal fun md5(f: File): String = md5(f.readBytes()) + + internal fun md5(data: ByteArray): String = MessageDigest.getInstance("MD5").digest(data).joinToString("") { "%02x".format(it) } + + /** The cloud listing, with MD5 and time filled in (one HEAD each where the listing lacks them, in parallel). */ + private fun remote(cloud: CloudTransport, wanted: (String) -> Boolean): Map { + val files = cloud.list() + val pool = Executors.newFixedThreadPool(PARALLEL, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("cloud")) + for (f in files) if (wanted(f.name)) pool.execute { runCatching { cloud.details(f) } } + pool.shutdown(); pool.awaitTermination(2, TimeUnit.MINUTES) + return files.associateBy { it.name } + } + + internal fun downWith(context: Context, store: Store, id: String, dir: File, cloud: CloudTransport, force: Boolean): Result { + val files = localFiles(dir) + val remote = remote(cloud) { it in files } + val local = files.mapValues { CloudPlan.Local(md5(it.value)) } + val base = baseline(context, store, id) + val plan = CloudPlan.down(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base) + val take = if (force) plan.transfer + plan.conflicts else plan.transfer + if (take.isNotEmpty() && files.isNotEmpty()) backup(context, store, id, dir, "local") + val next = HashMap(base.orEmpty()) + var bytes = 0L + for (name in take) { + val f = remote[name] ?: continue + val (data, modified) = cloud.get(f) + val dest = File(dir, name) + if (!dest.canonicalPath.startsWith(dir.canonicalPath + File.separator)) continue + dest.parentFile?.mkdirs() + dest.writeBytes(data) + if (modified > 0) dest.setLastModified(modified) + bytes += data.size + next[name] = CloudPlan.Entry(md5(data), f.md5, if (modified > 0) modified else f.modifiedMs) + } + for (name in plan.same) remote[name]?.let { r -> next[name] = CloudPlan.Entry(local.getValue(name).md5, r.md5, r.modifiedMs) } + // A download that completed sets the baseline even when the cloud was empty: uploads may follow. + saveBaseline(context, store, id, next) + val left = if (force) emptyList() else plan.conflicts + saveConflicts(context, store, id, left) + return Result("ok", take.size, bytes, if (take.isEmpty()) "up-to-date" else "downloaded", left.size) + } + + internal fun upWith(context: Context, store: Store, id: String, dir: File, cloud: CloudTransport, force: Boolean): Result { + val base = baseline(context, store, id) ?: return Result("skipped", 0, 0, "no-baseline") + val files = localFiles(dir) + if (files.isEmpty()) return Result("skipped", 0, 0, "nothing-local") + val remote = remote(cloud) { it in files } + val local = files.mapValues { CloudPlan.Local(md5(it.value)) } + val plan = CloudPlan.up(local, remote.mapValues { CloudPlan.Remote(it.value.md5, it.value.modifiedMs) }, base) + val send = if (force) plan.transfer + plan.conflicts else plan.transfer + val next = HashMap(base) + for (name in plan.same) remote[name]?.let { r -> next[name] = CloudPlan.Entry(local.getValue(name).md5, r.md5, r.modifiedMs) } + if (send.isNotEmpty()) { + // The cloud copies about to be replaced are kept first, as a download keeps the local ones. + backupCloud(context, store, id, cloud, send.mapNotNull { remote[it] }) + val data = send.associateWith { files.getValue(it).readBytes() } + val stamped = cloud.put(data) + for ((name, t) in stamped) if (t > 0) files[name]?.setLastModified(t) + val after = remote(cloud) { it in data } + for (name in data.keys) { + val r = after[name] + next[name] = CloudPlan.Entry(md5(data.getValue(name)), r?.md5, r?.modifiedMs ?: stamped[name] ?: -1L) + } + } + saveBaseline(context, store, id, next) + val left = if (force) emptyList() else plan.conflicts + saveConflicts(context, store, id, left) + return Result("ok", send.size, send.sumOf { files.getValue(it).length() }, if (send.isEmpty()) "up-to-date" else "uploaded", left.size) + } + + // ---- uploads that do not depend on one hook --------------------------------------------------- + + /** + * A game launched with cloud saves is marked dirty in the app's storage when its pre-launch step + * runs. The upload happens on the first of: the compat tool's exit request (`exit`), the end of + * the session (`session-end`), or the app's next start for a mark a killed app left (`recovery`) + * - so a session closed from the drawer, a game Steam killed, or a crash still uploads. The mark + * goes only after an upload that went through, or one skipped on purpose with its reason logged. + */ + private fun marks(context: Context) = context.applicationContext.getSharedPreferences("stores.cloud", Context.MODE_PRIVATE) + + fun markDirty(context: Context, store: Store, id: String) { + marks(context).edit().putLong("dirty:${store.id}:$id", System.currentTimeMillis()).apply() + } + + fun dirty(context: Context): List> = marks(context).all.keys.mapNotNull { k -> + val parts = k.split(':', limit = 3) + if (parts.size == 3 && parts[0] == "dirty") Store.byId(parts[1])?.let { it to parts[2] } else null + } + + private val markLock = Any() + + /** Why a skip is final: retrying cannot change it, so the mark goes. */ + private val FINAL_SKIPS = setOf("off", "no-cloud-saves", "no-baseline", "not-installed", "nothing-local") + + /** Uploads [id] if it is marked dirty; null when it is not (another trigger got there first). */ + fun uploadDirty(context: Context, store: Store, id: String, trigger: String): Result? = synchronized(markLock) { + val key = "dirty:${store.id}:$id" + if (!marks(context).contains(key)) return null + val r = sync(context, store, id, "up trigger=$trigger") { dir, cloud -> upWith(context, store, id, dir, cloud, force = false) } + if (r.result == "ok" || (r.result == "skipped" && r.reason in FINAL_SKIPS)) marks(context).edit().remove(key).apply() + r + } + + /** + * Every dirty game, uploaded; [running] says a game session is up, in which case `recovery` waits + * - a marked game may be the one running. + */ + fun uploadAllDirty(context: Context, trigger: String, running: Boolean = com.droiddeck.launcher.session.SessionState.running) { + if (trigger == "recovery" && running) return + for ((store, id) in dirty(context)) uploadDirty(context, store, id, trigger) + } + + private fun backupRoot(context: Context, store: Store, id: String) = + File(context.filesDir, "stores/cloud-backups/${store.id}-${id.replace(Regex("[^A-Za-z0-9._-]"), "_")}") + + /** The local folder as it is, before a download changes it; the last [BACKUPS] of each kind are kept. */ + private fun backup(context: Context, store: Store, id: String, dir: File, kind: String) { + val root = backupRoot(context, store, id) + runCatching { dir.copyRecursively(File(root, "$kind-${System.currentTimeMillis()}"), overwrite = true) } + prune(root, kind) + } + + private fun backupCloud(context: Context, store: Store, id: String, cloud: CloudTransport, files: List) { + if (files.isEmpty()) return + val root = backupRoot(context, store, id) + val target = File(root, "cloud-${System.currentTimeMillis()}") + for (f in files) { + val dest = File(target, f.name) + if (!dest.canonicalPath.startsWith(target.canonicalPath + File.separator)) continue + dest.parentFile?.mkdirs() + dest.writeBytes(cloud.get(f).first) + } + prune(root, "cloud") + } + + private fun prune(root: File, kind: String) { + root.listFiles { f -> f.name.startsWith("$kind-") }?.sortedByDescending { it.name }?.drop(BACKUPS)?.forEach { it.deleteRecursively() } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt b/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt new file mode 100644 index 0000000000..3446bf4ab7 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/CloudTransport.kt @@ -0,0 +1,191 @@ +/* + * The two stores' cloud-save services, after Bannerlator's GogCloudSaveManager and + * EpicCloudSaveManager (device-proven there, August 2026): GOG's cloudstorage (one object per + * file, Last-Modified and an MD5 ETag on a HEAD) and Epic's savesync data storage (a listing with + * lastModified and signed read links; signed write links asked for per upload). + */ +package com.droiddeck.launcher.stores + +import java.io.ByteArrayOutputStream +import java.net.HttpURLConnection +import java.net.URL +import java.net.URLEncoder +import java.text.SimpleDateFormat +import java.util.Locale +import java.util.TimeZone +import org.json.JSONArray +import org.json.JSONObject + +/** One file in a game's cloud: its key (a path with '/'), when it was written (-1 unknown) and its MD5 when known. */ +class CloudFile(val name: String, var modifiedMs: Long, var md5: String? = null, val readLink: String? = null) + +/** A store's cloud for one game. Every call is blocking network. */ +interface CloudTransport { + fun list(): List + /** Fills in [CloudFile.modifiedMs] / [CloudFile.md5] where the listing left them out. */ + fun details(file: CloudFile) {} + /** The file's bytes and its cloud time (-1 unknown). */ + fun get(file: CloudFile): Pair + /** Writes [files] (key → bytes); the cloud times assigned, where the service says (-1 unknown). */ + fun put(files: Map): Map +} + +private const val TIMEOUT = 30_000 + +private fun open(url: String, method: String, token: String?, ua: String): HttpURLConnection = + (URL(url).openConnection() as HttpURLConnection).apply { + requestMethod = method + connectTimeout = TIMEOUT + readTimeout = TIMEOUT + setRequestProperty("User-Agent", ua) + if (token != null) setRequestProperty("Authorization", "Bearer $token") + } + +private fun HttpURLConnection.body(): ByteArray = inputStream.use { input -> + ByteArrayOutputStream().also { input.copyTo(it) }.toByteArray() +} + +private fun httpDate(s: String?): Long = try { + SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).apply { timeZone = TimeZone.getTimeZone("GMT") }.parse(s!!.trim())?.time ?: -1L +} catch (e: Exception) { -1L } + +internal fun isoDate(s: String?): Long { + if (s == null || s.length < 19) return -1L + return try { + java.util.Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply { + clear() + set(s.substring(0, 4).toInt(), s.substring(5, 7).toInt() - 1, s.substring(8, 10).toInt(), + s.substring(11, 13).toInt(), s.substring(14, 16).toInt(), s.substring(17, 19).toInt()) + }.timeInMillis + } catch (e: Exception) { -1L } +} + +/** Thrown when the store says the game has no cloud saves for this client. */ +class NoCloudSaves : Exception("no cloud saves") + +/** GOG cloudstorage: `/v1//[/]`. */ +class GogCloud(private val userId: String, private val clientId: String, private val token: String) : CloudTransport { + private val base = "https://cloudstorage.gog.com/v1/$userId/$clientId" + private val ua = "GOG Galaxy" + + /** A path segment: '+' from a space is wrong in a path (it stores a second, '+'-named copy). */ + private fun key(name: String) = URLEncoder.encode(name, "UTF-8").replace("+", "%20") + + override fun list(): List { + val c = open(base, "GET", token, ua) + val code = c.responseCode + if (code == 404) { c.disconnect(); return emptyList() } + if (code !in 200..299) { + val err = runCatching { c.errorStream?.readBytes()?.decodeToString().orEmpty() }.getOrDefault("") + c.disconnect() + if ("not_enabled_for_client" in err || "disabled" in err) throw NoCloudSaves() + throw java.io.IOException("GOG cloud list HTTP $code") + } + val text = c.body().decodeToString().trim() + c.disconnect() + if (text.isEmpty() || text == "[]") return emptyList() + // A plain newline-separated list of keys (checked on device); a JSON array is read too. + if (text.startsWith("[")) { + val a = JSONArray(text) + return (0 until a.length()).mapNotNull { a.optJSONObject(it) }.mapNotNull { o -> + val n = o.optString("name").ifEmpty { return@mapNotNull null } + val lm = o.optLong("last_modified", 0L) + CloudFile(n, if (lm > 1_000_000_000_000L) lm else if (lm > 0) lm * 1000 else -1L) + } + } + return text.lines().map { it.trim() }.filter { it.isNotEmpty() }.map { CloudFile(it, -1L) } + } + + override fun details(file: CloudFile) { + if (file.modifiedMs > 0 && file.md5 != null) return + val c = open("$base/${key(file.name)}", "HEAD", token, ua) + if (c.responseCode in 200..299) { + httpDate(c.getHeaderField("Last-Modified")).takeIf { it > 0 }?.let { file.modifiedMs = it } + c.getHeaderField("ETag")?.replace("\"", "")?.trim()?.ifEmpty { null }?.let { file.md5 = it } + } + c.disconnect() + } + + override fun get(file: CloudFile): Pair { + val c = open("$base/${key(file.name)}", "GET", token, ua) + if (c.responseCode !in 200..299) { val code = c.responseCode; c.disconnect(); throw java.io.IOException("GOG cloud get HTTP $code") } + val modified = httpDate(c.getHeaderField("Last-Modified")) + return c.body().also { c.disconnect() } to modified + } + + override fun put(files: Map): Map = files.mapValues { (name, data) -> + val c = open("$base/${key(name)}", "PUT", token, ua) + c.doOutput = true + c.setRequestProperty("Content-Type", "application/octet-stream") + c.setFixedLengthStreamingMode(data.size) + c.outputStream.use { it.write(data) } + val code = c.responseCode + c.disconnect() + if (code !in 200..299) throw java.io.IOException("GOG cloud put HTTP $code") + // The server stamps the object itself; that time, so the next sync sees the two as equal. + CloudFile(name, -1L).also { details(it) }.modifiedMs + } +} + +/** Epic savesync: `/api/v1/access/egstore/savesync///`. */ +class EpicCloud(private val accountId: String, private val appName: String, private val token: String) : CloudTransport { + private val base = "https://datastorage-public-service-liveegs.live.use1a.on.epicgames.com/api/v1/access/egstore/savesync/$accountId/$appName/" + private val ua = "EpicGamesLauncher/15.17.1-22692490" + + override fun list(): List { + val c = open(base, "GET", token, ua) + val code = c.responseCode + if (code == 404) { c.disconnect(); return emptyList() } + if (code !in 200..299) { c.disconnect(); throw java.io.IOException("Epic cloud list HTTP $code") } + val root = JSONObject(c.body().decodeToString().ifEmpty { "{}" }) + c.disconnect() + val files = root.optJSONObject("files") ?: return emptyList() + return files.keys().asSequence().mapNotNull { k -> + val e = files.optJSONObject(k) ?: return@mapNotNull null + CloudFile(strip(k), isoDate(e.optString("lastModified", null)), e.optString("hash", "").ifEmpty { null }, e.optString("readLink", "").ifEmpty { null }) + }.toList() + } + + /** The listing names a file `<...>//`; the key is what is under the save folder. */ + private fun strip(key: String): String { + val marker = "/$appName/" + val i = key.indexOf(marker) + if (i >= 0) return key.substring(i + marker.length) + return key.removePrefix("$appName/") + } + + override fun get(file: CloudFile): Pair { + val link = file.readLink ?: throw java.io.IOException("Epic cloud: no read link") + val c = open(link, "GET", null, ua) + if (c.responseCode !in 200..299) { val code = c.responseCode; c.disconnect(); throw java.io.IOException("Epic cloud get HTTP $code") } + return c.body().also { c.disconnect() } to file.modifiedMs + } + + override fun put(files: Map): Map { + if (files.isEmpty()) return emptyMap() + val c = open(base, "POST", token, ua) + c.doOutput = true + c.setRequestProperty("Content-Type", "application/json") + val req = JSONObject().put("files", JSONArray(files.keys.toList())).toString().toByteArray() + c.outputStream.use { it.write(req) } + if (c.responseCode !in 200..299) { val code = c.responseCode; c.disconnect(); throw java.io.IOException("Epic cloud write links HTTP $code") } + val links = JSONObject(c.body().decodeToString()).optJSONObject("files") ?: JSONObject() + c.disconnect() + val out = HashMap() + for (k in links.keys()) { + val link = links.optJSONObject(k)?.optString("writeLink", "")?.ifEmpty { null } ?: continue + val name = strip(k) + val data = files[name] ?: files[k] ?: continue + val p = open(link, "PUT", null, ua) + p.doOutput = true + p.setRequestProperty("Content-Type", "application/octet-stream") + p.setFixedLengthStreamingMode(data.size) + p.outputStream.use { it.write(data) } + val code = p.responseCode + p.disconnect() + if (code !in 200..299) throw java.io.IOException("Epic cloud put HTTP $code") + out[name] = -1L + } + return out + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt b/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt new file mode 100644 index 0000000000..54f31f7512 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/LoginFlows.kt @@ -0,0 +1,55 @@ +package com.droiddeck.launcher.stores + +import android.net.Uri +import android.util.Log +import android.webkit.WebView +import com.droiddeck.launcher.stores.gog.GogAuth + +/** The three stores' sign-in flows for [StoreLoginActivity]. */ +internal object LoginFlows { + private const val TAG = "StoreLogin" + + fun forStore(store: Store, state: String): StoreLoginActivity.Flow? = when (store) { + Store.GOG -> GogFlow(state) + Store.EPIC -> epicFlow() + Store.AMAZON -> amazonFlow() + } + + /** Set by the Epic and Amazon backends when they are in the build; null keeps the chip's card honest. */ + @Volatile var epicFlow: () -> StoreLoginActivity.Flow? = { null } + @Volatile var amazonFlow: () -> StoreLoginActivity.Flow? = { null } + + /** + * GOG's implicit grant: the token arrives in the redirect URL's fragment. The CSRF state is + * checked only when GOG echoes one back - its social-login path returns none, and treating + * that as a mismatch threw away good tokens. + */ + private class GogFlow(private val state: String) : StoreLoginActivity.Flow { + override val startUrl: String get() = GogAuth.authUrl(state) + override val userAgent: String get() = GogAuth.GALAXY_UA + override fun isRedirect(url: String): Boolean = url.startsWith(GogAuth.REDIRECT_PREFIX) + + override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? { + val fragment = Uri.parse(url).fragment ?: return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_verification) + val frag = Uri.parse("x://x?$fragment") + val returned = frag.getQueryParameter("state") + if (returned != null && returned != state) { + Log.w(TAG, "gog: state mismatch; redirect rejected") + return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_verification) + } + val access = frag.getQueryParameter("access_token") + if (access.isNullOrEmpty()) { + // The keys only, never the values: the fragment carries live tokens. + Log.w(TAG, "gog: redirect without access_token; keys=${frag.queryParameterNames}") + return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_verification) + } + return try { + GogAuth.saveLogin(activity, access, frag.getQueryParameter("refresh_token"), frag.getQueryParameter("user_id")) + null + } catch (e: Exception) { + Log.w(TAG, "gog: saving the sign-in failed: ${e.javaClass.simpleName}") + activity.getString(com.droiddeck.launcher.R.string.stores_login_error_generic) + } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/SizeCache.kt b/app/src/main/java/com/droiddeck/launcher/stores/SizeCache.kt new file mode 100644 index 0000000000..c89df6b526 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/SizeCache.kt @@ -0,0 +1,46 @@ +package com.droiddeck.launcher.stores + +import java.io.File +import org.json.JSONObject + +/** + * Game sizes as found, kept per game AND the version they were found for, in + * `filesDir/stores//sizes.json`: a size is never fetched twice for one version, and a + * new version (a library refresh brings it) simply misses and is looked up again. The file carries + * its format; one in another format is discarded, not read. + */ +class SizeCache(private val file: File) { + private val sizes = HashMap>() + + init { + runCatching { + val o = JSONObject(file.readText()) + if (o.optInt("format") == FORMAT) { + val s = o.getJSONObject("sizes") + for (k in s.keys()) s.optJSONObject(k)?.let { e -> sizes[k] = e.optString("v") to e.optLong("b") } + } + } + } + + @Synchronized + fun get(id: String, version: String): Long? = sizes[id]?.takeIf { it.first == version && it.second > 0 }?.second + + @Synchronized + fun put(id: String, version: String, bytes: Long) { + if (bytes <= 0) return + sizes[id] = version to bytes + runCatching { + file.parentFile?.mkdirs() + val s = JSONObject() + for ((k, v) in sizes) s.put(k, JSONObject().put("v", v.first).put("b", v.second)) + val tmp = File(file.path + ".tmp") + tmp.writeText(JSONObject().put("format", FORMAT).put("sizes", s).toString()) + tmp.renameTo(file) + } + } + + companion object { + const val FORMAT = 1 + fun forStore(filesDir: File, store: Store) = SizeCache(File(filesDir, "stores/${store.id}/sizes.json")) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/SteamLiveShortcuts.kt b/app/src/main/java/com/droiddeck/launcher/stores/SteamLiveShortcuts.kt new file mode 100644 index 0000000000..475af804ee --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/SteamLiveShortcuts.kt @@ -0,0 +1,187 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Base64 +import android.util.Log +import com.droiddeck.launcher.frontend.AddedGames +import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.session.SessionService +import com.droiddeck.launcher.session.SessionState +import org.json.JSONArray +import org.json.JSONObject +import java.io.ByteArrayOutputStream +import java.io.DataInputStream +import java.io.File +import java.io.InputStream +import java.io.OutputStream +import java.net.HttpURLConnection +import java.net.Socket +import java.net.URL +import java.security.SecureRandom + +/** + * Adds and removes a store game's shortcut in a Steam client that is running right now, so it + * shows without waiting for the next client start. The runtime starts the client with its + * DevTools port open (droiddeck-session writes it to session/agent/cdp-port) and the client's own + * JavaScript API has `SteamClient.Apps.AddShortcut`; this speaks the small part of the DevTools + * protocol that one `Runtime.evaluate` needs, over a plain socket, nothing else. + * + * Best-effort throughout: no session, no port, no SharedJSContext, or an API that moved and the + * shortcut simply arrives with the shortcuts writer at the next start. The appid Steam derives + * (CRC of the quoted exe plus the name) is the one the writer computes, so the two never disagree. + */ +object SteamLiveShortcuts { + private const val TAG = "SteamLive" + private const val TAG_NAME = "droiddeck-app" + private const val COMPAT_TOOL = "droiddeck-proton-arm64" + + /** Adds every store game in [games] the client does not list yet. Blocking; worker thread. */ + fun sync(context: Context, games: List) { + val wanted = games.filter { it.source != Library.ADDED } + if (wanted.isEmpty() || !clientRunning()) return + val list = JSONArray() + for (g in wanted) list.put(JSONObject().put("name", g.name).put("exe", g.guestExe).put("dir", g.guestDir)) + val js = """ + (async () => { + const want = ${list}; + const have = await SteamClient.Apps.GetAllShortcuts(); + const strip = s => (s || "").replace(/^"|"$/g, ""); + let added = 0; + for (const w of want) { + if (have.some(a => a.data && strip(a.data.strExePath) === w.exe && a.data.strAppName === w.name)) continue; + const id = await SteamClient.Apps.AddShortcut(w.name, w.exe, w.dir, ""); + if (!id) continue; + try { SteamClient.Apps.AddUserTagToApps([id], ${JSONObject.quote(TAG_NAME)}); } catch (e) {} + try { SteamClient.Apps.SpecifyCompatTool(id, ${JSONObject.quote(COMPAT_TOOL)}); } catch (e) {} + added++; + } + return added; + })() + """.trimIndent() + val result = evaluate(context, js) ?: return + Log.i(TAG, "live shortcuts: $result added") + if (result != "0") StoresState.logLine("Steam: $result shortcut(s) added to the running client") + } + + /** Removes the shortcut with [appId] from the running client. Blocking; worker thread. */ + fun remove(context: Context, appId: Long) { + if (!clientRunning()) return + val result = evaluate(context, "(async () => { await SteamClient.Apps.RemoveShortcut(${appId and 0xFFFFFFFFL}); return 1; })()") + if (result != null) Log.i(TAG, "live shortcuts: removed $appId") + } + + private fun clientRunning(): Boolean = SessionState.running && SessionState.mode == SessionService.MODE_STEAM && !SessionState.stopRequested + + /** `Runtime.evaluate` of [expression] in the client's SharedJSContext; the value as text, or null. */ + private fun evaluate(context: Context, expression: String): String? { + val portFile = File(LinuxRuntime.sessionRoot(context), "agent/cdp-port") + val port = runCatching { portFile.readText().trim().toInt() }.getOrNull() ?: return null + val targets = runCatching { JSONArray(get("http://127.0.0.1:$port/json")) }.getOrNull() ?: return null + var wsUrl: String? = null + for (i in 0 until targets.length()) { + val t = targets.optJSONObject(i) ?: continue + if (t.optString("title").contains("SharedJSContext") || t.optString("url").contains("SharedJSContext")) { wsUrl = t.optString("webSocketDebuggerUrl").ifEmpty { null }; break } + } + if (wsUrl == null) { Log.w(TAG, "no SharedJSContext target"); return null } + return try { + WebSocket(wsUrl, 8000).use { ws -> + ws.sendText(JSONObject().put("id", 1).put("method", "Runtime.evaluate").put("params", JSONObject() + .put("expression", expression).put("returnByValue", true).put("awaitPromise", true).put("userGesture", true)).toString()) + val deadline = System.currentTimeMillis() + 15_000 + while (System.currentTimeMillis() < deadline) { + val message = JSONObject(ws.receiveText() ?: return null) + if (message.optInt("id") != 1) continue + if (message.has("error")) { Log.w(TAG, "cdp: ${message.optJSONObject("error")}"); return null } + val result = message.optJSONObject("result") ?: return null + result.optJSONObject("exceptionDetails")?.let { Log.w(TAG, "js: ${it.optJSONObject("exception")?.optString("description") ?: it.optString("text")}"); return null } + return result.optJSONObject("result")?.opt("value")?.toString() + } + null + } + } catch (e: Exception) { + Log.w(TAG, "cdp: ${e.javaClass.simpleName}: ${e.message}") + null + } + } + + private fun get(url: String): String { + val c = URL(url).openConnection() as HttpURLConnection + c.connectTimeout = 3000; c.readTimeout = 3000 + try { return c.inputStream.bufferedReader().readText() } finally { c.disconnect() } + } + + /** The least of RFC 6455 a localhost DevTools talk needs: upgrade, masked text out, text in. */ + private class WebSocket(url: String, timeoutMs: Int) : AutoCloseable { + private val socket: Socket + private val out: OutputStream + private val input: DataInputStream + + init { + val u = URL(url.replaceFirst("ws://", "http://")) + socket = Socket(u.host, if (u.port > 0) u.port else 80).apply { soTimeout = timeoutMs } + out = socket.getOutputStream() + input = DataInputStream(socket.getInputStream()) + val key = ByteArray(16).also { SecureRandom().nextBytes(it) } + val path = u.path.ifEmpty { "/" } + (u.query?.let { "?$it" } ?: "") + out.write(("GET $path HTTP/1.1\r\nHost: ${u.host}:${u.port}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n" + + "Sec-WebSocket-Key: ${Base64.encodeToString(key, Base64.NO_WRAP)}\r\nSec-WebSocket-Version: 13\r\n\r\n").toByteArray()) + out.flush() + val status = readLine(input) ?: throw java.io.IOException("no upgrade response") + if (!status.contains(" 101 ")) throw java.io.IOException("upgrade refused: $status") + while (true) { val line = readLine(input) ?: break; if (line.isEmpty()) break } + } + + private fun readLine(s: InputStream): String? { + val buf = ByteArrayOutputStream() + while (true) { + val b = s.read() + if (b < 0) return if (buf.size() == 0) null else buf.toString("UTF-8") + if (b == '\n'.code) return buf.toString("UTF-8").trimEnd('\r') + buf.write(b) + } + } + + fun sendText(text: String) { + val payload = text.toByteArray() + val mask = ByteArray(4).also { SecureRandom().nextBytes(it) } + val frame = ByteArrayOutputStream() + frame.write(0x81) + when { + payload.size < 126 -> frame.write(0x80 or payload.size) + payload.size < 65536 -> { frame.write(0x80 or 126); frame.write(payload.size ushr 8); frame.write(payload.size and 0xFF) } + else -> { frame.write(0x80 or 127); for (i in 7 downTo 0) frame.write(((payload.size.toLong() ushr (8 * i)) and 0xFF).toInt()) } + } + frame.write(mask) + for (i in payload.indices) frame.write((payload[i].toInt() xor mask[i % 4].toInt()) and 0xFF) + out.write(frame.toByteArray()); out.flush() + } + + /** The next text message (fragments joined); null on close. */ + fun receiveText(): String? { + val message = ByteArrayOutputStream() + while (true) { + val b0 = input.read(); if (b0 < 0) return null + val b1 = input.read(); if (b1 < 0) return null + val fin = b0 and 0x80 != 0 + val opcode = b0 and 0x0F + var len = (b1 and 0x7F).toLong() + if (len == 126L) len = input.readUnsignedShort().toLong() + else if (len == 127L) len = input.readLong() + val masked = b1 and 0x80 != 0 + val mask = if (masked) ByteArray(4).also { input.readFully(it) } else null + if (len > 16L * 1024 * 1024) throw java.io.IOException("frame too large") + val data = ByteArray(len.toInt()).also { input.readFully(it) } + if (mask != null) for (i in data.indices) data[i] = (data[i].toInt() xor mask[i % 4].toInt()).toByte() + when (opcode) { + 0x8 -> return null + 0x9 -> { out.write(byteArrayOf(0x8A.toByte(), 0x80.toByte(), 0, 0, 0, 0)); out.flush() } + 0x1, 0x0 -> { message.write(data); if (fin) return message.toString("UTF-8") } + else -> {} + } + } + } + + override fun close() { runCatching { out.write(byteArrayOf(0x88.toByte(), 0x80.toByte(), 0, 0, 0, 0)); out.flush() }; runCatching { socket.close() } } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/Store.kt b/app/src/main/java/com/droiddeck/launcher/stores/Store.kt new file mode 100644 index 0000000000..9ff97654b8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/Store.kt @@ -0,0 +1,15 @@ +package com.droiddeck.launcher.stores + +/** + * The three storefronts the Stores section talks to. [id] is what the sidecar, the prefs file + * names and the Games badge carry; it never changes once a game is installed under it. + */ +enum class Store(val id: String, val label: String, val shortLabel: String) { + GOG("gog", "GOG", "GOG"), + EPIC("epic", "Epic Games", "Epic"), + AMAZON("amazon", "Amazon Games", "Amazon"); + + companion object { + fun byId(id: String?): Store? = entries.firstOrNull { it.id == id } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt new file mode 100644 index 0000000000..1ea8440956 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt @@ -0,0 +1,48 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Log +import org.json.JSONObject +import java.io.File + +/** + * Each store's sign-in, kept in the app's private files: `filesDir/stores//credentials.json`. + * Tokens never go to shared storage and never into a log; the file holds whatever the store's auth + * flow handed back (access and refresh tokens, expiry, the account's display name, and for Amazon + * the device registration), as one JSON object the store's own client reads and writes. + */ +object StoreAccounts { + private const val TAG = "StoreAccounts" + private const val FILE = "credentials.json" + + /** The store's private folder: credentials, caches, the engine's scratch. */ + fun dir(context: Context, store: Store): File = File(File(context.filesDir, "stores"), store.id).apply { mkdirs() } + + fun credentialsFile(context: Context, store: Store): File = File(dir(context, store), FILE) + + /** The stored credentials, or null when signed out or unreadable (never logged: the text is the secret). */ + fun read(context: Context, store: Store): JSONObject? { + val f = credentialsFile(context, store) + if (!f.isFile) return null + return try { JSONObject(f.readText()) } catch (e: Exception) { Log.w(TAG, "${store.id}: credentials unreadable (${e.javaClass.simpleName})"); null } + } + + fun write(context: Context, store: Store, json: JSONObject) { + val f = credentialsFile(context, store) + val tmp = File(f.path + ".tmp") + tmp.writeText(json.toString()) + tmp.setReadable(false, false); tmp.setReadable(true, true) + if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) } + } + + fun clear(context: Context, store: Store) { + credentialsFile(context, store).delete() + } + + /** The account's display name when signed in (the store's own name for it, or a stand-in), else null. */ + fun signedInAs(context: Context, store: Store): String? { + val json = read(context, store) ?: return null + if (json.optString("access_token", "").isEmpty()) return null + return json.optString("display_name", "").ifEmpty { json.optString("username", "") }.ifEmpty { store.label } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreArt.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreArt.kt new file mode 100644 index 0000000000..4726c2db7b --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreArt.kt @@ -0,0 +1,63 @@ +package com.droiddeck.launcher.stores + +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Canvas +import android.graphics.Color +import android.util.Log +import java.io.File +import java.net.HttpURLConnection +import java.net.URL + +/** + * The store's art, placed in the game's folder under the names AddedGameArt already looks for - + * `cover.jpg` (the portrait capsule), `hero.jpg` and `header.jpg` (the wide art) - so a store game + * shows real art in the Games tab and the session's shortcuts writer copies it into the client's + * grid. Each image is decoded and re-encoded as an opaque JPEG over a dark ground: a store's PNG + * with an alpha fade would otherwise end in white, which is what the washed-out cards were. + */ +object StoreArt { + private const val TAG = "StoreArt" + + /** Fetches what the sidecar names; a missing or unreadable image is skipped, never an error. Blocking. */ + fun fetchInto(folder: File, sidecar: StoreGameSidecar) { + sidecar.cover?.let { place(it, File(folder, "cover.jpg")) } + val wide = sidecar.hero ?: return + if (place(wide, File(folder, "hero.jpg"))) { + val header = File(folder, "header.jpg") + if (!header.isFile) runCatching { File(folder, "hero.jpg").copyTo(header, overwrite = true) } + } + } + + private fun place(url: String, dst: File): Boolean { + if (dst.isFile && dst.length() > 0) return true + val bytes = get(url) ?: return false + val bitmap = runCatching { BitmapFactory.decodeByteArray(bytes, 0, bytes.size) }.getOrNull() ?: return false + val opaque = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) + Canvas(opaque).apply { drawColor(Color.rgb(0x12, 0x14, 0x17)); drawBitmap(bitmap, 0f, 0f, null) } + val tmp = File(dst.path + ".tmp") + return try { + tmp.outputStream().use { opaque.compress(Bitmap.CompressFormat.JPEG, 90, it) } + tmp.renameTo(dst) + } catch (e: Exception) { + Log.w(TAG, "${dst.name}: ${e.javaClass.simpleName}") + tmp.delete() + false + } finally { + bitmap.recycle(); opaque.recycle() + } + } + + private fun get(url: String): ByteArray? { + var c: HttpURLConnection? = null + return try { + c = (URL(url).openConnection() as HttpURLConnection).apply { connectTimeout = 10_000; readTimeout = 20_000; setRequestProperty("User-Agent", StoreNet.BROWSER_UA) } + if (c.responseCode != 200) null else c.inputStream.use { it.readBytes() } + } catch (e: Exception) { + Log.w(TAG, "${StoreLog.redactUrl(url)}: ${e.javaClass.simpleName}") + null + } finally { + c?.disconnect() + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt new file mode 100644 index 0000000000..c5c85378b4 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreCatalog.kt @@ -0,0 +1,135 @@ +package com.droiddeck.launcher.stores + +/** + * One title as the Stores page draws it, whichever store produced it: a card on a shelf, a tile + * in a grid, the hero of a game page. Library games and storefront results both project into + * this, so one card composable serves every tab. + */ +data class CatalogItem( + val store: Store, + /** Store-native id: GOG product id, Epic catalog item id (offer id for storefront results), Amazon product id. */ + val id: String, + val title: String, + /** Wide (16:9-ish) art for cards and heroes. */ + val imageUrl: String?, + /** Tall (2:3) box art when the store publishes one; falls back to [imageUrl]. */ + val tallImageUrl: String? = null, + /** Comma-joined genre / tag line. */ + val tags: String = "", + val isFree: Boolean = false, + /** False when the endpoint gave no price at all: the price row then draws nothing. */ + val hasPrice: Boolean = false, + /** Pre-formatted by the store ("$9.99"). */ + val finalPrice: String = "", + val originalPrice: String = "", + val discountPercent: Int = 0, + /** The web product page: where "Get for free" and "View on " land. */ + val storeUrl: String = "", + val developer: String = "", + val releaseDate: String = "", + val description: String = "", + /** True for a title the signed-in account owns (a library entry). */ + val owned: Boolean = false, + /** The install size in bytes when known, else 0. */ + val sizeBytes: Long = 0L, + /** Per-store identifiers the install needs (Epic namespace / catalog id / app name, Amazon entitlement / sku). */ + val extra: Map = emptyMap(), + /** Rated or tagged for adults by the store's own data; hidden from the storefront unless Show mature content is on. */ + val mature: Boolean = false, +) { + val isDiscounted: Boolean get() = discountPercent > 0 && originalPrice.isNotBlank() + /** Stable key across stores ("gog:1207658924"). */ + val key: String get() = "${store.id}:$id" +} + +/** A store's storefront shelves: what the Store tab shows above the account's own library. */ +class StoreShelves( + val whatsNew: List = emptyList(), + val deals: List = emptyList(), + val free: List = emptyList(), + val trending: List = emptyList(), +) { + val isEmpty: Boolean get() = whatsNew.isEmpty() && deals.isEmpty() && free.isEmpty() && trending.isEmpty() + val all: List get() = (whatsNew + deals + free + trending).distinctBy { it.id } +} + +/** A game a store installed, as found on disk through its sidecar. */ +/** Store tags that mark adult content, by slug or name, lowercased. */ +private val MATURE_TAGS = setOf("mature", "nsfw", "adult", "adult only", "adults only", "nudity", "sexual content", "sexual-content") + +/** Whether a store's own tags mark a title as adult. */ +fun matureByTags(tags: Collection): Boolean = tags.any { it.trim().lowercase() in MATURE_TAGS } + +/** + * Whether a store's own age ratings mark a title as adult: ESRB M (17) or AO (18), PEGI / USK / the + * store's own rating 18, or any rating system's 17+. Ratings are ages, as GOG gives them. + */ +fun matureByAge(ages: Collection): Boolean = ages.any { it >= 17 } + +/** + * The Installed tab's cards for [store], from the installs on disk - the same source as its count: + * each joined to its library item by store id (by title when the ids differ), else a card made + * from the sidecar's own title and art, so an install shows even before the library has loaded. + */ +fun installedCards(store: Store, installed: List, library: List): List { + val byId = library.associateBy { it.id } + val byTitle = library.associateBy { it.title.lowercase() } + return installed.filter { it.sidecar.store == store }.map { game -> + val s = game.sidecar + byId[s.id] ?: byTitle[s.title.lowercase()] + ?: CatalogItem(store, s.id, s.title, imageUrl = s.hero ?: s.cover, tallImageUrl = s.cover ?: s.hero, owned = true) + }.distinctBy { it.id }.sortedBy { it.title.lowercase() } +} + +/** + * A refreshed list with every unchanged item kept as the very object already shown, so a refresh + * that lands while the grid is on screen recomposes only the cards that changed - no reset, the + * scroll position and the pad's focus stay. + */ +fun mergeItems(old: List?, new: List): List { + if (old.isNullOrEmpty()) return new + val before = old.associateBy { it.key } + val merged = new.map { n -> before[n.key]?.takeIf { it == n } ?: n } + return if (merged.size == old.size && merged.indices.all { merged[it] === old[it] }) old else merged +} + +class InstalledStoreGame(val sidecar: StoreGameSidecar, val folder: java.io.File) { + val key: String get() = "${sidecar.store.id}:${sidecar.id}" +} + +private val TEMPLATE_KEY = Regex("product_(description|feature)_\\d+") +private val HTML_TAG = Regex("<[^>]+>") +private val ENTITY = Regex("&(#\\d+|#x[0-9a-fA-F]+|[a-zA-Z]+);") +private val NAMED_ENTITIES = mapOf("amp" to "&", "lt" to "<", "gt" to ">", "quot" to "\"", "apos" to "'", "nbsp" to " ", "ndash" to "–", "mdash" to "—", "hellip" to "…", "trade" to "™", "reg" to "®", "copy" to "©") + +/** + * Store copy as plain text: GOG's account endpoints return template keys + * (`product_description_2015545325`) for some products instead of words, and every store's + * descriptions carry HTML. Those keys go, tags go, entities are decoded, whitespace is collapsed; + * "" when nothing readable is left, so a page shows nothing rather than a placeholder. + */ +fun cleanStoreText(raw: String?): String { + if (raw.isNullOrBlank()) return "" + var s = raw.replace(TEMPLATE_KEY, " ") + s = s.replace(Regex("(?i)|

||"), "\n").replace(HTML_TAG, " ") + s = ENTITY.replace(s) { m -> + val e = m.groupValues[1] + when { + e.startsWith("#x") -> e.substring(2).toIntOrNull(16)?.toChar()?.toString() ?: " " + e.startsWith("#") -> e.substring(1).toIntOrNull()?.toChar()?.toString() ?: " " + else -> NAMED_ENTITIES[e.lowercase()] ?: " " + } + } + return s.lines().map { it.replace(Regex("[ \\t\\u00A0]+"), " ").trim() }.filter { it.isNotEmpty() }.joinToString("\n") +} + +/** Human sizes the way the Downloads page and the cards print them ("810.2 MB", "3.9 GB"). */ +fun formatBytes(bytes: Long): String = when { + bytes >= 1_073_741_824L -> "%.1f GB".format(bytes / 1_073_741_824.0) + bytes >= 1_048_576L -> "%.1f MB".format(bytes / 1_048_576.0) + bytes >= 1024L -> "%.0f KB".format(bytes / 1024.0) + bytes >= 0L -> "$bytes B" + else -> "" +} + +fun formatSpeed(bytesPerSec: Long): String = if (bytesPerSec <= 0L) "" else "${formatBytes(bytesPerSec)}/s" diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt new file mode 100644 index 0000000000..f1a97541b4 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreExe.kt @@ -0,0 +1,44 @@ +package com.droiddeck.launcher.stores + +import java.io.File + +/** + * Which .exe in an install folder is the game. The store's own hint (a manifest's launch + * executable) wins when the file exists; else the candidates, helpers and installers left out, + * the one named most like the title first, then the shallowest. Relative to the folder, forward + * slashes; "" when the folder has none. + */ +object StoreExe { + private val SKIP = listOf("redist", "unins", "setup", "crash", "report", "helper", "dotnet", "vcredist", "directx", "eac", "easyanticheat", "battleye", "launcher", "updater", "config") + + fun pick(folder: File, hint: String?, title: String): String { + val hinted = hint?.takeIf { it.isNotBlank() }?.replace('\\', '/')?.trimStart('/')?.let { File(folder, it) } + if (hinted != null && hinted.isFile) return relative(folder, hinted) + val candidates = ArrayList() + collect(folder, candidates, 0) + if (candidates.isEmpty()) return "" + val key = title.lowercase().replace(Regex("[^a-z0-9]"), "") + val best = candidates.firstOrNull { f -> + val n = f.nameWithoutExtension.lowercase().replace(Regex("[^a-z0-9]"), "") + n.isNotEmpty() && (n == key || (key.length >= 4 && key.startsWith(n)) || (n.length >= 4 && n.startsWith(key))) + } ?: candidates.sortedWith(compareBy { depth(folder, it) }.thenByDescending { it.length() }).first() + return relative(folder, best) + } + + private fun collect(dir: File, out: MutableList, depth: Int) { + if (depth > 4) return + val files = dir.listFiles() ?: return + for (f in files) { + if (f.isFile && f.name.endsWith(".exe", ignoreCase = true)) { + val lower = f.name.lowercase() + if (SKIP.none { lower.contains(it) }) out.add(f) + } + } + for (f in files) if (f.isDirectory && !f.name.startsWith(".")) collect(f, out, depth + 1) + } + + private fun depth(root: File, f: File): Int = relative(root, f).count { it == '/' } + + fun relative(root: File, f: File): String = + f.absolutePath.removePrefix(root.absolutePath).trimStart('/').replace('\\', '/') +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt new file mode 100644 index 0000000000..149f30fb31 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt @@ -0,0 +1,165 @@ +package com.droiddeck.launcher.stores + +import org.json.JSONArray +import org.json.JSONObject +import java.io.File + +/** + * What a store install leaves beside the game so the rest of the app can treat the folder like + * any added game: `.droiddeck-store.json` in the game's folder. + * + * [AddedGames.scanGame] reads it to pick the exe (instead of guessing from the folder), to name + * the shortcut after the store's title and to tag the game with its [store]. [launcher] is the + * small .bat the install writes when the game needs arguments or environment at launch (Epic's + * identity arguments, Amazon's FuelPump variables): a Steam shortcut can only carry a fixed Exe, + * so the launcher is what the shortcut runs and [exe] is what it starts. Every store install is a + * Steam shortcut; there is no switch for it (an `addToSteam` field in an older sidecar is ignored). + * + * Paths are relative to the game folder with forward slashes, so the folder can move between + * storage volumes and stay valid. + * + * An install writes the sidecar twice: at its start with [state] `installing` (store, id, title - + * no exe yet), and at its end with the exe, the launcher and `installed`. A folder whose sidecar + * still says `installing` is an unfinished install, never a game: the scan leaves it out and the + * Stores page offers to resume it. A sidecar without the field (an earlier build) is installed. + */ +class StoreGameSidecar( + val store: Store, + val id: String, + val title: String, + val exe: String, + val launcher: String? = null, + val args: List = emptyList(), + val env: Map = emptyMap(), + val installVersion: String = "", + val installedAt: Long = 0L, + val cover: String? = null, + val hero: String? = null, + /** Per-store identifiers the launcher needs again (Epic namespace / catalog id, Amazon entitlement). */ + val extra: Map = emptyMap(), + val state: String = STATE_INSTALLED, + /** An Epic game's launch choices (the Games tab's Epic card); both launch paths read them from here. */ + val epic: EpicOptions = EpicOptions(), + /** GOG / Epic cloud saves for this game: down before a launch, up after it (the game card's switch). */ + val cloud: Boolean = true, +) { + val isInstalled: Boolean get() = state == STATE_INSTALLED + + fun exeFile(folder: File): File = File(folder, exe) + fun launcherFile(folder: File): File? = launcher?.takeIf { it.isNotEmpty() }?.let { File(folder, it) } + + fun copy( + exe: String = this.exe, launcher: String? = this.launcher, args: List = this.args, + env: Map = this.env, installVersion: String = this.installVersion, + installedAt: Long = this.installedAt, cover: String? = this.cover, hero: String? = this.hero, + extra: Map = this.extra, state: String = this.state, epic: EpicOptions = this.epic, cloud: Boolean = this.cloud, + ) = StoreGameSidecar(store, id, title, exe, launcher, args, env, installVersion, installedAt, cover, hero, extra, state, epic, cloud) + + fun toJson(): JSONObject = JSONObject().apply { + put("version", VERSION) + put("store", store.id) + put("id", id) + put("title", title) + if (exe.isNotEmpty()) put("exe", exe) + if (state != STATE_INSTALLED) put("state", state) + if (store == Store.EPIC) put("epic", JSONObject().put("eos", epic.eos).put("offline", epic.offline)) + if (store != Store.AMAZON) put("cloud", cloud) + if (!launcher.isNullOrEmpty()) put("launcher", launcher) + if (args.isNotEmpty()) put("args", JSONArray(args)) + if (env.isNotEmpty()) put("env", JSONObject(env as Map<*, *>)) + if (installVersion.isNotEmpty()) put("installVersion", installVersion) + if (installedAt > 0L) put("installedAt", installedAt) + if (!cover.isNullOrEmpty()) put("cover", cover) + if (!hero.isNullOrEmpty()) put("hero", hero) + if (extra.isNotEmpty()) put("extra", JSONObject(extra as Map<*, *>)) + } + + fun write(folder: File) { + folder.mkdirs() + val file = File(folder, FILE_NAME) + val tmp = File(folder, "$FILE_NAME.tmp") + tmp.writeText(toJson().toString(2)) + if (!tmp.renameTo(file)) { file.delete(); tmp.renameTo(file) } + } + + companion object { + const val FILE_NAME = ".droiddeck-store.json" + const val VERSION = 1 + const val STATE_INSTALLED = "installed" + const val STATE_INSTALLING = "installing" + + fun file(folder: File): File = File(folder, FILE_NAME) + + /** + * Changes an Epic game's launch choices on disk: reads the sidecar fresh (never a copy held + * by a screen), applies [change], writes it, and reads it back. The written sidecar, or null + * when there is none or the write did not take. Blocking. + */ + fun updateEpic(folder: File, change: (EpicOptions) -> EpicOptions): StoreGameSidecar? { + val current = read(folder) ?: return null + val next = current.copy(epic = change(current.epic)) + next.write(folder) + return read(folder)?.takeIf { it.epic == next.epic } + } + + /** As [updateEpic], for the cloud-saves switch. */ + fun updateCloud(folder: File, on: Boolean): StoreGameSidecar? { + val current = read(folder) ?: return null + current.copy(cloud = on).write(folder) + return read(folder)?.takeIf { it.cloud == on } + } + + /** The sidecar in [folder], or null when there is none or it cannot be read. */ + fun read(folder: File): StoreGameSidecar? { + val f = file(folder) + if (!f.isFile) return null + return runCatching { parse(f.readText()) }.getOrNull() + } + + /** Pure parse; null for anything that is not a usable sidecar (so a scan falls back to guessing). */ + fun parse(text: String): StoreGameSidecar? { + val o = runCatching { JSONObject(text) }.getOrNull() ?: return null + val store = Store.byId(o.optString("store")) ?: return null + val id = o.optString("id", "") + val title = o.optString("title", "") + val exe = o.optString("exe", "").replace('\\', '/') + val state = o.optString("state", STATE_INSTALLED).ifEmpty { STATE_INSTALLED } + if (id.isEmpty() || title.isEmpty()) return null + // An unfinished install has no exe yet; a finished one must name one. + if (exe.isEmpty() && state == STATE_INSTALLED) return null + // A relative path only: a sidecar that points outside its folder is not trusted. + if (exe.isNotEmpty() && !relativeInside(exe)) return null + val launcher = o.optString("launcher", "").replace('\\', '/').takeIf { it.isNotEmpty() && relativeInside(it) } + val args = o.optJSONArray("args")?.let { a -> List(a.length()) { a.optString(it) }.filter { it.isNotEmpty() } } ?: emptyList() + val env = LinkedHashMap() + o.optJSONObject("env")?.let { e -> e.keys().forEach { k -> env[k] = e.optString(k) } } + val extra = LinkedHashMap() + o.optJSONObject("extra")?.let { e -> e.keys().forEach { k -> extra[k] = e.optString(k) } } + return StoreGameSidecar( + store, id, title, exe, launcher, args, env, + installVersion = o.optString("installVersion", ""), + installedAt = o.optLong("installedAt", 0L), + cover = o.optString("cover", "").ifEmpty { null }, + hero = o.optString("hero", "").ifEmpty { null }, + extra = extra, + state = state, + // An "overlay" (or "v") field from an earlier build is read past: there is no overlay. + epic = o.optJSONObject("epic")?.let { e -> EpicOptions(eos = e.optBoolean("eos", true), offline = e.optBoolean("offline", false)) } ?: EpicOptions(), + cloud = o.optBoolean("cloud", true), + ) + } + + /** Relative, no drive letter, no step up: an absolute or escaping path is refused, not trimmed into shape. */ + private fun relativeInside(path: String): Boolean = + path.isNotEmpty() && !path.startsWith("/") && !path.contains(":") && path.split('/').none { it == ".." || it.isEmpty() } + } +} + +/** + * An Epic game's launch choices. [eos]: pass a fresh exchange code (Epic sign-in). [offline]: start + * with the Epic identity arguments only, no code - for a game that will not start when sign-in fails. + */ +data class EpicOptions(val eos: Boolean = true, val offline: Boolean = false) { + /** Whether a launch asks for a code at all. */ + val wantsCode: Boolean get() = eos && !offline +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreInstallRoot.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreInstallRoot.kt new file mode 100644 index 0000000000..468f924198 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreInstallRoot.kt @@ -0,0 +1,158 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.os.StatFs +import android.os.storage.StorageManager +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.session.GameStorage +import java.io.File +import java.util.zip.CRC32 + +/** + * Where store games are installed: `//`. + * + * The default root is the app's own internal storage - the runtime's tree, where the folder sits at + * /root/Games/Stores and needs no bind - whatever the session's Game storage setting says. An SD + * card the app may write (its app folder, `GameStorage.options`) is offered as a second root at + * Install time and never chosen silently. Every root is scanned, so a game stays in the library + * wherever it went. A card that is the Steam library is already bound (`/mnt/droiddeck-sd`); any + * other card's root is bound at a fixed place of its own (`/mnt/droiddeck-stores/<volume>`), so a + * shortcut's path - and with it its appid - stays the same from one session to the next. + */ +object StoreInstallRoot { + private const val INTERNAL = "root/Games/Stores" + private const val ON_VOLUME = "Games" + const val GUEST_EXTERNAL = "/mnt/droiddeck-stores" + + /** One place a game can be installed: the folder, its name for the dialog, and whether it is a card. */ + 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 } + } + + /** The internal root: always there, always the default. */ + fun internalRoot(context: Context): File = File(LinuxRuntime.rootDir(context), INTERNAL) + + /** The roots a game can go to right now: internal first, then every writable card. */ + fun targets(context: Context): List<Target> { + val internal = Target(internalRoot(context), context.getString(com.droiddeck.launcher.R.string.games_internal), removable = false) + val cards = GameStorage.options(context).map { Target(File(it.path, ON_VOLUME), it.name, removable = true) } + return listOf(internal) + cards + } + + /** The default root, when nothing asks: internal storage. */ + fun installRoot(context: Context): File = internalRoot(context) + + /** Every root that may hold store games: the internal one, every card's, and the Steam library's (older installs). */ + fun roots(context: Context): List<File> { + val cards = GameStorage.options(context).map { File(it.path, ON_VOLUME) } + val library = GameStorage.effective(context)?.let { File(it.path, ON_VOLUME) } + return (listOf(internalRoot(context)) + cards + listOfNotNull(library)).distinctBy { it.absolutePath } + } + + /** + * The card roots that need a bind of their own, with where the session sees them. A root under + * the Steam library is left out: the library's bind already covers it, and the games there keep + * the guest path (and appid) they were registered with. + */ + fun externalRoots(context: Context): List<Pair<File, String>> { + val library = GameStorage.effective(context)?.path + return roots(context) + .filter { !isInternal(context, it) && (library == null || !it.absolutePath.startsWith("$library/")) } + .map { it to guestFor(context, it) } + } + + /** A card root's guest path, keyed on the volume (its UUID, else a hash of the path), so it is the same across sessions. */ + private fun guestFor(context: Context, root: File): String { + val sm = context.getSystemService(StorageManager::class.java) + val uuid = runCatching { sm?.getStorageVolume(root)?.uuid }.getOrNull()?.takeIf { it.isNotBlank() }?.replace(Regex("[^A-Za-z0-9-]"), "") + val key = uuid ?: "%08x".format(CRC32().apply { update(root.absolutePath.toByteArray()) }.value) + return "$GUEST_EXTERNAL/$key" + } + + fun isInternal(context: Context, root: File): Boolean = root.absolutePath == internalRoot(context).absolutePath + + /** Where a host path under a card root appears inside the session, or null when it is not under one. */ + fun externalGuestPath(context: Context, host: File): String? { + val path = host.absolutePath + for ((root, guest) in externalRoots(context)) { + val dir = root.absolutePath + if (path == dir) return guest + if (path.startsWith("$dir/")) return guest + "/" + path.removePrefix("$dir/") + } + return null + } + + /** The target a game folder sits under, for the page and the Downloads row; internal when unknown. */ + fun labelFor(context: Context, folder: File): String { + val path = folder.absolutePath + for (t in targets(context)) if (path.startsWith(t.root.absolutePath + "/")) return t.label + return context.getString(com.droiddeck.launcher.R.string.games_internal) + } + + fun isRemovable(context: Context, folder: File): Boolean = !folder.absolutePath.startsWith(internalRoot(context).absolutePath + "/") + + fun storeDir(root: File, store: Store): File = File(root, store.shortLabel) + + /** The game folders under every root, by store: what AddedGames scans in addition to the user's folders. */ + fun gameFolders(context: Context): List<File> = roots(context).flatMap { root -> + Store.entries.flatMap { store -> + storeDir(root, store).listFiles { f -> f.isDirectory }?.sortedBy { it.name.lowercase() } ?: emptyList() + } + } + + /** + * The folder a game installs into under [root]. An existing folder wins over a fresh one, so a + * repair, an update or a resume lands on what is already there: one with this game's sidecar + * anywhere, else one under any root with this game's folder name and no finished sidecar (an + * install an earlier build left without one, or that stopped before writing it). + */ + fun folderFor(context: Context, store: Store, id: String, title: String, root: File = installRoot(context)): File = + existingFolder(context, store, id, title) ?: File(storeDir(root, store), folderName(title, id)) + + /** The folder an earlier install of this game left, finished or not; null when there is none. */ + fun existingFolder(context: Context, store: Store, id: String, title: String): File? { + val all = roots(context).flatMap { r -> storeDir(r, store).listFiles { f -> f.isDirectory }?.toList().orEmpty() } + all.firstOrNull { folder -> StoreGameSidecar.read(folder)?.let { it.store == store && it.id == id } == true }?.let { return it } + val name = folderName(title, id) + return all.firstOrNull { it.name == name && StoreGameSidecar.read(it)?.isInstalled != true } + } + + /** An unfinished install: [id] from its `installing` sidecar, null when it has none (matched by folder name then). */ + class Unfinished(val store: Store, val id: String?, val folder: File) + + /** + * A folder under a store root is a game only once its sidecar says installed. Without one it is + * an install that never finished (stopped, killed, or from a build that wrote the sidecar last): + * not a Custom game, and not listed in Steam. + */ + fun isFinished(folder: File): Boolean = StoreGameSidecar.read(folder)?.isInstalled == true + + /** Every unfinished install under every root, for the Stores page's "Resume install". */ + fun unfinished(context: Context): List<Unfinished> = roots(context).flatMap { root -> + Store.entries.flatMap { store -> + storeDir(root, store).listFiles { f -> f.isDirectory }.orEmpty().mapNotNull { folder -> + val sidecar = StoreGameSidecar.read(folder) + when { + sidecar == null -> Unfinished(store, null, folder) + !sidecar.isInstalled -> Unfinished(store, sidecar.id, folder) + else -> null + } + } + } + } + + /** A scratch folder in the app's cache for an install's in-flight pieces, when the install itself is on a card. */ + fun scratchDir(context: Context, store: Store, id: String): File = + File(File(File(context.cacheDir, "stores"), store.id), id.replace(Regex("[^A-Za-z0-9._-]"), "_")) + + /** + * A folder name from a store title: letters, digits and a few safe marks, single spaces, at most + * 60 characters; the store id when nothing printable is left. The name is the shortcut's folder + * for ever, so it is chosen once at install and read back from the sidecar afterwards. + */ + fun folderName(title: String, id: String): String { + val cleaned = title.replace(Regex("[^A-Za-z0-9 ._'()\\-]"), " ").replace(Regex("\\s+"), " ").trim().trimEnd('.') + val cut = if (cleaned.length > 60) cleaned.take(60).trimEnd().trimEnd('.') else cleaned + return cut.ifEmpty { id.replace(Regex("[^A-Za-z0-9._-]"), "_").ifEmpty { "game" } } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt new file mode 100644 index 0000000000..91c0345742 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt @@ -0,0 +1,110 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.frontend.AddedGames +import com.droiddeck.launcher.frontend.LibraryCache +import com.droiddeck.launcher.frontend.Library +import java.io.File + +/** + * What happens around a store install so the rest of the app sees the game: the sidecar is + * written, the launcher .bat beside it when the game needs one, and Steam is told. + * + * Telling Steam is the same path an added folder takes: the game is one more folder the + * [AddedGames] scan finds, the scan's listing (`session/added-games.json`) is what the runtime's + * shortcuts writer reads at the client's next start, and here the listing is rewritten at once so + * a Steam client that restarts inside the running session already has it. When the client is up, + * [SteamLiveShortcuts] adds the shortcut over the client's DevTools port as well, so the game shows + * without a restart; that part is best-effort. + */ +object StoreInstalls { + private const val TAG = "StoreInstalls" + + /** + * Marks [folder] as an install under way before anything is fetched: a sidecar in state + * `installing` with the store, id and title. Whatever stops the install from here on - a + * failure, a cancel, the process killed - leaves a folder the scan skips and the Stores page + * offers to resume, never a half-written Custom game. A finished install being repaired or + * updated keeps its finished sidecar, so the game stays in Steam meanwhile. + */ + fun begin(folder: File, store: Store, id: String, title: String, cover: String?, hero: String?) { + if (StoreGameSidecar.read(folder)?.isInstalled == true) return + StoreGameSidecar(store, id, title, exe = "", cover = cover, hero = hero, state = StoreGameSidecar.STATE_INSTALLING).write(folder) + } + + /** + * Finishes an install: writes the launcher and the finished sidecar, then the art, then + * registers the game. Runs on the caller's worker thread. The launcher and sidecar writes + * throw, so a failure there fails the download with its message; the art and the Steam side + * are best-effort and only logged. + */ + fun complete(context: Context, folder: File, sidecar: StoreGameSidecar, onStep: ((Int, Int) -> Unit)? = null) { + val app = context.applicationContext + onStep?.invoke(0, STEPS) + // The user's launch choices survive an update or a repair. + val before = StoreGameSidecar.read(folder) + val finished = StoreLaunch.writeLauncher(folder, sidecar.copy(state = StoreGameSidecar.STATE_INSTALLED, epic = before?.epic ?: sidecar.epic, cloud = before?.cloud ?: sidecar.cloud)) + finished.write(folder) + if (StoreGameSidecar.read(folder)?.isInstalled != true) throw java.io.IOException("the install record could not be written in ${folder.name}") + onStep?.invoke(1, STEPS) + // The art before the listing, so the listing already carries it for the client's grid. + try { StoreArt.fetchInto(folder, finished) } catch (e: Exception) { Log.w(TAG, "art for ${folder.name}: ${e.message}") } + onStep?.invoke(2, STEPS) + register(app) + onStep?.invoke(STEPS, STEPS) + } + + /** The finishing steps [complete] reports: the record and launcher, the art, Steam. */ + private const val STEPS = 3 + + /** Rewrites the session's listing from a fresh scan and, with a client running, adds live. */ + fun register(context: Context) { + val app = context.applicationContext + try { + val games = AddedGames.scan(app) + AddedGames.writeListing(app, games) + LibraryCache.save(app, Library.launchableGames(app)) + SteamLiveShortcuts.sync(app, games) + } catch (e: Exception) { + Log.w(TAG, "registration: ${e.message}") + } + } + + /** + * Removes a store game: its folder with the sidecar, then the shortcut (the listing without + * it makes the writer drop the entry; the live client is asked too). Blocking. + */ + fun uninstall(context: Context, game: InstalledStoreGame) { + val app = context.applicationContext + val appId = AddedGames.scan(app).firstOrNull { it.folder.absolutePath == game.folder.absolutePath }?.appId + deleteTree(game.folder) + // The scratch cache an install on a card may have left in the app's cache. + deleteTree(StoreInstallRoot.scratchDir(app, game.sidecar.store, game.sidecar.id)) + register(app) + if (appId != null) SteamLiveShortcuts.remove(app, appId) + StoresState.logLine("uninstalled \"${game.sidecar.title}\"") + } + + /** + * What Cancel does: everything the install wrote goes - the game folder with its sidecar and + * the caches in [caches] - unless the folder holds a finished install being repaired or updated; + * then only the caches go and the working game stays. Off the caller's thread; the library + * follows when it is done, so the game reads Install again. + */ + fun discard(context: Context, folder: File, caches: List<File>) { + val app = context.applicationContext + Thread({ + val keep = StoreGameSidecar.read(folder)?.isInstalled == true + caches.forEach { deleteTree(it) } + if (!keep) deleteTree(folder) + StoresState.logLine(if (keep) "removed the partial update of ${folder.name}" else "removed ${folder.name}") + StoresState.notifyLibraryChanged(app) + }, "store-cancel-clean").start() + } + + fun deleteTree(dir: File) { + dir.listFiles()?.forEach { if (it.isDirectory) deleteTree(it) else it.delete() } + dir.delete() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt new file mode 100644 index 0000000000..4977baefcf --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt @@ -0,0 +1,160 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.frontend.Library +import java.io.File + +/** + * How a store game is started inside the session. The shortcut Steam gets can only name an exe, + * so when a game needs arguments or environment the install writes a small `.droiddeck-launch.bat` + * beside it and the shortcut runs that: Proton's steam.exe shim hands a .bat to Wine's cmd, the + * script sets the variables, steps into the exe's folder and starts it, and waits for it as a + * batch file does, so Steam sees the game running. + * + * Epic's online sign-in is a fresh exchange code per launch (it expires in minutes), which no fixed + * command line can carry: [prepare] mints one right before a launch and leaves it in + * `.droiddeck-epic-code`; the script reads the file, deletes it and passes the AUTH arguments. With + * no file the game starts in its offline identity mode, which most EOS titles accept. + */ +object StoreLaunch { + private const val TAG = "StoreLaunch" + const val LAUNCHER = ".droiddeck-launch.bat" + const val EPIC_CODE = ".droiddeck-epic-code" + + /** + * Writes the launcher for [sidecar] when it needs one (arguments, environment, or an Epic + * sign-in), removes a stale one otherwise, and returns the sidecar with [StoreGameSidecar.launcher] set. + */ + fun writeLauncher(folder: File, sidecar: StoreGameSidecar): StoreGameSidecar { + val needs = sidecar.args.isNotEmpty() || sidecar.env.isNotEmpty() || sidecar.store == Store.EPIC + val file = File(folder, LAUNCHER) + if (!needs) { file.delete(); return sidecar.copy(launcher = null) } + file.writeText(launcherText(sidecar)) + return sidecar.copy(launcher = LAUNCHER) + } + + /** The batch file's text; pure, so it can be checked without a folder. CRLF line ends, as cmd expects. */ + fun launcherText(sidecar: StoreGameSidecar): String { + val exe = sidecar.exe.replace('/', '\\') + val dir = exe.substringBeforeLast('\\', "") + val lines = ArrayList<String>() + lines += "@echo off" + lines += "setlocal" + lines += "rem Written by DroidDeck for ${sidecar.store.label}: ${sidecar.title}. Steam's shortcut runs this file." + lines += "cd /d \"%~dp0$dir\"" + for ((k, v) in sidecar.env) if (ENV_NAME.matches(k)) lines += "set \"$k=${v.replace("\"", "")}\"" + val args = sidecar.args.joinToString(" ") { quoteArg(it) } + val run = "\"%~dp0$exe\"" + (if (args.isNotEmpty()) " $args" else "") + if (sidecar.store == Store.EPIC) { + // The one-shot code reaches the game on its command line only. cmd expands %DD_X% when it + // parses the last line, so the variable is cleared on that same line before the game + // starts: nothing of it is left in the environment the game inherits (and dumps). + lines += "set \"$CODE_VAR=\"" + lines += "if exist \"%~dp0$EPIC_CODE\" set /p $CODE_VAR=<\"%~dp0$EPIC_CODE\"" + lines += "if exist \"%~dp0$EPIC_CODE\" del \"%~dp0$EPIC_CODE\"" + lines += "if defined $CODE_VAR goto signed" + lines += run + lines += "goto :eof" + lines += ":signed" + lines += "set \"$CODE_VAR=\" & $run -AUTH_LOGIN=unused -AUTH_PASSWORD=%$CODE_VAR% -AUTH_TYPE=exchangecode" + } else { + lines += run + } + return lines.joinToString("\r\n") + "\r\n" + } + + private val ENV_NAME = Regex("[A-Za-z_][A-Za-z0-9_]*") + + /** An argument on cmd's line: quoted when it has a space and is not already quoted; control characters dropped. */ + fun quoteArg(arg: String): String { + val clean = arg.filter { it >= ' ' && it != '\u007f' } + return if (' ' in clean && !clean.contains('"')) "\"$clean\"" else clean + } + + /** + * Everything a launch needs done first, then [onReady] on the main thread: for an Epic game a + * fresh exchange code written beside its launcher (a few seconds at most; the launch goes + * ahead without one when the store cannot be reached). Other stores need nothing. + */ + /** The launcher's variable for the one-shot code; a plain name, cleared before the game starts. */ + private const val CODE_VAR = "DD_X" + + /** + * Rewrites a game's launcher when its text is not what this build writes - an install from an + * earlier build keeps its old .bat otherwise. Called before every Epic launch. + */ + fun refreshLauncher(folder: File, sidecar: StoreGameSidecar) { + if (sidecar.launcher != LAUNCHER) return + val file = File(folder, LAUNCHER) + val text = launcherText(sidecar) + if (runCatching { file.readText() }.getOrNull() != text) { + file.writeText(text) + Log.i(TAG, "launcher rewritten for ${sidecar.id}") + } + } + + fun prepare(context: Context, store: Store, id: String, onReady: () -> Unit) { + if (store != Store.EPIC) { onReady(); return } + val app = context.applicationContext + Thread({ + epicCode(app, id) + StoresState.post(onReady) + }, "stores-launch-prep").start() + } + + /** What [epicCode] did, for the guest's request and the log: whether a code was written, and why not. */ + class CodeResult(val written: Boolean, val reason: String) + + /** + * Mints an Epic exchange code for the installed game [id] and writes it as [EPIC_CODE] into the + * very folder its sidecar and launcher are in, where the launcher .bat reads and deletes it. + * Blocking (network). Logs `epic launch id=<id> code=yes|no reason=...` - never the code. Every + * launch path comes here: the Games tab and Stores before they launch, and the compat tool for + * any launch, the Steam client's own Play button included ([StoreLaunchRequests]). + */ + fun epicCode(context: Context, id: String): CodeResult { + val app = context.applicationContext + val result = try { + val folder = StoreInstallRoot.gameFolders(app).firstOrNull { f -> StoreGameSidecar.read(f)?.let { it.store == Store.EPIC && it.id == id } == true } + val sidecar = folder?.let { StoreGameSidecar.read(it) } + sidecar?.let { refreshLauncher(folder, it) } + val options = sidecar?.epic + if (folder == null) CodeResult(false, "not-installed") + else if (options != null && !options.wantsCode) { + File(folder, EPIC_CODE).delete() + CodeResult(false, if (options.offline) "offline" else "eos-off") + } else { + val file = File(folder, EPIC_CODE) + file.delete() + val support = StoresState.backend(Store.EPIC) as? EpicLaunchSupport + val code = support?.exchangeCode(app) + when { + support == null -> CodeResult(false, "no-backend") + code != null -> { file.writeText(code); CodeResult(true, "ok") } + StoreAccounts.signedInAs(app, Store.EPIC) == null -> CodeResult(false, "signed-out") + support.signInExpired(app) -> { StoresState.markSignInExpired(Store.EPIC); CodeResult(false, "sign-in-expired") } + else -> CodeResult(false, "exchange-failed") + } + } + } catch (e: Exception) { + CodeResult(false, "error-" + e.javaClass.simpleName) + } + Log.i(TAG, "epic launch id=$id code=${if (result.written) "yes" else "no"} reason=${result.reason}") + return result + } + + fun prepare(context: Context, game: Library.SteamGame, onReady: () -> Unit) { + val store = Store.byId(game.source) + val id = game.storeId + if (store == null || id == null) { onReady(); return } + prepare(context, store, id, onReady) + } +} + +/** What the Epic backend adds for launches: a short-lived exchange code for the signed-in account. */ +interface EpicLaunchSupport { + fun exchangeCode(context: Context): String? + /** True when the stored sign-in can no longer be refreshed: the user has to sign in again. */ + fun signInExpired(context: Context): Boolean +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt new file mode 100644 index 0000000000..ee8c623627 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt @@ -0,0 +1,108 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.os.FileObserver +import android.util.Log +import com.droiddeck.launcher.runtime.LinuxRuntime +import java.io.File +import org.json.JSONObject + +/** + * The app's half of droiddeck-store-launch: the compat tool asks, before Proton starts a store + * game, for what the store needs at launch, through files under the session root's `stores/` + * (`req/<id>.json` in, `resp/<id>.json` out, both written tmp + rename). Today that is an Epic + * game's exchange code, which [StoreLaunch.epicCode] writes into the game's own folder; the answer + * says only whether it did and why not. Watched while a session runs. + */ +object StoreLaunchRequests { + private const val TAG = "StoreLaunchRequests" + private val lock = Any() + private var observer: FileObserver? = null + + fun dir(context: Context) = File(LinuxRuntime.sessionRoot(context), "stores") + + /** Before each session: nothing of the last one's requests; then watch for new ones. */ + fun start(context: Context) { + val app = context.applicationContext + synchronized(lock) { + observer?.stopWatching() + val root = dir(app) + root.deleteRecursively() + val req = File(root, "req").apply { mkdirs() } + File(root, "resp").mkdirs() + @Suppress("DEPRECATION") + observer = object : FileObserver(req.path, MOVED_TO or CLOSE_WRITE) { + override fun onEvent(event: Int, path: String?) { + if (path == null || !path.endsWith(".json")) return + val file = File(req, path) + Thread({ answer(app, file) }, "store-launch-request").start() + } + }.also { it.startWatching() } + } + } + + fun stop() { + synchronized(lock) { + observer?.stopWatching() + observer = null + } + } + + private fun answer(app: Context, file: File) { + val request = try { + JSONObject(file.readText()).also { file.delete() } + } catch (e: Exception) { + return // withdrawn already, or not complete yet (the rename brings it again) + } + val reply = when (request.optString("op")) { + "epic-code" -> { + val id = request.optString("id") + if (id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-id") + else { + val code = StoreLaunch.epicCode(app, id) + JSONObject().put("ok", true).put("code", code.written).put("reason", code.reason) + } + } + // Cloud saves: down before Proton starts (the wrapper waits a bounded time), up after the + // game exits (answered at once; the upload runs on without holding Steam). + "cloud-down" -> { + val store = Store.byId(request.optString("store")) + val id = request.optString("id") + if (store == null || id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-game") + else { + // Marked before the game runs: whatever ends it, an upload follows (CloudSaves.uploadDirty). + CloudSaves.markDirty(app, store, id) + CloudSaves.downloadOrDefer(app, store, id).let { JSONObject().put("ok", it.ok).put("result", it.result).put("files", it.files).put("reason", it.reason) } + } + } + "cloud-up" -> { + val store = Store.byId(request.optString("store")) + val id = request.optString("id") + if (store != null && id.isNotEmpty()) Thread({ CloudSaves.uploadDirty(app, store, id, "exit") }, "cloud-up").start() + JSONObject().put("ok", true) + } + // A browser a game opens inside the session (an EOS device sign-in, a store page): + // http(s) only, handed to Android's browser. + "open-url" -> { + val url = request.optString("url") + if (!(url.startsWith("https://") || url.startsWith("http://"))) JSONObject().put("ok", false).put("reason", "not-http") + else try { + app.startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url)).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)) + Log.i(TAG, "open-url: " + StoreLog.redactUrl(url)) + JSONObject().put("ok", true) + } catch (e: Exception) { + JSONObject().put("ok", false).put("reason", e.javaClass.simpleName) + } + } + else -> JSONObject().put("ok", false).put("reason", "unknown-op") + } + try { + val resp = File(dir(app), "resp").apply { mkdirs() } + val tmp = File(resp, file.name + ".tmp") + tmp.writeText(reply.toString()) + tmp.renameTo(File(resp, file.name)) + } catch (e: Exception) { + Log.w(TAG, "could not answer ${file.name}: ${e.message}") + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLogFiles.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLogFiles.kt new file mode 100644 index 0000000000..eb4a14fc2c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLogFiles.kt @@ -0,0 +1,79 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Log +import java.io.File +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.concurrent.Executors + +/** + * The stores' log on disk, app-private: `filesDir/logs/stores/stores-<yyyy-MM-dd>.log` (beside the + * session folders, where every log lives), one file a + * day, the last [KEEP_DAYS] kept, each capped near [MAX_BYTES] (the full one moves to `.1`). Lines + * arrive already redacted ([StoreLog.redactLine]). Written on one background thread, in order. The + * session's Share logs zip carries these files, so they can be attached to a report when asked. + */ +object StoreLogFiles { + private const val TAG = "StoreLogFiles" + private const val KEEP_DAYS = 7 + private const val MAX_BYTES = 2L * 1024 * 1024 + private val DAY = SimpleDateFormat("yyyy-MM-dd", Locale.US) + private val NAME = Regex("""stores-(\d{4}-\d{2}-\d{2})\.log(\.1)?""") + private val writer = Executors.newSingleThreadExecutor { r -> Thread(r, "store-log").apply { isDaemon = true } } + + fun dir(context: Context): File = File(context.applicationContext.filesDir, "logs/stores") + + private val pending = java.util.concurrent.ConcurrentLinkedQueue<String>() + private val flushQueued = java.util.concurrent.atomic.AtomicBoolean(false) + private val flusher = java.util.concurrent.Executors.newSingleThreadScheduledExecutor { r -> + Thread({ android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); r.run() }, "store-log-flush").apply { isDaemon = true } + } + + /** + * Queues one stamped line for today's file. Any thread; nothing touches the disk here. Lines are + * written in batches, at most every half second, on [writer] in order. + */ + fun append(context: Context, line: String) { + val app = context.applicationContext + pending.add(line) + if (flushQueued.compareAndSet(false, true)) { + flusher.schedule({ flushQueued.set(false); writer.execute { flush(app) } }, 500, java.util.concurrent.TimeUnit.MILLISECONDS) + } + } + + private fun flush(app: Context) { + if (pending.isEmpty()) return + val batch = StringBuilder() + while (true) { val l = pending.poll() ?: break; batch.append(l).append('\n') } + try { + val dir = dir(app).apply { mkdirs() } + val file = File(dir, "stores-${synchronized(DAY) { DAY.format(Date()) }}.log") + if (file.length() > MAX_BYTES) { + val old = File(dir, file.name + ".1") + old.delete() + file.renameTo(old) + } + file.appendText(batch.toString()) + } catch (e: Exception) { + Log.w(TAG, "could not write the stores log: ${e.message}") + } + } + + /** Drops the files older than [KEEP_DAYS] days. Called as the app starts. */ + fun prune(context: Context) { + val app = context.applicationContext + writer.execute { + val cutoff = synchronized(DAY) { DAY.format(Date(System.currentTimeMillis() - (KEEP_DAYS - 1) * 86_400_000L)) } + dir(app).listFiles()?.forEach { f -> + val day = NAME.matchEntire(f.name)?.groupValues?.get(1) ?: return@forEach + if (day < cutoff) f.delete() + } + } + } + + /** The files there are, oldest first, for a log share. */ + fun files(context: Context): List<File> = + dir(context).listFiles { f -> f.isFile && NAME.matches(f.name) }?.sortedBy { it.name }.orEmpty() +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt new file mode 100644 index 0000000000..b7ffa3fbd1 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt @@ -0,0 +1,308 @@ +package com.droiddeck.launcher.stores + +import android.annotation.SuppressLint +import android.graphics.Color +import android.net.Uri +import android.net.http.SslError +import android.os.Bundle +import android.os.Message +import android.util.Log +import android.util.TypedValue +import android.view.Gravity +import android.view.View +import android.view.ViewGroup +import android.webkit.CookieManager +import android.webkit.SslErrorHandler +import android.webkit.WebChromeClient +import android.webkit.WebResourceError +import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView +import android.webkit.WebViewClient +import android.widget.Button +import android.widget.FrameLayout +import android.widget.ImageButton +import android.widget.LinearLayout +import android.widget.ProgressBar +import android.widget.TextView +import androidx.activity.ComponentActivity +import androidx.activity.OnBackPressedCallback +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.gog.GogAuth +import java.util.concurrent.atomic.AtomicBoolean + +/** + * A store's own sign-in page in a WebView, one activity for the three stores: GOG's OAuth page + * (the token comes back in the redirect's fragment), Epic's web login (an authorization code on + * a JSON page), Amazon's device sign-in (a PKCE code on the return URL). Plain views, as the + * proven GOG flow was: the page is rendered with the store's own client identity, and a social + * sign-in hop to Google / Facebook / Apple runs under a real browser identity in a popup, since + * those providers refuse an embedded browser. + * + * Nothing from the page is logged but redacted URLs and key names; the tokens go straight to + * StoreAccounts' private file and the activity finishes. + */ +class StoreLoginActivity : ComponentActivity() { + companion object { + private const val TAG = "StoreLogin" + const val EXTRA_STORE = "store" + private const val KEY_STATE = "oauth_state" + private const val CHROME_UA = "Mozilla/5.0 (Linux; Android 14; SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.103 Mobile Safari/537.36" + + fun generateState(): String { + val bytes = ByteArray(24) + java.security.SecureRandom().nextBytes(bytes) + return android.util.Base64.encodeToString(bytes, android.util.Base64.URL_SAFE or android.util.Base64.NO_WRAP or android.util.Base64.NO_PADDING) + } + } + + /** What one store's flow decides: the page to open, the identity to open it with, and the redirect that ends it. */ + internal interface Flow { + val startUrl: String + val userAgent: String + /** True when [url] is the flow's end; the activity then calls [finish]. */ + fun isRedirect(url: String): Boolean + /** Runs off the main thread with the redirect; returns null on success, else a message to show. */ + fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? + /** Whether a JSON page at the redirect must be read out of the document instead of the URL (Epic). */ + val readsPage: Boolean get() = false + } + + private var store: Store? = null + private var flow: Flow? = null + private var webViewRef: WebView? = null + private var popupWebView: WebView? = null + private lateinit var contentHost: FrameLayout + private lateinit var popupHost: FrameLayout + private lateinit var progressBar: ProgressBar + private lateinit var errorPanel: LinearLayout + private lateinit var errorText: TextView + private lateinit var titleText: TextView + private var oauthState: String? = null + private val captured = AtomicBoolean(false) + + private fun dp(value: Int): Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value.toFloat(), resources.displayMetrics).toInt() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + store = Store.byId(intent.getStringExtra(EXTRA_STORE)) + oauthState = savedInstanceState?.getString(KEY_STATE) ?: generateState() + flow = store?.let { LoginFlows.forStore(it, oauthState!!) } + if (flow == null) { finish(); return } + setContentView(buildChrome()) + val webView = newWebView(flow!!.userAgent, isPopup = false) + webViewRef = webView + contentHost.addView(webView, 0, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + when { + popupWebView != null -> dismissPopup() + webViewRef?.canGoBack() == true -> webViewRef?.goBack() + else -> finish() + } + } + }) + webView.loadUrl(flow!!.startUrl) + } + + private fun buildChrome(): View { + val root = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL; setBackgroundColor(0xFF0A0B0D.toInt()) } + val bar = LinearLayout(this).apply { + orientation = LinearLayout.HORIZONTAL; gravity = Gravity.CENTER_VERTICAL; setBackgroundColor(0xFF121417.toInt()) + layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(48)) + } + titleText = TextView(this).apply { + text = getString(R.string.stores_login_title, store?.label ?: "") + setTextColor(0xFFF2F4F7.toInt()); textSize = 16f; setPadding(dp(16), 0, dp(8), 0) + layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f) + } + val reload = ImageButton(this).apply { + setImageResource(android.R.drawable.ic_menu_rotate); setBackgroundColor(Color.TRANSPARENT) + contentDescription = getString(R.string.stores_login_reload); layoutParams = LinearLayout.LayoutParams(dp(48), dp(48)) + setOnClickListener { reloadLogin() } + } + val close = ImageButton(this).apply { + setImageResource(android.R.drawable.ic_menu_close_clear_cancel); setBackgroundColor(Color.TRANSPARENT) + contentDescription = getString(R.string.stores_login_close); layoutParams = LinearLayout.LayoutParams(dp(48), dp(48)) + setOnClickListener { finish() } + } + bar.addView(titleText); bar.addView(reload); bar.addView(close) + progressBar = ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal).apply { + max = 100; isIndeterminate = false + layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(3)) + } + contentHost = FrameLayout(this).apply { setBackgroundColor(0xFF0A0B0D.toInt()); layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1f) } + popupHost = FrameLayout(this).apply { setBackgroundColor(0xFF0A0B0D.toInt()); visibility = View.GONE } + contentHost.addView(popupHost, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + errorPanel = LinearLayout(this).apply { + orientation = LinearLayout.VERTICAL; gravity = Gravity.CENTER; setBackgroundColor(0xFF0A0B0D.toInt()) + setPadding(dp(24), dp(24), dp(24), dp(24)); visibility = View.GONE + } + errorText = TextView(this).apply { setTextColor(0xFFF2F4F7.toInt()); textSize = 15f; gravity = Gravity.CENTER } + val retry = Button(this).apply { text = getString(R.string.stores_login_retry); setOnClickListener { reloadLogin() } } + errorPanel.addView(errorText) + errorPanel.addView(retry, LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT).apply { topMargin = dp(16) }) + contentHost.addView(errorPanel, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + root.addView(bar); root.addView(progressBar); root.addView(contentHost) + return root + } + + private fun showError(message: String) { + runOnUiThread { errorText.text = message; errorPanel.visibility = View.VISIBLE; errorPanel.bringToFront(); progressBar.visibility = View.GONE } + } + + private fun clearError() { runOnUiThread { errorPanel.visibility = View.GONE } } + + private fun reloadLogin() { + dismissPopup(); clearError() + captured.set(false) + val fresh = generateState() + oauthState = fresh + flow = store?.let { LoginFlows.forStore(it, fresh) } ?: return + webViewRef?.let { it.settings.userAgentString = flow!!.userAgent; it.loadUrl(flow!!.startUrl) } + } + + @SuppressLint("SetJavaScriptEnabled") + private fun newWebView(userAgent: String, isPopup: Boolean): WebView = WebView(this).apply { + setBackgroundColor(0xFF0A0B0D.toInt()) + settings.javaScriptEnabled = true + settings.domStorageEnabled = true + settings.userAgentString = userAgent + // The stores' social-login buttons are window.open() popups; without these the call is dropped silently. + settings.setSupportMultipleWindows(true) + settings.javaScriptCanOpenWindowsAutomatically = true + if (isPopup) CookieManager.getInstance().setAcceptThirdPartyCookies(this, true) + webViewClient = Client(isPopup) + webChromeClient = Chrome(isPopup) + } + + private inner class Chrome(private val isPopup: Boolean) : WebChromeClient() { + override fun onProgressChanged(view: WebView?, newProgress: Int) { + progressBar.progress = newProgress + progressBar.visibility = if (newProgress in 1..99) View.VISIBLE else View.GONE + } + + override fun onCreateWindow(view: WebView?, isDialog: Boolean, isUserGesture: Boolean, resultMsg: Message?): Boolean { + if (resultMsg == null) return false + dismissPopup() + val child = newWebView(CHROME_UA, isPopup = true) + popupWebView = child + popupHost.addView(child, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) + popupHost.visibility = View.VISIBLE + popupHost.bringToFront() + titleText.text = getString(R.string.stores_login_provider_title) + (resultMsg.obj as WebView.WebViewTransport).webView = child + resultMsg.sendToTarget() + return true + } + + override fun onCloseWindow(window: WebView?) { if (isPopup || window === popupWebView) dismissPopup() } + } + + /** Deferred: destroying a WebView from inside one of its own callbacks crashes chromium. */ + private fun dismissPopup() { + val child = popupWebView ?: return + popupWebView = null + popupHost.post { destroyPopupView(child) } + } + + private fun destroyPopupView(child: WebView) { + child.stopLoading(); child.webChromeClient = null; child.loadUrl("about:blank") + popupHost.removeView(child); child.destroy() + if (popupWebView == null) { + popupHost.visibility = View.GONE + if (!isFinishing) titleText.text = getString(R.string.stores_login_title, store?.label ?: "") + } + } + + private inner class Client(private val isPopup: Boolean) : WebViewClient() { + override fun onPageStarted(view: WebView?, url: String?, favicon: android.graphics.Bitmap?) { + Log.d(TAG, "page[popup=$isPopup]: ${StoreLog.redactUrl(url)}") + // Amazon's return URL lands as a page load rather than a navigation we can intercept. + if (url != null && view != null && flow?.isRedirect(url) == true && flow?.readsPage != true) complete(view, url) + } + + override fun onPageFinished(view: WebView?, url: String?) { + // Epic's redirect is a JSON page: the code is in the document, read once it is loaded. + if (url != null && view != null && flow?.readsPage == true && flow?.isRedirect(url) == true) complete(view, url) + } + + override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) { + if (request?.isForMainFrame == true) showError(getString(R.string.stores_login_error_network, error?.description ?: "")) + } + + override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) { + val status = errorResponse?.statusCode ?: 0 + if (request?.isForMainFrame != true) return + if (GogAuth.isIdentityProviderHost(request.url?.host) && (status == 403 || status == 400)) showError(getString(R.string.stores_login_error_idp_blocked)) + else showError(getString(R.string.stores_login_error_http, status)) + } + + override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) { + handler?.cancel() + showError(getString(R.string.stores_login_error_ssl)) + } + + override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean = route(view, request.url) + + @Suppress("DEPRECATION") + override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean = route(view, Uri.parse(url)) + + private fun route(view: WebView, uri: Uri): Boolean { + val f = flow ?: return false + val url = uri.toString() + if (f.isRedirect(url) && !f.readsPage) { complete(view, url); return true } + // A social sign-in hop that stays in the main view: the provider gets a browser identity, + // and the store gets its own identity back afterwards (a plain Chrome UA makes GOG's + // page serve a form that never renders in a WebView). + if (!isPopup && store == Store.GOG) { + if (GogAuth.isGogHost(uri.host) && view.settings.userAgentString != f.userAgent) { view.settings.userAgentString = f.userAgent; return false } + if (GogAuth.isIdentityProviderHost(uri.host) && view.settings.userAgentString != CHROME_UA) { + view.settings.userAgentString = CHROME_UA + CookieManager.getInstance().setAcceptThirdPartyCookies(view, true) + view.loadUrl(url) + return true + } + } + return false + } + } + + private fun complete(view: WebView, url: String) { + if (!captured.compareAndSet(false, true)) return + val f = flow ?: return + dismissPopup() + runOnUiThread { + clearError() + titleText.text = getString(R.string.stores_login_finishing) + if (!f.readsPage) view.stopLoading() + } + if (f.readsPage) { + // The document's text, then the exchange off the main thread. + view.evaluateJavascript("(function(){ try { return document.body.innerText; } catch(e){ return ''; } })()") { json -> + Thread({ settle(f.finish(this, view, json ?: "")) }, "store-login").start() + } + } else Thread({ settle(f.finish(this, view, url)) }, "store-login").start() + } + + private fun settle(problem: String?) { + runOnUiThread { + if (problem == null) { StoresState.refresh(this); finish(); return@runOnUiThread } + captured.set(false) + if (!isFinishing && !isDestroyed) android.widget.Toast.makeText(this, problem, android.widget.Toast.LENGTH_LONG).show() + reloadLogin() + } + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + oauthState?.let { outState.putString(KEY_STATE, it) } + } + + override fun onDestroy() { + popupWebView?.let { popupWebView = null; destroyPopupView(it) } + webViewRef?.let { it.webChromeClient = null; (it.parent as? ViewGroup)?.removeView(it); it.destroy() } + webViewRef = null + super.onDestroy() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt new file mode 100644 index 0000000000..1a94c847aa --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt @@ -0,0 +1,164 @@ +package com.droiddeck.launcher.stores + +import android.util.Log +import java.io.BufferedReader +import java.io.InputStreamReader +import java.io.OutputStreamWriter +import java.net.HttpURLConnection +import java.net.URL + +/** + * The small HTTP the storefront catalogs and account calls need: a GET or a JSON POST with the + * right User-Agent, a bearer token when the call is signed in, and a body or null. Store URLs + * carry their authorization in the query string or the path, so a URL is only ever logged + * through [StoreLog.redactUrl]. Plain HttpURLConnection, like the rest of the app. + */ +internal object StoreNet { + private const val TAG = "StoreNet" + + /** A desktop browser: GOG's catalog and Epic's store GraphQL answer it; Epic's edge refuses the bare Java UA. */ + const val BROWSER_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0 Safari/537.36" + + fun get(url: String, bearer: String? = null, userAgent: String = BROWSER_UA, timeoutMs: Int = 20_000, headers: Map<String, String> = emptyMap()): String? { + var conn: HttpURLConnection? = null + return try { + conn = (URL(url).openConnection() as HttpURLConnection).apply { + connectTimeout = timeoutMs + readTimeout = timeoutMs + instanceFollowRedirects = true + setRequestProperty("User-Agent", userAgent) + setRequestProperty("Accept", "application/json, text/plain, */*") + if (bearer != null) setRequestProperty("Authorization", "Bearer $bearer") + headers.forEach { (k, v) -> setRequestProperty(k, v) } + } + val code = conn.responseCode + if (code !in 200..299) { Log.w(TAG, "GET $code ${StoreLog.redactUrl(url)}"); null } else readAll(conn) + } catch (e: Exception) { + Log.w(TAG, "GET failed ${StoreLog.redactUrl(url)}: ${e.javaClass.simpleName}") + null + } finally { + runCatching { conn?.disconnect() } + } + } + + fun postJson(url: String, body: String, bearer: String? = null, userAgent: String = BROWSER_UA, timeoutMs: Int = 25_000): String? { + var conn: HttpURLConnection? = null + return try { + conn = (URL(url).openConnection() as HttpURLConnection).apply { + connectTimeout = timeoutMs + readTimeout = timeoutMs + requestMethod = "POST" + doOutput = true + setRequestProperty("User-Agent", userAgent) + setRequestProperty("Content-Type", "application/json") + setRequestProperty("Accept", "application/json") + if (bearer != null) setRequestProperty("Authorization", "Bearer $bearer") + } + OutputStreamWriter(conn.outputStream, "UTF-8").use { it.write(body) } + val code = conn.responseCode + if (code !in 200..299) { Log.w(TAG, "POST $code ${StoreLog.redactUrl(url)}"); null } else readAll(conn) + } catch (e: Exception) { + Log.w(TAG, "POST failed ${StoreLog.redactUrl(url)}: ${e.javaClass.simpleName}") + null + } finally { + runCatching { conn?.disconnect() } + } + } + + /** + * A 600x900 poster for [title] from SteamGridDB by name, or "" - the lookup the GOG library and + * the Amazon poster backfill use, since those stores publish no tall art of their own. + */ + fun sgdbPoster(title: String): String = try { + val enc = java.net.URLEncoder.encode(title, "UTF-8") + val search = get("https://www.steamgriddb.com/api/v2/search/autocomplete/$enc", bearer = SGDB_KEY) + val data = search?.let { org.json.JSONObject(it).optJSONArray("data") } + if (data == null || data.length() == 0) "" else { + val gameId = data.getJSONObject(0).getInt("id") + val grids = get("https://www.steamgriddb.com/api/v2/grids/game/$gameId?dimensions=600x900&mimes=image/jpeg,image/png&limit=1", bearer = SGDB_KEY) + ?.let { org.json.JSONObject(it).optJSONArray("data") } + if (grids == null || grids.length() == 0) "" else grids.getJSONObject(0).optString("url", "") + } + } catch (_: Exception) { "" } + + private const val SGDB_KEY = "cf89227f12c773bb1117b6b109ae1659" + + private fun readAll(conn: HttpURLConnection): String { + val sb = StringBuilder() + BufferedReader(InputStreamReader(conn.inputStream, "UTF-8")).use { br -> + val buf = CharArray(8192) + var n: Int + while (br.read(buf).also { n = it } > 0) sb.append(buf, 0, n) + } + return sb.toString() + } +} + +/** + * Keeps credentials out of the logs. A store's CDN, manifest and token URLs carry their + * authorization in the query string (signed, time-limited tokens, `client_secret`, + * `refresh_token`) or in the authority's userinfo; one such line in logcat or a shared log hands a + * reader a live download. Every store URL that is written anywhere goes through [redactUrl] first. + */ +object StoreLog { + /** `scheme://host/path` only: query, fragment and `user:pass@` dropped; "[url]" when it cannot be parsed. */ + @JvmStatic + fun redactUrl(url: String?): String? { + if (url == null) return null + return try { + var out: String = url + val hash = out.indexOf('#') + if (hash >= 0) out = out.substring(0, hash) + val q = out.indexOf('?') + if (q >= 0) out = out.substring(0, q) + val scheme = out.indexOf("://") + if (scheme >= 0) { + val authStart = scheme + 3 + val pathStart = out.indexOf('/', authStart) + val authority = if (pathStart >= 0) out.substring(authStart, pathStart) else out.substring(authStart) + val at = authority.lastIndexOf('@') + if (at >= 0) out = out.substring(0, authStart) + authority.substring(at + 1) + (if (pathStart >= 0) out.substring(pathStart) else "") + } + out.ifEmpty { "[url]" } + } catch (e: Exception) { + "[url]" + } + } + + private val URL_PATTERN = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") + private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""") + private val SECRET = Regex("""(?i)(?<![A-Za-z0-9_])(__token__|f_token|hdnts|access_token|refresh_token|id_token|token)(["']?\s*[=:]\s*["']?)[^\s&;,"'}]+""") + private val CODE = Regex("""(?i)(?<![A-Za-z0-9_])(code|exchange_code|authorizationCode)(["']?\s*=\s*["']?)[^\s&;,"'}]+""") + private val AUTH_HEADER = Regex("""(?i)(authorization|cookie)(\s*[:=]\s*).*""") + + /** + * What any store line may say, for the Downloads log and logcat alike: every URL cut to + * `scheme://host/first-segment/…` (the segment kept only when it is a plain name - a GOG secure + * link carries its token in the path), no query, fragment or userinfo; and outside URLs, the + * values of token-like keys and of Authorization / Cookie headers blanked. + */ + @JvmStatic + fun redactLine(text: String): String { + var out = URL_PATTERN.replace(text) { m -> shortUrl(m.value) } + out = SECRET.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } + out = CODE.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } + out = AUTH_HEADER.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } + return out + } + + private fun shortUrl(url: String): String { + val schemeEnd = url.indexOf("://") + val scheme = url.substring(0, schemeEnd) + var rest = url.substring(schemeEnd + 3) + rest = rest.substringBefore('#').substringBefore('?') + val slash = rest.indexOf('/') + val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') + if (slash < 0) return "$scheme://$authority" + val path = rest.substring(slash + 1) + val first = path.substringBefore('/') + val more = path.length > first.length + if (first.isEmpty()) return "$scheme://$authority/" + val shown = if (SAFE_SEGMENT.matches(first)) first else "…" + return "$scheme://$authority/$shown" + if (more && shown != "…") "/…" else "" + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt new file mode 100644 index 0000000000..122b3778fb --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreSizes.kt @@ -0,0 +1,96 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.util.Log +import androidx.compose.runtime.mutableStateMapOf +import com.droiddeck.launcher.stores.amazon.AmazonApiClient +import com.droiddeck.launcher.stores.amazon.AmazonCredentialStore +import com.droiddeck.launcher.stores.amazon.AmazonManifest +import com.droiddeck.launcher.stores.amazon.AmazonPrefs +import com.droiddeck.launcher.stores.epic.EpicApiClient +import com.droiddeck.launcher.stores.epic.EpicCredentialStore +import com.droiddeck.launcher.stores.epic.EpicDownloadManager +import com.droiddeck.launcher.stores.epic.EpicInstallTags +import com.droiddeck.launcher.stores.epic.EpicPrefs +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.Executors + +/** + * Install sizes for owned Epic and Amazon games, whose library answers carry none: Epic's from the + * game's build manifest (the files this device installs), Amazon's from its download manifest. + * Asked for a card as it comes on screen, three at a time in the background, once per game - the + * answer is kept in the store's prefs (`size_<id>`, where the library reads it too). Until it is + * known the card shows nothing. + */ +object StoreSizes { + private const val TAG = "StoreSizes" + + /** Sizes found this run, by card key; the cards read them. */ + val known = mutableStateMapOf<String, Long>() + + private val asked = ConcurrentHashMap.newKeySet<String>() + private val pool = Executors.newFixedThreadPool(3, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("sizes")) + + fun size(item: CatalogItem): Long = known[item.key] ?: item.sizeBytes + + private val caches = ConcurrentHashMap<Store, SizeCache>() + private fun cache(context: Context, store: Store) = caches.getOrPut(store) { SizeCache.forStore(context.applicationContext.filesDir, store) } + + /** A card for [item] is on screen (or about to be): its size from the cache for this version, else looked up. */ + fun request(context: Context, item: CatalogItem) { + if (!item.owned || item.sizeBytes > 0 || item.store == Store.GOG) return + if (known.containsKey(item.key) || !asked.add(item.key)) return + val app = context.applicationContext + val version = item.extra["version"].orEmpty() + pool.execute { + val cached = cache(app, item.store).get(item.id, version) + if (cached != null) { StoresState.post { known[item.key] = cached }; return@execute } + val bytes = runCatching { fetch(app, item) }.onFailure { Log.w(TAG, "${item.key}: ${it.javaClass.simpleName}") }.getOrNull() ?: 0L + if (bytes > 0) { + cache(app, item.store).put(item.id, version, bytes) + when (item.store) { + Store.EPIC -> EpicPrefs.get(app).edit().putLong("size_${item.id}", bytes).apply() + Store.AMAZON -> AmazonPrefs.get(app).edit().putLong("size_${item.id}", bytes).apply() + Store.GOG -> {} + } + StoresState.post { known[item.key] = bytes } + } + } + } + + /** An installed game's size on disk, by card key; worked out once per install. */ + val onDisk = mutableStateMapOf<String, Long>() + + fun requestDisk(context: Context, key: String, game: InstalledStoreGame) { + if (onDisk.containsKey(key) || !asked.add("disk:$key")) return + val app = context.applicationContext + val version = "disk:${game.sidecar.installedAt}:${game.sidecar.installVersion}" + pool.execute { + val c = cache(app, game.sidecar.store) + val bytes = c.get("disk:${game.sidecar.id}", version) + ?: game.folder.walkTopDown().filter { it.isFile && !it.name.startsWith(".droiddeck") }.sumOf { it.length() }.also { c.put("disk:${game.sidecar.id}", version, it) } + if (bytes > 0) StoresState.post { onDisk[key] = bytes } + } + } + + private fun fetch(context: Context, item: CatalogItem): Long { return when (item.store) { + Store.EPIC -> { + val token = EpicCredentialStore.getValidAccessToken(context) ?: return 0L + val ns = item.extra["namespace"].orEmpty() + val catalogItemId = item.extra["catalogItemId"].orEmpty() + if (ns.isEmpty() || catalogItemId.isEmpty()) return 0L + val json = EpicApiClient.getManifestApiJson(token, ns, catalogItemId, item.id) ?: return 0L + val bytes = EpicDownloadManager.downloadManifest(json, EpicDownloadManager.parseCdnUrls(json)) ?: return 0L + val manifest = EpicDownloadManager.parseManifest(bytes) ?: return 0L + EpicDownloadManager.resolveInstallFiles(manifest, EpicInstallTags.tagsForDevice()).sumOf { it.fileSize() } + } + Store.AMAZON -> { + val token = AmazonCredentialStore.getValidAccessToken(context) ?: return 0L + val entitlement = item.extra["entitlementId"].orEmpty().ifEmpty { return 0L } + val spec = AmazonApiClient.getGameDownload(token, entitlement) ?: return 0L + val bytes = AmazonApiClient.getBytes(AmazonApiClient.appendPath(spec.downloadUrl, "manifest.proto"), token) ?: return 0L + AmazonManifest.parse(bytes).totalInstallSize + } + Store.GOG -> 0L + } } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresNative.kt new file mode 100644 index 0000000000..6d626353fa --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresNative.kt @@ -0,0 +1,54 @@ +package com.droiddeck.launcher.stores + +import android.util.Log + +/** + * The native download engine for the three stores: `libdroiddeckstores.so`, loaded the first time + * a store needs it, never at process start. The app has to run without it (a build without the + * Rust side, or an ABI the library was not built for), so [available] is a plain false then and + * Setup says "Stores engine not built"; the store managers fall back to their Java fetch loops. + */ +object StoresNative { + private const val TAG = "StoresNative" + private const val LIBRARY = "droiddeckstores" + + @Volatile private var loaded: Boolean? = null + @Volatile private var versionText: String? = null + @Volatile private var failure: String? = null + + /** True once the library is loaded and its version symbol binds; false when it cannot be. Cached. */ + val available: Boolean get() = ensureLoaded() + + /** The engine's own version string, or null when the library is not there. */ + val version: String? get() { ensureLoaded(); return versionText } + + /** Why the library did not load, for the Setup row; null when it did. */ + val problem: String? get() { ensureLoaded(); return failure } + + @Synchronized + fun ensureLoaded(): Boolean { + loaded?.let { return it } + val ok = try { + System.loadLibrary(LIBRARY) + versionText = nativeVersion() + Log.i(TAG, "engine ${versionText ?: "?"} loaded") + true + } catch (t: Throwable) { + // UnsatisfiedLinkError when the .so is missing or a symbol is not there; anything else is + // a surprise worth the same treatment: the Java paths carry on. + failure = t.javaClass.simpleName + (t.message?.let { ": $it" } ?: "") + Log.w(TAG, "engine unavailable: $failure") + false + } + loaded = ok + return ok + } + + /** Resets the cached answer, so a probe after an update tries the library again. */ + @Synchronized + fun forget() { loaded = null } + + /** The crate's version string; only after [ensureLoaded] said yes. */ + @JvmStatic + external fun nativeVersion(): String +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt new file mode 100644 index 0000000000..02111bed57 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt @@ -0,0 +1,271 @@ +package com.droiddeck.launcher.stores + +import android.content.Context +import android.os.Handler +import android.os.Looper +import android.util.Log +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateMapOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import com.droiddeck.launcher.stores.download.DownloadEntry +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +/** + * The Stores section's state for the life of the process (like the Flathub store's StoreState): + * who is signed in where, each store's library and shelves, what is installed on disk and what is + * being downloaded, so a download carries on - and its progress shows - while the user moves + * around the launcher or plays something meanwhile. + * + * Everything here is read on the main thread by Compose; the workers post their results back. + * The stores themselves plug in as [StoreBackend]s; a store with no backend in this build shows + * its sign-in card and nothing else. + */ +object StoresState { + private const val TAG = "Stores" + private val main = Handler(Looper.getMainLooper()) + + /** The signed-in account name per store; absent = signed out. */ + val accounts = mutableStateMapOf<Store, String>() + + /** Each store's owned games, once synced; absent until then. */ + val library = mutableStateMapOf<Store, List<CatalogItem>>() + + /** Each store's storefront shelves, once fetched. */ + val shelves = mutableStateMapOf<Store, StoreShelves>() + + /** What a store is doing for the user right now ("Syncing 12 games…"); absent when idle. */ + val status = mutableStateMapOf<Store, String>() + + /** Why the last sync or shelf fetch failed, per store; cleared by the next success. */ + val problems = mutableStateMapOf<Store, String>() + + /** Games the stores installed, as their sidecars say; rebuilt by [refresh]. */ + var installed by mutableStateOf<List<InstalledStoreGame>>(emptyList()) + private set + + /** + * The download queue as the Downloads page lists it, newest first. Replaced only when a row is + * added, removed, reordered or changes state - not on progress - so whatever iterates it does + * not recompose on every byte. A row's progress is read through [download]. + */ + var downloads by mutableStateOf<List<DownloadEntry>>(emptyList()) + private set + + /** Downloads queued, running or paused across the stores (the rail badge); changes only when the count does. */ + var activeDownloads by mutableStateOf(0) + private set + + private val rows = HashMap<String, androidx.compose.runtime.MutableState<DownloadEntry?>>() + + /** One row's live entry: a composable reading it recomposes for that row's progress only. Main thread. */ + fun download(key: String): DownloadEntry? = rows.getOrPut(key) { mutableStateOf(null) }.value + + /** Main thread: each row's holder set (equal entries change nothing), the list and the count only when they change. */ + /** The newest list as published, progress included; for the notification, which is not composed. */ + @Volatile var latestDownloads: List<DownloadEntry> = emptyList() + private set + private var notifiedAt = 0L + + internal fun publishDownloads(list: List<DownloadEntry>) { + latestDownloads = list + // The notification follows at most once a second. + val now = android.os.SystemClock.uptimeMillis() + if (now - notifiedAt >= 1000) { notifiedAt = now; com.droiddeck.launcher.stores.download.StoreDownloadService.tick() } + val keys = list.mapTo(HashSet()) { it.key } + for (e in list) rows.getOrPut(e.key) { mutableStateOf(null) }.value = e + for ((k, holder) in rows) if (k !in keys) holder.value = null + val shape = { l: List<DownloadEntry> -> l.map { it.key to it.state } } + if (shape(list) != shape(downloads)) downloads = list + activeDownloads = list.count { it.isActive } + } + + + /** The native engine's version once probed, "" when the probe said it is missing, null before. */ + var engine by mutableStateOf<String?>(null) + private set + + + private val backends = HashMap<Store, StoreBackend>() + + /** A store's implementation, registered once as the app starts; null when this build lacks it. */ + fun backend(store: Store): StoreBackend? = backends[store] + + fun register(backend: StoreBackend) { backends[backend.store] = backend } + + /** Reads what is on disk for every store: sign-ins, installed games, the engine probe. Off the main thread. */ + fun refresh(context: Context) { + val app = context.applicationContext + Thread({ + val found = Store.entries.mapNotNull { store -> StoreAccounts.signedInAs(app, store)?.let { store to it } } + val onDisk = StoreInstallRoot.gameFolders(app).mapNotNull { folder -> + StoreGameSidecar.read(folder)?.takeIf { it.isInstalled }?.let { InstalledStoreGame(it, folder) } + } + val partial = StoreInstallRoot.unfinished(app) + val version = if (StoresNative.available) StoresNative.version ?: "" else "" + main.post { + accounts.clear() + found.forEach { (store, name) -> accounts[store] = name } + installed = onDisk + unfinished = partial + engine = version + } + }, "stores-refresh").start() + } + + fun isSignedIn(store: Store): Boolean = accounts.containsKey(store) + + /** Stores whose sign-in has run out (a launch could not get a code): their chip dims until the user signs in again. */ + val expired = mutableStateMapOf<Store, Boolean>() + + fun markSignInExpired(store: Store) = post { expired[store] = true } + + /** Installs that stopped before they finished; their Install button reads "Resume install". */ + var unfinished by mutableStateOf<List<StoreInstallRoot.Unfinished>>(emptyList()) + internal set + + fun isUnfinished(item: CatalogItem): Boolean = unfinished.any { + it.store == item.store && (it.id == item.id || (it.id == null && it.folder.name == StoreInstallRoot.folderName(item.title, item.id))) + } + + /** Deletes an unfinished install's folder and caches, as Cancel would; drops its row if there is one. */ + fun clearUnfinished(context: Context, item: CatalogItem) { + val app = context.applicationContext + val key = "${item.store.id}:${item.id}" + if (com.droiddeck.launcher.stores.download.DownloadQueue.entry(key) != null) { com.droiddeck.launcher.stores.download.DownloadQueue.clear(app, key); return } + val folder = unfinished.firstOrNull { + it.store == item.store && (it.id == item.id || (it.id == null && it.folder.name == StoreInstallRoot.folderName(item.title, item.id))) + }?.folder ?: return + StoreInstalls.discard(app, folder, listOf(java.io.File(folder, ".chunks"), java.io.File(folder, ".gog_chunks"), StoreInstallRoot.scratchDir(app, item.store, item.id))) + } + + fun installedGame(store: Store, id: String): InstalledStoreGame? = installed.firstOrNull { it.sidecar.store == store && it.sidecar.id == id } + + /** The user's library and the shelves for [store], from the cache first and the network when stale. */ + fun open(context: Context, store: Store, force: Boolean = false) { + val backend = backends[store] ?: return + val app = context.applicationContext + if (!isSignedIn(store)) return + if (force || !library.containsKey(store)) backend.syncLibrary(app, force) + if (force || !shelves.containsKey(store)) backend.loadShelves(app, force) + } + + fun signIn(context: Context, store: Store) { + expired.remove(store) + backends[store]?.signIn(context) ?: logLine("${store.label}: this build has no sign-in for it yet") + } + + fun signOut(context: Context, store: Store) { + val app = context.applicationContext + Thread({ + backends[store]?.signOut(app) + StoreAccounts.clear(app, store) + main.post { + accounts.remove(store); library.remove(store); status.remove(store); problems.remove(store) + logLine("${store.label}: signed out") + } + }, "stores-signout").start() + } + + /** A notification asked for the Downloads chip; the Stores page takes it and clears it. */ + var openDownloads by mutableStateOf(false) + + /** + * Android 13+: notifications need permission. Asked once, the first time a download starts from + * a screen; refused, downloads still run, without their notification. + */ + private fun askForNotifications(context: Context) { + val activity = context as? android.app.Activity ?: return + if (android.os.Build.VERSION.SDK_INT < 33) return + if (activity.checkSelfPermission("android.permission.POST_NOTIFICATIONS") == android.content.pm.PackageManager.PERMISSION_GRANTED) return + val p = activity.getSharedPreferences("stores", Context.MODE_PRIVATE) + if (p.getBoolean("asked_notifications", false)) return + p.edit().putBoolean("asked_notifications", true).apply() + runCatching { activity.requestPermissions(arrayOf("android.permission.POST_NOTIFICATIONS"), 7102) } + } + + /** An install waiting for the user to say where: the page shows the choice while this is set. */ + var pendingInstall by mutableStateOf<CatalogItem?>(null) + + /** + * The way a page starts an install: with a card in the device the user is asked where (the + * dialog's default is last time's pick, never a silent choice); with none it goes to internal + * storage at once. + */ + fun requestInstall(context: Context, item: CatalogItem) { + askForNotifications(context) + // A resume goes where the files already are; there is nothing to choose. + if (isUnfinished(item)) { install(context, item); return } + val targets = StoreInstallRoot.targets(context) + if (targets.size > 1) pendingInstall = item else install(context, item, targets.first().root) + } + + /** Starts an install into [root] (the internal root when null). */ + fun install(context: Context, item: CatalogItem, root: java.io.File? = null) { + val app = context.applicationContext + val target = root ?: StoreInstallRoot.installRoot(app) + backends[item.store]?.install(app, item, target) ?: logLine("${item.store.label}: this build cannot install yet") + } + + /** Removes the game's folder and its shortcut, then [onDone] on the main thread. */ + fun uninstall(context: Context, game: InstalledStoreGame, onDone: () -> Unit = {}) { + val app = context.applicationContext + Thread({ + runCatching { backends[game.sidecar.store]?.uninstall(app, game) } + StoreInstalls.uninstall(app, game) + main.post(onDone) + }, "stores-uninstall").start() + } + + /** Told on the main thread when an install or removal changed what is on disk; the launcher rebuilds its Games list. */ + @Volatile var libraryListener: (() -> Unit)? = null + + /** A game landed or went: the installed set is re-read and the launcher told. Any thread. */ + fun notifyLibraryChanged(context: Context) { + refresh(context) + main.post { libraryListener?.invoke() } + } + + /** Appends to the engine log with a clock, keeping the last 80 lines. Any thread. */ + fun logLine(raw: String) { + // Every store line passes here, the engines' included: no token or full URL is kept. + val text = StoreLog.redactLine(raw) + Log.i(TAG, text) + val app = appContext ?: return + StoreLogFiles.append(app, "${synchronized(CLOCK) { CLOCK.format(Date()) }} $text") + } + + private var appContext: Context? = null + + /** Called once as the app starts: where the log file goes, and the old days pruned. */ + fun init(context: Context) { + appContext = context.applicationContext + StoreLogFiles.prune(context) + // The EOS overlay an earlier build downloaded (656 MB) is gone with the feature. + val overlay = java.io.File(com.droiddeck.launcher.runtime.LinuxRuntime.rootDir(context), "root/.local/share/droiddeck/epic-overlay") + if (overlay.exists()) Thread({ overlay.deleteRecursively(); Log.i(TAG, "removed the old EOS overlay download") }, "epic-overlay-cleanup").start() + // Cloud saves a killed app never uploaded; not while a session (and maybe that game) runs. + val app = context.applicationContext + Thread({ CloudSaves.uploadAllDirty(app, "recovery") }, "cloud-recovery").start() + } + + internal fun post(block: () -> Unit) = main.post(block) + + private val CLOCK = SimpleDateFormat("HH:mm:ss.SSS", Locale.US) +} + +/** What one store contributes: sign-in, its library, its shelves, installs and removals. */ +interface StoreBackend { + val store: Store + fun signIn(context: Context) + fun signOut(context: Context) + fun syncLibrary(context: Context, force: Boolean) + fun loadShelves(context: Context, force: Boolean) + /** Queues an install of [item] under [root] (a StoreInstallRoot target). */ + fun install(context: Context, item: CatalogItem, root: java.io.File) + /** The store's own records of the install, before the folder goes (the queue row, cached ids). */ + fun uninstall(context: Context, game: InstalledStoreGame) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonApiClient.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonApiClient.java new file mode 100644 index 0000000000..bfe7a617d2 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonApiClient.java @@ -0,0 +1,206 @@ +package com.droiddeck.launcher.stores.amazon; + +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Amazon's distribution API, as the launcher calls it: every gaming call carries `X-Amz-Target`, + * `x-amzn-token`, the launcher's User-Agent and `Content-Encoding: amz-1.0`. + */ +public final class AmazonApiClient { + + private static final String TAG = "AmazonApi"; + private static final String ENTITLEMENTS_URL = "https://gaming.amazon.com/api/distribution/entitlements"; + private static final String DISTRIBUTION_URL = "https://gaming.amazon.com/api/distribution/v2/public"; + private static final String GAMING_USER_AGENT = "com.amazon.agslauncher.win/3.0.9202.1"; + private static final String DOWNLOAD_USER_AGENT = "nile/0.1 Amazon"; + private static final String KEY_ID = "d5dc8b8b-86c8-4fc4-ae93-18c0def5314d"; + + private AmazonApiClient() {} + + /** The owned games, every page, one per productId. */ + public static List<AmazonGame> getEntitlements(String accessToken, String deviceSerial) { + Map<String, AmazonGame> seen = new HashMap<>(); + String nextToken = null; + String hardwareHash; + try { + hardwareHash = AmazonPkce.sha256Upper(deviceSerial); + } catch (Exception e) { + Log.e(TAG, "hardware hash failed", e); + return new ArrayList<>(); + } + do { + try { + JSONObject body = new JSONObject(); + body.put("Operation", "GetEntitlements"); + body.put("clientId", "Sonic"); + body.put("syncPoint", JSONObject.NULL); + body.put("nextToken", nextToken != null ? nextToken : JSONObject.NULL); + body.put("maxResults", 50); + body.put("productIdFilter", JSONObject.NULL); + body.put("keyId", KEY_ID); + body.put("hardwareHash", hardwareHash); + String resp = postGaming(ENTITLEMENTS_URL, "com.amazon.animusdistributionservice.entitlement.AnimusEntitlementsService.GetEntitlements", accessToken, body.toString()); + if (resp == null) break; + JSONObject json = new JSONObject(resp); + JSONArray entitlements = json.optJSONArray("entitlements"); + if (entitlements == null) break; + for (int i = 0; i < entitlements.length(); i++) { + AmazonGame game = parseEntitlement(entitlements.getJSONObject(i)); + if (game != null && !game.productId.isEmpty()) seen.put(game.productId, game); + } + nextToken = json.optString("nextToken", null); + if (nextToken != null && nextToken.isEmpty()) nextToken = null; + } catch (Exception e) { + Log.e(TAG, "entitlements page failed: " + e.getClass().getSimpleName()); + break; + } + } while (nextToken != null); + return new ArrayList<>(seen.values()); + } + + static AmazonGame parseEntitlement(JSONObject e) { + try { + JSONObject product = e.optJSONObject("product"); + if (product == null) return null; + AmazonGame game = new AmazonGame(); + game.entitlementId = e.optString("id", ""); + game.productId = product.optString("id", ""); + game.title = product.optString("title", "Unknown"); + game.productSku = product.optString("sku", ""); + String productType = product.optString("productType", ""); + if (productType.isEmpty()) productType = product.optString("type", ""); + String parentId = product.optString("parentProductId", ""); + if (parentId.isEmpty()) parentId = product.optString("baseProductId", ""); + if (parentId.isEmpty()) parentId = product.optString("parentId", ""); + JSONObject detail = product.optJSONObject("productDetail"); + if (detail != null) { + // The opaque images only: the square icon, and for the wide card the second + // background (a plain key art) before the first (often faded) or the crown image. + game.artUrl = detail.optString("iconUrl", ""); + JSONObject details = detail.optJSONObject("details"); + if (details != null) { + if (game.artUrl.isEmpty()) game.artUrl = details.optString("pgCrownImageUrl", ""); + game.heroUrl = details.optString("backgroundUrl2", ""); + if (game.heroUrl.isEmpty()) game.heroUrl = details.optString("backgroundUrl1", ""); + if (game.heroUrl.isEmpty()) game.heroUrl = details.optString("pgCrownImageUrl", ""); + game.developer = details.optString("developer", ""); + game.publisher = details.optString("publisher", ""); + if (productType.isEmpty()) productType = details.optString("productType", ""); + if (parentId.isEmpty()) parentId = details.optString("parentProductId", ""); + } + } + boolean typeIsDlc = !productType.isEmpty() && !productType.equalsIgnoreCase("GAME"); + if (typeIsDlc || !parentId.isEmpty()) { game.isDLC = true; game.parentProductId = parentId; } + return game; + } catch (Exception ex) { + Log.e(TAG, "entitlement parse failed: " + ex.getClass().getSimpleName()); + return null; + } + } + + public static final class GameDownloadSpec { + public String downloadUrl; + public String versionId; + } + + /** The download root and version for a game; takes the entitlement id, not the product id. */ + public static GameDownloadSpec getGameDownload(String accessToken, String entitlementId) { + try { + JSONObject body = new JSONObject().put("entitlementId", entitlementId).put("Operation", "GetGameDownload"); + String resp = postGaming(DISTRIBUTION_URL, "com.amazon.animusdistributionservice.external.AnimusDistributionService.GetGameDownload", accessToken, body.toString()); + if (resp == null) return null; + JSONObject json = new JSONObject(resp); + GameDownloadSpec spec = new GameDownloadSpec(); + spec.downloadUrl = json.optString("downloadUrl", ""); + spec.versionId = json.optString("versionId", ""); + return spec.downloadUrl.isEmpty() ? null : spec; + } catch (Exception e) { + Log.e(TAG, "GetGameDownload failed: " + e.getClass().getSimpleName()); + return null; + } + } + + /** The live version id of a product, for update checks. */ + public static String getLiveVersionId(String accessToken, String productId) { + try { + JSONObject body = new JSONObject().put("adgProductIds", new JSONArray().put(productId)).put("Operation", "GetLiveVersionIds"); + String resp = postGaming(DISTRIBUTION_URL, "com.amazon.animusdistributionservice.external.AnimusDistributionService.GetLiveVersionIds", accessToken, body.toString()); + if (resp == null) return null; + JSONObject versions = new JSONObject(resp).optJSONObject("versionIds"); + return versions == null ? null : versions.optString(productId, null); + } catch (Exception e) { + Log.e(TAG, "GetLiveVersionIds failed: " + e.getClass().getSimpleName()); + return null; + } + } + + /** `baseUrl/segment`, keeping a query string on the base where it belongs. */ + public static String appendPath(String baseUrl, String segment) { + int q = baseUrl.indexOf('?'); + if (q >= 0) return baseUrl.substring(0, q) + "/" + segment + baseUrl.substring(q); + return baseUrl + "/" + segment; + } + + static String postGaming(String urlStr, String target, String accessToken, String body) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("POST"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(30000); + conn.setDoOutput(true); + conn.setRequestProperty("X-Amz-Target", target); + conn.setRequestProperty("x-amzn-token", accessToken); + conn.setRequestProperty("User-Agent", GAMING_USER_AGENT); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("Content-Encoding", "amz-1.0"); + try (OutputStream os = conn.getOutputStream()) { os.write(body.getBytes(StandardCharsets.UTF_8)); } + int code = conn.getResponseCode(); + String resp = AmazonAuthClient.readStream(code < 400 ? conn.getInputStream() : conn.getErrorStream()); + conn.disconnect(); + if (code < 200 || code >= 300) { Log.e(TAG, "HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); return null; } + return resp; + } catch (Exception e) { + Log.e(TAG, "POST failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } + } + + /** Raw bytes (the manifest). */ + public static byte[] getBytes(String urlStr, String accessToken) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(120000); + if (accessToken != null) conn.setRequestProperty("x-amzn-token", accessToken); + conn.setRequestProperty("User-Agent", DOWNLOAD_USER_AGENT); + int code = conn.getResponseCode(); + if (code < 200 || code >= 300) { Log.e(TAG, "bytes HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); conn.disconnect(); return null; } + java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); + try (java.io.InputStream in = conn.getInputStream()) { + byte[] buf = new byte[131072]; + int n; + while ((n = in.read(buf)) != -1) out.write(buf, 0, n); + } + conn.disconnect(); + return out.toByteArray(); + } catch (Exception e) { + Log.e(TAG, "bytes failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonAuthClient.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonAuthClient.java new file mode 100644 index 0000000000..2cd0f5eec0 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonAuthClient.java @@ -0,0 +1,174 @@ +package com.droiddeck.launcher.stores.amazon; + +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +/** + * Amazon's device-auth endpoints, as the Amazon Games launcher registers itself: + * POST api.amazon.com/auth/register (the PKCE code for bearer tokens, with a device registration) + * POST api.amazon.com/auth/token (access token renewal; the refresh token stays) + * POST api.amazon.com/auth/deregister (sign-out, best-effort) + */ +public final class AmazonAuthClient { + + private static final String TAG = "AmazonAuth"; + static final String REGISTER_URL = "https://api.amazon.com/auth/register"; + static final String REFRESH_URL = "https://api.amazon.com/auth/token"; + static final String DEREGISTER_URL = "https://api.amazon.com/auth/deregister"; + public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0"; + static final String APP_NAME = "AGSLauncher for Windows"; + static final String APP_VERSION = "1.0.0"; + static final String DEVICE_TYPE = "A2UMVHOX7UP4V7"; + static final String OS_VERSION = "10.0.19044.0"; + + public static final class RegisterResult { + public String accessToken; + public String refreshToken; + public long expiresIn; + public String name; + } + + private AmazonAuthClient() {} + + /** Exchanges the PKCE authorization code for bearer tokens; null on failure. */ + public static RegisterResult registerDevice(String authCode, String codeVerifier, String deviceSerial, String clientId) { + try { + JSONObject authData = new JSONObject(); + authData.put("authorization_code", authCode); + authData.put("client_domain", "DeviceLegacy"); + authData.put("client_id", clientId); + authData.put("code_algorithm", "SHA-256"); + authData.put("code_verifier", codeVerifier); + authData.put("use_global_authentication", false); + JSONObject regData = new JSONObject(); + regData.put("app_name", APP_NAME); + regData.put("app_version", APP_VERSION); + regData.put("device_model", "Windows"); + regData.put("device_name", JSONObject.NULL); + regData.put("device_serial", deviceSerial); + regData.put("device_type", DEVICE_TYPE); + regData.put("domain", "Device"); + regData.put("os_version", OS_VERSION); + JSONArray tokenTypes = new JSONArray().put("bearer").put("mac_dms"); + JSONArray extensions = new JSONArray().put("customer_info").put("device_info"); + JSONObject body = new JSONObject(); + body.put("auth_data", authData); + body.put("registration_data", regData); + body.put("requested_extensions", extensions); + body.put("requested_token_type", tokenTypes); + body.put("user_context_map", new JSONObject()); + String response = postJson(REGISTER_URL, body.toString(), null, null, null); + if (response == null) return null; + JSONObject success = new JSONObject(response).getJSONObject("response").getJSONObject("success"); + JSONObject bearer = success.getJSONObject("tokens").getJSONObject("bearer"); + RegisterResult result = new RegisterResult(); + result.accessToken = bearer.getString("access_token"); + result.refreshToken = bearer.getString("refresh_token"); + result.expiresIn = bearer.optLong("expires_in", 3600L); + JSONObject customer = success.optJSONObject("extensions") != null ? success.getJSONObject("extensions").optJSONObject("customer_info") : null; + result.name = customer != null ? customer.optString("name", "") : ""; + return result; + } catch (Exception e) { + Log.e(TAG, "registerDevice failed: " + e.getClass().getSimpleName()); + return null; + } + } + + /** A new access token; the refresh token is not returned and stays as it was. */ + public static RegisterResult refreshAccessToken(String refreshToken) { + try { + JSONObject body = new JSONObject(); + body.put("source_token", refreshToken); + body.put("source_token_type", "refresh_token"); + body.put("requested_token_type", "access_token"); + body.put("app_name", APP_NAME); + body.put("app_version", APP_VERSION); + String response = postJson(REFRESH_URL, body.toString(), null, "x-amzn-identity-auth-domain", "api.amazon.com"); + if (response == null) return null; + JSONObject json = new JSONObject(response); + RegisterResult result = new RegisterResult(); + result.accessToken = json.getString("access_token"); + result.refreshToken = refreshToken; + result.expiresIn = json.optLong("expires_in", 3600L); + return result; + } catch (Exception e) { + Log.e(TAG, "refresh failed: " + e.getClass().getSimpleName()); + return null; + } + } + + public static void deregisterDevice(String accessToken) { + try { + JSONObject body = new JSONObject().put("requested_extensions", new JSONArray().put("device_info").put("customer_info")); + postJson(DEREGISTER_URL, body.toString(), accessToken, null, null); + } catch (Exception e) { + Log.w(TAG, "deregister failed (ignored): " + e.getClass().getSimpleName()); + } + } + + static String postJson(String urlStr, String body, String bearerToken, String extraKey, String extraVal) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("POST"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(30000); + conn.setDoOutput(true); + conn.setRequestProperty("User-Agent", USER_AGENT); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("Accept", "application/json"); + if (bearerToken != null) conn.setRequestProperty("Authorization", "Bearer " + bearerToken); + if (extraKey != null) conn.setRequestProperty(extraKey, extraVal); + try (OutputStream os = conn.getOutputStream()) { os.write(body.getBytes(StandardCharsets.UTF_8)); } + int code = conn.getResponseCode(); + String resp = readStream(code < 400 ? conn.getInputStream() : conn.getErrorStream()); + conn.disconnect(); + if (code < 200 || code >= 300) { Log.e(TAG, "HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); return null; } + return resp; + } catch (Exception e) { + Log.e(TAG, "POST failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } + } + + static String getRequest(String urlStr, String token, String tokenHeader, String extraKey, String extraVal) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(120000); + if (token != null && tokenHeader != null) conn.setRequestProperty(tokenHeader, token); + if (extraKey != null) conn.setRequestProperty(extraKey, extraVal); + int code = conn.getResponseCode(); + String resp = readStream(code < 400 ? conn.getInputStream() : conn.getErrorStream()); + conn.disconnect(); + if (code < 200 || code >= 300) { Log.e(TAG, "GET HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); return null; } + return resp; + } catch (Exception e) { + Log.e(TAG, "GET failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } + } + + static String readStream(InputStream is) throws IOException { + if (is == null) return ""; + StringBuilder sb = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { + String line; + while ((line = br.readLine()) != null) sb.append(line); + } + return sb.toString(); + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt new file mode 100644 index 0000000000..d51a284109 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt @@ -0,0 +1,163 @@ +package com.droiddeck.launcher.stores.amazon + +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.util.Log +import android.webkit.WebView +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.InstalledStoreGame +import com.droiddeck.launcher.stores.LoginFlows +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreBackend +import com.droiddeck.launcher.stores.StoreExe +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoreInstalls +import com.droiddeck.launcher.stores.StoreLoginActivity +import com.droiddeck.launcher.stores.StoreShelves +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.download.DownloadEntry +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.download.DownloadStage +import java.io.File +import java.util.concurrent.atomic.AtomicBoolean + +/** + * Amazon Games in the Stores section. Amazon has no browsable catalog for third-party clients - + * Prime Gaming's claims live on gaming.amazon.com, signed in - so the Store tab's shelves are cut + * from the account's own library: owned-not-installed as Trending, installed as Your library. + */ +object AmazonBackend : StoreBackend { + private const val TAG = "AmazonBackend" + override val store: Store = Store.AMAZON + + init { LoginFlows.amazonFlow = { LoginFlow() } } + + override fun signIn(context: Context) { + context.startActivity(Intent(context, StoreLoginActivity::class.java).putExtra(StoreLoginActivity.EXTRA_STORE, store.id).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + } + + override fun signOut(context: Context) { + AmazonCredentialStore.load(context)?.let { Thread({ AmazonAuthClient.deregisterDevice(it.accessToken) }, "amazon-deregister").start() } + AmazonPrefs.get(context).edit().remove("library_cache").remove("library_synced_at").apply() + } + + override fun syncLibrary(context: Context, force: Boolean) { + val app = context.applicationContext + val cached = AmazonLibrary.cached(app) + if (cached.isNotEmpty()) StoresState.post { publish(app, cached) } + StoresState.post { StoresState.status[store] = "Fetching your library…" } + Thread({ + val result = AmazonLibrary.sync(app, force) { line -> StoresState.post { StoresState.status[store] = line } } + StoresState.post { + StoresState.status.remove(store) + when (result) { + is AmazonLibrary.SyncResult.Ok -> { publish(app, result.games); StoresState.problems.remove(store) } + is AmazonLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message + AmazonLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = "Amazon session expired: sign in again." + AmazonLibrary.SyncResult.Throttled -> publish(app, AmazonLibrary.cached(app)) + AmazonLibrary.SyncResult.Busy -> {} + } + } + }, "amazon-sync").start() + } + + /** The library and, from it, the shelves. */ + private fun publish(app: Context, games: List<AmazonGame>) { + val items = games.map { AmazonLibrary.toCatalogItem(app, it) } + StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], items) + val installed = StoresState.installed.filter { it.sidecar.store == store }.map { it.sidecar.id }.toSet() + StoresState.shelves[store] = StoreShelves(trending = items.filter { it.id !in installed }, free = emptyList(), deals = emptyList(), whatsNew = emptyList()) + } + + override fun loadShelves(context: Context, force: Boolean) { + // Nothing public to fetch; the shelves follow the library sync. + if (!StoresState.library.containsKey(store)) syncLibrary(context, force) + } + + override fun install(context: Context, item: CatalogItem, root: File) { + val app = context.applicationContext + val entitlementId = item.extra["entitlementId"] ?: "" + if (entitlementId.isEmpty()) { StoresState.logLine("Amazon: \"${item.title}\" has no entitlement to download with"); return } + val game = AmazonGame().apply { productId = item.id; this.entitlementId = entitlementId; title = item.title; productSku = item.extra["sku"] ?: ""; heroUrl = item.imageUrl ?: ""; artUrl = item.tallImageUrl ?: "" } + val folder = StoreInstallRoot.folderFor(app, Store.AMAZON, game.productId, game.title, root) + DownloadQueue.enqueue(app, DownloadEntry(store, item.id, item.title, cover = item.imageUrl, bytesTotal = item.sizeBytes, location = StoreInstallRoot.labelFor(app, folder))) { InstallJob(app, game, item, folder) } + } + + override fun uninstall(context: Context, game: InstalledStoreGame) {} + + private class InstallJob(val app: Context, val game: AmazonGame, val item: CatalogItem, val folder: File) : DownloadQueue.DownloadJob { + private val cancelled = AtomicBoolean(false) + + override fun run(handle: DownloadQueue.JobHandle): String? { + handle.stage(DownloadStage.MANIFEST, "Checking sign-in…") + val token = AmazonCredentialStore.getValidAccessToken(app) ?: throw AmazonDownloadManager.InstallException("Not signed in to Amazon Games") + StoreInstalls.begin(folder, Store.AMAZON, game.productId, game.title, item.tallImageUrl ?: item.imageUrl, item.imageUrl) + var downloading = false + val result = AmazonDownloadManager.install(app, game, token, folder, cancelled, object : AmazonDownloadManager.Callback { + override fun onProgress(message: String, pct: Int) { + when { + message.startsWith("Finishing") -> handle.stage(DownloadStage.INSTALL, message) + message.startsWith("Downloading") && !message.startsWith("Downloading manifest") -> { if (!downloading) { downloading = true; handle.stage(DownloadStage.DOWNLOAD, message) } else handle.progress(-1, -1, message) } + else -> handle.stage(DownloadStage.MANIFEST, message) + } + } + override fun onBytes(done: Long, total: Long, speedBps: Long) { handle.progress(done, total, null, speedBps) } + override fun onLog(line: String) { handle.log(line) } + override fun onSizes(downloadBytes: Long, diskBytes: Long) { handle.diskSize(diskBytes) } + }) ?: return null + if (cancelled.get()) return null + handle.stage(DownloadStage.INSTALL, "Registering with Steam…") + AmazonLibrary.rememberSize(app, game.productId, result.bytes) + AmazonPrefs.get(app).edit().putString("version_${game.productId}", result.versionId).apply() + val spec = AmazonLaunchHelper.buildLaunchSpec(folder, game.title) + val exe = spec.exeRelative.ifEmpty { StoreExe.pick(folder, null, game.title) } + if (exe.isEmpty()) handle.log("amazon: no exe found in ${folder.name}; pick one in Steam settings › Added games") + val env = LinkedHashMap<String, String>() + for (kv in AmazonLaunchHelper.buildFuelEnv(game.entitlementId, game.productSku)) env[kv.substringBefore('=')] = kv.substringAfter('=') + val sidecar = StoreGameSidecar( + Store.AMAZON, game.productId, game.title, exe = exe.ifEmpty { "game.exe" }, args = spec.args, env = env, + installVersion = result.versionId ?: "", installedAt = System.currentTimeMillis(), + cover = item.tallImageUrl ?: item.imageUrl, hero = item.imageUrl, + extra = mapOf("entitlementId" to game.entitlementId, "sku" to game.productSku), + ) + StoreInstalls.complete(app, folder, onStep = { done, steps -> handle.stageProgress(done.toLong(), steps.toLong()) }, sidecar = sidecar) + return folder.path + } + + override fun cancel(deleteFiles: Boolean) { + cancelled.set(true) + if (deleteFiles) StoreInstalls.discard(app, folder, emptyList()) + } + } + + /** Amazon's device sign-in: a PKCE challenge on the sign-in page, the code on the return URL, a device registration for tokens. */ + private class LoginFlow : StoreLoginActivity.Flow { + private val serial = AmazonPkce.generateDeviceSerial() + private val clientId = AmazonPkce.generateClientId(serial) + private val verifier = AmazonPkce.generateCodeVerifier() + override val startUrl: String = "https://www.amazon.com/ap/signin" + + "?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0" + + "&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select" + + "&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select" + + "&openid.mode=checkid_setup&openid.oa2.scope=device_auth_access" + + "&openid.ns.oa2=http%3A%2F%2Fwww.amazon.com%2Fap%2Fext%2Foauth%2F2&openid.oa2.response_type=code" + + "&openid.oa2.code_challenge_method=S256&openid.oa2.client_id=device%3A$clientId" + + "&language=en_US&marketPlaceId=ATVPDKIKX0DER&openid.return_to=https%3A%2F%2Fwww.amazon.com" + + "&openid.pape.max_auth_age=0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0" + + "&openid.assoc_handle=amzn_sonic_games_launcher&pageId=amzn_sonic_games_launcher" + + "&openid.oa2.code_challenge=${AmazonPkce.generateCodeChallenge(verifier)}" + override val userAgent: String get() = AmazonAuthClient.USER_AGENT + override fun isRedirect(url: String): Boolean = url.contains("openid.oa2.authorization_code=") + + override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? { + val code = Uri.parse(url).getQueryParameter("openid.oa2.authorization_code") + if (code.isNullOrEmpty()) { Log.w(TAG, "login: return URL without a code"); return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_verification) } + val result = AmazonAuthClient.registerDevice(code, verifier, serial, clientId) ?: return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_generic) + AmazonCredentialStore.save(activity, result.accessToken, result.refreshToken, serial, clientId, System.currentTimeMillis() + result.expiresIn * 1000L, result.name) + Log.i(TAG, "signed in") + return null + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonCredentialStore.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonCredentialStore.kt new file mode 100644 index 0000000000..9b9873c2bf --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonCredentialStore.kt @@ -0,0 +1,52 @@ +package com.droiddeck.launcher.stores.amazon + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreAccounts +import org.json.JSONObject + +/** + * The Amazon sign-in in StoreAccounts' private file: the bearer tokens, the device serial and + * client id the registration was made with (the entitlements call hashes the serial), the expiry. + */ +object AmazonCredentialStore { + private const val TAG = "AmazonAuth" + + class Credentials(val accessToken: String, val refreshToken: String?, val deviceSerial: String, val clientId: String, val expiresAt: Long) + + @JvmStatic + fun save(context: Context, accessToken: String, refreshToken: String?, deviceSerial: String, clientId: String, expiresAt: Long, displayName: String?) { + val json = StoreAccounts.read(context, Store.AMAZON) ?: JSONObject() + json.put("access_token", accessToken).put("device_serial", deviceSerial).put("client_id", clientId).put("expires_at", expiresAt) + if (!refreshToken.isNullOrEmpty()) json.put("refresh_token", refreshToken) + if (!displayName.isNullOrEmpty()) json.put("display_name", displayName) + StoreAccounts.write(context, Store.AMAZON, json) + } + + @JvmStatic + fun load(context: Context): Credentials? { + val json = StoreAccounts.read(context, Store.AMAZON) ?: return null + val token = json.optString("access_token", "").ifEmpty { return null } + return Credentials(token, json.optString("refresh_token", "").ifEmpty { null }, json.optString("device_serial", ""), json.optString("client_id", ""), json.optLong("expires_at", 0L)) + } + + @JvmStatic + fun isLoggedIn(context: Context): Boolean = load(context) != null + + /** A valid access token, refreshed within five minutes of expiry; null when signed out or the refresh failed. */ + @JvmStatic + fun getValidAccessToken(context: Context): String? { + val creds = load(context) ?: return null + if (creds.expiresAt - System.currentTimeMillis() < 5L * 60L * 1000L && creds.refreshToken != null) { + val result = AmazonAuthClient.refreshAccessToken(creds.refreshToken) + if (result != null) { + save(context, result.accessToken, creds.refreshToken, creds.deviceSerial, creds.clientId, System.currentTimeMillis() + result.expiresIn * 1000L, null) + Log.i(TAG, "token refreshed") + return result.accessToken + } + Log.w(TAG, "token refresh failed; using the stored token") + } + return creds.accessToken + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonDownloadManager.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonDownloadManager.java new file mode 100644 index 0000000000..82a8b23ea4 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonDownloadManager.java @@ -0,0 +1,288 @@ +package com.droiddeck.launcher.stores.amazon; + +import android.content.Context; +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; +import com.droiddeck.launcher.stores.download.StoreDownloadTier; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.security.MessageDigest; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + +/** + * The Amazon Games install pipeline, ported from Bannerlator: GetGameDownload for the download + * root and version, `manifest.proto`, then every file fetched (the native engine when it is + * there, eight Java threads otherwise), SHA-256 checked, written as `.tmp` and renamed. A + * complete file on disk is skipped by size, so a rerun resumes. Blocking; the queue runs it. + */ +public final class AmazonDownloadManager { + + private static final String TAG = "AmazonDownload"; + private static final int MAX_PARALLEL = 8; + private static final int MAX_RETRIES = 3; + private static final long PROGRESS_INTERVAL = 512L * 1024L; + private static final String DOWNLOAD_USER_AGENT = "nile/0.1 Amazon"; + private static final String IN_PROGRESS_MARKER = ".amazon_download_in_progress"; + private static final String COMPLETE_MARKER = ".amazon_download_complete"; + + public interface Callback { + void onProgress(String message, int pct); + default void onBytes(long done, long total, long speedBps) {} + default void onLog(String line) {} + /** What this run fetches and what the game takes on disk. */ + default void onSizes(long downloadBytes, long diskBytes) {} + } + + public static final class Result { + public final String versionId; + public final long bytes; + Result(String versionId, long bytes) { this.versionId = versionId; this.bytes = bytes; } + } + + public static final class InstallException extends Exception { + InstallException(String message) { super(message); } + } + + private AmazonDownloadManager() {} + + /** Installs the game into {@code installDir}; null when cancelled, an exception when it fails. */ + public static Result install(Context ctx, AmazonGame game, String accessToken, File installDir, AtomicBoolean cancel, Callback cb) throws InstallException { + if (game.entitlementId == null || game.entitlementId.isEmpty()) throw new InstallException("This game has no entitlement to download with"); + try { + installDir.mkdirs(); + new File(installDir, IN_PROGRESS_MARKER).createNewFile(); + cb.onProgress("Asking Amazon for the download…", 0); + AmazonApiClient.GameDownloadSpec spec = AmazonApiClient.getGameDownload(accessToken, game.entitlementId); + if (spec == null) throw new InstallException("Amazon gave no download for this game"); + cb.onLog("amazon: version " + spec.versionId); + cb.onProgress("Downloading manifest…", 1); + byte[] manifestBytes = AmazonApiClient.getBytes(AmazonApiClient.appendPath(spec.downloadUrl, "manifest.proto"), accessToken); + 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"); + cb.onSizes(manifest.totalInstallSize, manifest.totalInstallSize); + cb.onBytes(0, manifest.totalInstallSize, 0); + if (cancel.get()) return null; + + AtomicLong downloaded = new AtomicLong(0); + AtomicLong lastEmit = new AtomicLong(0); + AtomicLong lastSpeedMs = new AtomicLong(System.currentTimeMillis()); + AtomicLong lastSpeedBytes = new AtomicLong(0); + AtomicLong speedBps = new AtomicLong(0); + List<AmazonManifest.ManifestFile> files = manifest.allFiles; + final long total = manifest.totalInstallSize; + + if (AmazonNative.isAvailable()) { + if (!downloadAllNative(ctx, spec.downloadUrl, files, total, installDir, downloaded, lastEmit, lastSpeedMs, lastSpeedBytes, speedBps, cancel, cb)) { + if (cancel.get()) return null; + throw new InstallException("A file failed to download"); + } + } else { + cb.onLog("amazon: engine=built-in (" + MAX_PARALLEL + " threads)"); + ExecutorService pool = Executors.newFixedThreadPool(MAX_PARALLEL, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("amazon-dl")); + List<Future<Boolean>> futures = new ArrayList<>(); + for (AmazonManifest.ManifestFile file : files) { + final String dlUrl = spec.downloadUrl; + futures.add(pool.submit(() -> downloadFileWithRetry(file, dlUrl, installDir, downloaded, lastEmit, total, lastSpeedMs, lastSpeedBytes, speedBps, cb, cancel))); + } + pool.shutdown(); + try { + for (Future<Boolean> f : futures) { + if (!f.get()) { + pool.shutdownNow(); + if (cancel.get()) return null; + throw new InstallException("A file failed to download"); + } + } + } catch (InstallException e) { + throw e; + } catch (Exception e) { + pool.shutdownNow(); + throw new InstallException("download pool error: " + e.getClass().getSimpleName()); + } + } + if (cancel.get()) return null; + cb.onProgress("Finishing…", 98); + new File(installDir, IN_PROGRESS_MARKER).delete(); + new File(installDir, COMPLETE_MARKER).createNewFile(); + return new Result(spec.versionId, total); + } catch (InstallException e) { + throw e; + } catch (Exception e) { + Log.w(TAG, "install failed", e); + throw new InstallException(e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : "")); + } + } + + /** One entry per manifest file with the exact URL the Java loop would open and the SHA-256 when it would check it. */ + static String buildPlan(List<AmazonManifest.ManifestFile> files, String baseUrl) { + JSONArray arr = new JSONArray(); + try { + for (AmazonManifest.ManifestFile file : files) { + String hashHex = file.hashHex(); + arr.put(new JSONObject().put("relPath", file.unixPath()).put("url", AmazonApiClient.appendPath(baseUrl, "files/" + hashHex)) + .put("size", file.size).put("sha256hex", (file.hashAlgorithm == 0 && file.hashBytes.length > 0) ? hashHex : "")); + } + } catch (Exception e) { + Log.e(TAG, "plan failed", e); + return ""; + } + return arr.toString(); + } + + private static boolean downloadAllNative(Context ctx, String baseUrl, List<AmazonManifest.ManifestFile> files, long totalSize, File installDir, + AtomicLong totalDownloaded, AtomicLong lastEmit, AtomicLong lastSpeedMs, AtomicLong lastSpeedBytes, AtomicLong speedBps, + AtomicBoolean cancel, Callback cb) { + String plan = buildPlan(files, baseUrl); + if (plan.isEmpty()) return false; + StoreDownloadTier tier = StoreDownloadTier.Companion.current(ctx); + int maxWorkers = Math.max(1, Math.min(128, tier.getNetworkWindow())); + int processWorkers = Math.max(Math.max(1, Runtime.getRuntime().availableProcessors() / 2), tier.getProcessWorkers()); + cb.onLog("amazon: engine=native workers=" + maxWorkers + " process_workers=" + processWorkers + " tier=" + tier.getId()); + AmazonNative.Listener listener = new AmazonNative.Listener() { + @Override public void onProgress(long bytesDone, long bytesTotal, long filesDone, long filesTotal) { + totalDownloaded.set(bytesDone); + long emit = lastEmit.get(); + if (bytesDone - emit >= PROGRESS_INTERVAL && lastEmit.compareAndSet(emit, bytesDone)) { + sampleSpeed(bytesDone, lastSpeedMs, lastSpeedBytes, speedBps); + cb.onProgress("Downloading (" + filesDone + "/" + filesTotal + " files)" + speedSuffix(speedBps.get()), (int) (bytesDone * 95L / Math.max(1, totalSize))); + cb.onBytes(bytesDone, totalSize, speedBps.get()); + } + } + @Override public void onLog(String line) { cb.onLog(line); } + @Override public void onComplete(boolean success, String error, long bytesWritten) { + cb.onLog("amazon: complete success=" + success + " bytes=" + bytesWritten + (error.isEmpty() ? "" : " error=" + error)); + } + }; + AmazonNative.RunResult result = AmazonNative.runBlocking(plan, installDir.getAbsolutePath(), "", maxWorkers, processWorkers, cancel::get, listener); + if (result.cancelled) return false; + if (!result.success) { cb.onLog("amazon: engine failed: " + result.error); return false; } + return true; + } + + private static boolean downloadFileWithRetry(AmazonManifest.ManifestFile file, String baseUrl, File installDir, AtomicLong totalDownloaded, AtomicLong lastEmit, long totalSize, + 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; } + String fileUrl = AmazonApiClient.appendPath(baseUrl, "files/" + file.hashHex()); + for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) { + destFile.getParentFile().mkdirs(); + try { + if (!downloadFile(fileUrl, tmpFile, totalDownloaded, lastEmit, totalSize, lastSpeedMs, lastSpeedBytes, speedBps, cb, cancel)) { tmpFile.delete(); return false; } + if (file.hashAlgorithm == 0 && file.hashBytes.length > 0 && !Arrays.equals(sha256(tmpFile), file.hashBytes)) { + cb.onLog("amazon: SHA-256 mismatch for " + file.unixPath()); + tmpFile.delete(); + if (attempt < MAX_RETRIES) { Thread.sleep(1000L << (attempt - 1)); continue; } + return false; + } + if (destFile.exists()) destFile.delete(); + return tmpFile.renameTo(destFile); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + tmpFile.delete(); + return false; + } catch (Exception e) { + cb.onLog("amazon: attempt " + attempt + " failed for " + file.unixPath() + ": " + e.getClass().getSimpleName()); + tmpFile.delete(); + if (attempt < MAX_RETRIES) { try { Thread.sleep(1000L << (attempt - 1)); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); return false; } } + } + } + return false; + } + + /** Streams one file; false = cancelled. */ + private static boolean downloadFile(String urlStr, File tmpFile, AtomicLong totalDownloaded, AtomicLong lastEmit, long totalSize, + AtomicLong lastSpeedMs, AtomicLong lastSpeedBytes, AtomicLong speedBps, Callback cb, AtomicBoolean cancel) throws IOException { + HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setConnectTimeout(30000); + conn.setReadTimeout(120000); + conn.setRequestProperty("User-Agent", DOWNLOAD_USER_AGENT); + int code = conn.getResponseCode(); + if (code < 200 || code >= 300) { conn.disconnect(); throw new IOException("HTTP " + code + " " + StoreLog.redactUrl(urlStr)); } + try (InputStream in = conn.getInputStream(); FileOutputStream out = new FileOutputStream(tmpFile)) { + byte[] buf = new byte[65536]; + int n; + while ((n = in.read(buf)) >= 0) { + if (cancel.get()) { conn.disconnect(); return false; } + out.write(buf, 0, n); + long dl = totalDownloaded.addAndGet(n); + long emit = lastEmit.get(); + if (dl - emit >= PROGRESS_INTERVAL && lastEmit.compareAndSet(emit, dl)) { + sampleSpeed(dl, lastSpeedMs, lastSpeedBytes, speedBps); + cb.onProgress("Downloading: " + tmpFile.getName().replace(".tmp", "") + speedSuffix(speedBps.get()), (int) (dl * 95L / Math.max(1, totalSize))); + cb.onBytes(dl, totalSize, speedBps.get()); + } + } + } finally { + conn.disconnect(); + } + return true; + } + + /** The install size from the manifest alone, or -1. Blocking. */ + public static long fetchInstallSizeBytes(String accessToken, String entitlementId) { + try { + AmazonApiClient.GameDownloadSpec spec = AmazonApiClient.getGameDownload(accessToken, entitlementId); + if (spec == null) return -1; + byte[] bytes = AmazonApiClient.getBytes(AmazonApiClient.appendPath(spec.downloadUrl, "manifest.proto"), accessToken); + if (bytes == null) return -1; + AmazonManifest.ParsedManifest m = AmazonManifest.parse(bytes); + return m.totalInstallSize > 0 ? m.totalInstallSize : -1; + } catch (Exception e) { + return -1; + } + } + + public static boolean isInstalled(File installDir) { return new File(installDir, COMPLETE_MARKER).exists(); } + + private static byte[] sha256(File file) throws Exception { + MessageDigest digest = MessageDigest.getInstance("SHA-256"); + try (FileInputStream fis = new FileInputStream(file)) { + byte[] buf = new byte[65536]; + int n; + while ((n = fis.read(buf)) >= 0) digest.update(buf, 0, n); + } + return digest.digest(); + } + + 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("%.1f MB/s", bps / 1048576.0) : (bps / 1024) + " KB/s"); + } + + private static String fmt(long bytes) { + if (bytes >= 1073741824L) return String.format("%.1f GB", bytes / 1073741824.0); + if (bytes >= 1048576L) return String.format("%.1f MB", bytes / 1048576.0); + return String.format("%.0f KB", bytes / 1024.0); + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonGame.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonGame.java new file mode 100644 index 0000000000..891602b682 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonGame.java @@ -0,0 +1,28 @@ +package com.droiddeck.launcher.stores.amazon; + +/** + * One Amazon Games library entry. {@code productId} ("amzn1.adg.product.…") names the game, + * {@code entitlementId} (the top-level UUID of GetEntitlements) is what GetGameDownload takes, + * {@code productSku} goes to the game as AMAZON_GAMES_FUEL_PRODUCT_SKU. + */ +public class AmazonGame { + public String productId = ""; + public String entitlementId = ""; + public String title = ""; + /** iconUrl: square art. */ + public String artUrl = ""; + /** backgroundUrl1: the wide background. */ + public String heroUrl = ""; + public String developer = ""; + public String publisher = ""; + public String productSku = ""; + public String versionId = ""; + public boolean isDLC = false; + public String parentProductId = ""; + + /** The id's last segment, for display when there is no title. */ + public String shortId() { + int dot = productId.lastIndexOf('.'); + return (dot >= 0 && dot < productId.length() - 1) ? productId.substring(dot + 1) : productId; + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLaunchHelper.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLaunchHelper.java new file mode 100644 index 0000000000..525b3c7a12 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLaunchHelper.java @@ -0,0 +1,140 @@ +package com.droiddeck.launcher.stores.amazon; + +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +/** + * How an installed Amazon game is started: its `fuel.json` names the exe, a working subfolder and + * arguments when the publisher shipped one; otherwise the exe is scored (Unreal shipping binaries + * first, the title's name, never crash handlers or installers). The FuelPump variables the Amazon + * Games Services SDK reads go into the game's environment. + */ +public final class AmazonLaunchHelper { + + private static final String TAG = "AmazonLaunch"; + + public static final class LaunchSpec { + /** The exe relative to the install folder, forward slashes. */ + public String exeRelative = ""; + /** The arguments fuel.json names, in order. */ + public List<String> args = new ArrayList<>(); + } + + private AmazonLaunchHelper() {} + + public static LaunchSpec buildLaunchSpec(File installDir, String gameTitle) { + LaunchSpec spec = new LaunchSpec(); + String fuelCommand = null; + File fuelFile = new File(installDir, "fuel.json"); + if (fuelFile.exists()) { + try { + StringBuilder sb = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new FileReader(fuelFile))) { + String line; + while ((line = br.readLine()) != null) sb.append(line); + } + JSONObject main = new JSONObject(sb.toString()).optJSONObject("Main"); + if (main != null) { + String cmd = main.optString("Command", "").trim(); + if (!cmd.isEmpty()) fuelCommand = cmd; + JSONArray argsArr = main.optJSONArray("Args"); + if (argsArr != null) for (int i = 0; i < argsArr.length(); i++) { + String a = argsArr.optString(i); + if (a != null && !a.isEmpty()) spec.args.add(a); + } + } + } catch (Exception e) { + Log.w(TAG, "fuel.json unreadable; choosing the exe by name"); + } + } + if (fuelCommand != null && new File(installDir, fuelCommand.replace('\\', '/')).isFile()) { + spec.exeRelative = fuelCommand.replace('\\', '/'); + } else { + File chosen = choosePrimaryExe(installDir, gameTitle); + if (chosen != null) spec.exeRelative = relativePath(installDir, chosen); + } + return spec; + } + + /** The FuelPump variables, as KEY=VALUE, for the game's environment. */ + public static String[] buildFuelEnv(String entitlementId, String productSku) { + return new String[]{ + "FUEL_DIR=C:\\ProgramData\\Amazon Games Services\\Legacy", + "AMAZON_GAMES_SDK_PATH=C:\\ProgramData\\Amazon Games Services\\AmazonGamesSDK", + "AMAZON_GAMES_FUEL_ENTITLEMENT_ID=" + entitlementId, + "AMAZON_GAMES_FUEL_PRODUCT_SKU=" + productSku, + "AMAZON_GAMES_FUEL_DISPLAY_NAME=Player", + }; + } + + private static final Pattern UE_SHIPPING = Pattern.compile(".*-win(32|64)(-shipping)?\\.exe$", Pattern.CASE_INSENSITIVE); + private static final Pattern UE_BINARIES = Pattern.compile(".*/binaries/win(32|64)/.*\\.exe$", Pattern.CASE_INSENSITIVE); + private static final Pattern GENERIC_NAME = Pattern.compile("^[a-z]\\d{1,3}\\.exe$", Pattern.CASE_INSENSITIVE); + private static final String[] NEGATIVE_KEYWORDS = { "crash", "handler", "viewer", "compiler", "tool", "setup", "unins", "eac", "launcher", "steam" }; + + private static boolean isLikelyStub(File f) { + String n = f.getName().toLowerCase(); + if (GENERIC_NAME.matcher(n).matches()) return true; + if (f.length() < 1_000_000L) return true; + for (String kw : NEGATIVE_KEYWORDS) if (n.contains(kw)) return true; + return false; + } + + static int scoreExe(File f, String gameNameLower) { + int score = 50; + String path = f.getAbsolutePath().replace('\\', '/').toLowerCase(); + if (UE_SHIPPING.matcher(path).matches()) score += 300; + if (UE_BINARIES.matcher(path).find()) score += 250; + String fn = f.getName().toLowerCase(); + String cleanGame = gameNameLower.replaceAll("[^a-z]", ""); + String cleanFile = fn.replaceAll("[^a-z]", ""); + boolean nameMatch = path.contains(gameNameLower) || (cleanGame.length() >= 5 && cleanFile.length() >= 5 && cleanGame.substring(0, 5).equals(cleanFile.substring(0, 5))); + if (nameMatch) score += 100; + for (String kw : NEGATIVE_KEYWORDS) if (path.contains(kw)) { score -= 150; break; } + if (GENERIC_NAME.matcher(fn).matches()) score -= 200; + return score; + } + + static File choosePrimaryExe(File installDir, String gameTitle) { + if (!installDir.isDirectory()) return null; + List<File> all = new ArrayList<>(); + collectExe(installDir, all); + if (all.isEmpty()) return null; + List<File> pool = new ArrayList<>(); + for (File f : all) if (!isLikelyStub(f)) pool.add(f); + if (pool.isEmpty()) pool = all; + String lowerTitle = gameTitle.toLowerCase(); + File best = null; + int bestScore = Integer.MIN_VALUE; + for (File f : pool) { + int s = scoreExe(f, lowerTitle); + if (best == null || s > bestScore || (s == bestScore && f.length() > best.length())) { best = f; bestScore = s; } + } + return best; + } + + private static void collectExe(File dir, List<File> out) { + File[] entries = dir.listFiles(); + if (entries == null) return; + for (File f : entries) { + if (f.isDirectory()) collectExe(f, out); + else if (f.getName().toLowerCase().endsWith(".exe")) out.add(f); + } + } + + private static String relativePath(File base, File absolute) { + String b = base.getAbsolutePath(); + if (!b.endsWith("/")) b += "/"; + String a = absolute.getAbsolutePath(); + return a.startsWith(b) ? a.substring(b.length()) : absolute.getName(); + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt new file mode 100644 index 0000000000..75331ba2ca --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt @@ -0,0 +1,128 @@ +package com.droiddeck.launcher.stores.amazon + +import android.content.Context +import android.content.SharedPreferences +import android.util.Log +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreNet +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.Callable +import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicBoolean + +/** + * The signed-in account's Amazon Games library: the entitlements, DLC folded under their base + * game, cached so the tab paints at once, re-synced when forced or 15 minutes stale. Amazon + * publishes a square icon and a wide background only, so a 2:3 poster per title is backfilled + * from SteamGridDB (a few per pass). Ported from Bannerlator's AmazonLibraryRepo. + */ +object AmazonLibrary { + private const val TAG = "AmazonLibrary" + private const val CACHE_KEY = "library_cache" + private const val LAST_SYNC_KEY = "library_synced_at" + private const val THROTTLE_MS = 6L * 60L * 60L * 1000L + private const val POSTER_BUDGET = 40 + + private val syncing = AtomicBoolean(false) + + sealed interface SyncResult { + class Ok(val games: List<AmazonGame>) : SyncResult + class Failed(val message: String) : SyncResult + object NotLoggedIn : SyncResult + object Busy : SyncResult + object Throttled : SyncResult + } + + fun cached(context: Context): List<AmazonGame> { + val json = AmazonPrefs.get(context).getString(CACHE_KEY, null) ?: return emptyList() + return runCatching { + val arr = JSONArray(json) + val out = ArrayList<AmazonGame>(arr.length()) + for (i in 0 until arr.length()) { + val j = arr.optJSONObject(i) ?: continue + val g = AmazonGame() + g.productId = j.optString("productId", ""); g.entitlementId = j.optString("entitlementId", ""); g.title = j.optString("title", "") + g.artUrl = j.optString("artUrl", ""); g.heroUrl = j.optString("heroUrl", ""); g.developer = j.optString("developer", "") + g.publisher = j.optString("publisher", ""); g.productSku = j.optString("productSku", ""); g.versionId = j.optString("versionId", "") + if (g.productId.isNotEmpty()) out.add(g) + } + out.distinctBy { it.productId }.sortedBy { it.title.lowercase() } + }.getOrDefault(emptyList()) + } + + fun poster(context: Context, productId: String): String? = AmazonPrefs.get(context).getString("vcover_$productId", null)?.ifBlank { null } + + fun rememberSize(context: Context, productId: String, bytes: Long) { + if (bytes > 0) AmazonPrefs.get(context).edit().putLong("size_$productId", bytes).apply() + } + + fun sync(context: Context, force: Boolean = false, onStatus: (String) -> Unit = {}): SyncResult { + if (!syncing.compareAndSet(false, true)) return SyncResult.Busy + try { + val app = context.applicationContext + val p = AmazonPrefs.get(app) + val cachedList = cached(app) + if (!force && cachedList.isNotEmpty() && System.currentTimeMillis() - p.getLong(LAST_SYNC_KEY, 0L) < THROTTLE_MS) { + backfillPosters(p, cachedList, onStatus) + return SyncResult.Throttled + } + onStatus("Checking sign-in…") + val creds = AmazonCredentialStore.load(app) ?: return SyncResult.NotLoggedIn + val token = AmazonCredentialStore.getValidAccessToken(app) ?: return SyncResult.Failed("Amazon session expired: sign in again.") + onStatus("Fetching game list…") + val all = AmazonApiClient.getEntitlements(token, creds.deviceSerial) + if (all.isNullOrEmpty()) return if (cachedList.isEmpty()) SyncResult.Failed("No games found in your Amazon library") else SyncResult.Ok(cachedList) + val games = all.filter { !(it.isDLC && it.parentProductId.isNotEmpty()) }.ifEmpty { all } + .sortedWith { a, b -> a.title.compareTo(b.title, ignoreCase = true) } + for (fresh in games) cachedList.firstOrNull { it.productId == fresh.productId }?.let { fresh.versionId = it.versionId } + saveCache(p, games) + p.edit().putLong(LAST_SYNC_KEY, System.currentTimeMillis()).apply() + backfillPosters(p, games, onStatus) + Log.i(TAG, "sync: entitlements=${all.size} games=${games.size}") + return SyncResult.Ok(games) + } catch (e: Exception) { + Log.w(TAG, "sync failed: ${e.message}") + return SyncResult.Failed(e.message ?: "Sync failed") + } finally { + syncing.set(false) + } + } + + private fun backfillPosters(p: SharedPreferences, games: List<AmazonGame>, onStatus: (String) -> Unit) { + val missing = games.filter { it.productId.isNotEmpty() && !p.contains("vcover_${it.productId}") }.take(POSTER_BUDGET) + if (missing.isEmpty()) return + onStatus("Fetching cover art…") + val pool = Executors.newFixedThreadPool(4) + val futures = missing.map { g -> pool.submit(Callable { g.productId to StoreNet.sgdbPoster(g.title) }) } + pool.shutdown() + val ed = p.edit() + for (f in futures) { + val (pid, url) = runCatching { f.get() }.getOrNull() ?: continue + // A miss is recorded as "" so it is not asked again every pass. + ed.putString("vcover_$pid", url) + } + ed.apply() + } + + private fun saveCache(p: SharedPreferences, games: List<AmazonGame>) { + runCatching { + val arr = JSONArray() + for (g in games) arr.put(JSONObject().put("productId", g.productId).put("entitlementId", g.entitlementId).put("title", g.title).put("artUrl", g.artUrl) + .put("heroUrl", g.heroUrl).put("developer", g.developer).put("publisher", g.publisher).put("productSku", g.productSku).put("versionId", g.versionId)) + p.edit().putString(CACHE_KEY, arr.toString()).apply() + } + } + + fun toCatalogItem(context: Context, g: AmazonGame): CatalogItem = CatalogItem( + store = Store.AMAZON, id = g.productId, title = g.title.ifBlank { g.shortId() }, + imageUrl = g.heroUrl.ifBlank { null } ?: g.artUrl.ifBlank { null }, + tallImageUrl = poster(context, g.productId) ?: g.artUrl.ifBlank { null } ?: g.heroUrl.ifBlank { null }, + tags = listOfNotNull(g.developer.ifBlank { null }, g.publisher.ifBlank { null }.takeIf { it != g.developer }).joinToString(", "), + developer = g.developer, owned = true, + sizeBytes = AmazonPrefs.get(context).getLong("size_${g.productId}", 0L), + storeUrl = "https://gaming.amazon.com/home", + extra = mapOf("entitlementId" to g.entitlementId, "sku" to g.productSku, "version" to (g.versionId ?: "")), + ) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonManifest.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonManifest.java new file mode 100644 index 0000000000..25fb031788 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonManifest.java @@ -0,0 +1,158 @@ +package com.droiddeck.launcher.stores.amazon; + +import org.tukaani.xz.LZMAInputStream; +import org.tukaani.xz.XZInputStream; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Amazon's `manifest.proto`: a big-endian header length, a ManifestHeader protobuf naming the + * compression, then an LZMA (or XZ) body holding the Manifest protobuf - packages of files, each + * with a Windows path, a size and a SHA-256. Read with a tiny protobuf walker; the XZ library the + * app already carries does the decompression. + */ +public final class AmazonManifest { + + public static final class ManifestFile { + public final String path; + public final long size; + /** 0 = SHA-256. */ + public final int hashAlgorithm; + public final byte[] hashBytes; + + ManifestFile(String path, long size, int hashAlgorithm, byte[] hashBytes) { + this.path = path; this.size = size; this.hashAlgorithm = hashAlgorithm; this.hashBytes = hashBytes; + } + + public String unixPath() { return path.replace('\\', '/'); } + + public String hashHex() { + StringBuilder sb = new StringBuilder(); + for (byte b : hashBytes) sb.append(String.format("%02x", b & 0xFF)); + return sb.toString(); + } + } + + public static final class ParsedManifest { + public final List<ManifestFile> allFiles; + public final long totalInstallSize; + + ParsedManifest(List<ManifestFile> files) { + this.allFiles = files; + long total = 0; + for (ManifestFile f : files) total += f.size; + this.totalInstallSize = total; + } + } + + private AmazonManifest() {} + + public static ParsedManifest parse(byte[] data) throws IOException { + if (data == null || data.length < 4) throw new IOException("Manifest too short"); + int headerSize = ByteBuffer.wrap(data, 0, 4).order(ByteOrder.BIG_ENDIAN).getInt(); + if (headerSize < 0 || 4 + headerSize > data.length) throw new IOException("Invalid header size " + headerSize); + byte[] body = Arrays.copyOfRange(data, 4 + headerSize, data.length); + return parseManifest(decompress(body)); + } + + private static byte[] decompress(byte[] body) throws IOException { + if (body.length < 2) return body; + boolean isXz = (body[0] & 0xFF) == 0xFD && (body[1] & 0xFF) == 0x37; + try (InputStream in = isXz ? new XZInputStream(new ByteArrayInputStream(body), -1) : new LZMAInputStream(new ByteArrayInputStream(body), -1)) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buf = new byte[65536]; + int n; + while ((n = in.read(buf)) >= 0) bos.write(buf, 0, n); + return bos.toByteArray(); + } + } + + private static ParsedManifest parseManifest(byte[] bytes) { + List<ManifestFile> files = new ArrayList<>(); + ProtoReader r = new ProtoReader(bytes); + while (r.hasMore()) { + int tag = (int) r.readVarint(); + if ((tag >>> 3) == 1 && (tag & 7) == 2) parsePackage(r.readBytes(), files); else r.skip(tag & 7); + } + return new ParsedManifest(files); + } + + private static void parsePackage(byte[] bytes, List<ManifestFile> out) { + ProtoReader r = new ProtoReader(bytes); + while (r.hasMore()) { + int tag = (int) r.readVarint(); + int field = tag >>> 3, wire = tag & 7; + if (field == 2 && wire == 2) { ManifestFile f = parseFile(r.readBytes()); if (f != null) out.add(f); } + else r.skip(wire); + } + } + + private static ManifestFile parseFile(byte[] bytes) { + String path = ""; + long size = 0L; + int hashAlgo = 0; + byte[] hashValue = new byte[0]; + ProtoReader r = new ProtoReader(bytes); + while (r.hasMore()) { + int tag = (int) r.readVarint(); + int field = tag >>> 3, wire = tag & 7; + if (field == 1 && wire == 2) path = r.readString(); + else if (field == 3 && wire == 0) size = r.readVarint(); + else if (field == 5 && wire == 2) { + ProtoReader hr = new ProtoReader(r.readBytes()); + while (hr.hasMore()) { + int ht = (int) hr.readVarint(); + int hf = ht >>> 3, hw = ht & 7; + if (hf == 1 && hw == 0) hashAlgo = (int) hr.readVarint(); + else if (hf == 2 && hw == 2) hashValue = hr.readBytes(); + else hr.skip(hw); + } + } else r.skip(wire); + } + if (path.isEmpty()) return null; + return new ManifestFile(path, size, hashAlgo, hashValue); + } + + private static final class ProtoReader { + private final byte[] buf; + private int pos; + ProtoReader(byte[] buf) { this.buf = buf; } + boolean hasMore() { return pos < buf.length; } + long readVarint() { + long result = 0; + int shift = 0; + while (pos < buf.length) { + int b = buf[pos++] & 0xFF; + result |= (long) (b & 0x7F) << shift; + if ((b & 0x80) == 0) break; + shift += 7; + } + return result; + } + byte[] readBytes() { + int len = (int) readVarint(); + if (len < 0 || pos + len > buf.length) return new byte[0]; + byte[] out = Arrays.copyOfRange(buf, pos, pos + len); + pos += len; + return out; + } + String readString() { return new String(readBytes(), java.nio.charset.StandardCharsets.UTF_8); } + void skip(int wireType) { + switch (wireType) { + case 0: readVarint(); break; + case 1: pos += 8; break; + case 2: readBytes(); break; + case 5: pos += 4; break; + default: pos = buf.length; break; + } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonNative.kt new file mode 100644 index 0000000000..511dc946f7 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonNative.kt @@ -0,0 +1,84 @@ +// JNI symbols depend on this package path and class name (app/src/main/rust/stores/JNI.md). +package com.droiddeck.launcher.stores.amazon + +import android.util.Log +import com.droiddeck.launcher.stores.StoresNative +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + +/** + * The native Amazon file fetcher in `libdroiddeckstores.so`: one streamed GET per manifest file, + * SHA-256 checked, written as `<dest>.tmp` and renamed. The manager keeps manifest, auth, markers + * and post-install; [runBlocking] mirrors the Java loop's shape and polls the cancel probe. + */ +object AmazonNative { + const val TAG = "AmazonNative" + private const val CANCEL_POLL_MS = 100L + + /** Callbacks from the engine; every method is called on a native thread. */ + interface Listener { + /** Once up front with the resume-skipped credit, then after each committed file; [bytesDone] includes skipped files. */ + fun onProgress(bytesDone: Long, bytesTotal: Long, filesDone: Long, filesTotal: Long) + /** One diagnostic line; the engine itself does not write to logcat. */ + fun onLog(line: String) + /** Exactly once, when the run succeeds, fails or is cancelled. */ + fun onComplete(success: Boolean, error: String, bytesWritten: Long) + } + + fun interface CancelCheck { fun isCancelled(): Boolean } + + class RunResult(@JvmField val success: Boolean, @JvmField val cancelled: Boolean, @JvmField val error: String, @JvmField val bytesWritten: Long) + + @JvmStatic + fun isAvailable(): Boolean = StoresNative.available + + /** + * Runs one download to completion. [planJson]: `[{relPath, url, size, sha256hex}]`, every + * manifest file (the engine skips complete ones itself). [maxWorkers] <= 0 = the engine's default. + */ + @JvmStatic + fun runBlocking(planJson: String, installDir: String, caBundlePath: String, maxWorkers: Int, processWorkers: Int, isCancelled: CancelCheck?, listener: Listener): RunResult { + if (!StoresNative.ensureLoaded()) return RunResult(false, false, "engine not built", 0L) + val latch = CountDownLatch(1) + var outcome: RunResult? = null + val bridge = object : Listener { + override fun onProgress(bytesDone: Long, bytesTotal: Long, filesDone: Long, filesTotal: Long) = listener.onProgress(bytesDone, bytesTotal, filesDone, filesTotal) + override fun onLog(line: String) = listener.onLog(line) + override fun onComplete(success: Boolean, error: String, bytesWritten: Long) { + outcome = RunResult(success, false, error, bytesWritten) + listener.onComplete(success, error, bytesWritten) + latch.countDown() + } + } + val handle = try { + nativeStart(planJson, installDir, caBundlePath, maxWorkers, processWorkers, bridge) + } catch (t: Throwable) { + Log.w(TAG, "nativeStart threw: ${t.javaClass.simpleName}: ${t.message}") + return RunResult(false, false, "nativeStart: ${t.javaClass.simpleName}", 0L) + } + if (handle == 0L) return outcome ?: RunResult(false, false, "native start failed", 0L) + var cancelSent = false + try { + while (!latch.await(CANCEL_POLL_MS, TimeUnit.MILLISECONDS)) { + if (!cancelSent && isCancelled?.isCancelled() == true) { nativeCancel(handle); cancelSent = true } + } + } catch (_: InterruptedException) { + Thread.currentThread().interrupt() + if (!cancelSent) { nativeCancel(handle); cancelSent = true } + try { latch.await(30, TimeUnit.SECONDS) } catch (_: InterruptedException) {} + } finally { + nativeRelease(handle) + } + val r = outcome ?: RunResult(false, cancelSent, "no completion", 0L) + return RunResult(r.success && !cancelSent, cancelSent, r.error, r.bytesWritten) + } + + @JvmStatic + private external fun nativeStart(planJson: String, installDir: String, caBundlePath: String, maxWorkers: Int, processWorkers: Int, listener: Listener): Long + + @JvmStatic + private external fun nativeCancel(handle: Long) + + @JvmStatic + private external fun nativeRelease(handle: Long) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPkce.java b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPkce.java new file mode 100644 index 0000000000..9d0a064039 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPkce.java @@ -0,0 +1,53 @@ +package com.droiddeck.launcher.stores.amazon; + +import android.util.Base64; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.SecureRandom; +import java.util.UUID; + +/** The PKCE values Amazon's device sign-in takes, generated the way the Amazon Games launcher does. */ +public final class AmazonPkce { + private static final String DEVICE_TYPE = "A2UMVHOX7UP4V7"; + + private AmazonPkce() {} + + /** One per sign-in: a UUID as uppercase hex. */ + public static String generateDeviceSerial() { return UUID.randomUUID().toString().replace("-", "").toUpperCase(); } + + /** hex("serial#DEVICE_TYPE"). */ + public static String generateClientId(String serial) { + byte[] bytes = (serial + "#" + DEVICE_TYPE).getBytes(StandardCharsets.UTF_8); + StringBuilder sb = new StringBuilder(); + for (byte b : bytes) sb.append(String.format("%02x", b & 0xFF)); + return sb.toString(); + } + + public static String generateCodeVerifier() { + byte[] random = new byte[32]; + new SecureRandom().nextBytes(random); + return Base64.encodeToString(random, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); + } + + public static String generateCodeChallenge(String verifier) { + try { + byte[] hash = MessageDigest.getInstance("SHA-256").digest(verifier.getBytes(StandardCharsets.UTF_8)); + return Base64.encodeToString(hash, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); + } catch (Exception e) { + throw new RuntimeException("SHA-256 unavailable", e); + } + } + + /** SHA-256 of the input as uppercase hex: the entitlements call's hardwareHash. */ + public static String sha256Upper(String input) { + try { + byte[] hash = MessageDigest.getInstance("SHA-256").digest(input.getBytes(StandardCharsets.UTF_8)); + StringBuilder sb = new StringBuilder(); + for (byte b : hash) sb.append(String.format("%02x", b & 0xFF)); + return sb.toString().toUpperCase(); + } catch (Exception e) { + throw new RuntimeException("SHA-256 unavailable", e); + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPrefs.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPrefs.kt new file mode 100644 index 0000000000..6469b63af2 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonPrefs.kt @@ -0,0 +1,10 @@ +package com.droiddeck.launcher.stores.amazon + +import android.content.Context +import android.content.SharedPreferences + +/** Amazon's own records that are not credentials: the library cache, posters, versions. Public for the Java clients. */ +object AmazonPrefs { + @JvmStatic + fun get(context: Context): SharedPreferences = context.applicationContext.getSharedPreferences("stores.amazon", Context.MODE_PRIVATE) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadModels.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadModels.kt new file mode 100644 index 0000000000..9813f169d3 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadModels.kt @@ -0,0 +1,75 @@ +package com.droiddeck.launcher.stores.download + +import com.droiddeck.launcher.stores.Store + +/** + * The store-agnostic shape every store's download is projected into, so one queue and one + * Downloads page serve all three. Ported from Bannerlator's download registry, trimmed to what + * this app shows: a stage, one byte pair, speed and ETA, and the handles that drive it. + */ + +/** Where a download is. The names are what the Downloads page prints over its stage strip. */ +enum class DownloadStage { MANIFEST, DOWNLOAD, VERIFY, INSTALL, DONE } + +/** + * QUEUED / RUNNING / PAUSED are the active states (in memory only). INSTALLED is the terminal + * success; FAILED and CANCELLED stay listed for the session so the page can offer a retry or a + * dismissal. + */ +enum class DownloadState { QUEUED, RUNNING, PAUSED, INSTALLED, FAILED, CANCELLED } + +data class DownloadEntry( + val store: Store, + val id: String, + val name: String, + val cover: String? = null, + val state: DownloadState = DownloadState.QUEUED, + val stage: DownloadStage = DownloadStage.MANIFEST, + /** Bytes landed and the total, as the engine reports them (install size). */ + val bytesDone: Long = 0L, + val bytesTotal: Long = 0L, + /** Smoothed speed in bytes/s (0 = unknown) and seconds left (-1 = unknown). */ + val speedBps: Long = 0L, + val etaSeconds: Long = -1L, + /** The engine's current line ("Downloading: data.pak"), shown under the bar. */ + val detail: String = "", + /** Where it was installed (INSTALLED) or why it failed (FAILED). */ + val installPath: String? = null, + val error: String? = null, + /** 1-based place in the queue while QUEUED, else 0. */ + val queuePosition: Int = 0, + /** When the entry reached a terminal state, for ordering and ageing out. */ + val finishedAt: Long = 0L, + val startedAt: Long = 0L, + /** Where it is going: the install target's name ("Internal storage", the card's name). */ + val location: String = "", + /** + * The active stage's own progress outside Download (which runs on [bytesDone]/[bytesTotal]): + * an amount ([stageDone] of [stageTotal], bytes or items) and, when it counts files, [stageItems] + * of [stageItemsTotal]. Reset when the stage changes. + */ + val stageDone: Long = 0L, + val stageTotal: Long = 0L, + val stageItems: Int = 0, + val stageItemsTotal: Int = 0, + /** What the game takes on disk, when the store has said (the download itself is [bytesTotal], often compressed). */ + val diskBytes: Long = 0L, + /** Stages already passed, one bit per [DownloadStage] ordinal; stages can come back (Epic checks files before it fetches). */ + val stagesPassed: Int = 0, +) { + fun passed(s: DownloadStage): Boolean = state == DownloadState.INSTALLED || (stagesPassed and (1 shl s.ordinal)) != 0 + + val key: String get() = "${store.id}:$id" + val isActive: Boolean get() = state == DownloadState.QUEUED || state == DownloadState.RUNNING || state == DownloadState.PAUSED + /** The active stage's progress, 0..1, or -1 while it has nothing to count yet. */ + val stageFraction: Float get() = when { + state == DownloadState.INSTALLED || stage == DownloadStage.DONE -> 1f + stage == DownloadStage.DOWNLOAD -> if (bytesTotal > 0L) (bytesDone.toDouble() / bytesTotal).toFloat().coerceIn(0f, 1f) else -1f + stageTotal > 0L -> (stageDone.toDouble() / stageTotal).toFloat().coerceIn(0f, 1f) + stageItemsTotal > 0 -> (stageItems.toFloat() / stageItemsTotal).coerceIn(0f, 1f) + else -> -1f + } + /** 0..1 for a single bar (a card, the game page): the active stage's progress. */ + val fraction: Float get() = stageFraction.coerceAtLeast(0f) + val percent: Int get() = (fraction * 100).toInt() +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt new file mode 100644 index 0000000000..ac0d585e8d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt @@ -0,0 +1,379 @@ +package com.droiddeck.launcher.stores.download + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.StoresState +import java.util.concurrent.atomic.AtomicBoolean + +/** + * One queue for every store's downloads, [parallel] of them running at a time (1..3, Setup and + * the Downloads page), the rest waiting as data until a slot frees. A store hands in a + * [DownloadJob] - its whole install: manifest, files, verify, post-install - and reports back + * through the job's [JobHandle]; the queue owns the states, the registry row the UI draws and the + * foreground service that keeps the process alive while anything runs. + * + * Pause for these engines is "stop now, keep the files": every store's install skips files that + * are already complete and verified, so resuming is simply running the job again. Cancel removes + * what was downloaded (the job's own cleanup) and the row. + * + * Thread-safe: one [lock] guards the table; jobs run on their own threads, outside it. + */ +object DownloadQueue { + private const val TAG = "StoreDownloads" + + /** The work for one download. [run] blocks until it is over; [cancel] asks a running job to stop. */ + interface DownloadJob { + /** Runs the install; returns the install folder's path on success, throws or returns null on failure. */ + fun run(handle: JobHandle): String? + /** Stops a running job; [deleteFiles] false keeps what was fetched for a later resume. */ + fun cancel(deleteFiles: Boolean) + } + + /** What a running job reports with. */ + class JobHandle internal constructor(val key: String, internal val cancelled: AtomicBoolean) { + val isCancelled: Boolean get() = cancelled.get() + /** Enters [stage] (a repeat of the current one only updates the line); a new stage starts its own count from zero. */ + fun stage(stage: DownloadStage, detail: String = "") = enter(key, stage, stripSpeed(detail)) + /** + * The active stage's own count outside Download: an amount and, when it counts files, the + * files. [bytes] = the amount is bytes written, so the stage gets its own rate and ETA. + */ + fun stageProgress(done: Long, total: Long, items: Int = 0, itemsTotal: Int = 0, bytes: Boolean = false) = stageCount(key, done, total, items, itemsTotal, bytes) + /** The game's size on disk, once the store has said. */ + fun diskSize(bytes: Long) = update(key) { if (bytes > 0) it.copy(diskBytes = bytes) else it } + /** + * Bytes so far (negative = unchanged), the total (<= 0 = unchanged), a line. [speedBps] is the + * engine's own figure: a burst rate at the network side, which jumps while the write side + * paces the bar, so it is not what the page shows - the queue measures the install rate + * from the byte deltas itself ([measure]); the engine's figure stays in its log lines. + */ + fun progress(bytesDone: Long, bytesTotal: Long, detail: String? = null, @Suppress("UNUSED_PARAMETER") speedBps: Long = -1L) = measure(key, bytesDone, bytesTotal, detail?.let(::stripSpeed)) + fun log(line: String) = StoresState.logLine(line) + } + + /** "Downloading: data.pak 12.3 MB/s" → "Downloading: data.pak": the engine's burst rate is not shown twice. */ + private fun stripSpeed(s: String): String = s.replace(Regex("\\s+\\S+ [KMG]B/s\\s*$"), "").trimEnd() + + private fun enter(key: String, stage: DownloadStage, detail: String) { + synchronized(lock) { + val item = items[key] ?: return + val moved = item.entry.stage != stage || item.entry.state != DownloadState.RUNNING + transitionLocked(item, stage) + item.entry = item.entry.copy(detail = detail.ifEmpty { item.entry.detail }, state = DownloadState.RUNNING) + // A new stage shows at once; a new line within the same stage (one per file) waits its turn. + publishLocked(progressOnly = !moved) + } + } + + /** + * Moves an item to [stage] when it is elsewhere: the old stage is marked passed, the new one + * counts from zero, and the measured speed is cleared - the next stage writes or checks, it + * does not fetch, and a stale rate there would read as a download. Caller holds [lock]. + */ + private fun transitionLocked(item: Item, stage: DownloadStage) { + val e = item.entry + if (e.stage == stage) return + item.speedAt = 0L; item.speedBytes = 0L; item.speedEwma = 0.0 + item.entry = e.copy( + stage = stage, stagesPassed = e.stagesPassed or (1 shl e.stage.ordinal), speedBps = 0, etaSeconds = -1, + stageDone = 0, stageTotal = 0, stageItems = 0, stageItemsTotal = 0, + ) + } + + /** How far back the shown speed looks: a few seconds, so the figure settles instead of following each file. */ + private const val SPEED_WINDOW_MS = 3000.0 + + private fun stageCount(key: String, done: Long, total: Long, files: Int, filesTotal: Int, bytes: Boolean) { + synchronized(lock) { + val item = items[key] ?: return + val e = item.entry + var speed = e.speedBps + if (bytes) speed = sample(item, done) + val eta = if (bytes && speed > 0 && total > done) (total - done) / speed else -1L + item.entry = e.copy(stageDone = done, stageTotal = total, stageItems = files, stageItemsTotal = filesTotal, speedBps = if (bytes) speed else 0, etaSeconds = eta) + publishLocked(progressOnly = true) + } + } + + /** One sample of the rate behind [done] (bytes so far in this stage), smoothed over [SPEED_WINDOW_MS]. Caller holds [lock]. */ + private fun sample(item: Item, done: Long): Long { + val now = System.currentTimeMillis() + if (item.speedAt == 0L) { item.speedAt = now; item.speedBytes = done; return item.speedEwma.toLong() } + val dt = now - item.speedAt + // Sampled no faster than every quarter second, as an exponential average over + // SPEED_WINDOW_MS: one big file landing moves it, it does not define it. + if (dt >= 250) { + val inst = (done - item.speedBytes).coerceAtLeast(0) * 1000.0 / dt + val alpha = 1.0 - Math.exp(-dt / SPEED_WINDOW_MS) + item.speedEwma = if (item.speedEwma <= 0.0) inst else item.speedEwma + (inst - item.speedEwma) * alpha + item.speedAt = now; item.speedBytes = done + } + return item.speedEwma.toLong() + } + + private fun measure(key: String, bytesDone: Long, bytesTotal: Long, detail: String?) { + synchronized(lock) { + val item = items[key] ?: return + transitionLocked(item, DownloadStage.DOWNLOAD) + val e = item.entry + val done = if (bytesDone >= 0) bytesDone else e.bytesDone + val total = if (bytesTotal > 0) bytesTotal else e.bytesTotal + if (bytesDone >= 0) sample(item, done) + val speed = item.speedEwma.toLong() + val eta = if (speed > 0 && total > done) (total - done) / speed else -1L + val moved = e.stage != DownloadStage.DOWNLOAD || e.state != DownloadState.RUNNING + item.entry = e.copy(state = DownloadState.RUNNING, stage = DownloadStage.DOWNLOAD, bytesDone = done, bytesTotal = total, detail = detail ?: e.detail, speedBps = speed, etaSeconds = eta) + publishLocked(progressOnly = !moved) + } + } + + private class Item(var entry: DownloadEntry, val factory: () -> DownloadJob, var job: DownloadJob? = null, val cancelled: AtomicBoolean = AtomicBoolean(false), var pauseRequested: Boolean = false) { + /** The shown speed's state: the last sample's clock and bytes, and the running average (bytes/s). */ + var speedAt = 0L + var speedBytes = 0L + var speedEwma = 0.0 + } + + private val lock = Any() + private val items = LinkedHashMap<String, Item>() + private var running = 0 + + fun parallel(context: Context): Int = SessionPrefs.gameStoresParallel(context) + + /** Threads for a store's download pool: background priority, so the UI and its render thread come first. */ + @JvmStatic + fun workerFactory(name: String): java.util.concurrent.ThreadFactory { + val n = java.util.concurrent.atomic.AtomicInteger() + return java.util.concurrent.ThreadFactory { r -> + Thread({ + android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND) + r.run() + }, "$name-${n.incrementAndGet()}") + } + } + + fun setParallel(context: Context, count: Int) { + SessionPrefs.setGameStoresParallel(context, count) + advance(context.applicationContext) + } + + fun entry(key: String): DownloadEntry? = synchronized(lock) { items[key]?.entry } + + fun isActive(key: String): Boolean = entry(key)?.isActive == true + + /** + * Queues a download, or starts it when a slot is free. A key already active is left alone; a + * finished or failed row with the same key is replaced. + */ + fun enqueue(context: Context, entry: DownloadEntry, factory: () -> DownloadJob) { + val app = context.applicationContext + synchronized(lock) { + items[entry.key]?.let { if (it.entry.isActive) { Log.i(TAG, "${entry.key}: already queued"); return } } + items.remove(entry.key) + items[entry.key] = Item(entry.copy(state = DownloadState.QUEUED, stage = DownloadStage.MANIFEST, startedAt = System.currentTimeMillis()), factory) + renumber() + } + StoresState.logLine("${entry.store.label}: queued \"${entry.name}\"" + if (entry.bytesTotal > 0) " (${com.droiddeck.launcher.stores.formatBytes(entry.bytesTotal)})" else "") + publish(app) + advance(app) + } + + fun pause(key: String) { + val item = synchronized(lock) { items[key] } ?: return + synchronized(lock) { + if (item.entry.state == DownloadState.QUEUED) { item.entry = item.entry.copy(state = DownloadState.PAUSED, queuePosition = 0); renumber(); publishLocked(); return } + if (item.entry.state != DownloadState.RUNNING) return + item.pauseRequested = true + } + item.cancelled.set(true) + item.job?.cancel(deleteFiles = false) + StoresState.logLine("paused \"${item.entry.name}\"") + } + + fun resume(context: Context, key: String) { + val app = context.applicationContext + synchronized(lock) { + val item = items[key] ?: return + if (item.entry.state != DownloadState.PAUSED && item.entry.state != DownloadState.FAILED) return + items.remove(key) + items[key] = Item(item.entry.copy(state = DownloadState.QUEUED, speedBps = 0, etaSeconds = -1, error = null, startedAt = System.currentTimeMillis()), item.factory) + renumber() + publishLocked() + } + StoresState.logLine("resumed \"${entry(key)?.name}\"") + advance(app) + } + + fun retry(context: Context, key: String) = resume(context, key) + + fun cancel(context: Context, key: String) { + val app = context.applicationContext + val item: Item + val wasRunning: Boolean + synchronized(lock) { + item = items[key] ?: return + wasRunning = item.entry.state == DownloadState.RUNNING + if (!wasRunning) { + item.entry = item.entry.copy(state = DownloadState.CANCELLED, queuePosition = 0, finishedAt = System.currentTimeMillis()) + renumber() + publishLocked() + } + } + if (wasRunning) { + item.cancelled.set(true) + item.job?.cancel(deleteFiles = true) + } else { + // Never started: nothing is on disk, but the job knows where it would have gone. + Thread({ runCatching { item.factory().cancel(deleteFiles = true) } }, "store-dl-cancel").start() + } + StoresState.logLine("cancelled \"${item.entry.name}\"") + StoreDownloadService.finish(app, key) + advance(app) + // The row says Cancelled for a moment, then goes; the game reads Install again. + Thread({ Thread.sleep(3000); synchronized(lock) { if (items[key]?.entry?.state == DownloadState.CANCELLED) { items.remove(key); publishLocked() } } }, "store-dl-cancelled").start() + } + + /** + * Clear on a failed or paused row: deletes what it kept exactly as Cancel does (the job's own + * cleanup), then drops the row. An installed or cancelled row is only dropped. + */ + fun clear(context: Context, key: String) { + 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() + } + synchronized(lock) { items.remove(key); publishLocked() } + StoreDownloadService.finish(context.applicationContext, key) + } + + /** Drops every finished, failed or cancelled row; files a failed one kept stay (its game page's Clear removes them). */ + fun dismissFinished() { + synchronized(lock) { + items.entries.removeAll { !it.value.entry.isActive } + publishLocked() + } + } + + /** Drops a finished, failed or cancelled row from the list. */ + fun dismiss(key: String) { + synchronized(lock) { + val item = items[key] ?: return + if (item.entry.isActive) return + items.remove(key) + publishLocked() + } + } + + private fun renumber() { + var pos = 1 + for (item in items.values) if (item.entry.state == DownloadState.QUEUED) item.entry = item.entry.copy(queuePosition = pos++) + } + + private fun update(key: String, transform: (DownloadEntry) -> DownloadEntry) { + synchronized(lock) { + val item = items[key] ?: return + item.entry = transform(item.entry) + publishLocked() + } + } + + private fun publish(context: Context) { synchronized(lock) { publishLocked() }; StoreDownloadService.sync(context) } + + /** At most this often a progress-only change reaches the UI; a state or stage change goes at once. */ + private const val PROGRESS_PUBLISH_MS = 250L + private var lastPublishAt = 0L + private var publishPending = false + private val main = android.os.Handler(android.os.Looper.getMainLooper()) + + /** + * Newest first: what was started last sits on top, as the preview lists them. Caller holds + * [lock]. Engines report per chunk or per file - hundreds of times a second at full speed - so + * [progressOnly] changes are coalesced to [PROGRESS_PUBLISH_MS]; the UI then recomposes a few + * times a second, not on every callback. + */ + private fun publishLocked(progressOnly: Boolean = false) { + val now = android.os.SystemClock.uptimeMillis() + if (progressOnly) { + val wait = lastPublishAt + PROGRESS_PUBLISH_MS - now + if (wait > 0) { + if (!publishPending) { + publishPending = true + main.postDelayed({ synchronized(lock) { publishPending = false; publishLocked() } }, wait) + } + return + } + } + lastPublishAt = now + val list = items.values.map { it.entry }.sortedByDescending { it.startedAt } + StoresState.post { StoresState.publishDownloads(list) } + } + + /** Starts queued items while slots are free. */ + private fun advance(context: Context) { + while (true) { + val next: Item + synchronized(lock) { + if (running >= parallel(context)) return + next = items.values.firstOrNull { it.entry.state == DownloadState.QUEUED } ?: return + running++ + next.entry = next.entry.copy(state = DownloadState.RUNNING, stage = DownloadStage.MANIFEST, queuePosition = 0) + renumber() + publishLocked() + } + StoreDownloadService.start(context) + Thread({ runItem(context, next) }, "store-dl-${next.entry.key}").start() + } + } + + private fun runItem(context: Context, item: Item) { + // Downloads run beside the UI, never ahead of it: background priority for this thread and + // (through [workerFactory]) every pool a store's manager starts. + android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND) + val handle = JobHandle(item.entry.key, item.cancelled) + var result: String? = null + var error: String? = null + try { + val job = item.factory() + synchronized(lock) { item.job = job } + StoresState.logLine("${item.entry.store.label}: fetching manifest for \"${item.entry.name}\"") + result = job.run(handle) + if (result == null && !item.cancelled.get()) error = "install failed" + } catch (t: Throwable) { + error = t.message ?: t.javaClass.simpleName + Log.w(TAG, "${item.entry.key} failed", t) + } + synchronized(lock) { + running-- + val now = System.currentTimeMillis() + item.entry = when { + item.cancelled.get() && item.pauseRequested -> item.entry.copy(state = DownloadState.PAUSED, speedBps = 0, etaSeconds = -1) + item.cancelled.get() -> item.entry.copy(state = DownloadState.CANCELLED, finishedAt = now, speedBps = 0, etaSeconds = -1) + result != null -> item.entry.copy(state = DownloadState.INSTALLED, stage = DownloadStage.DONE, installPath = result, bytesDone = item.entry.bytesTotal, speedBps = 0, etaSeconds = -1, finishedAt = now) + else -> item.entry.copy(state = DownloadState.FAILED, error = error, speedBps = 0, etaSeconds = -1, finishedAt = now) + } + item.job = null + publishLocked() + } + when (item.entry.state) { + DownloadState.INSTALLED -> { + StoresState.logLine("installed \"${item.entry.name}\" → ${result}") + // The folder is on disk and registered: the Installed tab and the Games list follow. + StoresState.notifyLibraryChanged(context) + val games = runCatching { + com.droiddeck.launcher.frontend.Library.launchableGames(context).firstOrNull { it.gameFiles?.absolutePath == result }?.let { "app:${it.appId}" } + }.getOrNull() + StoreDownloadService.ended(context, item.entry, installed = true, gamesKey = games) + } + DownloadState.FAILED -> { + StoresState.logLine("${item.entry.store.label}: \"${item.entry.name}\" failed: $error") + StoreDownloadService.ended(context, item.entry, installed = false, gamesKey = null) + } + DownloadState.CANCELLED -> StoresState.notifyLibraryChanged(context) + else -> {} + } + StoreDownloadService.finish(context, item.entry.key) + advance(context) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt new file mode 100644 index 0000000000..2ea30d37b8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadService.kt @@ -0,0 +1,299 @@ +package com.droiddeck.launcher.stores.download + +import android.annotation.SuppressLint +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.net.wifi.WifiManager +import android.os.Build +import android.os.Handler +import android.os.IBinder +import android.os.Looper +import android.os.PowerManager +import android.util.Log +import com.droiddeck.launcher.MainActivity +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.formatBytes +import com.droiddeck.launcher.stores.formatSpeed +import java.util.concurrent.ConcurrentHashMap + +/** + * Keeps store downloads going with the screen off or the app in the background: a foreground + * service (dataSync) with a notification showing the active download's progress, Pause / Resume + * and a two-step Cancel; a Wi-Fi lock and a partial wake lock while anything is running, so the + * radio and the CPU do not sleep under a multi-gigabyte install; and a notification when a game + * has installed or failed. + * + * [start] is called when a download begins and [finish] when one ends; the service stops itself when + * nothing is running. + */ +class StoreDownloadService : Service() { + private var wifi: WifiManager.WifiLock? = null + private var wake: PowerManager.WakeLock? = null + private var wakeAt = 0L + /** The download whose Cancel was tapped once: its actions read Delete / Keep until [confirmUntil]. */ + private var confirmKey: String? = null + private var confirmUntil = 0L + private val main = Handler(Looper.getMainLooper()) + + override fun onCreate() { + super.onCreate() + instance = this + channels(this) + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + instance = this + startForegroundCompat(build()) + val key = intent?.getStringExtra(EXTRA_KEY) + when (intent?.action) { + ACTION_PAUSE -> key?.let { + DownloadQueue.pause(it) + // The progress notification goes with the last running download; this one stays to resume from. + pausedNote(this, it) + } + ACTION_RESUME -> key?.let { + runCatching { getSystemService(NotificationManager::class.java)?.cancel(pausedId(it)) } + DownloadQueue.resume(this, it) + } + ACTION_CANCEL -> key?.let { + confirmKey = it; confirmUntil = System.currentTimeMillis() + CONFIRM_MS + main.postDelayed({ if (System.currentTimeMillis() >= confirmUntil) { confirmKey = null; refresh() } }, CONFIRM_MS + 100) + } + ACTION_DELETE -> key?.let { confirmKey = null; DownloadQueue.cancel(this, it) } + ACTION_KEEP -> confirmKey = null + } + refresh() + if (active.isEmpty() && StoresState.latestDownloads.none { it.state == DownloadState.RUNNING }) stopNow() + return START_NOT_STICKY + } + + override fun onDestroy() { + releaseLocks() + if (instance === this) instance = null + super.onDestroy() + } + + override fun onBind(intent: Intent?): IBinder? = null + + private fun pending(action: String, key: String, code: Int): PendingIntent { + val i = Intent(this, StoreDownloadService::class.java).setAction(action).putExtra(EXTRA_KEY, key) + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) PendingIntent.getForegroundService(this, code, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + else PendingIntent.getService(this, code, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + } + + private fun build(): Notification { + val rows = StoresState.latestDownloads.filter { it.isActive } + val first = rows.firstOrNull { it.state == DownloadState.RUNNING } ?: rows.firstOrNull() + val title = if (rows.size > 1) getString(R.string.stores_notification_many, rows.size) else first?.name ?: getString(R.string.stores_notification_one) + val b = Notification.Builder(this, CHANNEL) + .setSmallIcon(android.R.drawable.stat_sys_download) + .setContentTitle(title) + .setOngoing(true).setOnlyAlertOnce(true) + .setContentIntent(openDownloads(this, 1)) + if (first == null) return b.setContentText(getString(R.string.stores_notification_waiting)).setProgress(0, 0, true).build() + b.setContentText(DownloadNotificationText.line(first, stageName(first.stage), getString(R.string.stores_dl_paused), getString(R.string.stores_dl_queued))) + val fraction = first.stageFraction + if (fraction >= 0f) b.setProgress(1000, (fraction * 1000).toInt(), false) else b.setProgress(0, 0, true) + val code = first.key.hashCode() + if (confirmKey == first.key && System.currentTimeMillis() < confirmUntil) { + b.addAction(Notification.Action.Builder(null, getString(R.string.stores_notification_delete), pending(ACTION_DELETE, first.key, code + 3)).build()) + b.addAction(Notification.Action.Builder(null, getString(R.string.stores_notification_keep), pending(ACTION_KEEP, first.key, code + 4)).build()) + } else { + if (first.state == DownloadState.PAUSED) b.addAction(Notification.Action.Builder(null, getString(R.string.stores_dl_resume), pending(ACTION_RESUME, first.key, code + 1)).build()) + else b.addAction(Notification.Action.Builder(null, getString(R.string.stores_dl_pause), pending(ACTION_PAUSE, first.key, code)).build()) + b.addAction(Notification.Action.Builder(null, getString(R.string.stores_dl_cancel), pending(ACTION_CANCEL, first.key, code + 2)).build()) + } + return b.build() + } + + private fun stageName(stage: DownloadStage): String = getString(when (stage) { + DownloadStage.MANIFEST -> R.string.stores_stage_manifest + DownloadStage.DOWNLOAD -> R.string.stores_stage_downloading + DownloadStage.VERIFY -> R.string.stores_stage_verifying + DownloadStage.INSTALL -> R.string.stores_stage_installing + DownloadStage.DONE -> R.string.stores_stage_done + }) + + private fun refresh() { + locks() + runCatching { (getSystemService(NOTIFICATION_SERVICE) as NotificationManager).notify(ID, build()) } + } + + /** + * While a download runs: a Wi-Fi lock (high-performance before Android 14, low-latency from it, + * where the old mode is deprecated) and a partial wake lock, taken with a timeout and renewed + * as progress comes in rather than held indefinitely. Released as soon as nothing runs. + */ + @SuppressLint("WakelockTimeout") + private fun locks() { + val running = StoresState.latestDownloads.any { it.state == DownloadState.RUNNING } + if (!running) { releaseLocks(); return } + if (wifi == null) { + runCatching { + val wm = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager + @Suppress("DEPRECATION") + val mode = if (Build.VERSION.SDK_INT >= 34) WifiManager.WIFI_MODE_FULL_LOW_LATENCY else WifiManager.WIFI_MODE_FULL_HIGH_PERF + wifi = wm.createWifiLock(mode, "DroidDeck:store-download").apply { setReferenceCounted(false); acquire() } + Log.i(TAG, "wifi lock held") + } + } + val now = System.currentTimeMillis() + if (wake == null) { + wake = (getSystemService(POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DroidDeck:store-download").apply { setReferenceCounted(false) } + Log.i(TAG, "wake lock held") + } + if (now - wakeAt > WAKE_RENEW_MS) { + wake?.acquire(WAKE_TIMEOUT_MS) + wakeAt = now + } + } + + private fun releaseLocks() { + wifi?.let { runCatching { if (it.isHeld) it.release() }; Log.i(TAG, "wifi lock released") } + wifi = null + wake?.let { runCatching { if (it.isHeld) it.release() }; Log.i(TAG, "wake lock released") } + wake = null + wakeAt = 0L + } + + private fun startForegroundCompat(n: Notification) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) startForeground(ID, n, android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC) + else startForeground(ID, n) + } + + private fun stopNow() { + releaseLocks() + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + } + + companion object { + private const val TAG = "StoreDownloadService" + private const val CHANNEL = "store_downloads" + private const val DONE_CHANNEL = "store_downloads_done" + private const val ID = 9102 + private const val CONFIRM_MS = 10_000L + private const val WAKE_TIMEOUT_MS = 10 * 60_000L + private const val WAKE_RENEW_MS = 5 * 60_000L + private const val EXTRA_KEY = "key" + private const val ACTION_PAUSE = "droiddeck.stores.PAUSE" + private const val ACTION_RESUME = "droiddeck.stores.RESUME" + private const val ACTION_CANCEL = "droiddeck.stores.CANCEL" + private const val ACTION_DELETE = "droiddeck.stores.DELETE" + private const val ACTION_KEEP = "droiddeck.stores.KEEP" + /** MainActivity: which page to open ("stores:downloads", or a Games selection such as "app:<id>"). */ + const val EXTRA_NAV = "droiddeck.nav" + private val active = ConcurrentHashMap.newKeySet<String>() + @Volatile private var instance: StoreDownloadService? = null + + private fun channels(context: Context) { + val nm = context.getSystemService(NotificationManager::class.java) ?: return + if (nm.getNotificationChannel(CHANNEL) == null) { + nm.createNotificationChannel(NotificationChannel(CHANNEL, context.getString(R.string.stores_notification_channel), NotificationManager.IMPORTANCE_LOW).apply { + description = context.getString(R.string.stores_notification_channel_hint) + setShowBadge(false) + }) + } + if (nm.getNotificationChannel(DONE_CHANNEL) == null) { + nm.createNotificationChannel(NotificationChannel(DONE_CHANNEL, context.getString(R.string.stores_notification_done_channel), NotificationManager.IMPORTANCE_DEFAULT)) + } + } + + private fun openPage(context: Context, nav: String, code: Int): PendingIntent = + PendingIntent.getActivity(context, code, Intent(context, MainActivity::class.java).putExtra(EXTRA_NAV, nav) + .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP), PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + + private fun openDownloads(context: Context, code: Int) = openPage(context, "stores:downloads", code) + + /** A download started: the service is up (idempotent). */ + fun start(context: Context) { + StoresState.latestDownloads.filter { it.state == DownloadState.RUNNING }.forEach { active.add(it.key) } + try { + val app = context.applicationContext + val intent = Intent(app, StoreDownloadService::class.java) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) app.startForegroundService(intent) else app.startService(intent) + } catch (e: Exception) { + Log.w(TAG, "could not start: ${e.message}") + } + } + + /** Progress moved: redraw the notification (StoresState calls it at most once a second). */ + fun tick() { + instance?.refresh() + } + + /** The queue changed: redraw the notification. */ + fun sync(context: Context) { + instance?.refresh() + } + + /** A download ended: drop it; the service stops with the last one. */ + fun finish(context: Context, key: String) { + active.remove(key) + val inst = instance ?: return + if (active.isEmpty() && StoresState.latestDownloads.none { it.state == DownloadState.RUNNING }) inst.stopNow() else inst.refresh() + } + + private fun pausedId(key: String) = ID + 0x20000 + (key.hashCode() and 0xFFFF) + + /** "<game> paused" with Resume, once the progress notification has gone with the service. */ + private fun pausedNote(context: Context, key: String) { + val entry = StoresState.latestDownloads.firstOrNull { it.key == key } ?: return + val i = Intent(context, StoreDownloadService::class.java).setAction(ACTION_RESUME).putExtra(EXTRA_KEY, key) + val resume = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) PendingIntent.getForegroundService(context, key.hashCode() + 5, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + else PendingIntent.getService(context, key.hashCode() + 5, i, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT) + val n = Notification.Builder(context, DONE_CHANNEL) + .setSmallIcon(android.R.drawable.stat_sys_download) + .setContentTitle(entry.name).setContentText(context.getString(R.string.stores_dl_paused)) + .setContentIntent(resume).setAutoCancel(true) + .addAction(Notification.Action.Builder(null, context.getString(R.string.stores_dl_resume), resume).build()) + .build() + runCatching { context.getSystemService(NotificationManager::class.java)?.notify(pausedId(key), n) } + } + + /** "<game> installed" (opens it in Games) or "<game> download failed" (opens Downloads); dismissible. */ + fun ended(context: Context, entry: DownloadEntry, installed: Boolean, gamesKey: String?) { + val app = context.applicationContext + channels(app) + val code = entry.key.hashCode() + val n = 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() + runCatching { app.getSystemService(NotificationManager::class.java)?.notify(ID + 1 + (code and 0xFFFF), n) } + } + } +} + +/** The notification's one line for a download. Pure, for the test. */ +object DownloadNotificationText { + fun line(d: DownloadEntry, stageName: String, paused: String, queued: String): String { + val parts = ArrayList<String>() + parts += d.name + when { + d.state == DownloadState.PAUSED -> parts += paused + d.state == DownloadState.QUEUED -> parts += queued + d.stage == DownloadStage.DOWNLOAD -> { + if (d.bytesTotal > 0) { parts += "${d.percent}%"; parts += "${formatBytes(d.bytesDone)}/${formatBytes(d.bytesTotal)}" } + if (d.speedBps > 0) parts += formatSpeed(d.speedBps) + if (d.etaSeconds >= 0) parts += eta(d.etaSeconds) + } + else -> parts += if (d.stageFraction >= 0f) "$stageName ${d.percent}%" else stageName + } + return parts.joinToString(" · ") + } + + fun eta(seconds: Long): String = when { + seconds < 60 -> "<1 min" + seconds < 3600 -> "${seconds / 60} min" + else -> "${seconds / 3600} h ${(seconds % 3600) / 60} min" + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadTier.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadTier.kt new file mode 100644 index 0000000000..3b12de899a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/download/StoreDownloadTier.kt @@ -0,0 +1,33 @@ +package com.droiddeck.launcher.stores.download + +import android.content.Context +import androidx.annotation.StringRes +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionPrefs + +/** + * How hard the native download engine may pull: the ceiling of chunk requests it keeps in flight, + * and the inflate / hash / write threads behind them. Three tiers, one app-wide setting (Setup › + * Stores and the Downloads page), read when a download starts so a change applies to the next one. + * + * The windows are the ones the engine was tuned with (16 / 32 / 96 requests): it ramps towards the + * ceiling only while throughput keeps rising, so a slow link settles well below any of them. + */ +enum class StoreDownloadTier(val id: String, @StringRes val label: Int, val networkWindow: Int, val decompressPerCore: Double) { + BALANCED("balanced", R.string.stores_tier_balanced, 16, 0.4), + FAST("fast", R.string.stores_tier_fast, 32, 0.5), + MAX("max", R.string.stores_tier_max, 96, 0.8); + + /** Inflate / hash / write workers for this device, at least two so a fetch never waits on a single writer. */ + /** Inflate / hash / write threads: by the tier, but two cores always left for the UI (at least 2 workers). */ + val processWorkers: Int get() { + val cores = Runtime.getRuntime().availableProcessors() + return (cores * decompressPerCore).toInt().coerceIn(2, maxOf(2, minOf(32, cores - 2))) + } + + companion object { + val ALL: List<StoreDownloadTier> = entries + fun byId(id: String?): StoreDownloadTier = entries.firstOrNull { it.id == id } ?: FAST + fun current(context: Context): StoreDownloadTier = byId(SessionPrefs.gameStoresSpeedTier(context)) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicApiClient.java b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicApiClient.java new file mode 100644 index 0000000000..3497d37da8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicApiClient.java @@ -0,0 +1,196 @@ +package com.droiddeck.launcher.stores.epic; + +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +/** + * Epic's library, catalog and launcher-assets services, as the launcher calls them: + * library-service.live.use1a.on.epicgames.com/library/api/public/items (owned items, paginated) + * catalog-public-service-prod06.ol.epicgames.com/catalog/api/shared/namespace/{ns}/bulk/items + * launcher-public-service-prod06.ol.epicgames.com/launcher/api/public/assets/v2/platform/Windows/... + * All with a bearer token. + */ +public final class EpicApiClient { + + private static final String TAG = "EpicApi"; + private static final String LIBRARY_URL = "https://library-service.live.use1a.on.epicgames.com/library/api/public/items?includeMetadata=true"; + private static final String CATALOG_BASE = "https://catalog-public-service-prod06.ol.epicgames.com/catalog/api/shared/namespace"; + private static final String MANIFEST_BASE = "https://launcher-public-service-prod06.ol.epicgames.com/launcher/api/public/assets/v2/platform/Windows/namespace"; + private static final String LEGENDARY_UA = "Legendary/0.1.0 (GameNative)"; + + private EpicApiClient() {} + + /** The owned games, every page; Unreal assets, private sandboxes and non-Windows items left out. */ + public static List<EpicGame> getLibraryItems(String accessToken) { + List<EpicGame> result = new ArrayList<>(); + String cursor = null; + do { + try { + String url = LIBRARY_URL + (cursor != null ? "&cursor=" + cursor : ""); + String resp = EpicAuthClient.getRequest(url, accessToken); + if (resp == null) break; + JSONObject json = new JSONObject(resp); + JSONArray records = json.optJSONArray("records"); + if (records == null) break; + for (int i = 0; i < records.length(); i++) { + JSONObject rec = records.getJSONObject(i); + String appName = rec.optString("appName", ""); + if (appName.isEmpty() || appName.equals("1")) continue; + String namespace = rec.optString("namespace", ""); + if ("ue".equals(namespace) || "89efe5924d3d467c839449ab6ab52e7f".equals(namespace)) continue; + if ("PRIVATE".equals(rec.optString("sandboxType", ""))) continue; + JSONArray platforms = rec.optJSONArray("platform"); + if (platforms != null && platforms.length() > 0) { + boolean windows = false; + for (int j = 0; j < platforms.length(); j++) { + String p = platforms.optString(j, ""); + if ("Windows".equals(p) || "Win32".equals(p)) { windows = true; break; } + } + if (!windows) continue; + } + EpicGame game = new EpicGame(); + game.appName = appName; + game.namespace = namespace; + game.catalogItemId = rec.optString("catalogItemId", ""); + result.add(game); + } + JSONObject meta = json.optJSONObject("responseMetadata"); + String next = meta != null ? meta.optString("nextCursor", null) : null; + cursor = (next == null || next.isEmpty() || next.equals(cursor)) ? null : next; + } catch (Exception e) { + Log.e(TAG, "library page failed: " + e.getClass().getSimpleName()); + break; + } + } while (cursor != null); + return result; + } + + /** Fills title, developer, art, description and the DLC flag from the catalog; true on success. */ + public static boolean enrichFromCatalog(String accessToken, EpicGame game) { + if (game.namespace.isEmpty() || game.catalogItemId.isEmpty()) return false; + try { + String url = CATALOG_BASE + "/" + game.namespace + "/bulk/items?id=" + game.catalogItemId + "&includeDLCDetails=true&includeMainGameDetails=true&country=US"; + String resp = getWithLegendaryUA(url, accessToken); + if (resp == null) return false; + JSONObject root = new JSONObject(resp); + JSONObject item = root.optJSONObject(game.catalogItemId); + if (item == null && root.length() > 0) item = root.optJSONObject(root.keys().next()); + if (item == null) return false; + game.title = item.optString("title", game.title.isEmpty() ? game.appName : game.title); + game.developer = item.optString("developer", ""); + game.description = item.optString("description", ""); + game.isDLC = item.has("mainGameItem"); + if (game.isDLC) { + JSONObject main = item.optJSONObject("mainGameItem"); + if (main != null) game.baseGameCatalogItemId = main.optString("id", ""); + } + JSONArray keyImages = item.optJSONArray("keyImages"); + if (keyImages != null) for (int i = 0; i < keyImages.length(); i++) { + JSONObject img = keyImages.getJSONObject(i); + String type = img.optString("type", ""), imgUrl = img.optString("url", ""); + if (imgUrl.isEmpty()) continue; + if ("DieselGameBoxTall".equals(type)) game.artCover = imgUrl; + else if (("DieselGameBox".equals(type) || "Thumbnail".equals(type)) && game.artSquare.isEmpty()) game.artSquare = imgUrl; + } + JSONObject attrs = item.optJSONObject("customAttributes"); + if (attrs != null) { + JSONObject offline = attrs.optJSONObject("CanRunOffline"); + if (offline != null) game.canRunOffline = !"false".equalsIgnoreCase(offline.optString("value", "true")); + } + String rd = item.optString("viewableDate", ""); + if (rd.isEmpty()) rd = item.optString("effectiveDate", ""); + if (!rd.isEmpty()) game.releaseDate = rd; + return true; + } catch (Exception e) { + Log.e(TAG, "catalog enrich failed for " + game.appName + ": " + e.getClass().getSimpleName()); + return false; + } + } + + /** + * The manifest API answer for a game, flattened to {"manifests": [...], "versionId": "..."} as + * the download manager reads it. {@code appName} is the library record's, not the catalog's. + */ + public static String getManifestApiJson(String accessToken, String namespace, String catalogItemId, String appName) { + try { + String url = MANIFEST_BASE + "/" + namespace + "/catalogItem/" + catalogItemId + "/app/" + appName + "/label/Live"; + String resp = getWithLegendaryUA(url, accessToken); + if (resp == null) return null; + JSONObject root = new JSONObject(resp); + JSONArray elements = root.optJSONArray("elements"); + if (elements == null || elements.length() == 0) return null; + JSONObject first = elements.getJSONObject(0); + JSONObject wrapper = new JSONObject(); + wrapper.put("manifests", first.optJSONArray("manifests")); + String ver = first.optString("buildVersion", ""); + if (ver.isEmpty()) ver = first.optString("versionId", ""); + wrapper.put("versionId", ver); + // The sidecar's EOS deployment id, when the app has one. + JSONObject sidecar = first.optJSONObject("sidecar"); + if (sidecar != null) { + String config = sidecar.optString("config", ""); + if (!config.isEmpty()) { + try { wrapper.put("deploymentId", new JSONObject(config).optString("deploymentId", "")); } catch (Exception ignored) {} + } + } + return wrapper.toString(); + } catch (Exception e) { + Log.e(TAG, "manifest API failed for " + appName + ": " + e.getClass().getSimpleName()); + return null; + } + } + + /** `customAttributes.AdditionalCommandLine.value` for the game, "" when it has none, null when the call failed. */ + public static String getAdditionalCommandLine(String accessToken, String namespace, String catalogItemId) { + return getCustomAttribute(accessToken, namespace, catalogItemId, "AdditionalCommandLine"); + } + + /** `customAttributes.<name>.value` for the game (CloudSaveFolder, AdditionalCommandLine), "" when it has none, null when the call failed. */ + public static String getCustomAttribute(String accessToken, String namespace, String catalogItemId, String name) { + try { + String url = CATALOG_BASE + "/" + namespace + "/bulk/items?id=" + catalogItemId + "&country=US"; + String resp = getWithLegendaryUA(url, accessToken); + if (resp == null) return null; + JSONObject root = new JSONObject(resp); + JSONObject item = root.optJSONObject(catalogItemId); + if (item == null && root.length() > 0) item = root.optJSONObject(root.keys().next()); + if (item == null) return ""; + JSONObject attrs = item.optJSONObject("customAttributes"); + if (attrs == null) return ""; + JSONObject attr = attrs.optJSONObject(name); + return attr == null ? "" : attr.optString("value", ""); + } catch (Exception e) { + Log.w(TAG, "catalog attribute " + name + ": " + e.getClass().getSimpleName()); + return null; + } + } + + static String getWithLegendaryUA(String urlStr, String accessToken) { + java.net.HttpURLConnection conn = null; + try { + conn = (java.net.HttpURLConnection) new java.net.URL(urlStr).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(30000); + conn.setRequestProperty("Authorization", "Bearer " + accessToken); + conn.setRequestProperty("User-Agent", LEGENDARY_UA); + int code = conn.getResponseCode(); + java.io.InputStream is = (code < 400) ? conn.getInputStream() : conn.getErrorStream(); + String body = EpicAuthClient.readStream(is); + if (code < 200 || code >= 300) { Log.e(TAG, "GET HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); return null; } + return body; + } catch (Exception e) { + Log.e(TAG, "GET failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } finally { + if (conn != null) conn.disconnect(); + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicAuthClient.java b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicAuthClient.java new file mode 100644 index 0000000000..959d4302b9 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicAuthClient.java @@ -0,0 +1,181 @@ +package com.droiddeck.launcher.stores.epic; + +import android.util.Base64; +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; + +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + +/** + * Epic's OAuth2 token endpoint, with the launcher's public client credentials (the ones every + * third-party Epic client uses): + * POST account-public-service-prod03.ol.epicgames.com/account/api/oauth/token + * Basic base64(clientId:clientSecret), form grant_type=authorization_code|refresh_token, token_type=eg1 + * GET .../account/api/oauth/exchange with a bearer token: a short-lived exchange code for a launch. + * Error bodies are never logged: they carry correlation ids and account context. + */ +public final class EpicAuthClient { + + private static final String TAG = "EpicAuth"; + + static final String CLIENT_ID = "34a02cf8f4414e29b15921876da36f9a"; + static final String CLIENT_SECRET = "daafbccc737745039dffe53d94fc76cf"; + private static final String TOKEN_URL = "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/token"; + private static final String EXCHANGE_URL = "https://account-public-service-prod03.ol.epicgames.com/account/api/oauth/exchange"; + public static final String USER_AGENT = "UELauncher/11.0.1-14907503+++Portal+Release-Live Windows/10.0.19041.1.256.64bit"; + + public static final class TokenResult { + public String accessToken; + public String refreshToken; + public String accountId; + public String displayName; + public long expiresAt; + } + + private EpicAuthClient() {} + + public static TokenResult exchangeCode(String authCode) { + return postToken("grant_type=authorization_code&code=" + authCode + "&token_type=eg1"); + } + + public static TokenResult refreshToken(String refreshToken) { + return postToken("grant_type=refresh_token&refresh_token=" + refreshToken + "&token_type=eg1"); + } + + private static TokenResult postToken(String formBody) { + try { + String creds64 = Base64.encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes(StandardCharsets.UTF_8), Base64.NO_WRAP); + HttpURLConnection conn = (HttpURLConnection) new URL(TOKEN_URL).openConnection(); + conn.setRequestMethod("POST"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(30000); + conn.setDoOutput(true); + conn.setRequestProperty("Authorization", "Basic " + creds64); + conn.setRequestProperty("User-Agent", USER_AGENT); + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); + byte[] bodyBytes = formBody.getBytes(StandardCharsets.UTF_8); + conn.setRequestProperty("Content-Length", String.valueOf(bodyBytes.length)); + try (OutputStream os = conn.getOutputStream()) { os.write(bodyBytes); } + int code = conn.getResponseCode(); + InputStream is = (code < 400) ? conn.getInputStream() : conn.getErrorStream(); + String resp = readStream(is); + conn.disconnect(); + if (code < 200 || code >= 300) { Log.e(TAG, "token HTTP " + code); return null; } + JSONObject json = new JSONObject(resp); + TokenResult result = new TokenResult(); + result.accessToken = json.optString("access_token", null); + result.refreshToken = json.optString("refresh_token", null); + result.accountId = json.optString("account_id", ""); + result.displayName = json.optString("displayName", ""); + long expiresIn = json.optLong("expires_in", 7200L); + String expiresAtStr = json.optString("expires_at", null); + long expiresAtMs; + if (expiresAtStr != null && !expiresAtStr.isEmpty()) { + try { expiresAtMs = parseIso8601(expiresAtStr); } catch (Exception e) { expiresAtMs = System.currentTimeMillis() + expiresIn * 1000L; } + } else expiresAtMs = System.currentTimeMillis() + expiresIn * 1000L; + result.expiresAt = expiresAtMs; + return result.accessToken != null ? result : null; + } catch (Exception e) { + Log.e(TAG, "token request failed: " + e.getClass().getSimpleName()); + return null; + } + } + + /** A short-lived exchange code for a game launch (-AUTH_PASSWORD); null on any failure. */ + public static String getExchangeCode(String accessToken) { + try { + String resp = getRequest(EXCHANGE_URL, accessToken); + // getRequest has logged the HTTP status; the code itself is never logged. + if (resp == null) { Log.i(TAG, "exchange code: no answer"); return null; } + return new JSONObject(resp).optString("code", null); + } catch (Exception e) { + Log.e(TAG, "exchange code failed: " + e.getClass().getSimpleName()); + return null; + } + } + + /** Authorized GET; the body or null. */ + public static String getRequest(String urlStr, String accessToken) { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(30000); + conn.setRequestProperty("Authorization", "Bearer " + accessToken); + conn.setRequestProperty("User-Agent", USER_AGENT); + int code = conn.getResponseCode(); + InputStream is = (code < 400) ? conn.getInputStream() : conn.getErrorStream(); + String resp = readStream(is); + if (code < 200 || code >= 300) { Log.e(TAG, "GET HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); return null; } + return resp; + } catch (Exception e) { + Log.e(TAG, "GET failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } finally { + if (conn != null) conn.disconnect(); + } + } + + /** Authorized GET of raw bytes (the manifest binary). */ + public static byte[] getBytes(String urlStr, String accessToken) { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(30000); + conn.setReadTimeout(120000); + if (accessToken != null) conn.setRequestProperty("Authorization", "Bearer " + accessToken); + conn.setRequestProperty("User-Agent", USER_AGENT); + int code = conn.getResponseCode(); + if (code < 200 || code >= 300) { Log.e(TAG, "bytes HTTP " + code + " from " + StoreLog.redactUrl(urlStr)); return null; } + return readAllBytes(conn.getInputStream()); + } catch (Exception e) { + Log.e(TAG, "bytes failed: " + StoreLog.redactUrl(urlStr) + " (" + e.getClass().getSimpleName() + ")"); + return null; + } finally { + if (conn != null) conn.disconnect(); + } + } + + static byte[] readAllBytes(InputStream in) throws IOException { + java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream(); + byte[] buf = new byte[131072]; + int n; + while ((n = in.read(buf)) != -1) out.write(buf, 0, n); + return out.toByteArray(); + } + + static String readStream(InputStream is) throws IOException { + if (is == null) return ""; + StringBuilder sb = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { + String line; + while ((line = br.readLine()) != null) sb.append(line); + } + return sb.toString(); + } + + /** "2026-03-29T12:00:00.000Z" to epoch millis, without java.time. */ + private static long parseIso8601(String s) { + int year = Integer.parseInt(s.substring(0, 4)); + int month = Integer.parseInt(s.substring(5, 7)); + int day = Integer.parseInt(s.substring(8, 10)); + int hour = Integer.parseInt(s.substring(11, 13)); + int minute = Integer.parseInt(s.substring(14, 16)); + int second = Integer.parseInt(s.substring(17, 19)); + long jd = 367L * year - (7 * (year + (month + 9) / 12)) / 4 + (275 * month) / 9 + day + 1721013L; + long epochDays = jd - 2440588L; + return epochDays * 86400000L + hour * 3600000L + minute * 60000L + second * 1000L; + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt new file mode 100644 index 0000000000..41d4a2367a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt @@ -0,0 +1,191 @@ +package com.droiddeck.launcher.stores.epic + +import android.content.Context +import android.content.Intent +import android.util.Log +import android.webkit.WebView +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.EpicLaunchSupport +import com.droiddeck.launcher.stores.InstalledStoreGame +import com.droiddeck.launcher.stores.LoginFlows +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreBackend +import com.droiddeck.launcher.stores.StoreExe +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoreInstalls +import com.droiddeck.launcher.stores.StoreLoginActivity +import com.droiddeck.launcher.stores.StoreShelves +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.download.DownloadEntry +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.download.DownloadStage +import org.json.JSONObject +import java.io.File +import java.util.concurrent.atomic.AtomicBoolean + +/** Epic Games in the Stores section: sign-in, the library, the public store, installs and the per-launch sign-in code. */ +object EpicBackend : StoreBackend, EpicLaunchSupport { + private const val TAG = "EpicBackend" + override val store: Store = Store.EPIC + + init { LoginFlows.epicFlow = { LoginFlow } } + + override fun signIn(context: Context) { + context.startActivity(Intent(context, StoreLoginActivity::class.java).putExtra(StoreLoginActivity.EXTRA_STORE, store.id).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + } + + override fun signOut(context: Context) { + EpicPrefs.get(context).edit().remove("library_cache").remove("library_synced_at").apply() + } + + override fun syncLibrary(context: Context, force: Boolean) { + val app = context.applicationContext + val cached = EpicLibrary.cached(app) + if (cached.isNotEmpty()) StoresState.post { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { EpicLibrary.toCatalogItem(app, it) }) } + StoresState.post { StoresState.status[store] = "Fetching your library…" } + Thread({ + val result = EpicLibrary.sync(app, force) { line -> StoresState.post { StoresState.status[store] = line } } + StoresState.post { + StoresState.status.remove(store) + when (result) { + is EpicLibrary.SyncResult.Ok -> { + StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], result.games.map { EpicLibrary.toCatalogItem(app, it) }); StoresState.problems.remove(store) + } + is EpicLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message + EpicLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = "Epic session expired: sign in again." + EpicLibrary.SyncResult.Throttled -> if (!StoresState.library.containsKey(store)) StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { EpicLibrary.toCatalogItem(app, it) }) + EpicLibrary.SyncResult.Busy -> {} + } + } + }, "epic-sync").start() + } + + override fun loadShelves(context: Context, force: Boolean) { + val app = context.applicationContext + EpicStoreCatalog.cachedFeatured(app)?.let { StoresState.post { StoresState.shelves[store] = markOwned(it) } } + Thread({ + val shelves = EpicStoreCatalog.featured(app, force) + StoresState.post { if (shelves != null) StoresState.shelves[store] = markOwned(shelves) } + }, "epic-shelves").start() + } + + /** Store offers whose catalog item is in the library are owned: their cards offer Install under the library's id. */ + private fun markOwned(s: StoreShelves): StoreShelves { + val owned = StoresState.library[store] ?: return s + val byCatalogItem = owned.associateBy { it.extra["catalogItemId"] ?: "" } + fun mark(l: List<CatalogItem>) = l.map { i -> + val match = i.extra["items"]?.split(',')?.firstNotNullOfOrNull { byCatalogItem[it] } + if (match != null) match.copy(imageUrl = i.imageUrl ?: match.imageUrl, tallImageUrl = i.tallImageUrl ?: match.tallImageUrl) else i + } + return StoreShelves(mark(s.whatsNew), mark(s.deals), mark(s.free), mark(s.trending)) + } + + override fun install(context: Context, item: CatalogItem, root: File) { + val app = context.applicationContext + val namespace = item.extra["namespace"] ?: "" + val catalogItemId = item.extra["catalogItemId"] ?: "" + if (namespace.isEmpty() || catalogItemId.isEmpty()) { StoresState.logLine("Epic: \"${item.title}\" is not in your library"); return } + val folder = StoreInstallRoot.folderFor(app, Store.EPIC, item.id, item.title, root) + val entry = DownloadEntry(store, item.id, item.title, cover = item.imageUrl, bytesTotal = item.sizeBytes, location = StoreInstallRoot.labelFor(app, folder)) + DownloadQueue.enqueue(app, entry) { InstallJob(app, item, namespace, catalogItemId, folder) } + } + + override fun uninstall(context: Context, game: InstalledStoreGame) {} + + /** A fresh exchange code for the signed-in account; null when signed out or offline. Blocking. */ + override fun exchangeCode(context: Context): String? { + val token = EpicCredentialStore.getValidAccessToken(context) ?: return null + return EpicAuthClient.getExchangeCode(token) + } + + /** The access token is past its time and the refresh just failed: only a new sign-in helps. */ + override fun signInExpired(context: Context): Boolean = EpicCredentialStore.expired(context) + + private class InstallJob(val app: Context, val item: CatalogItem, val namespace: String, val catalogItemId: String, val folder: File) : DownloadQueue.DownloadJob { + private val cancelled = AtomicBoolean(false) + // On a card the in-flight chunks go to the app's cache instead: the card's write rate paces + // the whole install, and chunks are read back once at assembly. Internal installs keep the + // cache beside the game. A resume keeps whichever cache the stopped run filled: a + // `.chunks` already beside the game is reused, not fetched again into the scratch folder. + private val scratch: File? = if (StoreInstallRoot.isRemovable(app, folder) && !hasChunks(File(folder, ".chunks"))) StoreInstallRoot.scratchDir(app, Store.EPIC, item.id) else null + + private fun hasChunks(dir: File): Boolean = dir.isDirectory && (dir.list()?.isNotEmpty() == true) + + override fun run(handle: DownloadQueue.JobHandle): String? { + handle.stage(DownloadStage.MANIFEST, "Checking sign-in…") + val token = EpicCredentialStore.getValidAccessToken(app) ?: throw EpicDownloadManager.InstallException("Not signed in to Epic Games") + StoreInstalls.begin(folder, Store.EPIC, item.id, item.title, item.tallImageUrl ?: item.imageUrl, item.imageUrl) + if (scratch == null && hasChunks(File(folder, ".chunks"))) handle.log("epic: resuming \"${item.title}\" with the chunks already beside it") + val manifestJson = EpicApiClient.getManifestApiJson(token, namespace, catalogItemId, item.id) + ?: throw EpicDownloadManager.InstallException("The manifest could not be fetched") + val tags = EpicInstallTags.tagsForDevice() + var downloading = false + val result = EpicDownloadManager.install(app, manifestJson, folder.path, scratch?.path ?: "", tags, cancelled, object : EpicDownloadManager.Callback { + override fun onProgress(message: String, pct: Int) { + when { + message.startsWith("Writing") || message.startsWith("Complete") -> handle.stage(DownloadStage.INSTALL, message) + message.startsWith("Downloading chunks") -> { if (!downloading) { downloading = true; handle.stage(DownloadStage.DOWNLOAD, message) } else handle.progress(-1, -1, message) } + message.startsWith("Checking") -> handle.stage(DownloadStage.MANIFEST, message) + else -> handle.stage(DownloadStage.MANIFEST, message) + } + } + override fun onBytes(done: Long, total: Long, speedBps: Long) { handle.progress(done, total, null, speedBps) } + override fun onLog(line: String) { handle.log(line) } + override fun onSizes(downloadBytes: Long, diskBytes: Long) { + handle.diskSize(diskBytes) + EpicLibrary.rememberSize(app, item.id, diskBytes) + } + override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) { + handle.stage(when (stage) { "check" -> DownloadStage.MANIFEST; "verify" -> DownloadStage.VERIFY; else -> DownloadStage.INSTALL }) + handle.stageProgress(done, total, items, itemsTotal, bytes = stage == "install") + } + }) ?: return null + if (cancelled.get()) return null + handle.stage(DownloadStage.INSTALL, "Registering with Steam…") + EpicLibrary.rememberSize(app, item.id, result.bytes) + val deploymentId = runCatching { JSONObject(manifestJson).optString("deploymentId", "") }.getOrDefault("") + val exe = StoreExe.pick(folder, result.launchExe, item.title) + if (exe.isEmpty()) handle.log("epic: no exe found in ${folder.name}; pick one in Steam settings › Added games") + val args = EpicLaunchData.arguments(app, token, item.id, namespace, catalogItemId, deploymentId) + val sidecar = StoreGameSidecar( + Store.EPIC, item.id, item.title, exe = exe.ifEmpty { "game.exe" }, args = args, + installVersion = result.buildVersion, installedAt = System.currentTimeMillis(), + cover = item.tallImageUrl ?: item.imageUrl, hero = item.imageUrl, + extra = EpicLaunchData.extras(item.id, namespace, catalogItemId, deploymentId), + ) + StoreInstalls.complete(app, folder, sidecar) + return folder.path + } + + override fun cancel(deleteFiles: Boolean) { + cancelled.set(true) + if (deleteFiles) StoreInstalls.discard(app, folder, listOfNotNull(File(folder, ".chunks"), scratch, StoreInstallRoot.scratchDir(app, Store.EPIC, item.id))) + } + } + + /** + * Epic's web sign-in: the login page redirects to a JSON page carrying an authorization code, + * read out of the document and exchanged for tokens. + */ + private object LoginFlow : StoreLoginActivity.Flow { + private const val REDIRECT = "https://www.epicgames.com/id/api/redirect" + override val startUrl: String = "https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fid%2Fapi%2Fredirect%3FclientId%3D${EpicAuthClient.CLIENT_ID}%26responseType%3Dcode" + override val userAgent: String get() = EpicAuthClient.USER_AGENT + override val readsPage: Boolean get() = true + override fun isRedirect(url: String): Boolean = url.startsWith(REDIRECT) + + override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? { + // The document's text comes JSON-quoted from evaluateJavascript. + var text = url + if (text.startsWith("\"")) text = text.substring(1, text.length - 1) + text = text.replace("\\n", "").replace("\\r", "").replace("\\\"", "\"") + val code = Regex("\"authorizationCode\":\"([^\"]+)\"").find(text)?.groupValues?.get(1) + if (code.isNullOrEmpty()) { Log.w(TAG, "login: no authorization code on the redirect page"); return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_verification) } + val result = EpicAuthClient.exchangeCode(code) ?: return activity.getString(com.droiddeck.launcher.R.string.stores_login_error_generic) + EpicCredentialStore.save(activity, result) + Log.i(TAG, "signed in") + return null + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicCredentialStore.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicCredentialStore.kt new file mode 100644 index 0000000000..abf545ae13 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicCredentialStore.kt @@ -0,0 +1,61 @@ +package com.droiddeck.launcher.stores.epic + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreAccounts +import org.json.JSONObject + +/** + * The Epic sign-in in StoreAccounts' private file: access and refresh tokens, the account id and + * display name (the launch arguments carry both), the expiry. Refreshed when within five minutes + * of expiring. + */ +object EpicCredentialStore { + private const val TAG = "EpicAuth" + + class Credentials(val accessToken: String, val refreshToken: String?, val accountId: String, val displayName: String, val expiresAt: Long) + + @JvmStatic + fun save(context: Context, result: EpicAuthClient.TokenResult) { + val json = JSONObject().put("access_token", result.accessToken).put("account_id", result.accountId ?: "") + .put("display_name", result.displayName ?: "").put("expires_at", result.expiresAt) + if (!result.refreshToken.isNullOrEmpty()) json.put("refresh_token", result.refreshToken) + StoreAccounts.write(context, Store.EPIC, json) + } + + @JvmStatic + fun load(context: Context): Credentials? { + val json = StoreAccounts.read(context, Store.EPIC) ?: return null + val token = json.optString("access_token", "").ifEmpty { return null } + return Credentials(token, json.optString("refresh_token", "").ifEmpty { null }, json.optString("account_id", ""), json.optString("display_name", ""), json.optLong("expires_at", 0L)) + } + + @JvmStatic + fun isLoggedIn(context: Context): Boolean = load(context) != null + + /** A valid access token, refreshed when within five minutes of expiry; null when signed out or the refresh failed. */ + @JvmStatic + /** The stored access token is past its expiry (a refresh, if one was tried, did not replace it). */ + fun expired(context: Context): Boolean { + val creds = load(context) ?: return false + return creds.expiresAt in 1 until System.currentTimeMillis() + } + + fun getValidAccessToken(context: Context): String? { + val creds = load(context) ?: return null + if (creds.expiresAt - System.currentTimeMillis() < 5L * 60L * 1000L && creds.refreshToken != null) { + val result = EpicAuthClient.refreshToken(creds.refreshToken) + if (result != null) { + if (result.refreshToken.isNullOrEmpty()) result.refreshToken = creds.refreshToken + if (result.accountId.isNullOrEmpty()) result.accountId = creds.accountId + if (result.displayName.isNullOrEmpty()) result.displayName = creds.displayName + save(context, result) + Log.i(TAG, "token refreshed") + return result.accessToken + } + Log.w(TAG, "token refresh failed; using the stored token") + } + return creds.accessToken + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicDownloadManager.java b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicDownloadManager.java new file mode 100644 index 0000000000..16ff379c09 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicDownloadManager.java @@ -0,0 +1,810 @@ +package com.droiddeck.launcher.stores.epic; + +import android.content.Context; +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; +import com.droiddeck.launcher.stores.download.StoreDownloadTier; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +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, the chunk fetch into `.chunks/<GUID>` (the native engine when it is there, the + * Java pool otherwise), and the assembly of files from those chunks. + * + * 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. + */ +public final class EpicDownloadManager { + + private static final String TAG = "EpicDownload"; + private static final String UA = EpicAuthClient.USER_AGENT; + + public interface Callback { + void onProgress(String message, int pct); + default void onBytes(long done, long total, long speedBps) {} + default void onLog(String line) {} + /** + * The active stage's own count: {@code stage} is "check" (the files already there, before + * anything is fetched), "verify" or "install" (bytes written), {@code done} of + * {@code total} an amount (bytes, or items when there are no bytes), {@code items} of + * {@code itemsTotal} the files, when it counts files. + */ + default void onStage(String stage, long done, long total, int items, int itemsTotal) {} + /** What this run fetches (compressed) and what the game takes on disk. */ + default void onSizes(long downloadBytes, long diskBytes) {} + } + + private EpicDownloadManager() {} + + public static final class CdnUrl { + public final String baseUrl, cloudDir, authParams; + CdnUrl(String baseUrl, String cloudDir, String authParams) { this.baseUrl = baseUrl; this.cloudDir = cloudDir; this.authParams = authParams; } + } + + public static final class ChunkInfo { + public int[] guid = new int[4]; + public long hash; + public byte[] sha1; + public int groupNum; + public int windowSize; + public long fileSize; + public String guidStr() { return String.format("%08X%08X%08X%08X", guid[0], guid[1], guid[2], guid[3]); } + public String getPath(String chunkDir) { return chunkDir + "/" + String.format("%02d", groupNum) + "/" + String.format("%016X", hash) + "_" + guidStr() + ".chunk"; } + } + + public static final class ChunkPart { + public int[] guid = new int[4]; + public int offset, size; + public String guidStr() { return String.format("%08X%08X%08X%08X", guid[0], guid[1], guid[2], guid[3]); } + } + + public static final class FileInfo { + public String filename = ""; + public List<ChunkPart> parts = new ArrayList<>(); + public List<String> installTags = new ArrayList<>(); + public byte[] sha1 = null; + public long fileSize() { long t = 0; for (ChunkPart p : parts) t += (p.size & 0xFFFFFFFFL); return t; } + } + + /** The parsed manifest: its chunks, files, and from the meta block the exe the launcher starts. */ + public static final class Manifest { + public String chunkDir = "ChunksV4"; + public List<ChunkInfo> uniqueChunks = new ArrayList<>(); + public List<FileInfo> files = new ArrayList<>(); + public String launchExe = ""; + public String launchCommand = ""; + public String buildVersion = ""; + public List<CdnUrl> cdnUrls = new ArrayList<>(); + } + + /** What an install leaves for the sidecar. */ + public static final class Result { + public final String launchExe; + public final String buildVersion; + public final long bytes; + Result(String launchExe, String buildVersion, long bytes) { this.launchExe = launchExe; this.buildVersion = buildVersion; this.bytes = bytes; } + } + + public static final class InstallException extends Exception { + InstallException(String message) { super(message); } + } + + /** + * Installs the app the manifest API JSON describes into {@code installDir}. {@code installTags} + * null = every file. {@code chunkCacheDirPath} "" keeps the chunk cache beside the game + * ({@code <installDir>/.chunks}); a path puts it there instead - the app's cache when the game + * goes to a card. Returns null when cancelled; throws when it fails. + */ + public static Result install(Context ctx, String manifestApiJson, String installDirPath, String chunkCacheDirPath, List<String> installTags, AtomicBoolean cancel, Callback cb) throws InstallException { + try { + if (cancel.get()) return null; + cb.onProgress("Reading CDN list…", 0); + List<CdnUrl> cdnUrls = parseCdnUrls(manifestApiJson); + if (cdnUrls.isEmpty()) throw new InstallException("No CDN in the manifest answer"); + for (CdnUrl c : cdnUrls) cb.onLog("epic: CDN " + c.baseUrl + " auth=" + (c.authParams.isEmpty() ? "no" : "yes")); + cb.onProgress("Downloading manifest…", 0); + byte[] manifestBytes = downloadManifest(manifestApiJson, cdnUrls); + if (manifestBytes == null) throw new InstallException("The manifest could not be downloaded"); + if (cancel.get()) return null; + cb.onProgress("Parsing manifest…", 0); + Manifest manifest = parseManifest(manifestBytes); + if (manifest == null) throw new InstallException("The manifest could not be parsed"); + manifest.cdnUrls = cdnUrls; + cb.onLog("epic: chunkDir=" + manifest.chunkDir + " chunks=" + manifest.uniqueChunks.size() + " files=" + manifest.files.size() + " exe=" + manifest.launchExe); + + File installDir = new File(installDirPath); + installDir.mkdirs(); + // "" = the cache beside the game; a path = the scratch cache (the app's own storage when + // the game goes to a card). The engine and the Java loops read the same value. + final String cachePath = chunkCacheDirPath == null ? "" : chunkCacheDirPath; + final File chunkCacheDir = cachePath.isEmpty() ? new File(installDir, ".chunks") : new File(cachePath); + chunkCacheDir.mkdirs(); + + List<FileInfo> selected = resolveInstallFiles(manifest, installTags); + cb.onLog("epic: " + selected.size() + "/" + manifest.files.size() + " files for tags " + (installTags == null ? "(all)" : installTags.toString())); + long installBytes = 0; + for (FileInfo f : selected) installBytes += f.fileSize(); + final long planned = installBytes; + // Free space is checked after the delta pass, against what is still missing (below). + + cb.onProgress("Checking files…", 0); + List<FileInfo> pending = new ArrayList<>(selected.size()); + int checked = 0, good = 0; + long checkedBytes = 0; + cb.onStage("check", 0, installBytes, 0, selected.size()); + for (FileInfo f : selected) { + if (cancel.get()) return null; + File out = new File(installDir, f.filename.replace("\\", "/")); + if (fileExistsWithCorrectHash(out, f.fileSize(), f.sha1)) good++; else pending.add(f); + checked++; + checkedBytes += f.fileSize(); + if ((checked & 15) == 0 || checked == selected.size()) cb.onStage("check", checkedBytes, installBytes, checked, selected.size()); + if ((checked & 63) == 0) cb.onProgress("Checking files… (" + checked + "/" + selected.size() + ")", 0); + } + cb.onLog("epic: delta " + good + " up to date, " + pending.size() + " to download"); + if (pending.isEmpty()) { + deleteDir(chunkCacheDir); + cb.onProgress("Complete", 100); + return new Result(manifest.launchExe, manifest.buildVersion, planned); + } + List<ChunkInfo> needed = uniqueChunksForFiles(manifest, pending); + // The cache holds whole chunk windows (~1 MiB each), and a window is shared with files + // this device does not install (other tags, other builds' data): the cache is often + // larger than the game itself (Metalstorm: 9.1 GB of chunks for 4.4 GB of files). Both + // have to fit at once until assembly, so the check counts both, on their own volumes. + long cacheMissing = 0, pendingBytes = 0; + for (ChunkInfo c : needed) if (!new File(chunkCacheDir, c.guidStr()).isFile()) cacheMissing += Math.max(c.windowSize, 1); + for (FileInfo f : pending) pendingBytes += f.fileSize(); + if (cachePath.isEmpty()) { + long free = installDir.getUsableSpace(); + if (free > 0 && cacheMissing + pendingBytes > free) throw new InstallException("Not enough free space: need " + fmt(cacheMissing + pendingBytes) + " (" + fmt(pendingBytes) + " of game files + " + fmt(cacheMissing) + " of download cache), only " + fmt(free) + " free"); + } else { + long freeCache = chunkCacheDir.getUsableSpace(), freeGame = installDir.getUsableSpace(); + if (freeCache > 0 && cacheMissing > freeCache) throw new InstallException("Not enough internal space for the download cache: need " + fmt(cacheMissing) + ", only " + fmt(freeCache) + " free"); + if (freeGame > 0 && pendingBytes > freeGame) throw new InstallException("Not enough free space: need " + fmt(pendingBytes) + ", only " + fmt(freeGame) + " free"); + } + cb.onLog("epic: cache " + (cachePath.isEmpty() ? "beside the game" : "scratch") + ", " + fmt(cacheMissing) + " to fetch for " + fmt(pendingBytes) + " of files"); + long totalBytes = 0; + for (ChunkInfo c : needed) totalBytes += Math.max(c.fileSize, 1); + final long fTotalBytes = totalBytes; + final int totalChunks = needed.size(); + final AtomicLong completedBytes = new AtomicLong(0); + final AtomicInteger completedCount = new AtomicInteger(0); + final AtomicInteger failCount = new AtomicInteger(0); + final AtomicLong lastSpeedMs = new AtomicLong(System.currentTimeMillis()); + final AtomicLong lastSpeedBytes = new AtomicLong(0); + final AtomicLong speedBps = new AtomicLong(0); + cb.onSizes(fTotalBytes, planned); + cb.onBytes(0, fTotalBytes, 0); + cb.onLog("epic: " + fmt(fTotalBytes) + " in " + totalChunks + " chunks"); + + boolean javaPool = true; + final int[] pendingIdx = pendingIndices(manifest, pending); + boolean engine = com.droiddeck.launcher.stores.StoresNative.INSTANCE.getAvailable() && pendingIdx != null; + if (engine) { + NativeOutcome r = runNativePool(ctx, manifestBytes, pendingIdx, needed, fTotalBytes, installDirPath, cachePath, cdnUrls, cancel, cb, + completedBytes, completedCount, lastSpeedMs, lastSpeedBytes, speedBps); + if (r.started) { + javaPool = false; + if (r.cancelled) return null; + if (!r.success) { cb.onLog("epic: engine failed: " + r.error); failCount.incrementAndGet(); } + } else cb.onLog("epic: engine not started (" + r.error + "); using the built-in pool"); + } + if (javaPool) { + cb.onLog("epic: engine=built-in (8 threads)"); + ExecutorService pool = Executors.newFixedThreadPool(8, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("epic-dl")); + for (ChunkInfo chunk : needed) { + final ChunkInfo fc = chunk; + pool.submit(() -> { + if (cancel.get()) return; + File cached = new File(chunkCacheDir, fc.guidStr()); + if (!cached.exists() && !downloadChunkStreaming(fc, manifest.chunkDir, cdnUrls, cached)) { + cb.onLog("FAIL chunk=" + fc.guidStr()); + failCount.incrementAndGet(); + return; + } + long done = completedBytes.addAndGet(Math.max(fc.fileSize, 1)); + int cnt = completedCount.incrementAndGet(); + sampleSpeed(done, lastSpeedMs, lastSpeedBytes, speedBps); + cb.onProgress("Downloading chunks (" + cnt + "/" + totalChunks + ")" + speedSuffix(speedBps.get()), (int) (done * 80L / Math.max(1, fTotalBytes))); + cb.onBytes(done, fTotalBytes, speedBps.get()); + }); + } + pool.shutdown(); + try { + while (!pool.awaitTermination(250, TimeUnit.MILLISECONDS)) { + if (cancel.get()) { pool.shutdownNow(); pool.awaitTermination(5, TimeUnit.SECONDS); return null; } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + pool.shutdownNow(); + throw new InstallException("interrupted"); + } + } + if (cancel.get()) return null; + if (failCount.get() > 0) throw new InstallException(failCount.get() + " chunk(s) failed to download"); + + // The engine's assembler when it starts (it drops each chunk after its last use and the + // cache at the end); the loop below when it does not. + if (engine) { + NativeOutcome r = runNativeAssembly(manifestBytes, pendingIdx, pending.size(), installDirPath, cachePath, cancel, cb); + if (r.started) { + if (r.cancelled) return null; + if (!r.success) throw new InstallException(r.error.isEmpty() ? "assembly failed" : r.error); + cb.onProgress("Complete", 100); + return new Result(manifest.launchExe, manifest.buildVersion, planned); + } + cb.onLog("epic: assembler not started (" + r.error + "); writing the files here"); + } + int totalFiles = pending.size(), doneFiles = 0; + long writtenBytes = 0, writeTotal = 0; + for (FileInfo f : pending) writeTotal += f.fileSize(); + for (FileInfo file : pending) { + if (cancel.get()) return null; + String relPath = file.filename.replace("\\", "/"); + File outFile = new File(installDir, relPath); + File parent = outFile.getParentFile(); + if (parent != null) parent.mkdirs(); + cb.onProgress("Writing: " + (relPath.contains("/") ? relPath.substring(relPath.lastIndexOf('/') + 1) : relPath), 80 + (int) (doneFiles * 20L / totalFiles)); + try (FileOutputStream fos = new FileOutputStream(outFile); BufferedOutputStream bos = new BufferedOutputStream(fos, 65536)) { + for (ChunkPart part : file.parts) { + File cachedChunk = new File(chunkCacheDir, part.guidStr()); + if (!cachedChunk.exists()) throw new InstallException("Missing chunk " + part.guidStr() + " for " + relPath); + byte[] data = readFile(cachedChunk); + bos.write(data, part.offset, part.size); + } + } + doneFiles++; + writtenBytes += file.fileSize(); + cb.onStage("install", writtenBytes, writeTotal, doneFiles, totalFiles); + } + deleteDir(chunkCacheDir); + cb.onProgress("Complete", 100); + return new Result(manifest.launchExe, manifest.buildVersion, planned); + } catch (InstallException e) { + throw e; + } catch (Exception e) { + Log.w(TAG, "install failed", e); + throw new InstallException(e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : "")); + } + } + + private static final class NativeOutcome { boolean started, success, cancelled; String error = ""; } + + /** The pending files as indices into the manifest's file list, what both native runs take; null when one is not in it. */ + private 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; + } + + /** The native chunk pool on the same counters; never throws. */ + private static NativeOutcome runNativePool(Context ctx, byte[] manifestBytes, int[] pendingIdx, List<ChunkInfo> needed, long totalBytes, + String installDirPath, String cachePath, List<CdnUrl> cdnUrls, AtomicBoolean cancel, Callback cb, + AtomicLong completedBytes, AtomicInteger completedCount, AtomicLong lastSpeedMs, AtomicLong lastSpeedBytes, AtomicLong speedBps) { + NativeOutcome r = new NativeOutcome(); + try { + String[] prefixes = new String[cdnUrls.size()]; + for (int i = 0; i < cdnUrls.size(); i++) prefixes[i] = cdnUrls.get(i).baseUrl + cdnUrls.get(i).cloudDir; + final int totalChunks = needed.size(); + StoreDownloadTier tier = StoreDownloadTier.Companion.current(ctx); + int workers = Math.max(1, Math.min(128, tier.getNetworkWindow())); + int process = Math.max(2, tier.getProcessWorkers()); + cb.onLog("epic: engine=native workers=" + workers + " process_workers=" + process + " tier=" + tier.getId() + (cachePath.isEmpty() ? "" : " cache=scratch")); + EpicNative.Result res = EpicNative.run(manifestBytes, installDirPath, cachePath, prefixes, pendingIdx, totalChunks, totalBytes, "", workers, process, 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) { + completedBytes.set(bytesDone); + completedCount.set(chunksDone); + sampleSpeed(bytesDone, lastSpeedMs, lastSpeedBytes, speedBps); + cb.onProgress("Downloading chunks (" + chunksDone + "/" + totalChunks + ")" + speedSuffix(speedBps.get()), (int) (bytesDone * 80L / Math.max(1, totalBytes))); + cb.onBytes(bytesDone, totalBytes, speedBps.get()); + } + @Override public void onLog(String line) { cb.onLog(line); } + @Override public void onComplete(boolean success, String error, long bytesCredited) {} + }); + r.started = res.started; r.success = res.success; r.cancelled = res.cancelled; r.error = res.error == null ? "" : res.error; + } catch (Throwable t) { + r.started = false; + r.error = t.getClass().getSimpleName() + ": " + t.getMessage(); + } + return r; + } + + /** The native assembler: the pending files written from the cache, the cache removed on success; never throws. */ + private static NativeOutcome runNativeAssembly(byte[] manifestBytes, int[] pendingIdx, int totalFiles, String installDirPath, String cachePath, AtomicBoolean cancel, Callback cb) { + NativeOutcome r = new NativeOutcome(); + try { + cb.onLog("epic: assembler=native files=" + totalFiles + (cachePath.isEmpty() ? "" : " cache=scratch")); + EpicNative.Result res = EpicNative.assemble(manifestBytes, installDirPath, 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("install", 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) {} + }); + r.started = res.started; r.success = res.success; r.cancelled = res.cancelled; r.error = res.error == null ? "" : res.error; + } catch (Throwable t) { + r.started = false; + r.error = t.getClass().getSimpleName() + ": " + t.getMessage(); + } + return r; + } + + // ---- CDN list and manifest download --------------------------------------------------------- + + public static List<CdnUrl> parseCdnUrls(String json) { + List<CdnUrl> result = new ArrayList<>(); + try { + JSONArray manifests = new JSONObject(json).optJSONArray("manifests"); + if (manifests == null) return result; + for (int i = 0; i < manifests.length(); i++) { + JSONObject m = manifests.optJSONObject(i); + if (m == null) continue; + String uri = m.optString("uri", ""); + int buildsIdx = uri.indexOf("/Builds"); + if (buildsIdx < 0) continue; + String baseUrl = uri.substring(0, buildsIdx); + if (!baseUrl.startsWith("http") || baseUrl.contains("cloudflare.epicgamescdn.com")) continue; + String afterBase = uri.substring(buildsIdx); + int q = afterBase.indexOf('?'); + if (q >= 0) afterBase = afterBase.substring(0, q); + int lastSlash = afterBase.lastIndexOf('/'); + if (lastSlash < 0) continue; + String cloudDir = afterBase.substring(0, lastSlash); + StringBuilder auth = new StringBuilder(); + JSONArray params = m.optJSONArray("queryParams"); + if (params != null) for (int j = 0; j < params.length(); j++) { + JSONObject p = params.optJSONObject(j); + if (p == null) continue; + auth.append(auth.length() == 0 ? "?" : "&").append(p.optString("name")).append('=').append(p.optString("value")); + } + result.add(new CdnUrl(baseUrl, cloudDir, auth.toString())); + } + } catch (Exception e) { + Log.e(TAG, "CDN parse: " + e.getClass().getSimpleName()); + } + return result; + } + + /** The manifest binary from the first CDN that serves it; the auth query goes on this URL only. */ + public static byte[] downloadManifest(String json, List<CdnUrl> cdnUrls) { + try { + JSONArray manifests = new JSONObject(json).optJSONArray("manifests"); + if (manifests == null || manifests.length() == 0) return null; + String firstUri = manifests.getJSONObject(0).optString("uri", ""); + String uriPath = firstUri.contains("?") ? firstUri.substring(0, firstUri.indexOf('?')) : firstUri; + int lastSlash = uriPath.lastIndexOf('/'); + if (lastSlash < 0) return null; + String manifestFilename = uriPath.substring(lastSlash + 1); + for (CdnUrl cdn : cdnUrls) { + byte[] bytes = downloadBytes(cdn.baseUrl + cdn.cloudDir + "/" + manifestFilename + cdn.authParams); + if (bytes != null && bytes.length > 4) return bytes; + } + } catch (Exception e) { + Log.e(TAG, "manifest download: " + e.getClass().getSimpleName()); + } + return null; + } + + // ---- manifest parsing ----------------------------------------------------------------------- + + public static Manifest parseManifest(byte[] bytes) { + try { + ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN); + int magic = buf.getInt(); + if (magic != 0x44BEC00C) return parseJsonManifest(bytes); + int headerSize = buf.getInt(); + int sizeUncompressed = buf.getInt(); + buf.getInt(); + buf.position(buf.position() + 20); + int storedAs = buf.get() & 0xFF; + int version = buf.getInt(); + String chunkDir = version >= 15 ? "ChunksV4" : version >= 6 ? "ChunksV3" : version >= 3 ? "ChunksV2" : "Chunks"; + buf.position(headerSize); + byte[] body = new byte[buf.remaining()]; + buf.get(body); + if ((storedAs & 1) != 0) { + Inflater inflater = new Inflater(); + inflater.setInput(body); + byte[] decomp = new byte[sizeUncompressed]; + int got = inflater.inflate(decomp); + inflater.end(); + if (got != sizeUncompressed) { Log.e(TAG, "manifest inflate size mismatch"); return null; } + body = decomp; + } + ByteBuffer b = ByteBuffer.wrap(body).order(ByteOrder.LITTLE_ENDIAN); + Manifest m = new Manifest(); + m.chunkDir = chunkDir; + // ManifestMeta: the launch exe and build version live here; the rest is skipped by size. + int metaStart = b.position(); + int metaSize = b.getInt(); + try { + b.get(); // data version + b.getInt(); // feature level + b.get(); // isFileData + b.getInt(); // app id + readFString(b); // app name + m.buildVersion = readFString(b); + m.launchExe = readFString(b).replace('\\', '/'); + m.launchCommand = readFString(b); + } catch (Exception e) { + Log.w(TAG, "manifest meta: " + e.getClass().getSimpleName()); + } + b.position(metaStart + metaSize); + + int cdlStart = b.position(); + int cdlSize = b.getInt(); + b.get(); + int chunkCount = b.getInt(); + List<ChunkInfo> chunks = new ArrayList<>(chunkCount); + for (int i = 0; i < chunkCount; i++) chunks.add(new ChunkInfo()); + for (ChunkInfo c : chunks) { c.guid[0] = b.getInt(); c.guid[1] = b.getInt(); c.guid[2] = b.getInt(); c.guid[3] = b.getInt(); } + for (ChunkInfo c : chunks) c.hash = b.getLong(); + for (ChunkInfo c : chunks) { c.sha1 = new byte[20]; b.get(c.sha1); } + for (ChunkInfo c : chunks) c.groupNum = b.get() & 0xFF; + for (ChunkInfo c : chunks) c.windowSize = b.getInt(); + for (ChunkInfo c : chunks) c.fileSize = b.getLong(); + b.position(cdlStart + cdlSize); + + int fmlStart = b.position(); + int fmlSize = b.getInt(); + b.get(); + int fileCount = b.getInt(); + List<FileInfo> files = new ArrayList<>(fileCount); + for (int i = 0; i < fileCount; i++) files.add(new FileInfo()); + for (FileInfo f : files) f.filename = readFString(b); + for (int i = 0; i < fileCount; i++) readFString(b); + for (FileInfo f : files) { f.sha1 = new byte[20]; b.get(f.sha1); } + b.position(b.position() + fileCount); + for (int i = 0; i < fileCount; i++) { + int tagCount = b.getInt(); + for (int j = 0; j < tagCount; j++) { String tag = readFString(b); if (tag != null && !tag.isEmpty()) files.get(i).installTags.add(tag); } + } + for (FileInfo f : files) { + int partCount = b.getInt(); + for (int j = 0; j < partCount; j++) { + int partStart = b.position(); + int partStructSize = b.getInt(); + ChunkPart part = new ChunkPart(); + part.guid[0] = b.getInt(); part.guid[1] = b.getInt(); part.guid[2] = b.getInt(); part.guid[3] = b.getInt(); + part.offset = b.getInt(); part.size = b.getInt(); + f.parts.add(part); + b.position(partStart + partStructSize); + } + } + b.position(fmlStart + fmlSize); + Map<String, ChunkInfo> seen = new LinkedHashMap<>(chunkCount * 2); + for (ChunkInfo c : chunks) seen.put(c.guidStr(), c); + m.uniqueChunks = new ArrayList<>(seen.values()); + m.files = files; + return m; + } catch (Exception e) { + Log.e(TAG, "manifest parse: " + e.getClass().getSimpleName()); + return null; + } + } + + private static Manifest parseJsonManifest(byte[] bytes) { + try { + JSONObject root = new JSONObject(new String(bytes, StandardCharsets.UTF_8)); + int manifestVersion = 0; + try { manifestVersion = Integer.parseInt(root.optString("ManifestFileVersion", "0")); } catch (NumberFormatException ignored) {} + Manifest m = new Manifest(); + m.chunkDir = manifestVersion >= 15 ? "ChunksV4" : manifestVersion >= 6 ? "ChunksV3" : manifestVersion >= 3 ? "ChunksV2" : "ChunksV4"; + m.launchExe = root.optString("LaunchExe", "").replace('\\', '/'); + m.launchCommand = root.optString("LaunchCommand", ""); + m.buildVersion = root.optString("BuildVersionString", ""); + JSONObject chunkHashList = root.optJSONObject("ChunkHashList"); + JSONObject dataGroupList = root.optJSONObject("DataGroupList"); + JSONObject chunkFilesizeList = root.optJSONObject("ChunkFilesizeList"); + if (chunkHashList == null) return null; + Map<String, ChunkInfo> chunkMap = new LinkedHashMap<>(); + Iterator<String> keys = chunkHashList.keys(); + while (keys.hasNext()) { + String guidHex = keys.next(); + if (guidHex.length() < 32) continue; + ChunkInfo c = new ChunkInfo(); + for (int k = 0; k < 4; k++) c.guid[k] = (int) Long.parseLong(guidHex.substring(k * 8, k * 8 + 8), 16); + String hashHex = chunkHashList.getString(guidHex); + if (hashHex != null && hashHex.length() >= 16) { try { c.hash = Long.parseUnsignedLong(hashHex.substring(0, 16), 16); } catch (Exception ignored) {} } + if (dataGroupList != null) { try { c.groupNum = Integer.parseInt(dataGroupList.optString(guidHex, "0")); } catch (NumberFormatException ignored) {} } + if (chunkFilesizeList != null) { try { c.fileSize = Long.parseLong(chunkFilesizeList.optString(guidHex, "0"), 16); } catch (NumberFormatException ignored) {} } + chunkMap.put(guidHex, c); + } + JSONArray fileList = root.optJSONArray("FileManifestList"); + if (fileList == null) return null; + List<FileInfo> files = new ArrayList<>(fileList.length()); + for (int i = 0; i < fileList.length(); i++) { + JSONObject fo = fileList.getJSONObject(i); + FileInfo fi = new FileInfo(); + fi.filename = fo.optString("Filename", ""); + JSONArray tags = fo.optJSONArray("InstallTags"); + if (tags != null) for (int t = 0; t < tags.length(); t++) { String tag = tags.optString(t, ""); if (!tag.isEmpty()) fi.installTags.add(tag); } + JSONArray parts = fo.optJSONArray("FileChunkParts"); + if (parts != null) for (int j = 0; j < parts.length(); j++) { + JSONObject po = parts.getJSONObject(j); + ChunkPart part = new ChunkPart(); + String g = po.optString("Guid", ""); + if (g.length() >= 32) for (int k = 0; k < 4; k++) part.guid[k] = (int) Long.parseLong(g.substring(k * 8, k * 8 + 8), 16); + try { part.offset = Integer.parseInt(po.optString("Offset", "0")); } catch (NumberFormatException ignored) {} + try { part.size = Integer.parseInt(po.optString("Size", "0")); } catch (NumberFormatException ignored) {} + fi.parts.add(part); + } + files.add(fi); + } + m.uniqueChunks = new ArrayList<>(chunkMap.values()); + m.files = files; + return m; + } catch (Exception e) { + Log.e(TAG, "json manifest parse: " + e.getClass().getSimpleName()); + return null; + } + } + + public static String readFString(ByteBuffer buf) { + int length = buf.getInt(); + if (length == 0) return ""; + if (length < 0) { + int chars = (-length) - 1; + byte[] bytes = new byte[chars * 2]; + buf.get(bytes); + buf.getShort(); + return new String(bytes, StandardCharsets.UTF_16LE); + } + byte[] bytes = new byte[length - 1]; + buf.get(bytes); + buf.get(); + return new String(bytes, StandardCharsets.US_ASCII); + } + + // ---- chunk fetch (the built-in pool) -------------------------------------------------------- + + public static byte[] downloadBytes(String urlStr) { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setConnectTimeout(30000); + conn.setReadTimeout(60000); + conn.setRequestProperty("User-Agent", UA); + if (conn.getResponseCode() != 200) { Log.w(TAG, "HTTP " + conn.getResponseCode() + " for " + StoreLog.redactUrl(urlStr)); return null; } + return EpicAuthClient.readAllBytes(conn.getInputStream()); + } catch (Exception e) { + Log.w(TAG, "download error [" + StoreLog.redactUrl(urlStr) + "]: " + e.getClass().getSimpleName()); + return null; + } finally { + if (conn != null) conn.disconnect(); + } + } + + /** One chunk streamed from the first CDN that serves it, SHA-1 checked, published by rename. */ + private static boolean downloadChunkStreaming(ChunkInfo chunk, String chunkDir, List<CdnUrl> cdnUrls, File outFile) { + String chunkPath = chunk.getPath(chunkDir); + File tmp = new File(outFile.getPath() + ".part"); + tmp.delete(); + for (CdnUrl cdn : cdnUrls) { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(cdn.baseUrl + cdn.cloudDir + "/" + chunkPath).openConnection(); + conn.setConnectTimeout(30000); + conn.setReadTimeout(60000); + conn.setRequestProperty("User-Agent", UA); + if (conn.getResponseCode() != 200) { conn.disconnect(); continue; } + MessageDigest sha = null; + if (chunk.sha1 != null && chunk.sha1.length == 20) { + boolean allZero = true; + for (byte x : chunk.sha1) if (x != 0) { allZero = false; break; } + if (!allZero) { try { sha = MessageDigest.getInstance("SHA-1"); } catch (Exception ignored) {} } + } + try (InputStream in = conn.getInputStream(); FileOutputStream fos = new FileOutputStream(tmp)) { + byte[] hdrBuf = new byte[41]; + readFully(in, hdrBuf); + ByteBuffer hdr = ByteBuffer.wrap(hdrBuf).order(ByteOrder.LITTLE_ENDIAN); + if (hdr.getInt() != 0xB1FE3AA2) { Log.w(TAG, "bad chunk magic"); continue; } + hdr.getInt(); + int headerSize = hdr.getInt(); + int compressedSize = hdr.getInt(); + hdr.position(hdr.position() + 24); + int storedAs = hdr.get() & 0xFF; + if (headerSize > 41) skipFully(in, headerSize - 41); + byte[] iobuf = new byte[131072]; + if ((storedAs & 1) != 0) { + Inflater inflater = new Inflater(); + byte[] obuf = new byte[131072]; + int remaining = compressedSize; + try { + while (remaining > 0 && !inflater.finished()) { + if (inflater.needsInput()) { + int n = in.read(iobuf, 0, Math.min(iobuf.length, remaining)); + if (n <= 0) break; + remaining -= n; + inflater.setInput(iobuf, 0, n); + } + int out = inflater.inflate(obuf); + if (out > 0) { fos.write(obuf, 0, out); if (sha != null) sha.update(obuf, 0, out); } + } + int out; + while ((out = inflater.inflate(obuf)) > 0) { fos.write(obuf, 0, out); if (sha != null) sha.update(obuf, 0, out); } + } finally { + inflater.end(); + } + } else { + int remaining = compressedSize; + while (remaining > 0) { + int n = in.read(iobuf, 0, Math.min(iobuf.length, remaining)); + if (n <= 0) break; + fos.write(iobuf, 0, n); + if (sha != null) sha.update(iobuf, 0, n); + remaining -= n; + } + } + } + if (sha != null && !MessageDigest.isEqual(sha.digest(), chunk.sha1)) { tmp.delete(); conn.disconnect(); continue; } + if (!tmp.renameTo(outFile)) { tmp.delete(); conn.disconnect(); continue; } + conn.disconnect(); + return true; + } catch (Exception e) { + tmp.delete(); + if (conn != null) conn.disconnect(); + } + } + tmp.delete(); + Log.e(TAG, "every CDN failed for chunk " + chunk.guidStr()); + return false; + } + + private static void readFully(InputStream in, byte[] buf) throws IOException { + int off = 0; + while (off < buf.length) { int n = in.read(buf, off, buf.length - off); if (n < 0) throw new IOException("stream ended"); off += n; } + } + + private static void skipFully(InputStream in, int count) throws IOException { + byte[] skip = new byte[Math.min(count, 4096)]; + int remaining = count; + while (remaining > 0) { int n = in.read(skip, 0, Math.min(skip.length, remaining)); if (n < 0) throw new IOException("stream ended"); remaining -= n; } + } + + // ---- selection and verification ------------------------------------------------------------- + + public static List<FileInfo> getRequiredInstallFiles(Manifest manifest) { + List<FileInfo> required = new ArrayList<>(); + for (FileInfo f : manifest.files) if (f.installTags.isEmpty()) required.add(f); + return required.isEmpty() ? new ArrayList<>(manifest.files) : required; + } + + /** Base files plus any file carrying one of the tags; null tags = every file; nothing matched = base only. */ + public static List<FileInfo> resolveInstallFiles(Manifest manifest, List<String> selectedTags) { + if (selectedTags == null) return new ArrayList<>(manifest.files); + if (selectedTags.isEmpty()) return getRequiredInstallFiles(manifest); + Set<String> want = new LinkedHashSet<>(selectedTags); + List<FileInfo> out = new ArrayList<>(); + for (FileInfo f : manifest.files) { + if (f.installTags.isEmpty()) { out.add(f); continue; } + for (String t : f.installTags) if (want.contains(t)) { out.add(f); break; } + } + return out.isEmpty() ? getRequiredInstallFiles(manifest) : out; + } + + public static List<ChunkInfo> uniqueChunksForFiles(Manifest manifest, List<FileInfo> files) { + Map<String, ChunkInfo> byGuid = new LinkedHashMap<>(); + for (ChunkInfo c : manifest.uniqueChunks) byGuid.put(c.guidStr(), c); + Set<String> seen = new LinkedHashSet<>(); + List<ChunkInfo> chunks = new ArrayList<>(); + for (FileInfo f : files) for (ChunkPart p : f.parts) { String g = p.guidStr(); if (seen.add(g)) { ChunkInfo c = byGuid.get(g); if (c != null) chunks.add(c); } } + return chunks; + } + + public static boolean fileExistsWithCorrectHash(File outputFile, long expectedSize, byte[] expectedHash) { + if (outputFile == null || !outputFile.exists() || outputFile.length() != expectedSize) return false; + if (expectedHash == null || expectedHash.length != 20) return false; + boolean allZero = true; + for (byte x : expectedHash) if (x != 0) { allZero = false; break; } + if (allZero) return false; + try (FileInputStream fis = new FileInputStream(outputFile)) { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + byte[] buf = new byte[64 * 1024]; + int n; + while ((n = fis.read(buf)) != -1) digest.update(buf, 0, n); + return MessageDigest.isEqual(digest.digest(), expectedHash); + } catch (Exception e) { + return false; + } + } + + /** The install size for the tags (null = every file), from the manifest alone; -1 on failure. Blocking. */ + public static long fetchInstallSizeBytes(String manifestApiJson, List<String> installTags) { + try { + List<CdnUrl> cdnUrls = parseCdnUrls(manifestApiJson); + if (cdnUrls.isEmpty()) return -1; + byte[] bytes = downloadManifest(manifestApiJson, cdnUrls); + if (bytes == null) return -1; + Manifest m = parseManifest(bytes); + if (m == null) return -1; + long total = 0; + for (FileInfo f : resolveInstallFiles(m, installTags)) total += Math.max(f.fileSize(), 0); + return total > 0 ? total : -1; + } catch (Exception e) { + return -1; + } + } + + // ---- helpers -------------------------------------------------------------------------------- + + static byte[] readFile(File f) throws IOException { + try (FileInputStream fis = new FileInputStream(f)) { + byte[] data = new byte[(int) f.length()]; + int off = 0; + while (off < data.length) { int r = fis.read(data, off, data.length - off); if (r < 0) break; off += r; } + return data; + } + } + + static void deleteDir(File dir) { + if (!dir.exists()) return; + File[] files = dir.listFiles(); + if (files != null) for (File f : files) { if (f.isDirectory()) deleteDir(f); else f.delete(); } + dir.delete(); + } + + 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("%.1f MB/s", bps / 1048576.0) : (bps / 1024) + " KB/s"); + } + + private static String fmt(long bytes) { + if (bytes >= 1073741824L) return String.format("%.1f GB", bytes / 1073741824.0); + if (bytes >= 1048576L) return String.format("%.1f MB", bytes / 1048576.0); + return String.format("%.0f KB", bytes / 1024.0); + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicGame.java b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicGame.java new file mode 100644 index 0000000000..2cb9c7fb36 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicGame.java @@ -0,0 +1,25 @@ +package com.droiddeck.launcher.stores.epic; + +/** + * One Epic Games library entry. {@code appName} is the launcher's app slug (the manifest URL and + * the launch arguments take it), {@code namespace} the product's sandbox, {@code catalogItemId} + * the catalog UUID; the three together address every Epic API call for the game. + */ +public class EpicGame { + public String appName = ""; + public String namespace = ""; + public String catalogItemId = ""; + public String title = ""; + public String developer = ""; + public String description = ""; + /** DieselGameBoxTall: the tall portrait art. */ + public String artCover = ""; + /** DieselGameBox or Thumbnail. */ + public String artSquare = ""; + public String version = ""; + public long installSize = 0L; + public boolean canRunOffline = true; + public boolean isDLC = false; + public String baseGameCatalogItemId = ""; + public String releaseDate = ""; +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicInstallTags.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicInstallTags.kt new file mode 100644 index 0000000000..dcb178d505 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicInstallTags.kt @@ -0,0 +1,44 @@ +package com.droiddeck.launcher.stores.epic + +import java.util.Locale + +/** + * Epic tags a manifest's files by language so a client installs the base files plus one language + * instead of every language. The table (the launcher's tag names per language) is Legendary's; + * the language is the device's locale, English when it is not one Epic ships. + */ +object EpicInstallTags { + private val LANGUAGE_TO_TAGS: Map<String, List<String>> = mapOf( + "ar" to listOf("Arabic", "ar"), "bg" to listOf("Bulgarian", "bg-BG", "bg"), + "zh-Hans" to listOf("Chinese", "ChineseSimplified", "zh-Hans", "zh_Hans", "zh"), "zh-Hant" to listOf("ChineseTraditional", "zh-Hant", "zh_Hant"), + "cs" to listOf("Czech", "cs-CZ", "cs"), "da" to listOf("Danish", "da-DK", "da"), "nl" to listOf("Dutch", "nl-NL", "nl"), + "en" to listOf("English", "en-US", "en"), "fi" to listOf("Finnish", "fi-FI", "fi"), "fr" to listOf("French", "fr-FR", "fr"), + "de" to listOf("German", "de-DE", "de"), "el" to listOf("Greek", "el-GR", "el"), "hu" to listOf("Hungarian", "hu-HU", "hu"), + "it" to listOf("Italian", "it-IT", "it"), "ja" to listOf("Japanese", "ja-JP", "ja"), "ko" to listOf("Korean", "ko-KR", "ko"), + "no" to listOf("Norwegian", "nb-NO", "no"), "pl" to listOf("Polish", "pl-PL", "pl"), "pt" to listOf("Portuguese", "pt-PT", "pt"), + "pt-BR" to listOf("PortugueseBrazilian", "Brazilian", "pt-BR", "br"), "ro" to listOf("Romanian", "ro-RO", "ro"), + "ru" to listOf("Russian", "ru-RU", "ru"), "es" to listOf("Spanish", "es-ES", "es"), "es-419" to listOf("SpanishLatinAmerica", "Latam", "es-MX", "es_mx"), + "sv" to listOf("Swedish", "sv-SE", "sv"), "th" to listOf("Thai", "th-TH", "th"), "tr" to listOf("Turkish", "tr-TR", "tr"), + "uk" to listOf("Ukrainian", "uk-UA", "uk"), "vi" to listOf("Vietnamese", "vi-VN", "vi"), + ) + + private fun key(locale: Locale): String? { + val l = locale.language.lowercase(Locale.ROOT) + val r = locale.country.uppercase(Locale.ROOT) + return when (l) { + "zh" -> if (r == "TW" || r == "HK" || r == "MO") "zh-Hant" else "zh-Hans" + "pt" -> if (r == "BR") "pt-BR" else "pt" + "es" -> if (r == "ES" || r.isEmpty()) "es" else "es-419" + "nb", "nn" -> "no" + else -> if (LANGUAGE_TO_TAGS.containsKey(l)) l else null + } + } + + /** The install tags for the device's language; English when Epic has no tags for it. Never empty. */ + @JvmStatic + fun tagsForDevice(): List<String> = LANGUAGE_TO_TAGS[key(Locale.getDefault()) ?: "en"] ?: LANGUAGE_TO_TAGS.getValue("en") + + /** The two-letter code handed to the game as -epiclocale; "en" when the language is not one Epic knows. */ + @JvmStatic + fun localeCodeForDevice(): String = key(Locale.getDefault())?.substringBefore('-') ?: "en" +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLaunchData.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLaunchData.kt new file mode 100644 index 0000000000..6119bda75e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLaunchData.kt @@ -0,0 +1,55 @@ +package com.droiddeck.launcher.stores.epic + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.stores.StoreGameSidecar + +/** + * What an Epic game's launch line needs beyond the exe, built once at install time into the + * sidecar's arguments: the portal and identity arguments every EOS title expects + * (`-EpicPortal -epicusername -epicuserid -epicsandboxid -epiclocale -epicapp -epicenv=Prod`), the + * deployment id when the app has an EOS sidecar, and the extra flags Epic ships for the title + * (`AdditionalCommandLine`). The sign-in triple (`-AUTH_LOGIN/-AUTH_PASSWORD/-AUTH_TYPE`) is not + * here: the exchange code lives minutes, so the launcher .bat takes it from a file the app writes + * right before a launch (StoreLaunch.prepare). + * + * The EOS overlay is never provisioned: nothing points the game's EOS SDK at an overlay component. + */ +object EpicLaunchData { + private const val TAG = "EpicLaunch" + + /** The static launch arguments for [appName] in [namespace]. Blocking: one catalog call for the extra flags. */ + fun arguments(context: Context, token: String?, appName: String, namespace: String, catalogItemId: String, deploymentId: String): List<String> { + val creds = EpicCredentialStore.load(context) + val displayName = creds?.displayName?.takeIf { it.isNotEmpty() } ?: "EpicUser" + val accountId = creds?.accountId?.takeIf { it.isNotEmpty() } ?: "0" + val args = ArrayList<String>() + args += "-EpicPortal" + args += "-epicusername=\"${sanitize(displayName).replace("\"", "")}\"" + args += "-epicuserid=${sanitize(accountId)}" + args += "-epicsandboxid=${sanitize(namespace)}" + args += "-epiclocale=${EpicInstallTags.localeCodeForDevice()}" + args += "-epicapp=${sanitize(appName)}" + args += "-epicenv=Prod" + if (deploymentId.isNotEmpty()) args += "-epicdeploymentid=${sanitize(deploymentId)}" + if (token != null && catalogItemId.isNotEmpty()) { + val extra = EpicApiClient.getAdditionalCommandLine(token, namespace, catalogItemId) + if (!extra.isNullOrBlank()) { + val clean = extra.filter { it >= ' ' && it != '\u007f' }.trim() + if (clean.isNotEmpty()) { args += clean; Log.i(TAG, "$appName: extra command line present") } + } + } + return args + } + + /** The sidecar's identifiers the launcher reads back. */ + fun extras(appName: String, namespace: String, catalogItemId: String, deploymentId: String): Map<String, String> = buildMap { + put("appName", appName); put("namespace", namespace); put("catalogItemId", catalogItemId) + if (deploymentId.isNotEmpty()) put("deploymentId", deploymentId) + } + + /** True when [sidecar] is an Epic install that can take a sign-in code. */ + fun wantsExchangeCode(sidecar: StoreGameSidecar): Boolean = sidecar.store == com.droiddeck.launcher.stores.Store.EPIC + + private fun sanitize(s: String): String = s.replace("\n", "").replace("\r", "").replace("\u0000", "") +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLibrary.kt new file mode 100644 index 0000000000..5b623828c1 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicLibrary.kt @@ -0,0 +1,119 @@ +package com.droiddeck.launcher.stores.epic + +import android.content.Context +import android.content.SharedPreferences +import android.util.Log +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.atomic.AtomicBoolean + +/** + * The signed-in account's Epic library: the library service's records, each enriched from the + * catalog (title, art, DLC flag), cached so the tab paints at once and re-synced when forced or + * 15 minutes stale. DLC entries are folded away under their base game. Ported from Bannerlator's + * EpicLibraryRepo. + */ +object EpicLibrary { + private const val TAG = "EpicLibrary" + private const val CACHE_KEY = "library_cache" + private const val LAST_SYNC_KEY = "library_synced_at" + private const val THROTTLE_MS = 6L * 60L * 60L * 1000L + + private val syncing = AtomicBoolean(false) + + sealed interface SyncResult { + class Ok(val games: List<EpicGame>) : SyncResult + class Failed(val message: String) : SyncResult + object NotLoggedIn : SyncResult + object Busy : SyncResult + object Throttled : SyncResult + } + + fun cached(context: Context): List<EpicGame> { + val json = EpicPrefs.get(context).getString(CACHE_KEY, null) ?: return emptyList() + return runCatching { + val arr = JSONArray(json) + val list = ArrayList<EpicGame>(arr.length()) + for (i in 0 until arr.length()) { + val j = arr.optJSONObject(i) ?: continue + val g = EpicGame() + g.appName = j.optString("appName", ""); g.namespace = j.optString("namespace", ""); g.catalogItemId = j.optString("catalogItemId", "") + g.title = j.optString("title", ""); g.artCover = j.optString("artCover", ""); g.artSquare = j.optString("artSquare", "") + g.developer = j.optString("developer", ""); g.description = j.optString("description", ""); g.version = j.optString("version", "") + g.installSize = j.optLong("installSize", 0L).let { if (it > 1_099_511_627_776L) 0L else it } + g.canRunOffline = j.optBoolean("canRunOffline", true) + if (g.appName.isNotEmpty()) list.add(g) + } + list.distinctBy { it.appName }.sortedBy { it.title.lowercase() } + }.getOrDefault(emptyList()) + } + + /** Remembers a game's install size once the manifest said it, for the Install button. */ + fun rememberSize(context: Context, appName: String, bytes: Long) { + if (bytes <= 0) return + val p = EpicPrefs.get(context) + p.edit().putLong("size_$appName", bytes).apply() + } + + fun sync(context: Context, force: Boolean = false, onStatus: (String) -> Unit = {}): SyncResult { + if (!syncing.compareAndSet(false, true)) return SyncResult.Busy + try { + val app = context.applicationContext + val p = EpicPrefs.get(app) + val cachedList = cached(app) + val lastSync = p.getLong(LAST_SYNC_KEY, 0L) + if (!force && cachedList.isNotEmpty() && System.currentTimeMillis() - lastSync < THROTTLE_MS) return SyncResult.Throttled + onStatus("Checking sign-in…") + val token = EpicCredentialStore.getValidAccessToken(app) ?: return SyncResult.NotLoggedIn + onStatus("Fetching game list…") + val raw = EpicApiClient.getLibraryItems(token) + if (raw.isNullOrEmpty()) return if (cachedList.isEmpty()) SyncResult.Failed("No games found in your Epic library") else SyncResult.Ok(cachedList) + val total = raw.size + var done = 0 + for (game in raw) { + EpicApiClient.enrichFromCatalog(token, game) + if (game.releaseDate.isNotEmpty()) p.edit().putString("release_${game.appName}", game.releaseDate).apply() + done++ + if (done % 5 == 0) onStatus("Loading game details… ($done/$total)") + } + val main = raw.filter { !it.isDLC } + val display = (if (main.isEmpty()) raw else main).distinctBy { it.appName }.sortedWith { a, b -> a.title.compareTo(b.title, ignoreCase = true) } + for (fresh in display) { + val old = cachedList.firstOrNull { it.appName == fresh.appName } ?: continue + fresh.version = old.version + fresh.installSize = old.installSize + } + saveCache(p, display) + p.edit().putLong(LAST_SYNC_KEY, System.currentTimeMillis()).apply() + Log.i(TAG, "sync: raw=${raw.size} shown=${display.size}") + return SyncResult.Ok(display) + } catch (e: Exception) { + Log.w(TAG, "sync failed: ${e.message}") + return SyncResult.Failed(e.message ?: "Sync failed") + } finally { + syncing.set(false) + } + } + + private fun saveCache(prefs: SharedPreferences, games: List<EpicGame>) { + runCatching { + val arr = JSONArray() + for (g in games) arr.put(JSONObject().put("appName", g.appName).put("namespace", g.namespace).put("catalogItemId", g.catalogItemId) + .put("title", g.title).put("artCover", g.artCover).put("artSquare", g.artSquare).put("developer", g.developer) + .put("description", g.description).put("version", g.version).put("installSize", g.installSize).put("canRunOffline", g.canRunOffline)) + prefs.edit().putString(CACHE_KEY, arr.toString()).apply() + } + } + + fun toCatalogItem(context: Context, g: EpicGame): CatalogItem = CatalogItem( + store = Store.EPIC, id = g.appName, title = g.title.ifBlank { g.appName }, + imageUrl = g.artSquare.ifBlank { null } ?: g.artCover.ifBlank { null }, + tallImageUrl = g.artCover.ifBlank { null } ?: g.artSquare.ifBlank { null }, + developer = g.developer, description = com.droiddeck.launcher.stores.cleanStoreText(g.description), owned = true, + sizeBytes = EpicPrefs.get(context).getLong("size_${g.appName}", g.installSize), + storeUrl = "https://store.epicgames.com/en-US/browse?q=${java.net.URLEncoder.encode(g.title, "UTF-8")}", + extra = mapOf("namespace" to g.namespace, "catalogItemId" to g.catalogItemId, "version" to (g.version ?: "")), + ) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicNative.kt new file mode 100644 index 0000000000..59849fd605 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicNative.kt @@ -0,0 +1,117 @@ +// JNI symbols depend on this package path and class name (app/src/main/rust/stores/JNI.md). +package com.droiddeck.launcher.stores.epic + +import android.util.Log +import com.droiddeck.launcher.stores.StoresNative +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference + +/** + * The native half of an Epic install 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. Everything around them (manifest, install tags, delta pass, post-install) + * stays in the manager. + * + * 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. + * + * Both block like the Java loops they replace and poll [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. + */ +object EpicNative { + private const val TAG = "EpicNative" + + /** Callbacks from a native run; every method is called on a native thread. One interface serves both runs. */ + interface Listener { + /** Fetch only, once before any fetch: the plan the engine derived (the manager cross-checks it). */ + fun onPlan(chunksTotal: Int, bytesTotal: Long, chunkDir: String) + /** Fetch: per accounted chunk (cached-skip or fetched), done/total = chunks. Assembly: per file written, done/total = files. */ + fun onProgress(bytesDone: Long, bytesTotal: Long, done: Int, total: Int) + /** Engine log line (already in logcat under `EpicNative`). */ + fun onLog(line: String) + /** Terminal. Fetch: bytes credited; assembly: 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, + @JvmField val error: String, @JvmField val bytes: Long, @JvmField val done: Int, @JvmField val total: Int, + ) + + private class Completion(val success: Boolean, val error: String, val bytes: Long) + + /** The fetch: chunks for [pendingFileIdx] into the cache. */ + @JvmStatic + fun run( + manifest: ByteArray, installDir: String, chunkCacheDir: String, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedChunks: Int, expectedBytes: Long, + caBundlePath: String, maxWorkers: Int, processWorkers: Int, cancel: AtomicBoolean?, listener: Listener, + ): Result = drive("nativeStart", cancel, listener) { inner -> + nativeStart(manifest, installDir, chunkCacheDir, cdnPrefixes, pendingFileIdx, expectedChunks, expectedBytes, caBundlePath, maxWorkers, processWorkers, inner) + } + + /** The assembly: the files [fileIdx] written from the cache, the cache removed on success. */ + @JvmStatic + 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 { + 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 { + 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) + 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 { + 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) + } + 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 (cancel != null && cancel.get()) { + nativeCancel(handle) + cancelSent = true + latch.await(5, TimeUnit.SECONDS) + break + } + } + } finally { + nativeRelease(handle) + } + val c = completion.get() + val success = !cancelSent && c != null && c.success + val error = when { cancelSent -> "cancelled"; c == null -> "no completion"; else -> c.error } + return Result(true, success, cancelSent, error, c?.bytes ?: 0L, doneRef.get(), totalRef.get()) + } + + @JvmStatic + private external fun nativeStart( + manifest: ByteArray, installDir: String, chunkCacheDir: String, cdnPrefixes: Array<String>, pendingFileIdx: IntArray, expectedChunks: Int, expectedBytes: Long, + caBundlePath: String, maxWorkers: Int, processWorkers: Int, listener: Listener, + ): Long + + @JvmStatic + private external fun nativeAssemble(manifest: ByteArray, installDir: String, chunkCacheDir: String, fileIdx: IntArray, listener: Listener): Long + + @JvmStatic + private external fun nativeCancel(handle: Long) + + @JvmStatic + private external fun nativeRelease(handle: Long) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicPrefs.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicPrefs.kt new file mode 100644 index 0000000000..ae2bfadb34 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicPrefs.kt @@ -0,0 +1,14 @@ +package com.droiddeck.launcher.stores.epic + +import android.content.Context +import android.content.SharedPreferences + +/** + * Epic's own records that are not credentials: the library cache, each game's release date, the + * cached deployment id and extra command line a launch needs. App-private preferences; the sign-in + * lives in StoreAccounts. Public: the Java clients read it too. + */ +object EpicPrefs { + @JvmStatic + fun get(context: Context): SharedPreferences = context.applicationContext.getSharedPreferences("stores.epic", Context.MODE_PRIVATE) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicStoreCatalog.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicStoreCatalog.kt new file mode 100644 index 0000000000..6d4f2d95c5 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicStoreCatalog.kt @@ -0,0 +1,225 @@ +package com.droiddeck.launcher.stores.epic + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreNet +import com.droiddeck.launcher.stores.StoreShelves +import org.json.JSONArray +import org.json.JSONObject +import java.text.SimpleDateFormat +import java.util.Date +import java.util.Locale +import java.util.TimeZone +import java.util.concurrent.ConcurrentHashMap + +/** + * The Epic Games Store catalog through the GraphQL endpoint the store website uses + * (`store.epicgames.com/graphql`, which wants a browser User-Agent): `Catalog.searchStore` with + * the base-game category, sorted by price / release date / relevance, on sale or free. Elements + * carry the offer id, the namespace, the catalog item ids (what the library records, so ownership + * is matched on those), key images and pre-formatted prices. "Free this week" comes from the + * static `freeGamesPromotions` feed. + */ +object EpicStoreCatalog { + private const val TAG = "EpicStore" + private const val GRAPHQL = "https://store.epicgames.com/graphql" + private const val PROMOS = "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions" + private const val FEATURED_TTL_MS = 3 * 60 * 60 * 1000L + private const val SEARCH_TTL_MS = 5 * 60 * 1000L + private const val KEY_FEATURED = "store_featured" + + private const val ELEMENT_FIELDS = + "title id namespace description effectiveDate releaseDate offerType productSlug urlSlug " + + "items{id namespace} keyImages{type url} seller{name} tags{name} " + + "catalogNs{mappings(pageType:\"productHome\"){pageSlug pageType}} " + + "price(country:\$country){totalPrice{discountPrice originalPrice discount currencyCode " + + "fmtPrice(locale:\$locale){originalPrice discountPrice}}}" + + private class Cached<T>(val value: T, val at: Long) + private var featuredCache: Cached<StoreShelves>? = null + private val searchCache = ConcurrentHashMap<String, Cached<List<CatalogItem>>>() + + private fun country(): String = Locale.getDefault().country.takeIf { it.length == 2 }?.uppercase(Locale.ROOT) ?: "US" + + private fun nowIso(): String { + val f = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US) + f.timeZone = TimeZone.getTimeZone("UTC") + return f.format(Date()) + } + + private fun searchStore(count: Int, keywords: String? = null, sortBy: String = "relevancy", sortDir: String = "DESC", onSale: Boolean? = null, freeGame: Boolean? = null, releasedOnly: Boolean = false): List<CatalogItem> { + val args = StringBuilder("category:\"games/edition/base\",count:\$count,country:\$country,locale:\$locale,sortBy:\$sortBy,sortDir:\$sortDir,allowCountries:\$country") + if (keywords != null) args.append(",keywords:\$keywords") + if (onSale != null) args.append(",onSale:$onSale") + if (freeGame != null) args.append(",freeGame:$freeGame") + if (releasedOnly) args.append(",releaseDate:\"[,${nowIso()}]\"") + val query = "query q(\$count:Int,\$country:String!,\$locale:String,\$sortBy:String,\$sortDir:String" + + (if (keywords != null) ",\$keywords:String" else "") + "){Catalog{searchStore($args){elements{$ELEMENT_FIELDS} paging{total}}}}" + val vars = JSONObject().put("count", count).put("country", country()).put("locale", "en-US").put("sortBy", sortBy).put("sortDir", sortDir) + if (keywords != null) vars.put("keywords", keywords) + val resp = StoreNet.postJson(GRAPHQL, JSONObject().put("query", query).put("variables", vars).toString()) ?: return emptyList() + val elements = runCatching { JSONObject(resp).optJSONObject("data")?.optJSONObject("Catalog")?.optJSONObject("searchStore")?.optJSONArray("elements") } + .onFailure { Log.w(TAG, "searchStore parse failed: ${it.message}") }.getOrNull() ?: return emptyList() + return parseElements(elements) + } + + private fun parseElements(arr: JSONArray): List<CatalogItem> { + val out = ArrayList<CatalogItem>(arr.length()) + for (i in 0 until arr.length()) arr.optJSONObject(i)?.let { e -> parseElement(e)?.let { out.add(it) } } + return out.distinctBy { it.id } + } + + fun parseElement(e: JSONObject): CatalogItem? { + val id = e.optString("id", "") + val title = e.optString("title", "") + if (id.isEmpty() || title.isEmpty()) return null + val ns = e.optString("namespace", "") + var wide: String? = null; var tall: String? = null; var thumb: String? = null + e.optJSONArray("keyImages")?.let { imgs -> + for (k in 0 until imgs.length()) { + val img = imgs.optJSONObject(k) ?: continue + val url = img.optString("url", "") + if (url.isBlank()) continue + // The opaque box images first; the logo variants are transparent and never used. + when (img.optString("type", "")) { + "DieselGameBox" -> wide = url + "OfferImageWide", "DieselStoreFrontWide" -> if (wide == null) wide = url + "DieselGameBoxTall" -> tall = url + "OfferImageTall", "DieselStoreFrontTall" -> if (tall == null) tall = url + "Thumbnail" -> if (thumb == null) thumb = url + } + } + } + val price = e.optJSONObject("price")?.optJSONObject("totalPrice") + val original = price?.optLong("originalPrice", -1L) ?: -1L + val discounted = price?.optLong("discountPrice", -1L) ?: -1L + val fmt = price?.optJSONObject("fmtPrice") + val fmtOriginal = fmt?.optString("originalPrice", "").orEmpty() + val fmtDiscount = fmt?.optString("discountPrice", "").orEmpty() + val hasPrice = original >= 0 && discounted >= 0 + val isFree = hasPrice && discounted == 0L + val discountPct = if (hasPrice && original > 0 && discounted < original) ((original - discounted) * 100 / original).toInt() else 0 + val finalPrice = when { isFree -> "Free"; fmtDiscount.isNotBlank() && fmtDiscount != "0" -> fmtDiscount; else -> fmtOriginal } + val tagLine = buildList { + e.optJSONArray("tags")?.let { tags -> for (t in 0 until tags.length()) { val n = tags.optJSONObject(t)?.optString("name").orEmpty(); if (n.isNotBlank() && n != "Windows" && size < 3) add(n) } } + }.joinToString(", ") + // Epic's public store GraphQL has no age rating on an offer (no ageGatings/ratings field on + // CatalogOffer or StoreConfig); only a content tag, if Epic sets one, can mark it. + val allTags = buildList { e.optJSONArray("tags")?.let { t -> for (k in 0 until t.length()) t.optJSONObject(k)?.optString("name")?.let { add(it) } } } + val itemIds = buildList { + e.optJSONArray("items")?.let { items -> for (j in 0 until items.length()) items.optJSONObject(j)?.optString("id")?.takeIf { it.isNotBlank() }?.let { add(it) } } + } + val slug = pageSlugOf(e) + val storeUrl = if (slug.isNotBlank()) "https://store.epicgames.com/en-US/p/$slug" else "https://store.epicgames.com/en-US/browse?q=${java.net.URLEncoder.encode(title, "UTF-8")}" + return CatalogItem( + store = Store.EPIC, id = id, title = title, imageUrl = wide ?: thumb, tallImageUrl = tall ?: thumb, tags = tagLine, + isFree = isFree, hasPrice = hasPrice, finalPrice = finalPrice, originalPrice = if (discountPct > 0) fmtOriginal else "", discountPercent = discountPct, + storeUrl = storeUrl, developer = e.optJSONObject("seller")?.optString("name", "").orEmpty(), + releaseDate = e.optString("releaseDate", e.optString("effectiveDate", "")), description = com.droiddeck.launcher.stores.cleanStoreText(e.optString("description", "")), + extra = buildMap { put("namespace", ns); if (itemIds.isNotEmpty()) put("items", itemIds.joinToString(",")); if (slug.isNotBlank()) put("slug", slug) }, + mature = com.droiddeck.launcher.stores.matureByTags(allTags), + ) + } + + private fun pageSlugOf(e: JSONObject): String { + e.optJSONObject("catalogNs")?.optJSONArray("mappings")?.let { m -> + for (i in 0 until m.length()) { val map = m.optJSONObject(i) ?: continue; if (map.optString("pageType") == "productHome") { val s = map.optString("pageSlug", ""); if (s.isNotBlank()) return s } } + } + var slug = e.optString("productSlug", "") + if (slug == "null") slug = "" + return slug.removeSuffix("/home") + } + + /** The Store tab's shelves: this week's giveaways as Free, sales as Deals, newest releases, trending = free-to-play picks. Blocking. */ + fun featured(context: Context, force: Boolean = false): StoreShelves? { + val now = System.currentTimeMillis() + featuredCache?.let { if (!force && now - it.at < FEATURED_TTL_MS) return it.value } + // Shelves saved on an earlier run are as good while they are young: shown, not fetched again. + if (!force && now - EpicPrefs.get(context).getLong("store_featured_at", 0L) < FEATURED_TTL_MS) cachedFeatured(context)?.let { return it } + val freeNow = fetchPromos() + val onSale = searchStore(24, sortBy = "currentPrice", sortDir = "ASC", onSale = true).filter { it.discountPercent > 0 } + val newest = searchStore(24, sortBy = "releaseDate", sortDir = "DESC", releasedOnly = true) + val freeToPlay = searchStore(24, sortBy = "relevancy", sortDir = "DESC", freeGame = true) + val result = StoreShelves(whatsNew = newest, deals = onSale, free = (freeNow + freeToPlay).distinctBy { it.id }, trending = searchStore(24, sortBy = "relevancy", sortDir = "DESC", releasedOnly = true)) + if (!result.isEmpty) { + featuredCache = Cached(result, now) + persist(context, result) + Log.i(TAG, "shelves: freeNow=${freeNow.size} sale=${onSale.size} new=${newest.size} f2p=${freeToPlay.size}") + return result + } + Log.w(TAG, "every store feed came back empty; using the on-disk mirror") + return loadPersisted(context) + } + + fun cachedFeatured(context: Context): StoreShelves? = featuredCache?.value ?: loadPersisted(context) + + fun search(query: String): List<CatalogItem> { + val term = query.trim() + if (term.length < 2) return emptyList() + val key = term.lowercase(Locale.ROOT) + val now = System.currentTimeMillis() + searchCache[key]?.let { if (now - it.at < SEARCH_TTL_MS) return it.value } + val results = searchStore(30, keywords = term) + searchCache[key] = Cached(results, now) + return results + } + + /** This week's giveaway titles (100% off right now). */ + private fun fetchPromos(): List<CatalogItem> { + val body = StoreNet.get("$PROMOS?locale=en-US&country=${country()}&allowCountries=${country()}") ?: return emptyList() + return runCatching { + val elements = JSONObject(body).optJSONObject("data")?.optJSONObject("Catalog")?.optJSONObject("searchStore")?.optJSONArray("elements") ?: JSONArray() + val out = ArrayList<CatalogItem>() + for (i in 0 until elements.length()) { + val el = elements.optJSONObject(i) ?: continue + val current = el.optJSONObject("promotions")?.optJSONArray("promotionalOffers") + if (current == null || current.length() == 0) continue + val inner = current.optJSONObject(0)?.optJSONArray("promotionalOffers") ?: continue + if (inner.length() == 0) continue + val discount = inner.optJSONObject(0)?.optJSONObject("discountSetting") + if (discount == null || discount.optInt("discountPercentage", -1) != 0) continue + val end = inner.optJSONObject(0)?.optString("endDate", "").orEmpty() + val base = parseElement(el) ?: continue + out.add(base.copy(isFree = true, hasPrice = true, finalPrice = "Free", originalPrice = base.originalPrice.ifBlank { + el.optJSONObject("price")?.optJSONObject("totalPrice")?.optJSONObject("fmtPrice")?.optString("originalPrice", "").orEmpty() + }, discountPercent = 100, tags = if (end.length >= 10) "Free until ${end.take(10)}" else base.tags)) + } + out.distinctBy { it.id } + }.onFailure { Log.w(TAG, "promos parse failed: ${it.message}") }.getOrDefault(emptyList()) + } + + private fun itemToJson(i: CatalogItem): JSONObject = JSONObject().apply { + put("id", i.id); put("title", i.title); put("image", i.imageUrl ?: ""); put("tall", i.tallImageUrl ?: ""); put("tags", i.tags) + put("free", i.isFree); put("hasPrice", i.hasPrice); put("final", i.finalPrice); put("orig", i.originalPrice); put("disc", i.discountPercent) + put("url", i.storeUrl); put("dev", i.developer); put("rel", i.releaseDate); put("desc", i.description); put("extra", JSONObject(i.extra as Map<*, *>)) + if (i.mature) put("mature", true) + } + + private fun itemFromJson(o: JSONObject): CatalogItem { + val extra = HashMap<String, String>() + o.optJSONObject("extra")?.let { e -> e.keys().forEach { k -> extra[k] = e.optString(k) } } + return CatalogItem( + store = Store.EPIC, id = o.optString("id"), title = o.optString("title"), imageUrl = o.optString("image").ifBlank { null }, tallImageUrl = o.optString("tall").ifBlank { null }, + tags = o.optString("tags"), isFree = o.optBoolean("free"), hasPrice = o.optBoolean("hasPrice"), finalPrice = o.optString("final"), originalPrice = o.optString("orig"), + discountPercent = o.optInt("disc"), storeUrl = o.optString("url"), developer = o.optString("dev"), releaseDate = o.optString("rel"), description = o.optString("desc"), extra = extra, + mature = o.optBoolean("mature"), + ) + } + + private fun listToJson(l: List<CatalogItem>) = JSONArray().apply { l.forEach { put(itemToJson(it)) } } + private fun listFromJson(a: JSONArray?): List<CatalogItem> = if (a == null) emptyList() else List(a.length()) { a.optJSONObject(it) }.filterNotNull().map(::itemFromJson) + + private fun persist(context: Context, f: StoreShelves) { + runCatching { + EpicPrefs.get(context).edit().putString(KEY_FEATURED, JSONObject().put("new", listToJson(f.whatsNew)).put("deals", listToJson(f.deals)).put("free", listToJson(f.free)).put("trending", listToJson(f.trending)).toString()).putLong("store_featured_at", System.currentTimeMillis()).apply() + } + } + + private fun loadPersisted(context: Context): StoreShelves? = runCatching { + val s = EpicPrefs.get(context).getString(KEY_FEATURED, null) ?: return null + val o = JSONObject(s) + StoreShelves(listFromJson(o.optJSONArray("new")), listFromJson(o.optJSONArray("deals")), listFromJson(o.optJSONArray("free")), listFromJson(o.optJSONArray("trending"))).takeIf { !it.isEmpty } + }.getOrNull() +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt new file mode 100644 index 0000000000..d889674d1f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt @@ -0,0 +1,101 @@ +package com.droiddeck.launcher.stores.gog + +import android.content.Context +import android.net.Uri +import android.util.Log +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreAccounts +import com.droiddeck.launcher.stores.StoreNet +import org.json.JSONObject +import java.net.HttpURLConnection +import java.net.URL + +/** + * The GOG sign-in: the token the OAuth page hands back, kept in StoreAccounts' private file, + * refreshed through auth.gog.com when it has expired (the client id and secret are GOG Galaxy's + * own, public ones, as every third-party GOG client uses). Nothing here is ever logged: the token + * endpoint's URL itself carries the secret and the refresh token. + */ +object GogAuth { + private const val TAG = "GogAuth" + private const val CLIENT_ID = "46899977096215655" + private const val CLIENT_SECRET = "9d85c43b1482497dbbce61f6e4aa173a433796eeae2ca8c5f6129f2dc4de46d9" + private const val TOKEN_URL = "https://auth.gog.com/token?client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=refresh_token&refresh_token=" + + /** The login page, as GOG Galaxy opens it: an implicit grant that returns the token in the redirect's fragment. */ + const val AUTH_URL = "https://auth.gog.com/auth?client_id=$CLIENT_ID" + + "&redirect_uri=https%3A%2F%2Fembed.gog.com%2Fon_login_success%3Forigin%3Dclient&response_type=token&layout=client2" + const val REDIRECT_PREFIX = "https://embed.gog.com/on_login_success" + const val GALAXY_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) GOG Galaxy/2.0" + + fun authUrl(state: String): String = "$AUTH_URL&state=${Uri.encode(state)}" + + fun isSignedIn(context: Context): Boolean = StoreAccounts.read(context, Store.GOG)?.optString("access_token", "")?.isNotEmpty() == true + + /** Saves a fresh sign-in and looks the account's name up for the chip. Blocking. */ + fun saveLogin(context: Context, accessToken: String, refreshToken: String?, userId: String?) { + var username = "" + runCatching { + val body = StoreNet.get("https://embed.gog.com/userData.json", bearer = accessToken, userAgent = GALAXY_UA) + if (body != null) username = JSONObject(body).optString("username", "") + } + val json = JSONObject().put("access_token", accessToken).put("username", username) + .put("login_time", System.currentTimeMillis() / 1000L).put("expires_in", 3600) + if (!refreshToken.isNullOrEmpty()) json.put("refresh_token", refreshToken) + if (!userId.isNullOrEmpty()) json.put("user_id", userId) + StoreAccounts.write(context, Store.GOG, json) + Log.i(TAG, "signed in") + } + + /** The raw stored token, expired or not; null when signed out. */ + fun storedToken(context: Context): String? = StoreAccounts.read(context, Store.GOG)?.optString("access_token", "")?.ifEmpty { null } + + /** A usable access token, refreshed when the recorded login time says it has expired; null when signed out or the refresh failed. */ + @JvmStatic + fun validToken(context: Context): String? { + val json = StoreAccounts.read(context, Store.GOG) ?: return null + val token = json.optString("access_token", "").ifEmpty { return null } + val loginTime = json.optLong("login_time", 0L) + val expiresIn = json.optLong("expires_in", 3600L) + val now = System.currentTimeMillis() / 1000L + return if (loginTime == 0L || now >= loginTime + expiresIn - 60) refresh(context) else token + } + + /** Refreshes the access token; the new one, or null on any failure. Blocking. */ + @JvmStatic + fun refresh(context: Context): String? { + val json = StoreAccounts.read(context, Store.GOG) ?: return null + val refreshToken = json.optString("refresh_token", "").ifEmpty { return null } + var conn: HttpURLConnection? = null + return try { + conn = (URL(TOKEN_URL + refreshToken).openConnection() as HttpURLConnection).apply { connectTimeout = 15_000; readTimeout = 15_000 } + if (conn.responseCode != 200) { Log.w(TAG, "refresh: HTTP ${conn.responseCode}"); return null } + val body = conn.inputStream.bufferedReader().readText() + val o = JSONObject(body) + val access = o.optString("access_token", "").ifEmpty { return null } + json.put("access_token", access) + o.optString("refresh_token", "").takeIf { it.isNotEmpty() }?.let { json.put("refresh_token", it) } + json.put("login_time", System.currentTimeMillis() / 1000L).put("expires_in", o.optLong("expires_in", 3600L)) + StoreAccounts.write(context, Store.GOG, json) + access + } catch (e: Exception) { + // Not the throwable: HttpURLConnection's messages echo the URL, which embeds the secrets. + Log.w(TAG, "refresh failed: ${e.javaClass.simpleName}") + null + } finally { + runCatching { conn?.disconnect() } + } + } + + /** Hosts the GOG login page hands off to for a social sign-in; they refuse an embedded browser's identity. */ + fun isIdentityProviderHost(host: String?): Boolean { + val h = host?.lowercase() ?: return false + return h == "google.com" || h.endsWith(".google.com") || h == "youtube.com" || h.endsWith(".youtube.com") || + h == "facebook.com" || h.endsWith(".facebook.com") || h == "apple.com" || h.endsWith(".apple.com") + } + + fun isGogHost(host: String?): Boolean { + val h = host?.lowercase() ?: return false + return h == "gog.com" || h.endsWith(".gog.com") + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt new file mode 100644 index 0000000000..0237d4de52 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt @@ -0,0 +1,121 @@ +package com.droiddeck.launcher.stores.gog + +import android.content.Context +import android.content.Intent +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.InstalledStoreGame +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreBackend +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoreInstalls +import com.droiddeck.launcher.stores.StoreLoginActivity +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.download.DownloadEntry +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.download.DownloadStage +import java.io.File +import java.util.concurrent.atomic.AtomicBoolean + +/** GOG in the Stores section: sign-in, the library, the public shelves, installs. */ +object GogBackend : StoreBackend { + override val store: Store = Store.GOG + + override fun signIn(context: Context) { + context.startActivity(Intent(context, StoreLoginActivity::class.java).putExtra(StoreLoginActivity.EXTRA_STORE, store.id).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + } + + override fun signOut(context: Context) { + GogPrefs.get(context).edit().remove("library_cache").remove("library_cache_v2").remove("library_synced_at").apply() + } + + override fun syncLibrary(context: Context, force: Boolean) { + val app = context.applicationContext + // The cache first, so the tab has something while the sync runs. + val cached = GogLibrary.cached(app) + if (cached.isNotEmpty()) StoresState.post { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { GogLibrary.toCatalogItem(app, it) }) } + StoresState.post { StoresState.status[store] = "Fetching your library…" } + Thread({ + val result = GogLibrary.sync(app, force) { line -> StoresState.post { StoresState.status[store] = line } } + StoresState.post { + StoresState.status.remove(store) + when (result) { + is GogLibrary.SyncResult.Ok -> { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], result.games.map { GogLibrary.toCatalogItem(app, it) }); StoresState.problems.remove(store) } + is GogLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message + GogLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = "GOG session expired: sign in again." + GogLibrary.SyncResult.Busy -> {} + } + } + }, "gog-sync").start() + } + + override fun loadShelves(context: Context, force: Boolean) { + val app = context.applicationContext + GogStoreCatalog.cachedFeatured(app)?.let { StoresState.post { StoresState.shelves[store] = it } } + Thread({ + val shelves = GogStoreCatalog.featured(app, force) + StoresState.post { if (shelves != null) StoresState.shelves[store] = markOwned(shelves) } + }, "gog-shelves").start() + } + + /** The shelves with the library's titles marked owned, so their cards offer Install. */ + private fun markOwned(s: com.droiddeck.launcher.stores.StoreShelves): com.droiddeck.launcher.stores.StoreShelves { + val owned = StoresState.library[store]?.associateBy { it.id } ?: return s + fun mark(l: List<CatalogItem>) = l.map { i -> owned[i.id]?.let { o -> i.copy(owned = true, sizeBytes = o.sizeBytes) } ?: i } + return com.droiddeck.launcher.stores.StoreShelves(mark(s.whatsNew), mark(s.deals), mark(s.free), mark(s.trending)) + } + + override fun install(context: Context, item: CatalogItem, root: File) { + val app = context.applicationContext + val game = GogGame(item.id, item.title, item.imageUrl ?: "", item.description, item.developer, item.tags, 2, item.tallImageUrl) + val folder = StoreInstallRoot.folderFor(app, Store.GOG, game.gameId, game.title, root) + val entry = DownloadEntry(store, item.id, item.title, cover = item.imageUrl, bytesTotal = item.sizeBytes, location = StoreInstallRoot.labelFor(app, folder)) + DownloadQueue.enqueue(app, entry) { InstallJob(app, game, item, folder) } + } + + override fun uninstall(context: Context, game: InstalledStoreGame) {} + + /** One GOG install, as the queue runs it. */ + private class InstallJob(val app: Context, val game: GogGame, val item: CatalogItem, val folder: File) : DownloadQueue.DownloadJob { + private val cancelled = AtomicBoolean(false) + + override fun run(handle: DownloadQueue.JobHandle): String? { + handle.stage(DownloadStage.MANIFEST) + StoreInstalls.begin(folder, Store.GOG, game.gameId, game.title, item.tallImageUrl ?: item.imageUrl, item.imageUrl) + var downloading = false + val result = GogDownloadManager.install(app, game, folder, object : GogDownloadManager.Callback { + override fun onProgress(message: String, pct: Int) { + when { + message.startsWith("Finishing") -> handle.stage(DownloadStage.INSTALL, message) + message.startsWith("Downloading") || message.startsWith("Verified") || message.startsWith("Resuming") -> { + if (!downloading) { downloading = true; handle.stage(DownloadStage.DOWNLOAD, message) } else handle.progress(-1, -1, message) + } + else -> handle.stage(DownloadStage.MANIFEST, message) + } + } + override fun onBytes(done: Long, total: Long, speedBps: Long) { handle.progress(done, total, null, speedBps) } + override fun onLog(line: String) { handle.log(line) } + override fun onSizes(downloadBytes: Long, diskBytes: Long) { handle.diskSize(diskBytes) } + override fun onStage(stage: String, done: Long, total: Long, items: Int, itemsTotal: Int) { + handle.stage(when (stage) { "check" -> DownloadStage.MANIFEST; "verify" -> DownloadStage.VERIFY; else -> DownloadStage.INSTALL }) + handle.stageProgress(done, total, items, itemsTotal, bytes = stage == "install") + } + }, cancelled) ?: return null + if (cancelled.get()) return null + handle.stage(DownloadStage.INSTALL, "Registering with Steam…") + val sidecar = StoreGameSidecar( + Store.GOG, game.gameId, game.title, exe = result.exeRelative.ifEmpty { "" }, + installVersion = result.buildId, installedAt = System.currentTimeMillis(), + cover = item.tallImageUrl ?: item.imageUrl, hero = item.imageUrl, + ) + if (sidecar.exe.isEmpty()) handle.log("gog: no exe found in ${folder.name}; pick one in Steam settings › Added games") + StoreInstalls.complete(app, folder, onStep = { done, steps -> handle.stageProgress(done.toLong(), steps.toLong()) }, sidecar = if (sidecar.exe.isEmpty()) sidecar.copy(exe = "game.exe") else sidecar) + return folder.path + } + + override fun cancel(deleteFiles: Boolean) { + cancelled.set(true) + if (deleteFiles) StoreInstalls.discard(app, folder, listOf(File(folder, ".gog_chunks"))) + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java new file mode 100644 index 0000000000..78f2b73bf1 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogDownloadManager.java @@ -0,0 +1,1079 @@ +package com.droiddeck.launcher.stores.gog; + +import android.content.Context; +import android.util.Log; + +import com.droiddeck.launcher.stores.StoreLog; +import com.droiddeck.launcher.stores.download.StoreDownloadTier; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.zip.GZIPInputStream; +import java.util.zip.Inflater; + +/** + * The GOG install pipeline, ported from Bannerlator's GogDownloadManager and made blocking: the + * download queue runs {@link #install} on its own thread and reads progress through {@link Callback}. + * + * Gen 2: builds → build manifest → depot manifests (base product, matching language) → secure + * link → per file: chunks fetched, inflated, MD5-checked, assembled (the native engine when it is + * there, else the Java pool here). Gen 1: build manifest → per-file Range GET. Neither available: + * the product's Windows installer is downloaded as a file to run once. + * + * Resume and repair come from the per-file size + MD5 check: a complete file is skipped, a damaged + * one re-fetched. Nothing here writes outside the install folder; URLs are only logged redacted. + */ +public final class GogDownloadManager { + + private static final String TAG = "GogDownload"; + private static final int TIMEOUT = 30_000; + private static final String MARKER = "_gog_manifest.json"; + + /** Progress out of a running install. Every method is called on the install's thread. */ + public interface Callback { + /** A new phase or file, as text, with the install's overall percentage. */ + void onProgress(String message, int pct); + /** Bytes landed against the planned total, and the current speed (0 = unknown). */ + default void onBytes(long done, long total, long speedBps) {} + /** A diagnostic line for the engine log. */ + default void onLog(String line) {} + /** What this run fetches and what the game takes on disk. */ + default void onSizes(long downloadBytes, long diskBytes) {} + /** + * The active stage's own count: {@code stage} is "verify" or "install", {@code done} of + * {@code total} an amount (bytes, or items when there are no bytes), {@code items} of + * {@code itemsTotal} the files, when it counts files. + */ + default void onStage(String stage, long done, long total, int items, int itemsTotal) {} + + } + + /** What an install leaves behind for the sidecar. */ + public static final class Result { + /** The chosen exe, relative to the install folder (forward slashes), or "" when none was found. */ + public final String exeRelative; + public final String buildId; + public final long bytes; + Result(String exeRelative, String buildId, long bytes) { this.exeRelative = exeRelative; this.buildId = buildId; this.bytes = bytes; } + } + + /** Thrown for every way an install can fail; the message is what the Downloads page shows. */ + public static final class InstallException extends Exception { + InstallException(String message) { super(message); } + } + + private GogDownloadManager() {} + + /** + * Installs {@code game} into {@code installPath}. Blocks until done; {@code cancelled} set from + * another thread stops it at the next file boundary (what was written stays for a resume). + * Returns null when cancelled. + */ + public static Result install(Context ctx, GogGame game, File installPath, Callback cb, AtomicBoolean cancelled) throws InstallException { + cb.onProgress("Checking sign-in…", 0); + String token = GogAuth.validToken(ctx); + if (token == null) throw new InstallException("Not signed in to GOG"); + if (cancelled.get()) return null; + cb.onProgress("Fetching builds…", 2); + + String buildsUrl = "https://content-system.gog.com/products/" + game.gameId + "/os/windows/builds?generation=2"; + String buildsJson = httpGet(buildsUrl, null); + if (buildsJson == null) buildsJson = httpGet(buildsUrl, token); + if (buildsJson != null) { + Gen2Outcome out = runGen2(ctx, game, token, buildsJson, installPath, cb, cancelled); + if (out.result != null || cancelled.get()) return out.result; + if (out.hardStop) throw new InstallException(out.error); + cb.onLog("gen2 unavailable: " + out.error); + } + if (cancelled.get()) return null; + cb.onProgress("Trying the older build format…", 10); + String builds1Url = "https://content-system.gog.com/products/" + game.gameId + "/os/windows/builds?generation=1"; + String builds1Json = httpGet(builds1Url, null); + if (builds1Json == null) builds1Json = httpGet(builds1Url, token); + if (builds1Json == null) throw new InstallException("No builds available for this game"); + Gen1Outcome one = runGen1(ctx, game, token, builds1Json, installPath, cb, cancelled); + if (one.result != null || cancelled.get()) return one.result; + if (!"NO_CS_BUILDS".equals(one.error)) throw new InstallException("Download failed: " + one.error); + cb.onProgress("No Galaxy builds; fetching the installer…", 12); + return runInstaller(ctx, game, token, installPath, cb, cancelled); + } + + // ---- gen2 ----------------------------------------------------------------------------------- + + private static final class Gen2Outcome { + Result result; String error; boolean hardStop; + } + + private static Gen2Outcome runGen2(Context ctx, GogGame game, String token, String buildsJson, File installPath, Callback cb, AtomicBoolean cancelled) { + Gen2Outcome out = new Gen2Outcome(); + try { + cb.onProgress("Fetching manifest…", 5); + Gen2Manifest gm = resolveGen2Manifest(buildsJson, token, cb); + if (gm == null) { out.error = "gen2 manifest unavailable"; return out; } + JSONObject manifest = gm.manifest; + JSONArray depots = manifest.optJSONArray("depots"); + if (depots == null) { out.error = "no depots in manifest"; return out; } + JSONArray products = manifest.optJSONArray("products"); + String tempExe = null; + if (products != null && products.length() > 0) { + tempExe = products.getJSONObject(0).optString("temp_executable", null); + if (tempExe != null && tempExe.isEmpty()) tempExe = null; + } + // The build manifest lists every product's depots (base + DLC), each tagged by productId; + // the base install takes the base product's only, under a secure link scoped to it. + String baseProductId = manifest.optString("baseProductId", null); + if ((baseProductId == null || baseProductId.isEmpty()) && products != null && products.length() > 0) + baseProductId = products.getJSONObject(0).optString("productId", null); + if (baseProductId == null || baseProductId.isEmpty()) baseProductId = game.gameId; + // The build's required redistributables (MSVC, DirectX, ...) are recorded for the engine + // log; Steam's shared redistributables run in the prefix on first launch as for any shortcut. + JSONArray deps = manifest.optJSONArray("dependencies"); + if (deps != null && deps.length() > 0) cb.onLog("gog: build lists dependencies " + deps); + + cb.onProgress("Reading depot manifests…", 10); + List<DepotFile> files = new ArrayList<>(); + List<String> depotJsons = new ArrayList<>(); + for (int i = 0; i < depots.length(); i++) { + if (cancelled.get()) return out; + JSONObject depot = depots.getJSONObject(i); + String depotPid = depot.optString("productId", ""); + if (!depotPid.isEmpty() && !depotPid.equals(baseProductId)) continue; + if (!languageCompatible(depot)) continue; + String manifestHash = depot.optString("manifest"); + if (manifestHash == null || manifestHash.isEmpty()) continue; + byte[] dmRaw = fetchBytes("https://gog-cdn-fastly.gog.com/content-system/v2/meta/" + buildCdnPath(manifestHash), null); + if (dmRaw == null) { cb.onLog("gog: depot " + i + " meta fetch failed"); continue; } + String dmStr = decompressBytes(dmRaw); + if (dmStr == null) { cb.onLog("gog: depot " + i + " decompress failed"); continue; } + parseDepotManifest(dmStr, files); + depotJsons.add(dmStr); + } + if (files.isEmpty()) { out.error = "no depot files collected"; return out; } + + cb.onProgress("Fetching CDN link…", 15); + String secureLinkUrl = "https://content-system.gog.com/products/" + baseProductId + "/secure_link?_version=2&generation=2&path=/"; + String cdnBase = parseCdnUrl(httpGet(secureLinkUrl, token)); + if (cdnBase == null) { out.error = "secure link refused"; return out; } + + installPath.mkdirs(); + long plannedBytes = 0; + for (DepotFile df : files) plannedBytes += df.totalSize; + if (plannedBytes > 0) { + long usable = installPath.getUsableSpace(); + if (usable > 0 && plannedBytes > usable) { + out.hardStop = true; + out.error = "Not enough free space: need " + formatBytes(plannedBytes) + ", only " + formatBytes(usable) + " free"; + return out; + } + } + // Largest first: the biggest archives start first and small files fill the tail. + Collections.sort(files, (a, b) -> Long.compare(b.totalSize, a.totalSize)); + + final int total = files.size(); + final long planned = plannedBytes; + final AtomicInteger doneCount = new AtomicInteger(0); + final AtomicLong totalBytes = new AtomicLong(0); + final AtomicLong lastSpeedMs = new AtomicLong(System.currentTimeMillis()); + final AtomicLong lastSpeedB = new AtomicLong(0); + final AtomicLong speedBps = new AtomicLong(0); + final AtomicBoolean anyFailed = new AtomicBoolean(false); + final AtomicReference<String> cdnBaseRef = new AtomicReference<>(cdnBase); + final AtomicInteger cdnRefreshCount = new AtomicInteger(0); + final int MAX_CDN_REFRESH = 5; + cb.onSizes(planned, planned); + cb.onBytes(0, planned, 0); + cb.onLog("gog: " + total + " files, " + formatBytes(planned) + ", base=" + baseProductId); + + int threads = downloadThreads(); + if (GogNative.isAvailable()) { + runNativeEngine(ctx, GogNative.KIND_GEN2_CHUNKS, "Verified…", depotJsons, installPath, cdnBaseRef, secureLinkUrl, token, + cdnRefreshCount, MAX_CDN_REFRESH, threads, threads, true, "gog base=" + baseProductId, + cancelled, anyFailed, doneCount, total, planned, totalBytes, lastSpeedMs, lastSpeedB, speedBps, cb); + } else { + cb.onLog("gog: engine=built-in (" + threads + " threads)"); + ExecutorService pool = Executors.newFixedThreadPool(threads, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("gog-dl")); + List<Future<Void>> futures = new ArrayList<>(); + for (DepotFile df : files) { + futures.add(pool.submit((Callable<Void>) () -> { + if (cancelled.get() || anyFailed.get()) return null; + File outFile = new File(installPath, df.relativePath); + outFile.getParentFile().mkdirs(); + if (fileVerified(outFile, df.totalSize, df.md5)) { + int done = doneCount.incrementAndGet(); + long tb = totalBytes.addAndGet(df.totalSize); + cb.onProgress("Verified…", 15 + (int) ((done / (float) total) * 80)); + cb.onBytes(tb, planned, speedBps.get()); + return null; + } + boolean ok = assembleDepotFile(df, outFile, cdnBaseRef, secureLinkUrl, token, cdnRefreshCount, MAX_CDN_REFRESH, cancelled, cb); + if (cancelled.get()) return null; + if (!ok) { cb.onLog("FAIL file=" + df.relativePath); anyFailed.set(true); return null; } + int done = doneCount.incrementAndGet(); + long tb = totalBytes.addAndGet(df.totalSize); + sampleSpeed(tb, lastSpeedMs, lastSpeedB, speedBps); + cb.onProgress("Downloading: " + baseName(df.relativePath) + speedSuffix(speedBps.get()), 15 + (int) ((done / (float) total) * 80)); + cb.onBytes(tb, planned, speedBps.get()); + return null; + })); + } + pool.shutdown(); + try { + for (Future<Void> f : futures) f.get(); + } catch (Exception e) { + pool.shutdownNow(); + out.error = "download pool error: " + e.getClass().getSimpleName(); + return out; + } + } + if (cancelled.get()) return out; + if (anyFailed.get()) { out.error = "one or more chunks failed to download"; return out; } + + cb.onProgress("Finishing…", 96); + writeFile(new File(installPath, MARKER), ("{\"gameId\":\"" + game.gameId + "\",\"buildId\":\"" + gm.buildId + "\"}").getBytes("UTF-8")); + deleteCounted(new File(installPath, ".gog_chunks"), cb); + String clientId = manifest.optString("clientId", null); + if (clientId != null && !clientId.isEmpty()) GogPrefs.get(ctx).edit().putString("client_id_" + game.gameId, clientId).apply(); + // The game's own client secret: cloud saves need a token issued to the game's client. + String clientSecret = manifest.optString("clientSecret", null); + if (clientSecret != null && !clientSecret.isEmpty()) GogPrefs.get(ctx).edit().putString("client_secret_" + game.gameId, clientSecret).apply(); + out.result = new Result(pickExe(installPath, tempExe, game.title), gm.buildId, planned); + return out; + } catch (Exception e) { + out.error = "exception: " + e.getClass().getSimpleName() + (e.getMessage() != null ? ": " + e.getMessage() : ""); + return out; + } + } + + private static final class Gen2Manifest { + final JSONObject manifest; final String buildId; + Gen2Manifest(JSONObject manifest, String buildId) { this.manifest = manifest; this.buildId = buildId; } + } + + /** The first Windows build's manifest, inflated and parsed; null on any failure. */ + private static Gen2Manifest resolveGen2Manifest(String buildsJson, String token, Callback cb) { + try { + JSONArray items = new JSONObject(buildsJson).optJSONArray("items"); + if (items == null || items.length() == 0) { cb.onLog("gog: no gen2 builds"); return null; } + String buildId = null, manifestUrl = null; + for (int i = 0; i < items.length(); i++) { + JSONObject item = items.getJSONObject(i); + if ("windows".equals(item.optString("os"))) { + buildId = item.optString("build_id"); + manifestUrl = item.optString("link"); + if (manifestUrl == null || manifestUrl.isEmpty()) manifestUrl = item.optString("meta_url"); + break; + } + } + if (buildId == null || manifestUrl == null || manifestUrl.isEmpty()) { cb.onLog("gog: no windows build"); return null; } + byte[] raw = fetchBytes(manifestUrl, token); + if (raw == null) { cb.onLog("gog: manifest fetch failed"); return null; } + String str = decompressBytes(raw); + if (str == null) { cb.onLog("gog: manifest decompress failed"); return null; } + return new Gen2Manifest(new JSONObject(str), buildId); + } catch (Exception e) { + cb.onLog("gog: manifest " + e.getClass().getSimpleName()); + return null; + } + } + + /** English or every-language depots; GOG tags the rest by language code. */ + private static boolean languageCompatible(JSONObject depot) { + JSONArray languages = depot.optJSONArray("languages"); + if (languages == null || languages.length() == 0) return true; + String s = languages.toString(); + return s.contains("*") || s.contains("en-US") || s.contains("\"en\"") || s.contains("english"); + } + + // ---- gen1 ----------------------------------------------------------------------------------- + + private static final class Gen1Outcome { Result result; String error; } + + private static Gen1Outcome runGen1(Context ctx, GogGame game, String token, String buildsJson, File installPath, Callback cb, AtomicBoolean cancelled) { + Gen1Outcome out = new Gen1Outcome(); + try { + JSONObject builds = new JSONObject(buildsJson); + JSONArray items = builds.optJSONArray("items"); + if (items == null || items.length() == 0) { out.error = builds.optInt("total_count", -1) == 0 ? "NO_CS_BUILDS" : "no items"; return out; } + String manifestUrl = null; + for (int i = 0; i < items.length(); i++) { + JSONObject item = items.getJSONObject(i); + if ("windows".equals(item.optString("os"))) { manifestUrl = item.optString("link"); break; } + } + if (manifestUrl == null || manifestUrl.isEmpty()) { out.error = "no windows manifest"; return out; } + cb.onProgress("Fetching manifest…", 12); + byte[] raw = fetchBytes(manifestUrl, token); + if (raw == null) { out.error = "manifest fetch failed"; return out; } + String manifestStr = decompressBytes(raw); + if (manifestStr == null) { out.error = "manifest decompress failed"; return out; } + JSONObject manifest = new JSONObject(manifestStr); + JSONArray depots = manifest.optJSONArray("depot"); + if (depots == null) { out.error = "no depot array"; return out; } + List<Gen1File> files = new ArrayList<>(); + for (int i = 0; i < depots.length(); i++) { + JSONObject depot = depots.getJSONObject(i); + if (depot.optBoolean("support", false)) continue; + JSONArray jFiles = depot.optJSONArray("files"); + if (jFiles == null) continue; + for (int j = 0; j < jFiles.length(); j++) { + JSONObject f = jFiles.getJSONObject(j); + String path = f.optString("path"), url = f.optString("url"); + long size = f.optLong("size", 0); + if (path == null || url == null || size == 0) continue; + files.add(new Gen1File(path, url, f.optLong("offset", 0), size)); + } + } + if (files.isEmpty()) { out.error = "no files in manifest"; return out; } + installPath.mkdirs(); + long planned = 0; + for (Gen1File gf : files) planned += gf.size; + final int total = files.size(); + final long plannedBytes = planned; + final AtomicInteger done = new AtomicInteger(0); + final AtomicLong totalBytes = new AtomicLong(0); + final AtomicLong lastSpeedMs = new AtomicLong(System.currentTimeMillis()); + final AtomicLong lastSpeedB = new AtomicLong(0); + final AtomicLong speedBps = new AtomicLong(0); + final AtomicBoolean anyFailed = new AtomicBoolean(false); + cb.onSizes(planned, planned); + cb.onBytes(0, planned, 0); + int threads = downloadThreads(); + if (GogNative.isAvailable()) { + runNativeEngine(ctx, GogNative.KIND_GEN1_RANGES, "Resuming…", Collections.singletonList(manifestStr), installPath, + new AtomicReference<>(""), null, null, new AtomicInteger(0), 0, threads, threads, false, "gog gen1=" + game.gameId, + cancelled, anyFailed, done, total, planned, totalBytes, lastSpeedMs, lastSpeedB, speedBps, cb); + } else { + ExecutorService pool = Executors.newFixedThreadPool(threads, com.droiddeck.launcher.stores.download.DownloadQueue.workerFactory("gog-dl")); + List<Future<Void>> futures = new ArrayList<>(); + for (Gen1File gf : files) { + futures.add(pool.submit((Callable<Void>) () -> { + if (cancelled.get() || anyFailed.get()) return null; + File outFile = new File(installPath, gf.path); + outFile.getParentFile().mkdirs(); + if (outFile.exists() && outFile.length() == gf.size) { + cb.onProgress("Resuming…", 15 + (int) ((done.incrementAndGet() / (float) total) * 80)); + cb.onBytes(totalBytes.addAndGet(gf.size), plannedBytes, speedBps.get()); + return null; + } + for (int attempt = 1; attempt <= 3; attempt++) { + if (cancelled.get()) return null; + outFile.delete(); + if (downloadRange(gf.url, gf.offset, gf.size, outFile)) { + int d = done.incrementAndGet(); + long tb = totalBytes.addAndGet(gf.size); + sampleSpeed(tb, lastSpeedMs, lastSpeedB, speedBps); + cb.onProgress("Downloading: " + baseName(gf.path) + speedSuffix(speedBps.get()), 15 + (int) ((d / (float) total) * 80)); + cb.onBytes(tb, plannedBytes, speedBps.get()); + return null; + } + if (attempt < 3) Thread.sleep(1000L << (attempt - 1)); + } + cb.onLog("FAIL file=" + gf.path); + anyFailed.set(true); + return null; + })); + } + pool.shutdown(); + try { for (Future<Void> f : futures) f.get(); } catch (Exception e) { pool.shutdownNow(); out.error = "gen1 pool error"; return out; } + } + if (cancelled.get()) return out; + if (anyFailed.get()) { out.error = "one or more files failed to download"; return out; } + cb.onProgress("Finishing…", 96); + writeFile(new File(installPath, MARKER), ("{\"gameId\":\"" + game.gameId + "\",\"generation\":1}").getBytes("UTF-8")); + out.result = new Result(pickExe(installPath, null, game.title), "", planned); + return out; + } catch (Exception e) { + out.error = "exception: " + e.getClass().getSimpleName(); + return out; + } + } + + // ---- installer fallback --------------------------------------------------------------------- + + /** Games with no content-system builds: the Windows installer, as one file, to run once in Proton. */ + private static Result runInstaller(Context ctx, GogGame game, String token, File installPath, Callback cb, AtomicBoolean cancelled) throws InstallException { + try { + String productJson = httpGet("https://api.gog.com/products/" + game.gameId + "?expand=downloads", token); + if (productJson == null) throw new InstallException("No downloadable builds for this game"); + JSONObject downloads = new JSONObject(productJson).optJSONObject("downloads"); + JSONArray installers = downloads == null ? null : downloads.optJSONArray("installers"); + if (installers == null || installers.length() == 0) throw new InstallException("No downloadable builds for this game"); + String manualUrl = null, fileName = null; + for (int i = 0; i < installers.length(); i++) { + JSONObject inst = installers.getJSONObject(i); + if (!"windows".equals(inst.optString("os"))) continue; + JSONArray files = inst.optJSONArray("files"); + if (files != null && files.length() > 0) { + JSONObject f = files.getJSONObject(0); + manualUrl = f.optString("downlink", null); + if (manualUrl == null) manualUrl = f.optString("manualUrl", null); + fileName = f.optString("filename", null); + } + if (manualUrl == null) manualUrl = inst.optString("manualUrl", null); + if (fileName == null || fileName.isEmpty()) fileName = "setup_" + game.gameId + ".exe"; + break; + } + if (manualUrl == null) throw new InstallException("No Windows installer for this game"); + String downloadUrl = resolveRedirect(manualUrl, token); + if (downloadUrl == null) throw new InstallException("The installer link could not be resolved"); + installPath.mkdirs(); + File outFile = new File(installPath, fileName); + cb.onProgress("Downloading installer: " + fileName, 15); + long size = downloadWithProgress(downloadUrl, outFile, cb, cancelled); + if (cancelled.get()) return null; + if (size <= 0) throw new InstallException("The installer download failed"); + cb.onLog("gog: installer downloaded; the game installs itself on first launch"); + return new Result(fileName, "", size); + } catch (InstallException e) { + throw e; + } catch (Exception e) { + throw new InstallException("Installer download error: " + e.getClass().getSimpleName()); + } + } + + private static String resolveRedirect(String url, String token) { + try { + if (url.startsWith("/")) url = "https://www.gog.com" + url; + for (int hop = 0; hop < 5; hop++) { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); conn.setInstanceFollowRedirects(false); + if (token != null) conn.setRequestProperty("Authorization", "Bearer " + token); + int code = conn.getResponseCode(); + String location = conn.getHeaderField("Location"); + if (code == 301 || code == 302 || code == 303 || code == 307 || code == 308) { + conn.disconnect(); + if (location == null) return null; + url = location.startsWith("/") ? "https://www.gog.com" + location : location; + continue; + } + if (code == 200) { + String ct = conn.getContentType(); + if (ct != null && ct.contains("application/json")) { + String body = readAll(conn.getInputStream()); + conn.disconnect(); + String inner = new JSONObject(body).optString("downlink", null); + if (inner != null && !inner.isEmpty()) { url = inner; continue; } + } else conn.disconnect(); + return url; + } + conn.disconnect(); + return null; + } + } catch (Exception ignored) {} + return null; + } + + private static long downloadWithProgress(String url, File out, Callback cb, AtomicBoolean cancelled) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(60_000); + long total = conn.getContentLengthLong(); + long downloaded = 0; + try (InputStream is = conn.getInputStream(); FileOutputStream fos = new FileOutputStream(out)) { + byte[] buf = new byte[131072]; + int n; + long windowStart = System.currentTimeMillis(), windowBytes = 0, speed = 0; + while ((n = is.read(buf)) != -1) { + if (cancelled.get()) return -1; + fos.write(buf, 0, n); + downloaded += n; windowBytes += n; + long elapsed = System.currentTimeMillis() - windowStart; + if (elapsed >= 500) { speed = windowBytes * 1000L / elapsed; windowStart = System.currentTimeMillis(); windowBytes = 0; } + if (total > 0) { + cb.onProgress("Downloading: " + out.getName() + speedSuffix(speed), 15 + (int) ((downloaded / (float) total) * 80)); + cb.onBytes(downloaded, total, speed); + } + } + } finally { + conn.disconnect(); + } + return downloaded; + } catch (Exception e) { + Log.w(TAG, "installer download failed: " + e.getClass().getSimpleName()); + return -1; + } + } + + // ---- native engine -------------------------------------------------------------------------- + + /** + * Drives the native engine for one fetch loop, producing what the Java pool produces: the same + * counters, progress strings and percentages. A secure-link expiry (the run dying on + * 401/403/404/500) refreshes the link and re-runs for the files not yet done. + */ + private static void runNativeEngine(Context ctx, int kind, String verifiedMsg, List<String> depotJsons, File installPath, + AtomicReference<String> cdnBaseRef, String secureLinkUrl, String token, + AtomicInteger cdnRefreshCount, int maxCdnRefresh, int maxWorkers, int processWorkers, + boolean sortLargestFirst, String label, AtomicBoolean cancelled, AtomicBoolean anyFailed, + AtomicInteger doneCount, int total, long planned, AtomicLong totalBytes, + AtomicLong lastSpeedMs, AtomicLong lastSpeedB, AtomicLong speedBps, Callback cb) { + StoreDownloadTier tier = StoreDownloadTier.Companion.current(ctx); + int workers = Math.max(1, Math.min(128, tier.getNetworkWindow())); + int process = Math.max(16, Math.max(processWorkers, tier.getProcessWorkers())); + // The tier's window goes to the engine as its ceiling (Max = 96); the per-host spread is the engine's. + cb.onLog("gog: plan kind=" + kind + " files=" + total + " bytes=" + planned + " window=" + workers + " process_workers=" + process + " tier=" + tier.getId() + " engine=native"); + final String[] manifests = depotJsons.toArray(new String[0]); + final java.util.Set<String> donePaths = java.util.concurrent.ConcurrentHashMap.newKeySet(); + int run = 0; + while (!cancelled.get()) { + run++; + final String base = cdnBaseRef.get(); + final CountDownLatch latch = new CountDownLatch(1); + final AtomicBoolean okRef = new AtomicBoolean(false); + final AtomicBoolean linkExpiryRef = new AtomicBoolean(false); + final AtomicReference<String> errRef = new AtomicReference<>(""); + // The bar between file completions: what earlier runs counted, plus the files this run + // skipped as already verified (the engine credits those no bytes), plus the engine's own + // byte count, which includes the files still in flight. Never shown going back. + final long runStart = totalBytes.get(); + final AtomicLong verifiedInRun = new AtomicLong(0); + final AtomicLong shownBytes = new AtomicLong(runStart); + GogNative.Listener listener = new GogNative.Listener() { + @Override public void onProgress(long bytesDone, long bytesTotal, int filesDone, int filesTotal, String file, long fileBytes, boolean verified) { + donePaths.add(file); + int done = doneCount.incrementAndGet(); + int pct = 15 + (int) ((done / (float) total) * 80); + long tb = totalBytes.addAndGet(fileBytes); + if (verified) verifiedInRun.addAndGet(fileBytes); + long shown = shownBytes.accumulateAndGet(tb, Math::max); + if (verified) { cb.onProgress(verifiedMsg, pct); cb.onBytes(shown, planned, speedBps.get()); return; } + sampleSpeed(shown, lastSpeedMs, lastSpeedB, speedBps); + cb.onProgress("Downloading: " + baseName(file) + speedSuffix(speedBps.get()), pct); + cb.onBytes(shown, planned, speedBps.get()); + } + @Override public void onBytes(long bytesDone, long bytesTotal) { + long shown = shownBytes.accumulateAndGet(Math.min(planned, runStart + verifiedInRun.get() + bytesDone), Math::max); + sampleSpeed(shown, lastSpeedMs, lastSpeedB, speedBps); + cb.onBytes(shown, planned, speedBps.get()); + } + @Override public void onLog(String line) { cb.onLog(line); } + @Override public void onComplete(boolean success, boolean wasCancelled, boolean linkExpiry, String error, long bytesWritten, int filesDone) { + okRef.set(success); linkExpiryRef.set(linkExpiry); errRef.set(error == null ? "" : error); latch.countDown(); + } + }; + // Every CDN of the set: the engine spreads its window across the hosts (JNI.md). + String[] bases = (base == null || base.isEmpty()) ? new String[0] : base.split("\n"); + if (run == 1) cb.onLog("gog: " + bases.length + " CDN host(s) offered"); + long handle = GogNative.start(kind, manifests, bases, installPath.getAbsolutePath(), donePaths.toArray(new String[0]), "", + workers, process, sortLargestFirst, label + " run=" + run, listener); + if (handle == 0L) { cb.onLog("gog: native start failed"); anyFailed.set(true); return; } + boolean interrupted = false; + try { + while (true) { + try { if (latch.await(250, TimeUnit.MILLISECONDS)) break; } catch (InterruptedException ie) { interrupted = true; } + if (cancelled.get() || interrupted) GogNative.cancel(handle); + } + } finally { + GogNative.release(handle); + } + if (interrupted) { Thread.currentThread().interrupt(); anyFailed.set(true); return; } + if (cancelled.get() || okRef.get()) return; + String err = errRef.get(); + if (linkExpiryRef.get() && tryRefreshCdn(cdnBaseRef, base, secureLinkUrl, token, cdnRefreshCount, maxCdnRefresh, cb)) { + cb.onLog("gog: secure link expired (" + err + "); refreshed, running again for the remaining files"); + continue; + } + cb.onLog("gog: engine failed: " + err); + anyFailed.set(true); + return; + } + } + + // ---- gen2 helpers (shared with the Java pool) ----------------------------------------------- + + private static void parseDepotManifest(String json, List<DepotFile> out) { + try { + JSONObject depotObj = new JSONObject(json).optJSONObject("depot"); + if (depotObj == null) return; + JSONArray depot = depotObj.optJSONArray("items"); + if (depot == null) return; + for (int i = 0; i < depot.length(); i++) { + JSONObject entry = depot.getJSONObject(i); + String path = entry.optString("path", "").replace("\\", "/"); + if (path.startsWith("/")) path = path.substring(1); + JSONArray chunks = entry.optJSONArray("chunks"); + if (path.isEmpty() || chunks == null || chunks.length() == 0) continue; + DepotFile df = new DepotFile(path); + String fileMd5 = entry.optString("md5", null); + if (fileMd5 != null && !fileMd5.isEmpty()) df.md5 = fileMd5; + long fileTotal = 0; + for (int c = 0; c < chunks.length(); c++) { + JSONObject chunk = chunks.getJSONObject(c); + String compressedMd5 = chunk.optString("compressedMd5"), decMd5 = chunk.optString("md5"); + String hash = (compressedMd5 != null && !compressedMd5.isEmpty()) ? compressedMd5 : decMd5; + if (hash == null || hash.isEmpty()) continue; + long decSize = chunk.optLong("size", 0); + df.chunks.add(new DepotFile.ChunkRef(hash, compressedMd5, decMd5, chunk.optLong("compressedSize", 0), decSize)); + fileTotal += decSize; + } + df.totalSize = fileTotal; + if (!df.chunks.isEmpty()) out.add(df); + } + } catch (Exception e) { + Log.w(TAG, "depot manifest parse: " + e.getClass().getSimpleName()); + } + } + + static String buildCdnPath(String hash) { return hash.substring(0, 2) + "/" + hash.substring(2, 4) + "/" + hash; } + + private static boolean assembleDepotFile(DepotFile df, File outFile, AtomicReference<String> cdnBaseRef, String secureLinkUrl, String token, + AtomicInteger cdnRefreshCount, int maxCdnRefresh, AtomicBoolean cancelled, Callback cb) { + File tmpFile = new File(outFile.getAbsolutePath() + ".bhtmp"); + File parent = outFile.getParentFile(); + if (parent != null) parent.mkdirs(); + tmpFile.delete(); + boolean ok = true; + try (FileOutputStream fos = new FileOutputStream(tmpFile)) { + for (DepotFile.ChunkRef chunk : df.chunks) { + if (cancelled.get()) { ok = false; break; } + byte[] inflated = fetchChunkVerified(cdnBaseRef, buildCdnPath(chunk.hash), chunk, secureLinkUrl, token, cdnRefreshCount, maxCdnRefresh, cancelled, cb); + if (inflated == null) { ok = false; break; } + fos.write(inflated); + } + } catch (Exception e) { + ok = false; + } + if (!ok || cancelled.get()) { tmpFile.delete(); return false; } + if (df.totalSize > 0 && tmpFile.length() != df.totalSize) { cb.onLog("size mismatch file=" + df.relativePath); tmpFile.delete(); return false; } + if (df.md5 != null && !df.md5.isEmpty()) { + String actual = md5HexFile(tmpFile); + if (actual == null || !actual.equalsIgnoreCase(df.md5)) { cb.onLog("md5 mismatch file=" + df.relativePath); tmpFile.delete(); return false; } + } + if (outFile.exists()) outFile.delete(); + return tmpFile.renameTo(outFile); + } + + /** + * Every CDN the secure link offers, as one value - their bases, one per line - so the set is + * refreshed (and compared on expiry) as a whole. GOG answers with several hosts in {@code urls[]}; + * taking only the first left every request on one host. + */ + private static String parseCdnUrl(String json) { + java.util.List<String> all = parseCdnUrls(json); + return all.isEmpty() ? null : String.join("\n", all); + } + + /** The CDN bases of a secure_link answer, in the order GOG gives them. */ + static java.util.List<String> parseCdnUrls(String json) { + java.util.List<String> out = new ArrayList<>(); + if (json == null) return out; + try { + JSONArray urls = new JSONObject(json).optJSONArray("urls"); + if (urls == null) return out; + for (int i = 0; i < urls.length(); i++) { + JSONObject u = urls.optJSONObject(i); + if (u == null) continue; + String urlFormat = u.optString("url_format"); + JSONObject params = u.optJSONObject("parameters"); + if (urlFormat == null || urlFormat.isEmpty() || params == null) continue; + java.util.Iterator<String> keys = params.keys(); + while (keys.hasNext()) { String k = keys.next(); urlFormat = urlFormat.replace("{" + k + "}", params.optString(k)); } + urlFormat = urlFormat.replace("\\/", "/"); + int idx = urlFormat.indexOf("/{path}"); + if (idx >= 0) urlFormat = urlFormat.substring(0, idx); + if (!out.contains(urlFormat)) out.add(urlFormat); + } + } catch (Exception ignored) { + } + return out; + } + + private static final AtomicInteger CDN_TURN = new AtomicInteger(); + + /** One of the set's CDNs, in turn, so the Java pool spreads its requests over all of them. */ + static String pickCdn(String set) { + String[] all = set.split("\n"); + return all[Math.floorMod(CDN_TURN.getAndIncrement(), all.length)]; + } + + /** The first CDN of the set - what a caller that takes a single base gets. */ + static String firstCdn(String set) { + int nl = set.indexOf('\n'); + return nl < 0 ? set : set.substring(0, nl); + } + + private static boolean downloadRange(String url, long offset, long size, File out) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); + conn.setRequestProperty("Range", "bytes=" + offset + "-" + (offset + size - 1)); + try (InputStream is = conn.getInputStream(); FileOutputStream fos = new FileOutputStream(out)) { + byte[] buf = new byte[131072]; + int n; + while ((n = is.read(buf)) != -1) fos.write(buf, 0, n); + } + conn.disconnect(); + return true; + } catch (Exception e) { + Log.w(TAG, "range download failed: " + out.getName() + " (" + e.getClass().getSimpleName() + ")"); + return false; + } + } + + static String httpGet(String url, String token) { + try { + HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); + conn.setRequestProperty("User-Agent", "GOG Galaxy"); + if (token != null) conn.setRequestProperty("Authorization", "Bearer " + token); + if (conn.getResponseCode() != 200) { conn.disconnect(); return null; } + String body = readAll(conn.getInputStream()); + conn.disconnect(); + return body; + } catch (Exception e) { + return null; + } + } + + private static String readAll(InputStream is) throws IOException { + StringBuilder sb = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"))) { + String line; + while ((line = br.readLine()) != null) sb.append(line); + } + return sb.toString(); + } + + static byte[] fetchBytes(String url, String token) { return fetchBytesEx(url, token).body; } + + /** gzip or zlib detected by magic, else the bytes as UTF-8. */ + static String decompressBytes(byte[] data) { + if (data == null || data.length < 2) return null; + try { + int b0 = data[0] & 0xFF, b1 = data[1] & 0xFF; + if (b0 == 0x1F && b1 == 0x8B) { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try (GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(data))) { + byte[] buf = new byte[4096]; + int n; + while ((n = gzip.read(buf)) != -1) bos.write(buf, 0, n); + } + return bos.toString("UTF-8"); + } + if (b0 == 0x78) { + byte[] out = inflateZlib(data); + return out == null ? null : new String(out, "UTF-8"); + } + return new String(data, "UTF-8"); + } catch (Exception e) { + return null; + } + } + + private static byte[] inflateZlib(byte[] data) { + try { + if (data == null || data.length < 2 || (data[0] & 0xFF) != 0x78) return null; + Inflater inf = new Inflater(); + inf.setInput(data); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buf = new byte[8192]; + while (!inf.finished()) { + int n = inf.inflate(buf); + if (n == 0) { if (inf.needsInput() || inf.needsDictionary()) break; } + bos.write(buf, 0, n); + } + inf.end(); + return bos.toByteArray(); + } catch (Exception e) { + return null; + } + } + + private static final class HttpResult { + final byte[] body; final int status; + HttpResult(byte[] body, int status) { this.body = body; this.status = status; } + } + + private static HttpResult fetchBytesEx(String url, String token) { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) new URL(url).openConnection(); + conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); + conn.setRequestProperty("User-Agent", "GOG Galaxy"); + if (token != null) conn.setRequestProperty("Authorization", "Bearer " + token); + int code = conn.getResponseCode(); + if (code != 200) { conn.disconnect(); return new HttpResult(null, code); } + int len = conn.getContentLength(); + ByteArrayOutputStream bos = len > 0 ? new ByteArrayOutputStream(len) : new ByteArrayOutputStream(); + byte[] buf = new byte[131072]; + try (InputStream is = conn.getInputStream()) { + int n; + while ((n = is.read(buf)) != -1) bos.write(buf, 0, n); + } + conn.disconnect(); + return new HttpResult(bos.toByteArray(), code); + } catch (Exception e) { + if (conn != null) try { conn.disconnect(); } catch (Exception ignored) {} + return new HttpResult(null, -1); + } + } + + /** The chunk path goes before the query string so the secure link's token survives. */ + private static String buildChunkUrl(String base, String chunkPath) { + int q = base.indexOf('?'); + return q >= 0 ? base.substring(0, q) + "/" + chunkPath + base.substring(q) : base + "/" + chunkPath; + } + + private static String md5Hex(byte[] data) { + try { return toHex(java.security.MessageDigest.getInstance("MD5").digest(data)); } catch (Exception e) { return null; } + } + + private static String md5HexFile(File f) { + try (FileInputStream fis = new FileInputStream(f)) { + java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5"); + byte[] buf = new byte[131072]; + int n; + while ((n = fis.read(buf)) != -1) md.update(buf, 0, n); + return toHex(md.digest()); + } catch (Exception e) { + return null; + } + } + + private static String toHex(byte[] d) { + StringBuilder sb = new StringBuilder(d.length * 2); + for (byte b : d) { int v = b & 0xFF; if (v < 16) sb.append('0'); sb.append(Integer.toHexString(v)); } + return sb.toString(); + } + + private static boolean fileVerified(File f, long expectedSize, String expectedMd5) { + if (f == null || !f.exists() || f.length() == 0) return false; + if (expectedSize > 0 && f.length() != expectedSize) return false; + if (expectedMd5 != null && !expectedMd5.isEmpty()) { + String actual = md5HexFile(f); + return actual != null && actual.equalsIgnoreCase(expectedMd5); + } + return true; + } + + private static void sleepBackoff(int attempt) { + try { Thread.sleep(Math.min(1000L << Math.max(0, attempt - 1), 8000L)); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } + } + + private static boolean tryRefreshCdn(AtomicReference<String> cdnBaseRef, String staleBase, String secureLinkUrl, String token, + AtomicInteger refreshCount, int cap, Callback cb) { + if (secureLinkUrl == null) return false; + synchronized (cdnBaseRef) { + String current = cdnBaseRef.get(); + if (staleBase == null || !staleBase.equals(current)) return true; + if (refreshCount.get() >= cap) { cb.onLog("gog: CDN refresh cap reached (" + cap + ")"); return false; } + String neu = parseCdnUrl(httpGet(secureLinkUrl, token)); + if (neu == null || neu.isEmpty()) { cb.onLog("gog: CDN refresh failed"); return false; } + cdnBaseRef.set(neu); + cb.onLog("gog: secure link refreshed #" + refreshCount.incrementAndGet()); + return true; + } + } + + private static byte[] fetchChunkVerified(AtomicReference<String> cdnBaseRef, String chunkPath, DepotFile.ChunkRef chunk, String secureLinkUrl, String token, + AtomicInteger cdnRefreshCount, int maxCdnRefresh, AtomicBoolean cancelled, Callback cb) { + int hardFail = 0, guard = 0; + while (guard++ < 8) { + if (cancelled.get()) return null; + String base = cdnBaseRef.get(); + HttpResult res = fetchBytesEx(buildChunkUrl(pickCdn(base), chunkPath), null); + if (res.body == null) { + int code = res.status; + if ((code == 401 || code == 403 || code == 404 || code == 500) && tryRefreshCdn(cdnBaseRef, base, secureLinkUrl, token, cdnRefreshCount, maxCdnRefresh, cb)) continue; + cb.onLog("chunk http fail code=" + code); + if (++hardFail >= 3) return null; + sleepBackoff(hardFail); + continue; + } + byte[] raw = res.body; + if (chunk.compressedSize > 0 && raw.length != chunk.compressedSize) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } + if (chunk.compressedMd5 != null && !chunk.compressedMd5.isEmpty()) { + String actual = md5Hex(raw); + if (actual == null || !actual.equalsIgnoreCase(chunk.compressedMd5)) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } + } + byte[] inflated = inflateZlib(raw); + if (inflated == null) inflated = raw; + if (chunk.size > 0 && inflated.length != chunk.size) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } + if (chunk.md5 != null && !chunk.md5.isEmpty()) { + String actual = md5Hex(inflated); + if (actual == null || !actual.equalsIgnoreCase(chunk.md5)) { if (++hardFail >= 3) return null; sleepBackoff(hardFail); continue; } + } + return inflated; + } + return null; + } + + private static void sampleSpeed(long totalBytes, AtomicLong lastSpeedMs, AtomicLong lastSpeedB, AtomicLong speedBps) { + long nowMs = System.currentTimeMillis(), prevMs = lastSpeedMs.get(); + if (nowMs - prevMs >= 500 && lastSpeedMs.compareAndSet(prevMs, nowMs)) { + long prevB = lastSpeedB.getAndSet(totalBytes); + long dt = nowMs - prevMs; + if (dt > 0) speedBps.set((totalBytes - prevB) * 1000L / dt); + } + } + + private static String speedSuffix(long bps) { + if (bps <= 0) return ""; + return " " + (bps >= 1048576 ? String.format("%.1f MB/s", bps / 1048576.0) : (bps / 1024) + " KB/s"); + } + + private static String baseName(String path) { return path.contains("/") ? path.substring(path.lastIndexOf('/') + 1) : path; } + + /** Download-pool threads for the built-in loop: cores times two, between 6 and 16. */ + private static int downloadThreads() { + int threads = Runtime.getRuntime().availableProcessors() * 2; + return Math.max(6, Math.min(16, threads)); + } + + private static void writeFile(File f, byte[] data) throws IOException { + try (FileOutputStream fos = new FileOutputStream(f)) { fos.write(data); } + } + + /** Removes the chunk cache, counting its files as the Install stage's progress (a large cache takes a while). */ + private static void deleteCounted(File dir, Callback cb) { + File[] files = dir.listFiles(); + if (files == null) { dir.delete(); return; } + int total = files.length, done = 0; + for (File f : files) { + deleteDir(f); + done++; + if ((done & 31) == 0 || done == total) cb.onStage("cleanup", done, total, done, total); + } + dir.delete(); + } + + static void deleteDir(File dir) { + if (dir == null || !dir.exists()) return; + File[] children = dir.listFiles(); + if (children != null) for (File c : children) deleteDir(c); + dir.delete(); + } + + /** + * The exe to run, relative to the install folder: the manifest's hint when present, else the + * candidate named most like the title, else the shallowest. "" when the folder has none. + */ + static String pickExe(File installPath, String tempExe, String title) { + if (tempExe != null) { + File hinted = new File(installPath, tempExe); + if (hinted.isFile()) return relative(installPath, hinted); + } + List<File> candidates = new ArrayList<>(); + collectExeRecursive(installPath, candidates); + if (candidates.isEmpty()) return ""; + String key = title.toLowerCase().replaceAll("[^a-z0-9]", ""); + File best = candidates.get(0); + for (File f : candidates) { + String n = f.getName().toLowerCase().replaceAll("\\.exe$", "").replaceAll("[^a-z0-9]", ""); + if (!n.isEmpty() && (n.equals(key) || key.startsWith(n) || n.startsWith(key))) { best = f; break; } + } + return relative(installPath, best); + } + + private static String relative(File root, File f) { + String rel = f.getAbsolutePath().substring(root.getAbsolutePath().length()); + if (rel.startsWith("/")) rel = rel.substring(1); + return rel.replace('\\', '/'); + } + + private static void collectExeRecursive(File dir, List<File> out) { + if (!dir.isDirectory()) return; + File[] files = dir.listFiles(); + if (files == null) return; + for (File f : files) { + if (f.isFile() && f.getName().toLowerCase().endsWith(".exe")) { + String path = f.getAbsolutePath().toLowerCase(); + if (!path.contains("redist") && !path.contains("unins") && !path.contains("setup") && !path.contains("crash") + && !path.contains("report") && !path.contains("helper") && !path.contains("dotnet") && !path.contains("vcredist") + && !path.contains("directx")) out.add(f); + } + } + for (File f : files) if (f.isDirectory()) collectExeRecursive(f, out); + } + + private static final class DepotFile { + final String relativePath; + final List<ChunkRef> chunks = new ArrayList<>(); + String md5; + long totalSize; + DepotFile(String relativePath) { this.relativePath = relativePath; } + static final class ChunkRef { + final String hash, compressedMd5, md5; + final long compressedSize, size; + ChunkRef(String hash, String compressedMd5, String md5, long compressedSize, long size) { + this.hash = hash; this.compressedMd5 = compressedMd5; this.md5 = md5; this.compressedSize = compressedSize; this.size = size; + } + } + } + + private static final class Gen1File { + final String path, url; final long offset, size; + Gen1File(String path, String url, long offset, long size) { this.path = path; this.url = url; this.offset = offset; this.size = size; } + } + + /** The install size of the Windows build (English / all-language depots), or -1. Blocking. */ + public static long fetchInstallSizeBytes(String gameId, String token) { + try { + String buildsUrl = "https://content-system.gog.com/products/" + gameId + "/os/windows/builds?generation=2"; + String buildsJson = httpGet(buildsUrl, null); + if (buildsJson == null) buildsJson = httpGet(buildsUrl, token); + if (buildsJson == null) return -1; + JSONArray items = new JSONObject(buildsJson).optJSONArray("items"); + if (items == null || items.length() == 0) return -1; + String manifestUrl = null; + for (int i = 0; i < items.length(); i++) { + JSONObject item = items.getJSONObject(i); + if ("windows".equals(item.optString("os"))) { + manifestUrl = item.optString("link"); + if (manifestUrl == null || manifestUrl.isEmpty()) manifestUrl = item.optString("meta_url"); + break; + } + } + if (manifestUrl == null || manifestUrl.isEmpty()) return -1; + byte[] raw = fetchBytes(manifestUrl, token); + if (raw == null) return -1; + String manifestStr = decompressBytes(raw); + if (manifestStr == null) return -1; + JSONArray depots = new JSONObject(manifestStr).optJSONArray("depots"); + if (depots == null) return -1; + long total = 0; + for (int i = 0; i < depots.length(); i++) { + JSONObject depot = depots.getJSONObject(i); + if (languageCompatible(depot)) total += depot.optLong("size", 0); + } + return total > 0 ? total : -1; + } catch (Exception e) { + Log.w(TAG, "install size " + gameId + ": " + e.getClass().getSimpleName()); + return -1; + } + } + + public static String formatBytes(long bytes) { + if (bytes <= 0) return "Unknown"; + if (bytes < 1024L * 1024) return String.format("%.1f KB", bytes / 1024.0); + if (bytes < 1024L * 1024 * 1024) return String.format("%.1f MB", bytes / (1024.0 * 1024)); + return String.format("%.2f GB", bytes / (1024.0 * 1024 * 1024)); + } + + /** Keeps the import used: every URL a log line names goes through here. */ + static String safe(String url) { return StoreLog.redactUrl(url); } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogGame.java b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogGame.java new file mode 100644 index 0000000000..63dbab9c74 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogGame.java @@ -0,0 +1,27 @@ +package com.droiddeck.launcher.stores.gog; + +/** One GOG game as the library cache holds it. */ +public class GogGame { + public final String gameId; + public final String title; + public final String imageUrl; + public final String description; + public final String developer; + public final String category; + /** Content-system generation of the Windows build: 1 or 2 (0 = unknown). */ + public final int generation; + /** gamesdb.gog.com 2:3 box art, or null when the lookup found none. */ + public final String verticalCover; + + public GogGame(String gameId, String title, String imageUrl, String description, String developer, + String category, int generation, String verticalCover) { + this.gameId = gameId; + this.title = title; + this.imageUrl = imageUrl; + this.description = description; + this.developer = developer; + this.category = category; + this.generation = generation; + this.verticalCover = verticalCover; + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt new file mode 100644 index 0000000000..7012efaab8 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt @@ -0,0 +1,211 @@ +package com.droiddeck.launcher.stores.gog + +import android.content.Context +import android.content.SharedPreferences +import android.util.Log +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreNet +import com.droiddeck.launcher.stores.cleanStoreText +import org.json.JSONArray +import org.json.JSONObject +import java.util.concurrent.Callable +import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicBoolean + +/** + * The signed-in account's GOG library: the owned ids from embed.gog.com, each product's details + * from api.gog.com, and a cache so the tab paints at once. A sync fetches only what the cache + * lacks unless forced or 15 minutes stale. Ported from Bannerlator's GogLibraryRepo. + */ +object GogLibrary { + private const val TAG = "GogLibrary" + // v2: the card art moved from the store page's backdrop to the Galaxy library art; an older cache is re-fetched. + private const val CACHE_KEY = "library_cache_v2" + private const val LAST_SYNC_KEY = "library_synced_at" + private const val THROTTLE_MS = 6L * 60L * 60L * 1000L + + private val syncing = AtomicBoolean(false) + + sealed interface SyncResult { + class Ok(val games: List<GogGame>, val fetched: Int) : SyncResult + class Failed(val message: String) : SyncResult + object NotLoggedIn : SyncResult + object Busy : SyncResult + } + + /** The cached library, alphabetical; empty until the first sync. */ + fun cached(context: Context): List<GogGame> { + val json = GogPrefs.get(context).getString(CACHE_KEY, null) ?: return emptyList() + return runCatching { + val arr = JSONArray(json) + (0 until arr.length()).mapNotNull { i -> + val o = arr.optJSONObject(i) ?: return@mapNotNull null + GogGame(o.optString("gameId", ""), o.optString("title", ""), o.optString("imageUrl", ""), cleanStoreText(o.optString("description", "")), + o.optString("developer", ""), o.optString("category", ""), o.optInt("generation", 1), o.optString("verticalCover", "").ifEmpty { null }) + }.filter { it.gameId.isNotEmpty() }.sortedBy { it.title.lowercase() } + }.getOrDefault(emptyList()) + } + + fun sizeOf(context: Context, gameId: String): Long = GogPrefs.get(context).getLong("size_$gameId", 0L) + + /** Owned ids diffed against the cache; fetches what is new, or everything when forced / empty / stale. Blocking. */ + fun sync(context: Context, force: Boolean = false, onStatus: (String) -> Unit = {}): SyncResult { + if (!syncing.compareAndSet(false, true)) return SyncResult.Busy + try { + val app = context.applicationContext + val p = GogPrefs.get(app) + val token = GogAuth.validToken(app) ?: return SyncResult.NotLoggedIn + onStatus("Fetching game list…") + val gamesJson = StoreNet.get("https://embed.gog.com/user/data/games", bearer = token, userAgent = GogAuth.GALAXY_UA) + ?: return SyncResult.Failed("Couldn't reach GOG") + val ids = ArrayList<String>() + val parsed = runCatching { + val owned = JSONObject(gamesJson).optJSONArray("owned") + if (owned != null) for (i in 0 until owned.length()) { + val id = owned.optLong(i).toString() + // 1801418160 is GOG's own "Galaxy" entry, in every library. + if (id != "1801418160" && id != "0") ids.add(id) + } + }.isSuccess + if (!parsed) return SyncResult.Failed("Error parsing library") + + val ownedSet = ids.toHashSet() + val cachedList = cached(app) + val stale = System.currentTimeMillis() - p.getLong(LAST_SYNC_KEY, 0L) >= THROTTLE_MS + val heavy = force || cachedList.isEmpty() || stale + val cachedIds = cachedList.mapTo(HashSet()) { it.gameId } + val idsToFetch = if (heavy) ids else ids.filter { it !in cachedIds } + val fetchSet = idsToFetch.toHashSet() + val merged = LinkedHashMap<String, GogGame>() + for (g in cachedList) if (g.gameId in ownedSet && g.gameId !in fetchSet) merged[g.gameId] = g + if (idsToFetch.isEmpty()) { + val list = merged.values.toList() + saveCache(p, list) + return SyncResult.Ok(list.sortedBy { it.title.lowercase() }, 0) + } + onStatus("Syncing ${idsToFetch.size} game${if (idsToFetch.size == 1) "" else "s"}…") + val pool = Executors.newFixedThreadPool(5) + val futures = idsToFetch.map { id -> pool.submit(Callable<GogGame?> { fetchGame(p, id, token) }) } + pool.shutdown() + var fetched = 0 + for ((idx, f) in futures.withIndex()) { + val g = runCatching { f.get() }.getOrNull() + if (g != null) { merged[g.gameId] = g; fetched++ } + if ((idx + 1) % 5 == 0) onStatus("Syncing… ${idx + 1}/${futures.size}") + } + val finalList = merged.values.toList() + saveCache(p, finalList) + if (heavy) p.edit().putLong(LAST_SYNC_KEY, System.currentTimeMillis()).apply() + Log.i(TAG, "sync: owned=${ids.size} fetched=$fetched cached=${finalList.size} heavy=$heavy") + return SyncResult.Ok(finalList.sortedBy { it.title.lowercase() }, fetched) + } catch (e: Exception) { + Log.w(TAG, "sync failed: ${e.message}") + return SyncResult.Failed(e.message ?: "Sync failed") + } finally { + syncing.set(false) + } + } + + /** One product's details, or null for anything that is not a Windows game (DLC, movies, other platforms). */ + private fun fetchGame(prefs: SharedPreferences, id: String, token: String): GogGame? { + try { + val productJson = StoreNet.get("https://api.gog.com/products/$id?expand=downloads,description", bearer = token) ?: return null + val prod = JSONObject(productJson) + if (prod.optBoolean("is_secret", false)) return null + val gameType = prod.optString("game_type", "") + if (gameType.isNotEmpty() && gameType != "game" && gameType != "pack") return null + var title = prod.optJSONObject("title")?.optString("*") + if (title.isNullOrEmpty()) title = prod.optString("title") + if (title.isNullOrEmpty()) return null + // Card art from the v2 game record, the way the Galaxy client draws its library: the dark + // Galaxy background for the wide card and the box art for the tall one. The product's + // `images.background` is the store page's fade-out backdrop (it ends in white on a + // card) and is never used; the logo or icon is the last resort. + val images = prod.optJSONObject("images") + var imageUrl = "" + var boxArt = "" + val v2 = runCatching { StoreNet.get("https://api.gog.com/v2/games/$id?locale=en-US")?.let { JSONObject(it) } }.getOrNull() + v2?.optJSONObject("_links")?.let { links -> + imageUrl = links.optJSONObject("galaxyBackgroundImage")?.optString("href", "").orEmpty() + boxArt = links.optJSONObject("boxArtImage")?.optString("href", "").orEmpty() + } + if (imageUrl.isEmpty()) imageUrl = images?.optString("logo2x", "") ?: "" + if (imageUrl.isEmpty()) imageUrl = images?.optString("icon", "") ?: "" + // The account endpoint hands back template keys for some products; the v2 record's text + // is tried next, and when that is a template too the game simply has no description. + val desc = cleanStoreText(prod.optJSONObject("description")?.optString("lead", "")) + .ifEmpty { cleanStoreText(v2?.optString("overview", "")) } + .ifEmpty { cleanStoreText(v2?.optString("description", "")) } + val developer = prod.optJSONObject("developers")?.optString("name", "") ?: prod.optString("developer", "") + val genres = prod.optJSONArray("genres") + val category = if (genres != null && genres.length() > 0) genres.optJSONObject(0)?.optString("name", "") ?: "" else "" + + var generation = 1 + var hasWindowsBuild = false + runCatching { + val buildsJson = StoreNet.get("https://content-system.gog.com/products/$id/os/windows/builds?generation=2", bearer = token, userAgent = GogAuth.GALAXY_UA) + val items = buildsJson?.let { JSONObject(it).optJSONArray("items") } + if (items != null && items.length() > 0) { + hasWindowsBuild = true + var maxGen = 0 + for (bi in 0 until items.length()) maxGen = maxOf(maxGen, items.optJSONObject(bi)?.optInt("generation", 0) ?: 0) + if (maxGen > 0) generation = maxGen + } + } + val ed = prefs.edit().putInt("gen_$id", generation) + prod.optString("release_date", "").takeIf { it.isNotEmpty() }?.let { ed.putString("release_$id", it) } + ed.apply() + if (prefs.getLong("size_$id", -1L) <= 0) { + val size = GogDownloadManager.fetchInstallSizeBytes(id, token) + if (size > 0) prefs.edit().putLong("size_$id", size).apply() + } + var hasWindowsInstaller = false + prod.optJSONObject("downloads")?.optJSONArray("installers")?.let { arr -> + for (di in 0 until arr.length()) if (arr.optJSONObject(di)?.optString("os", "") == "windows") { hasWindowsInstaller = true; break } + } + if (!hasWindowsBuild && !hasWindowsInstaller) return null + + var verticalCover: String? = boxArt.ifEmpty { prefs.getString("vcover_$id", null) } + if (verticalCover.isNullOrEmpty()) { + verticalCover = fetchVerticalCover(id).ifEmpty { StoreNet.sgdbPoster(title) } + } + if (!verticalCover.isNullOrEmpty()) prefs.edit().putString("vcover_$id", verticalCover).apply() + return GogGame(id, title, GogStoreCatalog.absolutize(imageUrl), desc, developer, category, generation, verticalCover?.ifEmpty { null }) + } catch (_: Exception) { + return null + } + } + + private fun fetchVerticalCover(productId: String): String = try { + val extJson = StoreNet.get("https://gamesdb.gog.com/platforms/gog/external_releases/$productId") + val gameId = extJson?.let { JSONObject(it).optString("game_id", "") }.orEmpty() + if (gameId.isEmpty()) "" else { + val gameJson = StoreNet.get("https://gamesdb.gog.com/games/$gameId") + val fmt = gameJson?.let { JSONObject(it).optJSONObject("vertical_cover") }?.optString("url_format", "").orEmpty() + if (fmt.isEmpty()) "" else fmt.replace("{formatter}", "").replace("{ext}", "webp") + } + } catch (_: Exception) { "" } + + private fun saveCache(prefs: SharedPreferences, games: List<GogGame>) { + runCatching { + val arr = JSONArray() + for (g in games) { + arr.put(JSONObject().put("gameId", g.gameId).put("title", g.title).put("imageUrl", g.imageUrl).put("description", g.description) + .put("developer", g.developer).put("category", g.category).put("generation", g.generation) + .apply { if (!g.verticalCover.isNullOrEmpty()) put("verticalCover", g.verticalCover) }) + } + prefs.edit().putString(CACHE_KEY, arr.toString()).apply() + } + } + + /** A cached game as the page's card. */ + fun toCatalogItem(context: Context, g: GogGame): CatalogItem = CatalogItem( + store = Store.GOG, id = g.gameId, title = g.title, + imageUrl = GogStoreCatalog.absolutize(g.imageUrl).ifBlank { null }, + tallImageUrl = GogStoreCatalog.absolutize(g.verticalCover ?: g.imageUrl).ifBlank { null }, + tags = g.category, developer = g.developer, description = g.description, owned = true, + sizeBytes = sizeOf(context, g.gameId), + storeUrl = "https://www.gog.com/en/game/${g.gameId}", + ) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt new file mode 100644 index 0000000000..0922eda9af --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogNative.kt @@ -0,0 +1,109 @@ +// JNI symbols depend on this package path and class name (app/src/main/rust/stores/JNI.md). +package com.droiddeck.launcher.stores.gog + +import android.util.Log +import com.droiddeck.launcher.stores.StoresNative + +/** + * The native GOG download engine in `libdroiddeckstores.so`: the gen2 chunk loop (depot manifests + * → chunk fetch + inflate + MD5 → assembled files) and the gen1 range loop. One [start] is one + * download loop; the manager owns everything before and after it. Method names and signatures + * are Bannerlator's `BlGogDownload`, only the package and class changed. + */ +object GogNative { + private const val TAG = "GogNative" + + /** gen2: depot manifests → chunk fetch + inflate + MD5 (base install, DLC, dependencies). */ + const val KIND_GEN2_CHUNKS = 0 + + /** gen1: build manifest → per-file HTTP Range GET streamed to disk. */ + const val KIND_GEN1_RANGES = 1 + + /** Callbacks from the engine; every method runs on a native thread. */ + interface Listener { + /** + * One file reached its final state. [verified] = resume-skip (existing file passed size+MD5; + * no bytes credited); otherwise a freshly assembled, verified and renamed file ([fileBytes] + * = its decompressed size). [filesDone] counts both; [bytesDone] counts assembled bytes only. + */ + fun onProgress(bytesDone: Long, bytesTotal: Long, filesDone: Int, filesTotal: Int, file: String, fileBytes: Long, verified: Boolean) + + /** + * Byte progress between file completions, every 250 ms while fetching (and once at the end): + * assembled bytes plus what the files in flight have so far. Drives the bar and the speed. + */ + fun onBytes(bytesDone: Long, bytesTotal: Long) + + /** Engine diagnostics (already in logcat under `GogNative`). */ + fun onLog(line: String) + + /** + * Exactly once per [start] that returned a non-zero handle. [linkExpiry] = the run died on an + * HTTP 401/403/404/500, the codes an expired secure link returns: refresh it and run again. + */ + fun onComplete(success: Boolean, cancelled: Boolean, linkExpiry: Boolean, error: String, bytesWritten: Long, filesDone: Int) + } + + @Volatile private var available: Boolean? = null + + /** True when the library loads and the GOG exports bind. Cached after the first call. */ + @JvmStatic + fun isAvailable(): Boolean { + available?.let { return it } + val ok = try { + StoresNative.ensureLoaded() && nativeProbe() == 1 + } catch (t: Throwable) { + Log.w(TAG, "native GOG engine unavailable: ${t.javaClass.simpleName}: ${t.message}") + false + } + available = ok + return ok + } + + /** + * Starts one download loop on a native thread; returns its handle (0 = not started, no + * callbacks). See JNI.md for the parameters; they are what the manager already holds. + */ + @JvmStatic + fun start( + kind: Int, depotManifests: Array<String>, cdnBases: Array<String>, installDir: String, skipPaths: Array<String>, + caBundlePath: String, maxWorkers: Int, processWorkers: Int, sortLargestFirst: Boolean, label: String, listener: Listener, + ): Long { + if (!isAvailable()) return 0L + return try { + nativeStart(kind, depotManifests, cdnBases, installDir, skipPaths, caBundlePath, maxWorkers, processWorkers, sortLargestFirst, label, listener) + } catch (t: Throwable) { + Log.e(TAG, "nativeStart threw: ${t.javaClass.simpleName}: ${t.message}") + 0L + } + } + + /** Requests cancellation; `onComplete(cancelled = true)` follows. Idempotent; 0 is a no-op. */ + @JvmStatic + fun cancel(handle: Long) { + if (handle == 0L) return + runCatching { nativeCancel(handle) } + } + + /** Releases the handle, once, after `onComplete`. */ + @JvmStatic + fun release(handle: Long) { + if (handle == 0L) return + runCatching { nativeRelease(handle) } + } + + @JvmStatic + private external fun nativeProbe(): Int + + @JvmStatic + private external fun nativeStart( + kind: Int, depotManifests: Array<String>, cdnBases: Array<String>, installDir: String, skipPaths: Array<String>, + caBundlePath: String, maxWorkers: Int, processWorkers: Int, sortLargestFirst: Boolean, label: String, listener: Listener, + ): Long + + @JvmStatic + private external fun nativeCancel(handle: Long) + + @JvmStatic + private external fun nativeRelease(handle: Long) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogPrefs.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogPrefs.kt new file mode 100644 index 0000000000..b5a3af6cb9 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogPrefs.kt @@ -0,0 +1,15 @@ +package com.droiddeck.launcher.stores.gog + +import android.content.Context +import android.content.SharedPreferences + +/** + * GOG's own records that are not credentials: the library cache, per-game facts the sync learnt + * (generation, release date, size, cover), a build's client id. App-private preferences; the + * sign-in itself lives in StoreAccounts. Public, not internal: the Java download manager reads it + * and Kotlin mangles internal members' names for Java. + */ +object GogPrefs { + @JvmStatic + fun get(context: Context): SharedPreferences = context.applicationContext.getSharedPreferences("stores.gog", Context.MODE_PRIVATE) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogStoreCatalog.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogStoreCatalog.kt new file mode 100644 index 0000000000..bad1caff4f --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogStoreCatalog.kt @@ -0,0 +1,209 @@ +package com.droiddeck.launcher.stores.gog + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreNet +import com.droiddeck.launcher.stores.StoreShelves +import org.json.JSONArray +import org.json.JSONObject +import java.net.URLEncoder +import java.util.Calendar +import java.util.Currency +import java.util.Locale +import java.util.concurrent.ConcurrentHashMap + +/** + * GOG's public catalog - the same unauthenticated service the gog.com store page uses: + * `catalog.gog.com/v1/catalog?order=desc:trending&productType=in:game,pack&...` with `discounted`, + * `price=between:0,0` and `query=like:` filters. Products carry pre-formatted prices and a + * discount string; Windows-only titles are kept. Cached in-process (shelves 30 min, searches + * 5 min) and the last good shelves mirrored to prefs so the tab paints offline too. + */ +object GogStoreCatalog { + private const val TAG = "GogStore" + private const val CATALOG = "https://catalog.gog.com/v1/catalog" + private const val FEATURED_TTL_MS = 3 * 60 * 60 * 1000L + private const val SEARCH_TTL_MS = 5 * 60 * 1000L + // v2: entries carry the mature mark; an older cache would show everything until it aged out. + private const val KEY_FEATURED = "store_featured_v2" + + private val supportedCurrencies = setOf( + "USD", "EUR", "GBP", "AUD", "CAD", "CHF", "DKK", "NOK", "PLN", "SEK", "BRL", "CNY", + "HKD", "ILS", "JPY", "KRW", "MXN", "NZD", "SGD", "TRY", "UAH", "ZAR", "INR", "RUB", + ) + + private class Cached<T>(val value: T, val at: Long) + private var featuredCache: Cached<StoreShelves>? = null + private val searchCache = ConcurrentHashMap<String, Cached<List<CatalogItem>>>() + + private fun country(): String = Locale.getDefault().country.takeIf { it.length == 2 }?.uppercase(Locale.ROOT) ?: "US" + + private fun currency(): String { + val cc = runCatching { Currency.getInstance(Locale("", country())).currencyCode }.getOrNull() + return if (cc != null && cc in supportedCurrencies) cc else "USD" + } + + private fun baseQuery(limit: Int, order: String, extra: String = ""): String = + "$CATALOG?limit=$limit&order=$order&productType=in:game,pack&page=1&countryCode=${country()}&locale=en-US¤cyCode=${currency()}$extra" + + /** GET the catalog; on a refusal of the locale's currency, retry as US / USD. */ + private fun fetchProducts(url: String): List<CatalogItem> { + var body = StoreNet.get(url) + if (body == null && !url.contains("countryCode=US&locale=en-US¤cyCode=USD")) { + body = StoreNet.get(url.replace(Regex("countryCode=[A-Z]{2}"), "countryCode=US").replace(Regex("currencyCode=[A-Z]{3}"), "currencyCode=USD")) + } + return if (body == null) emptyList() else parseProducts(body) + } + + /** The Store tab's shelves. Null only when every feed failed and nothing is cached. Blocking. */ + fun featured(context: Context, force: Boolean = false): StoreShelves? { + val now = System.currentTimeMillis() + featuredCache?.let { if (!force && now - it.at < FEATURED_TTL_MS) return it.value } + // Shelves saved on an earlier run are as good while they are young: shown, not fetched again. + if (!force && now - GogPrefs.get(context).getLong("store_featured_at", 0L) < FEATURED_TTL_MS) cachedFeatured(context)?.let { return it } + val trending = fetchProducts(baseQuery(24, "desc:trending")) + val newest = fetchProducts(baseQuery(40, "desc:releaseDate")).filter { !isFuture(it.releaseDate) }.take(24) + val deals = fetchProducts(baseQuery(24, "desc:discount", "&discounted=eq:true")) + val free = fetchProducts(baseQuery(24, "desc:trending", "&price=between:0,0")) + val result = StoreShelves(whatsNew = newest, deals = deals, free = free, trending = trending) + if (!result.isEmpty) { + featuredCache = Cached(result, now) + persist(context, result) + Log.i(TAG, "shelves: trending=${trending.size} new=${newest.size} deals=${deals.size} free=${free.size}") + return result + } + Log.w(TAG, "every catalog feed came back empty; using the on-disk mirror") + return loadPersisted(context) + } + + fun cachedFeatured(context: Context): StoreShelves? = featuredCache?.value ?: loadPersisted(context) + + fun search(query: String): List<CatalogItem> { + val term = query.trim() + if (term.length < 2) return emptyList() + val key = term.lowercase(Locale.ROOT) + val now = System.currentTimeMillis() + searchCache[key]?.let { if (now - it.at < SEARCH_TTL_MS) return it.value } + val results = fetchProducts(baseQuery(40, "desc:score", "&query=like:" + URLEncoder.encode(term, "UTF-8"))) + searchCache[key] = Cached(results, now) + return results + } + + private fun parseProducts(body: String): List<CatalogItem> = try { + val arr = JSONObject(body).optJSONArray("products") ?: JSONArray() + val out = ArrayList<CatalogItem>(arr.length()) + for (i in 0 until arr.length()) arr.optJSONObject(i)?.let { p -> parseProduct(p)?.let { out.add(it) } } + out.distinctBy { it.id } + } catch (e: Exception) { + Log.w(TAG, "catalog parse failed: ${e.message}") + emptyList() + } + + fun parseProduct(p: JSONObject): CatalogItem? { + val id = p.optString("id", "") + val title = p.optString("title", "") + if (id.isEmpty() || title.isEmpty()) return null + // Windows only: the app cannot run anything else. + val os = p.optJSONArray("operatingSystems") + if (os != null && os.length() > 0) { + var windows = false + for (i in 0 until os.length()) if (os.optString(i).equals("windows", true)) windows = true + if (!windows) return null + } + val price = p.optJSONObject("price") + val finalAmount = price?.optJSONObject("finalMoney")?.optString("amount")?.toDoubleOrNull() + val finalStr = price?.optString("final", "").orEmpty() + val baseStr = price?.optString("base", "").orEmpty() + val discount = price?.optString("discount", "").orEmpty().filter { it.isDigit() }.toIntOrNull() ?: 0 + val isFree = price != null && (finalAmount == 0.0 || finalStr == "$0.00" || finalStr == "0") + val genres = p.optJSONArray("genres") + val tags = buildList { + if (genres != null) for (i in 0 until minOf(3, genres.length())) genres.optJSONObject(i)?.optString("name")?.takeIf { it.isNotBlank() }?.let { add(it) } + }.joinToString(", ") + val slug = p.optString("slug", "") + val storeLink = p.optString("storeLink", "").ifBlank { if (slug.isNotBlank()) "https://www.gog.com/en/game/$slug" else "" } + val mature = isMature(p) + return CatalogItem( + store = Store.GOG, id = id, title = title, + imageUrl = p.optString("coverHorizontal", "").ifBlank { null }, + tallImageUrl = p.optString("coverVertical", "").ifBlank { null }, + tags = tags, isFree = isFree, hasPrice = price != null && finalStr.isNotBlank(), + finalPrice = finalStr, originalPrice = if (discount > 0) baseStr else "", + discountPercent = if (discount > 0 && baseStr.isNotBlank()) discount else 0, + storeUrl = storeLink, developer = p.optJSONArray("developers")?.optString(0, "").orEmpty(), + releaseDate = p.optString("releaseDate", ""), + extra = buildMap { if (slug.isNotBlank()) put("slug", slug) }, + mature = mature, + ) + } + + /** + * GOG's own data: `ratings[]` carries each system's age (`esrbRating` 17, `pegiRating` 18, + * `uskRating` 18, `gogRating` 18, ...) and `tags[]` its content tags (`mature`, `nsfw`, + * `sexual-content`, `nudity`). The catalog has no filter for either, so this is client-side. + */ + fun isMature(p: JSONObject): Boolean { + val ages = ArrayList<Int>() + p.optJSONArray("ratings")?.let { r -> for (i in 0 until r.length()) r.optJSONObject(i)?.optString("ageRating")?.toIntOrNull()?.let { ages.add(it) } } + val tags = ArrayList<String>() + p.optJSONArray("tags")?.let { t -> for (i in 0 until t.length()) t.optJSONObject(i)?.let { o -> tags.add(o.optString("slug")); tags.add(o.optString("name")) } } + return com.droiddeck.launcher.stores.matureByAge(ages) || com.droiddeck.launcher.stores.matureByTags(tags) + } + + /** GOG returns protocol-relative `//images…` URLs in a few places. */ + @JvmStatic + fun absolutize(url: String?): String { + if (url.isNullOrBlank()) return "" + return if (url.startsWith("//")) "https:$url" else url + } + + /** "YYYY.MM.DD" later than today: a pre-order, kept out of What's new. */ + private fun isFuture(date: String): Boolean { + if (date.length < 10) return false + val parts = date.substring(0, 10).split('.', '-') + if (parts.size != 3) return false + val y = parts[0].toIntOrNull() ?: return false + val m = parts[1].toIntOrNull() ?: return false + val d = parts[2].toIntOrNull() ?: return false + val cal = Calendar.getInstance() + val today = cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH) + return y * 10000 + m * 100 + d > today + } + + private fun itemToJson(i: CatalogItem): JSONObject = JSONObject().apply { + put("id", i.id); put("title", i.title); put("image", i.imageUrl ?: ""); put("tall", i.tallImageUrl ?: "") + put("tags", i.tags); put("free", i.isFree); put("hasPrice", i.hasPrice); put("final", i.finalPrice); put("orig", i.originalPrice) + put("disc", i.discountPercent); put("url", i.storeUrl); put("dev", i.developer); put("rel", i.releaseDate) + if (i.mature) put("mature", true) + } + + private fun itemFromJson(o: JSONObject): CatalogItem = CatalogItem( + store = Store.GOG, id = o.optString("id"), title = o.optString("title"), + imageUrl = o.optString("image").ifBlank { null }, tallImageUrl = o.optString("tall").ifBlank { null }, + tags = o.optString("tags"), isFree = o.optBoolean("free"), hasPrice = o.optBoolean("hasPrice"), + finalPrice = o.optString("final"), originalPrice = o.optString("orig"), discountPercent = o.optInt("disc"), + storeUrl = o.optString("url"), developer = o.optString("dev"), releaseDate = o.optString("rel"), + mature = o.optBoolean("mature"), + ) + + private fun listToJson(l: List<CatalogItem>) = JSONArray().apply { l.forEach { put(itemToJson(it)) } } + private fun listFromJson(a: JSONArray?): List<CatalogItem> { + if (a == null) return emptyList() + return List(a.length()) { i -> a.optJSONObject(i) }.filterNotNull().map(::itemFromJson) + } + + private fun persist(context: Context, f: StoreShelves) { + runCatching { + val o = JSONObject().put("trending", listToJson(f.trending)).put("new", listToJson(f.whatsNew)).put("deals", listToJson(f.deals)).put("free", listToJson(f.free)) + GogPrefs.get(context).edit().putString(KEY_FEATURED, o.toString()).putLong("store_featured_at", System.currentTimeMillis()).apply() + } + } + + private fun loadPersisted(context: Context): StoreShelves? = runCatching { + val s = GogPrefs.get(context).getString(KEY_FEATURED, null) ?: return null + val o = JSONObject(s) + StoreShelves(listFromJson(o.optJSONArray("new")), listFromJson(o.optJSONArray("deals")), listFromJson(o.optJSONArray("free")), listFromJson(o.optJSONArray("trending"))).takeIf { !it.isEmpty } + }.getOrNull() +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt index b18aa114ef..07237e0deb 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddAppDialog.kt @@ -83,6 +83,7 @@ import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.sp import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.DialogProperties @@ -312,7 +313,7 @@ internal fun rememberShown(onDismiss: () -> Unit): MutableTransitionState<Boolea @Composable internal fun AppDialog( shown: MutableTransitionState<Boolean>, close: () -> Unit, label: String, wide: Boolean, - modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit, + modifier: Modifier = Modifier, maxWidth: Dp? = null, content: @Composable ColumnScope.() -> Unit, ) { val pal = LocalPalette.current // The page's ring stays out of sight behind it. @@ -335,7 +336,7 @@ internal fun AppDialog( verticalArrangement = Arrangement.spacedBy(14.dp), modifier = Modifier .padding(12.dp) - .widthIn(max = if (wide) 860.dp else 560.dp) + .widthIn(max = maxWidth ?: if (wide) 860.dp else 560.dp) .fillMaxWidth() .heightIn(max = (LocalConfiguration.current.screenHeightDp - 24).dp) .shadow(24.dp, Shape16, ambientColor = Color.Black, spotColor = Color.Black) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt index 26751674e1..fcbefd1a01 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndContent.kt @@ -230,6 +230,11 @@ private fun Content( StorePage(s, a, modifier) return } + // Stores (GOG, Epic, Amazon) takes the whole pane: chips, shelves and a game's page inside it. + if (selected == "stores" && s.gameStoresEnabled) { + StoresPage(s, a, modifier) + return + } // The Games tab lays out its own list and detail. if (selected == "games" || selected.startsWith("app:")) { GamesPage(s, a, selected, onSelect, modifier) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt index c373f672b4..e623151511 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndGames.kt @@ -197,15 +197,19 @@ private fun ManageSaves(g: Library.SteamGame, prefix: java.io.File, a: FrontEndA } } Box { - SecondaryButton(stringResource(R.string.games_manage_saves), compact = true) { open = !open } - AnchoredMenu(open, onDismiss = { open = false }, title = stringResource(R.string.games_saves), note = summary) { first -> - MenuItem(stringResource(R.string.games_import_saves), checked = false, detail = stringResource(R.string.games_import_saves_hint), focusRequester = first) { + // A GOG or Epic game's saves are its cloud saves: the one place for them, named so. + val cloudGame = com.droiddeck.launcher.stores.Store.byId(g.source).let { it == com.droiddeck.launcher.stores.Store.GOG || it == com.droiddeck.launcher.stores.Store.EPIC } + val label = stringResource(if (cloudGame) R.string.store_cloud_saves else R.string.games_manage_saves) + SecondaryButton(label, compact = true) { open = !open } + AnchoredMenu(open, onDismiss = { open = false }, title = if (cloudGame) label else stringResource(R.string.games_saves), note = summary) { first -> + if (cloudGame) CloudRows(g, open, first) + MenuItem(stringResource(R.string.games_import_saves), checked = false, focusRequester = if (cloudGame) null else first) { open = false; a.onSaveImport(g) } - MenuItem(stringResource(R.string.games_export_gamehub), checked = false, detail = stringResource(R.string.games_export_gamehub_hint)) { + MenuItem(stringResource(R.string.games_export_gamehub), checked = false) { open = false; a.onSaveExport(g, com.droiddeck.launcher.session.GameSaves.Layout.GAMEHUB) } - MenuItem(stringResource(R.string.games_export_winlator), checked = false, detail = stringResource(R.string.games_export_winlator_hint)) { + MenuItem(stringResource(R.string.games_export_winlator), checked = false) { open = false; a.onSaveExport(g, com.droiddeck.launcher.session.GameSaves.Layout.WINLATOR) } found?.firstOrNull()?.let { d -> @@ -217,14 +221,74 @@ private fun ManageSaves(g: Library.SteamGame, prefix: java.io.File, a: FrontEndA } } +/** + * A GOG or Epic game's cloud saves: the Cloud saves switch, Upload and Download - three rows from + * the first frame, so nothing moves under the pad while the cloud is checked. Until the check is + * back the two actions are disabled and read "…"; then only their subtitles change: the last sync, + * "No cloud saves", or a conflict, when Upload and Download become Keep local and Keep cloud. + */ +@Composable +private fun CloudRows(g: Library.SteamGame, open: Boolean, first: androidx.compose.ui.focus.FocusRequester) { + val store = com.droiddeck.launcher.stores.Store.byId(g.source)?.takeIf { it != com.droiddeck.launcher.stores.Store.AMAZON } ?: return + val id = g.storeId ?: return + val context = androidx.compose.ui.platform.LocalContext.current.applicationContext + var busy by remember(g.gameId) { androidx.compose.runtime.mutableStateOf<String?>(null) } + var tick by remember(g.gameId) { androidx.compose.runtime.mutableStateOf(0) } + val status by androidx.compose.runtime.produceState<com.droiddeck.launcher.stores.CloudSaves.Status?>(null, g.gameId, open, tick) { + if (open) value = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { + runCatching { com.droiddeck.launcher.stores.CloudSaves.status(context, store, id) }.getOrNull() + } + } + // The game's own switch (sidecar "cloud"), known locally: right from the first frame. + val folder = g.gameFiles + var enabled by remember(g.gameId, open) { androidx.compose.runtime.mutableStateOf(folder?.let { com.droiddeck.launcher.stores.StoreGameSidecar.read(it)?.cloud } ?: true) } + val st = status + val supported = st?.supported ?: true + val conflict = st != null && st.conflicts.isNotEmpty() + fun sync(up: Boolean, force: Boolean = false) { + if (busy != null) return + busy = if (up) "up" else "down" + Thread({ + val r = if (up) com.droiddeck.launcher.stores.CloudSaves.upload(context, store, id, force) else com.droiddeck.launcher.stores.CloudSaves.download(context, store, id, force) + com.droiddeck.launcher.stores.StoresState.post { busy = null; tick++; android.widget.Toast.makeText(context, if (r.ok) context.getString(R.string.cloud_done, r.files) else r.reason, android.widget.Toast.LENGTH_SHORT).show() } + }, "cloud-manual").start() + } + MenuItem(stringResource(R.string.store_cloud_saves), checked = enabled && supported, enabled = supported, focusRequester = first, + detail = if (!supported) stringResource(R.string.cloud_none) else null) { + val f = folder ?: return@MenuItem + val next = !enabled + Thread({ + val written = runCatching { com.droiddeck.launcher.stores.StoreGameSidecar.updateCloud(f, next) }.getOrNull() + com.droiddeck.launcher.stores.StoresState.post { enabled = written?.cloud ?: enabled } + }, "cloud-switch").start() + } + val ready = st != null && supported && busy == null + val pending = stringResource(R.string.cloud_pending) + val summary = when { + st == null || busy != null -> pending + !supported -> stringResource(R.string.cloud_none) + conflict -> stringResource(R.string.cloud_conflict_files, st.conflicts.size) + st.lastSync > 0 -> stringResource(R.string.cloud_last_sync, android.text.format.DateUtils.getRelativeTimeSpanString(st.lastSync).toString()) + else -> stringResource(R.string.cloud_never) + } + // A conflict turns the two actions into its two answers; the rows stay where they are. + MenuItem(stringResource(if (conflict) R.string.cloud_keep_local else R.string.cloud_upload), checked = false, enabled = ready, detail = summary) { sync(up = true, force = conflict) } + MenuItem(stringResource(if (conflict) R.string.cloud_keep_cloud else R.string.cloud_download), checked = false, enabled = ready, detail = if (st == null || busy != null) pending else null) { sync(up = false, force = conflict) } +} + /** When it was last played (or where it is, if never) over its name, then room for Launch. */ @Composable private fun ColumnScope.GameHeroCopy(g: Library.SteamGame, titleSize: androidx.compose.ui.unit.TextUnit) { - Text( - (lastPlayedText(g.lastPlayed) ?: libraryLabel(g.library)).uppercase(), - fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.2.sp, color = LocalPalette.current.signal, - maxLines = 1, overflow = TextOverflow.Ellipsis, - ) + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { + SourceChip(g.source) + // A Custom game's chip already says what its eyebrow would; the line then stays empty. + val eyebrow = lastPlayedText(g.lastPlayed) ?: libraryLabel(g) + if (eyebrow.isNotEmpty()) Text( + eyebrow.uppercase(), + fontSize = 12.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 1.2.sp, color = LocalPalette.current.signal, + maxLines = 1, overflow = TextOverflow.Ellipsis, + ) + } Text( g.name, fontSize = titleSize, lineHeight = titleSize * 1.15f, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onBackground, maxLines = 2, overflow = TextOverflow.Ellipsis, @@ -277,10 +341,14 @@ private fun GameRow(g: Library.SteamGame, selected: Boolean, onSelect: () -> Uni } Column(modifier = Modifier.weight(1f)) { Text(g.name, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis) - Text( - playedSpan(g.lastPlayed)?.replaceFirstChar { it.uppercase() } ?: stringResource(R.string.games_never_played), - fontSize = 12.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, - ) + // Where it came from beside when it was played: a store's games sit among Steam's own. + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) { + SourceChip(g.source, small = true) + Text( + playedSpan(g.lastPlayed)?.replaceFirstChar { it.uppercase() } ?: stringResource(R.string.games_never_played), + fontSize = 12.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, + ) + } } } } @@ -329,11 +397,15 @@ private fun playedSpan(lastPlayed: Long): String? { ).toString() } +/** + * Where a never-played game lives, beside its source chip: Steam's library, the Games storage for + * a store install (the chip names the store), "Custom game" for a folder the user added. + */ @Composable -private fun libraryLabel(library: String): String = when (library) { - "internal" -> stringResource(R.string.games_internal) - Library.ADDED -> stringResource(R.string.games_added) - else -> library +private fun libraryLabel(g: Library.SteamGame): String = when { + g.library != Library.ADDED -> if (g.library == "internal") stringResource(R.string.games_internal) else g.library + g.source != Library.ADDED -> stringResource(R.string.games_store_storage) + else -> "" } /** diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt index efd4319ca0..ac8ebc1633 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt @@ -44,6 +44,7 @@ import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.Apps import androidx.compose.material.icons.outlined.DesktopWindows import androidx.compose.material.icons.outlined.Layers +import androidx.compose.material.icons.outlined.LocalMall import androidx.compose.material.icons.outlined.SportsEsports import androidx.compose.material.icons.outlined.Storefront import androidx.compose.material.icons.outlined.SystemUpdate @@ -57,6 +58,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -101,7 +103,7 @@ internal fun SideRail( modifier = modifier.width(if (iconOnly) 64.dp else 92.dp).background(colors.surface).padding(vertical = if (compact) 8.dp else 12.dp), ) { // Every section on screen at once: items shrink (to 44dp, still a touch target) before the rail scrolls. - val count = 6 + (if (s.storeEnabled) 1 else 0) + (if (s.isHomeApp) 1 else 0) + val count = 6 + (if (s.storeEnabled) 1 else 0) + (if (s.gameStoresEnabled) 1 else 0) + (if (s.isHomeApp) 1 else 0) BoxWithConstraints(Modifier.weight(1f).fillMaxWidth()) { val fit = (maxHeight / count - 4.dp).coerceIn(44.dp, if (iconOnly) 48.dp else if (compact) 52.dp else 60.dp) Column( @@ -112,6 +114,9 @@ internal fun SideRail( RailItem(stringResource(R.string.rail_steam), Icons.Outlined.SportsEsports, "steam", selected == "steam", compact, iconOnly, fit, onFocus = { onFocusSelect("steam") }) { onSelect("steam") } // Always there, so the items below it never move; an empty library says how to fill it. RailItem(stringResource(R.string.rail_games), Icons.Outlined.VideoLibrary, "games", selected == "games", compact, iconOnly, fit, onFocus = { onFocusSelect("games") }) { onSelect("games") } + // The game stores, right under Games: what they install lands there. Its badge counts + // the downloads in flight, so a download started here is never out of sight. + if (s.gameStoresEnabled) RailItem(stringResource(R.string.rail_stores), Icons.Outlined.LocalMall, "stores", selected == "stores", compact, iconOnly, fit, count = s.storeDownloadsActive, onFocus = { onFocusSelect("stores") }) { onSelect("stores") } RailItem(stringResource(R.string.rail_desktop), Icons.Outlined.DesktopWindows, "desktop", selected == "desktop", compact, iconOnly, fit, onFocus = { onFocusSelect("desktop") }) { onSelect("desktop") } if (s.storeEnabled) RailItem(stringResource(R.string.rail_store), Icons.Outlined.Storefront, "store", selected == "store", compact, iconOnly, fit, onFocus = { onFocusSelect("store") }) { onSelect("store") } RailItem(stringResource(R.string.rail_components), Icons.Outlined.Layers, "components", selected == "components", compact, iconOnly, fit, onFocus = { onFocusSelect("components") }) { onSelect("components") } @@ -134,7 +139,10 @@ internal fun SideRail( @Composable private fun RailItem( label: String, icon: ImageVector, key: String, current: Boolean, compact: Boolean, iconOnly: Boolean, height: Dp, - badge: Boolean = false, onFocus: () -> Unit = {}, onClick: () -> Unit, + badge: Boolean = false, + /** A number on the item (downloads in flight); 0 shows nothing. */ + count: Int = 0, + onFocus: () -> Unit = {}, onClick: () -> Unit, ) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current @@ -182,6 +190,12 @@ private fun RailItem( modifier = Modifier.align(Alignment.TopEnd).padding(top = if (iconOnly) 7.dp else 8.dp, end = if (iconOnly) 9.dp else 18.dp) .size(8.dp).clip(CircleShape).background(AttentionAmber), ) + if (count > 0) Text( + if (count > 9) "9+" else count.toString(), + fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.onSignal, maxLines = 1, + modifier = Modifier.align(Alignment.TopEnd).padding(top = if (iconOnly) 4.dp else 6.dp, end = if (iconOnly) 5.dp else 12.dp) + .clip(RoundedCornerShape(8.dp)).background(pal.signal).padding(horizontal = 5.dp, vertical = 1.dp), + ) } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt index 8781f419bf..e9717dc651 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndScreen.kt @@ -129,6 +129,17 @@ class FrontEndState( val animationsEnabled: Boolean = true, /** The Flathub Store, a beta the user turns on in Setup. */ val storeEnabled: Boolean = false, + /** The Stores section (GOG, Epic Games, Amazon Games), turned on in Setup. */ + val gameStoresEnabled: Boolean = false, + /** Setup › Stores: the download engine's speed tier (stores/download/StoreDownloadTier ids). */ + val gameStoresSpeedTier: String = "fast", + /** Setup › Stores: the tab a signed-in store opens on (SessionPrefs.STORES_OPEN_LIBRARY / STORE). */ + val storesOpenTab: String = SessionPrefs.STORES_OPEN_LIBRARY, + /** A page to open once: "stores:downloads", or a selection such as "app:<id>" (a notification's tap). */ + val navigate: String? = null, + val storesShowMature: Boolean = false, + /** Downloads queued, running or paused across the three stores; the rail item's badge. */ + val storeDownloadsActive: Int = 0, /** The Updates page: DroidDeck's own builds and the channel followed. */ val updates: UpdatesState = UpdatesState(), ) @@ -179,6 +190,13 @@ class FrontEndActions( val onLauncherFullscreen: (Boolean) -> Unit = {}, val onAnimationsEnabled: (Boolean) -> Unit = {}, val onStoreEnabled: (Boolean) -> Unit = {}, + val onGameStoresEnabled: (Boolean) -> Unit = {}, + val onGameStoresSpeedTier: (String) -> Unit = {}, + val onStoresOpenTab: (String) -> Unit = {}, + val onNavigated: () -> Unit = {}, + val onStoresShowMature: (Boolean) -> Unit = {}, + /** The Games list rebuilt now: a store install or removal changed what is on disk. */ + val onLibraryChanged: () -> Unit = {}, val onHomeApp: () -> Unit = {}, val onHomeScreen: (Boolean) -> Unit = {}, val onAndroidApp: (HomeApp.LaunchableApp, Int?) -> Unit = { _, _ -> }, @@ -356,6 +374,11 @@ fun FrontEndScreen(s: FrontEndState, a: FrontEndActions, page: (@Composable () - private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Composable () -> Unit)?, frontFocus: FrontFocus) { var selected by rememberSaveable { mutableStateOf(if (s.shortcutPicker) "games" else "steam") } LaunchedEffect(s.shortcutPicker) { if (s.shortcutPicker) selected = "games" } + LaunchedEffect(s.navigate) { + val target = s.navigate ?: return@LaunchedEffect + if (target == "stores:downloads") { com.droiddeck.launcher.stores.StoresState.openDownloads = true; selected = "stores" } else selected = target + a.onNavigated() + } var showWirelessAdbFix by rememberSaveable { mutableStateOf(false) } var showDeveloperDisplayChoice by rememberSaveable { mutableStateOf(false) } var wirelessAdbDesiredEnabled by rememberSaveable { mutableStateOf(false) } @@ -391,8 +414,9 @@ private fun FrontEndScreenBody(s: FrontEndState, a: FrontEndActions, page: (@Com selected = if (selected.startsWith("rom:")) "emu:" + selected.removePrefix("rom:").substringBefore(':') else "desktop" } LaunchedEffect(s.isHomeApp) { if (!s.isHomeApp && selected == "android-apps") selected = "steam" } - // The Store turned off in Setup takes its page with it. + // The Store turned off in Setup takes its page with it; so does Stores. LaunchedEffect(s.storeEnabled) { if (!s.storeEnabled && selected == "store") selected = "steam" } + LaunchedEffect(s.gameStoresEnabled) { if (!s.gameStoresEnabled && selected == "stores") selected = "games" } // The last game uninstalled leaves the Games tab on its empty state. LaunchedEffect(s.steamGames.isEmpty()) { if (s.steamGames.isEmpty() && selected.startsWith("app:")) selected = "games" diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt index 5965758003..171caf8187 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt @@ -76,6 +76,7 @@ import com.droiddeck.launcher.core.DeviceSupport import com.droiddeck.launcher.core.PhantomProcessLimit import com.droiddeck.launcher.core.PhantomProcessStatus import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.download.StoreDownloadTier import com.droiddeck.launcher.R // The Setup page: runtime and device checks, tools, frame generation and launch settings. @@ -367,6 +368,24 @@ internal fun SetupPanel( s.storeEnabled, ) { a.onStoreEnabled(it) } } + SettingsGroup(stringResource(R.string.setup_stores)) { + ToggleRow( + host, "stores-enabled", stringResource(R.string.setup_stores_show), + null, s.gameStoresEnabled, + ) { a.onGameStoresEnabled(it) } + // The same rows as the Stores page's own cog, for whoever looks here first. + SettingsRow(stringResource(R.string.setup_stores_open_on), null) { + SegmentedTabs( + listOf(SessionPrefs.STORES_OPEN_LIBRARY to stringResource(R.string.stores_tab_library), SessionPrefs.STORES_OPEN_STORE to stringResource(R.string.stores_tab_store)), + s.storesOpenTab, + ) { a.onStoresOpenTab(it) } + } + ToggleRow(host, "stores-show-mature", stringResource(R.string.stores_show_mature), null, s.storesShowMature) { a.onStoresShowMature(it) } + SettingsRow(stringResource(R.string.setup_stores_speed), null) { + SegmentedTabs(StoreDownloadTier.ALL.map { it.id to stringResource(it.label) }, s.gameStoresSpeedTier) { a.onGameStoresSpeedTier(it) } + } + StoresEngineRow() + } } } } @@ -456,6 +475,15 @@ internal fun LaunchSettings(s: FrontEndState, a: FrontEndActions, host: MenuHost } } if (controller != null) add { m -> SettingCard(stringResource(R.string.setup_card_controls), stringResource(R.string.setup_card_controls_hint), "card:controls", m, controller.onMapping) } + // An Epic game's own launch choices, kept in its sidecar so a launch from the Steam client + // honours them too (droiddeck-store-launch reads the same file). + // Epic's launch choices; cloud saves live in the game's Cloud saves view, so a GOG game has no card. + val cardStore = com.droiddeck.launcher.stores.Store.byId(game?.source.orEmpty())?.takeIf { it == com.droiddeck.launcher.stores.Store.EPIC } + if (cardStore != null && game?.gameFiles != null) add { m -> + Box(m) { + StoreLaunchCard(host, game.gameFiles!!, cardStore) + } + } if (wincompKey != null) add { m -> SettingCard( stringResource(R.string.wincomp_title), @@ -479,6 +507,53 @@ internal fun LaunchSettings(s: FrontEndState, a: FrontEndActions, host: MenuHost } } +/** Epic's account page behind its sign-in: signing in there presents any step the account still owes. */ +private const val EPIC_RESOLVE_URL = "https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Faccount%2Fpersonal" + +/** The Epic card: the sign-in and offline switches, read from and written to the game's sidecar, and Resolve Epic sign-in. */ +@Composable +private fun StoreLaunchCard(host: MenuHost, folder: java.io.File, store: com.droiddeck.launcher.stores.Store) { + val appContext = androidx.compose.ui.platform.LocalContext.current.applicationContext + var sidecar by remember(folder) { mutableStateOf(com.droiddeck.launcher.stores.StoreGameSidecar.read(folder)) } + val options = sidecar?.epic ?: com.droiddeck.launcher.stores.EpicOptions() + val epic = store == com.droiddeck.launcher.stores.Store.EPIC + val key = "store-" + store.id + val value = buildList { + if (epic && options.offline) add(stringResource(R.string.epic_card_offline)) + else if (epic && options.eos) add(stringResource(R.string.epic_card_eos)) + }.ifEmpty { listOf(stringResource(R.string.epic_card_none)) }.joinToString(" · ") + // Each switch goes straight to the sidecar on disk - both launch paths read it there - and the + // card shows what was read back, so a write that did not take is never shown as done. + fun set(change: (com.droiddeck.launcher.stores.EpicOptions) -> com.droiddeck.launcher.stores.EpicOptions) { + Thread({ + val written = runCatching { com.droiddeck.launcher.stores.StoreGameSidecar.updateEpic(folder, change) } + .onFailure { android.util.Log.w("EpicLaunchCard", "could not write ${folder.name}: ${it.message}") }.getOrNull() + if (written != null) android.util.Log.i("EpicLaunchCard", "epic options ${written.id} eos=${written.epic.eos} offline=${written.epic.offline}") + else android.util.Log.w("EpicLaunchCard", "epic options not saved for ${folder.name}") + com.droiddeck.launcher.stores.StoresState.post { sidecar = written ?: com.droiddeck.launcher.stores.StoreGameSidecar.read(folder) } + }, "epic-options").start() + } + SettingCard(store.shortLabel, value, "card:$key", Modifier.fillMaxSize()) { + host.open = if (host.open == key) null else key + } + AnchoredMenu(host.open == key, onDismiss = { if (host.open == key) host.open = null }, title = store.shortLabel) { first -> + if (epic) { + MenuItem(stringResource(R.string.epic_eos), checked = options.eos, focusRequester = first) { set { it.copy(eos = !it.eos) } } + MenuItem(stringResource(R.string.epic_offline), checked = options.offline) { set { it.copy(offline = !it.offline) } } + } + if (epic) { + // Epic asks some accounts to accept something once (privacy policy, EULA) before a game may + // sign in - EOS's "corrective action". Signing in on Epic's site shows it. + MenuItem(stringResource(R.string.epic_resolve), checked = false) { + host.open = null + runCatching { + appContext.startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(EPIC_RESOLVE_URL)).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)) + } + } + } + } +} + @Composable private fun SettingCard(label: String, value: String, id: String, modifier: Modifier, onClick: () -> Unit) { val colors = MaterialTheme.colorScheme diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt index 9cc04049b7..d894815aac 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -233,6 +233,19 @@ class MenuHost { @Composable fun rememberMenuHost(): MenuHost = remember { MenuHost() } +/** Under the anchor with left edges aligned, kept on screen: a dropdown that belongs to a tab. */ +private class BelowStartProvider(private val gap: Int) : PopupPositionProvider { + override fun calculatePosition(anchorBounds: IntRect, windowSize: IntSize, layoutDirection: LayoutDirection, popupContentSize: IntSize): IntOffset { + var x = anchorBounds.left + if (x + popupContentSize.width > windowSize.width - 8) x = windowSize.width - 8 - popupContentSize.width + if (x < 8) x = 8 + var y = anchorBounds.bottom + gap + if (y + popupContentSize.height > windowSize.height - 8) y = anchorBounds.top - gap - popupContentSize.height + if (y < 8) y = 8 + return IntOffset(x, y) + } +} + private class BelowEndProvider(private val gap: Int) : PopupPositionProvider { override fun calculatePosition(anchorBounds: IntRect, windowSize: IntSize, layoutDirection: LayoutDirection, popupContentSize: IntSize): IntOffset { var x = anchorBounds.right - popupContentSize.width @@ -245,7 +258,12 @@ private class BelowEndProvider(private val gap: Int) : PopupPositionProvider { } @Composable -fun AnchoredMenu(open: Boolean, onDismiss: () -> Unit, title: String? = null, note: String? = null, content: @Composable ColumnScope.(FocusRequester) -> Unit) { +fun AnchoredMenu( + open: Boolean, onDismiss: () -> Unit, title: String? = null, note: String? = null, + /** A tab's dropdown: under its anchor, left edges aligned, as wide as its entries (within [minWidth]..260 dp). */ + compact: Boolean = false, minWidth: androidx.compose.ui.unit.Dp = 120.dp, + content: @Composable ColumnScope.(FocusRequester) -> Unit, +) { val state = remember { MutableTransitionState(false) } state.targetState = open if (!state.currentState && !state.targetState && state.isIdle) return @@ -259,14 +277,14 @@ fun AnchoredMenu(open: Boolean, onDismiss: () -> Unit, title: String? = null, no } } val gap = with(LocalDensity.current) { 6.dp.roundToPx() } - val provider = remember(gap) { BelowEndProvider(gap) } + val provider = remember(gap, compact) { if (compact) BelowStartProvider(gap) else BelowEndProvider(gap) } val colors = MaterialTheme.colorScheme val pal = LocalPalette.current Popup(popupPositionProvider = provider, onDismissRequest = onDismiss, properties = PopupProperties(focusable = true)) { AnimatedVisibility( visibleState = state, - enter = fadeIn(Motion.tw(180)) + scaleIn(Motion.sp(0.7f), initialScale = 0.94f, transformOrigin = TransformOrigin(1f, 0f)), - exit = fadeOut(Motion.tw(140)) + scaleOut(Motion.tw(140), targetScale = 0.96f, transformOrigin = TransformOrigin(1f, 0f)), + enter = fadeIn(Motion.tw(180)) + scaleIn(Motion.sp(0.7f), initialScale = 0.94f, transformOrigin = TransformOrigin(if (compact) 0f else 1f, 0f)), + exit = fadeOut(Motion.tw(140)) + scaleOut(Motion.tw(140), targetScale = 0.96f, transformOrigin = TransformOrigin(if (compact) 0f else 1f, 0f)), label = "menu", ) { // A long list (a driver menu with its downloads) must not run off the screen: the menu is @@ -274,7 +292,7 @@ fun AnchoredMenu(open: Boolean, onDismiss: () -> Unit, title: String? = null, no val maxHeight = (androidx.compose.ui.platform.LocalConfiguration.current.screenHeightDp * 0.86f).dp Column( modifier = Modifier - .widthIn(min = 220.dp, max = 340.dp) + .then(if (compact) Modifier.widthIn(min = minWidth, max = 260.dp).width(androidx.compose.foundation.layout.IntrinsicSize.Max) else Modifier.widthIn(min = 220.dp, max = 340.dp)) .heightIn(max = maxHeight) .shadow(24.dp, RowShape, ambientColor = Color.Black, spotColor = Color.Black) .clip(RowShape) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt new file mode 100644 index 0000000000..c35a9beda7 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt @@ -0,0 +1,71 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.stores.Store + +// Where a game came from, as a small coloured chip: Steam, one of the stores, or an added folder. + +/** The chip's colours per source: a tinted ground and a readable ink on it, one pair per store. */ +internal class SourceColours(val fill: Color, val ink: Color, val dot: Color) + +internal fun sourceColours(source: String): SourceColours = when (source) { + Store.GOG.id -> SourceColours(Color(0xFF3A1A3C), Color(0xFFE6A3EA), Color(0xFFC25BC8)) + Store.EPIC.id -> SourceColours(Color(0xFF2A2A2A), Color(0xFFDDDDDD), Color(0xFFBDBDBD)) + Store.AMAZON.id -> SourceColours(Color(0xFF3A2A0A), Color(0xFFFFC266), Color(0xFFFF9900)) + Library.SOURCE_STEAM -> SourceColours(Color(0xFF16293D), Color(0xFF7CC4FF), Color(0xFF1A9FFF)) + else -> SourceColours(Color(0xFF1A1D22), Color(0xFF9AA3AF), Color(0xFF9AA3AF)) +} + +@Composable +internal fun sourceLabel(source: String): String = when (source) { + Library.SOURCE_STEAM -> stringResource(R.string.source_steam) + Store.GOG.id -> Store.GOG.label + Store.EPIC.id -> Store.EPIC.label + Store.AMAZON.id -> Store.AMAZON.label + else -> stringResource(R.string.source_added) +} + +/** The source chip itself; [small] for a list row, the regular size for a hero or a card. */ +@Composable +internal fun SourceChip(source: String, small: Boolean = false, modifier: Modifier = Modifier) { + val c = sourceColours(source) + Text( + sourceLabel(source), fontSize = if (small) 10.sp else 11.sp, fontWeight = FontWeight.Bold, letterSpacing = 0.4.sp, + color = c.ink, maxLines = 1, + modifier = modifier.clip(RoundedCornerShape(6.dp)).background(c.fill) + .padding(horizontal = if (small) 6.dp else 8.dp, vertical = if (small) 1.dp else 2.dp), + ) +} + +/** A status chip in the stores' colours: green for done ("Installed"), blue for busy, amber for a warning. */ +@Composable +internal fun StateChip(text: String, tone: ChipTone, small: Boolean = false) { + val pal = LocalPalette.current + val (fill, ink) = when (tone) { + ChipTone.OK -> Color(0xFF16321F) to pal.good + ChipTone.BUSY -> pal.signal.copy(alpha = 0.18f) to pal.signal + ChipTone.WARN -> Color(0xFF3A2E10) to Color(0xFFFFC24D) + ChipTone.DEAL -> Color(0xFF1E3A1A) to Color(0xFF9BE27A) + ChipTone.PLAIN -> MaterialTheme.colorScheme.surfaceVariant to MaterialTheme.colorScheme.onSurfaceVariant + } + Text( + text, fontSize = if (small) 9.sp else 11.sp, fontWeight = FontWeight.Bold, letterSpacing = 0.3.sp, color = ink, maxLines = 1, + modifier = Modifier.clip(RoundedCornerShape(6.dp)).background(fill).padding(horizontal = if (small) 5.dp else 8.dp, vertical = if (small) 1.dp else 2.dp), + ) +} + +internal enum class ChipTone { OK, BUSY, WARN, DEAL, PLAIN } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt new file mode 100644 index 0000000000..2e28a258f7 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt @@ -0,0 +1,118 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import coil.compose.AsyncImage +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.download.DownloadState +import com.droiddeck.launcher.stores.formatBytes + +// A store game's own page: hero, the one main action, how it is launched and where it lives. + +@Composable +internal fun StoreGameDetail(store: Store, key: String, s: FrontEndState, a: FrontEndActions, onBack: () -> Unit) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val narrow = LocalNarrowPane.current + 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 } + // Removal asks twice: one stray press of A should not cost a download. + var confirmRemove by remember(key) { mutableStateOf(false) } + LaunchedEffect(confirmRemove) { if (confirmRemove) { kotlinx.coroutines.delay(4000); confirmRemove = false } } + 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)) { + BackLink(store.label, compact = true, onClick = onBack) + SourceChip(store.id) + } + } + Rise(1) { + Box(Modifier.fillMaxWidth().heightIn(min = if (narrow) 150.dp else 180.dp).clip(Shape16).background(artBrush(hueOf(title)))) { + val art = item?.imageUrl ?: installed?.sidecar?.hero ?: installed?.sidecar?.cover + if (art != null) AsyncImage(model = art, contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier.matchParentSize()) + Spacer(Modifier.matchParentSize().background(Brush.horizontalGradient(0f to colors.background.copy(alpha = 0.94f), 0.5f to colors.background.copy(alpha = 0.55f), 1f to Color.Transparent))) + Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.align(Alignment.BottomStart).padding(horizontal = 22.dp, vertical = 18.dp)) { + // An install names where it went (internal storage, the card), a download where it is going. + 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 + 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) + Actions { + when { + installed != null -> { + // Launching, the prefix and the files belong to the Games tab, where every + // installed game has them; here an install only says it is in Steam and + // offers the one thing Games does not: removing it. + ActionChip(stringResource(R.string.stores_in_steam), ok = true) + SecondaryButton(if (confirmRemove) stringResource(R.string.stores_uninstall_confirm) else stringResource(R.string.stores_uninstall), enabled = download == null) { + if (!confirmRemove) confirmRemove = true + else { confirmRemove = false; StoresState.uninstall(ctx, installed) { StoresState.refresh(ctx); a.onLibraryChanged() }; onBack() } + } + } + download != null -> { + ActionChip(downloadLabel(download), ok = false) + if (download.state != DownloadState.PAUSED) SecondaryButton(stringResource(R.string.stores_dl_pause)) { com.droiddeck.launcher.stores.download.DownloadQueue.pause(download.key) } + 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) } + } + item?.owned == true && StoresState.isUnfinished(item) -> { + PrimaryButton(stringResource(R.string.stores_resume_install), main = true) { StoresState.requestInstall(ctx, item) } + ConfirmButton(stringResource(R.string.stores_dl_clear), stringResource(R.string.stores_dl_cancel_confirm), compact = true) { StoresState.clearUnfinished(ctx, item) } + } + item?.owned == true -> PrimaryButton( + if (item.sizeBytes > 0) stringResource(R.string.stores_install_size, formatBytes(item.sizeBytes)) else stringResource(R.string.stores_install), main = true, + ) { StoresState.requestInstall(ctx, item) } + item != null && item.isFree -> PrimaryButton(stringResource(R.string.stores_get_free), main = true) { openStoreUrl(ctx, item) } + item != null -> PrimaryButton(if (item.hasPrice && item.finalPrice.isNotBlank()) stringResource(R.string.stores_buy_price, item.finalPrice) else stringResource(R.string.stores_view_on, store.shortLabel), main = true) { openStoreUrl(ctx, item) } + } + BusyChip(s) + } + } + } + } + // Plain text only, and nothing at all when the store sent a template key instead of words. + // A description that is only the title again (some stores fill it so) is no description. + val description = item?.let { com.droiddeck.launcher.stores.cleanStoreText(it.description) }.orEmpty() + .takeUnless { it.trim().equals(title.trim(), ignoreCase = true) }.orEmpty() + if (description.isNotBlank()) Rise(2) { + Text(description, fontSize = 13.sp, lineHeight = 19.sp, color = colors.onSurfaceVariant, maxLines = 6, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(top = 12.dp)) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt new file mode 100644 index 0000000000..502691b3e0 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt @@ -0,0 +1,328 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.ui.draw.alpha +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.clickable +import androidx.compose.material.icons.automirrored.outlined.Undo +import androidx.compose.material.icons.outlined.Delete +import androidx.compose.material.icons.outlined.Close +import androidx.compose.material.icons.filled.Refresh +import androidx.compose.material.icons.filled.Pause +import coil.compose.AsyncImage +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.graphics.Brush +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.PlayArrow +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.key +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.download.DownloadEntry +import com.droiddeck.launcher.stores.download.DownloadQueue +import com.droiddeck.launcher.stores.download.DownloadStage +import com.droiddeck.launcher.stores.download.DownloadState +import com.droiddeck.launcher.stores.download.StoreDownloadTier +import com.droiddeck.launcher.stores.formatBytes +import com.droiddeck.launcher.stores.formatSpeed + +// The Downloads chip: one queue for the three stores, its settings and the engine log. + +@Composable +internal fun StoresDownloadsPane(s: FrontEndState, a: FrontEndActions) { + BoxWithConstraints(Modifier.fillMaxWidth()) { + val wide = maxWidth >= 700.dp + val list: @Composable () -> Unit = { DownloadList(s, a) } + val side: @Composable () -> Unit = { DownloadSettings(s, a) } + if (wide) Row(horizontalArrangement = Arrangement.spacedBy(24.dp)) { + Box(Modifier.weight(3f)) { list() } + Box(Modifier.weight(2f)) { side() } + } else Column { list(); side() } + } +} + +@Composable +private fun DownloadList(s: FrontEndState, a: FrontEndActions) { + val colors = MaterialTheme.colorScheme + val entries = StoresState.downloads + if (entries.isEmpty()) { + Box(Modifier.fillMaxWidth().padding(vertical = 40.dp), contentAlignment = Alignment.Center) { + Text(stringResource(R.string.stores_downloads_empty), fontSize = 13.sp, color = colors.onSurfaceVariant) + } + return + } + // Under way on top; finished below a line whose X clears them all (rows only - a failed + // download's kept files stay until its game page's Clear). + val (active, finished) = entries.partition { it.isActive } + Column(verticalArrangement = Arrangement.spacedBy(10.dp)) { + // The list changes only when rows come, go or change state; each row reads its own progress. + active.forEachIndexed { i, row -> key(row.key) { Rise(1 + i.coerceAtMost(5)) { DownloadCard(StoresState.download(row.key) ?: row, s, a) } } } + if (finished.isNotEmpty()) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth().padding(top = if (active.isEmpty()) 0.dp else 4.dp)) { + Box(Modifier.weight(1f).height(1.dp).background(LocalPalette.current.line)) + RoundAction(Icons.Outlined.Close, stringResource(R.string.stores_dl_clear_all), size = 30.dp) { DownloadQueue.dismissFinished() } + } + finished.forEach { row -> + key(row.key) { + val d = StoresState.download(row.key) ?: row + if (d.state == DownloadState.FAILED) DownloadCard(d, s, a) else FinishedRow(d, s, a) + } + } + } + } +} + +/** A finished download: its name and store, and Play when it installed. */ +@Composable +private fun FinishedRow(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp), + modifier = Modifier.fillMaxWidth().clip(Shape12).background(colors.surface).padding(horizontal = 12.dp, vertical = 6.dp), + ) { + Text(d.name, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false)) + SourceChip(d.store.id, small = true) + Text(stringResource(if (d.state == DownloadState.INSTALLED) R.string.stores_installed_chip else R.string.stores_dl_cancelled), fontSize = 11.sp, color = colors.onSurfaceVariant, maxLines = 1, modifier = Modifier.weight(1f)) + if (d.state == DownloadState.INSTALLED) RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_play), primary = true) { launchStoreGame(ctx, d.store, d.id, s, a) } + } +} + +/** + * A row's action as a round icon button - the label is its description - sized like the old + * compact buttons and focusable by pad. [primary] fills it with the accent, [danger] in red. + */ +@Composable +internal fun RoundAction( + icon: androidx.compose.ui.graphics.vector.ImageVector, description: String, primary: Boolean = false, danger: Boolean = false, + enabled: Boolean = true, size: androidx.compose.ui.unit.Dp = 36.dp, onClick: () -> Unit, +) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { androidx.compose.foundation.interaction.MutableInteractionSource() } + val hot = rememberHot(src) + val fill = when { danger -> Color(0xFFD9443B); primary -> pal.signal; else -> colors.surface.copy(alpha = 0.85f) } + val tint = if (primary || danger) Color.White else if (hot) pal.signal else colors.onBackground + Box( + contentAlignment = Alignment.Center, + modifier = Modifier.size(size).clip(androidx.compose.foundation.shape.CircleShape).background(fill) + .glideBorder(hot, androidx.compose.foundation.shape.CircleShape, if (primary || danger) colors.onBackground else pal.signal, pal.line) + .alpha(if (enabled) 1f else 0.4f) + .hoverable(src).clickable(interactionSource = src, indication = androidx.compose.foundation.LocalIndication.current, enabled = enabled, role = androidx.compose.ui.semantics.Role.Button, onClick = onClick) + .controllerConfirm(enabled = enabled, onClick = onClick), + ) { androidx.compose.material3.Icon(icon, description, tint = tint, modifier = Modifier.size(size * 0.5f)) } +} + +/** Cancel in two taps: the stop icon, then a red delete and a keep that undoes it (back by itself after a few seconds). */ +@Composable +private fun CancelAction(onDelete: () -> Unit) { + var armed by remember { mutableStateOf(false) } + androidx.compose.runtime.LaunchedEffect(armed) { if (armed) { kotlinx.coroutines.delay(4000); armed = false } } + if (!armed) RoundAction(Icons.Outlined.Close, stringResource(R.string.stores_dl_cancel)) { armed = true } + else { + RoundAction(Icons.Outlined.Delete, stringResource(R.string.stores_dl_cancel_confirm), danger = true) { armed = false; onDelete() } + RoundAction(Icons.AutoMirrored.Outlined.Undo, stringResource(R.string.stores_notification_keep)) { armed = false } + } +} + +@Composable +private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) { + val ctx = LocalContext.current + val pal = LocalPalette.current + val narrow = LocalNarrowPane.current + val ink = Color.White + val dim = Color.White.copy(alpha = 0.72f) + // The game's wide art is the row: cropped to it, a dark wash from the left so the copy reads + // in white there, the art clear on the right. + Box(Modifier.fillMaxWidth().heightIn(min = 88.dp).clip(Shape12).background(Color(0xFF101318)).border(1.dp, pal.line, Shape12)) { + if (!d.cover.isNullOrEmpty()) AsyncImage(model = d.cover, contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier.matchParentSize()) + else Spacer(Modifier.matchParentSize().background(artBrush(hueOf(d.name)))) + Spacer(Modifier.matchParentSize().background(Brush.horizontalGradient(0f to Color.Black.copy(alpha = 0.92f), 0.5f to Color.Black.copy(alpha = 0.7f), 1f to Color.Black.copy(alpha = 0.15f)))) + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 10.dp)) { + Column(verticalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.weight(1f)) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { + Text(d.name, fontSize = 14.sp, fontWeight = FontWeight.Bold, color = ink, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false)) + SourceChip(d.store.id, small = true) + val where = listOfNotNull(d.location.takeIf { it.isNotBlank() }, d.diskBytes.takeIf { it > 0 }?.let { formatBytes(it) }).joinToString(" · ") + if (where.isNotEmpty()) Text(where, fontSize = 11.sp, color = dim, maxLines = 1, overflow = TextOverflow.Ellipsis) + } + StageBar(d) + Row(horizontalArrangement = Arrangement.spacedBy(14.dp)) { + val meta = when (d.state) { + DownloadState.INSTALLED -> stringResource(R.string.stores_installed_chip) + DownloadState.CANCELLED -> stringResource(R.string.stores_dl_cancelled) + // The reason is in the engine log; the row only says what is kept for Resume. + DownloadState.FAILED -> 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 -> { + val head = downloadLabel(d) + when { + d.stage == DownloadStage.DOWNLOAD && d.bytesTotal > 0 -> stringResource(R.string.stores_dl_with, head, stringResource(R.string.stores_dl_amount, formatBytes(d.bytesDone), formatBytes(d.bytesTotal))) + d.stageItemsTotal > 0 -> stringResource(R.string.stores_dl_with, head, stringResource(R.string.stores_dl_items, d.stageItems, d.stageItemsTotal)) + else -> head + } + } + } + Text(meta, fontSize = 12.sp, color = dim, 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) + } + } + } + // The row's actions on the right, over the art: icons, their labels as descriptions. + Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically) { + when (d.state) { + DownloadState.RUNNING, DownloadState.QUEUED -> { + RoundAction(Icons.Filled.Pause, stringResource(R.string.stores_dl_pause)) { DownloadQueue.pause(d.key) } + CancelAction { DownloadQueue.cancel(ctx, d.key) } + } + DownloadState.PAUSED -> { + 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.INSTALLED -> RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_play), primary = true) { launchStoreGame(ctx, d.store, d.id, s, a) } + DownloadState.CANCELLED -> {} + } + } + } + } +} + +/** + * One segment per stage - Manifest, Download, Verify, Install - equal widths: a passed or skipped + * stage full, the active one filling with its own progress (a sliver while it has nothing to count), + * the rest empty. + */ +@Composable +private fun StageBar(d: DownloadEntry) { + val stages = listOf(DownloadStage.MANIFEST, DownloadStage.DOWNLOAD, DownloadStage.VERIFY, DownloadStage.INSTALL) + Row(horizontalArrangement = Arrangement.spacedBy(3.dp), modifier = Modifier.fillMaxWidth()) { + for (st in stages) { + val active = d.state != DownloadState.INSTALLED && d.stage == st + val fill = when { + active -> d.stageFraction.let { if (it < 0f) 0.04f else it } + // A stage a store skips or folds into another (Epic checks chunks while it fetches, + // Amazon writes while it downloads) reads complete once a later one has started. + d.passed(st) || st.ordinal < d.stage.ordinal || d.stage == DownloadStage.DONE -> 1f + else -> 0f + } + Box(Modifier.weight(1f)) { + ProgressBarThin( + fill, paused = active && d.state == DownloadState.PAUSED, verify = active && st == DownloadStage.VERIFY, + height = 6.dp, done = !active && fill >= 1f, + ) + } + } + } +} + +private fun eta(seconds: Long): String = when { + seconds < 60 -> "<1 min" + seconds < 3600 -> "~${seconds / 60} min" + else -> "~${seconds / 3600} h ${(seconds % 3600) / 60} m" +} + +/** The queue's settings beside it: speed tier, downloads at a time, and the engine log. */ +@Composable +private fun DownloadSettings(s: FrontEndState, a: FrontEndActions) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + SectionTitle(stringResource(R.string.stores_download_manager), null) + Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth().clip(Shape12).background(colors.surface).border(1.dp, pal.line, Shape12).padding(horizontal = 14.dp, vertical = 10.dp)) { + DownloadControls(s, a) + } +} + +/** + * The queue's two knobs on one line - the speed tier on the left, downloads at a time on the + * right; the same control sits in the cog's popup. A narrow page puts them on two tight lines. + */ +@Composable +internal fun DownloadControls(s: FrontEndState, a: FrontEndActions) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + var parallel by remember { mutableStateOf(DownloadQueue.parallel(ctx)) } + val tier: @Composable () -> Unit = { + Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { + Text(stringResource(R.string.setup_stores_speed), fontSize = 12.sp, color = colors.onSurfaceVariant) + SegmentedTabs(StoreDownloadTier.ALL.map { it.id to stringResource(it.label) }, s.gameStoresSpeedTier) { a.onGameStoresSpeedTier(it) } + } + } + val count: @Composable () -> Unit = { + Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { + Text(stringResource(R.string.stores_parallel), fontSize = 12.sp, color = colors.onSurfaceVariant) + SegmentedTabs(listOf(1 to "1", 2 to "2", 3 to "3"), parallel) { parallel = it; DownloadQueue.setParallel(ctx, it) } + } + } + BoxWithConstraints(Modifier.fillMaxWidth()) { + if (maxWidth >= 460.dp) Row(horizontalArrangement = Arrangement.spacedBy(16.dp), verticalAlignment = Alignment.Bottom) { tier(); count() } + else Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { tier(); count() } + } +} + +/** + * The chip row's cog: the section's own settings in a small card - which tab a store opens on + * and the speed tier. Setup › Stores repeats them under its gate. Every install is added to Steam; + * that is not a choice. + */ +@Composable +internal fun StoresSettingsDialog(s: FrontEndState, a: FrontEndActions, onDismiss: () -> Unit) { + val shown = rememberShown(onDismiss) + val close = { shown.targetState = false } + AppDialog(shown, close, "storesSettings", wide = false, maxWidth = 440.dp) { + DialogHeader(stringResource(R.string.stores_settings_eyebrow), stringResource(R.string.stores_title)) + Rise(1) { + Column { + SettingsRow(stringResource(R.string.setup_stores_open_on), null) { + SegmentedTabs( + listOf(SessionPrefs.STORES_OPEN_LIBRARY to stringResource(R.string.stores_tab_library), SessionPrefs.STORES_OPEN_STORE to stringResource(R.string.stores_tab_store)), + s.storesOpenTab, + ) { a.onStoresOpenTab(it) } + } + SettingsRow(stringResource(R.string.stores_show_mature), null) { + ToggleSwitch(s.storesShowMature, label = stringResource(R.string.stores_show_mature)) { a.onStoresShowMature(it) } + } + // The queue's two knobs as one compact row, the same control the Downloads page has. + Column(Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 11.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { DownloadControls(s, a) } + } + } + Rise(2) { Actions { SecondaryButton(stringResource(R.string.common_ok), onClick = close) } } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresInstallDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresInstallDialog.kt new file mode 100644 index 0000000000..e7d645a033 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresInstallDialog.kt @@ -0,0 +1,109 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.foundation.LocalIndication +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.SdCard +import androidx.compose.material.icons.outlined.Smartphone +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.formatBytes + +/** + * "Install <game>:" over one row per place - internal storage, the card - each the action + * itself: icon, name, free space. Asked only when a card is there. The row picked last time + * starts focused, so a pad confirms the usual place with one press; nothing is chosen for the user. + */ +@Composable +internal fun InstallWhereDialog(item: CatalogItem, onDismiss: () -> Unit) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val shown = rememberShown(onDismiss) + val close = { shown.targetState = false } + val targets = remember { StoreInstallRoot.targets(ctx) } + val remembered = remember { SessionPrefs.storesInstallTarget(ctx) } + val defaultIndex = targets.indexOfFirst { it.root.absolutePath == remembered }.coerceAtLeast(0) + val focus = remember { List(targets.size) { FocusRequester() } } + LaunchedEffect(Unit) { focusWithinFrames({ false }) { focus[defaultIndex] } } + AppDialog(shown, close, "installWhere", wide = false, maxWidth = 440.dp) { + Rise(0) { + Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { + Text( + stringResource(R.string.stores_install_title, item.title), fontSize = 15.sp, color = colors.onBackground, + maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f), + ) + Text( + stringResource(R.string.common_cancel), fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = colors.onSurfaceVariant, + modifier = Modifier.clip(Shape12).clickable(role = Role.Button, onClick = close).padding(horizontal = 10.dp, vertical = 6.dp), + ) + } + } + Rise(1) { + Column(verticalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth()) { + targets.forEachIndexed { i, t -> + TargetRow( + label = if (t.removable) t.label else stringResource(R.string.stores_target_internal), + free = stringResource(R.string.stores_target_free, formatBytes(t.freeBytes)), + removable = t.removable, focus = focus[i], + ) { + SessionPrefs.setStoresInstallTarget(ctx, t.root.absolutePath) + StoresState.install(ctx, item, t.root) + close() + } + } + } + } + } +} + +@Composable +private fun TargetRow(label: String, free: String, removable: Boolean, focus: FocusRequester, onPick: () -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(14.dp), + modifier = Modifier.fillMaxWidth().focusRequester(focus).clip(Shape12) + .background(if (hot) pal.signal.copy(alpha = 0.14f) else colors.surface.copy(alpha = 0.6f)) + .glideBorder(hot, Shape12, pal.signal, pal.line) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onPick) + .controllerConfirm(onClick = onPick) + .padding(horizontal = 14.dp, vertical = 10.dp), + ) { + 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) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt new file mode 100644 index 0000000000..b9e648c0d0 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt @@ -0,0 +1,686 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.ui.layout.boundsInWindow +import androidx.compose.ui.layout.onGloballyPositioned +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.draw.drawWithContent +import androidx.compose.ui.draw.drawBehind +import androidx.compose.ui.graphics.Brush +import androidx.compose.ui.draw.clipToBounds +import androidx.compose.ui.draw.blur +import androidx.compose.foundation.layout.wrapContentHeight +import androidx.activity.compose.BackHandler +import androidx.compose.animation.core.Spring +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.foundation.LocalIndication +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.clickable +import androidx.compose.foundation.focusGroup +import androidx.compose.foundation.horizontalScroll +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.interaction.collectIsPressedAsState +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.aspectRatio +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Check +import androidx.compose.material.icons.filled.PlayArrow +import androidx.compose.material.icons.filled.Settings +import androidx.compose.material.icons.outlined.Download +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.key +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.input.ImeAction +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.text.style.TextDecoration +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import coil.compose.AsyncImage +import com.droiddeck.launcher.R +import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.CatalogItem +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoresState +import com.droiddeck.launcher.stores.download.DownloadStage +import com.droiddeck.launcher.stores.download.DownloadState +import com.droiddeck.launcher.stores.formatBytes + +// The Stores section: GOG, Epic Games and Amazon Games libraries and storefronts, and one +// download queue for the three, in a single full-width pane. + +/** The chip row's fourth entry, beside the three stores. */ +private const val DOWNLOADS = "downloads" + +private val TABS = listOf("store", "library") + +/** + * The tabs a store has: Store and Library (whose dropdown filters All / Installed). Amazon has no + * public catalog, so Library alone. + */ +internal fun tabsFor(store: Store?): List<String> = if (store == Store.AMAZON) listOf("library") else TABS + +/** [tab] if [store] has it, else Library (the open-on Store choice, or an old Installed / All, falls back there). */ +internal fun tabFor(store: Store?, tab: String): String = if (tab in tabsFor(store)) tab else "library" + +@Composable +internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier) { + val ctx = LocalContext.current + val narrow = LocalNarrowPane.current + val padH = if (narrow) 14.dp else 22.dp + var chip by rememberSaveable { mutableStateOf(Store.GOG.id) } + var tab by rememberSaveable { mutableStateOf(if (SessionPrefs.storesOpenTab(ctx) == SessionPrefs.STORES_OPEN_STORE) "store" else "library") } + var query by rememberSaveable { mutableStateOf("") } + var openGame by rememberSaveable { mutableStateOf<String?>(null) } + var settings by rememberSaveable { mutableStateOf(false) } + LaunchedEffect(StoresState.openDownloads) { if (StoresState.openDownloads) { chip = DOWNLOADS; StoresState.openDownloads = false } } + val store = Store.byId(chip) + // A tab this store does not have (Store or All on Amazon) reads as Library. + val shownTab = tabFor(store, tab) + LaunchedEffect(Unit) { StoresState.refresh(ctx) } + // A store the account is signed into fills itself when its chip is on screen. + LaunchedEffect(chip, StoresState.accounts[store]) { if (store != null && StoresState.isSignedIn(store)) StoresState.open(ctx, store) } + // A pad's focus sits on something the page is about to replace - the card that opens a game, + // a tab's contents - and would be lost with it, leaving the next press to land on the rail. + // Each move says where focus goes next: into the game page's main action, back to the card + // it was opened from, or onto the first card of a tab or store just picked. + val ff = LocalFrontFocus.current + val inputMode = androidx.compose.ui.platform.LocalInputModeManager.current + var focusMove by remember { mutableStateOf<Pair<String, Int>?>(null) } + var lastOpened by remember { mutableStateOf<String?>(null) } + fun move(kind: String) { focusMove = kind to (focusMove?.second ?: 0) + 1 } + fun open(key: String) { lastOpened = key; openGame = key; move("detail") } + fun closeGame() { openGame = null; move("card") } + fun switchTab(to: String) { tab = to; move("first") } + // Picking a chip opens the tab Setup says (Library or Store); a tab the user switches to + // afterwards stays until another chip is picked. + val pickChip: (String) -> Unit = { id -> + chip = id; openGame = null; query = "" + if (id != DOWNLOADS) tab = tabFor(Store.byId(id), if (s.storesOpenTab == SessionPrefs.STORES_OPEN_STORE) "store" else "library") + move("first") + } + BackHandler(enabled = openGame != null) { closeGame() } + val scroll = rememberScrollState() + LaunchedEffect(chip, tab, openGame) { scroll.scrollTo(0) } + LaunchedEffect(focusMove, openGame) { + if (inputMode.inputMode != androidx.compose.ui.input.InputMode.Keyboard || ff == null) return@LaunchedEffect + val target = when { + openGame != null -> if (ff.primaryAttached > 0) ff.primary else ff.items["back:${store?.label}"] + focusMove?.first == "card" -> lastOpened?.let { ff.items["card:$it"] } ?: ff.firstTile.takeIf { ff.firstTileAttached > 0 } + focusMove?.first == "first" -> ff.firstTile.takeIf { ff.firstTileAttached > 0 } + else -> null + } ?: return@LaunchedEffect + var landed = false + focusWithinFrames({ landed }) { target.also { landed = runCatching { it.requestFocus() }.isSuccess } } + } + Column( + modifier = modifier.padding(horizontal = padH, vertical = if (narrow) 10.dp else 14.dp) + .bumpers( + onPrevious = { if (openGame == null && chip != DOWNLOADS) { val t = tabsFor(store); switchTab(t[(t.indexOf(shownTab) + t.size - 1) % t.size]) } }, + onNext = { if (openGame == null && chip != DOWNLOADS) { val t = tabsFor(store); switchTab(t[(t.indexOf(shownTab) + 1) % t.size]) } }, + ), + ) { + // The chip row stays put and paints nothing - the page's background shows through it. What + // scrolls is clipped at the row's lower edge and, once scrolled, faded out there: the + // content's own alpha, not a colour laid over it. + Box(Modifier.fillMaxWidth().padding(bottom = 8.dp)) { + Rise(0) { StoreChips(chip, s.storeDownloadsActive, onPick = pickChip, onSettings = { settings = true }) } + } + val fade = with(androidx.compose.ui.platform.LocalDensity.current) { 14.dp.toPx() } + Box( + Modifier.fillMaxWidth().weight(1f, fill = false).clipToBounds() + .graphicsLayer { compositingStrategy = androidx.compose.ui.graphics.CompositingStrategy.Offscreen } + .drawWithContent { + drawContent() + if (scroll.value > 0 && size.height > fade) drawRect( + Brush.verticalGradient(0f to Color.Transparent, fade / size.height to Color.Black), + blendMode = androidx.compose.ui.graphics.BlendMode.DstIn, + ) + }, + ) { + Column(modifier = Modifier.fillMaxWidth().verticalScroll(scroll).padding(top = 4.dp, bottom = 16.dp)) { + when { + chip == DOWNLOADS -> StoresDownloadsPane(s, a) + store == null -> {} + // Signed out, or a sign-in that ran out (a launch could not get its code): sign in again. + !StoresState.isSignedIn(store) || StoresState.expired[store] == true -> Rise(1) { SignInCard(store) } + // One focus group, so the pad walks the page's own controls - back, the hero's + // actions, the cards - and reaches the rail only with Left from them. + openGame != null -> Column(Modifier.fillMaxWidth().focusGroup()) { StoreGameDetail(store, openGame!!, s, a, onBack = { closeGame() }) } + else -> Storefront(store, shownTab, query, s, a, onTab = { switchTab(it) }, onQuery = { query = it }, onOpen = { open(it) }) + } + } + } + } + if (settings) StoresSettingsDialog(s, a) { settings = false } + StoresState.pendingInstall?.let { item -> InstallWhereDialog(item) { StoresState.pendingInstall = null } } +} + +// ---- the chip row ------------------------------------------------------------------------------- + +/** Four equal chips - the stores with a signed-in dot, Downloads with its count - and the cog. */ +@Composable +private fun StoreChips(selected: String, active: Int, onPick: (String) -> Unit, onSettings: () -> Unit) { + Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { + for (store in Store.entries) { + StoreChip( + label = store.shortLabel, on = selected == store.id, key = store.id, modifier = Modifier.weight(1f), + lead = { + val c = sourceColours(store.id) + Box(Modifier.size(12.dp).clip(CircleShape).background(c.dot).alpha(if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) 1f else 0.35f)) + }, + description = store.label + if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) "" else " " + stringResource(R.string.stores_chip_signed_out), + ) { onPick(store.id) } + } + StoreChip( + label = stringResource(R.string.stores_tab_downloads), on = selected == DOWNLOADS, key = DOWNLOADS, modifier = Modifier.weight(1f), + lead = { Icon(Icons.Outlined.Download, null, modifier = Modifier.size(18.dp)) }, + trail = if (active > 0) { { CountPill(active) } } else null, + ) { onPick(DOWNLOADS) } + IconChip(Icons.Filled.Settings, stringResource(R.string.stores_settings), onSettings) + } +} + +@Composable +private fun StoreChip( + label: String, on: Boolean, key: String, modifier: Modifier, lead: @Composable () -> Unit, + trail: (@Composable () -> Unit)? = null, description: String? = null, onClick: () -> Unit, +) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + val shape = RoundedCornerShape(99.dp) + val ink = if (on) colors.onBackground else if (hot) colors.onBackground else colors.onSurfaceVariant + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterHorizontally), + modifier = modifier.paneItem("storechip:$key").heightIn(min = 44.dp).clip(shape) + .background(if (on) pal.signal.copy(alpha = 0.16f) else colors.surface) + .glideBorder(hot, shape, pal.signal, if (on) pal.signal.copy(alpha = 0.7f) else pal.line) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Tab, onClick = onClick) + .controllerConfirm(onClick = onClick) + .then(if (description != null) Modifier.semantics { contentDescription = description } else Modifier) + .padding(horizontal = 12.dp, vertical = 8.dp), + ) { + androidx.compose.runtime.CompositionLocalProvider(androidx.compose.material3.LocalContentColor provides ink) { lead() } + Text(label, fontSize = 13.sp, fontWeight = FontWeight.Bold, color = ink, maxLines = 1, overflow = TextOverflow.Ellipsis) + trail?.invoke() + } +} + +/** The chip row's cog: icon only, the chips' height; opens the section's settings. */ +@Composable +private fun IconChip(icon: androidx.compose.ui.graphics.vector.ImageVector, description: String, onClick: () -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + Box( + contentAlignment = Alignment.Center, + modifier = Modifier.paneItem("storechip:settings").size(44.dp).clip(CircleShape).background(colors.surface) + .glideBorder(hot, CircleShape, pal.signal, pal.line) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onClick) + .controllerConfirm(onClick = onClick), + ) { Icon(icon, description, tint = if (hot) pal.signal else colors.onBackground, modifier = Modifier.size(20.dp)) } +} + +@Composable +internal fun CountPill(count: Int) { + val pal = LocalPalette.current + Text( + count.toString(), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = pal.onSignal, maxLines = 1, + modifier = Modifier.clip(RoundedCornerShape(8.dp)).background(pal.signal).padding(horizontal = 6.dp, vertical = 1.dp), + ) +} + +// ---- sign-in ----------------------------------------------------------------------------------- + +/** No account for this store yet: who it signs in as, and the button that opens its login page. */ +@Composable +private fun SignInCard(store: Store) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val narrow = LocalNarrowPane.current + Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(18.dp), + modifier = Modifier.fillMaxWidth().clip(Shape16).background(colors.surface).border(1.dp, pal.line, Shape16).padding(18.dp), + ) { + StoreLogo(store, 56.dp) + Column(modifier = Modifier.weight(1f)) { + Text(store.label, fontSize = 18.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground) + } + if (!narrow) PrimaryButton(stringResource(R.string.stores_signin), main = true) { StoresState.signIn(ctx, store) } + } + if (narrow) Actions { PrimaryButton(stringResource(R.string.stores_signin), main = true) { StoresState.signIn(ctx, store) } } + } +} + +/** A square in the store's colour with its name on it: the stores ship no logo this app may bundle. */ +@Composable +internal fun StoreLogo(store: Store, size: Dp) { + val c = sourceColours(store.id) + Box(contentAlignment = Alignment.Center, modifier = Modifier.size(size).clip(Shape14).background(c.fill).border(1.dp, c.dot.copy(alpha = 0.5f), Shape14)) { + Text(store.shortLabel, fontSize = (size.value / 4).sp, fontWeight = FontWeight.Black, color = c.ink, maxLines = 1) + } +} + +// ---- storefront -------------------------------------------------------------------------------- + +@Composable +private fun Storefront( + store: Store, tab: String, query: String, s: FrontEndState, a: FrontEndActions, + onTab: (String) -> Unit, onQuery: (String) -> Unit, onOpen: (String) -> Unit, +) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val library = StoresState.library[store] ?: emptyList() + val shelves = StoresState.shelves[store] + // The Installed tab, its count and every card's installed mark come from the installs on disk. + val installedItems = remember(StoresState.installed, library) { com.droiddeck.launcher.stores.installedCards(store, StoresState.installed, library) } + val installedKeys = remember(installedItems) { installedItems.map { it.id }.toSet() + StoresState.installed.filter { it.sidecar.store == store }.map { it.sidecar.id } } + val q = query.trim().lowercase() + fun matches(i: CatalogItem) = q.isEmpty() || i.title.lowercase().contains(q) + // The storefront leaves out what the store rates or tags as adult unless the user shows it; owned games always show. + fun shown(i: CatalogItem) = s.storesShowMature || !i.mature || i.owned + fun shelf(l: List<CatalogItem>?) = l?.filter(::shown) + // The Store tab's search: the catalog the shelves brought and the library, each title once. + val everything = remember(library, shelves) { (library + (shelves?.all ?: emptyList())).distinctBy { it.id } } + // Library's dropdown: All (every owned game, the default) or Installed. + var installedOnly by rememberSaveable(store) { mutableStateOf(false) } + var filterOpen by remember { mutableStateOf(false) } + val libraryLabel = if (installedOnly) stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_installed), installedItems.size) + else stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_library), library.size) + Rise(1) { + SubTabs( + listOf("store" to stringResource(R.string.stores_tab_store), "library" to libraryLabel).filter { it.first in tabsFor(store) }, + tab, + onPick = { key -> if (key == "library") { if (tab == "library") filterOpen = !filterOpen else onTab(key) } else onTab(key) }, + menuFor = { key, tabWidth -> + if (key == "library") AnchoredMenu(filterOpen, onDismiss = { filterOpen = false }, compact = true, minWidth = tabWidth) { first -> + MenuItem(stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_all), library.size), checked = !installedOnly, focusRequester = first) { installedOnly = false; filterOpen = false; onTab("library") } + MenuItem(stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_installed), installedItems.size), checked = installedOnly) { installedOnly = true; filterOpen = false; onTab("library") } + } + }, + trailing = { + // A purchase made elsewhere shows on a refresh; otherwise the library refreshes itself every six hours. + SmallTextButton(stringResource(R.string.stores_refresh)) { StoresState.open(ctx, store, force = true) } + val name = StoresState.accounts[store] ?: "" + SmallTextButton(stringResource(R.string.stores_signout_named, name)) { StoresState.signOut(ctx, store) } + }, + ) + } + Rise(2) { + SearchField( + query, onQuery, + placeholder = when { + tab == "store" -> stringResource(R.string.stores_search_catalog, store.label) + installedOnly -> stringResource(R.string.stores_search_installed) + else -> stringResource(R.string.stores_search_library) + }, + ) + } + StoresState.status[store]?.let { line -> Rise(3) { SyncBar(line) } } + StoresState.problems[store]?.let { Rise(3) { Box(Modifier.padding(bottom = 8.dp)) { Note(it) } } } + // The first card drawn on the tab is where a pad lands after a tab or chip change. + var firstPlaced = false + val card: @Composable (CatalogItem) -> Unit = { item -> + val first = !firstPlaced + firstPlaced = true + GameCard(item, store, s, a, installedKeys, onOpen, first) + } + when (tab) { + "library" -> if (installedOnly) Grid(installedItems.filter(::matches), stringResource(R.string.stores_installed_empty), card) + else Grid(library.filter(::matches), if (library.isEmpty() && StoresState.status[store] != null) stringResource(R.string.stores_library_loading) else stringResource(R.string.stores_nothing_matches), card) + else -> { + if (q.isNotEmpty()) Grid(everything.filter { matches(it) && shown(it) }, stringResource(R.string.stores_nothing_matches), card) + else { + Shelf(stringResource(R.string.stores_shelf_new), shelf(shelves?.whatsNew), card) + Shelf(stringResource(R.string.stores_shelf_deals), shelf(shelves?.deals), card) + Shelf(stringResource(R.string.stores_shelf_free), shelf(shelves?.free), card) + Shelf(stringResource(R.string.stores_shelf_trending), shelf(shelves?.trending), card) + Shelf(stringResource(R.string.stores_shelf_library), library, card) + if (shelves == null && library.isEmpty()) Rise(4) { Note(if (StoresState.status[store] != null) stringResource(R.string.stores_library_loading) else stringResource(R.string.stores_shelves_loading)) } + else if (shelves != null && shelves.isEmpty && store == Store.AMAZON) Rise(4) { Note(stringResource(R.string.stores_amazon_no_catalog)) } + } + } + } +} + +/** Underlined tabs as the preview draws them, with [trailing] (the account) at the right. */ +@Composable +private fun SubTabs( + tabs: List<Pair<String, String>>, selected: String, onPick: (String) -> Unit, + menuFor: @Composable (String, androidx.compose.ui.unit.Dp) -> Unit = { _, _ -> }, trailing: @Composable () -> Unit, +) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + Column(Modifier.fillMaxWidth()) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth()) { + Row(horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.weight(1f).horizontalScroll(rememberScrollState())) { + for ((key, label) in tabs) { + val on = key == selected + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + val pick = { onPick(key) } + val density = androidx.compose.ui.platform.LocalDensity.current + var width by remember { mutableStateOf(0.dp) } + Box(Modifier.onSizeChanged { width = with(density) { it.width.toDp() } }) { + Column( + modifier = Modifier.paneItem("storetab:$key").clip(RoundedCornerShape(8.dp)) + .background(if (hot) pal.signal.copy(alpha = 0.12f) else Color.Transparent) + .glideBorder(hot, RoundedCornerShape(8.dp), pal.signal) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Tab, onClick = pick) + .controllerConfirm(onClick = pick) + .padding(horizontal = 8.dp, vertical = 6.dp), + ) { + Text(label, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = if (on) colors.onBackground else colors.onSurfaceVariant, maxLines = 1) + Box(Modifier.padding(top = 4.dp).fillMaxWidth().height(2.dp).background(if (on) pal.signal else Color.Transparent)) + } + // A tab's own dropdown (Library's filter), anchored under it. + menuFor(key, width) + } + } + } + trailing() + } + Box(Modifier.fillMaxWidth().height(1.dp).background(pal.line)) + } +} + +@Composable +private fun SmallTextButton(text: String, onClick: () -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + Text( + text, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = if (hot) colors.onBackground else colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, + modifier = Modifier.paneItem("btn:$text").clip(RoundedCornerShape(8.dp)).glideBorder(hot, RoundedCornerShape(8.dp), pal.signal) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onClick) + .controllerConfirm(onClick = onClick).padding(horizontal = 8.dp, vertical = 6.dp), + ) +} + +@Composable +private fun SearchField(query: String, onQuery: (String) -> Unit, placeholder: String) { + AdbTextField( + value = query, onValueChange = onQuery, label = "", keyboardType = KeyboardType.Text, imeAction = ImeAction.Search, + placeholder = placeholder, compact = true, modifier = Modifier.padding(top = 8.dp, bottom = 4.dp), + ) +} + +/** A horizontal row of cards under a title; nothing at all when the list is empty or not here yet. */ +@Composable +private fun Shelf(title: String, items: List<CatalogItem>?, card: @Composable (CatalogItem) -> Unit) { + if (items.isNullOrEmpty()) return + val colors = MaterialTheme.colorScheme + Text(title, fontSize = 16.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, modifier = Modifier.padding(top = 14.dp, bottom = 6.dp)) + Row(horizontalArrangement = Arrangement.spacedBy(10.dp), modifier = Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()).padding(vertical = 2.dp)) { + for (item in items.take(24)) key(item.key) { Box(Modifier.width(CardWidth)) { card(item) } } + } +} + +/** Cards in rows that fill the width; [empty] when there are none. */ +@Composable +private fun Grid(items: List<CatalogItem>, empty: String, card: @Composable (CatalogItem) -> Unit) { + if (items.isEmpty()) { Box(Modifier.padding(vertical = 32.dp).fillMaxWidth(), contentAlignment = Alignment.Center) { Text(empty, fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) }; return } + BoxWithConstraints(Modifier.fillMaxWidth().padding(top = 10.dp)) { + val gap = 10.dp + val cols = ((maxWidth + gap) / (CardWidth + gap)).toInt().coerceAtLeast(1) + val w = (maxWidth - gap * (cols - 1)) / cols + Column(verticalArrangement = Arrangement.spacedBy(gap)) { + for (row in items.chunked(cols)) Row(horizontalArrangement = Arrangement.spacedBy(gap), modifier = Modifier.fillMaxWidth()) { + for (item in row) key(item.key) { Box(Modifier.width(w)) { card(item) } } + } + } + } +} + +/** + * A card's least width: shelf cards are this wide and a grid fits as many columns as the width + * allows at it (the columns then share the leftover), so a display-density setting changes the + * column count rather than the cards' size. 25% under the first 150 dp: five columns where there + * were four on the Pocket FIT. + */ +private val CardWidth = 112.dp + +/** + * One title: its wide art, the title, a price or status line and one button that says the one + * thing to do next - Install with its size, Play, the download's progress, Get for free, or View + * on the store. Tapping the card itself opens the game's page. + */ +@Composable +private fun GameCard(item: CatalogItem, store: Store, s: FrontEndState, a: FrontEndActions, installedKeys: Set<String>, onOpen: (String) -> Unit, first: Boolean = false) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + val pressed by src.collectIsPressedAsState() + val scale by animateFloatAsState(if (pressed) 0.985f else 1f, Motion.sp(0.5f, Spring.StiffnessMedium), label = "cardScale") + val installed = item.id in installedKeys + val download = StoresState.download("${store.id}:${item.id}")?.takeIf { it.isActive } + val open = { onOpen(item.key) } + // A size the library did not give is looked up once the card is on screen or close to it. + val screen = with(androidx.compose.ui.platform.LocalDensity.current) { androidx.compose.ui.platform.LocalConfiguration.current.screenHeightDp.dp.toPx() } + val ahead = screen / 2 + Column( + modifier = Modifier.fillMaxWidth() + .onGloballyPositioned { c -> + if (item.owned && item.sizeBytes <= 0) { + val b = c.boundsInWindow() + if (b.bottom > -ahead && b.top < screen + ahead && b.width > 0f) com.droiddeck.launcher.stores.StoreSizes.request(ctx, item) + } + } + .paneItem("card:${item.key}").then(if (first) Modifier.firstTile() else Modifier).graphicsLayer { scaleX = scale; scaleY = scale } + .clip(RoundedCornerShape(10.dp)).glideBorder(hot, RoundedCornerShape(10.dp), pal.signal, pal.line) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, onClick = open).controllerConfirm(onClick = open), + ) { + CardFace(item, download) { CardMarker(item, installed, download) } + } +} + +/** A store's sync as a thin bar and its count ("25/55"); a moving bar when it has no count yet. */ +@Composable +private fun SyncBar(line: String) { + val count = Regex("(\\d+)\\s*/\\s*(\\d+)").find(line) + val done = count?.groupValues?.get(1)?.toIntOrNull() + val total = count?.groupValues?.get(2)?.toIntOrNull()?.takeIf { it > 0 } + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp)) { + Box(Modifier.weight(1f)) { + if (done != null && total != null) androidx.compose.material3.LinearProgressIndicator(progress = { (done.toFloat() / total).coerceIn(0f, 1f) }, modifier = Modifier.fillMaxWidth().height(3.dp)) + else androidx.compose.material3.LinearProgressIndicator(modifier = Modifier.fillMaxWidth().height(3.dp)) + } + if (done != null && total != null) Text("$done/$total", fontSize = 11.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1) + } +} + +/** How tall the card's one line is, over the blurred foot of its art. */ +private val CardStrip = 26.dp + +/** + * The card is its art: sharp above, and under the one line a blurred, darkened copy of the same + * picture, placed so the two meet without a seam. Blur needs API 31; below it the copy is only + * darkened, more strongly. + */ +@Composable +private fun CardFace(item: CatalogItem, download: com.droiddeck.launcher.stores.download.DownloadEntry?, marker: @Composable () -> Unit) { + val url = item.imageUrl ?: item.tallImageUrl + val blurs = android.os.Build.VERSION.SDK_INT >= 31 + BoxWithConstraints(Modifier.fillMaxWidth()) { + val total = maxWidth * 9f / 16f + CardStrip + Box(Modifier.fillMaxWidth().height(total).background(Color(0xFF101318)).then(if (url == null) Modifier.background(artBrush(hueOf(item.title))) else Modifier)) { + if (url != null) AsyncImage(model = url, contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier.matchParentSize()) + Box(Modifier.align(Alignment.BottomCenter).fillMaxWidth().height(CardStrip).clipToBounds()) { + if (url != null) AsyncImage( + model = url, contentDescription = null, contentScale = ContentScale.Crop, + modifier = Modifier.fillMaxWidth().wrapContentHeight(Alignment.Bottom, unbounded = true).height(total).blur(24.dp), + ) + Spacer(Modifier.matchParentSize().background(Color.Black.copy(alpha = if (blurs) 0.45f else 0.7f))) + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), + modifier = Modifier.matchParentSize().padding(horizontal = 8.dp), + ) { + Text(item.title, fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f)) + marker() + } + } + // A soft edge where the sharp art meets the strip. + Spacer(Modifier.align(Alignment.BottomCenter).padding(bottom = CardStrip).fillMaxWidth().height(14.dp).background(Brush.verticalGradient(0f to Color.Transparent, 1f to Color.Black.copy(alpha = 0.35f)))) + if (download != null) Box(Modifier.align(Alignment.BottomCenter).padding(start = 6.dp, end = 6.dp, bottom = CardStrip + 5.dp)) { + ProgressBarThin(download.fraction, paused = download.state == DownloadState.PAUSED, verify = download.stage == DownloadStage.VERIFY) + } + } + } +} + +/** The card's right-hand value: done, resume, progress, size, or the price. */ +@Composable +private fun CardMarker(item: CatalogItem, installed: Boolean, download: com.droiddeck.launcher.stores.download.DownloadEntry?) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val small = 10.sp + when { + // Installed: its size on disk and a small check after it. + installed -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(3.dp)) { + val ctx = LocalContext.current + val game = StoresState.installedGame(item.store, item.id) + if (game != null) androidx.compose.runtime.LaunchedEffect(item.key) { com.droiddeck.launcher.stores.StoreSizes.requestDisk(ctx, item.key, game) } + com.droiddeck.launcher.stores.StoreSizes.onDisk[item.key]?.let { Text(formatBytes(it), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1) } + Icon(Icons.Filled.Check, stringResource(R.string.stores_installed_chip), tint = pal.good, modifier = Modifier.size(11.dp)) + } + download != null -> Text("${download.percent}%", fontSize = small, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1) + item.owned && StoresState.isUnfinished(item) -> Text(stringResource(R.string.stores_resume), fontSize = small, fontWeight = FontWeight.Bold, color = pal.signal, maxLines = 1) + // The size in the title's own type; nothing until it is known. + item.owned -> com.droiddeck.launcher.stores.StoreSizes.size(item).takeIf { it > 0 }?.let { Text(formatBytes(it), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1) } + item.isFree -> Text(stringResource(R.string.stores_free), fontSize = small, fontWeight = FontWeight.Bold, color = pal.good, maxLines = 1) + item.isDiscounted -> Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp)) { + StateChip("-${item.discountPercent}%", ChipTone.DEAL, small = true) + Text(item.finalPrice, fontSize = small, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1) + } + item.hasPrice -> Text(item.finalPrice, fontSize = small, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1) + } +} + +@Composable +internal fun CardArt(item: CatalogItem, modifier: Modifier) { + val url = item.imageUrl ?: item.tallImageUrl + val colors = MaterialTheme.colorScheme + // A dark ground under the image, so art with transparency never ends in white, and a quiet + // fade into the card's surface at the foot. + Box(modifier.background(colors.surfaceVariant).then(if (url == null) Modifier.background(artBrush(hueOf(item.title))) else Modifier)) { + if (url != null) AsyncImage(model = url, contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier.matchParentSize()) + else Text(item.title, fontSize = 10.sp, fontWeight = FontWeight.Black, color = Color.White, maxLines = 3, overflow = TextOverflow.Ellipsis, modifier = Modifier.align(Alignment.BottomStart).padding(6.dp)) + Spacer(Modifier.matchParentSize().background(androidx.compose.ui.graphics.Brush.verticalGradient(0.6f to Color.Transparent, 1f to colors.surface.copy(alpha = 0.85f)))) + } +} + +@Composable +internal fun ProgressBarThin(fraction: Float, paused: Boolean = false, verify: Boolean = false, height: Dp = 4.dp, done: Boolean = false) { + val pal = LocalPalette.current + val colors = MaterialTheme.colorScheme + val fill = when { done -> pal.good; paused -> Color(0xFFFFC24D); verify -> Color(0xFF9B6DFF); else -> pal.signal } + // The fill glides to each new value over a quarter second - progress arrives a few times a second + // and in steps - and is read only while drawing, so the bar animates without relayout. + val shown = androidx.compose.animation.core.animateFloatAsState( + fraction.coerceIn(0f, 1f), androidx.compose.animation.core.tween(250, easing = androidx.compose.animation.core.LinearEasing), label = "bar", + ) + Box( + Modifier.fillMaxWidth().height(height).clip(RoundedCornerShape(height / 2)).background(colors.surfaceVariant) + .drawBehind { drawRect(fill, size = androidx.compose.ui.geometry.Size(size.width * shown.value, size.height)) }, + ) +} + +@Composable +internal fun downloadLabel(d: com.droiddeck.launcher.stores.download.DownloadEntry): String = when (d.state) { + DownloadState.PAUSED -> stringResource(R.string.stores_dl_paused) + DownloadState.QUEUED -> stringResource(R.string.stores_dl_queued) + else -> if (d.stageFraction >= 0f) stringResource(R.string.stores_dl_active_percent, activeStageLabel(d), d.percent) else activeStageLabel(d) +} + +/** The active stage as the row and the page name it: Checking (files already there), Downloading, Verifying, Installing. */ +@Composable +internal fun activeStageLabel(d: com.droiddeck.launcher.stores.download.DownloadEntry): String = when (d.stage) { + DownloadStage.MANIFEST -> if (d.stageItemsTotal > 0) stringResource(R.string.stores_stage_checking) else stringResource(R.string.stores_stage_manifest) + DownloadStage.DOWNLOAD -> stringResource(R.string.stores_stage_downloading) + DownloadStage.VERIFY -> stringResource(R.string.stores_stage_verifying) + DownloadStage.INSTALL -> stringResource(R.string.stores_stage_installing) + DownloadStage.DONE -> stringResource(R.string.stores_stage_done) +} + +/** Two taps: the first arms it ([confirm] shows), the second acts; it disarms itself after a few seconds. */ +@Composable +internal fun ConfirmButton(label: String, confirm: String, compact: Boolean = false, onConfirm: () -> Unit) { + var armed by remember { mutableStateOf(false) } + LaunchedEffect(armed) { if (armed) { kotlinx.coroutines.delay(4000); armed = false } } + SecondaryButton(if (armed) confirm else label, compact = compact) { if (!armed) armed = true else { armed = false; onConfirm() } } +} + +@Composable +internal fun stageLabel(stage: DownloadStage): String = when (stage) { + DownloadStage.MANIFEST -> stringResource(R.string.stores_stage_manifest) + DownloadStage.DOWNLOAD -> stringResource(R.string.stores_stage_download) + DownloadStage.VERIFY -> stringResource(R.string.stores_stage_verify) + DownloadStage.INSTALL -> stringResource(R.string.stores_stage_install) + DownloadStage.DONE -> stringResource(R.string.stores_stage_done) +} + +/** The game's web page, in the device's browser: this app buys nothing and claims nothing itself. */ +internal fun openStoreUrl(ctx: android.content.Context, item: CatalogItem) { + val url = item.storeUrl.ifBlank { return } + runCatching { ctx.startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(url)).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK)) } +} + +/** + * Launches an installed store game the way the Games tab does: through its Steam shortcut. The + * shortcut exists once the client has (re)started since the install; until then the user is told. + */ +internal fun launchStoreGame(ctx: android.content.Context, store: Store, id: String, s: FrontEndState, a: FrontEndActions) { + val game = s.steamGames.firstOrNull { it.source == store.id && it.storeId == id } + if (game == null) { + android.widget.Toast.makeText(ctx, R.string.stores_launch_not_registered, android.widget.Toast.LENGTH_LONG).show() + return + } + com.droiddeck.launcher.stores.StoreLaunch.prepare(ctx, store, id) { a.onSteamGame(game) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresWidgets.kt new file mode 100644 index 0000000000..b6d15dc61c --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresWidgets.kt @@ -0,0 +1,20 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.stringResource +import com.droiddeck.launcher.R +import com.droiddeck.launcher.stores.StoresNative + +// Controls the Stores section and its Setup group share. + +/** Whether the native download engine is in this build, said where the stores are turned on. */ +@Composable +internal fun StoresEngineRow() { + val version = StoresNative.version + SettingsRow( + stringResource(R.string.setup_stores_engine), + version, + ) { + Chip(if (version != null) stringResource(R.string.setup_stores_engine_chip_ready) else stringResource(R.string.setup_stores_engine_chip_missing), ok = version != null) + } +} diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 5d391727e1..f346d1137f 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -902,11 +902,8 @@ <string name="games_manage_saves">Gestionar partidas guardadas</string> <string name="games_saves">Partidas guardadas</string> <string name="games_import_saves">Importar partidas guardadas…</string> - <string name="games_import_saves_hint">Zip de GameHub o Winlator. Antes se hace una copia de las partidas actuales.</string> <string name="games_export_gamehub">Exportar como zip de GameHub…</string> - <string name="games_export_gamehub_hint">Para GameHub y BannerHub</string> <string name="games_export_winlator">Exportar como zip de Winlator…</string> - <string name="games_export_winlator_hint">Para Winlator, WinNative y Bannerlator</string> <string name="games_open_save_folder">Abrir carpeta de guardado</string> <string name="games_never_played">Nunca jugado</string> <string name="games_runtime_busy_percent">Runtime ocupado · %1$d%%</string> diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 994372ea05..07a3c7f417 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -793,11 +793,8 @@ <string name="games_manage_saves">Gérer les sauvegardes</string> <string name="games_saves">Sauvegardes de jeu</string> <string name="games_import_saves">Importer des sauvegardes…</string> - <string name="games_import_saves_hint">Archive GameHub ou Winlator. Les sauvegardes actuelles seront sauvegardées avant l\'importation.</string> <string name="games_export_gamehub">Exporter en tant qu\'archive GameHub…</string> - <string name="games_export_gamehub_hint">Compatible avec GameHub et BannerHub</string> <string name="games_export_winlator">Exporter en tant qu\'archive Winlator…</string> - <string name="games_export_winlator_hint">Compatible avec Winlator, WinNative et Bannerlator</string> <string name="games_open_save_folder">Ouvrir le dossier de sauvegarde</string> <string name="games_never_played">Jamais lancé</string> <string name="games_runtime_busy_percent">Environnement d\'exécution occupé · %1$d%%</string> diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index eb417a2ebc..e2f18b9f6d 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -894,11 +894,8 @@ <string name="games_manage_saves">セーブデータの管理</string> <string name="games_saves">ゲームセーブデータ</string> <string name="games_import_saves">セーブデータをインポート…</string> - <string name="games_import_saves_hint">GameHubまたはWinlatorのアーカイブ。インポート前に現在のセーブデータをバックアップします。</string> <string name="games_export_gamehub">GameHubアーカイブとしてエクスポート…</string> - <string name="games_export_gamehub_hint">GameHubおよびBannerHubに適しています</string> <string name="games_export_winlator">Winlatorアーカイブとしてエクスポート…</string> - <string name="games_export_winlator_hint">Winlator、WinNative、Bannerlatorに適しています</string> <string name="games_open_save_folder">セーブフォルダを開く</string> <string name="games_never_played">未プレイ</string> <string name="games_runtime_busy_percent">実行環境がビジー · %1$d%%</string> diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index 768d09d280..a4fca82559 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -894,11 +894,8 @@ <string name="games_manage_saves">저장 데이터 관리</string> <string name="games_saves">게임 저장 데이터</string> <string name="games_import_saves">저장 데이터 가져오기…</string> - <string name="games_import_saves_hint">GameHub 또는 Winlator 아카이브. 가져오기 전에 현재 저장 데이터를 백업합니다.</string> <string name="games_export_gamehub">GameHub 아카이브로 내보내기…</string> - <string name="games_export_gamehub_hint">GameHub 및 BannerHub에 적합</string> <string name="games_export_winlator">Winlator 아카이브로 내보내기…</string> - <string name="games_export_winlator_hint">Winlator, WinNative 및 Bannerlator에 적합</string> <string name="games_open_save_folder">저장 폴더 열기</string> <string name="games_never_played">플레이한 적 없음</string> <string name="games_runtime_busy_percent">런타임 사용 중 · %1$d%%</string> diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 595673f172..0863f32653 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -877,11 +877,8 @@ <string name="games_manage_saves">Управление сохранениями</string> <string name="games_saves">Сохранения игры</string> <string name="games_import_saves">Импорт сохранений…</string> - <string name="games_import_saves_hint">Архив GameHub или Winlator. Перед импортом текущие сохранения будут сохранены в резервной копии.</string> <string name="games_export_gamehub">Экспорт как архив GameHub…</string> - <string name="games_export_gamehub_hint">Подходит для GameHub и BannerHub</string> <string name="games_export_winlator">Экспорт как архив Winlator…</string> - <string name="games_export_winlator_hint">Подходит для Winlator, WinNative и Bannerlator</string> <string name="games_open_save_folder">Открыть папку сохранений</string> <string name="games_never_played">Никогда не запускалась</string> <string name="games_runtime_busy_percent">Среда выполнения занята · %1$d%%</string> diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 0bb996a21c..ea26e2ddde 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -894,11 +894,8 @@ <string name="games_manage_saves">管理存档</string> <string name="games_saves">游戏存档</string> <string name="games_import_saves">导入存档…</string> - <string name="games_import_saves_hint">GameHub 或 Winlator 的 zip 包。会先备份当前存档。</string> <string name="games_export_gamehub">导出为 GameHub zip 包…</string> - <string name="games_export_gamehub_hint">用于 GameHub 和 BannerHub</string> <string name="games_export_winlator">导出为 Winlator zip 包…</string> - <string name="games_export_winlator_hint">用于 Winlator、WinNative 和 Bannerlator</string> <string name="games_open_save_folder">打开存档文件夹</string> <string name="games_never_played">从未游玩</string> <string name="games_runtime_busy_percent">运行时繁忙 · %1$d%%</string> diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index cfbd40670a..c236140ce2 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -894,11 +894,8 @@ <string name="games_manage_saves">管理存檔</string> <string name="games_saves">遊戲存檔</string> <string name="games_import_saves">匯入存檔…</string> - <string name="games_import_saves_hint">GameHub 或 Winlator zip。會先備份目前的存檔。</string> <string name="games_export_gamehub">匯出為 GameHub zip…</string> - <string name="games_export_gamehub_hint">適用於 GameHub 和 BannerHub</string> <string name="games_export_winlator">匯出為 Winlator zip…</string> - <string name="games_export_winlator_hint">適用於 Winlator、WinNative 和 Bannerlator</string> <string name="games_open_save_folder">開啟存檔資料夾</string> <string name="games_never_played">從未玩過</string> <string name="games_runtime_busy_percent">運行環境繁忙 · %1$d%%</string> diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index da2eaeb393..f7372765c1 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -894,11 +894,8 @@ <string name="games_manage_saves">管理存檔</string> <string name="games_saves">遊戲存檔</string> <string name="games_import_saves">匯入存檔…</string> - <string name="games_import_saves_hint">GameHub 或 Winlator 壓縮檔。匯入前會先備份目前存檔。</string> <string name="games_export_gamehub">匯出為 GameHub 壓縮檔…</string> - <string name="games_export_gamehub_hint">適用於 GameHub 和 BannerHub</string> <string name="games_export_winlator">匯出為 Winlator 壓縮檔…</string> - <string name="games_export_winlator_hint">適用於 Winlator、WinNative 和 Bannerlator</string> <string name="games_open_save_folder">開啟存檔資料夾</string> <string name="games_never_played">從未執行</string> <string name="games_runtime_busy_percent">執行環境繁忙 · %1$d%%</string> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 34725bd440..b12ca68dba 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -904,11 +904,8 @@ <string name="games_manage_saves">Manage saves</string> <string name="games_saves">Game saves</string> <string name="games_import_saves">Import saves…</string> - <string name="games_import_saves_hint">GameHub or Winlator zip. Current saves are backed up first.</string> <string name="games_export_gamehub">Export as GameHub zip…</string> - <string name="games_export_gamehub_hint">For GameHub and BannerHub</string> <string name="games_export_winlator">Export as Winlator zip…</string> - <string name="games_export_winlator_hint">For Winlator, WinNative and Bannerlator</string> <string name="games_open_save_folder">Open save folder</string> <string name="games_never_played">Never played</string> <string name="games_runtime_busy_percent">Runtime busy · %1$d%%</string> @@ -919,6 +916,8 @@ <string name="games_last_played">Last played %1$s</string> <string name="games_internal">Internal storage</string> <string name="games_added">Added game</string> + <string name="games_custom">Custom game</string> + <string name="games_store_storage">Games storage</string> <string name="gpu_auto_hint">The best pair for this GPU, kept up to date</string> <string name="gpu_manual_hint">Pick a pair yourself</string> <string name="gpu_checking">Checking for drivers…</string> @@ -1949,4 +1948,162 @@ <string name="device_info_copied">Copied.</string> <string name="force_ssbs_title">Force SSBS</string> <string name="force_ssbs_hint">Keeps a CPU speed setting on that Wine keeps turning off. Can make Windows games faster on Snapdragon 8 Elite; little change seen on older chips. Applies on the next game launch.</string> + + <!-- Stores: GOG, Epic Games and Amazon Games --> + <string name="rail_stores">Stores</string> + <string name="source_steam">Steam</string> + <string name="source_added">Custom</string> + <string name="setup_stores">Stores</string> + <string name="setup_stores_show">Show Stores in the rail</string> + <string name="setup_stores_open_on">Open a store on</string> + <string name="setup_stores_speed">Download speed tier</string> + <string name="setup_stores_engine">Stores engine</string> + <string name="setup_stores_engine_chip_ready">● Engine ready</string> + <string name="setup_stores_engine_chip_missing">Not built</string> + <string name="stores_tier_balanced">Balanced</string> + <string name="stores_tier_fast">Fast</string> + <string name="stores_tier_max">Max</string> + <string name="stores_title">Stores</string> + <string name="stores_settings">Stores settings</string> + <string name="stores_settings_eyebrow">Settings</string> + <string name="stores_chip_signed_out">(not signed in)</string> + <string name="stores_tab_store">Store</string> + <string name="stores_tab_installed">Installed</string> + <string name="stores_tab_library">Library</string> + <string name="stores_tab_all">All</string> + <string name="stores_tab_downloads">Downloads</string> + <string name="stores_tab_count">%1$s (%2$d)</string> + <string name="stores_signin">Sign in</string> + <string name="stores_signout_named">%1$s · sign out</string> + <string name="stores_search_catalog">Search the %1$s catalog…</string> + <string name="stores_search_installed">Search installed games…</string> + <string name="stores_search_library">Search your library…</string> + <string name="stores_installed_empty">Nothing installed</string> + <string name="stores_library_loading">Loading</string> + <string name="stores_nothing_matches">No games</string> + <string name="stores_shelves_loading">Loading the store…</string> + <string name="stores_amazon_no_catalog">No catalog</string> + <string name="stores_shelf_new">What\'s new</string> + <string name="stores_shelf_deals">Deals</string> + <string name="stores_shelf_free">Free games</string> + <string name="stores_shelf_trending">Trending</string> + <string name="stores_shelf_library">Your library</string> + <string name="stores_play">Play</string> + <string name="stores_install">Install</string> + <string name="stores_resume_install">Resume install</string> + <string name="stores_install_size">Install · %1$s</string> + <string name="stores_get_free">Get for free</string> + <string name="stores_view_on">View on %1$s</string> + <string name="stores_buy_price">Buy · %1$s</string> + <string name="stores_installed_chip">Installed</string> + <string name="stores_in_library">In library</string> + <string name="stores_free">Free</string> + <string name="stores_resume">Resume</string> + <string name="stores_refresh">Refresh</string> + <string name="stores_show_mature">Show mature content</string> + <string name="epic_card_title">Epic</string> + <string name="epic_card_eos">EOS</string> + <string name="epic_card_offline">Offline</string> + <string name="epic_card_none">Off</string> + <string name="epic_eos">Epic sign-in (EOS)</string> + <string name="epic_offline">Launch offline</string> + <string name="epic_resolve">Resolve Epic sign-in</string> + <string name="store_cloud_saves">Cloud saves</string> + <string name="cloud_upload">Upload to cloud</string> + <string name="cloud_download">Download from cloud</string> + <string name="cloud_none">No cloud saves</string> + <string name="cloud_conflict">Cloud conflict</string> + <string name="cloud_conflict_files">Cloud conflict · %1$d files</string> + <string name="cloud_pending">…</string> + <string name="cloud_keep_cloud">Keep cloud</string> + <string name="cloud_keep_local">Keep local</string> + <string name="cloud_checking">Checking cloud…</string> + <string name="cloud_last_sync">Last sync %1$s</string> + <string name="cloud_never">Not synced yet</string> + <string name="cloud_done">%1$d files</string> + <string name="stores_eyebrow_installed">installed</string> + <string name="stores_eyebrow_library">in your library</string> + <string name="stores_eyebrow_store">store</string> + <string name="stores_in_steam">✓ In Steam</string> + <string name="stores_uninstall">Uninstall</string> + <string name="stores_uninstall_confirm">Press again to uninstall</string> + <string name="stores_launch_not_registered">Steam picks the game up when it next starts; launch it from there, or start Steam now.</string> + <string name="stores_card_proton">Proton</string> + <string name="stores_card_proton_value">Steam\'s choice per game</string> + <string name="stores_card_audio">Audio</string> + <string name="stores_card_audio_value">DirectAudio</string> + <string name="stores_card_components">Components</string> + <string name="stores_card_components_value">FEX, DXVK, VKD3D</string> + <string name="stores_card_engine">Engine</string> + <string name="stores_card_engine_value">%1$s · native</string> + <string name="stores_card_engine_java">%1$s · built-in</string> + <string name="stores_how_it_runs">How it runs</string> + <string name="stores_kv_fetched">Fetched from</string> + <string name="stores_kv_fetched_value">%1$s, with your account</string> + <string name="stores_kv_installed_to">Installed to</string> + <string name="stores_kv_installed_to_value">Games storage (Games/%1$s/%2$s)</string> + <string name="stores_kv_launched_by">Launched by</string> + <string name="stores_kv_launched_by_value">Steam and Proton, the same path as Steam games</string> + <string name="stores_kv_launched_by_epic">Steam and Proton · Epic sign-in per launch, EOS overlay off</string> + <string name="stores_kv_launched_by_amazon">Steam and Proton · Amazon Games Services variables set</string> + <string name="stores_kv_steam">Steam</string> + <string name="stores_kv_steam_on">added to Steam on install (Deck UI + Steam Input)</string> + <string name="stores_downloads_empty">No downloads</string> + <string name="stores_dl_paused">Paused</string> + <string name="stores_dl_queued">Queued</string> + <string name="stores_dl_queued_at">Queued · #%1$d</string> + <string name="stores_dl_cancelled">Cancelled</string> + <string name="stores_dl_failed">Failed</string> + <string name="stores_dl_failed_reason">Failed · %1$s</string> + <string name="stores_dl_stage_percent">%1$s · %2$d%%</string> + <string name="stores_dl_bytes">%1$s / %2$s · %3$d%%</string> + <string name="stores_dl_pause">Pause</string> + <string name="stores_dl_resume">Resume</string> + <string name="stores_dl_cancel">Cancel</string> + <string name="stores_dl_retry">Retry</string> + <string name="stores_dl_clear">Clear</string> + <string name="stores_dl_clear_all">Clear finished</string> + <string name="stores_stage_manifest">Manifest</string> + <string name="stores_stage_download">Download</string> + <string name="stores_stage_verify">Verify</string> + <string name="stores_stage_install">Install</string> + <string name="stores_stage_done">Done</string> + <string name="stores_stage_checking">Checking</string> + <string name="stores_stage_downloading">Downloading</string> + <string name="stores_dl_cancel_confirm">Delete download?</string> + <string name="stores_dl_failed_kept">Failed · %1$s kept</string> + <string name="stores_stage_verifying">Verifying</string> + <string name="stores_stage_installing">Installing</string> + <string name="stores_dl_active_percent">%1$s %2$d%%</string> + <string name="stores_dl_with">%1$s · %2$s</string> + <string name="stores_dl_amount">%1$s / %2$s</string> + <string name="stores_dl_items">%1$d/%2$d files</string> + <string name="stores_download_manager">Download manager</string> + <string name="stores_parallel">Downloads at a time</string> + <string name="stores_install_where">Install to</string> + <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_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> + <string name="stores_notification_many">%1$d downloads</string> + <string name="stores_notification_waiting">Preparing…</string> + <string name="stores_notification_done_channel">Finished store downloads</string> + <string name="stores_notification_installed">%1$s installed</string> + <string name="stores_notification_failed">%1$s download failed</string> + <string name="stores_notification_delete">Delete</string> + <string name="stores_notification_keep">Keep</string> + <string name="stores_login_title">Sign in to %1$s</string> + <string name="stores_login_provider_title">Sign in with your provider</string> + <string name="stores_login_reload">Reload</string> + <string name="stores_login_close">Close</string> + <string name="stores_login_retry">Try again</string> + <string name="stores_login_finishing">Finishing sign-in…</string> + <string name="stores_login_error_network">The sign-in page could not be loaded: %1$s</string> + <string name="stores_login_error_idp_blocked">This provider refuses to sign in from an embedded page. Sign in with your store password instead.</string> + <string name="stores_login_error_http">The sign-in page answered with an error (HTTP %1$d).</string> + <string name="stores_login_error_ssl">The connection to the sign-in page is not secure.</string> + <string name="stores_login_error_verification">The sign-in could not be verified. Try again.</string> + <string name="stores_login_error_generic">Sign-in failed. Try again.</string> </resources> diff --git a/app/src/main/rust/stores/Cargo.lock b/app/src/main/rust/stores/Cargo.lock new file mode 100644 index 0000000000..b259f7d3b1 --- /dev/null +++ b/app/src/main/rust/stores/Cargo.lock @@ -0,0 +1,1729 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "async-compression" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79b3f8a79cccc2898f31920fc69f304859b3bd567490f75ebf51ae1c792a9ac" +dependencies = [ + "compression-codecs", + "compression-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytes" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33" + +[[package]] +name = "cc" +version = "1.2.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "compression-codecs" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2548391e9c1929c21bf6aa2680af86fe4c1b33e6cea9ac1cfeec0bd11218cf" +dependencies = [ + "compression-core", + "flate2", + "memchr", +] + +[[package]] +name = "compression-core" +version = "0.4.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789" + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "displaydoc" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "droiddeckstores" +version = "0.1.0" +dependencies = [ + "flate2", + "futures-util", + "jni", + "libc", + "reqwest", + "serde_json", + "sha1", + "sha2", + "tokio", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures-channel" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07bbe89c50d7a535e539b8c17bc0b49bdb77747034daa8087407d655f3f7cc1d" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" + +[[package]] +name = "futures-sink" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c39754e157331b013978ec91992bde1ac089843443c49cbc7f46150b0fad0893" + +[[package]] +name = "futures-task" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" + +[[package]] +name = "futures-util" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "r-efi", + "wasip2", + "wasm-bindgen", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "http" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be7462df143984c4598a256ef469b251d7d7f9e271135073e78fc535414f3d0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b021d93e26becf5dc7e1b75b1bed1fd93124b374ceb73f43d4d4eafec896a64a" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "hyper" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb92f162bf56536459fc83c79b974bb12837acfed43d6bc370a7916d0ae15ecc" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "http", + "http-body", + "httparse", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ca68d021ef39cf6463ab54c1d0f5daf03377b70561305bb89a8f83aab66e0f" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "tokio", + "tokio-rustls", + "tower-service", + "webpki-roots", +] + +[[package]] +name = "hyper-util" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96547c2556ec9d12fb1578c4eaf448b04993e7fb79cbaad930a656880a6bdfa0" +dependencies = [ + "base64", + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "hyper", + "ipnet", + "libc", + "percent-encoding", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "icu_collections" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38" + +[[package]] +name = "icu_properties" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14" + +[[package]] +name = "icu_provider" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "ipnet" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2" + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jni" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" +dependencies = [ + "cesu8", + "cfg-if", + "combine", + "jni-sys 0.3.1", + "log", + "thiserror 1.0.69", + "walkdir", + "windows-sys 0.45.0", +] + +[[package]] +name = "jni-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41a652e1f9b6e0275df1f15b32661cf0d4b78d4d87ddec5e0c3c20f097433258" +dependencies = [ + "jni-sys 0.4.1", +] + +[[package]] +name = "jni-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6377a88cb3910bee9b0fa88d4f42e1d2da8e79915598f65fb0c7ee14c878af2" +dependencies = [ + "jni-sys-macros", +] + +[[package]] +name = "jni-sys-macros" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38c0b942f458fe50cdac086d2f946512305e5631e720728f2a61aabcd47a6264" +dependencies = [ + "quote", + "syn", +] + +[[package]] +name = "js-sys" +version = "0.3.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "142bc4740e452c1e57ade0cbc129f139c9093e354346f0872ef985f4f5cf5f11" +dependencies = [ + "cfg-if", + "futures-util", + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.186" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66" + +[[package]] +name = "litemap" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0" + +[[package]] +name = "log" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616ec5685824bcc94416c6d4a7a446eea774a31efd7062c8480ba6fd06d7a6e5" + +[[package]] +name = "lru-slab" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154" + +[[package]] +name = "memchr" +version = "2.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b947ae49db0d222b1dbc6b113ce7248a3fc3a6ca21b696717bfc000ba4484d8" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "mio" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "potential_utf" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quinn" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" +dependencies = [ + "bytes", + "cfg_aliases", + "pin-project-lite", + "quinn-proto", + "quinn-udp", + "rustc-hash", + "rustls", + "socket2", + "thiserror 2.0.18", + "tokio", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-proto" +version = "0.11.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "434b42fec591c96ef50e21e886936e66d3cc3f737104fdb9b737c40ffb94c098" +dependencies = [ + "bytes", + "getrandom 0.3.4", + "lru-slab", + "rand", + "ring", + "rustc-hash", + "rustls", + "rustls-pki-types", + "slab", + "thiserror 2.0.18", + "tinyvec", + "tracing", + "web-time", +] + +[[package]] +name = "quinn-udp" +version = "0.5.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" +dependencies = [ + "cfg_aliases", + "libc", + "once_cell", + "socket2", + "tracing", + "windows-sys 0.60.2", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "reqwest" +version = "0.12.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eddd3ca559203180a307f12d114c268abf583f59b03cb906fd0b3ff8646c1147" +dependencies = [ + "base64", + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "js-sys", + "log", + "percent-encoding", + "pin-project-lite", + "quinn", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tokio-rustls", + "tower", + "tower-http", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "webpki-roots", +] + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-hash" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe" + +[[package]] +name = "rustls" +version = "0.23.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef86cd5876211988985292b91c96a8f2d298df24e75989a43a3c73f2d4d8168b" +dependencies = [ + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a7197ae7eb376e574fe940d068c30fe0462554a3ddbe4eca7838e049c937a9" +dependencies = [ + "web-time", + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c429a8649f110dddef65e2a5ad240f747e85f7758a6bccc7e5777bd33f756e" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9" +dependencies = [ + "indexmap", + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "simd-adler32" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "socket2" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" +dependencies = [ + "futures-core", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl 2.0.18", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinystr" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tinyvec" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e61e67053d25a4e82c844e8424039d9745781b3fc4f32b8d55ed50f5f667ef3" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1729aa945f29d91ba541258c8df89027d5792d85a8841fb65e8bf0f4ede4ef61" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "pin-project-lite", + "sync_wrapper", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "async-compression", + "bitflags", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-body-util", + "pin-project-lite", + "tokio", + "tokio-util", + "tower", + "tower-layer", + "tower-service", + "url", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "typenum" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ce102ab67701b8526c123c1bab5cbe42d7040ccfd0f64af1a385808d2f43de" + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.3+wasi-0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20064672db26d7cdc89c7798c48a0fdfac8213434a1186e5ef29fd560ae223d6" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.122" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed04576f974d2b2fba0f38c51dbc5518011e38c36bf1143164be765528fd409" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9473dbd2991ae90b6291c3c32c30c6187ac49aa32f9905d1cce280ec1e110b0f" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.122" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "916151b09da36bd82f6615cbf3a419e2f0ba23a03c6160e8e92eb6bd4aa1dec6" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.122" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "299047362ccbfce148b67ab7e73349f77748e00c8296f9542adfad2ad82c5c5e" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.122" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a929b2c61f11ba3e9bc35b50c1f25cb38e0e892c0c231ae2b8cf78d5dad4437" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621441cfc37b84979402712047321980c178f299193a3589d05b99e8763436" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "webpki-roots" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52f5ee44c96cf55f1b349600768e3ece3a8f26010c05265ab73f945bb1a2eb9d" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "writeable" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4" + +[[package]] +name = "yoke" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abe8c5fda708d9ca3df187cae8bfb9ceda00dd96231bed36e445a1a48e66f9ca" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bce33a6288fa3f072a8c2c7d0f2fdbb90e28298f0135c1f99b96c3db2efcc60b" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd425244944f4ab65ccff928e7323354c5a018c75838362fdce749dfad2ee1e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zerotrie" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zmij" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" diff --git a/app/src/main/rust/stores/Cargo.toml b/app/src/main/rust/stores/Cargo.toml new file mode 100644 index 0000000000..49c36453ef --- /dev/null +++ b/app/src/main/rust/stores/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "droiddeckstores" +version = "0.1.0" +edition = "2021" +# The engines are a port of Bannerlator's bl-steam-client store modules (GPL-3.0-or-later, derived +# from WinNative's wn-steam-client; the Epic selection/verify rules trace back to GameNative and +# Legendary, both GPL-3.0). DroidDeck is GPL-3.0, so the crate keeps the licence it came with. +license = "GPL-3.0-or-later" +publish = false + +[lib] +name = "droiddeckstores" +crate-type = ["cdylib"] + +[dependencies] +jni = "0.21" +# setpriority/gettid: every engine thread runs at nice +10 (see src/priority.rs). Already in the +# dependency tree through tokio, at the version Cargo.lock pins. +libc = "0.2" +# zlib inflate for GOG gen2 chunks and Epic chunk bodies. The pure-Rust backend keeps the .so free +# of a C zlib and of the NDK sysroot's libz, which the apk does not ship. +flate2 = { version = "1", default-features = false, features = ["rust_backend"] } +# The fetch core drives every request through ONE tokio current-thread runtime and feeds a sync +# process pool; rustls (via ring) is the TLS stack so there is no OpenSSL to cross-compile. `gzip` +# transparently inflates a CDN answer that comes back content-encoded. No `blocking` client: the +# three engines are async-fetch only. +reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "gzip"] } +tokio = { version = "1", default-features = false, features = ["rt", "net", "time", "sync"] } +futures-util = { version = "0.3", default-features = false, features = ["std"] } +# preserve_order keeps JSON object keys in source order, as in Bannerlator, so the GOG depot plan +# walks a manifest's files in the order the manifest lists them (largest-first sorting happens +# after, explicitly). +serde_json = { version = "1", features = ["preserve_order"] } +# Epic chunks are SHA-1 verified, Amazon files SHA-256; GOG's MD5 is the in-tree md5_small. +sha1 = "0.10" +sha2 = "0.10" + +[profile.release] +codegen-units = 1 +lto = "thin" +strip = "symbols" diff --git a/app/src/main/rust/stores/JNI.md b/app/src/main/rust/stores/JNI.md new file mode 100644 index 0000000000..9f1b385504 --- /dev/null +++ b/app/src/main/rust/stores/JNI.md @@ -0,0 +1,272 @@ +# `libdroiddeckstores.so` — the JNI contract + +The Kotlin side of the store download engines mirrors this file exactly. Every symbol the library +exports is listed here with the class it binds to, the JNI signature, when it is called and on +which thread. The engines came from Bannerlator's `bl-steam-client` crate; the method names and +signatures are identical to Bannerlator's `BlGogDownload` / `BlEpicDownload` / `BlAmazonDownload`, +only the package and class names changed. + +Load it once with `System.loadLibrary("droiddeckstores")`. The library is built by Gradle +(`buildRustStores`, see `app/build.gradle`) and is absent from a `-PskipRust=true` build, so every +call site must be ready for `UnsatisfiedLinkError` on `loadLibrary` or on the first `external` +call and degrade to "store unavailable". + +## Conventions that hold for all three engines + +- The `external` functions are declared `@JvmStatic` in a Kotlin `object` (a `JClass` arrives as + the second JNI argument). Declaring them in a plain `object` without `@JvmStatic` also binds — + the symbol name is the same — but keep `@JvmStatic` so the contract matches Bannerlator's. +- No overloads: the exported names are the short JNI form (`Java_<package>_<Class>_<method>`), so + adding a second `nativeStart` with a different parameter list would NOT bind. +- Listener objects are only ever reached through `CallVoidMethod` by name + signature on the + object that was passed to `nativeStart`. The library never does `FindClass`, so the listener's + class may be any class (interface, nested interface, anonymous object) that declares the listed + methods with exactly these signatures. The names suggested below are what the Rust doc comments + use. +- Every listener method runs on a native thread (the run's worker thread, a process-pool thread + or the throughput reporter) that is attached to the JVM as a daemon. Never touch Views directly; + post to the main thread. A Java exception thrown by a listener method is cleared and ignored by + the native side — it does not stop the run. +- `nativeStart` returns an opaque handle (`long`). `0` means the run did not start. The handle is + a heap pointer: call `nativeRelease(handle)` exactly once per non-zero handle (after + `onComplete`), never twice, never with a made-up value. +- `nativeCancel(handle)` is idempotent and safe at any time between `nativeStart` and + `nativeRelease`. The run stops at its next scheduling point; `onComplete` still fires. +- `nativeRelease(handle)` is safe even while a cancelled run is still winding down: the worker + thread owns its own references (cancel flag, listener `GlobalRef`). +- `caBundlePath`: a PEM bundle the HTTPS client adds as extra root certificates; `""` = the roots + rustls ships with (webpki). Bannerlator extracts the system CA store to a file for this; on + DroidDeck `""` is the sensible default unless a CDN fails TLS verification. +- `maxWorkers` is the ceiling of the adaptive in-flight request window; `processWorkers` is the + number of sync inflate+hash+write threads. Bannerlator passes its "download threads" preference + for both (8 by default). + +## `com.droiddeck.launcher.stores.StoresNative` + +| Method | JNI signature | Symbol | +|---|---|---| +| `nativeVersion(): String` | `()Ljava/lang/String;` | `Java_com_droiddeck_launcher_stores_StoresNative_nativeVersion` | + +Returns the crate version from `Cargo.toml` (`"0.1.0"`). Call it from Setup, after +`System.loadLibrary("droiddeckstores")`, to prove the library loads and binds; show the string +next to the other component versions. May return `null` only if the JVM cannot allocate a string. + +```kotlin +package com.droiddeck.launcher.stores + +object StoresNative { + @JvmStatic external fun nativeVersion(): String +} +``` + +## `com.droiddeck.launcher.stores.gog.GogNative` + +GOG gen2 chunk engine (depot manifests → chunk fetch + zlib inflate + MD5) and gen1 (build manifest +→ per-file HTTP Range GET streamed to disk). One `nativeStart` = one download loop (base install, +DLC install, or a dependency-redist assembly). The Kotlin manager owns everything before (token, +builds, manifest head, product + language depot selection, secure-link resolution, disk guard) and +after it (marker files, exe resolution, DLC markers, redist orchestration). + +| Method | JNI signature | Symbol | +|---|---|---| +| `nativeProbe(): Int` | `()I` | `Java_com_droiddeck_launcher_stores_gog_GogNative_nativeProbe` | +| `nativeStart(...)`: Long | `(I[Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;IIZLjava/lang/String;Ljava/lang/Object;)J` (last parameter is the listener; its static type is the app's choice) | `Java_com_droiddeck_launcher_stores_gog_GogNative_nativeStart` | +| `nativeCancel(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_gog_GogNative_nativeCancel` | +| `nativeRelease(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_gog_GogNative_nativeRelease` | + +`nativeProbe()` always returns `1`; it exists so `isAvailable()` can catch `UnsatisfiedLinkError` +once at startup instead of mid-download. + +`nativeStart` parameters, in order: + +| # | Parameter | Meaning | +|---|---|---| +| 1 | `kind: Int` | `0` = `KIND_GEN2_CHUNKS`, `1` = `KIND_GEN1_RANGES` (anything else → gen2) | +| 2 | `depotManifests: Array<String>` | gen2: the inflated depot-manifest JSON strings, in fetch order, already filtered by product + language; gen1: the inflated build manifest (one element) | +| 3 | `cdnBases: Array<String>` | gen2: **every** CDN base of the secure-link answer — one per `urls[]` entry, each resolved the way `parseCdnUrl` resolves `urls[0]` today (query string intact), in GOG's order — or the one unauthenticated dependency store base (an array of one); gen1: `emptyArray()` (file URLs are in the manifest). Blank entries are ignored and a base whose host already appeared is dropped (the first wins). The fetch core spreads the window across the distinct hosts with a per-host cap of `max(6, ceil(maxWorkers / hosts))`, rotating a failed chunk to another CDN; one base behaves exactly like the old single `cdnBase`. The start log line lists them as `hosts=a,b,c`. | +| 4 | `installDir: String` | absolute install directory | +| 5 | `skipPaths: Array<String>` | relative paths already completed by an earlier run of this same download (secure-link refresh re-run): counted done without re-hashing, no progress event | +| 6 | `caBundlePath: String` | see conventions | +| 7 | `maxWorkers: Int` | window ceiling for this loop (Bannerlator: its download-threads preference, 8, or 1 for redists) | +| 8 | `processWorkers: Int` | inflate/hash/write threads | +| 9 | `sortLargestFirst: Boolean` | base install = `true` (largest files first); DLC / dependency = `false` | +| 10 | `label: String` | log prefix, e.g. `"gog base"`; `""` → `"gog"` | +| 11 | `listener` | see below | + +Returns `0` without any callback when: the listener is null, the JavaVM cannot be obtained, the +install dir or manifest list is empty, `cdnBases` holds no usable base for gen2, or the worker +thread cannot be spawned. Otherwise the run is on a thread named `gog-dl` and `onComplete` fires exactly once. + +Suggested listener: `com.droiddeck.launcher.stores.gog.GogNative.Listener` (an interface). + +| Listener method | JNI signature | When | +|---|---|---| +| `onProgress(bytesDone: Long, bytesTotal: Long, filesDone: Int, filesTotal: Int, file: String, fileBytes: Long, verified: Boolean)` | `(JJIILjava/lang/String;JZ)V` | one file reached its final state. `verified = true` = resume-skip (existing file passed size+MD5; no bytes credited, `fileBytes` = its size); `false` = freshly assembled, size+MD5-verified and renamed (`fileBytes` = its decompressed size). `filesDone` counts both, `bytesDone` counts assembled bytes only. Process-pool / worker thread. | +| `onBytes(bytesDone: Long, bytesTotal: Long)` | `(JJ)V` | byte progress between file completions: every 250 ms while the run is fetching, only when the value moved, plus once when the fetch ends. `bytesDone` = bytes of assembled files + bytes of files still in flight (gen2: their finished chunks, decompressed; gen1: what the current attempt has written; a retried attempt starts over, so nothing is counted twice); `bytesTotal` is the same total `onProgress` reports. It never goes past the next `onProgress.bytesDone` by more than the in-flight files and is equal to it once they finish. Drive the progress bar and speed from this; keep `onProgress` for file counts and per-file events. Called from the engine's ticker thread. Optional: a listener without it still works (the call fails quietly). | +| `onLog(line: String)` | `(Ljava/lang/String;)V` | engine diagnostics; the same line already went to logcat under the tag `GogNative`. Any native thread. | +| `onComplete(success: Boolean, cancelled: Boolean, linkExpiry: Boolean, error: String, bytesWritten: Long, filesDone: Int)` | `(ZZZLjava/lang/String;JI)V` | exactly once, from the worker thread, last. `linkExpiry = true` = the run died on an HTTP 401/403/404/500, the codes the GOG secure link returns once it expired: refresh the link and call `nativeStart` again with the finished files in `skipPaths`. `error` is `""` on success. | + +On-disk protocol (what a resumed run, by either engine, relies on): a file is assembled into +`<file>.bhtmp`, then renamed over `<file>` once its size and MD5 match the manifest; an existing +`<file>` whose size+MD5 match is skipped. The engine writes nothing else. + +## `com.droiddeck.launcher.stores.epic.EpicNative` + +Two runs make up the native half of an 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 + API JSON, downloads the manifest, selects files (install tags), runs the delta/verify pass and + does every post-install step. The engine re-parses the same manifest bytes (ChunksV4 binary + or legacy JSON), rebuilds the chunk plan for the pending files, skips chunks already in the + cache, and writes verified chunks with a `.part` + rename protocol, so the on-disk state is the + same whichever side fetched. +2. **Assemble** (`nativeAssemble`): writes the pending files from the cache, one sequential write + per file, SHA-1 checked as the bytes go out when the manifest hashes the file. This is the + same loop the Kotlin manager runs today (`for part in file.parts: write(chunk[offset..+size])`, + same "Missing chunk X for Y" wording); the Kotlin loop remains a valid fallback. + +**The chunk cache directory** is the `chunkCacheDir` parameter of both calls (always pass the +same value to both for one install): + +- `""` — today's behaviour: the cache is `<installDir>/.chunks`. The fetch leaves it in place; + `nativeAssemble` removes it whole at the end of a successful run (as the Kotlin loop's + `deleteDir(.chunks)` does), and never deletes a chunk early. +- any other absolute path — a **scratch cache**, meant for internal storage when the game + installs to an SD card (each byte then hits the card once instead of twice; the card's write + speed stops starving the fetchers). The fetch fills it exactly like `.chunks`. The assembler + reference-counts chunk GUIDs across the files it was given and **deletes each cached chunk as + soon as the last file that uses it has been written and verified**, then removes the directory + at the end of a successful run. A cancelled or failed run (fetch or assembly) leaves the cache + exactly as it stands, for resume: the next run's delta pass skips finished files and the next + fetch skips chunks still cached. The directory is created by the fetch if it does not exist; + give each install its own (e.g. `<cacheRoot>/epic/<appName>`), since a successful assembly + removes it. + +| Method | JNI signature | Symbol | +|---|---|---| +| `nativeStart(...)`: Long | `([BLjava/lang/String;Ljava/lang/String;[Ljava/lang/String;[IIJLjava/lang/String;IILjava/lang/Object;)J` (last parameter is the listener) | `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStart` | +| `nativeAssemble(...)`: Long | `([BLjava/lang/String;Ljava/lang/String;[ILjava/lang/Object;)J` (last parameter is the listener) | `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeAssemble` | +| `nativeCancel(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeCancel` | +| `nativeRelease(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeRelease` | + +`nativeCancel` / `nativeRelease` take the handle of either run. There is no `nativeProbe`; +Bannerlator probes by catching the `Throwable` from `loadLibrary` / the first `nativeStart`. + +```kotlin +@JvmStatic private external fun nativeStart( + manifest: ByteArray, installDir: String, chunkCacheDir: String, cdnPrefixes: Array<String>, + pendingFileIdx: IntArray, expectedChunks: Int, expectedBytes: Long, caBundlePath: String, + maxWorkers: Int, processWorkers: Int, listener: Listener, +): Long +@JvmStatic private external fun nativeAssemble( + manifest: ByteArray, installDir: String, chunkCacheDir: String, fileIdx: IntArray, listener: Listener, +): Long +``` + +### `nativeStart` parameters, in order + +| # | Parameter | Meaning | +|---|---|---| +| 1 | `manifest: ByteArray` | the raw manifest bytes the manager downloaded (binary ChunksV4 or JSON) | +| 2 | `installDir: String` | absolute install directory | +| 3 | `chunkCacheDir: String` | `""` = `<installDir>/.chunks`; else the scratch cache directory (see above) | +| 4 | `cdnPrefixes: Array<String>` | the CDN base URLs from the manifest API answer (`.../ChunksV4/<n>/` style); one or many, distinct hosts widen the window | +| 5 | `pendingFileIdx: IntArray` | indices into the manifest's file list of the files still to fetch (the manager's delta/verify result) | +| 6 | `expectedChunks: Int` | the manager's own needed-chunk count for a cross-check; `-1` = no cross-check | +| 7 | `expectedBytes: Long` | the manager's `Σ max(fileSize, 1)` for a cross-check; `-1` = no cross-check | +| 8 | `caBundlePath: String` | see conventions | +| 9 | `maxWorkers: Int` | window ceiling (Bannerlator: 8) | +| 10 | `processWorkers: Int` | inflate/verify/write threads | +| 11 | `listener` | see below | + +Planning (manifest parse + plan + cross-check) runs synchronously on the calling thread, so a +plan failure returns `0` BEFORE any fetch; the manager then runs its own fallback loop. On `0`, +`onComplete(false, reason, 0)` has already been called synchronously (except for a null listener). +On success, `onPlan` has been called synchronously before `nativeStart` returns, then the fetch +runs on a new thread. The engine's `plan` log line names the cache: +`plan chunk_dir=ChunksV4 cache_dir=<dir> scratch=<bool> files_pending=… chunks=… bytes=… …`. + +### `nativeAssemble` parameters, in order + +| # | Parameter | Meaning | +|---|---|---| +| 1 | `manifest: ByteArray` | the same manifest bytes as the fetch | +| 2 | `installDir: String` | absolute install directory (files are written at `installDir/<filename with \\ → />`) | +| 3 | `chunkCacheDir: String` | the same value the fetch was given (`""` or the scratch directory) | +| 4 | `fileIdx: IntArray` | indices into the manifest's file list of the files to write — the fetch's `pendingFileIdx`, in one call | +| 5 | `listener` | see below | + +Parse + plan run on the calling thread: an unparsable manifest, an index out of range or an +empty `installDir` returns `0` after a synchronous `onComplete(false, reason, 0)` and nothing is +written. Otherwise the assembly runs on a new thread. Each file is created fresh and written in +part order; on any error (missing chunk, short chunk, I/O error, SHA-1 mismatch) the file being +written is deleted, the run fails with that error, no further files are touched and the cache is +left as it is. A manifest SHA-1 of all zeros (and a JSON manifest, which has none) means no hash +check — the Kotlin delta pass treats those the same way. The run's log lines: +`assemble cache_dir=… scratch=… files=… bytes=… chunks_referenced=…`, then `cache removed …` / +`cache not removed …: <why>` (not fatal) and `assembled files=… bytes=… chunks_removed_early=…`. + +### Listener + +Suggested: `com.droiddeck.launcher.stores.epic.EpicNative.Listener` (nested interface, as in +Bannerlator). One interface serves both runs. + +| Listener method | JNI signature | When | +|---|---|---| +| `onPlan(chunksTotal: Int, bytesTotal: Long, chunkDir: String)` | `(IJLjava/lang/String;)V` | fetch only: once, synchronously inside `nativeStart`, before any fetch: the plan the engine derived (`chunkDir` is the manifest's, e.g. `ChunksV4`, not the cache directory) | +| `onProgress(bytesDone: Long, bytesTotal: Long, done: Int, total: Int)` | `(JJII)V` | fetch: per accounted chunk (cached-skip or fetched), `done`/`total` = chunks; assembly: after each file written, `bytesDone` = bytes written so far, `done`/`total` = files. Native threads. | +| `onLog(line: String)` | `(Ljava/lang/String;)V` | engine log line, already in logcat under `EpicNative`; any native thread (also synchronously from `nativeStart`/`nativeAssemble` for the start line) | +| `onComplete(success: Boolean, error: String, bytes: Long)` | `(ZLjava/lang/String;J)V` | terminal, exactly once (worker thread; or synchronously when the call returns `0`). Fetch: `bytes` = credited bytes (cached-skip + fetched). Assembly: `bytes` = bytes written. `error` is `"cancelled"` on cancel. | + +Bannerlator's `BlEpicDownload.run(...)` wraps `nativeStart` in a blocking call that polls an +`AtomicBoolean` cancel flag every 250 ms, calls `nativeCancel` on cancel, waits up to 5 s for +`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). + +## `com.droiddeck.launcher.stores.amazon.AmazonNative` + +The file-fetch pool of an Amazon Games install: one streamed HTTP GET per manifest file, SHA-256 +checked, written as `<dest>.tmp` and renamed. The manager keeps manifest/auth/markers/post-install +and hands the engine a JSON plan. + +| Method | JNI signature | Symbol | +|---|---|---| +| `nativeStart(...)`: Long | `(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IILjava/lang/Object;)J` (last parameter is the listener) | `Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeStart` | +| `nativeCancel(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeCancel` | +| `nativeRelease(handle: Long)` | `(J)V` | `Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeRelease` | + +`nativeStart` parameters, in order: + +| # | Parameter | Meaning | +|---|---|---| +| 1 | `planJson: String` | a JSON array, one object per manifest file: `{"relPath": "...", "url": "https://...", "size": <long>, "sha256hex": "..."}` (`sha256hex` may be `""` = no hash check). Every manifest file goes in: the engine applies the size-based resume-skip itself. | +| 2 | `installDir: String` | absolute install directory | +| 3 | `caBundlePath: String` | see conventions | +| 4 | `maxWorkers: Int` | window size; `<= 0` = the engine default (8, Amazon's own `MAX_PARALLEL`) | +| 5 | `processWorkers: Int` | sync verify+write threads (`<= 0` → 1) | +| 6 | `listener` | see below | + +Returns `0` when the listener is null (no callback), or when `planJson` / `installDir` is empty +(`onComplete(false, "empty plan or install dir", 0)` fires synchronously first), or when the +listener `GlobalRef` cannot be taken (no callback). The engine keeps the `JavaVM` it takes from +the first `nativeStart`. + +Suggested listener: `com.droiddeck.launcher.stores.amazon.AmazonNative.Listener` (an interface). + +| Listener method | JNI signature | When | +|---|---|---| +| `onProgress(bytesDone: Long, bytesTotal: Long, filesDone: Long, filesTotal: Long)` | `(JJJJ)V` | once up front with the resume-skipped credit, then after each committed file (and as pieces stream in); `bytesDone` includes skipped files. Native threads. | +| `onLog(line: String)` | `(Ljava/lang/String;)V` | one diagnostic line (`engine=rust …`, `fetch-window …`, `summary …`). The Amazon engine does NOT write to logcat itself: the Kotlin side is the one place that logs these, so each line appears once. | +| `onComplete(success: Boolean, error: String, bytesWritten: Long)` | `(ZLjava/lang/String;J)V` | exactly once, when the run succeeds, fails or is cancelled (`error = "cancelled"`). | + +Bannerlator's `BlAmazonDownload.runBlocking(...)` polls a cancel probe every 100 ms, calls +`nativeCancel` once, and `nativeRelease` in `finally` after `onComplete`; on thread interrupt it +cancels and waits up to 30 s for the run to wind down before releasing. + +## Log tags + +Lines go to logcat under `GogNative` and `EpicNative` (the Amazon engine logs only through +`onLog`). Bannerlator used `BL_GOG_DL` / `BL_EPIC_DL` / `BL_AMAZON_DL`; the line grammar +(`fetch-start`, `fetch-window`, `throughput`, `fetch-end`, `summary`) is unchanged, so its tuning +notes still read against a DroidDeck log. diff --git a/app/src/main/rust/stores/src/amazon.rs b/app/src/main/rust/stores/src/amazon.rs new file mode 100644 index 0000000000..55826f9417 --- /dev/null +++ b/app/src/main/rust/stores/src/amazon.rs @@ -0,0 +1,990 @@ +//! Amazon Games download adapter over the shared fetch core. +//! +//! Replaces ONLY the byte-fetching pool of `AmazonDownloadManager.install()` (Step 4). +//! Java still resolves the download spec, fetches and parses `manifest.proto`, and hands +//! this module the resolved file list (one JSON entry per manifest file with the exact +//! URL the Java loop would have opened). The adapter mirrors the Java rules 1:1 where the +//! core allows it — see Bannerlator's `docs/RUST_AMAZON_PARITY.md` §4 / §9 for the table and the +//! documented deviations: +//! +//! * one `FetchItem` per WHOLE file, streamed (`FetchOptions.stream = true`): pieces are +//! appended to `<dest>.tmp` as they arrive, 8 files wide like Java's pool, memory bounded +//! to the pieces not yet written; +//! * resume-skip = `st_size == manifest size`, no hash check on skip (Java `:250`); +//! * SHA-256 is accumulated while streaming and checked in `on_finish` when the manifest +//! carries a hash; a mismatch deletes the tmp and is a retryable failure (Java `:269`); +//! * `<dest>.tmp` → delete existing dest → rename (Java `:280`); rename failure is fatal +//! without retry (Java `:281`); +//! * a piece credits its wire length to progress as it is written (Java `:347`), and pieces +//! of an attempt that later fails stay counted (Java never rolls `totalDownloaded` back). + +pub mod jni; + +use crate::fetch_core::{run_fetch, FetchItem, FetchOptions, FetchSink, SinkError}; +use sha2::{Digest, Sha256}; +use std::collections::HashMap; +use std::fs::{self, File}; +use std::io::Write; +use std::path::PathBuf; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +/// Java `AmazonDownloadManager.MAX_PARALLEL`. +pub const MAX_PARALLEL: usize = 8; +/// Java `AmazonDownloadManager.DOWNLOAD_USER_AGENT`. +pub const DOWNLOAD_USER_AGENT: &str = "nile/0.1 Amazon"; +/// Java `conn.setReadTimeout(120000)`; in stream mode the core applies this to the response +/// headers and then as an idle deadline per received piece — the same shape as a read timeout. +pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(120); +/// Java `new File(installDir, file.unixPath() + ".tmp")`. +pub const TMP_SUFFIX: &str = ".tmp"; +/// Log label used in the core's `fetch-window` lines. +pub const LABEL: &str = "amazon"; + +/// One resolved manifest file as serialised by Java (`AmazonDownloadManager.buildRustPlan`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PlanEntry { + /// `ManifestFile.unixPath()` — relative to the install dir, forward slashes. + pub rel_path: String, + /// `AmazonApiClient.appendPath(downloadUrl, "files/" + hashHex)` — signed, whole file. + pub url: String, + /// `ManifestFile.size`. + pub size: u64, + /// SHA-256 to verify against; EMPTY = Java would not verify (`hashAlgorithm != 0` or + /// no hash bytes). + pub sha256: Vec<u8>, +} + +/// Parse the JSON array `[{relPath, url, size, sha256hex}]`. `relPath` is normalised the same +/// way as `ManifestFile.unixPath()` so a caller that forgot is still byte-identical. +pub fn parse_plan(json: &str) -> Result<Vec<PlanEntry>, String> { + let value: serde_json::Value = + serde_json::from_str(json).map_err(|err| format!("plan json: {err}"))?; + let Some(array) = value.as_array() else { + return Err("plan json: expected an array".to_string()); + }; + let mut entries = Vec::with_capacity(array.len()); + for (index, item) in array.iter().enumerate() { + let rel_path = item + .get("relPath") + .and_then(|v| v.as_str()) + .map(unix_path) + .unwrap_or_default(); + if rel_path.is_empty() { + return Err(format!("plan json: entry {index} has no relPath")); + } + let url = item + .get("url") + .and_then(|v| v.as_str()) + .unwrap_or_default() + .to_string(); + if url.is_empty() { + return Err(format!("plan json: entry {index} ({rel_path}) has no url")); + } + let size = item + .get("size") + .and_then(|v| v.as_u64().or_else(|| v.as_i64().map(|s| s.max(0) as u64))) + .unwrap_or(0); + let sha_hex = item + .get("sha256hex") + .and_then(|v| v.as_str()) + .unwrap_or_default(); + let sha256 = if sha_hex.is_empty() { + Vec::new() + } else { + hex_decode(sha_hex) + .ok_or_else(|| format!("plan json: entry {index} ({rel_path}) bad sha256hex"))? + }; + entries.push(PlanEntry { + rel_path, + url, + size, + sha256, + }); + } + Ok(entries) +} + +/// `ManifestFile.unixPath()`. +pub fn unix_path(path: &str) -> String { + path.replace('\\', "/") +} + +/// Lowercase/uppercase hex → bytes; `None` on odd length or a non-hex digit. +pub fn hex_decode(hex: &str) -> Option<Vec<u8>> { + let bytes = hex.as_bytes(); + if bytes.len() % 2 != 0 { + return None; + } + let nibble = |b: u8| -> Option<u8> { + match b { + b'0'..=b'9' => Some(b - b'0'), + b'a'..=b'f' => Some(b - b'a' + 10), + b'A'..=b'F' => Some(b - b'A' + 10), + _ => None, + } + }; + let mut out = Vec::with_capacity(bytes.len() / 2); + for pair in bytes.chunks(2) { + out.push((nibble(pair[0])? << 4) | nibble(pair[1])?); + } + Some(out) +} + +/// `new File(installDir, rel)` — Java resolves the child textually under the parent, so +/// build the path by concatenation (never `Path::join`, which would let a leading `/` escape). +pub fn dest_path(install_dir: &str, rel_path: &str) -> PathBuf { + let base = install_dir.trim_end_matches('/'); + PathBuf::from(format!("{base}/{rel_path}")) +} + +/// `new File(installDir, rel + ".tmp")`. +pub fn tmp_path(install_dir: &str, rel_path: &str) -> PathBuf { + dest_path(install_dir, &format!("{rel_path}{TMP_SUFFIX}")) +} + +/// Java `:250`: `destFile.exists() && destFile.length() == file.size`. `File.length()` is +/// `st_size`, which is what `Metadata::len()` returns — including for a directory, so the +/// (degenerate) behaviours match too. No hash is checked on skip, on purpose. +pub fn is_present_and_complete(install_dir: &str, entry: &PlanEntry) -> bool { + fs::metadata(dest_path(install_dir, &entry.rel_path)) + .map(|meta| meta.len() == entry.size) + .unwrap_or(false) +} + +/// `scheme://host[:port]` of a URL — the per-host cap key. Amazon serves every file from the +/// one signed CDN base, so this yields a single host. +pub fn host_key(url: &str) -> String { + let (scheme, rest) = match url.find("://") { + Some(idx) => (&url[..idx], &url[idx + 3..]), + None => ("https", url), + }; + let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + format!("{scheme}://{}", &rest[..end]) +} + +/// Improvements round 1: `max(6, ceil(max_workers / distinct_hosts))` — a single-host store +/// (Amazon's one signed a2z base) gets the whole ceiling on its one host; the core clamps the +/// window to `hosts × per_host_cap`, so anything smaller would silently cut the ceiling. +pub fn per_host_cap_for(max_workers: usize, distinct_hosts: usize) -> usize { + let hosts = distinct_hosts.max(1); + let per_host = max_workers.max(1).div_ceil(hosts); + per_host.max(crate::fetch_tuning::PER_HOST_CAP) +} + +/// The resolved work for one run. +#[derive(Debug, Default)] +pub struct Plan { + /// Items to fetch (index into `entries` via `FetchItem.id`). + pub items: Vec<FetchItem>, + /// Every manifest file, in manifest order (fetched and skipped alike). + pub entries: Vec<PlanEntry>, + /// Σ size of all entries = `ParsedManifest.totalInstallSize`. + pub total_bytes: u64, + /// Bytes credited up front for files that passed the resume check. + pub skipped_bytes: u64, + /// Number of files that passed the resume check. + pub skipped_files: u64, + /// Distinct host keys in fetch order. + pub hosts: Vec<String>, +} + +/// Apply the Java resume-skip rule to every entry and turn the rest into whole-file items. +pub fn build_plan(entries: Vec<PlanEntry>, install_dir: &str) -> Plan { + let mut plan = Plan { + total_bytes: entries.iter().map(|e| e.size).sum(), + ..Plan::default() + }; + for (index, entry) in entries.iter().enumerate() { + if is_present_and_complete(install_dir, entry) { + plan.skipped_bytes = plan.skipped_bytes.saturating_add(entry.size); + plan.skipped_files += 1; + continue; + } + let host = host_key(&entry.url); + if !plan.hosts.contains(&host) { + plan.hosts.push(host); + } + plan.items.push(FetchItem { + id: index as u64, + urls: vec![entry.url.clone()], + reserve: entry.size, + range: None, + }); + } + if plan.hosts.is_empty() { + plan.hosts.push("https://amazon".to_string()); + } + plan.entries = entries; + plan +} + +/// One `<dest>.tmp` being streamed: the open handle, the running hash and the bytes written +/// by the current attempt. +struct OpenTmp { + file: File, + hasher: Sha256, + written: u64, +} + +/// Sink: stream pieces into `<dest>.tmp` (SHA-256 accumulated on the fly), verify and rename +/// in `on_finish`. `process` (whole-body mode) does the same in one step. +pub struct AmazonSink { + install_dir: String, + entries: Vec<PlanEntry>, + cancel: Arc<AtomicBool>, + files_done: AtomicU64, + /// Per-item open tmp files. The map lock is held only to look the entry up; the write + /// itself happens under the per-item lock so 8 streams never serialise on one mutex. + open: Mutex<HashMap<u64, Arc<Mutex<OpenTmp>>>>, +} + +impl AmazonSink { + pub fn new(install_dir: &str, entries: Vec<PlanEntry>, cancel: Arc<AtomicBool>) -> Self { + Self { + install_dir: install_dir.to_string(), + entries, + cancel, + files_done: AtomicU64::new(0), + open: Mutex::new(HashMap::new()), + } + } + + pub fn files_done(&self) -> u64 { + self.files_done.load(Ordering::Relaxed) + } + + fn entry(&self, item: &FetchItem) -> Result<&PlanEntry, SinkError> { + self.entries + .get(item.id as usize) + .ok_or_else(|| SinkError::Fatal(format!("plan index {} out of range", item.id))) + } + + /// Java `:260` — `destFile.getParentFile().mkdirs()` before each attempt; a failure + /// surfaces on the create/write below, like Java's. + fn ensure_parent(&self, entry: &PlanEntry) { + if let Some(parent) = dest_path(&self.install_dir, &entry.rel_path).parent() { + let _ = fs::create_dir_all(parent); + } + } + + /// Java `:280-284`: delete an existing dest, rename tmp → dest; rename failure fails the + /// file with NO retry (→ whole install fails). + fn rename_into_place(&self, entry: &PlanEntry) -> Result<(), SinkError> { + let dest = dest_path(&self.install_dir, &entry.rel_path); + let tmp = tmp_path(&self.install_dir, &entry.rel_path); + if dest.exists() { + let _ = fs::remove_file(&dest); + } + if let Err(err) = fs::rename(&tmp, &dest) { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Fatal(format!( + "Failed to rename tmp → {} ({err})", + dest.display() + ))); + } + self.files_done.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + + /// Java `:267-277`: SHA-256 check when the manifest carries a hash; a mismatch deletes the + /// tmp and is retried. + fn verify(&self, entry: &PlanEntry, digest: &[u8]) -> Result<(), SinkError> { + if !entry.sha256.is_empty() && digest != entry.sha256.as_slice() { + let _ = fs::remove_file(tmp_path(&self.install_dir, &entry.rel_path)); + return Err(SinkError::Retry(format!( + "SHA-256 mismatch for: {}", + entry.rel_path + ))); + } + Ok(()) + } + + /// Whole-body path (`FetchOptions.stream = false`): verify → write tmp → rename. + pub fn commit(&self, entry: &PlanEntry, body: &[u8]) -> Result<u64, SinkError> { + if !entry.sha256.is_empty() { + let digest = Sha256::digest(body); + if digest.as_slice() != entry.sha256.as_slice() { + return Err(SinkError::Retry(format!( + "SHA-256 mismatch for: {}", + entry.rel_path + ))); + } + } + self.ensure_parent(entry); + let tmp = tmp_path(&self.install_dir, &entry.rel_path); + if let Err(err) = fs::write(&tmp, body) { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Retry(format!("write {}: {err}", tmp.display()))); + } + self.rename_into_place(entry)?; + Ok(body.len() as u64) + } + + /// After the run: close and delete every tmp that never reached `on_finish` (a stream that + /// failed after max attempts, or was cut by cancel / a fatal error). Java deletes the tmp + /// of every failed or cancelled attempt (`:271`, `:288`, `:293`, `:297`), so no `.tmp` + /// may survive a run on either engine. Returns how many were removed. + pub fn cleanup_partials(&self) -> usize { + let leftovers: Vec<(u64, Arc<Mutex<OpenTmp>>)> = match self.open.lock() { + Ok(mut open) => open.drain().collect(), + Err(_) => return 0, + }; + let mut removed = 0; + for (id, open) in leftovers { + drop(open); + if let Some(entry) = self.entries.get(id as usize) { + if fs::remove_file(tmp_path(&self.install_dir, &entry.rel_path)).is_ok() { + removed += 1; + } + } + } + removed + } +} + +impl FetchSink for AmazonSink { + fn process(&self, item: &FetchItem, body: Vec<u8>) -> Result<u64, SinkError> { + if self.cancel.load(Ordering::Relaxed) { + return Err(SinkError::Fatal("cancelled".to_string())); + } + let entry = self.entry(item)?; + self.commit(entry, &body) + } + + fn on_chunk(&self, item: &FetchItem, offset: u64, data: &[u8]) -> Result<(), SinkError> { + if self.cancel.load(Ordering::Relaxed) { + // Java `:342-344`: cancel inside the read loop → tmp deleted (cleanup_partials). + return Err(SinkError::Fatal("cancelled".to_string())); + } + let entry = self.entry(item)?; + let tmp = tmp_path(&self.install_dir, &entry.rel_path); + let open = if offset == 0 { + // (Re)start of an attempt: Java opens a fresh FileOutputStream (truncating). + self.ensure_parent(entry); + let file = File::create(&tmp).map_err(|err| { + let _ = fs::remove_file(&tmp); + SinkError::Retry(format!("create {}: {err}", tmp.display())) + })?; + let open = Arc::new(Mutex::new(OpenTmp { + file, + hasher: Sha256::new(), + written: 0, + })); + match self.open.lock() { + Ok(mut map) => { + map.insert(item.id, Arc::clone(&open)); + } + Err(_) => return Err(SinkError::Fatal("sink map poisoned".to_string())), + } + open + } else { + let found = match self.open.lock() { + Ok(map) => map.get(&item.id).cloned(), + Err(_) => return Err(SinkError::Fatal("sink map poisoned".to_string())), + }; + match found { + Some(open) => open, + None => { + return Err(SinkError::Retry(format!( + "piece at offset {offset} without a stream start for: {}", + entry.rel_path + ))) + } + } + }; + let Ok(mut state) = open.lock() else { + return Err(SinkError::Fatal("sink item poisoned".to_string())); + }; + if state.written != offset { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Retry(format!( + "piece out of order ({} != {offset}) for: {}", + state.written, entry.rel_path + ))); + } + if let Err(err) = state.file.write_all(data) { + // Java: IOException inside downloadFile → tmp deleted → next attempt. + let _ = fs::remove_file(&tmp); + return Err(SinkError::Retry(format!("write {}: {err}", tmp.display()))); + } + state.hasher.update(data); + state.written = state.written.saturating_add(data.len() as u64); + Ok(()) + } + + fn on_finish(&self, item: &FetchItem, total_len: u64) -> Result<u64, SinkError> { + let entry = self.entry(item)?; + let tmp = tmp_path(&self.install_dir, &entry.rel_path); + let taken = match self.open.lock() { + Ok(mut map) => map.remove(&item.id), + Err(_) => return Err(SinkError::Fatal("sink map poisoned".to_string())), + }; + let (written, digest) = match taken { + Some(open) => { + let Ok(mut state) = open.lock() else { + return Err(SinkError::Fatal("sink item poisoned".to_string())); + }; + if let Err(err) = state.file.flush() { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Retry(format!("flush {}: {err}", tmp.display()))); + } + let written = state.written; + let digest = std::mem::replace(&mut state.hasher, Sha256::new()).finalize(); + (written, digest) + } + None => { + // Zero-byte body: no piece ever arrived. Java would have created an empty tmp. + if total_len != 0 { + return Err(SinkError::Retry(format!( + "finish without a stream for: {}", + entry.rel_path + ))); + } + self.ensure_parent(entry); + if let Err(err) = File::create(&tmp) { + return Err(SinkError::Retry(format!("create {}: {err}", tmp.display()))); + } + (0, Sha256::new().finalize()) + } + }; + if written != total_len { + let _ = fs::remove_file(&tmp); + return Err(SinkError::Retry(format!( + "stream length {written} != {total_len} for: {}", + entry.rel_path + ))); + } + self.verify(entry, digest.as_slice())?; + self.rename_into_place(entry)?; + Ok(0) + } +} + +/// Outcome of one `run_download`. +#[derive(Clone, Debug, Default)] +pub struct RunResult { + pub success: bool, + pub cancelled: bool, + pub error: String, + /// Bytes fetched by this run (skipped files excluded; failed attempts' pieces included, + /// like Java's `totalDownloaded`). + pub bytes_written: u64, + /// Files committed by this run plus files skipped by the resume check. + pub files_done: u64, + pub files_total: u64, +} + +/// Throughput bookkeeping for the final summary line (avg over the run, peak over ≥500 ms +/// progress intervals — the same sampling window the Java speed label uses). +struct SpeedMeter { + started: Instant, + last_at: Instant, + last_bytes: u64, + peak_bps: f64, +} + +impl SpeedMeter { + fn new() -> Self { + let now = Instant::now(); + Self { + started: now, + last_at: now, + last_bytes: 0, + peak_bps: 0.0, + } + } + + fn observe(&mut self, bytes: u64) { + let now = Instant::now(); + let dt = now.duration_since(self.last_at).as_secs_f64(); + if dt >= 0.5 { + let bps = bytes.saturating_sub(self.last_bytes) as f64 / dt; + if bps > self.peak_bps { + self.peak_bps = bps; + } + self.last_at = now; + self.last_bytes = bytes; + } + } + + fn summary(&self, bytes: u64) -> (f64, f64, f64) { + let elapsed = self.started.elapsed().as_secs_f64().max(0.001); + let avg = bytes as f64 / elapsed; + (elapsed, avg * 8.0 / 1_000_000.0, self.peak_bps * 8.0 / 1_000_000.0) + } +} + +/// Blocking: plan → stream-fetch → verify/rename, calling `progress(bytes_done, bytes_total, +/// files_done, files_total)` after each written piece and each finished file (skipped files +/// are pre-credited) and `log` for every diagnostic line. `bytes_done` includes skipped bytes, +/// matching Java's `totalDownloaded` (which credits skipped files at `:251`). +pub fn run_download( + plan_json: &str, + install_dir: &str, + ca_bundle_path: &str, + max_workers: usize, + process_workers: usize, + cancel: Arc<AtomicBool>, + progress: &(dyn Fn(u64, u64, u64, u64) + Sync), + log: &(dyn Fn(&str) + Sync), +) -> RunResult { + let max_workers = if max_workers == 0 { MAX_PARALLEL } else { max_workers }; + let process_workers = process_workers.max(1); + let entries = match parse_plan(plan_json) { + Ok(entries) => entries, + Err(err) => { + log(&format!("engine=rust plan error: {err}")); + return RunResult { + error: err, + ..RunResult::default() + }; + } + }; + let plan = build_plan(entries, install_dir); + let files_total = plan.entries.len() as u64; + let fetch_bytes: u64 = plan.items.iter().map(|item| item.reserve).sum(); + let per_host_cap = per_host_cap_for(max_workers, plan.hosts.len()); + log(&format!( + "engine=rust mode=stream plan={} files skip={} ({} bytes) fetch={} ({} bytes) hosts={} workers={} per_host_cap={} process={} dir={} {}", + files_total, + plan.skipped_files, + plan.skipped_bytes, + plan.items.len(), + fetch_bytes, + plan.hosts.len(), + max_workers, + per_host_cap, + process_workers, + install_dir, + crate::priority::log_field() + )); + progress(plan.skipped_bytes, plan.total_bytes, plan.skipped_files, files_total); + if plan.items.is_empty() { + log("summary bytes=0 elapsed=0.000 avg_mbps=0.0 peak_mbps=0.0 files=all-present"); + return RunResult { + success: true, + files_done: plan.skipped_files, + files_total, + ..RunResult::default() + }; + } + if cancel.load(Ordering::Relaxed) { + return RunResult { + cancelled: true, + error: "cancelled".to_string(), + files_done: plan.skipped_files, + files_total, + ..RunResult::default() + }; + } + + let opts = FetchOptions { + max_workers, + // Every Amazon file comes from the one signed CDN base: give that host the whole + // ceiling (`per_host_cap_for`), or the core would clamp the window to hosts × cap. + per_host_cap, + timeout: REQUEST_TIMEOUT, + headers: vec![("User-Agent".to_string(), DOWNLOAD_USER_AGENT.to_string())], + ca_bundle_path: ca_bundle_path.to_string(), + process_workers, + label: LABEL.to_string(), + // Whole files, streamed into the tmp as they arrive: 8 wide like Java's pool, memory + // bounded to the pieces not yet written. + stream: true, + ..FetchOptions::default() + }; + let sink = AmazonSink::new(install_dir, plan.entries.clone(), Arc::clone(&cancel)); + let meter = Mutex::new(SpeedMeter::new()); + let skipped_bytes = plan.skipped_bytes; + let skipped_files = plan.skipped_files; + let total_bytes = plan.total_bytes; + let progress_cb = |credited: u64, items_ok: u64| { + if let Ok(mut meter) = meter.lock() { + meter.observe(credited); + } + progress( + skipped_bytes.saturating_add(credited), + total_bytes, + skipped_files.saturating_add(items_ok), + files_total, + ); + }; + let outcome = run_fetch( + plan.items, + &plan.hosts, + &opts, + &sink, + cancel.as_ref(), + &progress_cb, + log, + ); + let leftovers = sink.cleanup_partials(); + let (elapsed, avg_mbps, peak_mbps) = meter + .lock() + .map(|meter| meter.summary(outcome.bytes_credited)) + .unwrap_or((0.0, 0.0, 0.0)); + let files_done = skipped_files.saturating_add(sink.files_done()); + log(&format!( + "summary bytes={} elapsed={:.3} avg_mbps={:.1} peak_mbps={:.1} files={}/{} items_ok={} tmp_removed={} cancelled={} error={}", + outcome.bytes_credited, + elapsed, + avg_mbps, + peak_mbps, + files_done, + files_total, + outcome.items_ok, + leftovers, + outcome.cancelled, + outcome.error.as_deref().unwrap_or("") + )); + let cancelled = outcome.cancelled || cancel.load(Ordering::Relaxed); + let error = if cancelled { + outcome.error.unwrap_or_else(|| "cancelled".to_string()) + } else { + outcome.error.unwrap_or_default() + }; + RunResult { + success: !cancelled && error.is_empty(), + cancelled, + error, + bytes_written: outcome.bytes_credited, + files_done, + files_total, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::AtomicUsize; + + static COUNTER: AtomicUsize = AtomicUsize::new(0); + + fn scratch_dir() -> String { + let n = COUNTER.fetch_add(1, Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!( + "amazon-test-{}-{}", + std::process::id(), + n + )); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + dir.to_string_lossy().into_owned() + } + + fn sha_hex(data: &[u8]) -> String { + crate::gog::to_hex(Sha256::digest(data).as_slice()) + } + + fn item_for(entry: &PlanEntry, id: u64) -> FetchItem { + FetchItem { + id, + urls: vec![entry.url.clone()], + reserve: entry.size, + range: None, + } + } + + const PLAN: &str = r#"[ + {"relPath":"Binaries\\Win64\\Game.exe","url":"https://d1.cdn.example/base/files/aa?Sig=1","size":4,"sha256hex":"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"}, + {"relPath":"data/level1.pak","url":"https://d1.cdn.example/base/files/bb?Sig=1","size":2,"sha256hex":""}, + {"relPath":"readme.txt","url":"https://d2.cdn.example/base/files/cc?Sig=1","size":0,"sha256hex":""} + ]"#; + + #[test] + fn parses_plan_and_normalises_paths() { + let entries = parse_plan(PLAN).unwrap(); + assert_eq!(entries.len(), 3); + assert_eq!(entries[0].rel_path, "Binaries/Win64/Game.exe"); + assert_eq!(entries[0].size, 4); + assert_eq!(entries[0].sha256.len(), 32); + assert!(entries[1].sha256.is_empty()); + assert_eq!(entries[2].size, 0); + } + + #[test] + fn rejects_bad_plan() { + assert!(parse_plan("{}").is_err()); + assert!(parse_plan(r#"[{"url":"x","size":1}]"#).is_err()); + assert!(parse_plan(r#"[{"relPath":"a","size":1}]"#).is_err()); + assert!(parse_plan(r#"[{"relPath":"a","url":"u","size":1,"sha256hex":"zz"}]"#).is_err()); + } + + #[test] + fn hex_roundtrip() { + assert_eq!(hex_decode("00ff10"), Some(vec![0x00, 0xff, 0x10])); + assert_eq!(hex_decode("00FF"), Some(vec![0x00, 0xff])); + assert_eq!(hex_decode("0"), None); + assert_eq!(hex_decode("0g"), None); + } + + #[test] + fn per_host_cap_gives_single_host_the_whole_ceiling() { + assert_eq!(per_host_cap_for(32, 1), 32); + assert_eq!(per_host_cap_for(32, 3), 11); + assert_eq!(per_host_cap_for(8, 1), 8); + assert_eq!(per_host_cap_for(4, 1), 6); + assert_eq!(per_host_cap_for(0, 0), 6); + } + + #[test] + fn host_key_strips_path_and_query() { + assert_eq!( + host_key("https://d1.cdn.example/base/files/aa?Sig=1"), + "https://d1.cdn.example" + ); + assert_eq!(host_key("http://h:8080/x"), "http://h:8080"); + assert_eq!(host_key("nohost"), "https://nohost"); + } + + #[test] + fn dest_path_never_escapes_install_dir() { + assert_eq!( + dest_path("/inst/", "a/b.txt"), + PathBuf::from("/inst/a/b.txt") + ); + assert_eq!(dest_path("/inst", "/abs.txt"), PathBuf::from("/inst//abs.txt")); + assert_eq!(tmp_path("/inst", "a.bin"), PathBuf::from("/inst/a.bin.tmp")); + } + + #[test] + fn build_plan_skips_only_size_matched_files() { + let dir = scratch_dir(); + let entries = parse_plan(PLAN).unwrap(); + // Entry 1 present with the right size → skipped (no hash check even if wrong bytes). + fs::create_dir_all(format!("{dir}/data")).unwrap(); + fs::write(format!("{dir}/data/level1.pak"), b"zz").unwrap(); + // Entry 0 present with the wrong size → fetched. + fs::create_dir_all(format!("{dir}/Binaries/Win64")).unwrap(); + fs::write(format!("{dir}/Binaries/Win64/Game.exe"), b"tes").unwrap(); + let plan = build_plan(entries, &dir); + assert_eq!(plan.total_bytes, 6); + assert_eq!(plan.skipped_files, 1); + assert_eq!(plan.skipped_bytes, 2); + assert_eq!(plan.items.len(), 2); + assert_eq!(plan.items[0].id, 0); + assert_eq!(plan.items[0].reserve, 4); + assert!(plan.items[0].range.is_none()); + assert_eq!(plan.items[1].id, 2); + assert_eq!( + plan.hosts, + vec!["https://d1.cdn.example".to_string(), "https://d2.cdn.example".to_string()] + ); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn zero_size_missing_file_is_fetched_and_present_zero_is_skipped() { + let dir = scratch_dir(); + let entry = PlanEntry { + rel_path: "empty.txt".into(), + url: "https://h/x".into(), + size: 0, + sha256: Vec::new(), + }; + assert!(!is_present_and_complete(&dir, &entry)); + fs::write(format!("{dir}/empty.txt"), b"").unwrap(); + assert!(is_present_and_complete(&dir, &entry)); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn whole_body_sink_verifies_writes_and_renames() { + let dir = scratch_dir(); + let body = b"test".to_vec(); + let entry = PlanEntry { + rel_path: "sub/Game.exe".into(), + url: "https://h/x".into(), + size: 4, + sha256: hex_decode(&sha_hex(&body)).unwrap(), + }; + let cancel = Arc::new(AtomicBool::new(false)); + let sink = AmazonSink::new(&dir, vec![entry.clone()], Arc::clone(&cancel)); + let item = item_for(&entry, 0); + // Mismatch → Retry, nothing on disk. + match sink.process(&item, b"nope".to_vec()) { + Err(SinkError::Retry(msg)) => assert!(msg.contains("SHA-256 mismatch")), + Err(SinkError::Fatal(msg)) => panic!("expected Retry, got Fatal({msg})"), + Ok(_) => panic!("expected Retry, got Ok"), + } + assert!(!dest_path(&dir, "sub/Game.exe").exists()); + assert!(!tmp_path(&dir, "sub/Game.exe").exists()); + // Match → written, renamed, tmp gone, credited body length. + assert_eq!(sink.process(&item, body.clone()).unwrap(), 4); + assert_eq!(fs::read(dest_path(&dir, "sub/Game.exe")).unwrap(), body); + assert!(!tmp_path(&dir, "sub/Game.exe").exists()); + assert_eq!(sink.files_done(), 1); + // Existing dest is replaced (Java `:280`). + assert_eq!(sink.process(&item, body.clone()).unwrap(), 4); + // Cancel → Fatal, no write. + cancel.store(true, Ordering::Relaxed); + assert!(matches!( + sink.process(&item, body), + Err(SinkError::Fatal(_)) + )); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn stream_sink_appends_verifies_and_renames() { + let dir = scratch_dir(); + let body = b"hello world".to_vec(); + let entry = PlanEntry { + rel_path: "sub/big.bin".into(), + url: "https://h/x".into(), + size: body.len() as u64, + sha256: hex_decode(&sha_hex(&body)).unwrap(), + }; + let sink = AmazonSink::new(&dir, vec![entry.clone()], Arc::new(AtomicBool::new(false))); + let item = item_for(&entry, 0); + sink.on_chunk(&item, 0, b"hello").unwrap(); + assert!(tmp_path(&dir, "sub/big.bin").exists()); + sink.on_chunk(&item, 5, b" wor").unwrap(); + sink.on_chunk(&item, 9, b"ld").unwrap(); + assert_eq!(sink.on_finish(&item, 11).unwrap(), 0); + assert_eq!(fs::read(dest_path(&dir, "sub/big.bin")).unwrap(), body); + assert!(!tmp_path(&dir, "sub/big.bin").exists()); + assert_eq!(sink.files_done(), 1); + assert_eq!(sink.cleanup_partials(), 0); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn stream_sink_restart_truncates_and_mismatch_retries() { + let dir = scratch_dir(); + let body = b"abcdef".to_vec(); + let entry = PlanEntry { + rel_path: "f.bin".into(), + url: "https://h/x".into(), + size: 6, + sha256: hex_decode(&sha_hex(&body)).unwrap(), + }; + let sink = AmazonSink::new(&dir, vec![entry.clone()], Arc::new(AtomicBool::new(false))); + let item = item_for(&entry, 0); + // Attempt 1 delivers garbage, then the core restarts at offset 0. + sink.on_chunk(&item, 0, b"zzzz").unwrap(); + sink.on_chunk(&item, 0, b"abc").unwrap(); + sink.on_chunk(&item, 3, b"xyz").unwrap(); + match sink.on_finish(&item, 6) { + Err(SinkError::Retry(msg)) => assert!(msg.contains("SHA-256 mismatch")), + Err(SinkError::Fatal(msg)) => panic!("expected Retry, got Fatal({msg})"), + Ok(_) => panic!("expected Retry, got Ok"), + } + assert!(!tmp_path(&dir, "f.bin").exists()); + assert!(!dest_path(&dir, "f.bin").exists()); + // Out-of-order piece → Retry. + sink.on_chunk(&item, 0, b"abc").unwrap(); + assert!(matches!( + sink.on_chunk(&item, 5, b"f"), + Err(SinkError::Retry(_)) + )); + // Length mismatch at finish → Retry. + sink.on_chunk(&item, 0, b"abc").unwrap(); + assert!(matches!(sink.on_finish(&item, 6), Err(SinkError::Retry(_)))); + // Piece without a start → Retry. + assert!(matches!( + sink.on_chunk(&item, 3, b"def"), + Err(SinkError::Retry(_)) + )); + // Good attempt. + sink.on_chunk(&item, 0, b"abc").unwrap(); + sink.on_chunk(&item, 3, b"def").unwrap(); + assert_eq!(sink.on_finish(&item, 6).unwrap(), 0); + assert_eq!(fs::read(dest_path(&dir, "f.bin")).unwrap(), body); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn stream_sink_cleanup_removes_unfinished_tmp_and_zero_len_finishes() { + let dir = scratch_dir(); + let big = PlanEntry { + rel_path: "cut.bin".into(), + url: "https://h/x".into(), + size: 10, + sha256: Vec::new(), + }; + let empty = PlanEntry { + rel_path: "empty.bin".into(), + url: "https://h/y".into(), + size: 0, + sha256: Vec::new(), + }; + let cancel = Arc::new(AtomicBool::new(false)); + let sink = AmazonSink::new(&dir, vec![big.clone(), empty.clone()], Arc::clone(&cancel)); + sink.on_chunk(&item_for(&big, 0), 0, b"12345").unwrap(); + assert!(tmp_path(&dir, "cut.bin").exists()); + // Zero-byte body: on_finish with no pieces creates and commits an empty file. + assert_eq!(sink.on_finish(&item_for(&empty, 1), 0).unwrap(), 0); + assert_eq!(fs::read(dest_path(&dir, "empty.bin")).unwrap(), b""); + // Cancel mid-stream → Fatal; cleanup deletes the partial tmp like Java does. + cancel.store(true, Ordering::Relaxed); + assert!(matches!( + sink.on_chunk(&item_for(&big, 0), 5, b"6"), + Err(SinkError::Fatal(_)) + )); + assert_eq!(sink.cleanup_partials(), 1); + assert!(!tmp_path(&dir, "cut.bin").exists()); + assert!(!dest_path(&dir, "cut.bin").exists()); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn stream_sink_skips_hash_when_manifest_has_none() { + let dir = scratch_dir(); + let entry = PlanEntry { + rel_path: "a.bin".into(), + url: "https://h/x".into(), + size: 3, + sha256: Vec::new(), + }; + let sink = AmazonSink::new(&dir, vec![entry.clone()], Arc::new(AtomicBool::new(false))); + let item = item_for(&entry, 0); + sink.on_chunk(&item, 0, b"abc").unwrap(); + assert_eq!(sink.on_finish(&item, 3).unwrap(), 0); + assert_eq!(fs::read(dest_path(&dir, "a.bin")).unwrap(), b"abc"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn run_download_all_present_short_circuits() { + let dir = scratch_dir(); + fs::write(format!("{dir}/a.bin"), b"abc").unwrap(); + let plan = r#"[{"relPath":"a.bin","url":"https://h/x","size":3,"sha256hex":""}]"#; + let seen = Mutex::new(Vec::new()); + let logs = Mutex::new(Vec::new()); + let result = run_download( + plan, + &dir, + "", + 0, + 1, + Arc::new(AtomicBool::new(false)), + &|done, total, fd, ft| seen.lock().unwrap().push((done, total, fd, ft)), + &|line| logs.lock().unwrap().push(line.to_string()), + ); + assert!(result.success); + assert_eq!(result.files_done, 1); + assert_eq!(result.files_total, 1); + assert_eq!(seen.lock().unwrap().as_slice(), &[(3, 3, 1, 1)]); + assert!(logs.lock().unwrap()[0].starts_with("engine=rust mode=stream")); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn run_download_reports_plan_error() { + let result = run_download( + "not json", + "/nonexistent", + "", + 8, + 2, + Arc::new(AtomicBool::new(false)), + &|_, _, _, _| {}, + &|_| {}, + ); + assert!(!result.success); + assert!(result.error.contains("plan json")); + } +} diff --git a/app/src/main/rust/stores/src/amazon/jni.rs b/app/src/main/rust/stores/src/amazon/jni.rs new file mode 100644 index 0000000000..26797773b8 --- /dev/null +++ b/app/src/main/rust/stores/src/amazon/jni.rs @@ -0,0 +1,229 @@ +//! JNI facade for the Amazon download adapter. +//! +//! Kotlin side: `com.droiddeck.launcher.stores.amazon.AmazonNative` (`nativeStart` / +//! `nativeCancel` / `nativeRelease`) with an `AmazonNative.Listener` +//! (`onProgress(JJJJ)V`, `onLog(String)V`, `onComplete(ZString;J)V`). Mirrors the shape of +//! the GOG and Epic facades: the run happens on a spawned native +//! thread, callbacks attach the JavaVM as daemon threads and hold a `GlobalRef` to the +//! listener; cancel flips an `AtomicBool`. This module keeps its own `JavaVM` handle (taken +//! from the calling `JNIEnv` on first use) so it depends on nothing outside this module. + +use crate::amazon::{run_download, MAX_PARALLEL}; +use jni::objects::{GlobalRef, JClass, JObject, JString, JValue}; +use jni::sys::{jint, jlong, JNI_FALSE, JNI_TRUE}; +use jni::{JNIEnv, JavaVM}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, OnceLock}; +use std::thread; + +/// Logcat tag (`adb logcat -s AmazonNative`). Every engine line reaches logcat exactly ONCE: +/// this module only forwards lines to the listener (`onLog`), and the Java manager is the one +/// place that writes them to `android.util.Log` + its debug file. (Round 1 fix: the native side +/// used to also call `__android_log_write`, so each line appeared twice.) +pub const LOG_TAG: &str = "AmazonNative"; + +static JVM: OnceLock<JavaVM> = OnceLock::new(); + +fn clear_pending_exception(env: &mut JNIEnv) { + if env.exception_check().unwrap_or(false) { + let _ = env.exception_clear(); + } +} + +fn jstring_to_string(env: &mut JNIEnv, value: &JString) -> Option<String> { + env.get_string(value) + .ok() + .map(|s| s.to_string_lossy().into_owned()) +} + +/// One live run; boxed and handed to Kotlin as an opaque `jlong`. +struct AmazonRun { + cancel: Arc<AtomicBool>, +} + +fn to_handle(run: Box<AmazonRun>) -> jlong { + Box::into_raw(run) as jlong +} + +unsafe fn from_handle<'a>(handle: jlong) -> Option<&'a AmazonRun> { + if handle == 0 { + None + } else { + Some(&*(handle as *const AmazonRun)) + } +} + +fn call_log(env: &mut JNIEnv, listener: &JObject, line: &str) { + let Ok(text) = env.new_string(line) else { + clear_pending_exception(env); + return; + }; + let text_obj = JObject::from(text); + let _ = env.call_method( + listener, + "onLog", + "(Ljava/lang/String;)V", + &[JValue::Object(&text_obj)], + ); + clear_pending_exception(env); +} + +fn call_progress( + env: &mut JNIEnv, + listener: &JObject, + bytes_done: u64, + bytes_total: u64, + files_done: u64, + files_total: u64, +) { + let _ = env.call_method( + listener, + "onProgress", + "(JJJJ)V", + &[ + JValue::Long(bytes_done as jlong), + JValue::Long(bytes_total as jlong), + JValue::Long(files_done as jlong), + JValue::Long(files_total as jlong), + ], + ); + clear_pending_exception(env); +} + +fn call_complete( + env: &mut JNIEnv, + listener: &JObject, + success: bool, + error: &str, + bytes_written: u64, +) { + let Ok(error) = env.new_string(error) else { + clear_pending_exception(env); + return; + }; + let error_obj = JObject::from(error); + let _ = env.call_method( + listener, + "onComplete", + "(ZLjava/lang/String;J)V", + &[ + JValue::Bool(if success { JNI_TRUE } else { JNI_FALSE }), + JValue::Object(&error_obj), + JValue::Long(bytes_written as jlong), + ], + ); + clear_pending_exception(env); +} + +/// Attach the current (native) thread and run `f` with the listener object. +fn with_attached_env(listener: &GlobalRef, f: impl FnOnce(&mut JNIEnv, &JObject)) { + let Some(vm) = JVM.get() else { + return; + }; + let Ok(mut env) = vm.attach_current_thread_as_daemon() else { + return; + }; + f(&mut env, listener.as_obj()); +} + +/// `AmazonNative.nativeStart(planJson, installDir, caBundlePath, maxWorkers, +/// processWorkers, listener) -> handle`. Returns 0 (and fires `onComplete(false, …)`) when the +/// inputs are unusable. The run itself happens on a spawned thread. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeStart( + mut env: JNIEnv, + _class: JClass, + plan_json: JString, + install_dir: JString, + ca_bundle_path: JString, + max_workers: jint, + process_workers: jint, + listener: JObject, +) -> jlong { + if listener.is_null() { + return 0; + } + if JVM.get().is_none() { + if let Ok(vm) = env.get_java_vm() { + let _ = JVM.set(vm); + } + } + let plan_json = jstring_to_string(&mut env, &plan_json).unwrap_or_default(); + let install_dir = jstring_to_string(&mut env, &install_dir).unwrap_or_default(); + let ca_bundle_path = jstring_to_string(&mut env, &ca_bundle_path).unwrap_or_default(); + if plan_json.is_empty() || install_dir.is_empty() { + call_complete(&mut env, &listener, false, "empty plan or install dir", 0); + return 0; + } + let Ok(listener) = env.new_global_ref(&listener) else { + clear_pending_exception(&mut env); + return 0; + }; + let max_workers = if max_workers <= 0 { + MAX_PARALLEL + } else { + max_workers as usize + }; + let process_workers = process_workers.max(1) as usize; + let cancel = Arc::new(AtomicBool::new(false)); + let run = Box::new(AmazonRun { + cancel: Arc::clone(&cancel), + }); + + thread::spawn(move || { + crate::priority::background(); + let log_listener = listener.clone(); + let log = move |line: &str| { + with_attached_env(&log_listener, |env, obj| call_log(env, obj, line)); + }; + let progress_listener = listener.clone(); + let progress = move |bytes_done: u64, bytes_total: u64, files_done: u64, files_total: u64| { + with_attached_env(&progress_listener, |env, obj| { + call_progress(env, obj, bytes_done, bytes_total, files_done, files_total) + }); + }; + let result = run_download( + &plan_json, + &install_dir, + &ca_bundle_path, + max_workers, + process_workers, + cancel, + &progress, + &log, + ); + with_attached_env(&listener, |env, obj| { + call_complete(env, obj, result.success, &result.error, result.bytes_written) + }); + }); + + to_handle(run) +} + +/// `AmazonNative.nativeCancel(handle)` — flips the run's cancel flag; the core stops +/// issuing requests, in-flight bodies are dropped, `onComplete(false, "cancelled", …)` follows. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeCancel( + _env: JNIEnv, + _class: JClass, + handle: jlong, +) { + if let Some(run) = unsafe { from_handle(handle) } { + run.cancel.store(true, Ordering::Relaxed); + } +} + +/// `AmazonNative.nativeRelease(handle)` — frees the handle. Safe at any time: the worker +/// thread owns its own `Arc` of the cancel flag and the listener `GlobalRef`. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_amazon_AmazonNative_nativeRelease( + _env: JNIEnv, + _class: JClass, + handle: jlong, +) { + if handle != 0 { + unsafe { + drop(Box::from_raw(handle as *mut AmazonRun)); + } + } +} diff --git a/app/src/main/rust/stores/src/epic.rs b/app/src/main/rust/stores/src/epic.rs new file mode 100644 index 0000000000..258c273b96 --- /dev/null +++ b/app/src/main/rust/stores/src/epic.rs @@ -0,0 +1,46 @@ +//! Epic Games Store download adapter for the shared fetch core (`crate::fetch_core`). +//! +//! Scope = the chunk-fetch inner loop of `EpicDownloadManager.java` (the fixed 8-thread pool that +//! fills `<installDir>/.chunks/<GUID>`), nothing more. The Java manager still parses the manifest +//! API JSON, downloads the manifest, selects files (install tags), runs the delta/verify pass, +//! assembles files from the chunk cache and does every post-install step. The adapter re-parses +//! the same manifest bytes, rebuilds the same chunk plan for the pending files Java hands it, +//! skips chunks already in the cache and writes verified chunks with the same `.part` + rename +//! protocol, so the on-disk state is identical whichever engine ran (see +//! Bannerlator's `docs/RUST_EPIC_PARITY.md` for the rule-by-rule table). +//! +//! Submodules: +//! - [`manifest`] — ChunksV4 binary manifest + legacy JSON manifest parsers (1:1 with the Java +//! `parseManifest` / `parseJsonManifest`). +//! - [`plan`] — install-tag selection, unique-chunk plan, chunk paths / CDN URLs. +//! - [`chunk`] — chunk header parse, zlib inflate, SHA-1 verify, cache write. +//! - [`driver`] — plan → `FetchItem`s → `fetch_core::run_fetch` with the chunk-cache sink. +//! - [`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. +//! - [`jni`] — `Java_com_droiddeck_launcher_stores_epic_EpicNative_native*` exports. +//! +//! The selective-install-tag, delta/resume and chunk verification rules mirrored here were ported +//! into the Java manager from GameNative (`service/epic/EpicDownloadManager.kt`, +//! `manifest/ManifestUtils.kt`), itself derived from Legendary; both are GPL-3.0 and this crate +//! carries the same licence (see `Cargo.toml`). + +pub mod assemble; +pub mod chunk; +pub mod driver; +pub mod jni; +pub mod manifest; +pub mod plan; + +/// User-Agent the Java manager sends on every chunk request (`EpicDownloadManager.UA`). +pub const USER_AGENT: &str = + "UELauncher/11.0.1-14907503+++Portal+Release-Live Windows/10.0.19041.1.256.64bit"; + +/// Name of the chunk cache directory under the install dir (`new File(installDir, ".chunks")`). +pub const CHUNK_CACHE_DIR: &str = ".chunks"; + +/// Fixed pool width of the Java chunk downloader (`Executors.newFixedThreadPool(8)`). +pub const JAVA_POOL_THREADS: usize = 8; + +/// logcat tag for every engine line (the Kotlin manager mirrors it into its own debug log). +pub const LOG_TAG: &str = "EpicNative"; diff --git a/app/src/main/rust/stores/src/epic/assemble.rs b/app/src/main/rust/stores/src/epic/assemble.rs new file mode 100644 index 0000000000..580cb8c241 --- /dev/null +++ b/app/src/main/rust/stores/src/epic/assemble.rs @@ -0,0 +1,525 @@ +//! Files from the chunk cache: the last stage of an Epic install, as a native run. +//! +//! The Kotlin manager's own loop does this too (`EpicDownloadManager.install`, after the chunk +//! fetch): for every pending file, open it, and for every part read the cached chunk and write +//! `chunk[offset..offset+size]`. This module does the same thing, in the same order, with the +//! same wording on a missing chunk, plus what the Kotlin loop cannot do without re-reading the +//! finished file from a slow card: +//! +//! - the file's SHA-1 is computed from the bytes as they are written (no extra I/O) and compared +//! with the manifest's, when the manifest has one; a mismatch deletes the file and fails the +//! run, so a bad assembly can never pass the next delta pass by size alone; +//! - with a scratch cache — one the caller placed apart from the install directory — every chunk +//! is reference-counted across the pending files and deleted the moment the last file that +//! needs it is written and verified, so the cache never holds more than what is still owed. +//! +//! The cache directory itself goes at the end of a successful run, scratch or not, which is what +//! the Kotlin loop does with `<installDir>/.chunks`. Cancellation and failure leave the cache +//! exactly as it stands, so the next run's delta pass skips the finished files and the fetch +//! skips the chunks still cached — on either engine. +//! +//! Why the cache may live elsewhere at all: an install to an SD card wrote every byte twice to +//! the card (chunk cache, then assembled file). With the cache on internal storage the card sees +//! each byte once, and the fetchers stop waiting on the card's write speed. + +use std::collections::HashMap; +use std::fs::{self, File}; +use std::io::{BufWriter, Write}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; + +use sha1::{Digest, Sha1}; + +use super::manifest::{parse_manifest, FileInfo, Manifest}; +use super::plan::resolve_cache_dir; + +/// Everything the JNI layer hands over for one assembly run. +pub struct AssembleRequest { + pub manifest_bytes: Vec<u8>, + pub install_dir: String, + /// `""` = `<installDir>/.chunks` (no scratch semantics); anything else = scratch cache. + pub chunk_cache_dir: String, + /// Indices into `manifest.files` of the files to assemble, in order. + pub file_indices: Vec<usize>, +} + +/// Terminal result of an assembly run. +#[derive(Clone, Debug, Default)] +pub struct AssembleOutcome { + pub success: bool, + pub cancelled: bool, + pub error: String, + /// Bytes written into assembled files this run. + pub bytes_written: u64, + pub files_done: u64, + pub files_total: u64, + pub bytes_total: u64, + /// Chunk files deleted early because no remaining file needed them (scratch cache only). + pub chunks_removed: u64, +} + +/// The parsed plan: manifest, the files to write, and how many of them still need each chunk. +/// Built up front so a bad index fails before anything is written. +#[derive(Debug)] +pub struct AssemblePlan { + pub manifest: Manifest, + pub install_dir: PathBuf, + pub cache_dir: PathBuf, + pub scratch_cache: bool, + pub files: Vec<usize>, + pub bytes_total: u64, + /// GUID → number of files in `files` whose parts reference it (a file counts once per GUID, + /// however many of its parts use the chunk). + pub refs: HashMap<String, u32>, +} + +/// Parse + plan. `Err` = nothing was written. +pub fn build_assemble_plan(req: &AssembleRequest) -> Result<AssemblePlan, String> { + if req.install_dir.is_empty() { + return Err("assemble: empty install dir".to_string()); + } + let manifest = parse_manifest(&req.manifest_bytes).map_err(|e| format!("assemble: {e}"))?; + for &i in &req.file_indices { + if i >= manifest.files.len() { + return Err(format!( + "assemble: file index {i} out of range ({} files)", + manifest.files.len() + )); + } + } + let refs = chunk_refs(&manifest, &req.file_indices); + let bytes_total = req + .file_indices + .iter() + .map(|&i| manifest.files[i].file_size()) + .sum(); + Ok(AssemblePlan { + install_dir: PathBuf::from(&req.install_dir), + cache_dir: resolve_cache_dir(&req.install_dir, &req.chunk_cache_dir), + scratch_cache: !req.chunk_cache_dir.is_empty(), + files: req.file_indices.clone(), + bytes_total, + refs, + manifest, + }) +} + +/// How many of `file_indices` reference each chunk GUID. +pub fn chunk_refs(m: &Manifest, file_indices: &[usize]) -> HashMap<String, u32> { + let mut refs: HashMap<String, u32> = HashMap::new(); + for &fi in file_indices { + let Some(f) = m.files.get(fi) else { + continue; + }; + for g in distinct_guids(f) { + *refs.entry(g).or_insert(0) += 1; + } + } + refs +} + +/// The GUIDs a file's parts reference, each once, in first-seen order. +fn distinct_guids(f: &FileInfo) -> Vec<String> { + let mut out: Vec<String> = Vec::with_capacity(f.parts.len()); + for p in &f.parts { + let g = p.guid_str(); + if !out.contains(&g) { + out.push(g); + } + } + out +} + +/// The manifest's relative path with Windows separators normalised, as the Kotlin loop does +/// (`filename.replace("\\", "/")`). +pub fn relative_path(f: &FileInfo) -> String { + f.filename.replace('\\', "/") +} + +/// A manifest SHA-1 that is all zero means "none" (the Kotlin delta pass treats it the same way). +fn expected_sha1(f: &FileInfo) -> Option<&[u8; 20]> { + f.sha1.as_ref().filter(|h| h.iter().any(|&b| b != 0)) +} + +/// Chunk bytes, read whole, with the last chunk kept: consecutive parts of one file usually come +/// from the same chunk, and a file's parts are the only place a chunk is read. +struct ChunkReader { + cache_dir: PathBuf, + last: Option<(String, Vec<u8>)>, +} + +impl ChunkReader { + fn read(&mut self, guid: &str, rel_path: &str) -> Result<&[u8], String> { + if self.last.as_ref().map(|(g, _)| g.as_str()) != Some(guid) { + let path = self.cache_dir.join(guid); + if !path.exists() { + return Err(format!("Missing chunk {guid} for {rel_path}")); + } + let data = fs::read(&path).map_err(|e| format!("read chunk {guid}: {e}"))?; + self.last = Some((guid.to_string(), data)); + } + Ok(self.last.as_ref().map(|(_, d)| d.as_slice()).unwrap_or(&[])) + } + + /// Drop the kept copy of a chunk that is about to be deleted. + fn forget(&mut self, guid: &str) { + if self.last.as_ref().map(|(g, _)| g.as_str()) == Some(guid) { + self.last = None; + } + } +} + +/// Write one file from the cache. Returns the bytes written. The output is removed on any error, +/// so a file only ever exists complete (and, when the manifest hashes it, verified) or not at all. +fn write_file(reader: &mut ChunkReader, f: &FileInfo, out_path: &Path) -> Result<u64, String> { + let rel_path = relative_path(f); + if let Some(parent) = out_path.parent() { + fs::create_dir_all(parent).map_err(|e| format!("mkdirs {}: {e}", parent.display()))?; + } + let result = (|| { + let file = File::create(out_path).map_err(|e| format!("create {rel_path}: {e}"))?; + let mut out = BufWriter::with_capacity(1024 * 1024, file); + let mut sha = expected_sha1(f).map(|_| Sha1::new()); + let mut written: u64 = 0; + for part in &f.parts { + let guid = part.guid_str(); + let data = reader.read(&guid, &rel_path)?; + let start = usize::try_from(part.offset).map_err(|_| { + format!("Chunk {guid} part offset {} invalid for {rel_path}", part.offset) + })?; + let len = part.size as u32 as usize; + let end = start + .checked_add(len) + .filter(|&e| e <= data.len()) + .ok_or_else(|| { + format!( + "Chunk {guid} too short for {rel_path}: need {start}+{len}, have {}", + data.len() + ) + })?; + let slice = &data[start..end]; + out.write_all(slice).map_err(|e| format!("write {rel_path}: {e}"))?; + if let Some(sha) = sha.as_mut() { + sha.update(slice); + } + written += len as u64; + } + out.flush().map_err(|e| format!("flush {rel_path}: {e}"))?; + drop(out); + if let (Some(expected), Some(sha)) = (expected_sha1(f), sha) { + if sha.finalize().as_slice() != &expected[..] { + return Err(format!("SHA-1 mismatch for {rel_path}")); + } + } + Ok(written) + })(); + if result.is_err() { + let _ = fs::remove_file(out_path); + } + result +} + +/// Run the plan. `progress(bytes_done, bytes_total, files_done, files_total)` fires after every +/// file; `log` receives the start line, failures and the terminal summary. +pub fn run_assemble( + plan: &AssemblePlan, + cancel: &AtomicBool, + progress: &(dyn Fn(u64, u64, u64, u64) + Sync), + log: &(dyn Fn(&str) + Sync), +) -> AssembleOutcome { + let files_total = plan.files.len() as u64; + let mut outcome = AssembleOutcome { + files_total, + bytes_total: plan.bytes_total, + ..AssembleOutcome::default() + }; + log(&format!( + "assemble cache_dir={} scratch={} {} files={files_total} bytes={} chunks_referenced={}", + plan.cache_dir.display(), + plan.scratch_cache, + crate::priority::log_field(), + plan.bytes_total, + plan.refs.len() + )); + + let mut refs = plan.refs.clone(); + let mut reader = ChunkReader { + cache_dir: plan.cache_dir.clone(), + last: None, + }; + for &fi in &plan.files { + if cancel.load(Ordering::Relaxed) { + outcome.cancelled = true; + outcome.error = "cancelled".to_string(); + log(&format!( + "cancelled during assembly ({}/{files_total} files)", + outcome.files_done + )); + return outcome; + } + let f = &plan.manifest.files[fi]; + let out_path = plan.install_dir.join(relative_path(f)); + match write_file(&mut reader, f, &out_path) { + Ok(written) => outcome.bytes_written += written, + Err(err) => { + outcome.error = err; + log(&format!( + "FAIL {} ({}/{files_total} files ok)", + outcome.error, outcome.files_done + )); + return outcome; + } + } + // Scratch cache: this file was the last user of a chunk → the chunk goes now, not at the + // end, so the cache holds only what is still owed to files not yet written. Released + // before the progress event, so "file done" also means "its chunks are freed". + if plan.scratch_cache { + for guid in distinct_guids(f) { + let Some(count) = refs.get_mut(&guid) else { + continue; + }; + *count = count.saturating_sub(1); + if *count == 0 { + reader.forget(&guid); + if fs::remove_file(plan.cache_dir.join(&guid)).is_ok() { + outcome.chunks_removed += 1; + } + } + } + } + outcome.files_done += 1; + progress( + outcome.bytes_written, + plan.bytes_total, + outcome.files_done, + files_total, + ); + } + + outcome.success = true; + // Done with the cache: remove it whole (a scratch cache is already down to the chunks no + // pending file referenced; the default one is what the Kotlin loop deleted at this point). + match fs::remove_dir_all(&plan.cache_dir) { + Ok(()) => log(&format!("cache removed {}", plan.cache_dir.display())), + Err(e) => log(&format!( + "cache not removed {}: {e}", + plan.cache_dir.display() + )), + } + log(&format!( + "assembled files={} bytes={} chunks_removed_early={}", + outcome.files_done, outcome.bytes_written, outcome.chunks_removed + )); + outcome +} + +#[cfg(test)] +mod tests { + use super::super::chunk::test_support::temp_dir; + use super::super::manifest::test_support::*; + use super::*; + use std::sync::Mutex; + + const A: [u32; 4] = [1, 2, 3, 4]; + const B: [u32; 4] = [5, 6, 7, 8]; + + fn chunk_a() -> Vec<u8> { + (0..300u32).map(|i| (i % 251) as u8).collect() + } + + fn chunk_b() -> Vec<u8> { + (0..100u32).map(|i| (200 - i) as u8).collect() + } + + fn sha(bytes: &[u8]) -> [u8; 20] { + let mut out = [0u8; 20]; + out.copy_from_slice(&Sha1::digest(bytes)); + out + } + + /// file 0 = A[0..100] ++ B[10..60]; file 1 = A[100..300]. A is shared, B is file 0's alone. + fn expected_files() -> (Vec<u8>, Vec<u8>) { + let (a, b) = (chunk_a(), chunk_b()); + let mut f0 = a[0..100].to_vec(); + f0.extend_from_slice(&b[10..60]); + (f0, a[100..300].to_vec()) + } + + fn manifest_bytes(file0_sha: [u8; 20]) -> Vec<u8> { + let (_, f1) = expected_files(); + let chunks = vec![ + TestChunk { guid: A, hash: 1, sha1: [0x11; 20], group: 1, window: 300, file_size: 300 }, + TestChunk { guid: B, hash: 2, sha1: [0x22; 20], group: 2, window: 100, file_size: 100 }, + ]; + let files = vec![ + TestFile { + name: "Game\\Binaries\\Game.exe".to_string(), + sha1: file0_sha, + tags: vec![], + parts: vec![(A, 0, 100), (B, 10, 50)], + }, + TestFile { + name: "Game/Content/data.pak".to_string(), + sha1: sha(&f1), + tags: vec![], + parts: vec![(A, 100, 200)], + }, + ]; + build_manifest(&chunks, &files, 21, false) + } + + fn guid(g: [u32; 4]) -> String { + super::super::manifest::guid_to_string(&g) + } + + fn fill_cache(cache: &Path) { + fs::create_dir_all(cache).unwrap(); + fs::write(cache.join(guid(A)), chunk_a()).unwrap(); + fs::write(cache.join(guid(B)), chunk_b()).unwrap(); + } + + fn request(install: &Path, cache: &str) -> AssembleRequest { + let (f0, _) = expected_files(); + AssembleRequest { + manifest_bytes: manifest_bytes(sha(&f0)), + install_dir: install.to_str().unwrap().to_string(), + chunk_cache_dir: cache.to_string(), + file_indices: vec![0, 1], + } + } + + fn run(plan: &AssemblePlan, cancel: bool) -> (AssembleOutcome, Vec<(u64, u64, u64, u64)>) { + let calls = Mutex::new(Vec::new()); + let progress = |b: u64, bt: u64, f: u64, ft: u64| calls.lock().unwrap().push((b, bt, f, ft)); + let log = |_: &str| {}; + let out = run_assemble(plan, &AtomicBool::new(cancel), &progress, &log); + (out, calls.into_inner().unwrap()) + } + + #[test] + fn default_cache_assembles_like_the_kotlin_loop_and_removes_the_cache_at_the_end() { + let dir = temp_dir("asm-default"); + let install = dir.join("install"); + let cache = install.join(".chunks"); + fill_cache(&cache); + let plan = build_assemble_plan(&request(&install, "")).unwrap(); + assert!(!plan.scratch_cache); + assert_eq!(plan.refs[&guid(A)], 2); + assert_eq!(plan.refs[&guid(B)], 1); + let (out, calls) = run(&plan, false); + assert!(out.success, "{}", out.error); + let (f0, f1) = expected_files(); + assert_eq!(fs::read(install.join("Game/Binaries/Game.exe")).unwrap(), f0); + assert_eq!(fs::read(install.join("Game/Content/data.pak")).unwrap(), f1); + assert_eq!(out.bytes_written, 350); + assert_eq!(calls, vec![(150, 350, 1, 2), (350, 350, 2, 2)]); + assert_eq!(out.chunks_removed, 0, "no early deletion without a scratch cache"); + assert!(!cache.exists(), "the cache goes at the end, as the Kotlin loop's deleteDir"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn scratch_cache_frees_each_chunk_when_its_last_file_is_done() { + let dir = temp_dir("asm-scratch"); + let install = dir.join("install"); + let cache = dir.join("cache"); + fill_cache(&cache); + let plan = build_assemble_plan(&request(&install, cache.to_str().unwrap())).unwrap(); + assert!(plan.scratch_cache); + // After file 0: B (only file 0 used it) must be gone, A (file 1 still needs it) must stay. + let seen = Mutex::new(Vec::new()); + let progress = |_: u64, _: u64, files_done: u64, _: u64| { + if files_done == 1 { + seen.lock().unwrap().push((cache.join(guid(A)).exists(), cache.join(guid(B)).exists())); + } + }; + let log = |_: &str| {}; + let out = run_assemble(&plan, &AtomicBool::new(false), &progress, &log); + assert!(out.success, "{}", out.error); + assert_eq!(*seen.lock().unwrap(), vec![(true, false)]); + assert_eq!(out.chunks_removed, 2); + assert!(!cache.exists()); + let (f0, f1) = expected_files(); + assert_eq!(fs::read(install.join("Game/Binaries/Game.exe")).unwrap(), f0); + assert_eq!(fs::read(install.join("Game/Content/data.pak")).unwrap(), f1); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn a_missing_chunk_fails_with_the_kotlin_wording_and_leaves_the_cache() { + let dir = temp_dir("asm-missing"); + let install = dir.join("install"); + let cache = dir.join("cache"); + fill_cache(&cache); + fs::remove_file(cache.join(guid(B))).unwrap(); + let plan = build_assemble_plan(&request(&install, cache.to_str().unwrap())).unwrap(); + let (out, calls) = run(&plan, false); + assert!(!out.success && !out.cancelled); + assert_eq!(out.error, format!("Missing chunk {} for Game/Binaries/Game.exe", guid(B))); + assert!(calls.is_empty()); + assert!(!install.join("Game/Binaries/Game.exe").exists(), "no partial file survives"); + assert!(cache.join(guid(A)).exists(), "failure leaves the cache for the next run"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn a_sha1_mismatch_deletes_the_file_and_fails() { + let dir = temp_dir("asm-sha"); + let install = dir.join("install"); + let cache = dir.join("cache"); + fill_cache(&cache); + let mut req = request(&install, cache.to_str().unwrap()); + req.manifest_bytes = manifest_bytes([0x99; 20]); + let plan = build_assemble_plan(&req).unwrap(); + let (out, _) = run(&plan, false); + assert!(!out.success); + assert_eq!(out.error, "SHA-1 mismatch for Game/Binaries/Game.exe"); + assert!(!install.join("Game/Binaries/Game.exe").exists()); + assert!(cache.join(guid(B)).exists(), "nothing was freed for a file that failed"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn an_all_zero_manifest_hash_means_no_check() { + let dir = temp_dir("asm-nohash"); + let install = dir.join("install"); + let cache = install.join(".chunks"); + fill_cache(&cache); + let mut req = request(&install, ""); + req.manifest_bytes = manifest_bytes([0u8; 20]); + let plan = build_assemble_plan(&req).unwrap(); + let (out, _) = run(&plan, false); + assert!(out.success, "{}", out.error); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn cancel_before_the_first_file_writes_nothing_and_keeps_the_cache() { + let dir = temp_dir("asm-cancel"); + let install = dir.join("install"); + let cache = dir.join("cache"); + fill_cache(&cache); + let plan = build_assemble_plan(&request(&install, cache.to_str().unwrap())).unwrap(); + let (out, calls) = run(&plan, true); + assert!(out.cancelled && !out.success); + assert!(calls.is_empty()); + assert!(cache.join(guid(A)).exists() && cache.join(guid(B)).exists()); + assert!(!install.join("Game").exists()); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn bad_inputs_fail_before_anything_is_written() { + let dir = temp_dir("asm-inputs"); + let mut req = request(&dir, ""); + req.file_indices = vec![0, 2]; + assert!(build_assemble_plan(&req).unwrap_err().contains("out of range")); + req.file_indices = vec![0]; + req.install_dir.clear(); + assert!(build_assemble_plan(&req).unwrap_err().contains("empty install dir")); + req.install_dir = dir.to_str().unwrap().to_string(); + req.manifest_bytes = vec![1, 2, 3]; + assert!(build_assemble_plan(&req).is_err()); + let _ = fs::remove_dir_all(&dir); + } +} diff --git a/app/src/main/rust/stores/src/epic/chunk.rs b/app/src/main/rust/stores/src/epic/chunk.rs new file mode 100644 index 0000000000..22ebb864db --- /dev/null +++ b/app/src/main/rust/stores/src/epic/chunk.rs @@ -0,0 +1,375 @@ +//! One chunk: header parse → (zlib inflate | copy) → SHA-1 → `.part` → rename into the cache. +//! +//! Port of `EpicDownloadManager.downloadChunkStreaming` minus the HTTP part (the fetch core +//! hands us the whole response body). Java's protocol, kept exactly: +//! - 41-byte header: magic(4) headerVersion(4) headerSize(4) compressedSize(4) GUID(16) hash(8) +//! storedAs(1); payload starts at `max(headerSize, 41)` (Java never seeks back); +//! - payload = the next `compressedSize` bytes, or whatever the body still has (a short body +//! is NOT an error — the SHA-1 check decides); +//! - `storedAs & 1` → zlib; corrupt zlib = failure; a stream that ends early = partial output; +//! - the decompressed bytes go to `<final>.part`; the SHA-1 (when the manifest has one) must +//! match or the `.part` is deleted; then an atomic rename publishes `<final>`. +//! A cache file therefore exists under its final name ONLY if it fully downloaded and verified, +//! on either engine. + +use std::fs::{self, File}; +use std::io::{self, BufWriter, Write}; +use std::path::{Path, PathBuf}; + +use sha1::{Digest, Sha1}; + +/// Chunk file magic (`0xB1FE3AA2`). +pub const CHUNK_MAGIC: u32 = 0xB1FE_3AA2; + +/// Bytes Java reads before looking at the header (`byte[] hdrBuf = new byte[41]`). +pub const CHUNK_HEADER_PREFIX: usize = 41; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ChunkHeader { + pub header_version: i32, + pub header_size: i32, + pub compressed_size: i32, + pub stored_as: u8, +} + +impl ChunkHeader { + pub fn is_compressed(&self) -> bool { + (self.stored_as & 1) != 0 + } + + /// Where the payload begins in the body (`if (headerSize > 41) skipFully(headerSize - 41)`). + pub fn payload_start(&self) -> usize { + if self.header_size > CHUNK_HEADER_PREFIX as i32 { + self.header_size as usize + } else { + CHUNK_HEADER_PREFIX + } + } +} + +/// `readFully(hdrBuf)` + header decode. `Err` = the Java `continue`/exception paths +/// (short stream, bad magic). +pub fn parse_chunk_header(body: &[u8]) -> Result<ChunkHeader, String> { + if body.len() < CHUNK_HEADER_PREFIX { + return Err(format!( + "Stream ended after {}/{} bytes", + body.len(), + CHUNK_HEADER_PREFIX + )); + } + let le = |o: usize| i32::from_le_bytes([body[o], body[o + 1], body[o + 2], body[o + 3]]); + let magic = le(0) as u32; + if magic != CHUNK_MAGIC { + return Err(format!("Bad chunk magic (streaming): 0x{magic:x}")); + } + Ok(ChunkHeader { + header_version: le(4), + header_size: le(8), + compressed_size: le(12), + stored_as: body[40], + }) +} + +/// The payload slice Java's read loop would consume: `compressedSize` bytes after the header, +/// truncated to what the body holds (`if (n <= 0) break;`). `Err` when the header skip runs +/// past the body (`skipFully` throws). +pub fn chunk_payload<'a>(body: &'a [u8], hdr: &ChunkHeader) -> Result<&'a [u8], String> { + 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 take = want.min(avail); + Ok(&body[start..start + take]) +} + +/// `Writer` that mirrors `fos.write(...)` + `sha.update(...)` on every block. +struct HashingWriter<W: Write> { + inner: W, + sha: Option<Sha1>, + written: u64, +} + +impl<W: Write> Write for HashingWriter<W> { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + let n = self.inner.write(buf)?; + if let Some(sha) = self.sha.as_mut() { + sha.update(&buf[..n]); + } + self.written += n as u64; + Ok(n) + } + + fn flush(&mut self) -> io::Result<()> { + self.inner.flush() + } +} + +/// `new File(outFile.getPath() + ".part")`. +pub fn part_path(final_path: &Path) -> PathBuf { + let mut s = final_path.as_os_str().to_os_string(); + s.push(".part"); + PathBuf::from(s) +} + +/// Process one downloaded body into `final_path`. Returns the number of DECOMPRESSED bytes +/// written. `Err(reason)` = Java's "try the next CDN" outcome; the `.part` never survives an +/// error and `final_path` is only ever created by the rename after verification. +pub fn write_verified_chunk( + body: &[u8], + expected_sha1: Option<&[u8; 20]>, + final_path: &Path, +) -> Result<u64, String> { + let tmp = part_path(final_path); + // `tmp.delete()` — clear any stale partial from a prior interrupted attempt. + let _ = fs::remove_file(&tmp); + + let result = write_part(body, expected_sha1, &tmp, final_path); + if result.is_err() { + let _ = fs::remove_file(&tmp); + } + result +} + +fn write_part( + body: &[u8], + expected_sha1: Option<&[u8; 20]>, + tmp: &Path, + final_path: &Path, +) -> Result<u64, String> { + let hdr = parse_chunk_header(body)?; + let payload = chunk_payload(body, &hdr)?; + + 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, + }; + + if hdr.is_compressed() { + // Java: inflate until the stream finishes or the input runs out; a corrupt stream throws + // (DataFormatException) → this CDN attempt fails. flate2 behaves the same way: EOF on a + // truncated stream is a clean end (partial output), corruption is an error. + let mut dec = flate2::read::ZlibDecoder::new(payload); + io::copy(&mut dec, &mut out).map_err(|e| format!("inflate: {e}"))?; + } else { + out.write_all(payload).map_err(|e| format!("write: {e}"))?; + } + out.flush().map_err(|e| format!("flush: {e}"))?; + let written = out.written; + let digest = out.sha.take().map(|s| s.finalize()); + drop(out); // close the file before the rename + + if let (Some(expected), Some(actual)) = (expected_sha1, digest) { + if actual.as_slice() != &expected[..] { + return Err("Chunk SHA-1 mismatch (streaming)".to_string()); + } + } + + // Verified → publish atomically (same directory → same filesystem). + fs::rename(tmp, final_path).map_err(|e| format!("Chunk rename failed: {e}"))?; + Ok(written) +} + +#[cfg(test)] +pub(crate) mod test_support { + use super::*; + + /// Build a chunk body the way the CDN serves it. + pub fn build_chunk_body( + decompressed: &[u8], + compress: bool, + extra_header: usize, + declared_compressed_size: Option<i32>, + ) -> Vec<u8> { + let payload = if compress { + let mut enc = + flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + enc.write_all(decompressed).unwrap(); + enc.finish().unwrap() + } else { + decompressed.to_vec() + }; + let header_size = (CHUNK_HEADER_PREFIX + extra_header) as i32; + let mut out = Vec::new(); + out.extend_from_slice(&CHUNK_MAGIC.to_le_bytes()); + out.extend_from_slice(&3i32.to_le_bytes()); // headerVersion + out.extend_from_slice(&header_size.to_le_bytes()); + out.extend_from_slice( + &declared_compressed_size + .unwrap_or(payload.len() as i32) + .to_le_bytes(), + ); + out.extend_from_slice(&[0xAB; 16]); // GUID (unused by the reader) + out.extend_from_slice(&[0xCD; 8]); // hash (unused by the reader) + out.push(if compress { 1 } else { 0 }); + out.extend(std::iter::repeat(0x5A).take(extra_header)); + assert_eq!(out.len(), header_size as usize); + out.extend_from_slice(&payload); + out + } + + pub fn sha1_of(data: &[u8]) -> [u8; 20] { + let mut h = Sha1::new(); + h.update(data); + let d = h.finalize(); + let mut out = [0u8; 20]; + out.copy_from_slice(&d); + out + } + + pub fn temp_dir(tag: &str) -> PathBuf { + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_nanos()) + .unwrap_or(0); + let dir = std::env::temp_dir().join(format!( + "epic-{tag}-{}-{nanos}", + std::process::id() + )); + fs::create_dir_all(&dir).unwrap(); + dir + } +} + +#[cfg(test)] +mod tests { + use super::test_support::*; + use super::*; + + fn sample_data() -> Vec<u8> { + (0..200_000u32).map(|i| (i % 251) as u8).collect() + } + + #[test] + fn header_parse_matches_java_fields() { + let body = build_chunk_body(b"hello", true, 5, None); + let h = parse_chunk_header(&body).unwrap(); + assert_eq!(h.header_version, 3); + assert_eq!(h.header_size, 46); + assert!(h.is_compressed()); + assert_eq!(h.payload_start(), 46); + let short = ChunkHeader { + header_version: 0, + header_size: 20, + compressed_size: 1, + stored_as: 0, + }; + assert_eq!(short.payload_start(), 41, "headerSize < 41 → payload still starts at 41"); + } + + #[test] + fn short_or_wrong_magic_bodies_are_rejected() { + assert!(parse_chunk_header(&[0u8; 40]).is_err()); + let mut body = build_chunk_body(b"x", false, 0, None); + body[0] ^= 0xFF; + assert!(parse_chunk_header(&body).unwrap_err().contains("Bad chunk magic")); + } + + #[test] + fn payload_is_truncated_to_the_body_not_an_error() { + let body = build_chunk_body(b"abcdef", false, 0, Some(100)); + let h = parse_chunk_header(&body).unwrap(); + assert_eq!(chunk_payload(&body, &h).unwrap(), b"abcdef"); + let neg = build_chunk_body(b"abcdef", false, 0, Some(-1)); + let h = parse_chunk_header(&neg).unwrap(); + assert_eq!(chunk_payload(&neg, &h).unwrap(), b""); + // headerSize past the body → skipFully throws. + let mut far = build_chunk_body(b"abcdef", false, 0, None); + far[8..12].copy_from_slice(&5000i32.to_le_bytes()); + let h = parse_chunk_header(&far).unwrap(); + assert!(chunk_payload(&far, &h).is_err()); + } + + #[test] + fn compressed_chunk_is_inflated_verified_and_renamed() { + let dir = temp_dir("ok"); + let data = sample_data(); + let body = build_chunk_body(&data, true, 3, None); + let final_path = dir.join("00000001000000020000000300000004"); + let n = write_verified_chunk(&body, Some(&sha1_of(&data)), &final_path).unwrap(); + assert_eq!(n, data.len() as u64); + assert_eq!(fs::read(&final_path).unwrap(), data); + assert!(!part_path(&final_path).exists(), ".part renamed away"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn stored_chunk_is_copied_as_is() { + let dir = temp_dir("stored"); + let data = b"stored-as-is payload".to_vec(); + let body = build_chunk_body(&data, false, 0, None); + let final_path = dir.join("chunk"); + let n = write_verified_chunk(&body, Some(&sha1_of(&data)), &final_path).unwrap(); + assert_eq!(n, data.len() as u64); + assert_eq!(fs::read(&final_path).unwrap(), data); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn sha1_mismatch_leaves_nothing_behind() { + let dir = temp_dir("sha"); + let data = sample_data(); + let body = build_chunk_body(&data, true, 0, None); + let final_path = dir.join("chunk"); + let err = write_verified_chunk(&body, Some(&[0x42; 20]), &final_path).unwrap_err(); + assert!(err.contains("SHA-1 mismatch"), "{err}"); + assert!(!final_path.exists()); + assert!(!part_path(&final_path).exists()); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn unverifiable_chunk_is_accepted_without_a_hash() { + let dir = temp_dir("nosha"); + let data = sample_data(); + let body = build_chunk_body(&data, true, 0, None); + let final_path = dir.join("chunk"); + assert_eq!( + write_verified_chunk(&body, None, &final_path).unwrap(), + data.len() as u64 + ); + assert_eq!(fs::read(&final_path).unwrap(), data); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn corrupt_zlib_fails_and_truncated_zlib_fails_the_hash() { + let dir = temp_dir("zlib"); + let data = sample_data(); + let mut body = build_chunk_body(&data, true, 0, None); + let final_path = dir.join("chunk"); + // Truncate the body: inflate ends early → partial output → SHA-1 mismatch. + let cut = body.len() - 1000; + let err = write_verified_chunk(&body[..cut], Some(&sha1_of(&data)), &final_path).unwrap_err(); + assert!(err.contains("SHA-1 mismatch") || err.contains("inflate"), "{err}"); + assert!(!final_path.exists()); + // Corrupt the deflate stream itself. + for b in body.iter_mut().skip(60).take(64) { + *b ^= 0x55; + } + let err = write_verified_chunk(&body, Some(&sha1_of(&data)), &final_path).unwrap_err(); + assert!(err.contains("SHA-1 mismatch") || err.contains("inflate"), "{err}"); + assert!(!final_path.exists()); + assert!(!part_path(&final_path).exists()); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn stale_part_file_is_cleared_before_the_attempt() { + let dir = temp_dir("stale"); + let final_path = dir.join("chunk"); + fs::write(part_path(&final_path), b"stale").unwrap(); + let body = [0u8; 10]; // too short → header failure + assert!(write_verified_chunk(&body, None, &final_path).is_err()); + assert!(!part_path(&final_path).exists()); + assert!(!final_path.exists()); + let _ = fs::remove_dir_all(&dir); + } +} diff --git a/app/src/main/rust/stores/src/epic/driver.rs b/app/src/main/rust/stores/src/epic/driver.rs new file mode 100644 index 0000000000..1c39f5ac6e --- /dev/null +++ b/app/src/main/rust/stores/src/epic/driver.rs @@ -0,0 +1,480 @@ +//! Plan → `FetchItem`s → `crate::fetch_core::run_fetch` with a sink that fills the chunk cache. +//! +//! This is the replacement for the body of the Java pool block in `EpicDownloadManager.install` +//! ("Download unique chunks — 8 parallel threads"). Inputs are exactly what that block sees: +//! the parsed manifest (re-parsed here from the same bytes), the pending file set (indices Java +//! computed after its delta/verify pass), the CDN prefixes (`baseUrl + cloudDir`, cloudflare +//! already skipped) and the chunk cache dir. Output = `<cache>/<GUID>` for every needed chunk, +//! where `<cache>` is `<installDir>/.chunks` unless the caller named another directory; the +//! files are assembled from it afterwards (by the Kotlin manager, or by [`super::assemble`]). + +use std::path::PathBuf; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +use crate::fetch_core::{run_fetch, FetchItem, FetchOptions, FetchSink, SinkError}; + +use super::manifest::{parse_manifest, Manifest}; +use super::plan::{ + cached_chunk_path, chunk_url, distinct_prefixes, per_host_cap, resolve_cache_dir, + total_credit_bytes, unique_chunks_for_files, +}; + +/// Java `conn.setReadTimeout(60000)` — the longer of the two Java timeouts (connect was 30 s). +pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(60); + +/// Everything the JNI layer hands over for one run. +pub struct EpicRequest { + pub manifest_bytes: Vec<u8>, + pub install_dir: String, + /// Chunk cache directory; `""` = `<installDir>/.chunks`. See [`resolve_cache_dir`]. + pub chunk_cache_dir: String, + /// `cdn.baseUrl + cdn.cloudDir` per manifest-API CDN entry, in Java's order. + pub cdn_prefixes: Vec<String>, + /// Indices into `manifest.files` of Java's `pendingFiles` (post delta/verify), in order. + pub pending_file_indices: Vec<usize>, + /// Java's `neededChunks.size()` / `totalBytes` for the plan cross-check (`None` = skip). + pub expected_chunks: Option<u64>, + pub expected_bytes: Option<u64>, + pub ca_bundle_path: String, + /// Window ceiling: the Steam speed tier's network window (Fast = 32) since improvements + /// round 1; the Java fallback pool keeps its fixed 8 (`super::JAVA_POOL_THREADS`). + pub max_workers: usize, + pub process_workers: usize, + pub label: String, +} + +/// The chunk plan for one run. +#[derive(Debug)] +pub struct EpicPlan { + pub manifest: Manifest, + pub cache_dir: PathBuf, + /// The cache is a directory the caller named, apart from the install: scratch space that + /// the assembler empties as it goes and removes when it is done. + pub scratch_cache: bool, + /// Chunk indices (into `manifest.unique_chunks`) in Java's submission order. + pub needed: Vec<usize>, + /// `Σ max(fileSize, 1)` over `needed`. + pub total_bytes: u64, + /// Distinct CDN prefixes = fetch-core host keys. + pub hosts: Vec<String>, +} + +/// Terminal result of a run, in the shape Java's pool block needs to reproduce its own exit +/// paths (`CANCELLED during chunk download (n/total chunks)`, `N chunks failed`, `chunksOK=`). +#[derive(Clone, Debug, Default)] +pub struct EpicOutcome { + pub success: bool, + pub cancelled: bool, + pub error: String, + /// Credited (manifest `max(fileSize,1)`) bytes: skipped-cached + fetched. + pub bytes_credited: u64, + /// Chunks accounted for (skipped-cached + fetched), Java's `completedCount`. + pub chunks_done: u64, + pub chunks_total: u64, + pub bytes_total: u64, + /// Decompressed bytes actually written to the cache this run. + pub decompressed_written: u64, +} + +/// Parse + plan (no I/O beyond reading the cache dir). `Err` = "engine could not start"; Java +/// then runs its own pool, since nothing has been fetched yet. +pub fn build_plan(req: &EpicRequest) -> Result<EpicPlan, String> { + let manifest = parse_manifest(&req.manifest_bytes).map_err(|e| format!("plan: {e}"))?; + for &i in &req.pending_file_indices { + if i >= manifest.files.len() { + return Err(format!( + "plan: pending file index {i} out of range ({} files)", + manifest.files.len() + )); + } + } + let needed = unique_chunks_for_files(&manifest, &req.pending_file_indices); + let total_bytes = total_credit_bytes(&manifest, &needed); + if let Some(expected) = req.expected_chunks { + if expected != needed.len() as u64 { + return Err(format!( + "plan: chunk count mismatch java={expected} rust={}", + needed.len() + )); + } + } + if let Some(expected) = req.expected_bytes { + if expected != total_bytes { + return Err(format!( + "plan: byte total mismatch java={expected} rust={total_bytes}" + )); + } + } + let hosts = distinct_prefixes(&req.cdn_prefixes); + if hosts.is_empty() { + return Err("plan: no CDN prefixes".to_string()); + } + let cache_dir = resolve_cache_dir(&req.install_dir, &req.chunk_cache_dir); + std::fs::create_dir_all(&cache_dir) + .map_err(|e| format!("plan: mkdirs {}: {e}", cache_dir.display()))?; + Ok(EpicPlan { + manifest, + cache_dir, + scratch_cache: !req.chunk_cache_dir.is_empty(), + needed, + total_bytes, + hosts, + }) +} + +/// Sink: one downloaded body → verified cache file. `id` indexes `fetch_chunks`. +struct ChunkCacheSink<'a> { + plan: &'a EpicPlan, + /// Chunk indices (into `manifest.unique_chunks`) of the items actually being fetched. + fetch_chunks: &'a [usize], + decompressed: AtomicU64, +} + +impl<'a> FetchSink for ChunkCacheSink<'a> { + fn process(&self, item: &FetchItem, body: Vec<u8>) -> Result<u64, SinkError> { + let Some(&ci) = self.fetch_chunks.get(item.id as usize) else { + return Err(SinkError::Fatal(format!("item id {} out of range", item.id))); + }; + let chunk = &self.plan.manifest.unique_chunks[ci]; + let final_path = cached_chunk_path(&self.plan.cache_dir, chunk); + match super::chunk::write_verified_chunk(&body, chunk.verifiable_sha1(), &final_path) { + Ok(written) => { + self.decompressed.fetch_add(written, Ordering::Relaxed); + Ok(chunk.credit_bytes()) + } + // Every per-attempt failure in Java is "try the next CDN"; the core's Retry rotates + // hosts and backs off the same way (bounded at its attempt cap). + Err(reason) => Err(SinkError::Retry(format!("{} {reason}", chunk.guid_str()))), + } + } +} + +/// Rolling speed sampler for the end-of-run summary: samples at ≥500 ms like the Java pool's +/// `lastSpeedMs` logic, keeps the peak. +struct SpeedMeter { + start: Instant, + last_at: Instant, + last_bytes: u64, + peak_bps: f64, +} + +impl SpeedMeter { + fn new() -> Self { + let now = Instant::now(); + Self { + start: now, + last_at: now, + last_bytes: 0, + peak_bps: 0.0, + } + } + + fn sample(&mut self, bytes_now: u64) { + let now = Instant::now(); + let dt = now.duration_since(self.last_at); + if dt >= Duration::from_millis(500) { + let delta = bytes_now.saturating_sub(self.last_bytes) as f64; + let bps = delta / dt.as_secs_f64(); + if bps > self.peak_bps { + self.peak_bps = bps; + } + self.last_at = now; + self.last_bytes = bytes_now; + } + } + + fn summary(&self, bytes: u64, decompressed: u64, skipped: u64) -> String { + let secs = self.start.elapsed().as_secs_f64().max(0.001); + let avg_bps = bytes as f64 / secs; + format!( + "summary bytes={bytes} decompressed={decompressed} skipped_chunks={skipped} elapsed={secs:.1}s avg_mbps={:.2} peak_mbps={:.2} avg_MBps={:.2}", + avg_bps * 8.0 / 1_000_000.0, + self.peak_bps * 8.0 / 1_000_000.0, + avg_bps / (1024.0 * 1024.0) + ) + } +} + +/// Run the plan. `progress(bytes_done, chunks_done)` fires once per accounted chunk (cached-skip +/// or fetched), exactly the cadence of the Java pool's per-task `progress(...)` call. `log` +/// receives engine lines (`fetch-window`, `summary`, failures). +pub fn run_plan( + plan: &EpicPlan, + req: &EpicRequest, + cancel: &AtomicBool, + progress: &(dyn Fn(u64, u64) + Sync), + log: &(dyn Fn(&str) + Sync), +) -> EpicOutcome { + let chunks_total = plan.needed.len() as u64; + let mut outcome = EpicOutcome { + chunks_total, + bytes_total: plan.total_bytes, + ..EpicOutcome::default() + }; + let host_cap = per_host_cap(req.max_workers, plan.hosts.len()); + log(&format!( + "plan chunk_dir={} cache_dir={} scratch={} {} files_pending={} chunks={} bytes={} hosts={} workers={} per_host_cap={host_cap} process_workers={}", + plan.manifest.chunk_dir, + plan.cache_dir.display(), + plan.scratch_cache, + crate::priority::log_field(), + req.pending_file_indices.len(), + chunks_total, + plan.total_bytes, + plan.hosts.len(), + req.max_workers, + req.process_workers + )); + + // Java: each pool task first checks the cancel flag, then `cachedFile.exists()` → credit + // without fetching. Account for the cached chunks up front (same credit, same callback). + let mut fetch_chunks: Vec<usize> = Vec::with_capacity(plan.needed.len()); + let mut pre_bytes: u64 = 0; + let mut pre_chunks: u64 = 0; + for &ci in &plan.needed { + if cancel.load(Ordering::Relaxed) { + outcome.cancelled = true; + outcome.error = "cancelled".to_string(); + outcome.bytes_credited = pre_bytes; + outcome.chunks_done = pre_chunks; + log(&format!( + "cancelled before fetch ({pre_chunks}/{chunks_total} chunks)" + )); + return outcome; + } + let chunk = &plan.manifest.unique_chunks[ci]; + if cached_chunk_path(&plan.cache_dir, chunk).exists() { + pre_bytes += chunk.credit_bytes(); + pre_chunks += 1; + progress(pre_bytes, pre_chunks); + } else { + fetch_chunks.push(ci); + } + } + for (i, h) in plan.hosts.iter().enumerate() { + log(&format!("host[{i}]={h}")); + } + log(&format!( + "skip cached={pre_chunks} bytes={pre_bytes} to_fetch={}", + fetch_chunks.len() + )); + + if fetch_chunks.is_empty() { + outcome.success = true; + outcome.bytes_credited = pre_bytes; + outcome.chunks_done = pre_chunks; + log("summary bytes=0 decompressed=0 elapsed=0.0s avg_mbps=0.00 peak_mbps=0.00 (nothing to fetch)"); + return outcome; + } + + let items: Vec<FetchItem> = fetch_chunks + .iter() + .enumerate() + .map(|(id, &ci)| { + let chunk = &plan.manifest.unique_chunks[ci]; + FetchItem { + id: id as u64, + urls: plan + .hosts + .iter() + .map(|h| chunk_url(h, &plan.manifest.chunk_dir, chunk)) + .collect(), + reserve: if chunk.file_size > 0 { + chunk.file_size as u64 + } else { + 0 + }, + range: None, + } + }) + .collect(); + + let opts = FetchOptions { + max_workers: req.max_workers.max(1), + // Improvements round 1: the tier ceiling split across the distinct CDNs (floor 6), so + // the whole window is reachable on 1 host or on all 3. + per_host_cap: host_cap, + timeout: REQUEST_TIMEOUT, + headers: vec![("User-Agent".to_string(), super::USER_AGENT.to_string())], + ca_bundle_path: req.ca_bundle_path.clone(), + process_workers: req.process_workers.max(1), + label: req.label.clone(), + // Whole-body mode (Epic chunks are ≤ ~1 MiB compressed; the core's byte budget scales + // with the window); `stream` and any future field keep the core's defaults. + ..FetchOptions::default() + }; + + let sink = ChunkCacheSink { + plan, + fetch_chunks: &fetch_chunks, + decompressed: AtomicU64::new(0), + }; + + let meter = Mutex::new(SpeedMeter::new()); + let progress_adapter = |bytes: u64, items_ok: u64| { + if let Ok(mut m) = meter.lock() { + m.sample(bytes); + } + progress(pre_bytes + bytes, pre_chunks + items_ok); + }; + + let fetched = run_fetch( + items, + &plan.hosts, + &opts, + &sink, + cancel, + &progress_adapter, + log, + ); + + let decompressed = sink.decompressed.load(Ordering::Relaxed); + outcome.bytes_credited = pre_bytes + fetched.bytes_credited; + outcome.chunks_done = pre_chunks + fetched.items_ok; + outcome.decompressed_written = decompressed; + if let Ok(m) = meter.lock() { + log(&m.summary(fetched.bytes_credited, decompressed, pre_chunks)); + } + + if fetched.cancelled { + outcome.cancelled = true; + outcome.error = "cancelled".to_string(); + log(&format!( + "cancelled during chunk download ({}/{chunks_total} chunks)", + outcome.chunks_done + )); + return outcome; + } + if let Some(err) = fetched.error { + outcome.error = err; + log(&format!( + "FAIL {} ({}/{chunks_total} chunks ok)", + outcome.error, outcome.chunks_done + )); + return outcome; + } + if fetched.items_ok != fetch_chunks.len() as u64 { + outcome.error = format!( + "fetch ended with {}/{} chunks", + fetched.items_ok, + fetch_chunks.len() + ); + log(&format!("FAIL {}", outcome.error)); + return outcome; + } + outcome.success = true; + log(&format!("chunksOK={}", outcome.chunks_done)); + outcome +} + +#[cfg(test)] +mod tests { + use super::super::manifest::test_support::*; + use super::*; + + fn request(dir: &str, pending: Vec<usize>) -> EpicRequest { + EpicRequest { + manifest_bytes: build_manifest(&sample_chunks(), &sample_files(), 21, true), + install_dir: dir.to_string(), + chunk_cache_dir: String::new(), + cdn_prefixes: vec![ + "https://fastly-download.epicgames.com/Builds/o/x/default".to_string(), + "https://download.epicgames.com/Builds/o/x/default".to_string(), + "https://fastly-download.epicgames.com/Builds/o/x/default".to_string(), + ], + pending_file_indices: pending, + expected_chunks: None, + expected_bytes: None, + ca_bundle_path: String::new(), + max_workers: super::super::JAVA_POOL_THREADS, + process_workers: 2, + label: "epic test".to_string(), + } + } + + #[test] + fn plan_matches_java_for_the_pending_set() { + let dir = super::super::chunk::test_support::temp_dir("plan"); + let req = request(dir.to_str().unwrap(), vec![0, 1]); + let plan = build_plan(&req).unwrap(); + assert_eq!(plan.needed, vec![0, 1]); + assert_eq!(plan.total_bytes, 700_000 + 1); + assert_eq!(plan.hosts.len(), 2, "duplicate CDN prefix collapsed"); + assert!(plan.cache_dir.ends_with(".chunks")); + assert!(plan.cache_dir.is_dir()); + assert!(!plan.scratch_cache); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn a_named_cache_dir_is_created_and_marked_scratch() { + let dir = super::super::chunk::test_support::temp_dir("scratch"); + let cache = dir.join("cache").join("Game"); + let mut req = request(dir.join("install").to_str().unwrap(), vec![0, 1]); + req.chunk_cache_dir = cache.to_str().unwrap().to_string(); + let plan = build_plan(&req).unwrap(); + assert_eq!(plan.cache_dir, cache); + assert!(plan.cache_dir.is_dir()); + assert!(plan.scratch_cache); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn plan_cross_check_rejects_mismatches() { + let dir = super::super::chunk::test_support::temp_dir("xcheck"); + let mut req = request(dir.to_str().unwrap(), vec![0, 1]); + req.expected_chunks = Some(3); + assert!(build_plan(&req).unwrap_err().contains("chunk count mismatch")); + req.expected_chunks = Some(2); + req.expected_bytes = Some(5); + assert!(build_plan(&req).unwrap_err().contains("byte total mismatch")); + req.expected_bytes = Some(700_001); + assert!(build_plan(&req).is_ok()); + req.pending_file_indices = vec![7]; + assert!(build_plan(&req).unwrap_err().contains("out of range")); + req.pending_file_indices = vec![0]; + req.cdn_prefixes.clear(); + assert!(build_plan(&req).unwrap_err().contains("no CDN")); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn fully_cached_plan_completes_without_fetching() { + let dir = super::super::chunk::test_support::temp_dir("cached"); + let req = request(dir.to_str().unwrap(), vec![0]); + let plan = build_plan(&req).unwrap(); + for &ci in &plan.needed { + std::fs::write( + cached_chunk_path(&plan.cache_dir, &plan.manifest.unique_chunks[ci]), + b"cached", + ) + .unwrap(); + } + let cancel = AtomicBool::new(false); + let calls = Mutex::new(Vec::new()); + let progress = |b: u64, c: u64| calls.lock().unwrap().push((b, c)); + let log = |_: &str| {}; + let out = run_plan(&plan, &req, &cancel, &progress, &log); + assert!(out.success); + assert_eq!(out.chunks_done, 2); + assert_eq!(out.bytes_credited, 700_001); + assert_eq!(*calls.lock().unwrap(), vec![(700_000, 1), (700_001, 2)]); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn cancel_before_fetch_reports_cancelled() { + let dir = super::super::chunk::test_support::temp_dir("cancel"); + let req = request(dir.to_str().unwrap(), vec![0]); + let plan = build_plan(&req).unwrap(); + let cancel = AtomicBool::new(true); + let progress = |_: u64, _: u64| {}; + let log = |_: &str| {}; + let out = run_plan(&plan, &req, &cancel, &progress, &log); + assert!(out.cancelled); + assert!(!out.success); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/app/src/main/rust/stores/src/epic/jni.rs b/app/src/main/rust/stores/src/epic/jni.rs new file mode 100644 index 0000000000..937d6e42ae --- /dev/null +++ b/app/src/main/rust/stores/src/epic/jni.rs @@ -0,0 +1,487 @@ +//! JNI exports for `com.droiddeck.launcher.stores.epic.EpicNative`. +//! +//! ```text +//! nativeStart(manifest: byte[], installDir: String, chunkCacheDir: String, cdnPrefixes: String[], +//! pendingFileIdx: int[], expectedChunks: int, expectedBytes: long, +//! caBundlePath: String, maxWorkers: int, processWorkers: int, +//! 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 +//! nativeRelease(handle) — frees the handle (after onComplete) +//! ``` +//! `chunkCacheDir` = "" keeps the cache at `<installDir>/.chunks`; any other directory is a +//! scratch cache (see `assemble.rs`). Listener (all methods run on native threads): +//! `onPlan(int chunksTotal, long bytesTotal, String chunkDir)` once before any fetch, +//! `onProgress(long bytesDone, long bytesTotal, int done, int total)` per chunk fetched (or, in +//! an assembly run, per file written), `onLog(String line)`, +//! `onComplete(boolean success, String error, long bytes)`. +//! +//! 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. + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; +use std::thread; + +use jni::objects::{GlobalRef, JByteArray, JClass, JIntArray, JObject, JObjectArray, JString, JValue}; +use jni::sys::{jint, jlong, JNI_FALSE, JNI_TRUE}; +use jni::{JNIEnv, JavaVM}; + +use super::assemble::{build_assemble_plan, run_assemble, AssembleRequest}; +use super::driver::{build_plan, run_plan, EpicRequest}; + +#[cfg(target_os = "android")] +#[link(name = "log")] +unsafe extern "C" { + fn __android_log_write(prio: i32, tag: *const i8, text: *const i8) -> i32; +} + +fn android_log(message: &str) { + #[cfg(target_os = "android")] + { + use std::ffi::CString; + let Ok(tag) = CString::new(super::LOG_TAG) else { + return; + }; + let sanitized = message.replace('\0', " "); + let Ok(text) = CString::new(sanitized) else { + return; + }; + unsafe { + let _ = __android_log_write(4, tag.as_ptr().cast(), text.as_ptr().cast()); + } + } + #[cfg(not(target_os = "android"))] + { + let _ = message; + } +} + +/// Per-run native state behind the `jlong` handle — the same type for a fetch run and an +/// assembly run, so one `nativeCancel` / `nativeRelease` pair serves both. +pub struct EpicDownloadHandle { + cancel: Arc<AtomicBool>, +} + +fn clear_pending_exception(env: &mut JNIEnv) { + if env.exception_check().unwrap_or(false) { + let _ = env.exception_clear(); + } +} + +fn jstring_to_string(env: &mut JNIEnv, value: &JString) -> Option<String> { + env.get_string(value) + .ok() + .map(|s| s.to_string_lossy().into_owned()) +} + +fn string_array_to_vec(env: &mut JNIEnv, array: &JObjectArray) -> Vec<String> { + let len = env.get_array_length(array).unwrap_or(0); + let mut out = Vec::with_capacity(len.max(0) as usize); + let mut i = 0; + while i < len { + match env.get_object_array_element(array, i) { + Ok(obj) => { + if obj.is_null() { + out.push(String::new()); + } else { + let js = JString::from(obj); + out.push(jstring_to_string(env, &js).unwrap_or_default()); + } + } + Err(_) => { + clear_pending_exception(env); + out.push(String::new()); + } + } + i += 1; + } + out +} + +fn int_array_to_vec(env: &JNIEnv, array: &JIntArray) -> Vec<i32> { + let len = env.get_array_length(array).unwrap_or(0); + if len <= 0 { + return Vec::new(); + } + let mut values = vec![0i32; len as usize]; + if env.get_int_array_region(array, 0, &mut values).is_err() { + return Vec::new(); + } + values +} + +fn call_on_log(env: &mut JNIEnv, listener: &JObject, line: &str) { + if listener.is_null() { + return; + } + let Ok(s) = env.new_string(line) else { + clear_pending_exception(env); + return; + }; + let obj = JObject::from(s); + let _ = env.call_method( + listener, + "onLog", + "(Ljava/lang/String;)V", + &[JValue::Object(&obj)], + ); + clear_pending_exception(env); +} + +fn call_on_plan(env: &mut JNIEnv, listener: &JObject, chunks_total: u64, bytes_total: u64, chunk_dir: &str) { + if listener.is_null() { + return; + } + let Ok(s) = env.new_string(chunk_dir) else { + clear_pending_exception(env); + return; + }; + let obj = JObject::from(s); + let _ = env.call_method( + listener, + "onPlan", + "(IJLjava/lang/String;)V", + &[ + JValue::Int(chunks_total.min(i32::MAX as u64) as jint), + JValue::Long(bytes_total as jlong), + JValue::Object(&obj), + ], + ); + clear_pending_exception(env); +} + +fn call_on_progress( + env: &mut JNIEnv, + listener: &JObject, + bytes_done: u64, + bytes_total: u64, + chunks_done: u64, + chunks_total: u64, +) { + if listener.is_null() { + return; + } + let _ = env.call_method( + listener, + "onProgress", + "(JJII)V", + &[ + JValue::Long(bytes_done as jlong), + JValue::Long(bytes_total as jlong), + JValue::Int(chunks_done.min(i32::MAX as u64) as jint), + JValue::Int(chunks_total.min(i32::MAX as u64) as jint), + ], + ); + clear_pending_exception(env); +} + +fn call_on_complete(env: &mut JNIEnv, listener: &JObject, success: bool, error: &str, bytes: u64) { + if listener.is_null() { + return; + } + let Ok(s) = env.new_string(error) else { + clear_pending_exception(env); + return; + }; + let obj = JObject::from(s); + let _ = env.call_method( + listener, + "onComplete", + "(ZLjava/lang/String;J)V", + &[ + JValue::Bool(if success { JNI_TRUE } else { JNI_FALSE }), + JValue::Object(&obj), + JValue::Long(bytes as jlong), + ], + ); + clear_pending_exception(env); +} + +/// Log to logcat AND to the listener (the Kotlin manager mirrors it into its own debug log). +fn log_both(env: &mut JNIEnv, listener: &JObject, line: &str) { + android_log(line); + call_on_log(env, listener, line); +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeStart( + mut env: JNIEnv, + _class: JClass, + manifest: JByteArray, + install_dir: JString, + chunk_cache_dir: JString, + cdn_prefixes: JObjectArray, + pending_file_idx: JIntArray, + expected_chunks: jint, + expected_bytes: jlong, + ca_bundle_path: JString, + max_workers: jint, + process_workers: jint, + listener: JObject, +) -> jlong { + if listener.is_null() { + android_log("nativeStart: 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}") + }; + + let host_count = super::plan::distinct_prefixes(&cdn_prefixes).len(); + let host_cap = super::plan::per_host_cap(max_workers, host_count); + log_both( + &mut env, + &listener, + &format!( + "engine=rust label=\"{label}\" install_dir={install_dir} cache_dir={} cdns={} hosts={host_count} pending_files={} workers={max_workers} per_host_cap={host_cap} 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(), + manifest_bytes.len() + ), + ); + + let req = EpicRequest { + manifest_bytes, + install_dir, + chunk_cache_dir, + cdn_prefixes, + pending_file_indices, + expected_chunks: if expected_chunks >= 0 { + Some(expected_chunks as u64) + } else { + None + }, + expected_bytes: if expected_bytes >= 0 { + Some(expected_bytes as u64) + } else { + None + }, + ca_bundle_path, + max_workers, + process_workers, + label, + }; + + let plan = match build_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; + } + }; + call_on_plan( + &mut env, + &listener, + plan.needed.len() as u64, + plan.total_bytes, + &plan.manifest.chunk_dir, + ); + + 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(); + run_on_thread(vm, listener, plan, req, thread_cancel); + }); + + Box::into_raw(handle) as jlong +} + +fn run_on_thread( + vm: JavaVM, + listener: GlobalRef, + plan: super::driver::EpicPlan, + req: EpicRequest, + cancel: Arc<AtomicBool>, +) { + let bytes_total = plan.total_bytes; + let chunks_total = plan.needed.len() as u64; + + // Progress fires from the core's process-pool threads; attach each as a daemon like the + // Steam engine's `dispatch_download_progress` does. + let progress = |bytes_done: u64, chunks_done: u64| { + let Ok(mut env) = vm.attach_current_thread_as_daemon() else { + return; + }; + call_on_progress( + &mut env, + listener.as_obj(), + bytes_done, + bytes_total, + chunks_done, + chunks_total, + ); + }; + 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 outcome = run_plan(&plan, &req, cancel.as_ref(), &progress, &log); + + 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_credited, + ); +} + +/// `EpicNative.nativeAssemble(manifest, installDir, chunkCacheDir, fileIdx, listener)`: write +/// the files `fileIdx` names from the chunk cache (see `assemble.rs`). Parse + plan run on the +/// calling thread, so bad inputs return 0 (after `onComplete(false, reason, 0)`) before anything +/// is written; the assembly runs on a new thread and reports per file through `onProgress(bytes, +/// bytesTotal, filesDone, filesTotal)`, then `onComplete(success, error, bytesWritten)`. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeAssemble( + mut env: JNIEnv, + _class: JClass, + manifest: JByteArray, + install_dir: JString, + chunk_cache_dir: JString, + file_idx: JIntArray, + listener: JObject, +) -> jlong { + if listener.is_null() { + android_log("nativeAssemble: 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 req = AssembleRequest { + manifest_bytes, + install_dir: jstring_to_string(&mut env, &install_dir).unwrap_or_default(), + chunk_cache_dir: jstring_to_string(&mut env, &chunk_cache_dir).unwrap_or_default(), + file_indices: int_array_to_vec(&env, &file_idx) + .into_iter() + .map(|i| i.max(0) as usize) + .collect(), + }; + let plan = match build_assemble_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 = |bytes_done: u64, bytes_total: u64, files_done: u64, files_total: u64| { + let Ok(mut env) = vm.attach_current_thread_as_daemon() else { + return; + }; + call_on_progress(&mut env, listener.as_obj(), bytes_done, bytes_total, files_done, files_total); + }; + 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 outcome = run_assemble(&plan, thread_cancel.as_ref(), &progress, &log); + 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, + _class: JClass, + handle: jlong, +) { + if handle == 0 { + return; + } + let handle = unsafe { &*(handle as *const EpicDownloadHandle) }; + handle.cancel.store(true, Ordering::Relaxed); + android_log("cancel requested"); +} + +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_epic_EpicNative_nativeRelease( + _env: JNIEnv, + _class: JClass, + handle: jlong, +) { + if handle == 0 { + return; + } + // The worker thread owns its own Arc clone of the cancel flag and its own GlobalRef, so + // freeing the handle is safe even if a (cancelled) run is still winding down. + drop(unsafe { Box::from_raw(handle as *mut EpicDownloadHandle) }); +} diff --git a/app/src/main/rust/stores/src/epic/manifest.rs b/app/src/main/rust/stores/src/epic/manifest.rs new file mode 100644 index 0000000000..579a56c320 --- /dev/null +++ b/app/src/main/rust/stores/src/epic/manifest.rs @@ -0,0 +1,987 @@ +//! Epic build manifest parsers — a 1:1 port of `EpicDownloadManager.parseManifest` (binary, +//! magic `0x44BEC00C`) and `parseJsonManifest` (legacy JSON manifests). +//! +//! Every quirk of the Java reader is kept on purpose (the Java manager stays the reference and +//! the two engines must derive the same plan from the same bytes): section skips by declared +//! size, `readFString` UTF-16LE/ASCII decoding, duplicate-GUID handling (last value, first +//! position), the JSON `Long.parseLong(.., 16)` / `parseInt` fallbacks, and "any exception → +//! parse failed". + +use std::collections::HashMap; +use std::io::Read; + +/// Binary manifest magic (`EpicDownloadManager.parseManifest`). +pub const MANIFEST_MAGIC: u32 = 0x44BE_C00C; + +/// One entry of the ChunkDataList section (`EpicDownloadManager.ChunkInfo`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ChunkInfo { + /// Four uint32 in binary read order. + pub guid: [u32; 4], + /// uint64 "hash" that forms the first half of the chunk file name. + pub hash: u64, + /// SHA-1 of the DECOMPRESSED chunk; `None` for JSON manifests (Java: `sha1 == null`). + pub sha1: Option<[u8; 20]>, + /// Subfolder number (DECIMAL `%02d`). `i32` so the JSON parser's `Integer.parseInt` range is + /// representable exactly (binary manifests always yield 0..=255). + pub group_num: i32, + /// Uncompressed size (0 for JSON manifests). + pub window_size: i32, + /// Compressed (wire) size. Signed like the Java `long`; JSON manifests may yield anything. + pub file_size: i64, +} + +impl ChunkInfo { + /// `String.format("%08X%08X%08X%08X", guid[0..4])`. + pub fn guid_str(&self) -> String { + guid_to_string(&self.guid) + } + + /// `ChunkInfo.getPath`: `"<chunkDir>/<%02d groupNum>/<%016X hash>_<GUID>.chunk"`. + pub fn path(&self, chunk_dir: &str) -> String { + format!( + "{}/{:02}/{:016X}_{}.chunk", + chunk_dir, + self.group_num, + self.hash, + self.guid_str() + ) + } + + /// Java `chunk.sha1 != null && length == 20 && !allZero` — the condition under which the + /// streaming downloader verifies the decompressed chunk. + pub fn verifiable_sha1(&self) -> Option<&[u8; 20]> { + match &self.sha1 { + Some(h) if h.iter().any(|b| *b != 0) => Some(h), + _ => None, + } + } + + /// `Math.max(chunk.fileSize, 1)` — the byte credit the Java pool uses for totals/progress. + pub fn credit_bytes(&self) -> u64 { + if self.file_size < 1 { + 1 + } else { + self.file_size as u64 + } + } +} + +/// One file part referencing a chunk (`EpicDownloadManager.ChunkPart`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ChunkPart { + pub guid: [u32; 4], + /// Offset into the DECOMPRESSED chunk (Java `int`). + pub offset: i32, + /// Part length (Java `int`; summed as `size & 0xFFFFFFFFL`). + pub size: i32, +} + +impl ChunkPart { + pub fn guid_str(&self) -> String { + guid_to_string(&self.guid) + } +} + +/// One entry of the FileManifestList section (`EpicDownloadManager.FileInfo`). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct FileInfo { + pub filename: String, + pub parts: Vec<ChunkPart>, + /// Install-tag names; empty = required/base file. + pub install_tags: Vec<String>, + /// Full-file SHA-1; `None` for JSON manifests. + pub sha1: Option<[u8; 20]>, +} + +impl FileInfo { + /// `FileInfo.fileSize()` = Σ `(part.size & 0xFFFFFFFFL)`. + pub fn file_size(&self) -> u64 { + self.parts.iter().map(|p| p.size as u32 as u64).sum() + } +} + +/// Parsed manifest (`EpicDownloadManager.EpicManifest.ParsedManifest`, minus the CDN list which +/// the Java side still owns). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Manifest { + pub chunk_dir: String, + pub unique_chunks: Vec<ChunkInfo>, + pub files: Vec<FileInfo>, +} + +impl Manifest { + /// `byGuid` map as `uniqueChunksForFiles` builds it: `LinkedHashMap.put` per chunk, so a + /// duplicate GUID resolves to the LAST `ChunkInfo`. + pub fn chunk_index_by_guid(&self) -> HashMap<String, usize> { + let mut map = HashMap::with_capacity(self.unique_chunks.len()); + for (i, c) in self.unique_chunks.iter().enumerate() { + map.insert(c.guid_str(), i); + } + map + } +} + +pub fn guid_to_string(guid: &[u32; 4]) -> String { + format!("{:08X}{:08X}{:08X}{:08X}", guid[0], guid[1], guid[2], guid[3]) +} + +/// `chunkDir` from the binary header version (`parseManifest`). +pub fn chunk_dir_for_binary_version(version: i32) -> &'static str { + if version >= 15 { + "ChunksV4" + } else if version >= 6 { + "ChunksV3" + } else if version >= 3 { + "ChunksV2" + } else { + "Chunks" + } +} + +/// `chunkDir` from `ManifestFileVersion` (`parseJsonManifest`; note the different `< 3` default). +pub fn chunk_dir_for_json_version(version: i32) -> &'static str { + if version >= 15 { + "ChunksV4" + } else if version >= 6 { + "ChunksV3" + } else if version >= 3 { + "ChunksV2" + } else { + "ChunksV4" + } +} + +// ── little-endian cursor with ByteBuffer-like failure semantics ───────────────────────────── + +/// Mirrors the `ByteBuffer` calls the Java parser makes: every under-run or out-of-range +/// `position()` is an exception there, which the Java code turns into "parse failed" (null). +struct Cursor<'a> { + buf: &'a [u8], + pos: usize, +} + +impl<'a> Cursor<'a> { + fn new(buf: &'a [u8]) -> Self { + Self { buf, pos: 0 } + } + + fn position(&self) -> usize { + self.pos + } + + /// `ByteBuffer.position(p)` — `p` must be within `0..=limit`. + fn set_position(&mut self, p: i64) -> Result<(), String> { + if p < 0 || p as u64 > self.buf.len() as u64 { + return Err(format!("position {p} out of range (limit {})", self.buf.len())); + } + self.pos = p as usize; + Ok(()) + } + + fn take(&mut self, n: usize) -> Result<&'a [u8], String> { + let end = self + .pos + .checked_add(n) + .ok_or_else(|| "buffer overflow".to_string())?; + if end > self.buf.len() { + return Err(format!( + "buffer underflow: need {n} at {} (limit {})", + self.pos, + self.buf.len() + )); + } + let out = &self.buf[self.pos..end]; + self.pos = end; + Ok(out) + } + + fn u8(&mut self) -> Result<u8, String> { + Ok(self.take(1)?[0]) + } + + fn i32(&mut self) -> Result<i32, String> { + let b = self.take(4)?; + Ok(i32::from_le_bytes([b[0], b[1], b[2], b[3]])) + } + + fn u32(&mut self) -> Result<u32, String> { + Ok(self.i32()? as u32) + } + + fn u64(&mut self) -> Result<u64, String> { + let b = self.take(8)?; + Ok(u64::from_le_bytes([ + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], + ])) + } + + fn sha1(&mut self) -> Result<[u8; 20], String> { + let b = self.take(20)?; + let mut out = [0u8; 20]; + out.copy_from_slice(b); + Ok(out) + } + + /// `ByteBuffer.position(position + n)` with the same range check. + fn skip(&mut self, n: i64) -> Result<(), String> { + let target = self.pos as i64 + n; + self.set_position(target) + } + + fn remaining(&self) -> &'a [u8] { + &self.buf[self.pos..] + } + + /// `EpicDownloadManager.readFString`: int32 length; `0` → ""; negative → `(-len)-1` UTF-16LE + /// code units + 2-byte terminator; positive → `len-1` US-ASCII bytes + 1-byte terminator. + fn fstring(&mut self) -> Result<String, String> { + let length = self.i32()?; + if length == 0 { + return Ok(String::new()); + } + if length < 0 { + // Java: `int chars = (-length) - 1;` — `-Integer.MIN_VALUE` wraps to itself, then -1 + // gives Integer.MAX_VALUE; `new byte[chars * 2]` overflows negative → exception. + let chars = length.wrapping_neg().wrapping_sub(1); + let byte_len = chars.checked_mul(2).ok_or("fstring length overflow")?; + if byte_len < 0 { + return Err("negative fstring length".to_string()); + } + let bytes = self.take(byte_len as usize)?; + self.take(2)?; // null terminator + let units: Vec<u16> = bytes + .chunks_exact(2) + .map(|p| u16::from_le_bytes([p[0], p[1]])) + .collect(); + Ok(String::from_utf16_lossy(&units)) + } else { + let bytes = self.take((length - 1) as usize)?; + self.take(1)?; // null terminator + // `new String(bytes, US_ASCII)`: every byte > 0x7F decodes to U+FFFD. + Ok(bytes + .iter() + .map(|&b| if b < 0x80 { b as char } else { '\u{FFFD}' }) + .collect()) + } + } +} + +// ── binary manifest ────────────────────────────────────────────────────────────────────── + +/// `EpicDownloadManager.parseManifest(bytes)`: binary when the magic matches, otherwise the JSON +/// parser. `Err` wherever Java returns `null`. +pub fn parse_manifest(bytes: &[u8]) -> Result<Manifest, String> { + let mut cur = Cursor::new(bytes); + let magic = cur.u32()?; + if magic != MANIFEST_MAGIC { + return parse_json_manifest(bytes); + } + parse_binary_after_magic(&mut cur) +} + +fn parse_binary_after_magic(cur: &mut Cursor<'_>) -> Result<Manifest, String> { + let header_size = cur.i32()?; + let size_uncompressed = cur.i32()?; + let _size_compressed = cur.i32()?; + cur.skip(20)?; // SHA-1 of the body, not checked by the Java parser + let stored_as = cur.u8()?; + let version = cur.i32()?; + let chunk_dir = chunk_dir_for_binary_version(version); + + cur.set_position(header_size as i64)?; + let body_slice = cur.remaining(); + + let inflated; + let body: &[u8] = if (stored_as & 1) != 0 { + if size_uncompressed < 0 { + return Err("negative sizeUncompressed".to_string()); + } + // Java: `inflater.inflate(new byte[sizeUncompressed])` — a single call that fills at most + // `sizeUncompressed` bytes; anything else (short, corrupt) is a size mismatch → null. + let mut out = Vec::with_capacity(size_uncompressed as usize); + let mut dec = flate2::read::ZlibDecoder::new(body_slice).take(size_uncompressed as u64); + dec.read_to_end(&mut out) + .map_err(|e| format!("manifest inflate failed: {e}"))?; + if out.len() != size_uncompressed as usize { + return Err(format!( + "decomp size mismatch: expected {size_uncompressed} got {}", + out.len() + )); + } + inflated = out; + &inflated + } else { + body_slice + }; + + let mut b = Cursor::new(body); + + // ManifestMeta: skipped by its declared size (the size field counts itself). + let meta_size = b.i32()?; + let meta_end = b.position() as i64 - 4 + meta_size as i64; + b.set_position(meta_end)?; + + // ChunkDataList + let cdl_start = b.position(); + let cdl_size = b.i32()?; + let _cdl_version = b.u8()?; + let chunk_count = b.i32()?; + if chunk_count < 0 { + return Err("negative chunkCount".to_string()); // `new ArrayList<>(negative)` throws + } + let chunk_count = chunk_count as usize; + let mut chunks: Vec<ChunkInfo> = (0..chunk_count) + .map(|_| ChunkInfo { + guid: [0; 4], + hash: 0, + sha1: None, + group_num: 0, + window_size: 0, + file_size: 0, + }) + .collect(); + for c in chunks.iter_mut() { + c.guid = [b.u32()?, b.u32()?, b.u32()?, b.u32()?]; + } + for c in chunks.iter_mut() { + c.hash = b.u64()?; + } + for c in chunks.iter_mut() { + c.sha1 = Some(b.sha1()?); + } + for c in chunks.iter_mut() { + c.group_num = b.u8()? as i32; + } + for c in chunks.iter_mut() { + c.window_size = b.i32()?; + } + for c in chunks.iter_mut() { + c.file_size = b.u64()? as i64; + } + b.set_position(cdl_start as i64 + cdl_size as i64)?; + + // FileManifestList + let fml_start = b.position(); + let fml_size = b.i32()?; + let _fml_version = b.u8()?; + let file_count = b.i32()?; + if file_count < 0 { + return Err("negative fileCount".to_string()); + } + let file_count = file_count as usize; + let mut files: Vec<FileInfo> = (0..file_count).map(|_| FileInfo::default()).collect(); + for f in files.iter_mut() { + f.filename = b.fstring()?; + } + for _ in 0..file_count { + b.fstring()?; // symlink targets + } + for f in files.iter_mut() { + f.sha1 = Some(b.sha1()?); + } + b.skip(file_count as i64)?; // per-file flags byte + for f in files.iter_mut() { + let tag_count = b.i32()?; + let mut j = 0; + while j < tag_count { + let tag = b.fstring()?; + if !tag.is_empty() { + f.install_tags.push(tag); + } + j += 1; + } + } + for f in files.iter_mut() { + let part_count = b.i32()?; + let mut j = 0; + while j < part_count { + let part_start = b.position(); + let part_struct_size = b.i32()?; + let guid = [b.u32()?, b.u32()?, b.u32()?, b.u32()?]; + let offset = b.i32()?; + let size = b.i32()?; + f.parts.push(ChunkPart { guid, offset, size }); + b.set_position(part_start as i64 + part_struct_size as i64)?; + j += 1; + } + } + b.set_position(fml_start as i64 + fml_size as i64)?; + + Ok(Manifest { + chunk_dir: chunk_dir.to_string(), + unique_chunks: dedup_last_wins_first_position(chunks), + files, + }) +} + +/// `LinkedHashMap<String, ChunkInfo>` keyed by GUID string: a repeated key keeps its FIRST +/// position but takes the LAST value. +fn dedup_last_wins_first_position(chunks: Vec<ChunkInfo>) -> Vec<ChunkInfo> { + let mut order: Vec<ChunkInfo> = Vec::with_capacity(chunks.len()); + let mut index: HashMap<String, usize> = HashMap::with_capacity(chunks.len()); + for c in chunks { + let key = c.guid_str(); + match index.get(&key).copied() { + Some(i) => order[i] = c, + None => { + index.insert(key, order.len()); + order.push(c); + } + } + } + order +} + +// ── JSON manifest (older games) ────────────────────────────────────────────────────────── + +/// `org.json` value → string coercion as `optString` / `getString` do on Android (numbers and +/// booleans print as-is, JSON null prints "null", containers print their JSON). +fn json_to_string(v: &serde_json::Value) -> String { + match v { + serde_json::Value::String(s) => s.clone(), + serde_json::Value::Null => "null".to_string(), + other => other.to_string(), + } +} + +/// `obj.optString(key, fallback)`. +fn opt_string(obj: &serde_json::Map<String, serde_json::Value>, key: &str, fallback: &str) -> String { + match obj.get(key) { + Some(v) => json_to_string(v), + None => fallback.to_string(), + } +} + +/// `(int) Long.parseLong(hex8, 16)` — the Java GUID quarter parse; invalid hex throws out of the +/// whole parse. +fn parse_guid_quarter(s: &str) -> Result<u32, String> { + let v = i64::from_str_radix(s, 16).map_err(|e| format!("bad guid hex '{s}': {e}"))?; + Ok(v as i32 as u32) +} + +fn parse_guid_from_hex(s: &str) -> Result<[u32; 4], String> { + // Java substrings by UTF-16 unit and `parseLong` throws on anything non-hex; a non-ASCII + // prefix can never parse, so refuse it before slicing (keeps the slice on char boundaries). + if s.len() < 32 || !s.as_bytes()[..32].is_ascii() { + return Err(format!("bad guid hex '{s}'")); + } + Ok([ + parse_guid_quarter(&s[0..8])?, + parse_guid_quarter(&s[8..16])?, + parse_guid_quarter(&s[16..24])?, + parse_guid_quarter(&s[24..32])?, + ]) +} + +/// `EpicDownloadManager.parseJsonManifest`. +pub fn parse_json_manifest(bytes: &[u8]) -> Result<Manifest, String> { + let text = String::from_utf8_lossy(bytes); + let root: serde_json::Value = + serde_json::from_str(&text).map_err(|e| format!("JSON manifest: {e}"))?; + let root = root + .as_object() + .ok_or_else(|| "JSON manifest: root is not an object".to_string())?; + + let manifest_version: i32 = opt_string(root, "ManifestFileVersion", "0") + .parse::<i32>() + .unwrap_or(0); + let chunk_dir = chunk_dir_for_json_version(manifest_version); + + let chunk_hash_list = root + .get("ChunkHashList") + .and_then(|v| v.as_object()) + .ok_or_else(|| "JSON manifest: no ChunkHashList".to_string())?; + let data_group_list = root.get("DataGroupList").and_then(|v| v.as_object()); + let chunk_filesize_list = root.get("ChunkFilesizeList").and_then(|v| v.as_object()); + + // `LinkedHashMap<String, ChunkInfo>` keyed by the RAW key string (not the normalized GUID). + let mut chunk_order: Vec<ChunkInfo> = Vec::new(); + let mut chunk_index: HashMap<String, usize> = HashMap::new(); + for (guid_hex, hash_value) in chunk_hash_list.iter() { + if guid_hex.len() < 32 || !guid_hex.is_char_boundary(32) { + continue; + } + let hash_hex = json_to_string(hash_value); + let guid = parse_guid_from_hex(guid_hex)?; + let hash = if hash_hex.len() >= 16 && hash_hex.is_char_boundary(16) { + u64::from_str_radix(&hash_hex[0..16], 16).unwrap_or(0) + } else { + 0 + }; + let group_num = match data_group_list { + Some(dgl) => opt_string(dgl, guid_hex, "0").parse::<i32>().unwrap_or(0), + None => 0, + }; + let file_size = match chunk_filesize_list { + Some(cfl) => i64::from_str_radix(&opt_string(cfl, guid_hex, "0"), 16).unwrap_or(0), + None => 0, + }; + let c = ChunkInfo { + guid, + hash, + sha1: None, + group_num, + window_size: 0, + file_size, + }; + match chunk_index.get(guid_hex).copied() { + Some(i) => chunk_order[i] = c, + None => { + chunk_index.insert(guid_hex.clone(), chunk_order.len()); + chunk_order.push(c); + } + } + } + + let file_list = root + .get("FileManifestList") + .and_then(|v| v.as_array()) + .ok_or_else(|| "JSON manifest: no FileManifestList".to_string())?; + + let mut files = Vec::with_capacity(file_list.len()); + for entry in file_list { + let file_obj = entry + .as_object() + .ok_or_else(|| "JSON manifest: FileManifestList entry is not an object".to_string())?; + let mut fi = FileInfo { + filename: opt_string(file_obj, "Filename", ""), + ..FileInfo::default() + }; + if let Some(tags) = file_obj.get("InstallTags").and_then(|v| v.as_array()) { + for t in tags { + let tag = json_to_string(t); + if !tag.is_empty() { + fi.install_tags.push(tag); + } + } + } + if let Some(parts) = file_obj.get("FileChunkParts").and_then(|v| v.as_array()) { + for p in parts { + let part_obj = p.as_object().ok_or_else(|| { + "JSON manifest: FileChunkParts entry is not an object".to_string() + })?; + let mut part = ChunkPart { + guid: [0; 4], + offset: 0, + size: 0, + }; + let part_guid = opt_string(part_obj, "Guid", ""); + if part_guid.len() >= 32 && part_guid.is_char_boundary(32) { + part.guid = parse_guid_from_hex(&part_guid)?; + } + part.offset = opt_string(part_obj, "Offset", "0").parse::<i32>().unwrap_or(0); + part.size = opt_string(part_obj, "Size", "0").parse::<i32>().unwrap_or(0); + fi.parts.push(part); + } + } + files.push(fi); + } + + Ok(Manifest { + chunk_dir: chunk_dir.to_string(), + unique_chunks: chunk_order, + files, + }) +} + +// ── tests ──────────────────────────────────────────────────────────────────────────────── + +#[cfg(test)] +pub(crate) mod test_support { + //! Synthetic binary-manifest builder used by this module's tests and by `plan`/`driver` + //! tests. Emits exactly the layout `parseManifest` walks (meta / CDL / FML with sizes). + + use super::*; + use std::io::Write; + + pub struct TestChunk { + pub guid: [u32; 4], + pub hash: u64, + pub sha1: [u8; 20], + pub group: u8, + pub window: i32, + pub file_size: u64, + } + + pub struct TestFile { + pub name: String, + pub sha1: [u8; 20], + pub tags: Vec<String>, + /// (guid, offset, size) + pub parts: Vec<([u32; 4], i32, i32)>, + } + + pub fn fstring_ascii(s: &str) -> Vec<u8> { + let mut v = Vec::new(); + if s.is_empty() { + v.extend_from_slice(&0i32.to_le_bytes()); + return v; + } + v.extend_from_slice(&((s.len() as i32) + 1).to_le_bytes()); + v.extend_from_slice(s.as_bytes()); + v.push(0); + v + } + + pub fn fstring_utf16(s: &str) -> Vec<u8> { + let units: Vec<u16> = s.encode_utf16().collect(); + let mut v = Vec::new(); + v.extend_from_slice(&(-((units.len() as i32) + 1)).to_le_bytes()); + for u in units { + v.extend_from_slice(&u.to_le_bytes()); + } + v.extend_from_slice(&[0, 0]); + v + } + + /// Body (meta + CDL + FML) without the outer header. + pub fn build_body(chunks: &[TestChunk], files: &[TestFile], meta_padding: usize) -> Vec<u8> { + let mut body = Vec::new(); + // ManifestMeta: size field counts itself. + let meta_size = 4 + meta_padding; + body.extend_from_slice(&(meta_size as i32).to_le_bytes()); + body.extend(std::iter::repeat(0xEEu8).take(meta_padding)); + + // ChunkDataList + let mut cdl = Vec::new(); + cdl.push(0u8); // version + cdl.extend_from_slice(&(chunks.len() as i32).to_le_bytes()); + for c in chunks { + for g in c.guid { + cdl.extend_from_slice(&g.to_le_bytes()); + } + } + for c in chunks { + cdl.extend_from_slice(&c.hash.to_le_bytes()); + } + for c in chunks { + cdl.extend_from_slice(&c.sha1); + } + for c in chunks { + cdl.push(c.group); + } + for c in chunks { + cdl.extend_from_slice(&c.window.to_le_bytes()); + } + for c in chunks { + cdl.extend_from_slice(&c.file_size.to_le_bytes()); + } + // Trailing padding the parser must skip via cdlSize. + cdl.extend_from_slice(&[0xAA, 0xBB, 0xCC]); + body.extend_from_slice(&((cdl.len() + 4) as i32).to_le_bytes()); + body.extend_from_slice(&cdl); + + // FileManifestList + let mut fml = Vec::new(); + fml.push(0u8); // version + fml.extend_from_slice(&(files.len() as i32).to_le_bytes()); + for f in files { + if f.name.is_ascii() { + fml.extend_from_slice(&fstring_ascii(&f.name)); + } else { + fml.extend_from_slice(&fstring_utf16(&f.name)); + } + } + for _ in files { + fml.extend_from_slice(&fstring_ascii("")); // symlink target + } + for f in files { + fml.extend_from_slice(&f.sha1); + } + for _ in files { + fml.push(0); // flags + } + for f in files { + fml.extend_from_slice(&(f.tags.len() as i32).to_le_bytes()); + for t in &f.tags { + fml.extend_from_slice(&fstring_ascii(t)); + } + } + for f in files { + fml.extend_from_slice(&(f.parts.len() as i32).to_le_bytes()); + for (guid, offset, size) in &f.parts { + // struct size 4 + 16 + 4 + 4 = 28, plus 2 bytes of padding the parser skips. + fml.extend_from_slice(&30i32.to_le_bytes()); + for g in guid { + fml.extend_from_slice(&g.to_le_bytes()); + } + fml.extend_from_slice(&offset.to_le_bytes()); + fml.extend_from_slice(&size.to_le_bytes()); + fml.extend_from_slice(&[0x11, 0x22]); + } + } + body.extend_from_slice(&((fml.len() + 4) as i32).to_le_bytes()); + body.extend_from_slice(&fml); + body + } + + /// Full manifest bytes: header + (optionally zlib-compressed) body. + pub fn build_manifest( + chunks: &[TestChunk], + files: &[TestFile], + version: i32, + compress: bool, + ) -> Vec<u8> { + let body = build_body(chunks, files, 7); + let payload = if compress { + let mut enc = + flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + enc.write_all(&body).unwrap(); + enc.finish().unwrap() + } else { + body.clone() + }; + let header_size: i32 = 41 + 3; // 3 bytes of extra header the parser must jump over + let mut out = Vec::new(); + out.extend_from_slice(&MANIFEST_MAGIC.to_le_bytes()); + out.extend_from_slice(&header_size.to_le_bytes()); + out.extend_from_slice(&(body.len() as i32).to_le_bytes()); + out.extend_from_slice(&(payload.len() as i32).to_le_bytes()); + out.extend_from_slice(&[0u8; 20]); + out.push(if compress { 1 } else { 0 }); + out.extend_from_slice(&version.to_le_bytes()); + out.extend_from_slice(&[0xF0, 0xF1, 0xF2]); + assert_eq!(out.len(), header_size as usize); + out.extend_from_slice(&payload); + out + } + + pub fn sample_chunks() -> Vec<TestChunk> { + vec![ + TestChunk { + guid: [0x0000_0001, 0x0000_0002, 0x0000_0003, 0x0000_0004], + hash: 0x0123_4567_89AB_CDEF, + sha1: [0x11; 20], + group: 5, + window: 1_048_576, + file_size: 700_000, + }, + TestChunk { + guid: [0xDEAD_BEEF, 0xFFFF_FFFF, 0x0000_0000, 0x8000_0000], + hash: 0xFFFF_FFFF_FFFF_FFFE, + sha1: [0x22; 20], + group: 123, + window: 4096, + file_size: 0, + }, + TestChunk { + guid: [0x0000_00AA, 0x0000_00BB, 0x0000_00CC, 0x0000_00DD], + hash: 0x10, + sha1: [0u8; 20], + group: 0, + window: 10, + file_size: 9, + }, + ] + } + + pub fn sample_files() -> Vec<TestFile> { + let c = sample_chunks(); + vec![ + TestFile { + name: "Game/Binaries/Win64/Game.exe".to_string(), + sha1: [0x51; 20], + tags: vec![], + parts: vec![(c[0].guid, 0, 1_048_576), (c[1].guid, 100, 4000)], + }, + TestFile { + name: "Game\\Content\\Localization\\de.pak".to_string(), + sha1: [0x52; 20], + tags: vec!["German".to_string(), "de-DE".to_string()], + parts: vec![(c[1].guid, 0, 4096)], + }, + TestFile { + name: "Game/Content/Ünïcode.pak".to_string(), + sha1: [0x53; 20], + tags: vec!["French".to_string()], + parts: vec![(c[2].guid, 0, 10), (c[0].guid, 5, 7)], + }, + ] + } +} + +#[cfg(test)] +mod tests { + use super::test_support::*; + use super::*; + + #[test] + fn binary_manifest_round_trips_uncompressed() { + let bytes = build_manifest(&sample_chunks(), &sample_files(), 21, false); + let m = parse_manifest(&bytes).expect("parse"); + assert_eq!(m.chunk_dir, "ChunksV4"); + assert_eq!(m.unique_chunks.len(), 3); + assert_eq!(m.files.len(), 3); + + let c0 = &m.unique_chunks[0]; + assert_eq!(c0.guid_str(), "00000001000000020000000300000004"); + assert_eq!(c0.hash, 0x0123_4567_89AB_CDEF); + assert_eq!(c0.sha1, Some([0x11; 20])); + assert_eq!(c0.group_num, 5); + assert_eq!(c0.window_size, 1_048_576); + assert_eq!(c0.file_size, 700_000); + assert_eq!( + c0.path("ChunksV4"), + "ChunksV4/05/0123456789ABCDEF_00000001000000020000000300000004.chunk" + ); + + let c1 = &m.unique_chunks[1]; + assert_eq!(c1.guid_str(), "DEADBEEFFFFFFFFF0000000080000000"); + assert_eq!( + c1.path("ChunksV4"), + "ChunksV4/123/FFFFFFFFFFFFFFFE_DEADBEEFFFFFFFFF0000000080000000.chunk" + ); + assert_eq!(c1.credit_bytes(), 1, "Math.max(fileSize, 1)"); + assert!(c1.verifiable_sha1().is_some()); + assert!(m.unique_chunks[2].verifiable_sha1().is_none(), "all-zero SHA-1 → unverifiable"); + + let f0 = &m.files[0]; + assert_eq!(f0.filename, "Game/Binaries/Win64/Game.exe"); + assert!(f0.install_tags.is_empty()); + assert_eq!(f0.sha1, Some([0x51; 20])); + assert_eq!(f0.parts.len(), 2); + assert_eq!(f0.parts[1].offset, 100); + assert_eq!(f0.parts[1].size, 4000); + assert_eq!(f0.file_size(), 1_048_576 + 4000); + + let f1 = &m.files[1]; + assert_eq!(f1.filename, "Game\\Content\\Localization\\de.pak"); + assert_eq!(f1.install_tags, vec!["German".to_string(), "de-DE".to_string()]); + + let f2 = &m.files[2]; + assert_eq!(f2.filename, "Game/Content/Ünïcode.pak", "UTF-16LE fstring"); + assert_eq!(f2.parts[1].guid_str(), c0.guid_str()); + } + + #[test] + fn binary_manifest_round_trips_compressed() { + let bytes = build_manifest(&sample_chunks(), &sample_files(), 17, true); + let m = parse_manifest(&bytes).expect("parse"); + assert_eq!(m.unique_chunks.len(), 3); + assert_eq!(m.files.len(), 3); + assert_eq!(m.files[2].filename, "Game/Content/Ünïcode.pak"); + } + + #[test] + fn chunk_dir_follows_version_thresholds() { + for (v, dir) in [(2, "Chunks"), (3, "ChunksV2"), (6, "ChunksV3"), (15, "ChunksV4")] { + let bytes = build_manifest(&sample_chunks(), &sample_files(), v, false); + assert_eq!(parse_manifest(&bytes).unwrap().chunk_dir, dir, "version {v}"); + } + assert_eq!(chunk_dir_for_json_version(1), "ChunksV4"); + assert_eq!(chunk_dir_for_json_version(4), "ChunksV2"); + } + + #[test] + fn duplicate_guid_keeps_first_position_last_value() { + let mut chunks = sample_chunks(); + let dup = TestChunk { + guid: chunks[0].guid, + hash: 0x99, + sha1: [0x77; 20], + group: 9, + window: 1, + file_size: 1, + }; + chunks.push(dup); + let bytes = build_manifest(&chunks, &sample_files(), 21, false); + let m = parse_manifest(&bytes).unwrap(); + assert_eq!(m.unique_chunks.len(), 3); + assert_eq!(m.unique_chunks[0].hash, 0x99); + assert_eq!(m.unique_chunks[0].group_num, 9); + } + + #[test] + fn truncated_manifest_is_a_parse_failure() { + let bytes = build_manifest(&sample_chunks(), &sample_files(), 21, false); + assert!(parse_manifest(&bytes[..bytes.len() - 40]).is_err()); + assert!(parse_manifest(&bytes[..20]).is_err()); + } + + #[test] + fn bad_uncompressed_size_is_a_parse_failure() { + let good = build_manifest(&sample_chunks(), &sample_files(), 21, true); + let real = i32::from_le_bytes([good[8], good[9], good[10], good[11]]); + // Claim MORE than the body inflates to: Java's single `inflate()` returns fewer bytes + // than `sizeUncompressed` → "Decomp size mismatch" → null. + let mut bigger = good.clone(); + bigger[8..12].copy_from_slice(&(real + 1).to_le_bytes()); + assert!(parse_manifest(&bigger).is_err()); + // Claim LESS: Java fills exactly `sizeUncompressed` bytes (silently truncating), then the + // FileManifestList end position lands past the limit → exception → null. + let mut smaller = good; + smaller[8..12].copy_from_slice(&(real - 1).to_le_bytes()); + assert!(parse_manifest(&smaller).is_err()); + } + + #[test] + fn fstring_decoding_matches_java() { + let mut v = fstring_ascii("abc"); + v.extend_from_slice(&fstring_utf16("dé")); + v.extend_from_slice(&fstring_ascii("")); + v.extend_from_slice(&[4, 0, 0, 0, b'a', 0xFF, b'b', 0]); // non-ASCII byte → U+FFFD + let mut c = Cursor::new(&v); + assert_eq!(c.fstring().unwrap(), "abc"); + assert_eq!(c.fstring().unwrap(), "dé"); + assert_eq!(c.fstring().unwrap(), ""); + assert_eq!(c.fstring().unwrap(), "a\u{FFFD}b"); + } + + #[test] + fn json_manifest_parses_like_java() { + let json = r#"{ + "ManifestFileVersion": "013", + "ChunkHashList": { + "0000000100000002000000030000000ff": "0123456789ABCDEF00", + "short": "00", + "DEADBEEFFFFFFFFF0000000080000000": "zz" + }, + "DataGroupList": { "0000000100000002000000030000000ff": "7", "DEADBEEFFFFFFFFF0000000080000000": "x" }, + "ChunkFilesizeList": { "0000000100000002000000030000000ff": "FF" }, + "FileManifestList": [ + { "Filename": "a.bin", "InstallTags": ["German", ""], + "FileChunkParts": [ { "Guid": "0000000100000002000000030000000ff", "Offset": "12", "Size": "34" }, + { "Guid": "tooshort", "Offset": "x", "Size": "-5" } ] }, + { "Filename": "b.bin" } + ] + }"#; + let m = parse_manifest(json.as_bytes()).expect("json parse"); + assert_eq!(m.chunk_dir, "ChunksV3"); + assert_eq!(m.unique_chunks.len(), 2, "keys shorter than 32 chars are skipped"); + let c0 = &m.unique_chunks[0]; + assert_eq!(c0.guid, [1, 2, 3, 0x0000_000F]); + assert_eq!(c0.hash, 0x0123_4567_89AB_CDEF); + assert_eq!(c0.group_num, 7); + assert_eq!(c0.file_size, 0xFF); + assert_eq!(c0.window_size, 0); + assert!(c0.sha1.is_none()); + let c1 = &m.unique_chunks[1]; + assert_eq!(c1.hash, 0, "unparseable hash → 0"); + assert_eq!(c1.group_num, 0, "unparseable group → 0"); + assert_eq!(c1.file_size, 0, "missing size → 0"); + + assert_eq!(m.files.len(), 2); + assert_eq!(m.files[0].install_tags, vec!["German".to_string()]); + assert_eq!(m.files[0].parts.len(), 2); + assert_eq!(m.files[0].parts[0].offset, 12); + assert_eq!(m.files[0].parts[0].size, 34); + assert_eq!(m.files[0].parts[1].guid, [0; 4]); + assert_eq!(m.files[0].parts[1].offset, 0); + assert_eq!(m.files[0].parts[1].size, -5); + assert!(m.files[0].sha1.is_none()); + assert!(m.files[1].parts.is_empty()); + } + + #[test] + fn json_manifest_failures_match_java_nulls() { + assert!(parse_manifest(br#"{"FileManifestList": []}"#).is_err(), "no ChunkHashList"); + assert!(parse_manifest(br#"{"ChunkHashList": {}}"#).is_err(), "no FileManifestList"); + assert!( + parse_manifest(br#"{"ChunkHashList": {"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz": "0"}, "FileManifestList": []}"#) + .is_err(), + "invalid GUID hex throws out of the parse" + ); + assert!(parse_manifest(b"not json at all").is_err()); + } +} diff --git a/app/src/main/rust/stores/src/epic/plan.rs b/app/src/main/rust/stores/src/epic/plan.rs new file mode 100644 index 0000000000..3dc2eeeb85 --- /dev/null +++ b/app/src/main/rust/stores/src/epic/plan.rs @@ -0,0 +1,289 @@ +//! Chunk plan — the part of `EpicDownloadManager.install` between "manifest parsed" and "submit +//! to the pool", ported 1:1: install-tag file selection (`resolveInstallFiles` / +//! `getRequiredInstallFiles`), the unique-chunk list for a file set (`uniqueChunksForFiles`), +//! the byte total (`Σ max(fileSize, 1)`), chunk cache paths and CDN URLs. +//! +//! At runtime the Java manager passes the INDICES of its `pendingFiles` (post delta/verify), so +//! the file selection functions here are exercised by the unit tests (and available to a future +//! caller) while the driver only runs `unique_chunks_for_files` on Java's pending set — the same +//! function Java runs, on the same manifest, in the same order. + +use std::collections::{HashMap, HashSet}; +use std::path::{Path, PathBuf}; + +use super::manifest::{ChunkInfo, Manifest}; + +/// `getRequiredInstallFiles`: files with no install tags; if NONE carries a tag, every file. +pub fn required_install_files(m: &Manifest) -> Vec<usize> { + let required: Vec<usize> = m + .files + .iter() + .enumerate() + .filter(|(_, f)| f.install_tags.is_empty()) + .map(|(i, _)| i) + .collect(); + if required.is_empty() { + (0..m.files.len()).collect() + } else { + required + } +} + +/// `resolveInstallFiles(manifest, selectedTags)`: +/// - `None` → every file (legacy / DLC / overlay path); +/// - `Some([])` → required(base) files only; +/// - otherwise → base files + any file carrying at least one selected tag, falling back to +/// required-only when nothing matched. +pub fn resolve_install_files(m: &Manifest, selected_tags: Option<&[String]>) -> Vec<usize> { + let Some(tags) = selected_tags else { + return (0..m.files.len()).collect(); + }; + if tags.is_empty() { + return required_install_files(m); + } + let want: HashSet<&str> = tags.iter().map(String::as_str).collect(); + let mut out = Vec::new(); + for (i, f) in m.files.iter().enumerate() { + if f.install_tags.is_empty() { + out.push(i); + continue; + } + if f.install_tags.iter().any(|t| want.contains(t.as_str())) { + out.push(i); + } + } + if out.is_empty() { + required_install_files(m) + } else { + out + } +} + +/// `uniqueChunksForFiles(manifest, files)`: chunk indices (into `unique_chunks`) in first-seen +/// order over `files[*].parts`, each GUID once, GUIDs absent from the chunk list dropped. +pub fn unique_chunks_for_files(m: &Manifest, file_indices: &[usize]) -> Vec<usize> { + let by_guid: HashMap<String, usize> = m.chunk_index_by_guid(); + let mut seen: HashSet<String> = HashSet::new(); + let mut chunks = Vec::new(); + for &fi in file_indices { + let Some(f) = m.files.get(fi) else { + continue; + }; + for p in &f.parts { + let g = p.guid_str(); + if seen.insert(g.clone()) { + if let Some(&ci) = by_guid.get(&g) { + chunks.push(ci); + } + } + } + } + chunks +} + +/// `totalBytes += Math.max(chunk.fileSize, 1)` over the needed chunks. +pub fn total_credit_bytes(m: &Manifest, chunk_indices: &[usize]) -> u64 { + chunk_indices + .iter() + .filter_map(|&i| m.unique_chunks.get(i)) + .map(ChunkInfo::credit_bytes) + .sum() +} + +/// `<installDir>/.chunks`. +pub fn chunk_cache_dir(install_dir: &str) -> PathBuf { + Path::new(install_dir).join(super::CHUNK_CACHE_DIR) +} + +/// Where this run's chunks go: `cache_dir` when the caller named one, else the default +/// `<installDir>/.chunks`. A separate directory exists so the cache can sit on internal storage +/// while the game installs to an SD card: a chunk is written once into the cache and once into +/// the assembled file, and with both on a 26 MB/s card the second write starved the fetchers. +pub fn resolve_cache_dir(install_dir: &str, cache_dir: &str) -> PathBuf { + if cache_dir.is_empty() { + chunk_cache_dir(install_dir) + } else { + PathBuf::from(cache_dir) + } +} + +/// Final cache file for a chunk: `new File(chunkCacheDir, chunk.guidStr())`. +pub fn cached_chunk_path(cache_dir: &Path, chunk: &ChunkInfo) -> PathBuf { + cache_dir.join(chunk.guid_str()) +} + +/// The Java pool's skip rule: a chunk whose cache file EXISTS (any size) is not fetched. +pub fn is_chunk_cached(cache_dir: &Path, chunk: &ChunkInfo) -> bool { + cached_chunk_path(cache_dir, chunk).exists() +} + +/// One CDN entry as the Java manager passes it: `cdn.baseUrl + cdn.cloudDir` (auth params are +/// never used on chunk URLs). Chunk URL = `prefix + "/" + chunk.getPath(chunkDir)`. +pub fn chunk_url(cdn_prefix: &str, chunk_dir: &str, chunk: &ChunkInfo) -> String { + format!("{}/{}", cdn_prefix, chunk.path(chunk_dir)) +} + +/// Undo the JSON string escaping the Java CDN scanner passes through. `EpicApiClient` re-serializes +/// the manifest API response with Android's `org.json`, which escapes every `/` as `\/`, and +/// `parseCdnUrls` takes the raw substrings — so a base URL arrives as `https:\/\/host\` and a cloud +/// dir as `/Builds\/Org\/…\/default\`. Java's `HttpURLConnection` (OkHttp) treats `\` as `/` and +/// only ever reached the first CDN, so it never noticed; reqwest's URL parser does the same +/// mapping, which leaves empty `//` path segments that Fastly/Akamai tolerate but CloudFront +/// rejects outright (immediate 4xx). Produce the URL Java intended: `\/` → `/`, any other `\` +/// dropped, and no trailing slash (the chunk path is joined with one). +pub fn normalize_prefix(raw: &str) -> String { + let unescaped = raw.replace("\\/", "/").replace('\\', ""); + unescaped.trim_end_matches('/').to_string() +} + +/// Distinct CDN prefixes in first-seen order, normalized (the fetch core wants one host key per +/// distinct endpoint; Java would simply try an identical URL twice, which changes nothing). +pub fn distinct_prefixes(cdn_prefixes: &[String]) -> Vec<String> { + let mut seen = HashSet::new(); + let mut out = Vec::new(); + for p in cdn_prefixes { + let n = normalize_prefix(p); + if n.is_empty() { + continue; + } + if seen.insert(n.clone()) { + out.push(n); + } + } + out +} + +/// Improvements round 1: split the window across the distinct CDNs so the whole ceiling is +/// reachable whatever the host count — `max(6, ceil(max_workers / hosts))` (3 Epic CDNs at a +/// ceiling of 32 → 11 each; a single host gets all 32). +pub fn per_host_cap(max_workers: usize, host_count: usize) -> usize { + let hosts = host_count.max(1); + let workers = max_workers.max(1); + let split = (workers + hosts - 1) / hosts; + split.max(6) +} + +#[cfg(test)] +mod tests { + use super::super::manifest::test_support::*; + use super::super::manifest::parse_manifest; + use super::*; + + fn manifest() -> Manifest { + parse_manifest(&build_manifest(&sample_chunks(), &sample_files(), 21, false)).unwrap() + } + + #[test] + fn install_tag_rules_match_java() { + let m = manifest(); + // null → all + assert_eq!(resolve_install_files(&m, None), vec![0, 1, 2]); + // empty → required only (file 0 has no tags) + assert_eq!(resolve_install_files(&m, Some(&[])), vec![0]); + // German → base + the German file + let de = vec!["German".to_string(), "de-DE".to_string()]; + assert_eq!(resolve_install_files(&m, Some(&de)), vec![0, 1]); + // unknown tag → nothing extra matched, but base files matched, so base only + let xx = vec!["Klingon".to_string()]; + assert_eq!(resolve_install_files(&m, Some(&xx)), vec![0]); + } + + #[test] + fn manifest_without_tags_treats_every_file_as_required() { + let mut files = sample_files(); + for f in files.iter_mut() { + f.tags.clear(); + } + let m = parse_manifest(&build_manifest(&sample_chunks(), &files, 21, false)).unwrap(); + assert_eq!(required_install_files(&m), vec![0, 1, 2]); + assert_eq!(resolve_install_files(&m, Some(&[])), vec![0, 1, 2]); + } + + #[test] + fn manifest_where_every_file_is_tagged_falls_back_to_all() { + let mut files = sample_files(); + files[0].tags = vec!["English".to_string()]; + let m = parse_manifest(&build_manifest(&sample_chunks(), &files, 21, false)).unwrap(); + // No untagged file, unknown tag selected → matched nothing → required → (none) → all. + let xx = vec!["Klingon".to_string()]; + assert_eq!(resolve_install_files(&m, Some(&xx)), vec![0, 1, 2]); + } + + #[test] + fn unique_chunks_follow_first_seen_part_order() { + let m = manifest(); + // file 2 first: chunk c[2] then c[0]; then file 0: c[0] (dup), c[1]. + assert_eq!(unique_chunks_for_files(&m, &[2, 0]), vec![2, 0, 1]); + assert_eq!(unique_chunks_for_files(&m, &[1]), vec![1]); + assert!(unique_chunks_for_files(&m, &[]).is_empty()); + assert!(unique_chunks_for_files(&m, &[99]).is_empty(), "out-of-range index ignored"); + // total = 700000 + max(0,1) + 9 + assert_eq!(total_credit_bytes(&m, &[0, 1, 2]), 700_000 + 1 + 9); + } + + #[test] + fn parts_referencing_unknown_guids_are_dropped() { + let mut files = sample_files(); + files[0] + .parts + .push(([0x1234, 0x5678, 0x9ABC, 0xDEF0], 0, 1)); + let m = parse_manifest(&build_manifest(&sample_chunks(), &files, 21, false)).unwrap(); + assert_eq!(unique_chunks_for_files(&m, &[0]), vec![0, 1]); + } + + #[test] + fn urls_and_cache_paths_match_java() { + let m = manifest(); + let c = &m.unique_chunks[0]; + assert_eq!( + chunk_url("https://fastly-download.epicgames.com/Builds/Org/o-x/y/default", "ChunksV4", c), + "https://fastly-download.epicgames.com/Builds/Org/o-x/y/default/ChunksV4/05/0123456789ABCDEF_00000001000000020000000300000004.chunk" + ); + let cache = chunk_cache_dir("/data/x/imagefs/epic_games/Game"); + assert_eq!(cache, PathBuf::from("/data/x/imagefs/epic_games/Game/.chunks")); + assert_eq!(resolve_cache_dir("/data/x/imagefs/epic_games/Game", ""), cache); + assert_eq!( + resolve_cache_dir("/sdcard/Games/Game", "/data/x/cache/epic/Game"), + PathBuf::from("/data/x/cache/epic/Game") + ); + assert_eq!( + cached_chunk_path(&cache, c), + PathBuf::from("/data/x/imagefs/epic_games/Game/.chunks/00000001000000020000000300000004") + ); + let prefixes = vec!["a".to_string(), "b".to_string(), "a".to_string()]; + assert_eq!(distinct_prefixes(&prefixes), vec!["a".to_string(), "b".to_string()]); + } + + #[test] + fn prefixes_are_unescaped_from_the_java_json_scan() { + // Exactly what Bannerlator's Epic debug log shows its Java manager extracting for a real title. + let raw = "https:\\/\\/egdownload.fastly-edge.com\\/Builds\\/Org\\/o-83e8\\/04b7\\/default\\"; + assert_eq!( + normalize_prefix(raw), + "https://egdownload.fastly-edge.com/Builds/Org/o-83e8/04b7/default" + ); + // Already-clean input is untouched (minus a trailing slash). + assert_eq!( + normalize_prefix("https://download.epicgames.com/Builds/x/default/"), + "https://download.epicgames.com/Builds/x/default" + ); + let m = manifest(); + let c = &m.unique_chunks[0]; + assert_eq!( + chunk_url(&normalize_prefix(raw), "ChunksV4", c), + "https://egdownload.fastly-edge.com/Builds/Org/o-83e8/04b7/default/ChunksV4/05/0123456789ABCDEF_00000001000000020000000300000004.chunk" + ); + let two = vec![raw.to_string(), "https://egdownload.fastly-edge.com/Builds/Org/o-83e8/04b7/default".to_string(), String::new()]; + assert_eq!(distinct_prefixes(&two).len(), 1, "escaped and clean forms collapse; empty dropped"); + } + + #[test] + fn per_host_cap_splits_the_window_with_a_floor_of_six() { + assert_eq!(per_host_cap(32, 3), 11); + assert_eq!(per_host_cap(32, 1), 32); + assert_eq!(per_host_cap(32, 2), 16); + assert_eq!(per_host_cap(8, 3), 6, "floor"); + assert_eq!(per_host_cap(8, 1), 8); + assert_eq!(per_host_cap(0, 0), 6, "degenerate inputs"); + } +} diff --git a/app/src/main/rust/stores/src/fetch_core.rs b/app/src/main/rust/stores/src/fetch_core.rs new file mode 100644 index 0000000000..e700ee3a2e --- /dev/null +++ b/app/src/main/rust/stores/src/fetch_core.rs @@ -0,0 +1,2656 @@ +//! Shared adaptive HTTP fetch core for the store download engines (Epic / GOG / Amazon). +//! +//! This is the generic half of the fetch layer Bannerlator's Steam depot writer grew: an +//! adaptive in-flight window (slow-start → hold at plateau → shrink on REAL errors only), a +//! speed-ranked per-host-capped scheduler, an in-flight RAW byte budget sized from the window +//! ceiling, per-item retry with exponential back-off + host rotation, one current-thread tokio +//! runtime driving every request through a [`FuturesUnordered`], and an mpsc hand-off to a sync +//! process pool that calls the adapter's [`FetchSink::process`] (inflate + hash + write). +//! +//! The tunables come from [`crate::fetch_tuning`], copied number for number from Bannerlator's +//! depot writer, so a `fetch-window` line from either app reads against the same tuning notes. +//! +//! What the core does NOT know: manifests, resume/skip rules, output layout, hashing. The adapter +//! builds the item list (already excluding present+verified items, exactly as its Java manager +//! does), maps host index → URL, and does everything with the body inside its sink. +//! +//! Log grammar (all lines go through the `log` callback; `label` is adapter-chosen, e.g. +//! `epic app=Fortnite`): +//! - `fetch-start label=… items=… bytes_reserved=… hosts=… ceiling=… budget=…MiB tier_max=… per_host_cap=… distinct_hosts=… window=… mode=body|stream` +//! - `fetch-window label=… window=N (min=… max=…) in_flight=… last=…MB/s ewma=…MB/s best=…MB/s reason=… cooldown=…ms err_rate=…% phase=… rtt=…ms budget_stalls=… host_stalls=…` +//! (same fields as the Steam engine's `fetch-window depot=…` line, so the two are A/B-comparable) +//! - `throughput label=… overall=…MB/s total=…MB elapsed=…s used=x/y servers: [host …MB/s …MB] …` every 5 s +//! - `fetch-end label=… items_ok=… bytes=… credited=… elapsed_ms=… avg_mbps=… peak_mbps=… result=ok|cancelled|error [error=…]` +//! +//! `MB/s` here means MiB/s (1024²), matching the Steam engine's lines. + +use crate::fetch_tuning::{ + budget_admits, connect_timeout, inflight_budget_bytes, retry_backoff_millis, AsyncFetchError, + FetchFailKind, BOOTSTRAP_WINDOW, MAX_CHUNK_ATTEMPTS, NOMINAL_CHUNK_RESERVE_BYTES, + RATE_LIMIT_COOLDOWN_MS, SERVER_EXPLORE_EVERY, USER_AGENT, WINDOW_BPS_EWMA_ALPHA, + WINDOW_COOLDOWN_MS, WINDOW_DECLINE_EPS, WINDOW_ERR_BURST_IMMEDIATE, WINDOW_ERR_RATE_HIGH, + WINDOW_ERR_RATE_LOW, WINDOW_HARD_CAP, WINDOW_IMPROVE_EPS, WINDOW_LOG_EVERY_PROBES, + WINDOW_MIN_FLOOR, WINDOW_PLATEAU_PATIENCE, WINDOW_PLATEAU_REARM_MAX_MS, + WINDOW_PLATEAU_REARM_MS, WINDOW_PROBE_INTERVAL_MS, WINDOW_SHRINK_FACTOR, + WINDOW_SLOW_START_FACTOR, WINDOW_STEP_UP, +}; +use futures_util::future::{select, Either}; +use futures_util::stream::FuturesUnordered; +use futures_util::StreamExt; +use std::collections::{HashMap, VecDeque}; +use std::fs; +use std::pin::pin; +use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; +use std::sync::{mpsc as std_mpsc, Arc, Mutex}; +use std::thread; +use std::time::{Duration, Instant}; +use tokio::sync::{Notify, Semaphore}; + +/// Maximum attempts per item (fetch failures AND `SinkError::Retry` both count). Same as Steam. +pub const MAX_ITEM_ATTEMPTS: u32 = MAX_CHUNK_ATTEMPTS; +/// How often the throughput reporter samples (and logs) — the samples feed `peak_mbps`. +pub const THROUGHPUT_SAMPLE_INTERVAL_MS: u64 = 5_000; +/// Upper bound on how long the driver sits in one `await` before re-checking cancel/error/feedback. +const DRIVER_POLL_MS: u64 = 250; + +// ── Delay awareness (bufferbloat) ── +// On a 5G hotspot the window kept growing (31 → 84) while the per-request delay went from 3 s to +// 11 s and throughput stayed at 10-20 MB/s: every extra request only queued behind the others in +// the link's buffer. The signal used here is time-to-first-byte (request sent → response headers), +// which on a link with a deep buffer grows with the queue, while on a fast link it stays at a few +// round trips however wide the window gets. The baseline is the lowest per-probe mean seen. + +/// A probe needs at least this many TTFB samples before its mean is trusted. +pub const LATENCY_MIN_SAMPLES: u32 = 3; +/// Mean TTFB at or above `baseline ×` this counts as queueing delay… +pub const LATENCY_HOLD_FACTOR: f64 = 2.5; +/// …and only when it is also this far above the baseline in absolute terms: a fast link whose +/// TTFB goes from 20 ms to 60 ms (new connections' TLS handshakes) is not bufferbloat. +pub const LATENCY_MIN_EXCESS_MS: f64 = 250.0; +/// Throughput above `best × (1 + this)` is a gain that justifies the extra delay: hold, but never +/// shrink, while it lasts. +pub const LATENCY_GAIN_EPS: f64 = 0.25; +/// Gentle proportional shrink while the delay keeps rising (gentler than the error shrink). +pub const LATENCY_SHRINK_FACTOR: f64 = 0.85; + +// ── Stalled hosts ── +// A CDN host can stop delivering while its connections stay open: on the device the Fastly host +// froze at 49 MB for the rest of the run while the other host carried everything, and its requests +// sat until their idle timeout. A host whose in-flight requests receive no byte for this long, +// while another host is receiving, has its requests cancelled and re-queued and is put to rest. + +/// No byte from any of a host's in-flight requests for this long (while another host delivers). +pub const HOST_STALL_MS: u64 = if cfg!(test) { 1_500 } else { 10_000 }; +/// First rest for a stalled host; doubles per further stall of the same host, reset on success. +pub const HOST_STALL_COOLDOWN_MS: u64 = 30_000; +pub const HOST_STALL_COOLDOWN_MAX_MS: u64 = 300_000; +/// How often the driver looks for stalled hosts. +const STALL_CHECK_MS: u64 = 250; +/// Stream mode: the longest a request may sit with no byte arriving before it is failed (the +/// store's own timeout when it is shorter). A dead connection on a single-host run, where the +/// stall detector has no healthy host to compare with, never holds an item longer than this. +pub const STREAM_IDLE_CAP: Duration = Duration::from_secs(30); + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// Public contract +// ───────────────────────────────────────────────────────────────────────────────────────────── + +/// One unit of work: a body to fetch from one of the hosts and hand to the sink. +#[derive(Clone, Debug)] +pub struct FetchItem { + /// Adapter-defined (index into the adapter's own table). Only echoed in errors / logs. + pub id: u64, + /// `len == hosts.len()` → `urls[host_idx]`; `len == 1` → the same URL on every host. Any other + /// non-zero length falls back to `urls.get(host_idx)` then `urls[0]`. Empty = fatal. + pub urls: Vec<String>, + /// Expected RAW (wire) bytes, for the in-flight byte budget; `0` → nominal 1 MiB. Ignored in + /// stream mode (streams are budgeted piece by piece as they arrive). + pub reserve: u64, + /// Inclusive HTTP `Range` (`bytes=a-b`); `None` = whole body. + pub range: Option<(u64, u64)>, +} + +#[derive(Clone, Debug)] +pub struct FetchOptions { + /// Window ceiling (tier); the core clamps it to `hosts × per_host_cap` and the hard cap. + pub max_workers: usize, + /// Max concurrent requests per distinct host (6 unless the store needs otherwise). Honoured + /// as given: 1 host × `per_host_cap` 8 = a ceiling of 8. + pub per_host_cap: usize, + /// Whole-body mode: the deadline for the entire request (connect + headers + body). Stream + /// mode: the deadline for the response headers, then an IDLE deadline per received piece. + pub timeout: Duration, + /// Sent on EVERY request (applied on the request builder, so they override the client-level + /// defaults — e.g. a store's own `User-Agent`, or `Authorization: Bearer …`). Validated once + /// when the client is built. + pub headers: Vec<(String, String)>, + /// PEM bundle path; `""` = the platform roots reqwest/rustls ships with. + pub ca_bundle_path: String, + /// Sync process-pool threads (inflate + hash + write). + pub process_workers: usize, + /// Log prefix, e.g. `"epic app=Fortnite"`. + pub label: String, + /// `false` (default): each item's whole body is buffered and handed to + /// [`FetchSink::process`]. `true`: the body is streamed — pieces go to + /// [`FetchSink::on_chunk`] in order as they arrive, then [`FetchSink::on_finish`]; for + /// multi-GB files (Amazon) that keeps memory at "pieces not yet written", not whole files. + /// Applies to every item of the run. + pub stream: bool, +} + +impl Default for FetchOptions { + fn default() -> Self { + Self { + max_workers: BOOTSTRAP_WINDOW, + per_host_cap: crate::fetch_tuning::PER_HOST_CAP, + timeout: Duration::from_secs(60), + headers: Vec::new(), + ca_bundle_path: String::new(), + process_workers: 2, + label: String::new(), + stream: false, + } + } +} + +/// What the sink says about a body it was handed. +#[derive(Clone, Debug)] +pub enum SinkError { + /// Counts as a failed attempt for that item: host cooled + demoted, back-off, re-dispatch on a + /// different host, up to [`MAX_ITEM_ATTEMPTS`]. In stream mode the whole item is re-streamed + /// (`on_chunk` sees `offset == 0` again). + Retry(String), + /// Abort the whole run; this becomes the run's first error. + Fatal(String), +} + +/// Runs on a process-pool thread. Whole-body mode uses [`FetchSink::process`]; stream mode uses +/// [`FetchSink::on_chunk`] + [`FetchSink::on_finish`] (default impls refuse, so a whole-body sink +/// is unaffected by the stream option existing). +pub trait FetchSink: Send + Sync { + /// Whole-body mode: the raw wire body. Do inflate + hash verify + write here. Returns the + /// number of bytes to credit to progress (adapter's choice, e.g. decompressed bytes). + fn process(&self, item: &FetchItem, body: Vec<u8>) -> Result<u64, SinkError>; + + /// Stream mode: one piece of the body, delivered in order (`offset` = bytes of this attempt + /// delivered before this piece). `offset == 0` marks the (re)start of an attempt — truncate any + /// partial output from a previous attempt. Each processed piece credits `data.len()` bytes. + fn on_chunk(&self, item: &FetchItem, offset: u64, data: &[u8]) -> Result<(), SinkError> { + let _ = (item, offset, data); + Err(SinkError::Fatal("streaming not supported by this sink".to_string())) + } + + /// Stream mode: the body ended after `total_len` bytes. Finalize (rename `.tmp`, verify the + /// hash, …). Returns EXTRA bytes to credit (pieces were already credited as they were + /// written; return 0 for none). + fn on_finish(&self, item: &FetchItem, total_len: u64) -> Result<u64, SinkError> { + let _ = (item, total_len); + Err(SinkError::Fatal("streaming not supported by this sink".to_string())) + } +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct FetchOutcome { + /// Sum of what the sink returned for every successfully processed item (stream mode: every + /// piece written + every `on_finish` extra; pieces of an attempt that later failed stay + /// counted). + pub bytes_credited: u64, + /// First error (fetch after max attempts, sink `Fatal`, client build failure). + pub error: Option<String>, + /// `cancel` was observed set. + pub cancelled: bool, + /// Items the sink accepted. + pub items_ok: u64, +} + +impl FetchOutcome { + pub fn ok(&self) -> bool { + self.error.is_none() && !self.cancelled + } +} + +/// Blocking: spawns its own current-thread tokio runtime + process pool and returns when every +/// item is processed, `cancel` is set, or the first fatal error is recorded. `progress` fires from +/// a process-pool thread after each successful `process` / each written piece + `on_finish` (the +/// caller throttles). +pub fn run_fetch( + items: Vec<FetchItem>, + hosts: &[String], + opts: &FetchOptions, + sink: &dyn FetchSink, + cancel: &AtomicBool, + progress: &(dyn Fn(u64, u64) + Sync), + log: &(dyn Fn(&str) + Sync), +) -> FetchOutcome { + let started = Instant::now(); + let label = opts.label.as_str(); + let stream = opts.stream; + let hosts: Vec<String> = if hosts.is_empty() { + vec![String::from("default")] + } else { + hosts.to_vec() + }; + let per_host_cap = opts.per_host_cap.max(1); + let distinct_hosts = distinct_host_count(&hosts); + let (bootstrap, win_min, win_max) = + window_bounds(opts.max_workers, distinct_hosts, per_host_cap); + let budget = inflight_budget_bytes(win_max); + let bytes_reserved: u64 = items.iter().map(reserve_bytes).sum(); + + log(&format!( + "fetch-start label={label} items={} bytes_reserved={bytes_reserved} hosts={} ceiling={win_max} \ +budget={}MiB tier_max={} per_host_cap={per_host_cap} distinct_hosts={distinct_hosts} window={bootstrap} \ +mode={}", + items.len(), + hosts.len(), + budget / (1024 * 1024), + opts.max_workers, + if stream { "stream" } else { "body" }, + )); + + let bytes_credited = AtomicU64::new(0); + let items_ok = AtomicU64::new(0); + let in_flight = AtomicU64::new(0); + let error_slot: Mutex<Option<String>> = Mutex::new(None); + let reporter_done = AtomicBool::new(false); + let meter = BandwidthMeter::new(&hosts); + // Stream mode: per-item "attempt N was rejected by the sink mid-stream" flags, so the driver's + // in-flight stream stops reading instead of feeding pieces nobody wants. + let abandon: Vec<AtomicU32> = if stream { + items.iter().map(|_| AtomicU32::new(NO_ATTEMPT)).collect() + } else { + Vec::new() + }; + + let proc_count = opts.process_workers.max(1).min(items.len().max(1)); + let items = &items; + let meter = &meter; + let abandon = &abandon; + + thread::scope(|scope| { + // Async(fetch) → sync(process): tokio unbounded mpsc drained with `blocking_recv`. Memory is + // bounded by the RAW byte budget enforced at DISPATCH (whole-body) / per piece (stream), not + // by channel capacity. Whole-body mode: ONE channel shared by every worker (any worker takes + // the next body). Stream mode: one channel PER worker and an item's pieces always go to the + // same worker (`idx % workers`), so pieces are written in order. + let channel_count = if stream { proc_count } else { 1 }; + let mut txs: Vec<tokio::sync::mpsc::UnboundedSender<ProcessMsg>> = + Vec::with_capacity(channel_count); + let mut rxs: Vec<Arc<Mutex<tokio::sync::mpsc::UnboundedReceiver<ProcessMsg>>>> = + Vec::with_capacity(channel_count); + for _ in 0..channel_count { + let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<ProcessMsg>(); + txs.push(tx); + rxs.push(Arc::new(Mutex::new(rx))); + } + // Sync(process) → async(driver): sink verdicts, polled non-blocking by the driver. + let (fb_tx, fb_rx) = std_mpsc::channel::<Feedback>(); + let bytes_credited = &bytes_credited; + let items_ok = &items_ok; + let in_flight = &in_flight; + let error_slot = &error_slot; + let reporter_done = &reporter_done; + + // Periodic throughput reporter — atomics only, never on the fetch hot path. Its samples are + // also the `peak_mbps` source. + let reporter = scope.spawn(move || { + crate::priority::background(); + loop { + let mut waited = 0u64; + while waited < THROUGHPUT_SAMPLE_INTERVAL_MS { + if reporter_done.load(Ordering::Relaxed) { + return; + } + thread::sleep(Duration::from_millis(100)); + waited += 100; + } + if reporter_done.load(Ordering::Relaxed) { + return; + } + meter.sample(); + log(&meter.summary_line(label)); + } + }); + + // ── Process pool ── + let mut proc_handles = Vec::with_capacity(proc_count); + for worker in 0..proc_count { + let rx = Arc::clone(&rxs[if stream { worker } else { 0 }]); + let fb = fb_tx.clone(); + proc_handles.push(scope.spawn(move || { + crate::priority::background(); + // Stream mode: (item → attempt, message) whose pieces the sink rejected; the rest + // of that attempt's pieces are dropped and its Finish becomes a Retry verdict. + let mut poisoned: HashMap<usize, (u32, String)> = HashMap::new(); + loop { + if cancel.load(Ordering::Relaxed) { + return; + } + if error_slot.lock().expect("err slot poisoned").is_some() { + return; + } + let msg = { + let mut guard = rx.lock().expect("rx poisoned"); + guard.blocking_recv() + }; + let Some(msg) = msg else { + return; + }; + match msg { + ProcessMsg::Body { + idx, + attempts, + server_idx, + body, + } => { + let raw_len = body.len() as u64; + // A body that was already queued when cancel / the first error landed + // must not be written any more. + if cancel.load(Ordering::Relaxed) + || error_slot.lock().expect("err slot poisoned").is_some() + { + in_flight.fetch_sub(raw_len, Ordering::Relaxed); + return; + } + let item = &items[idx]; + let res = sink.process(item, body); + // Free the budget on PROCESS-COMPLETE (actual raw bytes). + in_flight.fetch_sub(raw_len, Ordering::Relaxed); + let verdict = match res { + Ok(credited) => { + let total = bytes_credited + .fetch_add(credited, Ordering::Relaxed) + + credited; + let ok = items_ok.fetch_add(1, Ordering::Relaxed) + 1; + progress(total, ok); + SinkVerdict::Ok + } + Err(SinkError::Retry(message)) => SinkVerdict::Retry(message), + Err(SinkError::Fatal(message)) => { + record_first_error( + error_slot, + format!("{label}: item {} failed: {message}", item.id), + ); + SinkVerdict::Fatal + } + }; + let _ = fb.send(Feedback { + idx, + attempts, + server_idx, + verdict, + }); + } + ProcessMsg::Piece { + idx, + attempts, + offset, + data, + } => { + let len = data.len() as u64; + in_flight.fetch_sub(len, Ordering::Relaxed); + if poisoned.get(&idx).is_some_and(|(a, _)| *a == attempts) { + continue; // rest of a rejected attempt + } + if cancel.load(Ordering::Relaxed) + || error_slot.lock().expect("err slot poisoned").is_some() + { + return; + } + let item = &items[idx]; + match sink.on_chunk(item, offset, &data) { + Ok(()) => { + let total = + bytes_credited.fetch_add(len, Ordering::Relaxed) + len; + progress(total, items_ok.load(Ordering::Relaxed)); + } + Err(SinkError::Retry(message)) => { + poisoned.insert(idx, (attempts, message)); + if let Some(flag) = abandon.get(idx) { + flag.store(attempts, Ordering::Relaxed); + } + } + Err(SinkError::Fatal(message)) => { + record_first_error( + error_slot, + format!("{label}: item {} failed: {message}", item.id), + ); + if let Some(flag) = abandon.get(idx) { + flag.store(attempts, Ordering::Relaxed); + } + return; + } + } + } + ProcessMsg::Finish { + idx, + attempts, + server_idx, + total_len, + } => { + let item = &items[idx]; + let verdict = if poisoned + .get(&idx) + .is_some_and(|(a, _)| *a == attempts) + { + let (_, message) = poisoned.remove(&idx).expect("poisoned"); + SinkVerdict::Retry(message) + } else { + match sink.on_finish(item, total_len) { + Ok(extra) => { + let total = bytes_credited + .fetch_add(extra, Ordering::Relaxed) + + extra; + let ok = items_ok.fetch_add(1, Ordering::Relaxed) + 1; + progress(total, ok); + SinkVerdict::Ok + } + Err(SinkError::Retry(message)) => SinkVerdict::Retry(message), + Err(SinkError::Fatal(message)) => { + record_first_error( + error_slot, + format!( + "{label}: item {} failed: {message}", + item.id + ), + ); + SinkVerdict::Fatal + } + } + }; + let _ = fb.send(Feedback { + idx, + attempts, + server_idx, + verdict, + }); + } + } + } + })); + } + drop(fb_tx); + + // ── Async fetch driver on one current-thread runtime. `txs` are moved in and dropped when + // the driver returns, which closes the channels so the pool's `blocking_recv` yields + // `None`. ── + // The runtime drives on this (already lowered) thread; `on_thread_start` covers the + // blocking pool it starts for DNS lookups. + match tokio::runtime::Builder::new_current_thread() + .enable_all() + .on_thread_start(crate::priority::background) + .build() + { + Ok(rt) => { + rt.block_on(run_driver(DriverCtx { + items: items.as_slice(), + hosts: hosts.as_slice(), + opts, + label, + cancel, + log, + meter, + in_flight, + error_slot, + abandon: abandon.as_slice(), + txs, + fb_rx, + budget, + bootstrap, + win_min, + win_max, + per_host_cap, + })); + } + Err(err) => { + record_first_error(error_slot, format!("{label}: tokio runtime: {err}")); + drop(txs); + } + } + + for handle in proc_handles { + let _ = handle.join(); + } + reporter_done.store(true, Ordering::Relaxed); + let _ = reporter.join(); + }); + + // Final partial-interval sample so a short run still has a peak. + meter.sample(); + let elapsed = started.elapsed(); + let elapsed_secs = elapsed.as_secs_f64().max(0.001); + let wire_total = meter.total_bytes(); + let avg_mbps = (wire_total as f64) / (1024.0 * 1024.0) / elapsed_secs; + let peak_mbps = (meter.peak_bps() / (1024.0 * 1024.0)).max(avg_mbps); + let cancelled = cancel.load(Ordering::Relaxed); + let error = error_slot.lock().expect("err slot poisoned").take(); + let outcome = FetchOutcome { + bytes_credited: bytes_credited.load(Ordering::Relaxed), + error, + cancelled, + items_ok: items_ok.load(Ordering::Relaxed), + }; + let result = if cancelled { + "cancelled" + } else if outcome.error.is_some() { + "error" + } else { + "ok" + }; + let mut line = format!( + "fetch-end label={label} items_ok={} bytes={wire_total} credited={} elapsed_ms={} \ +avg_mbps={avg_mbps:.2} peak_mbps={peak_mbps:.2} result={result}", + outcome.items_ok, + outcome.bytes_credited, + elapsed.as_millis(), + ); + if let Some(err) = &outcome.error { + line.push_str(" error="); + line.push_str(err); + } + log(&line); + outcome +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// Pure helpers (unit-tested) +// ───────────────────────────────────────────────────────────────────────────────────────────── + +/// Bytes reserved against the in-flight budget at DISPATCH (nominal when the adapter has none). +pub fn reserve_bytes(item: &FetchItem) -> u64 { + if item.reserve == 0 { + NOMINAL_CHUNK_RESERVE_BYTES + } else { + item.reserve + } +} + +/// Window bounds: `max` = tier ceiling clamped to distinct-hosts × per-host-cap and the hard cap; +/// `min` = the floor; `bootstrap` = the start. Same arithmetic as the Steam depot writer. +pub fn window_bounds( + max_workers: usize, + distinct_hosts: usize, + per_host_cap: usize, +) -> (usize, usize, usize) { + let host_ceiling = distinct_hosts.saturating_mul(per_host_cap).max(1); + let max = max_workers.max(1).min(host_ceiling).min(WINDOW_HARD_CAP); + let min = WINDOW_MIN_FLOOR.min(max).max(1); + let bootstrap = BOOTSTRAP_WINDOW.clamp(min, max); + (bootstrap, min, max) +} + +/// Distinct host count (string equality) — the multiplier for the window's host ceiling. +pub fn distinct_host_count(hosts: &[String]) -> usize { + let mut keys: Vec<&str> = Vec::new(); + for h in hosts { + if !keys.contains(&h.as_str()) { + keys.push(h.as_str()); + } + } + keys.len().max(1) +} + +/// The URL to use for `item` on host `host_idx` (see [`FetchItem::urls`]). `None` when the item +/// carries no URL at all. +pub fn url_for_host(item: &FetchItem, host_idx: usize) -> Option<&str> { + if item.urls.is_empty() { + return None; + } + if item.urls.len() == 1 { + return Some(item.urls[0].as_str()); + } + Some( + item.urls + .get(host_idx) + .unwrap_or(&item.urls[0]) + .as_str(), + ) +} + +/// Byte count an inclusive range must deliver; `None` for an inverted range. +pub fn range_len(range: (u64, u64)) -> Option<u64> { + let (a, b) = range; + if b < a { + None + } else { + Some(b - a + 1) + } +} + +/// Validates a received body's length: for a range request it must be exactly `b-a+1`; otherwise +/// it must match `Content-Length` when the server sent one (as the Steam client does). +pub fn validate_body_len( + body_len: u64, + content_length: Option<u64>, + range: Option<(u64, u64)>, +) -> Result<(), AsyncFetchError> { + if let Some(range) = range { + let expected = match range_len(range) { + Some(n) => n, + None => { + return Err(AsyncFetchError { + message: format!("invalid range {}-{}", range.0, range.1), + kind: FetchFailKind::Other, + }) + } + }; + if body_len != expected { + return Err(AsyncFetchError { + message: format!("range body length mismatch (got {body_len}, want {expected})"), + kind: FetchFailKind::ServerFault, + }); + } + return Ok(()); + } + if let Some(expected) = content_length { + if body_len != expected { + return Err(AsyncFetchError { + message: format!("body truncated (length mismatch: got {body_len}, want {expected})"), + kind: FetchFailKind::ServerFault, + }); + } + } + Ok(()) +} + +/// Whether an HTTP status is acceptable for this request shape (200 always; 206 only when a range +/// was asked for). +pub fn status_accepted(status: u16, ranged: bool) -> bool { + status == 200 || (ranged && status == 206) +} + +/// Classifies a rejected HTTP status the same way the Steam async client does. +pub fn classify_status(status: u16) -> FetchFailKind { + if status == 429 { + FetchFailKind::RateLimited + } else if (500..600).contains(&status) { + FetchFailKind::ServerFault + } else { + FetchFailKind::Other + } +} + +/// The HTTP status carried by a `send_checked` rejection message, if that is what the error was. +/// A 4xx other than 429 is a CLIENT rejection (bad/expired signed URL, wrong host for this path, +/// 404): it says nothing about congestion, so the driver demotes the host but does not shrink the +/// shared window for it — one CDN that refuses a path must not throttle the healthy ones. +pub fn rejected_status(message: &str) -> Option<u16> { + let rest = message.strip_prefix("unexpected HTTP status (")?; + let digits = rest.strip_suffix(')')?; + digits.parse().ok() +} + +pub fn is_client_rejection(message: &str) -> bool { + matches!(rejected_status(message), Some(s) if (400..500).contains(&s) && s != 429) +} + +/// How many individual fetch failures a run logs verbatim before going quiet (the window and +/// throughput lines keep reporting the aggregate). +pub const FETCH_ERROR_LOG_LIMIT: u32 = 12; + +fn record_first_error(slot: &Mutex<Option<String>>, err: String) { + let mut guard = slot.lock().expect("err slot poisoned"); + if guard.is_none() { + *guard = Some(err); + } +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// HTTP client (one pooled reqwest client + per-request headers + optional Range + streaming) +// ───────────────────────────────────────────────────────────────────────────────────────────── + +/// The pooled client plus the pre-validated per-request headers. +struct Http { + client: reqwest::Client, + headers: Vec<(reqwest::header::HeaderName, reqwest::header::HeaderValue)>, +} + +fn build_http(opts: &FetchOptions, pool_max_idle_per_host: usize) -> Result<Http, String> { + let mut headers = Vec::with_capacity(opts.headers.len()); + for (name, value) in &opts.headers { + let hname = reqwest::header::HeaderName::from_bytes(name.as_bytes()) + .map_err(|err| format!("bad header name '{name}': {err}"))?; + let hvalue = reqwest::header::HeaderValue::from_str(value) + .map_err(|err| format!("bad header value for '{name}': {err}"))?; + headers.push((hname, hvalue)); + } + let mut builder = reqwest::Client::builder() + .user_agent(USER_AGENT) + .connect_timeout(connect_timeout()) + .pool_max_idle_per_host(pool_max_idle_per_host.max(1)) + .pool_idle_timeout(Duration::from_secs(90)); + if !opts.ca_bundle_path.is_empty() { + let pem = fs::read(&opts.ca_bundle_path).map_err(|err| format!("read CA bundle: {err}"))?; + let certs = reqwest::Certificate::from_pem_bundle(&pem) + .map_err(|err| format!("parse CA bundle: {err}"))?; + for cert in certs { + builder = builder.add_root_certificate(cert); + } + } + let client = builder.build().map_err(|err| format!("http client: {err}"))?; + Ok(Http { client, headers }) +} + +impl Http { + /// `GET url` with the run's headers (set per request, so they override the client defaults — + /// a store's own `User-Agent` wins over the Steam one) and the optional `Range`. + fn request(&self, url: &str, range: Option<(u64, u64)>) -> reqwest::RequestBuilder { + let mut request = self.client.get(url); + for (name, value) in &self.headers { + request = request.header(name.clone(), value.clone()); + } + if let Some((a, b)) = range { + request = request.header(reqwest::header::RANGE, format!("bytes={a}-{b}")); + } + request + } +} + +fn classify_reqwest_error(err: &reqwest::Error) -> FetchFailKind { + if err.is_timeout() { + FetchFailKind::Timeout + } else if err.is_connect() { + FetchFailKind::Connect + } else { + FetchFailKind::Other + } +} + +/// Sends the request and checks the status; shared by both modes. +async fn send_checked( + http: &Http, + url: &str, + range: Option<(u64, u64)>, + timeout: Option<Duration>, +) -> Result<reqwest::Response, AsyncFetchError> { + let mut request = http.request(url, range); + if let Some(timeout) = timeout { + request = request.timeout(timeout); + } + let response = match request.send().await { + Ok(response) => response, + Err(err) => { + return Err(AsyncFetchError { + message: format!("http get: {err}"), + kind: classify_reqwest_error(&err), + }); + } + }; + let status = response.status().as_u16(); + if !status_accepted(status, range.is_some()) { + return Err(AsyncFetchError { + message: format!("unexpected HTTP status ({status})"), + kind: classify_status(status), + }); + } + Ok(response) +} + +/// Whole-body mode: one attempt for one item (the driver owns retry/rotation). `timeout` bounds the +/// whole request (headers + body). The body is read piece by piece so `host_rx` sees every byte as +/// it arrives (the stall detector's signal); `ttfb` is set when the response headers arrive. +async fn fetch_once( + http: &Http, + url: &str, + range: Option<(u64, u64)>, + timeout: Duration, + host_rx: &AtomicU64, + started: Instant, + ttfb: &mut Option<Duration>, +) -> Result<Vec<u8>, AsyncFetchError> { + let whole = async { + let mut response = send_checked(http, url, range, None).await?; + *ttfb = Some(started.elapsed()); + let content_length = response.content_length(); + let mut body = Vec::with_capacity(content_length.unwrap_or(0).min(64 << 20) as usize); + loop { + match response.chunk().await { + Ok(Some(piece)) => { + host_rx.fetch_add(piece.len() as u64, Ordering::Relaxed); + body.extend_from_slice(&piece); + } + Ok(None) => break, + Err(err) => { + let kind = if err.is_timeout() { + FetchFailKind::Timeout + } else { + FetchFailKind::Other + }; + return Err(AsyncFetchError { + message: format!("http body: {err}"), + kind, + }); + } + } + } + validate_body_len(body.len() as u64, content_length, range)?; + Ok(body) + }; + match tokio::time::timeout(timeout, whole).await { + Ok(res) => res, + Err(_) => Err(AsyncFetchError { + message: "http get: request timed out".to_string(), + kind: FetchFailKind::Timeout, + }), + } +} + +/// Stream mode: one attempt for one item. Pieces are pushed to the item's pool worker as they +/// arrive (each one added to the in-flight budget until the pool consumes it); the future waits +/// while the budget is full, and stops early on cancel or when the sink rejected this attempt. +/// Returns the total body length after the last piece was queued. +#[allow(clippy::too_many_arguments)] +async fn fetch_stream( + http: &Http, + url: &str, + range: Option<(u64, u64)>, + timeout: Duration, + idx: usize, + attempts: u32, + tx: &tokio::sync::mpsc::UnboundedSender<ProcessMsg>, + in_flight: &AtomicU64, + budget: u64, + cancel: &AtomicBool, + abandon: Option<&AtomicU32>, + host_rx: &AtomicU64, + started: Instant, + ttfb: &mut Option<Duration>, +) -> Result<u64, AsyncFetchError> { + let idle = timeout.min(STREAM_IDLE_CAP); + let mut response = + match tokio::time::timeout(timeout, send_checked(http, url, range, None)).await { + Ok(res) => res?, + Err(_) => { + return Err(AsyncFetchError { + message: "http get: response headers timed out".to_string(), + kind: FetchFailKind::Timeout, + }) + } + }; + *ttfb = Some(started.elapsed()); + let content_length = response.content_length(); + let mut offset = 0u64; + loop { + // Budget gate: don't read ahead of the pool by more than the budget. + while in_flight.load(Ordering::Relaxed) >= budget { + if cancel.load(Ordering::Relaxed) { + return Err(AsyncFetchError { + message: "cancelled".to_string(), + kind: FetchFailKind::Other, + }); + } + tokio::time::sleep(Duration::from_millis(5)).await; + } + if abandon.is_some_and(|f| f.load(Ordering::Relaxed) == attempts) { + return Err(AsyncFetchError { + message: "sink rejected a piece of this attempt".to_string(), + kind: FetchFailKind::Other, + }); + } + let piece = match tokio::time::timeout(idle, response.chunk()).await { + Ok(Ok(Some(piece))) => piece, + Ok(Ok(None)) => break, + Ok(Err(err)) => { + return Err(AsyncFetchError { + message: format!("http body: {err}"), + kind: classify_reqwest_error(&err), + }); + } + Err(_) => { + return Err(AsyncFetchError { + message: format!("http body: idle timeout at offset {offset}"), + kind: FetchFailKind::Timeout, + }); + } + }; + if piece.is_empty() { + continue; + } + host_rx.fetch_add(piece.len() as u64, Ordering::Relaxed); + let data = piece.to_vec(); + let len = data.len() as u64; + in_flight.fetch_add(len, Ordering::Relaxed); + if tx + .send(ProcessMsg::Piece { + idx, + attempts, + offset, + data, + }) + .is_err() + { + in_flight.fetch_sub(len, Ordering::Relaxed); + return Err(AsyncFetchError { + message: "process pool gone".to_string(), + kind: FetchFailKind::Other, + }); + } + offset += len; + } + validate_body_len(offset, content_length, range)?; + Ok(offset) +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// Driver plumbing +// ───────────────────────────────────────────────────────────────────────────────────────────── + +/// `abandon` sentinel: no attempt of this item has been rejected by the sink. +const NO_ATTEMPT: u32 = u32::MAX; + +enum ProcessMsg { + /// Whole-body mode: the complete raw body. + Body { + idx: usize, + attempts: u32, + server_idx: usize, + body: Vec<u8>, + }, + /// Stream mode: one in-order piece of attempt `attempts` of item `idx`. + Piece { + idx: usize, + attempts: u32, + offset: u64, + data: Vec<u8>, + }, + /// Stream mode: the body ended; the pool finalizes and reports a verdict. + Finish { + idx: usize, + attempts: u32, + server_idx: usize, + total_len: u64, + }, +} + +enum SinkVerdict { + Ok, + Retry(String), + Fatal, +} + +struct Feedback { + idx: usize, + attempts: u32, + server_idx: usize, + verdict: SinkVerdict, +} + +/// An item awaiting (re)dispatch: `attempts` already spent; not eligible before `not_before`. +#[derive(Clone, Copy, Debug)] +struct PendingItem { + idx: usize, + attempts: u32, + not_before: Instant, +} + +enum Fetched { + Body(Vec<u8>), + Streamed(u64), +} + +/// The result the driver awaits for each dispatched fetch. +struct FetchDone { + idx: usize, + attempts: u32, + reserve: u64, + server_idx: usize, + elapsed: Duration, + /// Request sent → response headers, when the headers arrived. + ttfb: Option<Duration>, + /// Key in the driver's registry of running requests. + req_id: u64, + /// The request was cancelled because its host stalled (not a failure of the item). + stalled: bool, + res: Result<Fetched, AsyncFetchError>, +} + +struct DriverCtx<'a> { + items: &'a [FetchItem], + hosts: &'a [String], + opts: &'a FetchOptions, + label: &'a str, + cancel: &'a AtomicBool, + log: &'a (dyn Fn(&str) + Sync), + meter: &'a BandwidthMeter, + in_flight: &'a AtomicU64, + error_slot: &'a Mutex<Option<String>>, + abandon: &'a [AtomicU32], + txs: Vec<tokio::sync::mpsc::UnboundedSender<ProcessMsg>>, + fb_rx: std_mpsc::Receiver<Feedback>, + budget: u64, + bootstrap: usize, + win_min: usize, + win_max: usize, + per_host_cap: usize, +} + +async fn run_driver(ctx: DriverCtx<'_>) { + let DriverCtx { + items, + hosts, + opts, + label, + cancel, + log, + meter, + in_flight, + error_slot, + abandon, + txs, + fb_rx, + budget, + bootstrap, + win_min, + win_max, + per_host_cap, + } = ctx; + let timeout = opts.timeout; + let stream = opts.stream; + + let http = match build_http(opts, per_host_cap) { + Ok(http) => http, + Err(err) => { + record_first_error(error_slot, format!("{label}: {err}")); + drop(txs); + return; + } + }; + + let mut window = AdaptiveWindow::new(bootstrap, win_min, win_max, Instant::now()); + let mut sched = FetchScheduler::new(hosts, per_host_cap); + let mut retry: VecDeque<PendingItem> = VecDeque::new(); + let mut next_item = 0usize; + let mut errors_logged: u32 = 0; + // Bodies / finishes handed to the pool whose verdict has not come back yet. + let mut outstanding = 0usize; + // Stall detection: bytes received per server (bumped by the request futures as pieces + // arrive), when that count last moved, since when the server has had requests to answer, + // how many requests it is running, and each running request's abort switch. + let host_rx: Vec<AtomicU64> = hosts.iter().map(|_| AtomicU64::new(0)).collect(); + let mut last_rx: Vec<u64> = vec![0; hosts.len()]; + let mut last_rx_at: Vec<Option<Instant>> = vec![None; hosts.len()]; + let mut watch_from: Vec<Instant> = vec![Instant::now(); hosts.len()]; + let mut running: Vec<usize> = vec![0; hosts.len()]; + let mut stall_count: Vec<u32> = vec![0; hosts.len()]; + let mut aborts: HashMap<u64, (usize, Arc<Notify>)> = HashMap::new(); + let mut next_req_id: u64 = 0; + let mut last_stall_check = Instant::now(); + // Declared AFTER `txs`/`http` so it is dropped BEFORE them (its futures borrow both). + let mut inflight: FuturesUnordered<_> = FuturesUnordered::new(); + + 'outer: loop { + if cancel.load(Ordering::Relaxed) + || error_slot.lock().expect("err slot poisoned").is_some() + { + break; + } + + // ── Sink verdicts: Retry = counts as a failed attempt (cool + demote host, back-off, + // rotate); Fatal was already recorded in the error slot by the pool. ── + while let Ok(fb) = fb_rx.try_recv() { + outstanding = outstanding.saturating_sub(1); + match fb.verdict { + SinkVerdict::Ok => {} + SinkVerdict::Fatal => break 'outer, + SinkVerdict::Retry(message) => { + let now = Instant::now(); + sched.on_error(fb.server_idx, now, FetchFailKind::Other); + if window.record_err(now, FetchFailKind::Other) { + log(&window.summary_line(label, inflight.len(), now)); + } + if fb.attempts < MAX_ITEM_ATTEMPTS { + retry.push_back(PendingItem { + idx: fb.idx, + attempts: fb.attempts, + not_before: now + Duration::from_millis(retry_backoff_millis(fb.attempts)), + }); + } else { + record_first_error( + error_slot, + format!( + "{label}: item {} failed after {MAX_ITEM_ATTEMPTS} attempts: {message}", + items[fb.idx].id + ), + ); + break 'outer; + } + } + } + } + + let probe_now = Instant::now(); + if window.maybe_probe(probe_now, meter.total_bytes()) { + log(&window.summary_line(label, inflight.len(), probe_now)); + } + + // ── Stalled hosts: requests that receive nothing while another host delivers. ── + if probe_now.duration_since(last_stall_check) >= Duration::from_millis(STALL_CHECK_MS) { + last_stall_check = probe_now; + for s in 0..hosts.len() { + let rx = host_rx[s].load(Ordering::Relaxed); + if rx != last_rx[s] { + last_rx[s] = rx; + last_rx_at[s] = Some(probe_now); + } + } + let stall = Duration::from_millis(HOST_STALL_MS); + for s in 0..hosts.len() { + let silent_since = silent_since(last_rx_at[s], watch_from[s]); + if running[s] == 0 || probe_now.duration_since(silent_since) < stall { + continue; + } + let others_deliver = (0..hosts.len()).any(|o| { + o != s && last_rx_at[o].is_some_and(|at| probe_now.duration_since(at) < stall) + }); + if !others_deliver { + continue; + } + let mut requeued = 0usize; + for (srv, notify) in aborts.values() { + if *srv == s { + notify.notify_one(); + requeued += 1; + } + } + let rest = HOST_STALL_COOLDOWN_MS + .saturating_mul(1u64 << stall_count[s].min(8)) + .min(HOST_STALL_COOLDOWN_MAX_MS); + stall_count[s] = stall_count[s].saturating_add(1); + sched.rest(s, probe_now, Duration::from_millis(rest)); + window.note_stalled_host(); + log(&format!( + "host-stall label={label} host={} requeued={requeued} idle_ms={} cooldown_ms={rest}", + hosts[s], + probe_now.duration_since(silent_since).as_millis() + )); + // Do not fire again for the same silence while the cancelled requests wind down. + watch_from[s] = probe_now; + } + } + + // ── Dispatch up to the current window ── + while inflight.len() < window.current { + if cancel.load(Ordering::Relaxed) + || error_slot.lock().expect("err slot poisoned").is_some() + { + break 'outer; + } + let now = Instant::now(); + + // A due retry first, else the next fresh item. + let use_retry = retry.front().is_some_and(|p| p.not_before <= now); + let (idx, attempts) = if use_retry { + let p = *retry.front().expect("retry nonempty"); + (p.idx, p.attempts) + } else if next_item < items.len() { + (next_item, 0u32) + } else { + break; // nothing dispatchable now (retry not due, or all dispatched) + }; + let item = &items[idx]; + + // Byte-budget gate (hard memory bound): reserve at DISPATCH. Always admits when nothing + // is in flight, so an item bigger than the whole budget can't deadlock. Streams reserve + // nothing here — their pieces are budgeted as they arrive. + let reserve = if stream { 0 } else { reserve_bytes(item) }; + if !budget_admits(in_flight.load(Ordering::Relaxed), reserve, budget) { + window.note_budget_stall(); + break; // wait for a completion to free budget + } + + // Speed-ranked, per-host-capped host pick. + let Some(server_idx) = sched.pick(now) else { + window.note_host_stall(); + break; // every host at cap or cooling → wait for a completion + }; + let permit = match sched.host_sem(server_idx).clone().try_acquire_owned() { + Ok(permit) => permit, + Err(_) => { + window.note_host_stall(); + break; + } + }; + + let Some(url) = url_for_host(item, server_idx) else { + record_first_error(error_slot, format!("{label}: item {} has no url", item.id)); + break 'outer; + }; + let url = url.to_string(); + let range = item.range; + + // Commit: pop the work item, reserve budget, launch the fetch future. + if use_retry { + retry.pop_front(); + } else { + next_item += 1; + } + in_flight.fetch_add(reserve, Ordering::Relaxed); + let http = &http; + let tx = &txs[if stream { idx % txs.len() } else { 0 }]; + let abandon_flag = abandon.get(idx); + let rx_counter = &host_rx[server_idx]; + let started = Instant::now(); + if running[server_idx] == 0 { + // The host had nothing to answer until now: its silence is measured from here. + watch_from[server_idx] = started; + } + running[server_idx] += 1; + let req_id = next_req_id; + next_req_id += 1; + let abort = Arc::new(Notify::new()); + aborts.insert(req_id, (server_idx, Arc::clone(&abort))); + inflight.push(async move { + let mut ttfb: Option<Duration> = None; + let (res, stalled) = { + let fetch = async { + if stream { + fetch_stream( + http, + &url, + range, + timeout, + idx, + attempts + 1, + tx, + in_flight, + budget, + cancel, + abandon_flag, + rx_counter, + started, + &mut ttfb, + ) + .await + .map(Fetched::Streamed) + } else { + fetch_once(http, &url, range, timeout, rx_counter, started, &mut ttfb) + .await + .map(Fetched::Body) + } + }; + // The stall detector's switch: dropping the fetch future closes its connection. + match select(pin!(fetch), pin!(abort.notified())).await { + Either::Left((res, _)) => (res, false), + Either::Right(((), _)) => ( + Err(AsyncFetchError { + message: "host stalled; request re-queued".to_string(), + kind: FetchFailKind::Other, + }), + true, + ), + } + }; + drop(permit); // RAII per-host permit release (also on future drop / cancel-abort) + FetchDone { + idx, + attempts: attempts + 1, + reserve, + server_idx, + elapsed: started.elapsed(), + ttfb, + req_id, + stalled, + res, + } + }); + } + + // ── Nothing in flight? finished, or waiting on retry-not-due / host cooldown / the pool. ── + if inflight.is_empty() { + if next_item >= items.len() && retry.is_empty() && outstanding == 0 { + break; // every item fetched and processed + } + tokio::time::sleep(Duration::from_millis(5)).await; + continue; + } + + // ── Await one completion (drives ALL in-flight requests cooperatively), bounded so + // cancel / a pool Fatal / a Retry verdict are noticed promptly even mid-fetch. ── + let done = match tokio::time::timeout(Duration::from_millis(DRIVER_POLL_MS), inflight.next()) + .await + { + Ok(Some(done)) => done, + Ok(None) => continue, + Err(_elapsed) => continue, + }; + let now = Instant::now(); + aborts.remove(&done.req_id); + running[done.server_idx] = running[done.server_idx].saturating_sub(1); + if done.stalled { + // Not the item's fault: straight back into the queue for a healthy host, no window + // or host penalty beyond the rest the detector already gave the host. It still counts + // as an attempt, which keeps each stream attempt's pieces distinguishable. + in_flight.fetch_sub(done.reserve, Ordering::Relaxed); + retry.push_front(PendingItem { + idx: done.idx, + attempts: done.attempts, + not_before: now, + }); + continue; + } + if let Some(ttfb) = done.ttfb { + window.record_ttfb(ttfb.as_secs_f64() * 1000.0); + } + if done.res.is_ok() { + stall_count[done.server_idx] = 0; + } + match done.res { + Ok(Fetched::Body(body)) => { + let raw_len = body.len() as u64; + meter.record(done.server_idx, raw_len); + sched.on_success(done.server_idx, raw_len, done.elapsed); + window.record_ok(done.elapsed.as_secs_f64() * 1000.0); + // Reconcile the dispatch reservation to the actual raw size; the pool subtracts the + // actual size on process-complete, netting this item's budget to zero. + if raw_len >= done.reserve { + in_flight.fetch_add(raw_len - done.reserve, Ordering::Relaxed); + } else { + in_flight.fetch_sub(done.reserve - raw_len, Ordering::Relaxed); + } + outstanding += 1; + if txs[0] + .send(ProcessMsg::Body { + idx: done.idx, + attempts: done.attempts, + server_idx: done.server_idx, + body, + }) + .is_err() + { + break; // process side gone + } + } + Ok(Fetched::Streamed(total_len)) => { + meter.record(done.server_idx, total_len); + sched.on_success(done.server_idx, total_len, done.elapsed); + window.record_ok(done.elapsed.as_secs_f64() * 1000.0); + outstanding += 1; + if txs[done.idx % txs.len()] + .send(ProcessMsg::Finish { + idx: done.idx, + attempts: done.attempts, + server_idx: done.server_idx, + total_len, + }) + .is_err() + { + break; // process side gone + } + } + Err(err) => { + in_flight.fetch_sub(done.reserve, Ordering::Relaxed); + let client_rejection = is_client_rejection(&err.message); + if errors_logged < FETCH_ERROR_LOG_LIMIT { + errors_logged += 1; + let host = hosts.get(done.server_idx).map(String::as_str).unwrap_or("?"); + log(&format!( + "fetch-error label={label} item={} host={host} attempt={} kind={:?} \ +window_shrink={} msg={}", + items[done.idx].id, + done.attempts, + err.kind, + !client_rejection, + err.message + )); + if errors_logged == FETCH_ERROR_LOG_LIMIT { + log(&format!( + "fetch-error label={label} further errors not logged individually" + )); + } + } + sched.on_error(done.server_idx, now, err.kind); + if !client_rejection && window.record_err(now, err.kind) { + log(&window.summary_line(label, inflight.len(), now)); + } + if done.attempts < MAX_ITEM_ATTEMPTS { + let backoff = retry_backoff_millis(done.attempts); + retry.push_back(PendingItem { + idx: done.idx, + attempts: done.attempts, + not_before: now + Duration::from_millis(backoff), + }); + } else { + record_first_error( + error_slot, + format!( + "{label}: item {} failed after {MAX_ITEM_ATTEMPTS} attempts: {}", + items[done.idx].id, err.message + ), + ); + break; + } + } + } + } + + // Dropping `inflight` aborts any still-running requests (the cancel / error / final-failure + // teardown path); dropping the senders then closes the channels so the pool drains and exits. + drop(inflight); + drop(txs); +} + +/// Since when a server has been silent: its last byte, or when it was last given work to do if +/// that came later. +fn silent_since(last_rx_at: Option<Instant>, watch_from: Instant) -> Instant { + match last_rx_at { + Some(at) if at > watch_from => at, + _ => watch_from, + } +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// Bandwidth meter (per-host bytes + 5-s samples for peak) +// ───────────────────────────────────────────────────────────────────────────────────────────── + +pub struct BandwidthMeter { + start: Instant, + total_bytes: AtomicU64, + per_server_bytes: Vec<AtomicU64>, + hosts: Vec<String>, + /// (bytes, instant) at the last sample; peak = max sample rate in bytes/s. + last_sample: Mutex<(u64, Instant)>, + peak_bps: Mutex<f64>, +} + +impl BandwidthMeter { + pub fn new(hosts: &[String]) -> Self { + let now = Instant::now(); + Self { + start: now, + total_bytes: AtomicU64::new(0), + per_server_bytes: hosts.iter().map(|_| AtomicU64::new(0)).collect(), + hosts: hosts.to_vec(), + last_sample: Mutex::new((0, now)), + peak_bps: Mutex::new(0.0), + } + } + + #[inline] + pub fn record(&self, server_idx: usize, bytes: u64) { + self.total_bytes.fetch_add(bytes, Ordering::Relaxed); + if let Some(counter) = self.per_server_bytes.get(server_idx) { + counter.fetch_add(bytes, Ordering::Relaxed); + } + } + + #[inline] + pub fn total_bytes(&self) -> u64 { + self.total_bytes.load(Ordering::Relaxed) + } + + /// Take a throughput sample since the previous one (≥ 1 s apart to be counted) and fold it + /// into the peak. + pub fn sample(&self) { + let now = Instant::now(); + let total = self.total_bytes(); + let mut last = self.last_sample.lock().expect("meter poisoned"); + let dt = now.duration_since(last.1).as_secs_f64(); + if dt >= 1.0 { + let bps = total.saturating_sub(last.0) as f64 / dt; + let mut peak = self.peak_bps.lock().expect("meter poisoned"); + if bps > *peak { + *peak = bps; + } + *last = (total, now); + } + } + + pub fn peak_bps(&self) -> f64 { + *self.peak_bps.lock().expect("meter poisoned") + } + + pub fn summary_line(&self, label: &str) -> String { + let secs = self.start.elapsed().as_secs_f64().max(0.001); + let total = self.total_bytes(); + let mb = |b: u64| (b as f64) / (1024.0 * 1024.0); + let overall_mbps = mb(total) / secs; + let mut per: Vec<(usize, u64)> = self + .per_server_bytes + .iter() + .enumerate() + .map(|(i, c)| (i, c.load(Ordering::Relaxed))) + .filter(|(_, b)| *b > 0) + .collect(); + per.sort_by(|a, b| b.1.cmp(&a.1)); + let used = per.len(); + let pool = self.per_server_bytes.len(); + let mut servers = String::new(); + for (i, bytes) in per.iter().take(6) { + let host = self.hosts.get(*i).map(String::as_str).unwrap_or("?"); + servers.push_str(&format!( + " [{host} {:.1}MB/s {:.0}MB]", + mb(*bytes) / secs, + mb(*bytes) + )); + } + format!( + "throughput label={label} overall={overall_mbps:.2}MB/s total={:.0}MB elapsed={secs:.0}s used={used}/{pool} servers:{servers}", + mb(total) + ) + } +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// Scheduler + adaptive window — a copy of the Steam depot writer's (keyed on host strings). +// ───────────────────────────────────────────────────────────────────────────────────────────── + +#[derive(Clone, Debug, Default)] +struct ServerHealth { + ewma_bps: f64, + samples: u32, + consecutive_errors: u32, + cooldown_until: Option<Instant>, +} + +/// Speed-ranked, per-host-capped host picker. Cold hosts are probed first, then selection is +/// epsilon-greedy over the measured EWMA so a demoted-but-recovered host returns. Per-host caps key +/// on the host STRING (one `tokio::sync::Semaphore` each) so two entries that share a host still +/// share one cap. +struct FetchScheduler { + health: Vec<ServerHealth>, + server_host: Vec<usize>, + host_sems: Vec<Arc<Semaphore>>, + dispatch_counter: u64, +} + +impl FetchScheduler { + fn new(hosts: &[String], per_host_cap: usize) -> Self { + let mut host_keys: Vec<&str> = Vec::new(); + let mut server_host: Vec<usize> = Vec::with_capacity(hosts.len()); + for h in hosts { + let idx = host_keys + .iter() + .position(|k| *k == h.as_str()) + .unwrap_or_else(|| { + host_keys.push(h.as_str()); + host_keys.len() - 1 + }); + server_host.push(idx); + } + let host_sems = host_keys + .iter() + .map(|_| Arc::new(Semaphore::new(per_host_cap.max(1)))) + .collect(); + Self { + health: hosts.iter().map(|_| ServerHealth::default()).collect(), + server_host, + host_sems, + dispatch_counter: 0, + } + } + + fn host_sem(&self, server_idx: usize) -> &Arc<Semaphore> { + &self.host_sems[self.server_host[server_idx]] + } + + fn eligible(&self, server_idx: usize, now: Instant) -> bool { + self.host_sem(server_idx).available_permits() > 0 + && self.health[server_idx] + .cooldown_until + .map_or(true, |t| t <= now) + } + + fn pick(&mut self, now: Instant) -> Option<usize> { + let eligible: Vec<usize> = (0..self.health.len()) + .filter(|&i| self.eligible(i, now)) + .collect(); + if eligible.is_empty() { + return None; + } + if let Some(&cold) = eligible.iter().find(|&&i| self.health[i].samples == 0) { + return Some(cold); + } + self.dispatch_counter = self.dispatch_counter.wrapping_add(1); + if SERVER_EXPLORE_EVERY > 0 && self.dispatch_counter % SERVER_EXPLORE_EVERY == 0 { + return eligible + .iter() + .copied() + .min_by_key(|&i| self.health[i].samples); + } + eligible.iter().copied().max_by(|&a, &b| { + self.health[a] + .ewma_bps + .partial_cmp(&self.health[b].ewma_bps) + .unwrap_or(std::cmp::Ordering::Equal) + .then( + self.host_sem(a) + .available_permits() + .cmp(&self.host_sem(b).available_permits()), + ) + .then(b.cmp(&a)) + }) + } + + fn on_success(&mut self, server_idx: usize, bytes: u64, elapsed: Duration) { + let secs = elapsed.as_secs_f64().max(0.001); + let bps = bytes as f64 / secs; + let h = &mut self.health[server_idx]; + h.ewma_bps = if h.samples == 0 { + bps + } else { + 0.7 * h.ewma_bps + 0.3 * bps + }; + h.samples = h.samples.saturating_add(1); + h.consecutive_errors = 0; + h.cooldown_until = None; + } + + /// A stalled host: out of the rotation for `rest`, and its speed estimate cut so it comes + /// back behind the hosts that kept delivering. + fn rest(&mut self, server_idx: usize, now: Instant, rest: Duration) { + let h = &mut self.health[server_idx]; + h.cooldown_until = Some(now + rest); + h.ewma_bps *= 0.25; + } + + fn on_error(&mut self, server_idx: usize, now: Instant, kind: FetchFailKind) { + let h = &mut self.health[server_idx]; + h.consecutive_errors = h.consecutive_errors.saturating_add(1); + let mut backoff = retry_backoff_millis(h.consecutive_errors); + if kind == FetchFailKind::RateLimited { + backoff = backoff.max(RATE_LIMIT_COOLDOWN_MS); + } + h.cooldown_until = Some(now + Duration::from_millis(backoff)); + h.ewma_bps *= 0.5; + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum WindowReason { + Start, + GrowSlowStart, + GrowThroughputUp, + GrowProbe, + HoldPlateau, + HoldCooldown, + HoldCeiling, + HoldErrors, + HoldThroughputDown, + HoldLatency, + ShrinkLatency, + ShrinkRateLimited, + ShrinkTimeout, + ShrinkReset, + ShrinkServerFault, + ShrinkErrorRate, + ShrinkErrorBurst, +} + +impl WindowReason { + fn code(self) -> &'static str { + match self { + WindowReason::Start => "start", + WindowReason::GrowSlowStart => "grow:slow-start", + WindowReason::GrowThroughputUp => "grow:throughput-up", + WindowReason::GrowProbe => "grow:probe", + WindowReason::HoldPlateau => "hold:plateau", + WindowReason::HoldCooldown => "hold:cooldown", + WindowReason::HoldCeiling => "hold:ceiling", + WindowReason::HoldErrors => "hold:errors", + WindowReason::HoldThroughputDown => "hold:throughput-down", + WindowReason::HoldLatency => "hold:latency", + WindowReason::ShrinkLatency => "shrink:latency", + WindowReason::ShrinkRateLimited => "shrink:429", + WindowReason::ShrinkTimeout => "shrink:timeout", + WindowReason::ShrinkReset => "shrink:reset", + WindowReason::ShrinkServerFault => "shrink:5xx", + WindowReason::ShrinkErrorRate => "shrink:err-rate", + WindowReason::ShrinkErrorBurst => "shrink:err-burst", + } + } + + fn from_fail_kind(kind: FetchFailKind) -> Self { + match kind { + FetchFailKind::RateLimited => WindowReason::ShrinkRateLimited, + FetchFailKind::Timeout => WindowReason::ShrinkTimeout, + FetchFailKind::Connect => WindowReason::ShrinkReset, + FetchFailKind::ServerFault => WindowReason::ShrinkServerFault, + FetchFailKind::Other => WindowReason::ShrinkErrorBurst, + } + } +} + +/// Adaptive in-flight window: slow-start doubles every healthy probe until the first real back-off, +/// then grows additively; holds on plateau; shrinks proportionally only on real errors. +#[derive(Clone, Debug)] +struct AdaptiveWindow { + current: usize, + min: usize, + max: usize, + last_probe: Instant, + last_bytes: u64, + last_bps: f64, + bps_ewma: f64, + best_bps: f64, + slow_start: bool, + plateau_streak: u32, + plateau_since: Option<Instant>, + plateau_rearm_ms: u64, + ok_count: u32, + err_count: u32, + budget_stalls: u32, + host_stalls: u32, + last_budget_stalls: u32, + last_host_stalls: u32, + latency_ewma_ms: f64, + /// TTFB samples of the current probe interval. + ttfb_sum_ms: f64, + ttfb_samples: u32, + /// Lowest per-probe mean TTFB seen this run (0 = none yet): the no-queue baseline. + ttfb_base_ms: f64, + /// Mean TTFB of the last probe that had enough samples. + ttfb_last_ms: f64, + /// Consecutive probes with queueing delay. + latency_streak: u32, + stalled_hosts: u32, + cooldown_until: Option<Instant>, + last_reason: WindowReason, + last_err_rate: f64, + probes_since_log: u32, +} + +impl AdaptiveWindow { + fn new(bootstrap: usize, min: usize, max: usize, now: Instant) -> Self { + let max = max.max(1); + let min = min.clamp(1, max); + Self { + current: bootstrap.clamp(min, max), + min, + max, + last_probe: now, + last_bytes: 0, + last_bps: 0.0, + bps_ewma: 0.0, + best_bps: 0.0, + slow_start: true, + plateau_streak: 0, + plateau_since: None, + plateau_rearm_ms: WINDOW_PLATEAU_REARM_MS, + ok_count: 0, + err_count: 0, + budget_stalls: 0, + host_stalls: 0, + last_budget_stalls: 0, + last_host_stalls: 0, + latency_ewma_ms: 0.0, + ttfb_sum_ms: 0.0, + ttfb_samples: 0, + ttfb_base_ms: 0.0, + ttfb_last_ms: 0.0, + latency_streak: 0, + stalled_hosts: 0, + cooldown_until: None, + last_reason: WindowReason::Start, + last_err_rate: 0.0, + probes_since_log: 0, + } + } + + fn note_budget_stall(&mut self) { + self.budget_stalls = self.budget_stalls.saturating_add(1); + } + + fn note_host_stall(&mut self) { + self.host_stalls = self.host_stalls.saturating_add(1); + } + + fn record_ttfb(&mut self, ttfb_ms: f64) { + self.ttfb_sum_ms += ttfb_ms; + self.ttfb_samples = self.ttfb_samples.saturating_add(1); + } + + fn note_stalled_host(&mut self) { + self.stalled_hosts = self.stalled_hosts.saturating_add(1); + } + + /// The latency brake's shrink: proportional and gentle, never below the floor, at least −1. + fn shrink_latency(&mut self, now: Instant) { + let scaled = (self.current as f64 * LATENCY_SHRINK_FACTOR).floor() as usize; + self.current = scaled.min(self.current.saturating_sub(1)).max(self.min); + self.cooldown_until = Some(now + Duration::from_millis(WINDOW_COOLDOWN_MS)); + self.slow_start = false; + self.plateau_streak = 0; + self.plateau_since = None; + self.last_reason = WindowReason::ShrinkLatency; + } + + fn record_ok(&mut self, latency_ms: f64) { + self.ok_count = self.ok_count.saturating_add(1); + self.latency_ewma_ms = if self.latency_ewma_ms == 0.0 { + latency_ms + } else { + 0.8 * self.latency_ewma_ms + 0.2 * latency_ms + }; + } + + fn shrink(&mut self, now: Instant, reason: WindowReason) { + let scaled = (self.current as f64 * WINDOW_SHRINK_FACTOR).floor() as usize; + let next = scaled.min(self.current.saturating_sub(1)).max(self.min); + self.current = next.max(self.min); + self.cooldown_until = Some(now + Duration::from_millis(WINDOW_COOLDOWN_MS)); + self.slow_start = false; + self.best_bps = self.bps_ewma; + self.plateau_streak = 0; + self.plateau_since = None; + self.plateau_rearm_ms = WINDOW_PLATEAU_REARM_MS; + self.last_reason = reason; + } + + fn grow(&mut self, reason: WindowReason, multiplicative: bool) { + let next = if multiplicative { + self.current.saturating_mul(WINDOW_SLOW_START_FACTOR) + } else { + self.current + WINDOW_STEP_UP + }; + self.current = next.min(self.max).max(self.min); + self.last_reason = reason; + } + + fn record_err(&mut self, now: Instant, kind: FetchFailKind) -> bool { + self.err_count = self.err_count.saturating_add(1); + let immediate = kind == FetchFailKind::RateLimited + || self.err_count >= WINDOW_ERR_BURST_IMMEDIATE; + if !immediate { + return false; + } + let before = self.current; + self.shrink(now, WindowReason::from_fail_kind(kind)); + if before != self.current { + self.probes_since_log = 0; + true + } else { + false + } + } + + fn maybe_probe(&mut self, now: Instant, total_bytes: u64) -> bool { + let dt = now.duration_since(self.last_probe); + if dt < Duration::from_millis(WINDOW_PROBE_INTERVAL_MS) { + return false; + } + let secs = dt.as_secs_f64().max(0.001); + let sample_bps = total_bytes.saturating_sub(self.last_bytes) as f64 / secs; + self.bps_ewma = if self.bps_ewma == 0.0 { + sample_bps + } else { + WINDOW_BPS_EWMA_ALPHA * sample_bps + (1.0 - WINDOW_BPS_EWMA_ALPHA) * self.bps_ewma + }; + let events = self.ok_count + self.err_count; + let err_rate = if events == 0 { + 0.0 + } else { + self.err_count as f64 / events as f64 + }; + let before = self.current; + let cooling = self.cooldown_until.is_some_and(|t| now < t); + + // Queueing delay: this probe's mean TTFB against the run's baseline. + let probe_ttfb = (self.ttfb_samples >= LATENCY_MIN_SAMPLES) + .then(|| self.ttfb_sum_ms / self.ttfb_samples as f64); + let mut queueing = false; + let mut rising = false; + if let Some(ttfb) = probe_ttfb { + if self.ttfb_base_ms == 0.0 || ttfb < self.ttfb_base_ms { + self.ttfb_base_ms = ttfb; + } + queueing = ttfb >= self.ttfb_base_ms * LATENCY_HOLD_FACTOR + && ttfb - self.ttfb_base_ms >= LATENCY_MIN_EXCESS_MS; + rising = self.ttfb_last_ms > 0.0 && ttfb >= self.ttfb_last_ms; + self.ttfb_last_ms = ttfb; + } + if queueing { + self.latency_streak = self.latency_streak.saturating_add(1); + } else if probe_ttfb.is_some() { + self.latency_streak = 0; + } + + if err_rate > WINDOW_ERR_RATE_HIGH { + self.shrink(now, WindowReason::ShrinkErrorRate); + } else if cooling { + self.last_reason = WindowReason::HoldCooldown; + } else if queueing { + // More requests only made each one wait longer. Hold while a real gain still comes + // with the delay; once the delay keeps rising without one, give some of the window + // back, gently, and let the cooldown show whether that helped. + let gain = self.bps_ewma > self.best_bps * (1.0 + LATENCY_GAIN_EPS); + if gain { + self.best_bps = self.bps_ewma; + } + if !gain && rising && self.latency_streak >= 2 && self.current > self.min { + self.shrink_latency(now); + } else { + self.last_reason = WindowReason::HoldLatency; + } + } else if self.current >= self.max { + self.last_reason = WindowReason::HoldCeiling; + } else if err_rate > WINDOW_ERR_RATE_LOW { + self.last_reason = WindowReason::HoldErrors; + } else { + let improving = self.bps_ewma > self.best_bps * (1.0 + WINDOW_IMPROVE_EPS); + let falling = self.bps_ewma < self.best_bps * (1.0 - WINDOW_DECLINE_EPS); + if improving { + self.best_bps = self.bps_ewma; + self.plateau_streak = 0; + self.plateau_since = None; + self.plateau_rearm_ms = WINDOW_PLATEAU_REARM_MS; + let doubling = self.slow_start; + let reason = if doubling { + WindowReason::GrowSlowStart + } else { + WindowReason::GrowThroughputUp + }; + self.grow(reason, doubling); + } else if falling { + self.last_reason = WindowReason::HoldThroughputDown; + } else if self.plateau_streak < WINDOW_PLATEAU_PATIENCE { + self.plateau_streak += 1; + self.grow(WindowReason::GrowProbe, false); + } else { + let since = *self.plateau_since.get_or_insert(now); + if now.duration_since(since) >= Duration::from_millis(self.plateau_rearm_ms) { + self.plateau_streak = 0; + self.plateau_since = None; + self.plateau_rearm_ms = + (self.plateau_rearm_ms * 2).min(WINDOW_PLATEAU_REARM_MAX_MS); + } + self.last_reason = WindowReason::HoldPlateau; + } + } + + self.last_bytes = total_bytes; + self.last_bps = sample_bps; + self.last_probe = now; + self.last_err_rate = err_rate; + self.ok_count = 0; + self.err_count = 0; + self.last_budget_stalls = self.budget_stalls; + self.last_host_stalls = self.host_stalls; + self.budget_stalls = 0; + self.host_stalls = 0; + self.ttfb_sum_ms = 0.0; + self.ttfb_samples = 0; + self.probes_since_log = self.probes_since_log.saturating_add(1); + let changed = before != self.current; + if changed || self.probes_since_log >= WINDOW_LOG_EVERY_PROBES { + self.probes_since_log = 0; + true + } else { + false + } + } + + fn cooldown_left_ms(&self, now: Instant) -> u64 { + self.cooldown_until + .map(|t| t.saturating_duration_since(now).as_millis() as u64) + .unwrap_or(0) + } + + fn summary_line(&self, label: &str, in_flight_requests: usize, now: Instant) -> String { + let mbps = |bps: f64| bps / (1024.0 * 1024.0); + format!( + "fetch-window label={label} window={} (min={} max={}) in_flight={in_flight_requests} \ +last={:.2}MB/s ewma={:.2}MB/s best={:.2}MB/s reason={} cooldown={}ms err_rate={:.1}% \ +phase={} rtt={:.0}ms budget_stalls={} host_stalls={} ttfb={:.0}ms ttfb_base={:.0}ms \ +stalled_hosts={}", + self.current, + self.min, + self.max, + mbps(self.last_bps), + mbps(self.bps_ewma), + mbps(self.best_bps), + self.last_reason.code(), + self.cooldown_left_ms(now), + self.last_err_rate * 100.0, + if self.slow_start { "slow-start" } else { "steady" }, + self.latency_ewma_ms, + self.last_budget_stalls, + self.last_host_stalls, + self.ttfb_last_ms, + self.ttfb_base_ms, + self.stalled_hosts + ) + } +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// Tests +// ───────────────────────────────────────────────────────────────────────────────────────────── + +#[cfg(test)] +mod tests { + #[test] + fn client_rejection_is_4xx_but_not_429() { + assert_eq!(rejected_status("unexpected HTTP status (403)"), Some(403)); + assert!(is_client_rejection("unexpected HTTP status (403)")); + assert!(is_client_rejection("unexpected HTTP status (404)")); + assert!(!is_client_rejection("unexpected HTTP status (429)")); + assert!(!is_client_rejection("unexpected HTTP status (503)")); + assert!(!is_client_rejection("http get: connection reset")); + assert_eq!(rejected_status("http body: timeout"), None); + } + + use super::*; + use crate::fetch_tuning::{ + FETCH_INFLIGHT_BUDGET_FLOOR_BYTES, FETCH_INFLIGHT_BUDGET_HARD_CAP_BYTES, PER_HOST_CAP, + }; + use std::collections::HashMap; + use std::io::{Read, Write}; + use std::net::TcpListener; + use std::sync::atomic::AtomicUsize; + + fn item(id: u64, urls: &[&str], range: Option<(u64, u64)>) -> FetchItem { + FetchItem { + id, + urls: urls.iter().map(|s| s.to_string()).collect(), + reserve: 0, + range, + } + } + + #[test] + fn window_bounds_clamp_to_hosts_and_hard_cap() { + assert_eq!(window_bounds(64, 7, PER_HOST_CAP), (8, 2, 42)); + assert_eq!(window_bounds(8, 1, PER_HOST_CAP), (6, 2, 6)); + assert_eq!(window_bounds(1, 1, PER_HOST_CAP), (1, 1, 1)); + assert_eq!(window_bounds(10_000, 100, 6), (8, 2, WINDOW_HARD_CAP)); + assert_eq!(window_bounds(0, 0, 0), (1, 1, 1)); + } + + #[test] + fn budget_scales_with_ceiling_between_floor_and_cap() { + assert_eq!(inflight_budget_bytes(1), FETCH_INFLIGHT_BUDGET_FLOOR_BYTES); + assert_eq!(inflight_budget_bytes(16), FETCH_INFLIGHT_BUDGET_FLOOR_BYTES); + assert_eq!(inflight_budget_bytes(42), 63 * 1024 * 1024); + assert_eq!(inflight_budget_bytes(256), FETCH_INFLIGHT_BUDGET_HARD_CAP_BYTES); + assert!(budget_admits(0, u64::MAX, 1)); + assert!(!budget_admits(1, 100, 100)); + } + + #[test] + fn reserve_defaults_to_nominal() { + let mut i = item(1, &["u"], None); + assert_eq!(reserve_bytes(&i), NOMINAL_CHUNK_RESERVE_BYTES); + i.reserve = 77; + assert_eq!(reserve_bytes(&i), 77); + } + + #[test] + fn url_per_host_selection() { + let none = item(1, &[], None); + assert_eq!(url_for_host(&none, 0), None); + let one = item(1, &["a"], None); + assert_eq!(url_for_host(&one, 0), Some("a")); + assert_eq!(url_for_host(&one, 5), Some("a")); + let many = item(1, &["a", "b", "c"], None); + assert_eq!(url_for_host(&many, 1), Some("b")); + assert_eq!(url_for_host(&many, 2), Some("c")); + assert_eq!(url_for_host(&many, 9), Some("a")); + } + + #[test] + fn distinct_hosts_dedupe_by_string() { + let h = vec!["a".to_string(), "b".to_string(), "a".to_string()]; + assert_eq!(distinct_host_count(&h), 2); + assert_eq!(distinct_host_count(&[]), 1); + } + + #[test] + fn range_validation() { + assert_eq!(range_len((0, 0)), Some(1)); + assert_eq!(range_len((10, 19)), Some(10)); + assert_eq!(range_len((5, 4)), None); + assert!(validate_body_len(10, None, Some((10, 19))).is_ok()); + assert!(validate_body_len(10, Some(10), Some((10, 19))).is_ok()); + let e = validate_body_len(9, None, Some((10, 19))).unwrap_err(); + assert_eq!(e.kind, FetchFailKind::ServerFault); + assert_eq!( + validate_body_len(1, None, Some((5, 4))).unwrap_err().kind, + FetchFailKind::Other + ); + assert!(validate_body_len(100, None, None).is_ok()); + assert!(validate_body_len(100, Some(100), None).is_ok()); + assert!(validate_body_len(99, Some(100), None).is_err()); + assert!(status_accepted(200, false)); + assert!(status_accepted(200, true)); + assert!(status_accepted(206, true)); + assert!(!status_accepted(206, false)); + assert!(!status_accepted(404, true)); + assert_eq!(classify_status(429), FetchFailKind::RateLimited); + assert_eq!(classify_status(503), FetchFailKind::ServerFault); + assert_eq!(classify_status(404), FetchFailKind::Other); + } + + #[test] + fn scheduler_probes_cold_hosts_then_ranks() { + let hosts = vec!["a".to_string(), "b".to_string()]; + let mut s = FetchScheduler::new(&hosts, 2); + let now = Instant::now(); + assert_eq!(s.pick(now), Some(0)); + s.on_success(0, 1_000_000, Duration::from_millis(100)); + assert_eq!(s.pick(now), Some(1)); + s.on_success(1, 1_000_000, Duration::from_millis(500)); + assert_eq!(s.pick(now), Some(0)); + s.on_error(0, now, FetchFailKind::Timeout); + assert_eq!(s.pick(now), Some(1)); + s.on_error(1, now, FetchFailKind::RateLimited); + assert_eq!(s.pick(now), None); + assert_eq!(s.pick(now + Duration::from_secs(6)), Some(0)); + } + + #[test] + fn window_shrinks_only_on_error_bursts() { + let now = Instant::now(); + let mut w = AdaptiveWindow::new(8, 2, 42, now); + assert!(!w.record_err(now, FetchFailKind::Timeout)); + assert!(!w.record_err(now, FetchFailKind::Timeout)); + assert!(w.record_err(now, FetchFailKind::Timeout)); + assert_eq!(w.current, 6); + assert!(!w.slow_start); + let line = w.summary_line("t", 3, now); + assert!(line.starts_with("fetch-window label=t window=6 (min=2 max=42) in_flight=3 ")); + assert!(line.contains("reason=shrink:timeout")); + assert!(line.contains("phase=steady")); + } + + const MIB: u64 = 1024 * 1024; + + /// Drive the window through probes 2.1 s apart, each with `samples` TTFB values and the + /// bytes that arrived in it; returns (reason, window) after every probe. + fn run_probes(w: &mut AdaptiveWindow, t0: Instant, probes: &[(f64, u64)]) -> Vec<(WindowReason, usize)> { + let mut total = 0u64; + let mut out = Vec::new(); + for (k, &(ttfb_ms, bytes)) in probes.iter().enumerate() { + for _ in 0..5 { + w.record_ttfb(ttfb_ms); + } + total += bytes; + w.maybe_probe(t0 + Duration::from_millis(2_100 * (k as u64 + 1)), total); + out.push((w.last_reason, w.current)); + } + out + } + + #[test] + fn rising_delay_without_more_throughput_holds_then_shrinks_gently() { + let t0 = Instant::now(); + let mut w = AdaptiveWindow::new(8, 2, 256, t0); + // Flat ~15 MB/s while the time to first byte climbs from 0.4 s to 12 s (the 5G log). + let flat = 32 * MIB; + let probes: Vec<(f64, u64)> = [400.0, 400.0, 450.0, 900.0, 1500.0, 2500.0, 4000.0, 6000.0, 9000.0, 12000.0] + .iter() + .map(|&ttfb| (ttfb, flat)) + .collect(); + let seen = run_probes(&mut w, t0, &probes); + let first_hold = seen.iter().position(|(r, _)| *r == WindowReason::HoldLatency).expect("hold:latency"); + let first_shrink = seen.iter().position(|(r, _)| *r == WindowReason::ShrinkLatency).expect("shrink:latency"); + assert!(first_hold < first_shrink, "{seen:?}"); + let before = seen[first_shrink - 1].1; + assert_eq!(seen[first_shrink].1, (before as f64 * LATENCY_SHRINK_FACTOR).floor() as usize, "{seen:?}"); + // From the first sign of queueing on, the window never grows again. + for pair in seen[first_hold..].windows(2) { + assert!(pair[1].1 <= pair[0].1, "grew under queueing delay: {seen:?}"); + } + assert!(seen.last().unwrap().1 < before, "{seen:?}"); + let line = w.summary_line("t", 0, t0); + assert!(line.contains(" ttfb_base=400ms"), "{line}"); + } + + #[test] + fn a_fast_link_keeps_growing_while_its_delay_stays_low() { + let t0 = Instant::now(); + let mut w = AdaptiveWindow::new(8, 2, 256, t0); + // Throughput doubles with the window; TTFB wobbles between 25 and 70 ms (new connections). + let probes: Vec<(f64, u64)> = (0..6) + .map(|k| ([25.0, 70.0, 40.0, 60.0, 30.0, 65.0][k], (8 * MIB) << k)) + .collect(); + let seen = run_probes(&mut w, t0, &probes); + assert!( + seen.iter().all(|(r, _)| *r != WindowReason::HoldLatency && *r != WindowReason::ShrinkLatency), + "{seen:?}" + ); + assert!(w.current >= 128, "{seen:?}"); + } + + /// Two-behaviour stub for the stall test: `freeze` answers the headers and 100 bytes, then + /// never sends another byte; otherwise every path gets 10 000 bytes in ten paced slices. + fn start_paced_stub(freeze: bool) -> String { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind"); + let port = listener.local_addr().expect("addr").port(); + thread::spawn(move || { + for stream in listener.incoming() { + let Ok(mut stream) = stream else { break }; + thread::spawn(move || { + let mut buf = Vec::new(); + let mut tmp = [0u8; 1024]; + while !buf.windows(4).any(|w| w == b"\r\n\r\n") { + match stream.read(&mut tmp) { + Ok(0) | Err(_) => return, + Ok(n) => buf.extend_from_slice(&tmp[..n]), + } + } + let body = vec![7u8; 10_000]; + let head = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ); + if stream.write_all(head.as_bytes()).is_err() { + return; + } + if freeze { + let _ = stream.write_all(&body[..100]); + let _ = stream.flush(); + thread::sleep(Duration::from_secs(30)); + return; + } + for slice in body.chunks(1_000) { + if stream.write_all(slice).is_err() { + return; + } + let _ = stream.flush(); + thread::sleep(Duration::from_millis(100)); + } + }); + } + }); + format!("http://127.0.0.1:{port}") + } + + #[test] + fn a_frozen_host_is_detected_and_its_items_move_to_the_healthy_one() { + let frozen = start_paced_stub(true); + let healthy = start_paced_stub(false); + let hosts = vec![frozen.clone(), healthy.clone()]; + let items: Vec<FetchItem> = (0..12u64) + .map(|i| { + let a = format!("{frozen}/item{i}"); + let b = format!("{healthy}/item{i}"); + item(i, &[a.as_str(), b.as_str()], None) + }) + .collect(); + let sink = CollectSink { + got: Mutex::new(HashMap::new()), + retry_once_id: None, + retried: AtomicUsize::new(0), + }; + let cancel = AtomicBool::new(false); + let lines: Mutex<Vec<String>> = Mutex::new(Vec::new()); + let started = Instant::now(); + let outcome = run_fetch( + items, + &hosts, + &opts("stall"), + &sink, + &cancel, + &|_: u64, _: u64| {}, + &|line: &str| lines.lock().unwrap().push(line.to_string()), + ); + let elapsed = started.elapsed(); + let lines = lines.lock().unwrap(); + assert!(outcome.ok(), "outcome={outcome:?} lines={lines:?}"); + assert_eq!(outcome.items_ok, 12); + let got = sink.got.lock().unwrap(); + assert!(got.values().all(|b| b.len() == 10_000)); + let stall = lines + .iter() + .find(|l| l.starts_with("host-stall label=stall ")) + .unwrap_or_else(|| panic!("no host-stall line: {lines:?}")); + assert!(stall.contains(&format!(" host={frozen} ")), "{stall}"); + assert!(!stall.contains(" requeued=0 "), "{stall}"); + // Well inside the 10 s request timeout that would otherwise have freed the items. + assert!(elapsed < Duration::from_secs(8), "took {elapsed:?}: {lines:?}"); + assert!(!lines.iter().any(|l| l.contains(&format!("host={healthy}")) && l.starts_with("host-stall"))); + } + + // ── Minimal HTTP/1.1 stub: one thread per connection, `Connection: close`, Range-aware. ── + struct Stub { + base: String, + hits: Arc<Mutex<HashMap<String, usize>>>, + } + + fn start_stub(bodies: HashMap<String, Vec<u8>>, flaky_first: &[&str]) -> Stub { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind"); + let port = listener.local_addr().expect("addr").port(); + let hits: Arc<Mutex<HashMap<String, usize>>> = Arc::new(Mutex::new(HashMap::new())); + let flaky: Vec<String> = flaky_first.iter().map(|s| s.to_string()).collect(); + let bodies = Arc::new(bodies); + let hits2 = Arc::clone(&hits); + thread::spawn(move || { + for stream in listener.incoming() { + let Ok(mut stream) = stream else { break }; + let bodies = Arc::clone(&bodies); + let hits = Arc::clone(&hits2); + let flaky = flaky.clone(); + thread::spawn(move || { + let mut buf = Vec::new(); + let mut tmp = [0u8; 1024]; + loop { + let n = match stream.read(&mut tmp) { + Ok(0) | Err(_) => return, + Ok(n) => n, + }; + buf.extend_from_slice(&tmp[..n]); + if buf.windows(4).any(|w| w == b"\r\n\r\n") { + break; + } + } + let text = String::from_utf8_lossy(&buf).to_string(); + let mut lines = text.lines(); + let request = lines.next().unwrap_or(""); + let path = request.split(' ').nth(1).unwrap_or("/").to_string(); + let mut range: Option<(u64, u64)> = None; + let mut auth = String::new(); + let mut ua = String::new(); + for line in lines { + let lower = line.to_ascii_lowercase(); + if let Some(v) = lower.strip_prefix("range: bytes=") { + let mut it = v.trim().split('-'); + let a = it.next().and_then(|s| s.parse().ok()); + let b = it.next().and_then(|s| s.parse().ok()); + if let (Some(a), Some(b)) = (a, b) { + range = Some((a, b)); + } + } + if let Some(v) = line.strip_prefix("Authorization: ") { + auth = v.trim().to_string(); + } else if let Some(v) = line.strip_prefix("authorization: ") { + auth = v.trim().to_string(); + } else if let Some(v) = line.strip_prefix("User-Agent: ") { + ua = v.trim().to_string(); + } else if let Some(v) = line.strip_prefix("user-agent: ") { + ua = v.trim().to_string(); + } + } + let count = { + let mut h = hits.lock().unwrap(); + let c = h.entry(path.clone()).or_insert(0); + *c += 1; + *c + }; + let response: Vec<u8> = if path == "/auth" || path == "/ua" { + let body = if path == "/auth" { auth } else { ua }.into_bytes(); + let mut r = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + body.len() + ) + .into_bytes(); + r.extend_from_slice(&body); + r + } else if flaky.contains(&path) && count == 1 { + b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec() + } else if let Some(full) = bodies.get(&path) { + match range { + Some((a, b)) if (b as usize) < full.len() && a <= b => { + let slice = &full[a as usize..=b as usize]; + let mut r = format!( + "HTTP/1.1 206 Partial Content\r\nContent-Length: {}\r\nContent-Range: bytes {a}-{b}/{}\r\nConnection: close\r\n\r\n", + slice.len(), + full.len() + ) + .into_bytes(); + r.extend_from_slice(slice); + r + } + _ => { + let mut r = format!( + "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + full.len() + ) + .into_bytes(); + r.extend_from_slice(full); + r + } + } + } else { + b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".to_vec() + }; + let _ = stream.write_all(&response); + let _ = stream.flush(); + }); + } + }); + Stub { + base: format!("http://127.0.0.1:{port}"), + hits, + } + } + + struct CollectSink { + got: Mutex<HashMap<u64, Vec<u8>>>, + retry_once_id: Option<u64>, + retried: AtomicUsize, + } + + impl FetchSink for CollectSink { + fn process(&self, item: &FetchItem, body: Vec<u8>) -> Result<u64, SinkError> { + if self.retry_once_id == Some(item.id) && self.retried.fetch_add(1, Ordering::SeqCst) == 0 { + return Err(SinkError::Retry("simulated hash mismatch".into())); + } + let n = body.len() as u64; + self.got.lock().unwrap().insert(item.id, body); + Ok(n * 2) + } + } + + fn opts(label: &str) -> FetchOptions { + FetchOptions { + max_workers: 8, + per_host_cap: 4, + timeout: Duration::from_secs(10), + headers: vec![("Authorization".to_string(), "Bearer test-token".to_string())], + ca_bundle_path: String::new(), + process_workers: 2, + label: label.to_string(), + stream: false, + } + } + + /// Stream-mode sink: appends pieces per item (offset 0 restarts), finalizes by length check. + struct StreamSink { + got: Mutex<HashMap<u64, Vec<u8>>>, + finished: Mutex<HashMap<u64, u64>>, + /// Reject the first piece of this item once (exercises the mid-stream Retry path). + retry_once_id: Option<u64>, + retried: AtomicUsize, + } + + impl FetchSink for StreamSink { + fn process(&self, _item: &FetchItem, _body: Vec<u8>) -> Result<u64, SinkError> { + Err(SinkError::Fatal("whole-body mode not expected".into())) + } + + fn on_chunk(&self, item: &FetchItem, offset: u64, data: &[u8]) -> Result<(), SinkError> { + let mut got = self.got.lock().unwrap(); + let buf = got.entry(item.id).or_default(); + if offset == 0 { + buf.clear(); + if self.retry_once_id == Some(item.id) + && self.retried.fetch_add(1, Ordering::SeqCst) == 0 + { + return Err(SinkError::Retry("simulated bad first piece".into())); + } + } + assert_eq!(buf.len() as u64, offset, "pieces must arrive in order"); + buf.extend_from_slice(data); + Ok(()) + } + + fn on_finish(&self, item: &FetchItem, total_len: u64) -> Result<u64, SinkError> { + let got = self.got.lock().unwrap(); + let len = got.get(&item.id).map(|b| b.len() as u64).unwrap_or(0); + if len != total_len { + return Err(SinkError::Retry(format!("length {len} != {total_len}"))); + } + self.finished.lock().unwrap().insert(item.id, total_len); + Ok(7) + } + } + + #[test] + fn end_to_end_stream_mode_against_local_stub() { + let mut bodies = HashMap::new(); + bodies.insert("/big".to_string(), (0..=255u8).cycle().take(300_000).collect::<Vec<u8>>()); + bodies.insert("/small".to_string(), b"tiny".to_vec()); + bodies.insert("/flaky".to_string(), vec![9u8; 20_000]); + let stub = start_stub(bodies, &["/flaky"]); + let hosts = vec!["127.0.0.1".to_string()]; + let items = vec![ + item(1, &[format!("{}/big", stub.base).as_str()], None), + item(2, &[format!("{}/small", stub.base).as_str()], None), + item(3, &[format!("{}/flaky", stub.base).as_str()], None), + item(4, &[format!("{}/big", stub.base).as_str()], Some((100, 1099))), + item(5, &[format!("{}/ua", stub.base).as_str()], None), + ]; + let sink = StreamSink { + got: Mutex::new(HashMap::new()), + finished: Mutex::new(HashMap::new()), + retry_once_id: Some(1), + retried: AtomicUsize::new(0), + }; + let mut options = opts("stream app=stub"); + options.stream = true; + options.per_host_cap = 8; + options.headers.push(("User-Agent".to_string(), "nile/0.1 Amazon".to_string())); + let cancel = AtomicBool::new(false); + let lines: Mutex<Vec<String>> = Mutex::new(Vec::new()); + let outcome = run_fetch( + items, + &hosts, + &options, + &sink, + &cancel, + &|_: u64, _: u64| {}, + &|line: &str| lines.lock().unwrap().push(line.to_string()), + ); + let lines = lines.lock().unwrap(); + assert!(outcome.ok(), "outcome={outcome:?} lines={lines:?}"); + assert_eq!(outcome.items_ok, 5); + let got = sink.got.lock().unwrap(); + assert_eq!(got[&1].len(), 300_000); + assert_eq!(got[&1][1000], (1000 % 256) as u8); + assert_eq!(got[&2], b"tiny".to_vec()); + assert_eq!(got[&3], vec![9u8; 20_000]); + assert_eq!(got[&4].len(), 1000); + assert_eq!(got[&4][0], 100u8); + assert_eq!(got[&5], b"nile/0.1 Amazon".to_vec()); + let finished = sink.finished.lock().unwrap(); + assert_eq!(finished.len(), 5); + // Pieces credited as written + 7 extra per on_finish; the rejected attempt of /big + // contributed nothing (its first piece was refused before any write). + let written: u64 = got.values().map(|b| b.len() as u64).sum(); + assert_eq!(outcome.bytes_credited, written + 5 * 7); + let hits = stub.hits.lock().unwrap(); + assert_eq!(hits["/flaky"], 2); + assert!(hits["/big"] >= 3, "big={} (2 items + 1 retry)", hits["/big"]); + assert!(lines[0].contains(" mode=stream"), "{}", lines[0]); + assert!(lines[0].contains(" ceiling=8 "), "{}", lines[0]); + assert!(lines.last().unwrap().ends_with("result=ok")); + } + + #[test] + fn end_to_end_against_local_stub() { + let mut bodies = HashMap::new(); + bodies.insert("/a".to_string(), vec![1u8; 5000]); + bodies.insert("/b".to_string(), (0..=255u8).cycle().take(70_000).collect::<Vec<u8>>()); + bodies.insert("/c".to_string(), b"hello world".to_vec()); + let stub = start_stub(bodies, &["/c"]); + let hosts = vec!["127.0.0.1".to_string()]; + let items = vec![ + item(1, &[format!("{}/a", stub.base).as_str()], None), + item(2, &[format!("{}/b", stub.base).as_str()], Some((1000, 1999))), + item(3, &[format!("{}/c", stub.base).as_str()], None), + item(4, &[format!("{}/auth", stub.base).as_str()], None), + ]; + let sink = CollectSink { + got: Mutex::new(HashMap::new()), + retry_once_id: Some(1), + retried: AtomicUsize::new(0), + }; + let cancel = AtomicBool::new(false); + let lines: Mutex<Vec<String>> = Mutex::new(Vec::new()); + let last_progress: Mutex<(u64, u64)> = Mutex::new((0, 0)); + let outcome = run_fetch( + items, + &hosts, + &opts("test app=stub"), + &sink, + &cancel, + &|bytes: u64, ok: u64| { + // Pool threads race on the callback order; keep the high-water mark. + let mut cur = last_progress.lock().unwrap(); + if bytes >= cur.0 { + *cur = (bytes, ok); + } + }, + &|line: &str| lines.lock().unwrap().push(line.to_string()), + ); + let lines = lines.lock().unwrap(); + assert!(outcome.ok(), "outcome={outcome:?} lines={lines:?}"); + assert_eq!(outcome.items_ok, 4); + let got = sink.got.lock().unwrap(); + assert_eq!(got[&1], vec![1u8; 5000]); + assert_eq!(got[&2].len(), 1000); + assert_eq!(got[&2][0], (1000 % 256) as u8); + assert_eq!(got[&3], b"hello world".to_vec()); + assert_eq!(got[&4], b"Bearer test-token".to_vec()); + let expected_credit: u64 = got.values().map(|b| b.len() as u64 * 2).sum(); + assert_eq!(outcome.bytes_credited, expected_credit); + assert_eq!(*last_progress.lock().unwrap(), (expected_credit, 4)); + // /c was served 503 once, then 200; /a was re-fetched once after the sink's Retry. + let hits = stub.hits.lock().unwrap(); + assert_eq!(hits["/c"], 2); + assert_eq!(hits["/a"], 2); + assert!(lines[0].starts_with("fetch-start label=test app=stub items=4 ")); + assert!(lines.last().unwrap().starts_with("fetch-end label=test app=stub items_ok=4 ")); + assert!(lines.last().unwrap().ends_with("result=ok")); + } + + #[test] + fn end_to_end_gives_up_after_max_attempts() { + let stub = start_stub(HashMap::new(), &[]); + let hosts = vec!["127.0.0.1".to_string()]; + let items = vec![item(9, &[format!("{}/missing", stub.base).as_str()], None)]; + let sink = CollectSink { + got: Mutex::new(HashMap::new()), + retry_once_id: None, + retried: AtomicUsize::new(0), + }; + let cancel = AtomicBool::new(false); + let outcome = run_fetch( + items, + &hosts, + &opts("t"), + &sink, + &cancel, + &|_: u64, _: u64| {}, + &|_: &str| {}, + ); + assert!(!outcome.ok()); + assert_eq!(outcome.items_ok, 0); + let err = outcome.error.unwrap(); + assert!(err.starts_with("t: item 9 failed after 5 attempts: "), "{err}"); + assert_eq!(stub.hits.lock().unwrap()["/missing"], 5); + } + + #[test] + fn cancel_before_start_returns_cancelled() { + let hosts = vec!["h".to_string()]; + let items = vec![item(1, &["http://127.0.0.1:9/x"], None)]; + let sink = CollectSink { + got: Mutex::new(HashMap::new()), + retry_once_id: None, + retried: AtomicUsize::new(0), + }; + let cancel = AtomicBool::new(true); + let outcome = run_fetch( + items, + &hosts, + &opts("t"), + &sink, + &cancel, + &|_: u64, _: u64| {}, + &|_: &str| {}, + ); + assert!(outcome.cancelled); + assert_eq!(outcome.items_ok, 0); + } +} diff --git a/app/src/main/rust/stores/src/fetch_tuning.rs b/app/src/main/rust/stores/src/fetch_tuning.rs new file mode 100644 index 0000000000..abbe2d4925 --- /dev/null +++ b/app/src/main/rust/stores/src/fetch_tuning.rs @@ -0,0 +1,211 @@ +//! The numbers and the two small types [`fetch_core`](crate::fetch_core) shares with the Steam +//! depot writer it was carved out of. +//! +//! In Bannerlator these live in `depot_writer.rs` and `cdn_client.rs`, next to the Steam CM and +//! depot code, so that the Steam engine and the store engines run on ONE set of tunables and their +//! `fetch-window` log lines stay A/B-comparable. DroidDeck ships no Steam depot engine (the Steam +//! client runs natively in the Linux runtime), so only this slice came across: the values are +//! copied verbatim and keep their names, which is what lets a GOG/Epic/Amazon log from either app +//! be read against the same tuning notes. +//! +//! The important behaviours are structural (slow-start ramp -> hold at plateau -> back off on REAL +//! errors, per-host cap, hard byte budget), not the exact numbers. The r3/r4 notes below record +//! why each number is what it is, because every one of them was arrived at by a device run that +//! went wrong the other way. + +use std::time::Duration; + +/// Default `User-Agent` of the pooled client. Every store engine overrides it per request with +/// the agent its own launcher sends (`FetchOptions::headers`), so this value is only what a +/// request without an explicit header would carry. +pub const USER_AGENT: &str = "Valve/Steam HTTP Client 1.0"; + +/// TCP connect deadline for every request the pooled client makes. +pub fn connect_timeout() -> Duration { + Duration::from_secs(15) +} + +/// Classified fetch failure so the fetch driver can back off proportionally (429/5xx/timeout/reset +/// shrink the window + cool the host harder than a one-off protocol error). +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum FetchFailKind { + Timeout, + RateLimited, + ServerFault, + Connect, + Other, +} + +#[derive(Clone, Debug)] +pub struct AsyncFetchError { + pub message: String, + pub kind: FetchFailKind, +} + +/// Attempts per item before the run gives up on it (fetch failures and sink retries both count). +pub const MAX_CHUNK_ATTEMPTS: u32 = 5; + +/// Floor for the in-flight byte budget — the pre-r4 fixed value, so no run ever gets *less* +/// buffered memory than it had before the budget started scaling. See [`inflight_budget_bytes`]. +pub const FETCH_INFLIGHT_BUDGET_FLOOR_BYTES: u64 = 24 * 1024 * 1024; + +/// Hard ceiling for the in-flight byte budget. This is THE OOM guard: whatever the tier, the host +/// count or the chunk size, the RAW bytes in memory for one run can never exceed this. 96 MiB = 4× +/// the old fixed budget and still a small fraction of the heap an Android game installer runs in, +/// and it is only ever reached by a fast-tier run whose CDN pool is large enough to support a 64+ +/// window. Raising this is the one change in the fetch layer that can reintroduce OOM, so it stays +/// fixed and small. +pub const FETCH_INFLIGHT_BUDGET_HARD_CAP_BYTES: u64 = 96 * 1024 * 1024; + +/// Assumed RAW (on-the-wire, still-compressed) size of one chunk when sizing the budget. Store +/// chunks are ~1 MiB; this is the same nominal [`NOMINAL_CHUNK_RESERVE_BYTES`] uses at dispatch. +pub const TYPICAL_CHUNK_BYTES: u64 = NOMINAL_CHUNK_RESERVE_BYTES; + +/// Headroom the budget carries over "one chunk per window slot" (3/2 = 1.5×), as a rational so the +/// arithmetic stays integer. The window is a count of concurrent *requests*; a slot's bytes are held +/// from DISPATCH until its WRITE completes, so at any instant some slots hold a fetched-but-not-yet- +/// written chunk while others hold a reservation for a fetch still on the wire. 1.5× covers that +/// overlap (and the occasional above-nominal chunk) without doubling the memory bound. +pub const BUDGET_HEADROOM_NUM: u64 = 3; +pub const BUDGET_HEADROOM_DEN: u64 = 2; + +/// Byte budget for the *compressed* bytes buffered in the fetch→process channel, scaled to the +/// window ceiling this run can actually reach. +/// +/// The decouple channel is bounded by BYTES, not chunk count: a count cap makes in-flight memory +/// swing wildly with chunk size (a 4-chunk cap is ~4 MB for 1 MB chunks but ~120 MB for 30 MB +/// chunks). Budgeting bytes keeps the heap bounded regardless of game size while giving the fetch +/// pool bandwidth-delay-product headroom so the socket stays busy while process workers +/// decompress+write. +/// +/// r4: a device run proved a fixed 24 MiB budget becomes the binding constraint the moment the +/// adaptive window works — on a 1 Gbps line the window grew to 42 but the budget admitted only ~24 +/// chunks, so `in_flight` stalled at 23–28 with `budget_stalls` in the hundreds and +/// `host_stalls=0`. A budget that refuses slots the window has already decided are safe is not a +/// memory guard, it is a throttle. So the budget scales WITH the effective ceiling (tier ∧ +/// hosts×per-host-cap ∧ hard cap) and is clamped between the old fixed value and a hard cap. Low +/// tiers are unaffected; a fast link gets a budget that stops binding before the window does. +/// +/// Budget only the RAW bytes actually queued (reserved at DISPATCH, released on WRITE-COMPLETE); the +/// decoded expansion is transient inside a process worker and never accumulates. See +/// [`budget_admits`] for the single-chunk deadlock guard. +pub fn inflight_budget_bytes(effective_ceiling: usize) -> u64 { + (effective_ceiling as u64) + .saturating_mul(TYPICAL_CHUNK_BYTES) + .saturating_mul(BUDGET_HEADROOM_NUM) + .saturating_div(BUDGET_HEADROOM_DEN) + .clamp( + FETCH_INFLIGHT_BUDGET_FLOOR_BYTES, + FETCH_INFLIGHT_BUDGET_HARD_CAP_BYTES, + ) +} + +// ─── Adaptive-window tuning ──────────────────────────────────────────────────────────────────── +// r3 retune: the first device run pinned the window at the floor on a 1 Gbps link. The controller +// shrank on throughput dips and latency jitter (normal CDN noise) as well as on errors, shrank a flat +// −6 on every single error, and only grew when throughput rose >10% in a 700 ms sample. The shrinks +// dominated, so the window ratcheted to the floor and stayed there. r3 makes growth eager and makes +// ERRORS the only shrink signal. + +/// Max concurrent requests to any single CDN host. CDNs throttle/reset per host, so a big window +/// is reached by spreading across MORE hosts, never by piling onto one. +pub const PER_HOST_CAP: usize = 6; +/// Initial in-flight window. Slow-start doubles from here, so this only sets how fast the first +/// couple of probes get useful — the ceiling still comes from the tier × hosts clamp. +pub const BOOTSTRAP_WINDOW: usize = 8; +/// Hard safety ceiling on the window regardless of tier (also clamped to distinct-hosts × per-host). +pub const WINDOW_HARD_CAP: usize = 256; +/// The window never shrinks below this (keeps a little pipelining even on a rough link). +pub const WINDOW_MIN_FLOOR: usize = 2; +/// Additive growth step once slow-start has ended (after the first real back-off). +pub const WINDOW_STEP_UP: usize = 4; +/// Slow-start multiplier: until the first back-off the window DOUBLES each healthy probe, so a fast +/// link reaches a useful window in seconds instead of never. +pub const WINDOW_SLOW_START_FACTOR: usize = 2; +/// Proportional shrink on a real error signal (never a flat step: a flat −6 slammed a small window +/// straight to the floor). Always at least −1 while above the floor. +pub const WINDOW_SHRINK_FACTOR: f64 = 0.75; +/// How often the adaptive window re-evaluates. Long enough that one jittery sample cannot drive a +/// decision; decisions use the EWMA, not the raw sample. +pub const WINDOW_PROBE_INTERVAL_MS: u64 = 2_000; +/// After a back-off, hold the window (no growth) for this long — hysteresis against oscillation. +pub const WINDOW_COOLDOWN_MS: u64 = 3_000; +/// EWMA smoothing for the measured throughput samples (higher = more responsive). +pub const WINDOW_BPS_EWMA_ALPHA: f64 = 0.4; +/// Throughput above `best × (1 + this)` counts as a genuine improvement (keeps slow-start climbing). +pub const WINDOW_IMPROVE_EPS: f64 = 0.05; +/// Throughput below `best × (1 − this)` counts as falling — growth STOPS (hold). It never shrinks: +/// only real errors shrink. +pub const WINDOW_DECLINE_EPS: f64 = 0.15; +/// Error-rate thresholds over one probe window: above HIGH shrinks; growth needs at/below LOW +/// (in between = hold). +pub const WINDOW_ERR_RATE_HIGH: f64 = 0.10; +pub const WINDOW_ERR_RATE_LOW: f64 = 0.05; +/// Errors inside ONE probe interval that trigger an immediate (don't-wait-for-the-probe) shrink. A +/// single stray timeout no longer moves the window; a burst does. A 429 always shrinks immediately. +pub const WINDOW_ERR_BURST_IMMEDIATE: u32 = 3; +/// Consecutive non-improving probes still allowed to grow before declaring a plateau — throughput +/// lags a window change by a probe or two, so one flat sample must not stop the ramp. +pub const WINDOW_PLATEAU_PATIENCE: u32 = 2; +/// Once plateaued, re-arm the ramp this long later (doubling, capped) to re-probe for headroom if the +/// link improved. Bounded re-probing, not a creep: a weak link answers with errors and shrinks back. +pub const WINDOW_PLATEAU_REARM_MS: u64 = 30_000; +pub const WINDOW_PLATEAU_REARM_MAX_MS: u64 = 300_000; +/// Even when the window does not change, emit one `fetch-window` line every N probes so a stuck +/// window explains itself in the log (`reason=` says why it is not moving). +pub const WINDOW_LOG_EVERY_PROBES: u32 = 5; +/// Every Nth exploit dispatch is instead an exploration pick (epsilon-greedy ≈ 1/N) so a +/// demoted-but-recovered host is retried rather than starved forever. +pub const SERVER_EXPLORE_EVERY: u64 = 8; +/// Reservation for an item whose compressed size the manifest didn't carry (keeps the byte budget +/// honest at dispatch time). +pub const NOMINAL_CHUNK_RESERVE_BYTES: u64 = 1024 * 1024; +/// Extra host cooldown floor when a host answers 429 (rate-limited) — back off harder than a +/// one-off timeout. +pub const RATE_LIMIT_COOLDOWN_MS: u64 = 5_000; + +/// Exponential back-off between attempts of one item: 0, 300, 600, 1200, 2400 ms … capped at 4 s. +pub fn retry_backoff_millis(attempt: u32) -> u64 { + if attempt == 0 { + return 0; + } + (300u64 << (attempt - 1)).min(4000) +} + +/// Byte-budget admission test for the fetch→process channel. Always admits when nothing is in +/// flight (`in_flight == 0`) so a single item LARGER than the whole budget can never deadlock — it +/// simply waits until the channel drains, then goes through alone. +#[inline] +pub fn budget_admits(in_flight: u64, raw_len: u64, budget: u64) -> bool { + in_flight == 0 || in_flight.saturating_add(raw_len) <= budget +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn backoff_is_exponential_and_capped() { + assert_eq!(retry_backoff_millis(0), 0); + assert_eq!(retry_backoff_millis(1), 300); + assert_eq!(retry_backoff_millis(2), 600); + assert_eq!(retry_backoff_millis(4), 2400); + assert_eq!(retry_backoff_millis(5), 4000); + assert_eq!(retry_backoff_millis(12), 4000); + } + + #[test] + fn budget_scales_between_floor_and_cap() { + assert_eq!(inflight_budget_bytes(1), FETCH_INFLIGHT_BUDGET_FLOOR_BYTES); + assert_eq!(inflight_budget_bytes(8), FETCH_INFLIGHT_BUDGET_FLOOR_BYTES); + assert_eq!(inflight_budget_bytes(32), 48 * 1024 * 1024); + assert_eq!(inflight_budget_bytes(1024), FETCH_INFLIGHT_BUDGET_HARD_CAP_BYTES); + } + + #[test] + fn single_oversized_item_is_always_admitted() { + assert!(budget_admits(0, u64::MAX, 1)); + assert!(budget_admits(10, 5, 15)); + assert!(!budget_admits(10, 6, 15)); + } +} diff --git a/app/src/main/rust/stores/src/gog.rs b/app/src/main/rust/stores/src/gog.rs new file mode 100644 index 0000000000..465d7346fa --- /dev/null +++ b/app/src/main/rust/stores/src/gog.rs @@ -0,0 +1,121 @@ +//! GOG gen2 chunk download engine — the Rust replacement for the byte-fetching inner loop of +//! `GogDownloadManager.java` (`assembleDepotFile` / `fetchChunkVerified` driven from the +//! per-file thread pool). Everything around that loop — token/builds/manifest head, base-product +//! + language depot selection, secure-link resolution, disk guard, `_gog_manifest.json` marker, +//! prefs, exe resolution, DLC markers, redist orchestration — stays in Java and is untouched. +//! +//! Parity contract: Bannerlator's `docs/RUST_GOG_PARITY.md`. The adapter is fed the SAME inflated depot-manifest +//! JSON strings Java already parsed, the SAME resolved CDN base (secure link) and the SAME +//! install dir, and produces the SAME on-disk layout (`<file>.bhtmp` staging, atomic rename, +//! size+MD5-verified finals) so a download can be resumed by either engine. +//! +//! Layout: +//! - [`plan`] — depot-manifest JSON → file/chunk plan (mirrors `parseDepotManifest`), CDN path +//! and chunk-URL builders, largest-first ordering. +//! - [`engine`] — resume/skip pass (mirrors `fileVerified`), `FetchItem` list, the `FetchSink` +//! (compressed size/MD5 → inflate → decompressed size/MD5 → positioned write → +//! whole-file size/MD5 → rename), summary logging. +//! - [`jni`] — `GogNative` JNI exports (`nativeStart` / `nativeCancel` / `nativeRelease`). + +pub mod engine; +pub mod jni; +pub mod plan; + +use std::fs::File; +use std::io::Read; +use std::path::Path; + +/// Lowercase hex of `bytes` (matches `GogDownloadManager.toHex`). +pub fn to_hex(bytes: &[u8]) -> String { + const DIGITS: &[u8; 16] = b"0123456789abcdef"; + let mut out = String::with_capacity(bytes.len() * 2); + for b in bytes { + out.push(DIGITS[(b >> 4) as usize] as char); + out.push(DIGITS[(b & 0x0f) as usize] as char); + } + out +} + +/// Lowercase hex MD5 of an in-memory buffer (`GogDownloadManager.md5Hex`). +pub fn md5_hex(data: &[u8]) -> String { + let mut hasher = crate::md5_small::Md5::new(); + hasher.update(data); + to_hex(&hasher.finalize()) +} + +/// Streaming lowercase hex MD5 of a file, never buffering the whole file +/// (`GogDownloadManager.md5HexFile`). `None` when the file cannot be read. +pub fn md5_hex_file(path: &Path) -> Option<String> { + let mut file = File::open(path).ok()?; + let mut hasher = crate::md5_small::Md5::new(); + let mut buf = vec![0u8; 128 * 1024]; + loop { + let n = file.read(&mut buf).ok()?; + if n == 0 { + break; + } + hasher.update(&buf[..n]); + } + Some(to_hex(&hasher.finalize())) +} + +/// zlib-inflate a chunk body (`GogDownloadManager.inflateZlib`): `None` when the body is not a +/// zlib stream (first byte != 0x78) or fails to inflate — the caller then treats the body as a +/// stored (non-compressed) chunk, exactly like Java's `if (inflated == null) inflated = raw`. +/// A truncated stream yields the partial output (Java's loop breaks on `n == 0` the same way); +/// the decompressed-size check that follows rejects it. +pub fn inflate_zlib(data: &[u8]) -> Option<Vec<u8>> { + if data.len() < 2 || data[0] != 0x78 { + return None; + } + let mut out = Vec::with_capacity(data.len().saturating_mul(3)); + let mut decoder = flate2::read::ZlibDecoder::new(data); + match decoder.read_to_end(&mut out) { + Ok(_) => Some(out), + Err(_) => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hex_is_lowercase_and_padded() { + assert_eq!(to_hex(&[0x00, 0x0a, 0xff, 0x10]), "000aff10"); + } + + #[test] + fn inflate_roundtrip_and_stored_fallback() { + use flate2::write::ZlibEncoder; + use flate2::Compression; + use std::io::Write; + let payload = b"hello gog chunk payload".repeat(64); + let mut enc = ZlibEncoder::new(Vec::new(), Compression::default()); + enc.write_all(&payload).unwrap(); + let compressed = enc.finish().unwrap(); + assert_eq!(compressed[0], 0x78); + assert_eq!(inflate_zlib(&compressed).unwrap(), payload); + // Not zlib → None (stored chunk path). + assert!(inflate_zlib(b"\x1f\x8bnot zlib").is_none()); + assert!(inflate_zlib(b"\x78").is_none()); + // Truncated zlib → partial output, like Java's `while (!inf.finished()) { if (n == 0) break; }` + // loop; the decompressed-size check downstream rejects it either way. + let mut truncated = compressed.clone(); + let mid = truncated.len() / 2; + truncated.truncate(mid); + assert!(inflate_zlib(&truncated).map(|out| out != payload).unwrap_or(true)); + // Corrupt zlib body → None (Java: DataFormatException → null → raw fallback). + let mut corrupt = compressed.clone(); + for b in corrupt.iter_mut().skip(2) { + *b = !*b; + } + assert!(inflate_zlib(&corrupt).is_none() || inflate_zlib(&corrupt).unwrap() != payload); + } + + #[test] + fn md5_hex_matches_rfc1321_vector() { + assert_eq!(md5_hex(b""), "d41d8cd98f00b204e9800998ecf8427e"); + assert_eq!(md5_hex(b"abc"), "900150983cd24fb0d6963f7d28e17f72"); + } +} diff --git a/app/src/main/rust/stores/src/gog/engine.rs b/app/src/main/rust/stores/src/gog/engine.rs new file mode 100644 index 0000000000..0233110c34 --- /dev/null +++ b/app/src/main/rust/stores/src/gog/engine.rs @@ -0,0 +1,1800 @@ +//! The GOG gen2 fetch loop on top of the shared fetch core. One call = one Java pool run +//! (`runGen2` base loop, `doInstallDlc` loop, or one `assembleDependencyInstaller`). +//! +//! What this mirrors, and where (see Bannerlator's `docs/RUST_GOG_PARITY.md`): +//! - resume/skip: [`file_verified`] = `GogDownloadManager.fileVerified` (exists, non-empty, size +//! when known, MD5 when known) — a verified file is reported as "Verified…" progress with NO +//! bytes credited, exactly like the Java task; +//! - per chunk: compressed size → compressed MD5 → inflate (stored fallback) → decompressed size → +//! decompressed MD5 (`fetchChunkVerified`); a mismatch is a retryable failure (Java: hard fail +//! ≤3 with backoff; the core: ≤5 attempts with backoff); +//! - per file: write into `<file>.bhtmp` (positioned, so chunks may land out of order), then +//! whole-file size + MD5, delete any existing final, rename (`assembleDepotFile`); +//! - failure of any file aborts the run (`anyFailed`), cancel aborts the run; in both cases every +//! unfinished `.bhtmp` is deleted (Java deletes its tmp on the failing/cancelled thread). + +use std::collections::HashSet; +use std::fs::{self, File, OpenOptions}; +use std::io::Write; +use std::os::unix::fs::FileExt; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use crate::fetch_core::{run_fetch, FetchItem, FetchOptions, FetchSink, SinkError}; + +use super::plan::{ + build_cdn_path, build_chunk_url, build_plan, host_key, parse_gen1_manifest, Gen1File, + PlannedFile, +}; +use super::{inflate_zlib, md5_hex, md5_hex_file}; + +/// Per-request timeout on chunk fetches (Java: `TIMEOUT = 30_000` connect + read). +pub const CHUNK_TIMEOUT: Duration = Duration::from_secs(30); +/// Java sends this on every content-system / CDN request (`fetchBytesEx`). +pub const USER_AGENT: &str = "GOG Galaxy"; +/// Java staging suffix (`assembleDepotFile`). +pub const TMP_SUFFIX: &str = ".bhtmp"; +/// Floor for the per-host cap (the core's default); a single-host store gets the whole ceiling. +pub const PER_HOST_CAP_FLOOR: usize = 6; + +/// `max(6, ceil(max_workers / distinct_hosts))` — one CDN host gets the whole ceiling, several +/// share it. +pub fn per_host_cap_for(max_workers: usize, distinct_hosts: usize) -> usize { + let hosts = distinct_hosts.max(1); + let workers = max_workers.max(1); + PER_HOST_CAP_FLOOR.max(workers.div_ceil(hosts)) +} + +/// Which Java loop this run replaces. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum PlanKind { + /// gen2: depot manifests → chunks → `assembleDepotFile` (base install, DLC, dependencies). + #[default] + Gen2Chunks, + /// gen1: build manifest → per-file HTTP Range GET streamed to disk (`runGen1`). + Gen1Ranges, +} + +impl PlanKind { + /// JNI mapping (`GogNative.KIND_*`): 0 = gen2 chunks, 1 = gen1 ranges, anything else → gen2. + pub fn from_i32(v: i32) -> Self { + if v == 1 { + PlanKind::Gen1Ranges + } else { + PlanKind::Gen2Chunks + } + } +} + +/// Inputs — exactly what the Java manager has in hand after manifest + secure-link resolution. +#[derive(Clone, Debug, Default)] +pub struct GogRequest { + pub kind: PlanKind, + /// gen2: inflated depot-manifest JSON strings, in the order Java fetched them (already filtered + /// by base-product / DLC-product and language in Java). gen1: the inflated build manifest. + pub depot_manifests: Vec<String>, + /// gen2: the resolved CDN bases from the secure-link answer (every `urls[]` entry, query + /// strings kept, in the order GOG lists them), or the one unauthenticated dependency store + /// base. Several bases on distinct hosts let the fetch core spread the window across CDNs + /// with its per-host cap; a base on a host already listed is dropped (the first one wins). + /// gen1: unused (file URLs live in the manifest). + pub cdn_bases: Vec<String>, + pub install_dir: String, + /// Files already completed by an earlier run of this same download (secure-link refresh + /// re-run): counted as done WITHOUT re-hashing and WITHOUT a progress event. + pub skip_paths: Vec<String>, + pub ca_bundle_path: String, + /// Java pool size: `resolveDownloadThreads` (base), 8 (DLC), 1 (dependency). + pub max_workers: usize, + /// Inflate/hash/write threads; Java did that on the same pool threads, so pass the same count. + pub process_workers: usize, + /// Base install: largest-first (LPT) ordering; DLC / dependency: manifest order. + pub sort_largest_first: bool, + /// Log prefix, e.g. `gog base=1207658930`. + pub label: String, +} + +/// Callbacks out of the engine (JNI listener or a test recorder). Must be callable from the +/// process-pool threads. +pub trait GogEvents: Sync { + /// One file reached its final state. `verified` = resume-skip (no bytes credited); otherwise a + /// freshly assembled + verified file (`file_bytes` credited). `files_done` counts both. + #[allow(clippy::too_many_arguments)] + fn on_file_done( + &self, + rel_path: &str, + file_bytes: u64, + verified: bool, + files_done: u32, + files_total: u32, + bytes_done: u64, + bytes_total: u64, + ); + fn on_log(&self, line: &str); + /// Periodic byte progress while files are in flight (~4/s, only when the value moved): + /// assembled bytes plus the bytes of files not yet finished. Same `bytes_total` as + /// `on_file_done`. Default: ignored (test recorders). + fn on_bytes(&self, _bytes_done: u64, _bytes_total: u64) {} +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct GogRunResult { + pub success: bool, + pub cancelled: bool, + /// The run died on an HTTP code Java treats as an expired secure link (401/403/404/500) — + /// the Java caller refreshes the link (cap 5) and re-runs with `skip_paths` filled in. + pub link_expiry: bool, + pub error: String, + /// Decompressed bytes of files assembled by THIS run (Java `totalBytes`). + pub bytes_written: u64, + /// Files done = verified-skipped + assembled (Java `doneCount`), excluding `skip_paths`. + pub files_done: u32, + pub files_verified: u32, + pub files_total: u32, +} + +/// `GogDownloadManager.fileVerified`: true only if the file exists, is non-empty, matches the +/// expected size when known and the expected MD5 when known. +pub fn file_verified(path: &Path, expected_size: u64, expected_md5: &str) -> bool { + let Ok(meta) = fs::metadata(path) else { + return false; + }; + if !meta.is_file() || meta.len() == 0 { + return false; + } + if expected_size > 0 && meta.len() != expected_size { + return false; + } + if !expected_md5.is_empty() { + return match md5_hex_file(path) { + Some(actual) => actual.eq_ignore_ascii_case(expected_md5), + None => false, + }; + } + true +} + +/// Finds an HTTP status in a fetch-core error string (`non-200 HTTP status (403)`, `status 403`, +/// `HTTP 403`, `http=403`, `code=403`). The link-expiry set is Java's `fetchChunkVerified` list. +pub fn http_status_in(message: &str) -> Option<u16> { + let lower = message.to_ascii_lowercase(); + for marker in ["status (", "status ", "http ", "http=", "code=", "code "] { + let mut from = 0; + while let Some(pos) = lower[from..].find(marker) { + let start = from + pos + marker.len(); + let digits: String = lower[start..].chars().take_while(|c| c.is_ascii_digit()).collect(); + if digits.len() == 3 { + if let Ok(code) = digits.parse::<u16>() { + if (100..600).contains(&code) { + return Some(code); + } + } + } + from = start; + } + } + None +} + +/// Java: `code == 401 || code == 403 || code == 404 || code == 500` → refresh the secure link. +pub fn is_link_expiry_status(code: u16) -> bool { + matches!(code, 401 | 403 | 404 | 500) +} + +struct FileState { + handle: Option<Arc<File>>, + chunks_done: usize, + /// Decompressed bytes of this file's finished chunks, credited to the sink's `partial` + /// counter until the file itself is finalized and moves into `bytes_done`. + partial: u64, + finalized: bool, + failed: bool, + tmp_path: PathBuf, + out_path: PathBuf, +} + +struct SpeedSampler { + started: Instant, + bucket_secs: u64, + bucket_bytes: u64, + peak_bps: f64, + wire_bytes: u64, +} + +impl SpeedSampler { + fn new() -> Self { + Self { + started: Instant::now(), + bucket_secs: 0, + bucket_bytes: 0, + peak_bps: 0.0, + wire_bytes: 0, + } + } + + fn record(&mut self, wire_len: u64) { + self.wire_bytes = self.wire_bytes.saturating_add(wire_len); + let now_secs = self.started.elapsed().as_secs(); + if now_secs != self.bucket_secs { + let bps = self.bucket_bytes as f64; + if bps > self.peak_bps { + self.peak_bps = bps; + } + self.bucket_secs = now_secs; + self.bucket_bytes = 0; + } + self.bucket_bytes = self.bucket_bytes.saturating_add(wire_len); + } + + fn finish(&mut self) -> (u64, f64, f64, f64) { + let bps = self.bucket_bytes as f64; + if bps > self.peak_bps { + self.peak_bps = bps; + } + let elapsed = self.started.elapsed().as_secs_f64().max(0.001); + let avg_mbps = self.wire_bytes as f64 * 8.0 / 1_000_000.0 / elapsed; + let peak_mbps = self.peak_bps * 8.0 / 1_000_000.0; + (self.wire_bytes, elapsed, avg_mbps, peak_mbps) + } +} + +/// Positioned writer for ONE chunk's decompressed byte range inside the file's `.bhtmp`: hashes +/// everything it is given (the decompressed MD5) and counts it, but only writes bytes inside the +/// chunk's expected range when the size is known — an inflate overshoot then fails the size +/// check instead of clobbering the neighbouring chunk. An I/O error is remembered so the sink can +/// tell "disk failed" (fatal) from "stream failed" (retry). +struct RangeWriter { + file: Arc<File>, + base: u64, + expected: u64, + written: u64, + md5: crate::md5_small::Md5, + io_error: Option<String>, +} + +impl RangeWriter { + fn new(file: Arc<File>, base: u64, expected: u64) -> Self { + Self { + file, + base, + expected, + written: 0, + md5: crate::md5_small::Md5::new(), + io_error: None, + } + } +} + +impl Write for RangeWriter { + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { + self.md5.update(buf); + let start = self.written; + let end = start.saturating_add(buf.len() as u64); + let cap = if self.expected > 0 { self.expected } else { u64::MAX }; + if start < cap { + let n = (end.min(cap) - start) as usize; + if let Err(err) = self.file.write_all_at(&buf[..n], self.base.saturating_add(start)) { + self.io_error = Some(err.to_string()); + return Err(err); + } + } + self.written = end; + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +enum ChunkMode { + Zlib(flate2::write::ZlibDecoder<RangeWriter>), + Stored(RangeWriter), +} + +/// Per-item streaming state for one attempt (reset whenever a piece arrives at `offset == 0`). +struct ChunkStream { + comp_md5: crate::md5_small::Md5, + comp_len: u64, + /// Decided on the first non-empty piece (`0x78` → zlib, else stored — Java's `inflateZlib`). + mode: Option<ChunkMode>, + /// The writer until the mode is decided (an empty body never decides). + writer: Option<RangeWriter>, +} + +impl ChunkStream { + fn new(file: Arc<File>, base: u64, expected: u64) -> Self { + Self { + comp_md5: crate::md5_small::Md5::new(), + comp_len: 0, + mode: None, + writer: Some(RangeWriter::new(file, base, expected)), + } + } +} + +/// The `FetchSink`. Stream mode (what `run_gen2` uses): pieces of a chunk are inflated as they +/// arrive straight into the chunk's byte range of the file, the compressed MD5 and the +/// decompressed size/MD5 accumulate on the fly, and `on_finish` runs the checks in Java's order +/// (compressed size → compressed MD5 → inflate → size → MD5). Body mode (`process`) is the same +/// pipeline on a whole buffer and stays available for tests / a body-mode fallback. +struct GogSink<'a> { + files: &'a [PlannedFile], + /// item id → (file index, chunk index) + table: Vec<(usize, usize)>, + states: Vec<Mutex<FileState>>, + /// item id → in-flight stream attempt (stream mode only) + streams: Vec<Mutex<Option<ChunkStream>>>, + files_total: u32, + bytes_total: u64, + files_done: &'a AtomicU32, + bytes_done: &'a AtomicU64, + events: &'a dyn GogEvents, + speed: Mutex<SpeedSampler>, + /// Bytes of files still in flight (finished chunks; for gen1, the current attempt's pieces). + /// `bytes_done + partial` is what the byte ticker reports between file completions. + partial: AtomicU64, +} + +impl<'a> GogSink<'a> { + fn log(&self, line: &str) { + self.events.on_log(line); + } + + /// `assembleDepotFile` head: `parent.mkdirs(); tmpFile.delete(); new FileOutputStream(tmp)` — + /// once per file, on its first chunk. Returns a shared handle for positioned writes. + fn file_handle(&self, file_idx: usize) -> Result<Arc<File>, SinkError> { + let file = &self.files[file_idx]; + let mut st = self + .states + .get(file_idx) + .and_then(|m| m.lock().ok()) + .ok_or_else(|| SinkError::Fatal("file state poisoned".to_string()))?; + if st.failed || st.finalized { + return Err(SinkError::Fatal(format!( + "chunk for a closed file: {}", + file.relative_path + ))); + } + if let Some(h) = st.handle.as_ref() { + return Ok(Arc::clone(h)); + } + if let Some(parent) = st.tmp_path.parent() { + let _ = fs::create_dir_all(parent); + } + let _ = fs::remove_file(&st.tmp_path); + let opened = OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(&st.tmp_path); + match opened { + Ok(h) => { + let h = Arc::new(h); + st.handle = Some(Arc::clone(&h)); + Ok(h) + } + Err(err) => { + st.failed = true; + let msg = format!("open tmp failed file={} err={err}", file.relative_path); + self.log(&msg); + Err(SinkError::Fatal(msg)) + } + } + } + + /// One verified chunk landed: count it; on the file's last chunk run the whole-file verify + + /// rename and fire the per-file progress event (Java: doneCount++, totalBytes += df.totalSize, + /// "Downloading: <name> <speed>"). + fn chunk_done(&self, file_idx: usize, chunk_bytes: u64) -> Result<(), SinkError> { + let file = &self.files[file_idx]; + let mut st = self + .states + .get(file_idx) + .and_then(|m| m.lock().ok()) + .ok_or_else(|| SinkError::Fatal("file state poisoned".to_string()))?; + if st.failed || st.finalized { + return Err(SinkError::Fatal(format!( + "chunk for a closed file: {}", + file.relative_path + ))); + } + st.chunks_done += 1; + st.partial += chunk_bytes; + self.partial.fetch_add(chunk_bytes, Ordering::Relaxed); + if st.chunks_done < file.chunks.len() { + return Ok(()); + } + if let Err(err) = self.finalize_file(file_idx, &mut st) { + st.failed = true; + return Err(SinkError::Fatal(err)); + } + let partial = std::mem::take(&mut st.partial); + drop(st); + let done = self.files_done.fetch_add(1, Ordering::Relaxed) + 1; + let bytes = self + .bytes_done + .fetch_add(file.total_size, Ordering::Relaxed) + + file.total_size; + self.partial.fetch_sub(partial, Ordering::Relaxed); + self.events.on_file_done( + &file.relative_path, + file.total_size, + false, + done, + self.files_total, + bytes, + self.bytes_total, + ); + Ok(()) + } + + /// `assembleDepotFile` tail: whole-file size + MD5 on the tmp, delete existing final, rename. + fn finalize_file(&self, file_idx: usize, st: &mut FileState) -> Result<(), String> { + let file = &self.files[file_idx]; + if let Some(handle) = st.handle.take() { + drop(handle); + } + let actual = fs::metadata(&st.tmp_path).map(|m| m.len()).unwrap_or(0); + if file.total_size > 0 && actual != file.total_size { + self.log(&format!( + "FILE size mismatch file={} exp={} got={}", + file.relative_path, file.total_size, actual + )); + let _ = fs::remove_file(&st.tmp_path); + return Err(format!("file size mismatch: {}", file.relative_path)); + } + if !file.md5.is_empty() { + let ok = match md5_hex_file(&st.tmp_path) { + Some(h) => h.eq_ignore_ascii_case(&file.md5), + None => false, + }; + if !ok { + self.log(&format!("FILE md5 mismatch file={}", file.relative_path)); + let _ = fs::remove_file(&st.tmp_path); + return Err(format!("file md5 mismatch: {}", file.relative_path)); + } + } + if st.out_path.exists() { + let _ = fs::remove_file(&st.out_path); + } + if let Err(err) = fs::rename(&st.tmp_path, &st.out_path) { + self.log(&format!("FILE rename failed file={} err={err}", file.relative_path)); + let _ = fs::remove_file(&st.tmp_path); + return Err(format!("file rename failed: {}", file.relative_path)); + } + st.finalized = true; + Ok(()) + } + + /// Delete every unfinished `.bhtmp` (Java: `tmpFile.delete()` on the failing/cancelled task). + fn cleanup_partials(&self) { + for st in &self.states { + let Ok(mut st) = st.lock() else { + continue; + }; + if st.finalized { + continue; + } + if let Some(handle) = st.handle.take() { + drop(handle); + } + if st.chunks_done > 0 || st.tmp_path.exists() { + let _ = fs::remove_file(&st.tmp_path); + } + } + } + + fn unfinalized_pending(&self) -> usize { + self.states + .iter() + .filter(|st| st.lock().map(|s| !s.finalized).unwrap_or(true)) + .count() + } +} + +impl<'a> FetchSink for GogSink<'a> { + /// Body mode: `fetchChunkVerified` on a whole buffer, then a positioned write of the inflated + /// bytes (used by tests and available as a non-stream fallback). + fn process(&self, item: &FetchItem, body: Vec<u8>) -> Result<u64, SinkError> { + let Some(&(file_idx, chunk_idx)) = self.table.get(item.id as usize) else { + return Err(SinkError::Fatal(format!("unknown item id {}", item.id))); + }; + let file = &self.files[file_idx]; + let chunk = &file.chunks[chunk_idx]; + if let Ok(mut speed) = self.speed.lock() { + speed.record(body.len() as u64); + } + + // fetchChunkVerified, in order: compressed size → compressed MD5 → inflate → size → MD5. + if chunk.compressed_size > 0 && body.len() as u64 != chunk.compressed_size { + let msg = format!( + "chunk compressed-size mismatch chunk={} exp={} got={}", + chunk.hash, + chunk.compressed_size, + body.len() + ); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + if !chunk.compressed_md5.is_empty() + && !md5_hex(&body).eq_ignore_ascii_case(&chunk.compressed_md5) + { + let msg = format!("chunk compressed-md5 mismatch chunk={}", chunk.hash); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + let inflated = match inflate_zlib(&body) { + Some(out) => out, + None => body, // stored (non-zlib) chunk + }; + if chunk.size > 0 && inflated.len() as u64 != chunk.size { + let msg = format!( + "chunk decompressed-size mismatch chunk={} exp={} got={}", + chunk.hash, + chunk.size, + inflated.len() + ); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + if !chunk.md5.is_empty() && !md5_hex(&inflated).eq_ignore_ascii_case(&chunk.md5) { + let msg = format!("chunk decompressed-md5 mismatch chunk={}", chunk.hash); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + + let handle = self.file_handle(file_idx)?; + if let Err(err) = handle.write_all_at(&inflated, chunk.offset) { + if let Some(mut st) = self.states.get(file_idx).and_then(|m| m.lock().ok()) { + st.failed = true; + let _ = st.handle.take(); + let _ = fs::remove_file(&st.tmp_path); + } + let msg = format!("write failed file={} err={err}", file.relative_path); + self.log(&msg); + return Err(SinkError::Fatal(msg)); + } + drop(handle); + let credited = inflated.len() as u64; + self.chunk_done(file_idx, credited)?; + Ok(credited) + } + + /// Stream mode: hash the compressed piece, inflate it straight into the chunk's byte range + /// (or pass it through for a stored chunk), hashing + counting the decompressed output. + fn on_chunk(&self, item: &FetchItem, offset: u64, data: &[u8]) -> Result<(), SinkError> { + let id = item.id as usize; + let Some(&(file_idx, chunk_idx)) = self.table.get(id) else { + return Err(SinkError::Fatal(format!("unknown item id {}", item.id))); + }; + let file = &self.files[file_idx]; + let chunk = &file.chunks[chunk_idx]; + if let Ok(mut speed) = self.speed.lock() { + speed.record(data.len() as u64); + } + let mut slot = self + .streams + .get(id) + .and_then(|m| m.lock().ok()) + .ok_or_else(|| SinkError::Fatal("chunk stream poisoned".to_string()))?; + if offset == 0 || slot.is_none() { + // (Re)start of an attempt: fresh hashers + inflater; positioned writes re-cover the range. + let handle = self.file_handle(file_idx)?; + *slot = Some(ChunkStream::new(handle, chunk.offset, chunk.size)); + } + let Some(stream) = slot.as_mut() else { + return Err(SinkError::Fatal("chunk stream missing".to_string())); + }; + stream.comp_md5.update(data); + stream.comp_len = stream.comp_len.saturating_add(data.len() as u64); + if data.is_empty() { + return Ok(()); + } + if stream.mode.is_none() { + let writer = stream + .writer + .take() + .ok_or_else(|| SinkError::Fatal("chunk writer missing".to_string()))?; + stream.mode = Some(if data[0] == 0x78 { + ChunkMode::Zlib(flate2::write::ZlibDecoder::new(writer)) + } else { + ChunkMode::Stored(writer) // stored (non-zlib) chunk — Java's `inflated = raw` + }); + } + let result = match stream.mode.as_mut() { + Some(ChunkMode::Zlib(dec)) => dec + .write_all(data) + .map_err(|err| (err.to_string(), dec.get_ref().io_error.clone())), + Some(ChunkMode::Stored(w)) => w + .write_all(data) + .map_err(|err| (err.to_string(), w.io_error.clone())), + None => Ok(()), + }; + match result { + Ok(()) => Ok(()), + Err((_, Some(io))) => { + // Disk, not stream: Java's `fos.write` exception → file fails → run aborts. + if let Some(mut st) = self.states.get(file_idx).and_then(|m| m.lock().ok()) { + st.failed = true; + let _ = st.handle.take(); + let _ = fs::remove_file(&st.tmp_path); + } + let msg = format!("write failed file={} err={io}", file.relative_path); + self.log(&msg); + Err(SinkError::Fatal(msg)) + } + Err((err, None)) => { + // Corrupt zlib body. Java: inflate → null → raw → decompressed-size mismatch → retry. + let msg = format!("chunk inflate failed chunk={} err={err}", chunk.hash); + self.log(&msg); + *slot = None; + Err(SinkError::Retry(msg)) + } + } + } + + /// Stream mode: the body ended — run Java's checks in order and count the chunk. + fn on_finish(&self, item: &FetchItem, _total_len: u64) -> Result<u64, SinkError> { + let id = item.id as usize; + let Some(&(file_idx, chunk_idx)) = self.table.get(id) else { + return Err(SinkError::Fatal(format!("unknown item id {}", item.id))); + }; + let file = &self.files[file_idx]; + let chunk = &file.chunks[chunk_idx]; + let taken = self + .streams + .get(id) + .and_then(|m| m.lock().ok()) + .ok_or_else(|| SinkError::Fatal("chunk stream poisoned".to_string()))? + .take(); + let stream = match taken { + Some(s) => s, + // Empty body with no pieces: same checks on zero bytes. + None => ChunkStream::new(self.file_handle(file_idx)?, chunk.offset, chunk.size), + }; + + if chunk.compressed_size > 0 && stream.comp_len != chunk.compressed_size { + let msg = format!( + "chunk compressed-size mismatch chunk={} exp={} got={}", + chunk.hash, chunk.compressed_size, stream.comp_len + ); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + if !chunk.compressed_md5.is_empty() + && !super::to_hex(&stream.comp_md5.finalize()).eq_ignore_ascii_case(&chunk.compressed_md5) + { + let msg = format!("chunk compressed-md5 mismatch chunk={}", chunk.hash); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + let writer = match stream.mode { + Some(ChunkMode::Zlib(dec)) => match dec.finish() { + Ok(w) => w, + Err(err) => { + let msg = format!("chunk inflate failed chunk={} err={err}", chunk.hash); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + }, + Some(ChunkMode::Stored(w)) => w, + None => match stream.writer { + Some(w) => w, + None => return Err(SinkError::Fatal("chunk writer missing".to_string())), + }, + }; + if let Some(io) = writer.io_error { + if let Some(mut st) = self.states.get(file_idx).and_then(|m| m.lock().ok()) { + st.failed = true; + let _ = st.handle.take(); + let _ = fs::remove_file(&st.tmp_path); + } + let msg = format!("write failed file={} err={io}", file.relative_path); + self.log(&msg); + return Err(SinkError::Fatal(msg)); + } + if chunk.size > 0 && writer.written != chunk.size { + let msg = format!( + "chunk decompressed-size mismatch chunk={} exp={} got={}", + chunk.hash, chunk.size, writer.written + ); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + if !chunk.md5.is_empty() + && !super::to_hex(&writer.md5.finalize()).eq_ignore_ascii_case(&chunk.md5) + { + let msg = format!("chunk decompressed-md5 mismatch chunk={}", chunk.hash); + self.log(&msg); + return Err(SinkError::Retry(msg)); + } + drop(writer.file); + self.chunk_done(file_idx, writer.written)?; + Ok(0) + } +} + +/// How often the byte ticker reports while files are in flight. +pub const BYTE_TICK: Duration = Duration::from_millis(250); + +/// The CDN bases to fetch from: empty entries dropped, then one per distinct host (`host_key`), +/// the first base seen for a host kept, order preserved. +pub fn distinct_bases(bases: &[String]) -> Vec<String> { + let mut seen: HashSet<String> = HashSet::new(); + bases + .iter() + .map(|b| b.trim()) + .filter(|b| !b.is_empty() && seen.insert(host_key(b))) + .map(str::to_string) + .collect() +} + +/// Runs `fetch` with a reporter thread beside it that calls `on_bytes(bytes_done + partial, +/// bytes_total)` every [`BYTE_TICK`] when the value moved. `on_file_done` only fires when a whole +/// file is finished, so a game made of a few large files gave the UI a progress bar that sat +/// still and then jumped; this fills the gaps. One last report is made when `fetch` returns. +fn with_byte_ticker<T>( + events: &dyn GogEvents, + bytes_done: &AtomicU64, + partial: &AtomicU64, + bytes_total: u64, + fetch: impl FnOnce() -> T, +) -> T { + let stop = AtomicBool::new(false); + let current = || bytes_done.load(Ordering::Relaxed) + partial.load(Ordering::Relaxed); + std::thread::scope(|scope| { + scope.spawn(|| { + crate::priority::background(); + let mut last = u64::MAX; + while !stop.load(Ordering::Relaxed) { + std::thread::sleep(BYTE_TICK); + let now = current(); + if now != last { + last = now; + events.on_bytes(now, bytes_total); + } + } + }); + let out = fetch(); + stop.store(true, Ordering::Relaxed); + events.on_bytes(current(), bytes_total); + out + }) +} + +/// Runs one download loop. Blocking; returns when every pending file is finalized, on the first +/// fatal file failure, or on cancel. Safe to call again with `skip_paths` extended by the files +/// this run reported done (that is how the Java side re-runs after a secure-link refresh). +pub fn run(req: &GogRequest, cancel: &AtomicBool, events: &dyn GogEvents) -> GogRunResult { + match req.kind { + PlanKind::Gen2Chunks => run_gen2(req, cancel, events), + PlanKind::Gen1Ranges => run_gen1(req, cancel, events), + } +} + +fn run_gen2(req: &GogRequest, cancel: &AtomicBool, events: &dyn GogEvents) -> GogRunResult { + let files = build_plan(&req.depot_manifests, req.sort_largest_first); + let files_total = files.len() as u32; + let install_dir = PathBuf::from(&req.install_dir); + let skip: HashSet<&str> = req.skip_paths.iter().map(String::as_str).collect(); + let planned_bytes: u64 = files.iter().map(|f| f.total_size).sum(); + let chunk_count: usize = files.iter().map(|f| f.chunks.len()).sum(); + let max_workers = req.max_workers.max(1); + let process_workers = req.process_workers.max(1); + let bases = distinct_bases(&req.cdn_bases); + let hosts: Vec<String> = bases.iter().map(|b| host_key(b)).collect(); + let per_host_cap = per_host_cap_for(max_workers, hosts.len()); + + events.on_log(&format!( + "engine=rust kind=gen2 mode=stream label={} files={} chunks={} planned_bytes={} skip_paths={} workers={} per_host_cap={} process_workers={} sort_largest_first={} hosts={} {}", + req.label, + files_total, + chunk_count, + planned_bytes, + skip.len(), + max_workers, + per_host_cap, + process_workers, + req.sort_largest_first, + hosts.join(","), + crate::priority::log_field() + )); + + let files_done = AtomicU32::new(0); + let bytes_done = AtomicU64::new(0); + let files_verified = AtomicU32::new(0); + + if files.is_empty() { + // Java: the caller already returned "no depot files collected" before the loop; reaching + // here means the two parsers disagree — surface it loudly rather than pretend success. + return GogRunResult { + success: false, + error: "no files in plan (manifest parse mismatch)".to_string(), + files_total, + ..Default::default() + }; + } + + // Resume / repair pass — Java does this per task on the pool threads; here it runs up front + // on `process_workers` threads so the fetch item list can exclude verified files. + let pending: Vec<AtomicBool> = files.iter().map(|_| AtomicBool::new(false)).collect(); + let next = AtomicUsize::new(0); + let verify_threads = process_workers.min(files.len().max(1)); + std::thread::scope(|scope| { + for _ in 0..verify_threads { + scope.spawn(|| { + crate::priority::background(); + loop { + if cancel.load(Ordering::Relaxed) { + break; + } + let idx = next.fetch_add(1, Ordering::Relaxed); + if idx >= files.len() { + break; + } + let file = &files[idx]; + if skip.contains(file.relative_path.as_str()) { + continue; + } + let out_path = install_dir.join(&file.relative_path); + if file_verified(&out_path, file.total_size, &file.md5) { + files_verified.fetch_add(1, Ordering::Relaxed); + let done = files_done.fetch_add(1, Ordering::Relaxed) + 1; + events.on_file_done( + &file.relative_path, + file.total_size, + true, + done, + files_total, + bytes_done.load(Ordering::Relaxed), + planned_bytes, + ); + } else { + pending[idx].store(true, Ordering::Relaxed); + } + } + }); + } + }); + if cancel.load(Ordering::Relaxed) { + return GogRunResult { + cancelled: true, + error: "cancelled".to_string(), + files_done: files_done.load(Ordering::Relaxed), + files_verified: files_verified.load(Ordering::Relaxed), + files_total, + ..Default::default() + }; + } + + // Fetch items: one per chunk of every pending file, in plan order (chunks in manifest order). + let mut table: Vec<(usize, usize)> = Vec::new(); + let mut items: Vec<FetchItem> = Vec::new(); + let mut states: Vec<Mutex<FileState>> = Vec::new(); + let mut pending_files = 0u32; + let mut pending_bytes = 0u64; + for (file_idx, file) in files.iter().enumerate() { + let out_path = install_dir.join(&file.relative_path); + let tmp_path = PathBuf::from(format!("{}{}", out_path.display(), TMP_SUFFIX)); + let is_pending = pending[file_idx].load(Ordering::Relaxed); + states.push(Mutex::new(FileState { + handle: None, + chunks_done: 0, + partial: 0, + // Files not pending (verified or skip-listed) are already final for this run. + finalized: !is_pending, + failed: false, + tmp_path, + out_path, + })); + if !is_pending { + continue; + } + pending_files += 1; + pending_bytes = pending_bytes.saturating_add(file.total_size); + for (chunk_idx, chunk) in file.chunks.iter().enumerate() { + let path = build_cdn_path(&chunk.hash); + items.push(FetchItem { + id: table.len() as u64, + // One URL per host, in `hosts` order: the core picks `urls[host_idx]`. + urls: bases.iter().map(|b| build_chunk_url(b, &path)).collect(), + reserve: chunk.compressed_size, + range: None, + }); + table.push((file_idx, chunk_idx)); + } + } + events.on_log(&format!( + "plan verified={} pending_files={} pending_chunks={} pending_bytes={}", + files_verified.load(Ordering::Relaxed), + pending_files, + items.len(), + pending_bytes + )); + + if items.is_empty() { + events.on_log("summary bytes=0 elapsed=0.000 avg_mbps=0.00 peak_mbps=0.00 (nothing to fetch)"); + return GogRunResult { + success: true, + bytes_written: 0, + files_done: files_done.load(Ordering::Relaxed), + files_verified: files_verified.load(Ordering::Relaxed), + files_total, + ..Default::default() + }; + } + + let streams: Vec<Mutex<Option<ChunkStream>>> = (0..table.len()).map(|_| Mutex::new(None)).collect(); + let sink = GogSink { + files: &files, + table, + states, + streams, + files_total, + bytes_total: planned_bytes, + files_done: &files_done, + bytes_done: &bytes_done, + events, + speed: Mutex::new(SpeedSampler::new()), + partial: AtomicU64::new(0), + }; + let opts = FetchOptions { + max_workers, + // The ceiling split across the distinct CDN hosts (floor 6); one host gets all of it. + per_host_cap, + timeout: CHUNK_TIMEOUT, + headers: vec![("User-Agent".to_string(), USER_AGENT.to_string())], + ca_bundle_path: req.ca_bundle_path.clone(), + process_workers, + label: req.label.clone(), + // Stream mode: pieces inflate straight to disk; no whole-body byte budget (the 24 MiB + // body budget + largest-first big chunks left 1-3 requests in flight at window 16). + stream: true, + }; + let log = |line: &str| events.on_log(line); + let progress = |_bytes: u64, _items_ok: u64| {}; + let outcome = with_byte_ticker(events, &bytes_done, &sink.partial, planned_bytes, || { + run_fetch(items, &hosts, &opts, &sink, cancel, &progress, &log) + }); + + let cancelled = outcome.cancelled || cancel.load(Ordering::Relaxed); + let mut error = outcome.error.clone().unwrap_or_default(); + let mut success = !cancelled && error.is_empty(); + if success && sink.unfinalized_pending() > 0 { + success = false; + error = format!( + "engine finished with {} unfinalized file(s)", + sink.unfinalized_pending() + ); + } + if !success { + sink.cleanup_partials(); + } + let link_expiry = !cancelled + && !success + && http_status_in(&error).map(is_link_expiry_status).unwrap_or(false); + + let (wire_bytes, elapsed, avg_mbps, peak_mbps) = sink + .speed + .lock() + .map(|mut s| s.finish()) + .unwrap_or((0, 0.0, 0.0, 0.0)); + events.on_log(&format!( + "summary bytes={} written={} elapsed={:.3} avg_mbps={:.2} peak_mbps={:.2} items_ok={} files_done={} verified={} success={} cancelled={} link_expiry={}{}", + wire_bytes, + bytes_done.load(Ordering::Relaxed), + elapsed, + avg_mbps, + peak_mbps, + outcome.items_ok, + files_done.load(Ordering::Relaxed), + files_verified.load(Ordering::Relaxed), + success, + cancelled, + link_expiry, + if error.is_empty() { String::new() } else { format!(" error={error}") } + )); + + GogRunResult { + success, + cancelled, + link_expiry, + error: if cancelled && error.is_empty() { "cancelled".to_string() } else { error }, + bytes_written: bytes_done.load(Ordering::Relaxed), + files_done: files_done.load(Ordering::Relaxed), + files_verified: files_verified.load(Ordering::Relaxed), + files_total, + } +} + +// ───────────────────────────────────────────────────────────────────────────────────────────── +// gen1: per-file HTTP Range GET streamed to disk (`runGen1` / `downloadRange`) +// ───────────────────────────────────────────────────────────────────────────────────────────── + +struct Gen1State { + handle: Option<File>, + /// Bytes this file's current attempt has written (credited to the sink's `partial`); a new + /// attempt starts over, so a retry never counts its bytes twice. + attempt_bytes: u64, + out_path: PathBuf, + finalized: bool, +} + +/// Stream-mode sink for gen1. Java's `downloadRange`: `outFile.delete()` before EVERY attempt, +/// `new FileOutputStream(out)`, copy the Range body, no size/hash check; success → `doneG1++`, +/// `totalBytesG1 += size`, `"Downloading: <name> <speed>"`. Pieces arrive in order with their +/// offset; `offset == 0` marks the (re)start of an attempt, exactly where Java deletes the file. +struct Gen1Sink<'a> { + files: &'a [Gen1File], + states: Vec<Mutex<Gen1State>>, + files_total: u32, + bytes_total: u64, + files_done: &'a AtomicU32, + bytes_done: &'a AtomicU64, + events: &'a dyn GogEvents, + speed: Mutex<SpeedSampler>, + /// Bytes of files still in flight (finished chunks; for gen1, the current attempt's pieces). + /// `bytes_done + partial` is what the byte ticker reports between file completions. + partial: AtomicU64, +} + +impl<'a> FetchSink for Gen1Sink<'a> { + fn process(&self, item: &FetchItem, _body: Vec<u8>) -> Result<u64, SinkError> { + Err(SinkError::Fatal(format!( + "gen1 sink is stream-only (item {})", + item.id + ))) + } + + fn on_chunk(&self, item: &FetchItem, offset: u64, data: &[u8]) -> Result<(), SinkError> { + let idx = item.id as usize; + let (Some(file), Some(state)) = (self.files.get(idx), self.states.get(idx)) else { + return Err(SinkError::Fatal(format!("unknown gen1 item {}", item.id))); + }; + if let Ok(mut speed) = self.speed.lock() { + speed.record(data.len() as u64); + } + let mut st = state + .lock() + .map_err(|_| SinkError::Fatal("gen1 state poisoned".to_string()))?; + if offset == 0 { + // New attempt: Java `outFile.delete()` + `new FileOutputStream(out)`. + self.partial + .fetch_sub(std::mem::take(&mut st.attempt_bytes), Ordering::Relaxed); + st.handle = None; + if let Some(parent) = st.out_path.parent() { + let _ = fs::create_dir_all(parent); + } + let _ = fs::remove_file(&st.out_path); + match OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(&st.out_path) + { + Ok(h) => st.handle = Some(h), + Err(err) => { + let msg = format!("gen1 open failed file={} err={err}", file.path); + self.events.on_log(&msg); + return Err(SinkError::Retry(msg)); + } + } + } + let Some(handle) = st.handle.as_ref() else { + return Err(SinkError::Retry(format!( + "gen1 piece before start file={}", + file.path + ))); + }; + if let Err(err) = handle.write_all_at(data, offset) { + let msg = format!("gen1 write failed file={} err={err}", file.path); + self.events.on_log(&msg); + st.handle = None; + return Err(SinkError::Retry(msg)); + } + st.attempt_bytes += data.len() as u64; + self.partial.fetch_add(data.len() as u64, Ordering::Relaxed); + Ok(()) + } + + fn on_finish(&self, item: &FetchItem, _total_len: u64) -> Result<u64, SinkError> { + let idx = item.id as usize; + let (Some(file), Some(state)) = (self.files.get(idx), self.states.get(idx)) else { + return Err(SinkError::Fatal(format!("unknown gen1 item {}", item.id))); + }; + { + let mut st = state + .lock() + .map_err(|_| SinkError::Fatal("gen1 state poisoned".to_string()))?; + st.handle = None; // close + st.finalized = true; + let attempt = std::mem::take(&mut st.attempt_bytes); + self.bytes_done.fetch_add(file.size, Ordering::Relaxed); + self.partial.fetch_sub(attempt, Ordering::Relaxed); + } + let done = self.files_done.fetch_add(1, Ordering::Relaxed) + 1; + let bytes = self.bytes_done.load(Ordering::Relaxed); + self.events.on_file_done( + &file.path, + file.size, + false, + done, + self.files_total, + bytes, + self.bytes_total, + ); + Ok(0) + } +} + +/// gen1 loop. Resume = `exists && length == size` ("Resuming…", no bytes). Items = one Range GET +/// per pending file; retries/back-off by the core (Java: 3 attempts, 1 s / 2 s). Failed or +/// cancelled attempts leave whatever was written, like Java (a wrong-size partial fails the +/// size-match resume and is re-pulled next time; a base-install cancel deletes the whole dir). +fn run_gen1(req: &GogRequest, cancel: &AtomicBool, events: &dyn GogEvents) -> GogRunResult { + let mut files: Vec<Gen1File> = Vec::new(); + for manifest in &req.depot_manifests { + parse_gen1_manifest(manifest, &mut files); + } + let files_total = files.len() as u32; + let install_dir = PathBuf::from(&req.install_dir); + let skip: HashSet<&str> = req.skip_paths.iter().map(String::as_str).collect(); + let planned_bytes: u64 = files.iter().map(|f| f.size).sum(); + let max_workers = req.max_workers.max(1); + let process_workers = req.process_workers.max(1); + let host = files + .first() + .map(|f| host_key(&f.url)) + .unwrap_or_else(|| "https://none".to_string()); + + let per_host_cap = per_host_cap_for(max_workers, 1); + events.on_log(&format!( + "engine=rust kind=gen1 mode=stream label={} files={} planned_bytes={} skip_paths={} workers={} per_host_cap={} process_workers={} host={} {}", + req.label, + files_total, + planned_bytes, + skip.len(), + max_workers, + per_host_cap, + process_workers, + host, + crate::priority::log_field() + )); + + let files_done = AtomicU32::new(0); + let bytes_done = AtomicU64::new(0); + let mut files_verified = 0u32; + + if files.is_empty() { + return GogRunResult { + success: false, + error: "no files in gen1 plan (manifest parse mismatch)".to_string(), + files_total, + ..Default::default() + }; + } + + let mut items: Vec<FetchItem> = Vec::new(); + let mut states: Vec<Mutex<Gen1State>> = Vec::with_capacity(files.len()); + let mut pending_bytes = 0u64; + for (idx, file) in files.iter().enumerate() { + if cancel.load(Ordering::Relaxed) { + return GogRunResult { + cancelled: true, + error: "cancelled".to_string(), + files_done: files_done.load(Ordering::Relaxed), + files_verified, + files_total, + ..Default::default() + }; + } + let out_path = install_dir.join(&file.path); + let skipped = skip.contains(file.path.as_str()); + let resumed = !skipped + && fs::metadata(&out_path) + .map(|m| m.len() == file.size) + .unwrap_or(false); + states.push(Mutex::new(Gen1State { + handle: None, + attempt_bytes: 0, + out_path, + finalized: skipped || resumed, + })); + if skipped { + continue; + } + if resumed { + files_verified += 1; + let done = files_done.fetch_add(1, Ordering::Relaxed) + 1; + events.on_file_done( + &file.path, + file.size, + true, + done, + files_total, + bytes_done.load(Ordering::Relaxed), + planned_bytes, + ); + continue; + } + pending_bytes = pending_bytes.saturating_add(file.size); + items.push(FetchItem { + id: idx as u64, + urls: vec![file.url.clone()], + reserve: file.size, + range: Some((file.offset, file.offset.saturating_add(file.size).saturating_sub(1))), + }); + } + events.on_log(&format!( + "plan resumed={} pending_files={} pending_bytes={}", + files_verified, + items.len(), + pending_bytes + )); + if items.is_empty() { + events.on_log("summary bytes=0 elapsed=0.000 avg_mbps=0.00 peak_mbps=0.00 (nothing to fetch)"); + return GogRunResult { + success: true, + files_done: files_done.load(Ordering::Relaxed), + files_verified, + files_total, + ..Default::default() + }; + } + + let sink = Gen1Sink { + files: &files, + states, + files_total, + bytes_total: planned_bytes, + files_done: &files_done, + bytes_done: &bytes_done, + events, + speed: Mutex::new(SpeedSampler::new()), + partial: AtomicU64::new(0), + }; + let hosts = vec![host]; + let opts = FetchOptions { + max_workers, + per_host_cap, + timeout: CHUNK_TIMEOUT, + headers: vec![("User-Agent".to_string(), USER_AGENT.to_string())], + ca_bundle_path: req.ca_bundle_path.clone(), + process_workers, + label: req.label.clone(), + stream: true, + }; + let log = |line: &str| events.on_log(line); + let progress = |_bytes: u64, _items_ok: u64| {}; + let outcome = with_byte_ticker(events, &bytes_done, &sink.partial, planned_bytes, || { + run_fetch(items, &hosts, &opts, &sink, cancel, &progress, &log) + }); + + let cancelled = outcome.cancelled || cancel.load(Ordering::Relaxed); + let error = outcome.error.clone().unwrap_or_default(); + let success = !cancelled && error.is_empty(); + let (wire_bytes, elapsed, avg_mbps, peak_mbps) = sink + .speed + .lock() + .map(|mut s| s.finish()) + .unwrap_or((0, 0.0, 0.0, 0.0)); + events.on_log(&format!( + "summary bytes={} written={} elapsed={:.3} avg_mbps={:.2} peak_mbps={:.2} items_ok={} files_done={} resumed={} success={} cancelled={}{}", + wire_bytes, + bytes_done.load(Ordering::Relaxed), + elapsed, + avg_mbps, + peak_mbps, + outcome.items_ok, + files_done.load(Ordering::Relaxed), + files_verified, + success, + cancelled, + if error.is_empty() { String::new() } else { format!(" error={error}") } + )); + GogRunResult { + success, + cancelled, + link_expiry: false, // gen1 has no secure-link refresh in Java + error: if cancelled && error.is_empty() { "cancelled".to_string() } else { error }, + bytes_written: bytes_done.load(Ordering::Relaxed), + files_done: files_done.load(Ordering::Relaxed), + files_verified, + files_total, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + use std::sync::Mutex as StdMutex; + + struct Recorder { + events: StdMutex<Vec<String>>, + } + + impl GogEvents for Recorder { + fn on_file_done( + &self, + rel_path: &str, + file_bytes: u64, + verified: bool, + files_done: u32, + files_total: u32, + bytes_done: u64, + _bytes_total: u64, + ) { + self.events.lock().unwrap().push(format!( + "done {rel_path} bytes={file_bytes} verified={verified} {files_done}/{files_total} total={bytes_done}" + )); + } + fn on_log(&self, line: &str) { + self.events.lock().unwrap().push(format!("log {line}")); + } + } + + fn temp_dir(tag: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "gog_engine_{tag}_{}_{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos() + )); + fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn file_verified_mirrors_java() { + let dir = temp_dir("verify"); + let path = dir.join("f.bin"); + assert!(!file_verified(&path, 3, ""), "missing → false"); + File::create(&path).unwrap(); + assert!(!file_verified(&path, 0, ""), "empty → false even when nothing is known"); + fs::write(&path, b"abc").unwrap(); + assert!(file_verified(&path, 0, ""), "exists+non-empty, nothing known → true"); + assert!(file_verified(&path, 3, "")); + assert!(!file_verified(&path, 4, "")); + assert!(file_verified(&path, 3, "900150983CD24FB0D6963F7D28E17F72"), "case-insensitive md5"); + assert!(!file_verified(&path, 3, "00000000000000000000000000000000")); + assert!(!file_verified(&dir, 0, ""), "a directory is never verified"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn http_status_detection() { + assert_eq!(http_status_in("non-200 HTTP status (403)"), Some(403)); + assert_eq!(http_status_in("item 12 failed after 5 attempts: http get: status 500 x"), Some(500)); + assert_eq!(http_status_in("HTTP 404 not found"), Some(404)); + assert_eq!(http_status_in("chunk=abc403def timed out"), None, "hash digits are not a status"); + assert_eq!(http_status_in("timeout"), None); + let bases = vec![ + "https://gog-cdn-fastly.gog.com/token/a?x=1".to_string(), + " ".to_string(), + "https://GOG-CDN-FASTLY.gog.com/token/b?x=2".to_string(), + "https://gog-cdn-akamai.gog.com/token/c?y=3".to_string(), + "https://gog-cdn-lumen.gog.com/token/d".to_string(), + ]; + assert_eq!( + distinct_bases(&bases), + vec![ + "https://gog-cdn-fastly.gog.com/token/a?x=1".to_string(), + "https://gog-cdn-akamai.gog.com/token/c?y=3".to_string(), + "https://gog-cdn-lumen.gog.com/token/d".to_string(), + ], + "blank dropped, second fastly base dropped, order kept" + ); + assert_eq!(per_host_cap_for(96, 3), 32); + assert_eq!(per_host_cap_for(96, 1), 96); + assert!(is_link_expiry_status(401)); + assert!(is_link_expiry_status(403)); + assert!(is_link_expiry_status(404)); + assert!(is_link_expiry_status(500)); + assert!(!is_link_expiry_status(429)); + assert!(!is_link_expiry_status(502)); + } + + #[test] + fn speed_sampler_reports_totals() { + let mut s = SpeedSampler::new(); + s.record(1_000_000); + s.record(1_000_000); + let (bytes, elapsed, avg, peak) = s.finish(); + assert_eq!(bytes, 2_000_000); + assert!(elapsed > 0.0); + assert!(avg > 0.0); + assert!(peak >= 8.0, "2 MB across at most two buckets → ≥8 Mbps peak, got {peak}"); + } + + /// Whole-file finalize through the sink with a synthetic body (no network): verifies the + /// chunk → positioned write → whole-file MD5 → rename path and the progress event shape. + #[test] + fn sink_assembles_verifies_and_renames() { + use flate2::write::ZlibEncoder; + use flate2::Compression; + + let dir = temp_dir("sink"); + let part_a = b"AAAAAAAAAA".to_vec(); + let part_b = b"bbbbb".to_vec(); + let whole = [part_a.clone(), part_b.clone()].concat(); + let z = |raw: &[u8]| { + let mut enc = ZlibEncoder::new(Vec::new(), Compression::default()); + enc.write_all(raw).unwrap(); + enc.finish().unwrap() + }; + let ca = z(&part_a); + let cb = z(&part_b); + let manifest = format!( + r#"{{"depot":{{"items":[{{"path":"sub\\out.bin","md5":"{}","chunks":[ + {{"compressedMd5":"{}","md5":"{}","compressedSize":{},"size":{}}}, + {{"compressedMd5":"{}","md5":"{}","compressedSize":{},"size":{}}}]}}]}}}}"#, + md5_hex(&whole), + md5_hex(&ca), md5_hex(&part_a), ca.len(), part_a.len(), + md5_hex(&cb), md5_hex(&part_b), cb.len(), part_b.len(), + ); + let files = build_plan(&[manifest], true); + assert_eq!(files.len(), 1); + let out_path = dir.join("sub/out.bin"); + let tmp_path = PathBuf::from(format!("{}{}", out_path.display(), TMP_SUFFIX)); + let files_done = AtomicU32::new(0); + let bytes_done = AtomicU64::new(0); + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + let sink = GogSink { + files: &files, + table: vec![(0, 0), (0, 1)], + states: vec![Mutex::new(FileState { + handle: None, + chunks_done: 0, + partial: 0, + finalized: false, + failed: false, + tmp_path: tmp_path.clone(), + out_path: out_path.clone(), + })], + streams: vec![Mutex::new(None), Mutex::new(None)], + files_total: 1, + bytes_total: whole.len() as u64, + files_done: &files_done, + bytes_done: &bytes_done, + events: &rec, + speed: Mutex::new(SpeedSampler::new()), + partial: AtomicU64::new(0), + }; + let item = |id: u64| FetchItem { id, urls: vec!["x".into()], reserve: 0, range: None }; + + // Second chunk first: positioned write must still produce the right bytes. + assert_eq!(sink.process(&item(1), cb.clone()).unwrap(), part_b.len() as u64); + assert!(tmp_path.exists() && !out_path.exists(), "still staging after 1 of 2 chunks"); + // A corrupt body is a retryable failure, not fatal. + match sink.process(&item(0), b"\x78garbage".to_vec()) { + Err(SinkError::Retry(msg)) => assert!(msg.contains("mismatch"), "{msg}"), + Err(SinkError::Fatal(msg)) => panic!("expected Retry, got Fatal({msg})"), + Ok(_) => panic!("expected Retry, got Ok"), + } + assert_eq!(sink.process(&item(0), ca.clone()).unwrap(), part_a.len() as u64); + assert!(out_path.exists() && !tmp_path.exists(), "renamed after the last chunk"); + assert_eq!(fs::read(&out_path).unwrap(), whole); + assert_eq!(files_done.load(Ordering::Relaxed), 1); + assert_eq!(bytes_done.load(Ordering::Relaxed), whole.len() as u64); + let events = rec.events.lock().unwrap(); + assert!(events.iter().any(|e| e == "done sub/out.bin bytes=15 verified=false 1/1 total=15"), "{events:?}"); + assert_eq!(sink.unfinalized_pending(), 0); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn sink_whole_file_md5_mismatch_is_fatal_and_deletes_tmp() { + let dir = temp_dir("sinkbad"); + let raw = b"stored chunk (not zlib)".to_vec(); + let manifest = format!( + r#"{{"depot":{{"items":[{{"path":"f.bin","md5":"00000000000000000000000000000000","chunks":[ + {{"compressedMd5":"{}","md5":"{}","compressedSize":{},"size":{}}}]}}]}}}}"#, + md5_hex(&raw), md5_hex(&raw), raw.len(), raw.len(), + ); + let files = build_plan(&[manifest], false); + let out_path = dir.join("f.bin"); + let tmp_path = PathBuf::from(format!("{}{}", out_path.display(), TMP_SUFFIX)); + let files_done = AtomicU32::new(0); + let bytes_done = AtomicU64::new(0); + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + let sink = GogSink { + files: &files, + table: vec![(0, 0)], + states: vec![Mutex::new(FileState { + handle: None, + chunks_done: 0, + partial: 0, + finalized: false, + failed: false, + tmp_path: tmp_path.clone(), + out_path: out_path.clone(), + })], + streams: vec![Mutex::new(None)], + files_total: 1, + bytes_total: raw.len() as u64, + files_done: &files_done, + bytes_done: &bytes_done, + events: &rec, + speed: Mutex::new(SpeedSampler::new()), + partial: AtomicU64::new(0), + }; + let item = FetchItem { id: 0, urls: vec!["x".into()], reserve: 0, range: None }; + match sink.process(&item, raw) { + Err(SinkError::Fatal(msg)) => assert!(msg.contains("md5 mismatch"), "{msg}"), + Err(SinkError::Retry(msg)) => panic!("expected Fatal, got Retry({msg})"), + Ok(_) => panic!("expected Fatal, got Ok"), + } + assert!(!tmp_path.exists() && !out_path.exists()); + assert_eq!(files_done.load(Ordering::Relaxed), 0); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn run_with_everything_verified_fetches_nothing() { + let dir = temp_dir("runverified"); + let raw = b"already here".to_vec(); + fs::write(dir.join("a.bin"), &raw).unwrap(); + let manifest = format!( + r#"{{"depot":{{"items":[ + {{"path":"a.bin","md5":"{}","chunks":[{{"compressedMd5":"{}","size":{}}}]}}, + {{"path":"skipped.bin","chunks":[{{"compressedMd5":"deadbeef","size":4}}]}}]}}}}"#, + md5_hex(&raw), md5_hex(&raw), raw.len(), + ); + let req = GogRequest { + depot_manifests: vec![manifest], + cdn_bases: vec!["https://example.invalid/store?tok=1".to_string()], + install_dir: dir.to_string_lossy().into_owned(), + skip_paths: vec!["skipped.bin".to_string()], + max_workers: 4, + process_workers: 2, + sort_largest_first: true, + label: "test".to_string(), + ..Default::default() + }; + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + let cancel = AtomicBool::new(false); + let res = run(&req, &cancel, &rec); + assert!(res.success, "{res:?}"); + assert_eq!(res.files_done, 1, "skip-listed file is neither hashed nor reported"); + assert_eq!(res.files_verified, 1); + assert_eq!(res.bytes_written, 0, "verified files credit no bytes (Java totalBytes)"); + let events = rec.events.lock().unwrap(); + assert!(events[0].starts_with("log engine=rust kind=gen2 mode=stream label=test files=2 chunks=2"), "{:?}", events[0]); + assert!(events.iter().any(|e| e == "done a.bin bytes=12 verified=true 1/2 total=0"), "{events:?}"); + assert!(!events.iter().any(|e| e.contains("skipped.bin")), "{events:?}"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn per_host_cap_gives_single_host_the_whole_ceiling() { + assert_eq!(per_host_cap_for(32, 1), 32); + assert_eq!(per_host_cap_for(32, 3), 11); + assert_eq!(per_host_cap_for(8, 1), 8); + assert_eq!(per_host_cap_for(4, 1), 6, "floor is the core default"); + assert_eq!(per_host_cap_for(0, 0), 6); + } + + /// Stream mode: zlib chunk split into pieces (with a poisoned first attempt restarting at + /// offset 0), a stored chunk, corrupt data → Retry, then the whole-file finalize. + #[test] + fn stream_sink_inflates_pieces_verifies_and_finalizes() { + use flate2::write::ZlibEncoder; + use flate2::Compression; + + let dir = temp_dir("stream"); + let part_a = b"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".repeat(50); // 2000 bytes, compresses well + let part_b = b"\x01\x02stored bytes".to_vec(); // first byte != 0x78 → stored + let whole = [part_a.clone(), part_b.clone()].concat(); + let mut enc = ZlibEncoder::new(Vec::new(), Compression::default()); + enc.write_all(&part_a).unwrap(); + let ca = enc.finish().unwrap(); + assert_eq!(ca[0], 0x78); + let manifest = format!( + r#"{{"depot":{{"items":[{{"path":"out.bin","md5":"{}","chunks":[ + {{"compressedMd5":"{}","md5":"{}","compressedSize":{},"size":{}}}, + {{"compressedMd5":"{}","md5":"{}","compressedSize":{},"size":{}}}]}}]}}}}"#, + md5_hex(&whole), + md5_hex(&ca), md5_hex(&part_a), ca.len(), part_a.len(), + md5_hex(&part_b), md5_hex(&part_b), part_b.len(), part_b.len(), + ); + let files = build_plan(&[manifest], true); + let out_path = dir.join("out.bin"); + let tmp_path = PathBuf::from(format!("{}{}", out_path.display(), TMP_SUFFIX)); + let files_done = AtomicU32::new(0); + let bytes_done = AtomicU64::new(0); + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + let sink = GogSink { + files: &files, + table: vec![(0, 0), (0, 1)], + states: vec![Mutex::new(FileState { + handle: None, + chunks_done: 0, + partial: 0, + finalized: false, + failed: false, + tmp_path: tmp_path.clone(), + out_path: out_path.clone(), + })], + streams: vec![Mutex::new(None), Mutex::new(None)], + files_total: 1, + bytes_total: whole.len() as u64, + files_done: &files_done, + bytes_done: &bytes_done, + events: &rec, + speed: Mutex::new(SpeedSampler::new()), + partial: AtomicU64::new(0), + }; + let item = |id: u64| FetchItem { id, urls: vec!["x".into()], reserve: 0, range: None }; + + // Attempt 1 of the zlib chunk: corrupt tail → Retry from on_chunk. + let mut corrupt = ca.clone(); + for b in corrupt.iter_mut().skip(2) { + *b = !*b; + } + let first_bad = sink.on_chunk(&item(0), 0, &corrupt); + match first_bad { + Err(SinkError::Retry(m)) => assert!(m.contains("inflate failed"), "{m}"), + Err(SinkError::Fatal(m)) => panic!("expected Retry, got Fatal({m})"), + Ok(()) => { + // Some corruptions only surface at finish: that must also be a Retry. + match sink.on_finish(&item(0), corrupt.len() as u64) { + Err(SinkError::Retry(_)) => {} + other => panic!("expected Retry at finish, got {}", match other { + Ok(_) => "Ok".to_string(), + Err(SinkError::Fatal(m)) => format!("Fatal({m})"), + Err(SinkError::Retry(m)) => format!("Retry({m})"), + }), + } + } + } + // Attempt 2: restart at offset 0 in three pieces. + let cut1 = ca.len() / 3; + let cut2 = 2 * ca.len() / 3; + sink.on_chunk(&item(0), 0, &ca[..cut1]).unwrap(); + sink.on_chunk(&item(0), cut1 as u64, &ca[cut1..cut2]).unwrap(); + sink.on_chunk(&item(0), cut2 as u64, &ca[cut2..]).unwrap(); + assert_eq!(sink.on_finish(&item(0), ca.len() as u64).unwrap(), 0); + assert!(tmp_path.exists() && !out_path.exists()); + // Stored chunk, one piece; wrong length first (→ Retry), then the real one. + sink.on_chunk(&item(1), 0, &part_b[..3]).unwrap(); + match sink.on_finish(&item(1), 3) { + Err(SinkError::Retry(m)) => assert!(m.contains("compressed-size mismatch"), "{m}"), + _ => panic!("expected Retry on short stored chunk"), + } + sink.on_chunk(&item(1), 0, &part_b).unwrap(); + assert_eq!(sink.on_finish(&item(1), part_b.len() as u64).unwrap(), 0); + assert!(out_path.exists() && !tmp_path.exists(), "renamed after the last chunk"); + assert_eq!(fs::read(&out_path).unwrap(), whole); + assert_eq!(files_done.load(Ordering::Relaxed), 1); + assert_eq!(bytes_done.load(Ordering::Relaxed), whole.len() as u64); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn gen1_sink_streams_pieces_and_restarts_on_offset_zero() { + let dir = temp_dir("gen1sink"); + let files = vec![Gen1File { + path: "sub/blob.bin".into(), + url: "https://example.invalid/blob".into(), + offset: 100, + size: 8, + }]; + let files_done = AtomicU32::new(0); + let bytes_done = AtomicU64::new(0); + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + let sink = Gen1Sink { + files: &files, + states: vec![Mutex::new(Gen1State { + handle: None, + attempt_bytes: 0, + out_path: dir.join("sub/blob.bin"), + finalized: false, + })], + files_total: 1, + bytes_total: 8, + files_done: &files_done, + bytes_done: &bytes_done, + events: &rec, + speed: Mutex::new(SpeedSampler::new()), + partial: AtomicU64::new(0), + }; + let item = FetchItem { id: 0, urls: vec!["x".into()], reserve: 8, range: Some((100, 107)) }; + // Attempt 1 writes a partial, attempt 2 restarts from offset 0 and truncates it. + sink.on_chunk(&item, 0, b"ZZZZZZZZZZZZ").unwrap(); + sink.on_chunk(&item, 0, b"abcd").unwrap(); + sink.on_chunk(&item, 4, b"efgh").unwrap(); + assert_eq!(sink.on_finish(&item, 8).unwrap(), 0); + assert_eq!(fs::read(dir.join("sub/blob.bin")).unwrap(), b"abcdefgh"); + assert_eq!(files_done.load(Ordering::Relaxed), 1); + assert_eq!(bytes_done.load(Ordering::Relaxed), 8); + let events = rec.events.lock().unwrap(); + assert!(events.iter().any(|e| e == "done sub/blob.bin bytes=8 verified=false 1/1 total=8"), "{events:?}"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn run_gen1_resumes_on_size_match_only() { + let dir = temp_dir("gen1run"); + fs::write(dir.join("ok.bin"), b"12345").unwrap(); + fs::write(dir.join("short.bin"), b"12").unwrap(); + let manifest = r#"{"depot":[{"files":[ + {"path":"ok.bin","url":"https://example.invalid/b","offset":0,"size":5}, + {"path":"short.bin","url":"https://example.invalid/b","offset":5,"size":5}, + {"path":"skipped.bin","url":"https://example.invalid/b","offset":10,"size":5}]}]}"#; + let req = GogRequest { + kind: PlanKind::Gen1Ranges, + depot_manifests: vec![manifest.to_string()], + install_dir: dir.to_string_lossy().into_owned(), + skip_paths: vec!["skipped.bin".to_string()], + max_workers: 2, + process_workers: 1, + ..Default::default() + }; + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + // Cancel before the fetch so no network is touched: the resume pass still runs. + let cancel = AtomicBool::new(false); + // Use a pre-set cancel via a second request: run the resume pass, then expect the fetch + // to be attempted for short.bin only. We can't reach the network here, so assert on the + // plan log line instead of the outcome. + cancel.store(true, Ordering::Relaxed); + let res = run(&req, &cancel, &rec); + assert!(res.cancelled, "{res:?}"); + let events = rec.events.lock().unwrap(); + assert!(events[0].starts_with("log engine=rust kind=gen1 mode=stream label= files=3"), "{:?}", events[0]); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn run_cancelled_before_fetch_reports_cancelled() { + let dir = temp_dir("runcancel"); + let manifest = r#"{"depot":{"items":[{"path":"a.bin","chunks":[{"compressedMd5":"ab","size":4}]}]}}"#; + let req = GogRequest { + depot_manifests: vec![manifest.to_string()], + cdn_bases: vec!["https://example.invalid/store".to_string()], + install_dir: dir.to_string_lossy().into_owned(), + max_workers: 1, + process_workers: 1, + ..Default::default() + }; + let rec = Recorder { events: StdMutex::new(Vec::new()) }; + let cancel = AtomicBool::new(true); + let res = run(&req, &cancel, &rec); + assert!(res.cancelled && !res.success); + assert_eq!(res.error, "cancelled"); + let _ = fs::remove_dir_all(&dir); + } + + #[test] + fn byte_ticker_reports_in_flight_bytes_between_file_completions() { + struct Bytes(StdMutex<Vec<(u64, u64)>>); + impl GogEvents for Bytes { + #[allow(clippy::too_many_arguments)] + fn on_file_done(&self, _: &str, _: u64, _: bool, _: u32, _: u32, _: u64, _: u64) {} + fn on_log(&self, _: &str) {} + fn on_bytes(&self, done: u64, total: u64) { + self.0.lock().unwrap().push((done, total)); + } + } + let rec = Bytes(StdMutex::new(Vec::new())); + let bytes_done = AtomicU64::new(100); + let partial = AtomicU64::new(0); + let out = with_byte_ticker(&rec, &bytes_done, &partial, 1000, || { + partial.store(50, Ordering::Relaxed); + std::thread::sleep(BYTE_TICK * 3); + bytes_done.store(400, Ordering::Relaxed); + partial.store(0, Ordering::Relaxed); + 7 + }); + assert_eq!(out, 7); + let calls = rec.0.lock().unwrap().clone(); + assert!(calls.contains(&(150, 1000)), "in-flight bytes reported mid-run: {calls:?}"); + assert_eq!(calls.last(), Some(&(400, 1000)), "final report when the fetch returns"); + assert!(calls.windows(2).all(|w| w[0] != w[1] || w[1] == (400, 1000)), "no repeats while idle: {calls:?}"); + } +} diff --git a/app/src/main/rust/stores/src/gog/jni.rs b/app/src/main/rust/stores/src/gog/jni.rs new file mode 100644 index 0000000000..eee602a6b6 --- /dev/null +++ b/app/src/main/rust/stores/src/gog/jni.rs @@ -0,0 +1,321 @@ +//! JNI facade for the GOG Rust download engine — the native half of +//! `com.droiddeck.launcher.stores.gog.GogNative` (Kotlin). Symbol names are bound to that +//! package/class; the `@JvmStatic external` declarations there are the other side of this file. +//! +//! Shape mirrors the Steam download JNI (`jni.rs` `nativeDownloadApp`): `nativeStart` validates +//! its inputs, takes a `GlobalRef` on the listener, spawns the worker thread and returns a handle +//! immediately; callbacks are delivered from the worker / process-pool threads through a +//! `JavaVM` attach; `nativeCancel` flips an `AtomicBool`; `nativeRelease` drops the Java side's +//! reference to the handle (the worker keeps its own until it exits). +//! +//! Log tag `GogNative`: every engine log line goes to logcat here AND to the listener's `onLog` +//! (which the Kotlin manager folds into its own debug log). + +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; +use std::thread; + +use jni::objects::{GlobalRef, JClass, JObject, JObjectArray, JString, JValue}; +use jni::sys::{jboolean, jint, jlong, JNI_FALSE, JNI_TRUE}; +use jni::{JNIEnv, JavaVM}; + +use super::engine::{self, GogEvents, GogRequest, GogRunResult, PlanKind}; + +pub const LOG_TAG: &str = "GogNative"; + +#[cfg(target_os = "android")] +#[link(name = "log")] +unsafe extern "C" { + fn __android_log_write(prio: i32, tag: *const i8, text: *const i8) -> i32; +} + +/// `android.util.Log.i(LOG_TAG, message)`; a no-op on non-Android hosts (cargo test). +pub fn android_log(message: &str) { + #[cfg(target_os = "android")] + { + use std::ffi::CString; + let Ok(tag) = CString::new(LOG_TAG) else { + return; + }; + let sanitized = message.replace('\0', " "); + let Ok(text) = CString::new(sanitized) else { + return; + }; + unsafe { + let _ = __android_log_write(4, tag.as_ptr().cast(), text.as_ptr().cast()); + } + } + #[cfg(not(target_os = "android"))] + { + let _ = message; + } +} + +/// One in-flight run. The Java side owns a `Box<Arc<..>>` (the `jlong`); the worker thread owns a +/// second `Arc` so `nativeRelease` is safe at any time after `nativeStart` returned. +pub struct GogRunHandle { + pub cancel: AtomicBool, +} + +struct JniEvents { + vm: JavaVM, + listener: GlobalRef, +} + +impl JniEvents { + fn with_env(&self, f: impl FnOnce(&mut JNIEnv)) { + let Ok(mut env) = self.vm.attach_current_thread_as_daemon() else { + return; + }; + f(&mut env); + } + + fn clear_exception(env: &mut JNIEnv) { + if env.exception_check().unwrap_or(false) { + let _ = env.exception_clear(); + } + } + + fn complete(&self, result: &GogRunResult) { + self.with_env(|env| { + let Ok(error) = env.new_string(&result.error) else { + return; + }; + let error = JObject::from(error); + let _ = env.call_method( + self.listener.as_obj(), + "onComplete", + "(ZZZLjava/lang/String;JI)V", + &[ + JValue::Bool(if result.success { JNI_TRUE } else { JNI_FALSE }), + JValue::Bool(if result.cancelled { JNI_TRUE } else { JNI_FALSE }), + JValue::Bool(if result.link_expiry { JNI_TRUE } else { JNI_FALSE }), + JValue::Object(&error), + JValue::Long(result.bytes_written as jlong), + JValue::Int(result.files_done as jint), + ], + ); + Self::clear_exception(env); + let _ = env.delete_local_ref(error); + }); + } +} + +impl GogEvents for JniEvents { + fn on_file_done( + &self, + rel_path: &str, + file_bytes: u64, + verified: bool, + files_done: u32, + files_total: u32, + bytes_done: u64, + bytes_total: u64, + ) { + self.with_env(|env| { + let Ok(path) = env.new_string(rel_path) else { + return; + }; + let path = JObject::from(path); + let _ = env.call_method( + self.listener.as_obj(), + "onProgress", + "(JJIILjava/lang/String;JZ)V", + &[ + JValue::Long(bytes_done as jlong), + JValue::Long(bytes_total as jlong), + JValue::Int(files_done as jint), + JValue::Int(files_total as jint), + JValue::Object(&path), + JValue::Long(file_bytes as jlong), + JValue::Bool(if verified { JNI_TRUE } else { JNI_FALSE }), + ], + ); + Self::clear_exception(env); + let _ = env.delete_local_ref(path); + }); + } + + fn on_bytes(&self, bytes_done: u64, bytes_total: u64) { + // A listener without `onBytes` (an app built against the older contract) makes this a + // NoSuchMethodError, which clear_exception drops: the run is unaffected. + self.with_env(|env| { + let _ = env.call_method( + self.listener.as_obj(), + "onBytes", + "(JJ)V", + &[ + JValue::Long(bytes_done as jlong), + JValue::Long(bytes_total as jlong), + ], + ); + Self::clear_exception(env); + }); + } + + fn on_log(&self, line: &str) { + android_log(line); + self.with_env(|env| { + let Ok(text) = env.new_string(line) else { + return; + }; + let text = JObject::from(text); + let _ = env.call_method( + self.listener.as_obj(), + "onLog", + "(Ljava/lang/String;)V", + &[JValue::Object(&text)], + ); + Self::clear_exception(env); + let _ = env.delete_local_ref(text); + }); + } +} + +fn jstring_to_string(env: &mut JNIEnv, value: &JString) -> Option<String> { + env.get_string(value) + .ok() + .map(|s| s.to_string_lossy().into_owned()) +} + +fn string_array_to_vec(env: &mut JNIEnv, array: &JObjectArray) -> Vec<String> { + if array.is_null() { + return Vec::new(); + } + let len = env.get_array_length(array).unwrap_or(0); + let mut out = Vec::with_capacity(len.max(0) as usize); + for i in 0..len { + let Ok(element) = env.get_object_array_element(array, i) else { + continue; + }; + if element.is_null() { + continue; + } + let text = JString::from(element); + if let Some(s) = jstring_to_string(env, &text) { + out.push(s); + } + let _ = env.delete_local_ref(text); + } + out +} + +unsafe fn handle_ref(handle: jlong) -> Option<&'static Arc<GogRunHandle>> { + let ptr = handle as *const Arc<GogRunHandle>; + if ptr.is_null() { + None + } else { + Some(unsafe { &*ptr }) + } +} + +/// Symbol-binding probe (see `GogNative.isAvailable`). Returns 1. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_gog_GogNative_nativeProbe( + _env: JNIEnv, + _class: JClass, +) -> jint { + 1 +} + +/// Starts one download loop on a worker thread and returns a handle (0 = could not start; the +/// listener then receives NO callbacks). Inputs are exactly what `GogDownloadManager` holds after +/// manifest + secure-link resolution — see `GogRequest`. +#[no_mangle] +#[allow(clippy::too_many_arguments)] +pub extern "system" fn Java_com_droiddeck_launcher_stores_gog_GogNative_nativeStart( + mut env: JNIEnv, + _class: JClass, + kind: jint, + depot_manifests: JObjectArray, + cdn_bases: JObjectArray, + install_dir: JString, + skip_paths: JObjectArray, + ca_bundle_path: JString, + max_workers: jint, + process_workers: jint, + sort_largest_first: jboolean, + label: JString, + listener: JObject, +) -> jlong { + if listener.is_null() { + android_log("nativeStart: null listener"); + return 0; + } + let Ok(vm) = env.get_java_vm() else { + android_log("nativeStart: JavaVM unavailable"); + return 0; + }; + let Ok(listener) = env.new_global_ref(&listener) else { + android_log("nativeStart: listener ref failed"); + return 0; + }; + let request = GogRequest { + kind: PlanKind::from_i32(kind), + depot_manifests: string_array_to_vec(&mut env, &depot_manifests), + cdn_bases: string_array_to_vec(&mut env, &cdn_bases), + install_dir: jstring_to_string(&mut env, &install_dir).unwrap_or_default(), + skip_paths: string_array_to_vec(&mut env, &skip_paths), + ca_bundle_path: jstring_to_string(&mut env, &ca_bundle_path).unwrap_or_default(), + max_workers: max_workers.max(1) as usize, + process_workers: process_workers.max(1) as usize, + sort_largest_first: sort_largest_first != JNI_FALSE, + label: jstring_to_string(&mut env, &label).unwrap_or_else(|| "gog".to_string()), + }; + let needs_base = request.kind == PlanKind::Gen2Chunks; + if (needs_base && super::engine::distinct_bases(&request.cdn_bases).is_empty()) + || request.install_dir.is_empty() + || request.depot_manifests.is_empty() + { + android_log("nativeStart: invalid request (no cdn base / empty install dir / no manifests)"); + return 0; + } + + let handle = Arc::new(GogRunHandle { + cancel: AtomicBool::new(false), + }); + let worker = Arc::clone(&handle); + let spawned = thread::Builder::new() + .name("gog-dl".to_string()) + .spawn(move || { + crate::priority::background(); + let events = JniEvents { vm, listener }; + let result = engine::run(&request, &worker.cancel, &events); + events.complete(&result); + }); + if spawned.is_err() { + android_log("nativeStart: worker thread spawn failed"); + return 0; + } + Box::into_raw(Box::new(handle)) as jlong +} + +/// Requests cancellation; the run stops at its next scheduling point and `onComplete` fires with +/// `cancelled = true`. Idempotent. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_gog_GogNative_nativeCancel( + _env: JNIEnv, + _class: JClass, + handle: jlong, +) { + if let Some(run) = unsafe { handle_ref(handle) } { + run.cancel.store(true, Ordering::Relaxed); + } +} + +/// Drops the Java side's reference. Call once, after `onComplete` (or after cancelling — the worker +/// keeps the run alive until it exits either way). +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_gog_GogNative_nativeRelease( + _env: JNIEnv, + _class: JClass, + handle: jlong, +) { + let ptr = handle as *mut Arc<GogRunHandle>; + if ptr.is_null() { + return; + } + unsafe { + drop(Box::from_raw(ptr)); + } +} diff --git a/app/src/main/rust/stores/src/gog/plan.rs b/app/src/main/rust/stores/src/gog/plan.rs new file mode 100644 index 0000000000..830106dc12 --- /dev/null +++ b/app/src/main/rust/stores/src/gog/plan.rs @@ -0,0 +1,370 @@ +//! Depot-manifest JSON → download plan. Mirrors `GogDownloadManager.parseDepotManifest` rule for +//! rule (see Bannerlator's `docs/RUST_GOG_PARITY.md` §3): +//! - root `depot.items[]`; `path` with `\` → `/` and one leading `/` stripped; +//! - a file is kept only when `path` is non-empty AND `chunks[]` is non-empty; +//! - per chunk: CDN hash = `compressedMd5` when non-empty, else `md5`; a chunk with neither is +//! dropped; `compressedSize` / `size` default 0; +//! - file `md5` kept when non-empty; `total_size` = Σ chunk `size` (decompressed); +//! - a file whose chunks all dropped is not added. +//! `org.json` coercion (`optString` turns numbers/bools into their text, `optLong` parses numeric +//! strings) is reproduced by [`opt_string`] / [`opt_u64`]. +//! +//! Additionally each chunk carries `offset` = prefix-sum of the preceding chunks' decompressed +//! sizes — Java appends sequentially so it never needs one; the Rust sink writes chunks at that +//! offset as they arrive so the final bytes are identical. + +use serde_json::Value; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ChunkRef { + /// CDN-path hash (`compressedMd5` preferred, else `md5`). + pub hash: String, + /// Verify compressed bytes before inflate (may be empty). + pub compressed_md5: String, + /// Verify decompressed bytes after inflate (may be empty). + pub md5: String, + /// Expected compressed size, 0 if absent. + pub compressed_size: u64, + /// Expected decompressed size, 0 if absent. + pub size: u64, + /// Decompressed offset inside the file (prefix-sum of preceding `size`s). + pub offset: u64, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct PlannedFile { + /// Install-dir-relative path, forward slashes, no leading slash. + pub relative_path: String, + /// Whole-file MD5 from the manifest ("" when absent). + pub md5: String, + /// Σ chunk decompressed sizes (0 if unknown). + pub total_size: u64, + pub chunks: Vec<ChunkRef>, +} + +/// `JSONObject.optString(key, "")` semantics: strings as-is, numbers/bools rendered, null/absent +/// → "". (`org.json` renders `JSONObject.NULL` as "null"; GOG manifests never carry JSON nulls in +/// these fields, and treating one as absent is the only safe reading.) +pub fn opt_string(obj: &Value, key: &str) -> String { + match obj.get(key) { + Some(Value::String(s)) => s.clone(), + Some(Value::Number(n)) => n.to_string(), + Some(Value::Bool(b)) => b.to_string(), + _ => String::new(), + } +} + +/// `JSONObject.optLong(key, 0)` semantics for a size field (numbers, or numeric strings; a +/// negative/float value that Java would truncate is clamped to 0 — sizes are never negative). +pub fn opt_u64(obj: &Value, key: &str) -> u64 { + match obj.get(key) { + Some(Value::Number(n)) => n + .as_u64() + .or_else(|| n.as_i64().map(|v| v.max(0) as u64)) + .or_else(|| n.as_f64().map(|v| if v > 0.0 { v as u64 } else { 0 })) + .unwrap_or(0), + Some(Value::String(s)) => s.trim().parse::<i64>().map(|v| v.max(0) as u64).unwrap_or(0), + _ => 0, + } +} + +/// Parses one inflated gen2 depot manifest and appends its files to `out` +/// (`GogDownloadManager.parseDepotManifest`). Malformed JSON adds nothing, like Java's catch-all. +pub fn parse_depot_manifest(json: &str, out: &mut Vec<PlannedFile>) { + let Ok(root) = serde_json::from_str::<Value>(json) else { + return; + }; + let Some(items) = root + .get("depot") + .and_then(|d| d.get("items")) + .and_then(Value::as_array) + else { + return; + }; + for entry in items { + if !entry.is_object() { + continue; + } + let mut path = opt_string(entry, "path").replace('\\', "/"); + if let Some(stripped) = path.strip_prefix('/') { + path = stripped.to_string(); + } + let chunks = entry.get("chunks").and_then(Value::as_array); + let Some(chunks) = chunks else { + continue; + }; + if path.is_empty() || chunks.is_empty() { + continue; + } + let file_md5 = opt_string(entry, "md5"); + let mut planned = PlannedFile { + relative_path: path, + md5: file_md5, + total_size: 0, + chunks: Vec::with_capacity(chunks.len()), + }; + let mut offset = 0u64; + for chunk in chunks { + if !chunk.is_object() { + continue; + } + let compressed_md5 = opt_string(chunk, "compressedMd5"); + let dec_md5 = opt_string(chunk, "md5"); + let compressed_size = opt_u64(chunk, "compressedSize"); + let dec_size = opt_u64(chunk, "size"); + let hash = if !compressed_md5.is_empty() { + compressed_md5.clone() + } else { + dec_md5.clone() + }; + if hash.is_empty() { + continue; + } + planned.chunks.push(ChunkRef { + hash, + compressed_md5, + md5: dec_md5, + compressed_size, + size: dec_size, + offset, + }); + offset = offset.saturating_add(dec_size); + } + planned.total_size = offset; + if !planned.chunks.is_empty() { + out.push(planned); + } + } +} + +/// One gen1 file: a byte range of a (pre-signed) depot blob (`GogDownloadManager.Gen1File`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Gen1File { + /// Install-dir-relative path, used verbatim (Java's gen1 path does not normalise slashes). + pub path: String, + pub url: String, + pub offset: u64, + pub size: u64, +} + +/// Parses a gen1 build manifest (`runGen1`): every `depot[]` entry that is not `support: true`, +/// its `files[]` → (`path`, `url`, `offset` default 0, `size` default 0); an entry with +/// `size == 0` is dropped (Java's `path == null || url == null` checks never fire — `optString` +/// returns "" — so `size == 0` is the only effective filter, mirrored here). +pub fn parse_gen1_manifest(json: &str, out: &mut Vec<Gen1File>) { + let Ok(root) = serde_json::from_str::<Value>(json) else { + return; + }; + let Some(depots) = root.get("depot").and_then(Value::as_array) else { + return; + }; + for depot in depots { + if !depot.is_object() { + continue; + } + if depot.get("support").and_then(Value::as_bool).unwrap_or(false) { + continue; + } + let Some(files) = depot.get("files").and_then(Value::as_array) else { + continue; + }; + for f in files { + if !f.is_object() { + continue; + } + let path = opt_string(f, "path"); + let offset = opt_u64(f, "offset"); + let size = opt_u64(f, "size"); + let url = opt_string(f, "url"); + if size == 0 { + continue; + } + out.push(Gen1File { path, url, offset, size }); + } + } +} + +/// Parses every manifest in order (the order Java fetched the depots) and optionally applies the +/// base-install largest-first (LPT) ordering — a STABLE sort by descending `total_size`, exactly +/// `Collections.sort(files, (a, b) -> Long.compare(b.totalSize, a.totalSize))`. The DLC and +/// dependency paths in Java do not sort, so they pass `sort_largest_first = false`. +pub fn build_plan(manifests: &[String], sort_largest_first: bool) -> Vec<PlannedFile> { + let mut files = Vec::new(); + for manifest in manifests { + parse_depot_manifest(manifest, &mut files); + } + if sort_largest_first { + files.sort_by(|a, b| b.total_size.cmp(&a.total_size)); + } + files +} + +/// `GogDownloadManager.buildCdnPath`: `"ab/cd/abcdef..."`. A hash shorter than 4 chars would +/// throw in Java (and fail that file); here it is returned unchanged and fails at fetch time. +pub fn build_cdn_path(hash: &str) -> String { + if !hash.is_char_boundary(2) || !hash.is_char_boundary(4) || hash.len() < 4 { + return hash.to_string(); + } + format!("{}/{}/{}", &hash[0..2], &hash[2..4], hash) +} + +/// `GogDownloadManager.buildChunkUrl`: appends the chunk path BEFORE any query string so the +/// secure-link token survives. +pub fn build_chunk_url(base: &str, chunk_path: &str) -> String { + match base.find('?') { + Some(q) => format!("{}/{}{}", &base[..q], chunk_path, &base[q..]), + None => format!("{base}/{chunk_path}"), + } +} + +/// Per-host key for the fetch core's host semaphores: `scheme://host[:port]` of `url` (lowercased +/// host). Java has one resolved CDN base per run, so the host list has exactly one entry. +pub fn host_key(url: &str) -> String { + let (scheme, rest) = match url.find("://") { + Some(i) => (&url[..i], &url[i + 3..]), + None => ("https", url), + }; + let end = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + format!("{}://{}", scheme.to_ascii_lowercase(), rest[..end].to_ascii_lowercase()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const MANIFEST: &str = r#"{ + "depot": { "items": [ + { "path": "\\bin\\game.exe", "md5": "AABBCCDDEEFF00112233445566778899", + "chunks": [ + { "compressedMd5": "c1c1c1c1", "md5": "d1d1d1d1", "compressedSize": 100, "size": 250 }, + { "compressedMd5": "c2c2c2c2", "md5": "d2d2d2d2", "compressedSize": "50", "size": 75 } + ] }, + { "path": "/data/pak0.pak", + "chunks": [ + { "md5": "onlydec1", "size": 1000 }, + { "compressedSize": 5, "size": 5 } + ] }, + { "path": "empty.txt", "chunks": [] }, + { "path": "", "chunks": [ { "compressedMd5": "zz", "size": 1 } ] }, + { "path": "nohash.bin", "chunks": [ { "size": 9 } ] }, + { "path": "dir", "type": "DepotDirectory" }, + "not-an-object" + ] } }"#; + + #[test] + fn parse_mirrors_java_rules() { + let mut files = Vec::new(); + parse_depot_manifest(MANIFEST, &mut files); + assert_eq!(files.len(), 2, "empty-chunk, empty-path, no-hash and directory entries drop"); + + let exe = &files[0]; + assert_eq!(exe.relative_path, "bin/game.exe"); + assert_eq!(exe.md5, "AABBCCDDEEFF00112233445566778899"); + assert_eq!(exe.total_size, 325); + assert_eq!(exe.chunks.len(), 2); + assert_eq!(exe.chunks[0].hash, "c1c1c1c1"); + assert_eq!(exe.chunks[0].offset, 0); + assert_eq!(exe.chunks[1].hash, "c2c2c2c2"); + assert_eq!(exe.chunks[1].compressed_size, 50, "numeric string coerces like optLong"); + assert_eq!(exe.chunks[1].offset, 250); + + let pak = &files[1]; + assert_eq!(pak.relative_path, "data/pak0.pak"); + assert_eq!(pak.md5, "", "absent file md5 → empty (no whole-file verify)"); + assert_eq!(pak.chunks.len(), 1, "chunk without any hash is dropped"); + assert_eq!(pak.chunks[0].hash, "onlydec1", "hash falls back to md5"); + assert_eq!(pak.chunks[0].compressed_md5, ""); + assert_eq!(pak.total_size, 1000); + } + + #[test] + fn malformed_manifest_adds_nothing() { + let mut files = Vec::new(); + parse_depot_manifest("{not json", &mut files); + parse_depot_manifest(r#"{"depot": {}}"#, &mut files); + parse_depot_manifest(r#"{"items": []}"#, &mut files); + assert!(files.is_empty()); + } + + #[test] + fn plan_orders_largest_first_stably_only_when_asked() { + let m1 = r#"{"depot":{"items":[ + {"path":"a","chunks":[{"md5":"a1","size":10}]}, + {"path":"b","chunks":[{"md5":"b1","size":30}]}]}}"# + .to_string(); + let m2 = r#"{"depot":{"items":[ + {"path":"c","chunks":[{"md5":"c1","size":30}]}, + {"path":"d","chunks":[{"md5":"d1","size":20}]}]}}"# + .to_string(); + let unsorted = build_plan(&[m1.clone(), m2.clone()], false); + let order: Vec<&str> = unsorted.iter().map(|f| f.relative_path.as_str()).collect(); + assert_eq!(order, ["a", "b", "c", "d"], "depot order, file order within depot"); + + let sorted = build_plan(&[m1, m2], true); + let order: Vec<&str> = sorted.iter().map(|f| f.relative_path.as_str()).collect(); + assert_eq!(order, ["b", "c", "d", "a"], "stable: b (30) before c (30) keeps source order"); + } + + #[test] + fn gen1_parse_mirrors_run_gen1() { + let json = r#"{"installDirectory":"Game","depot":[ + {"support":true,"files":[{"path":"support.bin","url":"u","offset":0,"size":5}]}, + {"files":[ + {"path":"a.bin","url":"https://cdn.gog.com/blob?t=1","offset":10,"size":20}, + {"path":"zero.bin","url":"u","offset":0,"size":0}, + {"path":"b.bin","url":"https://cdn.gog.com/blob?t=1","size":"7"}, + "junk" + ]}, + {"nofiles":true} + ]}"#; + let mut files = Vec::new(); + parse_gen1_manifest(json, &mut files); + assert_eq!( + files, + vec![ + Gen1File { path: "a.bin".into(), url: "https://cdn.gog.com/blob?t=1".into(), offset: 10, size: 20 }, + Gen1File { path: "b.bin".into(), url: "https://cdn.gog.com/blob?t=1".into(), offset: 0, size: 7 }, + ] + ); + let mut none = Vec::new(); + parse_gen1_manifest(r#"{"depots":[]}"#, &mut none); + assert!(none.is_empty()); + } + + #[test] + fn cdn_path_and_chunk_url() { + assert_eq!(build_cdn_path("abcdef0123"), "ab/cd/abcdef0123"); + assert_eq!(build_cdn_path("abc"), "abc"); + assert_eq!( + build_chunk_url("https://cdn.gog.com/content-system/v2/store/1?token=x&y=1", "ab/cd/abcdef"), + "https://cdn.gog.com/content-system/v2/store/1/ab/cd/abcdef?token=x&y=1" + ); + assert_eq!( + build_chunk_url("https://gog-cdn-fastly.gog.com/content-system/v2/dependencies/store", "ab/cd/abcdef"), + "https://gog-cdn-fastly.gog.com/content-system/v2/dependencies/store/ab/cd/abcdef" + ); + } + + #[test] + fn host_key_strips_path_and_query() { + assert_eq!(host_key("https://CDN.gog.com/a/b?c=1"), "https://cdn.gog.com"); + assert_eq!(host_key("http://host:8080/x"), "http://host:8080"); + assert_eq!(host_key("nohost"), "https://nohost"); + } + + #[test] + fn opt_helpers_coerce_like_org_json() { + let v: Value = serde_json::from_str(r#"{"n": 12, "s": "34", "b": true, "z": null, "f": 2.5, "neg": -4}"#).unwrap(); + assert_eq!(opt_string(&v, "n"), "12"); + assert_eq!(opt_string(&v, "s"), "34"); + assert_eq!(opt_string(&v, "b"), "true"); + assert_eq!(opt_string(&v, "z"), ""); + assert_eq!(opt_string(&v, "missing"), ""); + assert_eq!(opt_u64(&v, "n"), 12); + assert_eq!(opt_u64(&v, "s"), 34); + assert_eq!(opt_u64(&v, "f"), 2); + assert_eq!(opt_u64(&v, "neg"), 0); + assert_eq!(opt_u64(&v, "b"), 0); + assert_eq!(opt_u64(&v, "missing"), 0); + } +} diff --git a/app/src/main/rust/stores/src/lib.rs b/app/src/main/rust/stores/src/lib.rs new file mode 100644 index 0000000000..963826c40e --- /dev/null +++ b/app/src/main/rust/stores/src/lib.rs @@ -0,0 +1,29 @@ +//! DroidDeck's native store download engines (`libdroiddeckstores.so`). +//! +//! The GOG, Epic Games Store and Amazon Games download loops, ported from Bannerlator's +//! `bl-steam-client` crate (GPL-3.0-or-later, itself derived from WinNative's `wn-steam-client`). +//! Only the store engines and the fetch core they run on came across: DroidDeck's Steam client is +//! the real one, running in the Linux runtime, so none of the Steam CM / depot code is here. +//! +//! Each store module owns its manifest → plan → `FetchItem` mapping, its `FetchSink` (inflate + +//! hash + write in the exact output layout of the Kotlin manager that drives it) and its JNI facade. +//! The JNI contract — classes, method names, signatures, threading — is written down in `JNI.md` +//! next to `Cargo.toml`; the Kotlin side mirrors it exactly. +//! +//! What a run looks like: Kotlin hands over a manifest it already parsed (or, for Amazon, the +//! plan it built from one), the resolved CDN base(s) and the install directory, calls `nativeStart` +//! and gets a handle back at once. Everything after that happens on native threads: the fetch core +//! keeps an adaptive window of HTTP requests in flight on one tokio runtime, hands bodies to a sync +//! process pool that inflates, hashes and writes them, and reports progress through the listener +//! object Kotlin passed in. `nativeCancel` flips a flag; `onComplete` fires exactly once. + +#![allow(clippy::missing_safety_doc, clippy::result_large_err)] + +pub mod amazon; +pub mod epic; +pub mod fetch_core; +pub mod fetch_tuning; +pub mod gog; +pub mod md5_small; +pub mod priority; +pub mod stores_jni; diff --git a/app/src/main/rust/stores/src/md5_small.rs b/app/src/main/rust/stores/src/md5_small.rs new file mode 100644 index 0000000000..8abd58bcc4 --- /dev/null +++ b/app/src/main/rust/stores/src/md5_small.rs @@ -0,0 +1,197 @@ +//! Minimal MD5 (RFC 1321) for the store download engines (GOG chunk / file verification). +//! +//! In-crate on purpose: the crate is built `--locked` in CI with no way to add a dependency from +//! this environment, and the algorithm is 100 lines. Not for anything security-sensitive — it only +//! mirrors the checksum the Java managers already compare against. + +/// One-shot digest. +pub fn md5(data: &[u8]) -> [u8; 16] { + let mut h = Md5::new(); + h.update(data); + h.finalize() +} + +/// Lower-case hex of a 16-byte digest. +pub fn md5_hex(digest: &[u8; 16]) -> String { + const HEX: &[u8; 16] = b"0123456789abcdef"; + let mut out = String::with_capacity(32); + for b in digest { + out.push(HEX[(b >> 4) as usize] as char); + out.push(HEX[(b & 0x0f) as usize] as char); + } + out +} + +/// Streaming MD5: `update` any number of times, then `finalize`. +#[derive(Clone, Debug)] +pub struct Md5 { + state: [u32; 4], + buffer: [u8; 64], + buffer_len: usize, + total_len: u64, +} + +impl Default for Md5 { + fn default() -> Self { + Self::new() + } +} + +const S: [u32; 64] = [ + 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, + 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, + 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, +]; + +const K: [u32; 64] = [ + 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391, +]; + +impl Md5 { + pub fn new() -> Self { + Self { + state: [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476], + buffer: [0u8; 64], + buffer_len: 0, + total_len: 0, + } + } + + pub fn update(&mut self, mut data: &[u8]) { + self.total_len = self.total_len.wrapping_add(data.len() as u64); + if self.buffer_len > 0 { + let take = (64 - self.buffer_len).min(data.len()); + self.buffer[self.buffer_len..self.buffer_len + take].copy_from_slice(&data[..take]); + self.buffer_len += take; + data = &data[take..]; + if self.buffer_len == 64 { + let block = self.buffer; + self.compress(&block); + self.buffer_len = 0; + } + } + while data.len() >= 64 { + let mut block = [0u8; 64]; + block.copy_from_slice(&data[..64]); + self.compress(&block); + data = &data[64..]; + } + if !data.is_empty() { + self.buffer[..data.len()].copy_from_slice(data); + self.buffer_len = data.len(); + } + } + + pub fn finalize(mut self) -> [u8; 16] { + let bit_len = self.total_len.wrapping_mul(8); + let mut pad = [0u8; 72]; + pad[0] = 0x80; + // Pad to 56 mod 64, then append the 64-bit little-endian bit length. + let pad_len = if self.buffer_len < 56 { + 56 - self.buffer_len + } else { + 120 - self.buffer_len + }; + let total_len_before = self.total_len; + self.update(&pad[..pad_len]); + self.update(&bit_len.to_le_bytes()); + debug_assert_eq!(self.buffer_len, 0); + self.total_len = total_len_before; + let mut out = [0u8; 16]; + for (i, word) in self.state.iter().enumerate() { + out[i * 4..i * 4 + 4].copy_from_slice(&word.to_le_bytes()); + } + out + } + + fn compress(&mut self, block: &[u8; 64]) { + let mut m = [0u32; 16]; + for (i, word) in m.iter_mut().enumerate() { + *word = u32::from_le_bytes([ + block[i * 4], + block[i * 4 + 1], + block[i * 4 + 2], + block[i * 4 + 3], + ]); + } + let [mut a, mut b, mut c, mut d] = self.state; + for i in 0..64 { + let (f, g) = match i / 16 { + 0 => ((b & c) | (!b & d), i), + 1 => ((d & b) | (!d & c), (5 * i + 1) % 16), + 2 => (b ^ c ^ d, (3 * i + 5) % 16), + _ => (c ^ (b | !d), (7 * i) % 16), + }; + let f = f.wrapping_add(a).wrapping_add(K[i]).wrapping_add(m[g]); + a = d; + d = c; + c = b; + b = b.wrapping_add(f.rotate_left(S[i])); + } + self.state[0] = self.state[0].wrapping_add(a); + self.state[1] = self.state[1].wrapping_add(b); + self.state[2] = self.state[2].wrapping_add(c); + self.state[3] = self.state[3].wrapping_add(d); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn hex(data: &[u8]) -> String { + md5_hex(&md5(data)) + } + + #[test] + fn rfc_1321_vectors() { + assert_eq!(hex(b""), "d41d8cd98f00b204e9800998ecf8427e"); + assert_eq!(hex(b"a"), "0cc175b9c0f1b6a831c399e269772661"); + assert_eq!(hex(b"abc"), "900150983cd24fb0d6963f7d28e17f72"); + assert_eq!(hex(b"message digest"), "f96b697d7cb7938d525a2f31aaf161d0"); + assert_eq!( + hex(b"abcdefghijklmnopqrstuvwxyz"), + "c3fcd3d76192e4007dfb496cca67e13b" + ); + assert_eq!( + hex(b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), + "d174ab98d277d9f5a5611c2c9f419d9f" + ); + assert_eq!( + hex(b"12345678901234567890123456789012345678901234567890123456789012345678901234567890"), + "57edf4a22be3c955ac49da2e2107b67a" + ); + } + + #[test] + fn streaming_matches_one_shot_across_block_boundaries() { + let data: Vec<u8> = (0..1000u32).map(|i| (i * 7 % 251) as u8).collect(); + let whole = md5(&data); + for split in [0usize, 1, 55, 56, 63, 64, 65, 127, 128, 500, 999, 1000] { + let mut h = Md5::new(); + h.update(&data[..split]); + h.update(&data[split..]); + assert_eq!(h.finalize(), whole, "split={split}"); + } + let mut h = Md5::new(); + for b in &data { + h.update(std::slice::from_ref(b)); + } + assert_eq!(h.finalize(), whole); + } + + #[test] + fn padding_edge_lengths() { + // 55 / 56 / 64 bytes exercise the "pad fits in one block" vs "needs a second block" paths. + assert_eq!(hex(&[b'a'; 55]), "ef1772b6dff9a122358552954ad0df65"); + assert_eq!(hex(&[b'a'; 56]), "3b0c8ac703f828b04c6c197006d17218"); + assert_eq!(hex(&[b'a'; 64]), "014842d480b571495a4a0363793f7367"); + } +} diff --git a/app/src/main/rust/stores/src/priority.rs b/app/src/main/rust/stores/src/priority.rs new file mode 100644 index 0000000000..916008face --- /dev/null +++ b/app/src/main/rust/stores/src/priority.rs @@ -0,0 +1,55 @@ +//! Background priority for every thread the store engines create. +//! +//! A download keeps several threads busy at once (the run's worker, the tokio runtime and its +//! blocking pool for DNS, the inflate/hash/write pool, the throughput reporter, GOG's verify pass, +//! the Epic assembler). At the default nice 0 they compete as equals with the app's UI and input +//! threads, and on the device the menus' animations and controller navigation slowed visibly for +//! as long as a store download ran. At nice +10 the scheduler hands them only what the UI leaves, +//! which for a download bound by network and storage costs next to nothing. +//! +//! Linux applies `setpriority(PRIO_PROCESS, tid, …)` to the one thread `tid`, not the process, and +//! a new thread inherits its creator's nice value; every thread is still lowered explicitly at its +//! start so the guarantee does not depend on who spawned it. Only threads this library creates are +//! touched: the JNI calling thread belongs to the app and keeps its priority. + +/// The nice value store-engine threads run at. +pub const BACKGROUND_NICE: i32 = 10; + +/// Lower the calling thread to [`BACKGROUND_NICE`]. Best effort: a refusal leaves the thread at +/// its current priority and the download runs as before. +pub fn background() { + #[cfg(any(target_os = "android", target_os = "linux"))] + unsafe { + let tid = libc::gettid(); + let _ = libc::setpriority(libc::PRIO_PROCESS, tid as libc::id_t, BACKGROUND_NICE); + } +} + +/// The calling thread's nice value as a log field, e.g. `priority=nice10`. +pub fn log_field() -> String { + #[cfg(any(target_os = "android", target_os = "linux"))] + { + let nice = unsafe { libc::getpriority(libc::PRIO_PROCESS, libc::gettid() as libc::id_t) }; + format!("priority=nice{nice}") + } + #[cfg(not(any(target_os = "android", target_os = "linux")))] + { + "priority=default".to_string() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_lowered_thread_reports_nice10() { + let field = std::thread::spawn(|| { + background(); + log_field() + }) + .join() + .unwrap(); + assert_eq!(field, "priority=nice10"); + } +} diff --git a/app/src/main/rust/stores/src/stores_jni.rs b/app/src/main/rust/stores/src/stores_jni.rs new file mode 100644 index 0000000000..e69b7f5bd5 --- /dev/null +++ b/app/src/main/rust/stores/src/stores_jni.rs @@ -0,0 +1,26 @@ +//! The one export that is not a store engine: `com.droiddeck.launcher.stores.StoresNative`. +//! +//! `nativeVersion()` exists so the app can prove, at Setup time, that `libdroiddeckstores.so` +//! loaded and its JNI exports bind — before a user is mid-download and an +//! `UnsatisfiedLinkError` would surface as a failed install. It returns the crate version from +//! `Cargo.toml`, which the app can show next to the other component versions. + +use jni::objects::JClass; +use jni::sys::jstring; +use jni::JNIEnv; + +/// The crate version, as `Cargo.toml` declares it. +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + +/// `StoresNative.nativeVersion(): String` — the crate version (`"0.1.0"`). A null return means the +/// JVM could not allocate the string, which the caller treats like a failed load. +#[no_mangle] +pub extern "system" fn Java_com_droiddeck_launcher_stores_StoresNative_nativeVersion( + env: JNIEnv, + _class: JClass, +) -> jstring { + match env.new_string(VERSION) { + Ok(s) => s.into_raw(), + Err(_) => std::ptr::null_mut(), + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesStoreScanTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesStoreScanTest.kt new file mode 100644 index 0000000000..6284086c7e --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesStoreScanTest.kt @@ -0,0 +1,68 @@ +package com.droiddeck.launcher.frontend + +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoreLaunch +import java.io.File +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +/** A store install is one more folder the scan finds, named and aimed by its sidecar. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class AddedGamesStoreScanTest { + private val app get() = RuntimeEnvironment.getApplication() + + private fun install(store: Store, id: String, title: String, exe: String, launcher: Boolean = false): File { + SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "") + val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.installRoot(app), store), StoreInstallRoot.folderName(title, id)) + File(folder, exe).apply { parentFile!!.mkdirs(); writeText("game") } + File(folder, "unins000.exe").writeText("uninstaller") + var sidecar = StoreGameSidecar(store, id, title, exe, args = if (launcher) listOf("-EpicPortal") else emptyList()) + if (launcher) sidecar = StoreLaunch.writeLauncher(folder, sidecar) + sidecar.write(folder) + return folder + } + + @Test fun aStoreGameIsFoundUnderTheInternalRootWithItsSource() { + val folder = install(Store.GOG, "1207658924", "Heroes of Might and Magic 3: Complete", "HOMM3.exe") + val games = AddedGames.scan(app) + val game = games.single { it.folder.absolutePath == folder.absolutePath } + assertEquals("Heroes of Might and Magic 3: Complete", game.name) + assertEquals("HOMM3.exe", game.exe.name) + assertEquals(Store.GOG.id, game.source) + assertEquals("1207658924", game.storeId) + // Inside the runtime's tree the folder needs no bind: its guest path is its path from the tree's root. + assertEquals("/root/Games/Stores/GOG/Heroes of Might and Magic 3 Complete/HOMM3.exe", game.guestExe) + assertEquals("/root/Games/Stores/GOG/Heroes of Might and Magic 3 Complete", game.guestDir) + } + + @Test fun theLauncherIsWhatTheShortcutRunsAndTheExeIsWhatItShows() { + val folder = install(Store.EPIC, "abc", "Celeste", "Celeste.exe", launcher = true) + val game = AddedGames.scan(app).single { it.folder.absolutePath == folder.absolutePath } + assertEquals("Celeste.exe", game.exe.name) + assertTrue(game.guestExe.endsWith("/" + StoreLaunch.LAUNCHER)) + assertEquals(Store.EPIC.id, game.source) + } + + @Test fun anOlderSidecarsSteamSwitchIsIgnoredEveryInstallIsListed() { + val folder = install(Store.AMAZON, "amzn1.adg.product.x", "Yakuza 0", "Yakuza0.exe") + val file = StoreGameSidecar.file(folder) + file.writeText(file.readText().replaceFirst("{", "{\"addToSteam\": false,")) + assertTrue(AddedGames.scan(app).any { it.folder.absolutePath == folder.absolutePath }) + } + + @Test fun theListingCarriesTheLauncherAndTheTitle() { + val folder = install(Store.EPIC, "abc", "Celeste", "Celeste.exe", launcher = true) + val games = AddedGames.scan(app).filter { it.folder.absolutePath == folder.absolutePath } + val listing = AddedGames.writeListing(app, games).readText() + assertTrue(listing.contains("\"name\":\"Celeste\"")) + assertTrue(listing.contains(StoreLaunch.LAUNCHER)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt new file mode 100644 index 0000000000..854c04a9c6 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudPlanTest.kt @@ -0,0 +1,66 @@ +package com.droiddeck.launcher.stores + +import com.droiddeck.launcher.stores.CloudPlan.Entry +import com.droiddeck.launcher.stores.CloudPlan.Local +import com.droiddeck.launcher.stores.CloudPlan.Remote +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** The rules that keep a cloud sync from replacing real saves. (Robolectric for org.json.) */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class CloudPlanTest { + @Test fun noUploadBeforeADownloadHasSetTheBaseline() { + val plan = CloudPlan.up(mapOf("slot0.sav" to Local("new")), mapOf("slot0.sav" to Remote("real", 1000)), null) + assertEquals("no-baseline", plan.refused) + assertTrue(plan.transfer.isEmpty()) + } + + @Test fun theFirstDownloadLetsTheCloudWin() { + val plan = CloudPlan.down( + mapOf("slot0.sav" to Local("fresh-profile"), "only-local.cfg" to Local("x")), + mapOf("slot0.sav" to Remote("real", 1000), "slot1.sav" to Remote("other", 1000)), + null, + ) + assertEquals(setOf("slot0.sav", "slot1.sav"), plan.transfer.toSet()) + assertTrue(plan.conflicts.isEmpty()) + } + + @Test fun aFileChangedOnBothSidesIsAConflictEitherWay() { + val base = mapOf("slot0.sav" to Entry(localMd5 = "a", cloudMd5 = "a", cloudModified = 1000)) + val local = mapOf("slot0.sav" to Local("b")) + val remote = mapOf("slot0.sav" to Remote("c", 5000)) + assertEquals(listOf("slot0.sav"), CloudPlan.down(local, remote, base).conflicts) + assertTrue(CloudPlan.down(local, remote, base).transfer.isEmpty()) + assertEquals(listOf("slot0.sav"), CloudPlan.up(local, remote, base).conflicts) + assertTrue(CloudPlan.up(local, remote, base).transfer.isEmpty()) + } + + @Test fun theSideThatChangedWins() { + val base = mapOf("s" to Entry("a", "a", 1000)) + // Only local changed: up sends it, down leaves it. + assertEquals(listOf("s"), CloudPlan.up(mapOf("s" to Local("b")), mapOf("s" to Remote("a", 1000)), base).transfer) + assertTrue(CloudPlan.down(mapOf("s" to Local("b")), mapOf("s" to Remote("a", 1000)), base).transfer.isEmpty()) + // Only the cloud changed: down takes it, up leaves it. + assertEquals(listOf("s"), CloudPlan.down(mapOf("s" to Local("a")), mapOf("s" to Remote("c", 9000)), base).transfer) + assertTrue(CloudPlan.up(mapOf("s" to Local("a")), mapOf("s" to Remote("c", 9000)), base).transfer.isEmpty()) + // The same bytes on both sides: nothing moves. + assertEquals(listOf("s"), CloudPlan.up(mapOf("s" to Local("c")), mapOf("s" to Remote("c", 9000)), base).same) + } + + @Test fun timesAloneDecideWhenTheServiceGivesNoHash() { + val base = mapOf("s" to Entry("a", null, 1000)) + assertEquals(listOf("s"), CloudPlan.down(mapOf("s" to Local("a")), mapOf("s" to Remote(null, 60_000)), base).transfer) + // Within two seconds of the baseline: the same cloud copy. + assertTrue(CloudPlan.down(mapOf("s" to Local("a")), mapOf("s" to Remote(null, 2500)), base).transfer.isEmpty()) + } + + @Test fun theBaselineSurvivesItsJson() { + val b = mapOf("a/b.sav" to Entry("l", "c", 5), "x" to Entry("l2", null, -1)) + assertEquals(b, CloudPlan.fromJson(CloudPlan.toJson(b))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt new file mode 100644 index 0000000000..5258c19ad5 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavePathsTest.kt @@ -0,0 +1,52 @@ +package com.droiddeck.launcher.stores + +import java.io.File +import java.nio.file.Files +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +/** A store's save-location template, made concrete inside the game's prefix (Bannerlator's cases). */ +class CloudSavePathsTest { + private val root = Files.createTempDirectory("cloud").toFile() + private val prefix = File(root, "compatdata/3785150007/pfx").apply { mkdirs() } + private val install = File(root, "Games/Stores/GOG/ELDERBORN").apply { mkdirs() } + private val profile = File(prefix, "drive_c/users/steamuser") + + @Test fun gogElderbornLivesInLocalLow() { + val dir = CloudSavePaths.gog("<?APPLICATION_DATA_LOCAL_LOW?>/Hyperstrange/ELDERBORN", prefix, install) + assertEquals(File(profile, "AppData/LocalLow/Hyperstrange/ELDERBORN").path, dir!!.path) + } + + @Test fun gogTokensAndEnvironmentStyle() { + assertEquals(File(profile, "Saved Games/X").path, CloudSavePaths.gog("<?SAVED_GAMES?>/X", prefix, install)!!.path) + assertEquals(File(profile, "Documents/My Games/Y").path, CloudSavePaths.gog("<?DOCUMENTS?>\\My Games\\Y", prefix, install)!!.path) + assertEquals(File(profile, "AppData/Roaming/Z").path, CloudSavePaths.gog("%APPDATA%/Z", prefix, install)!!.path) + assertEquals(File(install, "saves").path, CloudSavePaths.gog("<?INSTALL?>/saves", prefix, install)!!.path) + assertNull(CloudSavePaths.gog("<?SOMETHING_ELSE?>/x", prefix, install)) + assertNull(CloudSavePaths.gog("no/token", prefix, install)) + } + + @Test fun aPathThatLeavesItsBoundaryIsRefused() { + assertNull(CloudSavePaths.gog("<?SAVED_GAMES?>/../../../../outside", prefix, install)) + assertNull(CloudSavePaths.epic("{InstallDir}/../other", prefix, install, "acc", "app")) + } + + @Test fun anExistingFolderIsMatchedWhateverItsCase() { + File(profile, "AppData/LocalLow/hyperstrange/elderborn").mkdirs() + val dir = CloudSavePaths.gog("<?APPLICATION_DATA_LOCAL_LOW?>/Hyperstrange/ELDERBORN", prefix, install) + assertEquals(File(profile, "AppData/LocalLow/hyperstrange/elderborn").path, dir!!.path) + } + + @Test fun epicTokensAndPlaceholders() { + assertEquals(File(profile, "AppData/Local/Metalstorm/Saved/SaveGames").path, + CloudSavePaths.epic("{AppData}/Metalstorm/Saved/SaveGames", prefix, install, "acc", "app")!!.path) + assertEquals(File(profile, "Saved Games/Studio/acc/app").path, + CloudSavePaths.epic("{UserSavedGames}/Studio/{EpicId}/{AppName}", prefix, install, "acc", "app")!!.path) + assertEquals(File(profile, "Documents/G").path, CloudSavePaths.epic("{UserDir}/G", prefix, install, "acc", "app")!!.path) + assertNull(CloudSavePaths.epic("{Nope}/G", prefix, install, "acc", "app")) + // Epic's folder names are matched by letters and digits alone when that names exactly one. + File(profile, "AppData/Local/My-Game").mkdirs() + assertEquals(File(profile, "AppData/Local/My-Game").path, CloudSavePaths.epic("{AppData}/MyGame", prefix, install, "acc", "app")!!.path) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt new file mode 100644 index 0000000000..a85ee7c183 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/CloudSavesTest.kt @@ -0,0 +1,96 @@ +package com.droiddeck.launcher.stores + +import java.io.File +import org.junit.Assert.assertArrayEquals +import org.junit.Assert.assertEquals +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 + +/** A sync against a cloud held in memory: the baseline gate, conflicts, backups and the no-prefix deferral. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class CloudSavesTest { + private val app get() = RuntimeEnvironment.getApplication() + + private class Memory : CloudTransport { + val files = HashMap<String, Pair<ByteArray, Long>>() + val puts = ArrayList<String>() + override fun list() = files.map { (k, v) -> CloudFile(k, v.second, CloudSaves.md5(v.first)) } + override fun get(file: CloudFile) = files.getValue(file.name) + override fun put(files: Map<String, ByteArray>): Map<String, Long> = + files.mapValues { (k, v) -> puts += k; this.files[k] = v to 99_000L; 99_000L } + } + + private fun dir() = File(app.filesDir, "saves-${System.nanoTime()}").apply { mkdirs() } + private val id get() = "g${System.nanoTime()}" + + @Test fun anUploadBeforeAnyDownloadIsRefused() { + val dir = dir(); File(dir, "slot0.sav").writeText("fresh profile") + val cloud = Memory().apply { files["slot0.sav"] = "real save".toByteArray() to 1000L } + val r = CloudSaves.upWith(app, Store.GOG, id, dir, cloud, force = false) + assertEquals("skipped", r.result); assertEquals("no-baseline", r.reason) + assertArrayEquals("real save".toByteArray(), cloud.files.getValue("slot0.sav").first) + } + + @Test fun afterADownloadALocalChangeGoesUpAndTheCloudCopyIsKeptFirst() { + val game = id; val dir = dir() + val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L } + assertEquals("ok", CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false).result) + assertEquals("v1", File(dir, "slot0.sav").readText()) + File(dir, "slot0.sav").writeText("v2 played") + val r = CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false) + assertEquals(1, r.files) + assertEquals("v2 played", String(cloud.files.getValue("slot0.sav").first)) + val kept = File(app.filesDir, "stores/cloud-backups/gog-$game").listFiles { f -> f.name.startsWith("cloud-") }!!.single() + assertEquals("v1", File(kept, "slot0.sav").readText()) + } + + @Test fun aFileChangedOnBothSidesIsLeftForTheUser() { + val game = id; val dir = dir() + val cloud = Memory().apply { files["slot0.sav"] = "v1".toByteArray() to 1000L } + CloudSaves.downWith(app, Store.GOG, game, dir, cloud, force = false) + File(dir, "slot0.sav").writeText("local v2") + cloud.files["slot0.sav"] = "cloud v2".toByteArray() to 50_000L + val up = CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = false) + assertEquals(1, up.conflicts) + assertEquals("cloud v2", String(cloud.files.getValue("slot0.sav").first)) + assertEquals(listOf("slot0.sav"), CloudSaves.conflicts(app, Store.GOG, game)) + // Keep local resolves it. + CloudSaves.upWith(app, Store.GOG, game, dir, cloud, force = true) + assertEquals("local v2", String(cloud.files.getValue("slot0.sav").first)) + assertTrue(CloudSaves.conflicts(app, Store.GOG, game).isEmpty()) + } + + @Test fun aDirtyMarkStaysUntilAnUploadGoesThroughOrIsSkippedForGood() { + val gameId = "1207664643" + val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "ELDERBORN-mark").apply { mkdirs() } + File(folder, "ELDERBORN.exe").writeText("MZ") + StoreGameSidecar(Store.GOG, gameId, "ELDERBORN", "ELDERBORN.exe").write(folder) + // A save location the store gave, but no prefix yet: the upload cannot run, the mark stays. + com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_template_$gameId", "<?SAVED_GAMES?>/ELDERBORN").apply() + CloudSaves.markDirty(app, Store.GOG, gameId) + assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId)) + val first = CloudSaves.uploadDirty(app, Store.GOG, gameId, "session-end")!! + assertEquals("no-prefix", first.reason) + assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId)) + // Recovery waits while a session runs. + CloudSaves.uploadAllDirty(app, "recovery", running = true) + assertTrue(CloudSaves.dirty(app).contains(Store.GOG to gameId)) + // The store has no cloud saves for it: a final skip, the mark goes; a second trigger finds nothing. + com.droiddeck.launcher.stores.gog.GogPrefs.get(app).edit().putString("cloud_template_$gameId", "").apply() + assertEquals("no-cloud-saves", CloudSaves.uploadDirty(app, Store.GOG, gameId, "exit")!!.reason) + assertTrue(CloudSaves.dirty(app).none { it.second == gameId }) + assertEquals(null, CloudSaves.uploadDirty(app, Store.GOG, gameId, "session-end")) + } + + @Test fun aFirstLaunchWithoutAPrefixIsDeferredNotBlocked() { + val folder = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "ELDERBORN").apply { mkdirs() } + File(folder, "ELDERBORN.exe").writeText("MZ") + StoreGameSidecar(Store.GOG, "1207664643", "ELDERBORN", "ELDERBORN.exe").write(folder) + assertEquals("deferred", CloudSaves.downloadOrDefer(app, Store.GOG, "1207664643").result) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/InstalledCardsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/InstalledCardsTest.kt new file mode 100644 index 0000000000..090280f772 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/InstalledCardsTest.kt @@ -0,0 +1,28 @@ +package com.droiddeck.launcher.stores + +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Test + +/** The Installed tab draws what its count counts: the installs on disk. */ +class InstalledCardsTest { + private fun install(store: Store, id: String, title: String) = + InstalledStoreGame(StoreGameSidecar(store, id, title, "game.exe", cover = "https://img/$id.jpg"), File("/x/$id")) + + @Test fun everyInstallIsACardWhetherOrNotTheLibraryHasLoaded() { + val installed = listOf(install(Store.GOG, "1", "DOOM + DOOM II"), install(Store.GOG, "2", "DOOM I Enhanced"), install(Store.EPIC, "e", "DOOMBLADE")) + // Library not loaded: cards from the sidecars. + val bare = installedCards(Store.GOG, installed, emptyList()) + assertEquals(listOf("DOOM + DOOM II", "DOOM I Enhanced"), bare.map { it.title }) + assertEquals("https://img/1.jpg", bare.first().tallImageUrl) + // Loaded: the library item wins, by id, or by title when the ids differ. + val library = listOf( + CatalogItem(Store.GOG, "1", "DOOM + DOOM II", imageUrl = "wide1", owned = true), + CatalogItem(Store.GOG, "999", "DOOM I Enhanced", imageUrl = "wide2", owned = true), + CatalogItem(Store.GOG, "3", "Not installed", imageUrl = null, owned = true), + ) + val joined = installedCards(Store.GOG, installed, library) + assertEquals(listOf("wide1", "wide2"), joined.map { it.imageUrl }) + assertEquals(1, installedCards(Store.EPIC, installed, library).size) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/MatureContentTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/MatureContentTest.kt new file mode 100644 index 0000000000..2380b692e7 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/MatureContentTest.kt @@ -0,0 +1,31 @@ +package com.droiddeck.launcher.stores + +import com.droiddeck.launcher.stores.gog.GogStoreCatalog +import org.json.JSONObject +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.annotation.Config + +/** Mature means what the store's own ratings or tags say; a title without them stays visible. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class MatureContentTest { + private fun gog(ratings: String, tags: String) = JSONObject("""{"id":"1","title":"T","ratings":[$ratings],"tags":[$tags]}""") + + @Test fun gogRatingsAndTagsDecide() { + assertTrue(GogStoreCatalog.isMature(gog("""{"name":"esrbRating","ageRating":"17"}""", ""))) + assertTrue(GogStoreCatalog.isMature(gog("""{"name":"gogRating","ageRating":"18"}""", ""))) + assertTrue(GogStoreCatalog.isMature(gog("""{"name":"pegiRating","ageRating":"12"}""", """{"name":"NSFW","slug":"nsfw"}"""))) + assertFalse(GogStoreCatalog.isMature(gog("""{"name":"esrbRating","ageRating":"13"},{"name":"uskRating","ageRating":"16"}""", """{"name":"Puzzle","slug":"puzzle"}"""))) + // No rating data at all: visible. + assertFalse(GogStoreCatalog.isMature(JSONObject("""{"id":"2","title":"U"}"""))) + } + + @Test fun tagNamesFromAnyStore() { + assertTrue(matureByTags(listOf("Action", "Sexual Content"))) + assertFalse(matureByTags(listOf("Action", "FPS", "RELAXING"))) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreCacheTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreCacheTest.kt new file mode 100644 index 0000000000..a8e4dd20b4 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreCacheTest.kt @@ -0,0 +1,50 @@ +package com.droiddeck.launcher.stores + +import java.io.File +import java.nio.file.Files +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertSame +import org.junit.Assert.assertNotSame +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** Sizes kept per version, and a refresh that touches only the cards that changed. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class StoreCacheTest { + private val dir = Files.createTempDirectory("sizes").toFile() + + @Test fun aSizeIsKeptForItsVersionAndAskedAgainForANewOne() { + val cache = SizeCache.forStore(dir, Store.EPIC) + cache.put("Metalstorm", "1.0.3", 4_400_000_000L) + // Read back from disk by a new instance, as the next app start does. + val again = SizeCache.forStore(dir, Store.EPIC) + assertEquals(4_400_000_000L, again.get("Metalstorm", "1.0.3")) + assertNull(again.get("Metalstorm", "1.0.4")) + assertNull(again.get("Other", "1.0.3")) + } + + @Test fun aFileInAnotherFormatIsDiscarded() { + val file = File(dir, "stores/gog/sizes.json").apply { parentFile!!.mkdirs(); writeText("""{"format":0,"sizes":{"1":{"v":"a","b":5}}}""") } + assertNull(SizeCache(file).get("1", "a")) + } + + @Test fun aRefreshKeepsTheObjectsOfUnchangedCards() { + val a = CatalogItem(Store.GOG, "1", "A", imageUrl = "x", owned = true) + val b = CatalogItem(Store.GOG, "2", "B", imageUrl = "y", owned = true) + val old = listOf(a, b) + // Nothing changed: the very list shown stays. + assertSame(old, mergeItems(old, listOf(a.copy(), b.copy()))) + // One card changed: the other is the same object, the changed one is new. + val merged = mergeItems(old, listOf(a.copy(), b.copy(title = "B2"))) + assertSame(a, merged[0]) + assertNotSame(b, merged[1]) + assertEquals("B2", merged[1].title) + // A first load is the new list as it is. + val fresh = listOf(a) + assertSame(fresh, mergeItems(null, fresh)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt new file mode 100644 index 0000000000..1171bce1a7 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt @@ -0,0 +1,127 @@ +package com.droiddeck.launcher.stores + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** Under Robolectric for a real org.json; the plain unit-test android.jar stubs it out. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class StoreGameSidecarTest { + private val sample = StoreGameSidecar( + Store.EPIC, "abc123", "Celeste", "Celeste.exe", launcher = ".droiddeck-launch.bat", + args = listOf("-EpicPortal", "-epicusername=\"Some One\""), env = mapOf("FOO" to "bar"), + installVersion = "1.4.0", installedAt = 1700000000L, cover = "https://x/c.jpg", + extra = mapOf("namespace" to "ns"), + ) + + @Test fun roundTripsThroughJson() { + val back = StoreGameSidecar.parse(sample.toJson().toString())!! + assertEquals(Store.EPIC, back.store) + assertEquals("abc123", back.id) + assertEquals("Celeste", back.title) + assertEquals("Celeste.exe", back.exe) + assertEquals(".droiddeck-launch.bat", back.launcher) + assertEquals(sample.args, back.args) + assertEquals(sample.env, back.env) + assertEquals("1.4.0", back.installVersion) + assertEquals(1700000000L, back.installedAt) + assertEquals("https://x/c.jpg", back.cover) + assertEquals("ns", back.extra["namespace"]) + } + + @Test fun defaultsWhenFieldsAreAbsent() { + val back = StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"bin\\game.exe","addToSteam":false}""")!! + // An older sidecar's Steam switch is read past, not acted on. + assertEquals("bin/game.exe", back.exe) + assertNull(back.launcher) + assertTrue(back.args.isEmpty()) + // No state field = an earlier build's finished install. + assertTrue(back.isInstalled) + } + + @Test fun epicLaunchChoicesRoundTripAndDefaultOn() { + val plain = StoreGameSidecar.parse(StoreGameSidecar(Store.EPIC, "m", "Metalstorm", "M.exe").toJson().toString())!! + assertEquals(EpicOptions(eos = true, offline = false), plain.epic) + assertTrue(plain.epic.wantsCode) + val set = StoreGameSidecar(Store.EPIC, "m", "Metalstorm", "M.exe", epic = EpicOptions(eos = true, offline = true)) + val back = StoreGameSidecar.parse(set.toJson().toString())!! + assertEquals(EpicOptions(eos = true, offline = true), back.epic) + assertFalse(back.epic.wantsCode) + // A sidecar from before the choices existed reads as the defaults. + assertEquals(EpicOptions(), StoreGameSidecar.parse("""{"store":"epic","id":"m","title":"M","exe":"M.exe"}""")!!.epic) + // An earlier build's overlay field is read past. + assertEquals(EpicOptions(), StoreGameSidecar.parse("""{"store":"epic","id":"m","title":"M","exe":"M.exe","epic":{"v":2,"eos":true,"offline":false,"overlay":true}}""")!!.epic) + } + + @Test fun aSwitchOnTheEpicCardChangesTheSidecarOnDisk() { + val folder = java.io.File(org.robolectric.RuntimeEnvironment.getApplication().filesDir, "Games/Stores/Epic/Metalstorm").apply { mkdirs() } + StoreGameSidecar(Store.EPIC, "m", "Metalstorm", "Metalstorm.exe", launcher = ".droiddeck-launch.bat").write(folder) + val written = StoreGameSidecar.updateEpic(folder) { it.copy(offline = true) }!! + assertTrue(written.epic.offline) + // What the Steam-launch helper reads: the file itself. + val onDisk = org.json.JSONObject(java.io.File(folder, StoreGameSidecar.FILE_NAME).readText()) + assertTrue(onDisk.getJSONObject("epic").getBoolean("offline")) + assertTrue(onDisk.getJSONObject("epic").getBoolean("eos")) + assertFalse(onDisk.getJSONObject("epic").has("overlay")) + // Everything else in it stays. + assertEquals(".droiddeck-launch.bat", StoreGameSidecar.read(folder)!!.launcher) + assertEquals(null, StoreGameSidecar.updateEpic(java.io.File(folder, "missing")) { it }) + } + + @Test fun anInstallUnderWayHasNoExeYetAndIsNotInstalled() { + val started = StoreGameSidecar(Store.EPIC, "Metalstorm", "Metalstorm", exe = "", state = StoreGameSidecar.STATE_INSTALLING) + val back = StoreGameSidecar.parse(started.toJson().toString())!! + assertFalse(back.isInstalled) + assertEquals("", back.exe) + // A finished one must still name its exe. + assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":""}""")) + assertTrue(StoreGameSidecar.parse(back.copy(exe = "Metalstorm.exe", state = StoreGameSidecar.STATE_INSTALLED).toJson().toString())!!.isInstalled) + } + + @Test fun refusesWhatIsNotASidecar() { + assertNull(StoreGameSidecar.parse("not json")) + assertNull(StoreGameSidecar.parse("""{"store":"steam","id":"1","title":"T","exe":"a.exe"}""")) + assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"","title":"T","exe":"a.exe"}""")) + // An exe outside the folder is not trusted: a sidecar must not point the shortcut anywhere. + assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"../other/a.exe"}""")) + assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"/abs/a.exe"}""")) + assertNull(StoreGameSidecar.parse("""{"store":"gog","id":"1","title":"T","exe":"C:\\a.exe"}""")) + } + + @Test fun folderNamesAreSafeAndStable() { + assertEquals("Heroes of Might and Magic 3 Complete", StoreInstallRoot.folderName("Heroes of Might and Magic 3: Complete", "1")) + assertEquals("What Remains of Edith Finch", StoreInstallRoot.folderName("What Remains of Edith Finch...", "1")) + assertEquals("1207658924", StoreInstallRoot.folderName("日本語のみ", "1207658924")) + assertTrue(StoreInstallRoot.folderName("x".repeat(200), "1").length <= 60) + } + + @Test fun launcherStartsTheExeFromItsFolderWithArgsAndEnv() { + val text = StoreLaunch.launcherText(sample.copy(exe = "Binaries/Win64/Celeste.exe")) + assertTrue(text.contains("cd /d \"%~dp0Binaries\\Win64\"\r\n")) + assertTrue(text.contains("set \"FOO=bar\"\r\n")) + assertTrue(text.contains("\r\n\"%~dp0Binaries\\Win64\\Celeste.exe\" -EpicPortal -epicusername=\"Some One\"\r\n")) + // Epic reads the one-shot exchange code the app leaves beside the launcher, then deletes it. + assertTrue(text.contains("set /p DD_X=<\"%~dp0.droiddeck-epic-code\"")) + // The code goes on the command line only: the variable is cleared on the very line that + // starts the game (cmd has already expanded it there), so the game inherits none of it. + assertTrue(text.contains("set \"DD_X=\" & \"%~dp0Binaries\\Win64\\Celeste.exe\" -EpicPortal -epicusername=\"Some One\" -AUTH_LOGIN=unused -AUTH_PASSWORD=%DD_X% -AUTH_TYPE=exchangecode\r\n")) + assertFalse(text.contains("AUTH=")) + assertFalse(text.contains("DD_AUTH")) + } + + @Test fun gogLauncherHasNoEpicBlockAndIsOnlyWrittenWhenNeeded() { + val plain = StoreGameSidecar(Store.GOG, "1", "T", "game.exe") + val text = StoreLaunch.launcherText(plain.copy(args = listOf("-windowed"))) + assertFalse(text.contains("exchangecode")) + assertTrue(text.contains("\"%~dp0game.exe\" -windowed\r\n")) + assertFalse(text.contains("DD_X")) + assertEquals("\"two words\"", StoreLaunch.quoteArg("two words")) + assertEquals("-x=\"a b\"", StoreLaunch.quoteArg("-x=\"a b\"")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreInstallRootTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreInstallRootTest.kt new file mode 100644 index 0000000000..3856ed562d --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreInstallRootTest.kt @@ -0,0 +1,62 @@ +package com.droiddeck.launcher.stores + +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.session.SessionPrefs +import java.io.File +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 + +/** Where a store game goes: internal storage unless the user picks a card, and back to its own folder on a rerun. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class StoreInstallRootTest { + private val app get() = RuntimeEnvironment.getApplication() + + @Test fun theDefaultRootIsInternalWhateverTheSteamLibrarySettingSays() { + val card = File(app.filesDir, "fake-card/steam").apply { mkdirs() } + SessionPrefs.setGameStorage(app, card.path, "Card") + assertEquals(File(LinuxRuntime.rootDir(app), "root/Games/Stores").absolutePath, StoreInstallRoot.installRoot(app).absolutePath) + // The library's Games folder is still scanned, for installs an earlier build put there. + assertTrue(StoreInstallRoot.roots(app).any { it.absolutePath == File(card, "Games").absolutePath }) + assertFalse(StoreInstallRoot.targets(app).first().removable) + } + + @Test fun anExistingInstallWinsOverAFreshFolderUnderTheChosenRoot() { + SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "") + val existing = File(StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.GOG), "Old Name").apply { mkdirs() } + StoreGameSidecar(Store.GOG, "42", "New Name", "game.exe").write(existing) + val other = File(app.filesDir, "elsewhere/Games") + assertEquals(existing.absolutePath, StoreInstallRoot.folderFor(app, Store.GOG, "42", "New Name", other).absolutePath) + assertEquals(File(StoreInstallRoot.storeDir(other, Store.GOG), "Fresh").absolutePath, StoreInstallRoot.folderFor(app, Store.GOG, "7", "Fresh", other).absolutePath) + } + + @Test fun aFolderLeftWithoutASidecarIsAnUnfinishedInstallAndIsResumedInPlace() { + SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "") + val epic = StoreInstallRoot.storeDir(StoreInstallRoot.internalRoot(app), Store.EPIC) + val half = File(epic, StoreInstallRoot.folderName("Metalstorm", "abc")).apply { mkdirs() } + File(half, ".chunks").mkdirs() + File(half, "Metalstorm.exe").writeText("MZ") + assertFalse(StoreInstallRoot.isFinished(half)) + assertTrue(StoreInstallRoot.unfinished(app).any { it.store == Store.EPIC && it.id == null && it.folder.absolutePath == half.absolutePath }) + // Install again, even with another root picked: the same folder, no duplicate. + assertEquals(half.absolutePath, StoreInstallRoot.folderFor(app, Store.EPIC, "abc", "Metalstorm", File(app.filesDir, "card/Games")).absolutePath) + // Begun and finished: the folder becomes a game. + StoreInstalls.begin(half, Store.EPIC, "abc", "Metalstorm", null, null) + assertTrue(StoreInstallRoot.unfinished(app).any { it.id == "abc" }) + StoreGameSidecar(Store.EPIC, "abc", "Metalstorm", "Metalstorm.exe").write(half) + assertTrue(StoreInstallRoot.isFinished(half)) + assertTrue(StoreInstallRoot.unfinished(app).none { it.folder.absolutePath == half.absolutePath }) + } + + @Test fun theScratchCacheIsPrivateAndNamedSafely() { + val dir = StoreInstallRoot.scratchDir(app, Store.EPIC, "a/b:c") + assertTrue(dir.absolutePath.startsWith(app.cacheDir.absolutePath)) + assertEquals("a_b_c", dir.name) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreLogTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreLogTest.kt new file mode 100644 index 0000000000..0b72b771bc --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreLogTest.kt @@ -0,0 +1,30 @@ +package com.droiddeck.launcher.stores + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Test + +/** Store lines reach the Downloads log and logcat; nothing in them may carry a token. */ +class StoreLogTest { + @Test fun gogSecureLinkKeepsOnlyTheHost() { + val line = "gog: window 32 in_flight 3 https://gog-cdn-fastly.gog.com/token=nva=1791500000~dirs=/content-system/v2/store/1207658924~token=0a1b2c3d4e5f/content-system/v2/store/1207658924/ab/cd/abcdef 12.1 MB/s" + val out = StoreLog.redactLine(line) + assertEquals("gog: window 32 in_flight 3 https://gog-cdn-fastly.gog.com/… 12.1 MB/s", out) + assertFalse(out.contains("0a1b2c3d4e5f")) + } + + @Test fun epicAkamaiUrlLosesItsQueryAndPath() { + val line = "epic: CDN https://epicgames-download1.akamaized.net/Builds/Org/o-abc/xyz/default/ChunksV4/12/ABCD.chunk?__token__=exp=1791500000~acl=/*~hmac=deadbeef&f_token=xyz done" + val out = StoreLog.redactLine(line) + assertEquals("epic: CDN https://epicgames-download1.akamaized.net/Builds/… done", out) + } + + @Test fun tokensOutsideUrlsAndHeadersAreBlanked() { + val out = StoreLog.redactLine("refresh access_token=abc.def refresh_token: 'xyz' code=AUTH123 hdnts=exp~hmac __token__=q") + assertEquals("refresh access_token=… refresh_token: '…' code=… hdnts=… __token__=…", out) + assertEquals("Authorization: …", StoreLog.redactLine("Authorization: Bearer eyJhbGciOi")) + assertEquals("""{"access_token":"…","expires_in":7200}""", StoreLog.redactLine("""{"access_token":"eyJ.abc","expires_in":7200}""")) + // Ordinary numbers stay: "HTTP code 403" has no separator to read as a secret. + assertEquals("HTTP code 403 from https://api.gog.com/products/…", StoreLog.redactLine("HTTP code 403 from https://user:pw@api.gog.com/products/1?expand=x")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadEntryTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadEntryTest.kt new file mode 100644 index 0000000000..23c6b93db4 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadEntryTest.kt @@ -0,0 +1,31 @@ +package com.droiddeck.launcher.stores.download + +import com.droiddeck.launcher.stores.Store +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** The Downloads row reads the active stage's own progress, and which stages are behind it. */ +class DownloadEntryTest { + private val base = DownloadEntry(Store.EPIC, "doomblade", "DOOMBLADE", state = DownloadState.RUNNING) + + @Test fun downloadRunsOnBytesAndInstallOnItsOwnCount() { + val dl = base.copy(stage = DownloadStage.DOWNLOAD, bytesDone = 50, bytesTotal = 200) + assertEquals(0.25f, dl.stageFraction, 0.001f) + // Past Download the bytes no longer drive the bar; the stage's own count does. + val inst = dl.copy(stage = DownloadStage.INSTALL, stageDone = 30, stageTotal = 40, stageItems = 3, stageItemsTotal = 4) + assertEquals(0.75f, inst.stageFraction, 0.001f) + assertEquals(75, inst.percent) + // Nothing counted yet: unknown, not empty and not full. + assertEquals(-1f, base.copy(stage = DownloadStage.INSTALL).stageFraction, 0f) + } + + @Test fun passedStagesComeFromTheBitsNotTheOrder() { + // Epic checks files before it fetches: Verify is behind Download there. + val e = base.copy(stage = DownloadStage.DOWNLOAD, stagesPassed = (1 shl DownloadStage.MANIFEST.ordinal) or (1 shl DownloadStage.VERIFY.ordinal)) + assertTrue(e.passed(DownloadStage.VERIFY)) + assertFalse(e.passed(DownloadStage.INSTALL)) + assertTrue(e.copy(state = DownloadState.INSTALLED).passed(DownloadStage.INSTALL)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt new file mode 100644 index 0000000000..8bd9870ad4 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/download/DownloadNotificationTextTest.kt @@ -0,0 +1,21 @@ +package com.droiddeck.launcher.stores.download + +import com.droiddeck.launcher.stores.Store +import org.junit.Assert.assertEquals +import org.junit.Test + +/** The notification's line: what the Downloads row says, on one line. */ +class DownloadNotificationTextTest { + private val base = DownloadEntry(Store.EPIC, "m", "Metalstorm", state = DownloadState.RUNNING) + + @Test fun downloadingShowsPercentSizesSpeedAndTime() { + val d = base.copy(stage = DownloadStage.DOWNLOAD, bytesDone = 1_288_490_189, bytesTotal = 2_684_354_560, speedBps = 12_897_484, etaSeconds = 185) + assertEquals("Metalstorm · 48% · 1.2 GB/2.5 GB · 12.3 MB/s · 3 min", DownloadNotificationText.line(d, "Downloading", "Paused", "Queued")) + } + + @Test fun installShowsTheStageNotTheSpeed() { + val d = base.copy(stage = DownloadStage.INSTALL, stageDone = 42, stageTotal = 100, speedBps = 50_000_000) + assertEquals("Metalstorm · Installing 42%", DownloadNotificationText.line(d, "Installing", "Paused", "Queued")) + assertEquals("Metalstorm · Paused", DownloadNotificationText.line(d.copy(state = DownloadState.PAUSED), "Installing", "Paused", "Queued")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt new file mode 100644 index 0000000000..22b418da68 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/gog/GogCdnTest.kt @@ -0,0 +1,31 @@ +package com.droiddeck.launcher.stores.gog + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** Every CDN GOG's secure link offers is used, not only the first. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class GogCdnTest { + private val answer = """{"urls":[ + {"endpoint_name":"fastly","url_format":"https:\/\/gog-cdn-fastly.gog.com\/token=nva={expires_at}~token={token}/{path}","parameters":{"expires_at":1,"token":"t"}}, + {"endpoint_name":"akamai","url_format":"{base_url}/{path}","parameters":{"base_url":"https://gog-cdn-akamai.gog.com/s"}}, + {"endpoint_name":"lumen","url_format":"https://cdn.gog.com/{path}","parameters":{}} + ]}""" + + @Test fun allHostsAreParsedInOrderAndTakenInTurn() { + val all = GogDownloadManager.parseCdnUrls(answer) + assertEquals(3, all.size) + assertEquals("https://gog-cdn-fastly.gog.com/token=nva=1~token=t", all[0]) + assertEquals("https://gog-cdn-akamai.gog.com/s", all[1]) + assertEquals("https://cdn.gog.com", all[2]) + val set = all.joinToString("\n") + assertEquals(all[0], GogDownloadManager.firstCdn(set)) + val picks = (0 until 6).map { GogDownloadManager.pickCdn(set) }.toSet() + assertEquals(all.toSet(), picks) + assertEquals(0, GogDownloadManager.parseCdnUrls("not json").size) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt new file mode 100644 index 0000000000..1ecd195796 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt @@ -0,0 +1,18 @@ +package com.droiddeck.launcher.ui + +import com.droiddeck.launcher.stores.Store +import org.junit.Assert.assertEquals +import org.junit.Test + +/** Amazon has no catalog of its own: Installed and Library only, and Store falls back to Library. */ +class StoreTabsTest { + @Test fun storeAndLibraryAmazonLibraryOnly() { + assertEquals(listOf("store", "library"), tabsFor(Store.GOG)) + assertEquals(listOf("library"), tabsFor(Store.AMAZON)) + assertEquals("library", tabFor(Store.AMAZON, "store")) + // An old saved Installed or All tab opens Library. + assertEquals("library", tabFor(Store.GOG, "all")) + assertEquals("library", tabFor(Store.EPIC, "installed")) + assertEquals("store", tabFor(Store.GOG, "store")) + } +} diff --git a/docs/development/stores.md b/docs/development/stores.md new file mode 100644 index 0000000000..ba6963ee5c --- /dev/null +++ b/docs/development/stores.md @@ -0,0 +1,193 @@ +# Stores: GOG, Epic Games and Amazon Games in the launcher + +How the Stores section (`app/src/main/java/com/droiddeck/launcher/stores/`, `ui/Stores*.kt`) fits +the rest of the app, and the on-disk contract that makes a store game an ordinary added game. + +## Where a store game lives + +``` +<Games storage>/Games/<Store>/<title>/ + <game files> + .droiddeck-store.json the sidecar (below) + .droiddeck-launch.bat only when the game needs arguments or environment (Epic, Amazon) + .droiddeck-epic-code one-shot, written right before an Epic launch, read and deleted by the .bat +``` + +The default root is the app's internal storage - the runtime's tree, `/root/Games/Stores`, no bind +needed - whatever the session's Game storage setting says. When a card the app may write is in the +device (`GameStorage.options`), Install asks "Install to" (internal / the card, free space shown; +the last pick is only the dialog's default). A card's root is `<card app folder>/Games`; when the +card is the Steam library the library's bind (`/mnt/droiddeck-sd`) already covers it, otherwise +`SessionService` binds the root at `/mnt/droiddeck-stores/<volume uuid>`, so a shortcut's guest +path - and its appid - is the same from one session to the next. Every root is +scanned (`StoreInstallRoot.roots`): internal, every card, and the Steam library's `Games` folder for +installs an earlier build put there. `<Store>` is `GOG`, `Epic` or `Amazon`; `<title>` is the title +with unsafe characters dropped, at most 60 characters (`StoreInstallRoot.folderName`), chosen once; +a rerun (repair, update) lands on the folder whose sidecar carries the game's id, wherever it is. +An Epic install on a card keeps its in-flight chunks in `cacheDir/stores/epic/<id>/`: the +`chunkCacheDir` of both native calls (`EpicNative.run` fetches into it, `EpicNative.assemble` +writes the files from it and drops each chunk after its last use, then the folder), and of the +manager's own loops when the engine is not there; `""` keeps the cache beside the game, as an +internal install has it. The scratch folder is removed on cancel-with-delete and on uninstall. +The cache holds whole ~1 MiB chunk windows, shared with files this device does not install, so it +is often larger than the game (Metalstorm: 9.1 GB of chunks for 4.4 GB of files); the free-space +check, made after the delta pass, counts the missing chunks and the missing files on their own +volumes. A successful run removes the cache. + +## The sidecar + +```json +{ + "version": 1, + "store": "epic", // gog | epic | amazon + "id": "Samorost3", // the store's own id: GOG product id, Epic app name, Amazon product id + "title": "Samorost 3", + "exe": "Samorost3.exe", // relative to the folder, forward slashes; what the icon is read from + "launcher": ".droiddeck-launch.bat",// optional; what the shortcut runs instead of exe + "args": ["-EpicPortal", "-epicusername=\"Name\"", "..."], + "env": {"FUEL_DIR": "C:\\ProgramData\\Amazon Games Services\\Legacy"}, + "installVersion": "1.4.0", + "installedAt": 1759900000000, + "cover": "https://...", "hero": "https://...", + "extra": {"namespace": "...", "catalogItemId": "..."} +} +``` + +An install writes the sidecar at its start with `"state": "installing"` (store, id, title, no exe) +and rewrites it finished at its end (exe, launcher, no state field = installed). A folder under a +store root whose sidecar says `installing`, or that has no sidecar at all, is an unfinished +install: `AddedGames.scan` leaves it out (it is neither a game nor a Custom folder), and the Stores +card and page offer **Resume install**, which reuses that folder - and an Epic `.chunks` beside it - +instead of starting a fresh one (`StoreInstallRoot.existingFolder`). A finished install being +repaired keeps its finished sidecar. The launcher and the finished sidecar are written before the +art and the Steam registration; a failure writing them fails the download with its message. + +`StoreGameSidecar.parse` refuses anything whose `exe` or `launcher` would point outside the folder. +Every store install is a Steam shortcut; an `addToSteam` field in a sidecar from an earlier build is +read past and ignored. + +## Registration with Steam + +`AddedGames.scan` walks the store folders as it walks the user's added folders, and +`AddedGames.scanGame` reads the sidecar: the exe (the folder's guess only when that file is gone), +the title as the shortcut's name, the store as the game's `source`. From there the path is the one +an added folder takes: `session/added-games.json` → `droiddeck-steam-shortcuts` at the client's +next start. `StoreInstalls.register` rewrites the listing at install time, so a client that restarts +inside the running session already has it, and asks a running client to add the shortcut live over +its DevTools port (`SteamLiveShortcuts`: `SteamClient.Apps.AddShortcut`, our tag, the compat tool). +The appid Steam derives (CRC32 of the quoted exe plus the name, high bit set) is the one the +listing carries, so the live add and the writer never disagree. Uninstall removes the folder and the +listing drops the entry; the live client is asked to remove it too. + +`Library.SteamGame.source` is `steam`, `gog`, `epic`, `amazon` or `added`; the Games tab shows it as +a chip on every row and in the hero. + +## Launch + +A Steam shortcut names an exe and nothing more, and the listing carries no launch options, so a +game that needs arguments or environment gets `.droiddeck-launch.bat` (`StoreLaunch.launcherText`): +`cd` into the exe's folder, `set` each variable, start the exe with the arguments and wait. Proton's +`steam.exe` shim hands a `.bat` to Wine's `cmd`. For Epic the script also reads +`.droiddeck-epic-code` when present, deletes it, and passes +`-AUTH_LOGIN=unused -AUTH_PASSWORD=<code> -AUTH_TYPE=exchangecode` on the game's command line only; +the variable it read the code into is cleared on that same line, so nothing of it is left in the +game's environment. A launcher from an earlier build is rewritten before the next Epic launch. + +The code is minted for every launch, wherever it starts. The Games tab and Stores call +`StoreLaunch.prepare` before they launch; the compat tool (`droiddeck-proton`, +`droiddeck-proton-wrap`) runs `droiddeck-store-launch` before Proton on every real launch, the Steam +client's Play button included. It finds the store launcher among Steam's arguments, reads the +sidecar beside it, and for an Epic game asks the app through `<session>/stores/req` and `resp` +(`StoreLaunchRequests` → `StoreLaunch.epicCode`), waiting up to 5 s; the app writes the code into +the game's folder itself. Each attempt logs `epic launch id=… code=yes|no reason=…`, never the code. + +An Epic game's launch choices are the Epic card in the Games tab's launch settings - "Epic sign-in +(EOS)" (on) and "Launch offline" (off), stored in the sidecar's `epic` block so both launch paths +read the same thing - and "Resolve Epic sign-in", which opens Epic's account page behind its +sign-in in Android's browser. + +A game's web links reach Android: `droiddeck-store-launch` sets the prefix's +`HKCU\Software\Wine\WineBrowser` `Browsers` to `/usr/local/bin/droiddeck-open-url`, which is also +`xdg-open` first on the game's PATH, and hands http(s) addresses to the app, which opens them in +Android's browser. EOS uses this when a sign-in needs a one-time step ("corrective action"): it +falls back to a browser, the user approves on the phone, and the game signs in. `BL_DEBUG_BROWSER=1` +in droiddeck-env adds `WINEDEBUG=+shell,+winebrowser`. Epic's EOS overlay is not provisioned (with +it games exited while loading); an `OverlayPath` an earlier build wrote is removed at launch. The +parked overlay work is on the branch `park/epic-eos-overlay`. + +## Cloud saves + +GOG and Epic games sync their saves with the store's cloud (`CloudSaves`, after Bannerlator's +managers): before a launch the cloud copy comes down, after the game exits what changed goes up. +`CloudPlan` decides per file against a baseline - each file's local MD5 and cloud MD5/time as of +this device's last sync: + +- **No upload before a download has completed once on this device** (no baseline): a first launch + creates new profile files that must never replace the real saves in the cloud. +- **The first download lets the cloud win** where the two differ. +- **A file changed on both sides since the baseline is a conflict:** neither side is overwritten; + Manage saves shows "Cloud conflict" with Keep cloud / Keep local. +- Otherwise the side that changed wins; bytes that match move nothing. + +Both sides are backed up before they are replaced: the local folder before a download writes into +it, the cloud copies before an upload replaces them, under +`files/stores/cloud-backups/<store>-<id>/local-*` and `cloud-*` (the last three of each). + +A game launched before Proton has made its prefix (a first launch) gets its download deferred: the +request is answered at once (`result=deferred reason=no-prefix`) and the download runs as soon as +the prefix's user folder appears, within a minute. Every path logs +`cloud <store> <id> down|up result=ok|skipped|deferred|failed files= bytes= reason=`. + +- **The folder:** GOG's public remote-config gives a location template per game (by its Galaxy client + id), e.g. `<?APPLICATION_DATA_LOCAL_LOW?>/Hyperstrange/ELDERBORN`; Epic's catalog gives + `CloudSaveFolder`, e.g. `{AppData}/Game/Saved/SaveGames`. `CloudSavePaths` expands it inside the + game's prefix (`compatdata/<shortcut appid>/pfx/drive_c/users/steamuser`) or its install folder, + case-insensitively, and refuses a path that leaves its boundary. A game whose store gives none + has no cloud saves. +- **The services:** GOG `cloudstorage.gog.com/v1/<user>/<client>` with a token issued to the game's + own client (the client secret is kept at install; the Galaxy token is the fallback); Epic's + savesync data storage with signed read and write links. +- **Which launches:** `droiddeck-store-launch` recognises a store game by the launcher .bat Steam runs + or, for a game without one (most GOG games), by its exe - the folders above it are searched for + the sidecar. +- **When:** every launch runs `droiddeck-store-launch`, which asks the app (`cloud-down`) and waits up + to 15 s; for a store game with cloud saves it returns 10, and the compat tool then waits for + Proton instead of exec'ing it and runs `droiddeck-store-launch --exited` after, which asks for + `cloud-up` without waiting. Each sync logs `cloud <store> <id> down|up files= bytes= result=`. +- **Uploads that do not depend on one hook:** the pre-launch step marks the game dirty in the app's + storage; the upload runs on the first of the compat tool's exit request (`trigger=exit`), the + session's end (`trigger=session-end`, after teardown, reading the prefix directly) or the app's + next start for a mark a killed app left (`trigger=recovery`, not while a session runs). The mark + goes only after an upload that went through or a final skip (off, no cloud saves, no baseline, + not installed, nothing local). +- **Per game:** a GOG or Epic game's hero button reads "Cloud saves" and opens its saves view: the + Cloud saves switch (on; the sidecar's `"cloud"`, read by both launch paths), the last sync, + Upload / Download and the conflict actions, or "No cloud saves". + +## Downloads + +`DownloadQueue` runs 1–3 jobs at a time (Setup / the Downloads page), each a store's whole install +(`DownloadJob.run`), with stages Manifest → Download → Verify → Install. Pause stops the job and +keeps the files; every store's install skips complete, verified files on the rerun, so resume is a +rerun. Cancel deletes the folder. `StoreDownloadService` holds a foreground notification while +anything runs. The native engine (`libdroiddeckstores.so`, `StoresNative`, `GogNative`, `EpicNative`, +`AmazonNative`; contract in `app/src/main/rust/stores/JNI.md`) is loaded on first use; without it +each manager runs its Java fetch loop. + +## Settings + +| Setting | Pref | Where | +|---|---|---| +| Show Stores in the rail | `SessionPrefs.gameStoresEnabled` (off) | Setup › Stores | +| Open a store on: Library · Store | `SessionPrefs.storesOpenTab` (library) | the chip row's cog, Setup › Stores | +| Download speed tier | `SessionPrefs.gameStoresSpeedTier` (fast) | the cog, Setup › Stores, Downloads | +| Downloads at a time | `SessionPrefs.gameStoresParallel` (1) | Downloads | +| Install to (last pick, dialog default only) | `SessionPrefs.storesInstallTarget` | the Install dialog | + +Store log lines (the engines' included) go through `StoresState.logLine`, which redacts them +(`StoreLog.redactLine`) and writes them to logcat and to `filesDir/logs/stores/stores-<date>.log` +(`StoreLogFiles`: one file a day, seven days kept, ~2 MB each before it rolls to `.1`). Nothing +shows them in the app; the session's Share logs zip carries them under `stores/`. + +Credentials live in `filesDir/stores/<store>/credentials.json` only (`StoreAccounts`); store URLs +are logged through `StoreLog.redactUrl`, which drops the query string where the signed tokens live. diff --git a/docs/progress-log.md b/docs/progress-log.md index 6c2e6295f5..4c67247a6b 100644 --- a/docs/progress-log.md +++ b/docs/progress-log.md @@ -7,6 +7,383 @@ the timeline, then lessons and backlog. Companion to the README (what the app *d --- +## 2026-10-08 - `feat/stores`: GOG, Epic Games and Amazon Games in the launcher (in progress) + +A new **Stores** section - the three storefronts' libraries and public catalogs, one download +queue for them, and every install registered in the Steam client exactly like an added folder. +Ported from Bannerlator's store clients (auth, API, library sync, download managers); its +Activities and XML are not ported, the UI is Compose, after the approved preview. The native +download engine is the Rust side's work (`libdroiddeckstores.so`, loaded lazily; the app runs +without it and Setup says "Stores engine not built"). + +- **Where it lives.** Rail item `stores` under Games, gated by Setup › Stores › "Show Stores in + the rail" (`SessionPrefs.gameStoresEnabled`, off by default); a count badge while anything is + downloading. The page is full width: four chips (GOG · Epic · Amazon · Downloads, a signed-in + dot per store) and a cog at the row's end whose popup holds the section's own settings - which + tab a store opens on (`storesOpenTab`, Library by default; applied on chip tap, never while the + user is switching tabs by hand) and the speed tier (`gameStoresSpeedTier`, Balanced / Fast / + Max = 16 / 32 / 96 requests in flight). Setup repeats the rows under the gate. A signed-out store + shows its sign-in card first. Every install is added to Steam - the user's call: "Add to Steam + shouldn't be optional, automatic" - so the switch an earlier build had (`gameStoresAddToSteam`, + the sidecar's `addToSteam`, the page's "Add to Steam" button) is gone; an older sidecar's field + is read past and ignored, and the game page shows a passive "In Steam" chip. The Games hero's + eyebrow for a never-played store game reads "Games storage" (the chip names the store), and + "Custom game" for a hand-added folder; Steam titles keep their library label. +- **A store game is an added game.** The install lands at `<Games storage>/Games/<Store>/<title>` + (the SD library's folder when one is chosen, else the runtime's own `/root/Games/Stores`) with a + `.droiddeck-store.json` sidecar (store, id, title, exe, launcher, args, env, version, + addToSteam, art). `AddedGames.scan` now walks those folders too and `scanGame` reads the sidecar + for the exe and the name, so the game goes down the existing path: `session/added-games.json` + → `droiddeck-steam-shortcuts` at the client's next start. The listing is rewritten at install + time, and with a client running the shortcut is added live over the client's DevTools port + (`SteamClient.Apps.AddShortcut` + our tag + the compat tool; `SteamLiveShortcuts`), best-effort. + Uninstall removes the folder, the sidecar and the shortcut. +- **Launch.** A shortcut can only name an exe and the listing carries no launch options, so a game + that needs arguments or environment gets a `.droiddeck-launch.bat` beside it (Proton's steam.exe + shim hands it to Wine's cmd): Epic's identity arguments, Amazon's FuelPump variables. Epic's + per-launch exchange code is minted by the app right before a launch into `.droiddeck-epic-code`, + which the script reads once and deletes; with no code the game starts in its offline identity + mode. The EOS overlay is never provisioned. +- **Games tab badges.** `Library.SteamGame.source` (steam / gog / epic / amazon / added, from the + sidecar) with a coloured chip on every row and in the hero eyebrow; `LibraryCache` keeps it. +- **Downloads.** `DownloadQueue` runs 1-3 at a time, stages Manifest → Download → Verify → + Install, pause = stop and keep the files (every engine skips verified files on the rerun), + cancel deletes; `StoreDownloadService` keeps the process alive meanwhile; the page shows speed, + ETA, the tier, downloads-at-a-time and the engine log. +- Tests: `StoreGameSidecarTest`, `AddedGamesStoreScanTest` (JVM), `tools/tests/test_store_shortcuts.py` + (the writer with a launcher .bat as Exe, the appid the app computes, removal). +- Files: `stores/*` (Store, StoreGameSidecar, StoreInstallRoot, StoreInstalls, StoreLaunch, + StoreAccounts, StoresState, StoresNative, SteamLiveShortcuts, download/*), `ui/StoresPage.kt`, + `ui/StoresDetail.kt`, `ui/StoresDownloads.kt`, `ui/StoreChips.kt`, `ui/StoresWidgets.kt`; + `frontend/AddedGames.kt`, `frontend/Library.kt`, `frontend/LibraryCache.kt`, + `ui/FrontEndRail.kt`, `ui/FrontEndContent.kt`, `ui/FrontEndSetup.kt`, `ui/FrontEndGames.kt`, + `ui/FrontEndScreen.kt`, `MainActivity.kt`, `session/SessionPrefs.kt`, `AndroidManifest.xml` + (the download service), `res/values/strings.xml`. +- **The three stores** (`stores/gog`, `stores/epic`, `stores/amazon`), ported from Bannerlator's + clients with their storage replaced: GOG's OAuth page (Galaxy identity, a browser identity for + the social-login hop), library from `embed.gog.com` + `api.gog.com`, shelves from + `catalog.gog.com`, gen2 chunks / gen1 ranges / plain installer; Epic's web login (the code off + the redirect's JSON page), library service + catalog, the store's GraphQL and the free-games + feed, ChunksV4 manifests with install tags and a delta pass, the manifest's launch exe; Amazon's + PKCE device sign-in, entitlements, `manifest.proto` (the app's xz library), SHA-256 per file, + `fuel.json` / exe scoring, FuelPump variables; Amazon has no public catalog, so its Store tab is + cut from the account's own games. Each manager hands the byte-fetch loop to the native engine + when `libdroiddeckstores.so` is there (`GogNative` / `EpicNative` / `AmazonNative`, JNI.md's + names and signatures) and runs its Java pool otherwise. +- Left out on purpose: GOG DLC installs and GOG redistributable installs (Steam seeds the shared + redistributables in the prefix as for any shortcut), Epic's Denuvo ownership token and overlay, + Amazon's SDK DLL deployment into the prefix (the prefix does not exist before the first launch), + cloud saves, and the stores' social tabs - none of them in the preview. +- **Card art (device feedback on `61a9a48`, GOG › Library washed out to white):** the GOG library + had drawn the product's `images.background` - the store page's fade-out backdrop, white once its + alpha is gone. The library now takes `api.gog.com/v2/games/<id>` `_links.galaxyBackgroundImage` + (the Galaxy client's dark 16:9 library art) and `boxArtImage` (the vertical cover), cached with + the sync (cache key bumped so old caches re-fetch); the catalog's `productId` filter was tried + and ignored by the service, so it is one call per owned game. Epic cards prefer `DieselGameBox` / + `DieselGameBoxTall` over the offer images; Amazon prefers `backgroundUrl2` and never the logo. + Cards draw over a dark surface with a fade into the card at the foot. On install `StoreArt` + places `cover.jpg`, `hero.jpg` and `header.jpg` in the game folder, re-encoded as opaque JPEGs + over a dark ground, under the names `AddedGameArt` looks for - so the Games tab and the client's + grid (through the listing) get real art for store games. +- **Device feedback on `9f5a2f3` (GOG › DOOM I Enhanced page):** (1) the pad lost focus when a + card opened its page (the card left composition; the next press landed on the rail). The page + now follows the Flathub store's pattern: a focus move is recorded on each change and + `focusWithinFrames` lands it - the game page's main action (or its back link), the card the page + was opened from on the way back, the first card after a tab or chip change (`firstTile`); the + page is a `focusGroup`, so Left is the way to the rail. (2) GOG's account endpoints return + template keys (`product_description_2015545325<br>product_feature_…`) for some products; the + v2 record has the same keys for them. `cleanStoreText` drops those keys, strips tags, decodes + entities; the library tries the product's lead, then v2 `overview`, then v2 `description`, and + shows nothing when all are templates. Epic's and Amazon's text goes through the same cleaner. +- **Speed shown = install rate.** Device evidence (Epic › Metalstorm, 1.68 GB on Max to the SD + card): the engine's windows burst at 0.4–20 MB/s with 0–3 of 20 requests in flight while the card + writes at 26 MB/s - the fetchers wait on the write side, so the engine's `speedBps` jumped while + the bar advanced at the write rate. `DownloadQueue` now measures the shown speed itself from the + byte deltas (an exponential average over 3 s, sampled at most every 250 ms) and derives the ETA + from it, for all three stores; the engine's own figure stays in its log lines, and the detail + line under the bar has it stripped. The tier and downloads-at-a-time controls sit on one compact + row, on the Downloads page and in the cog's popup. +- **Install location (user decision):** the default is the app's internal storage for all three + stores, whatever the session's Game storage says. With a card in the device, Install asks + "Install to" first (internal / the card, free space per target, pad-focusable, last pick as the + default only). A card that is not the Steam library gets a bind of its own in the session + (`/mnt/droiddeck-stores/<volume uuid>`; the library's own bind covers a library card, so installs + already registered there keep their appid); every root is scanned and uninstall works for + either. The Downloads + row shows where a game is going; the game page names the + place. Epic installs on a card keep their in-flight chunks in the app's cache: the engine's + `chunkCacheDir` (fetch and the new `nativeAssemble`, which drops each chunk after its last use) + and the manager's own loops read the same path; internal installs pass `""` as before. Cleaned on + cancel-with-delete and uninstall. +- **Half-installed Epic game shown as Custom (device, Metalstorm on the SD card).** The folder had + 2.85 of 4.4 GB of files, a 0-byte file where the assembly stopped (19:15:10, four minutes after it + began), 9.1 GB of `.chunks`, and no sidecar. The run stopped inside the assembly loop - before the + `.chunks` removal and the sidecar, both of which only ran at the very end; the events log does + not reach back far enough to say whether the process died or the loop threw. Either way three + things made it silent: the sidecar was only written last, the queue lives in memory (a process + death drops the row), and `AddedGames.scan` listed any sidecar-less folder under a store root as + a Custom game. Now: the sidecar is written at the start in state `installing` and finished at the + end; unfinished folders (installing, or no sidecar) are never scanned as games; the Stores card + reads **Resume install**, which reuses the folder and its `.chunks`; launcher/sidecar write + failures fail the download, art and Steam registration are best-effort. The 9.1 GB cache is + whole 1 MiB chunk windows shared with files outside this device's install tags - the free-space + check now counts cache and files; a successful run removes the cache (Kotlin loop and + `nativeAssemble` both). +- **The cog popup is capped at 440 dp**, like the Install dialog. +- **Store cards are art + one line** (maintainer feedback): no source chip (the tab says the + store), no "In library" chip, no action button. The title sits left; right-aligned is a check + when installed, Resume, the download percentage, the install size, or Free / price / discount. + A thin bar over the art while downloading. Install, Resume, Uninstall, Get and Buy live on the + game page; A on a card opens it. +- **No log panel:** the Downloads page's engine log is gone. Store lines go to logcat and to + app-private daily files (`filesDir/logs/stores/stores-<date>.log`, seven days, ~2 MB rolling to + `.1`), pruned as the app starts; the session's Share logs zip includes them under `stores/`. +- **Store log lines are redacted at the one choke point** (`StoresState.logLine`, which the engines' + lines reach too): every URL is cut to scheme, host and a plain first path segment (a GOG secure + link's token sits in the path, so it goes), no query, fragment or userinfo; token-like values + (`token`, `__token__`, `f_token`, `hdnts`, access/refresh/id tokens, `code=`) and Authorization / + Cookie headers are blanked outside URLs too. `StoreLogTest` covers a GOG secure link and an Epic + Akamai URL. The engine's own logcat lines (tag `EpicNative` etc.) are written on the native side. +- **GOG downloads use every CDN host:** the secure link lists several hosts; only the first was + taken (`used=1/1 servers`). All are parsed now and kept as one set, refreshed together on + expiry; the Java pool takes them in turn. The progress bars glide to each new value over 250 ms. With the engine's `0849a7c` the + native start takes the whole set (`cdnBases`; the engine spreads its window across the hosts) and + its `onBytes` - every 250 ms, including files still in flight - drives the bar and the speed, so a + large file no longer moves the bar in one jump. +- **Cloud saves device-proven (AYANEO Pocket FIT, `6a175f1`, ELDERBORN on GOG):** the launch logged + `cloud gog 1732383191 down result=ok files=5 bytes=237499 reason=downloaded` (template found) and + the in-game slot shows the cloud save (Catacombs: Tutorial part 1, 00:08:40). +- **Store cards a quarter smaller:** 112 dp least width (was 150) for shelf cards and grid cells; the + grid fits as many columns as the width allows and shares the rest, so a density setting changes + the column count; titles stay at 11 sp, art 16:9. Downloads rows unchanged. +- **Cache first, refreshed in the background:** a store's library shows from its saved copy at once + and is refreshed at most every six hours (or from the account row's Refresh), the result merged + so unchanged cards keep their objects - only changed cards recompose, the grid's scroll and the + pad's focus stay. Shelves saved on an earlier run count as fresh for three hours. Sizes are kept + per game and version in `filesDir/stores/<store>/sizes.json` (format-versioned) and looked up + again only for a new version. An installed card shows its size on disk with a small green check + after it. +- **Every owned card shows its size** at the right of its title strip, in the title's type: GOG's from + its catalog; Epic's (build manifest, the files this device installs) and Amazon's (download + manifest) looked up once a card is on screen or close, three at a time in the background, kept in + the store's prefs (`size_<id>`). Nothing shows until it is known; a marker (installed, Resume, a + download's percent) keeps the slot. +- **Two tabs, Store and Library** (the All count mixed owned games with whatever shelves had loaded: + GOG Library 33 / All 120): Library's dropdown filters All (every owned game, the default, the + tab's count) or Installed, and the tab reads the filter ("Library (33)", "Installed (1)"); the old + All view is gone. Amazon has Library alone. Search follows the filter; the shoulders cycle Store + and Library. The dropdown opens under the Library tab, left edges aligned, as wide as its + entries (at least the tab, at most 260 dp) - `AnchoredMenu(compact = true)`. +- **The Stores chip row paints nothing** (maintainer: a black box behind the chips): it sits over the + page's own background; the content scrolling under it is clipped at its edge and faded there by + the content's alpha once scrolled, never by a colour. +- **Pre-PR device checks pass (AYANEO Pocket FIT):** GOG install, launch and cloud saves down and up + (the session-end upload included); Epic with EOS sign-in; Amazon (Dread Templar); an SD-card install + (DOOM I Enhanced) launched from `/mnt/droiddeck-sd`. +- **Downloads page, icons and one Clear** (maintainer): the row actions are round icon buttons - + pause, resume, play, retry, and a cancel that asks with a red delete and an undo - their labels as + descriptions. Downloads under way sit on top; finished ones below a line whose X clears them all + (rows only; a failed download's kept files stay for its game page's Clear), installed ones as a + compact row with Play. +- **Saves menu without explanations:** Import / Export rows (Steam, Custom, GOG and Epic alike) lose + their descriptive subtitles; only live values remain (last sync, "…", conflict count, the save + folder and its size). The three strings are gone from every locale. +- **One place for a store game's saves:** a GOG or Epic game's hero button reads "Cloud saves" and + opens its saves view, which now holds the Cloud saves switch, the sync status, Upload / Download + and the conflict actions ("No cloud saves" when the store keeps none). The switch is gone from the + cards; GOG has no card left, Epic's keeps sign-in, offline and Resolve Epic sign-in. Steam and + Custom games keep "Manage saves". The view is laid out in full from the first frame - the switch (focused), + Upload and Download with "…" until the cloud check is back - and only subtitles change after; a + conflict turns the two actions into Keep local / Keep cloud in place. +- **Background downloads:** while a download runs the service holds a Wi-Fi lock and a partial wake + lock (timed, renewed with the progress, released when nothing runs). The notification shows a + real progress bar and "<game> · 42% · 1.2 GB/2.5 GB · 12.3 MB/s · 3 min" (the stage while + installing), with Pause / Resume and a two-step Cancel (Delete / Keep, ten seconds); a paused + download leaves a notice to resume from; "<game> installed" opens it in Games, "<game> download + failed" opens Downloads. Notifications are asked for once on Android 13+. The Android 15 dataSync + time limit (and its `onTimeout`) applies only to apps targeting 35+: this app targets 28 and + compiles against 34, so there is nothing to handle yet. +- **Cloud uploads do not depend on the exit hook:** a dirty mark per game launch; uploaded on the + first of exit request, session end, or the next app start (`trigger=exit|session-end|recovery`). +- **Cloud saves, device round 1 (ELDERBORN, `b323a0b`): nothing ran** - a GOG game without arguments + gets no launcher .bat, Steam runs its exe, and `droiddeck-store-launch` only recognised the .bat. + It now recognises the exe too (searching the folders above it for the sidecar), and every path + logs a result line. A first launch has no prefix yet: the download is deferred until Proton makes + it, without holding the launch. Uploads are now safe: none before a download has set this + device's baseline, a file changed on both sides is a conflict left for Keep cloud / Keep local, + and the cloud copy is backed up before an upload replaces it (`CloudPlan`, tests for each rule). +- **GOG and Epic cloud saves** (ported from Bannerlator's managers, device-proven there in August): + down before every launch where the cloud copy is newer (the compat tool waits up to 15 s), up + after the game exits (the compat tool waits for Proton for such a game, then asks without + waiting), newest wins per file with an MD5 check, a backup of what a download overwrites (last + three). The save folder comes from GOG's remote-config template or Epic's CloudSaveFolder, + expanded inside the game's prefix (`CloudSavePaths`, Bannerlator's cases as tests). A "Cloud + saves" switch on the GOG / Epic card, and cloud rows in Manage saves (last sync, Upload, + Download, "No cloud saves"). Not yet device-tested. +- **Epic sign-in device-proven without the overlay (AYANEO Pocket FIT, `bf81104`, Metalstorm):** EOS's + corrective-action browser flow → `droiddeck xdg-open: https://www.epicgames.com` → Chrome + `epicgames.com/id/authorize?user_code=…` → approved → `[eos] got logged in`, product user id + created, the game in its hangar at 92 fps. The EOS overlay crashed the game on every overlay-on + launch, so it is removed (user decision): no overlay switch, no download, no `OverlayPath`; a + launch removes a pointer an earlier build wrote, and the app deletes an earlier 656 MB download + once. The work is parked on `park/epic-eos-overlay` (at `bf81104`). Kept: the browser hand-off, + the Epic card (sign-in, offline, Resolve Epic sign-in) and `BL_DEBUG_BROWSER`. +- **A game launched from the Games tab with no session running sat at "Launching"** (device, + 2026-10-09-04-steam, and 2026-10-08-05 before it). The session put `steam://rungameid/<id>` on + the client's command line; the client took it seconds before its interface was up (focus went to + 769 at 04:29:07, the launch at 04:29:09) and its launch stopped at `LaunchApp waiting for user + response to ShowInterstitials` with nothing to answer - the next launch from inside Steam went + straight through. Not store-specific: the 2026-10-08 cold launch of another shortcut stopped the + same way. `droiddeck-session` now holds such a URL back and hands it over through the same + `steam-game` request an in-session launch uses, once gamescope's focused app is the interface + (769) and four seconds have passed, or after two minutes regardless. The `rc=139` at the end of + both sessions is the client crashing in its own shutdown, after "Shutdown", unrelated to the + launch. The shortcuts writer keeps a compat tool the user chose (`plan_mapping` skips an entry + that is not one it set itself), and the live add sets one only for a new shortcut. +- **Why EOS's browser flow never reached Android:** the hand-off script was never in the APK. The + build copies only `usr/local/bin/droiddeck-*`, `steam-compatibility` and `usr/bin/**` of the overlay + into the assets, so `usr/local/lib/droiddeck/browser/xdg-open` was skipped, the session staged + nothing, the wrapper did not put it on the PATH, and Wine's winebrowser ran the runtime's own + `/usr/bin/xdg-open`, which has no browser to open in a Steam session. GE-Proton 11-7's winebrowser + (Metalstorm's tool) is Wine's own - registry `Browsers` list, then `xdg-open` by name - with no + steam://openurl route. The script is now `usr/local/bin/droiddeck-open-url`: named as Wine's + browser in the prefix (`"Browsers"="/usr/local/bin/droiddeck-open-url,xdg-open"`, an absolute path + that does not depend on Wine's PATH), linked as `xdg-open` first on the game's PATH, and reading + the session channel from `/tmp/droiddeck-browser/launch-dir` when Wine does not pass + `BL_LAUNCH_DIR` on. Whether EOS calls ShellExecute at all in its browser flow (rather than handing + a verification URI and code to the game to show) is what the next run's `droiddeck xdg-open:` line, + or `BL_DEBUG_BROWSER=1` in droiddeck-env (`WINEDEBUG=+shell,+winebrowser`), will show. +- **Resolve Epic sign-in** (the Epic card): opens Epic's account page behind its sign-in in Android's + browser, where a pending corrective action (privacy policy, EULA) is presented. +- **Epic overlay off by default** (device A/B, Metalstorm: overlay pointer set → exits while + loading, twice; off → signs in to its Starform account and runs at 118 fps). `EpicOptions.overlay` + defaults to false; the sidecar's `epic` block carries `"v": 2`, and an older block (the old default + wrote `"overlay": true` unasked) reads as off - in the app and in `droiddeck-store-launch` alike. + The 656 MB overlay is fetched only for a game that turns it on (the card's switch, or a launch of + such a game), no longer after every library sync. +- **The card's switches are saved where both launch paths read them:** each toggle reads the + sidecar fresh from disk, writes it and reads it back (`StoreGameSidecar.updateEpic`), and the card + shows what was read back; the app logs `epic options <id> eos= offline= overlay=`. +- **Registry lines that a running wineserver would drop:** `droiddeck-store-launch` waits up to 8 s + for a wineserver still holding the prefix to exit before appending to `user.reg` (it rewrites the + file from memory when it exits, which is how `OverlayPath` vanished), reads the value back, and + logs `overlay-set`, `overlay-off`, `overlay-not-written` or `overlay-still-set`, plus + `server-running` when one never left. The helper's docstring is raw (no SyntaxWarning). +- **Browser hand-off diagnostics:** the game-PATH `xdg-open` logs every call as `scheme://host` + only and accepts `steam://openurl/<address>`. On the device it was never called while EOS waited + on its browser flow, so how EOS opens the browser under this Proton is still to be seen in the + next log. +- **The exchange code no longer reaches the game's environment** (device: Metalstorm's Player.log + dumped `DD_AUTH: … -AUTH_PASSWORD=<code>`). The launcher read the code into a variable the game + inherited. It now clears the variable on the same line that starts the game - cmd has expanded it + there already - so the code is on the command line only; the variable has a plain name. An old + launcher is rewritten before the next Epic launch (`StoreLaunch.refreshLauncher`, from both the + app and the Steam-launch request). +- **A store game page shows no description that is only its title again** (Metalstorm's). +- **Epic launch card** (Games tab › Launch settings, Epic games only): "Epic sign-in (EOS)" on, + "Launch offline" off, "Epic overlay" on, stored in the game's sidecar (`"epic": {"eos", "offline", + "overlay"}`, kept across updates) so both launch paths honour them alike - the app's + `StoreLaunch.epicCode` and `droiddeck-store-launch` for a launch from the Steam client: no code + when sign-in is off or offline, the overlay pointer written or removed per the switch. The card + reads e.g. "EOS · Overlay". Bannerlator's forced ownership token (`-epicovt`) is not ported, so + there is no switch for it. +- **EOS overlay** (device: Metalstorm's EOS grant answered `corrective_action_required`, the overlay + was not configured, and EOS fell back to a browser flow that never finished). Epic's own overlay + component ("EpicOnlineServicesOverlay", the mechanism from Legendary's `lfs/eos.py`, credits in + `EpicOverlay.kt`) is downloaded once, with the Epic account's token, to + `/root/.local/share/droiddeck/epic-overlay` in the runtime - after an Epic library sync, or at the + first launch that finds it missing. `droiddeck-store-launch` then writes the one pointer, + `HKCU\Software\Epic Games\EOS` `OverlayPath` = that folder through Z:, into the game's prefix + (`STEAM_COMPAT_DATA_PATH/pfx/user.reg`, appended as DirectAudio's key is; a prefix Proton has not + created yet gets it on the next launch). Logged as `epic overlay installed=<build> prefix=<appid>`. + A game's web links now open on Android: Wine's browser is set to xdg-open, and on a game's PATH + `droiddeck-open-url` hands http(s) addresses to the app, which opens them + with Android's browser - so a device-code sign-in can be finished on the phone. +- **Epic sign-in from Steam's own Play button (device, Metalstorm "Guest Account"):** the game was + started from the Steam client, so the app never minted its exchange code - the code was only + written when a launch began in the app. Now the compat tool (`droiddeck-proton` and + `droiddeck-proton-wrap`, `steam-compatibility`) runs `droiddeck-store-launch` on every real launch: + it recognises the store launcher among Steam's arguments, and for an Epic game asks the app + through `<session>/stores/req|resp` and waits up to 5 s; the app (`StoreLaunchRequests` → + `StoreLaunch.epicCode`) refreshes the token, mints the code and writes it into the game's own + folder - the code never crosses the channel or the log. No answer: the game starts with its + offline identity. A code file older than five minutes is dropped first. Every attempt logs + `epic launch id=<app> code=yes|no reason=<ok|signed-out|sign-in-expired|exchange-failed|timeout|…>`; + a sign-in that ran out dims the Epic chip and shows the sign-in card. The Games tab and Stores + still prepare a code before they launch; the compat tool's request is the one every path shares. +- **Amazon has Installed and Library only:** with no public catalog its Store tab repeated the + library (Trending = Your library) and All equalled Library. The open-on Store choice falls back to + Library for Amazon. Amazon's library API carries no install size, so a size shows only once a + game has been installed (the manifest gives it then); no placeholder otherwise. +- **Shelves are titles only** (no "Just landed" / "On sale this week" lines); the chip row sits on + the page's own ground with room under it, and what scrolls passes beneath it clipped, with a short + fade where it meets the row. +- **Show mature content** (Setup › Stores and the cog; off by default): the storefront's shelves + and search leave out titles the store itself rates or tags as adult; Library, Installed and owned + games always show, and a title without rating data stays. GOG: `ratings[].ageRating` 17+ (ESRB M + 17, PEGI / USK / GOG 18) or the tags `mature`, `nsfw`, `sexual-content`, `nudity` - the catalog + API has no server-side filter for either, so it is client-side. Epic: its public store GraphQL + carries no age rating on an offer (no age-gating or rating field on CatalogOffer or StoreConfig; + introspection is off), so only an adult content tag would mark one, and none of the probed + titles have one - Epic's storefront is effectively unfiltered. Amazon's Store tab is the user's + own games. +- **UI slow while downloading (device):** engines report per chunk or file, and each report + published a new queue that the whole front end read (the rail badge was computed from it in the + root composition). Now progress-only changes reach the UI at most four times a second (stage and + state changes at once), each row and card reads only its own entry, the list replaces itself only + when rows come, go or change state, the badge count changes only when it changes, and the + notification redraws at most once a second. Download threads run at background priority (the + queue's runner and every Java pool), the tier leaves two cores free (`processWorkers` capped at + cores - 2, at least 2), and the stores log is written in half-second batches. The native engine's + own threads need the same priority on its side. +- **Installed tab empty under "Installed (2)" (device):** the count came from the installs on disk, + the grid from the library filtered by id. Both now come from `installedCards`: each install joined + to its library item by id (or title), else a card from the sidecar's title and art. +- **GOG rows (device, DOOM + DOOM II):** bytes read "0 B" instead of blank; GOG and Amazon report + their size on disk from the manifest; their Install stage counts its finishing steps (record and + launcher, art, Steam) instead of sitting empty - GOG writes files in place while downloading, so + there is no file move to count. A stage a store skips reads complete once a later one starts. + The tier's window does reach GOG's engine (Max = 96 as its ceiling); the plan line now says so + (`gog: plan … window=96 … tier=max`). The per-host spread is the engine's and was not changed. +- **Stages named for what they do; both sizes; Cancel deletes.** Epic's check of the files already + there is part of Manifest ("Checking 120/515"), never a 100% Verify before anything is fetched. + Install has its own write rate and ETA from the bytes the assembler writes, and files n/N. The + row shows the download (compressed) as "1.6 GB / 2.5 GB" and the size on disk beside the + location; Epic remembers the disk size for the card and page. Cancel asks twice ("Delete + download?") and removes everything the install wrote - folder, sidecar, `.chunks`, scratch, + `.gog_chunks` - except a finished install being repaired, which keeps itself; the row says + Cancelled for three seconds, then goes, and the game reads Install. Pause keeps everything. A + failed row reads "Failed · 2.1 GB kept" (the reason is in the log) with Resume and Clear (Clear + deletes like Cancel); the game page offers Resume install and Clear for an unfinished install. + Library sync is a thin bar and "25/55", no sentence. +- **Per-stage progress** (device: Epic DOOMBLADE, 13 GB, ~3 min of assembly with a still bar). The + entry carries the active stage's own count (amount + files) and which stages are passed; the + Downloads row draws one segment per stage (Manifest, Download, Verify, Install), and its line + reads e.g. "Installing 42% · 120/515 files" (speed and ETA only during Download). Fed by Epic's + verify pass, its native and fallback assembly, and GOG's chunk-cache removal; Amazon writes while + it downloads and has no long stage after it. Leaving Download clears the measured speed. Cards + and the game page follow the active stage's percent. +- **The art is the card** (maintainer feedback): a store card is its art edge to edge, sharp above + and, under the one line, a blurred and darkened copy of the same picture (blur on API 31+, a + stronger scrim below it). A Downloads row is the game's wide art cropped to the row with a dark + wash from the left, the copy in white there and the actions on the right; no thumbnail. +- **Install dialog:** one plain line "Install <game>:", then one row per place (icon, name, free + space), Cancel small in the title line; 440 dp wide. +- **No flavor text** (standing rule from the maintainer): Setup › Stores and the cog popup keep + only control labels; the download-controls caption, the Downloads footnote, the sign-in blurbs + and footnote, the engine note and the Install-to caption are gone; empty states read "No games", + "Nothing installed", "No downloads", "No catalog". A Custom game's hero has no eyebrow (its chip + says it). +- **Logos reverted** (user decision, no third-party marks): back to the text source chips and the + coloured dots. +- **Device-proven on the AYANEO Pocket FIT, 2026-10-08 (GOG):** sign-in, the library (33 owned), + DOOM I Enhanced installed through the GOG engine to the SD Games root, shown in Games with the + GOG chip and real art, registered as a non-Steam game in the Steam client (grid art present), and + launched from the Games tab through the shortcut and droiddeck-proton to the main menu at 143 fps. +- Status: CI green (build, JVM and python tests). Epic and Amazon are still unproven on device - + their sign-in pages, an install, and for Epic the launcher .bat with the exchange code are the + next things to prove on hardware. + ## 2026-10-08 - `feat/directaudio-from-release`: DirectAudio from its own release, picked by interface, and the client gets the real engine Three things were wrong with audio at once, and one tidy-up @xXJSONDeruloXx asked for. diff --git a/tools/build_local.sh b/tools/build_local.sh index c446de5ade..cb567da7e9 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -321,8 +321,29 @@ tar -cf - -C "${bundle_dir}" . | zstd -19 -T0 -c > "${staging_dir}/pulseaudio.tz bundle_replaced=1 mv "${staging_dir}/pulseaudio.tzst" "${bundle_asset}" +# The GOG / Epic / Amazon download engines are Rust (app/src/main/rust/stores); Gradle's +# buildRustStores task cross-compiles them with cargo-ndk, which needs the Android target and the +# cargo-ndk binary installed once. DROIDDECK_SKIP_RUST=1 builds an apk without the engines (the +# stores then report themselves unavailable) for a machine without a Rust toolchain. +gradle_rust_arg="" +if [[ "${DROIDDECK_SKIP_RUST:-0}" == 1 ]]; then + gradle_rust_arg="-PskipRust=true" +else + export PATH="${HOME}/.cargo/bin:${PATH}" + for tool in rustup cargo; do + if ! command -v "${tool}" >/dev/null 2>&1; then + echo "${tool} is required for the store engines (https://rustup.rs); set DROIDDECK_SKIP_RUST=1 to build without them." >&2 + exit 1 + fi + done + rustup target add aarch64-linux-android + if ! command -v cargo-ndk >/dev/null 2>&1; then + cargo install cargo-ndk --locked + fi +fi + cd "${repo_root}" -./gradlew "${gradle_task}" --console=plain -PndkVersion="${ndk_version}" +./gradlew "${gradle_task}" --console=plain -PndkVersion="${ndk_version}" ${gradle_rust_arg} python3 tools/release/check_session_assets.py "app/build/outputs/apk/${build_variant}/app-${build_variant}.apk" cp -p "${bundle_backup}" "${bundle_asset}" bundle_replaced=0 diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-open-url b/tools/linuxfs/overlay/usr/local/bin/droiddeck-open-url new file mode 100755 index 0000000000..69d5c65aa5 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-open-url @@ -0,0 +1,61 @@ +#!/usr/bin/python3 +"""A game's web links to Android: Wine's winebrowser starts this (named as the browser in the +prefix's HKCU\\Software\\Wine\\WineBrowser, and as xdg-open first on a game's PATH), and an +http(s) address goes to the app, which opens it in Android's browser - so an EOS sign-in step or a +store page can be finished on the phone. Anything else goes on to the system's own xdg-open. + +The session channel is $BL_LAUNCH_DIR/stores; Wine may not pass that variable on to what it +starts, so droiddeck-proton also leaves it in LAUNCH_DIR_FILE.""" + +import json +import os +LAUNCH_DIR_FILE = "/tmp/droiddeck-browser/launch-dir" +import sys +import time +from pathlib import Path + + +def hand_over(url, launch_dir): + req = Path(launch_dir) / "stores" / "req" + req.mkdir(parents=True, exist_ok=True) + rid = "url-%d-%d" % (os.getpid(), int(time.time() * 1000)) + tmp = req / (rid + ".json.tmp") + tmp.write_text(json.dumps({"op": "open-url", "url": url})) + os.rename(tmp, req / (rid + ".json")) + + +def where(url): + """scheme://host only, for the log: no path, no query.""" + from urllib.parse import urlsplit + try: + u = urlsplit(url) + return "%s://%s" % (u.scheme, u.hostname or "") + except ValueError: + return "?" + + +def main(argv): + url = argv[0] if argv else "" + # The Steam client's own form (steam://openurl/<address>), in case a launcher passes it on. + if url.startswith("steam://openurl/"): + url = url[len("steam://openurl/"):] + launch_dir = os.environ.get("BL_LAUNCH_DIR", "") + if not launch_dir: + try: + launch_dir = open(LAUNCH_DIR_FILE).read().strip() + except OSError: + launch_dir = "" + print("droiddeck xdg-open: %s" % where(url), file=sys.stderr, flush=True) + if url.startswith(("https://", "http://")) and launch_dir: + hand_over(url, launch_dir) + return 0 + here = os.path.dirname(os.path.realpath(sys.argv[0])) + for d in os.environ.get("PATH", "").split(os.pathsep): + candidate = os.path.join(d, "xdg-open") + if os.access(candidate, os.X_OK) and os.path.realpath(candidate) != os.path.realpath(__file__) and os.path.abspath(d) != here: + os.execv(candidate, [candidate] + argv) + return 1 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 6c9a8b12b2..9a51694df6 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -606,6 +606,34 @@ case $mode in fi done ) & + # A game the app asked to launch on a cold start is not put on the client's command line: the + # client takes it before its interface is up, its launch stops at "waiting for user response to + # ShowInterstitials" with nobody to answer, and the game page reads Launching for ever (device, + # 2026-10-09, and 2026-10-08 before it). It is handed over once the interface has focus + # (gamescope's focused app 769) and has had a moment, through the same steam-game request an + # in-session launch uses; after two minutes it goes anyway. + if [ -n "${BL_LAUNCH_DIR:-}" ] && [ -z "${BL_DESKTOP:-}" ]; then + _bl_held= + _bl_rest=() + for _bl_arg in "$@"; do + case $_bl_arg in + steam://rungameid/*) _bl_held=${_bl_arg#steam://rungameid/} ;; + *) _bl_rest+=("$_bl_arg") ;; + esac + done + if [ -n "$_bl_held" ]; then + set -- "${_bl_rest[@]}" + ( + _bl_end=$((SECONDS + 120)) + until [ "$(xprop -root GAMESCOPE_FOCUSED_APP 2>/dev/null | sed -n 's/.*= *//p')" = 769 ] || [ "$SECONDS" -ge "$_bl_end" ]; do + sleep 1 + done + sleep 4 + echo "== steam: interface up; launching the requested game $_bl_held" + printf '%s\n' "$_bl_held" > "$BL_LAUNCH_DIR/steam-game.tmp" && mv -f "$BL_LAUNCH_DIR/steam-game.tmp" "$BL_LAUNCH_DIR/steam-game" + ) & + fi + fi # Debug aid while the runtime is being brought up: an app id dropped in ~/.bl-autolaunch is # handed to the client as a rungameid URL, so a launch can be reproduced without touching the # screen. Removed once the runtime is stable. diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch new file mode 100755 index 0000000000..cb93faa44f --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch @@ -0,0 +1,317 @@ +#!/usr/bin/python3 +r"""Before Proton starts a store game, asks the app for what that store needs at launch. + +Run by droiddeck-proton / droiddeck-proton-wrap (steam-compatibility) for every real game launch, +whoever started it: the Games tab, a pinned shortcut, or the Steam client's own Play button. It +looks for the store launcher DroidDeck wrote (.droiddeck-launch.bat) among the arguments; beside it +.droiddeck-store.json says which store and game it is. + +For an Epic game the game needs a fresh exchange code to sign in, which only the app can mint. The +request goes through files under $BL_LAUNCH_DIR/stores (the session root, the same path on both +sides): + + req/<id>.json {"op": "epic-code", "store": "epic", "id": "<app name>"} (tmp + rename) + resp/<id>.json {"ok": true, "code": true|false, "reason": "..."} + +The app writes .droiddeck-epic-code into the game's folder itself - the code never passes through +here - and the launcher .bat reads and deletes it. No answer within TIMEOUT seconds: the game starts +with its offline identity. A code file older than STALE_SECONDS is removed first, so a code left by +an earlier launch is never used. + +The game's prefix gets Wine's browser set to droiddeck-open-url, which hands web addresses to +Android: an EOS sign-in step that falls back to a browser (a consent Epic asks for once per product) +is finished there. An EOS overlay pointer (HKCU\Software\Epic Games\EOS "OverlayPath") an earlier +build wrote is removed - with it the game exited while loading. + + droiddeck-store-launch <the arguments Steam gave the compat tool> +""" + +import json +import os +import sys +import time +from pathlib import Path + +LAUNCHER = ".droiddeck-launch.bat" +SIDECAR = ".droiddeck-store.json" +EPIC_CODE = ".droiddeck-epic-code" +TIMEOUT = 5.0 +STALE_SECONDS = 300 +SERVER_WAIT = 8.0 +BROWSER = "/usr/local/bin/droiddeck-open-url" +EOS_KEY = "[Software\\\\Epic Games\\\\EOS]" +BROWSER_KEY = "[Software\\\\Wine\\\\WineBrowser]" + + +def log(line): + print("droiddeck-store-launch: " + line, file=sys.stderr, flush=True) + + +MAX_DEPTH = 6 + + +def sidecar_at(folder): + try: + sidecar = json.loads((folder / SIDECAR).read_text()) + except (OSError, ValueError): + return None + if not isinstance(sidecar, dict) or not sidecar.get("store") or not sidecar.get("id"): + return None + return sidecar + + +def find_store_game(argv): + """ + The (folder, sidecar) of a store game among [argv], or None when this is not one. Steam passes + the shortcut's target: the launcher .bat when the game has one (Epic, Amazon, a GOG game with + arguments), else the game's exe itself (most GOG games), which may sit in a subfolder - so the + folders above it are looked through too, up to MAX_DEPTH, for the sidecar. + """ + for raw in argv: + arg = raw.strip().strip('"').strip("'").replace("\\", "/") + low = arg.lower() + if not (low.endswith(LAUNCHER) or low.endswith(".exe") or low.endswith(".bat")) or "/" not in arg: + continue + folder = Path(arg).parent + for _ in range(MAX_DEPTH): + sidecar = sidecar_at(folder) + if sidecar is not None: + return folder, sidecar + if folder.parent == folder: + break + folder = folder.parent + return None + + +def drop_stale(folder, now=None): + """Removes a code file older than STALE_SECONDS; True when one was removed.""" + code = folder / EPIC_CODE + try: + if (now if now is not None else time.time()) - code.stat().st_mtime > STALE_SECONDS: + code.unlink() + return True + except OSError: + pass + return False + + +def ask(channel, request, timeout=TIMEOUT): + """Writes [request] under channel/req and waits for channel/resp; the answer, or None on timeout.""" + req_dir, resp_dir = channel / "req", channel / "resp" + req_dir.mkdir(parents=True, exist_ok=True) + resp_dir.mkdir(parents=True, exist_ok=True) + rid = "%d-%d" % (os.getpid(), int(time.time() * 1000)) + tmp = req_dir / (rid + ".json.tmp") + tmp.write_text(json.dumps(request)) + os.rename(tmp, req_dir / (rid + ".json")) + answer = resp_dir / (rid + ".json") + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + value = json.loads(answer.read_text()) + answer.unlink() + return value + except (OSError, ValueError): + time.sleep(0.05) + # Not answered: the request is withdrawn so a late app does not mint a code nobody reads. + try: + (req_dir / (rid + ".json")).unlink() + except OSError: + pass + return None + + +def windows_path(unix): + """A guest path as Wine names it through Z: (the whole filesystem).""" + return "Z:" + unix.replace("/", "\\") + + +def reg_escape(value): + return value.replace("\\", "\\\\").replace('"', '\\"') + + +def set_value(reg, key, name, value, now=None): + """ + Appends `key` with `"name"="value"` to a prefix's user.reg unless the last word on it already + says so. Appended rather than edited in place, as DirectAudio's key is: Wine reads later sections + over earlier ones, and nothing of the file is rewritten under a wineserver that might hold it. + False when there is no user.reg yet (Proton creates the prefix after this; the next launch has it). + """ + if not reg.is_file(): + return False + line = '"%s"="%s"' % (name, reg_escape(value)) + inside, last = False, None + for row in reg.read_text(errors="replace").splitlines(): + if row.startswith("["): + inside = row.startswith(key + " ") or row == key + elif inside and row.startswith('"%s"=' % name): + last = row + if last == line: + return True + with open(reg, "a") as out: + out.write("\n%s %d\n%s\n" % (key, int(now if now is not None else time.time()), line)) + return True + + +def remove_value(reg, key, name): + """Drops every `"name"=` line under `key` in user.reg; True when one was there.""" + if not reg.is_file(): + return False + rows, inside, dropped = [], False, False + for row in reg.read_text(errors="replace").splitlines(keepends=True): + if row.startswith("["): + inside = row.startswith(key + " ") or row.rstrip("\n") == key + elif inside and row.startswith('"%s"=' % name): + dropped = True + continue + rows.append(row) + if dropped: + tmp = reg.with_name(reg.name + ".dd-new") + tmp.write_text("".join(rows)) + os.replace(tmp, reg) + return dropped + + +def options(sidecar): + """An Epic game's launch choices from its sidecar, as the Games tab's Epic card sets them.""" + e = sidecar.get("epic") if isinstance(sidecar.get("epic"), dict) else {} + return {"eos": e.get("eos", True) is not False, "offline": e.get("offline", False) is True} + + +def server_socket(pfx): + """The wineserver socket for prefix [pfx], as Wine names it (server-<dev>-<inode> under /tmp/.wine-<uid>).""" + try: + st = os.stat(pfx) + except OSError: + return None + base = os.environ.get("BL_WINE_SERVER_BASE") or "/tmp/.wine-%d" % os.getuid() + return Path(base) / ("server-%x-%x" % (st.st_dev, st.st_ino)) / "socket" + + +def wait_for_server(pfx, timeout=SERVER_WAIT): + """ + Waits for a wineserver still holding this prefix (an earlier verb's, or the last launch's) to + exit: it rewrites user.reg from its own memory when it does, which drops a line appended + meanwhile. True when none is left. + """ + sock = server_socket(pfx) + if sock is None: + return True + deadline = time.monotonic() + timeout + while sock.exists() and time.monotonic() < deadline: + time.sleep(0.1) + return not sock.exists() + + +def has_value(reg, key, name, value=None): + """Whether the last word on `key` in user.reg sets "name" (to [value], when given).""" + if not reg.is_file(): + return False + inside, last = False, None + for row in reg.read_text(errors="replace").splitlines(): + if row.startswith("["): + inside = row.startswith(key + " ") or row == key + elif inside and row.startswith('"%s"=' % name): + last = row + if last is None: + return False + return value is None or last == '"%s"="%s"' % (name, reg_escape(value)) + + +def provision_prefix(compat_data, wait=SERVER_WAIT): + """Wine's browser set to the hand-off, and an earlier build's EOS overlay pointer removed.""" + if not compat_data: + return "no-prefix" + pfx = Path(compat_data) / "pfx" + reg = pfx / "user.reg" + if not reg.is_file(): + return "prefix-not-created" + # A wineserver still up for this prefix would write its own copy of user.reg over these lines. + busy = not wait_for_server(pfx, wait) + # winebrowser starts the first entry it can; an absolute path does not depend on the PATH Wine + # hands it. + set_value(reg, BROWSER_KEY, "Browsers", BROWSER + ",xdg-open") + removed = remove_value(reg, EOS_KEY, "OverlayPath") + state = "browser-set" if has_value(reg, BROWSER_KEY, "Browsers", BROWSER + ",xdg-open") else "browser-not-written" + if removed: + state += " old-overlay-removed" + return state + (" server-running" if busy else "") + + +CLOUD_TIMEOUT = 15.0 +CLOUD_STORES = ("gog", "epic") +# Exit status that tells droiddeck-proton this was a store game with cloud saves: it then waits for +# Proton instead of exec'ing it, and runs `droiddeck-store-launch --exited` after. +STORE_GAME = 10 + + +def cloud_on(sidecar): + """Cloud saves for this game (the game card's switch; on unless turned off).""" + return str(sidecar.get("store")) in CLOUD_STORES and sidecar.get("cloud", True) is not False + + +def exited(argv): + """After the game: ask the app to upload what changed, without waiting for it.""" + found = find_store_game(argv) + launch_dir = os.environ.get("BL_LAUNCH_DIR", "") + if found is None or not launch_dir or not cloud_on(found[1]): + return 0 + store, game = str(found[1].get("store")), str(found[1].get("id")) + ask(Path(launch_dir) / "stores", {"op": "cloud-up", "store": store, "id": game}, timeout=1.0) + log("cloud %s %s up requested" % (store, game)) + return 0 + + +def main(argv): + if argv and argv[0] == "--exited": + return exited(argv[1:]) + found = find_store_game(argv) + if found is None: + return 0 + folder, sidecar = found + store, game = str(sidecar.get("store")), str(sidecar.get("id")) + compat_data = os.environ.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") + prefix_id = os.path.basename(compat_data) if compat_data else "" + launch_dir = os.environ.get("BL_LAUNCH_DIR", "") + if store == "epic": + drop_stale(folder) + choice = options(sidecar) + if not (choice["eos"] and not choice["offline"]): + # Sign-in off, or offline: no code, and none left over from before. + try: + (folder / EPIC_CODE).unlink() + except OSError: + pass + log("epic launch id=%s code=no reason=%s" % (game, "offline" if choice["offline"] else "eos-off")) + elif not launch_dir: + log("epic launch id=%s code=no reason=no-channel" % game) + else: + answer = ask(Path(launch_dir) / "stores", {"op": "epic-code", "store": store, "id": game, "prefix": prefix_id}) + if answer is None: + log("epic launch id=%s code=no reason=timeout" % game) + else: + got = bool(answer.get("code")) + log("epic launch id=%s code=%s reason=%s" % (game, "yes" if got else "no", answer.get("reason", ""))) + log("epic prefix=%s %s" % (prefix_id or "?", provision_prefix(compat_data))) + if not cloud_on(sidecar): + if store in CLOUD_STORES: + log("cloud %s %s down result=skipped reason=off" % (store, game)) + return 0 + if launch_dir: + # The cloud copy where it is newer, before the game reads its saves; bounded, then on regardless. + answer = ask(Path(launch_dir) / "stores", {"op": "cloud-down", "store": store, "id": game}, timeout=CLOUD_TIMEOUT) + if answer is None: + log("cloud %s %s down result=timeout" % (store, game)) + else: + log("cloud %s %s down result=%s files=%s reason=%s" % (store, game, answer.get("result", "?"), answer.get("files", 0), answer.get("reason", ""))) + else: + log("cloud %s %s down result=skipped reason=no-channel" % (store, game)) + return STORE_GAME + + +if __name__ == "__main__": + try: + sys.exit(main(sys.argv[1:])) + except Exception as e: # never in the way of the launch + log("skipped: %s" % type(e).__name__) + sys.exit(0) diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index 351e32b0c2..af8e3dd9cd 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -96,6 +96,46 @@ TOOL_MANIFEST_VDF = """"manifest" # each package in use. A file that no longer matches - whatever changed it - is put back before # Proton starts. Only for the Proton build the choice was made for: after a Steam update the build # differs and the Proton's own new files are left alone until the user chooses again. +# A store game (GOG, Epic, Amazon) is started through the launcher .bat DroidDeck wrote beside it. +# An Epic game needs a fresh sign-in code, which only the app can mint: droiddeck-store-launch asks +# for it and the app writes it into the game's folder, so a launch from the client's own Play +# button signs in just as one from the app does. A real launch only; never in the way of it. +BL_STORE_SETUP = """ +bl_store_launch() { + [ "${verb:-}" = waitforexitandrun ] || return 0 + # A web address the game opens (Wine's winebrowser runs xdg-open) goes to Android's browser: + # droiddeck-open-url as xdg-open, first on the game's PATH, with the session channel left in a + # file in case Wine does not pass BL_LAUNCH_DIR on. (The prefix also names it as Wine's browser.) + if [ -x /usr/local/bin/droiddeck-open-url ]; then + mkdir -p /tmp/droiddeck-browser + ln -sfn /usr/local/bin/droiddeck-open-url /tmp/droiddeck-browser/xdg-open + [ -n "${BL_LAUNCH_DIR:-}" ] && printf '%s\n' "$BL_LAUNCH_DIR" > /tmp/droiddeck-browser/launch-dir + PATH="/tmp/droiddeck-browser:$PATH"; export PATH + fi + # BL_DEBUG_BROWSER=1 (droiddeck-env): Wine traces how the game opens a web address. + if [ "${BL_DEBUG_BROWSER:-}" = 1 ]; then + WINEDEBUG="${WINEDEBUG:+$WINEDEBUG,}+shell,+winebrowser"; export WINEDEBUG + fi + [ -x /usr/local/bin/droiddeck-store-launch ] || return 0 + /usr/local/bin/droiddeck-store-launch "$@" + # 10: a store game with cloud saves - the launch waits for Proton and uploads after (bl_run). + [ $? -eq 10 ] && BL_STORE_EXIT=1 + return 0 +} + +# Runs Proton: exec'd as always, except for a store game with cloud saves, where the wrapper stays +# to tell the app the game has exited so it can upload what changed. +bl_run() { + if [ -n "${BL_STORE_EXIT:-}" ]; then + "$@" + rc=$? + /usr/local/bin/droiddeck-store-launch --exited "${BL_STORE_ARGS[@]}" || true + exit $rc + fi + exec "$@" +} +""" + BL_COMPONENTS_SETUP = """ bl_components() { base=$1 @@ -332,7 +372,9 @@ if [ -n "$depot" ]; then bl_game_cpus bl_idtech3 "$@" bl_libpath - exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$depot/proton" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" + bl_store_launch "$@" + BL_STORE_ARGS=("$@") + bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$depot/proton" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" fi echo "droiddeck-proton: no ARM64 Proton depot yet - the client downloads one as this tool's dependency; retry the launch once it has" >&2 exit 1 @@ -346,7 +388,7 @@ def launcher_sh(sources=SOURCES): # something else. return (LAUNCHER_TEMPLATE % " ".join('"%s"' % n for n in sources)).replace( "verb=$1; shift", "verb=$1; shift\n" + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP + BL_IDTECH3_SETUP - + BL_COMPONENTS_SETUP + BL_LIBPATH_SETUP, 1) + + BL_COMPONENTS_SETUP + BL_LIBPATH_SETUP + BL_STORE_SETUP, 1) LAUNCHER_SH = launcher_sh() @@ -390,9 +432,11 @@ bl_directaudio "$here" bl_game_cpus bl_idtech3 "$@" bl_libpath +bl_store_launch "$@" +BL_STORE_ARGS=("$@") # Proton takes its base directory from dirname(sys.argv[0]) without resolving it, so it has to be # started from its own tree. -exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$here/%s" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" +bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$here/%s" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" """ EXTRA_COMPAT_TOOL_VDF = """"compatibilitytools" { @@ -619,7 +663,7 @@ def adopt_extras(tools): body = (EXTRA_WRAPPER_SH % entry_point).replace( 'here=$(dirname "$0")', 'here=$(dirname "$0")\n' + BL_DIRECTAUDIO_SETUP + BL_GAME_CPUS_SETUP + BL_IDTECH3_SETUP - + BL_COMPONENTS_SETUP + BL_LIBPATH_SETUP, 1) + + BL_COMPONENTS_SETUP + BL_LIBPATH_SETUP + BL_STORE_SETUP, 1) write_changed(wrapper, body, 0o755) write_changed(manifest, EXTRA_MANIFEST_VDF) write_changed(compat_vdf, EXTRA_COMPAT_TOOL_VDF % (name, display_name(entry))) diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index 85926c04f7..1fa4cd739a 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -260,7 +260,9 @@ class GameEnvironmentTest(unittest.TestCase): def test_both_proton_wrappers_call_environment_launcher(self): for script in (COMPAT["LAUNCHER_SH"], COMPAT["EXTRA_WRAPPER_SH"] % "proton"): subprocess.run(["bash", "-n"], input=script, text=True, check=True) - self.assertIn('exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', script) + # Started through bl_run, which execs it - or, for a store game with cloud saves, waits + # for it so droiddeck-store-launch --exited can follow (test_store_launch.CloudHooks). + self.assertIn('bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', script) def test_wrappers_preload_the_session_library_before_the_input_shim(self): overlay = "/root/.local/share/Steam/ubuntu12_64/gameoverlayrenderer.so" diff --git a/tools/tests/test_store_launch.py b/tools/tests/test_store_launch.py new file mode 100644 index 0000000000..46a255f758 --- /dev/null +++ b/tools/tests/test_store_launch.py @@ -0,0 +1,262 @@ +"""droiddeck-store-launch: a launch from anywhere - the Steam client's own Play button included - +asks the app for an Epic game's sign-in code before Proton starts. These pin how it recognises a +store game, how it asks, and that both Proton launchers call it.""" +import importlib.machinery +import importlib.util +import json +import os +from pathlib import Path +import tempfile +import threading +import time +import unittest + +BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin' + + +def load(name): + loader = importlib.machinery.SourceFileLoader(name.replace('-', '_'), str(BIN / name)) + spec = importlib.util.spec_from_loader(loader.name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +store = load('droiddeck-store-launch') + + +def load_path(path, name): + loader = importlib.machinery.SourceFileLoader(name, str(path)) + spec = importlib.util.spec_from_loader(name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module +compat = load('steam-compatibility') + + +def game(root, store_id='epic', ident='8b6a0e14'): + folder = Path(root) / 'Games/Stores/Epic/Metalstorm' + folder.mkdir(parents=True) + (folder / '.droiddeck-store.json').write_text(json.dumps({'store': store_id, 'id': ident, 'title': 'Metalstorm', 'exe': 'Metalstorm.exe'})) + (folder / '.droiddeck-launch.bat').write_text('@echo off\r\n') + return folder + + +class Detection(unittest.TestCase): + def test_the_launcher_among_steams_arguments_names_the_game(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root) + found = store.find_store_game(['/usr/bin/steam-runtime', '"%s/.droiddeck-launch.bat"' % folder, '-flag']) + self.assertIsNotNone(found) + self.assertEqual(folder, found[0]) + self.assertEqual('epic', found[1]['store']) + self.assertEqual('8b6a0e14', found[1]['id']) + + def test_anything_else_is_left_alone(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root) + # The exe of a store game is one too (a GOG game without a launcher); a stray file is not. + self.assertEqual(folder, store.find_store_game([str(folder / 'Metalstorm.exe')])[0]) + self.assertIsNone(store.find_store_game([str(Path(root) / 'elsewhere/readme.txt')])) + (folder / '.droiddeck-store.json').unlink() + self.assertIsNone(store.find_store_game([str(folder / '.droiddeck-launch.bat')])) + + def test_only_epic_asks_for_a_code(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog') + # Cloud saves off: nothing else to ask for either. + sc = json.loads((folder / '.droiddeck-store.json').read_text()); sc['cloud'] = False + (folder / '.droiddeck-store.json').write_text(json.dumps(sc)) + os.environ['BL_LAUNCH_DIR'] = str(Path(root) / 'session') + try: + self.assertEqual(0, store.main([str(folder / '.droiddeck-launch.bat')])) + finally: + del os.environ['BL_LAUNCH_DIR'] + self.assertFalse((Path(root) / 'session/stores').exists()) + + +class Asking(unittest.TestCase): + def test_a_request_is_answered_through_the_resp_folder(self): + with tempfile.TemporaryDirectory() as root: + channel = Path(root) / 'stores' + + def app(): + req = channel / 'req' + for _ in range(100): + names = [p for p in req.glob('*.json')] if req.exists() else [] + if names: + request = json.loads(names[0].read_text()) + self.assertEqual({'op': 'epic-code', 'store': 'epic', 'id': 'x'}, request) + names[0].unlink() + tmp = channel / 'resp' / (names[0].name + '.tmp') + tmp.write_text(json.dumps({'ok': True, 'code': True, 'reason': 'ok'})) + os.rename(tmp, channel / 'resp' / names[0].name) + return + time.sleep(0.02) + + t = threading.Thread(target=app) + t.start() + answer = store.ask(channel, {'op': 'epic-code', 'store': 'epic', 'id': 'x'}, timeout=3) + t.join() + self.assertEqual({'ok': True, 'code': True, 'reason': 'ok'}, answer) + + def test_no_answer_withdraws_the_request(self): + with tempfile.TemporaryDirectory() as root: + channel = Path(root) / 'stores' + self.assertIsNone(store.ask(channel, {'op': 'epic-code'}, timeout=0.2)) + self.assertEqual([], list((channel / 'req').iterdir())) + + def test_a_stale_code_is_dropped_before_asking(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root) + code = folder / '.droiddeck-epic-code' + code.write_text('old') + self.assertFalse(store.drop_stale(folder, now=code.stat().st_mtime + 60)) + self.assertTrue(store.drop_stale(folder, now=code.stat().st_mtime + store.STALE_SECONDS + 1)) + self.assertFalse(code.exists()) + + +class Prefix(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + root = Path(self.tmp.name) + self.compat = root / 'compatdata/3785150007' + (self.compat / 'pfx').mkdir(parents=True) + self.reg = self.compat / 'pfx/user.reg' + self.reg.write_text('WINE REGISTRY Version 2\n\n[Software\\\\Wine] 1\n"Version"="win10"\n') + + def tearDown(self): + self.tmp.cleanup() + + def test_wines_browser_is_the_hand_off_by_its_path(self): + self.assertEqual('browser-set', store.provision_prefix(str(self.compat))) + text = self.reg.read_text() + self.assertIn('[Software\\\\Wine\\\\WineBrowser] ', text) + self.assertIn('"Browsers"="/usr/local/bin/droiddeck-open-url,xdg-open"', text) + # A second launch adds nothing: the last word already says so. + store.provision_prefix(str(self.compat)) + self.assertEqual(text, self.reg.read_text()) + + def test_an_earlier_builds_overlay_pointer_is_removed(self): + self.reg.write_text(self.reg.read_text() + '\n[Software\\\\Epic Games\\\\EOS] 2\n"OverlayPath"="Z:\\\\x"\n"Other"="kept"\n') + self.assertEqual('browser-set old-overlay-removed', store.provision_prefix(str(self.compat))) + text = self.reg.read_text() + self.assertNotIn('OverlayPath', text) + self.assertIn('"Other"="kept"', text) + + def test_no_prefix_yet(self): + self.assertEqual('prefix-not-created', store.provision_prefix(self.tmp.name + '/compatdata/1')) + self.assertEqual('no-prefix', store.provision_prefix('')) + + +class Choices(unittest.TestCase): + def test_the_sidecars_epic_choices_are_honoured(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root) + sidecar = json.loads((folder / '.droiddeck-store.json').read_text()) + self.assertEqual({'eos': True, 'offline': False}, store.options(sidecar)) + # An earlier build's overlay field is read past. + self.assertEqual({'eos': True, 'offline': False}, store.options({'epic': {'v': 2, 'overlay': True}})) + sidecar['epic'] = {'eos': True, 'offline': True} + sidecar['cloud'] = False + (folder / '.droiddeck-store.json').write_text(json.dumps(sidecar)) + (folder / '.droiddeck-epic-code').write_text('left over') + compat = Path(root) / 'compatdata/1' + (compat / 'pfx').mkdir(parents=True) + reg = compat / 'pfx/user.reg' + reg.write_text('WINE REGISTRY Version 2\n\n[Software\\\\Epic Games\\\\EOS] 1\n"OverlayPath"="Z:\\\\x"\n"Other"="kept"\n') + os.environ['BL_LAUNCH_DIR'] = str(Path(root) / 'session') + os.environ['STEAM_COMPAT_DATA_PATH'] = str(compat) + try: + self.assertEqual(0, store.main([str(folder / '.droiddeck-launch.bat')])) + finally: + del os.environ['BL_LAUNCH_DIR'] + del os.environ['STEAM_COMPAT_DATA_PATH'] + # Offline: no request was made and no code is left; the old overlay pointer is gone. + self.assertFalse((Path(root) / 'session/stores/req').exists()) + self.assertFalse((folder / '.droiddeck-epic-code').exists()) + text = reg.read_text() + self.assertNotIn('OverlayPath', text) + self.assertIn('"Other"="kept"', text) + + +class Browser(unittest.TestCase): + def test_a_web_address_is_handed_to_the_app(self): + xdg = load_path(BIN / 'droiddeck-open-url', 'open_url') + with tempfile.TemporaryDirectory() as root: + os.environ['BL_LAUNCH_DIR'] = root + try: + self.assertEqual(0, xdg.main(['https://www.epicgames.com/activate'])) + finally: + del os.environ['BL_LAUNCH_DIR'] + reqs = list((Path(root) / 'stores/req').glob('*.json')) + self.assertEqual(1, len(reqs)) + self.assertEqual({'op': 'open-url', 'url': 'https://www.epicgames.com/activate'}, json.loads(reqs[0].read_text())) + self.assertEqual('https://www.epicgames.com', xdg.where('https://www.epicgames.com/activate?code=SECRET')) + + +class ExeTarget(unittest.TestCase): + def test_a_game_started_by_its_exe_is_found_from_a_subfolder(self): + # Most GOG games have no launcher .bat: Steam runs the exe, which may sit in a subfolder. + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='1207664643') + (folder / 'bin/x64').mkdir(parents=True) + exe = folder / 'bin/x64/ELDERBORN.exe' + exe.write_text('MZ') + found = store.find_store_game(['"%s"' % exe]) + self.assertEqual(folder, found[0]) + self.assertEqual('gog', found[1]['store']) + self.assertIsNone(store.find_store_game(['/root/Games/Other/x.exe'])) + + +class CloudHooks(unittest.TestCase): + def test_a_store_game_with_cloud_saves_asks_before_and_after(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='1207664643') + bat = str(folder / '.droiddeck-launch.bat') + os.environ['BL_LAUNCH_DIR'] = str(Path(root) / 'session') + try: + original = store.ask + asked = [] + store.ask = lambda channel, request, timeout=5.0: asked.append((request, timeout)) or {'ok': True, 'files': 2, 'reason': 'ok'} + # 10: the launcher waits for Proton and calls --exited afterwards. + self.assertEqual(store.STORE_GAME, store.main([bat])) + self.assertEqual(0, store.main(['--exited', bat])) + finally: + store.ask = original + del os.environ['BL_LAUNCH_DIR'] + self.assertEqual({'op': 'cloud-down', 'store': 'gog', 'id': '1207664643'}, asked[0][0]) + self.assertEqual(store.CLOUD_TIMEOUT, asked[0][1]) + self.assertEqual('cloud-up', asked[1][0]['op']) + + def test_cloud_off_or_amazon_runs_proton_as_before(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog', ident='1') + sidecar = json.loads((folder / '.droiddeck-store.json').read_text()) + sidecar['cloud'] = False + (folder / '.droiddeck-store.json').write_text(json.dumps(sidecar)) + self.assertEqual(0, store.main([str(folder / '.droiddeck-launch.bat')])) + self.assertFalse(store.cloud_on({'store': 'amazon', 'id': 'x'})) + self.assertTrue(store.cloud_on({'store': 'epic', 'id': 'x'})) + + def test_both_launchers_wait_for_proton_only_for_a_store_game(self): + for sh in (compat.LAUNCHER_SH, compat.EXTRA_WRAPPER_SH): + self.assertIn('bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', sh) + self.assertNotIn('exec ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env', sh) + self.assertIn('[ $? -eq 10 ] && BL_STORE_EXIT=1', compat.BL_STORE_SETUP) + self.assertIn('droiddeck-store-launch --exited "${BL_STORE_ARGS[@]}"', compat.BL_STORE_SETUP) + self.assertIn('exec "$@"', compat.BL_STORE_SETUP) + + +class Launchers(unittest.TestCase): + def test_both_proton_launchers_ask_before_proton_starts(self): + sh = compat.LAUNCHER_SH + self.assertIn('bl_store_launch() {', sh) + self.assertLess(sh.index('bl_store_launch "$@"'), sh.index('droiddeck-game-env "$depot/proton"')) + self.assertIn('bl_store_launch "$@"', compat.EXTRA_WRAPPER_SH) + self.assertIn('bl_store_launch() {', compat.BL_STORE_SETUP) + self.assertIn('waitforexitandrun', compat.BL_STORE_SETUP) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/tests/test_store_shortcuts.py b/tools/tests/test_store_shortcuts.py new file mode 100644 index 0000000000..082011e8fc --- /dev/null +++ b/tools/tests/test_store_shortcuts.py @@ -0,0 +1,74 @@ +"""The shortcuts writer and a store install: the app registers a game GOG, Epic or Amazon +installed through the same listing as an added folder, pointing the shortcut at the game's +launcher .bat when it has one. These pin the parts of the writer that contract relies on.""" +import importlib.machinery +import importlib.util +import json +from pathlib import Path +import subprocess +import tempfile +import unittest +import zlib + +BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin' + + +def load(name): + loader = importlib.machinery.SourceFileLoader(name, str(BIN / name)) + spec = importlib.util.spec_from_loader(name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +shortcuts = load('droiddeck-steam-shortcuts') + +FOLDER = '/root/Games/Stores/Epic/Celeste' +LAUNCHER = FOLDER + '/.droiddeck-launch.bat' + + +def app_id(exe, name): + """The appid the app computes for a shortcut (AddedGames.scanGame): CRC32 of the quoted exe + and the name, high bit set - what Steam itself derives for a shortcut it is given.""" + return (zlib.crc32(('"%s"' % exe + name).encode()) | 0x80000000) & 0xFFFFFFFF + + +class StoreShortcutsTest(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + self.steam = self.root / 'Steam' + self.config = self.steam / 'userdata/123/config' + self.config.mkdir(parents=True) + self.listing = self.root / 'added-games.json' + + def write(self, *games): + self.listing.write_text(json.dumps(list(games))) + subprocess.run(['python3', str(BIN / 'droiddeck-steam-shortcuts'), str(self.steam), str(self.listing)], + check=True, capture_output=True) + return shortcuts.parse((self.config / 'shortcuts.vdf').read_bytes())['shortcuts'] + + def test_a_launcher_bat_is_the_shortcuts_exe_and_the_folder_its_start_dir(self): + game = dict(name='Celeste', exe=LAUNCHER, folder=FOLDER, dir=FOLDER, appid=app_id(LAUNCHER, 'Celeste'), seen=0) + entries = self.write(game) + entry = next(iter(entries.values())) + self.assertEqual('"%s"' % LAUNCHER, entry['Exe']) + self.assertEqual('"%s"' % FOLDER, entry['StartDir']) + self.assertEqual('Celeste', entry['AppName']) + self.assertIn('droiddeck-app', entry['tags'].values()) + + def test_the_appid_the_app_computes_is_the_one_written(self): + appid = app_id(LAUNCHER, 'Celeste') + entries = self.write(dict(name='Celeste', exe=LAUNCHER, folder=FOLDER, dir=FOLDER, appid=appid, seen=0)) + entry = next(iter(entries.values())) + self.assertEqual(appid, entry['appid'] & 0xFFFFFFFF) + + def test_an_uninstalled_game_leaves_the_listing_and_the_shortcut_goes_with_it(self): + game = dict(name='Celeste', exe=LAUNCHER, folder=FOLDER, dir=FOLDER, appid=app_id(LAUNCHER, 'Celeste'), seen=0) + self.assertEqual(1, len(self.write(game))) + self.assertEqual(0, len(self.write())) + + +if __name__ == '__main__': + unittest.main() From 06e108a72637cd926b2570ee2d77765edd44d206 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 9 Oct 2026 12:36:05 -0400 Subject: [PATCH 07/54] Windows components: lay out installer .exe wrappers without running them (#480) * Windows components: lay out installer .exe wrappers without running them The components page could only take .msi packages apart; most catalog entries ship an installer .exe, so VC++ runtimes, .NET Framework 4.0-4.8 and the .NET Core/5+ runtimes sat under 'Need a Windows installer'. droiddeck-msi-install now opens those wrappers and lays out the packages inside the same way: - WiX bundles (Visual C++ 2012-2022, .NET Core/5+ desktop runtimes): the .wixburn section names the containers, each a cabinet found by its header (signing moves them); payloads get the names the bundle manifest lists, so each package finds its cab1.cab beside it. ARM64 packages are skipped, x86 and x64 both laid out. - Self-extracting cabinets (Visual C++ 2005-2010): cabextract, external cabinet beside the package (vc_red.cab). - Self-extracting 7-Zip archives (.NET Framework 4.0-4.8): 7-Zip, now bundled with the msitools (Arch Linux ARM 7zip 26.04: 7z + its 7z.so plugin, licences beside the others). The complete product (netfx_Full_*) wins over the 4.0 parts and the per-Windows copies. - An .exe that is its own setup program (NSIS, InnoSetup) reports that it holds no package. - --unpack opens an archive for the app: DirectX's redistributable for XACT's cabinet picks. The app executes the remaining catalog steps offline: set_windows (HKCU\Software\Wine Version), set_register_key, get_from_cab (through --unpack) and 'uninstall Wine Mono' (the mscoree override beside it is what matters), and reads url/file_name the catalog nests under 'environment' on a few .NET entries. Setup-program installers stay in the waiting section with a note that says what they are. Verified on this branch's engine with msitools, cabextract and 7z: VC++ 2010 x64 (19 files, 61 values), VC++ 2022 x64 bundle (26 files, 66 values), .NET 4.0 (1053 files, 6056 values, Framework64\v4.0.30319 with clr.dll and mscorlib.dll, NDP\v4 keys) and .NET 4.8 (1633 files, 522 GAC assemblies, 6495 values). Not yet run on a device. * Windows components: the pressed row greys out and shows its own two-step progress bar Pressing a switch now greys that row at once and runs a thin bar across it: the download 0..100, then the install 0..100, with a caption naming the file or the stage (a dependency's name in front while that installs first). Turning a component off greys the row the same way until the pick is saved. The page-wide bar at the bottom is gone; Done waits on the busy row. droiddeck-msi-install reports how far along it is ('progress 37% placing files'): reading, each cabinet, every 5% of files placed, the registry, done - and a wrapper maps each package into its share, so a bundle's two packages run 10..55 and 55..100. The app parses the percent and passes a typed Progress (component, stage, phase, percent) up to the editor. * Windows components: the ones turned on sort to the top of the list In 'All components' the picks for this game come first, in their usual alphabetical order among themselves; turning one off returns it to its place. The row moves once the install has finished and the pick is saved, not while its progress bar is running. * Windows components: an 'Installed for this game' section under the recommendations What is turned on for this game gets its own section between Recommended and All components, alphabetical; the recommendations stay where they are (a recommended pick stays there). A component in the section leaves All components, and goes back to its place when turned off. LB/RB jump through the new section too. --- .github/workflows/build.yml | 7 +- .../launcher/session/SessionFiles.kt | 4 +- .../launcher/session/WinComponents.kt | 216 +++++++++++-- .../launcher/ui/WinComponentsEditor.kt | 109 +++++-- app/src/main/res/values/strings.xml | 8 +- .../session/WinComponentsSupportTest.kt | 44 ++- .../usr/local/bin/droiddeck-msi-install | 306 ++++++++++++++++-- tools/msitools/licenses/7zip-License | 151 +++++++++ tools/msitools/licenses/7zip-unRarLicense | 41 +++ tools/msitools/licenses/NOTICE | 5 +- tools/msitools/packages.txt | 4 +- 11 files changed, 803 insertions(+), 92 deletions(-) create mode 100644 tools/msitools/licenses/7zip-License create mode 100644 tools/msitools/licenses/7zip-unRarLicense diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 80e3497d38..87df262956 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -254,10 +254,13 @@ jobs: done install -m644 msi-pkgs/usr/bin/msiinfo "$T/msiinfo" install -m644 msi-pkgs/usr/bin/cabextract "$T/cabextract" + # 7-Zip (the full build with its format plugin) opens installer .exe wrappers. + install -m644 msi-pkgs/usr/lib/7zip/7z "$T/7z" + install -m644 msi-pkgs/usr/lib/7zip/7z.so "$T/7z.so" for so in libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do cp -L "msi-pkgs/usr/lib/$so" "$T/$so" done - for f in NOTICE GPL-2 GPL-3 LGPL-2.1; do + for f in NOTICE GPL-2 GPL-3 LGPL-2.1 7zip-License 7zip-unRarLicense; do install -Dm644 "tools/msitools/licenses/$f" "$D/usr/local/share/licenses/droiddeck-msitools/$f" done rm -rf msi-pkgs @@ -391,7 +394,7 @@ jobs: test -f "$D/usr/local/bin/droiddeck-session" test -f "$D/usr/local/bin/droiddeck-proton-extra" test -f "$D/usr/local/bin/droiddeck-msi-install" - for f in msiinfo cabextract libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do + for f in msiinfo cabextract 7z 7z.so libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do test -f "$D/usr/local/lib/droiddeck-msitools/$f" || { echo "::error::droiddeck-msitools/$f missing"; exit 1; } done diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index e61a83413b..04dc792606 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -117,9 +117,9 @@ object SessionFiles { "usr/local/lib/mangoapp/libtracefs.so.1", ).map { it to it } // What the Windows components installer reads .msi packages with (tools/msitools). - val msitools = listOf("msiinfo", "cabextract", "libmsi-1.0.so.0", "libgsf-1.so.114", "libgcab-1.0.so.0") + val msitools = listOf("msiinfo", "cabextract", "7z", "7z.so", "libmsi-1.0.so.0", "libgsf-1.so.114", "libgcab-1.0.so.0") .map { "usr/local/lib/droiddeck-msitools/$it" } + - listOf("NOTICE", "GPL-2", "GPL-3", "LGPL-2.1").map { "usr/local/share/licenses/droiddeck-msitools/$it" } + listOf("NOTICE", "GPL-2", "GPL-3", "LGPL-2.1", "7zip-License", "7zip-unRarLicense").map { "usr/local/share/licenses/droiddeck-msitools/$it" } val fexPreloads = listOf("x86_64", "i386").flatMap { arch -> listOf("libblsession.so", "libfakeinput.so").map { "$arch/$it" to "usr/local/lib/droiddeck-fex/$arch/$it" } } + listOf("libfaultreport.so", "libthunkaudit.so", "libvulkan-thunk.so").map { "x86_64/$it" to "usr/local/lib/droiddeck-fex/x86_64/$it" } diff --git a/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt b/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt index d344127535..850ef5059c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt @@ -47,16 +47,35 @@ object WinComponents { // at the same path, so the native file at that path is the one the registration loads. private val FILE_STEPS = setOf("download_archive", "archive_extract", "copy_dll", "copy_file", "override_dll", "register_dll") private val INSTALLER_STEPS = setOf("install_exe", "install_msi") + /** + * Steps done here without any installer running: a Windows version or registry value for the + * prefix (written by droiddeck-wincomponents with the rest), files picked out of a cabinet, and + * "uninstall Wine Mono", which the mscoree override beside it already does for the game. + */ + private val OFFLINE_STEPS = setOf("set_windows", "set_register_key", "get_from_cab", "uninstall") + /** + * Installers that are their own setup program (NSIS, InnoSetup, IExpress scripts): nothing + * inside them is a package to lay out, so they wait for a way to run them. + */ + private val RUNS_OWN_SETUP = setOf( + "K-Lite", "ffdshow", "lavfilters702", "lavfilters741", "quicktime72", "dirac", "webview2", "aairruntime", + "gfw", "ie8_kb2936068", "art2kmin", "art2k7min", "vcredist6sp6", "VulkanRT", "jet40", "mdac28", "oalinst", + "dotnet20", "dotnet20sp1", "dotnet35", "dotnet35sp1", + ) private const val MSI_INSTALL = "/usr/local/bin/droiddeck-msi-install" - /** Where a downloaded .msi waits for the installer, inside the runtime so it can read it. */ + /** Where a downloaded package or installer waits for the installer, inside the runtime so it can read it. */ private const val MSI_CACHE = "$STORE/.packages" /** Catalog entries Proton already provides newer copies of, which an install would only shadow. */ - private val PROTON_PROVIDES = setOf("gecko") + private val PROTON_PROVIDES = setOf("gecko", "mono") + /** The keys the catalog nests under "environment" on a few .NET entries. */ + private val NESTED_KEYS = setOf("url", "file_name", "file_checksum", "file_size") fun validId(id: String): Boolean = ID.matches(id) && ".." !in id class Step(val action: String, val json: JSONObject) { - fun str(key: String): String = json.optString(key, "") + fun str(key: String): String = json.optString(key, "").ifEmpty { + if (key in NESTED_KEYS) json.optJSONObject("environment")?.optString(key, "").orEmpty() else "" + } } class Component( @@ -66,10 +85,27 @@ object WinComponents { enum class Support { READY, NEEDS_INSTALLER, UNSUPPORTED } - /** A step that installs a Windows Installer package, which droiddeck-msi-install does here. */ - private fun isMsi(step: Step): Boolean = step.action in INSTALLER_STEPS && step.str("url").startsWith("https://") && + /** The two halves of an install the page shows one after the other, each 0..100. */ + enum class Phase { DOWNLOAD, INSTALL } + + /** Progress: the component being worked on, what it is doing, the phase, 0..100 or -1 when unknown. */ + class Progress(val component: String, val stage: String, val phase: Phase, val percent: Int) + + /** The percent a droiddeck-msi-install progress line carries ("progress 37% placing files"), or -1. */ + private val PERCENT_LINE = Regex("""^(\d{1,3})% (.*)$""") + private fun engineLine(text: String): Pair<String, Int> = + PERCENT_LINE.find(text)?.let { it.groupValues[2] to it.groupValues[1].toInt().coerceIn(0, 100) } ?: (text to -1) + + /** + * A step that installs a Windows Installer package, or an installer .exe that is a wrapper + * around packages (a WiX bundle, a self-extracting cabinet or 7-Zip archive), which + * droiddeck-msi-install lays out here without running it. + */ + private fun isPackage(step: Step): Boolean = step.action in INSTALLER_STEPS && step.str("url").startsWith("https://") && (step.action == "install_msi" || listOf(step.str("file_name"), step.str("url").substringBefore('?')) - .any { it.endsWith(".msi", ignoreCase = true) }) + .any { it.endsWith(".msi", ignoreCase = true) || it.endsWith(".exe", ignoreCase = true) }) + + private fun protonProvides(name: String): Boolean = name in PROTON_PROVIDES || name.startsWith("mono-") fun fetch(): List<Component>? { val body = Downloader.downloadString(CATALOG_URL) ?: return null @@ -94,13 +130,14 @@ object WinComponents { /** Whether [c] can be installed here, its bundled components included. */ fun support(c: Component, all: Map<String, Component>, depth: Int = 0): Support { - if (c.status != "ready" || depth > 8 || c.name in PROTON_PROVIDES) return Support.UNSUPPORTED + if (c.status != "ready" || depth > 8 || protonProvides(c.name)) return Support.UNSUPPORTED val installers = c.steps.filter { it.action in INSTALLER_STEPS } + fun offline(step: Step) = step.action == "delete_dlls" || step.action in OFFLINE_STEPS || step.action in FILE_STEPS && fileStepOk(step) val own = when { - installers.isNotEmpty() && installers.all { isMsi(it) } && - c.steps.all { it in installers || it.action == "delete_dlls" || it.action in FILE_STEPS && fileStepOk(it) } -> Support.READY + installers.isNotEmpty() && c.name !in RUNS_OWN_SETUP && installers.all { isPackage(it) } && + c.steps.all { it in installers || offline(it) } -> Support.READY installers.isNotEmpty() -> Support.NEEDS_INSTALLER - c.steps.all { it.action in FILE_STEPS && fileStepOk(it) } -> Support.READY + c.steps.all { offline(it) } -> Support.READY else -> Support.UNSUPPORTED } if (own != Support.READY) return own @@ -109,9 +146,10 @@ object WinComponents { } private fun fileStepOk(step: Step): Boolean = when (step.action) { + // An .exe or .cab archive (DirectX's redistributable) is opened by the runtime's tools. "download_archive", "archive_extract" -> step.str("url").let { url -> !url.startsWith("http") || url.startsWith("https://github.com/") && - url.substringBefore('?').let { it.endsWith(".tar.xz") || it.endsWith(".zip") } + url.substringBefore('?').lowercase().let { it.endsWith(".tar.xz") || it.endsWith(".zip") || it.endsWith(".exe") || it.endsWith(".cab") } } "copy_dll", "copy_file" -> ".." !in step.str("dest") else -> true @@ -131,7 +169,7 @@ object WinComponents { * Installs [c] and the components it bundles that are not installed yet. [onProgress] gets a * line for the step and 0..100 (or -1). Returns null on success, else a message. */ - fun install(context: Context, c: Component, all: Map<String, Component>, onProgress: (String, Int) -> Unit): String? { + fun install(context: Context, c: Component, all: Map<String, Component>, onProgress: (Progress) -> Unit): String? { if (support(c, all) != Support.READY) return "${c.name} cannot be installed here yet" val order = ArrayList<Component>() fun visit(x: Component, depth: Int) { @@ -146,7 +184,7 @@ object WinComponents { val archives = HashMap<String, File>() return try { for (x in order) { - onProgress(x.name, -1) + onProgress(Progress(x.name, x.name, Phase.DOWNLOAD, -1)) installOne(context, x, work, archives, onProgress)?.let { return "${x.name}: $it" } } null @@ -158,18 +196,27 @@ object WinComponents { } } - private fun installOne(context: Context, c: Component, work: File, archives: HashMap<String, File>, onProgress: (String, Int) -> Unit): String? { + private fun installOne(context: Context, c: Component, work: File, archives: HashMap<String, File>, onProgress: (Progress) -> Unit): String? { val root = LinuxRuntime.rootDir(context) if (!root.isDirectory) return "The Linux runtime is not installed" val staging = File(root, "$STORE/.${c.name}.new").apply { FileUtils.delete(this); mkdirs() } val overrides = ArrayList<String>() var source: File? = null var msi: JSONObject? = null + // The registry values the steps themselves set (a Windows version, a key), beside the packages'. + val registry = ArrayList<JSONObject>() + // Archives the runtime's tools opened (installer .exe, cabinets), by the name the catalog + // calls them, and the folders they were opened into; cleaned up at the end. + val runtimeArchives = HashMap<String, File>() + val runtimeDirs = ArrayList<File>() + val cabs = HashMap<String, File>() + val temp = File(root, "$MSI_CACHE/.t-${c.name}").apply { FileUtils.delete(this) } + try { for (step in c.steps) when (step.action) { // A component may have several packages (PowerShell's 32- and 64-bit): each adds to the // same folder, the first one names the component. "install_msi", "install_exe" -> { - val (result, problem) = installMsi(context, c, step, staging, onProgress) + val (result, problem) = installPackage(context, c, step, staging, onProgress) if (result == null) return problem msi = msi?.apply { val notes = optJSONArray("notes") ?: JSONArray().also { put("notes", it) } @@ -183,19 +230,84 @@ object WinComponents { if (!url.startsWith("http")) continue source = archives[url] ?: run { val name = url.substringBefore('?').substringAfterLast('/') - val file = File(work, "${archives.size}-$name") - val ok = Downloader.downloadFile(url, file, false) { f -> onProgress("${c.name}: $name", if (f < 0) -1 else Math.round(f * 100f)) } + // A zip or tar.xz is opened here; an .exe or .cab by the runtime's tools, so it + // is downloaded where the runtime can read it and kept for get_from_cab steps. + val byRuntime = name.lowercase().let { it.endsWith(".exe") || it.endsWith(".cab") } + val file = if (byRuntime) File(root, "$MSI_CACHE/${archives.size}-$name").apply { parentFile?.mkdirs() } + else File(work, "${archives.size}-$name") + val ok = Downloader.downloadFile(url, file, false) { f -> onProgress(Progress(c.name, name, Phase.DOWNLOAD, if (f < 0) -1 else Math.round(f * 100f))) } if (!ok) return "download failed: $name" step.str("file_checksum").takeIf { it.length == 32 }?.let { md5 -> if (!digest(file, "MD5").equals(md5, ignoreCase = true)) return "checksum mismatch: $name" } - onProgress("${c.name}: unpacking $name", -1) - val dir = File(work, "${archives.size}-x").apply { mkdirs() } - if (!extract(file, dir)) return "could not unpack $name" - file.delete() + onProgress(Progress(c.name, "unpacking $name", Phase.INSTALL, -1)) + val dir = if (byRuntime) { + runtimeArchives[step.str("file_name").ifEmpty { name }] = file + runtimeDirs += file + unpackInRuntime(context, c, file, "${archives.size}-x", onProgress)?.also { runtimeDirs += it } + ?: return "could not unpack $name" + } else { + File(work, "${archives.size}-x").apply { mkdirs() }.also { if (!extract(file, it)) return "could not unpack $name" } + .also { file.delete() } + } dir.also { archives[url] = it } } } + // Files picked out of a cabinet: one the catalog downloaded (DirectX's redistributable, + // a self-extracting cabinet), or cabinets an earlier pick put under temp/. + "get_from_cab" -> { + val pattern = step.str("source") + val containers = runtimeArchives[pattern]?.let { listOf(it) } ?: run { + val folder = File(temp, pattern.substringBeforeLast('/', "").replace('\\', '/')) + val rx = globRegex(pattern.substringAfterLast('/')) + folder.listFiles { f -> f.isFile && rx.matches(f.name) }?.sortedBy { it.name }.orEmpty() + } + if (containers.isEmpty()) return "nothing is called $pattern" + val dest = step.str("dest").replace('\\', '/') + for (container in containers) { + val unpacked = cabs.getOrPut(container.path) { + unpackInRuntime(context, c, container, "c${cabs.size}", onProgress)?.also { runtimeDirs += it } + ?: return "could not open ${container.name}" + } + if (dest.startsWith("temp/", ignoreCase = true)) { + copyMatching(unpacked, step.str("file_name"), File(temp, dest.substring(5).trimEnd('/')), null) + } else { + val (arch, base) = when (dest.substringBefore('/').lowercase()) { + "win64", "system32" -> "win64" to "system32" + "win32", "syswow64" -> "win32" to "syswow64" + else -> null to "system32" + } + val sub = dest.substringAfter('/', "").trimEnd('/') + copyMatching(unpacked, step.str("file_name"), File(staging, if (sub.isEmpty()) base else "$base/$sub"), arch) + } + } + } + // The Windows version a game sees: HKCU\Software\Wine "Version", the last word wins. + "set_windows" -> step.str("version").takeIf { it.isNotEmpty() }?.let { version -> + registry.removeAll { it.optString("key") == "Software\\Wine" && it.optString("name") == "Version" } + registry += JSONObject().put("hive", "HKCU").put("key", "Software\\Wine").put("name", "Version").put("type", "sz").put("data", version) + } + "set_register_key" -> { + val full = step.str("key").replace("\\\\", "\\").trim('\\') + val hive = when (full.substringBefore('\\').uppercase()) { + "HKLM", "HKEY_LOCAL_MACHINE" -> "HKLM" + "HKCU", "HKEY_CURRENT_USER" -> "HKCU" + else -> return "a registry key outside HKLM and HKCU: $full" + } + val key = full.substringAfter('\\', "") + if (key.isEmpty()) return "a registry value without a key: $full" + val data = step.str("data") + val value = JSONObject().put("hive", hive).put("key", key).put("name", step.str("value")) + when (step.str("type").uppercase()) { + "REG_DWORD" -> value.put("type", "dword").put("data", data.trim().let { it.removePrefix("0x").toLongOrNull(if (it.startsWith("0x")) 16 else 10) ?: 0L }) + "REG_EXPAND_SZ" -> value.put("type", "expand_sz").put("data", data) + else -> value.put("type", "sz").put("data", data) + } + registry += value + } + // "Uninstall Wine Mono" before a real .NET Framework: the mscoree override the catalog + // sets beside it is what takes Mono out of the game's way; nothing to remove here. + "uninstall" -> Unit "copy_dll", "copy_file" -> { val from = source ?: return "a copy step before any archive" val dest = step.str("dest") @@ -226,6 +338,17 @@ object WinComponents { "register_dll" -> Unit else -> return "unsupported step ${step.action}" } + } finally { + runtimeDirs.forEach { FileUtils.delete(it) } + FileUtils.delete(temp) + } + if (registry.isNotEmpty()) { + // The packages' values are already in registry.json; the steps' own go after them. + val file = File(staging, "registry.json") + val values = runCatching { JSONObject(file.readText()).optJSONArray("values") }.getOrNull() ?: JSONArray() + registry.forEach { values.put(it) } + FileUtils.writeString(file, JSONObject().put("version", 1).put("values", values).toString(1)) + } val version = msi?.optString("version")?.takeIf { it.isNotEmpty() } ?: Regex("""\b(\d+\.\d+(\.\d+)*)\b""").find(c.description)?.value ?: "catalog" val meta = JSONObject() @@ -240,31 +363,64 @@ object WinComponents { return null } + /** The regex for a catalog file pattern: * matches anything, case does not matter. */ + private fun globRegex(pattern: String) = + Regex("^" + pattern.split("*").joinToString(".*") { Regex.escape(it) } + "$", RegexOption.IGNORE_CASE) + /** - * Downloads the component's .msi into the runtime and has droiddeck-msi-install lay it out in - * [staging]. Returns the installer's result (product, version, counts, notes), or null and why. - * Everything the installer prints also goes to Download/DroidDeck/tools. + * Has the runtime's tools open [archive] (an installer .exe, a cabinet, a self-extracting + * one) into a folder of the package cache called [tag]; that folder, or null and a log line. */ - private fun installMsi(context: Context, c: Component, step: Step, staging: File, onProgress: (String, Int) -> Unit): Pair<JSONObject?, String> { + private fun unpackInRuntime(context: Context, c: Component, archive: File, tag: String, onProgress: (Progress) -> Unit): File? { + val root = LinuxRuntime.rootDir(context) + val cache = File(root, MSI_CACHE) + val dir = File(cache, ".x-$tag").apply { FileUtils.delete(this); mkdirs() } + val inside = archive.relativeTo(root).path + var problem: String? = null + var ok = false + val status = GuestCommand.run(context, listOf(MSI_INSTALL, "--unpack", "/$inside", "/$MSI_CACHE/${dir.name}"), + logName = "wincomponents-${c.name}") { line -> + when { + line.startsWith("progress ") -> engineLine(line.removePrefix("progress ")).let { (text, _) -> onProgress(Progress(c.name, text, Phase.INSTALL, -1)) } + line.startsWith("result ") -> ok = true + line.startsWith("error ") -> problem = line.removePrefix("error ") + } + } + if (status != 0 || !ok) { + Log.w(TAG, "unpack ${archive.name}: ${problem ?: "status $status"}") + FileUtils.delete(dir) + return null + } + return dir + } + + /** + * Downloads the component's package (.msi) or installer (.exe) into the runtime and has + * droiddeck-msi-install lay it out in [staging]. Returns the installer's result (product, + * version, counts, notes), or null and why. Everything the installer prints also goes to + * Download/DroidDeck/tools. + */ + private fun installPackage(context: Context, c: Component, step: Step, staging: File, onProgress: (Progress) -> Unit): Pair<JSONObject?, String> { val root = LinuxRuntime.rootDir(context) val url = step.str("url") val name = url.substringBefore('?').substringAfterLast('/') val cache = File(root, MSI_CACHE).apply { mkdirs() } - val file = File(cache, "${c.name}.msi") + val ext = if (listOf(step.str("file_name"), name).any { it.endsWith(".exe", ignoreCase = true) }) "exe" else "msi" + val file = File(cache, "${c.name}-${c.steps.indexOf(step)}.$ext") try { - if (!Downloader.downloadFile(url, file, false) { f -> onProgress("${c.name}: $name", if (f < 0) -1 else Math.round(f * 100f)) }) { + if (!Downloader.downloadFile(url, file, false) { f -> onProgress(Progress(c.name, name, Phase.DOWNLOAD, if (f < 0) -1 else Math.round(f * 100f))) }) { return null to "download failed: $name" } step.str("file_checksum").takeIf { it.length == 32 }?.let { md5 -> if (!digest(file, "MD5").equals(md5, ignoreCase = true)) return null to "checksum mismatch: $name" } - onProgress("${c.name}: installing", -1) + onProgress(Progress(c.name, "installing", Phase.INSTALL, 0)) var result: JSONObject? = null var problem: String? = null val status = GuestCommand.run(context, listOf(MSI_INSTALL, "/$MSI_CACHE/${file.name}", "/$STORE/${staging.name}"), logName = "wincomponents-${c.name}") { line -> when { - line.startsWith("progress ") -> onProgress("${c.name}: ${line.removePrefix("progress ")}", -1) + line.startsWith("progress ") -> engineLine(line.removePrefix("progress ")).let { (text, pct) -> onProgress(Progress(c.name, text, Phase.INSTALL, pct)) } line.startsWith("result ") -> result = runCatching { JSONObject(line.removePrefix("result ")) }.getOrNull() line.startsWith("error ") -> problem = line.removePrefix("error ") } @@ -273,7 +429,7 @@ object WinComponents { if (status != 0 || done == null) return null to (problem ?: "the installer stopped (status $status)") return done to "" } catch (e: Exception) { - Log.e(TAG, "msi ${c.name}", e) + Log.e(TAG, "package ${c.name}", e) return null to (e.message ?: "the installer failed") } finally { file.delete() diff --git a/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt index 9538f345e7..25e9539277 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/WinComponentsEditor.kt @@ -82,7 +82,12 @@ internal fun WinComponentsDialog( // Detector names (oalinst, physx...) already in the prefix from elsewhere. var present by remember { mutableStateOf(emptySet<String>()) } var picks by remember { mutableStateOf(WinComponents.picks(context, appKey)) } - var progress by remember { mutableStateOf<Pair<String, Int>?>(null) } + // The row being worked on: its id, the latest progress (a dependency's while that installs + // first), and whether it is turning off rather than on. The row greys out and shows the bar + // the moment the switch is pressed, and only that row; the rest of the page stays as it is. + var busyId by remember { mutableStateOf<String?>(null) } + var progress by remember { mutableStateOf<WinComponents.Progress?>(null) } + var reverting by remember { mutableStateOf(false) } var error by remember { mutableStateOf<String?>(null) } // The component the error is about, so it shows on its own row and not only at the bottom. var failed by remember { mutableStateOf<String?>(null) } @@ -91,6 +96,7 @@ internal fun WinComponentsDialog( // pad, so the d-pad always has somewhere to start; LB and RB jump between the sections, since // the full list runs past sixty switches. val recFocus = remember { FocusRequester() } + val activeFocus = remember { FocusRequester() } val allFocus = remember { FocusRequester() } val waitFocus = remember { FocusRequester() } val doneFocus = remember { FocusRequester() } @@ -116,22 +122,35 @@ internal fun WinComponentsDialog( } fun toggle(id: String, on: Boolean) { // Switches stay enabled during a download - a disabled one drops the pad's focus - so a press - // then is ignored here. - if (progress != null) return + // then is ignored here; the busy row is greyed instead. + if (busyId != null) return error = null failed = null - if (!on) { setPicks(picks - id); return } + if (!on) { + // Turning off is a saved pick, quick, but the row greys out until it is written so the + // press is seen to land; the files leave the prefix at the game's next launch. + busyId = id; reverting = true; progress = null + val next = picks - id + picks = next + coroutine.launch { + withContext(Dispatchers.IO) { WinComponents.setPicks(context, appKey, next) } + busyId = null; reverting = false + } + return + } val all = catalog.orEmpty() val c = all[id] if (id in installed || c == null) { setPicks(picks + id); return } - progress = id to -1 + busyId = id; reverting = false + progress = WinComponents.Progress(id, "", WinComponents.Phase.DOWNLOAD, -1) coroutine.launch { val problem = withContext(Dispatchers.IO) { - WinComponents.install(context, c, all) { stage, percent -> progress = stage to percent } + WinComponents.install(context, c, all) { p -> progress = p } } progress = null installed = withContext(Dispatchers.IO) { WinComponents.installedIds(context).toSet() } if (problem != null) { error = problem; failed = id } else setPicks(picks + id) + busyId = null } } @@ -142,12 +161,18 @@ internal fun WinComponentsDialog( val recIds = if (all == null) emptyList() else recommended.distinctBy { installable(it.componentName, all) } val ready = all.orEmpty().values.filter { WinComponents.support(it, all.orEmpty()) == Support.READY }.map { it.name } val extra = (installed + picks).filter { all?.containsKey(it) != true } - val list = (ready + extra).distinct().sortedBy { WinComponentNames.of(it).lowercase() } + // What is turned on for this game gets its own section under the recommendations, which stay + // where they are; the rest of the list is everything else, so a component turned off goes back + // to its place in the alphabet. + val recSet = recIds.map { installable(it.componentName, all.orEmpty()) }.toSet() + val active = picks.filter { it !in recSet && (all?.containsKey(it) == true || it in installed) }.distinct() + .sortedBy { WinComponentNames.of(it).lowercase() } + val list = (ready + extra).distinct().filter { it !in active }.sortedBy { WinComponentNames.of(it).lowercase() } val waiting = all.orEmpty().values.filter { WinComponents.support(it, all.orEmpty()) == Support.NEEDS_INSTALLER } .map { it.name }.sortedBy { WinComponentNames.of(it).lowercase() } val hasRec = all != null && recIds.any { supportOf(installable(it.componentName, all)) == Support.READY } - val sections = listOfNotNull(recFocus.takeIf { hasRec }, allFocus.takeIf { list.isNotEmpty() }, - waitFocus.takeIf { waiting.isNotEmpty() }, doneFocus) + val sections = listOfNotNull(recFocus.takeIf { hasRec }, activeFocus.takeIf { active.isNotEmpty() }, + allFocus.takeIf { list.isNotEmpty() }, waitFocus.takeIf { waiting.isNotEmpty() }, doneFocus) var section by remember { mutableStateOf(0) } fun jump(step: Int) { section = (section + step + sections.size) % sections.size @@ -164,7 +189,7 @@ internal fun WinComponentsDialog( withFrameNanos { } runCatching { target.requestFocus() } withFrameNanos { } - if (switchFocused || (target !== recFocus && target !== allFocus)) return@LaunchedEffect + if (switchFocused || (target !== recFocus && target !== activeFocus && target !== allFocus)) return@LaunchedEffect } } @@ -189,9 +214,24 @@ internal fun WinComponentsDialog( // The catalog key stays in the detail: it is what a log or a bug report names. val detail = listOfNotNull(reason, c?.description?.takeIf { it.isNotEmpty() }, status, id).joinToString(" · ") val usable = support == Support.READY + val busy = id == busyId + val caption = when { + !busy -> null + reverting -> stringResource(R.string.wincomp_reverting) + else -> progress?.let { p -> + val stage = if (p.component != id && p.stage.isNotEmpty()) "${WinComponentNames.of(p.component)}: ${p.stage}" else p.stage + val text = when (p.phase) { + WinComponents.Phase.DOWNLOAD -> stringResource(R.string.wincomp_downloading, stage) + WinComponents.Phase.INSTALL -> stringResource(R.string.wincomp_installing, stage) + } + if (p.percent >= 0) "$text · ${p.percent}%" else text + } + } ComponentRow( WinComponentNames.of(id), detail, checked = id in picks, enabled = usable || id in picks, - dim = !usable, modifier = (if (usable && focus != null) Modifier.focusRequester(focus) else Modifier) + dim = !usable || busy, busy = busy, caption = caption, + percent = if (busy && !reverting) progress?.percent ?: -1 else -1, + modifier = (if (usable && focus != null) Modifier.focusRequester(focus) else Modifier) .onFocusChanged { if (it.isFocused) switchFocused = true }, ) { on -> toggle(id, on) } } @@ -220,6 +260,11 @@ internal fun WinComponentsDialog( } } else if (all != null && gameDir != null) Small(stringResource(R.string.wincomp_no_recommendation)) + if (active.isNotEmpty()) { + Section(stringResource(R.string.wincomp_active, active.size)) + val focusOf = firstUsable(active, activeFocus) + Panel { active.forEachIndexed { i, id -> if (i > 0) Divider(); Item(id, null, focusOf(id)) } } + } if (list.isNotEmpty()) { Section(stringResource(R.string.wincomp_all, list.size)) val focusOf = firstUsable(list, allFocus) @@ -238,16 +283,11 @@ internal fun WinComponentsDialog( if (showWaiting) Panel { waiting.forEachIndexed { i, id -> if (i > 0) Divider(); Item(id, null, null) } } } - progress?.let { (stage, percent) -> - Small(if (percent >= 0) "$stage · $percent%" else stage) - if (percent >= 0) LinearProgressIndicator(progress = { percent / 100f }, modifier = Modifier.fillMaxWidth()) - else LinearProgressIndicator(Modifier.fillMaxWidth()) - } error?.let { Small(it, error = true) } Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(top = 4.dp)) { Small(stringResource(R.string.wincomp_next_launch)) Spacer(Modifier.weight(1f)) - PrimaryButton(stringResource(R.string.game_env_done), enabled = progress == null, modifier = Modifier.focusRequester(doneFocus), onClick = close) + PrimaryButton(stringResource(R.string.game_env_done), enabled = busyId == null, modifier = Modifier.focusRequester(doneFocus), onClick = close) } } } @@ -264,17 +304,34 @@ private fun Panel(content: @Composable androidx.compose.foundation.layout.Column private fun Divider() = Box(Modifier.fillMaxWidth().padding(horizontal = 14.dp).heightIn(min = 1.dp, max = 1.dp).background(LocalPalette.current.line)) +/** + * One component: name, detail and its switch; while [busy] the row is greyed and a thin bar runs + * across it with [caption] above (the download, then the install, each 0..100 as [percent], or + * indeterminate at -1). The switch stays enabled while busy so a pad's focus does not drop; the + * press is ignored by the caller instead. + */ @Composable -private fun ComponentRow(name: String, detail: String, checked: Boolean, enabled: Boolean, dim: Boolean, modifier: Modifier, onChange: (Boolean) -> Unit) { +private fun ComponentRow( + name: String, detail: String, checked: Boolean, enabled: Boolean, dim: Boolean, busy: Boolean = false, + caption: String? = null, percent: Int = -1, modifier: Modifier, onChange: (Boolean) -> Unit, +) { val colors = MaterialTheme.colorScheme - Row( - verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), - modifier = Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 10.dp).alpha(if (dim) 0.55f else 1f), - ) { - Column(Modifier.weight(1f)) { - Text(name, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground) - if (detail.isNotEmpty()) Text(detail, fontSize = 12.sp, lineHeight = 16.sp, color = colors.onSurfaceVariant) + Column(Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 10.dp)) { + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(12.dp), + modifier = Modifier.fillMaxWidth().alpha(if (dim) 0.55f else 1f), + ) { + Column(Modifier.weight(1f)) { + Text(name, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground) + if (detail.isNotEmpty()) Text(detail, fontSize = 12.sp, lineHeight = 16.sp, color = colors.onSurfaceVariant) + } + ToggleSwitch(checked, enabled, name, modifier.alpha(if (busy) 0.5f else 1f), onChange) + } + if (busy) { + if (!caption.isNullOrEmpty()) Text(caption, fontSize = 12.sp, lineHeight = 16.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(top = 4.dp)) + val bar = Modifier.fillMaxWidth().padding(top = 6.dp).heightIn(min = 4.dp, max = 4.dp) + if (percent >= 0) LinearProgressIndicator(progress = { percent / 100f }, modifier = bar) + else LinearProgressIndicator(modifier = bar) } - ToggleSwitch(checked, enabled, name, modifier, onChange) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b12ca68dba..bc495bc9cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -326,16 +326,20 @@ <string name="wincomp_found_steam">Steam installs %1$s with this game</string> <string name="wincomp_already_here">Already in this game</string> <string name="wincomp_no_recommendation">Nothing in the game\'s folder points at a component.</string> + <string name="wincomp_active">Installed for this game (%1$d)</string> <string name="wincomp_all">All components (%1$d)</string> <string name="wincomp_installers">Need a Windows installer (%1$d)</string> - <string name="wincomp_installers_note">These run a real installer inside the prefix (.NET, PhysX, XNA, the full Visual C++ installers…). DroidDeck cannot do that yet; it is coming next.</string> + <string name="wincomp_installers_note">These are their own setup programs (codec packs, browsers, old runtimes) with no package inside to take apart. DroidDeck cannot run a Windows setup program yet; it is coming next.</string> <string name="wincomp_show">Show</string> <string name="wincomp_hide">Hide</string> - <string name="wincomp_needs_installer">Needs a Windows installer (coming)</string> + <string name="wincomp_needs_installer">Runs its own setup program (coming)</string> <string name="wincomp_unsupported">Not supported here yet</string> <string name="wincomp_loading">Loading the components…</string> <string name="wincomp_failed">Could not install: %1$s</string> <string name="wincomp_downloaded">Downloaded</string> + <string name="wincomp_downloading">Downloading %1$s</string> + <string name="wincomp_installing">Installing: %1$s</string> + <string name="wincomp_reverting">Turning off…</string> <string name="wincomp_download_size">%1$s download</string> <string name="wincomp_next_launch">Takes effect the next time the game starts.</string> <string name="mode_stretch169">Stretch 16:9 games</string> diff --git a/app/src/test/java/com/droiddeck/launcher/session/WinComponentsSupportTest.kt b/app/src/test/java/com/droiddeck/launcher/session/WinComponentsSupportTest.kt index 2e43be1ce9..12a7c84441 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/WinComponentsSupportTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/WinComponentsSupportTest.kt @@ -32,15 +32,49 @@ class WinComponentsSupportTest { step("install_msi", "url" to "$base/ps-x86.msi"), step("install_msi", "url" to "$base/ps-x64.msi")))) } - @Test fun otherInstallersStillWait() { - assertEquals(Support.NEEDS_INSTALLER, support(component("dotnet48", step("install_exe", "url" to "$base/ndp48.exe")))) - // An uninstall step first (wine-mono) is not something done here. - assertEquals(Support.NEEDS_INSTALLER, support(component("mono", step("uninstall"), - step("install_msi", "url" to "$base/mono.msi")))) + @Test fun installerWrappersInstallHere() { + // A WiX bundle, a self-extracting cabinet or 7-Zip archive: the packages inside are laid out. + assertEquals(Support.READY, support(component("vcredist2022", + step("install_exe", "url" to "$base/vcredist2022__VC_redist.x86.exe", "file_name" to "VC_redist.x86.exe"), + step("install_exe", "url" to "$base/vcredist2022__VC_redist.x64.exe", "file_name" to "VC_redist.x64.exe"), + step("override_dll", "dll" to "vcruntime140", "type" to "native,builtin")))) + // .NET Framework: "uninstall Wine Mono", the Windows version, the installer, a registry key. + assertEquals(Support.READY, support(component("dotnet40", + step("uninstall", "file_name" to "Wine Mono"), step("set_windows", "version" to "win7"), + step("install_exe", "url" to "https://download.microsoft.com/x/dotNetFx40_Full_x86_x64.exe", "file_name" to "dotNetFx40_Full_x86_x64.exe"), + step("set_windows", "version" to "win10"), + step("set_register_key", "key" to "HKLM\\\\Software\\\\Microsoft\\\\NET Framework Setup\\\\NDP\\\\v4\\\\Full", "value" to "Install", "data" to "0001", "type" to "REG_DWORD"), + step("override_dll", "dll" to "mscoree", "type" to "native")))) + } + + @Test fun nestedCatalogKeysAreRead() { + // A few .NET entries keep url and file_name under "environment". + val nested = WinComponents.Step("install_exe", JSONObject().put("environment", JSONObject() + .put("WINEDLLOVERRIDES", "fusion=b").put("file_name", "ndp48-x86-x64-allos-enu.exe") + .put("url", "https://download.visualstudio.microsoft.com/x/ndp48-x86-x64-allos-enu.exe"))) + assertEquals("ndp48-x86-x64-allos-enu.exe", nested.str("file_name")) + assertEquals(Support.READY, support(component("dotnet48", nested))) + } + + @Test fun cabinetPicksInstallHere() { + assertEquals(Support.READY, support(component("xact", + step("download_archive", "url" to "$base/directx_Jun2010_redist.exe", "file_name" to "directx_Jun2010_redist.exe"), + step("get_from_cab", "source" to "directx_Jun2010_redist.exe", "file_name" to "*XACT_x86*.cab", "dest" to "temp/XACT_x86/"), + step("get_from_cab", "source" to "XACT_x86/*.cab", "file_name" to "xactengine*.dll", "dest" to "win32/"), + step("override_dll", "dll" to "xactengine3_7", "type" to "native,builtin"), + step("register_dll", "dlls" to "")))) + } + + @Test fun setupProgramsStillWait() { + // NSIS / InnoSetup installers hold no package to lay out. + assertEquals(Support.NEEDS_INSTALLER, support(component("K-Lite", + step("install_exe", "url" to "$base/K-Lite.exe", "file_name" to "K-Lite_1960.exe")))) assertEquals(Support.NEEDS_INSTALLER, support(component("plain", step("install_msi", "url" to "http://example.com/a.msi")))) } @Test fun whatProtonShipsIsNotOffered() { assertEquals(Support.UNSUPPORTED, support(component("gecko", step("install_msi", "url" to "$base/gecko.msi")))) + assertEquals(Support.UNSUPPORTED, support(component("mono", step("uninstall"), step("install_msi", "url" to "$base/mono.msi")))) + assertEquals(Support.UNSUPPORTED, support(component("mono-10.4.1", step("install_msi", "url" to "$base/mono.msi")))) } } diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-msi-install b/tools/linuxfs/overlay/usr/local/bin/droiddeck-msi-install index 3df6861736..98b1af030e 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-msi-install +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-msi-install @@ -1,7 +1,8 @@ #!/usr/bin/python3 """Installs a Windows Installer package (.msi) the way Wine's own msiexec would, without running it. -droiddeck-msi-install <package.msi> <component dir> +droiddeck-msi-install <package.msi | installer.exe> <component dir> +droiddeck-msi-install --unpack <archive | installer.exe | cabinet> <dir> A .msi is a database: which files go to which folder, which registry values to write, which .NET and side-by-side assemblies to register. This reads those tables (msiinfo), unpacks the package's @@ -11,15 +12,26 @@ for droiddeck-wincomponents, which puts it into a game's prefix at launch: <component dir>/drive_c/... the files, at the paths they get on 64-bit Windows <component dir>/registry.json the registry values, in the view (32/64-bit) Windows uses +An installer .exe is opened the same way when it is a wrapper around packages: a WiX bundle +(Visual C++ 2012 and later, the .NET Core and 5+ runtimes: the packages sit in a cabinet the +bundle's own header locates, named in its manifest), a self-extracting cabinet (Visual C++ 2005 +to 2010) or a self-extracting 7-Zip archive (.NET Framework 4.0 to 4.8). The packages inside are +laid out one after the other into the same component dir; the 32- and 64-bit ones both, an ARM64 +one never. An .exe that is its own setup program (NSIS, InnoSetup) holds no package: an error says so. + What a package does by running its own code (custom actions such as DXSETUP, self-registering DLLs) cannot be done this way; each is named in the notes, so nothing is left out silently. +--unpack opens an archive or installer for the caller to take files out of (cabextract for a +cabinet or self-extracting cabinet, 7-Zip for the rest) and prints the files it found. + Prints "progress <text>" lines while it works and ends with "result <json>": the counts and notes. """ import json import os import re import shutil +import struct import subprocess import sys import tempfile @@ -28,6 +40,11 @@ from pathlib import Path TOOLS = Path(os.environ.get("DROIDDECK_MSITOOLS", "/usr/local/lib/droiddeck-msitools")) MSIINFO = os.environ.get("DROIDDECK_MSIINFO", str(TOOLS / "msiinfo")) CABEXTRACT = os.environ.get("DROIDDECK_CABEXTRACT", str(TOOLS / "cabextract")) +SEVENZIP = os.environ.get("DROIDDECK_7Z", str(TOOLS / "7z")) +MSI_MAGIC = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" +BURN_MAGIC = 0x00F14300 +# A bundle's manifest names ARM64 packages too; a game's prefix wants the x86 and x64 ones. +SKIP_PLATFORMS = ("arm64", "arm") def tool_env(): @@ -89,8 +106,21 @@ class MsiError(Exception): pass -def progress(text): - print("progress " + text, flush=True) +def progress(text, fraction=None): + """A progress line: "progress <text>", or "progress NN% <text>" when how far along is known.""" + if fraction is None: + print("progress " + text, flush=True) + else: + print("progress %d%% %s" % (max(0, min(100, int(fraction * 100))), text), flush=True) + + +class Span: + """The slice of the whole job one package's progress maps into (a wrapper runs several).""" + def __init__(self, lo=0.0, hi=1.0): + self.lo, self.hi = lo, hi + + def report(self, fraction, text): + progress(text, self.lo + (self.hi - self.lo) * max(0.0, min(1.0, fraction))) # ---------------------------------------------------------------- reading the package @@ -339,9 +369,10 @@ def clr_version(path): class Installer: - def __init__(self, msi, out, package=None): + def __init__(self, msi, out, package=None, span=None): self.pkg = package or Package(msi) self.out = Path(out) + self.span = span or Span() self.notes = [] self.registry = [] self.files = {} # File key -> rel path it was installed to @@ -539,23 +570,30 @@ class Installer: def unpack(self, work): """Every cabinet in the package, unpacked; returns File key -> unpacked file.""" found = {} - for index, row in enumerate(self.pkg.table("Media")): + media = [row for row in self.pkg.table("Media") if row.get("Cabinet", "")] + for index, row in enumerate(media): cabinet = row.get("Cabinet", "") - if not cabinet: - continue - if not cabinet.startswith("#"): - self.notes.append("external cabinet %s is not part of the package; its files are missing" % cabinet) - continue - progress("unpacking %s" % cabinet[1:]) - cab = Path(work) / ("media%d.cab" % index) - self.pkg.stream(cabinet[1:], cab) + self.span.report(0.05 + 0.3 * index / max(1, len(media)), "unpacking %s" % cabinet.lstrip("#")) dest = Path(work) / ("media%d" % index) dest.mkdir() + if cabinet.startswith("#"): + cab = Path(work) / ("media%d.cab" % index) + self.pkg.stream(cabinet[1:], cab) + own = True + else: + # An external cabinet: a file beside the package, as an installer wrapper lays it out + # (vc_red.cab beside vc_red.msi, netfx_Full.mzz beside netfx_Full_x64.msi). + cab = sibling(Path(self.pkg.path), cabinet) + if cab is None: + self.notes.append("external cabinet %s is not part of the package; its files are missing" % cabinet) + continue + own = False result = subprocess.run([CABEXTRACT, "-q", "-d", str(dest), str(cab)], capture_output=True, timeout=1800, env=tool_env()) if result.returncode != 0: raise MsiError("cabextract: %s" % result.stderr.decode("utf-8", "replace").strip()[:300]) - cab.unlink() + if own: + cab.unlink() for entry in dest.rglob("*"): if entry.is_file(): found.setdefault(entry.relative_to(dest).as_posix(), entry) @@ -604,9 +642,13 @@ class Installer: if rel: self.files[key] = rel assembly_folders = set() - for key, rel in destinations.items(): - if not rel: - continue + todo = [(key, rel) for key, rel in destinations.items() if rel] + last = -1 + for done, (key, rel) in enumerate(todo): + step = done * 20 // max(1, len(todo)) + if step != last: + last = step + self.span.report(0.35 + 0.55 * done / max(1, len(todo)), "placing files (%d of %d)" % (done, len(todo))) source = unpacked.get(key) if source is None: missing += 1 @@ -728,13 +770,14 @@ class Installer: self.notes.append("%d %s" % (count, what)) def install(self): - progress("reading %s" % (self.pkg.summary.get("subject") or Path(self.pkg.path).name)) + self.span.report(0.0, "reading %s" % (self.pkg.summary.get("subject") or Path(self.pkg.path).name)) self.run_set_actions() self.select() with tempfile.TemporaryDirectory(dir=str(self.out.parent), prefix=".msi-") as work: unpacked = self.unpack(work) - progress("placing files") + self.span.report(0.35, "placing files") self.install_files(unpacked) + self.span.report(0.9, "writing the registry values") self.install_registry() self.check() if not self.counts["files"] and not self.registry: @@ -748,18 +791,235 @@ class Installer: pass with open(self.out / "registry.json", "w", encoding="utf-8") as out: json.dump({"version": 1, "values": values + self.registry}, out, indent=1) + self.span.report(1.0, "done") return {"product": self.props.get("ProductName", ""), "version": self.props.get("ProductVersion", ""), "counts": self.counts, "notes": self.notes} +# ---------------------------------------------------------------- installer wrappers + +def sibling(path, name): + """A file next to [path] called [name], whatever its case; None when there is none.""" + folder = path.parent + candidate = folder / name + if candidate.is_file(): + return candidate + lower = name.lower() + for entry in folder.iterdir(): + if entry.is_file() and entry.name.lower() == lower: + return entry + return None + + +def kind_of(path): + """What a file is by its first bytes: msi, exe, cab or other.""" + with open(path, "rb") as source: + head = source.read(8) + if head.startswith(MSI_MAGIC): + return "msi" + if head[:2] == b"MZ": + return "exe" + if head[:4] == b"MSCF": + return "cab" + return "other" + + +def run_tool(args, timeout=1800): + try: + return subprocess.run(args, capture_output=True, timeout=timeout, env=tool_env()) + except OSError as error: + raise MsiError("%s is not available (%s)" % (Path(args[0]).name, error)) + + +def burn_containers(data): + """A WiX bundle's containers as (offset, size) pairs: the .wixburn section of the stub names + their sizes; each is a cabinet laid out after the stub, found by its header since signing can + move them. None when the file is no bundle.""" + try: + pe = struct.unpack_from("<I", data, 0x3C)[0] + if data[pe:pe + 4] != b"PE\0\0": + return None + count, optional = struct.unpack_from("<H", data, pe + 6)[0], struct.unpack_from("<H", data, pe + 20)[0] + table = pe + 24 + optional + section = None + for i in range(count): + name, _, _, raw_size, raw_offset = struct.unpack_from("<8sIIII", data, table + 40 * i) + if name.rstrip(b"\0") == b".wixburn": + section = (raw_offset, raw_size) + if section is None or section[1] < 52: + return None + offset = section[0] + if struct.unpack_from("<I", data, offset)[0] != BURN_MAGIC: + return None + stub, _, _, _, _, containers = struct.unpack_from("<IIIIII", data, offset + 24) + sizes = struct.unpack_from("<%dI" % containers, data, offset + 48) + except (struct.error, IndexError): + return None + found = [] + position = stub + for size in sizes: + while True: + start = data.find(b"MSCF", position) + if start < 0 or start + 12 > len(data): + return None + if struct.unpack_from("<I", data, start + 8)[0] == size: + break + position = start + 4 + found.append((start, size)) + position = start + size + return found + + +def unpack_burn(path, dest, containers): + """Unpacks a bundle's containers into [dest] and gives the payloads the names its manifest + lists (a9 -> packages/vcRuntimeMinimum_amd64/vc_runtimeMinimum_x64.msi).""" + data = path.read_bytes() + names = {} + for index, (start, size) in enumerate(containers): + progress("unpacking the bundle, part %d of %d" % (index + 1, len(containers)), 0.02 + 0.08 * index / max(1, len(containers))) + cab = dest / ("container%d.cab" % index) + cab.write_bytes(data[start:start + size]) + folder = dest / ("container%d" % index) + folder.mkdir() + result = run_tool([CABEXTRACT, "-q", "-d", str(folder), str(cab)]) + cab.unlink() + if result.returncode != 0: + raise MsiError("cabextract: %s" % result.stderr.decode("utf-8", "replace").strip()[:300]) + if index == 0: + manifest = folder / "0" + if manifest.is_file(): + text = manifest.read_text("utf-8", "replace") + for file_path, source in re.findall(r'<Payload\b[^>]*?\bFilePath="([^"]+)"[^>]*?\bSourcePath="([^"]+)"', text): + names[source] = file_path + continue + for entry in list(folder.iterdir()): + wanted = names.get(entry.name) + if not wanted: + continue + rel = Path(*[part for part in wanted.replace("\\", "/").split("/") if part not in ("", ".", "..")]) + target = folder / rel + target.parent.mkdir(parents=True, exist_ok=True) + entry.rename(target) + + +def unpack_installer(path, dest): + """Opens an installer or archive into [dest]: a bundle by its header, a cabinet (or + self-extracting one) with cabextract, anything else with 7-Zip.""" + kind = kind_of(path) + if kind == "exe": + containers = burn_containers(path.read_bytes()) + if containers: + unpack_burn(path, dest, containers) + return "bundle" + if kind == "cab" or kind == "exe" and b"MSCF" in path.read_bytes(): + result = run_tool([CABEXTRACT, "-q", "-d", str(dest), str(path)]) + if result.returncode == 0 and any(dest.rglob("*")): + return "cabinet" + for stale in list(dest.iterdir()): + shutil.rmtree(stale) if stale.is_dir() else stale.unlink() + progress("unpacking %s" % path.name, 0.02) + result = run_tool([SEVENZIP, "x", "-y", "-bd", "-bso0", "-bsp0", "-o" + str(dest), str(path)]) + if result.returncode not in (0, 1) or not any(dest.rglob("*")): + raise MsiError("could not open %s: %s" % (path.name, (result.stderr or result.stdout).decode("utf-8", "replace").strip()[:200] + or "nothing inside it can be unpacked")) + return "archive" + + +def packages_in(root): + """The Windows Installer packages an unpacked installer holds, shallowest first, by name.""" + found = [] + for entry in sorted(root.rglob("*")): + if entry.is_file() and kind_of(entry) == "msi": + found.append(entry) + found.sort(key=lambda entry: (len(entry.relative_to(root).parts), entry.name.lower())) + return found + + +def choose_packages(found, root): + """Which packages make up the install: the ones at the top of the installer (a bundle keeps + each in its own folder, so there every level counts), for the x86 and x64 platforms. The .NET + Framework 4.5+ installers carry a complete product (netfx_Full_*) beside the 4.0 parts they + patch (netfx_Core_*, netfx_Extended_*) and per-Windows-version copies in NetFx* folders: the + complete product wins, the copies are left out.""" + if not found: + return [] + depth = min(len(entry.relative_to(root).parts) for entry in found) + bundle = (root / "container1").is_dir() + top = [entry for entry in found if bundle or len(entry.relative_to(root).parts) == depth] + names = [entry.name.lower() for entry in top] + if any("full" in name for name in names) and any("_core" in name or "_extended" in name for name in names): + top = [entry for entry in top if not ("_core" in entry.name.lower() or "_extended" in entry.name.lower())] + chosen = [] + for entry in top: + template = Package(entry).summary.get("template", "").split(";")[0].strip().lower() + if template in SKIP_PLATFORMS: + continue + chosen.append(entry) + return chosen + + +def install_wrapper(path, out): + """Lays out every package inside an installer wrapper; the result is the sum of them.""" + total = {"product": "", "version": "", "counts": {"files": 0, "assemblies": 0, "registry": 0}, "notes": [], "packages": []} + with tempfile.TemporaryDirectory(dir=str(out.parent), prefix=".unpack-") as work: + dest = Path(work) / "x" + dest.mkdir() + how = unpack_installer(path, dest) + packages = choose_packages(packages_in(dest), dest) + if not packages: + raise MsiError("%s holds no installer package: it is its own setup program, which cannot be run here" % path.name) + for index, package in enumerate(packages): + lo, hi = 0.1 + 0.9 * index / len(packages), 0.1 + 0.9 * (index + 1) / len(packages) + progress("package %d of %d: %s" % (index + 1, len(packages), package.name), lo) + try: + result = Installer(str(package), out, span=Span(lo, hi)).install() + except MsiError as error: + # A package with nothing for this prefix (a patch for another Windows) is skipped, + # unless it is the only one; anything else is the whole installer's failure. + if "installs nothing" in str(error) and len(packages) > 1: + total["notes"].append("%s: %s" % (package.name, error)) + continue + raise + total["packages"].append(package.name) + if not total["product"]: + total["product"], total["version"] = result["product"], result["version"] + for key in total["counts"]: + total["counts"][key] += result["counts"].get(key, 0) + for note in result["notes"]: + if note not in total["notes"]: + total["notes"].append(note) + total["how"] = how + return total + + +def unpack_only(path, dest): + dest.mkdir(parents=True, exist_ok=True) + how = unpack_installer(path, dest) + files = sorted(str(entry.relative_to(dest)) for entry in dest.rglob("*") if entry.is_file()) + return {"how": how, "files": len(files), "names": files[:2000]} + + def main(): - if len(sys.argv) != 3: - print("usage: droiddeck-msi-install <package.msi> <component dir>", file=sys.stderr) + args = sys.argv[1:] + if len(args) == 3 and args[0] == "--unpack": + try: + result = unpack_only(Path(args[1]), Path(args[2])) + except MsiError as error: + print("error " + str(error), flush=True) + return 1 + print("result " + json.dumps(result), flush=True) + return 0 + if len(args) != 2: + print("usage: droiddeck-msi-install <package.msi | installer.exe> <component dir>\n" + " droiddeck-msi-install --unpack <archive> <dir>", file=sys.stderr) return 2 - out = Path(sys.argv[2]) + source, out = Path(args[0]), Path(args[1]) out.mkdir(parents=True, exist_ok=True) try: - result = Installer(sys.argv[1], out).install() + if kind_of(source) == "msi": + result = Installer(str(source), out).install() + else: + result = install_wrapper(source, out) except MsiError as error: print("error " + str(error), flush=True) return 1 diff --git a/tools/msitools/licenses/7zip-License b/tools/msitools/licenses/7zip-License new file mode 100644 index 0000000000..9c301dc1da --- /dev/null +++ b/tools/msitools/licenses/7zip-License @@ -0,0 +1,151 @@ + 7-Zip source code + ~~~~~~~~~~~~~~~~~ + License for use and distribution + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + 7-Zip Copyright (C) 1999-2026 Igor Pavlov. + + The licenses for files are: + + - CPP/7zip/Compress/Rar* files: the "GNU LGPL" with "unRAR license restriction" + - CPP/7zip/Compress/LzfseDecoder.cpp: the "BSD 3-clause License" + - C/ZstdDec.c: the "BSD 3-clause License" + - C/Xxh64.c: the "BSD 2-clause License" + - Some files are "public domain" files, if "public domain" status is stated in source file. + - the "GNU LGPL" for all other files. If there is no license information in + some source file, that file is under the "GNU LGPL". + + The "GNU LGPL" with "unRAR license restriction" means that you must follow both + "GNU LGPL" rules and "unRAR license restriction" rules. + + + + +GNU LGPL information +-------------------- + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, + you can get a copy of the GNU Lesser General Public License from + http://www.gnu.org/ + + + + +BSD 3-clause License in 7-Zip code +---------------------------------- + + The "BSD 3-clause License" is used for the following code in 7z.dll + 1) LZFSE data decompression. + CPP/7zip/Compress/LzfseDecoder.cpp. + That code was derived from the code in the "LZFSE compression library" developed by Apple Inc, + that also uses the "BSD 3-clause License". + 2) ZSTD data decompression. + C/ZstdDec.c + that code was developed using original zstd decoder code as reference code. + The original zstd decoder code was developed by Facebook Inc, + that also uses the "BSD 3-clause License". + + Copyright (c) 2015-2016, Apple Inc. All rights reserved. + Copyright (c) Facebook, Inc. All rights reserved. + Copyright (c) 2023-2026 Igor Pavlov. + +Text of the "BSD 3-clause License" +---------------------------------- + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +--- + + + + +BSD 2-clause License in 7-Zip code +---------------------------------- + + The "BSD 2-clause License" is used for the XXH64 code in 7-Zip. + C/Xxh64.c + + XXH64 code in 7-Zip was derived from the original XXH64 code developed by Yann Collet. + + Copyright (c) 2012-2021 Yann Collet. + Copyright (c) 2023-2026 Igor Pavlov. + +Text of the "BSD 2-clause License" +---------------------------------- + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +--- + + + + +unRAR license restriction +------------------------- + +The decompression engine for RAR archives was developed using source +code of unRAR program. +All copyrights to original unRAR code are owned by Alexander Roshal. + +The license for original unRAR code has the following restriction: + + The unRAR sources cannot be used to re-create the RAR compression algorithm, + which is proprietary. Distribution of modified unRAR sources in separate form + or as a part of other software is permitted, provided that it is clearly + stated in the documentation and source comments that the code may + not be used to develop a RAR (WinRAR) compatible archiver. + +-- diff --git a/tools/msitools/licenses/7zip-unRarLicense b/tools/msitools/licenses/7zip-unRarLicense new file mode 100644 index 0000000000..5f78b728da --- /dev/null +++ b/tools/msitools/licenses/7zip-unRarLicense @@ -0,0 +1,41 @@ + ****** ***** ****** unRAR - free utility for RAR archives + ** ** ** ** ** ** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ****** ******* ****** License for use and distribution of + ** ** ** ** ** ** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ** ** ** ** ** ** FREE portable version + ~~~~~~~~~~~~~~~~~~~~~ + + The source code of unRAR utility is freeware. This means: + + 1. All copyrights to RAR and the utility unRAR are exclusively + owned by the author - Alexander Roshal. + + 2. The unRAR sources may be used in any software to handle RAR + archives without limitations free of charge, but cannot be used + to re-create the RAR compression algorithm, which is proprietary. + Distribution of modified unRAR sources in separate form or as a + part of other software is permitted, provided that it is clearly + stated in the documentation and source comments that the code may + not be used to develop a RAR (WinRAR) compatible archiver. + + 3. The unRAR utility may be freely distributed. No person or company + may charge a fee for the distribution of unRAR without written + permission from the copyright holder. + + 4. THE RAR ARCHIVER AND THE UNRAR UTILITY ARE DISTRIBUTED "AS IS". + NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED. YOU USE AT + YOUR OWN RISK. THE AUTHOR WILL NOT BE LIABLE FOR DATA LOSS, + DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING + OR MISUSING THIS SOFTWARE. + + 5. Installing and using the unRAR utility signifies acceptance of + these terms and conditions of the license. + + 6. If you don't agree with terms of the license you must remove + unRAR files from your storage devices and cease to use the + utility. + + Thank you for your interest in RAR and unRAR. + + + Alexander L. Roshal \ No newline at end of file diff --git a/tools/msitools/licenses/NOTICE b/tools/msitools/licenses/NOTICE index ee62a42c51..5c625a0106 100644 --- a/tools/msitools/licenses/NOTICE +++ b/tools/msitools/licenses/NOTICE @@ -9,6 +9,9 @@ builds, unchanged: https://gitlab.gnome.org/GNOME/gcab cabextract cabextract 1.11 GPL-3.0-or-later https://www.cabextract.org.uk/ + 7z, 7z.so 7-Zip 26.04 LGPL-2.1-or-later, with the unRAR restriction + (7zip-License, 7zip-unRarLicense); https://7-zip.org/ -The packages and their sources: https://github.com/Droid-Deck/DroidDeck/releases/tag/msitools-0.106-r1, +The packages and their sources: https://github.com/Droid-Deck/DroidDeck/releases/tag/msitools-0.106-r1 +(7-Zip from Arch Linux ARM's mirror, see tools/msitools/packages.txt), https://github.com/archlinuxarm/PKGBUILDs. The licence texts are beside this file. diff --git a/tools/msitools/packages.txt b/tools/msitools/packages.txt index 5a04b4c626..8329e68a7f 100644 --- a/tools/msitools/packages.txt +++ b/tools/msitools/packages.txt @@ -1,6 +1,7 @@ # What droiddeck-msi-install runs: msitools' msiinfo reads a Windows Installer package's tables # and streams, cabextract unpacks its cabinets (every compression a cabinet can use, which gcab -# alone cannot). Arch Linux ARM's aarch64 packages, hosted unchanged on this repo's +# alone cannot), 7-Zip (7z + its 7z.so format plugin) opens the installer .exe wrappers around +# packages (self-extracting 7-Zip and cabinet files, WiX bundles). Arch Linux ARM's aarch64 packages, hosted unchanged on this repo's # msitools-0.106-r1 release because the mirror keeps only the newest of each. CI fetches each, # checks the sha256 and stages the files below into the apk under usr/local/lib/droiddeck-msitools; # SessionFiles copies them into the runtime at every launch. Everything else they need (glib, gio, @@ -10,3 +11,4 @@ 4028f44d83b2956ba2931d32aee8cceecbd6481e2febf86a3b6bfed69ef403d5 https://github.com/Droid-Deck/DroidDeck/releases/download/msitools-0.106-r1/libgsf-1.14.60-1-aarch64.pkg.tar.xz 3d7bd103313cb36d6cb703a5e583363e56ff255f105bcc0727916d195967976d https://github.com/Droid-Deck/DroidDeck/releases/download/msitools-0.106-r1/gcab-1.6-2-aarch64.pkg.tar.xz 87fe375a22de1519982234ce99f909ae13c06500784baaa1e8d7a0c0be0d14ee https://github.com/Droid-Deck/DroidDeck/releases/download/msitools-0.106-r1/cabextract-1.11-3-aarch64.pkg.tar.xz +e3bc83ac5960a4363f7ce05eb1d50b03d60d627a4a29f9b02c8e0466a5381b1f http://mirror.archlinuxarm.org/aarch64/extra/7zip-26.04-1-aarch64.pkg.tar.xz From 71e2ae86830802b6780d6624db6c2dba5b0bb2e8 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 9 Oct 2026 13:28:03 -0400 Subject: [PATCH 08/54] Logs: every log in app-private storage, shared as one scrubbed zip (#484) Session folders, tool and installer logs and the stores' log now all live under the app's private files/logs instead of Download/DroidDeck, and every existing "Share logs" action bundles them into one zip that goes through a final scrub. - Where: LinuxRuntime.logDir() = files/logs; sessions keep their dated folders and 30-session pruning, tools/ and stores/ beside them. The "Logs" setting keeps its meaning. Game save backups stay in Download/DroidDeck/Saves. - Migration: at first start the old session folders, tools/ and loose logs move from Download/DroidDeck into files/logs (Saves untouched), and every moved folder is scrubbed under the current rules. - Scrubbing: one rule set (SecretScrub) for the stores' log and the zip. The scrub marker carries the rules version, so a rules change re-scrubs every folder at the next start (in the background, a few at a time). Steam login account names and emails, account-name fields, OAuth codes, tokens in URLs and private LAN addresses are removed; Windows exception codes and server addresses are kept. - Share: one zip of the current session folder plus the last 7 days of stores/ and tools/, written to cache/share and handed to the share sheet. --- README.md | 4 +- .../com/droiddeck/launcher/MainActivity.kt | 2 + .../launcher/agent/AgentBridgeProvider.kt | 9 +- .../droiddeck/launcher/core/LogRedactor.kt | 57 +++++++++- .../droiddeck/launcher/core/SecretScrub.kt | 72 ++++++++++++ .../launcher/files/FileManagerScreen.kt | 3 +- .../launcher/runtime/FlatpakManager.kt | 2 +- .../launcher/runtime/GuestCommand.kt | 4 +- .../launcher/runtime/LinuxRuntime.java | 14 ++- .../launcher/session/LogMigration.kt | 73 ++++++++++++ .../launcher/session/SessionArtifacts.kt | 80 +++++++++---- .../launcher/session/SessionFiles.kt | 24 +--- .../launcher/session/SessionLogShare.kt | 51 ++++++--- .../launcher/session/SessionPaths.kt | 10 +- .../launcher/session/SessionPrefs.kt | 2 +- .../launcher/session/WinComponents.kt | 2 +- .../com/droiddeck/launcher/stores/StoreNet.kt | 38 +------ app/src/main/res/xml/log_share_paths.xml | 2 +- .../launcher/core/LogRedactorTest.kt | 61 ++++++++++ .../launcher/core/SecretScrubTest.kt | 39 +++++++ .../launcher/session/SessionLogShareTest.kt | 7 +- .../session/SessionLogShareZipTest.kt | 107 ++++++++++++++++++ docs/development/flatpak.md | 2 +- docs/progress-log.md | 54 +++++++++ 24 files changed, 598 insertions(+), 121 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/session/LogMigration.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/core/SecretScrubTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/session/SessionLogShareZipTest.kt diff --git a/README.md b/README.md index 1473653c81..22fe4e442d 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Before Steam launches, you must turn off **Restrict child processes** in Develop ## Community -Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, Preview builds, and device reports. Bug reports go in its **#bug-reports** forum; attach the session folder from `Download/DroidDeck/` so the logs come with it. +Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, Preview builds, and device reports. Bug reports go in its **#bug-reports** forum; attach the zip from **Share logs** (Setup, or the session drawer) so the logs come with it. ## Build @@ -29,7 +29,7 @@ Run `tools/build_local.sh` with Docker, Java 17, the Android SDK/NDK, and `zstd` ## Limits -Compatibility and performance vary by device; hardware validation is limited. Desktop compositing uses software rendering. Firefox sandboxing is reduced under proot. See the session logs in `Download/DroidDeck/` when diagnosing problems. +Compatibility and performance vary by device; hardware validation is limited. Desktop compositing uses software rendering. Firefox sandboxing is reduced under proot. Logs are kept app-private (`files/logs/`, the last 30 sessions); **Share logs** packs the newest session, with the stores and tool logs, into one scrubbed zip. ## Credits and licence diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index aff3514d1b..0ef2f323b5 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -868,6 +868,8 @@ class MainActivity : ComponentActivity() { if (wanted.isNotEmpty()) requestPermissions(wanted.toTypedArray(), 1) // A session folder left without its ending - the process was killed - gets it now. if (!SessionState.running) Thread({ + com.droiddeck.launcher.session.LogMigration.run(this) + SessionLogShare.clear(this) SessionArtifacts.finishAbandoned(this) SessionArtifacts.scrubOlder(this) SessionArtifacts.prune(this) diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt index 80648e8875..2b8ba775b0 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentBridgeProvider.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.agent +import com.droiddeck.launcher.session.SessionPaths import android.Manifest import android.content.ContentProvider import android.content.ContentValues @@ -107,7 +108,7 @@ class AgentBridgeProvider : ContentProvider() { private fun state(context: Context): JSONObject { val runtimeVersion = LinuxRuntimeInstaller.installedVersion(context) - val dir = SessionState.logDirectory ?: SessionState.logFile?.parentFile ?: latestSessionDirectory() + val dir = SessionState.logDirectory ?: SessionState.logFile?.parentFile ?: SessionPaths.sessionFolders(context).lastOrNull() val session = JSONObject() .put("id", SessionState.sessionId ?: dir?.name ?: JSONObject.NULL) .put("phase", SessionState.phase.name) @@ -163,12 +164,6 @@ class AgentBridgeProvider : ContentProvider() { .put("session", session) } - private fun latestSessionDirectory(): File? { - val parent = LinuxRuntime.debugLogDir() - return parent.listFiles { file -> file.isDirectory && file.name.startsWith("session-") } - ?.maxWithOrNull(compareBy<File> { it.lastModified() }.thenBy { it.name }) - } - private fun stop(context: Context): JSONObject { if (SessionState.running) { SessionService.stop(context) diff --git a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt index bac157b82e..ad48f26641 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/LogRedactor.kt @@ -75,9 +75,24 @@ object LogRedactor { * user who signs in with a plain account name, not an email, had it in every log. The single * space then a non-space keeps "OnLoginStateChange 0 1 0 0" (no account yet) as it is. */ - private val LOGIN_STATE = Regex("(OnLoginStateChange )(\\S+)") + private val LOGIN_STATE = Regex("(?i)(OnLoginStateChange:? )([^\\s<]\\S*)") private val LOGIN_USERS = Regex("(OnLoginUsersChanged )(\\S.*)$") + /** + * An account name as a field: `AccountName`, `account_name`, `username` (`"AccountName" + * "someone"` in a VDF, `account_name=...`, `"username": "..."`) and `login` - the last only + * as `login=`, a quoted key or a VDF pair, so "Login: OnLoginStateChange ..." keeps its label. + * A quoted value is blanked whole; one already blanked is left as it is. + */ + private const val ACCOUNT_VALUE = "(\"[^\"<\\r\\n]+\"|'[^'<\\r\\n]+'|[^\\s\"'<>&;,}]+)" + private val ACCOUNT_KV = Regex( + "(?i)(?<![A-Za-z0-9_])(account[_-]?name|user[_-]?name|epicusername)" + + "([\"']?\\s*[=:]\\s*|[\"']\\s+)" + ACCOUNT_VALUE + ) + private val LOGIN_KV = Regex( + "(?i)(?<![A-Za-z0-9_])(login)(\\s*=\\s*|[\"']\\s*[=:]\\s*|[\"']\\s+)" + ACCOUNT_VALUE + ) + /** The device's Steam accounts and persona names as patterns (see [learnAccounts]). */ @Volatile private var accounts: List<Regex> = emptyList() @@ -205,13 +220,49 @@ object LogRedactor { * only when that matches, so a folder scrubbed under older rules is scrubbed again on the way * out. */ - const val RULES_VERSION = 2 + const val RULES_VERSION = 3 + + /** + * A private IPv4 address (10/8, 172.16/12, 192.168/16, link-local 169.254/16): the home + * network's layout, e.g. its router as the resolver. Blanked in a shared zip only; public + * server addresses stay. Not part of a longer dotted number (a version like 10.0.19041.1). + */ + private val LAN_IPV4 = Regex( + "(?<![\\d.])(?:10\\.\\d{1,3}|192\\.168|172\\.(?:1[6-9]|2\\d|3[01])|169\\.254)\\.\\d{1,3}\\.\\d{1,3}(?!\\.?\\d)" + ) /** [src]'s lines, scrubbed, to [out]. */ fun scrubTo(src: java.io.File, out: java.io.Writer) { src.forEachLine { line -> out.write(redact(line)); out.write("\n") } } + /** + * The last pass for a file going into a shared zip: [redact], then the rules the stores' log + * uses too ([SecretScrub]) - URLs without query, fragment and user:password and with any + * token-bearing path segment blanked, token values and Authorization / Cookie headers blanked + * - and private IPv4 addresses as `<lan-address>`. A line already clean comes out unchanged. + */ + fun redactForShare(line: String): String { + if (line.isEmpty()) return line + return try { + val out = SecretScrub.scrub(redact(line), SecretScrub.Urls.KEEP_PATH, "<redacted:token>") + LAN_IPV4.replace(out, "<lan-address>") + } catch (t: Throwable) { + "<redaction failed; line withheld>" + } + } + + /** [src]'s lines through [redactForShare], to [out]: the pass every text file in a shared zip gets. */ + fun scrubForShare(src: java.io.File, out: java.io.Writer) { + src.forEachLine { line -> out.write(redactForShare(line)); out.write("\n") } + } + + /** `<redacted:account>`, inside [value]'s quotes when it had them. */ + private fun quoted(value: String): String { + val q = value.first().takeIf { it == '"' || it == '\'' }?.toString().orEmpty() + return "$q<redacted:account>$q" + } + /** [line] with every credential shape replaced. Null- and exception-safe by construction. */ fun redact(line: String): String { if (line.isEmpty()) return line @@ -240,6 +291,8 @@ object LogRedactor { for (r in own) out = r.replace(out, "<redacted:ip>") out = LOGIN_STATE.replace(out) { "${it.groupValues[1]}<redacted:account>" } out = LOGIN_USERS.replace(out) { "${it.groupValues[1]}<redacted:account>" } + out = ACCOUNT_KV.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" } + out = LOGIN_KV.replace(out) { "${it.groupValues[1]}${it.groupValues[2]}${quoted(it.groupValues[3])}" } for (r in accounts) out = r.replace(out, "<redacted:account>") out = EMAIL.replace(out, "<redacted:email>") out = RESIDUAL.replace(out) { "${it.groupValues[1]}=<redacted:token>" } diff --git a/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt b/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt new file mode 100644 index 0000000000..c91ca60ef9 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/core/SecretScrub.kt @@ -0,0 +1,72 @@ +package com.droiddeck.launcher.core + +/** + * The one set of rules that keeps URLs and tokens out of anything the app writes or shares: the + * stores' log ([com.droiddeck.launcher.stores.StoreLog.redactLine]) and the last pass over every + * text file in a shared log zip ([LogRedactor.redactForShare]). + * + * - Every URL loses its query, fragment and `user:password@`. [Urls.SHORT] then keeps only the host + * and a plain first path segment (the stores' lines, where a GOG secure link carries its token in + * the path); [Urls.KEEP_PATH] keeps the path but blanks any segment that carries a token. + * - Outside URLs, the values of token-like keys (`token`, `__token__`, `f_token`, `hdnts`, access / + * refresh / id tokens; exchange and authorization codes of four characters or more, and a bare + * `code=` where it is one: in a query string or on a line with OAuth keys such as `client_id`, + * `redirect_uri` or `state`, never an 8-hex exception code like Proton's `code=c0000005`) and of + * Authorization / Cookie headers are blanked with [mark]. + * + * A line already scrubbed comes out unchanged. + */ +object SecretScrub { + enum class Urls { SHORT, KEEP_PATH } + + private val URL = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") + private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""") + private val TOKEN_SEGMENT = Regex("""(?i)(token|hdnts|hmac|signature|sig|exp)=""") + private val TOKEN = Regex("""(?i)(?<![A-Za-z0-9_])(__token__|f_token|hdnts|access_token|refresh_token|id_token|token)(["']?\s*[=:]\s*["']?)[^\s&;,"'<>}]+""") + /** Codes that are only ever OAuth grants. */ + private val GRANT_CODE = Regex("""(?i)(?<![A-Za-z0-9_])(exchange_code|authorization_?code)(["']?\s*=\s*["']?)[^\s&;,"'<>}]{4,}""") + /** A bare `code=`: a grant only right after `?` / `&` (a query string) or beside OAuth keys ([OAUTH_CONTEXT]). */ + private val CODE = Regex("""(?i)(?<![A-Za-z0-9_])(code)(["']?\s*=\s*["']?)([^\s&;,"'<>}]{4,})""") + private val OAUTH_CONTEXT = Regex("""(?i)(?<![A-Za-z0-9_])(client_id|client_secret|redirect_uri|state|grant_type|response_type|access_token|refresh_token|id_token)["']?\s*[=:]""") + /** A Windows exception or status code (`code=c0000005`, `code=0x80000003`): never a grant. */ + private val STATUS_CODE = Regex("""(?i)(0x)?[0-9a-f]{8}""") + private val HEADER = Regex("""(?i)\b(authorization|cookie|set-cookie)(\s*[:=]\s*).+""") + + fun scrub(text: String, urls: Urls, mark: String): String { + var out = URL.replace(text) { m -> if (urls == Urls.SHORT) short(m.value) else keepPath(m.value, mark) } + out = TOKEN.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + out = GRANT_CODE.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + val oauth = OAUTH_CONTEXT.containsMatchIn(text) + val src = out + out = CODE.replace(src) { m -> + val inQuery = m.range.first > 0 && src[m.range.first - 1] in "?&" + val grant = !STATUS_CODE.matches(m.groupValues[3]) && (oauth || inQuery) + if (grant) m.groupValues[1] + m.groupValues[2] + mark else m.value + } + out = HEADER.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + mark } + return out + } + + private fun split(url: String): Triple<String, String, String?> { + val schemeEnd = url.indexOf("://") + val rest = url.substring(schemeEnd + 3).substringBefore('#').substringBefore('?') + val slash = rest.indexOf('/') + val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') + return Triple(url.substring(0, schemeEnd), authority, if (slash >= 0) rest.substring(slash + 1) else null) + } + + private fun short(url: String): String { + val (scheme, authority, path) = split(url) + if (path == null) return "$scheme://$authority" + val first = path.substringBefore('/') + if (first.isEmpty()) return "$scheme://$authority/" + val shown = if (SAFE_SEGMENT.matches(first)) first else "…" + return "$scheme://$authority/$shown" + if (path.length > first.length && shown != "…") "/…" else "" + } + + private fun keepPath(url: String, mark: String): String { + val (scheme, authority, path) = split(url) + val clean = path?.split('/')?.joinToString("/") { seg -> if (TOKEN_SEGMENT.containsMatchIn(seg)) mark else seg } + return "$scheme://$authority" + (clean?.let { "/$it" } ?: "") + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt index 9f9361ad7b..46f2f708b2 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/FileManagerScreen.kt @@ -1020,7 +1020,8 @@ fun FileManagerScreen( // The ROMs folder chosen on the main screen: what the session shows as /root/ROMs. SessionPrefs.romsDir(context).takeIf { it.isNotEmpty() }?.let(::File)?.takeIf { it.isDirectory } ?.let { add(locItem(stringResource(R.string.fm_rail_roms), Icons.Filled.SportsEsports, it)) } - File("/storage/emulated/0/Download/DroidDeck").takeIf { it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_session_logs), Icons.Filled.Description, it)) } + // The app's own logs folder: private, which this file manager (the app itself) can read. + com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context).takeIf { it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_session_logs), Icons.Filled.Description, it)) } File("/storage/emulated/0/Pictures").takeIf { it.isDirectory }?.let { add(locItem(stringResource(R.string.fm_rail_pictures), Icons.Filled.Image, it)) } } val favItems = remember(favTick) { FavoritesStore.list(context).map(::File).filter { it.exists() } } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt b/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt index 8f31e2d2f1..c5c1fa6db6 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/FlatpakManager.kt @@ -87,7 +87,7 @@ object FlatpakManager { ?.maxByOrNull { it.first }?.second } - /** Runs [argv] in the runtime, its output also in Download/DroidDeck/flatpak-<verb>.log. */ + /** Runs [argv] in the runtime, its output also in files/logs/tools/flatpak-<verb>.log. */ private fun runGuest(context: Context, argv: List<String>, fakeRoot: Boolean, onLine: (String) -> Unit): Int { val name = "flatpak-" + (argv.getOrNull(2)?.takeIf { argv.getOrNull(1) == HELPER } ?: "setup") return GuestCommand.run(context, argv, fakeRoot, name, onLine = onLine) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt b/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt index a5a653c3e8..286059588f 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestCommand.kt @@ -17,7 +17,7 @@ object GuestCommand { /** * Runs [argv] and hands each output line to [onLine]; returns the exit status. [fakeRoot] is * for the package tools, which refuse any uid but 0. With [logName], everything the command - * says also goes to Download/DroidDeck/tools/<logName>.log beside the session logs, so a problem can + * says also goes to files/logs/tools/<logName>.log beside the session logs, so a problem can * be handed over like a session's. With [linkDir], hard links (which Android denies apps) * become symlinks to files proot keeps there (its link2symlink). */ @@ -25,7 +25,7 @@ object GuestCommand { linkDir: File? = null, onLine: (String) -> Unit): Int { val log = logName?.let { try { - File(File(LinuxRuntime.debugLogDir(), SessionPaths.TOOLS_DIR).apply { mkdirs() }, "$it.log").printWriter() + File(File(LinuxRuntime.logDir(context), SessionPaths.TOOLS_DIR).apply { mkdirs() }, "$it.log").printWriter() } catch (e: Exception) { null } } log?.println("== ${java.util.Date()} ${argv.joinToString(" ")}") diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java index 38a33980a3..082bf814e9 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntime.java @@ -44,10 +44,20 @@ public final class LinuxRuntime { public static final String GUEST_RUNTIME_DIR = "/run/droiddeck"; private static final String SYSTEM_FONTS = "/system/fonts"; private static final String GUEST_SYSTEM_FONTS = "/usr/local/share/fonts/android"; - /** Where every Linux session's debug log lands: public, so a user can just hand the folder over. */ + /** The folder under Downloads where builds before private logs wrote them; read only to move them. */ public static final String DEBUG_LOG_DIR = "DroidDeck"; - public static File debugLogDir() { + /** + * Where every log lands: app-private, {@code files/logs}. Session folders, tools/ (one-off + * commands) and stores/ sit under it; the files directory is bound into every session at the + * same path, so the guest writes there too. Share logs is how a folder leaves the device. + */ + public static File logDir(Context context) { + return new File(context.getFilesDir(), "logs"); + } + + /** {@code Download/DroidDeck}, where earlier builds wrote logs. Only LogMigration reads it. */ + public static File legacyLogDir() { return new File(android.os.Environment.getExternalStoragePublicDirectory( android.os.Environment.DIRECTORY_DOWNLOADS), DEBUG_LOG_DIR); } diff --git a/app/src/main/java/com/droiddeck/launcher/session/LogMigration.kt b/app/src/main/java/com/droiddeck/launcher/session/LogMigration.kt new file mode 100644 index 0000000000..c2b34f8165 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/LogMigration.kt @@ -0,0 +1,73 @@ +package com.droiddeck.launcher.session + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.core.FileUtils +import com.droiddeck.launcher.runtime.LinuxRuntime +import java.io.File + +/** + * Once, on the first start of a build with private logs: the session folders, tools/ and the loose + * tool logs that earlier builds left in `Download/DroidDeck/` move into `files/logs/`. Best effort: + * an item that cannot be moved stays where it is. Moved session folders lose their old scrub markers + * ([SessionArtifacts.unmarkMoved]), so [SessionArtifacts.scrubOlder] scrubs them under the current rules. Nothing else in that folder is touched - game + * save backups (Saves/) stay public - and after this pass the app never reads it again. + */ +object LogMigration { + private const val TAG = "LogMigration" + private const val MARKER = ".moved-from-downloads" + + @Synchronized + fun run(context: Context) { + val target = LinuxRuntime.logDir(context).apply { mkdirs() } + val marker = File(target, MARKER) + if (marker.exists()) return + val legacy = LinuxRuntime.legacyLogDir() + val items = if (legacy.isDirectory) legacy.listFiles() else emptyArray() + // Unreadable (no storage access yet): try again next start rather than give up on them. + if (items == null) { Log.w(TAG, "$legacy cannot be listed; logs there stay until it can"); return } + var moved = 0 + val movedFolders = ArrayList<File>() + for (f in items) { + val isLog = SessionPaths.isSessionFolder(f) || + (f.isDirectory && f.name == SessionPaths.TOOLS_DIR) || + (f.isFile && f.name.endsWith(".log")) + if (!isLog) continue + val dest = File(if (f.isFile) File(target, SessionPaths.TOOLS_DIR).apply { mkdirs() } else target, f.name) + if (dest.exists() && f.name != SessionPaths.TOOLS_DIR) continue + if (move(f, dest)) { + moved++ + if (SessionPaths.isSessionFolder(dest)) movedFolders.add(dest) + } else Log.w(TAG, "could not move ${f.name}; left in place") + } + // Earlier builds scrubbed these under older rules (or not at all) and marked them done: + // without their markers, the pass over older folders that follows at the same start puts + // them through the current redactor. + SessionArtifacts.unmarkMoved(movedFolders) + try { marker.writeText("moved $moved item(s)\n") } catch (e: Exception) { Log.w(TAG, "could not mark the move: ${e.message}") } + if (moved > 0) Log.i(TAG, "moved $moved log item(s) from $legacy into $target") + } + + /** A rename where it works (same volume), else a copy then the source's removal; false = left as it was. */ + private fun move(src: File, dest: File): Boolean { + if (src.renameTo(dest)) return true + return try { + copy(src, dest) + FileUtils.delete(src) + true + } catch (e: Exception) { + Log.w(TAG, "copy of ${src.name} failed: ${e.message}") + false + } + } + + private fun copy(src: File, dest: File) { + if (src.isDirectory) { + dest.mkdirs() + src.listFiles()?.forEach { copy(it, File(dest, it.name)) } + } else { + dest.parentFile?.mkdirs() + src.inputStream().use { input -> dest.outputStream().use { input.copyTo(it) } } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt index 500c2f7d33..cc0f5ad821 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionArtifacts.kt @@ -31,16 +31,20 @@ object SessionArtifacts { /** Written last; a folder without it did not get its ending. */ const val COMPLETE_MARKER = ".complete" - /** Every file in the folder has been through the redactor, own addresses and accounts included (-2: accounts added). */ - private const val SCRUBBED_MARKER = ".scrubbed-2" + /** + * Every file in the folder has been through the redactor under its current rules. Named after + * [LogRedactor.RULES_VERSION], so a folder scrubbed under older rules is scrubbed again. + */ + private val SCRUBBED_MARKER = ".scrubbed-r${LogRedactor.RULES_VERSION}" + private val OLD_SCRUBBED_MARKER = Regex("""\.scrubbed-(r?\d+)""") /** * Written by a session's own ending, and only when every file in the folder, subfolders * included (whatever the session script copied into steam/ and droiddeck-esync/ as well as what * the app wrote), went through the redactor without a failure. It lists each of those files - * with its size and modification time, under the redactor's rules version: a share copies a - * file as it is only when all three still match ([scrubbedFiles]), and scrubs anything else - - * a file added or changed since, a hidden one, or every file of a folder without it. + * with its size and modification time, under the redactor's rules version ([scrubbedFiles]). + * A share no longer relies on it: every text file goes through the share pass on the way into + * the zip regardless. */ const val SCRUBBED_TREE_MARKER = ".scrubbed-tree" @@ -67,24 +71,48 @@ object SessionArtifacts { } /** - * Once, for session folders written before every file was scrubbed and before the device's own - * addresses were (network.txt listed them; the client's IPv6 check logs "external address" - * into steam/connection_log.txt): the whole folder, steam/ included, through the redactor. - * A marker records it. Runs at app start with [finishAbandoned]. + * Every session folder not yet scrubbed under the redactor's current rules - written before + * every file was scrubbed, before the device's own addresses or the Steam account were, or + * moved in from Download/ ([LogMigration] strips their markers first, [unmarkMoved]): the + * whole folder, steam/ and every other subfolder included, through the redactor, and the + * current marker written in place of the old ones. Runs at app start with [finishAbandoned]. + * + * A folder holds some 30 MB of Steam logs, about half a minute's work, so folders go through + * a few at a time; one the process does not get to keeps its old marker and is taken up at + * the next start. */ @Synchronized fun scrubOlder(context: Context) { val current = SessionPaths.current() - val dirs = LinuxRuntime.debugLogDir().listFiles { f -> + val dirs = LinuxRuntime.logDir(context).listFiles { f -> SessionPaths.isSessionFolder(f) && f != current && !File(f, SCRUBBED_MARKER).exists() - } ?: return + }?.toList() ?: return if (dirs.isEmpty()) return LogRedactor.learnFromRuntime(LinuxRuntime.rootDir(context)) - dirs.forEach { dir -> - Record(dir).let { r -> scrubFolder(dir, r); File(dir, "steam").takeIf { it.isDirectory }?.let { scrubFolder(it, r) } } - try { File(dir, SCRUBBED_MARKER).writeText("scrubbed ${now()}\n") } catch (e: Exception) {} + val workers = (Runtime.getRuntime().availableProcessors() / 2).coerceIn(1, 4) + val pool = java.util.concurrent.Executors.newFixedThreadPool(workers) { r -> + Thread({ android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND); r.run() }, "scrub-logs") + } + val done = try { + pool.invokeAll(dirs.map { dir -> java.util.concurrent.Callable { scrubAndMark(dir) } }) + .count { f -> try { f.get() } catch (e: Exception) { false } } + } finally { + pool.shutdown() + } + val failed = dirs.size - done + Log.i(TAG, "logs: scrubbed $done folders under r${LogRedactor.RULES_VERSION}" + + if (failed > 0) ", $failed not fully (again at the next start)" else "") + } + + /** + * Folders [LogMigration] just moved in lose every scrub marker an earlier build left, so + * [scrubOlder], which runs right after it, puts them through the current rules whatever those + * markers claimed. + */ + fun unmarkMoved(dirs: List<File>) { + dirs.filter { SessionPaths.isSessionFolder(it) }.forEach { dir -> + dir.listFiles { f -> OLD_SCRUBBED_MARKER.matches(f.name) || f.name == SCRUBBED_TREE_MARKER }?.forEach { it.delete() } } - Log.i(TAG, "scrubbed ${dirs.size} older session folder(s)") } /** @@ -143,15 +171,17 @@ object SessionArtifacts { * Records [record]'s folder as scrubbed - every file went through without a failure - or * leaves it unmarked, so the next share and the pass over older folders scrub it again. */ - private fun markScrubbed(record: Record) { + private fun markScrubbed(record: Record): Boolean { val dir = record.dir val tree = File(dir, SCRUBBED_TREE_MARKER) + // A marker from older rules vouches for nothing now, whether or not this pass succeeded. + dir.listFiles { f -> OLD_SCRUBBED_MARKER.matches(f.name) }?.forEach { it.delete() } if (record.failed) { tree.delete() Log.w(TAG, "$dir is not fully scrubbed; it is scrubbed again when shared") - return + return false } - try { + return try { File(dir, SCRUBBED_MARKER).writeText("scrubbed ${now()}\n") val lines = StringBuilder("rules ${LogRedactor.RULES_VERSION}\n") record.files.forEach { (f, s) -> @@ -159,8 +189,11 @@ object SessionArtifacts { .append(f.relativeTo(dir).path).append('\n') } tree.writeText(lines.toString()) + true } catch (e: Exception) { tree.delete() + File(dir, SCRUBBED_MARKER).delete() + false } } @@ -181,9 +214,8 @@ object SessionArtifacts { }.toSet() } - /** The end-of-session scrub of [dir] on its own (steam/ already in place), for tests. */ - @androidx.annotation.VisibleForTesting - internal fun scrubAndMark(dir: File) = markScrubbed(scrubTree(Record(dir))) + /** The end-of-session scrub of [dir] on its own (steam/ already in place); also the pass over older and moved folders. */ + internal fun scrubAndMark(dir: File): Boolean = markScrubbed(scrubTree(Record(dir))) /** * [record]'s folder and every folder under it (steam/, droiddeck-esync/): the session script @@ -223,7 +255,7 @@ object SessionArtifacts { */ @Synchronized fun finishAbandoned(context: Context) { - val parent = LinuxRuntime.debugLogDir() + val parent = LinuxRuntime.logDir(context) val abandoned = parent.listFiles { f -> SessionPaths.isSessionFolder(f) && !File(f, COMPLETE_MARKER).exists() }?.sortedWith(SessionPaths.chronological) ?: return @@ -282,9 +314,7 @@ object SessionArtifacts { val current = SessionPaths.current() val gone = SessionPaths.sessionFolders(context).filter { it != current } gone.forEach { com.droiddeck.launcher.core.FileUtils.delete(it) } - com.droiddeck.launcher.core.FileUtils.delete(File(LinuxRuntime.debugLogDir(), SessionPaths.TOOLS_DIR)) - // Before tools/, those logs sat loose beside the session folders. - LinuxRuntime.debugLogDir().listFiles { f -> f.isFile && f.name.endsWith(".log") }?.forEach { it.delete() } + com.droiddeck.launcher.core.FileUtils.delete(File(LinuxRuntime.logDir(context), SessionPaths.TOOLS_DIR)) Log.i(TAG, "cleared ${gone.size} session folder(s)") return gone.size } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 04dc792606..e768b6f69a 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -361,25 +361,9 @@ object SessionFiles { ).joinToString(newline) /** - * Where the session writes its log. Downloads is the point - a failed run is handed over as a - * folder rather than dug out of app-private storage - but the session script redirects its own - * output there with `exec`, and a redirection a non-interactive shell cannot open ends that - * shell. So a public directory is used only once it is proven writable; otherwise the app's - * own files directory, which is bound into the session anyway, stands in. + * Where the session writes its log: app-private `files/logs`, bound into the session at the same + * path, so the session script's own redirection always opens. A folder leaves the device only + * through Share logs, which scrubs every file again on the way into the zip. */ - fun logDirectory(context: Context): File { - val public = LinuxRuntime.debugLogDir() - if (public.isDirectory || public.mkdirs()) { - val probe = File(public, ".writable") - try { - if (probe.createNewFile() || probe.isFile) { - probe.delete() - return public - } - } catch (ignored: Exception) { - } - } - Log.w(TAG, "$public is not writable (storage permission?); logging to files/logs") - return File(context.filesDir, "logs").apply { mkdirs() } - } + fun logDirectory(context: Context): File = LinuxRuntime.logDir(context).apply { mkdirs() } } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt index c12a69f39d..85760c8224 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionLogShare.kt @@ -18,9 +18,11 @@ import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream /** - * Zips the most recent session's log folder and hands it to Android's share sheet. Works where - * the folder could not be written to Downloads (no storage permission, or a device that refuses - * it) and landed in the app's private files instead, which a user cannot otherwise reach. + * Share logs: one zip, made when asked, of a session folder (the newest by default) with the last + * seven days of the stores' log and the one-off command logs beside it, handed to Android's share + * sheet. Logs live in app-private `files/logs`; this is how they leave the device. Every text file + * goes through [LogRedactor.redactForShare] on the way in, so even a file written before a rule + * existed is clean in the zip. The zip sits in `cache/share/` until the next share or app start. */ object SessionLogShare { private val main = Handler(Looper.getMainLooper()) @@ -62,7 +64,7 @@ object SessionLogShare { val files = folder.walkTopDown().filter { it.isFile } .filterNot { it.parentFile?.name == "steam" && it.length() > STEAM_LOG_MAX_BYTES }.toList() if (files.isEmpty()) return null - val out = File(context.cacheDir, "shared-logs").apply { deleteRecursively(); mkdirs() } + val out = shareDir(context).apply { deleteRecursively(); mkdirs() } val zip = File(out, "DroidDeck-${folder.name}.zip") // Scrubbed on the way into the zip: a session shared while it runs has not had its end-of- // session pass yet, and the redactor changes nothing in a line that is already clean. @@ -71,11 +73,11 @@ object SessionLogShare { // since, goes in as it is; anything else (added or changed since, hidden, or any file of a // session still running) is scrubbed again. Scrubbing tens of MB of the client's logs on // every share kept the share sheet from appearing for ten seconds or more. - val scrubbed = SessionArtifacts.scrubbedFiles(folder) val live = liveSteamLogs(context, folder) - // The stores' own log (last 7 days, app-private, already redacted), under stores/. - val storeLogs = com.droiddeck.launcher.stores.StoreLogFiles.files(context) - val total = (files + live + storeLogs).sumOf { it.length() }.coerceAtLeast(1L) + val logs = LinuxRuntime.logDir(context) + val stores = recent(File(logs, STORES_DIR)) + val tools = recent(File(logs, SessionPaths.TOOLS_DIR)) + val total = (files + live + stores + tools).sumOf { it.length() }.coerceAtLeast(1L) var done = 0L var shown = -1 fun advance(f: File) { @@ -84,18 +86,20 @@ object SessionLogShare { if (percent != shown) { shown = percent; onProgress?.invoke(percent / 100f) } } ZipOutputStream(zip.outputStream().buffered()).use { z -> - files.forEach { f -> addEntry(z, folder.name + "/" + f.relativeTo(folder).path, f, f.relativeTo(folder).path in scrubbed); advance(f) } - live.forEach { f -> addEntry(z, folder.name + "/steam/" + f.name, f, false); advance(f) } - storeLogs.forEach { f -> addEntry(z, folder.name + "/stores/" + f.name, f, false); advance(f) } + files.forEach { f -> addEntry(z, folder.name + "/" + f.relativeTo(folder).path, f); advance(f) } + live.forEach { f -> addEntry(z, folder.name + "/steam/" + f.name, f); advance(f) } + stores.forEach { f -> addEntry(z, "$STORES_DIR/" + f.name, f); advance(f) } + tools.forEach { f -> addEntry(z, SessionPaths.TOOLS_DIR + "/" + f.name, f); advance(f) } } return zip } - private fun addEntry(z: ZipOutputStream, name: String, f: File, scrubbed: Boolean) { + private fun addEntry(z: ZipOutputStream, name: String, f: File) { z.putNextEntry(ZipEntry(name)) - if (!scrubbed && LogRedactor.isText(f)) { + // Every text file, every time: the folder's own scrub may predate a rule. + if (LogRedactor.isText(f)) { val w = z.bufferedWriter() - LogRedactor.scrubTo(f, w) + LogRedactor.scrubForShare(f, w) w.flush() } else { f.inputStream().use { it.copyTo(z) } @@ -119,6 +123,25 @@ object SessionLogShare { /** The client's content and bootstrap logs grow large over months and say nothing about a session. */ private const val STEAM_LOG_MAX_BYTES = 8L * 1024 * 1024 + /** The stores' engine log, one file a day, under files/logs. */ + const val STORES_DIR = "stores" + private const val RECENT_DAYS = 7 + + private fun shareDir(context: Context): File = File(context.cacheDir, "share") + + /** The files in [dir] changed in the last [RECENT_DAYS] days. */ + private fun recent(dir: File): List<File> { + val since = System.currentTimeMillis() - RECENT_DAYS * 86_400_000L + return dir.listFiles { f -> f.isFile && f.lastModified() >= since }?.sortedBy { it.name }.orEmpty() + } + + /** Deletes the last share's zip; at app start, so a zip never outlives the share it was made for. */ + fun clear(context: Context) { + shareDir(context).deleteRecursively() + // Where builds before this one put it. + File(context.cacheDir, "shared-logs").deleteRecursively() + } + fun shareIntent(context: Context, zip: File): Intent { val uri = FileProvider.getUriForFile(context, context.packageName + ".logs", zip) val send = Intent(Intent.ACTION_SEND) diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt index 2b8f38eec4..5b94c6a44c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPaths.kt @@ -8,19 +8,20 @@ import java.util.Date import java.util.Locale /** - * One folder per session under `Download/DroidDeck/`, holding everything that session recorded, + * One folder per session under app-private `files/logs/`, holding everything that session recorded, * named for the day, that day's session number and what was run, so the one a report is about can * be picked out without opening any: * * ``` - * Download/DroidDeck/2026-09-30-03-steam/ + * files/logs/2026-09-30-03-steam/ * device.txt what this device is, and every setting the session ran with * session.log the guest session: proot, gamescope, the client's stdout * wayland.log the app's compositor * steam.log the Steam client's own log, scrubbed (Steam mode) * steam/ the rest of the client's logs, scrubbed (Steam mode) * desktop.log KWin, Plasma and the programs on it (desktop mode) - * Download/DroidDeck/tools/ one-off commands: Flatpak installs, AppImage imports + * files/logs/tools/ one-off commands: Flatpak installs, AppImage imports + * files/logs/stores/ the stores' engine log, one file a day * ``` * * Folders from before this naming (`session-20260930-180642`) are still recognised, shared and @@ -137,8 +138,7 @@ object SessionPaths { /** Every session folder in the places logs are written, oldest first. */ fun sessionFolders(context: Context): List<File> = - listOf(com.droiddeck.launcher.runtime.LinuxRuntime.debugLogDir(), File(context.filesDir, "logs")) - .flatMap { parent -> parent.listFiles { f -> isSessionFolder(f) }?.toList() ?: emptyList() } + (com.droiddeck.launcher.runtime.LinuxRuntime.logDir(context).listFiles { f -> isSessionFolder(f) }?.toList() ?: emptyList()) .sortedWith(chronological) internal fun slug(text: String): String = diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 2fd7b05c59..af23de85c2 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -493,7 +493,7 @@ object SessionPrefs { } /** - * Whether a session writes its folder under Download/DroidDeck. Off, the same logs are kept in + * Whether a session keeps its folder under files/logs. Off, the same logs are kept in * the app's cache for the session's lifetime (the scripts need somewhere to write) and thrown * away at the end, so nothing accumulates in Downloads. */ diff --git a/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt b/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt index 850ef5059c..b4a0ef751d 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/WinComponents.kt @@ -398,7 +398,7 @@ object WinComponents { * Downloads the component's package (.msi) or installer (.exe) into the runtime and has * droiddeck-msi-install lay it out in [staging]. Returns the installer's result (product, * version, counts, notes), or null and why. Everything the installer prints also goes to - * Download/DroidDeck/tools. + * files/logs/tools. */ private fun installPackage(context: Context, c: Component, step: Step, staging: File, onProgress: (Progress) -> Unit): Pair<JSONObject?, String> { val root = LinuxRuntime.rootDir(context) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt index 1a94c847aa..4eeba0cab5 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreNet.kt @@ -125,40 +125,12 @@ object StoreLog { } } - private val URL_PATTERN = Regex("""[A-Za-z][A-Za-z0-9+.\-]*://[^\s"'<>]+""") - private val SAFE_SEGMENT = Regex("""[A-Za-z0-9._\-]{1,40}""") - private val SECRET = Regex("""(?i)(?<![A-Za-z0-9_])(__token__|f_token|hdnts|access_token|refresh_token|id_token|token)(["']?\s*[=:]\s*["']?)[^\s&;,"'}]+""") - private val CODE = Regex("""(?i)(?<![A-Za-z0-9_])(code|exchange_code|authorizationCode)(["']?\s*=\s*["']?)[^\s&;,"'}]+""") - private val AUTH_HEADER = Regex("""(?i)(authorization|cookie)(\s*[:=]\s*).*""") - /** - * What any store line may say, for the Downloads log and logcat alike: every URL cut to - * `scheme://host/first-segment/…` (the segment kept only when it is a plain name - a GOG secure - * link carries its token in the path), no query, fragment or userinfo; and outside URLs, the - * values of token-like keys and of Authorization / Cookie headers blanked. + * What any store line may say, for the stores' log and logcat alike: URLs cut to their host and a + * plain first path segment, token values and Authorization / Cookie headers blanked - the shared + * rules of [com.droiddeck.launcher.core.SecretScrub], which a shared log zip applies too. */ @JvmStatic - fun redactLine(text: String): String { - var out = URL_PATTERN.replace(text) { m -> shortUrl(m.value) } - out = SECRET.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } - out = CODE.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } - out = AUTH_HEADER.replace(out) { m -> m.groupValues[1] + m.groupValues[2] + "…" } - return out - } - - private fun shortUrl(url: String): String { - val schemeEnd = url.indexOf("://") - val scheme = url.substring(0, schemeEnd) - var rest = url.substring(schemeEnd + 3) - rest = rest.substringBefore('#').substringBefore('?') - val slash = rest.indexOf('/') - val authority = (if (slash >= 0) rest.substring(0, slash) else rest).substringAfterLast('@') - if (slash < 0) return "$scheme://$authority" - val path = rest.substring(slash + 1) - val first = path.substringBefore('/') - val more = path.length > first.length - if (first.isEmpty()) return "$scheme://$authority/" - val shown = if (SAFE_SEGMENT.matches(first)) first else "…" - return "$scheme://$authority/$shown" + if (more && shown != "…") "/…" else "" - } + fun redactLine(text: String): String = + com.droiddeck.launcher.core.SecretScrub.scrub(text, com.droiddeck.launcher.core.SecretScrub.Urls.SHORT, "…") } diff --git a/app/src/main/res/xml/log_share_paths.xml b/app/src/main/res/xml/log_share_paths.xml index cc98eee1e5..9bed7b6831 100644 --- a/app/src/main/res/xml/log_share_paths.xml +++ b/app/src/main/res/xml/log_share_paths.xml @@ -1,4 +1,4 @@ <?xml version="1.0" encoding="utf-8"?> <paths> - <cache-path name="shared_logs" path="shared-logs/" /> + <cache-path name="shared_logs" path="share/" /> </paths> diff --git a/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt b/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt index 8b21590974..b1c97962d8 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/LogRedactorTest.kt @@ -87,4 +87,65 @@ class LogRedactorTest { assertEquals(once, LogRedactor.redact(once)) } } + + // The Steam UI's own login line (webhelper_js.txt), as the privacy scan found it. + @Test fun loginStateLineLosesAnEmailAccount() { + val line = "[2026-10-07 21:14:03] SteamUI: INFO: Login: OnLoginStateChange someone.masked@example.com 2 1 0 0" + val out = LogRedactor.redact(line) + assertFalse(out.contains("someone.masked")) + assertFalse(out.contains("example.com")) + assertEquals("[2026-10-07 21:14:03] SteamUI: INFO: Login: OnLoginStateChange <redacted:account> 2 1 0 0", out) + assertEquals(out, LogRedactor.redactForShare(line)) + } + + @Test fun loginStateLineLosesAPlainAccountName() { + val line = "[2026-10-07 21:14:03] SteamUI: INFO: Login: OnLoginStateChange maskeduser42 2 1 0 0" + val out = LogRedactor.redact(line) + assertFalse(out.contains("maskeduser42")) + assertEquals("[2026-10-07 21:14:03] SteamUI: INFO: Login: OnLoginStateChange <redacted:account> 2 1 0 0", out) + assertEquals(out, LogRedactor.redactForShare(line)) + } + + @Test fun loginStateWithoutAnAccountIsKept() { + val line = "SteamUI: INFO: Login: OnLoginStateChange 0 1 0 0" + assertEquals(line, LogRedactor.redact(line)) + } + + @Test fun accountNameFieldsAreRedacted() { + val lines = listOf( + "\t\"AccountName\"\t\t\"maskeduser42\"", + "account_name=maskeduser42 remember=1", + "{\"login\":\"maskeduser42\",\"persist\":1}", + "auth: login=maskeduser42&remember=1", + "accountName: maskeduser42", + "{\"username\": \"masked user 42\"}", + ) + for (line in lines) { + val out = LogRedactor.redactForShare(line) + assertFalse(out, out.contains("maskeduser42") || out.contains("masked user")) + assertTrue(out, out.contains("<redacted:account>")) + assertEquals(out, LogRedactor.redactForShare(out)) + } + } + + @Test fun loginAsAWordIsNotAField() { + for (line in listOf("SteamUI: INFO: Login: OnLoginStateChange 0 1 0 0", "login refresh pending", "Login: state 5")) { + assertEquals(line, LogRedactor.redact(line)) + } + } + + @Test fun privateAddressesLeaveTheZipPublicOnesStay() { + assertEquals("I/LinuxNetworkLink: resolver: <lan-address> <lan-address> 8.8.8.8", + LogRedactor.redactForShare("I/LinuxNetworkLink: resolver: 192.168.1.1 10.0.0.138 8.8.8.8")) + assertEquals("gateway <lan-address>:53 link <lan-address> lan <lan-address>", + LogRedactor.redactForShare("gateway 172.16.4.20:53 link 169.254.10.2 lan 172.31.255.254")) + for (line in listOf( + "connecting to CM 162.254.193.47:27017", + "public 172.32.0.1 and 11.0.0.1 and 193.168.1.1", + "Windows 10.0.19041.1 build 10.0.19045", + "Proton 10.0-3, driver 25.1.0.4", + )) assertEquals(line, LogRedactor.redactForShare(line)) + val once = LogRedactor.redactForShare("resolver: 192.168.0.1") + assertEquals(once, LogRedactor.redactForShare(once)) + } } diff --git a/app/src/test/java/com/droiddeck/launcher/core/SecretScrubTest.kt b/app/src/test/java/com/droiddeck/launcher/core/SecretScrubTest.kt new file mode 100644 index 0000000000..86ff6ece8f --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/SecretScrubTest.kt @@ -0,0 +1,39 @@ +package com.droiddeck.launcher.core + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Test + +class SecretScrubTest { + private fun share(line: String) = SecretScrub.scrub(line, SecretScrub.Urls.KEEP_PATH, "<redacted:token>") + + @Test fun windowsExceptionCodesAreKept() { + for (line in listOf( + "0024:trace:seh:dispatch_exception code=406d1388 flags=0 addr=00006FFFFFC0A6E8", + "0024:warn:seh:dispatch_exception code=c0000005 flags=0 addr=0000000140012345", + "0030:trace:seh:handle_syscall_fault code=80000003 flags=0", + "exception code=0xC0000005 at 0x140012345", + // An exception code stays one even on a line that also carries OAuth keys. + "client_id=abc code=c0000005", + )) assertEquals(line, share(line)) + } + + @Test fun anOAuthCodeIsBlanked() { + assertEquals("redirect client_id=34a02cf8 redirect_uri=x state=1 code=<redacted:token>", + share("redirect client_id=34a02cf8 redirect_uri=x state=1 code=AUTHCODE123")) + assertEquals("GET /on_login_success?code=<redacted:token>&scope=", + share("GET /on_login_success?code=AUTHCODE123&scope=")) + assertEquals("exchange_code=<redacted:token>", share("exchange_code=0123456789abcdef")) + assertEquals("authorizationCode=<redacted:token>", share("authorizationCode=ABCD1234XYZ")) + } + + @Test fun aCodeWithoutOAuthAroundItIsKept() { + for (line in listOf("exit code=1337", "status code=ERR_TIMEOUT", "code 403")) assertEquals(line, share(line)) + } + + @Test fun scrubbingTwiceChangesNothing() { + val once = share("client_id=1 code=AUTHCODE123 ?code=XYZW1234567") + assertFalse(once.contains("AUTHCODE123")) + assertEquals(once, share(once)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt index 39978490a6..1d58f179fa 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareTest.kt @@ -33,12 +33,13 @@ class SessionLogShareTest { "rules $rules\n" + files.joinToString("") { val f = File(folder, it); "${f.length()}\t${f.lastModified()}\t$it\n" }) } - @Test fun aRecordedUnchangedFileGoesInAsItIs() { + @Test fun aRecordedUnchangedFileIsScrubbedAgainOnTheWayIntoTheZip() { val folder = tmp.newFolder("2026-10-06-01-steam") - // Stands in for a file the ending scrubbed; the share must not pass it through again. + // A file the ending recorded as scrubbed still gets the share's final pass: the record + // may predate a rule, and a zip is the one way a log leaves the device. File(folder, "session.log").writeText("$secret\n") record(folder, "session.log") - assertTrue(zip(folder).getValue("session.log").contains(leaked)) + assertFalse(zip(folder).getValue("session.log").contains(leaked)) } @Test fun anythingTheRecordDoesNotVouchForIsScrubbed() { diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareZipTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareZipTest.kt new file mode 100644 index 0000000000..81f230d662 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionLogShareZipTest.kt @@ -0,0 +1,107 @@ +package com.droiddeck.launcher.session + +import com.droiddeck.launcher.runtime.LinuxRuntime +import java.io.File +import java.util.zip.ZipFile +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 + +/** A shared zip is scrubbed on the way in, whatever the files on disk still hold. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class SessionLogShareZipTest { + private val app get() = RuntimeEnvironment.getApplication() + + @Test fun everyTextFileInTheZipIsScrubbedAndTheStoresAndToolsLogsComeAlong() { + val logs = LinuxRuntime.logDir(app) + val session = File(logs, "2026-10-08-01-steam").apply { mkdirs() } + // Written raw, as a file from before a rule existed would be. + File(session, "session.log").writeText( + "fetch https://gog-cdn-fastly.gog.com/token=nva=1791500000~dirs=/x~token=0a1b2c3d4e5f/content-system/v2/store/1/ab/cd/f\n" + + "fetch https://epicgames-download1.akamaized.net/Builds/Org/a/ChunksV4/12/ABCD.chunk?__token__=exp=1~hmac=deadbeefcafe\n" + + "login refresh_token=abcdef123456 user someone@example.com code=AUTHCODE123\n" + + "Authorization: Bearer abc.def.ghi\n" + + "kept: EResult 5, app 1086940, steamapps/common/Game\n" + ) + File(logs, "stores").apply { mkdirs() }.resolve("stores-2026-10-08.log").writeText("epic: f_token=zzzzzzzz12 window 32\n") + File(logs, SessionPaths.TOOLS_DIR).apply { mkdirs() }.resolve("flatpak-install.log").writeText("== install\nCookie: steamLoginSecure=76561198000000000%7C%7Ceyabc\n") + + val zip = SessionLogShare.zipFolder(app, session)!! + assertTrue(zip.path.startsWith(File(app.cacheDir, "share").path)) + val text = ZipFile(zip).use { z -> z.entries().toList().associate { e -> e.name to z.getInputStream(e).bufferedReader().readText() } } + assertTrue(text.keys.containsAll(listOf("2026-10-08-01-steam/session.log", "stores/stores-2026-10-08.log", "tools/flatpak-install.log"))) + val all = text.values.joinToString("\n") + for (secret in listOf("0a1b2c3d4e5f", "deadbeefcafe", "abcdef123456", "someone@example.com", "AUTHCODE123", "abc.def.ghi", "zzzzzzzz12", "eyabc")) { + assertFalse("$secret survived", all.contains(secret)) + } + // What makes a log worth reading stays. + assertTrue(all.contains("kept: EResult 5, app 1086940, steamapps/common/Game")) + assertTrue(all.contains("https://gog-cdn-fastly.gog.com/")) + + SessionLogShare.clear(app) + assertFalse(zip.exists()) + } + + @Test fun theSharePassChangesNothingInACleanLine() { + val line = "dxvk: Device 0x5 (Adreno 750) https://github.com/doitsujin/dxvk/releases code 403" + assertEquals(line, com.droiddeck.launcher.core.LogRedactor.redactForShare(line)) + } + + // Folders an earlier build scrubbed and marked under older rules, their copied Steam UI log + // still naming the account: one app start (the move out of Download/, then the pass over + // older folders) leaves every one scrubbed on disk under the current rules. + private fun olderFolder(parent: File, name: String, marker: String = ".scrubbed-2"): File { + val dir = File(parent, name).apply { mkdirs() } + File(dir, marker).writeText("scrubbed earlier\n") + File(dir, SessionArtifacts.SCRUBBED_TREE_MARKER).writeText("rules 2\n") + File(dir, "steam").apply { mkdirs() }.resolve("webhelper_js.txt").writeText( + "[2026-10-01 20:00:00] SteamUI: INFO: Login: OnLoginStateChange someone.masked@example.com 2 1 0 0\n" + + "[2026-10-01 20:00:01] SteamUI: INFO: Login: OnLoginStateChange maskeduser42 2 1 0 0\n" + + "[2026-10-01 20:00:02] SteamUI: INFO: Login: OnLoginStateChange 0 1 0 0\n" + + "0024:trace:seh:dispatch_exception code=c0000005 flags=0\n" + ) + return dir + } + + private fun assertScrubbed(dir: File) { + val text = File(dir, "steam/webhelper_js.txt").readText() + for (secret in listOf("someone.masked", "example.com", "maskeduser42")) assertFalse("$secret survived in $dir", text.contains(secret)) + assertTrue(text.contains("Login: OnLoginStateChange <redacted:account> 2 1 0 0")) + assertTrue(text.contains("Login: OnLoginStateChange 0 1 0 0")) + assertTrue(text.contains("code=c0000005")) + val markers = dir.list()!!.filter { it.startsWith(".scrubbed-") && it != SessionArtifacts.SCRUBBED_TREE_MARKER } + assertEquals("$dir", listOf(".scrubbed-r${com.droiddeck.launcher.core.LogRedactor.RULES_VERSION}"), markers) + assertTrue(File(dir, SessionArtifacts.SCRUBBED_TREE_MARKER).readText() + .startsWith("rules ${com.droiddeck.launcher.core.LogRedactor.RULES_VERSION}\n")) + } + + @Test fun aFolderMarkedUnderOlderRulesIsScrubbedAgainAtStart() { + val dir = olderFolder(LinuxRuntime.logDir(app), "2026-10-01-01-steam") + SessionArtifacts.scrubOlder(app) + assertScrubbed(dir) + } + + @Test fun everyMigratedFolderIsScrubbedUnderTheCurrentRulesInOneStart() { + val legacy = LinuxRuntime.legacyLogDir().apply { mkdirs() } + val names = (1..9).map { "2026-10-0${it}-01-steam" } + names.forEachIndexed { i, name -> + // Old markers of every kind an earlier build left, one even claiming the current rules. + olderFolder(legacy, name, marker = listOf(".scrubbed-2", ".scrubbed-1", ".scrubbed-r${com.droiddeck.launcher.core.LogRedactor.RULES_VERSION}")[i % 3]) + } + File(legacy, "Saves").apply { mkdirs() }.resolve("keep.txt").writeText("save\n") + + // What MainActivity runs at start. + LogMigration.run(app) + SessionArtifacts.scrubOlder(app) + + val logs = LinuxRuntime.logDir(app) + for (name in names) assertScrubbed(File(logs, name)) + assertEquals(listOf("Saves"), legacy.list()!!.toList()) + } +} diff --git a/docs/development/flatpak.md b/docs/development/flatpak.md index 7ed3ae7257..dd7faae506 100644 --- a/docs/development/flatpak.md +++ b/docs/development/flatpak.md @@ -22,7 +22,7 @@ The script then unpacks them without hooks and checks what the binaries link. Li the Desktop package normally brings (PyGObject, json-glib, fuse3 and others) are fetched too, but only when their files are missing. Flathub is added as a per-user remote from a copy of its `.flatpakrepo` carried in the script, so that step needs no network. Every store command -writes its output to `Download/DroidDeck/flatpak-<verb>.log`, next to the session logs. The +writes its output to `files/logs/tools/flatpak-<verb>.log`, next to the session logs. The setup script also puts Flatpak's own error text in the failure message the app shows. ## bubblewrap without namespaces diff --git a/docs/progress-log.md b/docs/progress-log.md index 4c67247a6b..23d1529b76 100644 --- a/docs/progress-log.md +++ b/docs/progress-log.md @@ -7,6 +7,60 @@ the timeline, then lessons and backlog. Companion to the README (what the app *d --- +## 2026-10-08 - `feat/private-logs`: every log in app-private storage, shared as one scrubbed zip + +- **Where logs live:** `files/logs/` (`LinuxRuntime.logDir`), nothing under `Download/DroidDeck` + any more. Session folders (`<day>-NN-<what>/`), `tools/` (GuestCommand, Flatpak, Windows + component installers) and `stores/` (the stores' engine log, one file a day) all sit there. The + files directory is bound into every session at the same path, so the session script, gamescope + and the guest tools write there as before. Thirty sessions kept; the Logs switch off still means + a cache folder deleted at session end. Game save backups stay public in `Download/DroidDeck/Saves`. +- **Moving the old ones:** on the first start of this build, session folders, `tools/` and loose + `*.log` files move from `Download/DroidDeck/` into `files/logs/` (rename, else copy and delete; + an item that fails stays). A marker records the pass; `Saves/` and anything else there is never + touched, and the app does not read `Download/DroidDeck/` again. +- **Share logs:** one zip in `cache/share/`, made on demand: the newest session folder (or the one + the session screen shares), the last seven days of `stores/` and `tools/`. Every text file goes + through `LogRedactor.redactForShare` on the way in - the existing rules (Steam tokens, JWTs, + SteamIDs, account names, emails, own addresses, MACs, serials) plus URLs without query, fragment + or userinfo and with token-bearing path segments blanked (GOG secure links), `__token__`, + `f_token`, `hdnts`, OAuth `code=`, and Authorization / Cookie headers - so a file written before a + rule existed is clean in the zip. The zip is deleted at the next share or app start. + `SessionLogShareTest` covers it. Nothing was relaxed: the per-session scrub, `scrubOlder` and + every existing rule stay as they were. +- **File manager:** the Session logs shortcut opens `files/logs` (the app reads its own storage). +- **On main after Stores (#476) and the KDE Plasma desktop (#473):** the stores' log is already + `files/logs/stores/stores-<date>.log` and the zip carries its last seven days under `stores/`. One + set of rules scrubs both the stores' log and the zip (`SecretScrub`): `StoreLog.redactLine` keeps + URLs to their host and first plain segment, the zip pass keeps paths with token segments blanked, + both blank token values and Authorization / Cookie headers; the two test suites pass on it. The + Plasma desktop writes nothing outside the session folder (its output is the session's + `desktop.log`), so there was nothing new to move. +- **Privacy scan fixes:** the Steam login account (an email, 374 times) was still in older + folders' copied `steam/webhelper_js.txt` (`SteamUI: INFO: Login: OnLoginStateChange <account>`): + those folders carried a scrub marker from before the account rules, and the marker never + expired. The marker is now named after the redactor's rules version (`.scrubbed-r3`), so every + folder scrubbed under older rules goes through again at app start, steam/ and every subfolder + included; folders moved out of `Download/DroidDeck` lose every old marker + (`SessionArtifacts.unmarkMoved`), so the same pass takes them whatever those markers said. New rules: `OnLoginStateChange <anything>`, and + `AccountName` / `account_name` / `username` / `login` fields (`login` only as `login=`, a quoted + key or a VDF pair, so the "Login:" label stays). The `code=` rule no longer touches Proton's + exception codes (`code=c0000005`, `406d1388`, `80000003`): a bare `code=` is blanked only right + after `?`/`&` or on a line with OAuth keys (`client_id`, `redirect_uri`, `state`, tokens), and + never when it is 8 hex digits; exchange / authorization codes are always blanked. Tests: + `LogRedactorTest` (the login line with an email and with a plain name), `SecretScrubTest`, + `SessionLogShareZipTest` (older and moved folders scrubbed on disk). +- **Device check (31 migrated folders):** only 4 carried `.scrubbed-r3` a minute in. Nothing was + skipped - the pass was still running: one folder holds ~30 MB of Steam logs, ~30 s through the + redactor, one folder after another (about 15 minutes for 31). The start pass now scrubs a few + folders at a time (half the cores, at most 4, background priority), old markers go whether or + not a folder's pass succeeds, a folder the process does not finish is taken up at the next start, + and it logs one line: `logs: scrubbed N folders under r3`. Test: nine migrated folders carrying + `.scrubbed-2`, `.scrubbed-1` or a stale `.scrubbed-r3` all end up scrubbed under r3 after one + migration + start pass. +- **LAN addresses in the zip:** private IPv4 addresses (10/8, 172.16/12, 192.168/16, 169.254/16; + e.g. the router in `LinuxNetworkLink: resolver: ...`) become `<lan-address>` in the zip pass; + public server addresses and dotted version numbers stay (`LogRedactorTest`). ## 2026-10-08 - `feat/stores`: GOG, Epic Games and Amazon Games in the launcher (in progress) A new **Stores** section - the three storefronts' libraries and public catalogs, one download From 5af14829b05b9460a9eb6164da1ed7d5b1c595b9 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 9 Oct 2026 14:42:23 -0400 Subject: [PATCH 09/54] Stores: seal the GOG, Epic and Amazon sign-in files with an Android Keystore key (#486) The stores' sign-ins (files/stores/<store>/credentials.json: access and refresh tokens, account id and name) were plain JSON. Other apps could not read them, but every program in the Linux session runs as the app and could. They are now sealed with AES-256-GCM under a non-exportable AndroidKeyStore key (StrongBox when the device has one), so the session only ever sees ciphertext. - Format: a versioned envelope {v, alg, iv, ct}, written atomically and readable only by the app. No new dependencies (javax.crypto and KeyStore). - Migration: an existing plaintext file is sealed at the next start, checked by decrypting it, then the plaintext is removed; users stay signed in. - No Keystore on the device: the file stays plain and the log says so once per start. - A sealed file that can never be opened (tampered, key gone or invalidated, broken envelope) is removed and that store asks for sign-in again. A transient Keystore error keeps the file and retries. --- .../com/droiddeck/launcher/MainActivity.kt | 2 + .../launcher/stores/CredentialCipher.kt | 105 +++++++++++ .../launcher/stores/StoreAccounts.kt | 128 ++++++++++++- .../droiddeck/launcher/stores/StoresState.kt | 5 + .../launcher/stores/amazon/AmazonBackend.kt | 2 +- .../launcher/stores/amazon/AmazonLibrary.kt | 3 +- .../launcher/stores/epic/EpicBackend.kt | 2 +- .../launcher/stores/gog/GogBackend.kt | 2 +- .../stores/StoreAccountsCipherTest.kt | 173 ++++++++++++++++++ docs/development/stores.md | 36 +++- docs/progress-log.md | 20 ++ 11 files changed, 462 insertions(+), 16 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/CredentialCipher.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/StoreAccountsCipherTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 0ef2f323b5..95545f537f 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -866,6 +866,8 @@ class MainActivity : ComponentActivity() { SessionPrefs.setMicAsked(this) } if (wanted.isNotEmpty()) requestPermissions(wanted.toTypedArray(), 1) + // A store sign-in an earlier build left in plain text is sealed with the Keystore key. + Thread({ com.droiddeck.launcher.stores.StoreAccounts.encryptAll(this) }, "store-credentials").start() // A session folder left without its ending - the process was killed - gets it now. if (!SessionState.running) Thread({ com.droiddeck.launcher.session.LogMigration.run(this) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/CredentialCipher.kt b/app/src/main/java/com/droiddeck/launcher/stores/CredentialCipher.kt new file mode 100644 index 0000000000..2a7e84357e --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/CredentialCipher.kt @@ -0,0 +1,105 @@ +package com.droiddeck.launcher.stores + +import android.os.Build +import android.security.keystore.KeyGenParameterSpec +import android.security.keystore.KeyProperties +import android.util.Base64 +import org.json.JSONObject +import java.security.KeyStore +import javax.crypto.Cipher +import javax.crypto.KeyGenerator +import javax.crypto.SecretKey +import javax.crypto.spec.GCMParameterSpec + +/** + * The stores' sign-in files at rest: AES-256-GCM under a key that lives in the AndroidKeyStore and + * never leaves it, as a small versioned envelope `{"v":1,"alg":"AES/GCM","iv":...,"ct":...}` + * (base64). The session's Linux side shares the app's files directory but not its Keystore, so + * what it can see of a sign-in is the envelope only. + */ +object CredentialCipher { + const val VERSION = 1 + const val ALG = "AES/GCM" + private const val TRANSFORMATION = "AES/GCM/NoPadding" + private const val TAG_BITS = 128 + + /** Where the key comes from: the AndroidKeyStore on a device, a plain key in tests. */ + fun interface KeyProvider { + /** The key, created on first use. Throws when no key can be had (no Keystore on this ROM). */ + fun key(): SecretKey + + /** The key that sealed earlier files: never a new one. Throws [KeyMissing] when there is none. */ + fun existing(): SecretKey = key() + } + + /** The Keystore answered and holds no key: nothing sealed before can open again. */ + class KeyMissing : java.security.GeneralSecurityException("no key") + + /** + * Whether [e], from [open], means the file can never open: it does not authenticate (tampered, + * or sealed under another key), the key is permanently invalidated or gone, or the file is not + * an envelope. Anything else - the Keystore busy or erroring, an I/O error - may pass. + */ + fun isPermanent(e: Throwable): Boolean = e is javax.crypto.AEADBadTagException || + e is android.security.keystore.KeyPermanentlyInvalidatedException || + e is KeyMissing || e is org.json.JSONException || e is IllegalArgumentException + + /** + * An AES-256 key in the AndroidKeyStore: not exportable, encrypt/decrypt with GCM only, no user + * authentication (background downloads and launch-time code fetches use it with the screen + * off). StrongBox where the device has it, the TEE otherwise. + */ + object Keystore : KeyProvider { + private const val ALIAS = "droiddeck-store-credentials" + private const val PROVIDER = "AndroidKeyStore" + + @Synchronized + override fun existing(): SecretKey { + val ks = KeyStore.getInstance(PROVIDER).apply { load(null) } + return ks.getKey(ALIAS, null) as? SecretKey ?: throw KeyMissing() + } + + @Synchronized + override fun key(): SecretKey { + val ks = KeyStore.getInstance(PROVIDER).apply { load(null) } + (ks.getKey(ALIAS, null) as? SecretKey)?.let { return it } + if (Build.VERSION.SDK_INT >= 28) { + try { return generate(strongBox = true) } catch (e: Exception) { /* no StrongBox: the TEE */ } + } + return generate(strongBox = false) + } + + private fun generate(strongBox: Boolean): SecretKey { + val spec = KeyGenParameterSpec.Builder(ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT) + .setKeySize(256) + .setBlockModes(KeyProperties.BLOCK_MODE_GCM) + .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) + .setUserAuthenticationRequired(false) + .apply { if (strongBox && Build.VERSION.SDK_INT >= 28) setIsStrongBoxBacked(true) } + .build() + return KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, PROVIDER).run { init(spec); generateKey() } + } + } + + /** [plain] sealed into the envelope. The cipher picks the IV (the Keystore requires it). */ + fun seal(plain: String, keys: KeyProvider): JSONObject { + val cipher = Cipher.getInstance(TRANSFORMATION).apply { init(Cipher.ENCRYPT_MODE, keys.key()) } + val ct = cipher.doFinal(plain.toByteArray(Charsets.UTF_8)) + return JSONObject().put("v", VERSION).put("alg", ALG) + .put("iv", b64(cipher.iv)).put("ct", b64(ct)) + } + + /** The text inside [envelope]. Throws when it is not ours, of another version, or does not authenticate. */ + fun open(envelope: JSONObject, keys: KeyProvider): String { + require(envelope.optInt("v", -1) == VERSION && envelope.optString("alg") == ALG) { "unknown envelope" } + val iv = Base64.decode(envelope.getString("iv"), Base64.NO_WRAP) + val ct = Base64.decode(envelope.getString("ct"), Base64.NO_WRAP) + val cipher = Cipher.getInstance(TRANSFORMATION).apply { init(Cipher.DECRYPT_MODE, keys.existing(), GCMParameterSpec(TAG_BITS, iv)) } + return String(cipher.doFinal(ct), Charsets.UTF_8) + } + + /** Whether [json] is an envelope rather than plain credentials. */ + fun isEnvelope(json: JSONObject): Boolean = json.has("v") && json.has("ct") && json.has("iv") + + private fun b64(bytes: ByteArray) = Base64.encodeToString(bytes, Base64.NO_WRAP) +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt index 1ea8440956..77599e0a47 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreAccounts.kt @@ -2,6 +2,7 @@ package com.droiddeck.launcher.stores import android.content.Context import android.util.Log +import androidx.annotation.VisibleForTesting import org.json.JSONObject import java.io.File @@ -9,40 +10,147 @@ import java.io.File * Each store's sign-in, kept in the app's private files: `filesDir/stores/<store>/credentials.json`. * Tokens never go to shared storage and never into a log; the file holds whatever the store's auth * flow handed back (access and refresh tokens, expiry, the account's display name, and for Amazon - * the device registration), as one JSON object the store's own client reads and writes. + * the device registration), as one JSON object the store's own client reads and writes - sealed + * with [CredentialCipher] under a key in the AndroidKeyStore. Every read and write of a sign-in + * goes through here. + * + * - A plain file an earlier build wrote is sealed the first time it is read ([encryptAll] at app + * start), verified by opening it again, and the plain text is replaced. + * - Where the Keystore does not work (some ROMs), the file stays plain, the log says so once, and + * the next start tries again. + * - A sealed file that can never open again (the key is gone: data cleared, a backup restored on + * another device) is deleted and the store reads as signed out; one that fails for a reason that + * may pass is kept and tried again (see [read]). */ object StoreAccounts { private const val TAG = "StoreAccounts" private const val FILE = "credentials.json" + @VisibleForTesting + @Volatile + internal var keys: CredentialCipher.KeyProvider = CredentialCipher.Keystore + + /** The Keystore failed once in this process: files stay plain until the next start tries again. */ + @VisibleForTesting + @Volatile + internal var keystoreFailed = false + /** The store's private folder: credentials, caches, the engine's scratch. */ fun dir(context: Context, store: Store): File = File(File(context.filesDir, "stores"), store.id).apply { mkdirs() } fun credentialsFile(context: Context, store: Store): File = File(dir(context, store), FILE) - /** The stored credentials, or null when signed out or unreadable (never logged: the text is the secret). */ + /** Stores whose sealed sign-in could not be opened this time for a reason that may pass (see [read]). */ + private val unavailable = java.util.concurrent.ConcurrentHashMap.newKeySet<Store>() + + @Volatile + private var unavailableLogged = false + + /** + * The stored credentials, or null when signed out or unreadable (never logged: the text is the + * secret). + * + * A sealed file is deleted - the store signs in again - only when it can never open: it does + * not authenticate (tampered, or another key: data cleared, restored on another device), the + * key is permanently invalidated or gone from the Keystore, or it is not an envelope at all. Any + * other failure (the Keystore busy or erroring, an I/O error) keeps the file, marks the store + * [isUnavailable] - signed in, its actions failing for now - and the next read tries again. + */ + @Synchronized fun read(context: Context, store: Store): JSONObject? { val f = credentialsFile(context, store) - if (!f.isFile) return null - return try { JSONObject(f.readText()) } catch (e: Exception) { Log.w(TAG, "${store.id}: credentials unreadable (${e.javaClass.simpleName})"); null } + if (!f.isFile) { unavailable.remove(store); return null } + val text = try { f.readText() } catch (e: Exception) { return markUnavailable(store, e) } + val json = try { JSONObject(text) } catch (e: Exception) { return unreadable(f, store, e) } + if (!CredentialCipher.isEnvelope(json)) { + unavailable.remove(store) + seal(f, json)?.let { Log.i(TAG, "stores: credentials encrypted ${store.id}") } + return json + } + return try { + JSONObject(CredentialCipher.open(json, keys)).also { unavailable.remove(store) } + } catch (e: Exception) { + if (CredentialCipher.isPermanent(e)) unreadable(f, store, e) else markUnavailable(store, e) + } } + /** The store's sign-in is on disk but could not be opened this time (see [read]). */ + fun isUnavailable(store: Store): Boolean = store in unavailable + + private fun unreadable(f: File, store: Store, e: Exception): JSONObject? { + Log.w(TAG, "stores: credentials for ${store.id} cannot be opened (${e.javaClass.simpleName}); signed out") + f.delete() + unavailable.remove(store) + return null + } + + private fun markUnavailable(store: Store, e: Exception): JSONObject? { + unavailable.add(store) + if (!unavailableLogged) { + unavailableLogged = true + Log.w(TAG, "stores: credentials unreadable this start (${e.javaClass.simpleName})") + } + return null + } + + @Synchronized fun write(context: Context, store: Store, json: JSONObject) { + unavailable.remove(store) val f = credentialsFile(context, store) - val tmp = File(f.path + ".tmp") - tmp.writeText(json.toString()) - tmp.setReadable(false, false); tmp.setReadable(true, true) - if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) } + if (seal(f, json) == null) put(f, json.toString()) } + @Synchronized fun clear(context: Context, store: Store) { + unavailable.remove(store) credentialsFile(context, store).delete() + File(credentialsFile(context, store).path + ".tmp").delete() } - /** The account's display name when signed in (the store's own name for it, or a stand-in), else null. */ + /** At app start: every plain sign-in an earlier build left is sealed now. */ + fun encryptAll(context: Context) { + Store.entries.forEach { store -> + // An older build's temp file held the plain text too. + File(credentialsFile(context, store).path + ".tmp").delete() + read(context, store) + } + } + + /** + * The account's display name when signed in (the store's own name for it, or a stand-in), else + * null. A sign-in that could not be opened this time ([isUnavailable]) still counts, under the + * store's name, so the sign-in card does not ask for what is still there. + */ fun signedInAs(context: Context, store: Store): String? { - val json = read(context, store) ?: return null + val json = read(context, store) ?: return if (isUnavailable(store)) store.label else null if (json.optString("access_token", "").isEmpty()) return null return json.optString("display_name", "").ifEmpty { json.optString("username", "") }.ifEmpty { store.label } } + + /** + * [json] sealed into [f], checked by opening what was written. Null - and [f] as it was - when + * the Keystore cannot seal or the check fails. + */ + private fun seal(f: File, json: JSONObject): File? { + if (keystoreFailed) return null + val text = json.toString() + val envelope = try { + CredentialCipher.seal(text, keys).also { check(CredentialCipher.open(it, keys) == text) { "check failed" } } + } catch (e: Exception) { + keystoreFailed = true + Log.w(TAG, "stores: keystore unavailable, credentials stay plain (${e.javaClass.simpleName})") + return null + } + put(f, envelope.toString()) + return f + } + + /** [text] into [f] through a temp file and a rename, readable and writable by the app alone. */ + private fun put(f: File, text: String) { + val tmp = File(f.path + ".tmp") + tmp.writeText(text) + tmp.setReadable(false, false); tmp.setReadable(true, true) + tmp.setWritable(false, false); tmp.setWritable(true, true) + if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) } + } } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt index 02111bed57..c969e19e14 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt @@ -118,6 +118,11 @@ object StoresState { fun isSignedIn(store: Store): Boolean = accounts.containsKey(store) + /** A store action's error line when it found no usable sign-in: gone, or on disk but unreadable for now. */ + fun notSignedInLine(store: Store): String = + if (StoreAccounts.isUnavailable(store)) "${store.shortLabel} sign-in could not be read: try again." + else "${store.shortLabel} session expired: sign in again." + /** Stores whose sign-in has run out (a launch could not get a code): their chip dims until the user signs in again. */ val expired = mutableStateMapOf<Store, Boolean>() diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt index d51a284109..cf40c19e67 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt @@ -55,7 +55,7 @@ object AmazonBackend : StoreBackend { when (result) { is AmazonLibrary.SyncResult.Ok -> { publish(app, result.games); StoresState.problems.remove(store) } is AmazonLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message - AmazonLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = "Amazon session expired: sign in again." + AmazonLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = StoresState.notSignedInLine(store) AmazonLibrary.SyncResult.Throttled -> publish(app, AmazonLibrary.cached(app)) AmazonLibrary.SyncResult.Busy -> {} } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt index 75331ba2ca..401fd09cb6 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonLibrary.kt @@ -6,6 +6,7 @@ import android.util.Log import com.droiddeck.launcher.stores.CatalogItem import com.droiddeck.launcher.stores.Store import com.droiddeck.launcher.stores.StoreNet +import com.droiddeck.launcher.stores.StoresState import org.json.JSONArray import org.json.JSONObject import java.util.concurrent.Callable @@ -70,7 +71,7 @@ object AmazonLibrary { } onStatus("Checking sign-in…") val creds = AmazonCredentialStore.load(app) ?: return SyncResult.NotLoggedIn - val token = AmazonCredentialStore.getValidAccessToken(app) ?: return SyncResult.Failed("Amazon session expired: sign in again.") + val token = AmazonCredentialStore.getValidAccessToken(app) ?: return SyncResult.Failed(StoresState.notSignedInLine(Store.AMAZON)) onStatus("Fetching game list…") val all = AmazonApiClient.getEntitlements(token, creds.deviceSerial) if (all.isNullOrEmpty()) return if (cachedList.isEmpty()) SyncResult.Failed("No games found in your Amazon library") else SyncResult.Ok(cachedList) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt index 41d4a2367a..9fa0e05d06 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt @@ -53,7 +53,7 @@ object EpicBackend : StoreBackend, EpicLaunchSupport { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], result.games.map { EpicLibrary.toCatalogItem(app, it) }); StoresState.problems.remove(store) } is EpicLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message - EpicLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = "Epic session expired: sign in again." + EpicLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = StoresState.notSignedInLine(store) EpicLibrary.SyncResult.Throttled -> if (!StoresState.library.containsKey(store)) StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { EpicLibrary.toCatalogItem(app, it) }) EpicLibrary.SyncResult.Busy -> {} } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt index 0237d4de52..3de03c68d5 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt @@ -42,7 +42,7 @@ object GogBackend : StoreBackend { when (result) { is GogLibrary.SyncResult.Ok -> { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], result.games.map { GogLibrary.toCatalogItem(app, it) }); StoresState.problems.remove(store) } is GogLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message - GogLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = "GOG session expired: sign in again." + GogLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = StoresState.notSignedInLine(store) GogLibrary.SyncResult.Busy -> {} } } diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreAccountsCipherTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreAccountsCipherTest.kt new file mode 100644 index 0000000000..b4346c7ca5 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreAccountsCipherTest.kt @@ -0,0 +1,173 @@ +package com.droiddeck.launcher.stores + +import org.json.JSONObject +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File +import java.security.SecureRandom +import javax.crypto.SecretKey +import javax.crypto.spec.SecretKeySpec + +/** The stores' sign-in files sealed at rest; a plain key stands in for the AndroidKeyStore. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class StoreAccountsCipherTest { + private val app get() = RuntimeEnvironment.getApplication() + private fun newKey(): SecretKey = SecretKeySpec(ByteArray(32).also { SecureRandom().nextBytes(it) }, "AES") + private val key = newKey() + private val fake = CredentialCipher.KeyProvider { key } + private val broken = CredentialCipher.KeyProvider { throw java.security.KeyStoreException("no keystore") } + + private val creds = JSONObject().put("access_token", "AT-secret-123456").put("refresh_token", "RT-secret-654321") + .put("account_id", "acct-1").put("display_name", "Someone").put("expires_at", 1_900_000_000_000L) + + private fun file(store: Store) = StoreAccounts.credentialsFile(app, store) + + @Before fun setUp() { StoreAccounts.keys = fake; StoreAccounts.keystoreFailed = false } + @After fun tearDown() { + StoreAccounts.keys = CredentialCipher.Keystore; StoreAccounts.keystoreFailed = false + for (store in Store.entries) StoreAccounts.clear(app, store) + } + + @Test fun envelopeRoundTrips() { + val envelope = CredentialCipher.seal(creds.toString(), fake) + assertEquals(1, envelope.getInt("v")) + assertEquals("AES/GCM", envelope.getString("alg")) + assertFalse(envelope.toString().contains("secret")) + assertEquals(creds.toString(), CredentialCipher.open(envelope, fake)) + // A fresh IV every time. + assertFalse(envelope.getString("iv") == CredentialCipher.seal(creds.toString(), fake).getString("iv")) + } + + @Test fun writtenCredentialsAreSealedOnDisk() { + StoreAccounts.write(app, Store.EPIC, creds) + val text = file(Store.EPIC).readText() + assertFalse(text.contains("AT-secret") || text.contains("RT-secret") || text.contains("Someone")) + assertTrue(CredentialCipher.isEnvelope(JSONObject(text))) + assertEquals(creds.toString(), StoreAccounts.read(app, Store.EPIC).toString()) + assertEquals("Someone", StoreAccounts.signedInAs(app, Store.EPIC)) + assertFalse(File(file(Store.EPIC).path + ".tmp").exists()) + } + + @Test fun aPlainFileFromAnEarlierBuildIsSealedAndStaysSignedIn() { + for (store in Store.entries) file(store).writeText(creds.toString()) + File(file(Store.GOG).path + ".tmp").writeText(creds.toString()) + + StoreAccounts.encryptAll(app) + + for (store in Store.entries) { + val text = file(store).readText() + assertTrue(store.id, CredentialCipher.isEnvelope(JSONObject(text))) + assertFalse(store.id, text.contains("secret")) + assertEquals(store.id, creds.toString(), StoreAccounts.read(app, store).toString()) + } + assertFalse(File(file(Store.GOG).path + ".tmp").exists()) + } + + @Test fun aTamperedFileReadsAsSignedOutAndIsDeleted() { + StoreAccounts.write(app, Store.GOG, creds) + val envelope = JSONObject(file(Store.GOG).readText()) + val ct = android.util.Base64.decode(envelope.getString("ct"), android.util.Base64.NO_WRAP) + ct[0] = (ct[0].toInt() xor 1).toByte() + file(Store.GOG).writeText(envelope.put("ct", android.util.Base64.encodeToString(ct, android.util.Base64.NO_WRAP)).toString()) + + assertNull(StoreAccounts.read(app, Store.GOG)) + assertNull(StoreAccounts.signedInAs(app, Store.GOG)) + assertFalse(file(Store.GOG).exists()) + } + + @Test fun aFileSealedUnderALostKeyReadsAsSignedOutAndIsDeleted() { + StoreAccounts.write(app, Store.AMAZON, creds) + // Data cleared, or a backup restored on another device: the Keystore key is a new one. + val other = newKey() + StoreAccounts.keys = CredentialCipher.KeyProvider { other } + assertNull(StoreAccounts.read(app, Store.AMAZON)) + assertFalse(file(Store.AMAZON).exists()) + } + + @Test fun garbageReadsAsSignedOut() { + file(Store.EPIC).writeText("{\"v\":1,\"alg\":\"AES/GCM\",\"iv\":\"!!\",\"ct\":\"??\"}") + assertNull(StoreAccounts.read(app, Store.EPIC)) + file(Store.EPIC).writeText("not json") + assertNull(StoreAccounts.read(app, Store.EPIC)) + } + + @Test fun withoutAKeystoreCredentialsStayPlainAndTheNextStartSealsThem() { + StoreAccounts.keys = broken + StoreAccounts.write(app, Store.EPIC, creds) + assertEquals(creds.toString(), file(Store.EPIC).readText()) + assertEquals(creds.toString(), StoreAccounts.read(app, Store.EPIC).toString()) + assertTrue(StoreAccounts.keystoreFailed) + + // The next start, on a Keystore that works. + StoreAccounts.keys = fake + StoreAccounts.keystoreFailed = false + StoreAccounts.encryptAll(app) + assertTrue(CredentialCipher.isEnvelope(JSONObject(file(Store.EPIC).readText()))) + assertEquals(creds.toString(), StoreAccounts.read(app, Store.EPIC).toString()) + } + + @Test fun signOutRemovesTheFile() { + StoreAccounts.write(app, Store.GOG, creds) + StoreAccounts.clear(app, Store.GOG) + assertFalse(file(Store.GOG).exists()) + assertNull(StoreAccounts.read(app, Store.GOG)) + } + + /** A Keystore whose key is there but which fails to hand it over [failures] times first. */ + private class Flaky(private val key: SecretKey, var failures: Int, private val error: () -> Exception) : CredentialCipher.KeyProvider { + override fun key(): SecretKey = key + override fun existing(): SecretKey { if (failures > 0) { failures--; throw error() }; return key } + } + + @Test fun aTransientKeystoreFailureKeepsTheFileAndALaterReadSucceeds() { + StoreAccounts.write(app, Store.EPIC, creds) + val sealed = file(Store.EPIC).readText() + for (error in listOf<() -> Exception>( + { java.security.ProviderException("busy") }, + { java.security.KeyStoreException("keystore down") }, + { java.security.InvalidKeyException("not now") }, + { java.io.IOException("read") }, + )) { + StoreAccounts.keys = Flaky(key, 1, error) + assertNull(StoreAccounts.read(app, Store.EPIC)) + assertTrue(StoreAccounts.isUnavailable(Store.EPIC)) + assertEquals(sealed, file(Store.EPIC).readText()) + // Still signed in for the page: no sign-in card, and an action says why it failed. + assertEquals("Epic sign-in could not be read: try again.", StoresState.notSignedInLine(Store.EPIC)) + + // The next access, once the Keystore answers. + assertEquals(creds.toString(), StoreAccounts.read(app, Store.EPIC).toString()) + assertFalse(StoreAccounts.isUnavailable(Store.EPIC)) + assertEquals("Someone", StoreAccounts.signedInAs(app, Store.EPIC)) + assertEquals("Epic session expired: sign in again.", StoresState.notSignedInLine(Store.EPIC)) + } + } + + @Test fun signedInAsKeepsTheStoreWhileItsSignInIsUnavailable() { + StoreAccounts.write(app, Store.GOG, creds) + StoreAccounts.keys = Flaky(key, 5) { java.security.ProviderException("busy") } + assertEquals("GOG", StoreAccounts.signedInAs(app, Store.GOG)) + assertTrue(file(Store.GOG).exists()) + } + + @Test fun aKeyGoneFromTheKeystoreReadsAsSignedOutAndIsDeleted() { + StoreAccounts.write(app, Store.AMAZON, creds) + StoreAccounts.keys = object : CredentialCipher.KeyProvider { + override fun key(): SecretKey = newKey() + override fun existing(): SecretKey = throw CredentialCipher.KeyMissing() + } + assertNull(StoreAccounts.read(app, Store.AMAZON)) + assertFalse(StoreAccounts.isUnavailable(Store.AMAZON)) + assertFalse(file(Store.AMAZON).exists()) + } +} diff --git a/docs/development/stores.md b/docs/development/stores.md index ba6963ee5c..4ce7d1aab3 100644 --- a/docs/development/stores.md +++ b/docs/development/stores.md @@ -189,5 +189,37 @@ Store log lines (the engines' included) go through `StoresState.logLine`, which (`StoreLogFiles`: one file a day, seven days kept, ~2 MB each before it rolls to `.1`). Nothing shows them in the app; the session's Share logs zip carries them under `stores/`. -Credentials live in `filesDir/stores/<store>/credentials.json` only (`StoreAccounts`); store URLs -are logged through `StoreLog.redactUrl`, which drops the query string where the signed tokens live. +Store URLs are logged through `StoreLog.redactUrl`, which drops the query string where the signed +tokens live. + +## Security + +- **Sign-ins:** `filesDir/stores/<store>/credentials.json` only, read and written through + `StoreAccounts` alone (`GogAuth`, `EpicCredentialStore`, `AmazonCredentialStore`, cloud saves + all call it). The file is an envelope `{"v":1,"alg":"AES/GCM","iv":...,"ct":...}` (base64): + AES-256-GCM (`CredentialCipher`) under a key generated in the AndroidKeyStore (alias + `droiddeck-store-credentials`; encrypt/decrypt, GCM, no padding, not exportable, no user + authentication so background downloads and launch-time exchange codes work with the screen off; + StrongBox where present, else the TEE). Written through a temp file and a rename, mode 600. +- **Earlier plain files:** sealed at app start (`StoreAccounts.encryptAll`, and on any read), + verified by opening the result, the plain file replaced; the log says + `stores: credentials encrypted <store>`. A plain `.tmp` left by an older build is deleted. +- **No Keystore** (some ROMs): the file stays plain, the log says + `stores: keystore unavailable, credentials stay plain (<exception class>)` once, and the next + start tries again. +- **A file that can never open** - it does not authenticate (`AEADBadTagException`: tampered, or + another key after data was cleared or a backup restored on another device), the key is + permanently invalidated or gone from the Keystore, or it is not an envelope: deleted, and the + store reads as signed out, so its sign-in card shows. +- **A failure that may pass** (`KeyStoreException`, `ProviderException`, any other + `InvalidKeyException`, an I/O error, StrongBox busy): the file is kept, the store stays signed in + (no sign-in card) and is marked unavailable (`StoreAccounts.isUnavailable`), a store action shows + `<Store> sign-in could not be read: try again.`, the log says + `stores: credentials unreadable this start (<exception class>)` once, and the next access tries + again. +- **The Linux session:** the app's files directory is bound into every session, so the guest can + see `credentials.json` - as the envelope only; the key never leaves the Keystore. Nothing in the + guest reads the files: `droiddeck-store-launch` asks the app over `<session>/stores/req|resp` + for an Epic exchange code, which the app writes beside the game's launcher; the launcher reads + it into a variable and deletes it before the game starts (single use, expires in minutes). + Steam's own client files are outside this. diff --git a/docs/progress-log.md b/docs/progress-log.md index 23d1529b76..7d15e1d2cc 100644 --- a/docs/progress-log.md +++ b/docs/progress-log.md @@ -7,6 +7,26 @@ the timeline, then lessons and backlog. Companion to the README (what the app *d --- +## 2026-10-09 - `feat/store-credentials-keystore`: store sign-ins sealed with the Android Keystore + +- **What:** `filesDir/stores/<store>/credentials.json` (GOG, Epic, Amazon: tokens, ids, names, + expiry) is now an AES-256-GCM envelope (`{"v":1,"alg":"AES/GCM","iv","ct"}`) under a + non-exportable AndroidKeyStore key (StrongBox if present, else TEE; no user authentication). + `StoreAccounts` is the one layer; `CredentialCipher` holds the envelope and the key. +- **Upgrade:** a plain file is sealed at the first start (verified, then replaced); users stay + signed in. Log: `stores: credentials encrypted <store>`. +- **Fallbacks:** no Keystore - stays plain, logged once, retried next start. A file that can never + open (bad tag, key invalidated or missing, not an envelope) - deleted, signed out (sign-in card). + Any other Keystore or I/O failure - file kept, store stays signed in but unavailable (actions show + `<Store> sign-in could not be read: try again.`), logged once, retried on the next access. Never + a crash. +- **Session:** the guest sees the envelope only; it never read the files (the Epic code comes over + the request channel, written beside the launcher and deleted by it). +- **Tests:** `StoreAccountsCipherTest` (round trip, sealed on disk, migration of all three stores, + tampered file, lost key, key missing, garbage, no-Keystore fallback then sealed next start, + transient Keystore / I/O failures keep the file and a later read succeeds, sign-out). Not yet + on a device. + ## 2026-10-08 - `feat/private-logs`: every log in app-private storage, shared as one scrubbed zip - **Where logs live:** `files/logs/` (`LinuxRuntime.logDir`), nothing under `Download/DroidDeck` From c761b831129e8bbf75b35d0ea9f2a7eed276eb64 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 9 Oct 2026 14:55:03 -0400 Subject: [PATCH 10/54] feat(stores): bring the launcher's motion to the stores pages (#482) * refactor(ui): share End session's step-out as StepOut * feat(ui): let PageFlood carry a tile's art * feat(stores): bring the launcher's motion to the stores pages * fix(stores): lay out the pane's rows in a column * fix(stores): a game page no longer leaves the pad's focus on a dead card and crashes the next press * perf(stores): GOG remembers its non-games and the library cache loads off the main thread * fix(ui): a step-out's backdrop never takes the pad's focus * feat(stores): modes step out of the store chip, each store in its own colour, search in the header * feat(stores): sign-in pages in the Steam tab's style and an are-you-sure for sign out * style(stores): drop the sign-in page's store eyebrow, which the title already names * style(stores): the sign-in page is the store's name and Sign in * fix(ui): the focus ring follows a control until it stops and meets one still rising into place * feat(stores): the store a pad's focus is on shows without A, and a press never lands on where it sent focus * perf(stores): keep chip motion responsive while libraries change --- .../com/droiddeck/launcher/MainActivity.kt | 11 +- .../launcher/stores/StoreLoginActivity.kt | 27 +- .../droiddeck/launcher/stores/StoresState.kt | 5 + .../launcher/stores/amazon/AmazonBackend.kt | 12 +- .../launcher/stores/epic/EpicBackend.kt | 14 +- .../launcher/stores/gog/GogBackend.kt | 14 +- .../launcher/stores/gog/GogLibrary.kt | 25 +- .../com/droiddeck/launcher/ui/FocusGlide.kt | 34 +- .../com/droiddeck/launcher/ui/FrontEndRail.kt | 3 +- .../com/droiddeck/launcher/ui/PageFlood.kt | 29 +- .../droiddeck/launcher/ui/SessionOverlay.kt | 39 - .../droiddeck/launcher/ui/SettingsWidgets.kt | 5 + .../com/droiddeck/launcher/ui/SteamHome.kt | 17 +- .../java/com/droiddeck/launcher/ui/StepOut.kt | 326 ++++++ .../com/droiddeck/launcher/ui/StoresDetail.kt | 23 +- .../droiddeck/launcher/ui/StoresDownloads.kt | 284 +++++- .../com/droiddeck/launcher/ui/StoresMotion.kt | 390 +++++++ .../com/droiddeck/launcher/ui/StoresPage.kt | 962 +++++++++++++++--- .../launcher/ui/WirelessAdbFixPage.kt | 11 + app/src/main/res/values/strings.xml | 5 + .../com/droiddeck/launcher/ui/StepOutTest.kt | 54 + .../droiddeck/launcher/ui/StoreTabsTest.kt | 12 +- 22 files changed, 2005 insertions(+), 297 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StepOut.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/StoresMotion.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/StepOutTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 95545f537f..9adc0cbef7 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -916,6 +916,7 @@ class MainActivity : ComponentActivity() { /** On the Components page the pad's LB / RB step through GPU drivers, FEX, DXVK and VKD3D-Proton, wrapping around. */ override fun dispatchKeyEvent(event: KeyEvent): Boolean { + if (event.keyCode == KeyEvent.KEYCODE_BUTTON_A) com.droiddeck.launcher.ui.HeldKeys.confirm = event.action == KeyEvent.ACTION_DOWN if (showComponents && (event.keyCode == KeyEvent.KEYCODE_BUTTON_L1 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R1)) { if (event.action == KeyEvent.ACTION_DOWN && event.repeatCount == 0) { val all = listOf(com.droiddeck.launcher.ui.GPU_TAB) + ComponentsManager.COMPONENTS @@ -924,7 +925,15 @@ class MainActivity : ComponentActivity() { } return true } - return super.dispatchKeyEvent(event) + // Compose throws when its focus tree has lost the focused node; one key is dropped + // instead of the app. + return try { + super.dispatchKeyEvent(event) + } catch (e: IllegalStateException) { + if (e.message?.contains("active focus target") != true) throw e + android.util.Log.w("MainActivity", "key ${event.keyCode} dropped: ${e.message}") + true + } } override fun onResume() { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt index b7ffa3fbd1..277d42bb59 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLoginActivity.kt @@ -47,6 +47,11 @@ class StoreLoginActivity : ComponentActivity() { private const val TAG = "StoreLogin" const val EXTRA_STORE = "store" private const val KEY_STATE = "oauth_state" + /** + * The colour the Stores page flooded to before opening this page, read once: the page opens + * on it, with no window animation, and its chrome rises in, as a session opens on Play's blue. + */ + @Volatile internal var floodColor: Int? = null private const val CHROME_UA = "Mozilla/5.0 (Linux; Android 14; SM-S918B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.6533.103 Mobile Safari/537.36" fun generateState(): String { @@ -80,6 +85,8 @@ class StoreLoginActivity : ComponentActivity() { private lateinit var titleText: TextView private var oauthState: String? = null private val captured = AtomicBoolean(false) + /** Opened behind a flood: it opens and closes with no window animation. */ + private var flooded = false private fun dp(value: Int): Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value.toFloat(), resources.displayMetrics).toInt() @@ -88,8 +95,20 @@ class StoreLoginActivity : ComponentActivity() { store = Store.byId(intent.getStringExtra(EXTRA_STORE)) oauthState = savedInstanceState?.getString(KEY_STATE) ?: generateState() flow = store?.let { LoginFlows.forStore(it, oauthState!!) } + floodColor?.let { c -> + floodColor = null + flooded = true + window.setBackgroundDrawable(android.graphics.drawable.ColorDrawable(c)) + @Suppress("DEPRECATION") overridePendingTransition(0, 0) + } if (flow == null) { finish(); return } - setContentView(buildChrome()) + val chrome = buildChrome() + setContentView(chrome) + if (flooded) { + chrome.alpha = 0f + chrome.translationY = dp(14).toFloat() + chrome.animate().alpha(1f).translationY(0f).setStartDelay(60).setDuration(320).start() + } val webView = newWebView(flow!!.userAgent, isPopup = false) webViewRef = webView contentHost.addView(webView, 0, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)) @@ -105,6 +124,12 @@ class StoreLoginActivity : ComponentActivity() { webView.loadUrl(flow!!.startUrl) } + override fun finish() { + super.finish() + // Back to the Stores page, which is still covered in the flood and drains it from there. + @Suppress("DEPRECATION") if (flooded) overridePendingTransition(0, 0) + } + private fun buildChrome(): View { val root = LinearLayout(this).apply { orientation = LinearLayout.VERTICAL; setBackgroundColor(0xFF0A0B0D.toInt()) } val bar = LinearLayout(this).apply { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt index c969e19e14..954293fcdc 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt @@ -158,6 +158,11 @@ object StoresState { if (force || !shelves.containsKey(store)) backend.loadShelves(app, force) } + /** A store not signed into: its public storefront only, for its sign-in page to show. */ + fun preview(context: Context, store: Store) { + if (!shelves.containsKey(store)) backends[store]?.loadShelves(context.applicationContext, false) + } + fun signIn(context: Context, store: Store) { expired.remove(store) backends[store]?.signIn(context) ?: logLine("${store.label}: this build has no sign-in for it yet") diff --git a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt index cf40c19e67..1523741a4d 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/amazon/AmazonBackend.kt @@ -45,11 +45,15 @@ object AmazonBackend : StoreBackend { override fun syncLibrary(context: Context, force: Boolean) { val app = context.applicationContext - val cached = AmazonLibrary.cached(app) - if (cached.isNotEmpty()) StoresState.post { publish(app, cached) } - StoresState.post { StoresState.status[store] = "Fetching your library…" } Thread({ - val result = AmazonLibrary.sync(app, force) { line -> StoresState.post { StoresState.status[store] = line } } + // The cache off the main thread; with it on screen a background sync keeps quiet. + val cached = AmazonLibrary.cached(app) + val quiet = cached.isNotEmpty() && !force + StoresState.post { + if (cached.isNotEmpty()) publish(app, cached) + if (!quiet) StoresState.status[store] = "Fetching your library…" + } + val result = AmazonLibrary.sync(app, force) { line -> if (!quiet) StoresState.post { StoresState.status[store] = line } } StoresState.post { StoresState.status.remove(store) when (result) { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt index 9fa0e05d06..3d1d8de94b 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/epic/EpicBackend.kt @@ -41,11 +41,15 @@ object EpicBackend : StoreBackend, EpicLaunchSupport { override fun syncLibrary(context: Context, force: Boolean) { val app = context.applicationContext - val cached = EpicLibrary.cached(app) - if (cached.isNotEmpty()) StoresState.post { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { EpicLibrary.toCatalogItem(app, it) }) } - StoresState.post { StoresState.status[store] = "Fetching your library…" } Thread({ - val result = EpicLibrary.sync(app, force) { line -> StoresState.post { StoresState.status[store] = line } } + // The cache off the main thread; with it on screen a background sync keeps quiet. + val cached = EpicLibrary.cached(app).map { EpicLibrary.toCatalogItem(app, it) } + val quiet = cached.isNotEmpty() && !force + StoresState.post { + if (cached.isNotEmpty()) StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached) + if (!quiet) StoresState.status[store] = "Fetching your library…" + } + val result = EpicLibrary.sync(app, force) { line -> if (!quiet) StoresState.post { StoresState.status[store] = line } } StoresState.post { StoresState.status.remove(store) when (result) { @@ -54,7 +58,7 @@ object EpicBackend : StoreBackend, EpicLaunchSupport { } is EpicLibrary.SyncResult.Failed -> StoresState.problems[store] = result.message EpicLibrary.SyncResult.NotLoggedIn -> StoresState.problems[store] = StoresState.notSignedInLine(store) - EpicLibrary.SyncResult.Throttled -> if (!StoresState.library.containsKey(store)) StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { EpicLibrary.toCatalogItem(app, it) }) + EpicLibrary.SyncResult.Throttled -> if (!StoresState.library.containsKey(store)) StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached) EpicLibrary.SyncResult.Busy -> {} } } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt index 3de03c68d5..1dbf53460d 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogBackend.kt @@ -31,12 +31,16 @@ object GogBackend : StoreBackend { override fun syncLibrary(context: Context, force: Boolean) { val app = context.applicationContext - // The cache first, so the tab has something while the sync runs. - val cached = GogLibrary.cached(app) - if (cached.isNotEmpty()) StoresState.post { StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached.map { GogLibrary.toCatalogItem(app, it) }) } - StoresState.post { StoresState.status[store] = "Fetching your library…" } Thread({ - val result = GogLibrary.sync(app, force) { line -> StoresState.post { StoresState.status[store] = line } } + // The cache first, so the tab has something while the sync runs; read here, not on the + // main thread. With a cache on screen a background sync keeps quiet: only Refresh shows its bar. + val cached = GogLibrary.cached(app).map { GogLibrary.toCatalogItem(app, it) } + val quiet = cached.isNotEmpty() && !force + StoresState.post { + if (cached.isNotEmpty()) StoresState.library[store] = com.droiddeck.launcher.stores.mergeItems(StoresState.library[store], cached) + if (!quiet) StoresState.status[store] = "Fetching your library…" + } + val result = GogLibrary.sync(app, force) { line -> if (!quiet) StoresState.post { StoresState.status[store] = line } } StoresState.post { StoresState.status.remove(store) when (result) { diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt index 7012efaab8..778f972a43 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogLibrary.kt @@ -23,6 +23,9 @@ object GogLibrary { // v2: the card art moved from the store page's backdrop to the Galaxy library art; an older cache is re-fetched. private const val CACHE_KEY = "library_cache_v2" private const val LAST_SYNC_KEY = "library_synced_at" + // Owned products that are not Windows games (DLC, films, soundtracks, other platforms): never in + // the cache, so without this every sync would fetch each of them again. + private const val SKIPPED_KEY = "library_skipped" private const val THROTTLE_MS = 6L * 60L * 60L * 1000L private val syncing = AtomicBoolean(false) @@ -75,18 +78,22 @@ object GogLibrary { val stale = System.currentTimeMillis() - p.getLong(LAST_SYNC_KEY, 0L) >= THROTTLE_MS val heavy = force || cachedList.isEmpty() || stale val cachedIds = cachedList.mapTo(HashSet()) { it.gameId } - val idsToFetch = if (heavy) ids else ids.filter { it !in cachedIds } + val skipped = java.util.Collections.newSetFromMap(java.util.concurrent.ConcurrentHashMap<String, Boolean>()) + if (!heavy) skipped.addAll(p.getStringSet(SKIPPED_KEY, emptySet()).orEmpty()) + val idsToFetch = if (heavy) ids else ids.filter { it !in cachedIds && it !in skipped } val fetchSet = idsToFetch.toHashSet() val merged = LinkedHashMap<String, GogGame>() for (g in cachedList) if (g.gameId in ownedSet && g.gameId !in fetchSet) merged[g.gameId] = g if (idsToFetch.isEmpty()) { val list = merged.values.toList() + skipped.retainAll(ownedSet) + p.edit().putStringSet(SKIPPED_KEY, HashSet(skipped)).apply() saveCache(p, list) return SyncResult.Ok(list.sortedBy { it.title.lowercase() }, 0) } onStatus("Syncing ${idsToFetch.size} game${if (idsToFetch.size == 1) "" else "s"}…") val pool = Executors.newFixedThreadPool(5) - val futures = idsToFetch.map { id -> pool.submit(Callable<GogGame?> { fetchGame(p, id, token) }) } + val futures = idsToFetch.map { id -> pool.submit(Callable<GogGame?> { fetchGame(p, id, token, skipped) }) } pool.shutdown() var fetched = 0 for ((idx, f) in futures.withIndex()) { @@ -96,6 +103,8 @@ object GogLibrary { } val finalList = merged.values.toList() saveCache(p, finalList) + skipped.retainAll(ownedSet) + p.edit().putStringSet(SKIPPED_KEY, HashSet(skipped)).apply() if (heavy) p.edit().putLong(LAST_SYNC_KEY, System.currentTimeMillis()).apply() Log.i(TAG, "sync: owned=${ids.size} fetched=$fetched cached=${finalList.size} heavy=$heavy") return SyncResult.Ok(finalList.sortedBy { it.title.lowercase() }, fetched) @@ -108,16 +117,18 @@ object GogLibrary { } /** One product's details, or null for anything that is not a Windows game (DLC, movies, other platforms). */ - private fun fetchGame(prefs: SharedPreferences, id: String, token: String): GogGame? { + private fun fetchGame(prefs: SharedPreferences, id: String, token: String, skipped: MutableSet<String>): GogGame? { + // Not a game this app installs: remembered, so the next sync does not ask again. + fun skip(): GogGame? { skipped.add(id); return null } try { val productJson = StoreNet.get("https://api.gog.com/products/$id?expand=downloads,description", bearer = token) ?: return null val prod = JSONObject(productJson) - if (prod.optBoolean("is_secret", false)) return null + if (prod.optBoolean("is_secret", false)) return skip() val gameType = prod.optString("game_type", "") - if (gameType.isNotEmpty() && gameType != "game" && gameType != "pack") return null + if (gameType.isNotEmpty() && gameType != "game" && gameType != "pack") return skip() var title = prod.optJSONObject("title")?.optString("*") if (title.isNullOrEmpty()) title = prod.optString("title") - if (title.isNullOrEmpty()) return null + if (title.isNullOrEmpty()) return skip() // Card art from the v2 game record, the way the Galaxy client draws its library: the dark // Galaxy background for the wide card and the box art for the tall one. The product's // `images.background` is the store page's fade-out backdrop (it ends in white on a @@ -164,7 +175,7 @@ object GogLibrary { prod.optJSONObject("downloads")?.optJSONArray("installers")?.let { arr -> for (di in 0 until arr.length()) if (arr.optJSONObject(di)?.optString("os", "") == "windows") { hasWindowsInstaller = true; break } } - if (!hasWindowsBuild && !hasWindowsInstaller) return null + if (!hasWindowsBuild && !hasWindowsInstaller) return skip() var verticalCover: String? = boxArt.ifEmpty { prefs.getString("vcover_$id", null) } if (verticalCover.isNullOrEmpty()) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt b/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt index eb74f6c27c..ac8ba10850 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FocusGlide.kt @@ -147,6 +147,10 @@ private fun follow(): AnimationSpec<Float> = Motion.sp(1f, 2400f) /** How long after a focus move the ring keeps its stretchy springs and keeps checking where it is. */ private const val SETTLE_MS = 450L +/** How long the control must stand still before the ring stops checking where it is. */ +private const val STILL_MS = 120L +/** The longest the ring keeps checking after a move, for a control that never stands still. */ +private const val TRACK_MAX_MS = 2_000L /** Moves closer together than this are a held direction: the ring runs along as one, no stretch. */ private const val REPEAT_MS = 180L @@ -213,17 +217,23 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable launch { corner.animateTo(r, Motion.tw(90)) } dot(here).forEachIndexed { i, v -> launch { edges[i].animateTo(v, Motion.tw(90)) } } } - // Across as a dot, every edge together, on a timed ease. + // Across as a dot, every edge together, on a timed ease, steering each frame for + // where the control is now: one still rising into place is met where it ends up. val distance = hypot(b.center.x - here.x, b.center.y - here.y) / density.density val travel = (180 + distance * 0.12f).toInt().coerceIn(220, 380) - dot(b.center).forEachIndexed { i, v -> - jobs[i] = launch { edges[i].animateTo(v, Motion.tw(travel, easing = FastOutSlowInEasing)) } - } + val p = Animatable(0f) + val flight = launch { p.animateTo(1f, Motion.tw(travel, easing = FastOutSlowInEasing)) } // Takes the control's shape as it arrives, not after: no resting as a dot. - delay(Motion.ms((travel * 0.78f).toInt()).toLong()) + while (flight.isActive && p.value < 0.78f) { + val to = (glide.boundsOf(s) ?: b).center + dot(Offset(here.x + (to.x - here.x) * p.value, here.y + (to.y - here.y) * p.value)).forEachIndexed { i, v -> edges[i].snapTo(v) } + withFrameNanos { } + } + flight.cancel() + val at = glide.boundsOf(s) ?: b launch { solid.animateTo(0f, Motion.tw(150)) } launch { corner.animateTo(endCorner, Motion.sp(0.8f, 600f)) } - listOf(b.left, b.top, b.right, b.bottom).forEachIndexed { i, v -> + listOf(at.left, at.top, at.right, at.bottom).forEachIndexed { i, v -> jobs[i] = launch { edges[i].animateTo(v, Motion.sp(0.7f, 750f)) } } } @@ -267,8 +277,11 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable var first = src !== shown val start = System.nanoTime() var last: Rect? = null + var seen: Rect? = null + var stillSince = start while (true) { val b = glide.boundsOf(src) + if (b != seen) { seen = b; stillSince = System.nanoTime() } if (b == null) { if (first) scope.launch { alpha.animateTo(0f, Motion.tw(140)) } } else if (first) { @@ -318,9 +331,12 @@ internal fun FocusGlideHost(modifier: Modifier = Modifier, content: @Composable if (last == null) scope.launch { alpha.animateTo(1f, Motion.tw(120)) } last = b } - // Layer animations (a tile growing, a row sliding) move it without a relayout, so - // look again each frame for a moment after anything changes. - if ((System.nanoTime() - start) / 1_000_000 > SETTLE_MS) break + // Layer animations (a tile growing, a row sliding, a page rising in) move it without + // a relayout, so look again each frame until it has stood still a moment: a fixed + // window let the ring park mid-rise and jump over when something next laid out. + val now = System.nanoTime() + val settled = (now - start) / 1_000_000 > SETTLE_MS && (now - stillSince) / 1_000_000 > STILL_MS + if (settled || (now - start) / 1_000_000 > TRACK_MAX_MS) break withFrameNanos { } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt index ac8ebc1633..e5f91a76ec 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndRail.kt @@ -193,8 +193,9 @@ private fun RailItem( if (count > 0) Text( if (count > 9) "9+" else count.toString(), fontSize = 10.sp, fontWeight = FontWeight.Bold, color = pal.onSignal, maxLines = 1, + // Pops with the Downloads chip's count as an install's dot lands there. modifier = Modifier.align(Alignment.TopEnd).padding(top = if (iconOnly) 4.dp else 6.dp, end = if (iconOnly) 5.dp else 12.dp) - .clip(RoundedCornerShape(8.dp)).background(pal.signal).padding(horizontal = 5.dp, vertical = 1.dp), + .landingPop().clip(RoundedCornerShape(8.dp)).background(pal.signal).padding(horizontal = 5.dp, vertical = 1.dp), ) } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt b/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt index 9a0cfa8949..a2b24b594d 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/PageFlood.kt @@ -22,7 +22,13 @@ import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.DrawScope +import androidx.compose.ui.geometry.RoundRect +import androidx.compose.ui.geometry.isSpecified +import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.drawscope.clipPath +import androidx.compose.ui.graphics.drawscope.translate +import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.unit.dp @@ -67,7 +73,13 @@ internal const val PAGE_RETURN_MS = 900 * The focus ring stays out of it until it is done. */ @Composable -internal fun PageFlood(from: Origin, leaving: Boolean, content: @Composable () -> Unit) { +internal fun PageFlood( + from: Origin, + leaving: Boolean, + /** What the tile shows instead of the control's fill: a game card's art, cropped to the tile as it grows. */ + art: Painter? = null, + content: @Composable () -> Unit, +) { val pal = LocalPalette.current val glide = LocalFocusGlide.current // Left, top, right, bottom: 0 on the control, 1 on the pane's edge. @@ -113,7 +125,7 @@ internal fun PageFlood(from: Origin, leaving: Boolean, content: @Composable () - Box(Modifier.fillMaxSize().clipToBounds().onSizeChanged { size = Size(it.width.toFloat(), it.height.toFloat()) }) { Box(Modifier.fillMaxSize().graphicsLayer { alpha = shown.value; translationY = (1f - shown.value) * 14.dp.toPx() }) { content() } Canvas(Modifier.fillMaxSize()) { - if (tile.value > 0.01f) drawTile(from, edges.map { it.value }, pal.surface, pal.line2, tile.value) + if (tile.value > 0.01f) drawTile(from, edges.map { it.value }, pal.surface, pal.line2, tile.value, art) } } } @@ -122,7 +134,7 @@ internal fun PageFlood(from: Origin, leaving: Boolean, content: @Composable () - * The control's tile with its edges [v] of the way to the pane's (left, top, right, bottom): * sinking with the page behind, blobby in flight, square once it fills the pane. */ -private fun DrawScope.drawTile(from: Origin, v: List<Float>, fill: Color, line: Color, alpha: Float) { +private fun DrawScope.drawTile(from: Origin, v: List<Float>, fill: Color, line: Color, alpha: Float, art: Painter? = null) { val (l, t, r, b) = v val mean = v.sumOf { it.coerceIn(0f, 1f).toDouble() }.toFloat() / 4f // The control sinks with the page behind it (scaled to 0.95 about the pane's centre). @@ -139,6 +151,17 @@ private fun DrawScope.drawTile(from: Origin, v: List<Float>, fill: Color, line: val blob = sin(PI * mean).toFloat().coerceAtLeast(0f) val corner = lerp(from.corner * (1f - mean), minOf(w, h) * 0.42f, blob) drawRoundRect(fill, Offset(left, top), Size(w, h), CornerRadius(corner), alpha = alpha) + val pic = art?.intrinsicSize?.takeIf { it.isSpecified && it.width > 0f && it.height > 0f } + if (art != null && pic != null && w > 0f && h > 0f) { + // The art covers the tile at every size (a centre crop), so a portrait card opens into the + // wide pane without stretching. + val k = maxOf(w / pic.width, h / pic.height) + val dw = pic.width * k + val dh = pic.height * k + clipPath(Path().apply { addRoundRect(RoundRect(left, top, left + w, top + h, CornerRadius(corner))) }) { + translate(left + (w - dw) / 2f, top + (h - dh) / 2f) { with(art) { draw(Size(dw, dh), alpha = alpha) } } + } + } // The control's outline, kept while it is still the size of one. val edge = (1f - mean * 3f).coerceIn(0f, 1f) if (edge > 0f) drawRoundRect(line, Offset(left, top), Size(w, h), CornerRadius(corner), alpha = alpha * edge, style = Stroke(1.dp.toPx())) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index 4a5d4fe882..16716c6277 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -1003,45 +1003,6 @@ private val StepBoxCorner = 16.dp private val StepFillet = 10.dp private val StepBoxMaxWidth = 264.dp -/** - * The stair-step outline: the pill [p] on top, a box under it from [left] to the pill's right edge - * and down to [bottom], the pull between them [pullTop] (the box's top). Right edges are flush and - * the inside corner where the pull meets the box is rounded the other way. While the box is still - * no wider than the pill it is just the pill, stretched down. - */ -private fun stepPath(path: Path, p: Rect, left: Float, bottom: Float, pullTop: Float, box: Float, fillet: Float) { - path.reset() - val r = p.right; val tl = p.left; val tt = p.top - val pb = maxOf(bottom, p.bottom) - val l = minOf(left, tl) - val pt = minOf(pullTop, pb) - val sw = tl - l - val h = (pb - pt).coerceAtLeast(0f) - val rt = minOf(p.height / 2f, p.width / 2f) - val rbr = minOf(box, (pb - tt) / 2f) - val rbl = minOf(lerp(minOf(box, (pb - tt) / 2f), minOf(box, h / 2f), (sw / box).coerceIn(0f, 1f)), (r - l) / 2f) - val rpl = minOf(box, sw / 2f, h / 2f) - val f = minOf(fillet, sw / 2f) - val yl = minOf(pt + rpl, pb - rbl) - fun corner(rect: Rect, start: Float, sweep: Float, endX: Float, endY: Float) { - if (rect.width < 0.5f) path.lineTo(endX, endY) else path.arcTo(rect, start, sweep, false) - } - path.moveTo(tl + rt, tt) - path.lineTo(r - rt, tt) - corner(Rect(r - 2 * rt, tt, r, tt + 2 * rt), -90f, 90f, r, tt + rt) - path.lineTo(r, pb - rbr) - corner(Rect(r - 2 * rbr, pb - 2 * rbr, r, pb), 0f, 90f, r - rbr, pb) - path.lineTo(l + rbl, pb) - corner(Rect(l, pb - 2 * rbl, l + 2 * rbl, pb), 90f, 90f, l, pb - rbl) - path.lineTo(l, yl) - corner(Rect(l, pt, l + 2 * rpl, pt + 2 * rpl), 180f, 90f, l + rpl, pt) - path.lineTo(tl - f, pt) - corner(Rect(tl - 2 * f, pt - 2 * f, tl, pt), 90f, -90f, tl, pt - f) - path.lineTo(tl, tt + rt) - corner(Rect(tl, tt, tl + 2 * rt, tt + 2 * rt), 180f, 90f, tl + rt, tt) - path.close() -} - /** * Stop's confirm, grown out of the Stop pill at [pill] (px, in the sheet): a pull the pill's width * drops out of it and a wider box steps out to the left under it for the choices, right edges diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt index d894815aac..d091bc1bb5 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SettingsWidgets.kt @@ -115,6 +115,11 @@ import androidx.compose.ui.window.PopupProperties private val RowShape = RoundedCornerShape(12.dp) private val GroupShape = RoundedCornerShape(14.dp) +/** Whether a pad's A is held right now, as the activity sees it: a focus move waits for its release. */ +internal object HeldKeys { + @Volatile var confirm = false +} + internal fun Modifier.controllerConfirm(enabled: Boolean = true, onClick: () -> Unit): Modifier = onPreviewKeyEvent { event -> val keyEvent = event.nativeKeyEvent if (keyEvent.keyCode != KeyEvent.KEYCODE_BUTTON_A) { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt b/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt index be8df6b3de..db261e51a2 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SteamHome.kt @@ -126,6 +126,20 @@ internal fun CapsuleWall( games: List<Library.SteamGame>, driftMs: Int = if (games.isEmpty()) 75_000 else 40_000, modifier: Modifier = Modifier, +) = TiltedWall(games.size, driftMs, modifier) { i, m -> + Capsule(if (games.isEmpty()) null else games[i], RoundedCornerShape(10.dp), m) +} + +/** + * [CapsuleWall]'s tilted, drifting columns for any [count] of capsules; [capsule] draws number i + * (0 until [count], or 0 for each blank one when [count] is 0) at the modifier it is given. + */ +@Composable +internal fun TiltedWall( + count: Int, + driftMs: Int, + modifier: Modifier = Modifier, + capsule: @Composable (index: Int, modifier: Modifier) -> Unit, ) { val capW = 112.dp val capH = 168.dp @@ -165,8 +179,7 @@ internal fun CapsuleWall( }, ) { repeat(perRun * 2) { i -> - val g = if (games.isEmpty()) null else games[(c * 5 + (i % perRun) * 3) % games.size] - Capsule(g, RoundedCornerShape(10.dp), Modifier.size(capW, capH)) + capsule(if (count == 0) 0 else (c * 5 + (i % perRun) * 3) % count, Modifier.size(capW, capH)) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StepOut.kt b/app/src/main/java/com/droiddeck/launcher/ui/StepOut.kt new file mode 100644 index 0000000000..7ab5cbcc0d --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StepOut.kt @@ -0,0 +1,326 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.AnimationVector1D +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.LocalIndication +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.focusGroup +import androidx.compose.foundation.hoverable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.ColumnScope +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.widthIn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.runtime.withFrameNanos +import androidx.compose.ui.Alignment +import androidx.compose.ui.ExperimentalComposeUiApi +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.focus.FocusRequester +import androidx.compose.ui.focus.focusProperties +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Matrix +import androidx.compose.ui.graphics.Path +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.graphics.lerp +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.input.InputMode +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalInputModeManager +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.IntSize +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import kotlin.math.roundToInt + +// End session's confirm, for any control: a pull drops out of the control and a wider box steps +// out under it for the choices, one edge flush with the control. Stop draws it from its pill on +// the drawer; the stores draw it from Install, the Library tab and a download's cancel. + +/** + * The stair-step outline: the pill [p] on top, a box under it from [left] to the pill's right edge + * and down to [bottom], the pull between them [pullTop] (the box's top). Right edges are flush and + * the inside corner where the pull meets the box is rounded the other way. While the box is still + * no wider than the pill it is just the pill, stretched down. [pillCorner] is the control's own + * corner: a pill's is half its height. + */ +internal fun stepPath( + path: Path, p: Rect, left: Float, bottom: Float, pullTop: Float, box: Float, fillet: Float, + pillCorner: Float = minOf(p.height / 2f, p.width / 2f), +) { + path.reset() + val r = p.right; val tl = p.left; val tt = p.top + val pb = maxOf(bottom, p.bottom) + val l = minOf(left, tl) + val pt = minOf(pullTop, pb) + val sw = tl - l + val h = (pb - pt).coerceAtLeast(0f) + val rt = minOf(pillCorner, p.height / 2f, p.width / 2f) + val rbr = minOf(box, (pb - tt) / 2f) + val rbl = minOf(lerpF(minOf(box, (pb - tt) / 2f), minOf(box, h / 2f), (sw / box).coerceIn(0f, 1f)), (r - l) / 2f) + val rpl = minOf(box, sw / 2f, h / 2f) + val f = minOf(fillet, sw / 2f) + val yl = minOf(pt + rpl, pb - rbl) + fun corner(rect: Rect, start: Float, sweep: Float, endX: Float, endY: Float) { + if (rect.width < 0.5f) path.lineTo(endX, endY) else path.arcTo(rect, start, sweep, false) + } + path.moveTo(tl + rt, tt) + path.lineTo(r - rt, tt) + corner(Rect(r - 2 * rt, tt, r, tt + 2 * rt), -90f, 90f, r, tt + rt) + path.lineTo(r, pb - rbr) + corner(Rect(r - 2 * rbr, pb - 2 * rbr, r, pb), 0f, 90f, r - rbr, pb) + path.lineTo(l + rbl, pb) + corner(Rect(l, pb - 2 * rbl, l + 2 * rbl, pb), 90f, 90f, l, pb - rbl) + path.lineTo(l, yl) + corner(Rect(l, pt, l + 2 * rpl, pt + 2 * rpl), 180f, 90f, l + rpl, pt) + path.lineTo(tl - f, pt) + corner(Rect(tl - 2 * f, pt - 2 * f, tl, pt), 90f, -90f, tl, pt - f) + path.lineTo(tl, tt + rt) + corner(Rect(tl, tt, tl + 2 * rt, tt + 2 * rt), 180f, 90f, tl + rt, tt) + path.close() +} + +/** [stepPath] for a box that steps out to the right of the pill, out to [right], left edges flush. */ +internal fun stepPathRight( + path: Path, p: Rect, right: Float, bottom: Float, pullTop: Float, box: Float, fillet: Float, + pillCorner: Float = minOf(p.height / 2f, p.width / 2f), +) { + // Drawn as the left-hand outline about the pill's centre, which maps the pill onto itself, + // then mirrored back: x -> (p.left + p.right) - x. + val axis = p.left + p.right + stepPath(path, p, axis - right, bottom, pullTop, box, fillet, pillCorner) + path.transform(Matrix().apply { this[0, 0] = -1f; this[3, 0] = axis }) +} + +private fun lerpF(a: Float, b: Float, t: Float) = a + (b - a) * t + +/** Which way the box steps out of its control. */ +internal enum class StepSide { + /** Out to the left, right edges flush (a control at the right: Stop, a row's cancel). */ + Left, + /** Out to the right, left edges flush (a control at the left: Install, a tab). */ + Right, +} + +/** The choices' side of a [StepOut]: each piece fades in on its own beat, and [first] takes focus. */ +internal class StepOutScope(private val items: List<Animatable<Float, AnimationVector1D>>, val first: FocusRequester, val open: Boolean) { + /** Piece [i] (0 first) of the box: it fades and rises in after the box has stepped out. */ + fun Modifier.stepItem(i: Int): Modifier = graphicsLayer { + val v = items[i.coerceIn(0, items.lastIndex)].value + alpha = v + translationY = (1f - v) * 6.dp.toPx() + } +} + +/** Room between the control and the box that steps out under it: the pull. */ +private val OutPull = 6.dp +private val OutBoxCorner = 16.dp +private val OutFillet = 10.dp + +/** + * A confirm or a short list grown out of the control at [pill] (px, in this element's space, which + * should fill the screen area it may cover): a pull the control's width drops out of it, then a + * box steps out to [side] for [content]. [onDismiss] (B, a tap outside) asks to fold it; it folds + * back width first once [open] is false, then [onClosed]. Exactly StopConfirm's springs. + */ +@OptIn(ExperimentalComposeUiApi::class) +@Composable +internal fun StepOut( + open: Boolean, + pill: Rect, + side: StepSide, + accent: Color, + onDismiss: () -> Unit, + onClosed: () -> Unit, + pillCorner: Dp? = null, + maxWidth: Dp = 300.dp, + items: Int = 4, + handle: @Composable () -> Unit, + content: @Composable StepOutScope.() -> Unit, +) { + val colors = MaterialTheme.colorScheme + val density = LocalDensity.current + val inputMode = LocalInputModeManager.current + val edge = remember { Animatable(0f) } + val bottom = remember { Animatable(0f) } + val tint = remember { Animatable(0f) } + val dim = remember { Animatable(0f) } + val pieces = remember { List(items) { Animatable(0f) } } + var grown by remember { mutableStateOf(false) } + var measured by remember { mutableStateOf<IntSize?>(null) } + val first = remember { FocusRequester() } + val pull = with(density) { OutPull.toPx() } + val corner = pillCorner?.let { with(density) { it.toPx() } } ?: minOf(pill.height, pill.width) / 2f + + BoxWithConstraints(Modifier.fillMaxSize()) { + val hostWidth = constraints.maxWidth.toFloat() + val room = if (side == StepSide.Left) pill.right else hostWidth - pill.left + val maxW = with(density) { maxWidth.toPx() }.coerceAtMost(room).coerceAtLeast(pill.width) + LaunchedEffect(open, measured) { + // Asked to close before it ever measured: nothing to fold. + if (!open && !grown) { onClosed(); return@LaunchedEffect } + val c = measured ?: return@LaunchedEffect + val rest = if (side == StepSide.Left) pill.left else pill.right + val out = if (side == StepSide.Left) (pill.right - c.width).coerceAtLeast(0f) else (pill.left + c.width).coerceAtMost(hostWidth) + val boxBottom = pill.bottom + pull + c.height + if (open) { + if (!grown) { + edge.snapTo(rest); bottom.snapTo(pill.bottom); tint.snapTo(0f) + pieces.forEach { it.snapTo(0f) } + grown = true + } + coroutineScope { + launch { dim.animateTo(1f, Motion.tw(260)) } + launch { tint.animateTo(1f, Motion.tw(200)) } + launch { bottom.animateTo(boxBottom, Motion.sp(0.7f, 380f)) } + launch { delay(Motion.ms(110).toLong()); edge.animateTo(out, Motion.sp(0.55f, 300f)) } + pieces.forEachIndexed { i, a -> + launch { delay(Motion.ms(260 + i * 55).toLong()); a.animateTo(1f, Motion.tw(220)) } + } + if (inputMode.inputMode == InputMode.Keyboard) launch { + delay(Motion.ms(300).toLong()) + withFrameNanos { } + runCatching { first.requestFocus() } + } + } + } else { + coroutineScope { + pieces.forEach { launch { it.animateTo(0f, Motion.tw(90)) } } + launch { delay(Motion.ms(60).toLong()); dim.animateTo(0f, Motion.tw(300)) } + launch { delay(Motion.ms(60).toLong()); edge.animateTo(rest, Motion.sp(1f, 420f)) } + launch { delay(Motion.ms(160).toLong()); bottom.animateTo(pill.bottom, Motion.sp(1f, 380f)) } + launch { delay(Motion.ms(200).toLong()); tint.animateTo(0f, Motion.tw(200)) } + } + grown = false + onClosed() + } + } + + // Opened by touch and then answered with a pad: the pad starts in the box, not behind it. + LaunchedEffect(open, grown, inputMode.inputMode) { + if (open && grown && inputMode.inputMode == InputMode.Keyboard) { withFrameNanos { }; runCatching { first.requestFocus() } } + } + // The rest of the screen dims behind it, and a tap there folds it. Never a pad's stop. + Box( + Modifier.fillMaxSize().graphicsLayer { alpha = dim.value }.background(Color(0x59000000)) + .focusProperties { canFocus = false } + .clickable(interactionSource = remember { MutableInteractionSource() }, indication = null, enabled = open, onClick = onDismiss), + ) + val path = remember { Path() } + val boxCorner = with(density) { OutBoxCorner.toPx() } + val fillet = with(density) { OutFillet.toPx() } + val rim = with(density) { 1.4.dp.toPx() } + val restFill = colors.surface + val hotFill = lerp(colors.surface, accent, 0.22f) + Canvas(Modifier.fillMaxSize()) { + if (!grown) return@Canvas + if (side == StepSide.Left) stepPath(path, pill, edge.value, bottom.value, pill.bottom + pull, boxCorner, fillet, corner) + else stepPathRight(path, pill, edge.value, bottom.value, pill.bottom + pull, boxCorner, fillet, corner) + drawPath(path, lerp(hotFill, restFill, tint.value)) + drawPath(path, accent.copy(alpha = lerpF(0.9f, 0.55f, tint.value)), style = Stroke(rim)) + } + // The control's label rides on top, as the pull's handle. + if (grown) Box( + contentAlignment = Alignment.Center, + modifier = Modifier.offset { IntOffset(pill.left.roundToInt(), pill.top.roundToInt()) } + .size(with(density) { pill.width.toDp() }, with(density) { pill.height.toDp() }), + ) { handle() } + val x = if (side == StepSide.Left) pill.right - (measured?.width?.toFloat() ?: maxW) else pill.left + Column( + modifier = Modifier + .offset { IntOffset(x.roundToInt(), (pill.bottom + pull).roundToInt()) } + .widthIn(min = with(density) { pill.width.toDp() }, max = with(density) { maxW.toDp() }) + .onSizeChanged { if (measured != it) measured = it } + .controllerBack(onDismiss) + // A pad stays in the box until it is answered or folded, as it did in Stop's dialog. + .focusProperties { exit = { FocusRequester.Cancel } } + .focusGroup() + .padding(start = 14.dp, end = 14.dp, top = 12.dp, bottom = 12.dp), + ) { StepOutScope(pieces, first, open).content() } + } +} + +/** + * One choice in a [StepOut]: a capsule that fills when focused, like Stop's Cancel and Stop. [danger] + * draws it in red; [icon] and [trailing] make it a row (a place to install to and its free space). + */ +@Composable +internal fun StepChoice( + label: String, + modifier: Modifier = Modifier, + danger: Boolean = false, + icon: ImageVector? = null, + trailing: String? = null, + selected: Boolean = false, + enabled: Boolean = true, + onClick: () -> Unit, +) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + val shape = RoundedCornerShape(20.dp) + val accent = if (danger) colors.error else pal.signal + Row( + verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp), + modifier = modifier.heightIn(min = 40.dp).clip(shape) + .background(if (hot || selected) accent.copy(alpha = if (danger) 0.18f else 0.14f) else Color.Transparent) + .glideBorder(hot, shape, accent, if (selected) accent.copy(alpha = 0.5f) else Color.Transparent) + .hoverable(src) + .clickable(interactionSource = src, indication = LocalIndication.current, enabled = enabled, role = Role.Button, onClick = onClick) + .controllerConfirm(enabled = enabled, onClick = onClick) + .padding(horizontal = 16.dp), + ) { + if (icon != null) Icon(icon, null, tint = if (hot) accent else colors.onBackground, modifier = Modifier.size(20.dp)) + Text( + label, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = if (danger) colors.error else colors.onBackground, + maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = if (trailing != null) Modifier.weight(1f) else Modifier, + ) + if (trailing != null) Text(trailing, fontSize = 12.sp, color = colors.onSurfaceVariant, maxLines = 1) + } +} + +/** A [StepOut]'s heading line, the first piece to fade in. */ +@Composable +internal fun StepOutScope.StepTitle(text: String) { + Text( + text, fontSize = 15.sp, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, + modifier = Modifier.stepItem(0).padding(start = 4.dp, bottom = 10.dp), + ) +} + +/** The choices of a [StepOut] stacked, each the box's width. */ +@Composable +internal fun StepList(content: @Composable ColumnScope.() -> Unit) { + Column(verticalArrangement = Arrangement.spacedBy(6.dp), content = content) +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt index 2e28a258f7..a8f823c0ac 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresDetail.kt @@ -1,5 +1,7 @@ package com.droiddeck.launcher.ui +import androidx.compose.ui.layout.boundsInRoot +import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -94,11 +96,11 @@ internal fun StoreGameDetail(store: Store, key: String, s: FrontEndState, a: Fro ConfirmButton(stringResource(R.string.stores_dl_cancel), stringResource(R.string.stores_dl_cancel_confirm)) { com.droiddeck.launcher.stores.download.DownloadQueue.cancel(ctx, download.key) } } item?.owned == true && StoresState.isUnfinished(item) -> { - PrimaryButton(stringResource(R.string.stores_resume_install), main = true) { StoresState.requestInstall(ctx, 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) } } - item?.owned == true -> PrimaryButton( - if (item.sizeBytes > 0) stringResource(R.string.stores_install_size, formatBytes(item.sizeBytes)) else stringResource(R.string.stores_install), main = true, + item?.owned == true -> InstallButton( + if (item.sizeBytes > 0) stringResource(R.string.stores_install_size, formatBytes(item.sizeBytes)) else stringResource(R.string.stores_install), ) { StoresState.requestInstall(ctx, item) } item != null && item.isFree -> PrimaryButton(stringResource(R.string.stores_get_free), main = true) { openStoreUrl(ctx, item) } item != null -> PrimaryButton(if (item.hasPrice && item.finalPrice.isNotBlank()) stringResource(R.string.stores_buy_price, item.finalPrice) else stringResource(R.string.stores_view_on, store.shortLabel), main = true) { openStoreUrl(ctx, item) } @@ -116,3 +118,18 @@ internal fun StoreGameDetail(store: Store, key: String, s: FrontEndState, a: Fro Text(description, fontSize = 13.sp, lineHeight = 19.sp, color = colors.onSurfaceVariant, maxLines = 6, overflow = TextOverflow.Ellipsis, modifier = Modifier.padding(top = 12.dp)) } } + +/** + * Install (or Resume install) that says where it sits when pressed: where to install grows out of + * it, and the download it starts leaves it as a dot for the Downloads chip. + */ +@Composable +private fun InstallButton(label: String, onInstall: () -> Unit) { + val placed = remember { arrayOfNulls<androidx.compose.ui.layout.LayoutCoordinates>(1) } + Box(Modifier.onGloballyPositioned { placed[0] = it }) { + PrimaryButton(label, main = true) { + StoresMotion.markInstall(placed[0]?.takeIf { it.isAttached }?.boundsInRoot(), label) + onInstall() + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt index 502691b3e0..d55b828058 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt @@ -1,11 +1,40 @@ package com.droiddeck.launcher.ui +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.LinearEasing +import androidx.compose.animation.core.RepeatMode +import androidx.compose.animation.core.animateFloat +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.animation.core.infiniteRepeatable +import androidx.compose.animation.core.rememberInfiniteTransition +import androidx.compose.animation.expandHorizontally +import androidx.compose.animation.expandVertically +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.scaleOut +import androidx.compose.animation.shrinkHorizontally +import androidx.compose.animation.shrinkVertically +import androidx.compose.foundation.Canvas +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.composed +import androidx.compose.ui.draw.drawWithContent +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.geometry.CornerRadius +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.TransformOrigin +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.layout.boundsInRoot +import androidx.compose.ui.layout.onGloballyPositioned +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import androidx.compose.ui.draw.alpha import androidx.compose.foundation.layout.size import androidx.compose.foundation.hoverable import androidx.compose.foundation.clickable -import androidx.compose.material.icons.automirrored.outlined.Undo -import androidx.compose.material.icons.outlined.Delete import androidx.compose.material.icons.outlined.Close import androidx.compose.material.icons.filled.Refresh import androidx.compose.material.icons.filled.Pause @@ -77,37 +106,81 @@ internal fun StoresDownloadsPane(s: FrontEndState, a: FrontEndActions) { @Composable private fun DownloadList(s: FrontEndState, a: FrontEndActions) { val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val scope = rememberCoroutineScope() val entries = StoresState.downloads - if (entries.isEmpty()) { - Box(Modifier.fillMaxWidth().padding(vertical = 40.dp), contentAlignment = Alignment.Center) { - Text(stringResource(R.string.stores_downloads_empty), fontSize = 13.sp, color = colors.onSurfaceVariant) - } - return - } + // Clearing: the finished rows go into the divider's X one by one, bottom first, and only then + // leave the queue. + var clearing by remember { mutableStateOf(false) } // Under way on top; finished below a line whose X clears them all (rows only - a failed // download's kept files stay until its game page's Clear). val (active, finished) = entries.partition { it.isActive } + val finishedShown = if (clearing) emptyList() else finished Column(verticalArrangement = Arrangement.spacedBy(10.dp)) { - // The list changes only when rows come, go or change state; each row reads its own progress. - active.forEachIndexed { i, row -> key(row.key) { Rise(1 + i.coerceAtMost(5)) { DownloadCard(StoresState.download(row.key) ?: row, s, a) } } } - if (finished.isNotEmpty()) { - Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth().padding(top = if (active.isEmpty()) 0.dp else 4.dp)) { - Box(Modifier.weight(1f).height(1.dp).background(LocalPalette.current.line)) - RoundAction(Icons.Outlined.Close, stringResource(R.string.stores_dl_clear_all), size = 30.dp) { DownloadQueue.dismissFinished() } + AnimatedVisibility(entries.isEmpty(), enter = fadeIn(Motion.tw(300, 300)), exit = fadeOut(Motion.tw(120))) { + Box(Modifier.fillMaxWidth().padding(vertical = 40.dp), contentAlignment = Alignment.Center) { + Text(stringResource(R.string.stores_downloads_empty), fontSize = 13.sp, color = colors.onSurfaceVariant) } - finished.forEach { row -> - key(row.key) { - val d = StoresState.download(row.key) ?: row - if (d.state == DownloadState.FAILED) DownloadCard(d, s, a) else FinishedRow(d, s, a) + } + // The list changes only when rows come, go or change state; each row reads its own progress. + // A row pours out of the count's side (the right), and leaves the way it came; one that + // installed stays a beat for its bar to gather into Play first. + AnimatedRows( + active, key = { it.key }, + enter = { i, later -> + val wait = if (later) 0 else 90 * i.coerceAtMost(4) + expandHorizontally(Motion.tw(360, wait), expandFrom = Alignment.End) + expandVertically(Motion.tw(320, wait), expandFrom = Alignment.Top) + + fadeIn(Motion.tw(300, wait + 60)) + }, + exit = { row, _, _ -> + if (StoresState.download(row.key)?.state == DownloadState.INSTALLED) + fadeOut(Motion.tw(220, GATHER_HOLD_MS)) + shrinkVertically(Motion.tw(300, GATHER_HOLD_MS)) + else shrinkHorizontally(Motion.tw(260), shrinkTowards = Alignment.End) + shrinkVertically(Motion.tw(300, 120)) + fadeOut(Motion.tw(200, 80)) + }, + ) { row, _ -> DownloadCard(StoresState.download(row.key) ?: row, s, a) } + AnimatedVisibility( + finishedShown.isNotEmpty(), + enter = expandVertically(Motion.tw(260)) + fadeIn(Motion.tw(220)), + exit = fadeOut(Motion.tw(200, 60 * finished.size.coerceAtMost(8) + 160)) + shrinkVertically(Motion.tw(220, 60 * finished.size.coerceAtMost(8) + 200)), + ) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp), modifier = Modifier.fillMaxWidth().padding(top = if (active.isEmpty()) 0.dp else 4.dp)) { + Box(Modifier.weight(1f).height(1.dp).background(pal.line)) + RoundAction(Icons.Outlined.Close, stringResource(R.string.stores_dl_clear_all), size = 30.dp) { + if (!clearing) { + val n = finished.size.coerceAtMost(8) + clearing = true + scope.launch { + delay(Motion.ms(60 * n + 420).toLong()) + DownloadQueue.dismissFinished() + clearing = false + } + } } } } + AnimatedRows( + finishedShown, key = { it.key }, + // One that just finished joins after its row above has gathered; the rest are simply there. + enter = { i, later -> if (later) expandVertically(Motion.tw(300, GATHER_HOLD_MS)) + fadeIn(Motion.tw(220, GATHER_HOLD_MS + 50)) else fadeIn(Motion.tw(300, 60 * i.coerceAtMost(5))) }, + // Into the X at the top right, bottom row first. + exit = { _, i, n -> + val wait = 60 * (n - 1 - i).coerceIn(0, 8) + scaleOut(Motion.tw(220, wait), targetScale = 0.2f, transformOrigin = TransformOrigin(1f, 0f)) + + fadeOut(Motion.tw(200, wait + 40)) + shrinkVertically(Motion.tw(240, wait + 80)) + }, + ) { row, later -> + val d = StoresState.download(row.key) ?: row + if (d.state == DownloadState.FAILED) DownloadCard(d, s, a) else FinishedRow(d, s, a, bloom = later) + } } } -/** A finished download: its name and store, and Play when it installed. */ +/** How long a row that installed holds for its bar to gather into Play before it moves under the line. */ +private const val GATHER_HOLD_MS = 900 + +/** A finished download: its name and store, and Play when it installed - blooming out of a dot when it just did. */ @Composable -private fun FinishedRow(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) { +private fun FinishedRow(d: DownloadEntry, s: FrontEndState, a: FrontEndActions, bloom: Boolean = false) { val ctx = LocalContext.current val colors = MaterialTheme.colorScheme Row( @@ -117,10 +190,24 @@ private fun FinishedRow(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) Text(d.name, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f, fill = false)) SourceChip(d.store.id, small = true) Text(stringResource(if (d.state == DownloadState.INSTALLED) R.string.stores_installed_chip else R.string.stores_dl_cancelled), fontSize = 11.sp, color = colors.onSurfaceVariant, maxLines = 1, modifier = Modifier.weight(1f)) - if (d.state == DownloadState.INSTALLED) RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_play), primary = true) { launchStoreGame(ctx, d.store, d.id, s, a) } + if (d.state == DownloadState.INSTALLED) Box(Modifier.bloomIn(bloom, GATHER_HOLD_MS + 150)) { + RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_play), primary = true) { launchStoreGame(ctx, d.store, d.id, s, a) } + } } } +/** Grows out of a dot (sp 0.5 / 300) after [waitMs], when [on]; otherwise just there. */ +private fun Modifier.bloomIn(on: Boolean, waitMs: Int): Modifier = composed { + val k = remember { Animatable(if (on && Motion.scale > 0f) 0f else 1f) } + LaunchedEffect(Unit) { + if (k.value < 1f) { + delay(Motion.ms(waitMs).toLong()) + k.animateTo(1f, Motion.sp(0.5f, 300f)) + } + } + graphicsLayer { scaleX = k.value; scaleY = k.value; alpha = k.value.coerceIn(0f, 1f) } +} + /** * A row's action as a round icon button - the label is its description - sized like the old * compact buttons and focusable by pad. [primary] fills it with the accent, [danger] in red. @@ -146,18 +233,62 @@ internal fun RoundAction( ) { androidx.compose.material3.Icon(icon, description, tint = tint, modifier = Modifier.size(size * 0.5f)) } } -/** Cancel in two taps: the stop icon, then a red delete and a keep that undoes it (back by itself after a few seconds). */ +/** + * Cancel as End session's confirm: the X drops a pull and Keep and Delete step out to its left, + * in red. Keep's rim runs out the four seconds after which it folds back by itself. + */ @Composable private fun CancelAction(onDelete: () -> Unit) { - var armed by remember { mutableStateOf(false) } - androidx.compose.runtime.LaunchedEffect(armed) { if (armed) { kotlinx.coroutines.delay(4000); armed = false } } - if (!armed) RoundAction(Icons.Outlined.Close, stringResource(R.string.stores_dl_cancel)) { armed = true } - else { - RoundAction(Icons.Outlined.Delete, stringResource(R.string.stores_dl_cancel_confirm), danger = true) { armed = false; onDelete() } - RoundAction(Icons.AutoMirrored.Outlined.Undo, stringResource(R.string.stores_notification_keep)) { armed = false } + val colors = MaterialTheme.colorScheme + val placed = remember { arrayOfNulls<androidx.compose.ui.layout.LayoutCoordinates>(1) } + val keep = stringResource(R.string.stores_notification_keep) + val delete = stringResource(R.string.stores_dl_cancel_confirm) + Box(Modifier.onGloballyPositioned { placed[0] = it }) { + RoundAction(Icons.Outlined.Close, stringResource(R.string.stores_dl_cancel)) { + val at = placed[0]?.takeIf { it.isAttached }?.boundsInRoot() ?: return@RoundAction + StoresMotion.ask(StepAsk( + anchor = at, side = StepSide.Left, accent = colors.error, items = 2, + handle = { androidx.compose.material3.Icon(Icons.Outlined.Close, null, tint = colors.error, modifier = Modifier.size(18.dp)) }, + ) { + val left = remember { Animatable(1f) } + LaunchedEffect(Unit) { + // Wall-clock seconds, as before; the rim follows them when animations run. + if (Motion.scale > 0f) launch { left.animateTo(0f, androidx.compose.animation.core.tween((CANCEL_HOLD_MS / Motion.scale).toInt(), easing = LinearEasing)) } + delay(CANCEL_HOLD_MS.toLong()) + StoresMotion.fold() + } + Row(horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End)) { + StepChoice(keep, enabled = open, modifier = Modifier.stepItem(0).focusRequester(first).timeRim({ left.value }, colors.onBackground)) { StoresMotion.fold() } + StepChoice(delete, danger = true, enabled = open, modifier = Modifier.stepItem(1)) { StoresMotion.fold(); onDelete() } + } + }) + } } } +private const val CANCEL_HOLD_MS = 4000 + +/** A capsule's outline drawn for [left] (1 to 0) of its length, clockwise from the top centre: time running out. */ +private fun Modifier.timeRim(left: () -> Float, color: Color): Modifier = drawWithContent { + drawContent() + val v = left() + if (v <= 0f) return@drawWithContent + val w = 1.5.dp.toPx() + val r = size.height / 2f + val outline = androidx.compose.ui.graphics.Path().apply { + moveTo(size.width / 2f, w / 2f) + lineTo(size.width - r, w / 2f) + arcTo(Rect(size.width - 2 * r + w / 2f, w / 2f, size.width - w / 2f, size.height - w / 2f), -90f, 180f, false) + lineTo(r, size.height - w / 2f) + arcTo(Rect(w / 2f, w / 2f, 2 * r - w / 2f, size.height - w / 2f), 90f, 180f, false) + lineTo(size.width / 2f, w / 2f) + } + val measure = androidx.compose.ui.graphics.PathMeasure().apply { setPath(outline, false) } + val part = androidx.compose.ui.graphics.Path() + measure.getSegment(0f, measure.length * v, part, true) + drawPath(part, color.copy(alpha = 0.8f), style = androidx.compose.ui.graphics.drawscope.Stroke(w)) +} + @Composable private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) { val ctx = LocalContext.current @@ -216,7 +347,10 @@ private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) 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.INSTALLED -> RoundAction(Icons.Filled.PlayArrow, stringResource(R.string.stores_play), primary = true) { launchStoreGame(ctx, d.store, d.id, s, a) } + // Just installed (this row is on its way under the line): Play blooms where the bar gathered. + 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) } + } DownloadState.CANCELLED -> {} } } @@ -227,29 +361,87 @@ private fun DownloadCard(d: DownloadEntry, s: FrontEndState, a: FrontEndActions) /** * One segment per stage - Manifest, Download, Verify, Install - equal widths: a passed or skipped * stage full, the active one filling with its own progress (a sliver while it has nothing to count), - * the rest empty. + * the rest empty. Fills ride a spring; a stage that completes swells and settles (the bloop); a + * paused one breathes; and once the download has installed the four gather into one green line + * that draws up into a dot, where Play blooms. */ @Composable private fun StageBar(d: DownloadEntry) { + val pal = LocalPalette.current + val colors = MaterialTheme.colorScheme val stages = listOf(DownloadStage.MANIFEST, DownloadStage.DOWNLOAD, DownloadStage.VERIFY, DownloadStage.INSTALL) - Row(horizontalArrangement = Arrangement.spacedBy(3.dp), modifier = Modifier.fillMaxWidth()) { - for (st in stages) { - val active = d.state != DownloadState.INSTALLED && d.stage == st - val fill = when { - active -> d.stageFraction.let { if (it < 0f) 0.04f else it } - // A stage a store skips or folds into another (Epic checks chunks while it fetches, - // Amazon writes while it downloads) reads complete once a later one has started. - d.passed(st) || st.ordinal < d.stage.ordinal || d.stage == DownloadStage.DONE -> 1f - else -> 0f - } - Box(Modifier.weight(1f)) { - ProgressBarThin( - fill, paused = active && d.state == DownloadState.PAUSED, verify = active && st == DownloadStage.VERIFY, - height = 6.dp, done = !active && fill >= 1f, - ) - } + val installed = d.state == DownloadState.INSTALLED + val actives = stages.map { st -> !installed && d.stage == st } + val fills = stages.mapIndexed { i, st -> + when { + actives[i] -> d.stageFraction.let { if (it < 0f) 0.04f else it } + // A stage a store skips or folds into another (Epic checks chunks while it fetches, + // Amazon writes while it downloads) reads complete once a later one has started. + d.passed(st) || st.ordinal < d.stage.ordinal || d.stage == DownloadStage.DONE -> 1f + else -> 0f } } + val done = fills.mapIndexed { i, f -> !actives[i] && f >= 1f } + val shown = fills.mapIndexed { i, f -> animateFloatAsState(f, Motion.sp(0.8f, 300f), label = "stage$i") } + val bloops = remember { List(4) { Animatable(1f) } } + val wasDone = remember { done.toBooleanArray() } + done.forEachIndexed { i, dn -> + LaunchedEffect(i, dn) { + if (dn && !wasDone[i] && Motion.scale > 0f) { + bloops[i].animateTo(1.6f, Motion.tw(90)) + bloops[i].animateTo(1f, Motion.sp(0.45f, 600f)) + } + wasDone[i] = dn + } + } + val gather = remember { Animatable(0f) } + LaunchedEffect(installed) { if (installed) gather.animateTo(1f, Motion.tw(520, easing = androidx.compose.animation.core.CubicBezierEasing(0.6f, 0f, 0.15f, 1f))) } + val paused = d.state == DownloadState.PAUSED + val breath = breathing(paused) + val verifyColor = Color(0xFF9B6DFF) + val pausedColor = Color(0xFFFFC24D) + Canvas(Modifier.fillMaxWidth().height(10.dp)) { + val h = 6.dp.toPx() + val cy = size.height / 2f + val g = gather.value + if (g < 0.5f) { + // The segments: the gaps close and every fill turns done-green as the gather starts. + val join = (g * 2f).coerceIn(0f, 1f) + val gap = 3.dp.toPx() * (1f - join) + val segW = (size.width - gap * 3f) / 4f + for (i in 0 until 4) { + val x = i * (segW + gap) + drawRoundRect(colors.surfaceVariant, Offset(x, cy - h / 2f), Size(segW, h), CornerRadius(h / 2f)) + val base = when { + done[i] -> pal.good + actives[i] && paused -> pausedColor.copy(alpha = breath) + actives[i] && stages[i] == DownloadStage.VERIFY -> verifyColor + else -> pal.signal + } + val fill = androidx.compose.ui.util.lerp(shown[i].value.coerceIn(0f, 1f), 1f, join) + val hh = h * bloops[i].value + if (fill > 0f) drawRoundRect( + androidx.compose.ui.graphics.lerp(base, pal.good, join), Offset(x, cy - hh / 2f), Size(segW * fill, hh), CornerRadius(hh / 2f), + ) + } + } else { + // One line drawing up into a dot at its right end. + val p = ((g - 0.5f) * 2f).coerceIn(0f, 1f) + val dot = 10.dp.toPx() + val left = androidx.compose.ui.util.lerp(0f, size.width - dot, p) + val hh = androidx.compose.ui.util.lerp(h, dot, p) + drawRoundRect(pal.good, Offset(left, cy - hh / 2f), Size(size.width - left, hh), CornerRadius(hh / 2f), alpha = 1f - p * 0.4f) + } + } +} + +/** 1, or while [on] (and animations run), an alpha going 1 → 0.5 → 1 over the rail's live-dot period. */ +@Composable +private fun breathing(on: Boolean): Float { + if (!on || Motion.scale == 0f) return 1f + val t = rememberInfiniteTransition(label = "breath") + val v by t.animateFloat(1f, 0.5f, infiniteRepeatable(androidx.compose.animation.core.tween(800), RepeatMode.Reverse), label = "breathe") + return v } private fun eta(seconds: Long): String = when { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresMotion.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresMotion.kt new file mode 100644 index 0000000000..3072b26115 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresMotion.kt @@ -0,0 +1,390 @@ +package com.droiddeck.launcher.ui + +import android.os.SystemClock +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.EnterTransition +import androidx.compose.animation.ExitTransition +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.FastOutSlowInEasing +import androidx.compose.animation.core.MutableTransitionState +import androidx.compose.animation.core.spring +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.SideEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.key +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.runtime.snapshotFlow +import androidx.compose.ui.Modifier +import androidx.compose.ui.composed +import androidx.compose.ui.draw.drawBehind +import androidx.compose.ui.geometry.CornerRadius +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.drawscope.DrawScope +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.drawscope.rotate +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.graphics.lerp +import androidx.compose.ui.layout.onSizeChanged +import androidx.compose.ui.unit.dp +import androidx.compose.ui.util.lerp +import com.droiddeck.launcher.stores.Store +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import kotlin.math.PI +import kotlin.math.atan2 +import kotlin.math.max +import kotlin.math.sin + +// The Stores section's moves, in the launcher's own vocabulary: an install bloops into a dot that +// flies to the Downloads chip, sign-in floods in the store's colour and comes home to the chip's +// dot, and the confirms step out of the control that asked (StepOut). Everything here is in root +// coordinates; the Stores page draws it in its own space. + +internal object StoresMotion { + /** The Install (or Resume install) button last pressed, its label and when: a download that starts soon after flies from it. */ + var installFrom: Rect? = null + private set + var installLabel: String = "" + private set + private var installAt = 0L + + fun markInstall(bounds: Rect?, label: String) { + installFrom = bounds + installLabel = label + installAt = SystemClock.uptimeMillis() + } + + /** The button just pressed, if it was pressed in the last few seconds; not consumed. */ + fun recentInstall(): Rect? = installFrom.takeIf { SystemClock.uptimeMillis() - installAt < 5_000 } + + /** The button a download that just started came from, once; stale after 20 s (a slow manifest). */ + fun takeInstall(): Rect? = installFrom.takeIf { SystemClock.uptimeMillis() - installAt < 20_000 }.also { installAt = 0L } + + /** Where the Downloads chip sits, for a download's dot to land in. */ + var downloadsChip by mutableStateOf<Rect?>(null) + + /** Bumped each time a dot lands: the chip's count and the rail's badge pop on it. */ + var landed by mutableIntStateOf(0) + + /** Dots in flight to the Downloads chip. */ + val flights = mutableStateListOf<Flight>() + + fun fly(from: Rect) { + if (Motion.scale == 0f || downloadsChip == null) { landed++; return } + flights += Flight(from, SystemClock.uptimeMillis()) + } + + /** The confirm or list stepping out of a control on the Stores page now, and whether it is open. */ + var step by mutableStateOf<StepAsk?>(null) + private set + var stepOpen by mutableStateOf(false) + private set + + fun ask(a: StepAsk) { + step?.onDismiss() + step = a + stepOpen = true + } + + /** Drops whatever is stepped out or flooding, at once: the page it belonged to has gone. */ + fun drop() { + step?.let { step = null; it.onDismiss() } + stepOpen = false + signIn = null + } + + /** Folds the current one back into its control. */ + fun fold() { stepOpen = false } + + internal fun closed(a: StepAsk) { + if (step === a) step = null + a.onDismiss() + } + + /** A store's sign-in flooding the page, or draining back out of it. */ + var signIn by mutableStateOf<SignInFlood?>(null) + + /** Per store, bumped to send its chip's dot one pulse ring (signed in, picked). */ + val pulses = androidx.compose.runtime.mutableStateMapOf<Store, Int>() + + fun pulse(store: Store) { pulses[store] = (pulses[store] ?: 0) + 1 } + + /** Where each store's dot sits on its chip, for a sign-in to come home to. */ + val dots = HashMap<Store, Rect>() + + /** The card a game page is about to open from. */ + private var card: CardMark? = null + private var cardAt = 0L + + fun markCard(c: CardMark) { + card = c + cardAt = SystemClock.uptimeMillis() + } + + /** The card game [key] was just opened from, once; a page opened another way shows without a flood. */ + fun takeCard(key: String): CardMark? = card?.takeIf { it.key == key && SystemClock.uptimeMillis() - cardAt < 1_000 }.also { card = null } +} + +/** A game card at [bounds] (root px) with corner [corner] (px) and its art, for the page it opens to flood out of. */ +internal class CardMark(val key: String, val bounds: Rect, val corner: Float, val art: String?) + +internal class Flight(val from: Rect, val id: Long) + +/** + * A confirm or short list for [StepOut] on the Stores page, asked by a control that sits at [anchor] + * (root px). [content] gets a close that folds it; [onDismiss] runs once it has folded, however. + */ +internal class StepAsk( + val anchor: Rect, + val side: StepSide, + val accent: Color, + val pillCorner: androidx.compose.ui.unit.Dp? = null, + val items: Int = 4, + val onDismiss: () -> Unit = {}, + val handle: @Composable () -> Unit, + val content: @Composable StepOutScope.() -> Unit, +) + +internal class SignInFlood(val store: Store, val from: Rect, val start: Color, val color: Color) { + /** Covered and handed to the login page; waiting for the page to come back. */ + var away by mutableStateOf(false) + /** Back from the login page: draining into [to]. */ + var drainTo by mutableStateOf<Rect?>(null) +} + +/** The dots flying to the Downloads chip, drawn over the page whose root offset is [origin]. */ +@Composable +internal fun FlightLayer(origin: Offset) { + for (f in StoresMotion.flights.toList()) key(f.id) { FlightDot(f, origin) } +} + +/** + * One install's dot: the button squashes into a 14dp dot (width first, a little bloop), which then + * flies on an arc to the Downloads chip, stretched along its path like the focus ring's drop. + */ +@Composable +private fun FlightDot(f: Flight, origin: Offset) { + val pal = LocalPalette.current + val t = remember { Animatable(0f) } + LaunchedEffect(Unit) { + t.animateTo(1f, Motion.tw(150, easing = FastOutSlowInEasing)) + t.animateTo(2f, Motion.tw(300, easing = FastOutSlowInEasing)) + StoresMotion.landed++ + StoresMotion.flights.remove(f) + } + Canvas(Modifier.fillMaxSize()) { + val r = 7.dp.toPx() + val from = f.from.translate(-origin) + val chip = (StoresMotion.downloadsChip ?: return@Canvas).translate(-origin) + val a = from.center + // The count sits at the chip's right end. + val b = Offset(chip.right - 22.dp.toPx(), chip.center.y) + val v = t.value + if (v < 1f) { + // Squash: the width goes first, then the height, into the dot; a bloop at the end. + val w = lerp(from.width, 2 * r, (v * 1.4f).coerceAtMost(1f)) + val h = lerp(from.height, 2 * r, ((v - 0.25f) / 0.75f).coerceIn(0f, 1f)) + val k = 1f - 0.15f * sin(PI * v).toFloat() + drawRoundRect(pal.signal, Offset(a.x - w * k / 2, a.y - h * k / 2), Size(w * k, h * k), CornerRadius(minOf(w, h) * k / 2)) + } else { + val p = v - 1f + val c = Offset((a.x + b.x) / 2, minOf(a.y, b.y) - 80.dp.toPx()) + val q = 1f - p + val pos = Offset(q * q * a.x + 2 * q * p * c.x + p * p * b.x, q * q * a.y + 2 * q * p * c.y + p * p * b.y) + val d = Offset(2 * q * (c.x - a.x) + 2 * p * (b.x - c.x), 2 * q * (c.y - a.y) + 2 * p * (b.y - c.y)) + val stretch = sin(PI * p).toFloat() + val angle = atan2(d.y, d.x) * 180f / PI.toFloat() + rotate(angle, pos) { + val w = 2 * r * (1f + 0.5f * stretch) + val h = 2 * r * (1f - 0.25f * stretch) + drawOval(pal.signal, Offset(pos.x - w / 2, pos.y - h / 2), Size(w, h)) + } + } + } +} + +/** Pops (1.3 to 1, with one ring) each time a download's dot lands; for the count on the chip and the rail. */ +internal fun Modifier.landingPop(): Modifier = composed { + val pal = LocalPalette.current + val scale = remember { Animatable(1f) } + val ring = remember { Animatable(1f) } + val seen = remember { intArrayOf(StoresMotion.landed) } + LaunchedEffect(StoresMotion.landed) { + if (StoresMotion.landed == seen[0]) return@LaunchedEffect + seen[0] = StoresMotion.landed + coroutineScope { + launch { scale.snapTo(1.3f); scale.animateTo(1f, Motion.sp(0.45f, 500f)) } + launch { ring.snapTo(0f); ring.animateTo(1f, Motion.tw(400)) } + } + } + this.drawBehind { + if (ring.value < 1f) { + val rr = max(size.width, size.height) / 2f * lerp(0.4f, 1.6f, ring.value) + drawCircle(pal.signal.copy(alpha = 0.5f * (1f - ring.value)), rr, center, style = Stroke(1.5.dp.toPx())) + } + }.graphicsLayer { scaleX = scale.value; scaleY = scale.value } +} + +/** + * A pressed button in [start] stretching out over this element in [color] (LaunchFlood's move: + * each edge on its own loose spring, the furthest first, blobby in flight). [from] is in this + * element's px. Calls [onCovered] once, when nothing but [color] shows; then holds. + */ +@Composable +internal fun ColorFlood(from: Rect, start: Color, color: Color, onCovered: () -> Unit) { + val edges = remember { List(4) { Animatable(0f) } } + val reached = remember { BooleanArray(4) } + var size by remember { mutableStateOf(Size.Zero) } + LaunchedEffect(size != Size.Zero) { + if (size == Size.Zero) return@LaunchedEffect + val travel = listOf(from.left, from.top, size.width - from.right, size.height - from.bottom).map { it.coerceAtLeast(0f) } + val far = travel.max().coerceAtLeast(1f) + coroutineScope { + edges.forEachIndexed { i, edge -> + val lead = travel[i] / far + launch { + delay(Motion.ms((120 * (1f - lead)).toInt()).toLong()) + // Overshoots past the edge, off the element, so the wobble is felt in the pull. + edge.animateTo(1f, if (Motion.scale == 0f) Motion.sp() else spring(dampingRatio = 0.48f, stiffness = lerp(110f, 190f, lead))) { + if (value >= 1f) reached[i] = true + } + reached[i] = true + } + } + snapshotFlow { edges.indices.all { reached[it] || edges[it].value >= 1f } }.first { it } + onCovered() + } + } + Canvas(Modifier.fillMaxSize().onSizeChangedCompat { size = it }) { + val v = edges.mapIndexed { i, e -> if (reached[i]) 1f else e.value } + drawFloodShape(from, from.minDimension / 2f, v, lerp(start, color, (v.average().toFloat() * 2f).coerceIn(0f, 1f))) + } +} + +/** + * [color] over the whole element drawing down into [to] (px, here), critically damped, the near + * edges first (PageFlood's way back), then fading out on it; [onDone] after. + */ +@Composable +internal fun ColorDrain(color: Color, to: Rect, toCorner: Float, onDone: () -> Unit) { + val edges = remember { List(4) { Animatable(1f) } } + val alpha = remember { Animatable(1f) } + var size by remember { mutableStateOf(Size.Zero) } + LaunchedEffect(size != Size.Zero) { + if (size == Size.Zero) return@LaunchedEffect + val travel = listOf(to.left, to.top, size.width - to.right, size.height - to.bottom).map { it.coerceAtLeast(0f) } + val far = travel.max().coerceAtLeast(1f) + coroutineScope { + edges.mapIndexed { i, edge -> + val lead = travel[i] / far + launch { + delay(Motion.ms((120 * (1f - lead)).toInt()).toLong()) + edge.animateTo(0f, if (Motion.scale == 0f) Motion.sp() else spring(dampingRatio = 1f, stiffness = lerp(170f, 260f, lead))) + } + }.forEach { it.join() } + } + alpha.animateTo(0f, Motion.tw(160)) + onDone() + } + Canvas(Modifier.fillMaxSize().onSizeChangedCompat { size = it }) { + drawFloodShape(to, toCorner, edges.map { it.value }, color.copy(alpha = color.alpha * alpha.value)) + } +} + +private fun Modifier.onSizeChangedCompat(onSize: (Size) -> Unit) = + onSizeChanged { onSize(Size(it.width.toFloat(), it.height.toFloat())) } + +/** A rect from [r] with its edges [v] of the way to this scope's (left, top, right, bottom): blobby in flight. */ +private fun DrawScope.drawFloodShape(r: Rect, restCorner: Float, v: List<Float>, color: Color) { + val left = lerp(r.left, 0f, v[0]) + val top = lerp(r.top, 0f, v[1]) + val right = lerp(r.right, size.width, v[2]) + val bottom = lerp(r.bottom, size.height, v[3]) + val w = (right - left).coerceAtLeast(0f) + val h = (bottom - top).coerceAtLeast(0f) + val mean = v.sumOf { it.coerceIn(0f, 1f).toDouble() }.toFloat() / 4f + val blob = sin(PI * mean).toFloat().coerceAtLeast(0f) + val corner = lerp(restCorner * (1f - mean), minOf(w, h) * 0.42f, blob) + drawRoundRect(color, Offset(left, top), Size(w, h), CornerRadius(corner)) +} + +/** Sends one pulse ring out of a dot each time [trigger] changes after the first composition. */ +internal fun Modifier.pulseRing(trigger: Int, color: Color): Modifier = composed { + val ring = remember { Animatable(1f) } + val seen = remember { intArrayOf(trigger) } + LaunchedEffect(trigger) { + if (trigger == seen[0]) return@LaunchedEffect + seen[0] = trigger + if (Motion.scale == 0f) return@LaunchedEffect + ring.snapTo(0f) + ring.animateTo(1f, Motion.tw(600)) + } + drawBehind { + if (ring.value < 1f) drawCircle( + color.copy(alpha = 0.6f * (1f - ring.value)), size.minDimension / 2f * lerp(0.4f, 1.6f, ring.value) * 1.4f, center, + style = Stroke(1.5.dp.toPx()), + ) + } +} + +/** + * [items] in a column where a row that comes enters with [enter] and a row that goes stays on to + * leave with [exit] (a Column has no item animations of its own). [content] gets the row and + * whether it came after the list was first shown. + */ +@Composable +internal fun <T> AnimatedRows( + items: List<T>, + key: (T) -> Any, + enter: (index: Int, later: Boolean) -> EnterTransition, + exit: (item: T, index: Int, count: Int) -> ExitTransition, + content: @Composable (item: T, later: Boolean) -> Unit, +) { + val order = remember { mutableListOf<Any>() } + val last = remember { mutableMapOf<Any, T>() } + val states = remember { mutableMapOf<Any, MutableTransitionState<Boolean>>() } + val later = remember { mutableMapOf<Any, Boolean>() } + val mounted = remember { booleanArrayOf(false) } + val now = items.associateBy(key) + // Keep a leaving row where it was among the ones that stay. + val merged = ArrayList<Any>() + var j = 0 + for (k in now.keys) { + val pos = order.indexOf(k) + if (pos >= 0) { + while (j < pos) { val o = order[j]; if (o !in now && o !in merged) merged += o; j++ } + j = maxOf(j, pos + 1) + } + merged += k + } + while (j < order.size) { val o = order[j]; if (o !in now && o !in merged) merged += o; j++ } + now.forEach { (k, v) -> last[k] = v } + val shown = merged.filter { k -> + val st = states.getOrPut(k) { MutableTransitionState(false).also { later[k] = mounted[0] } } + st.targetState = k in now + val gone = k !in now && st.isIdle && !st.currentState + if (gone) { states.remove(k); last.remove(k); later.remove(k) } + !gone + } + order.clear(); order.addAll(shown) + SideEffect { mounted[0] = true } + shown.forEachIndexed { i, k -> + val item = last[k] ?: return@forEachIndexed + val st = states[k] ?: return@forEachIndexed + key(k) { + AnimatedVisibility(st, enter = enter(i, later[k] == true), exit = exit(item, i, shown.size)) { content(item, later[k] == true) } + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt index b9e648c0d0..ec3516dbe0 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresPage.kt @@ -1,5 +1,35 @@ package com.droiddeck.launcher.ui +import androidx.compose.animation.AnimatedContent +import androidx.compose.animation.SizeTransform +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.scaleOut +import androidx.compose.animation.slideInHorizontally +import androidx.compose.animation.slideInVertically +import androidx.compose.animation.slideOutHorizontally +import androidx.compose.animation.togetherWith +import androidx.compose.animation.core.Animatable +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.icons.outlined.SdCard +import androidx.compose.material.icons.outlined.Smartphone +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.focus.focusProperties +import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.graphics.luminance +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.layout.boundsInParent +import androidx.compose.ui.layout.boundsInRoot +import androidx.compose.ui.layout.positionInRoot +import com.droiddeck.launcher.stores.StoreInstallRoot +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import androidx.compose.ui.layout.boundsInWindow import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.layout.onSizeChanged @@ -23,8 +53,10 @@ import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.collectIsPressedAsState import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.aspectRatio @@ -38,8 +70,18 @@ import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll +import androidx.compose.foundation.lazy.grid.GridCells +import androidx.compose.foundation.lazy.grid.GridItemSpan +import androidx.compose.foundation.lazy.grid.LazyGridState +import androidx.compose.foundation.lazy.grid.LazyGridScope +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid +import androidx.compose.foundation.lazy.grid.items import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Check +import androidx.compose.material.icons.filled.Search +import androidx.compose.material.icons.automirrored.filled.Login +import androidx.compose.foundation.layout.widthIn +import androidx.compose.foundation.basicMarquee import androidx.compose.material.icons.filled.PlayArrow import androidx.compose.material.icons.filled.Settings import androidx.compose.material.icons.outlined.Download @@ -48,6 +90,7 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.State import androidx.compose.runtime.getValue import androidx.compose.runtime.key import androidx.compose.runtime.mutableStateOf @@ -91,20 +134,25 @@ import com.droiddeck.launcher.stores.formatBytes /** The chip row's fourth entry, beside the three stores. */ private const val DOWNLOADS = "downloads" -private val TABS = listOf("store", "library") +private val TABS = listOf("store", "library", "installed") /** - * The tabs a store has: Store and Library (whose dropdown filters All / Installed). Amazon has no - * public catalog, so Library alone. + * The modes a store has, picked from its chip (A steps them out of it) or cycled with LB / RB: + * Store, Library and Installed. Amazon has no public catalog, so no Store. */ -internal fun tabsFor(store: Store?): List<String> = if (store == Store.AMAZON) listOf("library") else TABS +internal fun tabsFor(store: Store?): List<String> = if (store == Store.AMAZON) TABS.drop(1) else TABS -/** [tab] if [store] has it, else Library (the open-on Store choice, or an old Installed / All, falls back there). */ +/** [tab] if [store] has it, else Library (the open-on Store choice on Amazon falls back there). */ internal fun tabFor(store: Store?, tab: String): String = if (tab in tabsFor(store)) tab else "library" +/** What the pane under the chips shows: a store's tab (signed in or not) or the downloads. */ +private data class PaneKey(val chip: String, val tab: String, val signedIn: Boolean) + +@OptIn(androidx.compose.ui.ExperimentalComposeUiApi::class, androidx.compose.animation.ExperimentalAnimationApi::class) @Composable internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier) { val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme val narrow = LocalNarrowPane.current val padH = if (narrow) 14.dp else 22.dp var chip by rememberSaveable { mutableStateOf(Store.GOG.id) } @@ -116,6 +164,13 @@ internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier val store = Store.byId(chip) // A tab this store does not have (Store or All on Amazon) reads as Library. val shownTab = tabFor(store, tab) + val pane = PaneKey( + chip, if (store == null) "" else shownTab, + store == null || (StoresState.isSignedIn(store) && StoresState.expired[store] != true), + ) + // Incoming and outgoing panes must not share a scroll state while both are on screen. + val grids = remember { HashMap<PaneKey, LazyGridState>() } + val grid = grids.getOrPut(pane) { LazyGridState() } LaunchedEffect(Unit) { StoresState.refresh(ctx) } // A store the account is signed into fills itself when its chip is on screen. LaunchedEffect(chip, StoresState.accounts[store]) { if (store != null && StoresState.isSignedIn(store)) StoresState.open(ctx, store) } @@ -138,22 +193,138 @@ internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier if (id != DOWNLOADS) tab = tabFor(Store.byId(id), if (s.storesOpenTab == SessionPrefs.STORES_OPEN_STORE) "store" else "library") move("first") } + // A pad moving along the chips shows each store it stops on, without A: a beat after it + // settles, so sweeping past two chips does not open both. Focus stays on the chip. + var focusedChip by remember { mutableStateOf<String?>(null) } + LaunchedEffect(focusedChip) { + val id = focusedChip ?: return@LaunchedEffect + delay(140) + if (id != chip) { + chip = id; openGame = null; query = "" + if (id != DOWNLOADS) tab = tabFor(Store.byId(id), if (s.storesOpenTab == SessionPrefs.STORES_OPEN_STORE) "store" else "library") + } + } + // A store's chip steps its modes out of itself (End session's shape, in the store's colour); + // picking one shows that store in it. A store not signed in has none: its chip just opens it. + val pal = LocalPalette.current + fun openModes(st: Store, at: Rect?) { + // Another store's chip (a tap; a pad's focus has already shown it): that store. + if (chip != st.id) { pickChip(st.id); return } + // Not signed in: nothing to pick, so A goes on to its Sign in. + if (at == null || !StoresState.isSignedIn(st) || StoresState.expired[st] == true) { move("first"); return } + val modes = tabsFor(st) + val current = if (chip == st.id) shownTab else null + var picked = false + StoresMotion.ask(StepAsk( + anchor = at, side = StepSide.Right, accent = storePalette(pal, st).signal, items = modes.size, + // Folded: focus goes to the first card of what was picked, or back to the chip. + onDismiss = { move(if (picked) "first" else "item:storechip:${st.id}") }, + handle = { ChipLabel(st, MaterialTheme.colorScheme.onBackground) }, + ) { + StoreTheme(st) { + val library = StoresState.library[st].orEmpty() + val installedCount = remember(StoresState.installed, library) { com.droiddeck.launcher.stores.installedCards(st, StoresState.installed, library).size } + StepList { + modes.forEachIndexed { i, m -> + val label = when (m) { + "store" -> stringResource(R.string.stores_tab_store) + "installed" -> stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_installed), installedCount) + else -> stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_library), library.size) + } + val focusHere = if (current != null) m == current else i == 0 + StepChoice( + label, selected = m == current, enabled = open, + modifier = Modifier.fillMaxWidth().stepItem(i).then(if (focusHere) Modifier.focusRequester(first) else Modifier), + ) { + picked = true + if (chip != st.id) { chip = st.id; openGame = null; query = "" } + tab = m + StoresMotion.fold() + } + } + } + } + }) + } BackHandler(enabled = openGame != null) { closeGame() } val scroll = rememberScrollState() - LaunchedEffect(chip, tab, openGame) { scroll.scrollTo(0) } + val detailScroll = rememberScrollState() + // A store or tab just picked starts at the top. Closing a game page leaves the grid where it + // was, so the page draws back into the card it came from and focus lands on it. + LaunchedEffect(chip, tab) { scroll.scrollTo(0); grid.scrollToItem(0) } + LaunchedEffect(openGame) { detailScroll.scrollTo(0) } LaunchedEffect(focusMove, openGame) { if (inputMode.inputMode != androidx.compose.ui.input.InputMode.Keyboard || ff == null) return@LaunchedEffect - val target = when { - openGame != null -> if (ff.primaryAttached > 0) ff.primary else ff.items["back:${store?.label}"] - focusMove?.first == "card" -> lastOpened?.let { ff.items["card:$it"] } ?: ff.firstTile.takeIf { ff.firstTileAttached > 0 } - focusMove?.first == "first" -> ff.firstTile.takeIf { ff.firstTileAttached > 0 } - else -> null - } ?: return@LaunchedEffect - var landed = false - focusWithinFrames({ landed }) { target.also { landed = runCatching { it.requestFocus() }.isSuccess } } + // Looked up each frame, and asked again until focus is really there: what it goes to (the + // game page's action, the card under it) is composed and placed a frame or two after the + // move that asks for it, and a request before then is dropped without a word. + // A mode with nothing in it (Installed, none installed) has no first card: after a few + // frames focus goes to the store's chip rather than falling back to the rail. + var frames = 0 + fun attached(id: String) = (ff.attached[id] ?: 0) > 0 + fun target(): Pair<androidx.compose.ui.focus.FocusRequester, String?>? { + val kind = focusMove?.first + return when { + openGame != null -> if (ff.primaryAttached > 0) ff.primary to FrontFocus.PRIMARY + else "back:${store?.label}".let { id -> ff.items[id]?.takeIf { attached(id) }?.let { it to id } } + kind == "card" -> lastOpened?.let { "card:$it" }?.let { id -> ff.items[id]?.takeIf { attached(id) }?.let { it to id } } + ?: ff.firstTile.takeIf { ff.firstTileAttached > 0 }?.let { it to null } + kind == "first" -> ff.firstTile.takeIf { ff.firstTileAttached > 0 }?.let { it to null } + // A store not signed in: its Sign in. + ?: ff.primary.takeIf { ff.primaryAttached > 0 }?.let { it to FrontFocus.PRIMARY } + ?: "storechip:$chip".let { id -> ff.items[id]?.takeIf { ++frames > 12 && attached(id) }?.let { it to id } } + kind?.startsWith("item:") == true -> kind.removePrefix("item:").let { id -> ff.items[id]?.takeIf { attached(id) }?.let { it to id } } + else -> null + } + } + // The press that asked for this move must not land on where focus goes: its release + // would press that too (A on a chip went on to Sign in). Wait for A to come up. + repeat(60) { if (!HeldKeys.confirm) return@repeat; androidx.compose.runtime.withFrameNanos { } } + repeat(45) { + androidx.compose.runtime.withFrameNanos { } + val (req, id) = target() ?: return@repeat + if (id != null && ff.last == id) return@LaunchedEffect + if (runCatching { req.requestFocus() }.isSuccess && id == null) return@LaunchedEffect + } } + + // The game page is a layer over the grid: it floods out of the card it was opened from (the + // cog's PageFlood, carrying the card's art), and Back draws it into that card again. [detail] + // outlives [openGame] while it draws back. + var detail by remember { mutableStateOf<String?>(null) } + var leaving by remember { mutableStateOf(false) } + var detailFrom by remember { mutableStateOf<Origin?>(null) } + var detailArt by remember { mutableStateOf<String?>(null) } + var viewportAt by remember { mutableStateOf(Offset.Zero) } + var viewportH by remember { mutableStateOf(0.dp) } + val density = androidx.compose.ui.platform.LocalDensity.current + var hostAt by remember { mutableStateOf(Offset.Zero) } + LaunchedEffect(openGame) { + val g = openGame + if (g != null) { + val mark = StoresMotion.takeCard(g) + detailFrom = mark?.let { Origin(it.bounds.translate(-viewportAt), it.corner) } + detailArt = mark?.art + leaving = false + detail = g + } else if (detail != null) { + if (detailFrom != null) { + leaving = true + delay(Motion.ms(PAGE_RETURN_MS).toLong()) + } + detail = null + leaving = false + detailFrom = null + } + } + val covering = detail != null && !leaving + val coveringNow = androidx.compose.runtime.rememberUpdatedState(covering) + val gridScale by animateFloatAsState(if (covering) 0.95f else 1f, if (covering) Motion.tw(480) else Motion.tw(460, 40), label = "gridSink") + val gridAlpha by animateFloatAsState(if (covering) 0f else 1f, if (covering) Motion.tw(320, 80) else Motion.tw(300, 100), label = "gridFade") + + Box(modifier.onGloballyPositioned { hostAt = it.positionInRoot() }) { Column( - modifier = modifier.padding(horizontal = padH, vertical = if (narrow) 10.dp else 14.dp) + modifier = Modifier.fillMaxSize().padding(horizontal = padH, vertical = if (narrow) 10.dp else 14.dp) .bumpers( onPrevious = { if (openGame == null && chip != DOWNLOADS) { val t = tabsFor(store); switchTab(t[(t.indexOf(shownTab) + t.size - 1) % t.size]) } }, onNext = { if (openGame == null && chip != DOWNLOADS) { val t = tabsFor(store); switchTab(t[(t.indexOf(shownTab) + 1) % t.size]) } }, @@ -163,79 +334,387 @@ internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier // scrolls is clipped at the row's lower edge and, once scrolled, faded out there: the // content's own alpha, not a colour laid over it. Box(Modifier.fillMaxWidth().padding(bottom = 8.dp)) { - Rise(0) { StoreChips(chip, s.storeDownloadsActive, onPick = pickChip, onSettings = { settings = true }) } + StoreTheme(Store.byId(focusedChip ?: chip)) { + Rise(0) { StoreChips(chip, focusedChip, s.storeDownloadsActive, onStore = { st, at -> openModes(st, at) }, onDownloads = { if (chip != DOWNLOADS) pickChip(DOWNLOADS) else move("first") }, + onFocusChip = { focusedChip = it }, onSettings = { settings = true }) } + } } val fade = with(androidx.compose.ui.platform.LocalDensity.current) { 14.dp.toPx() } Box( - Modifier.fillMaxWidth().weight(1f, fill = false).clipToBounds() + Modifier.fillMaxWidth().weight(1f).clipToBounds() + .onGloballyPositioned { viewportAt = it.positionInRoot(); viewportH = with(density) { it.size.height.toDp() } } .graphicsLayer { compositingStrategy = androidx.compose.ui.graphics.CompositingStrategy.Offscreen } .drawWithContent { drawContent() - if (scroll.value > 0 && size.height > fade) drawRect( + val scrolled = if (detail != null) detailScroll.value > 0 + else if (store != null && pane.signedIn) grid.firstVisibleItemIndex > 0 || grid.firstVisibleItemScrollOffset > 0 + else scroll.value > 0 + if (scrolled && size.height > fade) drawRect( Brush.verticalGradient(0f to Color.Transparent, fade / size.height to Color.Black), blendMode = androidx.compose.ui.graphics.BlendMode.DstIn, ) }, ) { - Column(modifier = Modifier.fillMaxWidth().verticalScroll(scroll).padding(top = 4.dp, bottom = 16.dp)) { - when { - chip == DOWNLOADS -> StoresDownloadsPane(s, a) - store == null -> {} - // Signed out, or a sign-in that ran out (a launch could not get its code): sign in again. - !StoresState.isSignedIn(store) || StoresState.expired[store] == true -> Rise(1) { SignInCard(store) } - // One focus group, so the pad walks the page's own controls - back, the hero's - // actions, the cards - and reaches the rail only with Left from them. - openGame != null -> Column(Modifier.fillMaxWidth().focusGroup()) { StoreGameDetail(store, openGame!!, s, a, onBack = { closeGame() }) } - else -> Storefront(store, shownTab, query, s, a, onTab = { switchTab(it) }, onQuery = { query = it }, onOpen = { open(it) }) + // Under a game page the grid stays composed - its scroll and the card to come back to + // stay put - but sinks away, and a pad can neither reach it nor be sent to it. Once the + // page starts drawing back the grid takes focus again, so it lands on the card at once. + val gridFocus = if (covering) null else ff + CompositionLocalProvider(LocalFrontFocus provides gridFocus) { + Column( + modifier = Modifier.fillMaxSize() + .graphicsLayer { scaleX = gridScale; scaleY = gridScale; alpha = gridAlpha } + // Always the same group, only its rule changes: a focus group added around + // the card that has focus leaves the focus tree refusing every request after. + .focusProperties { enter = { if (coveringNow.value) androidx.compose.ui.focus.FocusRequester.Cancel else androidx.compose.ui.focus.FocusRequester.Default } } + .focusGroup(), + ) { + AnimatedContent( + targetState = pane, + modifier = Modifier.fillMaxSize(), + transitionSpec = { + val sameStore = initialState.chip == targetState.chip && initialState.signedIn == targetState.signedIn + (if (sameStore && initialState.tab != targetState.tab) { + // LB / RB: the tab's contents leave the way the bumper points and the + // next tab's come in from the other side, the column nearest leading. + val dir = if (TABS.indexOf(targetState.tab) > TABS.indexOf(initialState.tab)) 1 else -1 + (slideInHorizontally(Motion.sp(0.7f, Spring.StiffnessMediumLow)) { it / 6 * dir } + fadeIn(Motion.tw(220, 60))) + .togetherWith(slideOutHorizontally(Motion.tw(170)) { -it / 6 * dir } + fadeOut(Motion.tw(170)) + scaleOut(Motion.tw(170), targetScale = 0.97f)) + } else { + // Another store: the shelf sinks away, the next rises in (the pane's own move). + (fadeIn(Motion.tw(300, 80)) + slideInVertically(Motion.tw(420, 80)) { it / 24 }) + .togetherWith(fadeOut(Motion.tw(170)) + scaleOut(Motion.tw(170), targetScale = 0.97f)) + }).using(SizeTransform(clip = false)) + }, + label = "storesPane", + ) { k -> + // What is leaving is not where a pad's next move should land. + val outgoing = transition.targetState == androidx.compose.animation.EnterExitState.PostExit + CompositionLocalProvider(LocalFrontFocus provides if (outgoing) null else LocalFrontFocus.current) { + // AnimatedContent stacks its child in a Box: the pane's rows need their own column. + // A pad moving while the pane changes must land on what is coming, not on + // what is leaving: focus there vanishes with it. Same group always, only + // its rule changes (adding one around focus breaks the focus tree). + val leavingNow = androidx.compose.runtime.rememberUpdatedState(outgoing) + val st = Store.byId(k.chip) + // Keep the outgoing pane's palette stable while the next store comes in. + StoreTheme(st) { + Column( + Modifier.fillMaxSize() + .focusProperties { enter = { if (leavingNow.value) androidx.compose.ui.focus.FocusRequester.Cancel else androidx.compose.ui.focus.FocusRequester.Default } } + .focusGroup(), + ) { + when { + k.chip == DOWNLOADS -> Column(Modifier.fillMaxWidth().verticalScroll(scroll).padding(top = 4.dp, bottom = 16.dp)) { StoresDownloadsPane(s, a) } + st == null -> {} + // Signed out, or a sign-in that ran out (a launch could not get its code): sign in again. + !k.signedIn -> Box(Modifier.fillMaxWidth().verticalScroll(scroll).padding(top = 4.dp, bottom = 16.dp)) { SignInCard(st, viewportH - 20.dp) } + else -> Storefront(st, k.tab, query, s, a, grids.getOrPut(k) { LazyGridState() }, onQuery = { query = it }, onOpen = { open(it) }) + } + } + } + } + } + } + } + detail?.let { key -> + val detailStore = Store.byId(key.substringBefore(':')) ?: store + StoreTheme(detailStore) { + val body: @Composable () -> Unit = { + // One focus group, so the pad walks the page's own controls - back, the hero's + // actions - and reaches the rail only with Left from them. It takes every touch, + // so nothing reaches the grid under it. + Column( + Modifier.fillMaxSize().pointerInput(Unit) { detectTapGestures { } } + .verticalScroll(detailScroll).padding(top = 4.dp, bottom = 16.dp), + ) { + if (detailStore != null) Column(Modifier.fillMaxWidth().focusGroup()) { StoreGameDetail(detailStore, key, s, a, onBack = { closeGame() }) } + } + } + val from = detailFrom + if (from == null) body() + else { + // Sized up front: the flood draws the art only once it knows its size, and a painter + // left to take its size from drawing would never load. The card's own bitmap stands + // in from the memory cache until the full one is decoded. + val art = detailArt?.let { url -> + coil.compose.rememberAsyncImagePainter( + remember(url) { + coil.request.ImageRequest.Builder(ctx).data(url).size(1280, 720) + .placeholderMemoryCacheKey(url).build() + }, + ) + } + PageFlood(from, leaving, art) { body() } + } + } } } + } + // Over the whole page: an install's dot on its way to Downloads, a confirm stepping out of the + // control that asked for it, and a sign-in's flood. + FlightLayer(hostAt) + StoresMotion.step?.let { ask -> + key(ask) { + StepOut( + open = StoresMotion.stepOpen, pill = ask.anchor.translate(-hostAt), side = ask.side, accent = ask.accent, + onDismiss = { StoresMotion.fold() }, onClosed = { StoresMotion.closed(ask) }, + pillCorner = ask.pillCorner, items = ask.items, handle = ask.handle, content = ask.content, + ) } } + SignInFloodLayer(hostAt) + } + // Leaving the section: nothing stays stepped out of a control that is no longer on screen. + androidx.compose.runtime.DisposableEffect(Unit) { onDispose { StoresMotion.drop() } } + // An install that started from a button here flies from it to the Downloads chip. + val activeKeys = StoresState.downloads.filter { it.isActive }.map { it.key }.toSet() + val seenActive = remember { mutableSetOf<String>().also { it.addAll(activeKeys) } } + LaunchedEffect(activeKeys) { + val fresh = activeKeys - seenActive + seenActive.clear(); seenActive.addAll(activeKeys) + if (fresh.isNotEmpty()) StoresMotion.takeInstall()?.let { StoresMotion.fly(it) } + } if (settings) StoresSettingsDialog(s, a) { settings = false } - StoresState.pendingInstall?.let { item -> InstallWhereDialog(item) { StoresState.pendingInstall = null } } + StoresState.pendingInstall?.let { item -> InstallWhere(item) } +} + +/** + * "Install <game>:" with a row per place, grown out of the Install button that asked (End + * session's step-out, in blue); the plain dialog when that button is not on screen. + */ +@Composable +private fun InstallWhere(item: CatalogItem) { + val ctx = LocalContext.current + val pal = LocalPalette.current + val from = remember(item) { StoresMotion.recentInstall() } + if (from == null) { InstallWhereDialog(item) { StoresState.pendingInstall = null }; return } + val title = stringResource(R.string.stores_install_title, item.title) + val internalLabel = stringResource(R.string.stores_target_internal) + val label = StoresMotion.installLabel + LaunchedEffect(item) { + val targets = StoreInstallRoot.targets(ctx) + val remembered = SessionPrefs.storesInstallTarget(ctx) + val defaultIndex = targets.indexOfFirst { it.root.absolutePath == remembered }.coerceAtLeast(0) + StoresMotion.ask(StepAsk( + anchor = from, side = StepSide.Right, accent = pal.signal, pillCorner = 12.dp, items = 1 + targets.size, + onDismiss = { if (StoresState.pendingInstall === item) StoresState.pendingInstall = null }, + handle = { Text(label, fontSize = 15.sp, fontWeight = FontWeight.SemiBold, color = pal.signal, maxLines = 1) }, + ) { + StepTitle(title) + StepList { + targets.forEachIndexed { i, t -> + 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)), + enabled = open, + modifier = Modifier.fillMaxWidth().stepItem(1 + i).then(if (i == defaultIndex) Modifier.focusRequester(first) else Modifier), + ) { + SessionPrefs.setStoresInstallTarget(ctx, t.root.absolutePath) + // The fold and the dot leave together: the download it starts flies from Install. + StoresMotion.markInstall(from, label) + StoresState.install(ctx, item, t.root) + StoresMotion.fold() + } + } + } + }) + } +} + +/** A sign-in's flood: out of the button in the store's colour, and home again once the login page closes. */ +@Composable +private fun SignInFloodLayer(hostAt: Offset) { + val f = StoresMotion.signIn ?: return + val ctx = LocalContext.current + val lifecycle = androidx.compose.ui.platform.LocalLifecycleOwner.current.lifecycle + val density = androidx.compose.ui.platform.LocalDensity.current + key(f) { + var paused by remember { mutableStateOf(false) } + // Signed in: into the store's dot on its chip. Not (closed, or it failed): back into the button. + fun back() { if (f.drainTo == null) f.drainTo = if (StoresState.isSignedIn(f.store)) StoresMotion.dots[f.store] ?: f.from else f.from } + androidx.compose.runtime.DisposableEffect(lifecycle) { + val obs = androidx.lifecycle.LifecycleEventObserver { _, e -> + if (e == androidx.lifecycle.Lifecycle.Event.ON_PAUSE && f.away) paused = true + if (e == androidx.lifecycle.Lifecycle.Event.ON_RESUME && f.away && paused) back() + } + lifecycle.addObserver(obs) + onDispose { lifecycle.removeObserver(obs) } + } + // The store had no login page to open: nothing will come back, so drain now. + LaunchedEffect(f.away) { if (f.away) { delay(1_500); if (!paused) back() } } + val to = f.drainTo + if (to == null) ColorFlood(f.from.translate(-hostAt), f.start, f.color) { + if (!f.away) { + f.away = true + com.droiddeck.launcher.stores.StoreLoginActivity.floodColor = f.color.toArgb() + StoresState.signIn(ctx, f.store) + } + } else { + val corner = if (to === f.from) with(density) { 12.dp.toPx() } else to.minDimension / 2f + ColorDrain(f.color, to.translate(-hostAt), corner) { + if (StoresMotion.signIn === f) StoresMotion.signIn = null + if (StoresState.isSignedIn(f.store)) StoresMotion.pulse(f.store) + } + } + } } // ---- the chip row ------------------------------------------------------------------------------- -/** Four equal chips - the stores with a signed-in dot, Downloads with its count - and the cog. */ +/** + * Four equal chips - the stores with a signed-in dot, Downloads with its count - and the cog. The + * selection is one shape that belongs to the row, not each chip's background: it glides from chip + * to chip like the focus ring, its leading edge first, tinted in the store's colour, and a hop + * past a chip goes as a drop. + */ @Composable -private fun StoreChips(selected: String, active: Int, onPick: (String) -> Unit, onSettings: () -> Unit) { - Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { - for (store in Store.entries) { +private fun StoreChips( + selected: String, focused: String?, active: Int, onStore: (Store, Rect?) -> Unit, onDownloads: () -> Unit, onSettings: () -> Unit, + /** The chip a pad's focus is on (null when it leaves the row): the page shows that store. */ + onFocusChip: (String?) -> Unit, +) { + val pal = LocalPalette.current + val keys = Store.entries.map { it.id } + DOWNLOADS + val bounds = remember { androidx.compose.runtime.mutableStateMapOf<String, Rect>() } + val rootBounds = remember { HashMap<String, Rect>() } + // The fill travels with focus; only the expensive pane change waits for the debounce. + val highlighted = focused ?: selected + val tint = androidx.compose.animation.animateColorAsState( + Store.byId(highlighted)?.let { sourceColours(it.id).dot } ?: pal.signal, Motion.tw(160), label = "chipTint", + ) + Box(Modifier.fillMaxWidth()) { + Row(horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { + for (store in Store.entries) { + val signedIn = StoresState.isSignedIn(store) && StoresState.expired[store] != true + StoreChip( + label = store.shortLabel, on = selected == store.id, key = store.id, + modifier = Modifier.weight(1f).onGloballyPositioned { bounds[store.id] = it.boundsInParent(); rootBounds[store.id] = it.boundsInRoot() }, + lead = { + val c = sourceColours(store.id) + // A store's dot pulses once when it is picked signed in, and when a sign-in lands. + var picked by remember { mutableStateOf(0) } + val on = selected == store.id + val wasOn = remember { booleanArrayOf(on) } + LaunchedEffect(on) { + if (on && !wasOn[0] && signedIn) picked++ + wasOn[0] = on + } + Box( + Modifier.size(12.dp) + .onGloballyPositioned { StoresMotion.dots[store] = it.boundsInRoot() } + .pulseRing(picked + (StoresMotion.pulses[store] ?: 0), c.dot), + ) { Box(Modifier.matchParentSize().clip(CircleShape).background(c.dot).alpha(if (signedIn) 1f else 0.35f)) } + }, + description = store.label + if (signedIn) "" else " " + stringResource(R.string.stores_chip_signed_out), + onFocused = onFocusChip, + ) { onStore(store, rootBounds[store.id]) } + } StoreChip( - label = store.shortLabel, on = selected == store.id, key = store.id, modifier = Modifier.weight(1f), - lead = { - val c = sourceColours(store.id) - Box(Modifier.size(12.dp).clip(CircleShape).background(c.dot).alpha(if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) 1f else 0.35f)) + label = stringResource(R.string.stores_tab_downloads), on = selected == DOWNLOADS, key = DOWNLOADS, + modifier = Modifier.weight(1f).onGloballyPositioned { + bounds[DOWNLOADS] = it.boundsInParent() + StoresMotion.downloadsChip = it.boundsInRoot() }, - description = store.label + if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) "" else " " + stringResource(R.string.stores_chip_signed_out), - ) { onPick(store.id) } + lead = { Icon(Icons.Outlined.Download, null, modifier = Modifier.size(18.dp)) }, + trail = if (active > 0) { { CountPill(active) } } else null, + onFocused = onFocusChip, + ) { onDownloads() } + IconChip(Icons.Filled.Settings, stringResource(R.string.stores_settings), onSettings) } - StoreChip( - label = stringResource(R.string.stores_tab_downloads), on = selected == DOWNLOADS, key = DOWNLOADS, modifier = Modifier.weight(1f), - lead = { Icon(Icons.Outlined.Download, null, modifier = Modifier.size(18.dp)) }, - trail = if (active > 0) { { CountPill(active) } } else null, - ) { onPick(DOWNLOADS) } - IconChip(Icons.Filled.Settings, stringResource(R.string.stores_settings), onSettings) + // Over the chips: the fill is translucent, and the other chips' rest fill would hide it in flight. + ChipGlide(keys.indexOf(highlighted), bounds[highlighted], tint) + } +} + +/** + * The selection under the chips: [at] (in the row) of chip [index], in [tint]. A move to the next + * chip stretches it - the leading edge on the focus ring's lead spring, the trailing one a beat + * later on its trail spring - and a hop past a chip pinches it to a drop, carries it and opens it. + */ +@Composable +private fun BoxScope.ChipGlide(index: Int, at: Rect?, tint: State<Color>) { + val density = androidx.compose.ui.platform.LocalDensity.current + val left = remember { Animatable(Float.NaN) } + val right = remember { Animatable(0f) } + val pinch = remember { Animatable(0f) } + val top = remember { mutableStateOf(0f) } + val bottom = remember { mutableStateOf(0f) } + val was = remember { intArrayOf(-1) } + LaunchedEffect(index, at) { + val b = at ?: return@LaunchedEffect + top.value = b.top; bottom.value = b.bottom + val from = was[0] + was[0] = index + if (left.value.isNaN() || from == index || from < 0 || Motion.scale == 0f) { + left.snapTo(b.left); right.snapTo(b.right); pinch.snapTo(0f) + return@LaunchedEffect + } + coroutineScope { + if (kotlin.math.abs(index - from) > 1) { + // Too far to stretch: pinch into a drop where it is, carry it over, open it there. + pinch.animateTo(1f, Motion.tw(90)) + val l = launch { left.animateTo(b.left, Motion.sp(0.7f, 380f)) } + val r = launch { right.animateTo(b.right, Motion.sp(0.7f, 380f)) } + l.join(); r.join() + pinch.animateTo(0f, Motion.sp(0.6f, 500f)) + } else { + val forward = index > from + val (lead, trail) = if (forward) right to left else left to right + val leadTo = if (forward) b.right else b.left + val trailTo = if (forward) b.left else b.right + launch { lead.animateTo(leadTo, Motion.sp(0.62f, 700f)) } + launch { delay(Motion.ms(40).toLong()); trail.animateTo(trailTo, Motion.sp(0.78f, 360f)) } + } + } + } + androidx.compose.foundation.Canvas(Modifier.matchParentSize()) { + if (left.value.isNaN() || bottom.value <= top.value) return@Canvas + val l = minOf(left.value, right.value) + val r = maxOf(left.value, right.value) + val cx = (l + r) / 2f + val cy = (top.value + bottom.value) / 2f + val drop = with(density) { 6.dp.toPx() } + val p = pinch.value + val x0 = androidx.compose.ui.util.lerp(l, cx - drop, p) + val x1 = androidx.compose.ui.util.lerp(r, cx + drop, p) + val y0 = androidx.compose.ui.util.lerp(top.value, cy - drop, p) + val y1 = androidx.compose.ui.util.lerp(bottom.value, cy + drop, p) + val size = androidx.compose.ui.geometry.Size(x1 - x0, y1 - y0) + val corner = androidx.compose.ui.geometry.CornerRadius(minOf(size.width, size.height) / 2f) + val at0 = Offset(x0, y0) + // A drop in flight is solid; on a chip it is the chip's tinted fill and outline. + drawRoundRect(tint.value.copy(alpha = androidx.compose.ui.util.lerp(0.16f, 0.9f, p)), at0, size, corner) + drawRoundRect(tint.value.copy(alpha = 0.7f * (1f - p)), at0, size, corner, style = androidx.compose.ui.graphics.drawscope.Stroke(with(density) { 1.dp.toPx() })) } } @Composable private fun StoreChip( label: String, on: Boolean, key: String, modifier: Modifier, lead: @Composable () -> Unit, - trail: (@Composable () -> Unit)? = null, description: String? = null, onClick: () -> Unit, + trail: (@Composable () -> Unit)? = null, description: String? = null, + onFocused: (String?) -> Unit = {}, onClick: () -> Unit, ) { + val inputMode = androidx.compose.ui.platform.LocalInputModeManager.current val colors = MaterialTheme.colorScheme val pal = LocalPalette.current val src = remember { MutableInteractionSource() } val hot = rememberHot(src) + val pressed by src.collectIsPressedAsState() + val scale by animateFloatAsState(if (pressed) 0.95f else 1f, Motion.sp(0.5f, Spring.StiffnessMedium), label = "chipScale") val shape = RoundedCornerShape(99.dp) val ink = if (on) colors.onBackground else if (hot) colors.onBackground else colors.onSurfaceVariant + // The selected look is the row's glide under the chip, so a chip here draws only its rest state. Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.CenterHorizontally), - modifier = modifier.paneItem("storechip:$key").heightIn(min = 44.dp).clip(shape) - .background(if (on) pal.signal.copy(alpha = 0.16f) else colors.surface) - .glideBorder(hot, shape, pal.signal, if (on) pal.signal.copy(alpha = 0.7f) else pal.line) + modifier = modifier.paneItem("storechip:$key") + .onFocusChanged { f -> + if (f.isFocused && inputMode.inputMode == androidx.compose.ui.input.InputMode.Keyboard) onFocused(key) + else if (!f.isFocused) onFocused(null) + } + .heightIn(min = 44.dp).graphicsLayer { scaleX = scale; scaleY = scale }.clip(shape) + .background(if (on) Color.Transparent else colors.surface) + .glideBorder(hot, shape, pal.signal, if (on) Color.Transparent else pal.line) .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Tab, onClick = onClick) .controllerConfirm(onClick = onClick) .then(if (description != null) Modifier.semantics { contentDescription = description } else Modifier) @@ -268,31 +747,88 @@ internal fun CountPill(count: Int) { val pal = LocalPalette.current Text( count.toString(), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = pal.onSignal, maxLines = 1, - modifier = Modifier.clip(RoundedCornerShape(8.dp)).background(pal.signal).padding(horizontal = 6.dp, vertical = 1.dp), + // Pops as an install's dot lands in it. + modifier = Modifier.landingPop().clip(RoundedCornerShape(8.dp)).background(pal.signal).padding(horizontal = 6.dp, vertical = 1.dp), ) } // ---- sign-in ----------------------------------------------------------------------------------- -/** No account for this store yet: who it signs in as, and the button that opens its login page. */ +/** + * No account for this store yet: the Steam tab's shape in the store's colour. A tilted wall of the + * store's own games drifts behind (its public storefront; blank capsules in its colour when it has + * none), fading out toward the words, and Sign in sits large at the foot. [height] is the pane's. + */ @Composable -private fun SignInCard(store: Store) { +private fun SignInCard(store: Store, height: Dp) { val ctx = LocalContext.current val colors = MaterialTheme.colorScheme val pal = LocalPalette.current val narrow = LocalNarrowPane.current - Column(verticalArrangement = Arrangement.spacedBy(12.dp)) { - Row( - verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(18.dp), - modifier = Modifier.fillMaxWidth().clip(Shape16).background(colors.surface).border(1.dp, pal.line, Shape16).padding(18.dp), - ) { - StoreLogo(store, 56.dp) - Column(modifier = Modifier.weight(1f)) { - Text(store.label, fontSize = 18.sp, fontWeight = FontWeight.SemiBold, color = colors.onBackground) + val c = sourceColours(store.id) + // The storefront needs no account; it is what the wall is made of. + LaunchedEffect(store) { StoresState.preview(ctx, store) } + // A handful of games would repeat down every column: under eight, the blank wall in its colour. + val art = remember(StoresState.shelves[store]) { + StoresState.shelves[store]?.all.orEmpty().filter { !it.mature }.mapNotNull { it.tallImageUrl ?: it.imageUrl }.distinct().take(48) + .takeIf { it.size >= 8 }.orEmpty() + } + val shape = RoundedCornerShape(20.dp) + val capsule = RoundedCornerShape(10.dp) + Box(Modifier.fillMaxWidth().height(height.coerceAtLeast(320.dp)).clip(shape).background(colors.background).border(1.dp, pal.line, shape)) { + // The storefront arrives a moment after the page: its wall fades in over the blank one. + androidx.compose.animation.Crossfade(art, animationSpec = Motion.tw(600), label = "signInWall") { tiles -> + TiltedWall(tiles.size, if (tiles.isEmpty()) 75_000 else 50_000) { i, m -> + if (tiles.isEmpty()) Box(m.clip(capsule).background(Brush.linearGradient(listOf(c.fill, colors.background))).border(1.dp, c.dot.copy(alpha = 0.22f), capsule)) + else Box(m.clip(capsule).background(c.fill)) { AsyncImage(model = tiles[i], contentDescription = null, contentScale = ContentScale.Crop, modifier = Modifier.matchParentSize()) } } - if (!narrow) PrimaryButton(stringResource(R.string.stores_signin), main = true) { StoresState.signIn(ctx, store) } } - if (narrow) Actions { PrimaryButton(stringResource(R.string.stores_signin), main = true) { StoresState.signIn(ctx, store) } } + // The wall gives way to the words: dark at the left and the foot, and the store's glow under them. + Box( + Modifier.fillMaxSize().background( + Brush.horizontalGradient( + 0f to colors.background.copy(alpha = 0.97f), 0.45f to colors.background.copy(alpha = 0.9f), + 0.78f to colors.background.copy(alpha = 0.35f), 1f to colors.background.copy(alpha = 0.15f), + ), + ), + ) + Box(Modifier.fillMaxSize().background(Brush.verticalGradient(0.5f to Color.Transparent, 1f to colors.background.copy(alpha = 0.92f)))) + Box( + Modifier.fillMaxSize().drawBehind { + drawRect(Brush.radialGradient(listOf(c.dot.copy(alpha = 0.22f), Color.Transparent), center = Offset(size.width * 0.16f, size.height * 0.9f), radius = size.height * 0.75f)) + }, + ) + Column( + verticalArrangement = Arrangement.spacedBy(10.dp), + modifier = Modifier.align(Alignment.BottomStart).padding(start = if (narrow) 20.dp else 40.dp, bottom = if (narrow) 20.dp else 36.dp, end = 20.dp), + ) { + Rise(2) { + Text( + store.label, fontSize = if (narrow) 30.sp else 42.sp, lineHeight = if (narrow) 34.sp else 46.sp, + fontWeight = FontWeight.Black, color = colors.onBackground, + ) + } + Spacer(Modifier.height(6.dp)) + Rise(3) { SignInButton(store, large = true) } + } + } +} + +/** + * Sign in: the button floods the page in the store's colour (LaunchFlood's move), the login page + * opens on that colour, and the flood comes home into the store's dot once it closes. + */ +@Composable +private fun SignInButton(store: Store, large: Boolean = false) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val placed = remember { arrayOfNulls<androidx.compose.ui.layout.LayoutCoordinates>(1) } + Box(Modifier.onGloballyPositioned { placed[0] = it }) { + PrimaryButton(stringResource(R.string.stores_signin), main = true, large = large, icon = if (large) Icons.AutoMirrored.Filled.Login else null) { + val at = placed[0]?.takeIf { it.isAttached }?.boundsInRoot() + if (at == null || Motion.scale == 0f || StoresMotion.signIn != null) StoresState.signIn(ctx, store) + else StoresMotion.signIn = SignInFlood(store, at, colors.primary, sourceColours(store.id).dot) + } } } @@ -310,7 +846,8 @@ internal fun StoreLogo(store: Store, size: Dp) { @Composable private fun Storefront( store: Store, tab: String, query: String, s: FrontEndState, a: FrontEndActions, - onTab: (String) -> Unit, onQuery: (String) -> Unit, onOpen: (String) -> Unit, + grid: LazyGridState, + onQuery: (String) -> Unit, onOpen: (String) -> Unit, ) { val ctx = LocalContext.current val colors = MaterialTheme.colorScheme @@ -326,101 +863,113 @@ private fun Storefront( fun shelf(l: List<CatalogItem>?) = l?.filter(::shown) // The Store tab's search: the catalog the shelves brought and the library, each title once. val everything = remember(library, shelves) { (library + (shelves?.all ?: emptyList())).distinctBy { it.id } } - // Library's dropdown: All (every owned game, the default) or Installed. - var installedOnly by rememberSaveable(store) { mutableStateOf(false) } - var filterOpen by remember { mutableStateOf(false) } - val libraryLabel = if (installedOnly) stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_installed), installedItems.size) - else stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_library), library.size) - Rise(1) { - SubTabs( - listOf("store" to stringResource(R.string.stores_tab_store), "library" to libraryLabel).filter { it.first in tabsFor(store) }, - tab, - onPick = { key -> if (key == "library") { if (tab == "library") filterOpen = !filterOpen else onTab(key) } else onTab(key) }, - menuFor = { key, tabWidth -> - if (key == "library") AnchoredMenu(filterOpen, onDismiss = { filterOpen = false }, compact = true, minWidth = tabWidth) { first -> - MenuItem(stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_all), library.size), checked = !installedOnly, focusRequester = first) { installedOnly = false; filterOpen = false; onTab("library") } - MenuItem(stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_installed), installedItems.size), checked = installedOnly) { installedOnly = true; filterOpen = false; onTab("library") } - } - }, - trailing = { - // A purchase made elsewhere shows on a refresh; otherwise the library refreshes itself every six hours. - SmallTextButton(stringResource(R.string.stores_refresh)) { StoresState.open(ctx, store, force = true) } - val name = StoresState.accounts[store] ?: "" - SmallTextButton(stringResource(R.string.stores_signout_named, name)) { StoresState.signOut(ctx, store) } - }, - ) + // Installed is a mode of its own now, picked from the store's chip like Store and Library. + val installedOnly = tab == "installed" + // Until the cache is read (a moment, off the main thread) there is nothing to count or to say is missing. + val loaded = StoresState.library.containsKey(store) + val showGrid = tab != "store" || q.isNotEmpty() + val visible = remember(library, installedItems, everything, tab, q, s.storesShowMature) { + when (tab) { + "library" -> library.filter(::matches) + "installed" -> installedItems.filter(::matches) + else -> everything.filter { matches(it) && shown(it) } + } } - Rise(2) { - SearchField( - query, onQuery, - placeholder = when { - tab == "store" -> stringResource(R.string.stores_search_catalog, store.label) - installedOnly -> stringResource(R.string.stores_search_installed) - else -> stringResource(R.string.stores_search_library) - }, - ) - } - StoresState.status[store]?.let { line -> Rise(3) { SyncBar(line) } } - StoresState.problems[store]?.let { Rise(3) { Box(Modifier.padding(bottom = 8.dp)) { Note(it) } } } - // The first card drawn on the tab is where a pad lands after a tab or chip change. + val empty = stringResource(when { + installedOnly -> R.string.stores_installed_empty + tab == "library" && library.isEmpty() && StoresState.status[store] != null -> R.string.stores_library_loading + else -> R.string.stores_nothing_matches + }) + // A lazy item can be recomposed or prefetched out of order; the first grid card is identified + // by its key, rather than whichever happens to compose first. var firstPlaced = false val card: @Composable (CatalogItem) -> Unit = { item -> - val first = !firstPlaced + val first = if (showGrid) item.key == visible.firstOrNull()?.key else !firstPlaced firstPlaced = true GameCard(item, store, s, a, installedKeys, onOpen, first) } - when (tab) { - "library" -> if (installedOnly) Grid(installedItems.filter(::matches), stringResource(R.string.stores_installed_empty), card) - else Grid(library.filter(::matches), if (library.isEmpty() && StoresState.status[store] != null) stringResource(R.string.stores_library_loading) else stringResource(R.string.stores_nothing_matches), card) - else -> { - if (q.isNotEmpty()) Grid(everything.filter { matches(it) && shown(it) }, stringResource(R.string.stores_nothing_matches), card) - else { - Shelf(stringResource(R.string.stores_shelf_new), shelf(shelves?.whatsNew), card) - Shelf(stringResource(R.string.stores_shelf_deals), shelf(shelves?.deals), card) - Shelf(stringResource(R.string.stores_shelf_free), shelf(shelves?.free), card) - Shelf(stringResource(R.string.stores_shelf_trending), shelf(shelves?.trending), card) - Shelf(stringResource(R.string.stores_shelf_library), library, card) - if (shelves == null && library.isEmpty()) Rise(4) { Note(if (StoresState.status[store] != null) stringResource(R.string.stores_library_loading) else stringResource(R.string.stores_shelves_loading)) } - else if (shelves != null && shelves.isEmpty && store == Store.AMAZON) Rise(4) { Note(stringResource(R.string.stores_amazon_no_catalog)) } + LazyVerticalGrid( + columns = GridCells.Adaptive(CardWidth), state = grid, modifier = Modifier.fillMaxSize(), + horizontalArrangement = Arrangement.spacedBy(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp), + contentPadding = PaddingValues(top = 4.dp, bottom = 16.dp), + ) { + item(key = "header", span = { GridItemSpan(maxLineSpan) }) { + Rise(1) { + ModeHeader( + when { + tab == "store" -> stringResource(R.string.stores_tab_store) + !loaded -> stringResource(if (installedOnly) R.string.stores_tab_installed else R.string.stores_tab_library) + installedOnly -> stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_installed), installedItems.size) + else -> stringResource(R.string.stores_tab_count, stringResource(R.string.stores_tab_library), library.size) + }, + query, onQuery, + placeholder = when { + tab == "store" -> stringResource(R.string.stores_search_catalog, store.label) + installedOnly -> stringResource(R.string.stores_search_installed) + else -> stringResource(R.string.stores_search_library) + }, + ) { + // A purchase made elsewhere shows on a refresh; otherwise the library refreshes itself every six hours. + SmallTextButton(stringResource(R.string.stores_refresh)) { StoresState.open(ctx, store, force = true) } + val name = StoresState.accounts[store] ?: "" + SignOutButton(store, name) + } + } + } + StoresState.status[store]?.let { line -> item(key = "sync", span = { GridItemSpan(maxLineSpan) }) { Rise(3) { SyncBar(line) } } } + StoresState.problems[store]?.let { line -> item(key = "problem", span = { GridItemSpan(maxLineSpan) }) { Rise(3) { Box(Modifier.padding(bottom = 8.dp)) { Note(line) } } } } + when (tab) { + "library", "installed" -> if (!loaded) Unit + else storeGrid(visible, empty, card) + else -> { + if (q.isNotEmpty()) storeGrid(visible, empty, card) + else { + item(key = "shelves", span = { GridItemSpan(maxLineSpan) }) { + Column { + Shelf(stringResource(R.string.stores_shelf_new), shelf(shelves?.whatsNew), card) + Shelf(stringResource(R.string.stores_shelf_deals), shelf(shelves?.deals), card) + Shelf(stringResource(R.string.stores_shelf_free), shelf(shelves?.free), card) + Shelf(stringResource(R.string.stores_shelf_trending), shelf(shelves?.trending), card) + Shelf(stringResource(R.string.stores_shelf_library), library, card) + if (shelves == null && library.isEmpty()) Rise(4) { Note(if (StoresState.status[store] != null) stringResource(R.string.stores_library_loading) else stringResource(R.string.stores_shelves_loading)) } + else if (shelves != null && shelves.isEmpty && store == Store.AMAZON) Rise(4) { Note(stringResource(R.string.stores_amazon_no_catalog)) } + } + } + } } } } } -/** Underlined tabs as the preview draws them, with [trailing] (the account) at the right. */ +/** + * What the store is showing (Store, Library, Installed), a magnifier beside it that opens into the + * search field across the row, and [trailing] (the account) at the right, over a rule. + */ @Composable -private fun SubTabs( - tabs: List<Pair<String, String>>, selected: String, onPick: (String) -> Unit, - menuFor: @Composable (String, androidx.compose.ui.unit.Dp) -> Unit = { _, _ -> }, trailing: @Composable () -> Unit, -) { +private fun ModeHeader(title: String, query: String, onQuery: (String) -> Unit, placeholder: String, trailing: @Composable () -> Unit) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current + // Open while it holds a query; an empty field closes again once focus leaves it. + var searching by remember { mutableStateOf(query.isNotEmpty()) } + val open by animateFloatAsState(if (searching) 1f else 0f, Motion.sp(0.75f, 420f), label = "searchOpen") Column(Modifier.fillMaxWidth()) { - Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth()) { - Row(horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.weight(1f).horizontalScroll(rememberScrollState())) { - for ((key, label) in tabs) { - val on = key == selected - val src = remember { MutableInteractionSource() } - val hot = rememberHot(src) - val pick = { onPick(key) } - val density = androidx.compose.ui.platform.LocalDensity.current - var width by remember { mutableStateOf(0.dp) } - Box(Modifier.onSizeChanged { width = with(density) { it.width.toDp() } }) { - Column( - modifier = Modifier.paneItem("storetab:$key").clip(RoundedCornerShape(8.dp)) - .background(if (hot) pal.signal.copy(alpha = 0.12f) else Color.Transparent) - .glideBorder(hot, RoundedCornerShape(8.dp), pal.signal) - .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Tab, onClick = pick) - .controllerConfirm(onClick = pick) - .padding(horizontal = 8.dp, vertical = 6.dp), - ) { - Text(label, fontSize = 13.sp, fontWeight = FontWeight.SemiBold, color = if (on) colors.onBackground else colors.onSurfaceVariant, maxLines = 1) - Box(Modifier.padding(top = 4.dp).fillMaxWidth().height(2.dp).background(if (on) pal.signal else Color.Transparent)) + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.fillMaxWidth().heightIn(min = 52.dp).padding(vertical = 4.dp)) { + Text( + title, fontSize = 15.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, maxLines = 1, + modifier = Modifier.padding(start = 4.dp, end = 4.dp), + ) + BoxWithConstraints(Modifier.weight(1f), contentAlignment = Alignment.CenterStart) { + if (searching || open > 0.01f) { + // The field grows out of the magnifier, the row's width at most. + val w = androidx.compose.ui.unit.lerp(44.dp, maxWidth.coerceAtMost(520.dp), open) + Box(Modifier.width(w).graphicsLayer { alpha = (open * 1.6f).coerceAtMost(1f) }) { + AdbTextField( + value = query, onValueChange = onQuery, label = "", keyboardType = KeyboardType.Text, imeAction = ImeAction.Search, + placeholder = placeholder, compact = true, focusOnShow = true, + onFocusChange = { focused -> if (!focused && query.isEmpty()) searching = false }, + ) } - // A tab's own dropdown (Library's filter), anchored under it. - menuFor(key, width) - } - } + } else SearchIcon(stringResource(R.string.stores_search_open)) { searching = true } } trailing() } @@ -428,26 +977,102 @@ private fun SubTabs( } } +/** A store chip's face: its dot and short name, for what steps out of it to carry. */ @Composable -private fun SmallTextButton(text: String, onClick: () -> Unit) { +private fun ChipLabel(store: Store, ink: Color) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { + Box(Modifier.size(12.dp).clip(CircleShape).background(sourceColours(store.id).dot)) + Text(store.shortLabel, fontSize = 13.sp, fontWeight = FontWeight.Bold, color = ink, maxLines = 1) + } +} + +/** The page's accent in [store]'s own colour (GOG purple, Epic white, Amazon orange); [base] for none. */ +internal fun storePalette(base: Palette, store: Store?): Palette { + val (accent, deep) = when (store) { + Store.GOG -> Color(0xFFC25BC8) to Color(0xFF9A3FA1) + Store.EPIC -> Color(0xFFE6E6E6) to Color(0xFFBDBDBD) + Store.AMAZON -> Color(0xFFFF9900) to Color(0xFFE07800) + null -> return base + } + val on = if (accent.luminance() > 0.18f) Color(0xFF0B0B0D) else Color.White + return Palette( + base.id, base.label, base.detail, base.background, base.surface, base.surfaceVariant, base.line, base.line2, + base.onBackground, base.onSurfaceVariant, primary = accent, primary2 = deep, onPrimary = on, signal = accent, + good = base.good, error = base.error, + ) +} + +/** [content] with the focus ring, buttons and step-outs in [store]'s colour. */ +@Composable +internal fun StoreTheme(store: Store?, content: @Composable () -> Unit) { + val base = LocalPalette.current + val p = remember(base, store) { storePalette(base, store) } + // Downloads uses the base palette, but the provider stays in the same focus tree. + val scheme = MaterialTheme.colorScheme.copy(primary = p.primary, onPrimary = p.onPrimary) + CompositionLocalProvider(LocalPalette provides p) { MaterialTheme(colorScheme = scheme, typography = MaterialTheme.typography, content = content) } +} + +@Composable +private fun SmallTextButton(text: String, key: String = text, onClick: () -> Unit) { val colors = MaterialTheme.colorScheme val pal = LocalPalette.current val src = remember { MutableInteractionSource() } val hot = rememberHot(src) Text( text, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = if (hot) colors.onBackground else colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis, - modifier = Modifier.paneItem("btn:$text").clip(RoundedCornerShape(8.dp)).glideBorder(hot, RoundedCornerShape(8.dp), pal.signal) + modifier = Modifier.paneItem("btn:$key").clip(RoundedCornerShape(8.dp)).glideBorder(hot, RoundedCornerShape(8.dp), pal.signal) .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onClick) .controllerConfirm(onClick = onClick).padding(horizontal = 8.dp, vertical = 6.dp), ) } +/** + * "<account> · sign out", which asks first: are you sure steps out of it (End session's shape, in + * red), Stay signed in first, so a stray A costs nothing. + */ @Composable -private fun SearchField(query: String, onQuery: (String) -> Unit, placeholder: String) { - AdbTextField( - value = query, onValueChange = onQuery, label = "", keyboardType = KeyboardType.Text, imeAction = ImeAction.Search, - placeholder = placeholder, compact = true, modifier = Modifier.padding(top = 8.dp, bottom = 4.dp), - ) +private fun SignOutButton(store: Store, name: String) { + val ctx = LocalContext.current + val colors = MaterialTheme.colorScheme + val label = stringResource(R.string.stores_signout_named, name) + val title = stringResource(R.string.stores_signout_title, store.label) + val note = stringResource(R.string.stores_signout_note) + val stay = stringResource(R.string.stores_signout_stay) + val go = stringResource(R.string.stores_signout_go) + var at by remember { mutableStateOf<Rect?>(null) } + Box(Modifier.onGloballyPositioned { at = it.boundsInRoot() }) { + SmallTextButton(label, key = "signout") { + val from = at + if (from == null) { StoresState.signOut(ctx, store); return@SmallTextButton } + StoresMotion.ask(StepAsk( + anchor = from, side = StepSide.Left, accent = colors.error, pillCorner = 8.dp, items = 3, + handle = { Text(label, fontSize = 12.sp, fontWeight = FontWeight.SemiBold, color = colors.error, maxLines = 1, overflow = TextOverflow.Ellipsis) }, + ) { + StepTitle(title) + Text(note, fontSize = 13.sp, color = colors.onSurfaceVariant, modifier = Modifier.stepItem(1).padding(start = 4.dp, bottom = 12.dp)) + Row(horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End), modifier = Modifier.stepItem(2)) { + StepChoice(stay, enabled = open, modifier = Modifier.focusRequester(first)) { StoresMotion.fold() } + StepChoice(go, danger = true, enabled = open) { StoresMotion.fold(); StoresState.signOut(ctx, store) } + } + }) + } + } +} + +/** The header's magnifier: a round button the search field opens out of. */ +@Composable +private fun SearchIcon(description: String, onClick: () -> Unit) { + val colors = MaterialTheme.colorScheme + val pal = LocalPalette.current + val src = remember { MutableInteractionSource() } + val hot = rememberHot(src) + Box( + contentAlignment = Alignment.Center, + modifier = Modifier.paneItem("storesearch").size(40.dp).clip(CircleShape).background(if (hot) pal.signal.copy(alpha = 0.14f) else Color.Transparent) + .glideBorder(hot, CircleShape, pal.signal) + .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, role = Role.Button, onClick = onClick) + .controllerConfirm(onClick = onClick), + ) { Icon(Icons.Filled.Search, description, tint = if (hot) pal.signal else colors.onSurfaceVariant, modifier = Modifier.size(20.dp)) } } /** A horizontal row of cards under a title; nothing at all when the list is empty or not here yet. */ @@ -461,20 +1086,14 @@ private fun Shelf(title: String, items: List<CatalogItem>?, card: @Composable (C } } -/** Cards in rows that fill the width; [empty] when there are none. */ -@Composable -private fun Grid(items: List<CatalogItem>, empty: String, card: @Composable (CatalogItem) -> Unit) { - if (items.isEmpty()) { Box(Modifier.padding(vertical = 32.dp).fillMaxWidth(), contentAlignment = Alignment.Center) { Text(empty, fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) }; return } - BoxWithConstraints(Modifier.fillMaxWidth().padding(top = 10.dp)) { - val gap = 10.dp - val cols = ((maxWidth + gap) / (CardWidth + gap)).toInt().coerceAtLeast(1) - val w = (maxWidth - gap * (cols - 1)) / cols - Column(verticalArrangement = Arrangement.spacedBy(gap)) { - for (row in items.chunked(cols)) Row(horizontalArrangement = Arrangement.spacedBy(gap), modifier = Modifier.fillMaxWidth()) { - for (item in row) key(item.key) { Box(Modifier.width(w)) { card(item) } } - } +/** Only visible cards (and the grid's prefetch) compose; [empty] spans the row when there are none. */ +private fun LazyGridScope.storeGrid(games: List<CatalogItem>, empty: String, card: @Composable (CatalogItem) -> Unit) { + if (games.isEmpty()) item(key = "empty", span = { GridItemSpan(maxLineSpan) }) { + Box(Modifier.padding(vertical = 32.dp).fillMaxWidth(), contentAlignment = Alignment.Center) { + Text(empty, fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant) } } + else items(games, key = { it.key }, contentType = { "game" }) { card(it) } } /** @@ -501,13 +1120,20 @@ private fun GameCard(item: CatalogItem, store: Store, s: FrontEndState, a: Front val scale by animateFloatAsState(if (pressed) 0.985f else 1f, Motion.sp(0.5f, Spring.StiffnessMedium), label = "cardScale") val installed = item.id in installedKeys val download = StoresState.download("${store.id}:${item.id}")?.takeIf { it.isActive } - val open = { onOpen(item.key) } + // Where it sits, so the game page can flood out of it (and draw back into it). + val placed = remember { arrayOfNulls<androidx.compose.ui.layout.LayoutCoordinates>(1) } + val corner = with(androidx.compose.ui.platform.LocalDensity.current) { 10.dp.toPx() } + val open = { + placed[0]?.takeIf { it.isAttached }?.let { StoresMotion.markCard(CardMark(item.key, it.boundsInRoot(), corner, item.imageUrl ?: item.tallImageUrl)) } + onOpen(item.key) + } // A size the library did not give is looked up once the card is on screen or close to it. val screen = with(androidx.compose.ui.platform.LocalDensity.current) { androidx.compose.ui.platform.LocalConfiguration.current.screenHeightDp.dp.toPx() } val ahead = screen / 2 Column( modifier = Modifier.fillMaxWidth() .onGloballyPositioned { c -> + placed[0] = c if (item.owned && item.sizeBytes <= 0) { val b = c.boundsInWindow() if (b.bottom > -ahead && b.top < screen + ahead && b.width > 0f) com.droiddeck.launcher.stores.StoreSizes.request(ctx, item) @@ -517,7 +1143,7 @@ private fun GameCard(item: CatalogItem, store: Store, s: FrontEndState, a: Front .clip(RoundedCornerShape(10.dp)).glideBorder(hot, RoundedCornerShape(10.dp), pal.signal, pal.line) .hoverable(src).clickable(interactionSource = src, indication = LocalIndication.current, onClick = open).controllerConfirm(onClick = open), ) { - CardFace(item, download) { CardMarker(item, installed, download) } + CardFace(item, download, hot) { CardMarker(item, installed, download) } } } @@ -544,8 +1170,9 @@ private val CardStrip = 26.dp * picture, placed so the two meet without a seam. Blur needs API 31; below it the copy is only * darkened, more strongly. */ +@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class) @Composable -private fun CardFace(item: CatalogItem, download: com.droiddeck.launcher.stores.download.DownloadEntry?, marker: @Composable () -> Unit) { +private fun CardFace(item: CatalogItem, download: com.droiddeck.launcher.stores.download.DownloadEntry?, hot: Boolean, marker: @Composable () -> Unit) { val url = item.imageUrl ?: item.tallImageUrl val blurs = android.os.Build.VERSION.SDK_INT >= 31 BoxWithConstraints(Modifier.fillMaxWidth()) { @@ -562,7 +1189,12 @@ private fun CardFace(item: CatalogItem, download: com.droiddeck.launcher.stores. verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp), modifier = Modifier.matchParentSize().padding(horizontal = 8.dp), ) { - Text(item.title, fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f)) + // A title too long for the strip scrolls through while the card has focus. + Text( + item.title, fontSize = 11.sp, fontWeight = FontWeight.Bold, color = Color.White, maxLines = 1, + overflow = if (hot) TextOverflow.Clip else TextOverflow.Ellipsis, + modifier = Modifier.weight(1f).then(if (hot) Modifier.basicMarquee(iterations = Int.MAX_VALUE, initialDelayMillis = 700, velocity = 40.dp) else Modifier), + ) marker() } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/WirelessAdbFixPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/WirelessAdbFixPage.kt index f369427a03..f934ac8bad 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/WirelessAdbFixPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/WirelessAdbFixPage.kt @@ -358,8 +358,11 @@ internal fun AdbTextField( onFocusChange: (Boolean) -> Unit = {}, onNext: (() -> Unit)? = null, onDone: (() -> Unit)? = null, + /** Takes focus (and the keyboard) once, as soon as it is shown: a field that opened on request. */ + focusOnShow: Boolean = false, ) { val context = LocalContext.current + val focusAsked = remember { booleanArrayOf(false) } val palette = LocalPalette.current val onValueChangeLatest = rememberUpdatedState(onValueChange) val onNextLatest = rememberUpdatedState(onNext) @@ -434,6 +437,14 @@ internal fun AdbTextField( } }, update = { editText -> + if (focusOnShow && !focusAsked[0]) { + focusAsked[0] = true + editText.post { + editText.requestFocus() + (editText.context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE) as? android.view.inputmethod.InputMethodManager) + ?.showSoftInput(editText, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT) + } + } editText.setTextColor(colors.onBackground.toArgb()) editText.setHintTextColor(colors.onSurfaceVariant.toArgb()) editText.hint = placeholder diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bc495bc9cf..fccdf24a1c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1979,9 +1979,14 @@ <string name="stores_tab_count">%1$s (%2$d)</string> <string name="stores_signin">Sign in</string> <string name="stores_signout_named">%1$s · sign out</string> + <string name="stores_signout_title">Sign out of %1$s?</string> + <string name="stores_signout_note">Installed games stay. The library is fetched again when you sign back in.</string> + <string name="stores_signout_stay">Stay signed in</string> + <string name="stores_signout_go">Sign out</string> <string name="stores_search_catalog">Search the %1$s catalog…</string> <string name="stores_search_installed">Search installed games…</string> <string name="stores_search_library">Search your library…</string> + <string name="stores_search_open">Search</string> <string name="stores_installed_empty">Nothing installed</string> <string name="stores_library_loading">Loading</string> <string name="stores_nothing_matches">No games</string> diff --git a/app/src/test/java/com/droiddeck/launcher/ui/StepOutTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/StepOutTest.kt new file mode 100644 index 0000000000..dccb220cdb --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/StepOutTest.kt @@ -0,0 +1,54 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.graphics.Path +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class StepOutTest { + private val pill = Rect(400f, 100f, 500f, 140f) + + @Test fun leftOutlineRunsFromTheBoxLeftToThePillRight() { + val path = Path() + stepPath(path, pill, left = 240f, bottom = 300f, pullTop = 146f, box = 16f, fillet = 10f) + val b = path.getBounds() + assertEquals(240f, b.left, 0.5f) + assertEquals(500f, b.right, 0.5f) + assertEquals(100f, b.top, 0.5f) + assertEquals(300f, b.bottom, 0.5f) + } + + @Test fun rightOutlineMirrorsItAboutThePill() { + val path = Path() + stepPathRight(path, pill, right = 660f, bottom = 300f, pullTop = 146f, box = 16f, fillet = 10f) + val b = path.getBounds() + assertEquals(400f, b.left, 0.5f) + assertEquals(660f, b.right, 0.5f) + assertEquals(100f, b.top, 0.5f) + assertEquals(300f, b.bottom, 0.5f) + } + + @Test fun aClosedOutlineIsJustThePill() { + val path = Path() + stepPathRight(path, pill, right = pill.right, bottom = pill.bottom, pullTop = pill.bottom + 6f, box = 16f, fillet = 10f, pillCorner = 12f) + val b = path.getBounds() + assertEquals(pill.left, b.left, 0.5f) + assertEquals(pill.right, b.right, 0.5f) + assertEquals(pill.bottom, b.bottom, 0.5f) + } + + @Test fun aCardMarkIsTakenOnceAndOnlyForItsGame() { + StoresMotion.markCard(CardMark("gog:1", pill, 10f, null)) + assertNull(StoresMotion.takeCard("gog:2")) + StoresMotion.markCard(CardMark("gog:1", pill, 10f, null)) + assertNotNull(StoresMotion.takeCard("gog:1")) + assertNull(StoresMotion.takeCard("gog:1")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt index 1ecd195796..b8af293269 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/StoreTabsTest.kt @@ -4,15 +4,15 @@ import com.droiddeck.launcher.stores.Store import org.junit.Assert.assertEquals import org.junit.Test -/** Amazon has no catalog of its own: Installed and Library only, and Store falls back to Library. */ +/** A store's modes are Store, Library and Installed; Amazon has no catalog of its own, so no Store. */ class StoreTabsTest { - @Test fun storeAndLibraryAmazonLibraryOnly() { - assertEquals(listOf("store", "library"), tabsFor(Store.GOG)) - assertEquals(listOf("library"), tabsFor(Store.AMAZON)) + @Test fun storeLibraryInstalledAmazonWithoutStore() { + assertEquals(listOf("store", "library", "installed"), tabsFor(Store.GOG)) + assertEquals(listOf("library", "installed"), tabsFor(Store.AMAZON)) assertEquals("library", tabFor(Store.AMAZON, "store")) - // An old saved Installed or All tab opens Library. + // An old saved All tab opens Library; Installed is a mode again. assertEquals("library", tabFor(Store.GOG, "all")) - assertEquals("library", tabFor(Store.EPIC, "installed")) + assertEquals("installed", tabFor(Store.EPIC, "installed")) assertEquals("store", tabFor(Store.GOG, "store")) } } From d99ed8e00b00246a3e718af1bf68660c622fd2de Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 9 Oct 2026 17:35:37 -0400 Subject: [PATCH 11/54] fix(desktop): address KDE feedback and keep local builds in sync with CI (#488) --- .github/workflows/build-desktop-kde.yml | 25 ++- .github/workflows/build.yml | 84 +--------- .gitignore | 1 + README.md | 6 +- app/build.gradle | 39 ++++- .../com/droiddeck/launcher/MainActivity.kt | 5 +- .../com/droiddeck/launcher/SessionActivity.kt | 6 +- .../droiddeck/launcher/input/PadBridge.java | 25 ++- .../launcher/runtime/DesktopCatalog.kt | 6 +- .../launcher/session/SessionFiles.kt | 5 + .../launcher/session/SessionService.kt | 1 + .../droiddeck/launcher/input/PadKeyTest.kt | 28 ++++ .../launcher/runtime/DesktopCatalogTest.kt | 29 ++++ tools/build_local.sh | 137 +++++---------- tools/desktop-kde/README.md | 12 ++ tools/desktop-kde/build-kwin.sh | 69 ++++++++ tools/desktop-kde/build.sh | 6 + tools/desktop-kde/kwin-build-seeds.txt | 16 ++ .../share/licenses/droiddeck-desktop/NOTICE | 5 + .../0001-respect-nested-desktop-scale.patch | 158 ++++++++++++++++++ tools/desktop-kde/release.env | 6 +- tools/docker_preflight.py | 24 +++ tools/linuxfs/check-preloads.sh | 35 ++++ .../desktop/droiddeck-clipboard.desktop | 6 + tools/linuxfs/desktop/droiddeck-desktop | 3 + tools/linuxfs/desktop/droiddeck-gpu | 2 +- .../usr/local/bin/droiddeck-desktop-bookmarks | 111 +++++++++++- .../usr/local/bin/droiddeck-desktop-clipboard | 11 ++ .../local/bin/droiddeck-desktop-window-rules | 30 ++++ .../usr/local/bin/droiddeck-gpu-window | 60 +++++++ .../overlay/usr/local/bin/droiddeck-session | 13 +- .../usr/local/bin/droiddeck-steam-desktop-ui | 33 ++++ tools/local-cross.Dockerfile | 4 + tools/proot/build.sh | 1 + tools/release/check_android_libraries.py | 57 +++++++ tools/release/check_session_assets.py | 17 +- tools/release/runtime_inputs.py | 147 ++++++++++++++++ tools/test_local.sh | 12 ++ tools/tests/test_desktop_bookmarks.py | 109 ++++++++++++ tools/tests/test_runtime_inputs.py | 76 +++++++++ tools/tests/test_steam_desktop_ui.py | 23 +++ 41 files changed, 1252 insertions(+), 191 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/input/PadKeyTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/runtime/DesktopCatalogTest.kt create mode 100644 tools/desktop-kde/build-kwin.sh create mode 100644 tools/desktop-kde/kwin-build-seeds.txt create mode 100644 tools/desktop-kde/patches/0001-respect-nested-desktop-scale.patch create mode 100644 tools/docker_preflight.py create mode 100644 tools/linuxfs/check-preloads.sh create mode 100644 tools/linuxfs/desktop/droiddeck-clipboard.desktop create mode 100644 tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-clipboard create mode 100644 tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-window-rules create mode 100755 tools/linuxfs/overlay/usr/local/bin/droiddeck-gpu-window create mode 100755 tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-desktop-ui create mode 100644 tools/release/check_android_libraries.py create mode 100644 tools/release/runtime_inputs.py create mode 100755 tools/test_local.sh create mode 100644 tools/tests/test_runtime_inputs.py create mode 100644 tools/tests/test_steam_desktop_ui.py diff --git a/.github/workflows/build-desktop-kde.yml b/.github/workflows/build-desktop-kde.yml index 6566d92bd8..9390e1af85 100644 --- a/.github/workflows/build-desktop-kde.yml +++ b/.github/workflows/build-desktop-kde.yml @@ -24,11 +24,30 @@ permissions: contents: write jobs: + kwin: + runs-on: ubuntu-24.04-arm + timeout-minutes: 90 + steps: + - uses: actions/checkout@v4 + - name: Build KWin with nested desktop scaling + run: | + sudo apt-get update -qq && sudo apt-get install -y -qq zstd xz-utils gpgv patch binutils >/dev/null + bash tools/desktop-kde/build-kwin.sh out/kwin + - uses: actions/upload-artifact@v4 + with: + name: desktop-kde-kwin + retention-days: 7 + path: out/kwin/ build: + needs: kwin runs-on: ubuntu-24.04 timeout-minutes: 90 steps: - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: desktop-kde-kwin + path: out/kwin - uses: actions/create-github-app-token@v2 id: components-token if: github.event_name == 'workflow_dispatch' && inputs.tag != '' @@ -50,7 +69,8 @@ jobs: run: | set -euo pipefail sudo apt-get update -qq && sudo apt-get install -y -qq zstd xz-utils gpgv >/dev/null - tools/desktop-kde/build.sh out + KDE_KWIN_OVERRIDE="$PWD/out/kwin" tools/desktop-kde/build.sh out + cp out/kwin/desktop-kde.kwin.txt out/ echo "size $(stat -c %s out/desktop-kde.tar.zst) sha256 $(cat out/desktop-kde.sha256)" # The package itself too, briefly: enough to try a build on a device before publishing it. - uses: actions/upload-artifact@v4 @@ -61,6 +81,7 @@ jobs: path: | out/desktop-kde.tar.zst out/desktop-kde.packages.txt + out/desktop-kde.kwin.txt out/desktop-kde.sha256 - name: Publish if: github.event_name == 'workflow_dispatch' && inputs.tag != '' @@ -76,5 +97,5 @@ jobs: fi gh release create "$TAG" --latest=false --prerelease=false --title "$TAG" \ --notes "KDE Plasma desktop package for DroidDeck's Linux runtime ($GITHUB_REPOSITORY tools/desktop-kde, $GITHUB_SHA). size $(stat -c %s out/desktop-kde.tar.zst), sha256 $(cat out/desktop-kde.sha256). desktop-kde.packages.txt lists the signature-checked Arch Linux ARM packages it was built from." \ - out/desktop-kde.tar.zst out/desktop-kde.sha256 out/desktop-kde.packages.txt + out/desktop-kde.tar.zst out/desktop-kde.sha256 out/desktop-kde.packages.txt out/desktop-kde.kwin.txt gh release edit "$TAG" --latest=false --prerelease=false diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 87df262956..1e89603968 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -276,7 +276,7 @@ jobs: if: steps.preloads.outputs.cache-hit != 'true' run: | set -euo pipefail - docker run --rm -v "$PWD:/src" -w /src debian:bullseye bash -c ' + docker run --rm -v "$PWD:/src" -w /src public.ecr.aws/docker/library/debian:bullseye bash -c ' set -euo pipefail printf "deb http://archive.debian.org/debian bullseye main\ndeb http://archive.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list apt-get -o Acquire::Check-Valid-Until=false update -qq @@ -340,8 +340,9 @@ jobs: install -m755 sink-out/module-aaudio-sink.so pa-bundle/modules/arm64/module-aaudio-sink.so install -m755 sink-out/module-directaudio-native-sink.so pa-bundle/modules/arm64/module-directaudio-native-sink.so (cd pa-bundle && tar --use-compress-program='zstd -19' -cf ../pulseaudio.tzst .) - mv pulseaudio.tzst app/src/main/assets/pulseaudio.tzst - tar --use-compress-program=unzstd -tf app/src/main/assets/pulseaudio.tzst | grep -c '\.so$' + mkdir -p app/build/prepared-assets + mv pulseaudio.tzst app/build/prepared-assets/pulseaudio.tzst + tar --use-compress-program=unzstd -tf app/build/prepared-assets/pulseaudio.tzst | grep -c '\.so$' rm -rf pa-bundle sink-out - name: Cache proot id: proot @@ -361,42 +362,7 @@ jobs: - name: Check the preloads run: | set -euo pipefail - D=app/src/main/assets/linuxfs - NEED=$(readelf -d "$D/libfakeinput.so" | sed -n 's/.*NEEDED.*\[\(.*\)\]/\1/p') - for bad in libstdc++.so.6 libgcc_s.so.1; do - if printf '%s\n' "$NEED" | grep -qx "$bad"; then - echo "::error::libfakeinput.so links $bad - the C++ runtime must stay static"; exit 1 - fi - done - syms() { readelf -Ws "$1" | awk '$4 == "FUNC" && $5 == "GLOBAL" {sub(/@.*/, "", $8); print $8}'; } - FAKE=$(syms "$D/libfakeinput.so") - for sym in open openat ioctl read close poll ppoll select stat fstat access scandir; do - grep -qx "$sym" <<<"$FAKE" || { echo "::error::libfakeinput.so does not export $sym"; exit 1; } - done - # The static C++ runtime stays private: every session process preloads this library, - # and an exported personality routine takes over other programs' unwinding (shadPS4 - # aborted in pthread_exit). - if grep -qxE '__gxx_personality_v0|__cxa_throw|_Unwind_Resume' <<<"$FAKE"; then - echo "::error::libfakeinput.so exports the C++ runtime"; exit 1 - fi - SESSION=$(syms "$D/libblsession.so") - for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap drmPrimeFDToHandle drmCloseBufferHandle; do - grep -qx "$sym" <<<"$SESSION" || { echo "::error::libblsession.so does not export $sym"; exit 1; } - done - # Zink's GEM_CLOSE is caught at ioctl(); an exported drmIoctl made NMS replay old frames. - if grep -qx drmIoctl <<<"$SESSION"; then - echo "::error::libblsession.so exports drmIoctl"; exit 1 - fi - SSBS=$(syms "$D/libssbs.so") - for sym in sigaction signal bsd_signal sysv_signal; do - grep -qx "$sym" <<<"$SSBS" || { echo "::error::libssbs.so does not export $sym"; exit 1; } - done - test -f "$D/usr/local/bin/droiddeck-session" - test -f "$D/usr/local/bin/droiddeck-proton-extra" - test -f "$D/usr/local/bin/droiddeck-msi-install" - for f in msiinfo cabextract 7z 7z.so libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do - test -f "$D/usr/local/lib/droiddeck-msitools/$f" || { echo "::error::droiddeck-msitools/$f missing"; exit 1; } - done + bash tools/linuxfs/check-preloads.sh app/src/main/assets/linuxfs # The store download engines (app/src/main/rust/stores) are Rust, built by Gradle's # buildRustStores task through cargo-ndk. rustup and cargo are on the runner image; the Android @@ -447,8 +413,10 @@ jobs: DROIDDECK_BUILD_LABEL: ${{ steps.build-label.outputs.label }} run: | set -o pipefail + python3 tools/release/runtime_inputs.py record ./gradlew assembleRelease --console=plain -PndkVersion="$NDK_VERSION" 2>&1 | tee /tmp/build.log python3 tools/release/check_session_assets.py app/build/outputs/apk/release/app-release.apk + python3 tools/release/runtime_inputs.py apk app/build/outputs/apk/release/app-release.apk - name: Check the store engines' library # The exports Kotlin binds must be in the apk's libdroiddeckstores.so, or the stores fail on @@ -457,21 +425,8 @@ jobs: run: | set -euo pipefail NDK="${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}" - NM="$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-nm" - SO=app/build/rustJniLibs/arm64-v8a/libdroiddeckstores.so - test -s "$SO" - ls -la "$SO" - unzip -l app/build/outputs/apk/release/app-release.apk | grep -q 'lib/arm64-v8a/libdroiddeckstores.so' \ - || { echo "::error::libdroiddeckstores.so is not in the apk"; exit 1; } - for sym in \ - 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_amazon_AmazonNative_nativeStart; do - "$NM" -D --defined-only "$SO" | grep -q " ${sym}\$" \ - || { echo "::error::libdroiddeckstores.so does not export $sym"; exit 1; } - done + python3 tools/release/check_android_libraries.py app/build/outputs/apk/release/app-release.apk \ + --readelf "$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-readelf" git diff --stat -- app/src/main/rust/stores/Cargo.lock git diff --exit-code -- app/src/main/rust/stores/Cargo.lock \ || { echo "::error::Cargo.lock changed during the build; commit the regenerated lockfile (uploaded as stores-cargo-lock)"; exit 1; } @@ -502,27 +457,6 @@ jobs: - name: Unit tests run: ./gradlew testReleaseUnitTest --console=plain -PndkVersion="$NDK_VERSION" - - name: Check the native dependency closure - run: | - set -euo pipefail - APK=$(find app/build/outputs/apk -name '*.apk' | head -1) - WORK=$(mktemp -d) - unzip -q "$APK" 'lib/arm64-v8a/*' -d "$WORK" - cd "$WORK/lib/arm64-v8a" - SYSTEM="libc.so libm.so libdl.so liblog.so libandroid.so libz.so libvulkan.so libstdc++.so - libGLESv2.so libEGL.so libnativewindow.so libjnigraphics.so libaaudio.so - libOpenSLES.so libmediandk.so libcamera2ndk.so libsync.so libneuralnetworks.so" - fail=0 - for so in *.so; do - for need in $(readelf -d "$so" | sed -n 's/.*NEEDED.*\[\(.*\)\]/\1/p'); do - [ -f "$need" ] && continue - case " $(echo $SYSTEM) " in *" $need "*) continue ;; esac - echo "::error::$so needs $need, which is neither in the apk nor a platform library" - fail=1 - done - done - exit $fail - - name: Sign with the testkey (v1 + v2 + v3) run: | set -euo pipefail diff --git a/.gitignore b/.gitignore index f1c4fc665c..a9e068c0d1 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ captures/ app/src/main/assets/linuxfs/ app/src/main/jniLibs/arm64-v8a/libproot.so app/src/main/jniLibs/arm64-v8a/.proot-inputs +app/src/main/jniLibs/arm64-v8a/.proot-outputs app/src/main/jniLibs/arm64-v8a/libproot-loader.so app/src/main/jniLibs/arm64-v8a/libdirectaudiorelay.so # The store download engines (app/src/main/rust/stores) are cross-compiled by Gradle into diff --git a/README.md b/README.md index 22fe4e442d..ceaef7ff73 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,11 @@ Join the [DroidDeck Discord](https://discord.gg/JRGAvawjsm) for help, Preview bu ## Build -Run `tools/build_local.sh` with Docker, Java 17, the Android SDK/NDK, and `zstd` installed. It builds the ARM64 audio sinks from PulseAudio 13.0 and packages them into the APK at `app/build/outputs/apk/release/app-release.apk`. Set `DROIDDECK_PA13_SOURCE_DIR` to an existing PulseAudio 13.0 source directory to skip downloading it. To install the APK on an attached device, run `tools/deploy_local.sh`. +Run `tools/build_local.sh` with Docker, Java 17, the Android SDK/NDK, Rust through rustup, GitHub CLI, Python 3, and `zstd` installed. It prepares the same native libraries, pinned components and audio sinks as CI, then checks the APK's assets, JNI exports and dependencies. The APK is at `app/build/outputs/apk/release/app-release.apk`. Set `DROIDDECK_PA13_SOURCE_DIR` to an existing PulseAudio 13.0 source directory to skip downloading it. Set `DROIDDECK_SIGNING_ENV` to a signing environment file to install over a release build; relative keystore paths resolve beside that file. To install on an attached device, run `tools/deploy_local.sh`. + +After preparation, ordinary `./gradlew :app:assembleRelease` builds can reuse the verified inputs. If native sources, component pins or staged files change, packaging stops with instructions to rerun the helper. Generated audio stays under `app/build/`; the tracked base bundle is never modified. `./gradlew -PskipRust=true :app:testDebugUnitTest` needs no native preparation. For an APK without store engines, use `DROIDDECK_SKIP_RUST=1 tools/build_local.sh`; it omits cached engine libraries too. + +Run the helper and preload regression suite with `bash tools/test_local.sh`. On macOS it runs in Linux through Docker, matching the helpers' runtime and CI instead of compiling Linux preloads against macOS headers. ## Limits diff --git a/app/build.gradle b/app/build.gradle index 4a1e808c9d..a69f39097e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -198,6 +198,28 @@ def stageSessionScripts = tasks.register('stageSessionScripts', Copy) { } tasks.named('preBuild').configure { dependsOn(stageSessionScripts) } +// Prepared audio lives under build/, so native builds never rewrite the tracked base bundle. +// Copying into one asset root also prevents an old base bundle winning a duplicate asset merge. +def runtimeAssetsDir = layout.buildDirectory.dir('runtimeAssets') +def stageRuntimeAssets = tasks.register('stageRuntimeAssets', Sync) { + dependsOn(stageSessionScripts) + from(layout.projectDirectory.dir('src/main/assets')) { exclude 'pulseaudio.tzst' } + from(layout.buildDirectory.dir('prepared-assets')) + into(runtimeAssetsDir) +} +android.sourceSets.main.assets.setSrcDirs([runtimeAssetsDir]) +tasks.named('preBuild').configure { dependsOn(stageRuntimeAssets) } + +// Unit tests and previews remain usable without Docker. Packaging an APK requires the complete +// runtime payload prepared by the local helper or CI, with matching sources and output hashes. +def verifyRuntimeInputs = tasks.register('verifyRuntimeInputs', Exec) { + workingDir rootProject.projectDir + commandLine 'python3', 'tools/release/runtime_inputs.py', 'verify' +} +tasks.configureEach { task -> + if (task.name in ['packageDebug', 'packageRelease']) task.dependsOn(verifyRuntimeInputs) +} + // The GOG / Epic / Amazon download engines are Rust (app/src/main/rust/stores, ported from // Bannerlator's bl-steam-client crate). cargo-ndk cross-compiles them for arm64-v8a into the build // directory, which is one more jniLibs source below: nothing compiled is committed, the same rule @@ -214,6 +236,11 @@ def buildRustStores = tasks.register('buildRustStores', Exec) { onlyIf { !skipRust } inputs.dir(rustCrateDir.dir('src')) inputs.files(rustCrateDir.file('Cargo.toml'), rustCrateDir.file('Cargo.lock')) + inputs.property('ndkVersion', android.ndkVersion) + if (!skipRust) inputs.property('rustToolchain', providers.exec { + environment 'PATH', "${System.getProperty('user.home')}/.cargo/bin${File.pathSeparator}${System.getenv('PATH')}" + commandLine 'rustup', 'run', 'stable', 'rustc', '-Vv' + }.standardOutput.asText) outputs.dir(rustJniLibsDir) workingDir rustCrateDir // The configuration cache serialises the task, so everything it needs is resolved here, not @@ -223,12 +250,18 @@ def buildRustStores = tasks.register('buildRustStores', Exec) { environment 'PATH', "${cargoBin}${File.pathSeparator}${System.getenv('PATH')}" environment 'ANDROID_NDK_HOME', android.ndkDirectory.absolutePath environment 'CARGO_TERM_COLOR', 'never' + if (!skipRust) environment 'RUSTC', providers.exec { + commandLine 'rustup', 'which', '--toolchain', 'stable', 'rustc' + }.standardOutput.asText.get().trim() // --platform 26 = minSdkVersion: the engines link against the API 26 libc, like the C++ code. - commandLine 'cargo', 'ndk', '--target', 'arm64-v8a', '--platform', '26', + // Homebrew's cargo/rustc can precede rustup on macOS and use a different sysroot, despite + // rustup reporting the Android target installed. Run both through the selected toolchain. + commandLine 'rustup', 'run', 'stable', 'cargo', 'ndk', '--target', 'arm64-v8a', '--platform', '26', '--output-dir', rustJniLibsDir.get().asFile.absolutePath, - 'build', '--release' + 'build', '--release', '--locked' } -android.sourceSets.main.jniLibs.srcDir(rustJniLibsDir) +// Skipping Rust must omit its library even if another build left a cached .so behind. +if (!skipRust) android.sourceSets.main.jniLibs.srcDir(rustJniLibsDir) tasks.named('preBuild').configure { dependsOn(buildRustStores) } dependencies { diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 9adc0cbef7..8a4e271135 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -595,11 +595,10 @@ class MainActivity : ComponentActivity() { ), FrontEndActions( onPlay = { startSteamSession() }, - // Steam's desktop client as a window on the desktop: under gamescope the - // client puts itself into Big Picture whatever it is started with. + // The client's desktop UI, with gamescope's Deck integration disabled. onPlayDesktopUi = { startSession(Intent(this, SessionActivity::class.java) - .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_DESKTOP) + .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_STEAM) .putExtra(SessionService.EXTRA_STEAM_UI, "desktop"), steamSession = true) }, // A store game may need something done first (an Epic game its sign-in code). diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index c54e26dafb..363305fa5b 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1330,7 +1330,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { return super.dispatchKeyEvent(event) } if (pipUi) return true - val fromController = event.device != null && PadBridge.isFromController(event.device) + val fromController = PadBridge.isControllerKey(event) if (fromController && event.action == KeyEvent.ACTION_DOWN) { if (drawerOpen && !drawerControllerActive) sessionOverlay.requestFocus() drawerControllerActive = true @@ -1382,11 +1382,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } return true } - if (pcKeyboardOpen && event.device != null && PadBridge.isFromController(event.device)) return super.dispatchKeyEvent(event) + if (pcKeyboardOpen && fromController) return super.dispatchKeyEvent(event) if (event.keyCode != KeyEvent.KEYCODE_BACK && padBridge?.onKeyEvent(event) == true) return true // A hardware keyboard, forwarded to the compositor's wl_keyboard. Back is left to the // activity, which opens the drawer. - val fromPad = event.device != null && PadBridge.isFromController(event.device) + val fromPad = fromController if (CompositorHost.isStarted && event.keyCode != KeyEvent.KEYCODE_BACK && !fromPad) { val down = event.action == KeyEvent.ACTION_DOWN if (down || event.action == KeyEvent.ACTION_UP) { diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java index 08328b5d3c..e7a781576b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -127,6 +127,29 @@ public final class PadBridge { || (sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK; } + /** A Bluetooth keyboard can share a device with a joystick. Classify the key, not just + * the device, so its letters and modifiers still reach the desktop keyboard. */ + public static boolean isControllerKey(KeyEvent event) { + return isFromController(event.getDevice()) + && isPadKey(event.getKeyCode(), event.getDevice().getKeyboardType()); + } + + public static boolean isPadKey(int keyCode, int keyboardType) { + if ((keyCode >= KeyEvent.KEYCODE_BUTTON_A && keyCode <= KeyEvent.KEYCODE_BUTTON_MODE) + || (keyCode >= KeyEvent.KEYCODE_BUTTON_1 && keyCode <= KeyEvent.KEYCODE_BUTTON_16)) return true; + if (keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC) return false; + switch (keyCode) { + case KeyEvent.KEYCODE_DPAD_UP: + case KeyEvent.KEYCODE_DPAD_DOWN: + case KeyEvent.KEYCODE_DPAD_LEFT: + case KeyEvent.KEYCODE_DPAD_RIGHT: + case KeyEvent.KEYCODE_BACK: + case KeyEvent.KEYCODE_HOME: + case KeyEvent.KEYCODE_MENU: return true; + default: return false; + } + } + /** Whether any real controller is attached right now. */ public static boolean anyControllerConnected() { for (int id : InputDevice.getDeviceIds()) { @@ -155,7 +178,7 @@ public final class PadBridge { /** @return true when the event was a pad button and has been consumed. */ public synchronized boolean onKeyEvent(KeyEvent event) { - if (!isFromController(event.getDevice())) return false; + if (!isControllerKey(event)) return false; noteDevice(event.getDevice()); boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN; switch (event.getKeyCode()) { diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt index 5c48fc31ca..1da9049835 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopCatalog.kt @@ -95,12 +95,14 @@ object DesktopCatalog { // The desktop package installed to the end: KWin is there and the marker install() writes once // the whole package is extracted. An install cut short (the app killed, storage full) leaves - // KWin without the marker, and the next desktop installs it again. The launcher is staged by + // KWin without the marker, and the next desktop installs it again. A newer pinned package also + // replaces the installed one, keeping the user's home and settings. The launcher is staged by // the app at every session, so it cannot tell. SessionFiles uses the same test. fun desktopInstalled(context: Context): Boolean = desktopInstalled(LinuxRuntime.rootDir(context)) fun desktopInstalled(root: File): Boolean = - File(root, "usr/bin/kwin_wayland").isFile && File(root, ".droiddeck-pkg-$DESKTOP_ID").isFile + File(root, "usr/bin/kwin_wayland").isFile && + FileUtils.readString(File(root, ".droiddeck-pkg-$DESKTOP_ID"))?.trim() == BuildConfig.DESKTOP_KDE_TAG /** Downloads, verifies and installs one package. Returns null on success, else a message. */ fun install(context: Context, entry: Entry, listener: LinuxRuntimeInstaller.ProgressListener?): String? { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index e768b6f69a..a84ebc66a5 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -48,9 +48,12 @@ object SessionFiles { "usr/local/bin/droiddeck-agent" to "usr/local/bin/droiddeck-agent", "usr/local/bin/steam-compatibility" to "usr/local/bin/steam-compatibility", "usr/local/bin/droiddeck-clipboard" to "usr/local/bin/droiddeck-clipboard", + "usr/local/bin/droiddeck-desktop-clipboard" to "usr/local/bin/droiddeck-desktop-clipboard", + "usr/local/bin/droiddeck-gpu-window" to "usr/local/bin/droiddeck-gpu-window", "usr/local/bin/droiddeck-steam-install" to "usr/local/bin/droiddeck-steam-install", "usr/local/bin/droiddeck-steam-ui-scale" to "usr/local/bin/droiddeck-steam-ui-scale", "usr/local/bin/droiddeck-steam-language" to "usr/local/bin/droiddeck-steam-language", + "usr/local/bin/droiddeck-steam-desktop-ui" to "usr/local/bin/droiddeck-steam-desktop-ui", "usr/local/bin/droiddeck-steam-library" to "usr/local/bin/droiddeck-steam-library", "usr/local/bin/droiddeck-seed-redists" to "usr/local/bin/droiddeck-seed-redists", "usr/local/bin/droiddeck-proton-extra" to "usr/local/bin/droiddeck-proton-extra", @@ -98,9 +101,11 @@ object SessionFiles { // Games and emulators from the menu, full screen in a gamescope of their own. "usr/local/bin/droiddeck-gpu" to "usr/local/bin/droiddeck-gpu", "usr/local/bin/droiddeck-desktop-gpu" to "usr/local/bin/droiddeck-desktop-gpu", + "usr/local/bin/droiddeck-desktop-window-rules" to "usr/local/bin/droiddeck-desktop-window-rules", // KWin at the app's surface size, for Plasma's session (it starts KWin through this name). "usr/local/bin/kwin_wayland_wrapper" to "usr/local/bin/kwin_wayland_wrapper", "usr/lib/firefox/defaults/pref/droiddeck.js" to "usr/lib/firefox/defaults/pref/droiddeck.js", + "etc/xdg/autostart/droiddeck-clipboard.desktop" to "etc/xdg/autostart/droiddeck-clipboard.desktop", ) // The patched gamescope (tools/gamescope): the runtime's own version rebuilt with the ARM64 // client fixes, over /usr/local/bin so it comes first in the session's PATH. Only when the diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index d563ffcf1f..aab87ddf74 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -734,6 +734,7 @@ class SessionService : Service() { // Decided only once its sysfs is in place: without it the client would find no Deck, and // with the Deck asked for the pad's own nodes are withdrawn - no controller at all. val wantsDeck = uinput && SessionState.mode == MODE_STEAM && + SessionState.steamUi != "desktop" && SessionPrefs.steamController(this) == SessionPrefs.CONTROLLER_DECK && !File(Environment.getExternalStorageDirectory(), NO_DECK_PAD_SWITCH).exists() deckBinds = if (wantsDeck) SteamDeckPad.prepare(this, fakeInputDir.parentFile!!.parentFile!!) else emptyList() diff --git a/app/src/test/java/com/droiddeck/launcher/input/PadKeyTest.kt b/app/src/test/java/com/droiddeck/launcher/input/PadKeyTest.kt new file mode 100644 index 0000000000..e14666e8f8 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/input/PadKeyTest.kt @@ -0,0 +1,28 @@ +package com.droiddeck.launcher.input + +import android.view.InputDevice +import android.view.KeyEvent +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +class PadKeyTest { + @Test fun keyboardLettersAndModifiersAreNotPadEvents() { + for (type in listOf(InputDevice.KEYBOARD_TYPE_ALPHABETIC, InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)) { + for (key in listOf(KeyEvent.KEYCODE_A, KeyEvent.KEYCODE_K, KeyEvent.KEYCODE_SHIFT_LEFT, + KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.KEYCODE_ESCAPE, KeyEvent.KEYCODE_ENTER)) { + assertFalse(PadBridge.isPadKey(key, type)) + } + } + } + + @Test fun keyboardArrowsStayWithKeyboardAndPadArrowsStayWithPad() { + assertFalse(PadBridge.isPadKey(KeyEvent.KEYCODE_DPAD_LEFT, InputDevice.KEYBOARD_TYPE_ALPHABETIC)) + assertTrue(PadBridge.isPadKey(KeyEvent.KEYCODE_DPAD_LEFT, InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)) + } + + @Test fun gamepadButtonsRemainPadEventsOnHybridDevices() { + assertTrue(PadBridge.isPadKey(KeyEvent.KEYCODE_BUTTON_A, InputDevice.KEYBOARD_TYPE_ALPHABETIC)) + assertTrue(PadBridge.isPadKey(KeyEvent.KEYCODE_BUTTON_START, InputDevice.KEYBOARD_TYPE_ALPHABETIC)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/DesktopCatalogTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/DesktopCatalogTest.kt new file mode 100644 index 0000000000..f7586544dd --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/DesktopCatalogTest.kt @@ -0,0 +1,29 @@ +package com.droiddeck.launcher.runtime + +import com.droiddeck.launcher.BuildConfig +import java.io.File +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +class DesktopCatalogTest { + @get:Rule val temporary = TemporaryFolder() + + @Test fun desktopNeedsTheCompleteCurrentPinnedPackage() { + val root = temporary.root + val kwin = File(root, "usr/bin/kwin_wayland").apply { + parentFile.mkdirs() + writeText("fixture") + } + val marker = File(root, ".droiddeck-pkg-${DesktopCatalog.DESKTOP_ID}") + assertFalse(DesktopCatalog.desktopInstalled(root)) + marker.writeText("older-package") + assertFalse(DesktopCatalog.desktopInstalled(root)) + marker.writeText("${BuildConfig.DESKTOP_KDE_TAG}\n") + assertTrue(DesktopCatalog.desktopInstalled(root)) + kwin.delete() + assertFalse(DesktopCatalog.desktopInstalled(root)) + } +} diff --git a/tools/build_local.sh b/tools/build_local.sh index cb567da7e9..646626c397 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -13,7 +13,7 @@ else fi sdk_dir=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-"${HOME}/Library/Android/sdk"}} java_dir=${JAVA_HOME:-"/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home"} -image_name=${DROIDDECK_BUILD_IMAGE:-droiddeck-local-cross:24.04-v2} +image_name=${DROIDDECK_BUILD_IMAGE:-droiddeck-local-cross:24.04-v3} build_variant=${DROIDDECK_BUILD_VARIANT:-release} case "$build_variant" in debug) gradle_task=assembleDebug ;; @@ -33,7 +33,7 @@ if ! command -v docker >/dev/null 2>&1; then echo "Docker is required to cross-compile the glibc ARM64 preload libraries." >&2 exit 1 fi -for tool in curl tar zstd shasum unzip; do +for tool in curl tar zstd shasum unzip python3; do if ! command -v "${tool}" >/dev/null 2>&1; then echo "${tool} is required to build the CI-equivalent APK." >&2 exit 1 @@ -62,24 +62,10 @@ export NDK="${sdk_dir}/ndk/${ndk_version}" staging_dir=$(mktemp -d "${TMPDIR:-/tmp}/droiddeck-build.XXXXXX") bundle_asset="${repo_root}/app/src/main/assets/pulseaudio.tzst" -bundle_backup="${staging_dir}/pulseaudio.original.tzst" -bundle_replaced=0 linuxfs_dir="${repo_root}/app/src/main/assets/linuxfs" -linuxfs_backup="${staging_dir}/linuxfs.original" -linuxfs_preexisting=0 -linuxfs_replaced=0 cleanup() { local exit_code=$? trap - EXIT - if [[ "${bundle_replaced}" == 1 ]]; then - cp -p "${bundle_backup}" "${bundle_asset}" || exit_code=1 - fi - if [[ "${linuxfs_replaced}" == 1 ]]; then - rm -rf -- "${linuxfs_dir}" || exit_code=1 - if [[ "${linuxfs_preexisting}" == 1 ]]; then - mv "${linuxfs_backup}" "${linuxfs_dir}" || exit_code=1 - fi - fi rm -rf -- "${staging_dir}" || exit_code=1 exit "${exit_code}" } @@ -87,29 +73,11 @@ trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM -if [[ -d "${linuxfs_dir}" ]]; then - cp -a "${linuxfs_dir}" "${linuxfs_backup}" - linuxfs_preexisting=1 -fi -linuxfs_replaced=1 +# A wedged daemon used to hang indefinitely and then trigger an unnecessary image rebuild. +python3 "${repo_root}/tools/docker_preflight.py" "${image_name}" rm -rf -- "${linuxfs_dir}" mkdir -p "${linuxfs_dir}" -# Docker Desktop's VM restarts now and then, and until its engine has loaded its image store it -# answers "No such image" for images it has. The rebuild that follows hangs on the registry -# (the base image's credentials go through docker-credential-desktop), so give a restarting daemon -# up to a minute before deciding the image is really missing. -image_present=0 -for _ in $(seq 1 30); do - if inspect_error=$(docker image inspect "${image_name}" 2>&1 >/dev/null); then image_present=1; break; fi - sleep 2 -done -if [[ "${image_present}" = 0 ]]; then - echo "Build image ${image_name} not found (${inspect_error:-no error}); building it." >&2 - docker build --platform linux/amd64 -t "${image_name}" \ - -f "${repo_root}/tools/local-cross.Dockerfile" "${repo_root}" -fi - docker run --rm --platform linux/amd64 \ --user "$(id -u):$(id -g)" \ -v "${repo_root}:/src" -w /src "${image_name}" bash -lc ' @@ -126,6 +94,9 @@ docker run --rm --platform linux/amd64 \ aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ -o "$d/libblfastpath.so" tools/proot/fastpath/fastpath.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$d/libblfastpath.so" + aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -pthread \ + -o "$d/libssbs.so" tools/linuxfs/ssbs/ssbs_adapter.c -ldl + aarch64-linux-gnu-strip --strip-unneeded "$d/libssbs.so" mkdir -p "$d/usr/local/bin" aarch64-linux-gnu-gcc -O2 -Wall -Wextra -o "$d/usr/local/bin/droiddeck-clipboard" tools/linuxfs/clipboard/clipboard.c -ldl aarch64-linux-gnu-strip --strip-unneeded "$d/usr/local/bin/droiddeck-clipboard" @@ -142,34 +113,11 @@ docker run --rm --platform linux/amd64 \ install -Dm644 tools/linuxfs/desktop/firefox-droiddeck.js \ "$d/usr/lib/firefox/defaults/pref/droiddeck.js" - need=$(aarch64-linux-gnu-readelf -d "$d/libfakeinput.so" | sed -n "s/.*NEEDED.*\\[\\(.*\\)\\]/\\1/p") - for bad in libstdc++.so.6 libgcc_s.so.1; do - if printf "%s\\n" "$need" | grep -qx "$bad"; then - echo "libfakeinput.so links $bad; the C++ runtime must stay static" >&2 - exit 1 - fi - done - syms() { aarch64-linux-gnu-readelf -Ws "$1" | awk '\''$4 == "FUNC" && $5 == "GLOBAL" {sub(/@.*/, "", $8); print $8}'\''; } - fake=$(syms "$d/libfakeinput.so") - for sym in open openat ioctl read close poll ppoll select stat fstat access scandir; do - printf "%s\\n" "$fake" | grep -qx "$sym" || { - echo "libfakeinput.so does not export $sym" >&2 - exit 1 - } - done - session=$(syms "$d/libblsession.so") - for sym in socket bind getsockname setsockopt statfs statvfs; do - printf "%s\\n" "$session" | grep -qx "$sym" || { - echo "libblsession.so does not export $sym" >&2 - exit 1 - } - done - test -f "$d/usr/local/bin/droiddeck-session" - test -f "$d/usr/local/bin/droiddeck-proton-extra" + READELF=aarch64-linux-gnu-readelf bash tools/linuxfs/check-preloads.sh "$d" ' docker run --rm --platform linux/amd64 \ - -v "${repo_root}:/src" -w /src debian:bullseye bash -c ' + -v "${repo_root}:/src" -w /src public.ecr.aws/docker/library/debian:bullseye bash -c ' set -euo pipefail printf "deb http://archive.debian.org/debian bullseye main\ndeb http://archive.debian.org/debian-security bullseye-security main\n" > /etc/apt/sources.list apt-get -o Acquire::Check-Valid-Until=false update -qq @@ -253,6 +201,26 @@ elif [[ -d "${sync_assets}" ]]; then echo "No tools/droiddeck-esync/release.env: the APK bundles the droiddeck-esync packs already in ${sync_assets}." >&2 fi +msi_pkgs="${staging_dir}/msi-pkgs" +msi_dir="${linuxfs_dir}/usr/local/lib/droiddeck-msitools" +mkdir -p "${msi_pkgs}" "${msi_dir}" +while read -r package_sha256 package_url; do + [[ -n "${package_url}" ]] || continue + package_archive=$(cached "${package_sha256}" "$(basename "${package_url}")" \ + bash -c 'curl -fsSL --retry 3 -o "$out" "$0"' "${package_url}") + tar -xJf "${package_archive}" -C "${msi_pkgs}" +done < <(grep -v '^#' "${repo_root}/tools/msitools/packages.txt") +for tool in msiinfo cabextract; do install -m644 "${msi_pkgs}/usr/bin/${tool}" "${msi_dir}/${tool}"; done +for tool in 7z 7z.so; do install -m644 "${msi_pkgs}/usr/lib/7zip/${tool}" "${msi_dir}/${tool}"; done +for library in libmsi-1.0.so.0 libgsf-1.so.114 libgcab-1.0.so.0; do + cp -L "${msi_pkgs}/usr/lib/${library}" "${msi_dir}/${library}" +done +mkdir -p "${linuxfs_dir}/usr/local/share/licenses/droiddeck-msitools" +for license in NOTICE GPL-2 GPL-3 LGPL-2.1 7zip-License 7zip-unRarLicense; do + install -m644 "${repo_root}/tools/msitools/licenses/${license}" \ + "${linuxfs_dir}/usr/local/share/licenses/droiddeck-msitools/${license}" +done + mango_dir="${linuxfs_dir}/usr/local/lib/mangoapp" mango_pkgs="${staging_dir}/mango-pkgs" mkdir -p "${mango_dir}" "${mango_pkgs}" @@ -295,16 +263,18 @@ sink_output="${staging_dir}/sink-out" # proot is rebuilt only when its sources (source.env, the patches, the build script) changed since # the libraries in jniLibs were built. proot_out="${repo_root}/app/src/main/jniLibs/arm64-v8a" -proot_inputs=$(cd "${repo_root}/tools/proot" && find . -type f ! -name '*.pyc' | LC_ALL=C sort | xargs shasum -a 256 | shasum -a 256 | cut -d' ' -f1) +proot_sources=$(cd "${repo_root}/tools/proot" && find . -type f ! -name '*.pyc' | LC_ALL=C sort | xargs shasum -a 256 | shasum -a 256 | cut -d' ' -f1) +proot_inputs=$(printf '%s\n' "${proot_sources}" "${ndk_version}" | shasum -a 256 | cut -d' ' -f1) if [[ -f "${proot_out}/libproot.so" && -f "${proot_out}/libproot-loader.so" \ - && "$(cat "${proot_out}/.proot-inputs" 2>/dev/null)" = "${proot_inputs}" ]]; then + && "$(cat "${proot_out}/.proot-inputs" 2>/dev/null)" = "${proot_inputs}" ]] \ + && (cd "${proot_out}" && shasum -a 256 -c .proot-outputs >/dev/null 2>&1); then echo "proot: sources unchanged, keeping ${proot_out}/libproot.so" else "${repo_root}/tools/proot/build.sh" "${proot_out}" echo "${proot_inputs}" > "${proot_out}/.proot-inputs" + (cd "${proot_out}" && shasum -a 256 libproot.so libproot-loader.so > .proot-outputs) fi -cp -p "${bundle_asset}" "${bundle_backup}" bundle_dir="${staging_dir}/pulseaudio-bundle" mkdir -p "${bundle_dir}" zstd -dc "${bundle_asset}" | tar -xf - -C "${bundle_dir}" @@ -318,8 +288,8 @@ install -m755 "${sink_output}/module-aaudio-sink.so" \ install -m755 "${sink_output}/module-directaudio-native-sink.so" \ "${bundle_dir}/modules/arm64/module-directaudio-native-sink.so" tar -cf - -C "${bundle_dir}" . | zstd -19 -T0 -c > "${staging_dir}/pulseaudio.tzst" -bundle_replaced=1 -mv "${staging_dir}/pulseaudio.tzst" "${bundle_asset}" +mkdir -p "${repo_root}/app/build/prepared-assets" +mv "${staging_dir}/pulseaudio.tzst" "${repo_root}/app/build/prepared-assets/pulseaudio.tzst" # The GOG / Epic / Amazon download engines are Rust (app/src/main/rust/stores); Gradle's # buildRustStores task cross-compiles them with cargo-ndk, which needs the Android target and the @@ -336,17 +306,17 @@ else exit 1 fi done - rustup target add aarch64-linux-android + rustup target add --toolchain stable aarch64-linux-android if ! command -v cargo-ndk >/dev/null 2>&1; then cargo install cargo-ndk --locked fi fi cd "${repo_root}" +python3 tools/release/runtime_inputs.py record ./gradlew "${gradle_task}" --console=plain -PndkVersion="${ndk_version}" ${gradle_rust_arg} python3 tools/release/check_session_assets.py "app/build/outputs/apk/${build_variant}/app-${build_variant}.apk" -cp -p "${bundle_backup}" "${bundle_asset}" -bundle_replaced=0 +python3 tools/release/runtime_inputs.py apk "app/build/outputs/apk/${build_variant}/app-${build_variant}.apk" apk="${repo_root}/app/build/outputs/apk/${build_variant}/app-${build_variant}.apk" audio_check="${staging_dir}/audio-check" @@ -362,28 +332,12 @@ for audio_file in \ fi done -docker run --rm --platform linux/amd64 -e build_variant="${build_variant}" -v "${repo_root}:/src:ro" -w /src "${image_name}" \ - bash -lc ' - set -euo pipefail - apk=app/build/outputs/apk/${build_variant}/app-${build_variant}.apk - work=$(mktemp -d) - unzip -q "$apk" "lib/arm64-v8a/*" -d "$work" - cd "$work/lib/arm64-v8a" - system="libc.so libm.so libdl.so liblog.so libandroid.so libz.so libvulkan.so libstdc++.so - libGLESv2.so libEGL.so libnativewindow.so libjnigraphics.so libaaudio.so - libOpenSLES.so libmediandk.so libcamera2ndk.so libsync.so libneuralnetworks.so" - fail=0 - for so in *.so; do - for need in $(readelf -d "$so" | sed -n "s/.*NEEDED.*\\[\\(.*\\)\\]/\\1/p"); do - [ -f "$need" ] && continue - case " $(echo $system) " in *" $need "*) continue ;; esac - echo "missing: $so -> $need" - fail=1 - done - done - [ "$fail" -eq 0 ] - echo "every NEEDED resolves" - ' +readelf_path=$(find "${NDK}/toolchains/llvm/prebuilt" -name llvm-readelf | head -1) +[[ -x "${readelf_path}" ]] || { echo "NDK llvm-readelf is missing" >&2; exit 1; } +stores_check=() +[[ "${DROIDDECK_SKIP_RUST:-0}" != 1 ]] || stores_check+=(--without-stores) +python3 tools/release/check_android_libraries.py "${apk}" --readelf "${readelf_path}" "${stores_check[@]}" +git diff --exit-code -- app/src/main/rust/stores/Cargo.lock build_tools=$(find "${sdk_dir}/build-tools" -mindepth 1 -maxdepth 1 -type d -print | sort -V | tail -1) if [[ ! -x "${build_tools}/zipalign" || ! -x "${build_tools}/apksigner" ]]; then @@ -435,6 +389,7 @@ if [[ -n "${signing_env}" ]]; then ( set -a # shellcheck disable=SC1090 + cd "$(dirname "${signing_env}")" . "${signing_env}" set +a BUILD_TOOLS="${build_tools}" "${repo_root}/tools/release/sign-apk.sh" "${apk}" standard "${apk}.release" diff --git a/tools/desktop-kde/README.md b/tools/desktop-kde/README.md index 4daa7cffef..7521692a5d 100644 --- a/tools/desktop-kde/README.md +++ b/tools/desktop-kde/README.md @@ -21,3 +21,15 @@ at every session): Plasma's own `startplasma-wayland`, with KWin nested in the a through `kwin_wayland_wrapper`. KWin composites in software (QPainter) - the Adreno node is not a DRM device - so games and emulators from the menu go through `droiddeck-gpu`, and Steam goes back to the app as a Steam session. + +`build-kwin.sh` builds KWin 6.7.5's Wayland executable and library on a native ARM64 runner, using a disposable +Arch Linux ARM chroot with every build package signature-checked. The KDE source tarball is pinned +by SHA-256 (the same digest in Arch's KWin PKGBUILD); `patches/0001-respect-nested-desktop-scale.patch` +lets Display Settings control the inner desktop scale independently of the outer window size. +It keeps layer sizes, pointer/touch coordinates and the cursor in the same physical pixels. +The behavior is enabled only with `DROIDDECK_NESTED_SCALE`, which the desktop script supplies. + +The desktop workflow replaces the same-version `kwin_wayland` executable and `libkwin.so`; the rest +comes from the signature-checked Arch packages. `desktop-kde.kwin.txt` records the source, patch, +executable, library and build package hashes. Corresponding source is +https://download.kde.org/stable/plasma/6.7.5/kwin-6.7.5.tar.xz plus the patch in this repository. diff --git a/tools/desktop-kde/build-kwin.sh b/tools/desktop-kde/build-kwin.sh new file mode 100644 index 0000000000..f86f28e4ac --- /dev/null +++ b/tools/desktop-kde/build-kwin.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# Build the pinned KWin executable with the nested-scale patch on native aarch64 Linux. +# Its dependencies come from signature-checked Arch Linux ARM packages in a disposable chroot. +set -euo pipefail +out=$(realpath -m "${1:?out dir}") +here=$(cd "$(dirname "$0")" && pwd) +[ "$(uname -m)" = aarch64 ] || { echo 'KWin needs a native aarch64 runner' >&2; exit 1; } +version=6.7.5 +source_sha=6baa910b732d93c48c90f9c1cc685cc93d0b8de0cdf138c24192c045bc3a48e2 +mirror=https://fl.us.mirror.archlinuxarm.org/aarch64 +builder=68B3537F39A313B3E574D06777193F152BDBE6A6 +work=$(mktemp -d) +trap 'sudo umount "$work/root/proc" 2>/dev/null || true; sudo rm -rf "$work"' EXIT +mkdir -p "$work/db" "$work/root" "$work/pkgs" "$out" +fetch() { curl -fsSL --proto '=https' --proto-redir '=https' --retry 6 --retry-all-errors -o "$1" "$2"; } +for repo in core extra alarm; do + fetch "$work/db/$repo.db" "$mirror/$repo/$repo.db" + mkdir -p "$work/db/x_$repo" + tar -xzf "$work/db/$repo.db" -C "$work/db/x_$repo" +done +touch "$work/runtime.txt" +python3 "$here/closure.py" "$work/db" "$work/runtime.txt" "$here/kwin-build-seeds.txt" > "$work/packages.txt" +# Refuse a repository whose KWin headers/libraries have moved beyond the pinned source. +grep -Eq '^extra/kwin-6\.7\.5-[0-9]+-aarch64\.pkg\.tar\.' "$work/packages.txt" +export work mirror builder here +download() { + local entry=$1 sum=$2 file=${1#*/} + fetch "$work/pkgs/$file" "$mirror/$entry" + fetch "$work/pkgs/$file.sig" "$mirror/$entry.sig" + gpgv --status-fd 1 --keyring "$here/archlinuxarm-builder.gpg" "$work/pkgs/$file.sig" "$work/pkgs/$file" 2>/dev/null \ + | grep -Eq "^\[GNUPG:\] VALIDSIG .* $builder\$" + echo "$sum $work/pkgs/$file" | sha256sum -c - >/dev/null +} +export -f fetch download +xargs -P 8 -n 2 bash -c 'set -euo pipefail; download "$@"' _ < "$work/packages.txt" +while read -r entry sum; do + sudo tar -xf "$work/pkgs/${entry#*/}" -C "$work/root" --no-same-owner \ + --exclude=.PKGINFO --exclude=.MTREE --exclude=.INSTALL --exclude=.BUILDINFO --exclude=.CHANGELOG +done < "$work/packages.txt" +fetch "$work/kwin.tar.xz" "https://download.kde.org/stable/plasma/$version/kwin-$version.tar.xz" +echo "$source_sha $work/kwin.tar.xz" | sha256sum -c - +sudo mkdir -p "$work/root/build" "$work/root/proc" "$work/root/dev" +sudo mknod -m 666 "$work/root/dev/null" c 1 3 +sudo mknod -m 666 "$work/root/dev/zero" c 1 5 +sudo mknod -m 666 "$work/root/dev/random" c 1 8 +sudo mknod -m 666 "$work/root/dev/urandom" c 1 9 +sudo mount -t proc proc "$work/root/proc" +sudo tar -xf "$work/kwin.tar.xz" -C "$work/root/build" +sudo patch -d "$work/root/build/kwin-$version" -p1 < "$here/patches/0001-respect-nested-desktop-scale.patch" +sudo chroot "$work/root" /usr/bin/env PATH=/usr/bin:/bin HOME=/root LC_ALL=C.UTF-8 \ + /usr/bin/cmake -S "/build/kwin-$version" -B /build/out -G Ninja \ + -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_INSTALL_LIBEXECDIR=lib -DBUILD_TESTING=OFF +sudo chroot "$work/root" /usr/bin/env PATH=/usr/bin:/bin HOME=/root LC_ALL=C.UTF-8 \ + /usr/bin/cmake --build /build/out --target kwin_wayland --parallel 4 +sudo cp "$work/root/build/out/bin/kwin_wayland" "$out/kwin_wayland" +sudo cp "$work/root/build/out/bin/libkwin.so.$version" "$out/libkwin.so.$version" +sudo chown "$(id -u):$(id -g)" "$out/kwin_wayland" "$out/libkwin.so.$version" +strip --strip-unneeded "$out/kwin_wayland" "$out/libkwin.so.$version" +# The backend lives in libkwin, not the small launcher. Catch accidentally shipping only the +# executable, which boots successfully but still loads the distribution's unpatched backend. +grep -aq DROIDDECK_NESTED_SCALE "$out/libkwin.so.$version" +{ + echo "KWin $version source sha256 $source_sha" + echo "Patch sha256 $(sha256sum "$here/patches/0001-respect-nested-desktop-scale.patch" | cut -d' ' -f1)" + echo "Executable sha256 $(sha256sum "$out/kwin_wayland" | cut -d' ' -f1)" + echo "Library sha256 $(sha256sum "$out/libkwin.so.$version" | cut -d' ' -f1)" + echo 'Build packages (Arch Linux ARM signatures verified):' + cat "$work/packages.txt" +} > "$out/desktop-kde.kwin.txt" diff --git a/tools/desktop-kde/build.sh b/tools/desktop-kde/build.sh index fccccd4e5a..b64c004824 100755 --- a/tools/desktop-kde/build.sh +++ b/tools/desktop-kde/build.sh @@ -56,6 +56,12 @@ done < "$work/pkglist.txt" cp -a "$here/overlay/." "$work/root/" +# Same-version KWin, built by build-kwin.sh with the nested desktop scale patch. +if [ -n "${KDE_KWIN_OVERRIDE:-}" ]; then + install -m 755 "$KDE_KWIN_OVERRIDE/kwin_wayland" "$work/root/usr/bin/kwin_wayland" + install -m 755 "$KDE_KWIN_OVERRIDE/libkwin.so.6.7.5" "$work/root/usr/lib/libkwin.so.6.7.5" +fi + # Weight that no desktop on a phone reads. rm -rf "$work/root/usr/share/doc" "$work/root/usr/share/man" "$work/root/usr/share/info" \ "$work/root/usr/share/gtk-doc" "$work/root/usr/include" "$work/root/usr/lib/pkgconfig" \ diff --git a/tools/desktop-kde/kwin-build-seeds.txt b/tools/desktop-kde/kwin-build-seeds.txt new file mode 100644 index 0000000000..b544ad4b0f --- /dev/null +++ b/tools/desktop-kde/kwin-build-seeds.txt @@ -0,0 +1,16 @@ +# Native build tools and KWin's Arch makedepends; dependencies are resolved and signature-checked. +kwin +base-devel +cmake +ninja +gcc +make +pkgconf +extra-cmake-modules +kdoctools +krunner +plasma-wayland-protocols +python +vulkan-headers +wayland-protocols +xorg-xwayland diff --git a/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE b/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE index c13011cd1b..23bc15cc14 100644 --- a/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE +++ b/tools/desktop-kde/overlay/usr/share/licenses/droiddeck-desktop/NOTICE @@ -8,5 +8,10 @@ Third-party files in DroidDeck's KDE desktop package (tools/desktop-kde/overlay) - steam.svg: Papirus icon theme (https://github.com/PapirusDevelopmentTeam/papirus-icon-theme), GPL-3.0. +- kwin_wayland: KWin 6.7.5, GPL-2.0-or-later. Built with DroidDeck's nested desktop scale patch. + Corresponding source: https://download.kde.org/stable/plasma/6.7.5/kwin-6.7.5.tar.xz and + https://github.com/Droid-Deck/DroidDeck/tree/fix/kde-desktop-followups/tools/desktop-kde/patches + (the source and patch hashes are recorded in desktop-kde.kwin.txt beside the package). + Everything else in the package is Arch Linux ARM's own packages, under their own licenses (/usr/share/licenses). diff --git a/tools/desktop-kde/patches/0001-respect-nested-desktop-scale.patch b/tools/desktop-kde/patches/0001-respect-nested-desktop-scale.patch new file mode 100644 index 0000000000..af8c1bcfb0 --- /dev/null +++ b/tools/desktop-kde/patches/0001-respect-nested-desktop-scale.patch @@ -0,0 +1,158 @@ +--- a/src/backends/wayland/wayland_output.cpp ++++ b/src/backends/wayland/wayland_output.cpp +@@ -294,6 +294,11 @@ + } + } + ++qreal WaylandOutput::presentationScale() const ++{ ++ return qEnvironmentVariableIsSet("DROIDDECK_NESTED_SCALE") ? m_pendingScale : scale(); ++} ++ + bool WaylandOutput::present(const QList<OutputLayer *> &layersToUpdate, const std::shared_ptr<OutputFrame> &frame) + { + auto cursorLayers = layersToUpdate | std::views::filter([](OutputLayer *layer) { +@@ -312,7 +317,7 @@ + m_surface->attachBuffer(buffer); + m_mapped = true; + } +- m_viewport->setDestination(QSize(std::round(modeSize().width() / scale()), std::round(modeSize().height() / scale()))); ++ m_viewport->setDestination(QSize(std::round(modeSize().width() / presentationScale()), std::round(modeSize().height() / presentationScale()))); + m_surface->setScale(1); + // commit the subsurfaces before the main surface + for (OutputLayer *layer : layersToUpdate) { +@@ -375,9 +380,11 @@ + next.enabled = props->enabled.value_or(m_state.enabled); + next.transform = props->transform.value_or(m_state.transform); + next.position = props->pos.value_or(m_state.position); +- // intentionally ignored, as it would get overwritten +- // with the fractional scale protocol anyways +- // next.scale = props->scale.value_or(m_state.scale); ++ // DroidDeck owns the outer window size; Display Settings owns the inner UI scale. ++ if (qEnvironmentVariableIsSet("DROIDDECK_NESTED_SCALE")) { ++ next.scale = props->scale.value_or(m_state.scale); ++ next.scaleSetting = props->scaleSetting.value_or(m_state.scaleSetting); ++ } + next.desiredMode = props->desiredMode.value_or(m_state.desiredMode); + next.uuid = props->uuid.value_or(m_state.uuid); + next.replicationSource = props->replicationSource.value_or(m_state.replicationSource); +@@ -466,7 +473,9 @@ + State next = m_state; + next.modes = {mode}; + next.currentMode = mode; +- next.scale = m_pendingScale; ++ if (!qEnvironmentVariableIsSet("DROIDDECK_NESTED_SCALE")) { ++ next.scale = m_pendingScale; ++ } + setState(next); + + Q_EMIT m_backend->outputsQueried(); +--- a/src/backends/wayland/wayland_output.h ++++ b/src/backends/wayland/wayland_output.h +@@ -93,6 +93,8 @@ + + void init(const QSize &pixelSize, qreal scale, bool fullscreen); + ++ // The outer window scale, independent of the desktop UI scale in DroidDeck. ++ qreal presentationScale() const; + bool isReady() const; + KWayland::Client::Surface *surface() const; + WaylandCursor *cursor() const; +--- a/src/backends/wayland/wayland_backend.cpp ++++ b/src/backends/wayland/wayland_backend.cpp +@@ -107,8 +107,9 @@ + WaylandOutput *output = m_seat->backend()->findOutput(m_pointer->enteredSurface()); + Q_ASSERT(output); + const auto subsurface = m_seat->backend()->findSubSurface(m_pointer->enteredSurface()); +- const QPointF absolutePos = output->position() + relativeToSurface +- + (subsurface ? subsurface->position() : QPoint()); ++ const QPointF absolutePos = output->position() ++ + (relativeToSurface + (subsurface ? subsurface->position() : QPoint())) ++ * output->presentationScale() / output->scale(); + Q_EMIT pointerMotionAbsolute(absolutePos, std::chrono::milliseconds(time), this); + }); + connect(pointer, &Pointer::buttonStateChanged, this, [this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState nativeState) { +@@ -182,7 +183,11 @@ + , m_relativePointer(relativePointer) + { + connect(relativePointer, &RelativePointer::relativeMotion, this, [this](const QSizeF &delta, const QSizeF &deltaNonAccelerated, quint64 timestamp) { +- Q_EMIT pointerMotion(sizeToPoint(delta), sizeToPoint(deltaNonAccelerated), std::chrono::microseconds(timestamp), this); ++ const auto pointer = m_seat->pointerDevice(); ++ const auto output = pointer && pointer->m_pointer ++ ? m_seat->backend()->findOutput(pointer->m_pointer->enteredSurface()) : nullptr; ++ const qreal factor = output ? output->presentationScale() / output->scale() : 1.0; ++ Q_EMIT pointerMotion(sizeToPoint(delta) * factor, sizeToPoint(deltaNonAccelerated) * factor, std::chrono::microseconds(timestamp), this); + }); + } + +@@ -199,13 +204,19 @@ + connect(touch, &Touch::sequenceStarted, this, [this](TouchPoint *tp) { + auto o = m_seat->backend()->findOutput(tp->surface()); + Q_ASSERT(o); +- const QPointF position = o->position() + tp->position(); ++ const auto subsurface = m_seat->backend()->findSubSurface(tp->surface()); ++ const QPointF position = o->position() ++ + (tp->position() + (subsurface ? subsurface->position() : QPoint())) ++ * o->presentationScale() / o->scale(); + Q_EMIT touchDown(tp->id(), position, std::chrono::milliseconds(tp->time()), this); + }); + connect(touch, &Touch::pointAdded, this, [this](TouchPoint *tp) { + auto o = m_seat->backend()->findOutput(tp->surface()); + Q_ASSERT(o); +- const QPointF position = o->position() + tp->position(); ++ const auto subsurface = m_seat->backend()->findSubSurface(tp->surface()); ++ const QPointF position = o->position() ++ + (tp->position() + (subsurface ? subsurface->position() : QPoint())) ++ * o->presentationScale() / o->scale(); + Q_EMIT touchDown(tp->id(), position, std::chrono::milliseconds(tp->time()), this); + }); + connect(touch, &Touch::pointRemoved, this, [this](TouchPoint *tp) { +@@ -214,7 +225,10 @@ + connect(touch, &Touch::pointMoved, this, [this](TouchPoint *tp) { + auto o = m_seat->backend()->findOutput(tp->surface()); + Q_ASSERT(o); +- const QPointF position = o->position() + tp->position(); ++ const auto subsurface = m_seat->backend()->findSubSurface(tp->surface()); ++ const QPointF position = o->position() ++ + (tp->position() + (subsurface ? subsurface->position() : QPoint())) ++ * o->presentationScale() / o->scale(); + Q_EMIT touchMotion(tp->id(), position, std::chrono::milliseconds(tp->time()), this); + }); + } +--- a/src/backends/wayland/wayland_layer.cpp ++++ b/src/backends/wayland/wayland_layer.cpp +@@ -95,7 +95,7 @@ + // this is a bit annoying, we need a new Wayland protocol + // to do this properly with fractional scaling. Until we + // have that, it may cause blurriness in some cases! +- const Rect logicalTarget = targetRect().scaled(1.0 / m_output->scale()).rounded(); ++ const Rect logicalTarget = targetRect().scaled(1.0 / output->presentationScale()).rounded(); + if (m_viewport) { + m_viewport->setSource(sourceRect()); + m_viewport->setDestination(logicalTarget.size()); +--- a/src/backends/wayland/wayland_qpainter_backend.cpp ++++ b/src/backends/wayland/wayland_qpainter_backend.cpp +@@ -124,7 +124,9 @@ + wl_buffer *buffer = static_cast<WaylandOutput *>(m_output.get())->backend()->importBuffer(m_back->buffer()); + Q_ASSERT(buffer); + +- static_cast<WaylandOutput *>(m_output.get())->cursor()->update(buffer, m_back->buffer()->size() / m_output->scale(), hotspot().toPoint()); ++ auto output = static_cast<WaylandOutput *>(m_output.get()); ++ output->cursor()->update(buffer, m_back->buffer()->size() / output->presentationScale(), ++ (hotspot() * output->scale() / output->presentationScale()).toPoint()); + m_swapchain->release(m_back); + return true; + } +--- a/src/backends/wayland/wayland_egl_backend.cpp ++++ b/src/backends/wayland/wayland_egl_backend.cpp +@@ -202,7 +202,9 @@ + wl_buffer *buffer = m_backend->backend()->importBuffer(m_buffer->buffer()); + Q_ASSERT(buffer); + +- static_cast<WaylandOutput *>(m_output.get())->cursor()->update(buffer, m_buffer->buffer()->size() / m_output->scale(), (hotspot() / m_output->scale()).toPoint()); ++ auto output = static_cast<WaylandOutput *>(m_output.get()); ++ output->cursor()->update(buffer, m_buffer->buffer()->size() / output->presentationScale(), ++ (hotspot() / output->presentationScale()).toPoint()); + + EGLNativeFence releaseFence{m_backend->eglDisplayObject()}; + m_swapchain->release(m_buffer, releaseFence.takeFileDescriptor()); diff --git a/tools/desktop-kde/release.env b/tools/desktop-kde/release.env index 22a241e4be..7151b33260 100644 --- a/tools/desktop-kde/release.env +++ b/tools/desktop-kde/release.env @@ -1,4 +1,4 @@ DESKTOP_KDE_REPO=Droid-Deck/DroidDeck-Components -DESKTOP_KDE_TAG=desktop-kde-r1 -DESKTOP_KDE_SHA256=55747e75d0ec055ab7ea1e92a757f3be227cc3205bb9101f5b53cadef6318579 -DESKTOP_KDE_SIZE=846370256 +DESKTOP_KDE_TAG=desktop-kde-r2 +DESKTOP_KDE_SHA256=9ba832677b498654136c69d8c8330434d17e4d53cd099acac9bde3ba429b35bf +DESKTOP_KDE_SIZE=846933224 diff --git a/tools/docker_preflight.py b/tools/docker_preflight.py new file mode 100644 index 0000000000..b4e9232755 --- /dev/null +++ b/tools/docker_preflight.py @@ -0,0 +1,24 @@ +"""Check the engine before modifying build assets; never rebuild for a daemon error.""" +from pathlib import Path +import subprocess +import sys + + +def run(*args): + try: + return subprocess.run(['docker', *args], capture_output=True, text=True, timeout=15) + except subprocess.TimeoutExpired: + sys.exit('Docker is not responding. Restart Docker Desktop, then rerun tools/build_local.sh.') + + +if __name__ == '__main__': + engine = run('info', '--format', '{{.ServerVersion}}') + if engine.returncode: + sys.exit('Docker is unavailable: ' + engine.stderr.strip()) + image = run('image', 'inspect', sys.argv[1]) + if image.returncode: + if 'No such image' not in image.stderr: + sys.exit('Cannot inspect the build image: ' + image.stderr.strip()) + repo = Path(__file__).resolve().parents[1] + subprocess.run(['docker', 'build', '--platform', 'linux/amd64', '-t', sys.argv[1], + '-f', str(repo / 'tools/local-cross.Dockerfile'), str(repo)], check=True) diff --git a/tools/linuxfs/check-preloads.sh b/tools/linuxfs/check-preloads.sh new file mode 100644 index 0000000000..6e9e1dcff6 --- /dev/null +++ b/tools/linuxfs/check-preloads.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Shared native ABI checks for CI and local builds. +set -euo pipefail +D=${1:?asset directory} +NEED=$("${READELF:-readelf}" -d "$D/libfakeinput.so" | sed -n 's/.*NEEDED.*\[\(.*\)\]/\1/p') +for bad in libstdc++.so.6 libgcc_s.so.1; do + if printf '%s\n' "$NEED" | grep -qx "$bad"; then + echo "::error::libfakeinput.so links $bad - the C++ runtime must stay static"; exit 1 + fi +done +syms() { "${READELF:-readelf}" -Ws "$1" | awk '$4 == "FUNC" && $5 == "GLOBAL" {sub(/@.*/, "", $8); print $8}'; } +FAKE=$(syms "$D/libfakeinput.so") +for sym in open openat ioctl read close poll ppoll select stat fstat access scandir; do + grep -qx "$sym" <<<"$FAKE" || { echo "::error::libfakeinput.so does not export $sym"; exit 1; } +done +# The static C++ runtime stays private: every session process preloads this library, +# and an exported personality routine takes over other programs' unwinding (shadPS4 +# aborted in pthread_exit). +if grep -qxE '__gxx_personality_v0|__cxa_throw|_Unwind_Resume' <<<"$FAKE"; then + echo "::error::libfakeinput.so exports the C++ runtime"; exit 1 +fi +SESSION=$(syms "$D/libblsession.so") +for sym in socket bind getsockname setsockopt statfs statvfs syscall shm_open shm_unlink mmap mmap64 munmap drmPrimeFDToHandle drmCloseBufferHandle; do + grep -qx "$sym" <<<"$SESSION" || { echo "::error::libblsession.so does not export $sym"; exit 1; } +done +# Zink's GEM_CLOSE is caught at ioctl(); an exported drmIoctl made NMS replay old frames. +if grep -qx drmIoctl <<<"$SESSION"; then + echo "::error::libblsession.so exports drmIoctl"; exit 1 +fi +SSBS=$(syms "$D/libssbs.so") +for sym in sigaction signal bsd_signal sysv_signal; do + grep -qx "$sym" <<<"$SSBS" || { echo "::error::libssbs.so does not export $sym"; exit 1; } +done +test -f "$D/usr/local/bin/droiddeck-session" +test -f "$D/usr/local/bin/droiddeck-proton-extra" diff --git a/tools/linuxfs/desktop/droiddeck-clipboard.desktop b/tools/linuxfs/desktop/droiddeck-clipboard.desktop new file mode 100644 index 0000000000..2708364836 --- /dev/null +++ b/tools/linuxfs/desktop/droiddeck-clipboard.desktop @@ -0,0 +1,6 @@ +[Desktop Entry] +Type=Application +Name=Android clipboard +Exec=/usr/local/bin/droiddeck-desktop-clipboard +OnlyShowIn=KDE; +NoDisplay=true diff --git a/tools/linuxfs/desktop/droiddeck-desktop b/tools/linuxfs/desktop/droiddeck-desktop index c955251843..6fb8c1a723 100644 --- a/tools/linuxfs/desktop/droiddeck-desktop +++ b/tools/linuxfs/desktop/droiddeck-desktop @@ -38,6 +38,7 @@ export MOZ_SHM_NO_SEALS=1 # to the app as a Steam session - SteamOS's "Return to Gaming Mode". The session's own GL choice # (zink on Turnip, for the Steam client) is put back by droiddeck-gpu. export KWIN_COMPOSE=Q QT_QUICK_BACKEND=software LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe +export DROIDDECK_NESTED_SCALE=1 export DROIDDECK_GLTHREAD=${mesa_glthread:-} unset MESA_LOADER_DRIVER_OVERRIDE LIBGL_KOPPER_DRI2 mesa_glthread # The desktop's own log, beside the session's, so KWin and the programs on it can be read apart @@ -72,6 +73,8 @@ python3 /usr/local/bin/droiddeck-pad-defaults 2>&1 | sed 's/^/[pads] /' # Games and emulators from the menu run through droiddeck-gpu; BL_DESKTOP_GPU=on/off overrides, # and each entry keeps "Run in a desktop window" either way. python3 /usr/local/bin/droiddeck-desktop-gpu 2>&1 | sed 's/^/[gpu] /' +# The windowed action still opens melonDS at the size/placement the LXQt desktop used. +/usr/local/bin/droiddeck-desktop-window-rules 2>&1 | sed 's/^/[rules] /' # SteamOS's desktop icons: Return to Gaming Mode and Steam, put there once - what the user moves # or deletes stays that way. desktop_dir=$(xdg-user-dir DESKTOP 2>/dev/null); [ -n "$desktop_dir" ] && [ "$desktop_dir" != "$HOME" ] || desktop_dir=$HOME/Desktop diff --git a/tools/linuxfs/desktop/droiddeck-gpu b/tools/linuxfs/desktop/droiddeck-gpu index 7f5b29432b..e68f551782 100644 --- a/tools/linuxfs/desktop/droiddeck-gpu +++ b/tools/linuxfs/desktop/droiddeck-gpu @@ -31,7 +31,7 @@ export MESA_LOADER_DRIVER_OVERRIDE=zink GALLIUM_DRIVER=zink LIBGL_KOPPER_DRI2=tr [ -n "${DROIDDECK_GLTHREAD:-}" ] && export mesa_glthread=$DROIDDECK_GLTHREAD # AppImages cannot mount under proot; the menu entries already say so, a hand-typed one may not. export APPIMAGE_EXTRACT_AND_RUN=1 -gamescope "${args[@]}" -- "$@" +gamescope "${args[@]}" -- /usr/local/bin/droiddeck-gpu-window "$@" rc=$? echo "== droiddeck-gpu $(date -u +%H:%M:%S): exited $rc" exit $rc diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-bookmarks b/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-bookmarks index 0172e12fec..865f7e3b68 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-bookmarks +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-bookmarks @@ -1,13 +1,114 @@ #!/usr/bin/env python3 -"""Expose the selected secondary library in PCManFM's Places sidebar.""" +"""Expose available Steam libraries in Dolphin, retaining the old PCManFM bookmark.""" +import hashlib +import json import os from pathlib import Path +import re +import runpy import sys import tempfile +from urllib.parse import unquote, urlsplit +import xml.etree.ElementTree as ET LIBRARY = Path("/mnt/droiddeck-sd") URI = "file:///mnt/droiddeck-sd" +KDE_OWNER = "http://www.kde.org" +MARK = "DroidDeckSteamLibrary" +ICON_NS = "http://www.freedesktop.org/standards/desktop-bookmarks" +ET.register_namespace("bookmark", ICON_NS) + + +def steam_libraries(home, data): + steam = data / "Steam" + libraries = {steam: "Internal Storage"} + listing = os.environ.get("BL_STEAM_LIBRARIES") + selected = None + if listing: + try: + selected = {Path(e["path"]): str(e["label"]) for e in json.loads(Path(listing).read_text())} + except (OSError, ValueError, KeyError, TypeError): + pass # Fall back to the client's registered libraries if the app's list is unreadable. + vdf = steam / "steamapps/libraryfolders.vdf" + if vdf.is_file(): + parser = runpy.run_path(str(Path(__file__).with_name("droiddeck-steam-ui-scale"))) + folders = parser["parse"](vdf.read_text()).get("libraryfolders") + if not isinstance(folders, parser["Block"]): + raise ValueError("missing libraryfolders") + for _, entry in folders.pairs: + if not isinstance(entry, parser["Block"]): + continue + path = entry.get("path") + if not isinstance(path, str): + continue + path = Path(re.sub(r'\\(["\\])', r'\1', path)) + managed = path == LIBRARY or path.is_relative_to(home / "Games") + if selected is not None and managed and path not in selected: + continue + if path == LIBRARY and selected is None and not os.environ.get("BL_LIBRARY_LABEL"): + continue + label = entry.get("label") + label = re.sub(r'\\(["\\])', r'\1', label) if isinstance(label, str) else "" + libraries[path] = label or path.name + if selected is not None: + libraries.update(selected) + elif os.environ.get("BL_LIBRARY_LABEL"): + libraries[LIBRARY] = os.environ["BL_LIBRARY_LABEL"] + libraries[steam] = "Internal Storage" + return {path.as_uri(): "Steam (" + (" ".join(label.split()) or path.name) + ")" + for path, label in libraries.items() if path.is_absolute() and (path / "steamapps").is_dir()} + + +def place_uri(uri): + url = urlsplit(uri or "") + if url.scheme == "file" and url.netloc in ("", "localhost") and url.path.startswith("/"): + return Path(unquote(url.path)).as_uri() + return uri + + +def update_places(path, libraries): + if path.is_file(): + parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True, insert_pis=True)) + root = ET.fromstring(path.read_bytes(), parser=parser) + if root.tag != "xbel": + raise ValueError("invalid Dolphin places file") + else: + if not libraries: + return + # Leave KDE's file version unset so it still adds its normal Home/Downloads/etc. places. + root = ET.Element("xbel", {"version": "1.0"}) + before = ET.tostring(root) + seen = set() + personal = {place_uri(b.get("href")) for b in root.findall("bookmark") + if b.find("info/metadata[@owner='" + KDE_OWNER + "']/" + MARK) is None} + for bookmark in root.findall("bookmark"): + metadata = bookmark.find("info/metadata[@owner='" + KDE_OWNER + "']") + if metadata is None or metadata.find(MARK) is None: + continue + uri = place_uri(bookmark.get("href")) + if uri not in libraries or uri in seen or uri in personal: + root.remove(bookmark) + continue + seen.add(uri) + title = bookmark.find("title") + if title is None: + title = ET.SubElement(bookmark, "title") + title.text = libraries[uri] + for uri, label in libraries.items(): + if uri in seen or uri in personal: + continue + bookmark = ET.SubElement(root, "bookmark", {"href": uri}) + ET.SubElement(bookmark, "title").text = label + info = ET.SubElement(bookmark, "info") + icon = ET.SubElement(info, "metadata", {"owner": "http://freedesktop.org"}) + ET.SubElement(icon, "{" + ICON_NS + "}icon", {"name": "steam"}) + metadata = ET.SubElement(info, "metadata", {"owner": KDE_OWNER}) + ET.SubElement(metadata, "ID").text = "droiddeck-steam-" + hashlib.sha256(uri.encode()).hexdigest()[:16] + ET.SubElement(metadata, MARK).text = "true" + ET.SubElement(metadata, "isSystemItem").text = "false" + if ET.tostring(root) != before: + atomic_write(path, ET.tostring(root, encoding="unicode", xml_declaration=True)) def bookmarks_file(home, config): @@ -31,6 +132,10 @@ def update_bookmark(path, label): content += URI + " " + label + "\n" if content == original: return + atomic_write(path, content) + + +def atomic_write(path, content): path.parent.mkdir(parents=True, exist_ok=True) temporary = None try: @@ -51,10 +156,12 @@ def main(): # An unbound mount-point directory may remain from a previous session. available = bool(label) and (LIBRARY / "steamapps").is_dir() update_bookmark(bookmarks_file(home, config), label if available else None) + data = Path(os.environ.get("XDG_DATA_HOME") or home / ".local/share") + update_places(data / "user-places.xbel", steam_libraries(home, data)) if __name__ == "__main__": try: main() - except OSError as error: + except (OSError, ValueError, ET.ParseError) as error: print("== desktop library bookmark: " + str(error), file=sys.stderr) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-clipboard b/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-clipboard new file mode 100644 index 0000000000..0de5aff202 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-clipboard @@ -0,0 +1,11 @@ +#!/bin/bash +# Plasma starts this after its nested Xwayland display is available. The session launcher still +# sees the outer compositor before startplasma, so starting its X11 helper there cannot work. +[ -n "${BL_ANDROID_CLIPBOARD:-}" ] && [ -n "${DISPLAY:-}" ] || exit 0 +# The native helper dies with its parent. Keep this shell until it exits: Plasma's autostart +# launcher may itself exit immediately after starting us. +/usr/local/bin/droiddeck-clipboard "$BL_ANDROID_CLIPBOARD" & +child=$! +trap 'kill "$child" 2>/dev/null' EXIT +trap 'exit 0' TERM INT +wait "$child" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-window-rules b/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-window-rules new file mode 100644 index 0000000000..df6527ecd9 --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-desktop-window-rules @@ -0,0 +1,30 @@ +#!/bin/bash +# Port the old desktop's melonDS window rule once; later window rules belong to the user. +set -euo pipefail +config=${XDG_CONFIG_HOME:-$HOME/.config} +marker=$config/droiddeck-window-rules-v1 +[ ! -e "$marker" ] || exit 0 +rules=$(kreadconfig6 --file kwinrulesrc --group General --key rules) +IFS=, read -ra groups <<< "$rules" +for group in "${groups[@]}"; do + title=$(kreadconfig6 --file kwinrulesrc --group "$group" --key title) + wmclass=$(kreadconfig6 --file kwinrulesrc --group "$group" --key wmclass) + case ${title,,}${wmclass,,} in + *melonds*) mkdir -p "$config"; touch "$marker"; exit 0 ;; + esac +done +write() { kwriteconfig6 --file kwinrulesrc --group droiddeck-melonds --key "$1" "$2"; } +write Description 'melonDS desktop window' +write title '^melonDS.*' +write titlematch 3 +write types 1 +write size 800,600 +write sizerule 3 +write placement Centered +write placementrule 2 +write ignoregeometry true +write ignoregeometryrule 3 +kwriteconfig6 --file kwinrulesrc --group General --key rules "${rules:+$rules,}droiddeck-melonds" +kwriteconfig6 --file kwinrulesrc --group General --key count "$(( ${#groups[@]} + 1 ))" +mkdir -p "$config" +touch "$marker" diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-gpu-window b/tools/linuxfs/overlay/usr/local/bin/droiddeck-gpu-window new file mode 100755 index 0000000000..42a01f274f --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-gpu-window @@ -0,0 +1,60 @@ +#!/usr/bin/python3 +"""Let GTK draw close buttons inside gamescope, which supplies no window decorations.""" +import ctypes as c +import os +import signal +import subprocess +import sys + + +def advertise_compositing(): + # This is gamescope's private X server: it composites alpha buffers already, but GTK also + # requires the compositing-manager selection and frame-extents hint before enabling CSD. + x = c.CDLL('libX11.so.6') + display_type, window_type = c.c_void_p, c.c_ulong + x.XOpenDisplay.argtypes = [c.c_char_p] + x.XOpenDisplay.restype = display_type + display = x.XOpenDisplay(None) + if not display: + raise RuntimeError('no X display') + x.XDefaultRootWindow.argtypes = [display_type] + x.XDefaultRootWindow.restype = window_type + x.XDefaultScreen.argtypes = [display_type] + x.XDefaultScreen.restype = c.c_int + root = x.XDefaultRootWindow(display) + x.XInternAtom.argtypes = [display_type, c.c_char_p, c.c_int] + x.XInternAtom.restype = window_type + atom = lambda name: x.XInternAtom(display, name.encode(), False) + manager = atom('_NET_WM_CM_S%d' % x.XDefaultScreen(display)) + x.XGetSelectionOwner.argtypes = [display_type, window_type] + x.XGetSelectionOwner.restype = window_type + if not x.XGetSelectionOwner(display, manager): + x.XCreateSimpleWindow.argtypes = [display_type, window_type, c.c_int, c.c_int, + c.c_uint, c.c_uint, c.c_uint, window_type, window_type] + x.XCreateSimpleWindow.restype = window_type + owner = x.XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0) + x.XSetSelectionOwner.argtypes = [display_type, window_type, window_type, window_type] + x.XSetSelectionOwner(display, manager, owner, 0) + x.XChangeProperty.argtypes = [display_type, window_type, window_type, window_type, + c.c_int, c.c_int, c.c_void_p, c.c_int] + extents = window_type(atom('_GTK_FRAME_EXTENTS')) + x.XChangeProperty(display, root, atom('_NET_SUPPORTED'), atom('ATOM'), 32, 2, c.byref(extents), 1) + x.XSync.argtypes = [display_type, c.c_int] + x.XSync(display, False) + # Keep the selection's connection alive until the program exits. + return x, display + + +if __name__ == '__main__': + if len(sys.argv) < 2: + raise SystemExit('usage: droiddeck-gpu-window <program> [args...]') + os.environ.setdefault('GTK_CSD', '1') + try: + connection = advertise_compositing() + except (OSError, RuntimeError): + os.execvp(sys.argv[1], sys.argv[1:]) + child = subprocess.Popen(sys.argv[1:]) + for sig in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP): + signal.signal(sig, lambda number, frame: child.send_signal(number)) + status = child.wait() + raise SystemExit(status if status >= 0 else 128 - status) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 9a51694df6..419290c38b 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -214,7 +214,9 @@ if [ -z "${BL_INSIDE:-}" ]; then elif [ "${BL_REFRESH:-0}" -gt 0 ]; then args+=(-r "$BL_REFRESH") fi - [ "$mode" = steam ] && args+=(-e) + # Steam integration advertises a Deck session and makes the client select Big Picture even + # without -gamepadui. The desktop UI uses ordinary gamescope window management instead. + [ "$mode" = steam ] && [ "${BL_STEAM_UI:-}" != desktop ] && args+=(-e) # Two Xwayland servers, as SteamOS runs Steam: the client on the first, games on the second # (STEAM_GAME_DISPLAY_0). The client then sizes the games' server to each game's Game Resolution # (GAMESCOPE_XWAYLAND_MODE_CONTROL; native unless one is set), and gamescope scales it to the @@ -886,10 +888,19 @@ BUSCONF droiddeck-steam-language "$HOME" "$BL_STEAM_LANGUAGE" || true fi step "starting the Steam client${BL_STEAM_UI:+ ($BL_STEAM_UI UI)}" + desktop_ui_pid= + if [ "${BL_STEAM_UI:-}" = desktop ]; then + /usr/local/bin/droiddeck-steam-desktop-ui & + desktop_ui_pid=$! + fi "$steam_root/steamrtarm64/steam" "${_bl_ui[@]}" "${_bl_cdp[@]}" "${_bl_lang[@]}" -clientbeta "$steam_channel" \ -no-cef-sandbox -cef-force-gpu -cef-ozone-platform=x11 \ -cef-use-gl=angle -cef-use-angle=vulkan "$@" rc=$? + if [ -n "$desktop_ui_pid" ]; then + kill "$desktop_ui_pid" 2>/dev/null || true + wait "$desktop_ui_pid" 2>/dev/null || true + fi echo "== steam exited rc=$rc" if [ -n "${BL_ADDED_GAMES:-}" ] && [ -f "$BL_ADDED_GAMES" ]; then droiddeck-steam-shortcuts "$steam_root" "$BL_ADDED_GAMES" || true diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-desktop-ui b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-desktop-ui new file mode 100755 index 0000000000..6eda34131a --- /dev/null +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-steam-desktop-ui @@ -0,0 +1,33 @@ +#!/usr/bin/python3 +"""Honor a desktop launch even when Steam remembers Big Picture from its last session.""" +import os +from pathlib import Path +import runpy +import time + + +def select_desktop(evaluate, timeout=90): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + result = evaluate("typeof SteamClient !== 'undefined' && " + "typeof SteamClient.UI.ExitBigPictureMode === 'function' && " + "(SteamClient.UI.ExitBigPictureMode(), true)") + if result.get('value') is True: + return True + except Exception: + # CEF and SharedJSContext are not ready during the client's initial startup/update. + pass + time.sleep(1) + return False + + +if __name__ == '__main__': + base = os.environ.get('BL_LAUNCH_DIR') + if not base: + raise SystemExit('BL_LAUNCH_DIR is not set') + agent = runpy.run_path('/usr/local/bin/droiddeck-agent') + selected = select_desktop(lambda expression: agent['cdp_eval']( + Path(base) / 'agent', expression, timeout=2)) + print('== steam: desktop UI selected' if selected else + '== steam: desktop UI was not ready within 90 seconds', flush=True) diff --git a/tools/local-cross.Dockerfile b/tools/local-cross.Dockerfile index ccfd37f760..8d143e7caa 100644 --- a/tools/local-cross.Dockerfile +++ b/tools/local-cross.Dockerfile @@ -6,8 +6,12 @@ RUN apt-get update \ && apt-get install -y \ binutils \ binutils-aarch64-linux-gnu \ + gcc \ + g++ \ gcc-aarch64-linux-gnu \ g++-aarch64-linux-gnu \ + python3 \ + zstd \ unzip \ && mkdir -p /src diff --git a/tools/proot/build.sh b/tools/proot/build.sh index 897e2e0172..a899370fd9 100755 --- a/tools/proot/build.sh +++ b/tools/proot/build.sh @@ -49,6 +49,7 @@ PATH="$WORK/bin:$PATH" make -C "$WORK/proot/src" -j"$(getconf _NPROCESSORS_ONLN) install -m755 "$WORK/proot/src/proot" "$OUTDIR/libproot.so" install -m755 "$WORK/proot/src/loader/loader" "$OUTDIR/libproot-loader.so" "$TOOLCHAIN/llvm-strip" --strip-unneeded "$OUTDIR/libproot.so" +"$TOOLCHAIN/llvm-strip" --strip-unneeded "$OUTDIR/libproot-loader.so" grep -q '^#define HAVE_SECCOMP_FILTER' "$WORK/proot/src/build.h" || { echo "ERROR: proot was built without seccomp acceleration"; exit 1; } grep -q '^#define HAVE_PROCESS_VM' "$WORK/proot/src/build.h" || { echo "ERROR: proot was built without process_vm"; exit 1; } NEEDED=$("$TOOLCHAIN/llvm-readelf" -d "$OUTDIR/libproot.so" | sed -n 's/.*NEEDED.*\[\(.*\)\]/\1/p' | tr '\n' ' ') diff --git a/tools/release/check_android_libraries.py b/tools/release/check_android_libraries.py new file mode 100644 index 0000000000..765e226acc --- /dev/null +++ b/tools/release/check_android_libraries.py @@ -0,0 +1,57 @@ +"""Validate the JNI entry points and Android ELF dependency closure in the actual APK.""" +import argparse +from pathlib import Path +import re +import subprocess +import tempfile +from zipfile import ZipFile + + +SYSTEM = set('libc.so libm.so libdl.so liblog.so libandroid.so libz.so libvulkan.so libstdc++.so ' + 'libGLESv2.so libEGL.so libnativewindow.so libjnigraphics.so libaaudio.so ' + 'libOpenSLES.so libmediandk.so libcamera2ndk.so libsync.so libneuralnetworks.so'.split()) +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_amazon_AmazonNative_nativeStart'] + + +def check(apk, readelf, stores=True): + errors = [] + with tempfile.TemporaryDirectory() as temp, ZipFile(apk) as package: + libraries = {} + for name in package.namelist(): + if name.startswith('lib/arm64-v8a/') and name.endswith('.so'): + path = Path(temp) / Path(name).name + path.write_bytes(package.read(name)) + libraries[path.name] = path + for name, path in libraries.items(): + dynamic = subprocess.check_output([readelf, '-d', str(path)], text=True) + for dependency in re.findall(r'NEEDED.*\[(.*?)\]', dynamic): + if dependency not in libraries and dependency not in SYSTEM: + errors.append(name + ' needs missing ' + dependency) + library = libraries.get('libdroiddeckstores.so') + if stores: + if library is None: + errors.append('APK missing libdroiddeckstores.so') + else: + symbols = subprocess.check_output([readelf, '--dyn-syms', '--wide', str(library)], text=True) + exports = {line.split()[-1] for line in symbols.splitlines() + if ' GLOBAL ' in line and ' UND ' not in line and line.split()} + errors += ['Store library missing ' + symbol for symbol in EXPORTS if symbol not in exports] + elif library is not None: + errors.append('Rust was skipped but the APK contains a cached store library') + return errors + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('apk') + parser.add_argument('--readelf', default='readelf') + parser.add_argument('--without-stores', action='store_true') + args = parser.parse_args() + errors = check(args.apk, args.readelf, not args.without_stores) + if errors: + parser.exit(1, '\n'.join(errors) + '\n') + print('APK native exports and dependencies verified') diff --git a/tools/release/check_session_assets.py b/tools/release/check_session_assets.py index 8b3b81ca79..89d05f01b3 100644 --- a/tools/release/check_session_assets.py +++ b/tools/release/check_session_assets.py @@ -7,14 +7,25 @@ from zipfile import ZipFile OVERLAY = Path(__file__).resolve().parents[1] / 'linuxfs/overlay' -def check(apk, overlay=OVERLAY): +def source_assets(overlay=OVERLAY): sources = list((overlay / 'usr/local/bin').glob('droiddeck-*')) sources += [path for path in [overlay / 'usr/local/bin/steam-compatibility'] if path.is_file()] sources += [path for path in (overlay / 'usr/bin').rglob('*') if path.is_file()] + result = {source: 'assets/linuxfs/' + source.relative_to(overlay).as_posix() for source in sources} + if overlay == OVERLAY: + desktop = overlay.parent / 'desktop' + for name in ['droiddeck-desktop', 'droiddeck-gpu', 'droiddeck-desktop-gpu', 'kwin_wayland_wrapper']: + result[desktop / name] = 'assets/linuxfs/usr/local/bin/' + name + result[desktop / 'firefox-droiddeck.js'] = 'assets/linuxfs/usr/lib/firefox/defaults/pref/droiddeck.js' + result[desktop / 'droiddeck-clipboard.desktop'] = 'assets/linuxfs/etc/xdg/autostart/droiddeck-clipboard.desktop' + result[overlay.parent.parent / 'mangoapp/mangoapp'] = 'assets/linuxfs/usr/local/bin/mangoapp' + return result + + +def check(apk, overlay=OVERLAY): errors = [] with ZipFile(apk) as package: - for source in sources: - asset = 'assets/linuxfs/' + source.relative_to(overlay).as_posix() + for source, asset in source_assets(overlay).items(): try: content = package.read(asset) except KeyError: diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py new file mode 100644 index 0000000000..04f91c8c89 --- /dev/null +++ b/tools/release/runtime_inputs.py @@ -0,0 +1,147 @@ +"""Attest prepared runtime inputs and reject stale local or CI APK payloads.""" +import hashlib +import json +from pathlib import Path +import subprocess +import sys +import tarfile +import shutil +from zipfile import ZipFile + + +ROOT = Path(__file__).resolve().parents[2] +MANIFEST = Path('app/build/runtime-inputs.json') +SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst', + 'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', + 'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env', + 'tools/mangoapp', 'tools/msitools', 'tools/build_local.sh', + 'tools/release/runtime_inputs.py'] + + +def digest(path): + with path.open('rb') as source: + result = hashlib.file_digest(source, 'sha256') if hasattr(hashlib, 'file_digest') else None + if result is None: + result = hashlib.sha256() + for block in iter(lambda: source.read(1024 * 1024), b''): + result.update(block) + return result.hexdigest() + + +def sources(root): + files = [] + for name in SOURCE_PATHS: + path = root / name + files.extend(path.rglob('*') if path.is_dir() else [path]) + return {str(path.relative_to(root)): digest(path) for path in sorted(set(files)) + if path.is_file() and '__pycache__' not in path.parts and path.suffix != '.pyc'} + + +def outputs(root): + result = {} + for tree in ['linuxfs', 'directaudio', 'droiddeck-esync']: + base = root / 'app/src/main/assets' / tree + for path in base.rglob('*'): + if path.is_file() and not any(part.startswith('.') for part in path.relative_to(base).parts): + result['assets/' + tree + '/' + str(path.relative_to(base))] = str(path.relative_to(root)) + for library in ['libproot.so', 'libproot-loader.so', 'libdirectaudiorelay.so']: + result['lib/arm64-v8a/' + library] = 'app/src/main/jniLibs/arm64-v8a/' + library + result['assets/pulseaudio.tzst'] = 'app/build/prepared-assets/pulseaudio.tzst' + return result + + +def required(): + paths = ['assets/linuxfs/' + name for name in ['libfakeinput.so', 'libblsession.so', + 'libblfastpath.so', 'libssbs.so', 'usr/local/bin/droiddeck-clipboard', + 'usr/local/bin/gamescope', 'usr/local/lib/droiddeck/uruntime']] + for arch in ['x86_64', 'i386']: + paths += ['assets/linuxfs/' + arch + '/' + lib for lib in ['libfakeinput.so', 'libblsession.so']] + paths += ['assets/linuxfs/x86_64/' + lib for lib in + ['libfaultreport.so', 'libthunkaudit.so', 'libvulkan-thunk.so']] + paths += ['assets/linuxfs/usr/local/lib/droiddeck-msitools/' + name for name in + ['msiinfo', 'cabextract', '7z', '7z.so', 'libmsi-1.0.so.0', 'libgsf-1.so.114', 'libgcab-1.0.so.0']] + paths += ['assets/linuxfs/usr/local/lib/mangoapp/' + name for name in + ['mangoapp', 'libfmt.so.10', 'libspdlog.so.1.13', 'libglfw.so.3', 'libtraceevent.so.1', 'libtracefs.so.1']] + for driver in ['linux-wine11', 'linux-wine11-systhread']: + paths += ['assets/directaudio/' + driver + '/' + name for name in + ['aarch64-unix/winedirectaudio.so', 'aarch64-windows/winedirectaudio.drv', + 'i386-windows/winedirectaudio.drv', 'version.txt']] + paths += ['assets/droiddeck-esync/index.json', 'assets/droiddeck-esync/index.json.sig', + 'lib/arm64-v8a/libproot.so', 'lib/arm64-v8a/libproot-loader.so', + 'lib/arm64-v8a/libdirectaudiorelay.so', 'assets/pulseaudio.tzst'] + return paths + + +def record(root=ROOT): + # Refresh scripts even on a CI native-cache hit; these are source, not cached binaries. + if root == ROOT: + from check_session_assets import source_assets + for source, asset in source_assets().items(): + target = root / 'app/src/main' / asset + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(source, target) + paths = outputs(root) + missing = [name for name in required() if name not in paths or not (root / paths[name]).is_file()] + if missing: + raise ValueError('Missing runtime inputs:\n' + '\n'.join(missing)) + # Check inside the compressed payload, where a plain Gradle build used to ship no sinks. + process = subprocess.Popen(['zstd', '-dc', str(root / paths['assets/pulseaudio.tzst'])], stdout=subprocess.PIPE) + try: + with tarfile.open(fileobj=process.stdout, mode='r|') as archive: + names = {member.name.removeprefix('./') for member in archive} + finally: + process.stdout.close() + if process.wait(): + raise ValueError('Invalid audio bundle') + for name in ['pactl', 'modules/arm64/module-aaudio-sink.so', 'modules/arm64/module-directaudio-native-sink.so']: + if name not in names: + raise ValueError('Audio bundle missing ' + name) + data = {'version': 1, 'sources': sources(root), 'outputs': { + name: {'path': path, 'sha256': digest(root / path)} for name, path in sorted(paths.items())}} + (root / MANIFEST).write_text(json.dumps(data, sort_keys=True, indent=2) + '\n') + + +def verify(root=ROOT): + try: + data = json.loads((root / MANIFEST).read_text()) + except (OSError, ValueError): + raise ValueError('Runtime inputs have not been prepared. Run tools/build_local.sh.') + if data.get('version') != 1 or data.get('sources') != sources(root): + raise ValueError('Runtime sources changed since preparation. Run tools/build_local.sh.') + for name in required(): + if name not in data['outputs']: + raise ValueError('Runtime manifest missing ' + name) + for name, output in data['outputs'].items(): + path = root / output['path'] + if not path.is_file() or digest(path) != output['sha256']: + raise ValueError('Missing or changed runtime input: ' + name + '. Run tools/build_local.sh.') + if outputs(root) != {name: output['path'] for name, output in data['outputs'].items()}: + raise ValueError('Runtime file set changed. Run tools/build_local.sh.') + return data + + +def check_apk(apk, root=ROOT): + data = verify(root) + with ZipFile(apk) as package: + for name, output in data['outputs'].items(): + try: + content = package.read(name) + except KeyError: + raise ValueError('APK missing ' + name) + if hashlib.sha256(content).hexdigest() != output['sha256']: + raise ValueError('APK has stale runtime input: ' + name) + + +if __name__ == '__main__': + try: + if sys.argv[1] == 'record': + record() + elif sys.argv[1] == 'verify': + verify() + elif sys.argv[1] == 'apk': + check_apk(sys.argv[2]) + else: + raise ValueError('Expected record, verify or apk <path>') + except (ValueError, OSError) as error: + sys.exit(str(error)) + print('Runtime inputs verified') diff --git a/tools/test_local.sh b/tools/test_local.sh new file mode 100755 index 0000000000..a2a69da629 --- /dev/null +++ b/tools/test_local.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# Session helpers and ELF preload tests need Linux (/proc, inotify and glibc), as in CI. +set -euo pipefail +repo_root=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) +cd "$repo_root" +if [[ $(uname -s) == Linux ]]; then + exec python3 -m unittest discover -s tools/tests "$@" +fi +image=${DROIDDECK_BUILD_IMAGE:-droiddeck-local-cross:24.04-v3} +python3 tools/docker_preflight.py "$image" +exec docker run --rm --platform linux/amd64 --user "$(id -u):$(id -g)" \ + -v "$repo_root:/src:ro" -w /src "$image" python3 -m unittest discover -s tools/tests "$@" diff --git a/tools/tests/test_desktop_bookmarks.py b/tools/tests/test_desktop_bookmarks.py index 113730bc6f..4c53dc6894 100644 --- a/tools/tests/test_desktop_bookmarks.py +++ b/tools/tests/test_desktop_bookmarks.py @@ -1,8 +1,10 @@ import os +import json from pathlib import Path import runpy import tempfile import unittest +import xml.etree.ElementTree as ET from unittest.mock import patch @@ -65,5 +67,112 @@ class DesktopBookmarksTest(unittest.TestCase): self.assertEqual(self.bookmarks.read_text(), "") +class DolphinPlacesTest(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + self.home = Path(temporary.name) + self.data = self.home / ".local/share" + self.steam = self.data / "Steam" + (self.steam / "steamapps").mkdir(parents=True) + self.places = self.data / "user-places.xbel" + self.environment = patch.dict(os.environ, {}, clear=True) + self.environment.start() + self.addCleanup(self.environment.stop) + + def registered(self, libraries): + entries = [] + for index, (path, label) in enumerate(libraries): + quote = lambda s: json.dumps(str(s), ensure_ascii=False) + entries.append(f'"{index}" {{ "path" {quote(path)} "label" {quote(label)} "apps" {{ "1" "0" }} }}') + (self.steam / "steamapps/libraryfolders.vdf").write_text('"libraryfolders" { ' + " ".join(entries) + ' }') + + def libraries(self): + return MODULE["steam_libraries"](self.home, self.data) + + def update(self, libraries): + MODULE["update_places"](self.places, libraries) + return ET.parse(self.places).getroot() + + def test_internal_and_all_available_registered_libraries_with_escaped_names(self): + external = self.home / 'USB café "Games"' + (external / "steamapps").mkdir(parents=True) + missing = self.home / "removed drive" + self.registered([(self.steam, ""), (external, 'Games "USB"'), (missing, "Gone")]) + self.assertEqual({self.steam.as_uri(): "Steam (Internal Storage)", external.as_uri(): 'Steam (Games "USB")'}, self.libraries()) + + def test_app_list_adds_new_library_before_steam_and_removes_unbound_managed_path(self): + old = self.home / "Games/old" + new = self.home / "Games/new" + for library in (old, new): + (library / "steamapps").mkdir(parents=True) + self.registered([(old, "Old")]) + listing = self.home / "libraries.json" + listing.write_text(json.dumps([{"path": str(new), "label": "New\nLibrary"}])) + os.environ["BL_STEAM_LIBRARIES"] = str(listing) + self.assertEqual({self.steam.as_uri(): "Steam (Internal Storage)", new.as_uri(): "Steam (New Library)"}, self.libraries()) + + def test_unreadable_app_list_falls_back_to_registered_libraries(self): + external = self.home / "USB" + (external / "steamapps").mkdir(parents=True) + self.registered([(external, "Drive")]) + os.environ["BL_STEAM_LIBRARIES"] = str(self.home / "missing.json") + self.assertIn(external.as_uri(), self.libraries()) + + def test_xml_escaping_stable_ids_and_idempotence(self): + libraries = {self.steam.as_uri(): "Steam (A & <B>)"} + root = self.update(libraries) + self.assertEqual("Steam (A & <B>)", root.findtext("bookmark/title")) + identity = root.findtext("bookmark/info/metadata[@owner='http://www.kde.org']/ID") + before = self.places.stat().st_mtime_ns + self.update(libraries) + self.assertEqual(before, self.places.stat().st_mtime_ns) + root = self.update({self.steam.as_uri(): "Renamed"}) + self.assertEqual(identity, root.findtext("bookmark/info/metadata[@owner='http://www.kde.org']/ID")) + self.assertEqual("Renamed", root.findtext("bookmark/title")) + + def test_personal_places_and_metadata_survive_unplugging_and_returning(self): + personal = '<bookmark href="file:///root/Documents"><title>Minekeep' + self.places.write_text('' + personal + '') + self.update({self.steam.as_uri(): "Steam"}) + root = self.update({}) + self.assertEqual(["file:///root/Documents"], [b.get("href") for b in root.findall("bookmark")]) + self.assertEqual("keep", root.findtext("bookmark/info/metadata/private")) + self.assertIn("", self.places.read_text()) + self.assertEqual(2, len(self.update({self.steam.as_uri(): "Steam"}).findall("bookmark"))) + + def test_personal_place_at_library_url_is_not_duplicated_renamed_or_removed(self): + self.places.write_text(f'My games') + before = self.places.read_bytes() + self.update({self.steam.as_uri(): "Steam"}) + self.update({}) + self.assertEqual(before, self.places.read_bytes()) + + def test_user_hidden_state_on_managed_place_is_kept(self): + root = self.update({self.steam.as_uri(): "Steam"}) + metadata = root.find("bookmark/info/metadata[@owner='http://www.kde.org']") + ET.SubElement(metadata, "IsHidden").text = "true" + self.places.write_bytes(ET.tostring(root)) + root = self.update({self.steam.as_uri(): "Renamed"}) + self.assertEqual("true", root.findtext("bookmark/info/metadata/IsHidden")) + + def test_personal_place_with_trailing_slash_is_not_duplicated(self): + self.places.write_text(f'My games') + root = self.update({self.steam.as_uri(): "Steam"}) + self.assertEqual(1, len(root.findall("bookmark"))) + self.assertEqual("My games", root.findtext("bookmark/title")) + + def test_invalid_xml_is_not_overwritten(self): + self.places.write_text("unfinished") + with self.assertRaises(ET.ParseError): + self.update({self.steam.as_uri(): "Steam"}) + self.assertEqual("unfinished", self.places.read_text()) + + def test_new_places_leave_default_version_for_kde_to_initialize(self): + root = self.update({self.steam.as_uri(): "Steam"}) + self.assertEqual("1.0", root.get("version")) + self.assertIsNone(root.find("info/metadata/Version")) + + if __name__ == "__main__": unittest.main() diff --git a/tools/tests/test_runtime_inputs.py b/tools/tests/test_runtime_inputs.py new file mode 100644 index 0000000000..030f254324 --- /dev/null +++ b/tools/tests/test_runtime_inputs.py @@ -0,0 +1,76 @@ +import importlib.util +import io +from pathlib import Path +import subprocess +import tarfile +import tempfile +import unittest +from zipfile import ZipFile + + +spec = importlib.util.spec_from_file_location('runtime_inputs', Path(__file__).resolve().parents[1] / 'release/runtime_inputs.py') +runtime = importlib.util.module_from_spec(spec) +spec.loader.exec_module(runtime) + + +class RuntimeInputsTest(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name) + paths = runtime.outputs(self.root) + for name in runtime.required(): + path = self.root / paths.get(name, 'app/src/main/' + name) + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(b'prepared runtime') + self.source = self.root / 'tools/linuxfs/preload/test.c' + self.source.parent.mkdir(parents=True) + self.source.write_text('current source') + self.audio(['pactl', 'modules/arm64/module-aaudio-sink.so', 'modules/arm64/module-directaudio-native-sink.so']) + + def audio(self, names): + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode='w') as archive: + for name in names: + entry = tarfile.TarInfo('./' + name) + entry.size = 1 + archive.addfile(entry, io.BytesIO(b'x')) + path = self.root / 'app/build/prepared-assets/pulseaudio.tzst' + path.write_bytes(subprocess.run(['zstd', '-q', '-c'], input=stream.getvalue(), capture_output=True, check=True).stdout) + + def test_missing_proot_cannot_be_attested(self): + (self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').unlink() + with self.assertRaisesRegex(ValueError, 'libproot.so'): + runtime.record(self.root) + + def test_base_audio_without_compiled_sinks_is_rejected(self): + self.audio(['pactl']) + with self.assertRaisesRegex(ValueError, 'module-aaudio-sink'): + runtime.record(self.root) + + def test_changed_native_source_rejects_old_outputs(self): + runtime.record(self.root) + self.source.write_text('new implementation') + with self.assertRaisesRegex(ValueError, 'sources changed'): + runtime.verify(self.root) + + def test_modified_prebuilt_binary_is_rejected(self): + runtime.record(self.root) + (self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').write_bytes(b'old APK binary') + with self.assertRaisesRegex(ValueError, 'changed runtime input'): + runtime.verify(self.root) + + def test_an_old_apk_is_rejected_even_when_staging_is_current(self): + runtime.record(self.root) + data = runtime.verify(self.root) + apk = self.root / 'old.apk' + with ZipFile(apk, 'w') as package: + for name, output in data['outputs'].items(): + content = b'stale' if name == 'assets/linuxfs/libssbs.so' else (self.root / output['path']).read_bytes() + package.writestr(name, content) + with self.assertRaisesRegex(ValueError, 'stale runtime input'): + runtime.check_apk(apk, self.root) + + def test_complete_current_payload_passes(self): + runtime.record(self.root) + runtime.verify(self.root) diff --git a/tools/tests/test_steam_desktop_ui.py b/tools/tests/test_steam_desktop_ui.py new file mode 100644 index 0000000000..955288c316 --- /dev/null +++ b/tools/tests/test_steam_desktop_ui.py @@ -0,0 +1,23 @@ +import runpy +from pathlib import Path +import unittest +from unittest.mock import Mock, patch + + +select_desktop = runpy.run_path(str(Path(__file__).resolve().parents[1] / + 'linuxfs/overlay/usr/local/bin/droiddeck-steam-desktop-ui'))['select_desktop'] + + +class SteamDesktopUiTest(unittest.TestCase): + def test_waits_for_cef_and_native_api_then_selects_once(self): + evaluate = Mock(side_effect=[ConnectionError(), {'value': False}, {'value': True}]) + with patch('time.sleep'): + self.assertTrue(select_desktop(evaluate)) + self.assertEqual(evaluate.call_count, 3) + self.assertIn('SteamClient.UI.ExitBigPictureMode()', evaluate.call_args.args[0]) + + def test_a_client_that_never_starts_has_a_bounded_wait(self): + evaluate = Mock() + with patch('time.monotonic', side_effect=[0, 91]): + self.assertFalse(select_desktop(evaluate)) + evaluate.assert_not_called() From d250b43eb9bcfd0126c19571a72104464b426660 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Fri, 9 Oct 2026 19:50:03 -0400 Subject: [PATCH 12/54] Games: revamped add games feature (#492) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding and managing your own (non-Steam) games moves out of the Steam settings into the Games tab, and works like Steam's own non-Steam Properties. - A + beside "Games N" opens a compact in-app picker: the storage first, then that storage's root, every folder, .exe files only. Picking an exe adds the game with its name, art, Custom badge and Steam shortcut. - "Add all games in this folder" adds every game folder inside at once. A summary opens immediately and fills in as each game is found, with a "?" where the exe was a close call and a red X to remove a game. - An edit button on each Custom game opens its editor: Name, Target (ranked exes, Other exe…), Start in, Launch options, the four pieces of art with thumbnails, and Remove. Edits go through the same Steam shortcut, so the appid never changes and nothing duplicates. - Art chooser per piece: images in the game folder, Steam's official art, SteamGridDB alternatives, your own image, or back to automatic. - Finding the exe: up to five folders deep, every exe scored (name match, Unreal -Shipping builds, bin folders and size up; launchers, crash reporters, installers, redistributables and depth down). - Real titles from the game's files (Steam manifest, GOG info, the exe's version info), keeping the folder's spacing when both spell the same title, underscores read as spaces. - Art: the exact Steam appid when the game names one, strict name matches otherwise, Steam's hashed asset paths for newer games, SteamGridDB as a fallback (built-in key from the SGDB_API_KEY secret, optional own key in Setup, sealed with the Keystore), and a cover made from the exe's icon when nothing else has one. - Removing a game is instant and keeps its files; adding it again brings it back. Steam's shortcut list is rewritten once, in the background. - The Games section is gone from Steam settings; folders added before keep listing their games. Artwork switch and SteamGridDB key are in Setup. --- .github/workflows/build.yml | 1 + app/build.gradle | 1 + .../com/droiddeck/launcher/MainActivity.kt | 118 ++-- .../droiddeck/launcher/frontend/AddedExes.kt | 48 ++ .../launcher/frontend/AddedGameArt.kt | 305 ++++++++-- .../launcher/frontend/AddedGameEdits.kt | 77 +++ .../droiddeck/launcher/frontend/AddedGames.kt | 295 +++++++++- .../launcher/frontend/AddedGamesPublisher.kt | 40 ++ .../launcher/frontend/GameExePicker.kt | 130 ++++ .../launcher/frontend/GameIdentifier.kt | 328 +++++++++++ .../droiddeck/launcher/frontend/Library.kt | 27 +- .../launcher/frontend/PeVersionInfo.kt | 250 ++++++++ .../launcher/frontend/SteamGridDb.kt | 160 +++++ .../launcher/session/SessionPrefs.kt | 58 ++ .../launcher/stores/SteamLiveShortcuts.kt | 47 +- .../launcher/stores/StoreInstalls.kt | 2 +- .../com/droiddeck/launcher/ui/AddAppDialog.kt | 20 +- .../droiddeck/launcher/ui/AddedGameEditor.kt | 555 ++++++++++++++++++ .../droiddeck/launcher/ui/ExePickerDialog.kt | 234 ++++++++ .../droiddeck/launcher/ui/FrontEndGames.kt | 145 ++++- .../droiddeck/launcher/ui/FrontEndScreen.kt | 12 + .../droiddeck/launcher/ui/FrontEndSetup.kt | 9 + .../launcher/ui/ModeSettingsDialog.kt | 64 +- .../droiddeck/launcher/ui/SettingsWidgets.kt | 13 + app/src/main/res/values-es/strings.xml | 59 +- app/src/main/res/values-fr/strings.xml | 55 +- app/src/main/res/values-ja/strings.xml | 55 +- app/src/main/res/values-ko/strings.xml | 55 +- app/src/main/res/values-ru/strings.xml | 59 +- app/src/main/res/values-zh-rCN/strings.xml | 55 +- app/src/main/res/values-zh-rHK/strings.xml | 55 +- app/src/main/res/values-zh-rTW/strings.xml | 55 +- app/src/main/res/values/strings.xml | 59 +- .../launcher/frontend/AddedExesTest.kt | 106 ++++ .../frontend/AddedGameArtLookupTest.kt | 102 ++++ .../launcher/frontend/AddedGameEditsTest.kt | 148 +++++ .../frontend/AddedGamesCandidatesTest.kt | 151 +++++ .../launcher/frontend/AddedGamesRemoveTest.kt | 74 +++ .../frontend/AddedGamesSpellingTest.kt | 14 + .../launcher/frontend/GameIdentifierTest.kt | 219 +++++++ .../droiddeck/launcher/ui/PickListingTest.kt | 52 ++ docs/development/added-games.md | 77 +++ docs/progress-log.md | 26 + .../usr/local/bin/droiddeck-steam-shortcuts | 19 +- tools/tests/test_steam_shortcuts.py | 36 ++ 45 files changed, 4175 insertions(+), 295 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/AddedGameEdits.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/AddedGamesPublisher.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/GameExePicker.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/GameIdentifier.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/PeVersionInfo.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/frontend/SteamGridDb.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/AddedGameEditor.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/ExePickerDialog.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedExesTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedGameArtLookupTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedGameEditsTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesCandidatesTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesRemoveTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/AddedGamesSpellingTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/GameIdentifierTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/PickListingTest.kt create mode 100644 docs/development/added-games.md diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1e89603968..389705314a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -411,6 +411,7 @@ jobs: id: build env: DROIDDECK_BUILD_LABEL: ${{ steps.build-label.outputs.label }} + SGDB_API_KEY: ${{ secrets.SGDB_API_KEY }} run: | set -o pipefail python3 tools/release/runtime_inputs.py record diff --git a/app/build.gradle b/app/build.gradle index a69f39097e..7734b1098b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,6 +94,7 @@ android { buildConfigField "String", "RELEASE_SIGNER", "\"${releaseSigner}\"" buildConfigField "long", "COMMIT_TIME", "${commitTime}L * 1000L" buildConfigField "String", "VARIANT_SUFFIXES", "\"${variantSuffixes}\"" + buildConfigField "String", "SGDB_API_KEY", "\"${System.getenv("SGDB_API_KEY") ?: project.findProperty("sgdbApiKey") ?: ""}\"" buildConfigField "String", "DESKTOP_KDE_TAG", "\"${desktopKde.DESKTOP_KDE_TAG}\"" buildConfigField "String", "DESKTOP_KDE_URL", "\"https://github.com/${desktopKde.DESKTOP_KDE_REPO}/releases/download/${desktopKde.DESKTOP_KDE_TAG}/desktop-kde.tar.zst\"" buildConfigField "String", "DESKTOP_KDE_SHA256", "\"${desktopKde.DESKTOP_KDE_SHA256}\"" diff --git a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt index 8a4e271135..435f535a3a 100644 --- a/app/src/main/java/com/droiddeck/launcher/MainActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/MainActivity.kt @@ -301,29 +301,34 @@ class MainActivity : ComponentActivity() { GameSaves.savesDir().mkdirs() pickSaveDir.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_saves_pick_folder, name, layout.label(this)), GameSaves.savesDir().path)) } - private val pickAddedGamesDir = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r -> - if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> - SessionPrefs.setAddedGamesDirs(this, addedGamesDirs + path) - addedGamesDirs = SessionPrefs.addedGamesDirs(this) - addedGamesArt = SessionPrefs.addedGamesArt(this) - refreshAddedGames() - refresh() - } + /** The Games tab's +: the picked .exe becomes a game (or selects / switches the one it belongs to). */ + private fun addGameExe(path: String) { + val exe = java.io.File(path) + Thread({ + val result = try { com.droiddeck.launcher.frontend.AddedGames.addExe(this, exe) } catch (e: Exception) { + Log.w(TAG, "add game: ${e.message}"); com.droiddeck.launcher.frontend.AddedGames.AddResult.NotFound + } + val game = when (result) { + is com.droiddeck.launcher.frontend.AddedGames.AddResult.Added -> result.game + is com.droiddeck.launcher.frontend.AddedGames.AddResult.Existing -> result.game + is com.droiddeck.launcher.frontend.AddedGames.AddResult.Switched -> result.game + else -> null + } + ui.post { + if (game == null) { + if (result is com.droiddeck.launcher.frontend.AddedGames.AddResult.Unreachable) + android.widget.Toast.makeText(this, R.string.games_add_unreachable, android.widget.Toast.LENGTH_LONG).show() + return@post + } + updateAddedGames(listOf(game.folder.path), then = { fetchAddedGameArt() }) + navRequest = "app:${game.steamAppId ?: game.appId.toInt()}" + } + }, "add-game-exe").start() } - private var pendingAddedGame: String? = null - private val pickAddedGameExe = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r -> - val folder = pendingAddedGame ?: return@registerForActivityResult - pendingAddedGame = null - if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> - SessionPrefs.setAddedGameExe(this, folder, path) - refreshAddedGames() - refresh() - } - } - private var addedGamesDirs by mutableStateOf>(emptyList()) private var addedGamesArt by mutableStateOf(true) @Volatile private var artFetchRunning = false - private var addedGames by mutableStateOf>(emptyList()) + /** The user entered a SteamGridDB API key of their own. */ + private var sgdbUserKey by mutableStateOf(false) private val pickRomsDir = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r -> if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> SessionPrefs.setRomsDir(this, path) @@ -590,6 +595,8 @@ class MainActivity : ComponentActivity() { gameStoresSpeedTier = gameStoresSpeedTier, storesOpenTab = storesOpenTab, navigate = navRequest, + addedGamesArt = addedGamesArt, + sgdbUserKey = sgdbUserKey, storesShowMature = storesShowMature, storeDownloadsActive = com.droiddeck.launcher.stores.StoresState.activeDownloads, ), @@ -606,6 +613,20 @@ class MainActivity : ComponentActivity() { onGameShortcut = { g -> com.droiddeck.launcher.frontend.GameShortcuts.pin(this, g) }, onExportGameFile = { g -> pickGameExport(g) }, onSyncGameFiles = { pickGameExport(null) }, + onAddGameExe = { path -> addGameExe(path) }, + onAddedGamesChanged = { folders -> + // New games want art; an edit does not. + val known = steamGames.mapNotNull { it.gameFiles?.path }.toSet() + updateAddedGames(folders, then = { if (folders.any { it !in known }) fetchAddedGameArt() }) + }, + onAddedGameRemoved = { folder, appId, name -> removeAddedGame(folder, appId, name) }, + onAddedGamesArt = { on -> SessionPrefs.setAddedGamesArt(this, on); addedGamesArt = on; if (on) refreshAddedGames() }, + onSgdbKey = { key -> + Thread({ + com.droiddeck.launcher.frontend.SteamGridDb.setUserKey(this, key) + ui.post { sgdbUserKey = key.isNotBlank(); refreshAddedGames() } + }, "sgdb-key").start() + }, onStopGameFileSync = { Thread({ GameFileSync.disable(this) @@ -1252,9 +1273,6 @@ class MainActivity : ComponentActivity() { wifiDiscoveryLocation = wifiDiscoveryLocation, wifiDiscoveryAsked = wifiDiscoveryAsked, wifiDiscoveryBlocked = wifiDiscoveryBlocked, - addedGamesDirs = if (mode == SessionService.MODE_STEAM) addedGamesDirs else null, - addedGames = if (mode == SessionService.MODE_STEAM) addedGames else emptyList(), - addedGamesArt = addedGamesArt, deckyInstalled = if (mode == SessionService.MODE_STEAM) decky.deckyInstalled else null, deckyLatestRelease = if (mode == SessionService.MODE_STEAM) decky.deckyReleases.firstOrNull() else null, deckyChecking = decky.deckyChecking, deckyStage = decky.deckyStage, deckyPercent = decky.deckyPercent, @@ -1335,14 +1353,6 @@ class MainActivity : ComponentActivity() { SessionPrefs.setRunSteamAtStartup(this, on) runSteamAtStartup = on }, - onPickAddedGamesDir = { pickAddedGamesDir.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_pick_added_games), addedGamesDirs.lastOrNull())) }, - onAddedGamesArt = { on -> SessionPrefs.setAddedGamesArt(this, on); addedGamesArt = on; if (on) refreshAddedGames() }, - onForgetAddedGamesDir = { dir -> SessionPrefs.setAddedGamesDirs(this, addedGamesDirs - dir); addedGamesDirs = SessionPrefs.addedGamesDirs(this); refreshAddedGames(); refresh() }, - onAddedGameExe = { folder, path -> SessionPrefs.setAddedGameExe(this, folder, path); refreshAddedGames(); refresh() }, - onPickAddedGameExe = { folder -> - pendingAddedGame = folder - pickAddedGameExe.launch(InAppFilePicker.buildIntent(this, listOf("exe"), getString(R.string.main_pick_game_exe), folder)) - }, onDeckyInstall = { release -> decky.installDecky(release) }, onDeckyCheck = { decky.refreshDecky() }, onDeckyEnabled = { enabled -> DeckyManager.setSupervisorEnabled(this, enabled); decky.deckySupervisor = enabled }, @@ -1397,18 +1407,42 @@ class MainActivity : ComponentActivity() { ) } - /** The added games as the settings page lists them; a scan of the folder, on this thread (one level, small). */ - /** The added games for the session settings: a folder walk, so off the main thread. */ - private fun refreshAddedGames() { + /** + * The Games tab after these added games were added or edited: each read alone and put in + * place of its old entry (or added), no library-wide scan. [then] runs after, on the main thread. + */ + private fun updateAddedGames(folders: List, then: () -> Unit = {}) { + if (folders.isEmpty()) return Thread({ - val games = scanAddedGames() - ui.post { addedGames = games; fetchAddedGameArt() } - }, "added-games").start() + val fresh = folders.mapNotNull { f -> + com.droiddeck.launcher.frontend.AddedGames.single(this, f)?.let { com.droiddeck.launcher.frontend.Library.addedGame(this, it) } + } + ui.post { + val paths = folders.toSet() + steamGames = (steamGames.filterNot { it.library == Library.ADDED && it.gameFiles?.path in paths } + fresh).distinctBy { it.gameId } + then() + } + }, "added-games-update").start() } - /** Walks the added-games folders, which can sit on slow shared storage or an SD card. */ - private fun scanAddedGames() = com.droiddeck.launcher.frontend.AddedGames.scan(this).map { g -> - com.droiddeck.launcher.ui.AddedGameRow(g.folder.path, g.folderName(), g.exe.path, g.exe.name, (g.candidates + g.exe).map { c -> c.path to c.name }.distinctBy { it.first }) + /** An added game removed: off the Games tab now; the rest (cache, Steam, listing) off the main thread. */ + private fun removeAddedGame(folder: String, appId: Long?, name: String) { + steamGames = steamGames.filterNot { it.library == Library.ADDED && it.gameFiles?.path == folder } + Thread({ com.droiddeck.launcher.frontend.AddedGames.remove(this, java.io.File(folder), appId, name) }, "added-game-remove").start() + } + + /** After the added games changed: art that is missing is fetched (off the main thread). */ + private fun refreshAddedGames() { + sgdbKeyKnown() + fetchAddedGameArt() + } + + /** Whether the user has a SteamGridDB key of their own, for Setup; read off the main thread. */ + private fun sgdbKeyKnown() { + Thread({ + val set = com.droiddeck.launcher.frontend.SteamGridDb.userKey(this).isNotEmpty() + ui.post { sgdbUserKey = set } + }, "sgdb-key-state").start() } private fun fetchAddedGameArt() { @@ -1447,7 +1481,6 @@ class MainActivity : ComponentActivity() { mangoapp = SessionPrefs.mangoapp(this) steamController = SessionPrefs.steamController(this) runSteamAtStartup = SessionPrefs.runSteamAtStartup(this) - addedGamesDirs = SessionPrefs.addedGamesDirs(this) hdrOn = SessionPrefs.hdr(this, mode) fpsLimit = SessionPrefs.fpsLimit(this, mode) upscaler = SessionPrefs.upscaler(this) @@ -1469,12 +1502,10 @@ class MainActivity : ComponentActivity() { // added-games folders, the storage volumes) lands while it animates in. Thread({ drivers.refreshDrivers() - val games = scanAddedGames() val storage = GameStorage.options(this) val deckyInstalled = if (mode == SessionService.MODE_STEAM) DeckyManager.installed(this) else null val deckySupervisor = mode == SessionService.MODE_STEAM && DeckyManager.supervisorEnabled(this) ui.post { - addedGames = games storageOptions = storage if (mode == SessionService.MODE_STEAM) { decky.deckyInstalled = deckyInstalled @@ -1523,6 +1554,7 @@ class MainActivity : ComponentActivity() { private fun refresh() { if (!busy && LinuxRuntimeInstaller.isBusy()) followRuntimeOperation { LinuxRuntimeInstaller.attach(it) } + addedGamesArt = SessionPrefs.addedGamesArt(this) desktopInstalled = DesktopCatalog.desktopInstalled(this) offlineAccount = OfflineMode.account(this) offline = OfflineMode.enabled(this) diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt new file mode 100644 index 0000000000..62afb0b8af --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedExes.kt @@ -0,0 +1,48 @@ +package com.droiddeck.launcher.frontend + +import android.content.Context +import org.json.JSONArray +import org.json.JSONObject +import java.io.File + +/** + * Games added from the Games tab's + rather than a Games folder: one picked .exe, or each game + * "Add all games in this folder" found. Kept by the game's folder in `files/added-exes.json`, + * which [AddedGames.scan] merges in. The exe chosen later, the shortcut appid, the edits and the + * removal live where every added game keeps them ([com.droiddeck.launcher.session.SessionPrefs]); + * this holds which folders are games and the exe they came with. + */ +object AddedExes { + /** [picked]: the user chose [exe]; false = [GameExePicker]'s best guess, which a scan ranks again. */ + class Entry(val folder: String, val exe: String, val picked: Boolean = true) + + private fun file(context: Context) = File(context.filesDir, "added-exes.json") + + @Synchronized + fun list(context: Context): List = try { + val array = JSONArray(file(context).takeIf { it.isFile }?.readText() ?: "[]") + (0 until array.length()).mapNotNull { i -> + val o = array.optJSONObject(i) ?: return@mapNotNull null + val folder = o.optString("folder"); val exe = o.optString("exe") + if (folder.isEmpty() || exe.isEmpty()) null else Entry(folder, exe, o.optBoolean("picked", true)) + } + } catch (e: Exception) { + emptyList() + } + + /** Adds [entry], replacing one for the same folder. */ + @Synchronized + fun put(context: Context, entry: Entry) = write(context, list(context).filter { it.folder != entry.folder } + entry) + + @Synchronized + fun drop(context: Context, folder: String) = write(context, list(context).filter { it.folder != folder }) + + private fun write(context: Context, entries: List) { + val array = JSONArray() + entries.forEach { array.put(JSONObject().put("folder", it.folder).put("exe", it.exe).put("picked", it.picked)) } + val f = file(context) + val tmp = File(f.path + ".tmp") + tmp.writeText(array.toString()) + if (!tmp.renameTo(f)) { f.delete(); tmp.renameTo(f) } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt index ecc92a1db6..a29e43679b 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGameArt.kt @@ -12,8 +12,9 @@ import java.net.URL import java.net.URLEncoder /** - * Artwork for an added game: what the user put in the game's folder first, else what Steam's - * store has for the same title, fetched once into the app's cache. Four pieces, named the way + * Artwork for an added game: what the user put in the game's folder first, else what Steam has + * for it - by the Steam appid its files name, else by a store search on its title - else what + * SteamGridDB has ([SteamGridDb]), fetched once into the app's cache. Four pieces, named the way * the client names them in its grid folder: the portrait capsule, the wide header, the hero * background and the logo; plus an icon pulled out of the game's own .exe. The session hands * the paths to the runtime's shortcuts writer, which copies them into the client's grid. @@ -28,13 +29,40 @@ object AddedGameArt { val any get() = portrait != null || header != null || hero != null || logo != null } + /** + * The pieces a user can choose, as the client takes them: [assetType] is its custom-artwork + * number (null: the icon, set by path); [cacheName] the automatic file in the game's cache. + */ + enum class Slot(val key: String, val cacheName: String, val assetType: Int?) { + COVER("p", "p.jpg", 0), BACKGROUND("hero", "hero.jpg", 1), LOGO("logo", "logo.png", 2), ICON("icon", "icon.png", null), + } + + /** Where a piece came from; [AUTO] = nothing found yet. */ + const val FOLDER = "folder" + const val STEAM = "steam" + const val SGDB = "sgdb" + const val FILE = "file" + const val EXE = "exe" + const val AUTO = "" + + /** A slot as it stands: the file shown and where it came from (one of [FOLDER].. [AUTO]); [chosen] = the user picked it. */ + class SlotState(val slot: Slot, val file: File?, val source: String, val chosen: Boolean) + + /** One image the chooser offers: its group ([FOLDER], [STEAM], [SGDB]), a thumbnail and the full image (URL or path). */ + class Option(val group: String, val thumb: String, val full: String) + + private val CHOSEN = Regex("""chosen-(\w+)\.(\w+)""") + + private fun chosen(cache: File, slot: Slot): File? = + cache.listFiles { f -> CHOSEN.matchEntire(f.name)?.groupValues?.get(1) == slot.key }?.firstOrNull() + private val IMAGE = listOf("png", "jpg", "jpeg") private val PORTRAIT = listOf("cover", "poster", "grid", "boxart", "capsule", "folder", "library_600x900", "portrait") private val HEADER = listOf("header", "banner", "library_header", "wide") private val HERO = listOf("hero", "background", "library_hero") private val LOGO = listOf("logo") - private fun cacheDir(context: Context, game: AddedGames.Game) = File(context.filesDir, "added-art/${game.appId}") + internal fun cacheDir(context: Context, game: AddedGames.Game) = File(context.filesDir, "added-art/${game.appId}") /** An image in the folder (or its `art` subfolder) under one of the given names, any image extension. */ private fun find(folder: File, names: List): File? { @@ -58,20 +86,112 @@ object AddedGameArt { ) } - /** The folder's art, else the cached Steam art, without touching the network. */ + /** The user's choice for each piece, else the folder's art, else the cached art; no network. */ fun resolve(context: Context, game: AddedGames.Game): Art { val own = local(game) val cache = cacheDir(context, game) fun cached(name: String) = File(cache, name).takeIf { it.isFile } return Art( - portrait = own.portrait ?: cached("p.jpg"), - header = own.header ?: cached("header.jpg"), - hero = own.hero ?: cached("hero.jpg"), - logo = own.logo ?: cached("logo.png"), - icon = own.icon ?: exeIcon(context, game), + portrait = chosen(cache, Slot.COVER) ?: own.portrait ?: cached("p.jpg") ?: exeArt(context, game, cover = true), + header = own.header ?: cached("header.jpg") ?: exeArt(context, game, cover = false), + hero = chosen(cache, Slot.BACKGROUND) ?: own.hero ?: cached("hero.jpg"), + logo = chosen(cache, Slot.LOGO) ?: own.logo ?: cached("logo.png"), + icon = chosen(cache, Slot.ICON) ?: own.icon ?: cached("icon-sgdb.png") ?: exeIcon(context, game), ) } + /** Each choosable piece with its file and where it came from, for the game's editor; no network. */ + fun describe(context: Context, game: AddedGames.Game): List { + val own = local(game) + val cache = cacheDir(context, game) + fun cached(name: String): Pair? = File(cache, name).takeIf { it.isFile }?.let { f -> + f to (File(cache, "$name.src").takeIf { it.isFile }?.readText()?.trim()?.takeIf { it.isNotEmpty() } ?: STEAM) + } + return Slot.values().map { slot -> + chosen(cache, slot)?.let { return@map SlotState(slot, it, chosenSource(context, game, slot).first.ifEmpty { FILE }, true) } + val auto: Pair? = when (slot) { + Slot.COVER -> own.portrait?.let { it to FOLDER } ?: cached("p.jpg") ?: exeArt(context, game, cover = true)?.let { it to EXE } + Slot.BACKGROUND -> own.hero?.let { it to FOLDER } ?: cached("hero.jpg") + Slot.LOGO -> own.logo?.let { it to FOLDER } ?: cached("logo.png") + Slot.ICON -> own.icon?.let { it to FOLDER } ?: File(cache, "icon-sgdb.png").takeIf { it.isFile }?.let { it to SGDB } + ?: exeIcon(context, game)?.let { it to EXE } + } + SlotState(slot, auto?.first, auto?.second ?: AUTO, false) + } + } + + private val CHOOSABLE = setOf("png", "jpg", "jpeg", "webp") + + /** Images in the game's folder (and its art folders) that could stand for a piece. */ + internal fun folderImages(folder: File): List = + listOf(folder, File(folder, "art"), File(folder, ".art")).flatMap { dir -> + dir.listFiles { f -> f.isFile && f.extension.lowercase() in CHOOSABLE }?.sortedBy { it.name.lowercase() }.orEmpty() + }.take(12) + + /** + * What the chooser offers for [slot]: the game folder's images, Steam's official piece (when the + * game has a Steam appid and Steam has the piece), and SteamGridDB's best few (with a key). + * Blocking: it asks the network. + */ + fun options(context: Context, game: AddedGames.Game, slot: Slot): List