ref: ff60685f4e08699ba9655bbdd9773b6f4a8d9140
parent: 20c54ccd583d2f847e6f264d7ced057f652dd095
author: Bryan Bishop <[email protected]>
date: Sat Jan 7 10:13:24 EST 2012
text analysis and insertion code (TX_FAR targets only) hg-commit-id: e4c007ad1c11
--- a/extras/analyze_incbins.py
+++ b/extras/analyze_incbins.py
@@ -204,6 +204,32 @@
print "Inserting map id=" + str(map_id)
wrapper_insert_map_header_asm(map_id)
+def reset_incbins():
+ "reset asm before inserting another diff"
+ asm = None
+ incbin_lines = []
+ processed_incbins = {}
+ load_asm()
+ isolate_incbins()
+ process_incbins()
+
+def apply_diff(diff):
+ print "... Applying diff."
+
+ #write the diff to a file
+ fh = open("temp.patch", "w")
+ fh.write(diff)
+ fh.close()
+
+ #apply the patch
+ os.system("patch ../pokered.asm temp.patch")
+
+ #remove the patch
+ os.system("rm temp.patch")
+
+ #confirm it's working
+ subprocess.check_call("cd ../; make clean; LC_CTYPE=UTF-8 make", shell=True)
+
if __name__ == "__main__":
#load map headers
load_rom()
--- /dev/null
+++ b/extras/insert_texts.py
@@ -1,0 +1,118 @@
+#!/usr/bin/python2.7
+#author: Bryan Bishop <[email protected]>
+#date: 2012-01-07
+#insert TX_FAR targets into pokered.asm
+import extract_maps
+from analyze_texts import analyze_texts
+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
+import analyze_incbins
+import os, sys
+import subprocess
+spacing = " "
+tx_fars = None
+
+def find_tx_far_entry(map_id, text_id):
+ for tx_far_line in tx_fars:
+ if tx_far_line[0] == map_id and tx_far_line[1] == text_id:
+ return tx_far_line
+
+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_all_tx_fars():
+ for tx_far in tx_fars:
+ map_id = tx_far[0]
+ text_id = tx_far[1]
+ #if map_id <= 185: continue #i'm just trying to get it going faster
+
+ insert_tx_far(map_id, text_id, tx_far_line=tx_far)
+
+ reset_incbins()
+ analyze_incbins.reset_incbins()
+ asm = None
+ incbin_lines = []
+ processed_incbins = {}
+ analyze_incbins.asm = None
+ analyze_incbins.incbin_lines = []
+ analyze_incbins.processed_incbins = {}
+
+ load_asm()
+ isolate_incbins()
+ process_incbins()
+
+if __name__ == "__main__":
+ #load map headers and object data
+ extract_maps.load_rom()
+ extract_maps.load_map_pointers()
+ extract_maps.read_all_map_headers()
+
+ #load texts (these two have different formats)
+ all_texts = pretty_map_headers.analyze_texts.analyze_texts()
+ pretty_map_headers.all_texts = all_texts
+ tx_fars = pretty_map_headers.find_all_tx_fars()
+
+ #load incbins
+ reset_incbins()
+
+ #insert _ViridianCityText10
+ #insert_tx_far(1, 10)
+
+ insert_all_tx_fars()
+
+ #just me testing a pokemart sign duplicate
+ #insert_tx_far(3, 14)
+
--- a/extras/pretty_map_headers.py
+++ b/extras/pretty_map_headers.py
@@ -633,7 +633,7 @@
def find_all_tx_fars():
global all_texts
- tx_fars = [] #[map_id, text_id, text_pointer, tx_far_pointer, tx_far_start_address, TX_FAR]
+ tx_fars = [] #[map_id, text_id, text_pointer, tx_far_pointer, TX_FAR]
for map_id in all_texts:
map2 = all_texts[map_id]
for text_id in map2.keys():
@@ -643,10 +643,14 @@
if "TX_FAR" in command.keys():
TX_FAR = command["TX_FAR"]
if TX_FAR[0]["type"] == 0x0:
- tx_fars.append([map_id, text_id, analyze_texts.get_text_pointer(int(extract_maps.map_headers[map_id]["texts_pointer"], 16), text_id), TX_FAR[0]["start_address"], TX_FAR])
+ tx_fars.append([map_id, text_id, analyze_texts.get_text_pointer(int(extract_maps.map_headers[map_id]["texts_pointer"], 16), text_id), command["pointer"], TX_FAR])
return tx_fars
-def print_tx_far(tx_far):
+def tx_far_label_maker(map_name, text_id):
+ label = "_" + map_name_cleaner(map_name, None)[:-2] + "Text" + str(text_id)
+ return label
+
+def tx_far_pretty_printer(tx_far):
"pretty output for a tx_far"
map_id = tx_far[0]
map2 = extract_maps.map_headers[map_id]
@@ -655,7 +659,7 @@
tx_far_start_address = tx_far[3]
text_far = tx_far[4]
lines = text_far[0]["lines"]
- label = "_" + map_name_cleaner(map2["name"], None)[:-2] + "Text" + str(text_id)
+ label = tx_far_label_maker(map2["name"], text_id)
#add the ending byte on the next line
#lines[len(lines.keys())+1] = [text_far[1]["type"]]
@@ -663,8 +667,8 @@
#add the ending byte to the last line- always seems $57
lines[len(lines.keys())-1].append(text_far[1]["type"])
- output = ""
- output += label + ":\n"
+ output = "\n"
+ output += label + ": ; " + hex(tx_far_start_address) + "\n"
first = True
for line_id in lines:
line = lines[line_id]
@@ -701,7 +705,8 @@
#if you want the ending byte on the last line
#if not (byte == 0x57 or byte == 0x50 or byte == 0x58):
- output += ", "
+ if not first_byte:
+ output += ", "
output += "$" + hex(byte)[2:]
was_byte = True
@@ -719,8 +724,7 @@
output += "\n"
- #TODO: add $50 to the end of this
- output += "\n"
+ #output += "\n"
return output
def print_all_headers():
@@ -753,6 +757,8 @@
#print out only the object data for pallet town (map 0)
#print object_data_pretty_printer(0)
+ #prepare to pretty print tx_fars
+ #first you must load all_texts
tx_fars = find_all_tx_fars()
for entry in tx_fars:
- print print_tx_far(entry)
+ print tx_far_pretty_printer(entry)
--- a/textpre.awk
+++ b/textpre.awk
@@ -287,6 +287,7 @@
substr(f,1,1) == "t" ||
substr(f,1,1) == "s" ||
substr(f,1,1) == "v" ||
+ substr(f,1,1) == "d" ||
substr(f,1,1) == "l") {
c = c substr(f,1,1)
f = substr(f,2,length(f) - 1)