Guest User

PRTools.py

a guest
Jun 17th, 2015
354
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import hashlib
  2. import os
  3. import binascii
  4. import struct
  5. import glob
  6. from shutil import rmtree
  7.  
  8. # os.getcwd()
  9. # os.chdir()
  10.  
  11. inputName = 'savedata.bin'
  12.  
  13. def handler(inputName):
  14.     fileName = ''
  15.     fileExtension = ''
  16.  
  17.     if inputName == '':
  18.         inputName = 'savedata.bin'
  19.  
  20.     fileName = inputName.partition('.')[0]
  21.     fileExtension = inputName.partition('.')[2]
  22.    
  23.     print('Please choose operation')
  24.     print('1: Decompile')
  25.     print('2: Compile')
  26.     print('Else: Quit')
  27.     pickOp = int(raw_input('--:'))
  28.     # pickOp = 2
  29.     if pickOp == 1:
  30.         print('Decompiling')
  31.         saveDecompile(inputName, fileName, fileExtension)
  32.         return
  33.     elif pickOp == 2:
  34.         print('Compiling')
  35.         saveCompile(fileName)
  36.         return
  37.     else:
  38.         return
  39.     return
  40.  
  41.  
  42.  
  43. def saveDecompile(inputName, fileName, fileExtension):
  44.    
  45.     with open(inputName, 'rb+') as saveFile:
  46.         saveData = dict()
  47.        
  48.         saveFile.seek(-1, 2)
  49.         print('File Length ' + str(saveFile.tell() + 1))
  50.         saveData['File Length'] = (saveFile.tell() + 1)
  51.         saveFile.seek(0)
  52.        
  53.         saveData['SHA1'] = binascii.hexlify(saveFile.read(int('14', 16)))
  54.        
  55.         saveData['UNK1'] = binascii.hexlify(saveFile.read(int('C', 16)))
  56.         saveData['UNK2'] = binascii.hexlify(saveFile.read(int('4', 16)))
  57.        
  58.         print('Mii Data Offset location, should be 36', saveFile.tell()) # Should be 36, or 0x24
  59.         saveData['Mii Data Offset'] = binascii.hexlify(saveFile.read(int('4', 16)))
  60.         saveData['UNK3'] = binascii.hexlify(saveFile.read(int('4', 16)))
  61.        
  62.         print('Mii-Trainer Data Offset location, should be 44', saveFile.tell()) # Should be 44, or 0x2C
  63.         saveData['Mii-Trainer Data Offset'] = binascii.hexlify(saveFile.read(int('4', 16)))
  64.         saveData['UNK4'] = binascii.hexlify(saveFile.read(int('4', 16)))
  65.        
  66.         print('Pokemon Data Offset location, should be 52', saveFile.tell()) # Should be 52, or 0x34
  67.         saveData['Pokemon Data Offset'] = binascii.hexlify(saveFile.read(int('4', 16)))
  68.         saveData['UNK5'] = binascii.hexlify(saveFile.read(int('4', 16)))
  69.        
  70.         print('Pokemon Data End Offset location, should be 60', saveFile.tell()) # Should be 60, or 0x3C
  71.         saveData['Pokemon Data End Offset'] = binascii.hexlify(saveFile.read(int('4', 16)))
  72.        
  73.         saveData['UNK6'] = binascii.hexlify(saveFile.read(int('53', 16) - saveFile.tell()))
  74.        
  75.         saveData['Language'] = binascii.hexlify(saveFile.read(int('1', 16)))
  76.        
  77.         saveData['UNK7'] = binascii.hexlify(saveFile.read(int(saveData['Mii Data Offset'], 16) - saveFile.tell()))
  78.         saveData['UNK8'] = binascii.hexlify(saveFile.read(int('4', 16))) # Possibly size of each Mii data entry
  79.        
  80.         print('Mii Data Offset location, should be 8876', saveFile.tell()) # Should be 8876, or 0x22AC
  81.         saveData['Mii Count'] = binascii.hexlify(saveFile.read(int('4', 16)))
  82.        
  83.         print('Mii Data location, should be 8880', saveFile.tell()) # Should be 8880, or 0x22B0
  84.         saveData['Mii Data'] = binascii.hexlify(saveFile.read(int(saveData['Mii Count'], 16) * 40))
  85.  
  86.         print('Mii-Trainer Data location, should be ' + str(int(saveData['Mii-Trainer Data Offset'], 16)), saveFile.tell())
  87.         saveData['UNK9'] = binascii.hexlify(saveFile.read(int('4', 16))) # Possibly size of each Mii-Trainer data entry
  88.         print('unk9 is ' + str(int(saveData['UNK9'], 16)))
  89.        
  90.         saveData['Mii-Trainer Count'] = binascii.hexlify(saveFile.read(int('4', 16))) # This may be off by one, which would explain UNK12. Doesn't matter, I'm not touching it
  91.         print('Mii-Trainer Count', int(saveData['Mii-Trainer Count'], 16))
  92.        
  93.         saveData['Mii-Trainer Data'] = binascii.hexlify(saveFile.read(int(saveData['Mii Count'], 16) * 44))
  94.         saveData['UNK12'] = binascii.hexlify(saveFile.read(int('2C', 16))) # Could be Hayley's data?
  95.        
  96.         print('Pokemon Data location, should be ' + str(int(saveData['Pokemon Data Offset'], 16)), saveFile.tell())
  97.         saveData['UNK10'] = binascii.hexlify(saveFile.read(int('4', 16))) # Possibly size of each Pokemon data entry
  98.        
  99.         saveData['Pokemon Count'] = binascii.hexlify(saveFile.read(int('4', 16)))
  100.         print('Pokemon Count', int(saveData['Pokemon Count'], 16))
  101.        
  102.         # saveData['Pokemon Data'] = binascii.hexlify(saveFile.read(int(saveData['Pokemon Count'], 16) * 164))
  103.        
  104.         pokemonData1 = list()
  105.         pokemonData2 = list()
  106.         pokemonData3 = list()
  107.         pokemonData4 = list()
  108.         i = 0
  109.         j = range(int(saveData['Pokemon Count'], 16))
  110.         for i in j:
  111.             print('Pokemon ' + str(i+1) + 'loading')
  112.             pokemonData1.append(binascii.hexlify(saveFile.read(int('88', 16))))
  113.             pokemonData2.append(binascii.hexlify(saveFile.read(int('4', 16))))
  114.             pokemonData3.append(binascii.hexlify(saveFile.read(int('14', 16))))
  115.             pokemonData4.append(binascii.hexlify(saveFile.read(int('4', 16))))
  116.             print('Pokemon ' + str(i+1) + ' loading done')
  117.        
  118.         saveData['UNK11'] = binascii.hexlify(saveFile.read(int('4', 16)))
  119.        
  120.     print('Making extraction directories')
  121.     if os.path.exists(fileName+'-EXT'):
  122.         # os.removedir(fileName+'-EXT')
  123.         rmtree(fileName+'-EXT', 1)
  124.         print('Deleted old extraction directory')
  125.     os.mkdir(fileName+'-EXT')
  126.     os.mkdir(fileName+'-EXT' + '/' + 'Unhandled Data')
  127.     os.mkdir(fileName+'-EXT' + '/' + 'Pokemon')
  128.     os.mkdir(fileName+'-EXT' + '/' + 'Trainers')
  129.     print('Directories done')
  130.    
  131.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'File Length' + '.txt', 'w') as tempData:
  132.         tempData.write(str(saveData['File Length']))
  133.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'SHA1' + '.hex', 'wb') as tempData:
  134.         tempData.write(binascii.unhexlify(saveData['SHA1'])) # Don't know why I'm even bothering
  135.    
  136.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK1' + '.hex', 'wb') as tempData:
  137.         tempData.write(binascii.unhexlify(saveData['UNK1']))
  138.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK2' + '.hex', 'wb') as tempData:
  139.         tempData.write(binascii.unhexlify(saveData['UNK2']))
  140.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK3' + '.hex', 'wb') as tempData:
  141.         tempData.write(binascii.unhexlify(saveData['UNK3']))
  142.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK4' + '.hex', 'wb') as tempData:
  143.         tempData.write(binascii.unhexlify(saveData['UNK4']))
  144.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK5' + '.hex', 'wb') as tempData:
  145.         tempData.write(binascii.unhexlify(saveData['UNK5']))
  146.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK6' + '.hex', 'wb') as tempData:
  147.         tempData.write(binascii.unhexlify(saveData['UNK6']))
  148.    
  149.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'LANG' + '.hex', 'wb') as tempData:
  150.         tempData.write(binascii.unhexlify(saveData['Language']))
  151.    
  152.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK7' + '.hex', 'wb') as tempData:
  153.         tempData.write(binascii.unhexlify(saveData['UNK7']))
  154.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK8' + '.hex', 'wb') as tempData:
  155.         tempData.write(binascii.unhexlify(saveData['UNK8']))
  156.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii Count' + '.hex', 'wb') as tempData:
  157.         tempData.write(binascii.unhexlify(saveData['Mii Count']))
  158.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii Data' + '.hex', 'wb') as tempData:
  159.         tempData.write(binascii.unhexlify(saveData['Mii Data']))
  160.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK9' + '.hex', 'wb') as tempData:
  161.         tempData.write(binascii.unhexlify(saveData['UNK9']))
  162.    
  163.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii-Trainer Count' + '.hex', 'wb') as tempData:
  164.         tempData.write(binascii.unhexlify(saveData['Mii-Trainer Count']))
  165.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii-Trainer Data' + '.hex', 'wb') as tempData:
  166.         tempData.write(binascii.unhexlify(saveData['Mii-Trainer Data']))
  167.    
  168.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK12' + '.hex', 'wb') as tempData:
  169.         tempData.write(binascii.unhexlify(saveData['UNK12']))
  170.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK10' + '.hex', 'wb') as tempData:
  171.         tempData.write(binascii.unhexlify(saveData['UNK10']))
  172.  
  173.     i = 0
  174.     j = range(int(saveData['Pokemon Count'], 16))
  175.     for i in j:
  176.         print('Pokemon ' + str(i+1) + ' saving')
  177.         with open(fileName + '-EXT' + '/' + 'Pokemon' + '/' + 'Ranch-Pokemon' + '{:04d}'.format(i+1) + '.pkm', 'wb') as tempData:
  178.             tempData.write(binascii.unhexlify(pokemonData1[i]))
  179.         with open(fileName + '-EXT' + '/' + 'Trainers' + '/' + 'Ranch-Pokemon' + '{:04d}'.format(i+1) + '.rid', 'wb') as tempData:
  180.             tempData.write(binascii.unhexlify(pokemonData2[i]))
  181.         with open(fileName + '-EXT' + '/' + 'Trainers' + '/' + 'Ranch-Pokemon' + '{:04d}'.format(i+1) + '.tnr', 'wb') as tempData:
  182.             tempData.write(binascii.unhexlify(pokemonData3[i]))
  183.         with open(fileName + '-EXT' + '/' + 'Trainers' + '/' + 'Ranch-Pokemon' + '{:04d}'.format(i+1) + '.pad', 'wb') as tempData:
  184.             tempData.write(binascii.unhexlify(pokemonData4[i]))
  185.         print('Pokemon ' + str(i+1) + ' saving done')
  186.    
  187.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK11' + '.hex', 'wb') as tempData:
  188.         tempData.write(binascii.unhexlify(saveData['UNK11']))
  189.     print('Done')
  190.  
  191. def saveCompile(fileName):
  192.    
  193.     saveData = dict()
  194.    
  195.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'File Length' + '.txt', 'r') as fileData:
  196.         saveData['File Length'] = int(fileData.read())
  197.     # with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'SHA1' + '.hex', 'rb') as fileData:
  198.         # saveData['SHA1'] = binascii.hexlify(fileData.read())
  199.    
  200.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK1' + '.hex', 'rb') as fileData:
  201.         saveData['UNK1'] = binascii.hexlify(fileData.read())
  202.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK2' + '.hex', 'rb') as fileData:
  203.         saveData['UNK2'] = binascii.hexlify(fileData.read())
  204.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK3' + '.hex', 'rb') as fileData:
  205.         saveData['UNK3'] = binascii.hexlify(fileData.read())
  206.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK4' + '.hex', 'rb') as fileData:
  207.         saveData['UNK4'] = binascii.hexlify(fileData.read())
  208.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK5' + '.hex', 'rb') as fileData:
  209.         saveData['UNK5'] = binascii.hexlify(fileData.read())
  210.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK6' + '.hex', 'rb') as fileData:
  211.         saveData['UNK6'] = binascii.hexlify(fileData.read())
  212.    
  213.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'LANG' + '.hex', 'rb') as fileData:
  214.         saveData['Language'] = binascii.hexlify(fileData.read())
  215.    
  216.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK7' + '.hex', 'rb') as fileData:
  217.         saveData['UNK7'] = binascii.hexlify(fileData.read())
  218.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK8' + '.hex', 'rb') as fileData:
  219.         saveData['UNK8'] = binascii.hexlify(fileData.read())
  220.    
  221.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii Count' + '.hex', 'rb') as fileData:
  222.         saveData['Mii Count'] = binascii.hexlify(fileData.read())
  223.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii Data' + '.hex', 'rb') as fileData:
  224.         saveData['Mii Data'] = binascii.hexlify(fileData.read())
  225.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK9' + '.hex', 'rb') as fileData:
  226.         saveData['UNK9'] = binascii.hexlify(fileData.read())
  227.    
  228.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii-Trainer Count' + '.hex', 'rb') as fileData:
  229.         saveData['Mii-Trainer Count'] = binascii.hexlify(fileData.read())
  230.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'Mii-Trainer Data' + '.hex', 'rb') as fileData:
  231.         saveData['Mii-Trainer Data'] = binascii.hexlify(fileData.read())
  232.    
  233.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK12' + '.hex', 'rb') as fileData:
  234.         saveData['UNK12'] = binascii.hexlify(fileData.read())
  235.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK10' + '.hex', 'rb') as fileData:
  236.         saveData['UNK10'] = binascii.hexlify(fileData.read())
  237.    
  238.     pokemonFileList = glob.glob(fileName + '-EXT' + '/' + 'Pokemon' + '/*.pkm')
  239.     print(pokemonFileList)
  240.    
  241.     pokemonData1 = list()
  242.     pokemonData2 = list()
  243.     pokemonData3 = list()
  244.     pokemonData4 = list()
  245.     pokemonNewData = list()
  246.    
  247.     print('Pokemon file list length', len(pokemonFileList))
  248.     saveData['Pokemon Count'] = '{:08X}'.format(len(pokemonFileList))
  249.    
  250.     i = 0
  251.     j = range(int(saveData['Pokemon Count'], 16))
  252.     for i in j:
  253.         with open(pokemonFileList[i], 'rb') as fileData:
  254.             print('Pokemon file name', fileData.name)
  255.             if fileData.name.count('Ranch-Pokemon'):
  256.                 pokemonData1.append(binascii.hexlify(fileData.read()))
  257.                
  258.                 fileData2 = open(fileData.name.partition('.')[0].replace('Pokemon', 'Trainers', 1) + '.rid', 'rb')
  259.                 fileData3 = open(fileData.name.partition('.')[0].replace('Pokemon', 'Trainers', 1) + '.tnr', 'rb')
  260.                 fileData4 = open(fileData.name.partition('.')[0].replace('Pokemon', 'Trainers', 1) + '.pad', 'rb')
  261.                
  262.                 tempData = binascii.hexlify(fileData2.read())
  263.                 if len(tempData) != 0:
  264.                     pokemonData2.append(tempData)
  265.                     pokemonDataPrev2 = tempData
  266.                 else:
  267.                     pokemonData2.append(pokemonDataPrev2)
  268.                    
  269.                 tempData = binascii.hexlify(fileData3.read())
  270.                 if len(tempData) != 0:
  271.                     pokemonData3.append(tempData)
  272.                     pokemonDataPrev3 = tempData
  273.                 else:
  274.                     pokemonData3.append(pokemonDataPrev3)
  275.                
  276.                 tempData = binascii.hexlify(fileData4.read())
  277.                 if len(tempData) != 0:
  278.                     pokemonData4.append(tempData)
  279.                     pokemonDataPrev4 = tempData
  280.                 else:
  281.                     pokemonData4.append(pokemonDataPrev4)
  282.                
  283.                 fileData2.close()
  284.                 fileData3.close()
  285.                 fileData4.close()
  286.                 print('Pokemon Data 1', pokemonData1[-1])
  287.                 print('Pokemon Data 2', pokemonData2[-1])
  288.                 print('Pokemon Data 3', pokemonData3[-1])
  289.                 print('Pokemon Data 4', pokemonData4[-1])
  290.             else:
  291.                 pokemonNewData.append(binascii.hexlify(fileData.read()))
  292.                 print('New Pokemon Data', pokemonNewData[-1])
  293.    
  294.     with open(fileName + '-EXT' + '/' + 'Unhandled Data' + '/' + 'UNK11' + '.hex', 'rb') as fileData:
  295.         saveData['UNK11'] = binascii.hexlify(fileData.read())
  296.    
  297.     saveData['SHA1'] = '{:040X}'.format(0)
  298.     saveData['Mii Data Offset'] = '{:08X}'.format(1)
  299.     saveData['Mii-Trainer Data Offset'] = '{:08X}'.format(2)
  300.     saveData['Pokemon Data Offset'] = '{:08X}'.format(3)
  301.     saveData['Pokemon Data End Offset'] = '{:08X}'.format(4)
  302.    
  303.     with open(fileName + '.bin', 'wb') as tempData:
  304.         tempData.seek(0)
  305.        
  306.         print('Padding for SHA1')
  307.         tempData.write(binascii.unhexlify(saveData['SHA1']))
  308.        
  309.         print('UNK1 is ' + str(saveData['UNK1']))
  310.         tempData.write(binascii.unhexlify(saveData['UNK1']))
  311.        
  312.         print('UNK2 is ' + str(saveData['UNK2']))
  313.         tempData.write(binascii.unhexlify(saveData['UNK2']))
  314.        
  315.         print('Padding for Mii Data Offset')
  316.         tempData.write(binascii.unhexlify(saveData['Mii Data Offset']))
  317.        
  318.         print('UNK3 is ' + str(saveData['UNK3']))
  319.         tempData.write(binascii.unhexlify(saveData['UNK3']))
  320.        
  321.         print('Padding for Mii-Trainer Data Offset')
  322.         tempData.write(binascii.unhexlify(saveData['Mii-Trainer Data Offset']))
  323.        
  324.         print('UNK4 is ' + str(saveData['UNK4']))
  325.         tempData.write(binascii.unhexlify(saveData['UNK4']))
  326.        
  327.         print('Padding for Pokemon Data Offset')
  328.         tempData.write(binascii.unhexlify(saveData['Pokemon Data Offset']))
  329.        
  330.         print('UNK5 is ' + str(saveData['UNK5']))
  331.         tempData.write(binascii.unhexlify(saveData['UNK5']))
  332.        
  333.         print('Padding for Pokemon Data End Offset')
  334.         tempData.write(binascii.unhexlify(saveData['Pokemon Data End Offset']))
  335.        
  336.         print('UNK6 is ' + str(saveData['UNK6']))
  337.         tempData.write(binascii.unhexlify(saveData['UNK6']))
  338.        
  339.         print('Language is ' + str(saveData['Language']))
  340.         tempData.write(binascii.unhexlify(saveData['Language']))
  341.        
  342.         # print('UNK7 is ' + str(saveData['UNK7']))
  343.         tempData.write(binascii.unhexlify(saveData['UNK7']))
  344.        
  345.         saveData['Mii Data Offset'] = '{:08X}'.format(tempData.tell())
  346.         print('Mii Data Offset is ' + str(saveData['Mii Data Offset']))
  347.        
  348.         print('UNK8 is ' + str(saveData['UNK8']))
  349.         tempData.write(binascii.unhexlify(saveData['UNK8']))
  350.        
  351.         print('Mii Count is ' + str(saveData['Mii Count']))
  352.         tempData.write(binascii.unhexlify(saveData['Mii Count']))
  353.        
  354.         # print('Mii Data is ' + str(saveData['Mii Data']))
  355.         tempData.write(binascii.unhexlify(saveData['Mii Data']))
  356.        
  357.         saveData['Mii-Trainer Data Offset'] = '{:08X}'.format(tempData.tell())
  358.         print('Mii-Trainer Data Offset is ' + str(saveData['Mii-Trainer Data Offset']))
  359.        
  360.         print('UNK9 is ' + str(saveData['UNK9']))
  361.         tempData.write(binascii.unhexlify(saveData['UNK9']))
  362.        
  363.         print('Mii-Trainer Count is ' + str(saveData['Mii-Trainer Count']))
  364.         tempData.write(binascii.unhexlify(saveData['Mii-Trainer Count']))
  365.        
  366.         # print('Mii-Trainer Data is ' + str(saveData['Mii-Trainer Data']))
  367.         tempData.write(binascii.unhexlify(saveData['Mii-Trainer Data']))
  368.        
  369.         # print('UNK12 is ' + str(saveData['UNK12']))
  370.         tempData.write(binascii.unhexlify(saveData['UNK12']))
  371.        
  372.         saveData['Pokemon Data Offset'] = '{:08X}'.format(tempData.tell())
  373.         print('Pokemon Data Offset is ' + str(saveData['Pokemon Data Offset']))
  374.        
  375.         print('UNK10 is ' + str(saveData['UNK10']))
  376.         tempData.write(binascii.unhexlify(saveData['UNK10']))
  377.        
  378.         print('Pokemon Count is ' + str(saveData['Pokemon Count']))
  379.         tempData.write(binascii.unhexlify(saveData['Pokemon Count']))
  380.        
  381.         i = 0
  382.         j = range(len(pokemonData1))
  383.         print('Pokemon data 1 len', len(pokemonData1))
  384.         for i in j:
  385.             tempData.write(binascii.unhexlify(pokemonData1[i]))
  386.             tempData.write(binascii.unhexlify(pokemonData2[i]))
  387.             tempData.write(binascii.unhexlify(pokemonData3[i]))
  388.             tempData.write(binascii.unhexlify(pokemonData4[i]))
  389.        
  390.         i = 0
  391.         j = range(len(pokemonNewData))
  392.         print('Pokemon new data len', len(pokemonNewData))
  393.         if len(pokemonNewData) != 0:
  394.             for i in j:
  395.                 print('New Pokemon data', pokemonNewData[i])
  396.                 tempData.write(binascii.unhexlify(pokemonNewData[i]))
  397.                 tempData.write(binascii.unhexlify(pokemonData2[-1]))
  398.                 tempData.write(binascii.unhexlify(pokemonData3[-1]))
  399.                 tempData.write(binascii.unhexlify(pokemonData4[-1]))
  400.        
  401.         saveData['Pokemon Data End Offset'] = '{:08X}'.format(tempData.tell())
  402.         print('Pokemon Data End Offset is ' + str(saveData['Pokemon Data End Offset']))
  403.        
  404.         print('UNK11 is ' + str(saveData['UNK11']))
  405.         tempData.write(binascii.unhexlify(saveData['UNK11']))
  406.        
  407.         while tempData.tell() < saveData['File Length']:
  408.             tempData.write(binascii.unhexlify('00'))
  409.        
  410.         tempData.seek(int('24', 16))
  411.         tempData.write(binascii.unhexlify(saveData['Mii Data Offset']))
  412.         tempData.seek(int('2C', 16))
  413.         tempData.write(binascii.unhexlify(saveData['Mii-Trainer Data Offset']))
  414.         tempData.seek(int('34', 16))
  415.         tempData.write(binascii.unhexlify(saveData['Pokemon Data Offset']))
  416.         tempData.seek(int('3C', 16))
  417.         tempData.write(binascii.unhexlify(saveData['Pokemon Data End Offset']))
  418.     print('Done')
  419.     calcHash(fileName + '.bin')
  420.  
  421. def calcHash(toHash):
  422.     with open(toHash, 'rb+') as fileData:
  423.         fileData.seek(20)
  424.         # sha1Result = '{:040X}'.format(hashlib.sha1(fileData.read()).hexdigest())
  425.         sha1Result = hashlib.sha1(fileData.read()).hexdigest()
  426.         print('New SHA1', sha1Result)
  427.         fileData.seek(0)
  428.         fileData.write(binascii.unhexlify(sha1Result))
  429.  
  430. handler(inputName)
RAW Paste Data