Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3112402562 | ||
|
|
bc0530463e | ||
|
|
2d07c3df04 | ||
|
|
05a5f745f5 | ||
|
|
1ce0a43db0 | ||
|
|
b5f7ac6700 | ||
|
|
d06caf9838 | ||
|
|
03c8a0e4c2 | ||
|
|
1bd561531a | ||
|
|
64d72913fa | ||
|
|
1b31899896 | ||
|
|
38a84a8e8f | ||
|
|
c2dd90d1b6 | ||
|
|
e637a2e0bd | ||
|
|
261dee73c4 | ||
|
|
ac8455925e | ||
|
|
270dfbdcc3 |
No files matched your search
@@ -405,39 +405,6 @@ 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:
|
||||
@@ -457,52 +424,6 @@ 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'
|
||||
|
||||
|
||||
@@ -15,9 +15,6 @@ 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).
|
||||
|
||||
@@ -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. **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.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
|
||||
@@ -188,39 +188,6 @@ 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'
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<!-- Store download progress and "installed" / "failed" notices (Android 13+). -->
|
||||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<!-- The session keeps running while the app is not on screen: a foreground service holds the
|
||||
process at perceptible priority, and a high-performance WiFi lock keeps a backgrounded
|
||||
download from being throttled to nothing by the radio's power-save. -->
|
||||
@@ -129,13 +127,6 @@
|
||||
android:screenOrientation="sensorLandscape"
|
||||
android:theme="@style/AppTheme" />
|
||||
|
||||
<!-- A store's own sign-in page (GOG, Epic Games, Amazon Games) in a WebView. -->
|
||||
<activity
|
||||
android:name=".stores.StoreLoginActivity"
|
||||
android:exported="false"
|
||||
android:screenOrientation="sensorLandscape"
|
||||
android:theme="@style/AppTheme" />
|
||||
|
||||
<activity
|
||||
android:name=".agent.AgentStartActivity"
|
||||
android:exported="true"
|
||||
@@ -172,11 +163,6 @@
|
||||
<service
|
||||
android:name=".core.WirelessAdbPairingService"
|
||||
android:exported="false" />
|
||||
<!-- Keeps the process alive while a store (GOG, Epic, Amazon) game downloads. -->
|
||||
<service
|
||||
android:name=".stores.download.StoreDownloadService"
|
||||
android:exported="false"
|
||||
android:foregroundServiceType="dataSync" />
|
||||
<!-- Hands a zipped session folder to the share sheet; nothing else is exposed. -->
|
||||
<provider
|
||||
android:name="androidx.core.content.FileProvider"
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -1216,6 +1217,7 @@ struct DeckHidraw {
|
||||
FakeInputRingHeader *ring = nullptr;
|
||||
size_t mapping_size = 0;
|
||||
uint8_t pending_feature = 0;
|
||||
bool touch = false; // Steam's touch controller (below), not the Deck
|
||||
};
|
||||
|
||||
// What the Deck has and an Xbox pad does not, after the ring's events: motion (PadMotion) and the
|
||||
@@ -1315,13 +1317,15 @@ __attribute__((visibility("hidden"))) static bool is_deck_hidraw_path(const char
|
||||
return pathname && fake_deck_enabled() && !strcmp(pathname, DECK_HIDRAW_PATH);
|
||||
}
|
||||
|
||||
// The app's own pads are the Deck now; their evdev nodes are withdrawn from everyone.
|
||||
// The app's first pad is the Deck now: its evdev node is withdrawn from everyone. Further players'
|
||||
// pads are left to the client alone, which hands them to games through Steam Input.
|
||||
__attribute__((visibility("hidden"))) static bool is_withdrawn_pad_path(const char *pathname) {
|
||||
if (!fake_deck_enabled() || !pathname || strncmp(pathname, "/dev/input/", 11)) return false;
|
||||
const char *event = strrchr(pathname, '/') + 1;
|
||||
if (strncmp(event, "event", 5) && strncmp(event, "js", 2)) return false;
|
||||
int number = get_event_number(event);
|
||||
return number >= 0 && number < UINPUT_EVENT_BASE;
|
||||
if (number < 0 || number >= UINPUT_EVENT_BASE) return false;
|
||||
return number == 0 || !process_is_steam_client();
|
||||
}
|
||||
|
||||
static inline void put16(uint8_t *at, int value) {
|
||||
@@ -1483,6 +1487,321 @@ __attribute__((visibility("hidden"))) static int open_deck_hidraw(int flags) {
|
||||
return pair[0];
|
||||
}
|
||||
|
||||
|
||||
// ---- Steam's touch controller ----
|
||||
//
|
||||
// docs/development/steam-touch-controller.md. Steam builds its touch controller (Steam Input's
|
||||
// Mobile Touch type, the one Steam Link's on-screen controls are) for any HID device with the ids
|
||||
// 0000:11fb. With FAKE_TOUCHCTL_RING set, the client is offered one as /dev/hidraw17 beside the
|
||||
// Deck, its sysfs written by the app (SteamDeckPad.kt). The app draws the controls; this side only
|
||||
// carries the device's traffic through the file FAKE_TOUCHCTL_RING names, which both map:
|
||||
//
|
||||
// - the app writes the 40-byte input report (under a seqlock), whether the device is plugged in,
|
||||
// and the battery level;
|
||||
// - this side writes what the client tells the device: the app and action set it is on (output
|
||||
// report 4), the action set layers added and removed (5, 6), rumble (1, also passed to the app's
|
||||
// vibration like any pad's) and the setting report (3), and how many readers have it open.
|
||||
//
|
||||
// Plugged in and out by the app (while its controls are up and no physical pad is in use), as
|
||||
// Steam Link withdraws its touch controls: an unplugged device ends its open stream, refuses new
|
||||
// opens, and a udev event (libblsession's stand-in monitor, udevmon.c) tells the client's HID
|
||||
// discovery either way. Only the client is offered it; a game reads Steam Input's virtual pad.
|
||||
static constexpr const char *TOUCH_HIDRAW_PATH = "/dev/hidraw17";
|
||||
static constexpr unsigned int TOUCH_HIDRAW_MINOR = 17;
|
||||
static constexpr int TOUCH_REPORT_BYTES = 40;
|
||||
static constexpr int TOUCH_REPORT_INTERVAL_US = 8000;
|
||||
static constexpr const char *TOUCH_NAME = "Mobile Touch Control";
|
||||
static constexpr const char *TOUCH_SERIAL = "DROIDDECK0001";
|
||||
static constexpr const char *TOUCH_DEVPATH = "/devices/droiddeck/usb2/2-1:1.0/0003:0000:11FB.0002/hidraw/hidraw17";
|
||||
// Generic Desktop / Game Pad: for a vendor id that is not Valve's the client opens only a device
|
||||
// with a gamepad usage. One 40-byte input report, one 64-byte output and one 64-byte feature
|
||||
// report, no report ids. Must match SteamDeckPad.kt.
|
||||
static const uint8_t kTouchReportDescriptor[] = {
|
||||
0x05, 0x01, 0x09, 0x05, 0xa1, 0x01,
|
||||
0x09, 0x02, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x28, 0x81, 0x02,
|
||||
0x09, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x91, 0x02,
|
||||
0x09, 0x04, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0xb1, 0x02, 0xc0};
|
||||
|
||||
// The shared file. Must match SteamTouchDevice.kt.
|
||||
struct TouchRingFile {
|
||||
uint32_t magic; // TCH1
|
||||
uint32_t version;
|
||||
uint64_t report_seq; // odd while the app writes report
|
||||
uint8_t report[TOUCH_REPORT_BYTES];
|
||||
uint32_t plugged;
|
||||
uint32_t battery; // percent
|
||||
// Written here.
|
||||
uint64_t state_seq; // odd while being written
|
||||
uint32_t opened;
|
||||
uint32_t appid;
|
||||
uint32_t action_set;
|
||||
uint32_t layer_count;
|
||||
uint32_t layers[8];
|
||||
uint32_t rumble_seq;
|
||||
uint16_t rumble_low;
|
||||
uint16_t rumble_high;
|
||||
uint32_t rumble_ms;
|
||||
uint32_t setting30;
|
||||
uint32_t action_seq;
|
||||
};
|
||||
static constexpr uint32_t TOUCH_RING_MAGIC = 0x31484354;
|
||||
static constexpr size_t TOUCH_RING_SIZE = 256;
|
||||
static_assert(offsetof(TouchRingFile, report) == 16, "TouchRingFile layout");
|
||||
static_assert(offsetof(TouchRingFile, plugged) == 56, "TouchRingFile layout");
|
||||
static_assert(offsetof(TouchRingFile, state_seq) == 64, "TouchRingFile layout");
|
||||
static_assert(offsetof(TouchRingFile, layers) == 88, "TouchRingFile layout");
|
||||
static_assert(offsetof(TouchRingFile, rumble_seq) == 120, "TouchRingFile layout");
|
||||
static_assert(offsetof(TouchRingFile, action_seq) == 136, "TouchRingFile layout");
|
||||
static_assert(sizeof(TouchRingFile) <= TOUCH_RING_SIZE, "TouchRingFile layout");
|
||||
|
||||
__attribute__((visibility("hidden"))) static TouchRingFile *touch_ring() {
|
||||
static TouchRingFile *ring = nullptr;
|
||||
static std::once_flag once;
|
||||
std::call_once(once, [] {
|
||||
const char *path = getenv("FAKE_TOUCHCTL_RING");
|
||||
if (!path || !*path || !fake_deck_enabled()) return;
|
||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
||||
int fd = my_open(path, O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
Logger::log("touch: %s not openable (%s)\n", path, strerror(errno));
|
||||
return;
|
||||
}
|
||||
void *mapping = mmap(nullptr, TOUCH_RING_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
syscall(SYS_close, fd);
|
||||
if (mapping == MAP_FAILED || static_cast<TouchRingFile *>(mapping)->magic != TOUCH_RING_MAGIC) {
|
||||
Logger::log("touch: %s %s\n", path, mapping == MAP_FAILED ? "could not be mapped" : "has no valid header");
|
||||
if (mapping != MAP_FAILED) munmap(mapping, TOUCH_RING_SIZE);
|
||||
return;
|
||||
}
|
||||
ring = static_cast<TouchRingFile *>(mapping);
|
||||
});
|
||||
return ring;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static bool touch_plugged() {
|
||||
TouchRingFile *ring = touch_ring();
|
||||
return ring && __atomic_load_n(&ring->plugged, __ATOMIC_ACQUIRE) != 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static bool is_touch_hidraw_path(const char *pathname) {
|
||||
return pathname && !strcmp(pathname, TOUCH_HIDRAW_PATH) && process_is_steam_client() && touch_ring();
|
||||
}
|
||||
|
||||
// The state the client gave the device, written under the ring's seqlock.
|
||||
template <typename F>
|
||||
__attribute__((visibility("hidden"))) static void touch_state(F &&write) {
|
||||
TouchRingFile *ring = touch_ring();
|
||||
if (!ring) return;
|
||||
static std::mutex mutex;
|
||||
std::lock_guard<std::mutex> guard(mutex);
|
||||
__atomic_fetch_add(&ring->state_seq, 1, __ATOMIC_ACQ_REL);
|
||||
write(*ring);
|
||||
__atomic_fetch_add(&ring->state_seq, 1, __ATOMIC_ACQ_REL);
|
||||
}
|
||||
|
||||
static inline uint32_t get32(const uint8_t *at) {
|
||||
return static_cast<uint32_t>(at[0]) | static_cast<uint32_t>(at[1]) << 8 | static_cast<uint32_t>(at[2]) << 16 |
|
||||
static_cast<uint32_t>(at[3]) << 24;
|
||||
}
|
||||
|
||||
// An output report from the client (Steam Link's CVirtualController::HandleFeatureReports): the
|
||||
// type, then its fields.
|
||||
__attribute__((visibility("hidden"))) static void touch_output(const uint8_t *data, size_t size) {
|
||||
if (size < 9) return;
|
||||
static std::atomic<unsigned> seen[8];
|
||||
uint8_t type = data[0];
|
||||
unsigned n = type < 8 ? ++seen[type] : 0;
|
||||
if (n && (n & (n - 1)) == 0)
|
||||
Logger::log("touch: output report %u (x%u): app %u, %u\n", type, n, get32(data + 1), get32(data + 5));
|
||||
switch (type) {
|
||||
case 1: { // rumble: low, high, duration
|
||||
uint16_t low = data[1] | data[2] << 8, high = data[3] | data[4] << 8;
|
||||
uint32_t ms = get32(data + 5);
|
||||
touch_state([&](TouchRingFile &r) {
|
||||
r.rumble_low = low;
|
||||
r.rumble_high = high;
|
||||
r.rumble_ms = ms;
|
||||
r.rumble_seq++;
|
||||
});
|
||||
send_vibration(low, high, static_cast<uint16_t>(std::min<uint32_t>(ms, 0xffff)), 0);
|
||||
break;
|
||||
}
|
||||
case 3: // a setting: 0x30 and a value
|
||||
if (data[1] == 0x30) touch_state([&](TouchRingFile &r) { r.setting30 = data[2] | data[3] << 8; });
|
||||
break;
|
||||
case 4: // the app and its action set
|
||||
touch_state([&](TouchRingFile &r) {
|
||||
r.appid = get32(data + 1);
|
||||
r.action_set = get32(data + 5);
|
||||
r.layer_count = 0;
|
||||
r.action_seq++;
|
||||
});
|
||||
break;
|
||||
case 5: // a layer added to the app's action set
|
||||
case 6: // and removed
|
||||
touch_state([&](TouchRingFile &r) {
|
||||
uint32_t layer = get32(data + 5);
|
||||
uint32_t count = std::min<uint32_t>(r.layer_count, 8);
|
||||
uint32_t kept = 0;
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
if (r.layers[i] != layer) r.layers[kept++] = r.layers[i];
|
||||
if (type == 5 && kept < 8) r.layers[kept++] = layer;
|
||||
r.layer_count = kept;
|
||||
r.action_seq++;
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static bool read_touch_report(TouchRingFile *ring, uint8_t *out) {
|
||||
for (int attempt = 0; attempt < 8; attempt++) {
|
||||
uint64_t seq = __atomic_load_n(&ring->report_seq, __ATOMIC_ACQUIRE);
|
||||
if (seq & 1) continue;
|
||||
memcpy(out, ring->report, TOUCH_REPORT_BYTES);
|
||||
__atomic_thread_fence(__ATOMIC_ACQUIRE);
|
||||
if (seq == __atomic_load_n(&ring->report_seq, __ATOMIC_RELAXED)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static void *touch_report_thread(void *arg) {
|
||||
auto *holder = static_cast<std::shared_ptr<DeckHidraw> *>(arg);
|
||||
std::shared_ptr<DeckHidraw> self = *holder;
|
||||
delete holder;
|
||||
TouchRingFile *ring = touch_ring();
|
||||
uint8_t report[TOUCH_REPORT_BYTES] = {};
|
||||
while (touch_plugged()) {
|
||||
read_touch_report(ring, report);
|
||||
if (send(self->peer, report, sizeof(report), MSG_DONTWAIT | MSG_NOSIGNAL) < 0 &&
|
||||
errno != EAGAIN && errno != EWOULDBLOCK)
|
||||
break;
|
||||
struct timespec interval = {0, TOUCH_REPORT_INTERVAL_US * 1000L};
|
||||
nanosleep(&interval, nullptr);
|
||||
}
|
||||
// Ending the stream is the unplug: the client's read fails and it lets the controller go.
|
||||
syscall(SYS_close, self->peer);
|
||||
touch_state([](TouchRingFile &r) { if (r.opened) r.opened--; });
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static int open_touch_hidraw(int flags) {
|
||||
if (!touch_plugged()) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
int pair[2];
|
||||
if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, pair) < 0) return -1;
|
||||
int buffer = TOUCH_REPORT_BYTES * 4;
|
||||
setsockopt(pair[1], SOL_SOCKET, SO_SNDBUF, &buffer, sizeof(buffer));
|
||||
if (!(flags & O_CLOEXEC)) fcntl(pair[0], F_SETFD, 0);
|
||||
if (flags & O_NONBLOCK) fcntl(pair[0], F_SETFL, fcntl(pair[0], F_GETFL) | O_NONBLOCK);
|
||||
auto touch = std::make_shared<DeckHidraw>();
|
||||
touch->peer = pair[1];
|
||||
touch->touch = true;
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> guard(controller_mutex());
|
||||
deck_map()[pair[0]] = touch;
|
||||
}
|
||||
touch_state([](TouchRingFile &r) { r.opened++; });
|
||||
pthread_t thread;
|
||||
auto *arg = new std::shared_ptr<DeckHidraw>(touch);
|
||||
if (pthread_create(&thread, nullptr, touch_report_thread, arg) != 0) {
|
||||
delete arg;
|
||||
std::lock_guard<std::recursive_mutex> guard(controller_mutex());
|
||||
deck_map().erase(pair[0]);
|
||||
syscall(SYS_close, pair[0]);
|
||||
syscall(SYS_close, pair[1]);
|
||||
touch_state([](TouchRingFile &r) { if (r.opened) r.opened--; });
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
pthread_detach(thread);
|
||||
Logger::log("touch: %s opened as fd %d\n", TOUCH_HIDRAW_PATH, pair[0]);
|
||||
return pair[0];
|
||||
}
|
||||
|
||||
static int copy_ioctl_string(ioctl_request_t op, void *argp, const char *value);
|
||||
|
||||
__attribute__((visibility("hidden"))) static int ioctl_touch(DeckHidraw &touch, ioctl_request_t op, void *argp) {
|
||||
if (_IOC_TYPE(op) != 'H') {
|
||||
errno = ENOTTY;
|
||||
return -1;
|
||||
}
|
||||
size_t size = _IOC_SIZE(op);
|
||||
switch (_IOC_NR(op)) {
|
||||
case 0x01: // HIDIOCGRDESCSIZE
|
||||
*static_cast<int *>(argp) = sizeof(kTouchReportDescriptor);
|
||||
return 0;
|
||||
case 0x02: { // HIDIOCGRDESC
|
||||
auto *descriptor = static_cast<uint8_t *>(argp);
|
||||
uint32_t want;
|
||||
memcpy(&want, descriptor, sizeof(want));
|
||||
memcpy(descriptor + 4, kTouchReportDescriptor, std::min<size_t>(want, sizeof(kTouchReportDescriptor)));
|
||||
return 0;
|
||||
}
|
||||
case 0x03: { // HIDIOCGRAWINFO
|
||||
struct {
|
||||
uint32_t bustype;
|
||||
int16_t vendor;
|
||||
int16_t product;
|
||||
} info = {BUS_USB, 0x0000, static_cast<int16_t>(0x11fb)};
|
||||
memcpy(argp, &info, sizeof(info));
|
||||
return 0;
|
||||
}
|
||||
case 0x04: return copy_ioctl_string(op, argp, TOUCH_NAME); // HIDIOCGRAWNAME
|
||||
case 0x05: return copy_ioctl_string(op, argp, "usb-droiddeck-2/input0"); // HIDIOCGRAWPHYS
|
||||
case 0x08: return copy_ioctl_string(op, argp, TOUCH_SERIAL); // HIDIOCGRAWUNIQ
|
||||
case 0x06: // HIDIOCSFEATURE: Steam Link's controller ignores these
|
||||
touch.pending_feature = size > 0 ? static_cast<uint8_t *>(argp)[0] : 0;
|
||||
return static_cast<int>(size);
|
||||
case 0x07: { // HIDIOCGFEATURE: report 2 is the battery
|
||||
auto *buf = static_cast<uint8_t *>(argp);
|
||||
uint8_t id = size ? buf[0] : 0;
|
||||
memset(buf, 0, size);
|
||||
if (size) buf[0] = id;
|
||||
if (size > 2) {
|
||||
TouchRingFile *ring = touch_ring();
|
||||
buf[1] = 2;
|
||||
buf[2] = static_cast<uint8_t>(ring && ring->battery ? std::min<uint32_t>(ring->battery, 100) : 100);
|
||||
}
|
||||
return static_cast<int>(std::min<size_t>(size, 18));
|
||||
}
|
||||
default:
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Plugging in and out: the app flips `plugged`; this tells the client's HID discovery, which
|
||||
// otherwise never looks again (the sandbox gives it no kernel uevents).
|
||||
__attribute__((visibility("hidden"))) static void touch_uevent(const char *action) {
|
||||
using inject_fn = int (*)(const char *, const char *, const char *, const char *, unsigned, unsigned);
|
||||
static auto inject = reinterpret_cast<inject_fn>(dlsym(RTLD_DEFAULT, "bl_udevmon_inject"));
|
||||
int sent = inject ? inject(action, TOUCH_DEVPATH, "hidraw", TOUCH_HIDRAW_PATH, DECK_HIDRAW_MAJOR, TOUCH_HIDRAW_MINOR) : -1;
|
||||
Logger::log("touch: %s (udev event to %d monitor%s)\n", action, sent, sent == 1 ? "" : "s");
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static void *touch_plug_thread(void *) {
|
||||
bool was = touch_plugged();
|
||||
Logger::log("touch: %s at start\n", was ? "plugged in" : "unplugged");
|
||||
for (;;) {
|
||||
struct timespec interval = {0, 50 * 1000 * 1000};
|
||||
nanosleep(&interval, nullptr);
|
||||
bool now = touch_plugged();
|
||||
if (now == was) continue;
|
||||
was = now;
|
||||
touch_uevent(now ? "add" : "remove");
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void start_touch_controller() {
|
||||
config();
|
||||
if (!process_is_steam_client() || !touch_ring()) return;
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, nullptr, touch_plug_thread, nullptr) == 0) pthread_detach(thread);
|
||||
}
|
||||
|
||||
// libudev (systemd's sd-device) accepts a device only where its directory is on sysfs, which it
|
||||
// checks with fstatfs on the directory it resolved. The Deck's sysfs is the app's own files bound
|
||||
// into /sys, so those paths are said to be sysfs; nothing else is touched.
|
||||
@@ -1823,6 +2142,7 @@ EXPORT int open(const char *pathname, int flags, ...) {
|
||||
|
||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags);
|
||||
if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(flags);
|
||||
if (is_touch_hidraw_path(pathname)) return open_touch_hidraw(flags);
|
||||
if (is_withdrawn_pad_path(pathname)) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
@@ -1897,6 +2217,7 @@ EXPORT int openat(int dirfd, const char *pathname, int flags, ...) {
|
||||
|
||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags);
|
||||
if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(flags);
|
||||
if (is_touch_hidraw_path(pathname)) return open_touch_hidraw(flags);
|
||||
if (is_withdrawn_pad_path(pathname)) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
@@ -1957,6 +2278,12 @@ static int fake_stat_path(const char *pathname, S *statbuf, Real real) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
if (is_touch_hidraw_path(pathname)) {
|
||||
memset(statbuf, 0, sizeof(*statbuf));
|
||||
statbuf->st_mode = S_IFCHR | 0666;
|
||||
statbuf->st_rdev = makedev(DECK_HIDRAW_MAJOR, TOUCH_HIDRAW_MINOR);
|
||||
return 0;
|
||||
}
|
||||
if (is_deck_hidraw_path(pathname)) {
|
||||
memset(statbuf, 0, sizeof(*statbuf));
|
||||
statbuf->st_mode = S_IFCHR | 0666;
|
||||
@@ -2008,7 +2335,7 @@ static int fake_stat_fd(int fd, S *buf, Real real) {
|
||||
std::lock_guard<std::recursive_mutex> guard(controller_mutex());
|
||||
if (ret == 0 && deck_map().count(fd)) {
|
||||
buf->st_mode = (buf->st_mode & ~S_IFMT) | S_IFCHR;
|
||||
buf->st_rdev = makedev(DECK_HIDRAW_MAJOR, DECK_HIDRAW_MINOR);
|
||||
buf->st_rdev = makedev(DECK_HIDRAW_MAJOR, deck_map()[fd]->touch ? TOUCH_HIDRAW_MINOR : DECK_HIDRAW_MINOR);
|
||||
return ret;
|
||||
}
|
||||
auto controller = controller_map().find(fd);
|
||||
@@ -2054,6 +2381,7 @@ EXPORT int access(const char *pathname, int mode) {
|
||||
static auto my_access = reinterpret_cast<decltype(&::access)>(dlsym(RTLD_NEXT, "access"));
|
||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
||||
if (is_deck_hidraw_path(pathname) && process_is_steam_client()) return 0;
|
||||
if (is_touch_hidraw_path(pathname)) return 0;
|
||||
if (is_withdrawn_pad_path(pathname) || is_deck_hidraw_path(pathname)) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
@@ -2090,6 +2418,7 @@ EXPORT int faccessat(int dirfd, const char *pathname, int mode, int flags) {
|
||||
static auto my_faccessat = reinterpret_cast<decltype(&::faccessat)>(dlsym(RTLD_NEXT, "faccessat"));
|
||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
||||
if (is_deck_hidraw_path(pathname) && process_is_steam_client()) return 0;
|
||||
if (is_touch_hidraw_path(pathname)) return 0;
|
||||
if (is_withdrawn_pad_path(pathname) || is_deck_hidraw_path(pathname)) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
@@ -2211,7 +2540,8 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) {
|
||||
auto maker = uinput_map().find(fd);
|
||||
if (maker != uinput_map().end()) return ioctl_uinput(*maker->second, op, argp);
|
||||
auto deck = deck_map().find(fd);
|
||||
if (deck != deck_map().end()) return ioctl_deck(*deck->second, op, argp);
|
||||
if (deck != deck_map().end())
|
||||
return deck->second->touch ? ioctl_touch(*deck->second, op, argp) : ioctl_deck(*deck->second, op, argp);
|
||||
auto controller = controller_map().find(fd);
|
||||
if (controller == controller_map().end()) {
|
||||
guard.unlock();
|
||||
@@ -2537,7 +2867,10 @@ EXPORT ssize_t write(int fd, const void *buf, size_t count) {
|
||||
std::unique_lock<std::recursive_mutex> guard(controller_mutex());
|
||||
auto made = uinput_map().find(fd);
|
||||
if (made != uinput_map().end()) return write_uinput(*made->second, buf, count);
|
||||
if (deck_map().count(fd)) return static_cast<ssize_t>(count);
|
||||
if (deck_map().count(fd)) {
|
||||
if (deck_map()[fd]->touch) touch_output(static_cast<const uint8_t *>(buf), count);
|
||||
return static_cast<ssize_t>(count);
|
||||
}
|
||||
auto controller = controller_map().find(fd);
|
||||
if (controller != controller_map().end()) {
|
||||
if (fake_fd_is_stale(fd)) {
|
||||
@@ -2562,7 +2895,11 @@ EXPORT ssize_t writev(int fd, const struct iovec *iov, int iovcnt) {
|
||||
std::unique_lock<std::recursive_mutex> guard(controller_mutex());
|
||||
if (deck_map().count(fd)) {
|
||||
ssize_t total = 0;
|
||||
for (int i = 0; i < iovcnt; i++) total += static_cast<ssize_t>(iov[i].iov_len);
|
||||
bool touch = deck_map()[fd]->touch;
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
if (touch) touch_output(static_cast<const uint8_t *>(iov[i].iov_base), iov[i].iov_len);
|
||||
total += static_cast<ssize_t>(iov[i].iov_len);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
auto made = uinput_map().find(fd);
|
||||
|
||||
@@ -29,10 +29,5 @@ class App : Application() {
|
||||
GpuClockPin.clearLeftover(this)
|
||||
BwrapSpawner.start(this)
|
||||
com.droiddeck.launcher.frontend.GameFileSync.start(this)
|
||||
// The game stores the Stores section can talk to; the native engine loads only when one needs it.
|
||||
com.droiddeck.launcher.stores.StoresState.init(this)
|
||||
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.gog.GogBackend)
|
||||
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.epic.EpicBackend)
|
||||
com.droiddeck.launcher.stores.StoresState.register(com.droiddeck.launcher.stores.amazon.AmazonBackend)
|
||||
}
|
||||
}
|
||||
@@ -164,12 +164,6 @@ class MainActivity : ComponentActivity() {
|
||||
private var launcherFullscreen by mutableStateOf(true)
|
||||
private var animationsEnabled by mutableStateOf(true)
|
||||
private var storeEnabled by mutableStateOf(false)
|
||||
private var gameStoresEnabled by mutableStateOf(false)
|
||||
private var gameStoresSpeedTier by mutableStateOf("fast")
|
||||
private var storesOpenTab by mutableStateOf(SessionPrefs.STORES_OPEN_LIBRARY)
|
||||
/** A page a notification asked for ("stores:downloads", "app:<id>"), until the front end has gone there. */
|
||||
private var navRequest by mutableStateOf<String?>(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)
|
||||
@@ -443,7 +437,6 @@ 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.
|
||||
@@ -530,10 +523,6 @@ 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 {
|
||||
@@ -587,12 +576,6 @@ 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() },
|
||||
@@ -603,8 +586,7 @@ class MainActivity : ComponentActivity() {
|
||||
.putExtra(SessionService.EXTRA_MODE, SessionService.MODE_DESKTOP)
|
||||
.putExtra(SessionService.EXTRA_STEAM_UI, "desktop"), steamSession = true)
|
||||
},
|
||||
// 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) } },
|
||||
onSteamGame = { g -> if (shortcutPicker) chooseGameShortcut(g) else launchGame(g) },
|
||||
onGameShortcut = { g -> com.droiddeck.launcher.frontend.GameShortcuts.pin(this, g) },
|
||||
onExportGameFile = { g -> pickGameExport(g) },
|
||||
onSyncGameFiles = { pickGameExport(null) },
|
||||
@@ -720,12 +702,6 @@ 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
|
||||
@@ -927,10 +903,6 @@ 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 ->
|
||||
@@ -988,8 +960,6 @@ 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
|
||||
|
||||
@@ -145,6 +145,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
if (resumed && !pipUi && SessionState.deckPad) padMotion?.start() else padMotion?.stop()
|
||||
}
|
||||
private var onScreenControls: OnScreenControls? = null
|
||||
/** Steam's touch controls (SteamTouchControls), made once the session offers the device. */
|
||||
private var steamTouchControls: com.droiddeck.launcher.input.SteamTouchControls? = null
|
||||
private var steamTouchAvailable by mutableStateOf(false)
|
||||
private val steamTouchRetry = Runnable { updateOnScreenControls() }
|
||||
/** While Steam's touch controls are up: how the game's touch config says touches outside them
|
||||
* act (Steam Link's input and mouse modes), in place of the drawer's touch mode. */
|
||||
private var steamTouchOptions: com.droiddeck.launcher.input.SteamTouchConfig.Options? = null
|
||||
/** The touch mode touches are routed by: the drawer's, or the game's while Steam's controls are up. */
|
||||
private var routedTouchMode = SessionPrefs.TOUCH_AUTO
|
||||
private var keyboard: KeyboardHost? = null
|
||||
private var controllerSettings by mutableStateOf<ControllerPrefs.Settings?>(null)
|
||||
private val sessionClipboard by lazy {
|
||||
@@ -295,6 +304,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
}
|
||||
override fun onInputDeviceRemoved(deviceId: Int) {
|
||||
Log.i(TAG, "input device $deviceId disconnected")
|
||||
padBridge?.onDeviceRemoved(deviceId)
|
||||
updateOnScreenControls()
|
||||
updatePointerCapture()
|
||||
}
|
||||
@@ -560,7 +570,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
SessionPrefs.setBackActionsInverted(this@SessionActivity, inverted)
|
||||
backActionsInverted = inverted
|
||||
},
|
||||
onOsc = { v -> SessionPrefs.setOscMode(this@SessionActivity, v); readPrefs(); updateOnScreenControls() },
|
||||
onOsc = { v ->
|
||||
SessionPrefs.setOscMode(this@SessionActivity, v)
|
||||
readPrefs()
|
||||
updateOnScreenControls()
|
||||
if (v == SessionPrefs.OSC_STEAM_TOUCH && com.droiddeck.launcher.input.SteamTouchDevice.current == null)
|
||||
Toast.makeText(this@SessionActivity, R.string.osc_style_next_session, Toast.LENGTH_LONG).show()
|
||||
},
|
||||
onEditSteamTouch = if (steamTouchAvailable && oscMode == SessionPrefs.OSC_STEAM_TOUCH && onScreenButtonsVisible) ({
|
||||
drawerOpen = false
|
||||
steamTouchControls?.startEditing()
|
||||
}) else null,
|
||||
onSuspendPolicy = { policy ->
|
||||
SessionPrefs.setSuspendPolicy(this@SessionActivity, SessionState.mode, policy)
|
||||
suspendPolicy = policy
|
||||
@@ -795,6 +815,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
if (nextTouchMode == SessionPrefs.TOUCH_OFF) cursorVisible = false
|
||||
}
|
||||
touchMode = nextTouchMode
|
||||
routedTouchMode = effectiveTouchMode()
|
||||
frameGen = FrameGen.mode(this)
|
||||
fexPreset = SessionPrefs.fexPreset(this)
|
||||
suspendPolicy = SessionPrefs.suspendPolicy(this, SessionState.mode)
|
||||
@@ -1654,8 +1675,32 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
}
|
||||
}
|
||||
|
||||
/** The touch mode in force: the game's own while Steam's touch controls are shown. */
|
||||
private fun effectiveTouchMode(): String {
|
||||
val options = steamTouchOptions
|
||||
if (options == null || steamTouchControls?.visibility != View.VISIBLE) return SessionPrefs.touchMode(this)
|
||||
return when {
|
||||
options.inputMode == com.droiddeck.launcher.input.SteamTouchConfig.INPUT_CONTROLLER -> SessionPrefs.TOUCH_OFF
|
||||
options.mouseMode == com.droiddeck.launcher.input.SteamTouchConfig.MOUSE_RELATIVE -> SessionPrefs.TOUCH_PAD
|
||||
else -> SessionPrefs.TOUCH_DIRECT
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyTouchRouting() {
|
||||
val next = effectiveTouchMode()
|
||||
if (::touchpad.isInitialized) {
|
||||
val speed = steamTouchOptions?.trackpadSensitivity?.takeIf { steamTouchControls?.visibility == View.VISIBLE } ?: 1f
|
||||
touchpad.sensitivity = 1.15f * speed
|
||||
}
|
||||
if (next == routedTouchMode) return
|
||||
if (::touchpad.isInitialized) touchpad.cancel()
|
||||
if (CompositorHost.isStarted) WaylandCompositor.nativeSendTouch(3, -1, 0, 0)
|
||||
if (next == SessionPrefs.TOUCH_OFF) cursorVisible = false
|
||||
routedTouchMode = next
|
||||
}
|
||||
|
||||
/** Touchpad on the desktop, direct in Steam, unless the drawer says otherwise. */
|
||||
private fun usingTouchpad(): Boolean = when (SessionPrefs.touchMode(this)) {
|
||||
private fun usingTouchpad(): Boolean = when (routedTouchMode) {
|
||||
SessionPrefs.TOUCH_PAD -> true
|
||||
SessionPrefs.TOUCH_DIRECT -> false
|
||||
else -> SessionState.mode != SessionService.MODE_STEAM || SessionState.steamUi == "desktop"
|
||||
@@ -1677,7 +1722,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
if (event.isFromSource(android.view.InputDevice.SOURCE_MOUSE)) return onMouse(event)
|
||||
// Child views get the event first, so menus, keyboards and the on-screen pad still work.
|
||||
if (touchMode == SessionPrefs.TOUCH_OFF) return true
|
||||
if (routedTouchMode == SessionPrefs.TOUCH_OFF) return true
|
||||
if (usingTouchpad()) {
|
||||
val rect = drawnRect() ?: return false
|
||||
if (touchpad.bounds != rect) {
|
||||
@@ -1796,6 +1841,41 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
what, d.name, d.vendorId, d.productId, d.id, d.sources))
|
||||
}
|
||||
|
||||
/** Steam's touch controls, once the session has made the device (SessionService). */
|
||||
private fun steamTouch(): com.droiddeck.launcher.input.SteamTouchControls? {
|
||||
val device = com.droiddeck.launcher.input.SteamTouchDevice.current ?: return null
|
||||
steamTouchControls?.let { view ->
|
||||
if (view.device === device) return view
|
||||
// A new session made a new device: the view of the old one goes.
|
||||
view.releaseAll()
|
||||
(view.parent as? android.view.ViewGroup)?.removeView(view)
|
||||
steamTouchControls = null
|
||||
}
|
||||
val root = onScreenControls?.parent as? android.view.ViewGroup ?: return null
|
||||
val view = com.droiddeck.launcher.input.SteamTouchControls(this, device,
|
||||
onMenu = { drawerOpen = true }, onKeyboard = ::togglePcKeyboard)
|
||||
view.visibility = View.GONE
|
||||
view.onOptions = { options ->
|
||||
steamTouchOptions = options
|
||||
applyTouchRouting()
|
||||
}
|
||||
// Paste: the clipboard is shared with the session's X servers; Ctrl+V types it.
|
||||
view.onPaste = {
|
||||
sendGuestKey(29, true); sendGuestKey(47, true) // KEY_LEFTCTRL, KEY_V
|
||||
sendGuestKey(47, false); sendGuestKey(29, false)
|
||||
}
|
||||
// The client reads a controller's config when it connects: after a save, plug the device
|
||||
// out and back in so it takes the new one.
|
||||
view.onSaved = {
|
||||
device.plugged = false
|
||||
uiHandler.postDelayed({ updateOnScreenControls() }, 700)
|
||||
}
|
||||
root.addView(view, root.indexOfChild(onScreenControls) + 1)
|
||||
steamTouchControls = view
|
||||
steamTouchAvailable = true
|
||||
return view
|
||||
}
|
||||
|
||||
private fun updateOnScreenControls() {
|
||||
val forced = File(Environment.getExternalStorageDirectory(), "Download/droiddeck-osc")
|
||||
.takeIf { it.isFile }
|
||||
@@ -1806,20 +1886,49 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
onScreenButtonsVisible = false
|
||||
controls.releaseAll()
|
||||
controls.visibility = View.GONE
|
||||
steamTouchControls?.let { it.releaseAll(); it.visibility = View.GONE }
|
||||
com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false
|
||||
return
|
||||
}
|
||||
val buttonsOnly = forced == SessionPrefs.OSC_STEAM_QAM
|
||||
// Steam's touch controller stands in for the app's pad, and is plugged into the client only
|
||||
// while it is shown - as Steam Link withdraws its controls when a physical pad is in use.
|
||||
val wantsSteam = forced == SessionPrefs.OSC_STEAM_TOUCH && SessionState.mode == SessionService.MODE_STEAM
|
||||
val steam = if (wantsSteam) steamTouch() else null
|
||||
// The service makes the device while the session starts, after this activity: the app's
|
||||
// own pad until then, and another look in a moment.
|
||||
if (wantsSteam && steam == null && !isFinishing && !isDestroyed) {
|
||||
uiHandler.removeCallbacks(steamTouchRetry)
|
||||
uiHandler.postDelayed(steamTouchRetry, 1000)
|
||||
}
|
||||
if (steam == null) {
|
||||
steamTouchControls?.let { it.releaseAll(); it.visibility = View.GONE }
|
||||
com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = false
|
||||
}
|
||||
controls.setButtonsOnly(buttonsOnly)
|
||||
val show = when (forced) {
|
||||
SessionPrefs.OSC_ALWAYS -> true
|
||||
SessionPrefs.OSC_STEAM_QAM -> true
|
||||
SessionPrefs.OSC_NEVER -> false
|
||||
// Shown throughout a Steam session: a handheld's built-in pad would keep Auto off for good.
|
||||
// Outside Steam it is Auto.
|
||||
SessionPrefs.OSC_STEAM_TOUCH -> SessionState.mode == SessionService.MODE_STEAM ||
|
||||
(!PadBridge.anyControllerConnected() && SessionState.mode != SessionService.MODE_DESKTOP)
|
||||
// Auto: the touch pad when there is no controller - except on the desktop, where the
|
||||
// screen is a touchpad for the pointer and a pad over it would be in the way. A game
|
||||
// started from the rail, or Steam, gets it; the drawer turns it on anywhere.
|
||||
else -> !PadBridge.anyControllerConnected() && SessionState.mode != SessionService.MODE_DESKTOP
|
||||
}
|
||||
onScreenButtonsVisible = show
|
||||
if (steam != null) {
|
||||
if (controls.visibility == View.VISIBLE) { controls.releaseAll(); controls.visibility = View.GONE }
|
||||
if (!show) steam.releaseAll()
|
||||
steam.visibility = if (show) View.VISIBLE else View.GONE
|
||||
com.droiddeck.launcher.input.SteamTouchDevice.current?.plugged = show
|
||||
applyTouchRouting()
|
||||
Log.i(TAG, "on-screen controls: Steam's touch controller " + if (show) "shown" else "hidden")
|
||||
return
|
||||
}
|
||||
if (show == (controls.visibility == View.VISIBLE)) return
|
||||
if (!show) controls.releaseAll()
|
||||
controls.visibility = if (show) View.VISIBLE else View.GONE
|
||||
@@ -2038,6 +2147,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
uiHandler.removeCallbacks(steamTouchRetry)
|
||||
if (runtimeRemovalBlocked) { super.onDestroy(); return }
|
||||
// Deliberately does NOT end the session: this activity can be destroyed while the user is
|
||||
// in another app, and the whole point of the service is that Steam survives that.
|
||||
|
||||
@@ -6,8 +6,6 @@ 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
|
||||
|
||||
@@ -18,21 +16,13 @@ object AddedGames {
|
||||
private const val LEGACY_LIBRARY = "/mnt/bannerlator-sd"
|
||||
|
||||
class Game(
|
||||
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,
|
||||
val folder: File, val name: String, val exe: File, 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<File>,
|
||||
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
|
||||
}
|
||||
@@ -76,16 +66,9 @@ 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
|
||||
}
|
||||
|
||||
@@ -116,14 +99,9 @@ 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 <root>/<Store>/, 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 }
|
||||
}
|
||||
|
||||
@@ -147,19 +125,13 @@ object AddedGames {
|
||||
|
||||
private fun scanGame(context: Context, folder: File, out: MutableList<Game>) {
|
||||
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 }
|
||||
// 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
|
||||
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
|
||||
// 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)
|
||||
@@ -171,11 +143,10 @@ 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, (if (adopted != null) adopted else launch).parentFile ?: folder) ?: return
|
||||
val guestDir = guestPath(context, exe.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,
|
||||
source = sidecar?.store?.id ?: Library.ADDED, storeId = sidecar?.id))
|
||||
steamId?.toLong() ?: ((appId shl 32) or 0x02000000L), candidates, steamId))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,18 +24,12 @@ 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, and the [SteamGame.source] of a plain added folder. */
|
||||
/** The [SteamGame.library] of a game added to the library rather than installed by Steam. */
|
||||
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<Rom>) {
|
||||
@@ -161,7 +155,6 @@ 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 }
|
||||
|
||||
@@ -26,8 +26,6 @@ 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() }
|
||||
@@ -38,8 +36,7 @@ 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)
|
||||
.put("source", g.source).apply { g.storeId?.let { put("storeId", it) } },
|
||||
.putFile("gameFiles", g.gameFiles).putFile("protonPrefix", g.protonPrefix).putFile("icon", g.icon),
|
||||
)
|
||||
val f = file(context)
|
||||
val tmp = File(f.path + ".tmp")
|
||||
|
||||
@@ -10,6 +10,8 @@ import android.view.MotionEvent;
|
||||
import com.droiddeck.launcher.session.SessionState;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
|
||||
/**
|
||||
* A physical controller, republished as a synthetic evdev device the Steam client can see.
|
||||
@@ -25,11 +27,14 @@ import java.io.File;
|
||||
* to the client instead ({@link com.droiddeck.launcher.session.SteamDeckPad}), which has a Quick
|
||||
* Access button of its own; an Xbox pad has none, so there the client's menu is reached with the
|
||||
* Guide-then-A chord the client itself answers.
|
||||
*
|
||||
* <p>Each further controller is another player: slots 1 to 3, an Xbox 360 pad each, which in a
|
||||
* Steam session only the client reads (libfakeinput) and hands games through Steam Input.
|
||||
*/
|
||||
public final class PadBridge {
|
||||
private static final String TAG = "PadBridge";
|
||||
/** Slot 0 is the one the session exports; extra slots would each show as another pad. */
|
||||
private static final int SLOT = 0;
|
||||
/** Player slots: the session prepares a ring for each (SessionService). */
|
||||
public static final int SLOTS = 4;
|
||||
private static final float DEAD_ZONE = 0.12f;
|
||||
// The client takes A as part of the chord only once it has had Guide held for a while, and a
|
||||
// client starved of CPU needs longer. Too short, and it acts on A as well, selecting whatever it
|
||||
@@ -50,8 +55,13 @@ public final class PadBridge {
|
||||
chordHandler = new Handler(thread.getLooper());
|
||||
}
|
||||
|
||||
private final File fakeInputDir;
|
||||
private final FakeInputWriter writer;
|
||||
private final PadState state = new PadState();
|
||||
/** The input device id holding each slot, or {@link #NO_CONTROLLER}. */
|
||||
private final int[] owners = new int[SLOTS];
|
||||
private final FakeInputWriter[] playerWriters = new FakeInputWriter[SLOTS];
|
||||
private final PadState[] playerStates = new PadState[SLOTS];
|
||||
private final PadState effectiveState = new PadState();
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private boolean open;
|
||||
@@ -79,29 +89,50 @@ public final class PadBridge {
|
||||
/** No physical controller is driving the pad: nothing yet, or the on-screen controls were last. */
|
||||
public static final int NO_CONTROLLER = -1;
|
||||
/**
|
||||
* The Android input device id of the physical controller that last drove the pad, or
|
||||
* {@link #NO_CONTROLLER}. Every pad feeds the one exported slot, so this is how a game's rumble
|
||||
* on that slot finds the motors in the player's hands (RumbleComponent).
|
||||
* The Android input device id of the physical controller that last drove any slot, or
|
||||
* {@link #NO_CONTROLLER}: where rumble goes when it names no slot of ours (RumbleComponent).
|
||||
*/
|
||||
private static volatile int activeControllerId = NO_CONTROLLER;
|
||||
/** Per slot, the controller its rumble belongs to; slot 0's is none while the on-screen controls lead. */
|
||||
private static final AtomicIntegerArray slotControllers = new AtomicIntegerArray(SLOTS);
|
||||
static {
|
||||
for (int slot = 0; slot < SLOTS; slot++) slotControllers.set(slot, NO_CONTROLLER);
|
||||
}
|
||||
|
||||
public static int activeControllerId() { return activeControllerId; }
|
||||
|
||||
/** The controller playing in this ring slot, or the last one used for any other slot. */
|
||||
public static int controllerForSlot(int slot) {
|
||||
return slot >= 0 && slot < SLOTS ? slotControllers.get(slot) : activeControllerId;
|
||||
}
|
||||
|
||||
public PadBridge(File fakeInputDir) {
|
||||
writer = new FakeInputWriter(fakeInputDir.getAbsolutePath(), SLOT);
|
||||
this.fakeInputDir = fakeInputDir;
|
||||
writer = new FakeInputWriter(fakeInputDir.getAbsolutePath(), 0);
|
||||
Arrays.fill(owners, NO_CONTROLLER);
|
||||
for (int slot = 1; slot < SLOTS; slot++) playerStates[slot] = new PadState();
|
||||
}
|
||||
|
||||
/** Opens the ring; safe to call more than once. */
|
||||
public synchronized boolean start() {
|
||||
if (!open) {
|
||||
open = writer.open();
|
||||
Log.i(TAG, "ring slot " + SLOT + (open ? " open" : " NOT open"));
|
||||
Log.i(TAG, "ring slot 0" + (open ? " open" : " NOT open"));
|
||||
}
|
||||
return open;
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
activeControllerId = NO_CONTROLLER;
|
||||
for (int slot = 0; slot < SLOTS; slot++) {
|
||||
owners[slot] = NO_CONTROLLER;
|
||||
slotControllers.set(slot, NO_CONTROLLER);
|
||||
if (playerWriters[slot] != null) {
|
||||
playerWriters[slot].destroy();
|
||||
playerWriters[slot] = null;
|
||||
}
|
||||
if (playerStates[slot] != null) playerStates[slot].clear();
|
||||
}
|
||||
systemGuidePressed = false;
|
||||
systemQamPressed = false;
|
||||
qamChordActive = false;
|
||||
@@ -155,38 +186,40 @@ 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;
|
||||
noteDevice(event.getDevice());
|
||||
if (!isFromController(event.getDevice()) || !isPadKey(event.getKeyCode())) return false;
|
||||
int slot = slotFor(event.getDevice());
|
||||
PadState pad = stateFor(slot);
|
||||
noteDevice(event.getDevice(), slot);
|
||||
boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
|
||||
switch (event.getKeyCode()) {
|
||||
case KeyEvent.KEYCODE_BUTTON_A: state.press(0, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_B: state.press(1, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_X: state.press(2, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_Y: state.press(3, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_L1: state.press(4, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R1: state.press(5, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_A: pad.press(0, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_B: pad.press(1, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_X: pad.press(2, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_Y: pad.press(3, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_L1: pad.press(4, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R1: pad.press(5, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_SELECT:
|
||||
case KeyEvent.KEYCODE_BACK: state.press(6, pressed); break;
|
||||
case KeyEvent.KEYCODE_BACK: pad.press(6, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_START:
|
||||
case KeyEvent.KEYCODE_MENU: state.press(7, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_THUMBL: state.press(8, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_THUMBR: state.press(9, pressed); break;
|
||||
case KeyEvent.KEYCODE_MENU: pad.press(7, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_THUMBL: pad.press(8, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_THUMBR: pad.press(9, pressed); break;
|
||||
// The client's own in-game menu is opened by this one; the interposer publishes it as
|
||||
// BTN_MODE, which SDL reports as the "guide" button.
|
||||
case KeyEvent.KEYCODE_BUTTON_MODE:
|
||||
case KeyEvent.KEYCODE_HOME: state.press(PadState.GUIDE, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_L2: state.leftTrigger = pressed ? 1f : 0f; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R2: state.rightTrigger = pressed ? 1f : 0f; break;
|
||||
case KeyEvent.KEYCODE_DPAD_UP: state.up = pressed; break;
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT: state.right = pressed; break;
|
||||
case KeyEvent.KEYCODE_DPAD_DOWN: state.down = pressed; break;
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT: state.left = pressed; break;
|
||||
case KeyEvent.KEYCODE_HOME: pad.press(PadState.GUIDE, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = pressed ? 1f : 0f; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = pressed ? 1f : 0f; break;
|
||||
case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break;
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break;
|
||||
case KeyEvent.KEYCODE_DPAD_DOWN: pad.down = pressed; break;
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT: pad.left = pressed; break;
|
||||
default: return false;
|
||||
}
|
||||
if (pressed) notePlayerInput();
|
||||
statButtons++;
|
||||
scheduleStats();
|
||||
publish();
|
||||
publish(slot);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -194,33 +227,35 @@ public final class PadBridge {
|
||||
public synchronized boolean onMotionEvent(MotionEvent event) {
|
||||
if (!isFromController(event.getDevice())) return false;
|
||||
if (event.getAction() != MotionEvent.ACTION_MOVE) return false;
|
||||
noteDevice(event.getDevice());
|
||||
int slot = slotFor(event.getDevice());
|
||||
PadState pad = stateFor(slot);
|
||||
noteDevice(event.getDevice(), slot);
|
||||
statAxes++;
|
||||
scheduleStats();
|
||||
state.leftX = axis(event, MotionEvent.AXIS_X);
|
||||
pad.leftX = axis(event, MotionEvent.AXIS_X);
|
||||
// Android's Y axis grows downwards and evdev's ABS_Y does too, so no flip here: what the
|
||||
// pad reports as "down" is what the client is told.
|
||||
state.leftY = axis(event, MotionEvent.AXIS_Y);
|
||||
state.rightX = axis(event, MotionEvent.AXIS_Z);
|
||||
state.rightY = axis(event, MotionEvent.AXIS_RZ);
|
||||
pad.leftY = axis(event, MotionEvent.AXIS_Y);
|
||||
pad.rightX = axis(event, MotionEvent.AXIS_Z);
|
||||
pad.rightY = axis(event, MotionEvent.AXIS_RZ);
|
||||
float lt = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
|
||||
float rt = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
|
||||
// Some pads only report the triggers on BRAKE/GAS.
|
||||
if (lt == 0f) lt = event.getAxisValue(MotionEvent.AXIS_BRAKE);
|
||||
if (rt == 0f) rt = event.getAxisValue(MotionEvent.AXIS_GAS);
|
||||
state.leftTrigger = lt;
|
||||
state.rightTrigger = rt;
|
||||
pad.leftTrigger = lt;
|
||||
pad.rightTrigger = rt;
|
||||
float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
|
||||
float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
|
||||
state.up = hatY < -0.5f;
|
||||
state.right = hatX > 0.5f;
|
||||
state.down = hatY > 0.5f;
|
||||
state.left = hatX < -0.5f;
|
||||
if (Math.max(Math.max(Math.abs(state.leftX), Math.abs(state.leftY)), Math.max(Math.abs(state.rightX), Math.abs(state.rightY))) > 0.5f
|
||||
|| lt > 0.5f || rt > 0.5f || state.up || state.right || state.down || state.left) {
|
||||
pad.up = hatY < -0.5f;
|
||||
pad.right = hatX > 0.5f;
|
||||
pad.down = hatY > 0.5f;
|
||||
pad.left = hatX < -0.5f;
|
||||
if (Math.max(Math.max(Math.abs(pad.leftX), Math.abs(pad.leftY)), Math.max(Math.abs(pad.rightX), Math.abs(pad.rightY))) > 0.5f
|
||||
|| lt > 0.5f || rt > 0.5f || pad.up || pad.right || pad.down || pad.left) {
|
||||
notePlayerInput();
|
||||
}
|
||||
publish();
|
||||
publish(slot);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -231,6 +266,7 @@ public final class PadBridge {
|
||||
public synchronized void applyTouch(java.util.function.Consumer<PadState> mutation) {
|
||||
mutation.accept(state);
|
||||
activeControllerId = NO_CONTROLLER;
|
||||
slotControllers.set(0, NO_CONTROLLER);
|
||||
notePlayerInput();
|
||||
statOnScreen++;
|
||||
scheduleStats();
|
||||
@@ -255,6 +291,18 @@ public final class PadBridge {
|
||||
public synchronized void releaseAll() {
|
||||
state.clear();
|
||||
publish();
|
||||
for (int slot = 1; slot < SLOTS; slot++) {
|
||||
if (playerWriters[slot] == null) continue;
|
||||
playerStates[slot].clear();
|
||||
publish(slot);
|
||||
}
|
||||
}
|
||||
|
||||
/** A controller went away: its player's buttons are let go, and a further player's pad is unplugged. */
|
||||
public synchronized void onDeviceRemoved(int deviceId) {
|
||||
for (int slot = 0; slot < SLOTS; slot++) {
|
||||
if (owners[slot] == deviceId) free(slot);
|
||||
}
|
||||
}
|
||||
|
||||
/** Opens the client's Quick Access menu: a tap of the Deck's button, or the Guide-then-A chord. */
|
||||
@@ -310,16 +358,71 @@ public final class PadBridge {
|
||||
publish();
|
||||
}
|
||||
|
||||
private static boolean isPadKey(int keyCode) {
|
||||
switch (keyCode) {
|
||||
case KeyEvent.KEYCODE_BACK:
|
||||
case KeyEvent.KEYCODE_MENU:
|
||||
case KeyEvent.KEYCODE_HOME:
|
||||
case KeyEvent.KEYCODE_DPAD_UP:
|
||||
case KeyEvent.KEYCODE_DPAD_RIGHT:
|
||||
case KeyEvent.KEYCODE_DPAD_DOWN:
|
||||
case KeyEvent.KEYCODE_DPAD_LEFT:
|
||||
return true;
|
||||
default:
|
||||
return KeyEvent.isGamepadButton(keyCode);
|
||||
}
|
||||
}
|
||||
|
||||
/** The slot this controller plays in: the one it has, else the first free one, else player 1's. */
|
||||
private int slotFor(InputDevice device) {
|
||||
int id = device.getId();
|
||||
if (owners[0] == id) return 0;
|
||||
for (int slot = 1; slot < SLOTS; slot++) {
|
||||
if (owners[slot] == id) return slot;
|
||||
}
|
||||
for (int slot = 0; slot < SLOTS; slot++) {
|
||||
if (owners[slot] != NO_CONTROLLER && InputDevice.getDevice(owners[slot]) == null) free(slot);
|
||||
}
|
||||
for (int slot = 0; slot < SLOTS; slot++) {
|
||||
if (owners[slot] != NO_CONTROLLER) continue;
|
||||
owners[slot] = id;
|
||||
Log.i(TAG, "\"" + device.getName() + "\" (id " + id + ") is player " + (slot + 1));
|
||||
return slot;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private PadState stateFor(int slot) {
|
||||
return slot == 0 ? state : playerStates[slot];
|
||||
}
|
||||
|
||||
private void free(int slot) {
|
||||
Log.i(TAG, "player " + (slot + 1) + " (id " + owners[slot] + ") left");
|
||||
owners[slot] = NO_CONTROLLER;
|
||||
slotControllers.set(slot, NO_CONTROLLER);
|
||||
if (slot == 0) {
|
||||
state.clear();
|
||||
publish();
|
||||
return;
|
||||
}
|
||||
playerStates[slot].clear();
|
||||
if (playerWriters[slot] != null) {
|
||||
playerWriters[slot].destroy();
|
||||
playerWriters[slot] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Per event, so the common case - the same pad as last time - is a single compare. */
|
||||
private void noteDevice(InputDevice device) {
|
||||
private void noteDevice(InputDevice device, int slot) {
|
||||
activeControllerId = device.getId();
|
||||
slotControllers.set(slot, device.getId());
|
||||
if (device.getId() == lastDeviceId) return;
|
||||
lastDeviceId = device.getId();
|
||||
statDevice = device.getName();
|
||||
if (seenDevices.add(device.getId())) {
|
||||
Log.i(TAG, String.format(java.util.Locale.ROOT, "first input from \"%s\" (%04x:%04x, id %d, sources 0x%x); ring %s, deck pad %b",
|
||||
Log.i(TAG, String.format(java.util.Locale.ROOT, "first input from \"%s\" (%04x:%04x, id %d, sources 0x%x); slot %d, ring %s, deck pad %b",
|
||||
device.getName(), device.getVendorId(), device.getProductId(), device.getId(), device.getSources(),
|
||||
open ? "open" : "not open yet", SessionState.getDeckPad()));
|
||||
slot, open ? "open" : "not open yet", SessionState.getDeckPad()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,6 +442,23 @@ public final class PadBridge {
|
||||
statButtons = statAxes = statOnScreen = statDropped = 0;
|
||||
}
|
||||
|
||||
private void publish(int slot) {
|
||||
if (slot == 0) {
|
||||
publish();
|
||||
return;
|
||||
}
|
||||
FakeInputWriter player = playerWriters[slot];
|
||||
if (player == null) {
|
||||
player = new FakeInputWriter(fakeInputDir.getAbsolutePath(), slot);
|
||||
if (!player.open()) {
|
||||
statDropped++;
|
||||
return;
|
||||
}
|
||||
playerWriters[slot] = player;
|
||||
}
|
||||
player.writePad(playerStates[slot]);
|
||||
}
|
||||
|
||||
private void publish() {
|
||||
if (!open && !start()) {
|
||||
statDropped++;
|
||||
|
||||
@@ -60,6 +60,7 @@ class PadMotion(private val context: Context, private val rotation: () -> Int) :
|
||||
Handler(worker.looper).post {
|
||||
gyro.fill(0)
|
||||
FakeInputWriter.writeMotion(SLOT, accel, gyro)
|
||||
SteamTouchDevice.current?.setMotion(accel, gyro)
|
||||
}
|
||||
worker.quitSafely()
|
||||
}
|
||||
@@ -85,6 +86,7 @@ class PadMotion(private val context: Context, private val rotation: () -> Int) :
|
||||
target[1] = counts(-z * scale)
|
||||
target[2] = counts(y * scale)
|
||||
FakeInputWriter.writeMotion(SLOT, accel, gyro)
|
||||
SteamTouchDevice.current?.setMotion(accel, gyro)
|
||||
}
|
||||
|
||||
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) = Unit
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.util.LruCache
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* What each touch control is bound to, and the icon it shows - Steam's, as its configurator keeps
|
||||
* them.
|
||||
*
|
||||
* A Steam Input binding is one string: `<action>, <label>, <icon>, <#foreground #background>`, e.g.
|
||||
* `xinput_button A, Jump, ghost_045_move_0400.png, #232323 #E4E4E4`. The icon is a file name looked
|
||||
* up as Steam does (steamui.so): the game's own `TouchMenuIcons` folder, then the binding icon
|
||||
* library `tenfoot/resource/images/library/controller/binding_icons`. Steam draws it filled with
|
||||
* the foreground colour through its own alpha (mask-image) on a button of the background colour.
|
||||
* Steam Link is sent these per control by the client it streams from; here they are read from the
|
||||
* game's touch config, and editing one writes the binding string back, as the configurator does.
|
||||
*/
|
||||
object SteamTouchBindings {
|
||||
|
||||
/** Steam's binding icon colours (the configurator's palette, steamui sp.js), and its defaults. */
|
||||
val PALETTE = listOf(
|
||||
"#FFFFFF", "#E4E4E4", "#AAAAAA", "#787878", "#434343", "#222222", "#0000AD", "#0045AD", "#0074AD",
|
||||
"#00ADAD", "#33AD69", "#00AD3D", "#00AD00", "#48B119", "#74AD00", "#96AD00", "#ADA200", "#AD5D00",
|
||||
"#AD3A00", "#AD0000", "#AD0051", "#AD007F", "#AD00AD", "#6800AD", "#4800AD",
|
||||
)
|
||||
const val DEFAULT_FOREGROUND = "#232323"
|
||||
const val DEFAULT_BACKGROUND = "#E4E4E4"
|
||||
|
||||
/** A parsed binding string. Empty strings for absent fields. */
|
||||
data class Binding(val action: String, val label: String, val icon: String, val foreground: String, val background: String) {
|
||||
fun compose(): String =
|
||||
if (icon.isEmpty() && foreground.isEmpty() && background.isEmpty()) "$action, $label, "
|
||||
else "$action, $label, $icon, ${listOf(foreground, background).filter { it.isNotEmpty() }.joinToString(" ")}"
|
||||
|
||||
val hasIcon get() = icon.isNotEmpty()
|
||||
}
|
||||
|
||||
fun parse(binding: String): Binding {
|
||||
val parts = binding.split(',').map { it.trim() }
|
||||
val colors = parts.getOrNull(3).orEmpty().split(' ').filter { it.startsWith("#") }
|
||||
return Binding(parts.getOrNull(0).orEmpty(), parts.getOrNull(1).orEmpty(), parts.getOrNull(2).orEmpty(),
|
||||
colors.getOrNull(0).orEmpty(), colors.getOrNull(1).orEmpty())
|
||||
}
|
||||
|
||||
/** Where a control's binding lives: its input in the group the preset puts on its source. */
|
||||
data class Ref(val groupId: String, val input: String)
|
||||
|
||||
/** The source and input names a control reads (D-pad: its four directions). */
|
||||
private fun inputsOf(type: Int): List<Pair<String, String>> = when (type) {
|
||||
SteamTouchConfig.A -> listOf("button_diamond" to "button_a")
|
||||
SteamTouchConfig.B -> listOf("button_diamond" to "button_b")
|
||||
SteamTouchConfig.X -> listOf("button_diamond" to "button_x")
|
||||
SteamTouchConfig.Y -> listOf("button_diamond" to "button_y")
|
||||
SteamTouchConfig.SELECT -> listOf("switch" to "button_menu")
|
||||
SteamTouchConfig.START -> listOf("switch" to "button_escape")
|
||||
SteamTouchConfig.BUMPER_LEFT -> listOf("switch" to "left_bumper")
|
||||
SteamTouchConfig.BUMPER_RIGHT -> listOf("switch" to "right_bumper")
|
||||
SteamTouchConfig.TRIGGER_LEFT -> listOf("left_trigger" to "click")
|
||||
SteamTouchConfig.TRIGGER_RIGHT -> listOf("right_trigger" to "click")
|
||||
SteamTouchConfig.JOYSTICK_LEFT_BUTTON -> listOf("joystick" to "click")
|
||||
SteamTouchConfig.JOYSTICK_RIGHT_BUTTON -> listOf("right_joystick" to "click")
|
||||
SteamTouchConfig.MACRO_1_FINGER -> listOf("switch" to "button_macro_1finger")
|
||||
SteamTouchConfig.MACRO_2_FINGER -> listOf("switch" to "button_macro_2finger")
|
||||
SteamTouchConfig.DPAD -> DPAD_INPUTS.map { "dpad" to it }
|
||||
in SteamTouchConfig.MACRO_0..SteamTouchConfig.MACRO_0 + 7 -> listOf("switch" to "button_macro${type - SteamTouchConfig.MACRO_0}")
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
/** D-pad directions, in the order [refsFor] returns them: up, down, left, right. */
|
||||
val DPAD_INPUTS = listOf("dpad_north", "dpad_south", "dpad_west", "dpad_east")
|
||||
|
||||
/**
|
||||
* Where control [type]'s bindings are for preset [preset] (the action set less one) and its
|
||||
* active [layers]: a layer's group wins over the base's for the same source.
|
||||
*/
|
||||
fun refsFor(mappings: KeyValues.Node, type: Int, preset: Int, layers: List<Int> = emptyList()): List<Ref?> {
|
||||
val presets = mappings.children("preset").associateBy { it.string("id")?.toIntOrNull() ?: -1 }
|
||||
val stack = layers.reversed().mapNotNull { presets[it - 1] } + listOfNotNull(presets[preset] ?: presets.values.firstOrNull())
|
||||
return inputsOf(type).map { (source, input) ->
|
||||
stack.firstNotNullOfOrNull { p -> groupFor(p, source)?.let { Ref(it, input) } }
|
||||
}
|
||||
}
|
||||
|
||||
private fun groupFor(preset: KeyValues.Node, source: String): String? =
|
||||
preset.child("group_source_bindings")?.entries?.firstOrNull { (_, v) ->
|
||||
val parts = (v as? String)?.split(' ') ?: return@firstOrNull false
|
||||
parts.getOrNull(0) == source && parts.getOrNull(1) == "active" && !parts.contains("modeshift")
|
||||
}?.first
|
||||
|
||||
/** The binding at [ref]: its first activator's first binding (Full_Press when there is one). */
|
||||
fun bindingAt(mappings: KeyValues.Node, ref: Ref): Binding? {
|
||||
val group = mappings.children("group").firstOrNull { it.string("id") == ref.groupId } ?: return null
|
||||
val activators = group.child("inputs")?.child(ref.input)?.child("activators") ?: return null
|
||||
val activator = activators.child("Full_Press") ?: activators.entries.firstNotNullOfOrNull { it.second as? KeyValues.Node }
|
||||
return activator?.child("bindings")?.string("binding")?.let(::parse)
|
||||
}
|
||||
|
||||
/** A short glyph for a binding with no icon or label: what it presses. */
|
||||
fun glyph(binding: Binding?): String? {
|
||||
val words = binding?.action?.split(' ') ?: return null
|
||||
val arg = words.getOrNull(1)?.uppercase() ?: return null
|
||||
return when (words[0]) {
|
||||
"xinput_button" -> XINPUT_GLYPHS[arg] ?: arg
|
||||
"key_press" -> KEY_GLYPHS[arg] ?: arg.take(4)
|
||||
"mouse_button" -> when (arg) { "LEFT" -> "LMB"; "RIGHT" -> "RMB"; "MIDDLE" -> "MMB"; else -> "M" + arg.take(2) }
|
||||
"mouse_wheel" -> if (arg.contains("UP")) "▲" else "▼"
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private val XINPUT_GLYPHS = mapOf(
|
||||
"SHOULDER_LEFT" to "LB", "SHOULDER_RIGHT" to "RB", "TRIGGER_LEFT" to "LT", "TRIGGER_RIGHT" to "RT",
|
||||
"JOYSTICK_LEFT" to "L3", "JOYSTICK_RIGHT" to "R3", "START" to "☰", "SELECT" to "⧉", "BACK" to "⧉",
|
||||
"DPAD_UP" to "▲", "DPAD_DOWN" to "▼", "DPAD_LEFT" to "◀", "DPAD_RIGHT" to "▶",
|
||||
)
|
||||
private val KEY_GLYPHS = mapOf(
|
||||
"RETURN" to "⏎", "SPACE" to "␣", "ESCAPE" to "Esc", "TAB" to "Tab", "BACKSPACE" to "⌫",
|
||||
"LEFT_SHIFT" to "⇧", "RIGHT_SHIFT" to "⇧", "LEFT_CONTROL" to "Ctrl", "RIGHT_CONTROL" to "Ctrl",
|
||||
"LEFT_ALT" to "Alt", "RIGHT_ALT" to "Alt", "UP_ARROW" to "↑", "DOWN_ARROW" to "↓",
|
||||
"LEFT_ARROW" to "←", "RIGHT_ARROW" to "→",
|
||||
)
|
||||
|
||||
// ---- Writing a binding back ----
|
||||
|
||||
/**
|
||||
* Rewrites every binding of [ref]'s input in config [text] (VDF) with [change], keeping the rest
|
||||
* of the file byte for byte. Returns the new text, or null when the input is not there.
|
||||
*/
|
||||
fun rewrite(text: String, ref: Ref, change: (Binding) -> Binding): String? {
|
||||
val tokens = Vdf.tokens(text)
|
||||
val group = Vdf.blocks(tokens, 0, tokens.size, "group").firstOrNull { (s, e) ->
|
||||
Vdf.value(tokens, s, e, "id") == ref.groupId
|
||||
} ?: return null
|
||||
val inputs = Vdf.blocks(tokens, group.first, group.second, "inputs").firstOrNull() ?: return null
|
||||
val input = Vdf.blocks(tokens, inputs.first, inputs.second, ref.input).firstOrNull() ?: return null
|
||||
val targets = (input.first until input.second).filter { i ->
|
||||
tokens[i].text.equals("binding", true) && tokens[i].quoted && i + 1 < input.second && tokens[i + 1].quoted
|
||||
}.map { tokens[it + 1] }
|
||||
if (targets.isEmpty()) return null
|
||||
val out = StringBuilder(text)
|
||||
for (t in targets.sortedByDescending { it.start }) out.replace(t.start, t.end, change(parse(t.text)).compose())
|
||||
return out.toString()
|
||||
}
|
||||
|
||||
/** A VDF tokenizer that keeps where each token sits, for edits that leave the rest untouched. */
|
||||
private object Vdf {
|
||||
class Token(val text: String, val quoted: Boolean, val start: Int, val end: Int)
|
||||
|
||||
fun tokens(text: String): List<Token> {
|
||||
val out = mutableListOf<Token>()
|
||||
var i = 0
|
||||
while (i < text.length) {
|
||||
val c = text[i]
|
||||
when {
|
||||
c.isWhitespace() -> i++
|
||||
c == '/' && i + 1 < text.length && text[i + 1] == '/' -> while (i < text.length && text[i] != '\n') i++
|
||||
c == '{' || c == '}' -> { out += Token(c.toString(), false, i, i + 1); i++ }
|
||||
c == '"' -> {
|
||||
val start = i + 1
|
||||
i++
|
||||
while (i < text.length && text[i] != '"') { if (text[i] == '\\') i++; i++ }
|
||||
out += Token(text.substring(start, minOf(i, text.length)), true, start, minOf(i, text.length))
|
||||
i++
|
||||
}
|
||||
else -> {
|
||||
val start = i
|
||||
while (i < text.length && !text[i].isWhitespace() && text[i] != '{' && text[i] != '}' && text[i] != '"') i++
|
||||
out += Token(text.substring(start, i), false, start, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** The bodies (token index ranges inside the braces) of direct children named [key] of the
|
||||
* block spanning [from, to). */
|
||||
fun blocks(tokens: List<Token>, from: Int, to: Int, key: String): List<Pair<Int, Int>> {
|
||||
val found = mutableListOf<Pair<Int, Int>>()
|
||||
var i = from
|
||||
var depth = 0
|
||||
while (i < to) {
|
||||
val t = tokens[i]
|
||||
if (t.text == "{" && !t.quoted) depth++
|
||||
else if (t.text == "}" && !t.quoted) depth--
|
||||
else if (depth == 0 && t.quoted && t.text.equals(key, true) && i + 1 < to && tokens[i + 1].text == "{" && !tokens[i + 1].quoted) {
|
||||
val open = i + 1
|
||||
var d = 0
|
||||
var j = open
|
||||
while (j < to) {
|
||||
if (tokens[j].text == "{" && !tokens[j].quoted) d++
|
||||
if (tokens[j].text == "}" && !tokens[j].quoted) { d--; if (d == 0) break }
|
||||
j++
|
||||
}
|
||||
found += (open + 1) to j
|
||||
i = j + 1
|
||||
continue
|
||||
}
|
||||
i++
|
||||
}
|
||||
// The whole file is one top-level block ("controller_mappings"): look inside it too.
|
||||
if (found.isEmpty() && from == 0) {
|
||||
val top = tokens.indexOfFirst { it.text == "{" && !it.quoted }
|
||||
if (top > 0) {
|
||||
var d = 0
|
||||
var j = top
|
||||
while (j < to) {
|
||||
if (tokens[j].text == "{" && !tokens[j].quoted) d++
|
||||
if (tokens[j].text == "}" && !tokens[j].quoted) { d--; if (d == 0) break }
|
||||
j++
|
||||
}
|
||||
return blocks(tokens, top + 1, j, key)
|
||||
}
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
/** A direct key-value of the block spanning [from, to). */
|
||||
fun value(tokens: List<Token>, from: Int, to: Int, key: String): String? {
|
||||
var depth = 0
|
||||
var i = from
|
||||
while (i < to) {
|
||||
val t = tokens[i]
|
||||
if (t.text == "{" && !t.quoted) depth++
|
||||
else if (t.text == "}" && !t.quoted) depth--
|
||||
else if (depth == 0 && t.quoted && t.text.equals(key, true) && i + 1 < to && tokens[i + 1].quoted) return tokens[i + 1].text
|
||||
i++
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Icons ----
|
||||
|
||||
private val cache = object : LruCache<String, Bitmap>(24 * 1024 * 1024) {
|
||||
override fun sizeOf(key: String, value: Bitmap) = value.byteCount
|
||||
}
|
||||
private val missing = HashSet<String>()
|
||||
|
||||
fun libraryDir(context: Context) = File(SteamTouchConfig.steamRoot(context), "tenfoot/resource/images/library/controller/binding_icons")
|
||||
|
||||
/** The game's own icons folder (`TouchMenuIcons` in its install directory), when it has one. */
|
||||
fun gameIconsDir(context: Context, appId: Int): File? = try {
|
||||
val manifest = File(SteamTouchConfig.steamRoot(context), "steamapps/appmanifest_$appId.acf")
|
||||
val installDir = KeyValues.parse(manifest.readText()).let { it.child("AppState") ?: it }.string("installdir")
|
||||
installDir?.let { File(SteamTouchConfig.steamRoot(context), "steamapps/common/$it/TouchMenuIcons") }?.takeIf { it.isDirectory }
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
fun iconFile(context: Context, appId: Int, name: String): File? {
|
||||
if (name.isEmpty() || name.contains('/')) return null
|
||||
gameIconsDir(context, appId)?.let { File(it, name) }?.takeIf { it.isFile }?.let { return it }
|
||||
File(libraryDir(context), name).takeIf { it.isFile }?.let { return it }
|
||||
return File(SteamTouchConfig.steamRoot(context), "steamui/images/controller/$name").takeIf { it.isFile }
|
||||
}
|
||||
|
||||
/** An icon, decoded at about [size] px. Blocking (reads a file); cached. */
|
||||
fun icon(context: Context, appId: Int, name: String, size: Int = 128): Bitmap? {
|
||||
val key = "$appId/$name/$size"
|
||||
cache.get(key)?.let { return it }
|
||||
if (key in missing) return null
|
||||
val file = iconFile(context, appId, name)
|
||||
val bitmap = file?.let {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
BitmapFactory.decodeFile(it.path, bounds)
|
||||
var sample = 1
|
||||
while (bounds.outWidth / (sample * 2) >= size) sample *= 2
|
||||
BitmapFactory.decodeFile(it.path, BitmapFactory.Options().apply { inSampleSize = sample })
|
||||
}
|
||||
if (bitmap == null) missing += key else cache.put(key, bitmap)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
fun cachedIcon(appId: Int, name: String, size: Int = 128): Bitmap? = cache.get("$appId/$name/$size")
|
||||
|
||||
/** Icon names for the picker: the game's own first, then Steam's library by category. */
|
||||
fun iconNames(context: Context, appId: Int): List<String> {
|
||||
val own = gameIconsDir(context, appId)?.list()?.filter { it.endsWith(".png", true) }?.sorted().orEmpty()
|
||||
val library = libraryDir(context).list()?.filter { it.endsWith(".png", true) && it != "special_blank.png" }?.sorted().orEmpty()
|
||||
return own + library.filter { it !in own }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,654 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.RandomAccessFile
|
||||
|
||||
/**
|
||||
* Steam's touch configs, read and written where Steam keeps them.
|
||||
*
|
||||
* A touch config is a Steam Input config (`controller_mobile_touch`) like any other - bindings per
|
||||
* action set, synced to the account, shared as community configs - with one extra key,
|
||||
* `touch_layout`: where each on-screen control sits, per action set, as a hex-encoded
|
||||
* `CVirtualControllerLayouts` protobuf (steammessages_virtualcontroller.proto). Steam Link is
|
||||
* handed both by the client it streams from; here the client tells the device only the app and
|
||||
* action set it is on (SteamTouchDevice), so the config is found on disk: the file the client's
|
||||
* console log says it loaded for the touch controller, else the app's entry in
|
||||
* `configset_controller_mobile_touch.vdf`. With no layout in it, controls go where Steam Link puts
|
||||
* them by default. Saving a layout writes `touch_layout` into the app's autosave config, as the
|
||||
* client does when Steam Link saves one; Steam Cloud picks that file up like any autosave.
|
||||
* (docs/development/steam-touch-controller.md.)
|
||||
*/
|
||||
object SteamTouchConfig {
|
||||
private const val TAG = "SteamTouch"
|
||||
|
||||
// EControllerElementType.
|
||||
const val THUMB = 0
|
||||
const val STEAM = 1
|
||||
const val JOYSTICK_LEFT = 2
|
||||
const val JOYSTICK_LEFT_BUTTON = 3
|
||||
const val JOYSTICK_RIGHT = 4
|
||||
const val JOYSTICK_RIGHT_BUTTON = 5
|
||||
const val DPAD = 6
|
||||
const val A = 7
|
||||
const val B = 8
|
||||
const val X = 9
|
||||
const val Y = 10
|
||||
const val SELECT = 11
|
||||
const val START = 12
|
||||
const val TRIGGER_LEFT = 13
|
||||
const val TRIGGER_RIGHT = 14
|
||||
const val BUMPER_LEFT = 15
|
||||
const val BUMPER_RIGHT = 16
|
||||
const val MACRO_0 = 17 // 17..24 = macros 0-7
|
||||
const val TRACKPAD_CENTER = 25
|
||||
const val TRACKPAD_LEFT = 26
|
||||
const val TRACKPAD_RIGHT = 27
|
||||
const val KEYBOARD = 28
|
||||
const val MACRO_1_FINGER = 30
|
||||
const val MACRO_2_FINGER = 31
|
||||
const val MAGNIFYING_GLASS = 29
|
||||
const val PASTE = 34
|
||||
|
||||
/** Controls a layout may place though no binding asks for them (Steam Link's tray). */
|
||||
val OPTIONAL = setOf(PASTE)
|
||||
|
||||
/** A control's place: centre as a fraction of the screen, and its scale. */
|
||||
data class Element(val type: Int, val visible: Boolean, val x: Float, val y: Float, val xScale: Float = 1f, val yScale: Float = 1f)
|
||||
|
||||
data class Layout(val actionSet: Int, val elements: List<Element>, val color: FloatArray?, val version: Int?)
|
||||
|
||||
/** A decoded `touch_layout`; [rest] is every other top-level field, written back untouched. */
|
||||
class Layouts(val layouts: List<Layout>, val rest: ByteArray) {
|
||||
fun forActionSet(id: Int): Layout? = layouts.firstOrNull { it.actionSet == id }
|
||||
|
||||
/** The per-game options Steam Link keeps beside the layouts (CVirtualControllerLayouts
|
||||
* fields 2-4): input mode, mouse mode, trackpad sensitivity. */
|
||||
val options: Options get() = Options.decode(rest)
|
||||
}
|
||||
|
||||
/** EInputMode: 1 mouse, 2 controller, 3 both. EMouseMode: 2 absolute cursor (direct touch),
|
||||
* 3 touch, 4 relative (trackpad); 0 = not set. */
|
||||
data class Options(val inputMode: Int = INPUT_CONTROLLER, val mouseMode: Int = 0, val trackpadSensitivity: Float = 1f) {
|
||||
/** [rest] with these options in place of its own fields 2-4, the rest kept as it was. */
|
||||
fun encodeInto(rest: ByteArray): ByteArray {
|
||||
val out = ByteArrayOutputStream()
|
||||
val p = Proto(rest)
|
||||
while (p.more()) {
|
||||
val start = p.pos
|
||||
val (field, wire) = p.key()
|
||||
p.skip(wire)
|
||||
if (field !in 2..4) out.write(rest, start, p.pos - start)
|
||||
}
|
||||
tag(out, 2, 0); varint(out, inputMode.toLong())
|
||||
if (mouseMode != 0) { tag(out, 3, 0); varint(out, mouseMode.toLong()) }
|
||||
tag(out, 4, 5); fixed(out, trackpadSensitivity)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun decode(rest: ByteArray): Options {
|
||||
var input = INPUT_CONTROLLER
|
||||
var mouse = 0
|
||||
var sensitivity = 1f
|
||||
val p = Proto(rest)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
when {
|
||||
field == 2 && wire == 0 -> input = p.varint().toInt()
|
||||
field == 3 && wire == 0 -> mouse = p.varint().toInt()
|
||||
field == 4 && wire == 5 -> sensitivity = p.float()
|
||||
else -> p.skip(wire)
|
||||
}
|
||||
}
|
||||
return Options(input, mouse, sensitivity)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const val INPUT_MOUSE = 1
|
||||
const val INPUT_CONTROLLER = 2
|
||||
const val INPUT_BOTH = 3
|
||||
const val MOUSE_ABSOLUTE = 2
|
||||
const val MOUSE_TOUCH = 3
|
||||
const val MOUSE_RELATIVE = 4
|
||||
|
||||
/** The config's action sets in preset order: id as the device is told (preset id + 1) and title. */
|
||||
fun actionSets(config: Config): List<Pair<Int, String>> {
|
||||
val mappings = config.mappings ?: return listOf(1 to "Default")
|
||||
val titles = mappings.child("actions")?.entries?.mapNotNull { (key, v) ->
|
||||
(v as? KeyValues.Node)?.let { key to (it.string("title") ?: key) }
|
||||
}?.toMap().orEmpty()
|
||||
val sets = mappings.children("preset").mapNotNull { preset ->
|
||||
val id = preset.string("id")?.toIntOrNull() ?: return@mapNotNull null
|
||||
val name = preset.string("name") ?: ""
|
||||
(id + 1) to (titles[name]?.takeUnless { it.startsWith("#") } ?: name.ifEmpty { "Set ${id + 1}" })
|
||||
}.sortedBy { it.first }
|
||||
return sets.ifEmpty { listOf(1 to "Default") }
|
||||
}
|
||||
|
||||
/** A config as the overlay needs it. */
|
||||
class Config(
|
||||
val file: File?,
|
||||
val text: String?,
|
||||
val layouts: Layouts?,
|
||||
/** Bound controls per preset (action set id - 1). */
|
||||
val available: Map<Int, Set<Int>>,
|
||||
/** The parsed config, for its bindings (SteamTouchBindings). */
|
||||
val mappings: KeyValues.Node? = null,
|
||||
) {
|
||||
/** The config's own title, as Steam Link's tray shows it ("Default", "Gamepad"…). */
|
||||
val title: String? get() = mappings?.string("title")?.let { t ->
|
||||
if (t.startsWith("#")) mappings.child("localization")?.child("english")?.string("title") ?: "Default" else t
|
||||
}
|
||||
|
||||
/** The controls to show for [actionSet] (as the device is told it) and [layers]. */
|
||||
fun availableFor(actionSet: Int, layers: List<Int>): Set<Int> {
|
||||
val base = available[presetOf(actionSet)] ?: available.values.firstOrNull() ?: DEFAULT_SET
|
||||
return layers.fold(base) { set, layer -> set + (available[presetOf(layer)] ?: emptySet()) } + ALWAYS
|
||||
}
|
||||
}
|
||||
|
||||
/** Big Picture's own config uses Steam's basic UI bindings: the default set. */
|
||||
private val DEFAULT_SET = setOf(DPAD, A, B, X, Y, SELECT, START)
|
||||
private val ALWAYS = setOf(STEAM, THUMB, KEYBOARD)
|
||||
|
||||
/** The layout entry for the action set the device was told about (the client counts from 1,
|
||||
* 0 for an app with none). */
|
||||
fun layoutIdOf(actionSet: Int) = if (actionSet <= 0) 1 else actionSet
|
||||
private fun presetOf(actionSet: Int) = layoutIdOf(actionSet) - 1
|
||||
|
||||
// ---- Where Steam keeps them ----
|
||||
|
||||
/** A game's name from its app manifest, when it is installed. */
|
||||
fun gameName(context: Context, appId: Int): String? = try {
|
||||
val kv = KeyValues.parse(File(steamRoot(context), "steamapps/appmanifest_$appId.acf").readText())
|
||||
(kv.child("AppState") ?: kv).string("name")
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
fun steamRoot(context: Context) = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam")
|
||||
|
||||
fun configDir(context: Context): File? {
|
||||
val base = File(steamRoot(context), "steamapps/common/Steam Controller Configs")
|
||||
val account = accountId(context)
|
||||
if (account != null) File(base, "$account/config").takeIf { it.isDirectory }?.let { return it }
|
||||
return base.listFiles()?.filter { it.name.all(Char::isDigit) && File(it, "config").isDirectory }
|
||||
?.maxByOrNull { it.lastModified() }?.let { File(it, "config") }
|
||||
}
|
||||
|
||||
/** The signed-in account (loginusers.vdf's MostRecent) as a 32-bit account id. */
|
||||
private fun accountId(context: Context): Long? = try {
|
||||
val users = KeyValues.parse(File(steamRoot(context), "config/loginusers.vdf").readText())
|
||||
val node = users.child("users")
|
||||
val entries = node?.entries?.mapNotNull { (k, v) -> (v as? KeyValues.Node)?.let { k to it } }.orEmpty()
|
||||
val chosen = entries.firstOrNull { it.second.string("MostRecent") == "1" } ?: entries.firstOrNull()
|
||||
chosen?.first?.toLongOrNull()?.minus(76561197960265728L)
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
/** The config the client has for app [appId] on the touch controller. Blocking: reads files. */
|
||||
fun load(context: Context, appId: Int): Config {
|
||||
val dir = configDir(context)
|
||||
// The app's own saved config is what the client loads when there is one (its configset
|
||||
// entry); otherwise what it logged loading - a template, an official or a workshop config.
|
||||
val file = dir?.let { autosaveConfig(it, appId) }
|
||||
?: loggedConfig(context, appId)?.takeIf { isTouchConfig(it) }
|
||||
?: dir?.let { configsetConfig(context, it, appId) }
|
||||
if (file == null) {
|
||||
Log.i(TAG, "steam touch: app $appId has no touch config on disk; default layout")
|
||||
return Config(null, null, null, emptyMap())
|
||||
}
|
||||
return try {
|
||||
val text = file.readText()
|
||||
val kv = KeyValues.parse(text.removePrefix(""))
|
||||
val mappings = kv.child("controller_mappings") ?: kv
|
||||
val layouts = mappings.string("touch_layout")?.let { hex -> decodeLayouts(hexToBytes(hex)) }
|
||||
Log.i(TAG, "steam touch: app $appId uses ${file.path} (${layouts?.layouts?.size ?: 0} layouts)")
|
||||
Config(file, text, layouts, availability(mappings), mappings)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "steam touch: ${file.path} unreadable: $e")
|
||||
Config(file, null, null, emptyMap())
|
||||
}
|
||||
}
|
||||
|
||||
private fun isTouchConfig(file: File) = file.isFile &&
|
||||
file.bufferedReader().use { r -> generateSequence { r.readLine() }.take(40).any { it.contains("controller_mobile_touch") } }
|
||||
|
||||
/** From the client's console log: the last config it loaded for [appId] on the touch controller
|
||||
* (`Loaded Config for ... App ID <n>, Controller <i>: <path>`, where controller i reported
|
||||
* type 43). Guest paths come back as the app's own. */
|
||||
private fun loggedConfig(context: Context, appId: Int): File? = try {
|
||||
val log = File(steamRoot(context), "logs/console_log.txt")
|
||||
val tail = RandomAccessFile(log, "r").use { raf ->
|
||||
val start = maxOf(0L, raf.length() - 2_000_000L)
|
||||
raf.seek(start)
|
||||
val bytes = ByteArray((raf.length() - start).toInt())
|
||||
raf.readFully(bytes)
|
||||
String(bytes)
|
||||
}
|
||||
val touchIndex = Regex("""!! Controller (\d+) attributes:\s*\n\s*Type: 43""").findAll(tail).lastOrNull()?.groupValues?.get(1)
|
||||
if (touchIndex == null) null else {
|
||||
val loaded = Regex("""Loaded Config for [^\n]*? Path for App ID $appId, Controller $touchIndex: ([^\n]+)""")
|
||||
.findAll(tail).lastOrNull()?.groupValues?.get(1)?.trim()
|
||||
loaded?.let { guestPath -> File(LinuxRuntime.rootDir(context), guestPath.trimStart('/').replace("//", "/")) }
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
private fun autosaveConfig(dir: File, appId: Int): File? = try {
|
||||
val set = KeyValues.parse(File(dir, CONFIGSET).readText()).child("controller_config")?.child(appId.toString())
|
||||
if (set?.string("autosave") == "1") File(dir, "$appId/controller_mobile_touch.vdf").takeIf { it.isFile } else null
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
/** The app's entry in configset_controller_mobile_touch.vdf. */
|
||||
private fun configsetConfig(context: Context, dir: File, appId: Int): File? = try {
|
||||
val set = KeyValues.parse(File(dir, CONFIGSET).readText()).child("controller_config")?.child(appId.toString())
|
||||
when {
|
||||
set == null -> null
|
||||
set.string("autosave") == "1" -> File(dir, "$appId/controller_mobile_touch.vdf")
|
||||
set.string("template") != null -> File(steamRoot(context), "controller_base/templates/" + set.string("template"))
|
||||
else -> null
|
||||
}?.takeIf { it.isFile }
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
|
||||
private const val CONFIGSET = "configset_controller_mobile_touch.vdf"
|
||||
|
||||
// ---- What a config binds ----
|
||||
|
||||
/** Per preset, the controls its active sources bind - what Steam Link shows (UpdateElementAvailability). */
|
||||
private fun availability(mappings: KeyValues.Node): Map<Int, Set<Int>> {
|
||||
val groups = mappings.children("group").associateBy({ it.string("id") ?: "" }) { group ->
|
||||
group.child("inputs")?.entries?.filter { (_, v) -> (v as? KeyValues.Node)?.let(::hasBinding) == true }
|
||||
?.map { it.first }?.toSet().orEmpty()
|
||||
}
|
||||
return mappings.children("preset").associate { preset ->
|
||||
val id = preset.string("id")?.toIntOrNull() ?: 0
|
||||
val set = mutableSetOf<Int>()
|
||||
preset.child("group_source_bindings")?.entries?.forEach { (groupId, value) ->
|
||||
val parts = (value as? String)?.split(' ') ?: return@forEach
|
||||
if (parts.getOrNull(1) != "active" || parts.contains("modeshift")) return@forEach
|
||||
val bound = groups[groupId].orEmpty()
|
||||
when (parts[0]) {
|
||||
"button_diamond" -> mapOf("button_a" to A, "button_b" to B, "button_x" to X, "button_y" to Y)
|
||||
.forEach { (input, element) -> if (input in bound) set += element }
|
||||
"dpad" -> set += DPAD
|
||||
"joystick" -> { set += JOYSTICK_LEFT; if ("click" in bound) set += JOYSTICK_LEFT_BUTTON }
|
||||
"right_joystick" -> { set += JOYSTICK_RIGHT; if ("click" in bound) set += JOYSTICK_RIGHT_BUTTON }
|
||||
"left_trigger" -> set += TRIGGER_LEFT
|
||||
"right_trigger" -> set += TRIGGER_RIGHT
|
||||
"left_trackpad" -> set += TRACKPAD_LEFT
|
||||
"right_trackpad" -> set += TRACKPAD_RIGHT
|
||||
"center_trackpad" -> set += TRACKPAD_CENTER
|
||||
"switch" -> bound.forEach { input ->
|
||||
when (input) {
|
||||
"button_escape" -> set += START
|
||||
"button_menu" -> set += SELECT
|
||||
"left_bumper" -> set += BUMPER_LEFT
|
||||
"right_bumper" -> set += BUMPER_RIGHT
|
||||
"button_macro_1finger" -> set += MACRO_1_FINGER
|
||||
"button_macro_2finger" -> set += MACRO_2_FINGER
|
||||
else -> Regex("button_macro(\\d)").matchEntire(input)?.let { set += MACRO_0 + it.groupValues[1].toInt() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
id to set.toSet()
|
||||
}
|
||||
}
|
||||
|
||||
private fun hasBinding(input: KeyValues.Node): Boolean =
|
||||
input.child("activators")?.entries?.any { (_, activator) ->
|
||||
(activator as? KeyValues.Node)?.child("bindings")?.entries?.any { (k, v) -> k == "binding" && (v as? String)?.isNotBlank() == true } == true
|
||||
} == true
|
||||
|
||||
// ---- Steam Link's defaults ----
|
||||
|
||||
/** Where Steam Link puts a control with no layout (CVirtualController::BInitializeDefaultElement,
|
||||
* on a 1280x720 screen), and the same scheme for the controls its table leaves out, made room
|
||||
* for when a config binds sticks. */
|
||||
fun defaultElement(type: Int, available: Set<Int>): Element? {
|
||||
val leftStick = JOYSTICK_LEFT in available
|
||||
val rightStick = JOYSTICK_RIGHT in available
|
||||
val faceY = if (rightStick) -0.30f else 0f
|
||||
val p: Pair<Float, Float> = when (type) {
|
||||
THUMB -> 75f / 1280 to 75f / 720
|
||||
STEAM -> 636f / 1280 to 75f / 720
|
||||
SELECT -> 516f / 1280 to 75f / 720
|
||||
START -> 756f / 1280 to 75f / 720
|
||||
KEYBOARD -> 1205f / 1280 to 75f / 720
|
||||
PASTE -> 1115f / 1280 to 75f / 720
|
||||
DPAD -> if (leftStick) 0.147f to 0.47f else 200f / 1280 to 525f / 720
|
||||
A -> 1083f / 1280 to 607f / 720 + faceY
|
||||
B -> 1163f / 1280 to 527f / 720 + faceY
|
||||
X -> 1003f / 1280 to 527f / 720 + faceY
|
||||
Y -> 1083f / 1280 to 447f / 720 + faceY
|
||||
JOYSTICK_LEFT -> 0.137f to 0.76f
|
||||
JOYSTICK_RIGHT -> 0.85f to 0.76f
|
||||
JOYSTICK_LEFT_BUTTON -> 0.29f to 0.88f
|
||||
JOYSTICK_RIGHT_BUTTON -> 0.71f to 0.88f
|
||||
BUMPER_LEFT -> 0.06f to 0.205f
|
||||
TRIGGER_LEFT -> 0.16f to 0.205f
|
||||
BUMPER_RIGHT -> 0.94f to 0.205f
|
||||
TRIGGER_RIGHT -> 0.84f to 0.205f
|
||||
TRACKPAD_LEFT -> 0.3f to 0.55f
|
||||
TRACKPAD_CENTER -> 0.5f to 0.55f
|
||||
TRACKPAD_RIGHT -> 0.7f to 0.55f
|
||||
MACRO_1_FINGER -> 0.42f to 0.92f
|
||||
MACRO_2_FINGER -> 0.58f to 0.92f
|
||||
in MACRO_0..MACRO_0 + 7 -> (0.3f + 0.057f * (type - MACRO_0)) to 0.22f
|
||||
else -> return null
|
||||
}
|
||||
return Element(type, true, p.first, p.second)
|
||||
}
|
||||
|
||||
/** The controls to draw: the layout's own where it has one, Steam Link's defaults otherwise,
|
||||
* only those the config binds. */
|
||||
fun elementsFor(config: Config, actionSet: Int, layers: List<Int>): List<Element> {
|
||||
val available = config.availableFor(actionSet, layers)
|
||||
val layout = config.layouts?.forActionSet(layoutIdOf(actionSet))
|
||||
val placed = layout?.elements?.associateBy { it.type }.orEmpty()
|
||||
val optional = OPTIONAL.filter { placed[it]?.visible == true && it !in available }
|
||||
return (available + optional).mapNotNull { type ->
|
||||
val own = placed[type]
|
||||
when {
|
||||
own != null && !own.visible -> null
|
||||
own != null -> own
|
||||
else -> defaultElement(type, available)
|
||||
}
|
||||
}.sortedBy { it.type }
|
||||
}
|
||||
|
||||
fun layoutColor(config: Config, actionSet: Int): FloatArray? = config.layouts?.forActionSet(layoutIdOf(actionSet))?.color
|
||||
|
||||
// ---- Saving ----
|
||||
|
||||
/**
|
||||
* Saves [elements] as the layout of [actionSet] in app [appId]'s touch config: into the app's
|
||||
* autosave config (`<appid>/controller_mobile_touch.vdf`, made from the config in use when the
|
||||
* app has none) and selected for the app in the configset, as the client saves a layout Steam
|
||||
* Link sends it. Returns the file written, or null.
|
||||
*/
|
||||
fun saveLayout(
|
||||
context: Context,
|
||||
appId: Int,
|
||||
config: Config,
|
||||
actionSet: Int,
|
||||
elements: List<Element>,
|
||||
color: FloatArray? = null,
|
||||
bindings: Map<SteamTouchBindings.Ref, (SteamTouchBindings.Binding) -> SteamTouchBindings.Binding> = emptyMap(),
|
||||
): File? = saveLayouts(context, appId, config, mapOf(layoutIdOf(actionSet) to elements), color?.let { c -> setOf(layoutIdOf(actionSet)).associateWith { c } } ?: emptyMap(), bindings)
|
||||
|
||||
/**
|
||||
* Saves the layouts of several action sets (by layout id) at once, with their colours, any
|
||||
* binding edits and the per-game [options], into the game's autosave touch config.
|
||||
*/
|
||||
fun saveLayouts(
|
||||
context: Context,
|
||||
appId: Int,
|
||||
config: Config,
|
||||
sets: Map<Int, List<Element>>,
|
||||
colors: Map<Int, FloatArray> = emptyMap(),
|
||||
bindings: Map<SteamTouchBindings.Ref, (SteamTouchBindings.Binding) -> SteamTouchBindings.Binding> = emptyMap(),
|
||||
options: Options? = null,
|
||||
): File? {
|
||||
val dir = configDir(context) ?: return null
|
||||
// Icons, labels and colours live in the binding strings (SteamTouchBindings), as the
|
||||
// configurator writes them.
|
||||
val source = bindings.entries.fold(config.text ?: return null) { text, (ref, change) ->
|
||||
SteamTouchBindings.rewrite(text, ref, change) ?: text
|
||||
}
|
||||
val target = File(dir, "$appId/controller_mobile_touch.vdf")
|
||||
val old = config.layouts
|
||||
val keep = old?.layouts?.filter { it.actionSet !in sets.keys }.orEmpty()
|
||||
val written = sets.map { (id, elements) ->
|
||||
Layout(id, elements, colors[id] ?: old?.forActionSet(id)?.color ?: floatArrayOf(1f, 1f, 1f, 0.4f), old?.forActionSet(id)?.version)
|
||||
}
|
||||
val rest = old?.rest ?: byteArrayOf(0x10, 0x02) // input_mode = controller
|
||||
val layouts = Layouts((keep + written).sortedBy { it.actionSet }, options?.encodeInto(rest) ?: rest)
|
||||
val hex = bytesToHex(encodeLayouts(layouts))
|
||||
val line = "\t\"touch_layout\"\t\t\"$hex\""
|
||||
val existing = Regex("""\n\s*"touch_layout"\s+"[0-9a-fA-F]*"""")
|
||||
val text = if (existing.containsMatchIn(source)) source.replace(existing, "\n$line")
|
||||
else source.replaceFirst(Regex("""("controller_type"\s+"controller_mobile_touch"[^\n]*)"""), "$1\n$line")
|
||||
if (!text.contains("touch_layout")) return null
|
||||
target.parentFile?.mkdirs()
|
||||
val tmp = File(target.path + ".tmp")
|
||||
tmp.writeText(text)
|
||||
if (!tmp.renameTo(target)) return null
|
||||
selectAutosave(dir, appId)
|
||||
Log.i(TAG, "steam touch: saved layouts ${sets.keys} for app $appId to ${target.path}")
|
||||
return target
|
||||
}
|
||||
|
||||
/** Points the app's configset entry at its autosave config. */
|
||||
private fun selectAutosave(dir: File, appId: Int) {
|
||||
val file = File(dir, CONFIGSET)
|
||||
val text = if (file.isFile) file.readText() else "\"controller_config\"\n{\n}\n"
|
||||
val entry = "\t\"$appId\"\n\t{\n\t\t\"autosave\"\t\t\"1\"\n\t}"
|
||||
val block = Regex("""\n\s*"$appId"\s*\{[^}]*\}""")
|
||||
val next = if (block.containsMatchIn(text)) text.replace(block, "\n$entry")
|
||||
else text.substring(0, text.lastIndexOf('}')).trimEnd() + "\n$entry\n}\n"
|
||||
File(file.path + ".tmp").apply { writeText(next) }.renameTo(file)
|
||||
}
|
||||
|
||||
// ---- touch_layout: CVirtualControllerLayouts ----
|
||||
|
||||
fun decodeLayouts(bytes: ByteArray): Layouts {
|
||||
val layouts = mutableListOf<Layout>()
|
||||
val rest = ByteArrayOutputStream()
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val start = p.pos
|
||||
val (field, wire) = p.key()
|
||||
if (field == 1 && wire == 2) layouts += decodeLayout(p.bytes())
|
||||
else { p.skip(wire); rest.write(bytes, start, p.pos - start) }
|
||||
}
|
||||
return Layouts(layouts, rest.toByteArray())
|
||||
}
|
||||
|
||||
private fun decodeLayout(bytes: ByteArray): Layout {
|
||||
var actionSet = 0
|
||||
var version: Int? = null
|
||||
var color: FloatArray? = null
|
||||
val elements = mutableListOf<Element>()
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
when {
|
||||
field == 1 && wire == 0 -> version = p.varint().toInt()
|
||||
field == 2 && wire == 0 -> actionSet = p.varint().toInt()
|
||||
field == 4 && wire == 2 -> elements += decodeElement(p.bytes())
|
||||
field == 5 && wire == 2 -> color = decodeColor(p.bytes())
|
||||
else -> p.skip(wire)
|
||||
}
|
||||
}
|
||||
return Layout(actionSet, elements, color, version)
|
||||
}
|
||||
|
||||
private fun decodeElement(bytes: ByteArray): Element {
|
||||
var type = -1
|
||||
var visible = false
|
||||
var x = 0f
|
||||
var y = 0f
|
||||
var sx = 1f
|
||||
var sy = 1f
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
when {
|
||||
field == 1 && wire == 0 -> type = p.varint().toInt()
|
||||
field == 2 && wire == 0 -> visible = p.varint() != 0L
|
||||
field == 3 && wire == 5 -> x = p.float()
|
||||
field == 4 && wire == 5 -> y = p.float()
|
||||
field == 5 && wire == 5 -> sx = p.float()
|
||||
field == 6 && wire == 5 -> sy = p.float()
|
||||
else -> p.skip(wire)
|
||||
}
|
||||
}
|
||||
return Element(type, visible, x, y, sx, sy)
|
||||
}
|
||||
|
||||
private fun decodeColor(bytes: ByteArray): FloatArray {
|
||||
val c = floatArrayOf(1f, 1f, 1f, 1f)
|
||||
val p = Proto(bytes)
|
||||
while (p.more()) {
|
||||
val (field, wire) = p.key()
|
||||
if (field in 1..4 && wire == 5) c[field - 1] = p.float() else p.skip(wire)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
fun encodeLayouts(layouts: Layouts): ByteArray {
|
||||
val out = ByteArrayOutputStream()
|
||||
layouts.layouts.forEach { layout ->
|
||||
val l = ByteArrayOutputStream()
|
||||
layout.version?.let { tag(l, 1, 0); varint(l, it.toLong()) }
|
||||
tag(l, 2, 0); varint(l, layout.actionSet.toLong())
|
||||
layout.elements.forEach { e ->
|
||||
val m = ByteArrayOutputStream()
|
||||
tag(m, 1, 0); varint(m, e.type.toLong())
|
||||
tag(m, 2, 0); varint(m, if (e.visible) 1 else 0)
|
||||
tag(m, 3, 5); fixed(m, e.x)
|
||||
tag(m, 4, 5); fixed(m, e.y)
|
||||
tag(m, 5, 5); fixed(m, e.xScale)
|
||||
tag(m, 6, 5); fixed(m, e.yScale)
|
||||
tag(l, 4, 2); bytes(l, m.toByteArray())
|
||||
}
|
||||
layout.color?.let { c ->
|
||||
val m = ByteArrayOutputStream()
|
||||
for (i in 0 until 4) { tag(m, i + 1, 5); fixed(m, c[i]) }
|
||||
tag(l, 5, 2); bytes(l, m.toByteArray())
|
||||
}
|
||||
tag(out, 1, 2); bytes(out, l.toByteArray())
|
||||
}
|
||||
out.write(layouts.rest)
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
private fun tag(out: ByteArrayOutputStream, field: Int, wire: Int) = varint(out, (field shl 3 or wire).toLong())
|
||||
private fun varint(out: ByteArrayOutputStream, value: Long) {
|
||||
var v = value
|
||||
while (true) {
|
||||
if (v and 0x7fL.inv() == 0L) { out.write(v.toInt()); return }
|
||||
out.write((v and 0x7f or 0x80).toInt())
|
||||
v = v ushr 7
|
||||
}
|
||||
}
|
||||
private fun fixed(out: ByteArrayOutputStream, f: Float) {
|
||||
val bits = java.lang.Float.floatToIntBits(f)
|
||||
for (i in 0 until 4) out.write(bits ushr (8 * i) and 0xff)
|
||||
}
|
||||
private fun bytes(out: ByteArrayOutputStream, b: ByteArray) { varint(out, b.size.toLong()); out.write(b) }
|
||||
|
||||
private class Proto(val data: ByteArray) {
|
||||
var pos = 0
|
||||
fun varint(): Long {
|
||||
var shift = 0
|
||||
var result = 0L
|
||||
while (pos < data.size) {
|
||||
val b = data[pos++].toInt() and 0xff
|
||||
result = result or ((b and 0x7f).toLong() shl shift)
|
||||
if (b and 0x80 == 0) break
|
||||
shift += 7
|
||||
}
|
||||
return result
|
||||
}
|
||||
fun float(): Float {
|
||||
var bits = 0
|
||||
for (i in 0 until 4) bits = bits or ((data[pos + i].toInt() and 0xff) shl (8 * i))
|
||||
pos += 4
|
||||
return java.lang.Float.intBitsToFloat(bits)
|
||||
}
|
||||
fun bytes(): ByteArray {
|
||||
val n = varint().toInt()
|
||||
return data.copyOfRange(pos, pos + n).also { pos += n }
|
||||
}
|
||||
fun skip(wire: Int) {
|
||||
when (wire) {
|
||||
0 -> varint()
|
||||
1 -> pos += 8
|
||||
2 -> pos += varint().toInt()
|
||||
5 -> pos += 4
|
||||
else -> pos = data.size
|
||||
}
|
||||
}
|
||||
fun more() = pos < data.size
|
||||
fun key(): Pair<Int, Int> = varint().toInt().let { (it ushr 3) to (it and 7) }
|
||||
}
|
||||
|
||||
fun hexToBytes(hex: String) = ByteArray(hex.length / 2) { hex.substring(2 * it, 2 * it + 2).toInt(16).toByte() }
|
||||
fun bytesToHex(b: ByteArray) = b.joinToString("") { "%02x".format(it.toInt() and 0xff) }
|
||||
}
|
||||
|
||||
/** Valve's KeyValues text format, as far as controller configs use it. */
|
||||
class KeyValues private constructor() {
|
||||
class Node(val entries: MutableList<Pair<String, Any>> = mutableListOf()) {
|
||||
fun child(key: String): Node? = entries.firstOrNull { it.first.equals(key, true) && it.second is Node }?.second as Node?
|
||||
fun children(key: String): List<Node> = entries.filter { it.first.equals(key, true) && it.second is Node }.map { it.second as Node }
|
||||
fun string(key: String): String? = entries.firstOrNull { it.first.equals(key, true) && it.second is String }?.second as String?
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun parse(text: String): Node {
|
||||
val tokens = tokenize(text)
|
||||
var i = 0
|
||||
fun node(): Node {
|
||||
val n = Node()
|
||||
while (i < tokens.size) {
|
||||
val t = tokens[i++]
|
||||
if (t == "}" ) return n
|
||||
if (t == "{") continue
|
||||
if (i >= tokens.size) break
|
||||
val next = tokens[i]
|
||||
if (next == "{") { i++; n.entries += t to node() } else { i++; n.entries += t to next }
|
||||
}
|
||||
return n
|
||||
}
|
||||
return node()
|
||||
}
|
||||
|
||||
private fun tokenize(text: String): List<String> {
|
||||
val out = mutableListOf<String>()
|
||||
var i = 0
|
||||
while (i < text.length) {
|
||||
val c = text[i]
|
||||
when {
|
||||
c.isWhitespace() -> i++
|
||||
c == '/' && i + 1 < text.length && text[i + 1] == '/' -> while (i < text.length && text[i] != '\n') i++
|
||||
c == '{' || c == '}' -> { out += c.toString(); i++ }
|
||||
c == '"' -> {
|
||||
val sb = StringBuilder()
|
||||
i++
|
||||
while (i < text.length && text[i] != '"') {
|
||||
if (text[i] == '\\' && i + 1 < text.length) {
|
||||
i++
|
||||
sb.append(when (text[i]) { 'n' -> '\n'; 't' -> '\t'; else -> text[i] })
|
||||
} else sb.append(text[i])
|
||||
i++
|
||||
}
|
||||
i++
|
||||
out += sb.toString()
|
||||
}
|
||||
c == '[' -> { while (i < text.length && text[i] != ']') i++; i++ }
|
||||
else -> {
|
||||
val start = i
|
||||
while (i < text.length && !text[i].isWhitespace() && text[i] != '{' && text[i] != '}' && text[i] != '"') i++
|
||||
out += text.substring(start, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,171 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.util.Log
|
||||
import java.io.File
|
||||
import java.io.RandomAccessFile
|
||||
import java.nio.ByteOrder
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.FileChannel
|
||||
|
||||
/**
|
||||
* Steam's touch controller, as the app's side of it.
|
||||
*
|
||||
* libfakeinput offers the Steam client a HID device with the ids of Steam Link's touch controls
|
||||
* (0000:11fb), and the client builds its own touch controller for it: Steam Input's Mobile Touch
|
||||
* type, with the game's touch config, action sets, community configs and cloud sync all Steam's
|
||||
* (docs/development/steam-touch-controller.md). This class is the file the two share
|
||||
* (fakeinput_steam.cpp: TouchRingFile): the app writes the device's 40-byte input report and
|
||||
* whether it is plugged in; libfakeinput writes back what the client told the device - the app and
|
||||
* action set it is on, its layers, rumble.
|
||||
*/
|
||||
class SteamTouchDevice private constructor(private val buffer: ByteBuffer) {
|
||||
|
||||
/** What the client last told the device. */
|
||||
data class State(
|
||||
val opened: Int,
|
||||
val appId: Int,
|
||||
val actionSet: Int,
|
||||
val layers: List<Int>,
|
||||
val actionSeq: Int,
|
||||
)
|
||||
|
||||
// The input report (Steam Link's CVirtualController, docs/development/steam-touch-controller.md):
|
||||
// bytes 0-7 buttons, 8-15 sticks, 16-27 trackpads, 28-33 accelerometer, 34-39 gyro.
|
||||
private val report = ByteArray(REPORT_BYTES)
|
||||
private var reportSeq = 0L
|
||||
|
||||
/** The controls part of the report: button bits, sticks (LX, LY, RX, RY, already in the report's
|
||||
* units) and trackpads (centre, left, right: x, y each). */
|
||||
@Synchronized
|
||||
fun setControls(buttons: Long, sticks: ShortArray, pads: ShortArray) {
|
||||
val next = report.copyOf()
|
||||
for (i in 0 until 8) next[i] = (buttons ushr (8 * i)).toByte()
|
||||
for (i in 0 until 4) putShort(next, 8 + 2 * i, sticks[i])
|
||||
for (i in 0 until 6) putShort(next, 16 + 2 * i, pads[i])
|
||||
publish(next)
|
||||
}
|
||||
|
||||
/** Motion in the Deck's axes and units (PadMotion), which the touch report shares. */
|
||||
@Synchronized
|
||||
fun setMotion(accel: ShortArray, gyro: ShortArray) {
|
||||
// Sent only while the client asks for it (setting report 0x30, which Steam Link reads as
|
||||
// "gyro on") and the player has not turned motion off - as Steam Link does.
|
||||
if (!plugged || !gyroRequested || !motionEnabled) return
|
||||
val next = report.copyOf()
|
||||
for (i in 0 until 3) {
|
||||
// Accelerometer: the Deck's 1 g = 16384 is the touch report's ±2 g = ±32767 already.
|
||||
putShort(next, 28 + 2 * i, accel[i])
|
||||
// Gyro: the Deck's 2000 °/s = 32768; the touch report's full scale is 1000 °/s
|
||||
// (Steam Link: rad/s × 180/π / 1000 × 32767).
|
||||
putShort(next, 34 + 2 * i, (gyro[i] * 2).coerceIn(-32767, 32767).toShort())
|
||||
}
|
||||
publish(next)
|
||||
}
|
||||
|
||||
/** Whether the client has asked the device for motion (setting report 0x30 non-zero). */
|
||||
val gyroRequested get() = buffer.getInt(OFF_SETTING30) != 0
|
||||
|
||||
/** The player's motion switch (Steam Link's gyroscope option). */
|
||||
@Volatile var motionEnabled = true
|
||||
set(value) {
|
||||
field = value
|
||||
if (!value) synchronized(this) {
|
||||
val next = report.copyOf()
|
||||
for (i in 28 until 40) next[i] = 0
|
||||
publish(next)
|
||||
}
|
||||
}
|
||||
|
||||
private fun putShort(into: ByteArray, at: Int, value: Short) {
|
||||
into[at] = value.toInt().toByte()
|
||||
into[at + 1] = (value.toInt() shr 8).toByte()
|
||||
}
|
||||
|
||||
/** Writes the report under the seqlock libfakeinput reads it with. */
|
||||
private fun publish(next: ByteArray) {
|
||||
if (next.contentEquals(report)) return
|
||||
next.copyInto(report)
|
||||
buffer.putLong(OFF_REPORT_SEQ, ++reportSeq)
|
||||
for (i in 0 until REPORT_BYTES) buffer.put(OFF_REPORT + i, report[i])
|
||||
buffer.putLong(OFF_REPORT_SEQ, ++reportSeq)
|
||||
}
|
||||
|
||||
var plugged: Boolean
|
||||
get() = buffer.getInt(OFF_PLUGGED) != 0
|
||||
set(value) {
|
||||
if (value == plugged) return
|
||||
if (!value) synchronized(this) { publish(ByteArray(REPORT_BYTES)) }
|
||||
buffer.putInt(OFF_PLUGGED, if (value) 1 else 0)
|
||||
Log.i(TAG, "steam touch: " + if (value) "plugged in" else "unplugged")
|
||||
}
|
||||
|
||||
fun setBattery(percent: Int) = buffer.putInt(OFF_BATTERY, percent.coerceIn(0, 100))
|
||||
|
||||
fun state(): State {
|
||||
repeat(8) {
|
||||
val seq = buffer.getLong(OFF_STATE_SEQ)
|
||||
if (seq and 1L != 0L) return@repeat
|
||||
val count = buffer.getInt(OFF_LAYER_COUNT).coerceIn(0, 8)
|
||||
val read = State(
|
||||
opened = buffer.getInt(OFF_OPENED),
|
||||
appId = buffer.getInt(OFF_APPID),
|
||||
actionSet = buffer.getInt(OFF_ACTION_SET),
|
||||
layers = (0 until count).map { buffer.getInt(OFF_LAYERS + 4 * it) },
|
||||
actionSeq = buffer.getInt(OFF_ACTION_SEQ),
|
||||
)
|
||||
if (seq == buffer.getLong(OFF_STATE_SEQ)) return read
|
||||
}
|
||||
return State(0, 0, 0, emptyList(), -1)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SteamTouch"
|
||||
const val REPORT_BYTES = 40
|
||||
private const val SIZE = 256
|
||||
private const val MAGIC = 0x31484354 // TCH1
|
||||
private const val OFF_REPORT_SEQ = 8
|
||||
private const val OFF_REPORT = 16
|
||||
private const val OFF_PLUGGED = 56
|
||||
private const val OFF_BATTERY = 60
|
||||
private const val OFF_STATE_SEQ = 64
|
||||
private const val OFF_OPENED = 72
|
||||
private const val OFF_APPID = 76
|
||||
private const val OFF_ACTION_SET = 80
|
||||
private const val OFF_LAYER_COUNT = 84
|
||||
private const val OFF_LAYERS = 88
|
||||
private const val OFF_SETTING30 = 132
|
||||
private const val OFF_ACTION_SEQ = 136
|
||||
|
||||
@Volatile
|
||||
var current: SteamTouchDevice? = null
|
||||
private set
|
||||
|
||||
/** The shared file in the session's pad directory, made fresh each session. */
|
||||
fun ringFile(fakeInputDir: File) = File(fakeInputDir, "touchctl")
|
||||
|
||||
/** Creates the file for a new session (unplugged) and maps it. */
|
||||
fun prepare(fakeInputDir: File): File? = try {
|
||||
val file = ringFile(fakeInputDir)
|
||||
file.delete()
|
||||
RandomAccessFile(file, "rw").use { raf ->
|
||||
raf.setLength(SIZE.toLong())
|
||||
val map = raf.channel.map(FileChannel.MapMode.READ_WRITE, 0, SIZE.toLong()).order(ByteOrder.LITTLE_ENDIAN)
|
||||
map.putInt(4, 1)
|
||||
map.putInt(0, MAGIC)
|
||||
current = SteamTouchDevice(map)
|
||||
}
|
||||
file.setReadable(true, false)
|
||||
file.setWritable(true, false)
|
||||
file
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "steam touch: no shared file: $e")
|
||||
current = null
|
||||
null
|
||||
}
|
||||
|
||||
fun release() {
|
||||
current?.plugged = false
|
||||
current = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.PorterDuff
|
||||
import android.graphics.PorterDuffColorFilter
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.text.InputType
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.BaseAdapter
|
||||
import android.widget.EditText
|
||||
import android.widget.GridView
|
||||
import android.widget.HorizontalScrollView
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* Picks a touch control's icon, label and colours the way Steam's configurator does: an icon from
|
||||
* the game's own `TouchMenuIcons` and Steam's binding icon library, a foreground and a background
|
||||
* from Steam's palette. [onPicked] gets the binding with the new fields (the action untouched).
|
||||
*/
|
||||
class SteamTouchIconPicker(
|
||||
private val context: Context,
|
||||
private val host: ViewGroup,
|
||||
private val appId: Int,
|
||||
private val title: String,
|
||||
private val current: SteamTouchBindings.Binding,
|
||||
private val onPicked: (SteamTouchBindings.Binding) -> Unit,
|
||||
) {
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
private val loader = Executors.newFixedThreadPool(2)
|
||||
private var icon = current.icon
|
||||
private var foreground = current.foreground.ifEmpty { SteamTouchBindings.DEFAULT_FOREGROUND }
|
||||
private var background = current.background.ifEmpty { SteamTouchBindings.DEFAULT_BACKGROUND }
|
||||
private val density = context.resources.displayMetrics.density
|
||||
|
||||
private fun dp(v: Int) = (v * density).toInt()
|
||||
|
||||
fun show() {
|
||||
// Side by side, for a landscape screen: what is picked on the left, the icons on the right.
|
||||
val root = LinearLayout(context).apply { orientation = LinearLayout.HORIZONTAL; setPadding(dp(16), dp(8), dp(16), 0) }
|
||||
val left = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL }
|
||||
val label = EditText(context).apply {
|
||||
hint = "Label (optional)"
|
||||
setText(current.label)
|
||||
inputType = InputType.TYPE_CLASS_TEXT
|
||||
setSingleLine()
|
||||
}
|
||||
left.addView(label)
|
||||
val preview = ImageView(context)
|
||||
val names = SteamTouchBindings.iconNames(context, appId)
|
||||
val grid = GridView(context).apply {
|
||||
numColumns = GridView.AUTO_FIT
|
||||
columnWidth = dp(56)
|
||||
stretchMode = GridView.STRETCH_COLUMN_WIDTH
|
||||
verticalSpacing = dp(6)
|
||||
horizontalSpacing = dp(6)
|
||||
}
|
||||
val adapter = IconAdapter(names)
|
||||
grid.adapter = adapter
|
||||
grid.setOnItemClickListener { _, _, position, _ ->
|
||||
icon = names[position]
|
||||
adapter.notifyDataSetChanged()
|
||||
updatePreview(preview)
|
||||
}
|
||||
left.addView(LinearLayout(context).apply {
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
setPadding(0, dp(8), 0, 0)
|
||||
addView(preview, LinearLayout.LayoutParams(dp(56), dp(56)))
|
||||
addView(TextView(context).apply {
|
||||
text = if (names.isEmpty()) "Steam's icons were not found" else "${names.size} icons from Steam"
|
||||
setPadding(dp(12), 0, 0, 0)
|
||||
})
|
||||
})
|
||||
left.addView(paletteRow("Icon colour", { foreground }) { foreground = it; adapter.notifyDataSetChanged(); updatePreview(preview) })
|
||||
left.addView(paletteRow("Button colour", { background }) { background = it; adapter.notifyDataSetChanged(); updatePreview(preview) })
|
||||
root.addView(left, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f))
|
||||
root.addView(grid, LinearLayout.LayoutParams(0, dp(250), 1.2f).apply { marginStart = dp(16) })
|
||||
updatePreview(preview)
|
||||
showPanel(host, title, root, listOf(
|
||||
"No icon" to {
|
||||
onPicked(current.copy(label = label.text.toString().replace(",", " ").trim(), icon = "", foreground = "", background = ""))
|
||||
},
|
||||
"Cancel" to {},
|
||||
"Use" to {
|
||||
onPicked(current.copy(label = label.text.toString().replace(",", " ").trim(), icon = icon,
|
||||
foreground = if (icon.isEmpty()) "" else foreground, background = if (icon.isEmpty()) "" else background))
|
||||
},
|
||||
)) { loader.shutdown() }
|
||||
}
|
||||
|
||||
private fun paletteRow(name: String, value: () -> String, onPick: (String) -> Unit): View {
|
||||
val row = LinearLayout(context).apply { orientation = LinearLayout.HORIZONTAL; gravity = Gravity.CENTER_VERTICAL }
|
||||
val swatches = mutableListOf<View>()
|
||||
fun refresh() = swatches.forEachIndexed { i, v ->
|
||||
(v.background as GradientDrawable).setStroke(dp(if (SteamTouchBindings.PALETTE[i].equals(value(), true)) 3 else 1),
|
||||
if (SteamTouchBindings.PALETTE[i].equals(value(), true)) Color.rgb(102, 192, 244) else Color.GRAY)
|
||||
}
|
||||
SteamTouchBindings.PALETTE.forEach { hex ->
|
||||
val swatch = View(context).apply {
|
||||
background = GradientDrawable().apply { shape = GradientDrawable.OVAL; setColor(Color.parseColor(hex)) }
|
||||
setOnClickListener { onPick(hex); refresh() }
|
||||
}
|
||||
swatches += swatch
|
||||
row.addView(swatch, LinearLayout.LayoutParams(dp(30), dp(30)).apply { marginEnd = dp(6) })
|
||||
}
|
||||
refresh()
|
||||
return LinearLayout(context).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
setPadding(0, dp(8), 0, 0)
|
||||
addView(TextView(context).apply { text = name })
|
||||
addView(HorizontalScrollView(context).apply { addView(row) })
|
||||
}
|
||||
}
|
||||
|
||||
private fun updatePreview(view: ImageView) {
|
||||
if (icon.isEmpty()) { view.setImageDrawable(null); view.background = null; return }
|
||||
loader.execute {
|
||||
val bitmap = SteamTouchBindings.icon(context, appId, icon, 128)?.let { render(it, dp(56)) }
|
||||
main.post { view.setImageBitmap(bitmap) }
|
||||
}
|
||||
}
|
||||
|
||||
/** The icon as Steam draws it: on a disc of the background colour, filled with the foreground
|
||||
* through its own alpha. */
|
||||
private fun render(icon: Bitmap, size: Int): Bitmap {
|
||||
val out = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
|
||||
val canvas = Canvas(out)
|
||||
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
|
||||
paint.color = Color.parseColor(background)
|
||||
canvas.drawCircle(size / 2f, size / 2f, size / 2f, paint)
|
||||
drawSteamIcon(canvas, icon, android.graphics.RectF(size * 0.18f, size * 0.18f, size * 0.82f, size * 0.82f), Color.parseColor(foreground), paint)
|
||||
return out
|
||||
}
|
||||
|
||||
private inner class IconAdapter(val names: List<String>) : BaseAdapter() {
|
||||
private val loading = HashSet<String>()
|
||||
private var refreshQueued = false
|
||||
|
||||
/** Loads an icon in the background; the grid redraws from the cache once some have come in. */
|
||||
private fun load(name: String) {
|
||||
if (!loading.add(name)) return
|
||||
loader.execute {
|
||||
SteamTouchBindings.icon(context, appId, name, 96)
|
||||
main.post {
|
||||
loading.remove(name)
|
||||
if (refreshQueued) return@post
|
||||
refreshQueued = true
|
||||
main.postDelayed({ refreshQueued = false; notifyDataSetChanged() }, 120)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getCount() = names.size
|
||||
override fun getItem(position: Int) = names[position]
|
||||
override fun getItemId(position: Int) = position.toLong()
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
|
||||
val view = (convertView as? ImageView) ?: ImageView(context).apply {
|
||||
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(56))
|
||||
setPadding(dp(8), dp(8), dp(8), dp(8))
|
||||
}
|
||||
val name = names[position]
|
||||
view.tag = name
|
||||
view.background = GradientDrawable().apply {
|
||||
cornerRadius = dp(8).toFloat()
|
||||
setColor(Color.parseColor(background))
|
||||
if (name == icon) setStroke(dp(3), Color.rgb(102, 192, 244))
|
||||
}
|
||||
view.colorFilter = null
|
||||
val cached = SteamTouchBindings.cachedIcon(appId, name, 96)
|
||||
if (cached != null) {
|
||||
val t = tinted(name, cached)
|
||||
if ((view.drawable as? android.graphics.drawable.BitmapDrawable)?.bitmap !== t) view.setImageBitmap(t)
|
||||
} else {
|
||||
view.setImageDrawable(null)
|
||||
load(name)
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// One thumbnail per icon and colour, reused when the grid rebinds.
|
||||
private val thumbs = android.util.LruCache<String, Bitmap>(64)
|
||||
|
||||
private fun tinted(name: String, icon: Bitmap): Bitmap {
|
||||
val key = "$name|$foreground"
|
||||
thumbs.get(key)?.let { return it }
|
||||
val out = Bitmap.createBitmap(icon.width, icon.height, Bitmap.Config.ARGB_8888)
|
||||
drawSteamIcon(Canvas(out), icon, android.graphics.RectF(0f, 0f, icon.width.toFloat(), icon.height.toFloat()),
|
||||
Color.parseColor(foreground), Paint(Paint.FILTER_BITMAP_FLAG))
|
||||
thumbs.put(key, out)
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* A panel over the session, in the session's own window: a dialog's window of its own left
|
||||
* new bitmaps and layers undrawn in the session window afterwards on the Thor.
|
||||
*/
|
||||
fun showPanel(host: ViewGroup, title: String, content: View, buttons: List<Pair<String, () -> Unit>>, onClose: () -> Unit = {}): () -> Unit {
|
||||
val context = host.context
|
||||
val density = context.resources.displayMetrics.density
|
||||
fun dp(v: Int) = (v * density).toInt()
|
||||
val scrim = android.widget.FrameLayout(context).apply {
|
||||
setBackgroundColor(Color.argb(140, 0, 0, 0))
|
||||
isClickable = true
|
||||
isFocusable = true
|
||||
}
|
||||
// DroidDeck's panel: dark surface, rounded, a hairline border.
|
||||
val card = LinearLayout(context).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
background = GradientDrawable().apply {
|
||||
cornerRadius = dp(18).toFloat(); setColor(Color.rgb(18, 20, 23)); setStroke(dp(1), Color.rgb(38, 42, 49))
|
||||
}
|
||||
setPadding(dp(12), dp(14), dp(12), dp(10))
|
||||
isClickable = true
|
||||
}
|
||||
card.addView(TextView(context).apply {
|
||||
text = title
|
||||
textSize = 15f
|
||||
setTypeface(android.graphics.Typeface.DEFAULT_BOLD)
|
||||
gravity = Gravity.CENTER
|
||||
setTextColor(Color.WHITE)
|
||||
setPadding(dp(16), 0, dp(16), dp(4))
|
||||
})
|
||||
card.addView(content)
|
||||
val row = LinearLayout(context).apply { gravity = Gravity.CENTER; setPadding(dp(8), dp(10), dp(8), 0) }
|
||||
fun close() {
|
||||
(scrim.parent as? ViewGroup)?.removeView(scrim)
|
||||
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as android.view.inputmethod.InputMethodManager
|
||||
imm.hideSoftInputFromWindow(host.windowToken, 0)
|
||||
onClose()
|
||||
}
|
||||
buttons.forEachIndexed { i, (name, action) ->
|
||||
row.addView(TextView(context).apply {
|
||||
text = name
|
||||
gravity = Gravity.CENTER
|
||||
textSize = 14f
|
||||
setTextColor(Color.WHITE)
|
||||
val primary = i == buttons.size - 1
|
||||
setTextColor(if (primary) Color.rgb(3, 17, 31) else Color.rgb(242, 244, 247))
|
||||
background = GradientDrawable().apply {
|
||||
cornerRadius = dp(12).toFloat()
|
||||
if (primary) setColor(Color.rgb(26, 159, 255)) else { setColor(Color.rgb(26, 29, 34)); setStroke(dp(1), Color.rgb(52, 58, 67)) }
|
||||
}
|
||||
setPadding(dp(20), dp(10), dp(20), dp(10))
|
||||
setOnClickListener { close(); action() }
|
||||
}, LinearLayout.LayoutParams(0, dp(44), 1f).apply { marginStart = dp(6); marginEnd = dp(6) })
|
||||
}
|
||||
card.addView(row)
|
||||
val width = minOf(dp(640), (context.resources.displayMetrics.widthPixels * 0.9f).toInt())
|
||||
scrim.addView(card, android.widget.FrameLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER))
|
||||
scrim.setOnClickListener { close() }
|
||||
host.addView(scrim, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||
return { if (scrim.parent != null) close() }
|
||||
}
|
||||
|
||||
/** Steam's binding icon look (steamui: the icon multiplied with its foreground colour,
|
||||
* background-blend-mode: multiply, keeping the icon's own alpha). */
|
||||
fun drawSteamIcon(canvas: Canvas, icon: Bitmap, into: android.graphics.RectF, foreground: Int, paint: Paint) {
|
||||
val tint = Paint(paint).apply { colorFilter = PorterDuffColorFilter(foreground, PorterDuff.Mode.MULTIPLY) }
|
||||
canvas.drawBitmap(icon, null, into, tint)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -227,7 +227,8 @@ public final class LinuxRuntimeInstaller {
|
||||
// Its quarantined home is not user data to carry into an update.
|
||||
File pendingRemoval = removalDirectory(LinuxRuntime.rootDir(context));
|
||||
if (pendingRemoval.exists() && listener != null) listener.onProgress(context.getString(R.string.rtinst_removing_leftovers), -1);
|
||||
RuntimeFileTree.delete(pendingRemoval, null);
|
||||
if (!discard(pendingRemoval, null)) throw new IOException("Cannot clear the previous runtime: " + pendingRemoval);
|
||||
emptyTrash(LinuxRuntime.rootDir(context).getParentFile());
|
||||
String downloading = context.getString(R.string.rtinst_downloading);
|
||||
if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0);
|
||||
// Downloader reports a 0..1 fraction, or -1 while the total size is unknown.
|
||||
@@ -255,8 +256,7 @@ public final class LinuxRuntimeInstaller {
|
||||
File staging = new File(root.getParentFile(), LinuxRuntime.DIR + ".new");
|
||||
File old = new File(root.getParentFile(), LinuxRuntime.DIR + ".old");
|
||||
recoverInterruptedSwap(root, staging, old);
|
||||
RuntimeFileTree.delete(staging, null);
|
||||
if (!staging.mkdirs()) return false;
|
||||
if (!discard(staging, null) || !staging.mkdirs()) return false;
|
||||
if (listener != null) listener.onProgress(context.getString(R.string.rtinst_extracting), -1);
|
||||
if (!extract(context, archive, staging, listener)) {
|
||||
RuntimeFileTree.delete(staging, null);
|
||||
@@ -264,7 +264,10 @@ public final class LinuxRuntimeInstaller {
|
||||
}
|
||||
FileUtils.writeString(new File(staging, VERSION_FILE), release.version);
|
||||
|
||||
RuntimeFileTree.delete(old, null);
|
||||
if (!discard(old, null)) {
|
||||
discard(staging, null);
|
||||
return false;
|
||||
}
|
||||
if (root.isDirectory() && !root.renameTo(old)) {
|
||||
RuntimeFileTree.delete(staging, null);
|
||||
return false;
|
||||
@@ -292,7 +295,7 @@ public final class LinuxRuntimeInstaller {
|
||||
if (old.isDirectory()) old.renameTo(root);
|
||||
return false;
|
||||
}
|
||||
RuntimeFileTree.delete(old, null);
|
||||
discard(old, null);
|
||||
return LinuxRuntime.isInstalled(context);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "install", e);
|
||||
@@ -315,6 +318,35 @@ public final class LinuxRuntimeInstaller {
|
||||
}
|
||||
}
|
||||
|
||||
/** Where entries the app cannot unlink are set aside, out of the way of the runtime's names. */
|
||||
static final String TRASH = ".runtime-trash";
|
||||
|
||||
/**
|
||||
* Gets {@code tree} out from under its name. What can be deleted is; if something inside
|
||||
* cannot be (a file made by root or {@code su} carries another SELinux category, and the app
|
||||
* may not unlink it), the remainder is moved aside into {@link #TRASH} instead of blocking
|
||||
* every later install and removal on it. False only when even that move is refused.
|
||||
*/
|
||||
static boolean discard(File tree, java.util.function.LongConsumer progress) {
|
||||
if (RuntimeFileTree.deleteWhatCan(tree, progress)) return true;
|
||||
File trash = new File(tree.getParentFile(), TRASH);
|
||||
trash.mkdirs();
|
||||
File aside = new File(trash, tree.getName() + "-" + System.currentTimeMillis());
|
||||
if (tree.renameTo(aside)) {
|
||||
Log.w(TAG, "could not delete all of " + tree + "; set the rest aside in " + aside);
|
||||
return true;
|
||||
}
|
||||
Log.w(TAG, "could not delete or set aside " + tree);
|
||||
return !tree.exists();
|
||||
}
|
||||
|
||||
/** Another try at what {@link #discard} set aside. Whatever still cannot go stays there. */
|
||||
static void emptyTrash(File parent) {
|
||||
File trash = new File(parent, TRASH);
|
||||
if (trash.exists() && !RuntimeFileTree.deleteWhatCan(trash, null))
|
||||
Log.i(TAG, "some runtime leftovers still cannot be deleted: " + trash);
|
||||
}
|
||||
|
||||
private static boolean isEmptyDir(File dir) {
|
||||
String[] names = dir.list();
|
||||
return dir.isDirectory() && names != null && names.length == 0;
|
||||
@@ -369,15 +401,21 @@ public final class LinuxRuntimeInstaller {
|
||||
// Invalidate the installation before traversing it. A process killed halfway
|
||||
// through leaves a named removal to resume, never a launchable partial runtime.
|
||||
if (root.exists()) {
|
||||
RuntimeFileTree.delete(pending, progress);
|
||||
if (!root.renameTo(pending)) throw new IOException("Cannot prepare the runtime for removal");
|
||||
if (!discard(pending, progress) || !root.renameTo(pending))
|
||||
throw new IOException("Cannot prepare the runtime for removal");
|
||||
} else if (!pending.isDirectory() && !pending.mkdirs()) {
|
||||
throw new IOException("Cannot prepare the runtime for removal");
|
||||
}
|
||||
// Keep the quarantine until all leftovers are gone, so a failure stays retryable.
|
||||
RuntimeFileTree.delete(new File(root.getParentFile(), root.getName() + ".new"), progress);
|
||||
RuntimeFileTree.delete(new File(root.getParentFile(), root.getName() + ".old"), progress);
|
||||
RuntimeFileTree.delete(pending, progress);
|
||||
// An entry the app may not delete is set aside rather than failing the removal:
|
||||
// a quarantine that can never clear would block reinstalling for good.
|
||||
for (File leftover : new File[]{
|
||||
new File(root.getParentFile(), root.getName() + ".new"),
|
||||
new File(root.getParentFile(), root.getName() + ".old"),
|
||||
pending}) {
|
||||
if (!discard(leftover, progress)) throw new IOException("Cannot remove " + leftover);
|
||||
}
|
||||
emptyTrash(root.getParentFile());
|
||||
job.ok = true;
|
||||
report(text(app, R.string.rtinst_removed), 100);
|
||||
return true;
|
||||
|
||||
@@ -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>): String? {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.runtime;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.AccessDeniedException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
@@ -9,11 +10,15 @@ import java.nio.file.NoSuchFileException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.function.LongConsumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Checked removal of an app-owned runtime tree, including read-only directories. */
|
||||
final class RuntimeFileTree {
|
||||
private RuntimeFileTree() {}
|
||||
|
||||
/** Unit tests stand in for an entry the app cannot unlink (a file another SELinux user made). */
|
||||
static volatile Predicate<Path> refuseForTest;
|
||||
|
||||
static void delete(File root, LongConsumer progress) throws IOException {
|
||||
delete(root.toPath(), progress, new long[]{0});
|
||||
}
|
||||
@@ -30,10 +35,48 @@ final class RuntimeFileTree {
|
||||
for (Path child : children) delete(child, progress, removed);
|
||||
}
|
||||
}
|
||||
Files.delete(path);
|
||||
unlink(path);
|
||||
if (progress != null) progress.accept(++removed[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes what it can of {@code root} and carries on past entries it cannot, rather than
|
||||
* stopping at the first. Returns true when nothing is left.
|
||||
*/
|
||||
static boolean deleteWhatCan(File root, LongConsumer progress) {
|
||||
return deleteWhatCan(root.toPath(), progress, new long[]{0});
|
||||
}
|
||||
|
||||
private static boolean deleteWhatCan(Path path, LongConsumer progress, long[] removed) {
|
||||
BasicFileAttributes attrs;
|
||||
try { attrs = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); }
|
||||
catch (NoSuchFileException e) { return true; }
|
||||
catch (IOException e) { return false; }
|
||||
boolean emptied = true;
|
||||
if (attrs.isDirectory()) {
|
||||
try {
|
||||
makeWritable(path.toFile());
|
||||
try (DirectoryStream<Path> children = Files.newDirectoryStream(path)) {
|
||||
for (Path child : children) emptied &= deleteWhatCan(child, progress, removed);
|
||||
}
|
||||
} catch (IOException | RuntimeException e) {
|
||||
emptied = false;
|
||||
}
|
||||
}
|
||||
if (!emptied) return false;
|
||||
try { unlink(path); }
|
||||
catch (NoSuchFileException e) { return true; }
|
||||
catch (IOException e) { return false; }
|
||||
if (progress != null) progress.accept(++removed[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void unlink(Path path) throws IOException {
|
||||
Predicate<Path> refuse = refuseForTest;
|
||||
if (refuse != null && refuse.test(path)) throw new AccessDeniedException(path.toString());
|
||||
Files.delete(path);
|
||||
}
|
||||
|
||||
private static void makeWritable(File dir) throws IOException {
|
||||
if (!dir.setReadable(true, true) || !dir.setWritable(true, true) || !dir.setExecutable(true, true))
|
||||
throw new IOException("Cannot make directory removable: " + dir);
|
||||
|
||||
@@ -22,19 +22,19 @@ import kotlin.math.max
|
||||
* When a game plays a force-feedback effect on the fake pad (or Steam Input sends the Deck's
|
||||
* rumble report), the fake evdev layer inside the guest connects to the abstract socket [NAME]
|
||||
* and sends one packet: strong, weak, duration in ms and the pad slot, four little-endian 16-bit
|
||||
* values. This listens for those and plays the effect on the physical controller that last drove
|
||||
* the pad (PadBridge.activeControllerId) when Android exposes its motors - strong and weak
|
||||
* separately where it has two - and otherwise on the device's own vibrator, with the stronger
|
||||
* motor's strength: the on-screen pad, or a controller without rumble. Best effort both ways: a
|
||||
* missing listener costs the guest nothing, and a bad packet is dropped.
|
||||
* values. This listens for those and plays the effect on the physical controller playing in that
|
||||
* slot (PadBridge.controllerForSlot) when Android exposes its motors - strong and weak separately
|
||||
* where it has two - and otherwise on the device's own vibrator, with the stronger motor's
|
||||
* strength: the on-screen pad, or a controller without rumble. Best effort both ways: a missing
|
||||
* listener costs the guest nothing, and a bad packet is dropped.
|
||||
*/
|
||||
class RumbleComponent : SessionPart() {
|
||||
@Volatile private var server: LocalServerSocket? = null
|
||||
private var phone: Motors? = null
|
||||
/** Where the last effect went, so the next one (or a stop) can end it there. */
|
||||
private var playing: Motors? = null
|
||||
private var controllerId = PadBridge.NO_CONTROLLER
|
||||
private var controller: Motors? = null
|
||||
/** Per slot, where its last effect went, so the next one (or a stop) can end it there. */
|
||||
private val playing = HashMap<Int, Motors>()
|
||||
/** Each controller's motors, looked up once; null when it has none. */
|
||||
private val controllers = HashMap<Int, Motors?>()
|
||||
private var preferences: SharedPreferences? = null
|
||||
private var enabled = false
|
||||
private val preferenceListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
|
||||
@@ -46,7 +46,7 @@ class RumbleComponent : SessionPart() {
|
||||
|
||||
@Synchronized private fun refreshEnabled() {
|
||||
enabled = app()?.let { ControllerPrefs.rumbleEnabled(it) } == true
|
||||
if (!enabled) { playing?.cancel(); playing = null }
|
||||
if (!enabled) cancelAll()
|
||||
}
|
||||
|
||||
override fun start() {
|
||||
@@ -64,7 +64,7 @@ class RumbleComponent : SessionPart() {
|
||||
try {
|
||||
val bytes = ByteArray(8)
|
||||
DataInputStream(client.inputStream).readFully(bytes)
|
||||
buzz(u16(bytes, 0), u16(bytes, 2), u16(bytes, 4))
|
||||
buzz(u16(bytes, 0), u16(bytes, 2), u16(bytes, 4), u16(bytes, 6))
|
||||
} catch (e: Exception) {
|
||||
// A partial packet or a closed peer: nothing to play.
|
||||
} finally {
|
||||
@@ -79,11 +79,9 @@ class RumbleComponent : SessionPart() {
|
||||
preferences?.unregisterOnSharedPreferenceChangeListener(preferenceListener)
|
||||
preferences = null
|
||||
enabled = false
|
||||
playing?.cancel()
|
||||
playing = null
|
||||
cancelAll()
|
||||
phone = null
|
||||
controller = null
|
||||
controllerId = PadBridge.NO_CONTROLLER
|
||||
controllers.clear()
|
||||
val s = server
|
||||
server = null
|
||||
try { s?.close() } catch (e: Exception) { /* the accept loop ends on the next wake */ }
|
||||
@@ -91,13 +89,14 @@ class RumbleComponent : SessionPart() {
|
||||
|
||||
private fun u16(b: ByteArray, at: Int): Int = (b[at].toInt() and 0xFF) or ((b[at + 1].toInt() and 0xFF) shl 8)
|
||||
|
||||
@Synchronized private fun buzz(strong: Int, weak: Int, ms: Int) {
|
||||
@Synchronized private fun buzz(strong: Int, weak: Int, ms: Int, slot: Int) {
|
||||
if (!enabled) return
|
||||
val target = target()
|
||||
if (target !== playing) {
|
||||
playing?.cancel()
|
||||
playing = target
|
||||
target?.let { Log.i(TAG, "rumble: playing on ${it.name}") }
|
||||
val target = target(slot)
|
||||
val previous = playing[slot]
|
||||
if (target !== previous) {
|
||||
previous?.cancel()
|
||||
if (target == null) playing.remove(slot) else playing[slot] = target
|
||||
target?.let { Log.i(TAG, "rumble: slot $slot playing on ${it.name}") }
|
||||
}
|
||||
if (target == null) return
|
||||
if ((strong == 0 && weak == 0) || ms == 0) { target.cancel(); return }
|
||||
@@ -108,14 +107,17 @@ class RumbleComponent : SessionPart() {
|
||||
}
|
||||
}
|
||||
|
||||
/** The active controller's motors when it has any, else the phone's. */
|
||||
private fun target(): Motors? {
|
||||
val id = PadBridge.activeControllerId()
|
||||
if (id != controllerId) {
|
||||
controllerId = id
|
||||
controller = if (id == PadBridge.NO_CONTROLLER) null else try { controllerMotors(id) } catch (e: Exception) { null }
|
||||
}
|
||||
return controller ?: phone
|
||||
/** The motors of the controller playing in this slot when it has any, else the phone's. */
|
||||
private fun target(slot: Int): Motors? {
|
||||
val id = PadBridge.controllerForSlot(slot)
|
||||
if (id == PadBridge.NO_CONTROLLER) return phone
|
||||
if (!controllers.containsKey(id)) controllers[id] = try { controllerMotors(id) } catch (e: Exception) { null }
|
||||
return controllers[id] ?: phone
|
||||
}
|
||||
|
||||
private fun cancelAll() {
|
||||
playing.values.toSet().forEach { it.cancel() }
|
||||
playing.clear()
|
||||
}
|
||||
|
||||
/** One place an effect can play. Strengths are the evdev 0..65535 magnitudes. */
|
||||
|
||||
@@ -60,10 +60,6 @@ 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.
|
||||
|
||||
@@ -73,9 +73,7 @@ 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)
|
||||
// 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 total = (files + live).sumOf { it.length() }.coerceAtLeast(1L)
|
||||
var done = 0L
|
||||
var shown = -1
|
||||
fun advance(f: File) {
|
||||
@@ -86,7 +84,6 @@ 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
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ object SessionPrefs {
|
||||
const val OSC_ALWAYS = "always"
|
||||
const val OSC_STEAM_QAM = "steam-qam"
|
||||
const val OSC_NEVER = "never"
|
||||
/** Steam's touch controller (SteamTouchControls) in place of the app's pad; Steam sessions only. */
|
||||
const val OSC_STEAM_TOUCH = "steam-touch"
|
||||
|
||||
/** What Back does in a Steam session, first press then second: the labels of the two orders. */
|
||||
val BACK_MENU_THEN_QAM = R.string.back_menu_then_qam
|
||||
@@ -53,52 +55,6 @@ 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
|
||||
|
||||
@@ -325,8 +325,6 @@ 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")
|
||||
@@ -746,7 +744,9 @@ class SessionService : Service() {
|
||||
val wantsDeck = uinput && SessionState.mode == MODE_STEAM &&
|
||||
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()
|
||||
// Steam's touch controller rides on the Deck's sysfs and is offered only beside it.
|
||||
val wantsTouch = wantsDeck && !File(Environment.getExternalStorageDirectory(), NO_STEAM_TOUCH_SWITCH).exists()
|
||||
deckBinds = if (wantsDeck) SteamDeckPad.prepare(this, fakeInputDir.parentFile!!.parentFile!!, wantsTouch) else emptyList()
|
||||
SessionState.deckPad = deckBinds.isNotEmpty()
|
||||
if (wantsDeck && !SessionState.deckPad) Log.w(TAG, "deck pad: not available this session; the pad stays an Xbox 360 controller")
|
||||
logControllersAtStart()
|
||||
@@ -756,6 +756,10 @@ class SessionService : Service() {
|
||||
guest.add("FAKE_EVDEV_UINPUT=1")
|
||||
if (SessionState.deckPad) {
|
||||
guest.add("FAKE_EVDEV_DECK=1")
|
||||
// Steam's touch controller (SteamTouchDevice): the file the app and libfakeinput share.
|
||||
if (wantsTouch) com.droiddeck.launcher.input.SteamTouchDevice.prepare(fakeInputDir)?.let {
|
||||
guest.add("FAKE_TOUCHCTL_RING=" + it.path)
|
||||
}
|
||||
guest.add("FAKE_DECK_SYSFS_LISTING=" + SteamDeckPad.listingDir(fakeInputDir.parentFile!!.parentFile!!).path)
|
||||
}
|
||||
} else if (SessionState.mode == MODE_STEAM) {
|
||||
@@ -896,15 +900,6 @@ 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) }
|
||||
@@ -1416,10 +1411,6 @@ 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()
|
||||
@@ -1511,6 +1502,7 @@ class SessionService : Service() {
|
||||
}
|
||||
components.clear()
|
||||
FakeInputWriter.releaseAllRingSlots()
|
||||
com.droiddeck.launcher.input.SteamTouchDevice.release()
|
||||
val steamClientMayRun = SessionState.mode == MODE_STEAM || SessionState.mode == MODE_DESKTOP
|
||||
val finishAfterTeardown: () -> Unit = {
|
||||
if (prootPid > 1 || auxiliary.isNotEmpty()) {
|
||||
@@ -1771,6 +1763,7 @@ class SessionService : Service() {
|
||||
private const val NO_PAD_SWITCH = "Download/droiddeck-no-pad"
|
||||
private const val NO_UINPUT_SWITCH = "Download/droiddeck-no-uinput"
|
||||
private const val NO_DECK_PAD_SWITCH = "Download/droiddeck-no-deck-pad"
|
||||
private const val NO_STEAM_TOUCH_SWITCH = "Download/droiddeck-no-steam-touch"
|
||||
/** libfakeinput numbers the pads made through its /dev/uinput stand-in from here. */
|
||||
private const val FIRST_VIRTUAL_PAD = 16
|
||||
|
||||
|
||||
@@ -44,6 +44,24 @@ object SteamDeckPad {
|
||||
private const val HIDRAW = "$HID/hidraw/$NODE"
|
||||
private const val SERIAL = "DROIDDECK0001"
|
||||
|
||||
// Steam's touch controller (SteamTouchDevice), a second device beside the Deck's: 0000:11fb,
|
||||
// "Mobile Touch Control", on a USB device of its own. Must match libfakeinput (TOUCH_*).
|
||||
private const val TOUCH_MINOR = 17
|
||||
private const val TOUCH_NODE = "hidraw$TOUCH_MINOR"
|
||||
private const val TOUCH_USB = "$GUEST_DEVICES/usb2"
|
||||
private const val TOUCH_INTERFACE = "$TOUCH_USB/2-1:1.0"
|
||||
private const val TOUCH_HID = "$TOUCH_INTERFACE/0003:0000:11FB.0002"
|
||||
private const val TOUCH_HIDRAW = "$TOUCH_HID/hidraw/$TOUCH_NODE"
|
||||
private const val TOUCH_SERIAL = "DROIDDECK0001"
|
||||
|
||||
/** libfakeinput's kTouchReportDescriptor: Generic Desktop / Game Pad, 40-byte input report. */
|
||||
private val TOUCH_REPORT_DESCRIPTOR = intArrayOf(
|
||||
0x05, 0x01, 0x09, 0x05, 0xa1, 0x01,
|
||||
0x09, 0x02, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x28, 0x81, 0x02,
|
||||
0x09, 0x03, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0x91, 0x02,
|
||||
0x09, 0x04, 0x15, 0x00, 0x26, 0xff, 0x00, 0x75, 0x08, 0x95, 0x40, 0xb1, 0x02, 0xc0,
|
||||
).map { it.toByte() }.toByteArray()
|
||||
|
||||
/** InputPlumber's CONTROLLER_DESCRIPTOR, as libfakeinput answers HIDIOCGRDESC. */
|
||||
private val REPORT_DESCRIPTOR = intArrayOf(
|
||||
0x06, 0xff, 0xff, 0x09, 0x01, 0xa1, 0x01, 0x09, 0x02, 0x09, 0x03, 0x15, 0x00,
|
||||
@@ -55,7 +73,7 @@ object SteamDeckPad {
|
||||
fun listingDir(sessionRoot: File) = File(sessionRoot, "sys/deck/listing")
|
||||
|
||||
/** Writes the tree and returns the `host:guest` binds for it, or none if it could not be made. */
|
||||
fun prepare(context: Context, sessionRoot: File): List<String> {
|
||||
fun prepare(context: Context, sessionRoot: File, touch: Boolean = false): List<String> {
|
||||
val base = File(sessionRoot, "sys/deck")
|
||||
val devices = File(base, "devices")
|
||||
val hidrawClass = File(base, "class-hidraw")
|
||||
@@ -107,6 +125,34 @@ object SteamDeckPad {
|
||||
val udevData = File(LinuxRuntime.rootDir(context), "run/udev/data").apply { mkdirs() }
|
||||
File(udevData, "c$MAJOR:$MINOR").writeText("I:1\nE:ID_INPUT=1\nE:ID_INPUT_JOYSTICK=1\n")
|
||||
|
||||
if (touch) {
|
||||
write(TOUCH_USB, "uevent", "DEVTYPE=usb_device\nPRODUCT=0/11fb/0\nTYPE=0/0/0\nBUSNUM=002\nDEVNUM=002\n")
|
||||
write(TOUCH_USB, "idVendor", "0000\n")
|
||||
write(TOUCH_USB, "idProduct", "11fb\n")
|
||||
write(TOUCH_USB, "bcdDevice", "0000\n")
|
||||
write(TOUCH_USB, "manufacturer", "Valve Software\n")
|
||||
write(TOUCH_USB, "product", "Mobile Touch Control\n")
|
||||
write(TOUCH_USB, "serial", "$TOUCH_SERIAL\n")
|
||||
link(File(dir(TOUCH_USB), "subsystem"), "/sys/bus/usb")
|
||||
write(TOUCH_INTERFACE, "uevent", "DEVTYPE=usb_interface\nPRODUCT=0/11fb/0\nINTERFACE=3/0/0\n")
|
||||
write(TOUCH_INTERFACE, "bInterfaceNumber", "00\n")
|
||||
write(TOUCH_INTERFACE, "bInterfaceClass", "03\n")
|
||||
link(File(dir(TOUCH_INTERFACE), "subsystem"), "/sys/bus/usb")
|
||||
write(TOUCH_HID, "uevent", "DRIVER=hid-generic\nHID_ID=0003:00000000:000011FB\n" +
|
||||
"HID_NAME=Mobile Touch Control\nHID_PHYS=usb-droiddeck-2/input0\n" +
|
||||
"HID_UNIQ=$TOUCH_SERIAL\nMODALIAS=hid:b0003g0001v00000000p000011FB\n")
|
||||
File(dir(TOUCH_HID), "report_descriptor").writeBytes(TOUCH_REPORT_DESCRIPTOR)
|
||||
link(File(dir(TOUCH_HID), "subsystem"), "/sys/bus/hid")
|
||||
write(TOUCH_HIDRAW, "uevent", "MAJOR=$MAJOR\nMINOR=$TOUCH_MINOR\nDEVNAME=$TOUCH_NODE\n")
|
||||
write(TOUCH_HIDRAW, "dev", "$MAJOR:$TOUCH_MINOR\n")
|
||||
link(File(dir(TOUCH_HIDRAW), "subsystem"), "/sys/class/hidraw")
|
||||
link(File(dir(TOUCH_HIDRAW), "device"), TOUCH_HID)
|
||||
link(File(hidrawClass, TOUCH_NODE), TOUCH_HIDRAW)
|
||||
link(File(context.cacheDir, "drm/sys/$MAJOR:$TOUCH_MINOR"), TOUCH_HIDRAW)
|
||||
File(udevData, "c$MAJOR:$TOUCH_MINOR").writeText("I:1\nE:ID_INPUT=1\nE:ID_INPUT_JOYSTICK=1\n")
|
||||
Log.i(TAG, "deck pad: /dev/$TOUCH_NODE described as Steam's touch controller (0000:11fb)")
|
||||
}
|
||||
|
||||
Log.i(TAG, "deck pad: /dev/$NODE described as a Steam Deck controller (28de:1205)")
|
||||
listOf(devices.path + ":" + GUEST_DEVICES, hidrawClass.path + ":/sys/class/hidraw")
|
||||
} catch (e: Exception) {
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
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<String>, val conflicts: List<String>, val same: List<String>, 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<String, Local>, remote: Map<String, Remote>, baseline: Map<String, Entry>?): Plan {
|
||||
val transfer = ArrayList<String>(); val conflicts = ArrayList<String>(); val same = ArrayList<String>()
|
||||
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<String, Local>, remote: Map<String, Remote>, baseline: Map<String, Entry>?): Plan {
|
||||
if (baseline == null) return Plan(emptyList(), emptyList(), emptyList(), refused = "no-baseline")
|
||||
val transfer = ArrayList<String>(); val conflicts = ArrayList<String>(); val same = ArrayList<String>()
|
||||
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, Entry>): 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<String, Entry>? {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
/*
|
||||
* 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 `<?TOKEN?>` (or `%TOKEN%`) and a path, e.g. ELDERBORN's
|
||||
* `<?APPLICATION_DATA_LOCAL_LOW?>/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() }
|
||||
}
|
||||
@@ -1,387 +0,0 @@
|
||||
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<String> = emptyList())
|
||||
|
||||
private fun game(context: Context, store: Store, id: String): Pair<File, StoreGameSidecar>? =
|
||||
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/<its shortcut appid>/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<File?, String> {
|
||||
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<String, CloudPlan.Entry>? =
|
||||
CloudPlan.fromJson(prefs(context, store).getString("cloud_baseline_$id", null))
|
||||
|
||||
private fun saveBaseline(context: Context, store: Store, id: String, b: Map<String, CloudPlan.Entry>) =
|
||||
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<String> =
|
||||
prefs(context, store).getString("cloud_conflicts_$id", "").orEmpty().split('\n').filter { it.isNotEmpty() }
|
||||
|
||||
private fun saveConflicts(context: Context, store: Store, id: String, names: Collection<String>) =
|
||||
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<String, File> =
|
||||
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<String, CloudFile> {
|
||||
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<Pair<Store, String>> = 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<CloudFile>) {
|
||||
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() }
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
/*
|
||||
* 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<CloudFile>
|
||||
/** 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<ByteArray, Long>
|
||||
/** Writes [files] (key → bytes); the cloud times assigned, where the service says (-1 unknown). */
|
||||
fun put(files: Map<String, ByteArray>): Map<String, Long>
|
||||
}
|
||||
|
||||
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/<userId>/<clientId>[/<key>]`. */
|
||||
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<CloudFile> {
|
||||
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<ByteArray, Long> {
|
||||
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<String, ByteArray>): Map<String, Long> = 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/<accountId>/<appName>/`. */
|
||||
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<CloudFile> {
|
||||
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 `<...>/<appName>/<key>`; 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<ByteArray, Long> {
|
||||
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<String, ByteArray>): Map<String, Long> {
|
||||
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<String, Long>()
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
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/<store>/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<String, Pair<String, Long>>()
|
||||
|
||||
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"))
|
||||
}
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
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<AddedGames.Game>) {
|
||||
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() } }
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
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 }
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
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/<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.
|
||||
*/
|
||||
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 }
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
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 <store>" 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<String, String> = 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<CatalogItem> = emptyList(),
|
||||
val deals: List<CatalogItem> = emptyList(),
|
||||
val free: List<CatalogItem> = emptyList(),
|
||||
val trending: List<CatalogItem> = emptyList(),
|
||||
) {
|
||||
val isEmpty: Boolean get() = whatsNew.isEmpty() && deals.isEmpty() && free.isEmpty() && trending.isEmpty()
|
||||
val all: List<CatalogItem> 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<String>): 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<Int>): 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<InstalledStoreGame>, library: List<CatalogItem>): List<CatalogItem> {
|
||||
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<CatalogItem>?, new: List<CatalogItem>): List<CatalogItem> {
|
||||
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)<br\\s*/?>|</p>|</li>|</div>"), "\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"
|
||||
@@ -1,44 +0,0 @@
|
||||
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<File>()
|
||||
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<File> { depth(folder, it) }.thenByDescending { it.length() }).first()
|
||||
return relative(folder, best)
|
||||
}
|
||||
|
||||
private fun collect(dir: File, out: MutableList<File>, 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('\\', '/')
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
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<String> = emptyList(),
|
||||
val env: Map<String, String> = 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<String, String> = 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<String> = this.args,
|
||||
env: Map<String, String> = this.env, installVersion: String = this.installVersion,
|
||||
installedAt: Long = this.installedAt, cover: String? = this.cover, hero: String? = this.hero,
|
||||
extra: Map<String, String> = 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<String, String>()
|
||||
o.optJSONObject("env")?.let { e -> e.keys().forEach { k -> env[k] = e.optString(k) } }
|
||||
val extra = LinkedHashMap<String, String>()
|
||||
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
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
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: `<root>/<Store>/<title>`.
|
||||
*
|
||||
* 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" } }
|
||||
}
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
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()
|
||||
}
|
||||
@@ -1,308 +0,0 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
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 ""
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
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
|
||||
} }
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,271 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,288 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
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 ?: "")),
|
||||
)
|
||||
}
|
||||
@@ -1,158 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
// 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)
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
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()
|
||||
}
|
||||
@@ -1,379 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -1,299 +0,0 @@
|
||||
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"
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -1,810 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
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 = "";
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
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"
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
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", "")
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
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 ?: "")),
|
||||
)
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
// 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)
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,225 +0,0 @@
|
||||
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()
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
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")))
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,211 +0,0 @@
|
||||
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}",
|
||||
)
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
// 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)
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -1,209 +0,0 @@
|
||||
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()
|
||||
}
|
||||
@@ -83,7 +83,6 @@ 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
|
||||
@@ -313,7 +312,7 @@ internal fun rememberShown(onDismiss: () -> Unit): MutableTransitionState<Boolea
|
||||
@Composable
|
||||
internal fun AppDialog(
|
||||
shown: MutableTransitionState<Boolean>, close: () -> Unit, label: String, wide: Boolean,
|
||||
modifier: Modifier = Modifier, maxWidth: Dp? = null, content: @Composable ColumnScope.() -> Unit,
|
||||
modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit,
|
||||
) {
|
||||
val pal = LocalPalette.current
|
||||
// The page's ring stays out of sight behind it.
|
||||
@@ -336,7 +335,7 @@ internal fun AppDialog(
|
||||
verticalArrangement = Arrangement.spacedBy(14.dp),
|
||||
modifier = Modifier
|
||||
.padding(12.dp)
|
||||
.widthIn(max = maxWidth ?: if (wide) 860.dp else 560.dp)
|
||||
.widthIn(max = 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)
|
||||
|
||||
@@ -43,7 +43,7 @@ private fun Swatch(color: Int) {
|
||||
fun ColumnScope.ControllerRows(host: MenuHost, oscMode: String, c: ControllerPrefs.Settings, a: ControllerActions) {
|
||||
ChoiceRow(
|
||||
host, "controller-osc", stringResource(R.string.mode_osc), stringResource(R.string.ctrl_osc_hint),
|
||||
listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)),
|
||||
listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)),
|
||||
oscMode, note = stringResource(R.string.mode_osc_note), onPick = a.onOsc,
|
||||
)
|
||||
val tintOpen = host.open == "controller-tint"
|
||||
|
||||
@@ -230,11 +230,6 @@ 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)
|
||||
|
||||
@@ -197,19 +197,15 @@ private fun ManageSaves(g: Library.SteamGame, prefix: java.io.File, a: FrontEndA
|
||||
}
|
||||
}
|
||||
Box {
|
||||
// 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) {
|
||||
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) {
|
||||
open = false; a.onSaveImport(g)
|
||||
}
|
||||
MenuItem(stringResource(R.string.games_export_gamehub), checked = false) {
|
||||
MenuItem(stringResource(R.string.games_export_gamehub), checked = false, detail = stringResource(R.string.games_export_gamehub_hint)) {
|
||||
open = false; a.onSaveExport(g, com.droiddeck.launcher.session.GameSaves.Layout.GAMEHUB)
|
||||
}
|
||||
MenuItem(stringResource(R.string.games_export_winlator), checked = false) {
|
||||
MenuItem(stringResource(R.string.games_export_winlator), checked = false, detail = stringResource(R.string.games_export_winlator_hint)) {
|
||||
open = false; a.onSaveExport(g, com.droiddeck.launcher.session.GameSaves.Layout.WINLATOR)
|
||||
}
|
||||
found?.firstOrNull()?.let { d ->
|
||||
@@ -221,74 +217,14 @@ 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) {
|
||||
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(
|
||||
(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,
|
||||
)
|
||||
Text(
|
||||
g.name, fontSize = titleSize, lineHeight = titleSize * 1.15f, fontWeight = FontWeight.Bold,
|
||||
color = MaterialTheme.colorScheme.onBackground, maxLines = 2, overflow = TextOverflow.Ellipsis,
|
||||
@@ -341,14 +277,10 @@ 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)
|
||||
// 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,
|
||||
)
|
||||
}
|
||||
Text(
|
||||
playedSpan(g.lastPlayed)?.replaceFirstChar { it.uppercase() } ?: stringResource(R.string.games_never_played),
|
||||
fontSize = 12.sp, color = colors.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -397,15 +329,11 @@ 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(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 -> ""
|
||||
private fun libraryLabel(library: String): String = when (library) {
|
||||
"internal" -> stringResource(R.string.games_internal)
|
||||
Library.ADDED -> stringResource(R.string.games_added)
|
||||
else -> library
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,7 +44,6 @@ 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
|
||||
@@ -58,7 +57,6 @@ 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
|
||||
@@ -103,7 +101,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.gameStoresEnabled) 1 else 0) + (if (s.isHomeApp) 1 else 0)
|
||||
val count = 6 + (if (s.storeEnabled) 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(
|
||||
@@ -114,9 +112,6 @@ 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") }
|
||||
@@ -139,10 +134,7 @@ internal fun SideRail(
|
||||
@Composable
|
||||
private fun RailItem(
|
||||
label: String, icon: ImageVector, key: String, current: Boolean, compact: Boolean, iconOnly: Boolean, height: Dp,
|
||||
badge: Boolean = false,
|
||||
/** A number on the item (downloads in flight); 0 shows nothing. */
|
||||
count: Int = 0,
|
||||
onFocus: () -> Unit = {}, onClick: () -> Unit,
|
||||
badge: Boolean = false, onFocus: () -> Unit = {}, onClick: () -> Unit,
|
||||
) {
|
||||
val colors = MaterialTheme.colorScheme
|
||||
val pal = LocalPalette.current
|
||||
@@ -190,12 +182,6 @@ 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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,17 +129,6 @@ 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(),
|
||||
)
|
||||
@@ -190,13 +179,6 @@ 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 = { _, _ -> },
|
||||
@@ -374,11 +356,6 @@ 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) }
|
||||
@@ -414,9 +391,8 @@ 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; so does Stores.
|
||||
// The Store turned off in Setup takes its page with it.
|
||||
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"
|
||||
|
||||
@@ -76,7 +76,6 @@ 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.
|
||||
@@ -368,24 +367,6 @@ 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -475,15 +456,6 @@ 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),
|
||||
@@ -507,53 +479,6 @@ 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
|
||||
|
||||
@@ -270,6 +270,7 @@ fun ModeSettingsPage(s: ModeSettings, a: ModeSettingsActions) {
|
||||
listOf(
|
||||
SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto),
|
||||
SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always),
|
||||
SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch),
|
||||
SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam),
|
||||
SessionPrefs.OSC_NEVER to stringResource(R.string.common_never),
|
||||
), s.oscMode,
|
||||
|
||||
@@ -217,6 +217,8 @@ class DrawerActions(
|
||||
val onSteamMenu: (() -> Unit)?,
|
||||
val onQam: (() -> Unit)?,
|
||||
val onOsc: (String) -> Unit,
|
||||
/** Edits the Steam touch layout of the game in front, when Steam's controls are up. */
|
||||
val onEditSteamTouch: (() -> Unit)? = null,
|
||||
val controller: com.droiddeck.launcher.input.ControllerPrefs.Settings? = null,
|
||||
val onRumble: (Boolean) -> Unit = {},
|
||||
val onSteamButton: (Boolean) -> Unit = {},
|
||||
@@ -418,9 +420,14 @@ internal fun SessionDrawer(open: Boolean, requestedPage: SessionDrawerPage, cont
|
||||
listOf(SessionPrefs.TOUCH_AUTO to stringResource(R.string.drawer_touch_auto, a.touchAuto), SessionPrefs.TOUCH_PAD to stringResource(R.string.mode_touch_touchpad), SessionPrefs.TOUCH_DIRECT to stringResource(R.string.mode_touch_direct), SessionPrefs.TOUCH_OFF to stringResource(R.string.widgets_off)),
|
||||
a.touchMode, chipModifier = focus.track(page, "touch"), onPick = a.onTouch)
|
||||
ChoiceRow(host, "osc", stringResource(R.string.mode_osc), null,
|
||||
if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never))
|
||||
if (a.steam) listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_STEAM_TOUCH to stringResource(R.string.osc_steam_touch), SessionPrefs.OSC_STEAM_QAM to stringResource(R.string.mode_osc_qam), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never))
|
||||
else listOf(SessionPrefs.OSC_AUTO to stringResource(R.string.common_auto), SessionPrefs.OSC_ALWAYS to stringResource(R.string.common_always), SessionPrefs.OSC_NEVER to stringResource(R.string.common_never)),
|
||||
a.oscMode, chipModifier = focus.track(page, "osc"), onPick = a.onOsc)
|
||||
// Steam's touch controller is Steam's alone; elsewhere that choice behaves as Auto.
|
||||
if (!a.steam && a.oscMode == SessionPrefs.OSC_STEAM_TOUCH) SessionPrefs.OSC_AUTO else a.oscMode,
|
||||
chipModifier = focus.track(page, "osc"), onPick = a.onOsc)
|
||||
a.onEditSteamTouch?.let { edit ->
|
||||
ActionRow(stringResource(R.string.osc_steam_layout), stringResource(R.string.osc_steam_layout_hint), stringResource(R.string.ctrl_edit), edit)
|
||||
}
|
||||
a.controller?.let { c ->
|
||||
ToggleRow(host, "rumble", stringResource(R.string.ctrl_rumble), null, c.rumble,
|
||||
chipModifier = focus.track(page, "rumble"), onChange = a.onRumble)
|
||||
|
||||
@@ -233,19 +233,6 @@ 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
|
||||
@@ -258,12 +245,7 @@ private class BelowEndProvider(private val gap: Int) : PopupPositionProvider {
|
||||
}
|
||||
|
||||
@Composable
|
||||
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,
|
||||
) {
|
||||
fun AnchoredMenu(open: Boolean, onDismiss: () -> Unit, title: String? = null, note: String? = null, content: @Composable ColumnScope.(FocusRequester) -> Unit) {
|
||||
val state = remember { MutableTransitionState(false) }
|
||||
state.targetState = open
|
||||
if (!state.currentState && !state.targetState && state.isIdle) return
|
||||
@@ -277,14 +259,14 @@ fun AnchoredMenu(
|
||||
}
|
||||
}
|
||||
val gap = with(LocalDensity.current) { 6.dp.roundToPx() }
|
||||
val provider = remember(gap, compact) { if (compact) BelowStartProvider(gap) else BelowEndProvider(gap) }
|
||||
val provider = remember(gap) { 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(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)),
|
||||
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)),
|
||||
label = "menu",
|
||||
) {
|
||||
// A long list (a driver menu with its downloads) must not run off the screen: the menu is
|
||||
@@ -292,7 +274,7 @@ fun AnchoredMenu(
|
||||
val maxHeight = (androidx.compose.ui.platform.LocalConfiguration.current.screenHeightDp * 0.86f).dp
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.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))
|
||||
.widthIn(min = 220.dp, max = 340.dp)
|
||||
.heightIn(max = maxHeight)
|
||||
.shadow(24.dp, RowShape, ambientColor = Color.Black, spotColor = Color.Black)
|
||||
.clip(RowShape)
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
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 }
|
||||
@@ -1,118 +0,0 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
@@ -1,328 +0,0 @@
|
||||
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) } }
|
||||
}
|
||||
}
|
||||
Loaded 100 of 166 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user