diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt index e3b8c7932e..8dfa21a725 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunch.kt @@ -12,8 +12,11 @@ object StoreLaunch { const val EPIC_CODE = ".droiddeck-epic-code" const val EPIC_ARGS = ".droiddeck-epic-args" const val UBISOFT_LAUNCH = ".droiddeck-ubisoft.json" - private const val UBISOFT_SERVICE = "Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe" - const val EPIC_PARENT = "Z:\\usr\\local\\lib\\droiddeck-epic\\EpicGamesLauncher.exe" + /** DroidDeck-Bionic's Wine has Android's root on Z:; that tool sets this to the guest's root as its Wine names it. */ + const val GUEST_ROOT = "DROIDDECK_GUEST_ROOT" + private const val ROOT_VAR = "DD_R" + private const val UBISOFT_SERVICE = "%$ROOT_VAR%\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe" + const val EPIC_PARENT = "%$ROOT_VAR%\\usr\\local\\lib\\droiddeck-epic\\EpicGamesLauncher.exe" /** Per-game environment switch: 1 starts any Epic game under [EPIC_PARENT], 0 never does. */ const val EPIC_PARENT_SWITCH = "DD_EPIC_PARENT" /** Epic app names whose launchers treat the game as unowned unless a process named EpicGamesLauncher.exe started it. */ @@ -47,6 +50,10 @@ object StoreLaunch { for ((k, v) in sidecar.env) if (ENV_NAME.matches(k)) lines += "set \"$k=${v.replace("\"", "")}\"" val args = sidecar.args.joinToString(" ") { if (sidecar.store == Store.ROCKSTAR) batchArg(it) else quoteArg(it) } val run = "\"%~dp0$exe\"" + (if (args.isNotEmpty()) " $args" else "") + if (sidecar.store == Store.EPIC || sidecar.store == Store.UBISOFT) { + lines += "set \"$ROOT_VAR=Z:\"" + lines += "if defined $GUEST_ROOT set \"$ROOT_VAR=%$GUEST_ROOT%\"" + } if (sidecar.store == Store.EPIC) { lines += "set \"$PARENT_VAR=${if (sidecar.id in EPIC_PARENT_TITLES) "1" else ""}\"" lines += "if \"%$EPIC_PARENT_SWITCH%\"==\"1\" set \"$PARENT_VAR=1\"" @@ -137,7 +144,6 @@ object StoreLaunch { if (sidecar.store !in listOf(Store.UBISOFT, Store.ROCKSTAR) || !sidecar.isInstalled) return sidecar if (sidecar.store == Store.ROCKSTAR && java.nio.file.Files.isSymbolicLink(File(folder, LAUNCHER).toPath())) return sidecar.copy(launcher = null) val wrongExe = !sidecar.exeFile(folder).isFile || sidecar.exe.contains("installer", ignoreCase = true) || sidecar.exe.contains("autorun", ignoreCase = true) - if (sidecar.store == Store.UBISOFT && sidecar.launcher == LAUNCHER && !wrongExe) return sidecar val metadataExe = if (sidecar.store == Store.ROCKSTAR) com.droiddeck.launcher.stores.rockstar.RockstarMetadata.find(folder)?.exe ?.takeIf { File(folder, it).isFile && File(folder, it).canonicalPath.startsWith(folder.canonicalPath + File.separator) } else null val exe = metadataExe ?: if (wrongExe) StoreExe.pick(folder, null, sidecar.title).ifEmpty { sidecar.exe } else sidecar.exe diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt index cfde73c1d2..161ce66b00 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt @@ -168,8 +168,8 @@ class StoreGameSidecarTest { "if \"%DD_EPIC_PARENT%\"==\"1\" set \"DD_P=1\"", "if \"%DD_EPIC_PARENT%\"==\"0\" set \"DD_P=\"", "if defined DD_P set \"SteamAppId=\"", - "if defined DD_P if not exist \"Z:\\usr\\local\\lib\\droiddeck-epic\\EpicGamesLauncher.exe\" set \"DD_P=\"", - "if defined DD_P set DD_P=\"Z:\\usr\\local\\lib\\droiddeck-epic\\EpicGamesLauncher.exe\"", + "if defined DD_P if not exist \"%DD_R%\\usr\\local\\lib\\droiddeck-epic\\EpicGamesLauncher.exe\" set \"DD_P=\"", + "if defined DD_P set DD_P=\"%DD_R%\\usr\\local\\lib\\droiddeck-epic\\EpicGamesLauncher.exe\"", ) @Test fun epicTitlesThatCheckTheirParentStartUnderTheStandIn() { diff --git a/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt index 75e2df58cb..172b5c253a 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/ubisoft/UbisoftTest.kt @@ -105,11 +105,28 @@ class UbisoftTest { val text = StoreLaunch.launcherText(StoreGameSidecar(Store.UBISOFT, "space-sc", "Splinter Cell", "system/SplinterCell.exe")) val lines = text.trimEnd().split("\r\n") assertTrue(lines.contains("if not exist \"%~dp0.droiddeck-ubisoft.json\" goto direct")) - assertTrue(lines.contains("\"Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe\" \"%~dp0.droiddeck-ubisoft.json\"")) + val service = lines.indexOf("\"%DD_R%\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe\" \"%~dp0.droiddeck-ubisoft.json\"") + assertTrue(service in 0 until lines.indexOf(":direct")) + assertTrue(lines.indexOf("set \"DD_R=Z:\"") in 0 until lines.indexOf("if defined DROIDDECK_GUEST_ROOT set \"DD_R=%DROIDDECK_GUEST_ROOT%\"")) + assertTrue(lines.indexOf("if defined DROIDDECK_GUEST_ROOT set \"DD_R=%DROIDDECK_GUEST_ROOT%\"") < service) assertEquals("\"%~dp0system\\SplinterCell.exe\"", lines.last()) assertTrue(lines.indexOf(":direct") < lines.lastIndex) } + @Test fun anInstalledGamesOlderLauncherIsRewritten() { + val folder = java.nio.file.Files.createTempDirectory("ubisoft-adopt").toFile() + try { + File(folder, "system").mkdirs() + File(folder, "system/SplinterCell.exe").writeText("exe") + val sidecar = StoreGameSidecar(Store.UBISOFT, "space-sc", "Splinter Cell", "system/SplinterCell.exe", launcher = StoreLaunch.LAUNCHER) + File(folder, StoreLaunch.LAUNCHER).writeText("@echo off\r\nif not exist \"Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe\" goto direct\r\n") + StoreLaunch.adopt(folder, sidecar) + assertEquals(StoreLaunch.launcherText(sidecar), File(folder, StoreLaunch.LAUNCHER).readText()) + } finally { + folder.deleteRecursively() + } + } + @Test fun activationPayloadsSurviveTheHexHop() { val bytes = byteArrayOf(0, 1, 0x7f, -1, -128, 42) assertEquals("00017fff802a", UbisoftActivation.hex(bytes)) diff --git a/tools/bionic-runtime/tool/launch.sh b/tools/bionic-runtime/tool/launch.sh index c406cfd1d7..5b4458bb88 100644 --- a/tools/bionic-runtime/tool/launch.sh +++ b/tools/bionic-runtime/tool/launch.sh @@ -37,6 +37,7 @@ for v in $(env | sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p'); do WINE_OFFSCREEN_CHECK_MS|WINE_CPU_TOPOLOGY|WINE_LARGE_ADDRESS_AWARE|WINESTEAMNOEXEC|TZ) ;; DXVK_LOG_LEVEL|DXVK_HDR|VKD3D_DEBUG|VKD3D_SHADER_DEBUG) ;; DROIDDECK_GPU_LOAD|DROIDDECK_GPU_LOAD_DIR|DROIDDECK_VRAM_CAP|DROIDDECK_VRAM_CAP_MB|DROIDDECK_DISPLAY_DRIVER) ;; + DROIDDECK_GUEST_ROOT) ;; VKD3D_FEATURE_LEVEL|VKD3D_SHADER_MODEL|vk_x11_strict_image_count) ;; *) unset "$v" 2>/dev/null ;; esac diff --git a/tools/bionic-runtime/tool/proton b/tools/bionic-runtime/tool/proton index db8e171f93..69122097a8 100644 --- a/tools/bionic-runtime/tool/proton +++ b/tools/bionic-runtime/tool/proton @@ -26,6 +26,16 @@ real() { case "$p" in /data/*|/storage/*|/sdcard/*|/system/*|/apex/*|/vendor/*) echo "$p" ;; *) echo "$DDNATV_ROOT$p" ;; esac } +# A guest Z: path (also as NAME=Z:..., e.g. an installer's /DIR=) -> the Z: path of its Android file; anything else unchanged +zpath() { + local p k + case "$1" in + [Zz]:[\\/]*) p=${1:2}; p=$(real "${p//\\//}"); printf 'Z:%s\n' "${p//\//\\}" ;; + *=[Zz]:[\\/]*) k=${1%%=[Zz]:[\\/]*}; printf '%s=%s\n' "$k" "$(zpath "${1#"$k="}")" ;; + *) printf '%s\n' "$1" ;; + esac +} + here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) tool=$(real "$here") compat=$(real "${STEAM_COMPAT_DATA_PATH:-$HOME/.local/share/Steam/steamapps/compatdata/0}") @@ -33,24 +43,31 @@ compat=$(real "${STEAM_COMPAT_DATA_PATH:-$HOME/.local/share/Steam/steamapps/comp case "$verb" in run|waitforexitandrun|runinprefix) [ $# -gt 0 ] || exit 0 - # A Windows path (DroidDeck's Xbox launch names the game on a drive letter of its own) or a bare program name - # (DroidDeck's store steps run cmd.exe, which Wine finds in system32) goes to Wine unchanged. + # Another Windows path (DroidDeck's Xbox launch names the game on a drive letter of its own) or a bare program + # name (DroidDeck's store steps run cmd.exe, which Wine finds in system32) goes to Wine unchanged. case "$1" in + [Zz]:[\\/]*) exe=$(zpath "$1") ;; [A-Za-z]:[\\/]*) exe=$1 ;; /*) exe=$(real "$1") ;; */*) exe=$(real "$PWD/$1") ;; *) exe=$1 ;; esac shift + # DroidDeck's store and Xbox helpers name guest files by Z: paths (reg.exe import, cmd.exe /c, msiexec /i, setup + # /DIR=); this Wine's Z: is Android's root. + args=() + for a in "$@"; do args+=("$(zpath "$a")"); done export DROIDDECK_BIONIC_ROOT="$DDNATV_ROOT" - # The Xbox runtime's DLC and mount folders come as Z: paths of the guest; this Wine sees the Android side. + # The guest's root as this Wine names it; the store launch files DroidDeck writes (StoreLaunch.kt) start their + # helpers from it. + r=$(real /); r=${r%/}; DROIDDECK_GUEST_ROOT="Z:${r//\//\\}"; export DROIDDECK_GUEST_ROOT + # The Xbox runtime's DLC and mount folders come as ;-separated Z: paths of the guest. zreal() { - local out="" part p + local out="" part local -a parts IFS=';' read -r -a parts <<< "$1" for part in "${parts[@]}"; do - case "$part" in [Zz]:\\*) p=${part:2}; p=$(real "${p//\\//}"); part="Z:${p//\//\\}" ;; esac - out="${out:+$out;}$part" + out="${out:+$out;}$(zpath "$part")" done echo "$out" } @@ -75,7 +92,7 @@ print(" ".join(sorted(n for n in names if re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*" PY ) export DROIDDECK_BIONIC_KEEP - exec /system/bin/sh "$tool/bionic/launch.sh" "$verb" "$compat" "$exe" "$@" ;; + exec /system/bin/sh "$tool/bionic/launch.sh" "$verb" "$compat" "$exe" ${args[@]+"${args[@]}"} ;; getcompatpath) p=$(real "${1:-}") printf 'Z:%s\n' "${p//\//\\}" ;; diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-gog-maintenance b/tools/linuxfs/overlay/usr/local/bin/droiddeck-gog-maintenance index ec2aa51854..3d061e771c 100644 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-gog-maintenance +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-gog-maintenance @@ -243,6 +243,7 @@ def run_patch(folder, appid, installer, game): previous = dict(os.environ) try: os.environ.update(env) + os.environ["DD_STORE_PROTON"] = command pfx = Path(prefix) / "pfx" if not (pfx / "system.reg").is_file() or not (pfx / "drive_c/windows/system32/reg.exe").is_file(): subprocess.run([command, "run", "wineboot.exe", "-u"], env=env, timeout=90, check=True) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch index 76bdb395f0..3c703d76af 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch @@ -2,6 +2,7 @@ import base64 import binascii +import functools import json import os import re @@ -156,7 +157,27 @@ def ask(channel, request, timeout=TIMEOUT, claim=False): def windows_path(unix): - return "Z:" + unix.replace("/", "\\") + tool = bionic_tool(os.environ.get("DD_STORE_PROTON", "")) + return (tool and bionic_path(tool, unix)) or "Z:" + unix.replace("/", "\\") + + +def bionic_tool(proton): + if not proton or not os.environ.get("DDNATV_ROOT"): + return None + tool = Path(proton).parent + return str(tool / "proton") if (tool / "bionic" / "launch.sh").is_file() else None + + +@functools.lru_cache(maxsize=None) +def bionic_path(proton, unix): + # DroidDeck-Bionic's Wine sees Android's root on Z:, so a guest path is named by the Android file it maps to. + try: + result = subprocess.run([proton, "getcompatpath", unix], stdin=subprocess.DEVNULL, capture_output=True, + text=True, timeout=TIMEOUT) + except (OSError, subprocess.SubprocessError): + return None + path = result.stdout.strip() + return path if result.returncode == 0 and path[:3] == "Z:\\" else None def reg_escape(value): diff --git a/tools/tests/test_bionic_paths.py b/tools/tests/test_bionic_paths.py new file mode 100644 index 0000000000..9a1f2d57b0 --- /dev/null +++ b/tools/tests/test_bionic_paths.py @@ -0,0 +1,100 @@ +import importlib.machinery +import importlib.util +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path +from unittest.mock import patch + +TOOLS = Path(__file__).resolve().parents[1] +PROTON = TOOLS / 'bionic-runtime/tool/proton' +LAUNCH = TOOLS / 'bionic-runtime/tool/launch.sh' +BIN = TOOLS / 'linuxfs/overlay/usr/local/bin' +ANDROID = '/data/user/0/com.droiddeck.launcher/files/linuxfs' +BINDS = '/storage/emulated/0/Games:/home/games|/storage/1234-5678/Steam:/mnt/sd' + + +def load(name): + loader = importlib.machinery.SourceFileLoader(name.replace('-', '_'), str(BIN / name)) + spec = importlib.util.spec_from_loader(loader.name, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + + +store = load('droiddeck-store-launch') + + +def zpath(arg): + text = PROTON.read_text() + functions = text[text.index('real() {'):text.index('\nhere=')] + env = dict(os.environ, DDNATV_ROOT=ANDROID, DDNATV_BINDS=BINDS) + return subprocess.run(['bash', '-c', functions + '\nzpath "$1"', 'zpath', arg], env=env, + capture_output=True, text=True, check=True).stdout.rstrip('\n') + + +class ToolArguments(unittest.TestCase): + def test_a_guest_path_names_its_android_file(self): + self.assertEqual('Z:\\data\\user\\0\\com.droiddeck.launcher\\files\\linuxfs\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe', + zpath('Z:\\usr\\local\\lib\\droiddeck-ubisoft\\droiddeck-upc.exe')) + + def test_a_bound_folder_names_its_android_side(self): + self.assertEqual('Z:\\storage\\emulated\\0\\Games\\Brotato\\setup.exe', zpath('Z:\\home\\games\\Brotato\\setup.exe')) + self.assertEqual('Z:\\storage\\1234-5678\\Steam\\a.reg', zpath('z:/mnt/sd/a.reg')) + + def test_a_switch_value_is_mapped(self): + self.assertEqual('/DIR=Z:\\storage\\emulated\\0\\Games\\Game', zpath('/DIR=Z:\\home\\games\\Game')) + + def test_android_and_other_paths_stay(self): + for arg in ('Z:\\storage\\emulated\\0\\Download\\a.exe', 'Z:\\data\\user\\0\\com.droiddeck.launcher\\files\\linuxfs\\tmp\\a.bat', + 'C:\\windows\\system32\\reg.exe', 'X:\\Brotato.exe', '-dx12', '-n', '-e', 'cmd.exe', '/d', ''): + self.assertEqual(arg, zpath(arg)) + + def test_wine_keeps_the_guest_root(self): + self.assertIn('DROIDDECK_GUEST_ROOT) ;;', LAUNCH.read_text()) + + +class StorePaths(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.tool = Path(self.tmp.name) / 'compatibilitytools.d/DroidDeck-Bionic-12.8-r41-Proton' + (self.tool / 'bionic').mkdir(parents=True) + shutil.copy(PROTON, self.tool / 'proton') + (self.tool / 'proton').chmod(0o755) + (self.tool / 'bionic/launch.sh').write_text('') + + def env(self, **extra): + values = {'DD_STORE_PROTON': str(self.tool / 'proton'), 'DDNATV_ROOT': ANDROID, 'DDNATV_BINDS': BINDS} + values.update(extra) + return patch.dict(os.environ, values) + + def test_a_bionic_game_gets_android_paths(self): + with self.env(): + self.assertEqual('Z:\\data\\user\\0\\com.droiddeck.launcher\\files\\linuxfs\\root\\.cache\\droiddeck-ubisoft\\bridge-274', + store.windows_path('/root/.cache/droiddeck-ubisoft/bridge-274')) + self.assertEqual('Z:\\storage\\emulated\\0\\Games\\Watch_Dogs\\bin\\Watch_Dogs.exe', + store.windows_path('/home/games/Watch_Dogs/bin/Watch_Dogs.exe')) + + def test_the_wrapper_names_the_same_tool(self): + with self.env(DD_STORE_PROTON=str(self.tool / 'droiddeck-proton-wrap')): + self.assertEqual('Z:\\storage\\emulated\\0\\Games\\a.reg', store.windows_path('/home/games/a.reg')) + + def test_other_tools_keep_guest_paths(self): + (self.tool / 'bionic/launch.sh').unlink() + with self.env(): + self.assertEqual('Z:\\home\\games\\a.reg', store.windows_path('/home/games/a.reg')) + with patch.dict(os.environ, {'DD_STORE_PROTON': str(self.tool / 'proton')}): + os.environ.pop('DDNATV_ROOT', None) + self.assertEqual('Z:\\home\\games\\b.reg', store.windows_path('/home/games/b.reg')) + + def test_a_failing_tool_keeps_the_guest_path(self): + (self.tool / 'proton').write_text('#!/bin/sh\nexit 1\n') + with self.env(): + self.assertEqual('Z:\\home\\games\\c.reg', store.windows_path('/home/games/c.reg')) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/tests/test_store_launch.py b/tools/tests/test_store_launch.py index 84ce7dcfa6..e5a01aa1ce 100644 --- a/tools/tests/test_store_launch.py +++ b/tools/tests/test_store_launch.py @@ -1005,6 +1005,23 @@ class Ubisoft(unittest.TestCase): self.assertEqual('C:/users/steamuser/Saved Games/Ubisoft/u1/109/', launch['saves']) self.assertTrue((self.compat_data / 'pfx/drive_c/users/steamuser/Saved Games/Ubisoft/u1/109').is_dir()) + def test_a_bionic_launch_names_the_android_files(self): + self.runtime() + tool = self.root / 'DroidDeck-Bionic-12.8-r41-Proton' + (tool / 'bionic').mkdir(parents=True) + (tool / 'bionic/launch.sh').write_text('') + proton = Path(__file__).resolve().parents[1] / 'bionic-runtime/tool/proton' + (tool / 'proton').write_bytes(proton.read_bytes()) + (tool / 'proton').chmod(0o755) + env = {'DD_STORE_PROTON': str(tool / 'proton'), 'DDNATV_ROOT': '/data/user/0/app/files/linuxfs', + 'DDNATV_BINDS': '/storage/emulated/0/T:%s' % self.root} + with patch.dict(os.environ, env): + self.prepare(self.answer) + launch = json.loads((self.folder / store.UBISOFT_LAUNCH).read_text()) + self.assertEqual('Z:\\storage\\emulated\\0\\T\\Games\\Stores\\Ubisoft\\Splinter Cell\\system\\SplinterCell.exe', launch['exe']) + for key in ('bridge', 'log'): + self.assertTrue(launch[key].startswith('Z:\\storage\\emulated\\0\\T\\cache\\'), launch[key]) + def test_without_a_session_the_launch_file_is_removed_and_the_game_starts_as_is(self): self.runtime() (self.folder / store.UBISOFT_LAUNCH).write_text('{}')