unit_name property? (also, handy unit query script)

Discussion in 'Mod Discussions' started by shadowmint, June 10, 2013.

  1. shadowmint

    shadowmint New Member

    Messages:
    17
    Likes Received:
    7
    In my final efforts for the day of building an easy unit indexer, I've written this code that parses the json files and loads all the units, so you can run various queries on the dataset.

    Code:
    import json
    import logging
    import os
    from os.path import join, abspath
    
    
    def load_units():
      rtn = {}
      invalid = {}
      try:
        fp = open("unit_list.json")
      except Exception:
        logging.error("You need to run this from '/Planetary Annihilation/PA/media/pa/units'")
        exit(1)
      units = json.load(fp)
      fp.close()
      list = units["units"]
      for l in list:
        try:
          parts = l.lstrip("/").split("/")
          parts.insert(0, "..")
          parts.insert(0, "..")
          parts.insert(0, os.getcwd())
          full_path = os.getcwd()
          full_path = abspath(join(*parts))
          fp = open(full_path)
          udata = json.load(fp)
          fp.close()
          if not "unit_name" in udata:
            raise Exception("Invalid unit: 'unit_name' property missing")
          unit_name = udata["unit_name"]
          rtn[unit_name] = udata
        except Exception, e:
          invalid[full_path] = e
      return rtn, invalid
    
    units, bad_units = load_units()
    for name in units.keys():
      unit = units[name]
      attrib1 = unit["description"]
      print("%s -> %s" % (name, attrib1))
    
    for path in bad_units.keys():
      print("Path '%s' was not a valid unit: %s" % (path, bad_units[path]))
    
    Interestingly, it dumps out a pile of errors:

    Code:
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\land\unit_cannon\unit_cannon.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\base_orbital\base_orbital.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\sea\base_ship\base_ship.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\radar_satellite\radar_satellite.json' was not a valid unit: Invalid unit: 'unit_name' property missin
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\commanders\imperial\imperial.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\air\gunship\gunship.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\orbital_factory\orbital_factory.json' was not a valid unit: Invalid unit: 'unit_name' property missin
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\radar_satellite_adv\radar_satellite_adv.json' was not a valid unit: Invalid unit: 'unit_name' propert
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\land\nuke_launcher\nuke_launcher.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\land\fusion_plant\fusion_plant.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\orbital_launcher_adv\orbital_launcher_adv.json' was not a valid unit: Invalid unit: 'unit_name' prope
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\commanders\beast\beast.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\orbital_gas_mine\orbital_gas_mine.json' was not a valid unit: Invalid unit: 'unit_name' property miss
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\land\megabot\megabot.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\orbital_factory_adv\orbital_factory_adv.json' was not a valid unit: Invalid unit: 'unit_name' propert
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\orbital\orbital_lander\orbital_lander.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\air\missile\missile.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    Path 'c:\Program Files (x86)\Planetary Annihilation\PA\media\pa\units\wreckage\wreckage.json' was not a valid unit: Invalid unit: 'unit_name' property missing
    
    So, I guess unit_name isn't a universal key for units?

    Things like wreckage.json don't seem to have anything useful attached to them.

    Anyone guess what I might be able to use to uniquely identify valid units?
  2. bgolus

    bgolus Uber Alumni

    Messages:
    1,481
    Likes Received:
    2,299
    Those units aren't implemented, or are broken, and some things (like the missile) aren't actually units at all. They're under the unit directory at the moment to work around a missing feature.

    I'd use the file name.
  3. Raevn

    Raevn Moderator Alumni

    Messages:
    4,226
    Likes Received:
    4,324
    To correctly determine all values for a unit, you need to take inheritance into account.
    The "base_spec" parameter is a reference to an inherited blueprint; any missing values will take those of the inherited blueprint.

Share This Page