Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import struct
- #Gets data
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- #Tells if the string s is a SHIFT-JIS character or not
- def is_jis_char(s):
- if len(s) != 2: #If it's not a string of length 2, then...
- return False
- try: #Python error handling
- if len(s.decode('cp932')) != 1: #For example, 2 ascii characters will be length 2
- return False #Two bytes that decode to a SHIFT-JIS character will be length 1
- except UnicodeDecodeError: #The string couldn't be decoded
- return False #Not a SHIFT-JIS character
- return True #It is a SHIFT-JIS character
- filedata = get_data('item.sq') #Get data
- ptr = 0 #Initialize variables
- with open('itemdata.csv','wb') as f: #Open output file
- while ptr < 0x1028: #Until the end of the pointer table
- target = struct.unpack('<I',filedata[ptr:ptr+4])[0] #Find pointer target
- if ord(filedata[target]) > 31 and ord(filedata[target]) < 127 and \
- ord(filedata[target+1]) > 31 and ord(filedata[target+1]): #First 2 bytes are both ASCII
- s = filedata[target:filedata.find('\x00',target)] #Find the string at this position
- if len(s) > 24: #If the string is longer than 24
- f.write(hex(ptr) + ';' + s[:24] + '\r\n') #Write pointer and 1st 24 characters
- f.write(';' + s[24:] + '\r\n') #Write remaining characters
- else:
- f.write(hex(ptr) + ';' + s + '\r\n') #Write the pointer and the string
- elif is_jis_char(filedata[target:target+2]): #First 2 bytes are a SHIFT-JIS character
- s = filedata[target:filedata.find('\x00',target)] #Same as before
- if len(s) > 24:
- f.write(hex(ptr) + ';' + s[:24] + '\r\n')
- f.write(';' + s[24:] + '\r\n')
- else:
- f.write(hex(ptr) + ';' + s + '\r\n')
- ptr += 4 #Increment the pointer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement