Advertisement
Guest User

ZnK NPC Name Inserter v1

a guest
Aug 3rd, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | None | 0 0
  1. #Inserts NPC names at the end of each script file
  2. #and updates data.lst with new lengths
  3. import os
  4. import struct
  5.  
  6. def get_data(filename):
  7.     totalbytes = os.path.getsize(filename)
  8.     infile = open(filename, 'rb')
  9.     totalfiledata = infile.read(totalbytes)
  10.     infile.close()
  11.     return totalfiledata
  12.  
  13. def replacestr(origstr,replacestr,startpos,replacelen):
  14. #Returns a string with a replaced sub-string
  15. #origstr - the original string, replacestr = the string to replace
  16. #startpos - where the replacement string should go
  17. #replacelen - how many characters of the original string to replace
  18.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  19.  
  20. def output_data(outputfilename,namestring):
  21.     filedata = get_data(outputfilename + '.orig') #Grab data
  22.     startpos = filedata.rfind(filename) #Find start position for insert
  23.  
  24.     strpos = -1 #Find end position for insert
  25.     while filedata[strpos] == '\x00':
  26.         strpos -= 1
  27.     endpos = strpos + len(filedata)
  28.     #Replace the data
  29.     filedata = replacestr(filedata,namestring,startpos,endpos-startpos+1)
  30.    
  31.     outfile = open(outputfilename + '.bin','wb') #Write the output
  32.     outfile.write(filedata)
  33.     outfile.close()
  34.     return len(filedata)
  35.  
  36. filenamelist = ['c0000','c000b','c000c','c0010','c001c','c0100','c0100_1',
  37.     'c010b','c010c','c0110','c0110_1','c011b','c011c','c0140','c014c','c0150',
  38.     'c015c','c0170','c017c','c0200','c020b','c020c','c0210','c0220','c0240',
  39.     'c0250','c0300','c030b','c030c','c0310','c0320','c0330','c0340','c0350',
  40.     'c0400','c040b','c040c','c0410','c041c','c0420','c042c','c0450','c045b',
  41.     'c045c','c0470','c047b','c047c','c0500','c050b','c050c','c0510','c0570',
  42.     'c0580','c0590','c0591','c0592','c0593','c0594','c0595','c0596','c0597',
  43.     'c0600','c0800','c1000','c100b','c100c','c1010','c101c','c1020','c102c',
  44.     'c1030','c103c','c1040','c1050','c1100','c110b','c110c','c1110','c1120',
  45.     'c112c','c1130','c1150','c115c','c1160','c1170','c1200','c120b','c120c',
  46.     'c1210','c121b','c1220','c1300','c130b','c130c','c1310','c131b','c1320',
  47.     'c1330','c133b','c1340','c134b','c1400','c140b','c140c','c1410','c1420',
  48.     'c1430','c1440','c1450','c1460','e0000','e0010','e0110','e0111','e0310',
  49.     'e0410','e0510','e3000','e3010','e3110','e3300','e3400','e3500','m0000',
  50.     'm0001','m0002','m0010','m0011','m0012','m0013','m0100','m0101','m0102',
  51.     'm0110','m0111','m0112','m0113','m1000','m1010','m1020','m1030','m1060',
  52.     'm1070','m1080','m1090','m1099','m1140','m1150','m2000','m2010','m2020',
  53.     'm2030','m2040','m2050','m2060','m2099','m3000','m3001','m3002','m3004',
  54.     'm3010','m3011','m3012','m3013','m3014','m3020','m3021','m3022','m3023',
  55.     'm3030','m3031','m3032','m3033','m3034','m3035','m3099','r0000','r0000_1',
  56.     'r000b','r0020','r0030','r0040','r0050','r0100','r0110','r0120','r1000',
  57.     'r1010','r1020','r1030','r1040','r1500','r150b','r1520','r152b','r1530',
  58.     'r153b','r1540','r154b','r1580','r158b','r2000','r2010','r2020','r2030',
  59.     'r2040','r2050','r2060','r206b','r2070','r3000','r3060','r3070','r307b',
  60.     'r3080','r308b','r308e','r3100','r3110','r3120','r3130','t0000','t0010',
  61.     't0020','t0500','t050b','t0510','t051b','t0520','t052b','t0530','t053b',
  62.     't0600','t0610','t0620','t0630','t1000','t100b','t1010','t101b','t1020',
  63.     't102b','t1030','t103b','t1040','t104b','t1050','t105b','t1060','t106b',
  64.     't1100','t110b','t1110','t111b','t1120','t112b','t1130','t113b','t1140',
  65.     't114b','t1150','t115b','t1160','t116b','t1170','t117b','t1180','t118b',
  66.     't1190','t119b','t1200','t120b','t1210','t121b','t1500','t150b','t1510',
  67.     't151b','t1520','t152b','t1530','t1530_1','t153b','t1540','t1540_1',
  68.     't154b','t1550','t155b','t1560','t156b','t1600','t160b','t1610','t161b',
  69.     't1620','t162b','t1630','t163b','t1640','t164b','t1650','t165b','t2000',
  70.     't2010','t2020','t2100','t2500','t2510','t2520','t3000','t3010','t3510',
  71.     't4000','t4010','t4020','t4100']
  72.  
  73. #Load the data list file
  74. fileindexdata = get_data('data.orig')
  75. fileindexpos = fileindexdata.find('scena')
  76.  
  77. #Load the input data
  78. npcs = []
  79. with open('npcs.data','rb') as f:
  80.     for line in f:
  81.         line = line[:-2] #Trims newline characters off the end of each line
  82.         npcs.append(line)
  83.  
  84. firstpass1 = True #Don't output on first pass
  85. for name in npcs:
  86.     if name in filenamelist:
  87.         if firstpass1 == True: #Don't output on first pass
  88.             firstpass1 = False
  89.         else: #We have hit a file name in the input, so we need to output
  90.             #Update the file index data with the new length
  91.             newlength = output_data(filename,namestring) #Replace the string and output
  92.             rowpos = fileindexdata.find(filename,fileindexpos)
  93.             newlengthcode = struct.pack('<I',newlength)
  94.             fileindexdata = replacestr(fileindexdata,newlengthcode,rowpos+8,4)
  95.         filename = name #Switch to working on a new file
  96.         namestring = name #Re-initialize the replacement string
  97.     else: #It is an entry in the input with name data
  98.         namestring += '\x00' + name #Append the name to the replacement string
  99. #These lines cover output of the last file
  100. newlength = output_data(filename,namestring)
  101. rowpos = fileindexdata.find(filename,fileindexpos)
  102. newlengthcode = struct.pack('<I',newlength)
  103. fileindexdata = replacestr(fileindexdata,newlengthcode,rowpos+8,4)
  104.  
  105. #Output the data list file
  106. outfile = open('data.data','wb')
  107. outfile.write(fileindexdata)
  108. outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement