def __init__(self, label=None, id=None, address=None, params=None):
        self.id = id
        self.label = crystal.Label(name=label, address=address, object=self)
        self.address = address
        self.params = params

        max_byte_count = 0x4000

        # Some of these scripts need to be truncated before insertion, because
        # output_bank_opcodes doesn't know anything about stopping if some of
        # the local labels are not resolved yet.

        # Script_if_equal
        if address == 0x97540:
            max_byte_count = 86

        # disassemble and laso get the last address
        (asm, last_address, last_hl_address, last_a_address, used_3d97) = gbz80disasm.output_bank_opcodes(address, max_byte_count=max_byte_count, stop_at=command_pointers, include_last_address=False)

        # remove indentation
        asm = asm.replace("\n\t", "\n")
        if asm[0] == "\t":
            asm = asm[1:]

        # remove the last two newlines
        while asm[-1] == "\n":
            asm = asm[:-1]

        self.asm = asm
        self.last_address = last_address

        # make sure this gets dumped into main.asm
        #if crystal.script_parse_table[self.address] == None and crystal.script_parse_table[self.last_address] == None:
        crystal.script_parse_table[self.address : self.last_address] = self
예제 #2
0
def scan_for_map_scripts_pointer():
    for map_id in extract_maps.map_headers.keys(
    ):  #skip id=0 (Pallet Town) because the naming conventions are wonky
        map2 = extract_maps.map_headers[map_id]
        if map_id in extract_maps.bad_maps or map_id in [0, 39, 37, 38]:
            continue  #skip
        script_pointer = int(map2["script_pointer"], 16)

        main_asm_output, offset, last_hl_address, last_a_address, used_3d97 = output_bank_opcodes(
            script_pointer)
        hl_pointer = "None"

        first_script_text = ""
        if last_hl_address != None and last_hl_address != "None" and used_3d97 == True:
            if last_hl_address > 0x3fff:
                hl_pointer = extract_maps.calculate_pointer(
                    last_hl_address, int(map2["bank"], 16))
            else:
                hl_pointer = last_hl_address
            byte1 = ord(extract_maps.rom[hl_pointer])
            byte2 = ord(extract_maps.rom[hl_pointer + 1])
            address = byte1 + (byte2 << 8)

            if address > 0x3fff:
                first_script_pointer = extract_maps.calculate_pointer(
                    address, int(map2["bank"], 16))
            else:
                first_script_pointer = address

            #for later output
            first_script_text = " first_script=" + hex(first_script_pointer)

            #go ahead and insert this script pointer
            insert_asm(first_script_pointer,
                       map_name_cleaner(map2["name"], None)[:-2] + "Script0")

            #reset everything
            #analyze_incbins.reset_incbins()
            asm = None
            incbin_lines = []
            processed_incbins = {}
            analyze_incbins.asm = None
            analyze_incbins.incbin_lines = []
            analyze_incbins.processed_incbins = {}

            #reload
            load_asm()
            isolate_incbins()
            process_incbins()

            a_numbers = [0]
            last_a_id = 0
            script_pointers = [hex(first_script_pointer)]
            latest_script_pointer = first_script_pointer
            while last_a_id == (max(a_numbers)) or last_a_id == 0:
                asm_output, offset, last_hl_address2, last_a_id, byte1, byte2, address = None, None, None, None, None, None, None
                asm_output, offset, last_hl_address2, last_a_id, used_3d97_2 = output_bank_opcodes(
                    latest_script_pointer)

                if last_a_id == (max(a_numbers) + 1):
                    a_numbers.append(last_a_id)
                else:
                    break

                byte1 = ord(extract_maps.rom[hl_pointer + (2 * last_a_id)])
                byte2 = ord(extract_maps.rom[hl_pointer + (2 * last_a_id) + 1])
                address2 = byte1 + (byte2 << 8)
                if address2 > 0x3fff:
                    latest_script_pointer = extract_maps.calculate_pointer(
                        address2, int(map2["bank"], 16))
                else:
                    latest_script_pointer = address2

                script_pointers.append(hex(latest_script_pointer))
                #print "latest script pointer (part 1): " + hex(address2)
                #print "latest script pointer: " + hex(latest_script_pointer)

                #go ahead and insert the asm for this script
                result = insert_asm(
                    latest_script_pointer,
                    map_name_cleaner(map2["name"], None)[:-2] + "Script" +
                    str(len(script_pointers) - 1))

                if result:
                    #reset everything
                    #analyze_incbins.reset_incbins()
                    asm = None
                    incbin_lines = []
                    processed_incbins = {}
                    analyze_incbins.asm = None
                    analyze_incbins.incbin_lines = []
                    analyze_incbins.processed_incbins = {}

                    #reload
                    load_asm()
                    isolate_incbins()
                    process_incbins()

            print "map_id=" + str(map_id) + " scripts are: " + str(
                script_pointers)

        if last_hl_address == None: last_hl_address = "None"
        else: last_hl_address = hex(last_hl_address)

        if hl_pointer != None and hl_pointer != "None":
            hl_pointer = hex(hl_pointer)

        print "map_id=" + str(
            map_id) + " " + map2["name"] + " script_pointer=" + hex(
                script_pointer
            ) + " script_pointers=" + hl_pointer + first_script_text
        print main_asm_output
        print "\n\n"

        #insert asm for the main script
        result = insert_asm(
            script_pointer,
            map_name_cleaner(map2["name"], None)[:-2] + "Script")

        if result:
            #reset everything
            #analyze_incbins.reset_incbins()
            asm = None
            incbin_lines = []
            processed_incbins = {}
            analyze_incbins.asm = None
            analyze_incbins.incbin_lines = []
            analyze_incbins.processed_incbins = {}

            #reload
            load_asm()
            isolate_incbins()
            process_incbins()

        #insert script pointer list asm if there's anything of value
        if hl_pointer != None and hl_pointer != "None" and used_3d97 == True:
            start_address = int(hl_pointer, 16)  #where to insert this list
            total_size = len(a_numbers) * 2

            script_label = map_name_cleaner(map2["name"], None)[:-2] + "Script"
            scripts_label = script_label + "s"
            script_asm = scripts_label + ": ; " + hex(start_address) + "\n"
            script_asm += spacing + "dw"

            first = True
            for id in a_numbers:
                if first:
                    script_asm += " "
                    first = False
                else:
                    script_asm += ", "
                script_asm += script_label + str(id)
            script_asm += "\n"  #extra newline?

            result = insert_asm(start_address,
                                scripts_label,
                                text_asm=script_asm,
                                end_address=start_address + total_size)
            if result:
                #reset everything
                #analyze_incbins.reset_incbins()
                asm = None
                incbin_lines = []
                processed_incbins = {}
                analyze_incbins.asm = None
                analyze_incbins.incbin_lines = []
                analyze_incbins.processed_incbins = {}

                #reload
                load_asm()
                isolate_incbins()
                process_incbins()
            else:
                print "trouble inserting map script pointer list"
                print script_asm
                sys.exit(0)
