Created
March 29, 2024 18:43
-
-
Save feldoh/f4dc7fe71f89e7a0ccce5ac99f3f0746 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from lxml import etree | |
def find_about_folder(path): | |
while path != os.path.dirname(path): # stop at root directory | |
if 'About' in os.listdir(path): | |
return os.path.join(path, 'About') | |
path = os.path.dirname(path) | |
return None | |
def find_package_id(about_folder): | |
about_file = os.path.join(about_folder, 'About.xml') | |
tree = etree.parse(about_file) | |
package_id = tree.xpath('/ModMetaData/packageId')[0].text | |
return package_id | |
def update_xenotype_file(xenotype_file, root_dir): | |
xenotype_tree = etree.parse(xenotype_file) | |
gene_names = xenotype_tree.xpath('/Defs/XenotypeDef/genes/li/text()') | |
for dirpath, dirnames, filenames in os.walk(root_dir): | |
for filename in filenames: | |
if filename.endswith('.xml') and 'Defs' in dirpath: | |
file_path = os.path.join(dirpath, filename) | |
try: | |
tree = etree.parse(file_path) | |
for gene_name in gene_names: | |
gene_def = tree.xpath(f'/Defs/GeneDef[defName="{gene_name}"]') | |
if gene_def: | |
about_folder = find_about_folder(dirpath) | |
if about_folder: | |
package_id = find_package_id(about_folder) | |
li_elements = xenotype_tree.xpath(f'//li[text()="{gene_name}"]') | |
for li in li_elements: | |
li.attrib['MayRequire'] = package_id | |
print(f'Updated {gene_name} in {xenotype_file} with {package_id}') | |
except etree.XMLSyntaxError: | |
pass | |
xenotype_tree.write(xenotype_file, pretty_print=True, xml_declaration=True, encoding='utf-8') | |
xenotype_file = 'E:\\Epic\\RimWorld\\Mods\\RPGAdventureFlavourPack\\Compatibility\\arr624.geneticdrift\\1.4\\Defs\\XenotypeDefs\\MSS_WizardTales_Xenotypes.xml' | |
search_dirs = ['D:\\SteamLibrary\\steamapps\\workshop\\content\\294100', 'E:\\Epic\\RimWorld\\Data'] | |
for search_dir in search_dirs: | |
update_xenotype_file(xenotype_file, search_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very inefficient, don't judge me