shithub: pokered

Download patch

ref: 3075fabe2622b1107debc2e28239de6d785d8ba5
parent: 28d21c8e19762bbd163e9bacdc3633ec9aeb37bf
author: Bryan Bishop <kanzure@gmail.com>
date: Wed Jan 11 18:08:46 EST 2012

more automation to assist in text insertion

hg-commit-id: e631a0e3c275


--- a/extras/analyze_texts.py
+++ b/extras/analyze_texts.py
@@ -385,81 +385,91 @@
 
 def text_pretty_printer_at(start_address, label="SomeLabel"):
     commands = parse_text_script(start_address, None, None)
+    needs_to_begin_with_0 = False
     
     wanted_command = None
-    for command_id in commands:
-        command = commands[command_id]
-        if command["type"] == 0:
-            wanted_command = command_id
+    if needs_to_begin_with_0:
+        wanted_command = None
+        for command_id in commands:
+            command = commands[command_id]
+            if command["type"] == 0:
+                wanted_command = command_id
     
-    if wanted_command == None:
-        raise "error: address did not start with a $0 text"
+        if wanted_command == None:
+            raise "error: address did not start with a $0 text"
+    
+    #start with zero please
+    byte_count = 0
 
-    lines = commands[wanted_command]["lines"]
+    first_line = True
+    for this_command in commands.keys():
+        lines = commands[this_command]["lines"]
+    
+        #add the ending byte to the last line- always seems $57
+        lines[len(lines.keys())-1].append(commands[1]["type"])
+    
+        if first_line:
+            output  = "\n"
+            output += label + ": ; " + hex(start_address) + "\n"
+            first_line = False
 
-    #add the ending byte to the last line- always seems $57
-    lines[len(lines.keys())-1].append(commands[1]["type"])
-
-    output  = "\n"
-    output += label + ": ; " + hex(start_address) + "\n"
-    first = True
-    for line_id in lines:
-        line = lines[line_id]
-        output += spacing + "db "
-        if first:
-            output += "$0, "
-            first = False
-     
-        quotes_open = False
-        first_byte = True
-        was_byte = False
-        byte_count = 0
-        for byte in line:
-            if byte in txt_bytes:
-                if not quotes_open and not first_byte: #start text
-                    output += ", \""
-                    quotes_open = True
-                    first_byte = False
-                if not quotes_open and first_byte: #start text
-                    output += "\""
-                    quotes_open = True
-                output += txt_bytes[byte]
-            elif byte in constant_abbreviation_bytes:
-                if quotes_open:
-                    output += "\""
-                    quotes_open = False
-                if not first_byte:
-                    output += ", "
-                output += constant_abbreviation_bytes[byte]
-            else:
-                if quotes_open:
-                    output += "\""
-                    quotes_open = False
+        first = True #first byte
+        for line_id in lines:
+            line = lines[line_id]
+            output += spacing + "db "
+            if first and needs_to_begin_with_0:
+                output += "$0, "
+                first = False
+         
+            quotes_open = False
+            first_byte = True
+            was_byte = False
+            for byte in line:
+                if byte in txt_bytes:
+                    if not quotes_open and not first_byte: #start text
+                        output += ", \""
+                        quotes_open = True
+                        first_byte = False
+                    if not quotes_open and first_byte: #start text
+                        output += "\""
+                        quotes_open = True
+                    output += txt_bytes[byte]
+                elif byte in constant_abbreviation_bytes:
+                    if quotes_open:
+                        output += "\""
+                        quotes_open = False
+                    if not first_byte:
+                        output += ", "
+                    output += constant_abbreviation_bytes[byte]
+                else:
+                    if quotes_open:
+                        output += "\""
+                        quotes_open = False
+    
+                    #if you want the ending byte on the last line
+                    #if not (byte == 0x57 or byte == 0x50 or byte == 0x58):
+                    if not first_byte:
+                        output += ", "
+    
+                    output += "$" + hex(byte)[2:]
+                    was_byte = True
+    
+                    #add a comma unless it's the end of the line
+                    #if byte_count+1 != len(line):
+                    #    output += ", "
+    
+                first_byte = False
+                byte_count += 1
+            #close final quotes
+            if quotes_open:
+                output += "\""
+                quotes_open = False
+    
+            output += "\n"
 
-                #if you want the ending byte on the last line
-                #if not (byte == 0x57 or byte == 0x50 or byte == 0x58):
-                if not first_byte:
-                    output += ", "
-
-                output += "$" + hex(byte)[2:]
-                was_byte = True
-
-                #add a comma unless it's the end of the line
-                #if byte_count+1 != len(line):
-                #    output += ", "
-
-            first_byte = False
-            byte_count += 1
-        #close final quotes
-        if quotes_open:
-            output += "\""
-            quotes_open = False
-
-        output += "\n"
-
     #output += "\n"
     print output
-    return output
+    return (output, byte_count)
 
 def is_label_in_asm(label):
     for line in analyze_incbins.asm:
--- a/extras/insert_texts.py
+++ b/extras/insert_texts.py
@@ -3,7 +3,7 @@
 #date: 2012-01-07
 #insert TX_FAR targets into pokered.asm
 import extract_maps
-from analyze_texts import analyze_texts
+from analyze_texts import analyze_texts, text_pretty_printer_at
 from pretty_map_headers import map_name_cleaner, make_text_label, map_constants, find_all_tx_fars, tx_far_pretty_printer, tx_far_label_maker
 import pretty_map_headers
 from analyze_incbins import asm, offset_to_pointer, find_incbin_to_replace_for, split_incbin_line_into_three, generate_diff_insert, load_asm, isolate_incbins, process_incbins, reset_incbins, apply_diff
@@ -389,6 +389,39 @@
     print diff
     result = apply_diff(diff, try_fixing=False)
 
+def insert_text(address, label):
+    "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
+
+    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
+    #apply_diff(diff)
+
 if __name__ == "__main__":
     #load map headers and object data
     extract_maps.load_rom()
@@ -428,7 +461,7 @@
 
     #insert_asm(0x758df, "CinnabarGymText1")
 
-    #insert_text_label_tx_far(95, 1)
+    #insert_text_label_tx_far(91, 1)
     missed_17s = [] #[[95, 1], [95, 2], [96, 1], [97, 1], [99, 1], [99, 2], [99, 3], [100, 1], [100, 2], [100, 3], [100, 4], [100, 5], [100, 6], [124, 8], [124, 10], [124, 12], [124, 16], [124, 17], [133, 3], [139, 1], [139, 2], [139, 3], [141, 2], [141, 3], [154, 2], [154, 3], [169, 4], [171, 2], [171, 3], [174, 2], [174, 3], [176, 4], [176, 5], [182, 3], [215, 5], [91, 2], [91, 3], [124, 8], [124, 10], [124, 12], [124, 16], [124, 17], [139, 1], [139, 2], [139, 3], [141, 2], [169, 4], [171, 2], [174, 2], [176, 4], [176, 5]]
     for missed_17 in missed_17s:
         insert_text_label_tx_far(missed_17[0], missed_17[1])
@@ -443,6 +476,9 @@
         load_asm()
         isolate_incbins()
         process_incbins()
+
+    #insert_text(0x44276, "ViridianPokeCenterText4")
+    insert_text(0x2461, "VermilionMartText1")
 
     if len(failed_attempts) > 0:
         print "-- FAILED ATTEMPTS --"