1. We've removed some functionality from SMD in preparation for a migration to new forum software. We expect to make the move before August.

    Python script to change BlockConfig.xml hitpoint value

    Discussion in 'Third Party Software' started by Scypio, Jan 22, 2019.

    1. Scypio

      Joined:
      Sep 18, 2014
      Messages:
      542
      Disclaimer : Always back up your files before changing them. I cannot be held responsible if you somehow mess up your configs and they become unusable. I won't provide any support for this script as it works as it is and should be functionning unless schine change their BlockConfig.xml drastically. And even if that is the case this script is easy to change to match any type of xml.



      Just as the titles says it. It's a python script to change BlockConfig.xml without going through the hassle of parsing manually every time the values and change them.
      So the current script only allow you to change armor's hitpoint as it is but it can be easily changed by anyone to change whatever group of block and whatever value you want.
      One last point, this script does remove every comment added into the BlockConfig.xml so if the two files don't have the same size it's normal.


      So no fancy GUI or anything, it's basic code and you got to change it manually yourself to do what you want.
      I am not going to do a GUI or anything like that, this script works and it is coded in python. So it is a really high level language that anyone can understand and change themselves like they want to. I am just sharing what i did for the quickfire initiative.
      Just keep in mind that the value has to be a string so it must be enclosed between simple or double quotes.

      Code:
      #Works only in python 3
      import xml.etree.ElementTree
      
      def run():
       
         Tree = xml.etree.ElementTree.ElementTree()
         Tree.parse("BlockConfig.xml")
           
          # Change for list
         Value = "toto"
         List = "Grey Basic Armor", "Sapsun Capsule"
         Xpath = ".//Block"
         Links = Tree.iterfind(Xpath)
         for Iterations in Links:
             for i in List:
                 if (i == Iterations.attrib['name']):
                     Iterations.find("Hitpoints").text = Value
      
         # Basic armor
         Value = "toto"
         ListBasicArmor = "Grey", "White", "DarkGrey", "Black", "Yellow", "Orange", "Red", "Pink", "Purple", "Blue", "Teal", "Green", "Brown", "Glass"
         for i in ListBasicArmor:
             Xpath = ".//Basic/"+ i + "/Block/Hitpoints"
             ReplaceEverythingInPath(Tree, Xpath, Value)
         ReplaceEverythingInPath(Tree, ".//Doors/Basic/Block/Hitpoints", Value)
       
         # Standard armor
         Value = "toto"
         ListArmorColor = "Grey", "White", "DarkGrey", "Black", "Yellow", "Orange", "Red", "Pink", "Purple", "Blue", "Teal", "Green", "Brown"
         for i in ListArmorColor:
             Xpath = ".//Standard/"+ i + "/Block/Hitpoints"
             ReplaceEverythingInPath(Tree, Xpath, Value)
         ReplaceEverythingInPath(Tree, ".//Hazard/Block/Hitpoints", Value)
         ReplaceEverythingInPath(Tree, ".//Doors/Standard/Block/Hitpoints", Value)
       
         # Advanced armor
         Value = "toto"
         ListArmorColor = "Grey", "White", "DarkGrey", "Black", "Yellow", "Orange", "Red", "Pink", "Purple", "Blue", "Teal", "Green", "Brown"
         for i in ListArmorColor:
             Xpath = ".//Advanced/"+ i + "/Block/Hitpoints"
             ReplaceEverythingInPath(Tree, Xpath, Value)
         ReplaceEverythingInPath(Tree, ".//Doors/Advanced/Block/Hitpoints", Value)
       
         # Logic blocks
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Logic/Block/Hitpoints", Value)
       
         # Decorative Blocks
         Value = "toto"
         ListDecorativeBlocks = "NonCubic", "Medical", "Ingots", "Motherboards", "Circuits", "ChargedCircuits"
         for i in ListDecorativeBlocks:
             Xpath = ".//Decoration/" + i + "/Block/Hitpoints"
             ReplaceEverythingInPath(Tree, Xpath, Value)
         ReplaceEverythingInPath(Tree, ".//Decoration/Block/Hitpoints", Value)
       
         # Power and chambers
         Value = "toto"
         ListChambers = "GeneralChamber", "Chamber"
         for i in ListChambers:
             Xpath = ".//Power/" + i + "/Block/Hitpoints"
             ReplaceEverythingInPath(Tree, Xpath, Value)
         ReplaceEverythingInPath(Tree, ".//Power/Block/Hitpoints", Value)
       
         # General blocks
         # Old power, faction module, cargo...
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//General/Block/Hitpoints", Value)
       
         # Mines
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Ship/Weapons/Mines/Block/Hitpoints", Value)
       
         # Weapons blocks & computers
         ValueComputer = "toto"
         ValueModule = "toto"
         #ReplaceEverythingInPath(Tree, ".//Ship/Weapons/Block/Hitpoints", Value)
         Xpath = ".//Ship/Weapons/Block"
         Links = Tree.iterfind(Xpath)
         for Iterations in Links:
             if "Computer" in Iterations.attrib["name"]:
                 Iterations.find("Hitpoints").text = ValueComputer
             else:
                 Iterations.find("Hitpoints").text = ValueModule
       
         # Support Tools & computers
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Ship/SupportTools/Block/Hitpoints", Value)
       
         # Effects
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Ship/Effect/Block/Hitpoints", Value)
       
         # Shields
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Ship/Shields/Block/Hitpoints", Value)
       
         # Ship blocks
         # Thrusters, ship core, bobby...
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Ship/Block/Hitpoints", Value)
       
         # Factory
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Factory/Block/Hitpoints", Value)
       
         # Ressources Raw
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Manufacturing/Resources/Raw/Hitpoints", Value)
       
         # Ressources capsules
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Manufacturing/Resources/Capsules/Hitpoints", Value)
       
         # Manufactoring blocks
         # Paints, hardener, scrap
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//Manufacturing/Block/Hitpoints", Value)
       
         # Space Station
         # Warp gate, undeathinator, shop module...
         Value = "toto"
         ReplaceEverythingInPath(Tree, ".//SpaceStation/Block/Hitpoints", Value)
       
         # Terrain
         # Minerals, flora, terrain...
         Value = "toto"
         ListTerrain = "Mineral", "Flora"
         for i in ListChambers:
             Xpath = ".//Terrain/" + i + "/Block/Hitpoints"
             ReplaceEverythingInPath(Tree, Xpath, Value)
         ReplaceEverythingInPath(Tree, ".//Terrain/Block/Hitpoints", Value)
       
         Tree.write("New_BlockConfig.xml")
         return
       
       
      def ReplaceEverythingInPath(Tree, Xpath, Value):
         Links = Tree.iterfind(Xpath)
         for Iterations in Links:
             Iterations.text = Value
         return
       
       
      if __name__ == "__main__":
         run()
      
      As time goes on i might add some more code to support other group of blocks. Anyone with the will to code such things can do so by posting his code there.
       
      #1 Scypio, Jan 22, 2019
      Last edited: Feb 7, 2019
      • Like Like x 2
    2. Scypio

      Joined:
      Sep 18, 2014
      Messages:
      542
      Updated to add all kind of different blocks from the blockconfig.xml as of now. Every blocks should be here. Some group of blocks might not suits your needs but as always feel free to change it.
       
    3. Scypio

      Joined:
      Sep 18, 2014
      Messages:
      542
      Added a check on the attribute name so that you can have different values between weapons computer and weapons module.
       
    4. Scypio

      Joined:
      Sep 18, 2014
      Messages:
      542
      Slight update. Added a way to cycle through the whole file and check it's name. Will allow to create specific sets of blocks based on their name. Or whatever attribute you want. It's still better to go the original way, where you scanned for specific places in the xml tree but. Well. Here you have it.
      If you want to create several list best way to do it is create a function that you will call for each lists.
       
    5. kikaha

      Joined:
      Jul 10, 2013
      Messages:
      623
      Yeah right... like french or russian, or chinese or sanscrit. easy.

      other than that, i suppose its a good thing your script.
       
    Loading...