Skip to content

Commit

Permalink
add wgmodsID to meta.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-k0 committed Oct 25, 2024
1 parent e7e67bf commit 7983f43
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
5 changes: 5 additions & 0 deletions ModManagerCore/ModMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions ModManagerGUI/invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
3 changes: 0 additions & 3 deletions ModManagerGUI/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,6 @@ def export_mods(self):
self.close()





if __name__ == '__main__':
argadd = []
if sys.platform == "linux":
Expand Down
45 changes: 42 additions & 3 deletions ModManagerGUI/wgmodbrowser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7983f43

Please sign in to comment.