Advertisement
Guest User

YvT Item Insert v1

a guest
May 22nd, 2016
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. # Insert Nayuta no Kiseki item.tbl
  2. # To run, need itemdata.csv from the dump tool (with English text this time)
  3. # Max name length is 23 (extra truncated)
  4. # Max description length is 78 (extra truncated)
  5. # For item descriptions only 60 characters will display
  6. # 1. Put this script, item.orig (renamed item.tbl) and itemdata.csv
  7. #    in same folder
  8. # 2. Run script
  9. # Output is itemdata.tbl (game format)
  10.  
  11. import codecs
  12. import os
  13.  
  14. def get_data(filename):
  15.     totalbytes = os.path.getsize(filename)
  16.     infile = open(filename, 'rb')
  17.     totalfiledata = infile.read(totalbytes)
  18.     infile.close()
  19.     return totalfiledata
  20.  
  21. def replacestr(origstr,replacestr,startpos,replacelen):
  22. #Returns a string with a replaced sub-string
  23. #origstr - the original string, replacestr = the string to replace
  24. #startpos - where the replacement string should go
  25. #replacelen - how many characters of the original string to replace
  26.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  27.  
  28. TEXT_COL = 2
  29. filedata = get_data('item.orig')
  30.  
  31. # Read input data
  32. inputdata = []
  33. with codecs.open('itemdata.tsv','rb', encoding = 'utf-8') as f:
  34.     linecount = 1
  35.     for line in f:
  36. ##        print linecount
  37.         line = line.rstrip('\r\n').split('\t')
  38.         if line[0] != "":
  39.             addr = int(line[0],16)
  40.             name = line[TEXT_COL].encode('cp932')
  41.         else:
  42.             desc = line[TEXT_COL].encode('cp932')
  43.             inputdata.append((addr,name,desc))
  44.         linecount += 1
  45.  
  46. for addr, name, desc in inputdata:
  47.     if len(name) > 23:
  48.         print "Name %s at address %s len = %d, max len = 23, truncating" % \
  49.               (name, hex(addr), len(name))
  50.         name = name[:23]
  51.     if len(desc) > 63:
  52.         print ("Description %s at address %s len = %d, "
  53.               "max len = 78, truncating" % (desc, hex(addr), len(desc)))
  54.         desc = desc[:63]
  55.     name = name + '\x00' + '\xfd' * (24 - len(name) - 1)
  56.     desc = desc + '\x00' + '\xfd' * (64 - len(desc) - 1)
  57.     filedata = replacestr(filedata,name,addr,24)
  58.     filedata = replacestr(filedata,desc,addr + 0x18,64)
  59.  
  60. outfile = open('item.tbl','wb')
  61. outfile.write(filedata)
  62. outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement