mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
fix(game-env): keep x64 system DLLs that only x64 code loads in ARM64 prefixes (#2647)
* fix(game-env): keep x64 system DLLs that only x64 code loads in ARM64 prefixes The foreign-architecture repair treated every plain x64 DLL in system32 of an ARM64 prefix as broken and swapped in the ARM64 builtin. ARM64EC games load x64 DLLs fine, so this broke native-only DLLs such as the Khronos vulkan-1.dll loader (vulkan-1=n leaves Wine nothing to load) and native runtimes that games depend on. An x64 DLL is now foreign only when Proton's own ARM64 programs import it, found from the import tables of the Proton build's ARM64 executables. Prefixes the old rule already repaired get their native copy back. This replaces the Visual C++ runtime name list. * fix(game-env): keep the old rule when Proton's programs cannot be read, restore the newest backup across gaps * fix(game-env): number new backups above the highest one, treat truncated import tables as unreadable * fix(game-env): treat an import directory outside every section as unreadable * fix(game-env): treat truncated data directories and section tables as unreadable * perf(game-env): index foreign DLL backups per directory * fix(game-env): count only ARM64 Proton programs as roots, cover x64 component DLLs across launches --------- Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com>
This commit is contained in:
1 parent
7b4b86b0f3
commit
ae9bac225d
5 files changed
+439
-56
No files matched your search
@@ -942,14 +942,11 @@ PE = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wine-pe"))
|
||||
pe_machine = PE["pe_machine"]
|
||||
|
||||
|
||||
def repair_foreign_system_dlls(prefix, proton, env=None):
|
||||
def prefix_system_dlls(prefix, proton, env=None):
|
||||
dist = Path(proton).parent / "files/lib/wine"
|
||||
windows = Path(prefix) / "pfx/drive_c/windows"
|
||||
machines_by_folder = PE["proton_pe_machines"](proton, env or {})
|
||||
if machines_by_folder is None:
|
||||
return []
|
||||
repaired = []
|
||||
for folder, (libdir, machines) in machines_by_folder.items():
|
||||
for folder, (libdir, machines) in (machines_by_folder or {}).items():
|
||||
builtins = dist / libdir
|
||||
try:
|
||||
builtin_names = {name.lower(): name for name in os.listdir(builtins)}
|
||||
@@ -959,35 +956,83 @@ def repair_foreign_system_dlls(prefix, proton, env=None):
|
||||
names = sorted(os.listdir(windows / folder))
|
||||
except OSError:
|
||||
continue
|
||||
# Both restoration and repair may examine many DLLs. Index the backup
|
||||
# names from this directory listing instead of listing it for each DLL.
|
||||
backups = {}
|
||||
for entry in names:
|
||||
stem, marker, suffix = entry.partition(FOREIGN_SUFFIX)
|
||||
if not marker or not stem.lower().endswith(".dll"):
|
||||
continue
|
||||
if not suffix:
|
||||
serial = 0
|
||||
elif suffix.startswith(".") and suffix[1:].isdigit():
|
||||
serial = int(suffix[1:])
|
||||
else:
|
||||
continue
|
||||
backups.setdefault(stem, []).append((serial, windows / folder / entry))
|
||||
for copies in backups.values():
|
||||
copies.sort()
|
||||
for name in names:
|
||||
path = windows / folder / name
|
||||
builtin_name = builtin_names.get(name.lower())
|
||||
source = builtins / builtin_name if builtin_name else builtins / name
|
||||
if path.is_symlink() or not name.lower().endswith(".dll") or not source.is_file() or not path.is_file():
|
||||
if name.lower().endswith(".dll") and source.is_file():
|
||||
yield folder, name, windows / folder / name, source, machines, builtins, backups.get(name, ())
|
||||
|
||||
|
||||
def newest_foreign_backup(backups):
|
||||
return backups[-1][1] if backups else None
|
||||
|
||||
|
||||
def restore_compatible_system_dlls(prefix, proton, env=None):
|
||||
restored = []
|
||||
for folder, name, path, source, machines, builtins, backups in prefix_system_dlls(prefix, proton, env):
|
||||
if not path.is_symlink() or not path.is_file():
|
||||
continue
|
||||
try:
|
||||
if not os.path.samefile(path, source):
|
||||
continue
|
||||
machine = pe_machine(path)
|
||||
if machine is None or PE["machine_compatible"](path, machines, machine):
|
||||
continue
|
||||
link = windows / folder / (name + ".bl-new")
|
||||
backup = windows / folder / (name + FOREIGN_SUFFIX)
|
||||
serial = 1
|
||||
while os.path.lexists(backup):
|
||||
backup = windows / folder / (name + FOREIGN_SUFFIX + ".%d" % serial)
|
||||
serial += 1
|
||||
except OSError:
|
||||
continue
|
||||
backup = newest_foreign_backup(backups)
|
||||
if backup is None or backup.is_symlink() or not backup.is_file():
|
||||
continue
|
||||
machine = pe_machine(backup)
|
||||
if machine is None or not PE["machine_compatible"](backup, machines, machine, builtins, name):
|
||||
continue
|
||||
try:
|
||||
os.replace(backup, path)
|
||||
except OSError:
|
||||
continue
|
||||
restored.append("%s/%s (machine 0x%x)" % (folder, name, machine))
|
||||
return restored
|
||||
|
||||
|
||||
def repair_foreign_system_dlls(prefix, proton, env=None):
|
||||
repaired = []
|
||||
for folder, name, path, source, machines, builtins, backups in prefix_system_dlls(prefix, proton, env):
|
||||
if path.is_symlink() or not path.is_file():
|
||||
continue
|
||||
machine = pe_machine(path)
|
||||
if machine is None or PE["machine_compatible"](path, machines, machine, builtins):
|
||||
continue
|
||||
link = path.parent / (name + ".bl-new")
|
||||
backup = path.parent / (name + FOREIGN_SUFFIX)
|
||||
if backups:
|
||||
backup = path.parent / (name + FOREIGN_SUFFIX + ".%d" % (backups[-1][0] + 1))
|
||||
try:
|
||||
os.symlink(os.path.relpath(source, path.parent), link)
|
||||
os.replace(path, backup)
|
||||
try:
|
||||
os.symlink(os.path.relpath(source, path.parent), link)
|
||||
os.replace(path, backup)
|
||||
try:
|
||||
os.replace(link, path)
|
||||
except OSError:
|
||||
if not os.path.lexists(path) and os.path.lexists(backup):
|
||||
os.replace(backup, path)
|
||||
raise
|
||||
os.replace(link, path)
|
||||
except OSError:
|
||||
if link.is_symlink():
|
||||
link.unlink()
|
||||
continue
|
||||
repaired.append("%s/%s (machine 0x%x)" % (folder, name, machine))
|
||||
if not os.path.lexists(path) and os.path.lexists(backup):
|
||||
os.replace(backup, path)
|
||||
raise
|
||||
except OSError:
|
||||
if link.is_symlink():
|
||||
link.unlink()
|
||||
continue
|
||||
repaired.append("%s/%s (machine 0x%x)" % (folder, name, machine))
|
||||
return repaired
|
||||
|
||||
|
||||
@@ -1317,10 +1362,14 @@ def main():
|
||||
components = win_components(prefix_path, app_id, command[0], effective_env)
|
||||
if prefix_path and command:
|
||||
try:
|
||||
restored = restore_compatible_system_dlls(prefix_path, command[0], effective_env)
|
||||
fixed = repair_foreign_system_dlls(prefix_path, command[0], effective_env)
|
||||
except OSError as error:
|
||||
fixed = []
|
||||
restored = fixed = []
|
||||
print("droiddeck-game-env: prefix system DLL check skipped (%s)" % error, file=sys.stderr)
|
||||
if restored:
|
||||
print("droiddeck-game-env: restored system DLLs this Proton's own programs do not load: %s" % ", ".join(restored),
|
||||
file=sys.stderr)
|
||||
if fixed:
|
||||
print("droiddeck-game-env: replaced foreign-architecture system DLLs with this Proton's own: %s" % ", ".join(fixed),
|
||||
file=sys.stderr)
|
||||
|
||||
@@ -479,13 +479,15 @@ def save_state(compat, state):
|
||||
os.replace(temp, compat / STATE)
|
||||
|
||||
|
||||
def foreign_component(rel, path, machines):
|
||||
def foreign_component(rel, path, machines, dist=None):
|
||||
parts = Path(rel).parts
|
||||
folder = parts[2] if len(parts) > 2 and parts[:2] == ("drive_c", "windows") else None
|
||||
if not machines or folder not in PE_MACHINES or not rel.lower().endswith(".dll"):
|
||||
return False
|
||||
machine = pe_machine(path)
|
||||
return machine is not None and not machine_compatible(path, machines[folder][1], machine)
|
||||
libdir, compatible = machines[folder]
|
||||
builtins = dist / libdir if dist else None
|
||||
return machine is not None and not machine_compatible(path, compatible, machine, builtins)
|
||||
|
||||
|
||||
def pending_for(app_id, pending=PENDING):
|
||||
@@ -546,6 +548,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING
|
||||
skipped = set()
|
||||
placed_stems = set()
|
||||
machines = PE["proton_pe_machines"](proton, env) if proton else None
|
||||
dist = Path(proton).parent / "files/lib/wine" if proton else None
|
||||
saved_overrides = state.get("overrides") if isinstance(state.get("overrides"), dict) else {}
|
||||
saved_requires = state.get("requires") if isinstance(state.get("requires"), dict) else {}
|
||||
recorded = [c for c in state.get("components", []) if isinstance(c, str) and valid_id(c)]
|
||||
@@ -585,7 +588,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING
|
||||
kept_requires[cid] = list(requires)
|
||||
values += [(cid, value) for value in registry]
|
||||
for rel, src in files:
|
||||
foreign = foreign_component(rel, src, machines)
|
||||
foreign = foreign_component(rel, src, machines, dist)
|
||||
if foreign:
|
||||
skipped.add(Path(rel).stem.lower())
|
||||
changed = False
|
||||
@@ -606,7 +609,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING
|
||||
path = existing(pfx / rel)
|
||||
saved = compat / BACKUP / rel
|
||||
if saved.is_file():
|
||||
if foreign_component(rel, saved, machines):
|
||||
if foreign_component(rel, saved, machines, dist):
|
||||
if path is not None and not path.is_symlink():
|
||||
path.unlink()
|
||||
continue
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/python3
|
||||
import re
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -76,18 +76,105 @@ def pe_has_arm64ec_metadata(path):
|
||||
return False
|
||||
|
||||
|
||||
# Only x64 code and members of the same runtime family import these, so an x64 copy loads in an
|
||||
# ARM64EC prefix, and games check for a newer version than Wine's builtins report.
|
||||
VC_RUNTIME = re.compile(r"(msvc[pr]\d.*|vcruntime\d.*|vcomp\d*|vcamp\d.*|vccorlib\d.*|concrt\d.*|mfcm?\d.*)\.dll",
|
||||
re.IGNORECASE)
|
||||
def pe_imports(path):
|
||||
try:
|
||||
with open(path, "rb") as source:
|
||||
image = source.read()
|
||||
except OSError:
|
||||
return None
|
||||
try:
|
||||
if image[:2] != b"MZ":
|
||||
return None
|
||||
pe_offset = int.from_bytes(image[60:64], "little")
|
||||
if image[pe_offset:pe_offset + 4] != b"PE\0\0":
|
||||
return None
|
||||
section_count = int.from_bytes(image[pe_offset + 6:pe_offset + 8], "little")
|
||||
optional_size = int.from_bytes(image[pe_offset + 20:pe_offset + 22], "little")
|
||||
optional = pe_offset + 24
|
||||
magic = int.from_bytes(image[optional:optional + 2], "little")
|
||||
directories = optional + (112 if magic == 0x20B else 96)
|
||||
if directories > len(image):
|
||||
return None
|
||||
directory_count = int.from_bytes(image[directories - 4:directories], "little")
|
||||
sections = []
|
||||
for index in range(min(section_count, 96)):
|
||||
entry = optional + optional_size + index * 40
|
||||
if entry + 40 > len(image):
|
||||
return None
|
||||
sections.append((int.from_bytes(image[entry + 12:entry + 16], "little"),
|
||||
max(int.from_bytes(image[entry + 8:entry + 12], "little"),
|
||||
int.from_bytes(image[entry + 16:entry + 20], "little")),
|
||||
int.from_bytes(image[entry + 20:entry + 24], "little")))
|
||||
|
||||
def offset(rva):
|
||||
for address, size, raw in sections:
|
||||
if address <= rva < address + size:
|
||||
return rva - address + raw
|
||||
return None
|
||||
|
||||
names = set()
|
||||
for directory, step, field in ((1, 20, 12), (13, 32, 4)):
|
||||
if directory >= directory_count:
|
||||
continue
|
||||
if directories + directory * 8 + 8 > len(image):
|
||||
return None
|
||||
rva = int.from_bytes(image[directories + directory * 8:directories + directory * 8 + 4], "little")
|
||||
entry = offset(rva) if rva else None
|
||||
if rva and entry is None:
|
||||
return None
|
||||
while entry is not None:
|
||||
if entry + step > len(image):
|
||||
return None
|
||||
name_rva = int.from_bytes(image[entry + field:entry + field + 4], "little")
|
||||
if not name_rva:
|
||||
break
|
||||
name = offset(name_rva)
|
||||
end = image.find(b"\0", name) if name is not None else -1
|
||||
if end < 0:
|
||||
return None
|
||||
names.add(image[name:end].decode("ascii", "replace").lower())
|
||||
entry += step
|
||||
return names
|
||||
except (IndexError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def machine_compatible(path, machines, machine=None):
|
||||
PROGRAM_IMPORTS = {}
|
||||
|
||||
|
||||
def program_imports(builtins):
|
||||
key = str(builtins)
|
||||
if key not in PROGRAM_IMPORTS:
|
||||
try:
|
||||
files = {name.lower(): Path(builtins) / name for name in os.listdir(builtins)}
|
||||
except OSError:
|
||||
files = None
|
||||
pending = [name for name in files or () if name.endswith(".exe") and pe_machine(files[name]) in (None, 0xAA64)]
|
||||
seen = set()
|
||||
while pending and files is not None:
|
||||
name = pending.pop()
|
||||
if name in seen:
|
||||
continue
|
||||
seen.add(name)
|
||||
if name in files:
|
||||
imports = pe_imports(files[name])
|
||||
if imports is None:
|
||||
files = None
|
||||
else:
|
||||
pending.extend(imports)
|
||||
PROGRAM_IMPORTS[key] = None if files is None else frozenset(name for name in seen if name.endswith(".dll"))
|
||||
return PROGRAM_IMPORTS[key]
|
||||
|
||||
|
||||
def machine_compatible(path, machines, machine=None, builtins=None, name=None):
|
||||
machine = pe_machine(path) if machine is None else machine
|
||||
if machine == 0x8664 and 0xA641 in machines and VC_RUNTIME.fullmatch(Path(path).name):
|
||||
return True
|
||||
if machine == 0x8664 and 0xA641 in machines:
|
||||
if pe_has_arm64ec_metadata(path):
|
||||
return True
|
||||
imported = program_imports(builtins) if builtins is not None else None
|
||||
return imported is not None and (name or Path(path).name).lower() not in imported
|
||||
if machine == 0x8664 and pe_has_arm64ec_metadata(path):
|
||||
return 0xA641 in machines
|
||||
return False
|
||||
return machine in machines
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user