Skip to content

Commit

Permalink
Merge pull request #54 from AnonymousRandomPerson/main
Browse files Browse the repository at this point in the history
Fixed more pmdsky-debug sync issues
  • Loading branch information
AnonymousRandomPerson authored Dec 29, 2023
2 parents 4fbae42 + 03c0ba3 commit a417962
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions tools/sync_pmdsky_debug/sync_to_pmdsky_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -102,22 +108,30 @@ 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
symbol_preexisting = True
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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a417962

Please sign in to comment.