Skip to content

Commit c97bdfc

Browse files
committed
add safeguard when modimporter is outdated
1 parent 939ef75 commit c97bdfc

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Recommend users to update Mod Importer when it fails to run due to being outdated.
13+
1014
## [1.7.1] - 2022-07-14
1115

1216
### Fixed

hephaistos/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from hephaistos import backups, config, hashes, helpers, interactive, lua_mod, patchers, sjson_data
1010
from hephaistos.config import LOGGER
11-
from hephaistos.helpers import HadesNotFound, HUD, Platform, Scaling
11+
from hephaistos.helpers import HadesNotFound, HUD, ModImporterRuntimeError, Platform, Scaling
1212

1313

1414
class ParserBase(ArgumentParser):
@@ -95,6 +95,8 @@ def __start(self) -> None:
9595
# handle subcommand args via SubcommandBase.dispatch handler
9696
try:
9797
args.dispatch(**vars(args))
98+
except ModImporterRuntimeError as e:
99+
LOGGER.error(e)
98100
except Exception as e:
99101
LOGGER.exception(e) # log any unhandled exception
100102
# if in interactive mode, loop until user manually closes

hephaistos/helpers.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,26 +263,40 @@ def remember_cwd():
263263
os.chdir(cwd)
264264

265265

266+
class ModImporterRuntimeError(RuntimeError): ...
267+
268+
266269
def run_modimporter(modimporter_file: Path, clean_only: bool=False) -> None:
267270
"""Run modimporter from the Content directory, as if the user did it."""
268271
with remember_cwd():
269272
# temporarily switch to modimporter working dir (Content)
270273
os.chdir(modimporter_file.parent)
271274
# dynamically import modimporter.py if using Python version
272275
if modimporter_file.suffix == '.py':
273-
spec = importlib.util.spec_from_file_location("modimporter", modimporter_file.name)
274-
modimporter = importlib.util.module_from_spec(spec)
275-
spec.loader.exec_module(modimporter)
276-
modimporter.game = 'Hades'
277-
modimporter.clean_only = clean_only
278-
modimporter.LOGGER.setLevel(logging.ERROR)
279-
modimporter.start()
276+
try:
277+
spec = importlib.util.spec_from_file_location("modimporter", modimporter_file.name)
278+
modimporter = importlib.util.module_from_spec(spec)
279+
spec.loader.exec_module(modimporter)
280+
modimporter.game = 'Hades'
281+
modimporter.clean_only = clean_only
282+
modimporter.LOGGER.setLevel(logging.ERROR)
283+
modimporter.start()
284+
except AttributeError as e:
285+
LOGGER.error(e)
286+
raise ModImporterRuntimeError("Failed to import 'modimporter.py': your copy of 'modimporter.py' is likely outdated, please update it (a version >= 1.3.0 is required)")
280287
# otherwise execute modimporter directly if using binary version
281288
else:
282289
args = [modimporter_file.name, '--no-input', '--quiet', '--game', 'Hades']
283290
if clean_only:
284291
args += ['--clean']
285-
subprocess.run(args)
292+
try:
293+
subprocess.run(args, check=True, text=True, stderr=subprocess.PIPE)
294+
except subprocess.CalledProcessError as e:
295+
if "unrecognized arguments" in e.stderr:
296+
LOGGER.error(e)
297+
raise ModImporterRuntimeError("Failed to import 'modimporter.py': your copy of 'modimporter.py' is likely outdated, please update it (a version >= 1.3.0 is required)")
298+
else:
299+
raise e
286300

287301

288302
def configure_screen_variables(width: int, height: int, scaling: Scaling) -> Scaling:

0 commit comments

Comments
 (0)