Jump to content

How do I make sense of category enumerations in part.cfg files?


neamerjell

Recommended Posts

I made a Python 3.3 script to go through the \GameData folder, open all the part.cfg files, get the name =, title =, category =, and path of the file and put them into a CSV file. However, B9 Aerospace and KW Rocketry don't follow the normal standard in the "category =" line; they use "category = 0" or "category = 2"!

I am guessing that 0 is likely "Propulsion" and 2 is likely "Structural". Am I right? Is there another meaning for the numbers? Is it a holdover from a previous version that just happens to be backwards compatible?

Here is the script code in case you are interested:


import os

startpath = 'C:\Program Files (x86)\Steam\SteamApps\common\Kerbal Space Program\GameData'
linein = ''
internalname = ''
title = ''
category = ''
fullpath = ''
partpath = ''
idx = 0
havename = False

f = open("C:\\partlist.csv", 'w')

#Write column headers
f.write('NAME,TITLE,CATEGORY,PATH\n')

#Apparently a funtion that automatically handles the recursion through the file system, found it using Google
for root, dirs, files in os.walk(startpath):

#If the filename is "part.cfg" then open it and print the path to the console
for name in files:
if name.find("part.cfg") != -1:
fullpath = os.path.join(root, name)
print(fullpath)
i = open(fullpath, 'r')

for eachline in i:
linein = str(eachline)

#since outputting to a CSV, commas will screw everthing up, replace them with underscores
#Prime example: "Advanced S.A.S Module, Large"
linein = linein.replace(",", "_")

#only want to get the first occurance of "name =", discard all subsequent occurances
# "name =" also appears with in MODULE {} and RESOURCE {} blocks
if havename == False:
idx = linein.find('name = ')
if idx != -1:
internalname = linein[idx + 7:len(str(linein)) - 1]
havename = True

idx = linein.find('title = ')

if idx != -1:
title = linein[idx + 8:len(str(linein)) - 1]

idx = linein.find('category = ')

if idx != -1 and linein.find('sub') == -1:
category = linein[idx + 11:len(str(linein)) - 1]

i.close

#reset havename flag for next file
havename = False

#Create a path starting at "GameData\..."
partpath = fullpath[67:len(fullpath) - 1]

f.write(internalname + ',' + title + ',' + category + ',' + partpath + '\n')

f.close

Edited by neamerjell
Added code listing
Link to comment
Share on other sites

  • 4 weeks later...

Updated code! This may turn into a full blown project before long...


import os

startpath = 'C:\Program Files (x86)\Steam\SteamApps\common\Kerbal Space Program\GameData'

fullpath = ''
idx = 0
propulsion = False
aero = False
control = False
structural = False

#Funtion that automatically handles the recursion through the file system, found it using Google
for root, dirs, files in os.walk(startpath):

#If the filename is "part.cfg" then open it and print the path to the console
for name in files:
if name.find(".cfg") != -1:
fullpath = os.path.join(root, name)

newdata = None
i = open(fullpath, 'r')
newdata = i.read()
i.close

#Fix categories (specifically because of B9 Aerospace)
newdata = newdata.replace("category = 0", "category = Propulsion")
newdata = newdata.replace("subcategory = Propulsion","subcategory = 0")
newdata = newdata.replace("category = 2", "category = Structural")

#Fix parts missing from career mode that are available in sandbox
idx = newdata.find("TechRequired")
if idx == -1:
newdata = newdata.replace("cost","TechRequired = start\nentryCost = 0\ncost")

#Fix fuel tanks being listed under 'Structural', etc (again, B9 is bad for this)
idx = newdata.find("RESOURCE")
if idx != -1:
idx = newdata.find("LiquidFuel")
if idx != -1:
propulsion = True

idx = newdata.find("Oxidizer")
if idx != -1:
propulsion = True

idx = newdata.find("MonoPropellant")
if idx != -1:
newdata = newdata.replace("category = Structural", "category = Control")

if propulsion == True:
newdata = newdata.replace("category = Structural", "category = Propulsion")

print(fullpath)

f = open(fullpath, 'w')
f.write(newdata)
f.close

propulsion = False
aero = False
control = False
structural = False

Link to comment
Share on other sites

Hi - you might ask a moderator to move you over into one of the "Add-ons" sections; you're more likely to get responses from experienced modders there. You may also try the KSP wiki, which has an intro-to-modding section.

To answer your quick question, "category=0" is a holdover from a long while ago, yes.

And if you're getting into re-categorizing parts (personally, I like my Docking Ports to be "structural" elements), you might be interested in the ModuleManager mod, which lets you specify changes to the .cfg files (as deltas) which mean you can bring the deltas with you when you download the next version of KW, or KSP 0.24.0, or what have you.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...