Advertisement
Guest User

ZnK Cook v1

a guest
Aug 26th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. #Insert t_cook._dt (Zero no Kiseki)
  2. #Usage: python script.py (data.lst insert, default = no)
  3. #1. Need t_cook.orig (renamed original file) in a folder
  4. #2. Need input file: t_cook.data
  5. #2a. Go here: https://docs.google.com/spreadsheets/d/1FG6EMbwLaENqMbk1xJafqzwm_zrZU6S0XIvpabN4eNQ/edit?usp=sharing
  6. #2b. Copy to spreadsheet program (translated column and break type column).
  7. #2c. Export as CSV (Windows 932 or SHIFT-JIS encoding), tab delimiter
  8. #3. Optionally you need a data.lst file in the same folder
  9. #4. Run program: python script.py (data.lst insert, default = no)
  10. #For example: python namesinsert.py yes
  11. import struct
  12. import os
  13. import sys
  14.  
  15. def get_data(filename):
  16.     totalbytes = os.path.getsize(filename)
  17.     infile = open(filename, 'rb')
  18.     totalfiledata = infile.read(totalbytes)
  19.     infile.close()
  20.     return totalfiledata
  21.  
  22. def is_jis_char(s):
  23.     if len(s.decode('cp932')) == 1:
  24.         return True
  25.     return False
  26.  
  27. def replacestr(origstr,replacestr,startpos,replacelen):
  28. #Returns a string with a replaced sub-string
  29. #origstr - the original string, replacestr = the string to replace
  30. #startpos - where the replacement string should go
  31. #replacelen - how many characters of the original string to replace
  32.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  33.  
  34. def myprogram(updateindexdata):
  35.     pointerpos = 0x30
  36.     startofdata = 0x51c
  37.     filedata = get_data('t_cook.orig')
  38.     output = ""
  39.  
  40.     inputdata = []
  41.     with open('t_cook.data','rb') as f:
  42.         for line in f:
  43.             line = line[:-2] #Trims newline characters off the end of each line
  44.             line = line.split('\t')
  45.             line[0] = line[0].translate(None,' ')
  46.             inputdata.append(tuple(line))
  47.  
  48.     datapos = startofdata
  49.     for (line, breaktype) in inputdata:
  50.         output += line
  51.         if breaktype == 'space':
  52.             output += ' '
  53.         elif breaktype == 'linebreak':
  54.             output += '\\n'
  55.         elif breaktype == 'terminalcode':
  56.             output += '\x00'
  57.             filedata = replacestr(filedata,struct.pack('<H',datapos),pointerpos,2)
  58.             datapos = startofdata + len(output)
  59.             pointerpos += 0x30
  60.  
  61.     filedata = replacestr(filedata,output,startofdata,len(filedata[startofdata:]))
  62.     outfile = open('t_cook._dt','wb')
  63.     outfile.write(filedata)
  64.     outfile.close()
  65.  
  66.     if updateindexdata == True: #If you want to update the data.lst file...
  67.  
  68.         indexfilename = 'data.lst'
  69.         indexfiledata = get_data(indexfilename)
  70.        
  71.         pos = indexfiledata.find('text' + '\x00\x00\x00\x00') #Find position of "text" folder marker
  72.         pos = indexfiledata.find('t_cook',pos) #Find position of current filename
  73.         #In one line, compute the new file size, pack it in little endian "word" format, and
  74.         #Replace the relevant bytes in data.lst
  75.         indexfiledata = replacestr(indexfiledata,struct.pack('<I',len(filedata)),pos+8,4)
  76.  
  77.         #Write-back data.lst file
  78.         outfile = open(indexfilename,'wb')
  79.         outfile.write(indexfiledata)
  80.         outfile.close()
  81.  
  82. if __name__ == '__main__':
  83.  
  84.     #sys.argv.append('yes')
  85.     #No arguments: Run program (no replace of data.lst)
  86.     if len(sys.argv) == 1:
  87.         sys.argv.append(False)
  88.     #First "no" or "n" argument: no replace of data.lst
  89.     elif 'n' in sys.argv[1].lower():
  90.         sys.argv[1] = False
  91.     #Something else: do replace of data.lst
  92.     else:
  93.         sys.argv[1] = True
  94.        
  95.     #Run program
  96.     myprogram(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement