57 lines
2.3 KiB
Python
57 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Validate every original OMT index, mesh, material, canvas and RIFF boundary."""
|
|
import argparse
|
|
from collections import Counter
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
from omt import Database, FormatError, read_canvas, read_material, read_shape
|
|
|
|
|
|
def audit(original: Path) -> dict:
|
|
counts, conversions, failures = Counter(), Counter(), []
|
|
archives = sorted(original.rglob('*.omt'))
|
|
for path in archives:
|
|
try:
|
|
db = Database(path)
|
|
except (OSError, FormatError) as ex:
|
|
failures.append({'source': str(path), 'error': str(ex)})
|
|
continue
|
|
for entry in db.entries:
|
|
counts[entry.type] += 1
|
|
function = {'3DSh': read_shape, '3DMa': read_material, 'Canv': read_canvas}.get(entry.type)
|
|
try:
|
|
payload = db.payload(entry)
|
|
if function:
|
|
function(payload)
|
|
conversions[entry.type] += 1
|
|
elif entry.type == 'Wave':
|
|
start = payload.find(b'RIFF')
|
|
if start < 0 or payload[start+8:start+12] != b'WAVE':
|
|
raise FormatError('No RIFF WAVE header')
|
|
size = struct.unpack_from('<I', payload, start+4)[0]+8
|
|
if size < 12 or start+size > len(payload): raise FormatError('RIFF size exceeds resource')
|
|
conversions['Wave'] += 1
|
|
except (FormatError, struct.error) as ex:
|
|
failures.append({'source': str(path), 'type': entry.type,
|
|
'id': entry.id, 'name': entry.name, 'error': str(ex)})
|
|
return {'archives': len(archives), 'counts': dict(counts),
|
|
'decoded': dict(conversions), 'failures': failures}
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('original', type=Path)
|
|
parser.add_argument('--report', type=Path)
|
|
args = parser.parse_args()
|
|
result = audit(args.original)
|
|
if args.report:
|
|
args.report.parent.mkdir(parents=True, exist_ok=True)
|
|
args.report.write_text(json.dumps(result, indent=2)+'\n', encoding='utf-8')
|
|
print(json.dumps(result, indent=2))
|
|
raise SystemExit(bool(result['failures']))
|
|
|
|
|
|
if __name__ == '__main__': main()
|