Skip to content

Add duplicate mod import check and user-friendly error screen #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions modloader/modinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os.path
from os import listdir
from collections import defaultdict
try:
import renpy.config
except AttributeError:
Expand All @@ -14,15 +15,37 @@
moddirnames = []
mod_load_order = []

# Error catching: if the same name is repeated,
# save the old copies so we can report a detailed error.
overloaded_mod_names = defaultdict(list)

def add_mod(mod_name, mod):
"""Add a mod in the registry"""
print "Adding mod {}".format(mod_name)
if mod_name in modlist:
print("IMPORT ISSUE: Given a mod name of {0!r} by both {1!r} and {2!r}.".format(mod_name, modlist[mod_name].__module__, mod.__module__))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this print statement only for debugging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not. In case something goes wrong from the multiple import of these mods, I wanted to give some kind of output as soon as it's discovered, at least for mod developers, just in case they manage to create a corner case where we can't show the error screen.

overloaded_mod_names[mod_name].append(modlist[mod_name])
modlist[mod_name] = mod


def get_mods():
"""Get the mods in the registry"""
if overloaded_mod_names:
# Error: can't have multiple mods with the same name.
# Report the error now, as we've likely finished
# importing all mods, so have captured the full issue.
from modloader import report_mod_errors
complete_overloads = {
mod_name: mod_classes + [modlist[mod_name]] for mod_name, mod_classes in overloaded_mod_names.items()
}
formatted_overloads = {
mod_name: " and ".join("{0!r}".format(mod_class.__module__) for mod_class in mod_classes) for mod_name, mod_classes in complete_overloads.items()
}
report_mod_errors(
"While importing mods, ones with the same name were installed in multiple folders.\n\n"
+ "\n".join(" Mods named {0!r} were installed in {1}".format(mod_name, formatted_overload_list) for mod_name, formatted_overload_list in formatted_overloads.items())
+ "\n\nPlease remove the duplicated mods or change the names in their metadata."
)
return modlist


Expand Down