Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Insert t_cook._dt (Zero no Kiseki)
- #Usage: python script.py (data.lst insert, default = no)
- #1. Need t_cook.orig (renamed original file) in a folder
- #2. Need input file: t_cook.data
- #2a. Go here: https://docs.google.com/spreadsheets/d/1FG6EMbwLaENqMbk1xJafqzwm_zrZU6S0XIvpabN4eNQ/edit?usp=sharing
- #2b. Copy to spreadsheet program (translated column and break type column).
- #2c. Export as CSV (Windows 932 or SHIFT-JIS encoding), tab delimiter
- #3. Optionally you need a data.lst file in the same folder
- #4. Run program: python script.py (data.lst insert, default = no)
- #For example: python namesinsert.py yes
- import struct
- import os
- import sys
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- def is_jis_char(s):
- if len(s.decode('cp932')) == 1:
- return True
- return False
- def replacestr(origstr,replacestr,startpos,replacelen):
- #Returns a string with a replaced sub-string
- #origstr - the original string, replacestr = the string to replace
- #startpos - where the replacement string should go
- #replacelen - how many characters of the original string to replace
- return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
- def myprogram(updateindexdata):
- pointerpos = 0x30
- startofdata = 0x51c
- filedata = get_data('t_cook.orig')
- output = ""
- inputdata = []
- with open('t_cook.data','rb') as f:
- for line in f:
- line = line[:-2] #Trims newline characters off the end of each line
- line = line.split('\t')
- line[0] = line[0].translate(None,' ')
- inputdata.append(tuple(line))
- datapos = startofdata
- for (line, breaktype) in inputdata:
- output += line
- if breaktype == 'space':
- output += ' '
- elif breaktype == 'linebreak':
- output += '\\n'
- elif breaktype == 'terminalcode':
- output += '\x00'
- filedata = replacestr(filedata,struct.pack('<H',datapos),pointerpos,2)
- datapos = startofdata + len(output)
- pointerpos += 0x30
- filedata = replacestr(filedata,output,startofdata,len(filedata[startofdata:]))
- outfile = open('t_cook._dt','wb')
- outfile.write(filedata)
- outfile.close()
- if updateindexdata == True: #If you want to update the data.lst file...
- indexfilename = 'data.lst'
- indexfiledata = get_data(indexfilename)
- pos = indexfiledata.find('text' + '\x00\x00\x00\x00') #Find position of "text" folder marker
- pos = indexfiledata.find('t_cook',pos) #Find position of current filename
- #In one line, compute the new file size, pack it in little endian "word" format, and
- #Replace the relevant bytes in data.lst
- indexfiledata = replacestr(indexfiledata,struct.pack('<I',len(filedata)),pos+8,4)
- #Write-back data.lst file
- outfile = open(indexfilename,'wb')
- outfile.write(indexfiledata)
- outfile.close()
- if __name__ == '__main__':
- #sys.argv.append('yes')
- #No arguments: Run program (no replace of data.lst)
- if len(sys.argv) == 1:
- sys.argv.append(False)
- #First "no" or "n" argument: no replace of data.lst
- elif 'n' in sys.argv[1].lower():
- sys.argv[1] = False
- #Something else: do replace of data.lst
- else:
- sys.argv[1] = True
- #Run program
- myprogram(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement