Skip to content

Instantly share code, notes, and snippets.

@yoo2001818
Last active August 29, 2015 14:03
Show Gist options
  • Save yoo2001818/a21b44bc3cbca944e992 to your computer and use it in GitHub Desktop.
Save yoo2001818/a21b44bc3cbca944e992 to your computer and use it in GitHub Desktop.
Civ5 Mod Manager
from os.path import expanduser
import os
import sys
import shutil
import xml.etree.ElementTree as ElementTree
home = expanduser("~")
civExp = home+"/.local/share/Steam/SteamApps/common/Sid Meier's Civilization V/steamassets/assets/dlc/expansion2/"
civMod = home+"/.local/share/Aspyr/Sid Meier's Civilization 5/MP_MODS/"
civPkg = "expansion2.civ5pkg"
if len(sys.argv) == 1:
print (sys.argv[0]+" <install|remove>")
print ("")
print ("Expansion 2 path:")
print (civExp)
print ("MOD path:")
print (civMod)
print ("Please edit the path before executing it.")
sys.exit()
if sys.argv[1] == "remove":
# Clear up all mods folder
for filename in os.listdir(civExp):
if os.path.isfile(filename) == False:
if filename == "modmanager":
print ("modmanager directory deleted")
shutil.rmtree(civExp+os.sep+filename)
# Revert xml file
if os.path.exists(civExp+civPkg+".bak"):
os.remove(civExp+civPkg)
shutil.move(civExp+civPkg+".bak",civExp+civPkg)
print ("Reverted backup file")
if sys.argv[1] == "install":
if os.path.exists(civExp+civPkg+".bak"):
print ("It's already installed, please remove mods before installing.")
sys.exit()
print ("Scanning mods folder...")
# Move all mods into folder while parsing it.
modDirectory = ""
modName = ""
modFiles = []
directorys = []
modXMLs = []
os.mkdir(civExp+"modmanager"+os.sep)
modCount = 0
unImpCount = 0
for root, dirs, files in os.walk(civMod):
modFolder = 0
if modDirectory != "" and root.startswith(modDirectory):
modFolder = 1
for name in files:
if name.lower().endswith(".modinfo"):
# Parse xml
tree = ElementTree.parse(root+os.sep+name)
rootX = tree.getroot()
prop = rootX.find("Properties")
modNameSub = prop.find("Name").text
mpSupport = prop.find("SupportsMultiplayer").text
modXMLs.append(prop)
if mpSupport == "1":
if modFolder == 1 and root.startswith(modDirectory):
print ("Error: "+root+" folder is in mod folder!")
else:
modCount += 1
print ("Installing "+modNameSub)
modName = modNameSub
modFolder = 1
modDirectory = root
# Scan imported files.
filesX = rootX.find("Files")
for fileItem in filesX.findall("File"):
itemPath = fileItem.text
head, tail = os.path.split(itemPath)
doImport = fileItem.get("import") == "1"
if doImport:
print ("Processing "+itemPath+"...")
if tail.endswith(".xml"):
tail = str(modCount)+tail
modFiles.append(tail.lower())
shutil.copy(root+os.sep+itemPath, civExp+"modmanager"+os.sep+tail.lower())
else:
shutil.copy(root+os.sep+itemPath, civExp+"modmanager"+os.sep+tail.lower())
onactivate = rootX.find("Actions").find("OnModActivated")
for fileItem in onactivate.findall("UpdateDatabase"):
# Find...
itemPath = fileItem.text
head, tail = os.path.split(itemPath)
if not tail in modFiles:
print ("Processing unimported "+itemPath+"...")
unImpCount += 1
tail = "Import"+str(unImpCount)+"_"+tail
shutil.copy(root+os.sep+itemPath, civExp+"modmanager"+os.sep+tail.lower())
modFiles.append(tail.lower())
else:
print (modName+" Does not support multiplayer!")
print ("Copying is done, adding data into civ5Pkg file")
shutil.copy(civExp+civPkg,civExp+civPkg+".bak")
tree = ElementTree.parse(civExp+civPkg)
root = tree.getroot()
gameplay = root.find("Gameplay")
for modFile in modFiles:
gamedata = ElementTree.Element("GameData")
gamedata.text = modFile
gamedata.tail = "\n "
gameplay.append(gamedata)
diritem = ElementTree.Element("Directory")
diritem.text = "modmanager"
diritem.tail = "\n "
gameplay.append(diritem)
tree.write(civExp+civPkg, encoding="utf-8", xml_declaration=True)
print ("All done!")
for prop in modXMLs:
if prop.find("SupportsMultiplayer").text == "1":
print (prop.find("Name").text + " - " + prop.find("Teaser").text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment