diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents index 62b705effe..39ce61f43a 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents @@ -613,6 +613,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING if foreign_component(rel, saved, machines, dist, game): if path is not None and not path.is_symlink(): path.unlink() + keep[rel] = cid continue target = path or (pfx / rel) os.replace(saved, target) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe index 46643dc42f..7455d50403 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe @@ -174,20 +174,27 @@ def program_imports(builtins): return PROGRAM_IMPORTS[key] -def game_arm64ec_imports(folder): +def game_imports(folder): key = str(folder) if key not in GAME_IMPORTS: - names = set() + arm64, arm64ec = set(), set() root = os.path.realpath(folder) for top, dirs, files in os.walk(root): if os.path.relpath(top, root).count(os.sep) >= GAME_DEPTH: dirs[:] = [] for entry in files: + if not entry.lower().endswith((".exe", ".dll")): + continue path = os.path.join(top, entry) - if (entry.lower().endswith((".exe", ".dll")) and pe_machine(path) == 0x8664 - and pe_has_arm64ec_metadata(path)): - names |= pe_imports(path) or set() - GAME_IMPORTS[key] = frozenset(names) + machine = pe_machine(path) + if machine == 0xAA64: + found = arm64 + elif machine == 0x8664 and pe_has_arm64ec_metadata(path): + found = arm64ec + else: + continue + found |= pe_imports(path) or set() + GAME_IMPORTS[key] = (frozenset(arm64), frozenset(arm64ec)) return GAME_IMPORTS[key] @@ -198,8 +205,10 @@ def machine_compatible(path, machines, machine=None, builtins=None, name=None, g return True imported = program_imports(builtins) if builtins is not None else None name = (name or Path(path).name).lower() - if game and VC_RUNTIME.fullmatch(name) and name in game_arm64ec_imports(game): - return False + if game: + arm64, arm64ec = game_imports(game) + if name in arm64 or (name in arm64ec and VC_RUNTIME.fullmatch(name)): + return False return imported is not None and name not in imported if machine == 0x8664 and pe_has_arm64ec_metadata(path): return False diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index b1566040fb..2ca41f1dfe 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -3509,6 +3509,12 @@ def arm64ec_program_image(imports): return bytes(image) +def arm64x_program_image(imports): + image = bytearray(arm64ec_program_image(imports)) + image[68:70] = (0xAA64).to_bytes(2, "little") + return bytes(image) + + def bytes_path(folder, data): path = folder / "image.exe" path.write_bytes(data) @@ -3595,7 +3601,7 @@ class ForeignSystemDllTest(unittest.TestCase): def visual_cpp_prefix(self, tmp): proton, compat, windows = self.prefix(tmp) builtins = proton.parent / "files/lib/wine/aarch64-windows" - for name in ("vcruntime140.dll", "msvcp140.dll", "concrt140.dll", "vulkan-1.dll"): + for name in ("vcruntime140.dll", "msvcp140.dll", "concrt140.dll", "vulkan-1.dll", "d3dcompiler_47.dll"): (builtins / name).write_bytes(program_image(0xAA64, [])) (windows / "system32" / name.upper()).write_bytes(pe_image(0x8664)) return proton, compat, windows @@ -3626,6 +3632,51 @@ class ForeignSystemDllTest(unittest.TestCase): for name in ("VCRUNTIME140.DLL", "MSVCP140.DLL"): self.assertFalse((windows / "system32" / name).is_symlink()) + def arm64_game(self, tmp, exe_imports, plugin_imports=()): + game = Path(tmp) / "common/Game" + binaries = game / "BinArm64" + binaries.mkdir(parents=True) + (game / "Game.exe").write_bytes(program_image(0x8664, ["kernel32.dll"])) + (binaries / "Game.arm64.exe").write_bytes(program_image(0xAA64, exe_imports)) + (binaries / "plugin.dll").write_bytes(arm64x_program_image(list(plugin_imports))) + return {"STEAM_COMPAT_INSTALL_PATH": str(game)} + + def test_x64_dlls_an_arm64_game_image_imports_are_replaced_by_the_builtins(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.visual_cpp_prefix(tmp) + env = self.arm64_game(tmp, ["MSVCP140.dll", "D3DCOMPILER_47.dll"], ["VCRUNTIME140.dll"]) + fixed = MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env) + self.assertEqual(["system32/D3DCOMPILER_47.DLL (machine 0x8664)", "system32/MSVCP140.DLL (machine 0x8664)", + "system32/VCRUNTIME140.DLL (machine 0x8664)"], fixed) + for name in ("D3DCOMPILER_47.DLL", "MSVCP140.DLL", "VCRUNTIME140.DLL"): + self.assertTrue((windows / "system32" / name).is_symlink()) + self.assertEqual(0xAA64, MODULE["pe_machine"](windows / "system32" / name)) + for name in ("CONCRT140.DLL", "VULKAN-1.DLL"): + self.assertFalse((windows / "system32" / name).is_symlink()) + self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32" / name)) + self.assertEqual([], MODULE["restore_compatible_system_dlls"](str(compat), str(proton), env)) + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + self.assertEqual(3, len(MODULE["restore_compatible_system_dlls"](str(compat), str(proton)))) + self.assertFalse((windows / "system32/D3DCOMPILER_47.DLL").is_symlink()) + + def test_x64_dlls_an_arm64_game_does_not_import_stay(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.visual_cpp_prefix(tmp) + env = self.arm64_game(tmp, ["kernel32.dll"], ["user32.dll"]) + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + for name in ("D3DCOMPILER_47.DLL", "MSVCP140.DLL", "VCRUNTIME140.DLL"): + self.assertFalse((windows / "system32" / name).is_symlink()) + + def test_non_runtime_dlls_stay_for_x64_and_arm64ec_games(self): + for shipping in (program_image(0x8664, ["MSVCP140.dll", "D3DCOMPILER_47.dll"]), + arm64ec_program_image(["D3DCOMPILER_47.dll"])): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.visual_cpp_prefix(tmp) + env = self.arm64ec_game(tmp, shipping) + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + for name in ("D3DCOMPILER_47.DLL", "MSVCP140.DLL"): + self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32" / name)) + def test_arm64ec_images_below_the_scanned_depth_are_ignored(self): with tempfile.TemporaryDirectory() as tmp: proton, compat, windows = self.visual_cpp_prefix(tmp) diff --git a/tools/tests/test_wincomponents.py b/tools/tests/test_wincomponents.py index 1ed6bb0d2d..d2e188853d 100644 --- a/tools/tests/test_wincomponents.py +++ b/tools/tests/test_wincomponents.py @@ -6,7 +6,7 @@ import unittest from pathlib import Path from unittest import mock -from tools.tests.test_game_environment import arm64ec_program_image +from tools.tests.test_game_environment import arm64ec_program_image, program_image BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" MODULE = runpy.run_path(str(BIN / "droiddeck-wincomponents")) @@ -187,6 +187,40 @@ class WinComponentsTest(unittest.TestCase): self.assertFalse((self.pfx / "system32/msvcp140.dll").exists()) self.assertFalse((self.pfx / "system32/vcruntime140.dll").exists()) + def arm64_game(self, imports): + game = Path(self.tmp.name) / "common/Game/BinArm64" + game.mkdir(parents=True) + (game / "Game.arm64.exe").write_bytes(program_image(0xAA64, imports)) + return {"STEAM_COMPAT_INSTALL_PATH": str(game.parent)} + + def test_x64_dlls_an_arm64_game_imports_are_not_placed(self): + proton = self.proton() + env = self.arm64_game(["MSVCP140.dll", "D3DCOMPILER_47.dll"]) + names = ["concrt140", "d3dcompiler_47", "msvcp140", "vcruntime140"] + self.component("vc", names, {"system32/%s.dll" % name: self.pe_image(0x8664) for name in names}) + self.pick("vc") + for _ in range(2): + placed, _ = self.apply(proton, env) + self.assertEqual(placed, ["concrt140", "vcruntime140"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/vcruntime140.dll"), 0x8664) + self.assertFalse((self.pfx / "system32/msvcp140.dll").exists()) + self.assertFalse((self.pfx / "system32/d3dcompiler_47.dll").exists()) + + def test_saved_original_stays_tracked_while_a_game_defers_its_restoration(self): + proton = self.proton() + original = self.pe_image(0x8664) + b"original" + self.pfx.joinpath("system32").mkdir(parents=True) + self.pfx.joinpath("system32/msvcp140.dll").write_bytes(original) + self.component("vc", ["msvcp140"], {"system32/msvcp140.dll": self.pe_image(0x8664)}) + self.pick("vc") + self.apply(proton) + self.assertNotEqual(original, (self.pfx / "system32/msvcp140.dll").read_bytes()) + self.pick() + self.apply(proton, self.arm64_game(["MSVCP140.dll"])) + self.assertFalse((self.pfx / "system32/msvcp140.dll").exists()) + self.apply(proton) + self.assertEqual(original, (self.pfx / "system32/msvcp140.dll").read_bytes()) + def test_deselected_component_restores_the_saved_x64_dll(self): proton = self.proton() self.pfx.joinpath("system32").mkdir(parents=True)