Advertisement
Guest User

Wander Wonder Item Dump v1

a guest
Apr 12th, 2015
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. import os
  2. import struct
  3.  
  4. #Gets data
  5. def get_data(filename):
  6.     totalbytes = os.path.getsize(filename)
  7.     infile = open(filename, 'rb')
  8.     totalfiledata = infile.read(totalbytes)
  9.     infile.close()
  10.     return totalfiledata
  11.  
  12. #Tells if the string s is a SHIFT-JIS character or not
  13. def is_jis_char(s):
  14.     if len(s) != 2: #If it's not a string of length 2, then...
  15.         return False
  16.     try:            #Python error handling
  17.         if len(s.decode('cp932')) != 1: #For example, 2 ascii characters will be length 2
  18.             return False                #Two bytes that decode to a SHIFT-JIS character will be length 1
  19.     except UnicodeDecodeError:  #The string couldn't be decoded
  20.         return False            #Not a SHIFT-JIS character
  21.     return True                 #It is a SHIFT-JIS character
  22.  
  23. filedata = get_data('item.sq')  #Get data
  24. ptr = 0                         #Initialize variables
  25. with open('itemdata.csv','wb') as f:    #Open output file
  26.     while ptr < 0x1028:         #Until the end of the pointer table
  27.         target = struct.unpack('<I',filedata[ptr:ptr+4])[0] #Find pointer target
  28.         if ord(filedata[target]) > 31 and ord(filedata[target]) < 127 and \
  29.            ord(filedata[target+1]) > 31 and ord(filedata[target+1]):    #First 2 bytes are both ASCII
  30.             s = filedata[target:filedata.find('\x00',target)]           #Find the string at this position
  31.             if len(s) > 24:                                             #If the string is longer than 24
  32.                 f.write(hex(ptr) + ';' + s[:24] + '\r\n')               #Write pointer and 1st 24 characters
  33.                 f.write(';' + s[24:] + '\r\n')                          #Write remaining characters
  34.             else:
  35.                 f.write(hex(ptr) + ';' + s + '\r\n')                    #Write the pointer and the string
  36.         elif is_jis_char(filedata[target:target+2]):                #First 2 bytes are a SHIFT-JIS character
  37.             s = filedata[target:filedata.find('\x00',target)]       #Same as before
  38.             if len(s) > 24:
  39.                 f.write(hex(ptr) + ';' + s[:24] + '\r\n')
  40.                 f.write(';' + s[24:] + '\r\n')
  41.             else:
  42.                 f.write(hex(ptr) + ';' + s + '\r\n')
  43.         ptr += 4    #Increment the pointer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement