start using classes instead of functions everywhere

This commit is contained in:
Bryan Bishop 2012-03-24 00:58:14 -05:00
parent 253c897bbd
commit d497a0a001

View File

@ -523,6 +523,7 @@ def clean_up_long_info(long_info):
return long_info
def command_debug_information(command_byte=None, map_group=None, map_id=None, address=0, info=None, long_info=None, pksv_name=None):
"used to help debug in parse_script_engine_script_at"
info1 = "parsing command byte " + hex(command_byte) + " for map " + \
str(map_group) + "." + str(map_id) + " at " + hex(address)
info1 += " pksv: " + str(pksv_name)
@ -530,81 +531,11 @@ def command_debug_information(command_byte=None, map_group=None, map_id=None, ad
#info1 += " long_info: " + long_info
return info1
def process_00_subcommands(start_address, end_address):
"""split this text up into multiple lines
based on subcommands ending each line"""
print "process_00_subcommands(" + hex(start_address) + ", " + hex(end_address) + ")"
lines = {}
subsection = rom[start_address:end_address]
line_count = 0
current_line = []
for pbyte in subsection:
byte = ord(pbyte)
current_line.append(byte)
if byte == 0x4f or byte == 0x51 or byte == 0x55:
lines[line_count] = current_line
current_line = []
line_count += 1
#don't forget the last line
lines[line_count] = current_line
line_count += 1
return lines
def parse_text_from_bytes(bytes):
"""assembles a string based on bytes looked up in the chars table"""
line = ""
for byte in bytes:
if type(byte) != int:
byte = ord(byte)
if byte in chars.keys():
line += chars[byte]
else:
print "byte not known: " + hex(byte)
return line
def parse_text_at(address, count=10):
"""returns a list of bytes from an address
see parse_text_at2 for pretty printing"""
return parse_text_from_bytes(rom_interval(address, count, strings=False))
def rom_text_at(address, count=10):
"""prints out raw text from the ROM
like for 0x112110"""
return "".join([chr(x) for x in rom_interval(address, count, strings=False)])
def parse_text_at2(address, count=10):
"""returns a string of text from an address
this does not handle text commands"""
output = ""
commands = process_00_subcommands(address, address+count)
for (line_id, line) in commands.items():
output += parse_text_from_bytes(line)
output += "\n"
return output
def find_all_text_pointers_in_script_engine_script(script, bank):
"""returns a list of text pointers
based on each script-engine script command"""
#TODO: recursively follow any jumps in the script
if script == None: return []
addresses = set()
for (k, command) in script.items():
if command["type"] == 0x4B:
addresses.add(command["pointer"])
elif command["type"] == 0x4C:
addresses.add(command["pointer"])
elif command["type"] == 0x51:
addresses.add(command["text_pointer"])
elif command["type"] == 0x53:
addresses.add(command["text_pointer"])
elif command["type"] == 0x64:
addresses.add(command["won_pointer"])
addresses.add(command["lost_pointer"])
return addresses
def find_text_addresses():
class TextScript():
"a text is a sequence of commands different from a script-engine script"
def to_asm(self): pass
@staticmethod
def find_addresses():
"""returns a list of text pointers
useful for testing parse_text_engine_script_at
@ -692,11 +623,8 @@ def find_text_addresses():
texts2 = find_all_text_pointers_in_script_engine_script(script2, trainer_bank)
addresses.update(texts2)
return addresses
def old_parse_text_engine_script_at(address, map_group=None, map_id=None, debug=True):
return {}
def parse_text_engine_script_at(address, map_group=None, map_id=None, debug=True, show=True):
@staticmethod
def parse_text_at(address, map_group=None, map_id=None, debug=True, show=True):
"""parses a text-engine script ("in-text scripts")
http://hax.iimarck.us/files/scriptingcodes_eng.htm#InText
@ -907,6 +835,105 @@ def parse_text_engine_script_at(address, map_group=None, map_id=None, debug=True
# sys.exit()
return commands
def parse_text_engine_script_at(address, map_group=None, map_id=None, debug=True, show=True):
"""parses a text-engine script ("in-text scripts")
http://hax.iimarck.us/files/scriptingcodes_eng.htm#InText
see parse_text_at2, parse_text_at, and process_00_subcommands
"""
return TextScript.parse_text_at(address, map_group=map_group, map_id=map_id, debug=debug, show=show)
def find_text_addresses():
"""returns a list of text pointers
useful for testing parse_text_engine_script_at"""
return TextScript.find_addresses()
class EncodedText():
"""a sequence of bytes that, when decoded, represent readable text
based on the chars table from textpre.py and other places"""
def to_asm(self): pass
@staticmethod
def process_00_subcommands(start_address, end_address):
"""split this text up into multiple lines
based on subcommands ending each line"""
print "process_00_subcommands(" + hex(start_address) + ", " + hex(end_address) + ")"
lines = {}
subsection = rom[start_address:end_address]
line_count = 0
current_line = []
for pbyte in subsection:
byte = ord(pbyte)
current_line.append(byte)
if byte == 0x4f or byte == 0x51 or byte == 0x55:
lines[line_count] = current_line
current_line = []
line_count += 1
#don't forget the last line
lines[line_count] = current_line
line_count += 1
return lines
@staticmethod
def from_bytes(bytes):
"""assembles a string based on bytes looked up in the chars table"""
line = ""
for byte in bytes:
if type(byte) != int:
byte = ord(byte)
if byte in chars.keys():
line += chars[byte]
else:
print "byte not known: " + hex(byte)
return line
@staticmethod
def parse_text_at(address, count=10):
"""returns a string of text from an address
this does not handle text commands"""
output = ""
commands = process_00_subcommands(address, address+count)
for (line_id, line) in commands.items():
output += parse_text_from_bytes(line)
output += "\n"
return output
def process_00_subcommands(start_address, end_address):
"""split this text up into multiple lines
based on subcommands ending each line"""
return EncodedText.process_00_subcommands(start_address, end_address)
def parse_text_from_bytes(bytes):
"""assembles a string based on bytes looked up in the chars table"""
return EncodedText.from_bytes(bytes)
def parse_text_at(address, count=10):
"""returns a list of bytes from an address
see parse_text_at2 for pretty printing"""
return parse_text_from_bytes(rom_interval(address, count, strings=False))
def parse_text_at2(address, count=10):
"""returns a string of text from an address
this does not handle text commands"""
return EncodedText.parse_text_at(address, count)
def rom_text_at(address, count=10):
"""prints out raw text from the ROM
like for 0x112110"""
return "".join([chr(x) for x in rom_interval(address, count, strings=False)])
def find_all_text_pointers_in_script_engine_script(script, bank):
"""returns a list of text pointers
based on each script-engine script command"""
#TODO: recursively follow any jumps in the script
if script == None: return []
addresses = set()
for (k, command) in script.items():
if command["type"] == 0x4B:
addresses.add(command["pointer"])
elif command["type"] == 0x4C:
addresses.add(command["pointer"])
elif command["type"] == 0x51:
addresses.add(command["text_pointer"])
elif command["type"] == 0x53:
addresses.add(command["text_pointer"])
elif command["type"] == 0x64:
addresses.add(command["won_pointer"])
addresses.add(command["lost_pointer"])
return addresses
def translate_command_byte(crystal=None, gold=None):
"""takes a command byte from either crystal or gold
@ -1242,7 +1269,6 @@ def pretty_print_pksv_no_names():
for address in addresses:
print " " + hex(address)
recursive_scripts = set([])
def rec_parse_script_engine_script_at(address, origin=None):
"""this is called in parse_script_engine_script_at for recursion