Force Lod0 patch in Python

Posted by TomMK 
Force Lod0 patch in Python
Date: December 04, 2010 11:33PM
Posted by: TomMK
I'm trying to create a patch using Python (3.1) that forces GP4 to use a car's Lod 0 only (just like Lo2k's TeamEditor). If any of the programmers here can help me I'd be very grateful! Here's my code so far:

exe = open("gp4.exe", 'rb';)
data = exe.read()

data = data.replace(b'CAR_%s_CAR%d_LOD_%d', b'CAR_%s_CAR%d_LOD_0';)

patch = open("GP4patched.exe", 'wb';)
patch.write(data)
patch.close()

The script creates GP4patched.exe, but when I run it, Windows says "not a valid Win32 application". Do you know what I am doing wrong?



Edited 1 time(s). Last edit at 12/05/2010 09:01AM by TomMK.
Re: Force Lod0 patch in Python
Date: December 05, 2010 11:25AM
Posted by: Lo2k
maybe it lacks a final null char for the lod_0 string, because original string is 19 char long and your lod_0 one is only 18.





Edited 2 time(s). Last edit at 12/05/2010 11:25AM by Lo2k.
Re: Force Lod0 patch in Python
Date: December 05, 2010 09:13PM
Posted by: ZaZ
You read the whole file as a string and do a replace on all the data, which is never a good idea.
Last time I used Python was a long time ago, but I know that since 3.1 there were some changes in string handling for binary mode.
You could look at the documentation for this.
Anyway, it will still be a bad approach to do binary file operations like this.
Why not determine the fileversion and simply use a seek to go the the necessary offset and patch it instead of overwriting the entire file?





I'd rather have a bottle in front of me than a frontal lobotomy
Re: Force Lod0 patch in Python
Date: December 05, 2010 11:41PM
Posted by: TomMK
Out of interest, what language do you guys program your tools in? I'm good with Java but that's all, and it's not really suited to this type of program.

Lo2k - I'll try adding a null char to the end of my string.

ZaZ - would the offset always be the same even if the exe had been previously patched by another tool?

Thanks for the help guys, appreciate it!
Re: Force Lod0 patch in Python
Date: December 06, 2010 01:56AM
Posted by: ZaZ
I just use about any language. Depends which is best for the job. My favs are C#,C++,VB,Delphi. Don't like Java too much, it's too messy.
If you say you are good with Java, then you shouldn't have too much trouble with other modern generation languages though.
I don't know anything about your requirements, but I'd stay away from Python or Java.
Some suggestions:
Try one of the express editions of visual studio (.net dependency if your are not paying attention)
For simple patching you could use any C compiler. I like LCC for those things
To write something in Delphi give Lazarus a try. It's maturing rapidly. (could also be used to do patching, but more overhead)





I'd rather have a bottle in front of me than a frontal lobotomy
Re: Force Lod0 patch in Python
Date: December 06, 2010 05:59AM
Posted by: TomMK
C# looks like it might be a good language for me as it has similarities to Java. I'll check it out, thanks ZaZ.

I want to make two tools for GP4, mainly for my own use. One is this "force lod0" patch. The second will take a performance file and patch GP4.exe with it. Basically two things that TeamEditor does already!!

Lo2k - you could add these two functions to TeamPatcher and save me the effort..? ;)
Re: Force Lod0 patch in Python
Date: December 06, 2010 09:18AM
Posted by: TomMK
Added a null char to the end of my string and the patch works, thanks Lo2k!

exe = open("gp4.exe", 'rb';)
data = exe.read()

data = data.replace(b'CAR_%s_CAR%d_LOD_%d', b'CAR_%s_CAR%d_LOD_0\0';)

patch = open("GP4patched.exe", 'wb';)
patch.write(data)
patch.close()
Re: Force Lod0 patch in Python
Date: December 06, 2010 07:35PM
Posted by: Lo2k
Nice to hear that it worked :)

Concerning your query, I doubt I will add these features so soon...

Sorry, only registered users may post in this forum.

Click here to login

Maintainer: mortal, stephan | Design: stephan, Lo2k | Moderatoren: mortal, TomMK, Noog, stephan | Downloads: Lo2k | Supported by: Atlassian Experts Berlin | Forum Rules | Policy