Epic GTA2 Script Decompiler (AKA SCR Decompiler)

Anything to do with GTA1/GTA2 modding (tools, scripts and more).
User avatar
Sektor
Boss
Boss
Posts: 1423
Joined: 04 Mar 2008, 06:51
GH nick: Sektor
Location: GTAMP.com
Contact:

Re: SCR decompiler

Post by Sektor »

T.M. wrote:The compiler starts crying after your script exceeds 45544 bytes
Vike discovered it was 45536 bytes and that is B1E0 in miss2.exe. Change B1E0 to F0FF to increase limit to 61695 bytes. On my test script, the compiler gives an assertion error at more than 56332 bytes but that's still better than 45536.

Official compiler with those bytes changed: http://gtamp.com/GTA2/gta2script.7z

Scriptpad compatible version here.
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

Sektor wrote:Vike discovered it was 45536 bytes and that is B1E0 in miss2.exe. Change B1E0 to F0FF to increase limit to 61695 bytes.
Interesting! 45544-45536 = 8 bytes! and guess what? the first pointer takes 8 bytes of script data! mystery solved: that 8 bytes doesnt belong into script at all, its some sort of header they never get to use.
Sektor wrote:On my test script, the compiler gives an assertion error at more than 56332 bytes but that's still better than 45536.
Even MORE interesting!: i had found out earlier that maximum mission script size = 56260, but its tricky to test because i had to use minimal base-script, and the base script size affects on how big mission script you can make, so i think the limit you found is the real limit and that could also be extended!! :mrgreen:
Pyro wrote:Did you modify the SCR by hex in any way to make the trailer (well, train in your case) attach to car? Always thought adding trailers to cars normally (via MIS) was bugged and didn't work even using proper cars like a Truck Cab (both versions) and any trailer.
I didnt, only if you make it as a CAR_DATA declaration with 2D coordinates (compiler is buggy and will make trailer with same value as the car model). there are many maps with cars with trailers, nothing bugged in it! unless you place them wrong, probably thats why you havent been able to make them since you have no clue how it looks in game while you write the coords by hands ;)
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

Now i understand why i couldnt find any strings in the exes: JernejL had packed the exes with upx! edit: compiler still slow even after unpacking.

Edit: i noticed the new compiler makes weird error messages: "List index out of bounds (-1)" which appears randomly once i make enough big script, and i have never seen it before! did we break the compiler now? maybe theres some other things to change in the exe?
weirderror.png
weirderror.png (6.97 KiB) Viewed 46999 times
Edit2: no, that cant be compiler.exe error, its something to do with mispad.exe... maximum lines limited to 2000? :D edit3: nah, tested with more than 2000 lines, something to do with the script size, not lines...
User avatar
B-$hep
Immortal
Posts: 568
Joined: 24 Apr 2009, 21:43
GH nick: B-Shep
Location: EU

Re: SCR decompiler

Post by B-$hep »

List index out of bounds is very common error within Delphi apps.
If you use TList's, ListBoxes, Memos and such.

Very easy to "get out of bounds" without enough error checking.
Always wear safety glasses while programming.
User avatar
Pyro
Immortal
Posts: 414
Joined: 17 Mar 2010, 04:07
GH nick: Pyro
Location: Wales, UK

Re: SCR decompiler

Post by Pyro »

