def insert_base_stats(id): insert_asm = base_data_pretty_printer(id) address = 0x383de + (28 * (id)) line_number = find_incbin_to_replace_for(address) label = get_mon_name(id).title() + "BaseStats" if line_number == None: print "skipping, already inserted at " + hex(address) return #also do a name check if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping " + label + " because it is already in use.." return newlines = split_incbin_line_into_three(line_number, address, 28) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = insert_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print diff apply_diff(diff, try_fixing=False, do_compile=False)
def insert_base_stats(id): insert_asm = base_data_pretty_printer(id) address = 0x383de + (28 * (id)) line_number = find_incbin_to_replace_for(address) label = get_mon_name(id).title() + "BaseStats" if line_number == None: print "skipping, already inserted at " + hex(address) return #also do a name check if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping " + label + " because it is already in use.." return newlines = split_incbin_line_into_three(line_number, address, 28 ) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = insert_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print diff apply_diff(diff, try_fixing=False, do_compile=False)
def insert_tx_far(map_id, text_id, tx_far_line=None): "inserts a tx_far" global tx_fars if tx_far_line == None: tx_far_line = find_tx_far_entry(map_id, text_id) text_pointer = tx_far_line[2] start_address = tx_far_line[3] tx_far_object = tx_far_line[4] end_address = tx_far_object[1]["end_address"] + 1 #the end byte; +1 because of a bug somewhere :( line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping tx_far for map_id=" + str(map_id) + " text_id=" + str(text_id) + " text_pointer=" + hex(text_pointer) + " tx_far_start_address=" + hex(start_address) return #also do a name check label = tx_far_label_maker(extract_maps.map_headers[map_id]["name"], text_id) if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping tx_far for map_id=" + str(map_id) + " text_id=" + str(text_id) + " text_pointer=" + hex(text_pointer) + " tx_far_start_address=" + hex(start_address) return newlines = split_incbin_line_into_three(line_number, start_address, end_address - start_address) tx_far_asm = tx_far_pretty_printer(tx_far_line) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = tx_far_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") #where does this keep coming from?? #signs are dumb; cluster the labels please if "\"needs fulfilled!\", $55" in newlines: newlines = "\n" + label + ": " line_number += 1 if ("STRENGTH to move!" in newlines) or ("it the way it is." in newlines): newlines = "\n" + label + ": " line_number += 1 if "@\"" in newlines and not "@@\"" in newlines: newlines = newlines.replace("@", "@@") #Char52 doesn't work yet? oh well newlines = newlines.replace("Char52", "$52") diff = generate_diff_insert(line_number, newlines) print "working on map_id=" + str(map_id) + " text_id=" + str(text_id) print diff apply_diff(diff)
def insert_text_label_tx_far(map_id, text_id): if map_id in extract_maps.bad_maps: print "bad map id=" + str(map_id) return map2 = extract_maps.map_headers[map_id] if map2["texts"][text_id] == {0: {}}: return None base_label = map_name_cleaner(map2["name"], None)[:-2] label = base_label + "Text" + str(text_id) target_label = "_" + label start_address = map2["texts"][text_id][0]["start_address"] if 0x4000 <= start_address <= 0x7fff: start_address = extract_maps.calculate_pointer(start_address, int(map2["bank"],16)) include_byte = False print map2["texts"][text_id] if "type" in map2["texts"][text_id][1].keys(): if map2["texts"][text_id][1]["type"] == 0x50: include_byte = True tx_far_asm = txt_to_tx_far_pretty_printer(start_address, label, target_label, include_byte=include_byte) line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping text label that calls TX_FAR for map_id=" + str(map_id) + " text_id=" + str(text_id) + " because the address is taken " + hex(start_address) return #also do a name check if 1 < ("\n".join(analyze_incbins.asm)).count("\n" + label + ":"): print "skipping text label that calls TX_FAR for map_id=" + str(map_id) + " text_id" + str(text_id) + " because the label is already used (" + label + ":)" return extra = 0 if include_byte: extra += 1 newlines = split_incbin_line_into_three(line_number, start_address, 4 + extra ) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = tx_far_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print "working on map_id=" + str(map_id) + " text_id=" + str(text_id) print diff apply_diff(diff)
def insert_texts_label(map_id): #if not all_texts_are_tx_fars(map_id): return None map2 = extract_maps.map_headers[map_id] base_label = map_name_cleaner(map2["name"], None)[:-2] label = base_label + "Texts" texts_pointer = int(map2["texts_pointer"], 16) insert_asm = texts_label_pretty_printer(map_id) line_number = find_incbin_to_replace_for(texts_pointer) if line_number == None: print "skipping texts label for map_id=" + str( map_id) + " texts_pointer=" + hex( texts_pointer) + " because the address is taken" return #also do a name check if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping texts label for map_id=" + str( map_id) + " texts_pointer=" + hex( texts_pointer) + " because the label is already used" return newlines = split_incbin_line_into_three(line_number, texts_pointer, len(map2["referenced_texts"]) * 2) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = insert_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print "working on map_id=" + str(map_id) + " texts_pointer=" + hex( texts_pointer) print diff apply_diff(diff)
def insert_08_asm(map_id, text_id, line_id=0): map2 = extract_maps.map_headers[map_id] base_label = map_name_cleaner(map2["name"], None)[:-2] label = base_label + "Text" + str(text_id) start_address = all_texts[map_id][text_id][line_id]["start_address"] (text_asm, end_address) = text_asm_pretty_printer(label, start_address) print "end address is: " + hex(end_address) #find where to insert the assembly line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping text label for a $08 on map_id=" + str( map_id) + " text_id=" + str( text_id) + " because the address is taken" return #also do a name check if 1 <= ("\n".join(analyze_incbins.asm)).count("\n" + label + ":"): print "skipping text label for a $08 on map_id=" + str( map_id) + " text_id=" + str( text_id ) + " because the label is already taken (" + label + ":)" return newlines = split_incbin_line_into_three(line_number, start_address, end_address - start_address) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = text_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print "working on map_id=" + str(map_id) + " text_id=" + str(text_id) print diff result = apply_diff(diff) if result == False: failed_attempts[len(failed_attempts.keys())] = { "map_id": map_id, "text_id": text_id }
def insert_texts_label(map_id): #if not all_texts_are_tx_fars(map_id): return None map2 = extract_maps.map_headers[map_id] base_label = map_name_cleaner(map2["name"], None)[:-2] label = base_label + "Texts" texts_pointer = int(map2["texts_pointer"], 16) insert_asm = texts_label_pretty_printer(map_id) line_number = find_incbin_to_replace_for(texts_pointer) if line_number == None: print "skipping texts label for map_id=" + str(map_id) + " texts_pointer=" + hex(texts_pointer) + " because the address is taken" return #also do a name check if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping texts label for map_id=" + str(map_id) + " texts_pointer=" + hex(texts_pointer) + " because the label is already used" return newlines = split_incbin_line_into_three(line_number, texts_pointer, len(map2["referenced_texts"])*2 ) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = insert_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print "working on map_id=" + str(map_id) + " texts_pointer=" + hex(texts_pointer) print diff apply_diff(diff)
def insert_text(address, label, apply=False, try_fixing=True): "inserts a text script (but not $8s)" start_address = address line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping text at " + hex( start_address) + " with address " + label return "skip" #another reason to skip is if the interval is 0 processed_incbin = analyze_incbins.processed_incbins[line_number] if processed_incbin["interval"] == 0: print "skipping text at " + hex( start_address ) + " with address " + label + " because the interval is 0" return "skip" text_asm, byte_count = text_pretty_printer_at(start_address, label) end_address = start_address + byte_count newlines = split_incbin_line_into_three(line_number, start_address, byte_count) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = text_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") #where does this keep coming from?? #Char52 doesn't work yet newlines = newlines.replace("Char52", "$52") diff = generate_diff_insert(line_number, newlines) print diff if apply: return apply_diff(diff, try_fixing=try_fixing) else: #simulate a successful insertion return True
def insert_08_asm(map_id, text_id, line_id=0): map2 = extract_maps.map_headers[map_id] base_label = map_name_cleaner(map2["name"], None)[:-2] label = base_label + "Text" + str(text_id) start_address = all_texts[map_id][text_id][line_id]["start_address"] (text_asm, end_address) = text_asm_pretty_printer(label, start_address) print "end address is: " + hex(end_address) #find where to insert the assembly line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping text label for a $08 on map_id=" + str(map_id) + " text_id=" + str(text_id) + " because the address is taken" return #also do a name check if 1 <= ("\n".join(analyze_incbins.asm)).count("\n" + label + ":"): print "skipping text label for a $08 on map_id=" + str(map_id) + " text_id=" + str(text_id) + " because the label is already taken (" + label + ":)" return newlines = split_incbin_line_into_three(line_number, start_address, end_address - start_address ) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = text_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print "working on map_id=" + str(map_id) + " text_id=" + str(text_id) print diff result = apply_diff(diff) if result == False: failed_attempts[len(failed_attempts.keys())] = {"map_id": map_id, "text_id": text_id}
def insert_text(address, label, apply=False, try_fixing=True): "inserts a text script (but not $8s)" start_address = address line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping text at " + hex(start_address) + " with address " + label return "skip" #another reason to skip is if the interval is 0 processed_incbin = analyze_incbins.processed_incbins[line_number] if processed_incbin["interval"] == 0: print "skipping text at " + hex(start_address) + " with address " + label + " because the interval is 0" return "skip" text_asm, byte_count = text_pretty_printer_at(start_address, label) end_address = start_address + byte_count newlines = split_incbin_line_into_three(line_number, start_address, byte_count) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = text_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") #where does this keep coming from?? #Char52 doesn't work yet newlines = newlines.replace("Char52", "$52") diff = generate_diff_insert(line_number, newlines) print diff if apply: return apply_diff(diff, try_fixing=try_fixing) else: #simulate a successful insertion return True
def insert_asm(start_address, label, text_asm=None, end_address=None): if text_asm == None and end_address == None: (text_asm, end_address) = text_asm_pretty_printer(label, start_address, include_08=False) print "end address is: " + hex(end_address) #find where to insert the assembly line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping asm because the address is taken" return False #name check if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping asm because the label is taken" return False newlines = split_incbin_line_into_three(line_number, start_address, end_address - start_address) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = text_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print diff result = apply_diff(diff, try_fixing=True) return True
def insert_asm(start_address, label, text_asm=None, end_address=None): if text_asm == None and end_address == None: (text_asm, end_address) = text_asm_pretty_printer(label, start_address, include_08=False) print "end address is: " + hex(end_address) #find where to insert the assembly line_number = find_incbin_to_replace_for(start_address) if line_number == None: print "skipping asm because the address is taken" return False #name check if (label + ":") in "\n".join(analyze_incbins.asm): print "skipping asm because the label is taken" return False newlines = split_incbin_line_into_three(line_number, start_address, end_address - start_address ) newlines = newlines.split("\n") if len(newlines) == 2: index = 0 #replace the 1st line with new content elif len(newlines) == 3: index = 1 #replace the 2nd line with new content newlines[index] = text_asm if len(newlines) == 3 and newlines[2][-2:] == "$0": #get rid of the last incbin line if it is only including 0 bytes del newlines[2] #note that this has to be done after adding in the new asm newlines = "\n".join(line for line in newlines) newlines = newlines.replace("$x", "$") diff = generate_diff_insert(line_number, newlines) print diff result = apply_diff(diff, try_fixing=True) return True