Skip to content

Instantly share code, notes, and snippets.

@shakeyourbunny
Created November 8, 2020 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakeyourbunny/d67f542a1c52caa10df6d25362760791 to your computer and use it in GitHub Desktop.
Save shakeyourbunny/d67f542a1c52caa10df6d25362760791 to your computer and use it in GitHub Desktop.
RimWorld MOD DeSteamifier
#!/usr/bin/env python3
# desteamifys mod numbers
#
import os, sys
import xml.etree.ElementTree as et
def sanitize(filename):
clean = filename
dontwant = [ "?", "*", "<", ">", "\\", "|"]
for d in dontwant:
if d in clean:
clean = clean.replace(d, "")
clean = clean.replace(":", " -")
clean = clean.replace("/", " ")
return clean
desteamify_path = input("Enter path to desteamify and hit ENTER:")
if ":" not in desteamify_path:
desteamify_path = os.path.join(os.getcwd(), desteamify_path)
if not os.path.isdir(desteamify_path):
print(f"PATH {desteamify_path} does not exist.")
sys.exit(1)
os.chdir(desteamify_path)
desteamify_path = os.getcwd()
directories_full = list(filter(os.path.isdir, os.listdir(desteamify_path)))
directories_mods = [ d for d in directories_full if os.path.isfile(os.path.join(d, "About", "About.xml")) ]
num_dirs = len(directories_mods)
num_renamed = 0
for d in directories_mods:
aboutxml = os.path.join(d, "About", "About.xml")
with open(aboutxml, "r", encoding="utf-8") as f:
datastr=f.read()
tree = et.fromstring(datastr)
newname = sanitize(tree.find("name").text)
publishedfileidtxt = os.path.join(d, "About", "PublishedFileId.txt")
workshopid = 0
if os.path.isfile(publishedfileidtxt):
with open(publishedfileidtxt, "r", encoding="utf-8") as f:
workshopid = int(f.readlines()[0])
newname = sanitize(newname)
if workshopid > 0:
newname = newname + f" ({workshopid})"
if newname != d:
num_renamed += 1
print("Processing directory '{}' ".format(d), flush=True, end="")
print(f"(REN) {d} -> {newname}")
if not os.path.isdir(newname):
try:
os.rename(d, newname)
except:
print("")
else:
print("(ignored, new directory already exists.)")
print(f"Done, {num_dirs} mods total, {num_renamed} mods renamed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment