Skip to content

Commit

Permalink
Simplify ModController init logic
Browse files Browse the repository at this point in the history
Remove a clause that wasn't doing anything. Add comments to conditions
to say when each can happen, why we need to check for it, etc.

Replace an inefficient list comprehension in component.Mod with a for
loop that breaks when appropriate.
  • Loading branch information
cyberrumor committed Nov 4, 2023
1 parent 916ed6e commit 4c3399a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
14 changes: 7 additions & 7 deletions ammo/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ def __post_init__(self):
self.has_data_dir = True
break

def associated_plugins(self, plugins):
return [
plugin
for plugin in plugins
for file in self.files
if file.name == plugin.name
]
def associated_plugins(self, plugins) -> list:
result = []
for plugin in plugins:
if any(file.name == plugin.name for file in self.files):
if plugin not in result:
result.append(plugin)
return result

def files_in_place(self):
"""
Expand Down
56 changes: 26 additions & 30 deletions ammo/mod_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,50 +110,46 @@ def __init__(self, downloads_dir: Path, game: Game, *keywords):
for file_with_plugin in files_with_plugins:
with open(file_with_plugin, "r") as file:
for line in file:
# Empty lines, comments
if not line.strip() or line.startswith("#"):
if not line.strip() or line.strip().startswith("#"):
# Ignore empty lines and comments.
continue

# Initially assign all plugin parents as a DLC.
# If the plugin has a parent mod, assign parent as that Mod.
# This is used to track ownership for when a mod is disabled.
name = line.strip("*").strip()

# Don't manage order of manually installed mods that were deleted.
if not (self.game.data / name).exists():
# Ignore plugins that can't be found. These will automatically
# be removed from DLCList.txt/Plugins.txt on first write.
continue

# It is required for plugins to not require a parent mod to be able
# to handle DLC. The DLC class here acts as a transient parent,
# and was never added to self.mods.
parent_mod = DLC(name)
for mod in self.mods:

# Iterate through our mods in reverse so we can assign the conflict
# winning mod as the parent.
for mod in self.mods[::-1]:
if name in mod.plugins:
parent_mod = mod
break

enabled = False
pre_existing = False
if line.startswith("*"):
enabled = True
# Attempt to enable the parent mod,
# Only do this if all that mod's files are present.
if parent_mod.files_in_place():
parent_mod.enabled = True

for plug in self.plugins:
# Enable DLC if it's already in the plugins list as enabled.
if plug.name == name:
plug.enabled = True
pre_existing = True
break

if pre_existing:
# This file was already added from DLCList.txt
enabled = line.strip().startswith("*")
if enabled and parent_mod.files_in_place():
# If a plugin was enabled and it came from a mod that was
# correctly installed, the parent mod should be enabled,
# even if it wasn't enabled in the config. This avoids
# situations where you 'commit' and suddenly plugins are
# missing. This also ensures DLC plugins can be added
# to self.plugins.
parent_mod.enabled = True

if not parent_mod.enabled:
# The parent mod either wasn't enabled or wasn't installed correctly.
# Don't add this plugin to the list of managed plugins. It will be
# added automatically when the parent mod is enabled.
continue

plugin = Plugin(name, enabled, parent_mod)
# Only manage plugins belonging to enabled mods.
if parent_mod.enabled and plugin.name not in [
i.name for i in self.plugins
]:
if plugin.name not in [p.name for p in self.plugins]:
self.plugins.append(plugin)

# Populate self.downloads. Ignore downloads that have a '.part' file that
Expand Down

0 comments on commit 4c3399a

Please sign in to comment.