From 7983f43673643301f3ff56646ea8b01167b383de Mon Sep 17 00:00:00 2001 From: sam-k0 <56673835+sam-k0@users.noreply.github.com> Date: Fri, 25 Oct 2024 11:23:28 +0200 Subject: [PATCH] add wgmodsID to meta.xml --- ModManagerCore/ModMeta.cs | 5 ++++ ModManagerGUI/invoker.py | 6 +++++ ModManagerGUI/main.py | 3 --- ModManagerGUI/wgmodbrowser.py | 45 ++++++++++++++++++++++++++++++++--- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/ModManagerCore/ModMeta.cs b/ModManagerCore/ModMeta.cs index dff868b..5f2b227 100644 --- a/ModManagerCore/ModMeta.cs +++ b/ModManagerCore/ModMeta.cs @@ -83,6 +83,11 @@ public ModInfo(string xmlstr, bool isEnabled, string localFileName) Description = xmlDict["description"]; } + if(xmlKeyExists(xmlDoc, "wgid")) + { + ModID = xmlDict["wgid"]; + } + IsEnabled = isEnabled; LocalFileName = localFileName; diff --git a/ModManagerGUI/invoker.py b/ModManagerGUI/invoker.py index 7079c70..b59f56c 100644 --- a/ModManagerGUI/invoker.py +++ b/ModManagerGUI/invoker.py @@ -33,6 +33,12 @@ def __expected_core_path(self): raise Exception("Unsupported platform: "+sys.platform) return installation_path + def get_extraction_path(self): + extract_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Core", "extract") + if not os.path.isdir(extract_dir): + os.mkdir(extract_dir) + return extract_dir + def __parse_mod(self, jstr:str): jmod = json.loads(jstr) return Mod(jmod["ModName"], diff --git a/ModManagerGUI/main.py b/ModManagerGUI/main.py index 437ad4c..df419f2 100644 --- a/ModManagerGUI/main.py +++ b/ModManagerGUI/main.py @@ -513,9 +513,6 @@ def export_mods(self): self.close() - - - if __name__ == '__main__': argadd = [] if sys.platform == "linux": diff --git a/ModManagerGUI/wgmodbrowser.py b/ModManagerGUI/wgmodbrowser.py index 126ef3a..434b24d 100644 --- a/ModManagerGUI/wgmodbrowser.py +++ b/ModManagerGUI/wgmodbrowser.py @@ -3,6 +3,8 @@ import webbrowser import invoker import sys, os +import zipfile +import xml.etree.ElementTree as ET # cross platform way to get the download directory for modbrowser # creates the directory if it doesn't exist @@ -252,14 +254,51 @@ def open_wgmods_page(self): print("Opening mod page on WGMods for mod ID:", self.modid) webbrowser.open(f"https://wgmods.net/{self.modid}") + # Unpacks a .wotmod file, injects the mod ID into the meta.xml file, and repacks the .wotmod file + def inject_wgmodid_to_meta(self, localpath:str): + print("Extracting mod to ", self.invoker_ref.get_extraction_path()) + target = self.invoker_ref.get_extraction_path() + os.sep + self.mod_name + with zipfile.ZipFile(localpath, 'r') as zip_ref: + zip_ref.extractall(target) + + # find the meta.xml file + metafile = None + for root, dirs, files in os.walk(target): + for file in files: + if file == "meta.xml": + metafile = os.path.join(root, file) + break + if metafile != None: + break + # add the mod ID to the meta.xml file + if metafile != None: + tree = ET.parse(metafile) + root = tree.getroot() + modid = ET.SubElement(root, "wgid") + modid.text = str(self.modid) + tree.write(metafile) + print("Added mod ID to meta.xml file for mod:", self.mod_name) + else: + FileNotFoundError("Failed to add mod ID to meta.xml file for mod:", self.mod_name) + + # repack the mod back to a .wotmod file + with zipfile.ZipFile(localpath, 'w') as zip_ref: + for root, dirs, files in os.walk(target): + for file in files: + file_path = os.path.join(root, file) + arcname = os.path.relpath(file_path, target) + zip_ref.write(file_path, arcname) + + print("Repacked mod to ", localpath) + def download_install_wotmod(self, download_url:str): localfilename = download_url.split("/")[-1] # get the filename from the URL localpath = os.path.join(get_download_dir(), localfilename) # download the file wgmods.download_from_url(download_url, localpath) - - #TODO: Extract the wotmod to the extract directory and add a key to the meta.xml file about the mod ID - # This will allow update checking on wgmods.net + + # inject the mod ID into the meta.xml file + self.inject_wgmodid_to_meta(localpath) # install the mod self.invoker_ref.install_mod(localpath)