|
| 1 | +from os import chdir, listdir |
| 2 | +from re import compile, sub |
| 3 | + |
| 4 | +from PyQt6.QtGui import QIcon |
| 5 | +from PyQt6 import QtWidgets |
| 6 | +import mobase |
| 7 | + |
| 8 | + |
| 9 | +def traverse_and_unignore(modsPath: str): |
| 10 | + """ |
| 11 | + Mark all mods that have their updates ignored as no longer ignored |
| 12 | + and return them in a list |
| 13 | + """ |
| 14 | + ignored_regex = compile("ignoredVersion=\S+") |
| 15 | + unignored_mods = [] |
| 16 | + |
| 17 | + # Iterate through mod directories |
| 18 | + for mod_dir in listdir(modsPath): |
| 19 | + |
| 20 | + # Check for separators and files related to this program, and skip them |
| 21 | + if (str(mod_dir).__contains__('_separator') |
| 22 | + or str(mod_dir).__contains__('mo2-unignore.log') |
| 23 | + or str(mod_dir).__contains__('unignore.py')): |
| 24 | + continue |
| 25 | + |
| 26 | + file_data: str |
| 27 | + changed_file_data: str |
| 28 | + try: |
| 29 | + with open(f"{modsPath}\\{mod_dir}\\meta.ini", "r", encoding='UTF-8') as read_file: |
| 30 | + file_data = read_file.read() |
| 31 | + changed_file_data = sub( |
| 32 | + ignored_regex, 'ignoredVersion=', file_data) |
| 33 | + |
| 34 | + except FileNotFoundError: |
| 35 | + continue |
| 36 | + |
| 37 | + # Write changed data from memory to meta.ini file if the data changed |
| 38 | + if file_data != changed_file_data: |
| 39 | + try: |
| 40 | + with open(f"{modsPath}\\{mod_dir}\\meta.ini", 'w', encoding="utf-8") as write_file: |
| 41 | + write_file.write(changed_file_data) |
| 42 | + unignored_mods.append(mod_dir) |
| 43 | + except FileNotFoundError: |
| 44 | + continue |
| 45 | + |
| 46 | + return unignored_mods |
| 47 | + |
| 48 | + |
| 49 | +class UnignoreUpdates(mobase.IPluginTool): |
| 50 | + organizer: mobase.IOrganizer |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + super().__init__() |
| 54 | + |
| 55 | + def init(self, newOrganizer: mobase.IOrganizer): |
| 56 | + self.organizer = newOrganizer |
| 57 | + return True |
| 58 | + |
| 59 | + def name(self) -> str: |
| 60 | + return "Unignore Mod Updates" |
| 61 | + |
| 62 | + def author(self) -> str: |
| 63 | + return "Zediious" |
| 64 | + |
| 65 | + def description(self) -> str: |
| 66 | + return "Un-ignore updates for all mods in the list." |
| 67 | + |
| 68 | + def version(self) -> mobase.VersionInfo: |
| 69 | + return mobase.VersionInfo(1, 2, mobase.ReleaseType.FINAL) |
| 70 | + |
| 71 | + def isActive(self) -> bool: |
| 72 | + return self.organizer.pluginSetting(self.name(), "enabled") |
| 73 | + |
| 74 | + def settings(self): |
| 75 | + return [mobase.PluginSetting("Enabled", "Enable this plugin", True)] |
| 76 | + |
| 77 | + def displayName(self) -> str: |
| 78 | + return "Unignore Mod Updates" |
| 79 | + |
| 80 | + def tooltip(self) -> str: |
| 81 | + return "Un-ignore updates for all mods in the list." |
| 82 | + |
| 83 | + def icon(self): |
| 84 | + return QIcon('null') |
| 85 | + |
| 86 | + def display(self): |
| 87 | + """ |
| 88 | + Function called when tool is used in GUI |
| 89 | + """ |
| 90 | + updating_mods = traverse_and_unignore(self.organizer.modsPath()) |
| 91 | + updating_mods_string = "" |
| 92 | + if updating_mods != []: |
| 93 | + for mod in updating_mods: |
| 94 | + updating_mods_string += f"{mod}\n" |
| 95 | + else: |
| 96 | + updating_mods_string = "No mods had their updates ignored." |
| 97 | + |
| 98 | + QtWidgets.QMessageBox.information( |
| 99 | + self._parentWidget(), 'Mods who are now checking for updates', updating_mods_string) |
| 100 | + |
| 101 | + |
| 102 | +def createPlugin() -> mobase.IPlugin: |
| 103 | + return UnignoreUpdates() |
0 commit comments