From 03c0ba33be956997f086b076e75adb0f798a7882 Mon Sep 17 00:00:00 2001 From: AnonymousRandomPerson Date: Thu, 28 Dec 2023 21:10:40 -0600 Subject: [PATCH] Fixed more pmdsky-debug sync issues -Creating new header symbol now works when preceding symbol isn't in header. --- .../sync_pmdsky_debug/sync_to_pmdsky_debug.py | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/tools/sync_pmdsky_debug/sync_to_pmdsky_debug.py b/tools/sync_pmdsky_debug/sync_to_pmdsky_debug.py index 7944b6c7..73f00dbb 100644 --- a/tools/sync_pmdsky_debug/sync_to_pmdsky_debug.py +++ b/tools/sync_pmdsky_debug/sync_to_pmdsky_debug.py @@ -32,6 +32,12 @@ def read_symbol_array(symbol_path: str, symbol_type_key: str, yaml_manager: Yaml symbols_yaml: Dict[str, Any] = symbols_yaml_outer[list(symbols_yaml_outer.keys())[0]] return symbols_yaml[symbol_type_key] +def find_symbol_in_header(symbol_name: str, is_data: bool, header_contents: List[str]) -> int: + for i, line in enumerate(header_contents): + if is_data and re.search(fr' {symbol_name}[\[;]', line) or not is_data and f' {symbol_name}(' in line: + return i + return None + def sync_xmap_symbol(address: int, symbol: SymbolDetails, language: str, yaml_manager: YamlManager): if default_symbol_name.match(symbol.name): return @@ -102,13 +108,19 @@ def sync_xmap_symbol(address: int, symbol: SymbolDetails, language: str, yaml_ma return matching_symbol_entry = None - symbol_before = None symbol_array = read_symbol_array(symbol_path, symbol_type_key, yaml_manager) + # Add the symbol to the correspond header file. + base_symbol_path = base_symbol_path.replace('.yml', '.h') + header_path = symbol_path.replace(SYMBOLS_FOLDER, os.path.join('headers', symbol_type_key)).replace('.yml', '.h') + with open(header_path, 'r') as header_file: + header_contents = header_file.readlines() + # Find the existing symbol and replace its address, or make a new one if it isn't there. symbol_preexisting = False insert_index = None + target_header_line = None for i, symbol_entry in enumerate(symbol_array): if base_symbol_name == symbol_entry['name']: matching_symbol_entry = symbol_entry @@ -116,8 +128,10 @@ def sync_xmap_symbol(address: int, symbol: SymbolDetails, language: str, yaml_ma break else: # Keep track of the symbol directly before the target symbol. - # This will be used later as an anchor when appending to the header file. - symbol_before: str = symbol_entry['name'] + # This will be used as an anchor when appending to the header file. + symbol_header_line = find_symbol_in_header(symbol_entry['name'], symbol.is_data, header_contents) + if symbol_header_line is not None: + target_header_line = symbol_header_line if language_key in symbol_entry['address']: current_symbol_address: int | List[int] = symbol_entry['address'][language_key] if isinstance(current_symbol_address, list): @@ -171,32 +185,16 @@ def sync_xmap_symbol(address: int, symbol: SymbolDetails, language: str, yaml_ma if symbol_preexisting: return - # Add the symbol to the correspond header file. - base_symbol_path = base_symbol_path.replace('.yml', '.h') - header_path = symbol_path.replace(SYMBOLS_FOLDER, os.path.join('headers', symbol_type_key)).replace('.yml', '.h') - with open(header_path, 'r') as header_file: - header_contents = header_file.readlines() - # Look for the symbol that was immediately before the new symbol in the YAML. # The new symbol will be added directly after this anchor symbol. - target_line = None - if symbol_before is not None: - for i, line in enumerate(header_contents): - if symbol.is_data and re.search(fr' {symbol_before}[\[;]', line) or not symbol.is_data and f' {symbol_before}(' in line: - target_line = i - break - if target_line is None: - print(f'Could not find preceding symbol {symbol_before} to {base_symbol_name} in {base_symbol_path}') - return - - if target_line is None: + if target_header_line is None: if 'arm9' in header_path: for i, line in enumerate(header_contents): if line.startswith('// If declaring'): - target_line = i + target_header_line = i break else: - target_line = len(header_contents) - 2 + target_header_line = len(header_contents) - 2 # If the symbol is a data symbol, look through the ASM to find how much space the symbol takes. symbol_length = 0 @@ -244,7 +242,7 @@ def sync_xmap_symbol(address: int, symbol: SymbolDetails, language: str, yaml_ma else: symbol_header = f'void {base_symbol_name}(void);\n' - header_contents[target_line - 1] += symbol_header + header_contents[target_header_line - 1] += symbol_header with open(header_path, 'w') as header_file: header_file.writelines(header_contents)