Replacing Python on a Battlefield 2 server

Lately I’ve been doing some work (I promise it is real work) with Battlefield 2. The server part of the game has a cut-down version of Python built in so that you can interact with it, modifying aspects of the game and reacting to events etc. More details on this can be found on this wiki.

Anyway, the Python version it uses is pretty old (2.3.4, current version is 2.5.2) and it’s had functionality cut, e.g. no threading support. For the work I’m doing I needed threading support and a few other things so wanted to swap out the version of Python for another – turns out this wasn’t obvious so here are the steps:

  1. Replace the Battlefield 2 Python library (e.g. ‘bf2/bin/ia-32/libdice_py.so’) with the version of your choice. I just linked to my locally installed version ‘/usr/lib/libpython2.5.so’.
  2. Replace the Python library path at runtime to use the new libraries, e.g. edit ‘bf2/python/bf2/__init__.py’ and putting the equivalent of ‘sys.path = [‘/usr/lib/python2.5/’, ‘/usr/lib/python2.5/lib-dynload/’, ‘python’, ‘mods/bf2/python’, ‘admin’]’ after the ‘import sys’ line.

I do get the following warning at runtime: ‘sys:1: RuntimeWarning: Python C API version mismatch for module host: This Python has API version 1013, module host has version 1012.’ but it doesn’t seem to cause any problems (so far!).

Advertisements

28 thoughts on “Replacing Python on a Battlefield 2 server

  1. So is this running in a stable manner for you? I have some url calls I would like to make in the game and this would be a welcome help.

  2. So is this running in a stable manner for you? I have some url calls I would like to make in the game and this would be a welcome help.

  3. Well that is great news. Have you been able to get url calls working properly? I don’t know much about python, but recoding our in game management system has been a fun experience. Right now, I’d like to use a url call to allow admins to register their keyhash via a php page and get a success or fail notification in game.

  4. Well that is great news. Have you been able to get url calls working properly? I don't know much about python, but recoding our in game management system has been a fun experience. Right now, I'd like to use a url call to allow admins to register their keyhash via a php page and get a success or fail notification in game.

  5. Yep. Requests for webpages in the existing python return garbage results so far. I’d like to be able to have a python script call a webpage, parse the results, then display the results in game.

  6. Something like this should do the job:import urllibwebPageHandle = urllib.urlopen(‘https://blog.garethj.com/’)webPageContents = webpage.read()Then obviously you’ll have to parse webPageContents for the information you need and use the BF2 API to send that into the game. This page looks like a good tutorial if you need more help: http://www.voidspace.org.uk/python/articles/url…Hope that’s useful 🙂

  7. Yep. Requests for webpages in the existing python return garbage results so far. I'd like to be able to have a python script call a webpage, parse the results, then display the results in game.

  8. Yes. Thank you.Originally I wanted to use post methods, but I now think a simple get is more efficent for my level of coding. It also means that I can use the standard python lib that comes with bf2. I appreciate the help.

  9. Yes. Thank you.Originally I wanted to use post methods, but I now think a simple get is more efficent for my level of coding. It also means that I can use the standard python lib that comes with bf2. I appreciate the help.

  10. I tried your method and when the server starts it crashes with “Import Error:” and flashes back to shell. I am using python2.4 and have changed the usr/lib/python2.5 to usr/lib64/python2.4 any clues?

  11. I tried your method and when the server starts it crashes with “Import Error:” and flashes back to shell. I am using python2.4 and have changed the usr/lib/python2.5 to usr/lib64/python2.4 any clues?

  12. What exactly have you changed? Have you replaced (or linked) the libdice_py.so in the Battlefield directory with your own and editing the __init__.py? If that’s all done, there might be an issue with replacing a 32-bit Python with a 64-bit one (which is what your directory names suggest) so that could be the cause.

  13. What exactly have you changed? Have you replaced (or linked) the libdice_py.so in the Battlefield directory with your own and editing the __init__.py? If that's all done, there might be an issue with replacing a 32-bit Python with a 64-bit one (which is what your directory names suggest) so that could be the cause.

  14. I 1. Replace the Battlefield 2 Python library (e.g. ‘bf2/bin/ia-32/libdice_py.so’) with the version of your choice. I just linked to my locally installed version ‘/usr/lib/libpython2.5.so’. 2. Replace the Python library path at runtime to use the new libraries, e.g. edit ‘bf2/python/bf2/__init__.py’ and putting the equivalent of ’sys.path = [‘/usr/lib/python2.5/’, ‘/usr/lib/python2.5/lib-dynload/’, ‘python’, ‘mods/bf2/python’, ‘admin’]‘ after the ‘import sys’ line.Is what I did, but I did substitute my version for 2.5. I used locate to find the directories for python. I run the 64 bit version of bf2.exe so I figured 64bit libs would be a proper replacement.

  15. I 1. Replace the Battlefield 2 Python library (e.g. ‘bf2/bin/ia-32/libdice_py.so’) with the version of your choice. I just linked to my locally installed version ‘/usr/lib/libpython2.5.so’. 2. Replace the Python library path at runtime to use the new libraries, e.g. edit ‘bf2/python/bf2/__init__.py’ and putting the equivalent of ’sys.path = ['/usr/lib/python2.5/', '/usr/lib/python2.5/lib-dynload/', 'python', 'mods/bf2/python', 'admin']‘ after the ‘import sys’ line.Is what I did, but I did substitute my version for 2.5. I used locate to find the directories for python. I run the 64 bit version of bf2.exe so I figured 64bit libs would be a proper replacement.

  16. So sounds like you’ve done everything correctly. I assume you’ve kept the relative locations in the sys.path such as ‘python’, ‘mods/bd2/python’ and ‘admin’ as these are battlefield specific libraries it will try to load? Failing that, try the 32 bit version – maybe BF2 always runs a 32bit Python regardless of which version the game is running.

  17. So sounds like you've done everything correctly. I assume you've kept the relative locations in the sys.path such as 'python', 'mods/bd2/python' and 'admin' as these are battlefield specific libraries it will try to load? Failing that, try the 32 bit version – maybe BF2 always runs a 32bit Python regardless of which version the game is running.

  18. So sounds like you've done everything correctly. I assume you've kept the relative locations in the sys.path such as 'python', 'mods/bd2/python' and 'admin' as these are battlefield specific libraries it will try to load? Failing that, try the 32 bit version – maybe BF2 always runs a 32bit Python regardless of which version the game is running.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s