Files
DroidDeck/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents
T
MaxsTechReview 8ac5652131 fix(components): a native-only override falls back to builtin when the prefix has no native DLL (#2142)
* fix(components): a native-only override falls back to builtin when the prefix has no native DLL

A component that asks for a DLL native-only (for example .NET 4.8's mscoree=n) left the DLL unloadable when its native copy never reached the prefix. On ARM64 prefixes the .NET installer leaves Wine's builtin mscoree in system32, so every program that needs .NET exited at launch with 'mscoree.dll not found'.

At launch, an override of n for a DLL whose system32 or syswow64 copy is missing or is Wine's own (a link into the Proton build, or a builtin or placeholder stub) becomes n,b, and the session log says so. A real native copy is still loaded first, and the saved component state is unchanged, so the override returns to n once the native DLL is there.

* Treat any prefix symlink as builtin when checking native overrides

* Treat an unreadable prefix DLL as missing for native overrides
2026-11-05 18:00:38 -05:00

656 lines
24 KiB
Python
Executable File

#!/usr/bin/python3
import json
import os
import re
import shutil
import sys
import time
from pathlib import Path
STORE = Path("/opt/droiddeck/wincomponents")
SELECTION = Path.home() / ".config/droiddeck/wincomponents.json"
PENDING = Path.home() / ".config/droiddeck/wincomponents-pending.json"
PENDING_WAIT = 10.0
STATE = ".droiddeck-wincomponents.json"
BACKUP = ".droiddeck-wincomponents-backup"
BUILTIN_MARKS = (b"Wine builtin DLL", b"Wine placeholder DLL")
ARCH_DIRS = ("system32", "syswow64")
ID = re.compile(r"[A-Za-z0-9][A-Za-z0-9._-]{0,63}")
HIVES = {"HKLM": "system.reg", "HKCU": "user.reg"}
VALUE_TYPES = {"key", "sz", "expand_sz", "multi_sz", "dword", "binary", "append", "prepend"}
def valid_id(value):
return isinstance(value, str) and ID.fullmatch(value) is not None and ".." not in value
def wanted(app_id, selection=SELECTION):
try:
with open(selection, encoding="utf-8") as source:
config = json.load(source)
except FileNotFoundError:
return []
if config.get("version") != 1:
raise ValueError("unsupported components selection")
ids = config.get("games", {}).get(app_id, [])
if not isinstance(ids, list) or not all(valid_id(i) for i in ids):
raise ValueError("invalid components selection")
return ids
def component(store, cid):
root = Path(store) / cid
try:
with open(root / "component.json", encoding="utf-8") as source:
meta = json.load(source)
except (FileNotFoundError, ValueError):
return None
files = []
for arch in ARCH_DIRS:
folder = root / arch
if folder.is_dir():
for entry in sorted(folder.rglob("*")):
if entry.is_file() and not entry.is_symlink():
rel = entry.relative_to(folder).as_posix().lower()
files.append(("drive_c/windows/%s/%s" % (arch, rel), entry))
drive = root / "drive_c"
if drive.is_dir():
for entry in sorted(drive.rglob("*")):
if entry.is_file() and not entry.is_symlink():
files.append(("drive_c/" + entry.relative_to(drive).as_posix(), entry))
requires = [r for r in meta.get("requires", []) if valid_id(r)]
return [str(o) for o in meta.get("overrides", [])], files, requires, registry_values(root)
def registry_values(root):
try:
with open(Path(root) / "registry.json", encoding="utf-8") as source:
values = json.load(source).get("values", [])
except (FileNotFoundError, ValueError, AttributeError):
return []
good = []
for value in values if isinstance(values, list) else []:
if isinstance(value, dict) and value.get("hive") in HIVES and isinstance(value.get("key"), str) \
and value.get("type") in VALUE_TYPES and value["key"].strip("\\") and "\0" not in value["key"]:
good.append(value)
return good
def expand(store, ids, saved=None):
out, seen = [], set()
saved = saved if isinstance(saved, dict) else {}
def visit(cid, depth):
if cid in seen or depth > 8:
return
seen.add(cid)
found = component(store, cid)
parts = found[2] if found else [d for d in saved.get(cid, []) if valid_id(d)]
for dep in parts:
visit(dep, depth + 1)
out.append(cid)
for cid in ids:
visit(cid, 0)
return out
def spelled(path):
parts = path.parts
current = Path(parts[0])
for part in parts[1:]:
candidate = current / part
if not candidate.exists():
try:
with os.scandir(current) as entries:
for entry in entries:
if entry.name.lower() == part.lower() and entry.is_dir():
candidate = current / entry.name
break
except OSError:
pass
current = candidate
return current
def existing(path):
folder = spelled(path.parent)
path = folder / path.name
try:
with os.scandir(folder) as entries:
for entry in entries:
if entry.name.lower() == path.name.lower():
return folder / entry.name
except OSError:
pass
return None
def same(src, dst):
try:
if dst.is_symlink():
return False
a, b = src.stat(), dst.stat()
except OSError:
return False
return a.st_size == b.st_size and int(a.st_mtime) == int(b.st_mtime)
def builtin(path):
if path.is_symlink():
return True
try:
with open(path, "rb") as source:
head = source.read(512)
except OSError:
return False
return any(mark in head for mark in BUILTIN_MARKS)
def place(src, target, backup=None):
target = spelled(target.parent) / target.name
target.parent.mkdir(parents=True, exist_ok=True)
found = existing(target)
if found is not None and same(src, found):
return False
if found is not None and backup is not None and not backup.exists() and not builtin(found):
backup.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(found, backup)
final = found or target
temp = final.with_name(".%s.droiddeck" % final.name)
shutil.copy2(src, temp)
os.replace(temp, final)
return True
def reg_escape(text, extra=""):
out = []
for ch in text:
if ch == "\\" or ch == '"' or ch in extra:
out.append("\\" + ch)
elif ch == "\n":
out.append("\\n")
elif ch == "\r":
out.append("\\r")
elif ch == "\t":
out.append("\\t")
elif ch == "\0":
out.append("\\0")
elif ch < " " or ch > "~":
out.append("\\x%04x" % ord(ch))
else:
out.append(ch)
return "".join(out)
def reg_unescape(text):
out, i = [], 0
while i < len(text):
ch = text[i]
if ch != "\\" or i + 1 >= len(text):
out.append(ch)
i += 1
continue
nxt = text[i + 1]
if nxt == "x":
match = re.match(r"[0-9a-fA-F]{1,4}", text[i + 2:])
if match:
out.append(chr(int(match.group(0), 16)))
i += 2 + len(match.group(0))
continue
out.append({"n": "\n", "r": "\r", "t": "\t", "0": "\0"}.get(nxt, nxt))
i += 2
return "".join(out)
def reg_key_line(key):
return "[%s]" % "\\\\".join(reg_escape(part, "[]") for part in key.strip("\\").split("\\"))
def reg_value_line(name, kind, data):
head = "@" if name == "" else '"%s"' % reg_escape(name)
if kind == "sz":
return '%s="%s"' % (head, reg_escape(str(data)))
if kind == "expand_sz":
return '%s=str(2):"%s"' % (head, reg_escape(str(data)))
if kind == "multi_sz":
parts = [str(part) for part in data] if isinstance(data, list) else [str(data)]
return '%s=str(7):"%s"' % (head, reg_escape("".join(part + "\0" for part in parts)))
if kind == "dword":
return "%s=dword:%08x" % (head, int(data) & 0xFFFFFFFF)
if kind == "binary":
digits = re.sub(r"[^0-9a-f]", "", str(data).lower())
return "%s=hex:%s" % (head, ",".join(digits[i:i + 2] for i in range(0, len(digits) - 1, 2)))
return None
def reg_string(line):
match = re.match(r'^(?:@|"(?:[^"\\]|\\.)*")=(?:str\(2\):)?"((?:[^"\\]|\\.)*)"\s*$', line)
return reg_unescape(match.group(1)) if match else None
class RegistryFile:
def __init__(self, path):
self.path = Path(path)
with open(self.path, encoding="utf-8", errors="surrogateescape", newline="") as source:
self.lines = source.read().split("\n")
self.changed = False
self.index = {}
self.links = {}
root = ""
for line in self.lines[:4]:
match = re.match(r"^;; All keys relative to (.*)$", line)
if match:
root = reg_unescape(match.group(1)).strip("\\").lower()
current = None
for number, line in enumerate(self.lines):
if line.startswith("["):
key = self.parse_key(line)
current = key
if key is not None:
self.index.setdefault(key.lower(), number)
elif line == "#link" and current is not None:
target = self.link_target(number, root)
if target:
self.links[current.lower()] = target
def link_target(self, number, root):
for at in range(number + 1, min(number + 8, len(self.lines))):
line = self.lines[at]
if line.startswith("["):
return None
if line.startswith('"SymbolicLinkValue"=hex(6):'):
data = line.split(":", 1)[1]
while data.endswith("\\") and at + 1 < len(self.lines):
at += 1
data = data[:-1] + self.lines[at].strip()
try:
raw = bytes(int(part, 16) for part in data.split(",") if part.strip())
target = raw.decode("utf-16-le").strip("\\")
except ValueError:
return None
if root and target.lower().startswith(root + "\\"):
return target[len(root) + 1:]
return None
return None
def resolve(self, key):
key = key.strip("\\")
for _ in range(8):
lower = key.lower()
for link, target in self.links.items():
if lower == link or lower.startswith(link + "\\"):
key = target + key[len(link):]
break
else:
return key
return key
@staticmethod
def parse_key(line):
end, i = None, 1
while i < len(line):
if line[i] == "\\":
i += 2
continue
if line[i] == "]":
end = i
break
i += 1
return None if end is None else reg_unescape(line[1:end])
def section(self, key):
start = self.index.get(self.resolve(key).lower())
if start is None:
return None
end = start + 1
while end < len(self.lines) and not self.lines[end].startswith("["):
end += 1
return start + 1, end
@staticmethod
def value_name(line):
if line.startswith("@="):
return ""
match = re.match(r'^"((?:[^"\\]|\\.)*)"=', line)
return reg_unescape(match.group(1)).lower() if match else None
def find(self, key, name):
span = self.section(key)
if span is None:
return None
wanted = name.lower()
number = span[0]
while number < span[1]:
line = self.lines[number]
last = number
while self.lines[last].endswith("\\") and last + 1 < span[1]:
last += 1
if self.value_name(line) == wanted:
return number, last + 1
number = last + 1
return None
def get(self, key, name):
found = self.find(key, name)
return None if found is None else "\n".join(self.lines[found[0]:found[1]])
def ensure_key(self, key):
key = self.resolve(key)
if key.lower() in self.index:
return
now = time.time()
while self.lines and self.lines[-1] == "":
self.lines.pop()
self.lines += ["", "%s %d" % (reg_key_line(key), int(now)),
"#time=%x" % (int(now * 10000000) + 116444736000000000)]
self.index[key.lower()] = len(self.lines) - 2
self.lines.append("")
self.changed = True
def put(self, key, name, raw):
found = self.find(key, name)
if raw is None:
if found is None:
return False
del self.lines[found[0]:found[1]]
self.shift(found[0], found[0] - found[1])
self.changed = True
return True
new = raw.split("\n")
if found is not None:
if self.lines[found[0]:found[1]] == new:
return False
self.lines[found[0]:found[1]] = new
self.shift(found[0], len(new) - (found[1] - found[0]))
else:
self.ensure_key(key)
start, end = self.section(key)
while end > start and self.lines[end - 1] == "":
end -= 1
self.lines[end:end] = new
self.shift(end, len(new))
self.changed = True
return True
def shift(self, at, by):
if by:
for key, number in self.index.items():
if number >= at:
self.index[key] = number + by
def save(self):
if not self.changed:
return
temp = self.path.with_name("." + self.path.name + ".droiddeck")
with open(temp, "w", encoding="utf-8", errors="surrogateescape", newline="") as out:
out.write("\n".join(self.lines))
shutil.copymode(self.path, temp)
os.replace(temp, self.path)
self.changed = False
def list_line(name, current, item, separator, add, at_end=True):
text = reg_string(current) if current is not None else ""
if text is None:
return current
items = [part for part in text.split(separator) if part and part.lower() != item.lower()]
if add:
items = items + [item] if at_end else [item] + items
if not items:
return None
expand = current is None or "=str(2):" in current
return reg_value_line(name, "expand_sz" if expand else "sz", separator.join(items))
def apply_registry(pfx, values, state, held=()):
files = {}
for hive, name in HIVES.items():
if (pfx / name).is_file():
files[hive] = RegistryFile(pfx / name)
old = state.get("registry") if isinstance(state.get("registry"), dict) else {}
keep, notes, written, waiting = {}, [], 0, 0
for cid, value in values:
reg = files.get(value["hive"])
if reg is None:
waiting += 1
continue
if value["type"] == "key":
reg.ensure_key(value["key"])
continue
name = value.get("name") or ""
ident = "%s\t%s\t%s" % (value["hive"], value["key"].lower(), name.lower())
current = reg.get(value["key"], name)
record = keep.get(ident) or (old.get(ident) if isinstance(old.get(ident), dict) else None) or {"prev": current}
if value["type"] in ("append", "prepend"):
line = list_line(name, current, str(value.get("data", "")), value.get("separator") or ";", True,
value["type"] == "append")
record = dict(record, item=str(value.get("data", "")), separator=value.get("separator") or ";")
else:
line = reg_value_line(name, value["type"], value.get("data"))
if line != current and reg.put(value["key"], name, line):
written += 1
keep[ident] = dict(record, cid=cid, hive=value["hive"], key=value["key"], name=name)
for ident, record in old.items():
if ident in keep or not isinstance(record, dict) or not isinstance(record.get("key"), str):
continue
if record.get("cid") in held:
keep[ident] = record
continue
reg = files.get(record.get("hive"))
if reg is None:
keep[ident] = record
continue
name = record.get("name") or ""
if "item" in record:
line = list_line(name, reg.get(record["key"], name), record["item"], record.get("separator") or ";", False)
else:
line = record.get("prev")
if reg.put(record["key"], name, line):
written += 1
for reg in files.values():
reg.save()
state["registry"] = keep
if waiting:
notes.append("%d registry value(s) wait for the prefix: they go in on the next launch" % waiting)
return written, notes
def load_state(compat):
try:
with open(compat / STATE, encoding="utf-8") as source:
state = json.load(source)
return state if isinstance(state.get("files"), dict) else {"files": {}}
except (FileNotFoundError, ValueError, AttributeError):
return {"files": {}}
def save_state(compat, state):
temp = compat / (STATE + ".tmp")
with open(temp, "w", encoding="utf-8") as out:
json.dump(state, out, indent=1, sort_keys=True)
os.replace(temp, compat / STATE)
def pending_for(app_id, pending=PENDING):
try:
with open(pending, encoding="utf-8") as source:
config = json.load(source)
ids = config.get("games", {}).get(str(app_id), [])
revisions = config.get("revisions", {})
except (OSError, ValueError, AttributeError):
return {}
revisions = revisions if isinstance(revisions, dict) else {}
return {i: max(0, revisions[i]) if type(revisions.get(i)) is int else 0
for i in ids if valid_id(i)} if isinstance(ids, list) else {}
def wait_for_pending(app_id, pending=PENDING, store=STORE, timeout=PENDING_WAIT, sleep=time.sleep, clock=time.monotonic):
def missing():
left = []
for cid, revision in pending_for(app_id, pending).items():
try:
with open(Path(store) / cid / "component.json", encoding="utf-8") as source:
meta = json.load(source)
installed = meta.get("revision", 0)
ready = type(installed) is int and installed >= revision
except (OSError, ValueError, AttributeError):
ready = False
if not ready:
left.append(cid)
return left
left = missing()
if not left:
return None
asked = list(left)
start = clock()
while left and clock() - start < timeout:
sleep(0.5)
left = missing()
waited = int((clock() - start) * 1000)
if left:
return "wincomp: %s launched without %s (not ready)" % (app_id, ",".join(left))
return "wincomp: %s waited %dms for %s" % (app_id, waited, ",".join(asked))
def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING, wait=wait_for_pending):
compat = Path(compat_path)
pfx = compat / "pfx"
waited = wait(app_id, pending, store) if wait else None
ids = wanted(app_id, selection)
state = load_state(compat)
if not ids and not state["files"] and not state.get("registry"):
return [], []
notes, overrides, keep = ([waited] if waited else []), [], {}
copied = 0
installed = []
values = []
held = set()
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)]
owners = set(state["files"].values())
registry_owners = {r.get("cid") for r in (state.get("registry") or {}).values() if isinstance(r, dict)}
inferred = set()
guessed = set(state.get("guessed") or []) if "requires" in state else set(ids)
loose = [c for c in recorded if c not in ids and (c in owners or c in registry_owners)]
for cid in ids:
if cid in guessed and component(store, cid) is None and cid in recorded:
saved_requires = dict(saved_requires, **{cid: loose})
inferred.add(cid)
kept_overrides, kept_requires = {}, {}
for cid in expand(store, ids, saved_requires):
found = component(store, cid)
if found is None:
if cid in saved_requires or cid in recorded or cid in owners or cid in registry_owners:
held.add(cid)
if cid in saved_overrides:
names = [n for n in saved_overrides.get(cid, []) if isinstance(n, str)]
else:
names = sorted({Path(rel).stem.lower() for rel, owner in state["files"].items()
if owner == cid and rel.lower().endswith(".dll")})
overrides += names
kept_overrides[cid] = names
if isinstance(saved_requires.get(cid), list) and cid not in inferred:
kept_requires[cid] = saved_requires[cid]
notes.append("%s is not in the store; kept the files it placed until it is installed again" % cid)
else:
notes.append("%s is not installed; skipped" % cid)
continue
installed.append(cid)
names, files, requires, registry = found
overrides += names
kept_overrides[cid] = list(names)
if requires:
kept_requires[cid] = list(requires)
values += [(cid, value) for value in registry]
for rel, src in files:
if place(src, pfx / rel, None if rel in state["files"] else compat / BACKUP / rel):
copied += 1
keep[rel] = cid
removed = restored = 0
for rel, cid in state["files"].items():
if rel in keep:
continue
if cid in held:
keep[rel] = cid
continue
path = existing(pfx / rel)
saved = compat / BACKUP / rel
if saved.is_file():
target = path or (pfx / rel)
os.replace(saved, target)
restored += 1
elif path is not None and not path.is_symlink():
path.unlink()
removed += 1
if removed:
try:
(compat / "config_info").unlink()
except FileNotFoundError:
pass
compat.mkdir(parents=True, exist_ok=True)
new_state = {"files": keep, "components": installed + sorted(held), "overrides": kept_overrides,
"requires": kept_requires, "registry": state.get("registry", {})}
if inferred:
new_state["guessed"] = sorted(inferred)
written = 0
if values or new_state["registry"]:
written, registry_notes = apply_registry(pfx, values, new_state, held)
notes += registry_notes
save_state(compat, new_state)
overrides, fallback = reachable_overrides(pfx, overrides)
notes += fallback
if copied or removed or restored or written:
note = "components %s: %d file(s) placed, %d removed" % (",".join(ids) or "none", copied, removed)
note += ", %d put back as they were" % restored if restored else ""
notes.append(note + (", %d registry value(s) written" % written if written else ""))
return overrides, notes
def native_dll(pfx, name):
for arch in ARCH_DIRS:
path = existing(pfx / "drive_c/windows" / arch / (name + ".dll"))
if path is None or builtin(path) or not os.access(path, os.R_OK):
return False
return True
def reachable_overrides(pfx, names):
kept, notes = [], []
for entry in names:
name, sep, mode = entry.partition("=")
name = name.strip().lower()
if sep and mode.strip().lower() == "n" and not native_dll(pfx, name):
entry = name + "=n,b"
notes.append("%s=n has no native DLL in the prefix; using n,b" % name)
kept.append(entry)
return kept, notes
def overrides_value(names):
modes = {}
for entry in names:
name, _, mode = entry.partition("=")
name = name.strip().lower()
if name and re.fullmatch(r"[a-z0-9._-]+", name) and re.fullmatch(r"[nb,]*", mode):
modes.pop(name, None)
modes[name] = mode if "=" in entry else "n,b"
return ";".join("%s=%s" % item for item in modes.items())
def main():
if len(sys.argv) != 3:
print("usage: droiddeck-wincomponents <compatdata dir> <appid>", file=sys.stderr)
return 2
names, notes = apply(sys.argv[1], sys.argv[2])
for note in notes:
print(note)
print("WINEDLLOVERRIDES=" + overrides_value(names))
return 0
if __name__ == "__main__":
sys.exit(main())