AIScript Renaminator!

Discussion about the mod tools for Achron.

AIScript Renaminator!

Postby NeutralBase » Wed Apr 15, 2015 4:57 pm

I made a script that turns this:
Code: Select all
    <AIScripts>
        <AIScript>
            <Name>Script1</Name>
            <Path>./ais/grekim_factory_idle2.abn</Path>
        </AIScript>
        <AIScript>
            <Name>Script2</Name>
            <Path>./ais/grekim_factory_idle.abn</Path>
        </AIScript>
        <AIScript>
            <Name>Script3</Name>
            <Path>./ais/common_vecgir_idle.abn</Path>
        </AIScript>

into this:
Code: Select all
    <AIScripts>
        <AIScript>
            <Name>grekim_factory_idle2.abn</Name>
            <Path>./ais/grekim_factory_idle2.abn</Path>
        </AIScript>
        <AIScript>
            <Name>grekim_factory_idle.abn</Name>
            <Path>./ais/grekim_factory_idle.abn</Path>
        </AIScript>
        <AIScript>
            <Name>common_vecgir_idle.abn</Name>
            <Path>./ais/common_vecgir_idle.abn</Path>
        </AIScript>

All references to the scripts are also renamed. This should make editing the xml manually a lot easier. You will need to rerun this script every time you regenerate the XML with ResequenceXMLCompiler.

Here's the code:
Code: Select all
# This script reads from inputFile, renames all script names
# and then writes to outputFile. It does no error checking,
# use at your own risk.
# It also fails if the original input file contains duplicate script
# names (which shouldn't happen unless you messed something up).

inputFile = "Achron.ocs.xml"
outputFile = "Achron.ocs.xml"

import xml.etree.ElementTree as ET
from collections import namedtuple

tree = ET.parse(inputFile)
root = tree.getroot()

Script = namedtuple("Script",
    ["originalName", "element", "newName"]
  )

# Keep track of the old names (so we can rename references)
# and new names (so we can avoid duplicating names).
oldNames = {}
newNames = set()

# Find all the script definitions and pick their new names
for element in root.findall("./AIScripts/AIScript"):
  name = element.find("Name").text
  path = element.find("Path").text
  # Grab all the stuff after the last slash (the actual filename)
  # and use that as the new name for the script
  newName = path.rsplit("/", 1)[-1]
  # Make sure we don't end up with duplicate script names
  if(newName in newNames):
    counter = 1
    while (newName + str(counter)) in newNames:
      counter += 1
    newName = newName + str(counter)
 
  script = Script(name, element, newName)
  oldNames[name] = script
  newNames.add(newName)

# Rename all script definitions, giving them their new names
for original, element, newName in oldNames.values():
  element.find("Name").text = newName

# Find all references to each script and rename them
for element in root.findall("./ObjectClasses/ObjectClass/Objectives/Objective/Script"):
  script = element.text
  if(not script in oldNames):
    print "Warning: Reference to non-existent script name:", script
    continue
  element.text = oldNames[script].newName
 
# Save the xml file (obvious comment is obvious)
tree.write(outputFile)
NeutralBase
 
Posts: 25
Joined: Thu Feb 13, 2014 11:34 pm

Return to Mod Tools

cron