def main(): parser = argparse.ArgumentParser() parser.add_argument('original', type=argparse.FileType('rb')) parser.add_argument('disassembled', type=argparse.FileType('rb')) parser.add_argument('assembly', type=argparse.FileType('rb+'), nargs='?') parser.add_argument('new_assembly', type=argparse.FileType('wb'), nargs='?') args = parser.parse_args() if args.assembly: text = args.assembly.read().decode('ascii') if args.new_assembly: args.assembly = args.new_assembly else: args.assembly.seek(0) args.assembly.truncate() else: text = None oshex = BytesIO(dx11shaderanalyse.get_chunk(args.original, b'SHEX')) dshex = BytesIO(dx11shaderanalyse.get_chunk(args.disassembled, b'SHEX')) while True: dbytes = dshex.read(4) obytes = oshex.read(4) if not obytes and not dbytes: if args.assembly: args.assembly.write(text.encode('ascii')) return dbytes = struct.unpack('<I', dbytes)[0] obytes = struct.unpack('<I', obytes)[0] if obytes != dbytes: dstr = '%f' % struct.unpack('<f', struct.pack('<I', dbytes))[0] bstr = float_to_hex.hex_to_best_float_str(dbytes) ostr = float_to_hex.hex_to_best_float_str(obytes) print('0x%08X:' % args.original.tell()) print('Disassembled: 0x%08x %s %s' % (dbytes, dstr, bstr)) print(' Original: 0x%08x %s' % (obytes, ostr)) print() if text is not None: (before, garbage, text) = text.partition(dstr) args.assembly.write((before + ostr).encode('ascii'))
def main(): parser = argparse.ArgumentParser() parser.add_argument('original', type=argparse.FileType('rb')) parser.add_argument('disassembled', type=argparse.FileType('rb')) parser.add_argument('assembly', type=argparse.FileType('rb+'), nargs='?') parser.add_argument('new_assembly', type=argparse.FileType('wb'), nargs='?') args = parser.parse_args(); if args.assembly: text = args.assembly.read().decode('ascii') if args.new_assembly: args.assembly = args.new_assembly else: args.assembly.seek(0) args.assembly.truncate() else: text = None oshex = BytesIO(dx11shaderanalyse.get_chunk(args.original, b'SHEX')) dshex = BytesIO(dx11shaderanalyse.get_chunk(args.disassembled, b'SHEX')) while True: dbytes = dshex.read(4) obytes = oshex.read(4) if not obytes and not dbytes: if args.assembly: args.assembly.write(text.encode('ascii')) return dbytes = struct.unpack('<I', dbytes)[0] obytes = struct.unpack('<I', obytes)[0] if obytes != dbytes: dstr = '%f' % struct.unpack('<f', struct.pack('<I', dbytes))[0] bstr = float_to_hex.hex_to_best_float_str(dbytes) ostr = float_to_hex.hex_to_best_float_str(obytes) print('0x%08X:' % args.original.tell()) print('Disassembled: 0x%08x %s %s' % (dbytes, dstr, bstr)) print(' Original: 0x%08x %s' % (obytes, ostr)) print() if text is not None: (before, garbage, text) = text.partition(dstr) args.assembly.write((before + ostr).encode('ascii'))
def dump(stream, args): last_non_zero = 0 if args.truncate: out = io.StringIO() else: out = sys.stdout if args.offset: stream.seek(args.offset) for index in itertools.count(): zero = True for offset in range(args.stride // 4): if args.length and args.length <= index * args.stride + offset * 4: return buf = stream.read(4) if len(buf) == 0: return if len(buf) < 4: print('Remaining:', repr(buf), file=out) return if float_to_hex is not None: fval, = struct.unpack('<I', buf) fval = float_to_hex.hex_to_best_float_str(fval) else: fval, = struct.unpack('<f', buf) ival, = struct.unpack('<i', buf) uval, = struct.unpack('<I', buf) if uval: zero = False if args.stride == 4: print('{:08x}: '.format((index * args.stride + offset * 4)), end='', file=out) else: print('%d+%-4s ' % (index, '%d:' % (offset * 4)), end='', file=out) if args.format == 'int': print('{}'.format(ival), file=out) elif args.format == 'float': print('{}'.format(fval), file=out) else: print('0x{:08x} | {: 12d} | {}'.format(uval, ival, fval), file=out) if args.truncate and not zero: print(out.getvalue(), end='') out.seek(0) out.truncate(0) if args.stride != 4: print(file=out)
def dump_cb(stream): for off in itertools.count(): for component in 'xyzw': buf = stream.read(4) if len(buf) < 4: return if float_to_hex is not None: val = struct.unpack('<I', buf)[0] val = float_to_hex.hex_to_best_float_str(val) else: val = struct.unpack('<f', buf)[0] print('cbX[{}].{}: {}'.format(off, component, val))