View Full Version : Python API: get active mod name?


Dr Elmer Jiggle
Jan 24, 2006, 11:52 PM
I'd like to allow configuration of a mod I'm working on via an .ini file. Python includes support for .ini parsing via the ConfigParser module, so this should theoretically be very easy. Presumably if the mod is installed in CustomAssets, it should load CustomAssets/<modname>.ini. If it's installed in Mods it should load Mods/<modname>/<modname>.ini.

I can get the CustomAssets directory and the top level Mods directory without too much difficulty.


userDirectory = os.path.expanduser("~")
civ4Directory = os.path.join(userDirectory,
"My Documents\\My Games\\Sid Meier's Civilization 4")
customAssetsDirectory = os.path.join(civ4Directory, "CustomAssets")
modsDirectory = os.path.join(civ4Directory, "Mods")


The active mod directory, on the other hand, isn't so easy. I can't figure out how to determine the active mod name. I've tried looking at sys.argv and quite a variety of other places, but it doesn't appear to be available. The game plays some weird tricks with the module loader, so you can't use sys.path to find the directory either.

Has anyone figured out how to do this, or are they any bright ideas on another approach? The Hall of Fame mod uses an .ini file, but he just hardcoded the path, and ideally I'd like to avoid that. You never know when some genius is going to decide he wants to use a different directory name for your mod, and then suddenly everything breaks and you've got tons of bug reports.

Thanks.

Dr Elmer Jiggle
Jan 25, 2006, 06:59 PM
Don't everybody respond all at once! :mischief:

Kael
Jan 25, 2006, 07:17 PM
Your name scares me.

Belizan
Jan 25, 2006, 08:44 PM
I asked this same question (for different reasons--I had made a new random map generator and wanted to add some custom features for FFH), and no one seemed to know. Could be this is not possible :/. I certainly haven't seen anything promising thus far.

Dr Elmer Jiggle
Jan 25, 2006, 10:45 PM
Could be this is not possible :/. I certainly haven't seen anything promising thus far.

Thanks anyway. It does seem like it's impossible, but I'm not giving up yet. It just seems like such an obvious and simple thing. If it comes down to it, I might fire off an email to Firaxis support and hope it makes its way to the right people to make a change in a future patch. I did find a function called getModulePathName, but it doesn't seem to return any useful value.

The name: Well, let's just say there is a logic to it, and it's not creepy ;); however, I'm not a real doctor, and I don't jiggle. It's sort of an inside joke that only I understand.

SimCutie
Jan 26, 2006, 12:43 AM
Try My Main interface screen mod. It includes function to find out what is active mod player has selected.

http://forums.civfanatics.com/showthread.php?t=147018

See CvutilCustom.py line 43. the "SelectedMod" global variable has the value you want.

Dr Elmer Jiggle
Jan 26, 2006, 07:20 AM
Try My Main interface screen mod. It includes function to find out what is active mod player has selected.

Thanks! Boy, that sure doesn't look like a simple process. As far as I can tell, what you're doing is going through all the mods in the Mods directory and attempting to import their modules. If the import succeeds, then that mod must be on the module search path, so it's the active mod. If it fails, then that mod isn't on the module search path, so it's not the active mod. Interesting approach, though I sure wish there was an easier way.

I also wonder if there's a potential problem with this algorithm. Imagine someone has 3 different mods installed (call them A, B, and C), each of which implements a custom event handler using the module name CvCustomEventHandler. If mod B is active, won't your import also succeed when you're looking at mods A and C, since the import is based simply on the module name? More specifically, I imagine the sequence of events will go something like this:


Iterate over the Python modules in mod A

Attempt to import CvCustomEventHandler
It succeeds (since there is a CvCustomEventHandler.py in Mods/B/Assets/Python), so we assume A is the active mod

Iterate over the Python modules in mod B

Attempt to import CvCustomEventHandler
It succeeds (since there is a CvCustomEventHandler.py in Mods/B/Assets/Python), so we assume B is the active mod

Iterate over the Python modules in mod C

Attempt to import CvCustomEventHandler
It succeeds (since there is a CvCustomEventHandler.py in Mods/B/Assets/Python), so we assume C is the active mod



Still, I don't mean to complain. Thanks a lot. This is already a lot closer than I was before.