Just messed about with trailers on cars, and your right about it only working with CAR_DATA (was using PARKED_CAR_DATA previously which doesn't work). Maybe another one to add to list of weirdness. ;-)
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

Pyro wrote:Just messed about with trailers on cars, and your right about it only working with CAR_DATA (was using PARKED_CAR_DATA previously which doesn't work). Maybe another one to add to list of weirdness. ;-)
Nice, didnt know that! Guess what? I hacked the SCR file again... AND IT WORKS :D LOL so i can make the trailer for those! gta2 supports it but compiler doesnt! damn... who wants me to make the compiler already..? :lol: i do! i start to understand why you guys always use 3d coords btw: because compiler is buggy, some commands works only for cars that have 3d coords etc. :shock: i think i need to find out which commands do that and work around it in my decompiler: atm i just convert 3d to 2d whenever possible, but that might break the scripts after recompile with the bugged compiler. edit: i changed my decompiler to always output 3d coords! too much trouble with all the checks to avoid compiler crashes!
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

How should the output IF-structures format be? I looked at the official mission scripts for bil.scr (those that came with compiler) and noticed they used a format similar to:

Code: Select all

IF ((((counter1 = 1)
AND (counter2 = 1) )
AND ((counter3 = 1) OR (counter5 = 1)) )
AND (counter4 = 1) )
	// code here.
ENDIF
When i took some time to think about it, i thought it makes sense: the braces after IF command tells how many checks total that one IF structure has! brilliant! and after the IF command the AND/OR commands will tell how many sub-commands does it have at that line! i added space before the last brace for each line except the first IF because then i can debug and check that the first braces count after AND/OR is same as the braces count at end of that line.

What do you think?

--

Other news: i made indentations (and extra lines padding) for the subroutines, at same time i found out that you can use RETURN command inside WHILE_EXEC loop (and probably for WHILE loop as well), it seems to terminate the loop.
Here is how the indentations look like from decompiled script:
[mis]THREAD_TRIGGER trigger18 = THREAD_WAIT_FOR_CHAR_IN_CAR (p1, auto60, function18:)
function18:
START_BASIC_KF_TEMPLATE (trigger18, 4311, auto60, p1, TANK_GUN)
bonus2 = START_BONUS_CHECK (NO_ZONE, 3600, 30, 0, CAR, NOT_EXCLUSIVE, BY_ROCKET_LAUNCHER, TANK, NONE)
DO_BASIC_KF_TEMPLATE (bonus2, timer1, 120, onscreen1, savecount107, 30, 4311, p1, LIVES, 1)
RETURN

THREAD_TRIGGER trigger19 = THREAD_WAIT_FOR_CHAR_IN_CAR (p1, auto61, function19:)

function19:
START_BASIC_KF_TEMPLATE (trigger19, 4312, auto61, p1, CAR_MACHINE_GUN)
bonus3 = START_BONUS_CHECK (NO_ZONE, 3600, 60, 0, CHAR, NOT_EXCLUSIVE, BY_GUN, JEEP, NO_OCCUPATION)
DO_BASIC_KF_TEMPLATE (bonus3, timer1, 120, onscreen1, savecount107, 60, 4312, p1, LIVES, 1)
RETURN
[/mis]
edit: tested mis highlighter.
Last edited by T.M. on 22 Nov 2011, 15:32, edited 1 time in total.
User avatar
elypter
Immortal
Posts: 1120
Joined: 26 Dec 2009, 23:53
GH nick: elypter

Re: SCR decompiler

Post by elypter »

looks like a good way of displaying statements for me
yur sa'nok ngeyä
BenMillard
Immortal
Posts: 889
Joined: 16 May 2009, 06:14
GH nick: BenMillard
Location: London, UK
Contact:

Re: SCR decompiler

Post by BenMillard »

Amazing progress; this remains a great community. There's always something new to discover in GTA2!
User avatar
B-$hep
Immortal
Posts: 568
Joined: 24 Apr 2009, 21:43
GH nick: B-Shep
Location: EU

Re: SCR decompiler

Post by B-$hep »

T.M i have messed with compiler construction tools in few months ago. I wanted to make my own simple language toy compiler, but pretty much lost my interest.

You could use few tools to make the task easier. Many of them will generate wrapper fro different languages, including C / C++.

Pretty popular is Gold Parser Builder:
http://goldparser.org/

It's free and i recommend you to use latest version 5.x not the 4.x
It makes writing compiler alot easier. It does alot of work for you, which would be pretty difficult and boring to make by yourself.

Anyway, spend some time on the webpage, read the faq and stuff.
If you have some questions not answered in webpage, you could ask me also.
I will try to help you. I have messed with GPB pretty alot.

Another is ANTLR.
http://www.antlr.org/

Don't know much about it, but it seems more complicated. Im not sure.
I didn't like it.
Always wear safety glasses while programming.
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

I managed to make CHECK_MULTIPLIER_GREATER work by hacking the SCR file!

At least i think so, here is the script i used:

Code: Select all

PLAYER_PED p1 = (102.0, 218.5, 255.0) 12 90
CHAR_DATA chr1 // if referred at, the value is 2 in SCR.
counter done = 0

LEVELSTART

while_exec(done = 0)
    if(CHECK_HEADS_GREATER(p1, 1))
        ADD_MULTIPLIER (p1, 1)
    endif
    if(CHECK_CHAR_BEEN_PUNCHED_BY(p1, chr1)) // CHECK_MULTIPLIER_GREATER (p1, 2)
        kill_char(p1)
    endif
endwhile

LEVELEND
then i searched for the SCR code of CHECK_CHAR_BEEN_PUNCHED_BY and replaced with CHECK_MULTIPLIER_GREATER code which is "C400" in hex. and when i got my heads greater than 1, i started to die! so i guess it worked.

Its almost like they first made support for all the commands in GTA2 and then wrote the compiler to generate those codes, and then they ran out of time or just didnt add support for some commands because they never used them?
User avatar
Sektor
Boss
Boss
Posts: 1423
Joined: 04 Mar 2008, 06:51
GH nick: Sektor
Location: GTAMP.com
Contact:

Re: SCR decompiler

Post by Sektor »

Not sure if I will ever use CHECK_MULTIPLIER_GREATER but its cool to have more power. I hope you can find some other commands.

gta2.exe was made before miss2.exe and I think they copied a lot of GTA1 code.
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

I noticed (again) that all angles must be in range 0-359 for the compiler (it doesnt even make proper warning for that, just absurd error), but i can hack SCR and put any value i want and GTA2 will use it as expected: result = angle % 360, wonder why they made it so hard lol, there are other values that should be limited to some ranges but those werent limited like the angles were, i guess they got tired of checking them after that :D

I've been fixing the decompiler a bit recently: changed some integer values to be signed instead of unsigned. and some other fixes i forgot... oh and i optimized it slightly too, im not using string arrays anymore since i noticed they added a lot pointless overhead, so im using char arrays instead, this way i got like 80k smaller exe or something like that :S I also optimized the reader a bit: i wont copy the header from each command anymore, instead i take the pointer to it, therefore i also removed the header from each of the structs definitions (gladly i used a macro before so it was really easy to remove). i also extended the header size to 8 bytes from 6 bytes, since now when i know all the commands i am sure it is 8 bytes now, this gave some extra work too: i converted all structs to use same macro, hopefully i didnt create any bugs while doing so.
User avatar
Pyro
Immortal
Posts: 414
Joined: 17 Mar 2010, 04:07
GH nick: Pyro
Location: Wales, UK

Re: SCR decompiler

Post by Pyro »

Agree with Sektor - never used the CHECK_MULTIPLIER_GREATER myself but handy to have in some form or another.

With the angles, I've always been surprised that angle 0° for things is actually south rather than north (in GTA2 script, north is 180°) and it's even weirder for phones as they use their own unique angle system, where north for phones is 270°. Not sure why they did that, could be to do with the way the phone sprite in STY is shown. Anyway, I've always used a range between 0°-359°, not sure why anyone would want to enter 360° when it's the same as 0°.

Still, a lot of oddities with some code with things like coordinate types (integer or floating point) and ranges between values. As Sektor said, a lot of code was probably used from GTA1.
User avatar
Sektor
Boss
Boss
Posts: 1423
Joined: 04 Mar 2008, 06:51
GH nick: Sektor
Location: GTAMP.com
Contact:

Re: SCR decompiler

Post by Sektor »

T.M, can you please attach the latest decompiler code to the first post? Thanks.
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

What are you going to do with it? If the DLL isnt working: you need to compile both projects with "Multi-threaded DLL (/MD)" setting. Oh and you probably want the function definition too:
[syntax="cpp"]__declspec(dllimport) int decompile_scr(const string &base_script, string &base_script_output, const vector<string> &mission_scripts, vector<string> &mission_scripts_output);[/syntax]

I'll add the full source code soon, need to make some final changes...
User avatar
Sektor
Boss
Boss
Posts: 1423
Joined: 04 Mar 2008, 06:51
GH nick: Sektor
Location: GTAMP.com
Contact:

Re: SCR decompiler

Post by Sektor »

I wasn't going to do anything but I noticed the attached code has no mini_car and Vike wanted mini_car SCR code, to help him find mini_car in the gta2.exe.
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

Oh, here is the struct and comments explain the MINI_CAR:
[syntax="cpp"]struct SCR_CAR_DATA_DEC {
SCR_CMD_FORMAT(
Uint16 varname;
Uint16 unk3;
SCR_XYZ_f pos;
Uint16 rot;
Sint16 remap;
Uint16 car_id;
Uint16 trailer_id; // 0xFFFF if no trailer, 0xFFFE if MINI_CAR.
);
};[/syntax]
User avatar
elypter
Immortal
Posts: 1120
Joined: 26 Dec 2009, 23:53
GH nick: elypter

Re: SCR decompiler

Post by elypter »

is there theoretically a way to make a command that allows to declare parked gang cars?
yur sa'nok ngeyä
User avatar
T.M.
Immortal
Posts: 960
Joined: 29 Jan 2010, 15:00
Location: F21B3EED

Re: SCR decompiler

Post by T.M. »

in theory everything is possible if you have enough time ;)

but in reality: no, vike isnt going to do that, its absurdly complex.

there is no identifier in the SCR code that declares something as gang car and something as parked car, those both are just separate commands:
CREATE_GANG_CAR1
CREATE_GANG_CAR2
CREATE_GANG_CAR3
CREATE_GANG_CAR4
PARKED_CAR_DECSET_2D_STR
PARKED_CAR_DECSET_3D_STR
PARKED_CAR_DECSET_2D
PARKED_CAR_DECSET_3D

anyways, why would you need that ?
Post Reply