Skip to content

Commit

Permalink
give hints, if fns and asm files are not valid
Browse files Browse the repository at this point in the history
  • Loading branch information
mchlnix committed May 17, 2024
1 parent bc20ee8 commit adff756
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
8 changes: 4 additions & 4 deletions foundry/gui/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()

Expand All @@ -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:
Expand Down
62 changes: 49 additions & 13 deletions foundry/gui/dialogs/fns_asm_load_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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

Expand All @@ -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)

Expand Down

0 comments on commit adff756

Please sign in to comment.