From adff75618292b800410d84d19913c03f53c7c9ae Mon Sep 17 00:00:00 2001 From: Michael Nix Date: Fri, 17 May 2024 21:52:12 +0900 Subject: [PATCH] give hints, if fns and asm files are not valid --- foundry/gui/asm.py | 8 +-- foundry/gui/dialogs/fns_asm_load_dialog.py | 62 +++++++++++++++++----- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/foundry/gui/asm.py b/foundry/gui/asm.py index 2e08ca51..f28d2a0f 100644 --- a/foundry/gui/asm.py +++ b/foundry/gui/asm.py @@ -297,7 +297,7 @@ def make_fns_file_absolute(fns_file: Path, asm_file: Path) -> Path: if ".bank" in line: state = "reading" - prgs = [] + prg_banks = [] for i in range(VANILLA_PRG_BANK_COUNT): prg_name = f"prg{str(i).rjust(3, '0')}.asm" @@ -307,7 +307,7 @@ def make_fns_file_absolute(fns_file: Path, asm_file: Path) -> Path: if not path.exists(): raise ValueError(f"Couldn't find {path}. Make sure your smb3.asm is in the assembly directory.") - prgs.append(path.read_text()) + prg_banks.append(path.read_text()) assert fns_file.exists() @@ -327,8 +327,8 @@ def make_fns_file_absolute(fns_file: Path, asm_file: Path) -> Path: relative_offset_int = int(relative_offset.strip()[1:], 16) - for prg_index, prg in enumerate(prgs): - if f"\n{label_name}:" in prg or prg.startswith(f"{label_name}:"): + for prg_index, prg_bank in enumerate(prg_banks): + if f"\n{label_name}:" in prg_bank or prg_bank.startswith(f"{label_name}:"): relative_offset_int -= prg_offsets[prg_index] if relative_offset_int < 0: diff --git a/foundry/gui/dialogs/fns_asm_load_dialog.py b/foundry/gui/dialogs/fns_asm_load_dialog.py index b8dedad7..44e5dbd3 100644 --- a/foundry/gui/dialogs/fns_asm_load_dialog.py +++ b/foundry/gui/dialogs/fns_asm_load_dialog.py @@ -107,14 +107,26 @@ def __init__(self, parent, cur_fns_file: str = "", current_asm_file: str = ""): self._asm_line_edit.setText(self.asm_path) def _check_fns_file(self, path: str): - new_path = Path(path) + self._fns_is_good = False + self._check_ok_button() - self._fns_is_good = new_path.is_file() and self._check_fns_content(new_path) + new_path = Path(path) - if self._fns_is_good: - self._fns_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_DialogYesButton)) - else: + if not new_path.is_file(): self._fns_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_MessageBoxCritical)) + self._fns_check_icon.setToolTip("Given path is not a file/does not exist.") + return + + try: + self._check_fns_content(new_path) + except Exception as e: + self._fns_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_MessageBoxWarning)) + self._fns_check_icon.setToolTip(str(e)) + return + + self._fns_is_good = True + self._fns_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_DialogYesButton)) + self._fns_check_icon.setToolTip("") self._check_ok_button() @@ -129,10 +141,14 @@ def _check_fns_content(new_path: Path): if not line.strip(): continue - name, value = line.split("=") + try: + name, value = line.split("=") + + if not value.strip().startswith("$"): + raise ValueError() - if not value.strip().startswith("$"): - return False + except ValueError: + raise ValueError("Didn't find lines in the form of 'name = $1234'. File might be wrongly formatted.") lines_to_check -= 1 @@ -142,17 +158,37 @@ def _check_fns_content(new_path: Path): return True def _check_asm_file(self, path: str): - new_path = Path(path) + self._asm_is_good = False + self._check_ok_button() - self._asm_is_good = new_path.is_file() + new_path = Path(path) - if self._asm_is_good: - self._asm_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_DialogYesButton)) - else: + if not new_path.is_file(): + self._asm_check_icon.setToolTip("Given path is not a file/does not exist.") self._asm_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_MessageBoxCritical)) + return + + try: + self._check_asm_location(new_path) + except Exception as e: + self._asm_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_MessageBoxWarning)) + self._asm_check_icon.setToolTip(str(e)) + return + + self._asm_check_icon.setPixmap(self.style().standardPixmap(QStyle.StandardPixmap.SP_DialogYesButton)) + self._asm_check_icon.setToolTip("") + + self._asm_is_good = True self._check_ok_button() + @staticmethod + def _check_asm_location(path: Path): + prg_path = path.parent / "PRG" / "prg000.asm" + + if not prg_path.exists(): + raise ValueError(f"Couldn't find {prg_path}. Make sure your smb3.asm is in the assembly directory.") + def _check_ok_button(self): self._ok_button.setEnabled(self._fns_is_good and self._asm_is_good)