Skip to content

Commit

Permalink
Add ability to delete plugins
Browse files Browse the repository at this point in the history
Add the ability to execute commands like:
delete plugin 5
delete plugin all

The help text in the README has also been updated to indicate this
change.

Note that we continue to not want to be able to rename plugins due to
this being able to break your game (if you rename a plugin that another
depended on, for example).
  • Loading branch information
cyberrumor committed Feb 1, 2024
1 parent 5b3b052 commit 610ed25
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pip3 install --user --force-reinstall . || pip3 install --user --break-system-pa
| commit | | Apply pending changes |
| configure | \<index> | Configure a fomod |
| deactivate | (mod\|plugin) \<index> | Disabled components will not be loaded by game |
| delete | (mod\|download) \<index> | Removes specified file from the filesystem |
| delete | (mod\|download\|plugin) \<index> | Removes specified file from the filesystem |
| exit | | Quit |
| find | [\<keyword> ...] | Show only components with any keyword |
| help | | Show this menu |
Expand Down
6 changes: 6 additions & 0 deletions ammo/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class ComponentEnum(str, Enum):
class DeleteEnum(str, Enum):
MOD = "mod"
DOWNLOAD = "download"
PLUGIN = "plugin"


class RenameEnum(str, Enum):
MOD = "mod"
DOWNLOAD = "download"


@dataclass(slots=True, kw_only=True)
Expand Down
46 changes: 40 additions & 6 deletions ammo/mod_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
Plugin,
DeleteEnum,
ComponentEnum,
RenameEnum,
)
from .lib import normalize

Expand Down Expand Up @@ -534,13 +535,13 @@ def deactivate(self, component: ComponentEnum, index: Union[int, str]) -> None:
# Demote IndexErrors
raise Warning(e)

def rename(self, component: DeleteEnum, index: int, name: str) -> None:
def rename(self, component: RenameEnum, index: int, name: str) -> None:
"""
Names may contain alphanumerics and underscores
"""
if component not in list(DeleteEnum):
if component not in list(RenameEnum):
raise Warning(
f"Can only rename components of types {[i.value for i in list(DeleteEnum)]}, not {component}"
f"Can only rename components of types {[i.value for i in list(RenameEnum)]}, not {component}"
)
if self.changes is True:
raise Warning("You must `commit` changes before renaming.")
Expand All @@ -558,7 +559,7 @@ def rename(self, component: DeleteEnum, index: int, name: str) -> None:
f"Choose something else. These names are forbidden: {forbidden_names}"
)

if component == DeleteEnum.DOWNLOAD:
if component == RenameEnum.DOWNLOAD:
try:
download = self.downloads[index]
except IndexError as e:
Expand All @@ -584,7 +585,7 @@ def rename(self, component: DeleteEnum, index: int, name: str) -> None:
self.refresh()
return

assert component == DeleteEnum.MOD
assert component == RenameEnum.MOD
try:
mod = self.mods[index]
except IndexError as e:
Expand Down Expand Up @@ -625,8 +626,8 @@ def delete(self, component: DeleteEnum, index: Union[int, str]) -> None:
raise Warning(f"Expected int, got '{index}'")

if component == DeleteEnum.MOD:
deleted_mods = ""
if index == "all":
deleted_mods = ""
visible_mods = [i for i in self.mods if i.visible]
for mod in visible_mods:
self.deactivate(ComponentEnum.MOD, self.mods.index(mod))
Expand All @@ -648,6 +649,39 @@ def delete(self, component: DeleteEnum, index: Union[int, str]) -> None:
self.commit()
raise Warning(f"Deleted mod: {mod.name}")

if component == DeleteEnum.PLUGIN:

def get_plugin_file(plugin: Plugin) -> Union[None, Path]:
for p in plugin.mod.files:
if p.name == plugin.name:
return p

if index == "all":
deleted_plugins = ""
visible_plugins = [i for i in self.plugins if plugin.visible]
for plugin in visible_plugins:
self.deactivate(ComponentEnum.PLUGIN, self.plugins.index(plugin))
for plugin in visible_plugins:
if plugin.mod is None or plugin.name in (p.name for p in self.dlc):
raise Warning(
f"Deleted plugins:\n{deleted_plugins}\nCouldn't delete DLC {plugin.name}"
)
self.plugins.pop(self.plugins.index(plugin))
if (p := get_plugin_file(plugin)) and p.exists():
p.unlink()
deleted_plugins += f"{plugin.name}\n"
self.commit()
raise Warning(f"Deleted plugins:\n{deleted_plugins}")
try:
plugin = self.plugins.pop(index)
if (p := get_plugin_file(plugin)) and p.exists():
p.unlink()
except IndexError as e:
raise Warning(e)

self.commit()
raise Warning(f"Deleted plugin: {plugin.name}")

assert component == DeleteEnum.DOWNLOAD
if index == "all":
visible_downloads = [i for i in self.downloads if i.visible]
Expand Down

0 comments on commit 610ed25

Please sign in to comment.