Jump to content

Out of game utility to read cfg files in GameData folder?


Recommended Posts

Hello All. I am looking for some feedback on an idea for an out of game utility. (full disclosure, this is a cross post from reddit, but it has gotten buried in tons of other awesome posts, and may be better suited for this forum anyway).

I am interested in something with the capabilities of ModuleManager, but outside the game. Essentially I'd like to be able to read in all of the .cfg files in the GameData directory and process their contents to gather information. I thought it might be interesting to see, for example, a listing of all parts and their module properties, or all currently configured resource types, or parts currently configured within a particular tech tree node, etc...

I think this may have some uses for modders as a sort of static analysis/checking tool for ModuleManager configs. I can also see this capability driving something for more hardcore mod users to tweak parts to their liking with a personal ModuleManager cfg file.

I have some skills to code this up myself, but I wanted to see if anyone out there knew if something like this actually exists. Also is this something you might find useful?

Thanks!

Link to comment
Share on other sites

It is uncanny that I have been working on just such a thing... I wrote mine in Python 3. I'm having problems with it not writing the last 13 records to the csv file even though it is definitely reading and parsing the data from those files according to the messages I wrote to the console.


import os

startpath = 'C:\Program Files (x86)\Steam\SteamApps\common\Kerbal Space Program\GameData'
outpath = "C:\\Users\\Jeremy\\Desktop\\partlist.csv"

datain = ''
title = ''
fullpath = ''
partpath = ''
modname = ''
idx = 0
filecount = 0
readcount = 0
partcount = 0
strout = ''

f = open(outpath, 'w')

#column headers
f.write('PART COUNT,TITLE,MOD NAME,PATH\n')

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

for name in files:
filecount += 1

#If the filename ends in ".cfg" then open it
#something in Crowd Sourced Science mod files creates errors, and this is not a part mod anyway, so just skip it
if name.find(".cfg") != -1:
fullpath = os.path.join(root, name)

if fullpath.find('CrowdSourcedScience') == -1:
i = open(fullpath, 'r')
datain = i.read()
readcount += 1
i.close

#make sure it is actually a part file because not all .cfg files are part files
if datain.find("PART\n{") != -1:

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

idx = datain.find('title = ')
if idx != -1:
title = datain[idx + 8:datain.find("\n", idx + 8)]
#endif 'title = ' found

#Create a path starting at "GameData\..."
modname = fullpath[76:fullpath.find("\\", 76, len(fullpath))]
partcount += 1

#This fixed my earlier problem: it was not writing the last 13 or so records
#so I stored everything to a variable and then wrote the variable once
#This confirms my earlier suspicion that the write routine was bogging down somehow
strout += str(partcount) + ',' + title + ',' + modname + ',' + fullpath + '\n'

#This works perfectly, go figure
print(str(partcount) + ',' + title + ',' + modname + ',' + fullpath)

datain = None

#endif datain.find("PART\n{") != -1

#endif name.find(".cfg") != -1

#next (for name in files)

#next (for root, dirs, files in os.walk(startpath))


f.write(strout)
f.close
print('File Count:' + str(filecount) + '\n') # 14231
print('Read Count:' + str(readcount) + '\n') # 1595
print('Part Count:' + str(partcount) + '\n') # console displays 983, only 969 written to csv...
print("Done.")

I thought maybe the call to the os file management routines might have gotten out of sync or something, but every record is just right until just before the end of the list. I also thought maybe the program was closing the file before a write queue was finished.

EDIT: I fixed it! At least the initial test makes me think so. I stored all the stuff to a variable and wrote the variable in one shot rather than continuously writing to the file (which I suspected was bogging down somehow). I have installed a few mods since I started so the actual count is more like 989 but the count and the number of records in the csv match this time!

Edited by neamerjell
I fixed it! (at least I think so based on preliminary tests)
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...