77 lines
3.8 KiB
Python
77 lines
3.8 KiB
Python
"""Pin installed traffic-rule semantics and rule overlays without changing ATS."""
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
import zipfile
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:sys.path.insert(0,str(ROOT))
|
|
|
|
from telemetry.native_signals import AUTHORING_SOURCE,RULE_PROVENANCE,RULES_SHA256
|
|
from tools.extract_hashfs_assets import HashFSReader
|
|
from tools.hashfs_cityhash import cityhash64
|
|
from tools.verify_prefab_asset_overlays import (LOG,archive_index_fingerprint,
|
|
atomic_write_json,mounted_archives)
|
|
|
|
|
|
def verify_rule_sources(source, log_text):
|
|
source=Path(source).resolve()
|
|
asset='def/world/traffic_rules.sii'
|
|
mounts=mounted_archives(log_text)
|
|
checks,matches=[],[]
|
|
for mount in mounts:
|
|
path=Path(mount['path']);stat=path.stat()
|
|
if zipfile.is_zipfile(path):
|
|
with zipfile.ZipFile(path) as archive:
|
|
entries=[(i.filename,i.CRC,i.file_size,i.compress_size) for i in archive.infolist()]
|
|
present=any(i[0].replace('\\','/').lstrip('/').casefold()==asset for i in entries)
|
|
fingerprint=dict(kind='zip-central-directory',
|
|
directory_sha256=hashlib.sha256(json.dumps(entries).encode()).hexdigest())
|
|
else:
|
|
fingerprint=archive_index_fingerprint(path)
|
|
reader=HashFSReader(path)
|
|
try:present=cityhash64(str(reader.salt)+asset if reader.salt else asset) in reader.entries
|
|
finally:reader.close()
|
|
after=path.stat()
|
|
if after.st_size!=stat.st_size or after.st_mtime_ns!=stat.st_mtime_ns:
|
|
raise ValueError('Archive changed during traffic-rule verification')
|
|
checks.append(dict(path=str(path),size=stat.st_size,mtime_ns=stat.st_mtime_ns,
|
|
present=present,index_fingerprint=fingerprint))
|
|
if present:matches.append(path.resolve())
|
|
if matches!=[source]:
|
|
raise ValueError('Traffic-rule source absent, ambiguous or overridden: '+str(matches))
|
|
active=re.findall(r'\[mods\] Active local mod (.*?) \(name:',log_text)
|
|
headers=re.findall(r'\[mods\] Active (\d+) mods \(local: (\d+), workshop: (\d+)\)',log_text)
|
|
if (not headers or int(headers[-1][0])!=len(active) or int(headers[-1][2])!=0
|
|
or any((LOG.parent/'mod'/name).is_dir() for name in active)):
|
|
raise ValueError('Active traffic-rule mod population unverified')
|
|
versions=re.findall(r'Loaded pack set version ([\d.]+)',log_text)
|
|
if not versions or versions[-1]!='1.61.2.0':
|
|
raise ValueError('Unsupported installed signal-rule game version')
|
|
reader=HashFSReader(source)
|
|
try:payload=reader.read_asset(asset)[0]
|
|
finally:reader.close()
|
|
digest=hashlib.sha256(payload).hexdigest()
|
|
if digest!=RULES_SHA256:
|
|
raise ValueError('Installed traffic rules differ from verified semantic mapping')
|
|
return dict(schema_version=1,traffic_rules_sha256=digest,asset_path='/'+asset,
|
|
archive=str(source),asset_overlay_verified=True,checked_archives=checks,
|
|
loaded_archive_order=[m['path'] for m in mounts],active_mod_order=active,
|
|
game_version=versions[-1],authoring_source=AUTHORING_SOURCE,
|
|
curve_rule_position='start-locator',stop_point_not_full_stop_line=True)
|
|
|
|
|
|
def main():
|
|
cache=json.loads((ROOT/'diagnostics/map-cache/local-road-context.json').read_text())
|
|
source=Path(cache['game_executable']['path']).parents[2]/'def.scs'
|
|
proof=verify_rule_sources(source,LOG.read_text(encoding='utf-8',errors='replace'))
|
|
RULE_PROVENANCE.parent.mkdir(parents=True,exist_ok=True)
|
|
atomic_write_json(RULE_PROVENANCE,proof)
|
|
print(json.dumps(dict(path=str(RULE_PROVENANCE),verified=True,
|
|
checked_archives=len(proof['checked_archives']))))
|
|
|
|
|
|
if __name__=='__main__':main()
|