def scan_for_map_scripts_pointer():
    for map_id in extract_maps.map_headers.keys(): #skip id=0 (Pallet Town) because the naming conventions are wonky
        map2 = extract_maps.map_headers[map_id]
        if map_id in extract_maps.bad_maps or map_id in [0, 39, 37, 38]: continue #skip
        script_pointer = int(map2["script_pointer"], 16)

        main_asm_output, offset, last_hl_address, last_a_address, used_3d97 = output_bank_opcodes(script_pointer)
        hl_pointer = "None"
        
        first_script_text = ""
        if last_hl_address != None and last_hl_address != "None" and used_3d97==True:
            if last_hl_address > 0x3fff:
                hl_pointer = extract_maps.calculate_pointer(last_hl_address, int(map2["bank"], 16))
            else:
                hl_pointer = last_hl_address
            byte1 = ord(extract_maps.rom[hl_pointer])
            byte2 = ord(extract_maps.rom[hl_pointer+1])
            address = byte1 + (byte2 << 8)

            if address > 0x3fff:
                first_script_pointer = extract_maps.calculate_pointer(address, int(map2["bank"], 16))
            else:
                first_script_pointer = address

            #for later output
            first_script_text = " first_script=" + hex(first_script_pointer)

            #go ahead and insert this script pointer
            insert_asm(first_script_pointer, map_name_cleaner(map2["name"], None)[:-2] + "Script0")
            
            #reset everything
            #analyze_incbins.reset_incbins()
            asm = None
            incbin_lines = []
            processed_incbins = {}
            analyze_incbins.asm = None
            analyze_incbins.incbin_lines = []
            analyze_incbins.processed_incbins = {}
    
            #reload
            load_asm()
            isolate_incbins()
            process_incbins()

            a_numbers = [0]
            last_a_id = 0
            script_pointers = [hex(first_script_pointer)]
            latest_script_pointer = first_script_pointer
            while last_a_id == (max(a_numbers)) or last_a_id==0:
                asm_output, offset, last_hl_address2, last_a_id, byte1, byte2, address = None, None, None, None, None, None, None
                asm_output, offset, last_hl_address2, last_a_id, used_3d97_2 = output_bank_opcodes(latest_script_pointer)
                
                if last_a_id == (max(a_numbers) + 1):
                    a_numbers.append(last_a_id)
                else:
                    break
                
                byte1 = ord(extract_maps.rom[hl_pointer + (2*last_a_id)])
                byte2 = ord(extract_maps.rom[hl_pointer + (2*last_a_id) + 1])
                address2 = byte1 + (byte2 << 8)
                if address2 > 0x3fff:
                    latest_script_pointer = extract_maps.calculate_pointer(address2, int(map2["bank"], 16))
                else:
                    latest_script_pointer = address2

                script_pointers.append(hex(latest_script_pointer))
                #print "latest script pointer (part 1): " + hex(address2)
                #print "latest script pointer: " + hex(latest_script_pointer)

                #go ahead and insert the asm for this script
                result = insert_asm(latest_script_pointer, map_name_cleaner(map2["name"], None)[:-2] + "Script" + str(len(script_pointers) - 1))
                
                if result:
                    #reset everything
                    #analyze_incbins.reset_incbins()
                    asm = None
                    incbin_lines = []
                    processed_incbins = {}
                    analyze_incbins.asm = None
                    analyze_incbins.incbin_lines = []
                    analyze_incbins.processed_incbins = {}
            
                    #reload
                    load_asm()
                    isolate_incbins()
                    process_incbins()

            print "map_id=" + str(map_id) + " scripts are: " + str(script_pointers)
        
        if last_hl_address == None: last_hl_address = "None"
        else: last_hl_address = hex(last_hl_address)

        if hl_pointer != None and hl_pointer != "None": hl_pointer = hex(hl_pointer)

        print "map_id=" + str(map_id) + " " + map2["name"] + " script_pointer=" + hex(script_pointer) + " script_pointers=" + hl_pointer + first_script_text
        print main_asm_output
        print "\n\n"

        #insert asm for the main script
        result = insert_asm(script_pointer, map_name_cleaner(map2["name"], None)[:-2] + "Script")
        
        if result:
            #reset everything
            #analyze_incbins.reset_incbins()
            asm = None
            incbin_lines = []
            processed_incbins = {}
            analyze_incbins.asm = None
            analyze_incbins.incbin_lines = []
            analyze_incbins.processed_incbins = {}
    
            #reload
            load_asm()
            isolate_incbins()
            process_incbins()

        #insert script pointer list asm if there's anything of value
        if hl_pointer != None and hl_pointer != "None" and used_3d97==True:
            start_address = int(hl_pointer, 16) #where to insert this list
            total_size = len(a_numbers) * 2
            
            script_label = map_name_cleaner(map2["name"], None)[:-2] + "Script"
            scripts_label = script_label  + "s"
            script_asm = scripts_label + ": ; " + hex(start_address) + "\n"
            script_asm += spacing + "dw"

            first = True
            for id in a_numbers:
                if first:
                    script_asm += " "
                    first = False
                else:
                    script_asm += ", "
                script_asm += script_label + str(id)
            script_asm += "\n" #extra newline?

            result = insert_asm(start_address, scripts_label, text_asm=script_asm, end_address=start_address + total_size)
            if result:
                #reset everything
                #analyze_incbins.reset_incbins()
                asm = None
                incbin_lines = []
                processed_incbins = {}
                analyze_incbins.asm = None
                analyze_incbins.incbin_lines = []
                analyze_incbins.processed_incbins = {}
        
                #reload
                load_asm()
                isolate_incbins()
                process_incbins()
            else:
                print "trouble inserting map script pointer list"
                print script_asm
                sys.exit(0)