Python and binary files

posted in Blue stain for project Shoot
Published March 08, 2012
Advertisement
Hello.
That was a long silence. I would like to say I worked a lot on the game but I can't. The only thing I coded is the subject of this entry: a python script to "patch" binary files. I am posting it here as it could be useful to someone.
I tested it with Python 2.6 and 3.2. If you want to use the "crc" command, the "crcmod" package is needed.

The python script is used to apply modifications to one or more binary files using a really simple scripting "language". You can:
- read/write/swap values
- do simple calculations
- insert bytes (from other files or using a specified value)
- import other files inside the one being processed
- export/delete parts of the file
- use loops, if/else, macros
- send parameters to the script using the command line
- calculate crc, xor, ...

A small sample script to add 1 to each even byte of a binary file (even length) can be:

cursor 0 //set cursor to the first byte (it is done by default, this line is not needed)
loop SIZE2 #1 // SIZE2 is a constant representing the size of the file divided by 2, #1 is the variable 1 used to store the current loop index
read @0 #0 // read the byte at cursor position and store its value in variable 0
sum #0 1 #0 // add 1 to the variable 0 and store the result in variable 0
write @0 #0 // write at the cursor position the value of the variable 0
cursor @2 // move the cursor 2 bytes
end_loop


Commands and usage are described in the python file header.

Archive content:
- patch_bin.py: the python script
- demo.patch: demo script
- demo.bin: binary file to update with the demo patch
- import.bin: small binary file imported in the demo.bin by the demo patch

patch_bin.tar.gz
Previous Entry Alien test
Next Entry 3D Video
1 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

Not my game but...

1469 views

Spaceship update

1840 views

Big WIP

2290 views

Late creation

1318 views

Hidden gold

2178 views

Effects

2389 views

Let's rock

3257 views

Pick-ups

2410 views
Advertisement