#!/usr/bin/env python
"""Add or remove extra texture mappings

To merge submeshes, they need the same format. Sometimesm they differ 
only in some extra texture maps which aren't really used, so you can 
use this script to convert one into the type of the other.

Takes four arguments, first is the expected STRIDE of the input, second is 
the expected STRIDE of the output, third is the input, fourth is the output.
"""

import sys
import os.path

TEX2 = bytes.fromhex('0000803F000080BF')
TEX3 = bytes.fromhex('0000000000000000')
TEX4 = bytes.fromhex('0000803F000080BF')

if __name__ == '__main__':
	incoming_stride = int(sys.argv[1])
	outgoing_stride = int(sys.argv[2])
	assert(os.path.getsize(sys.argv[3]) % incoming_stride == 0)
	vs = []
	with open(sys.argv[3], 'rb') as fi:
		while True:
			chunk = fi.read(incoming_stride)
			if not chunk:
				break
			if incoming_stride < outgoing_stride:
				vs.append(chunk[:incoming_stride - 16])
				if incoming_stride == 68:
					vs.append(TEX2)
				if incoming_stride <= 76 and outgoing_stride > 76:
					vs.append(TEX3)
				if outgoing_stride == 92:
					vs.append(TEX4)
				vs.append(chunk[-16:])
			else:
				vs.append(chunk[:outgoing_stride - 16])
				vs.append(chunk[-16:])
	with open(sys.argv[4], 'wb') as fi:
		for chunk in vs:
			fi.write(chunk)



