okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
adfb7ecb2
main
1"""serve/loader_diag.py - name the file a Windows engine could not load. 2 3A Windows program that imports a DLL which is not there ends with exit code 0xC0000135 and prints nothing. The person only sees 4"the engine stopped". This reads the engine's import table (the same reader as packaging/binary_deps.py, copied because the 5packaging folder does not travel with Control) and lists the DLLs that are neither beside the engine, nor in the Windows 6folders, nor part of the GPU driver, so the start error can say which file is missing. 7""" 8 9from __future__import annotations 10 11import os 12import struct 13from pathlib import Path 14 15LOADER_FAILURE_CODES = (0xC0000135 ,)# STATUS_DLL_NOT_FOUND 16 17def pe_imports (path :Path )-> list [str ]: 18"""DLL names a PE file imports at load time (import table) plus delay-load imports.""" 19data = path .read_bytes () 20if data [:2 ]!= b"MZ" : 21raise ValueError ("not a PE file" ) 22pe = struct .unpack_from ("<I" ,data ,0x3C )[0 ] 23if data [pe :pe + 4 ]!= b"PE\0\0" : 24raise ValueError ("bad PE header" ) 25nsec ,= struct .unpack_from ("<H" ,data ,pe + 6 ) 26opt_size ,= struct .unpack_from ("<H" ,data ,pe + 20 ) 27opt = pe + 24 28magic ,= struct .unpack_from ("<H" ,data ,opt ) 29dd = opt + (112 if magic == 0x20B else 96 ) 30sections = [] 31sec = opt + opt_size 32for i in range (nsec ): 33vsize ,va ,rsize ,raw = struct .unpack_from ("<IIII" ,data ,sec + i * 40 + 8 ) 34sections .append ((va ,max (vsize ,rsize ),raw )) 35 36def off (rva :int )-> int : 37for va ,size ,raw in sections : 38if va <= rva < va + size : 39return raw + rva - va 40raise ValueError ("RVA outside every section" ) 41 42def cstr (o :int )-> str : 43return data [o :data .index (b"\0" ,o )].decode ("ascii" ,"replace" ) 44 45names :list [str ]= [] 46imp_rva ,imp_size = struct .unpack_from ("<II" ,data ,dd + 8 ) 47if imp_rva and imp_size : 48o = off (imp_rva ) 49while True : 50_ ,_ ,_ ,name_rva ,_ = struct .unpack_from ("<IIIII" ,data ,o ) 51if not name_rva : 52break 53names .append (cstr (off (name_rva ))) 54o += 20 55delay_rva ,delay_size = struct .unpack_from ("<II" ,data ,dd + 13 * 8 ) 56if delay_rva and delay_size : 57o = off (delay_rva ) 58while True : 59attrs ,name_rva = struct .unpack_from ("<II" ,data ,o ) 60if not name_rva : 61break 62names .append (cstr (off (name_rva ))) 63o += 32 64return names 65 66 67def missing_files (exe ,system_dirs = None )-> list [str ]: 68"""DLL names the engine (and the DLLs beside it) import that cannot be found: not in the engine's folder, not a Windows or 69driver library, and not in a Windows folder on this PC. Sorted, without duplicates; [] when nothing is missing or the file 70cannot be read (this is advice for an error message, it never raises).""" 71try : 72exe = Path (exe ) 73folder = exe .parent 74here = {p .name .lower ()for p in folder .iterdir ()if p .is_file ()} 75if system_dirs is None : 76root = os .environ .get ("SystemRoot" )or r"C:\Windows" 77system_dirs = [Path (root )/ "System32" ,Path (root )] 78files = [exe ]+ [p for p in sorted (folder .glob ("*.dll" ))] 79missing :set [str ]= set () 80for f in files : 81try : 82names = pe_imports (f ) 83except (ValueError ,OSError ,struct .error ): 84continue 85for name in names : 86low = name .lower () 87if low in here or low .startswith (("api-ms-win-" ,"ext-ms-win-" )): 88continue # beside the engine, or a virtual API-set name Windows resolves itself 89if any ((d / name ).exists ()for d in system_dirs ): 90continue 91missing .add (name )# not found anywhere the loader looks (a missing driver DLL is named too) 92return sorted (missing ,key = str .lower ) 93except OSError : 94return [] 95 96 97def missing_note (rc ,exe )-> str : 98"""The sentence for the start error, or "" when the exit status is not a missing-library one or nothing can be named.""" 99if rc is None or (rc & 0xFFFFFFFF )not in LOADER_FAILURE_CODES or not exe : 100return "" 101names = missing_files (exe ) 102if not names : 103return "" 104return (f"\n The engine is missing a file it needs ( { ', ' . join ( names ) } ). Reinstall the engine in Settings > Engine, " 105f"or send us the log." )