diff pytouhou/formats/ecl.py @ 43:7195aaf95f6e

Fix set_counter, and relative_jump(_ex)
author Thibaut Girka <thib@sitedethib.com>
date Thu, 18 Aug 2011 22:24:32 +0200
parents 1b0ca2fb89f9
children 25ca15f714ad
line wrap: on
line diff
--- a/pytouhou/formats/ecl.py
+++ b/pytouhou/formats/ecl.py
@@ -133,10 +133,15 @@ class ECL(object):
         ecl.main = []
 
         # Read subs
-        for offset in sub_offsets:
-            file.seek(offset)
+        for sub_offset in sub_offsets:
+            file.seek(sub_offset)
             ecl.subs.append([])
+
+            instruction_offsets = []
+
             while True:
+                instruction_offsets.append(file.tell() - sub_offset)
+
                 time, opcode = unpack('<IH', file.read(6))
                 if time == 0xffffffff or opcode == 0xffff:
                     break
@@ -147,8 +152,22 @@ class ECL(object):
                 else:
                     args = (data, )
                     print('Warning: unknown opcode %d' % opcode) #TODO
+
                 ecl.subs[-1].append((time, opcode, rank_mask, param_mask, args))
 
+
+            # Translate offsets to instruction pointers
+            for instr_offset, (i, instr) in zip(instruction_offsets, enumerate(ecl.subs[-1])):
+                time, opcode, rank_mask, param_mask, args = instr
+                if opcode == 2: # relative_jump
+                    frame, relative_offset = args
+                    args = frame, instruction_offsets.index(instr_offset + relative_offset)
+                elif opcode == 3: # relative_jump_ex
+                    frame, relative_offset, counter_id = args
+                    args = frame, instruction_offsets.index(instr_offset + relative_offset), counter_id
+                ecl.subs[-1][i] = time, opcode, rank_mask, param_mask, args
+
+
         # Read main
         file.seek(main_offset)
         while True: