Skip to content

Commit

Permalink
show error messages at initialization in a message box when using Win…
Browse files Browse the repository at this point in the history
…dows
  • Loading branch information
qarlosh committed Oct 13, 2020
1 parent 6008d4a commit eb3430c
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions heroed/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
__version__ = "0.3.0"
__version__ = "0.3.1"

import os
import os.path
import ctypes
import shutil
import argparse
import contextlib

from heroed.editor import Editor
from heroed import hero
Expand All @@ -11,24 +14,56 @@
DEFAULT_MOD_NAME = "MY FIRST MOD"


@contextlib.contextmanager
def handle_init_exceptions(parser):
"""Handle exceptions during initialization. If
the OS is Windows, show any error in a UI dialog"""
try:
yield
except Exception as err:
if os.name == "nt":
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(
None,
"To use 'heroed' you must specify the 'hero.rom' file. "
+ "You can drag'n drop the rom file over heroed.exe from the file explorer, "
+ "or pass it as a parameter from the command line.\n"
+ "Remember that 'heroed' is a console application!\n\n"
+ ("Error:\n%s\n\n" % str(err))
+ ("%s" % usage),
"heroed error",
0,
)
raise


class ArgumentParserExcept(argparse.ArgumentParser):
def error(self, message):
"""raise exception instead of exiting
(3.9: ArgumentParser has exit_on_error...)
"""
raise argparse.ArgumentError(None, message)


def main_editor():
parser = argparse.ArgumentParser(
"heroed", description="HEROED - MSX H.E.R.O. Editor"
parser = ArgumentParserExcept(
"heroed",
description="HEROED - MSX H.E.R.O. Editor",
)
parser.add_argument(
"romfile", metavar="romfile", help="The MSX H.E.R.O. ROM file to edit"
)
parser.add_argument(
"-v", "--version", action="version", version=__version__
)
args = parser.parse_args()

if not os.path.isfile(args.romfile):
raise FileNotFoundError('"%s" file not found' % args.romfile)
with handle_init_exceptions(parser):
args = parser.parse_args()
if not os.path.isfile(args.romfile):
raise FileNotFoundError('"%s" file not found' % args.romfile)
editor = Editor(open(args.romfile, "r+b"))
ui = UI()

editor = Editor(open(args.romfile, "r+b"))

ui = UI()
ui.version = __version__
# fmt: off
if (message_0 := editor.read_title_screen_message(0)) ==\
Expand Down Expand Up @@ -110,10 +145,6 @@ def on_level_layout_changed():
elif keystroke.lower() == "h":
ui.show_help()

#elif keystroke.lower() == "d":
# ui.show_screen_data = not ui.show_screen_data
# ui.draw_screen_data()

elif keystroke.lower() == "n":
# Modify the name of the mod
ui.mod_name = ui.input(
Expand Down

0 comments on commit eb3430c

Please sign in to comment.