Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Inserts NPC names at the end of each script file
- #and updates data.lst with new lengths
- import os
- import struct
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- 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 output_data(outputfilename,namestring):
- filedata = get_data(outputfilename + '.orig') #Grab data
- startpos = filedata.rfind(filename) #Find start position for insert
- strpos = -1 #Find end position for insert
- while filedata[strpos] == '\x00':
- strpos -= 1
- endpos = strpos + len(filedata)
- #Replace the data
- filedata = replacestr(filedata,namestring,startpos,endpos-startpos+1)
- outfile = open(outputfilename + '.bin','wb') #Write the output
- outfile.write(filedata)
- outfile.close()
- return len(filedata)
- filenamelist = ['c0000','c000b','c000c','c0010','c001c','c0100','c0100_1',
- 'c010b','c010c','c0110','c0110_1','c011b','c011c','c0140','c014c','c0150',
- 'c015c','c0170','c017c','c0200','c020b','c020c','c0210','c0220','c0240',
- 'c0250','c0300','c030b','c030c','c0310','c0320','c0330','c0340','c0350',
- 'c0400','c040b','c040c','c0410','c041c','c0420','c042c','c0450','c045b',
- 'c045c','c0470','c047b','c047c','c0500','c050b','c050c','c0510','c0570',
- 'c0580','c0590','c0591','c0592','c0593','c0594','c0595','c0596','c0597',
- 'c0600','c0800','c1000','c100b','c100c','c1010','c101c','c1020','c102c',
- 'c1030','c103c','c1040','c1050','c1100','c110b','c110c','c1110','c1120',
- 'c112c','c1130','c1150','c115c','c1160','c1170','c1200','c120b','c120c',
- 'c1210','c121b','c1220','c1300','c130b','c130c','c1310','c131b','c1320',
- 'c1330','c133b','c1340','c134b','c1400','c140b','c140c','c1410','c1420',
- 'c1430','c1440','c1450','c1460','e0000','e0010','e0110','e0111','e0310',
- 'e0410','e0510','e3000','e3010','e3110','e3300','e3400','e3500','m0000',
- 'm0001','m0002','m0010','m0011','m0012','m0013','m0100','m0101','m0102',
- 'm0110','m0111','m0112','m0113','m1000','m1010','m1020','m1030','m1060',
- 'm1070','m1080','m1090','m1099','m1140','m1150','m2000','m2010','m2020',
- 'm2030','m2040','m2050','m2060','m2099','m3000','m3001','m3002','m3004',
- 'm3010','m3011','m3012','m3013','m3014','m3020','m3021','m3022','m3023',
- 'm3030','m3031','m3032','m3033','m3034','m3035','m3099','r0000','r0000_1',
- 'r000b','r0020','r0030','r0040','r0050','r0100','r0110','r0120','r1000',
- 'r1010','r1020','r1030','r1040','r1500','r150b','r1520','r152b','r1530',
- 'r153b','r1540','r154b','r1580','r158b','r2000','r2010','r2020','r2030',
- 'r2040','r2050','r2060','r206b','r2070','r3000','r3060','r3070','r307b',
- 'r3080','r308b','r308e','r3100','r3110','r3120','r3130','t0000','t0010',
- 't0020','t0500','t050b','t0510','t051b','t0520','t052b','t0530','t053b',
- 't0600','t0610','t0620','t0630','t1000','t100b','t1010','t101b','t1020',
- 't102b','t1030','t103b','t1040','t104b','t1050','t105b','t1060','t106b',
- 't1100','t110b','t1110','t111b','t1120','t112b','t1130','t113b','t1140',
- 't114b','t1150','t115b','t1160','t116b','t1170','t117b','t1180','t118b',
- 't1190','t119b','t1200','t120b','t1210','t121b','t1500','t150b','t1510',
- 't151b','t1520','t152b','t1530','t1530_1','t153b','t1540','t1540_1',
- 't154b','t1550','t155b','t1560','t156b','t1600','t160b','t1610','t161b',
- 't1620','t162b','t1630','t163b','t1640','t164b','t1650','t165b','t2000',
- 't2010','t2020','t2100','t2500','t2510','t2520','t3000','t3010','t3510',
- 't4000','t4010','t4020','t4100']
- #Load the data list file
- fileindexdata = get_data('data.orig')
- fileindexpos = fileindexdata.find('scena')
- #Load the input data
- npcs = []
- with open('npcs.data','rb') as f:
- for line in f:
- line = line[:-2] #Trims newline characters off the end of each line
- npcs.append(line)
- firstpass1 = True #Don't output on first pass
- for name in npcs:
- if name in filenamelist:
- if firstpass1 == True: #Don't output on first pass
- firstpass1 = False
- else: #We have hit a file name in the input, so we need to output
- #Update the file index data with the new length
- newlength = output_data(filename,namestring) #Replace the string and output
- rowpos = fileindexdata.find(filename,fileindexpos)
- newlengthcode = struct.pack('<I',newlength)
- fileindexdata = replacestr(fileindexdata,newlengthcode,rowpos+8,4)
- filename = name #Switch to working on a new file
- namestring = name #Re-initialize the replacement string
- else: #It is an entry in the input with name data
- namestring += '\x00' + name #Append the name to the replacement string
- #These lines cover output of the last file
- newlength = output_data(filename,namestring)
- rowpos = fileindexdata.find(filename,fileindexpos)
- newlengthcode = struct.pack('<I',newlength)
- fileindexdata = replacestr(fileindexdata,newlengthcode,rowpos+8,4)
- #Output the data list file
- outfile = open('data.data','wb')
- outfile.write(fileindexdata)
- outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement