Advertisement
Guest User

ED3 Script Test

a guest
May 12th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import os
  3.  
  4. def get_data(filename):
  5.     totalbytes = os.path.getsize(filename)
  6.     infile = open(filename, 'rb')
  7.     totalfiledata = infile.read(totalbytes)
  8.     infile.close()
  9.     return totalfiledata
  10.  
  11. def is_jis_char(s):
  12.     if len(s) != 2: #If it's not a string of length 2, then...
  13.         return False
  14.     try:            #Python error handling
  15.         if len(s.decode('cp932')) != 1: #For example, 2 ascii characters will be length 2
  16.             return False                #Two bytes that decode to a SHIFT-JIS character will be length 1
  17.     except UnicodeDecodeError:  #The string couldn't be decoded
  18.         return False            #Not a SHIFT-JIS character
  19.     return True                 #It is a SHIFT-JIS character
  20.  
  21. def decode_string(s):
  22.     mode = True
  23.     s1 = u'。、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙゚'
  24.     s2 = u'。、Xをぁぃぅぇぉゃゅょっーあいうえおかきくけこさしすせそたちつてと\
  25. なにぬねのはひふへほまみむめもやゆよらりるれろわんXX'
  26.     s3 = u'。、Xヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテト\
  27. ナニヌネノハヒフヘホマミムメモヤユヨラリルレロワンXX'
  28.     t = zip(s1,s2,s3) #Put 'em, togther, whatdayya got? :)
  29.     sd = ''
  30.     pos = 0
  31.     while pos < len(s):
  32.         if ord(s[pos]) == 0xA2: #Set hiragana flag
  33.             mode = False
  34.             pos += 1
  35.         elif ord(s[pos]) == 0xA3: #Set katakana flag, the default
  36.             mode = True
  37.             pos += 1
  38.         elif ord(s[pos]) == 1: #Linebreak, not sure
  39.             sd += '\n'
  40.             pos += 1
  41.         elif ord(s[pos]) == 2: #Delay, not sure
  42.             sd += 'delay'
  43.             pos += 1
  44.         elif ord(s[pos]) == 3:
  45.             sd += 'Unknown Opcode 03'
  46.             pos += 1
  47.         elif ord(s[pos]) == 4:
  48.             sd += 'Unknown Opcode 04'
  49.             pos += 1
  50.         elif ord(s[pos]) == 5:
  51.             sd += 'Unknown Opcode 05'
  52.             pos += 1
  53.         elif ord(s[pos]) == 6:
  54.             sd += 'Unknown Opcode 06'
  55.             pos += 1
  56.         elif ord(s[pos]) == 0x14:
  57.             sd += 'Unknown Opcode 14'
  58.             pos += 1
  59.         elif ord(s[pos]) >= 0x20 and ord(s[pos]) < 0x7F: #ASCII
  60.             sd += s[pos]
  61.             pos += 1
  62.         elif pos < len(s) - 1 and is_jis_char(s[pos:pos+2]): #SHIFT-JIS char, double-byte
  63.             sd += s[pos:pos+2]
  64.             pos += 2
  65.         elif unicode(s[pos],'cp932') in s1:     #SHIFT-JIS char, single-byte
  66.             for x, y, z in t:
  67.                 if unicode(s[pos],'cp932') == x:
  68.                     if mode == True:
  69.                         sd += y.encode('cp932')
  70.                     else:
  71.                         sd += z.encode('cp932')
  72.                     pos += 1
  73.                     break
  74.         else:
  75.             print 'Unknown character.'
  76.             print pos
  77.             return sd
  78.     return sd
  79.                
  80.  
  81. filedata = get_data('crap2.dat')
  82. pos = 0x4e0
  83. opcodes = ('\x00\x0f','\x04\x0f')
  84. while True:
  85.     r = []
  86.     for op in opcodes:
  87.         r.append(filedata.find(op,pos))
  88.     pos = min(r) + 2
  89.     print hex(pos)
  90.     if pos == 1:
  91.         break
  92.     endpos = filedata.find('\x00', pos)
  93.     s = filedata[pos:endpos]
  94.     sd = decode_string(s)
  95.     print sd.decode('cp932')
  96.     pos = endpos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement