367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
137 lines
7.1 KiB
Python
137 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Prepare a private client-folder startup dependency; never install or launch.
|
|
|
|
Only the original embedded manifest payload is rewritten. Original executable
|
|
code, imports, entry point, resource table, SDK exports and file size remain
|
|
byte-identical. The private assembly lets basename core loads use the same
|
|
D3D12 child directory as the original Windows Agility path.
|
|
"""
|
|
import argparse
|
|
import codecs
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import xml.etree.ElementTree as ET
|
|
|
|
ORIGINAL_SHA = 'c49ce22c3e0aaeb7a7007a5c5cb792e4c3e6cdecc55a1145a5933fc113e3e57f'
|
|
NS = 'urn:schemas-microsoft-com:asm.v1'
|
|
ASSEMBLY = {'type': 'win32', 'name': 'D3D12', 'version': '1.0.0.0',
|
|
'processorArchitecture': 'amd64'}
|
|
ASSEMBLY_FILES = frozenset({
|
|
'D3D12Core.dll', 'GWGraphicsProviderWindows.dll', 'GWGraphicsProviderWine.dll',
|
|
'GWNativeBridge.dll', 'GWNativeWire.dll', 'GWNativeBackend.dll', 'forever_codec.dll',
|
|
'libcrypto-3-x64.dll', 'libgcc_s_seh-1.dll', 'libstdc++-6.dll',
|
|
'libwinpthread-1.dll', 'zlib1.dll'})
|
|
|
|
|
|
def validate_deployment(executable, expected_exe_sha, expected_core_sha):
|
|
"""Require the inseparable EXE/private-assembly set before any launch."""
|
|
executable = Path(executable).absolute()
|
|
companion = executable.parent / 'D3D12'
|
|
manifest, core = companion / 'D3D12.manifest', companion / 'D3D12Core.dll'
|
|
for path in (executable, companion, manifest, core):
|
|
if path.is_symlink() or not path.exists():
|
|
raise ValueError('Missing or linked private startup companion: ' + str(path))
|
|
if not companion.is_dir() or not all(path.is_file() for path in (executable, manifest, core)):
|
|
raise ValueError('Invalid private startup deployment types')
|
|
for path, digest in ((executable, expected_exe_sha), (core, expected_core_sha)):
|
|
if hashlib.sha256(path.read_bytes()).hexdigest() != digest:
|
|
raise ValueError('Private startup deployment hash differs: ' + str(path))
|
|
root = ET.fromstring(manifest.read_bytes())
|
|
identity = root.find(f'{{{NS}}}assemblyIdentity')
|
|
files = root.findall(f'{{{NS}}}file')
|
|
names = [node.get('name') for node in files]
|
|
if (root.tag != f'{{{NS}}}assembly' or identity is None or identity.attrib != ASSEMBLY
|
|
or 'D3D12Core.dll' not in names or len(names) != len(set(names))
|
|
or any(name not in ASSEMBLY_FILES for name in names)
|
|
or any(set(node.attrib) != {'name'} for node in files)):
|
|
raise ValueError('Private assembly identity/file differs from executable dependency')
|
|
for name in names:
|
|
path = companion / name
|
|
if path.is_symlink() or not path.is_file():
|
|
raise ValueError('Missing or linked declared private assembly file: ' + str(path))
|
|
return {'executable_sha256': expected_exe_sha, 'core_sha256': expected_core_sha,
|
|
'assembly_manifest_sha256': hashlib.sha256(manifest.read_bytes()).hexdigest(),
|
|
'private_assembly_complete': True}
|
|
|
|
|
|
def startup_manifest(original):
|
|
root = ET.fromstring(original)
|
|
if root.tag != f'{{{NS}}}assembly' or root.get('manifestVersion') != '1.0':
|
|
raise ValueError('Expected original application manifest')
|
|
for node in root.iter(f'{{{NS}}}assemblyIdentity'):
|
|
if node.get('name', '').lower() == 'd3d12':
|
|
raise ValueError('Private startup dependency already exists')
|
|
for prefix, uri in [('', NS), ('v', 'urn:schemas-microsoft-com:asm.v3'),
|
|
('w', 'http://schemas.microsoft.com/SMI/2005/WindowsSettings'),
|
|
('c', 'urn:schemas-microsoft-com:compatibility.v1')]:
|
|
ET.register_namespace(prefix, uri)
|
|
dependency = ET.SubElement(root, f'{{{NS}}}dependency')
|
|
assembly = ET.SubElement(dependency, f'{{{NS}}}dependentAssembly')
|
|
ET.SubElement(assembly, f'{{{NS}}}assemblyIdentity', ASSEMBLY)
|
|
result = ET.tostring(root, encoding='utf-8', short_empty_elements=True)
|
|
# Keep the original UTF-8 declaration/BOM while compacting element names.
|
|
# There is room in this resource; only namespace prefixes need change.
|
|
preamble = codecs.BOM_UTF8 if original.startswith(codecs.BOM_UTF8) else b''
|
|
remaining = original[len(preamble):]
|
|
if remaining.startswith(b'<?xml '):
|
|
end = remaining.find(b'?>')
|
|
if end < 0:
|
|
raise ValueError('Incomplete original XML declaration')
|
|
preamble += remaining[:end + 2]
|
|
result = preamble + result
|
|
if len(result) > len(original):
|
|
raise ValueError('Dependency exceeds existing manifest allocation')
|
|
return result + b' ' * (len(original) - len(result))
|
|
|
|
|
|
def prepare(source, output):
|
|
import pefile
|
|
source, output = Path(source).resolve(strict=True), Path(output).absolute()
|
|
if output.exists() or not output.parent.is_dir() or source == output:
|
|
raise ValueError('Fresh output beside an existing owned directory required')
|
|
original = source.read_bytes()
|
|
if hashlib.sha256(original).hexdigest() != ORIGINAL_SHA:
|
|
raise ValueError('Require exact original Forever70334 executable')
|
|
pe = pefile.PE(data=original, fast_load=True)
|
|
pe.parse_data_directories(directories=[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_RESOURCE']])
|
|
entries = [language.data.struct
|
|
for entry in pe.DIRECTORY_ENTRY_RESOURCE.entries if entry.id == 24
|
|
for name in entry.directory.entries if name.id == 1
|
|
for language in name.directory.entries]
|
|
if len(entries) != 1:
|
|
raise ValueError('Require one original application manifest resource')
|
|
entry = entries[0]
|
|
offset, size = pe.get_offset_from_rva(entry.OffsetToData), entry.Size
|
|
payload = startup_manifest(original[offset:offset + size])
|
|
candidate = original[:offset] + payload + original[offset + size:]
|
|
assert len(candidate) == len(original)
|
|
assert candidate[:offset] == original[:offset] and candidate[offset + size:] == original[offset + size:]
|
|
with output.open('xb') as stream:
|
|
stream.write(candidate)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
receipt = {'kind': 'private-client-startup-manifest', 'source': str(source),
|
|
'source_sha256': ORIGINAL_SHA, 'output': str(output),
|
|
'sha256': hashlib.sha256(candidate).hexdigest(), 'bytes': len(candidate),
|
|
'manifest_offset': offset, 'manifest_bytes': size,
|
|
'only_manifest_payload_changed': True, 'code_imports_entrypoint_headers_unchanged': True,
|
|
'private_assembly': ASSEMBLY, 'game_launch': False,
|
|
'required_companions': ['D3D12/D3D12.manifest', 'D3D12/D3D12Core.dll'],
|
|
'runtime_acceptance': 'pending'}
|
|
with output.with_suffix(output.suffix + '.receipt.json').open('x') as stream:
|
|
json.dump(receipt, stream, indent=2)
|
|
stream.write('\n')
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
return receipt
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--source', required=True, type=Path)
|
|
parser.add_argument('--output', required=True, type=Path)
|
|
args = parser.parse_args()
|
|
print(json.dumps(prepare(args.source, args.output), indent=2))
|