Skip to content

Instantly share code, notes, and snippets.

@Tustin
Last active June 21, 2018 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tustin/f3cac1c520a388f0c50ef3a0140a44c3 to your computer and use it in GitHub Desktop.
Save Tustin/f3cac1c520a388f0c50ef3a0140a44c3 to your computer and use it in GitHub Desktop.
import sys, os, struct, glob
from collections import OrderedDict
class TrophyData(object):
def __init__(self, name, data, buffer_size, actual_file_size):
self.name = name
self.data = data
self.buffer_size = buffer_size
self.actual_file_size = actual_file_size
def pad_data(input, block_size = 16):
mod = len(input) % block_size
if not mod == 0:
input = input.ljust(len(input) + (block_size - mod), '\x00') # hack... might need adjustments
return input
if len(sys.argv) < 2:
sys.exit('missing trophy folder')
if not os.path.exists(sys.argv[1]):
sys.exit('trophy folder given could not be found')
# create trp and write header
trp_file = open('TROPHY.TRP', 'wb+')
trp_file.write(bytearray([0xDC, 0xA2, 0x4D, 0x00])) # magic
trp_file.write(struct.pack('>i', 0x01)) # version
trophy_files = list()
total_files_size = 0
os.chdir(sys.argv[1])
for file in glob.glob("*.SFM"):
full_path = os.path.join(sys.argv[1], file)
file_handle = open(file, "rb")
file_data = pad_data(file_handle.read())
total_files_size += len(file_data)
if file == 'TROPCONF.SFM':
trophy_files.insert(0, TrophyData(file, file_data, len(file_data), os.path.getsize(file)))
else:
trophy_files.append(TrophyData(file, file_data, len(file_data), os.path.getsize(file)))
for file in glob.glob("*.PNG"):
file_handle = open(file, "rb")
file_data = pad_data(file_handle.read())
total_files_size += len(file_data)
trophy_files.append(TrophyData(file, file_data, len(file_data), os.path.getsize(file)))
# need to calculate the size of the trophy.trp header to add it to the total size of the files
file_header_size = len(trophy_files) * 0x40 + 0x40
total_files_size += file_header_size
trp_file.write(struct.pack('>q', total_files_size)) # file_size
trp_file.write(struct.pack('>i', len(trophy_files))) # files_count
trp_file.write(struct.pack('>i', 0x40)) # element_size??
trp_file.write(struct.pack('>i', 0x00)) # dev_flag
trp_file.write(bytearray([0x00] * 36)) # padding
# loop over all the trophy files again
it = 0
last_file = trophy_files[0]
current_file_position = 0
for file in trophy_files:
trp_file.write(file.name.ljust(32, '\x00')) # name, padded to 32 chars
trophy_data_position = len(trophy_files) * 0x40 + 0x40
if it > 0:
trophy_data_position = len(last_file.data)
current_file_position += trophy_data_position
trp_file.write(struct.pack('>q', current_file_position)) # position
trp_file.write(struct.pack('>q', file.actual_file_size)) # file_size
if (file.name.endswith('.SFM')):
trp_file.write(struct.pack('>i', 0x01)) # is_sfm_file??
trp_file.write(bytearray([0x00] * 12)) # padding
else:
trp_file.write(bytearray([0x00] * 16)) # padding
last_file = trophy_files[it]
it += 1
for file in trophy_files:
trp_file.write(file.data)
trp_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment