shithub: pokered

Download patch

ref: 99fcdc90f60a133d760a6a3413c60205a2eda33c
parent: baa077c579fadf6d150e0634ef96dac877515868
author: Bryan Bishop <kanzure@gmail.com>
date: Tue Jan 10 13:04:00 EST 2012

added text_pretty_printer_at to analyze_texts

hg-commit-id: 04e647ab44f8


--- a/extras/analyze_texts.py
+++ b/extras/analyze_texts.py
@@ -5,7 +5,7 @@
 import extract_maps
 import analyze_incbins #for asm
 try:
-    from pretty_map_headers import map_name_cleaner
+    from pretty_map_headers import map_name_cleaner, txt_bytes, spacing, constant_abbreviation_bytes
 except Exception, exc: pass
 from operator import itemgetter
 import sys
@@ -383,6 +383,85 @@
                             print "missing $08 on map_id=" + str(map_id) + " text_id=" + str(text_id) + " line_id=" + str(line_id) + " at " + hex(current_line["start_address"])
     return missing_08s
 
+def text_pretty_printer_at(start_address, label="SomeLabel"):
+    commands = parse_text_script(start_address, None, None)
+    
+    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"
+
+    lines = commands[wanted_command]["lines"]
+
+    #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
+
+                #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
+
+
 if __name__ == "__main__":
     extract_maps.load_rom()
     extract_maps.load_map_pointers()
@@ -403,3 +482,5 @@
     print "total text commands: " + str(total_text_commands)
     print "total text scripts: " + str(should_be_total)
     print "missing 08s: " + str(missing_08s)
+
+    text_pretty_printer_at(0x800b1)    
--- a/extras/insert_texts.py
+++ b/extras/insert_texts.py
@@ -278,12 +278,13 @@
                 isolate_incbins()
                 process_incbins()
 
-def insert_08_asm(map_id, text_id):
+#TODO: if line_id !=0 then don't include the label?
+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][0]["start_address"]
+    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)
@@ -328,9 +329,9 @@
     for map_id in all_texts:
         for text_id in all_texts[map_id].keys():
             if 0 in all_texts[map_id][text_id].keys():
-                if "type" in all_texts[map_id][text_id][0].keys():
-                    if all_texts[map_id][text_id][0]["type"] == 0x8:
-                        all_08s.append([map_id, text_id])
+                for line_id in all_texts[map_id][text_id].keys():
+                    if all_texts[map_id][text_id][line_id]["type"] == 0x8:
+                        all_08s.append([map_id, text_id, line_id])
     return all_08s
 
 def insert_all_08s():
@@ -339,9 +340,10 @@
         map_id = the_08_line[0]
         if map_id <= 86: continue #speed things up
         text_id = the_08_line[1]
+        line_id = the_08_line[2]
 
         print "processing map_id=" + str(map_id) + " text_id=" + str(text_id)
-        insert_08_asm(map_id, text_id)
+        insert_08_asm(map_id, text_id, line_id)
         
         #reset everything
         analyze_incbins.reset_incbins()
@@ -424,7 +426,7 @@
     #insert_08_asm(83, 1)
     #insert_all_08s()
 
-    insert_asm(0x2f9e, "GetMonName")
+    insert_asm(0x19926, "VermilionCityText5_2")
 
     if len(failed_attempts) > 0:
         print "-- FAILED ATTEMPTS --"
--- a/extras/pretty_map_headers.py
+++ b/extras/pretty_map_headers.py
@@ -8,7 +8,7 @@
 import sprite_helper
 import random
 import string
-import analyze_texts #hopefully not a dependency loop
+#import analyze_texts #hopefully not a dependency loop
 
 base = 16
 spacing = "    "