diff options
author | Bryan Bishop <kanzure@gmail.com> | 2013-09-02 12:24:39 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2013-09-02 12:33:03 -0500 |
commit | 5b02c020f4784ba570f7da73a9835f5e984b8a90 (patch) | |
tree | 0ab6294cad1eaba379dad95d718312bda530b22b | |
parent | 2cf0cfa34d008d3f845082a86ec037311570396b (diff) |
make wram.py use config for paths
The WRAMProcessor class handles reading all constants and labels, which
are then used in gbz80disasm.
-rw-r--r-- | pokemontools/gbz80disasm.py | 13 | ||||
-rw-r--r-- | pokemontools/wram.py | 139 |
2 files changed, 98 insertions, 54 deletions
diff --git a/pokemontools/gbz80disasm.py b/pokemontools/gbz80disasm.py index 7499982..7b629a9 100644 --- a/pokemontools/gbz80disasm.py +++ b/pokemontools/gbz80disasm.py @@ -7,7 +7,12 @@ from ctypes import c_int8 import random import json -from wram import * +import config +import wram + +conf = config.Config() +wramp = wram.WRAMProcessor(conf) +wramp.initialize() # New versions of json don't have read anymore. if not hasattr(json, "read"): @@ -593,9 +598,9 @@ def find_label(local_address, bank_id=0): if get_local_address(label_entry["address"]) == local_address: if label_entry["bank"] == bank_id or label_entry["bank"] == 0: return label_entry["label"] - if local_address in wram_labels.keys(): - return wram_labels[local_address][-1] - for constants in [gbhw_constants, hram_constants]: + if local_address in wramp.wram_labels.keys(): + return wramp.wram_labels[local_address][-1] + for constants in [wramp.gbhw_constants, wramp.hram_constants]: if local_address in constants.keys() and local_address >= 0xff00: return constants[local_address] return None diff --git a/pokemontools/wram.py b/pokemontools/wram.py index 5a5fa75..60001aa 100644 --- a/pokemontools/wram.py +++ b/pokemontools/wram.py @@ -4,7 +4,15 @@ RGBDS BSS section and constant parsing. """ import os -path = os.path.dirname(os.path.abspath(__file__)) + +def make_wram_labels(wram_sections): + wram_labels = {} + for section in wram_sections: + for label in section['labels']: + if label['address'] not in wram_labels.keys(): + wram_labels[label['address']] = [] + wram_labels[label['address']] += [label['label']] + return wram_labels def read_bss_sections(bss): sections = [] @@ -55,34 +63,6 @@ def read_bss_sections(bss): sections.append(section) return sections -def read_wram_sections(): - """ - Opens the wram file and calls read_bss_sections. - """ - wram_content = None - wram_file_path = os.path.join(os.path.dirname(path), 'wram.asm') - try: - wram_file_handler = open(wram_file_path, 'r') - except IOError as exception: - wram_content = [""] - else: - wram_content = wram_file_handler.readlines() - wram_sections = read_bss_sections(wram_content) - return wram_sections - -wram_sections = read_wram_sections() - -def make_wram_labels(wram_sections): - wram_labels = {} - for section in wram_sections: - for label in section['labels']: - if label['address'] not in wram_labels.keys(): - wram_labels[label['address']] = [] - wram_labels[label['address']] += [label['label']] - return wram_labels - -wram_labels = make_wram_labels(wram_sections) - def constants_to_dict(constants): return dict((eval(constant[constant.find('EQU')+3:constant.find(';')].replace('$','0x')), constant[:constant.find('EQU')].strip()) for constant in constants) @@ -95,31 +75,90 @@ def read_constants(filepath): """ Load lines from a file and call scrape_constants. """ - try: - file_handler = open(filepath, "r") - except IOError as exception: - lines = [""] - else: + lines = None + + with open(filepath, "r") as file_handler: lines = file_handler.readlines() + constants = scrape_constants(lines) return constants -def read_hram_constants(): - """ - Load constants from hram.asm. - """ - hram_path = os.path.join(os.path.dirname(path), 'hram.asm') - return read_constants(hram_path) - -# TODO: get rid of this global -hram_constants = read_hram_constants() - -def read_gbhw_constants(): +class WRAMProcessor(object): """ - Load constants from gbhw.asm. + RGBDS BSS section and constant parsing. """ - gbhw_path = os.path.join(os.path.dirname(path), 'gbhw.asm') - return read_constants(gbhw_path) -# TODO: get rid of this global -gbhw_constants = read_gbhw_constants() + def __init__(self, config): + """ + Setup for WRAM parsing. + """ + self.config = config + + self.paths = {} + self.paths["wram"] = os.path.join(self.config.path, "wram.asm") + self.paths["hram"] = os.path.join(self.config.path, "hram.asm") + self.paths["gbhw"] = os.path.join(self.config.path, "gbhw.asm") + + def initialize(self): + """ + Read constants. + """ + self.setup_wram_sections() + self.setup_wram_labels() + self.setup_hram_constants() + self.setup_gbhw_constants() + + def read_wram_sections(self): + """ + Opens the wram file and calls read_bss_sections. + """ + wram_content = None + wram_file_path = self.paths["wram"] + + with open(wram_file_path, "r") as wram: + wram_content = wram.readlines() + + wram_sections = read_bss_sections(wram_content) + return wram_sections + + def setup_wram_sections(self): + """ + Call read_wram_sections and set a variable. + """ + self.wram_sections = self.read_wram_sections() + return self.wram_sections + + def setup_wram_labels(self): + """ + Make wram labels based on self.wram_sections as input. + """ + self.wram_labels = make_wram_labels(self.wram_sections) + return self.wram_labels + + def read_hram_constants(self): + """ + Read constants from hram.asm using read_constants. + """ + hram_constants = read_constants(self.paths["hram"]) + return hram_constants + + def setup_hram_constants(self): + """ + Call read_hram_constants and set a variable. + """ + self.hram_constants = self.read_hram_constants() + return self.hram_constants + + def read_gbhw_constants(self): + """ + Read constants from gbhw.asm using read_constants. + """ + gbhw_constants = read_constants(self.paths["gbhw"]) + return gbhw_constants + + def setup_gbhw_constants(self): + """ + Call read_gbhw_constants and set a variable. + """ + self.gbhw_constants = self.read_gbhw_constants() + return self.gbhw_constants |