Quantcast

Jump to content

» «
Photo

[REL|SRC] SCM and DirectX Hooking Source Code

285 replies to this topic
Spooky
  • Spooky

    Prince of the Yolkfolk

  • Members
  • Joined: 13 Jan 2002

#1

Posted 30 September 2004 - 07:18 PM

Hey modders, as promised here's the source code for the Analog Speedometer Mod which demonstrates how to execute scm opcodes from an external program and also how to hook the IDirect3D8 and IDirect3DDevice8 interfaces in order to draw custom stuff in Vice City.

It's written in C++ and requires the DirectX 8.1 SDK (this is also included in the later DirectX 9 SDKs, available here - 227mb). It should compile using any recent version of Microsoft Visual C++, but it was developed and has only been tested in version 7.1 (.NET).

I've very heavily commented the files GameScripting.cpp and Custom.cpp which contain the scm hooking code and the speedometer stuff respectively. I went a bit crazy with the commenting and i've put quite a bit of info in the ReadMe.txt for the source code and also on the download page, but if you have any problems please post here or email me.

And now for the downloads and info:
http://spookie.powerpill.co.uk/speedo/

Happy modding!

Kryptos
  • Kryptos

    Hacker

  • Members
  • Joined: 05 Nov 2003

#2

Posted 30 September 2004 - 07:20 PM Edited by Kryptos, 30 September 2004 - 07:32 PM.

I'm speechless... This is incredible, it's a real push to the open source nature of this community. Outstanding.

The source code is great, especially with the heavy commenting, something I tend to shy away from. Most of my code is illegible to most other people because I don't include comments whatsoever, or very few at most.

Great job on this Spookie, it's amazing.

Un3462
  • Un3462

    Mack Pimp

  • Members
  • Joined: 27 Jun 2002

#3

Posted 30 September 2004 - 08:13 PM

good work, nice and clever execution. one question though; why muck around in global var space when you've already created a threadstruct with 16 locals (partype 3) begging to be used?

TwoZero
  • TwoZero

    Ghetto Star

  • The Connection
  • Joined: 15 Apr 2003

#4

Posted 30 September 2004 - 08:24 PM

Thank you very much for the source... very interesting smile.gif

Spooky
  • Spooky

    Prince of the Yolkfolk

  • Members
  • Joined: 13 Jan 2002

#5

Posted 30 September 2004 - 08:48 PM

QUOTE (CyQ @ Sep 30 2004, 21:13)
good work, nice and clever execution. one question though; why muck around in global var space when you've already created a threadstruct with 16 locals (partype 3) begging to be used?

I'd wrote that when I only knew what a few of the thread struct members were, before you game me the whole thing.

Even afterwards I didn't think of it, but yeah using the locals would be cleaner smile.gif

steve-m
  • steve-m

  • Feroci
  • Joined: 26 Jul 2002

#6

Posted 01 October 2004 - 02:02 AM

Wow, great stuff Spooky, and also thanks for the detailed commenting.

Since i'm a Delphi freak, I might try to convert the opcode executor if I find the time. Could get interesting, due to the use of varargs... wink.gif

And I promise to also publish source codes of final versions of my tools in the future.

PatrickW
  • PatrickW

    GTA Juggernaut

  • The Connection
  • Joined: 07 Jan 2004
  • Netherlands
  • Best Script 2013 [DYOM]
    Best Script 2012 [DYOM]

#7

Posted 01 October 2004 - 02:19 AM

This is great spooky, by far the coolest speedo i've seen for GTA, and that only as a small demo project for the new GTA modding technology, you guys uncovered.
cookie.gif cookie.gif cookie.gif

Do you have any idea what the performance of scm code executed through this hook is, in relation to running it from within the mian.scm ?.

If the performance penalty is not too big, it would be great to do mission coding in C++ inlove.gif

More stars for spooky and sheep !!!!

Luke
  • Luke

    suckmyrocket

  • Moderator
  • Joined: 01 Dec 2003
  • None

#8

Posted 01 October 2004 - 02:59 PM

Truly brilliant. smile.gif I look forward to the rest of projectx

Smithers2 gets goes out to buy a learn C++ in 24 hours book

Hammer83
  • Hammer83

    Hustler

  • Members
  • Joined: 11 Jan 2003

#9

Posted 02 October 2004 - 12:06 AM

Amazing, thank you very? very much.

ST.MU, can't wait for Delphi counterpart smile.gif

Again, thanks a lot.

ghost of delete key
  • ghost of delete key

    aggronormical

  • The Connection
  • Joined: 27 Dec 2003
  • None

#10

Posted 02 October 2004 - 11:35 AM

QUOTE (ST.MU @ Sep 30 2004, 22:02)
Since i'm a Delphi freak, ...

Huh. Didn't know. nervous.gif

Ya learn something new every day, whether you want to or not. turn.gif

I've recently seen the light of Delphi, myself. Good gravy, it is.


QUOTE
I might try to convert the opcode executor if I find the time.
Could get interesting, due to the use of varargs... wink.gif


What version are you on?
I'm using Delphi 7 Enterprise, and C++ Builder 6. I haven't seen any earlier versions, so I don't know about them, but the Delphi helpfiles in v7 have extensive C++ cross-references, which is a beautiful thing.


QUOTE
And I promise to also publish source codes of final versions of my tools in the future.


soo-WEET!

will that also include your works to-date?

(say 'yes', say 'yes', say 'yes' inlove.gif )

we need to talk under the table...

after I get some sleep


BTW @ Spooky-

Absolutely great stuff, I've envisioned something like this, but have been mostly clueless up till now. Thanx immensely for the edumacashum, it will not go to waste biggrin.gif

I smell an opensource project coming around the corner, just in time for SA4PC.
Just thinking out loud.

JernejL
  • JernejL

    Big Homie

  • Feroci
  • Joined: 11 Mar 2002

#11

Posted 02 October 2004 - 02:13 PM

QUOTE (ST.MU @ Oct 1 2004, 03:02)
Since i'm a Delphi freak, I might try to convert the opcode executor if I find the time. Could get interesting, due to the use of varargs... wink.gif

there will alaways be help wink.gif

Hammer83
  • Hammer83

    Hustler

  • Members
  • Joined: 11 Jan 2003

#12

Posted 02 October 2004 - 08:38 PM

Delfi, if only every great modders retired the way you did... inlove.gif biggrin.gif

DexX
  • DexX

    Black Hat

  • Feroci
  • Joined: 16 May 2002

#13

Posted 02 October 2004 - 11:20 PM Edited by ashdexx, 02 October 2004 - 11:56 PM.

QUOTE (Hammer83 @ Oct 2 2004, 14:38)
Delfi, if only every great modders retired the way you did...  inlove.gif   biggrin.gif

I've said it before... lol.gif

Spooky, or anyone else who knows the answers, i have a few in depth questions about the technical potential for this, which is clearly very high.

Coding... (c++)
For example, could you hook a trainer-like function into the code, that would alter the memory addresses of the police weapons, to a different weapon, every time im wasted / busted (using this method of scripting)? The purpose being to write to any address in the game, like a trainer, along with the scm functions, as some of the hardcoded stuff cannot be changed via normal scm-scripting.

Art... (directx)
Or how about the material for the water in vice. I think it sucks, and swapping out the texture itself doesnt help much. Is / could there be any way to overwrite the material used by the game, to a new one? If so, would i have the full power of DX8 at my disposal, for other effects? EMBP (environment mapped bump mapping) would look lovely on the water, for instance. Or, can i overwrite any materials in the game? bump mapping on the roads would be a nice touch.

These arent requests for features, they're questions of technical feasability. So please, indulge me smile.gif

Spooky
  • Spooky

    Prince of the Yolkfolk

  • Members
  • Joined: 13 Jan 2002

#14

Posted 03 October 2004 - 02:46 AM

Thanks to everyone for the kind words and support smile.gif


QUOTE (ST.MU @ Oct 1 2004, 03:02)
Since I'm a Delphi freak, I might try to convert the opcode executor if I find the time. Could get interesting, due to the use of varargs...

I'm a still a Delphi freak at heart smile.gif It was just easier for the DX stuff using C++. It would be interesting converting this... I think the dynamic array of Variant type would work, but I've still never used it myself.


QUOTE (ST.MU @ Oct 1 2004, 03:02)
And I promise to also publish source codes of final versions of my tools in the future.

Don't get me wrong by my other posts, I'm not an open source nut now, I just don't agree with not sharing gta modding information among this community when this community has shared so much with everyone. This is something you could never be accused of as you've helped tirelessly in almost all sections of modding gta.

I'm not saying don't share your source, as I've seen from sharing mine it can help people even if it's just for language translation but don't feel obligated to do so.


QUOTE (PatrickW @ Oct 1 2004, 03:19)
Do you have any idea what the performance of scm code executed through this hook is, in relation to running it from within the mian.scm ?.

It'll have a bit more overhead with the script stub generation, but the actual number of assembly instructions that get executed to generate it isn't very many, so I can't see it being a noticeable difference.


QUOTE (PatrickW @ Oct 1 2004, 03:19)
If the performance penalty is not too big, it would be great to do mission coding in C++

Structuring proper missions might be a bit more challenging than when using the normal scripting language, for example in C++ you couldn't make a loop to check if the player is in a specific position or something, you'd need to do this check once per frame otherwise the game would be frozen until your loop breaks. You could of course make another (windows) thread for your mission scripts... hmm. This has me thinking now smile.gif . Doing this you'd also have to know that when you're executing opcodes - the game isn't. Managing multiple threads can get a bit tricky.


QUOTE (ghost of delete key @ Oct 2 2004, 12:35)
I smell an opensource project coming around the corner

It would be very cool to do an open source community project with this kinda stuff...


QUOTE (ashdexx @ Oct 3 2004, 00:20)
Coding... (c++)
For example, could you hook a trainer-like function into the code, that would alter the memory addresses of the police weapons, to a different weapon, every time im wasted / busted (using this method of scripting)? The purpose being to write to any address in the game, like a trainer, along with the scm functions, as some of the hardcoded stuff cannot be changed via normal scm-scripting.

Yep, without a problem. (Most) trainers use WriteProcessMemory in order to write to the games memory, and if it's a recurring thing such as your example, it's usually on a timer that would use ReadProcessMemory to figure out if you're wasted/busted and if so use WriteProcessMemory to change the police guns. In our case we're running from a dll inside the game's process, so you can access the memory directly. e.g.

if the wasted/busted flag was a byte at 0x00123456, and the police gun was a dword at 0x00654321, you could simply use...

CODE
BYTE* bWastedBusted = (BYTE*)0x00123456
DWORD* dwPoliceGun = (DWORD*)0x00654321
if (*bWastedBusted) *dwPoliceGun = 1;

This would check if you're wasted/busted and if so change the police gun to type 1. I hope this makes sense, I'm writing it on the fly, and reading back it doesn't seem very clear wink.gif .


QUOTE (ashdexx @ Oct 3 2004, 00:20)
Art... (directx)
Or how about the material for the water in vice. I think it sucks, and swapping out the texture itself doesnt help much. Is / could there be any way to overwrite the material used by the game, to a new one? If so, would i have the full power of DX8 at my disposal, for other effects? EMBP (environment mapped bump mapping) would look lovely on the water, for instance. Or, can i overwrite any materials in the game? bump mapping on the roads would be a nice touch.

Again this is not a problem, anything the game does with Direct3D8, you have control over with the hook. The game will render the water and roads with DrawPrimitive at some stage, the only tricky part is figuring out which call is to draw the roads and which to draw the water...

There's a few basic ways I know of determining what is currently being drawn, they are -
  • Counting vertices.
    When calling DrawPrimitive the game specifies how many vertices are in the passed vertices buffer, and it'll usually range from around 24 for a box to thousands for a building. If you know how many vertices are in say the water you can compare for that within the hooked DrawPrimitive function and if found, either stop the call and do your own drawing or simply modify their call.

  • Texture checking.
    Before a call to DrawPrimitive you have to set the texture to be wrapped onto the vertices using SetTexture. If you did a check on the texture passed to SetTexture to see if it's the water texture, you could set a flag in your code so subsequent calls to DrawPrimitive know it is the water being drawn. When SetTexture is called again and it's not the water texture, reset the flag.

  • Return address.
    This is a bit more of a low level check - If the game draws the water, roads, building, actors etc from different parts of the game's code, you can check return address of the call to DrawPrimitive on the stack, if it's equal to the one you've determined is the water, do your stuff.
If you decide to go ahead with the stuff mentioned and need any help, don't hesitate to ask smile.gif .


</novel>

DexX
  • DexX

    Black Hat

  • Feroci
  • Joined: 16 May 2002

#15

Posted 03 October 2004 - 03:26 AM Edited by ashdexx, 03 October 2004 - 03:48 AM.

Whoa, holy s***!

QUOTE
Spooky's post novel


Well, this is all very good and bad. Good for the community to have such power at their fingertips, and in my case bad, since i dont know enough c++ to write a small trainer (and i've tried!) sad.gif
well...no, i DID work, but it could only write to one address, and had no gui. it was lame.

but i simply wont be able to rest until i make some tweaks to Vice's graphics..

i just had another realization - lighting is also controlled by directx isn't it..
Spooky, your going to be the death of me cookie.gif

SoLo_
  • SoLo_

    Rat

  • Members
  • Joined: 27 Apr 2004

#16

Posted 03 October 2004 - 04:55 AM

Wow, and I thought Nutter's would get as good as it would ever be... dontgetit.gif VERY nice man, VERY nice.

GTA3Freak-2001
  • GTA3Freak-2001

    Run out of Cake.

  • Members
  • Joined: 25 Nov 2001

#17

Posted 03 October 2004 - 07:18 AM

Wow this is amazing, truly a step ahead in GTA modding nice qork spooky smile.gif

kyeman
  • kyeman

    Snitch

  • Members
  • Joined: 13 Sep 2004

#18

Posted 03 October 2004 - 04:03 PM

Very cool indeed.

I just wanted to throw some ideas on the table. Since there is plenty of info on how to call into the scm interpreter directly, and that most of the functional scm instructions relate to either Game, Player, Actor, Vehicle, Object, or Mission, the opportunity is there to implement these scm instructions as methods of OLE/ActiveX objects. What this could do is make GTA mission coding and modding available to Javascript, VBscript and PERLscript, in a completely object oriented manner using the Windows Scripting Host

It might be something to think about for San Andreas perhaps. I'm sure there would have been alot more user created missions had it been easier to code them.

Kryptos
  • Kryptos

    Hacker

  • Members
  • Joined: 05 Nov 2003

#19

Posted 03 October 2004 - 05:50 PM

I've been thinking about coding a makeshift SCM interpreter that reads Perl-based syntax. It would then merely use spooky/sheep's method of directly using the interpreter to input the script at runtime. It's a pretty lame idea, but at least it would give people a way of directly debugging missions at runtime and using a nicer syntax, in my opinion anyway. I've always wanted to see a compiler capable of giving a GTA modder the same abilities that MSVC++ has, namely in the debugging field. It's a lot easier then writing a quick SCM script and then debugging it by going back and forth a few hundred times; of course I should mention that Kyeman/eAi's exception handler is an amazing accomplishment as well for SCM debugging. Anyhow, great information and great ideas.

dans
  • dans

    Snitch

  • Members
  • Joined: 29 Jul 2002

#20

Posted 03 October 2004 - 09:07 PM

QUOTE (Spooky @ Oct 3 2004, 02:46)
QUOTE (ST.MU @ Oct 1 2004, 03:02)
And I promise to also publish source codes of final versions of my tools in the future.

Don't get me wrong by my other posts, I'm not an open source nut now, I just don't agree with not sharing gta modding information among this community when this community has shared so much with everyone. This is something you could never be accused of as you've helped tirelessly in almost all sections of modding gta.

I'm not saying don't share your source, as I've seen from sharing mine it can help people even if it's just for language translation but don't feel obligated to do so.

I've bugged steve-o for the realease of his tools every sunday when he's online on ICQ. My calendar have "bug steve-o" on every sunday for a year ahead!

He _will_ release his sources! sly.gif

Spooky
  • Spooky

    Prince of the Yolkfolk

  • Members
  • Joined: 13 Jan 2002

#21

Posted 04 October 2004 - 07:10 PM

QUOTE (kyeman @ Oct 3 2004, 17:03)
I just wanted to throw some ideas on the table. Since there is plenty of info on how to call into the scm interpreter directly, and that most of the functional scm instructions relate to either Game, Player, Actor, Vehicle, Object, or Mission, the opportunity is there to implement these scm instructions as methods of OLE/ActiveX objects. What this could do is make GTA mission coding and modding available to Javascript, VBscript and PERLscript, in a completely object oriented manner using the Windows Scripting Host

Great idea! Although I've had no experience making ActiveX objects myself, so are you offering? smile.gif

It might get a bit difficult when people want help in a specific scriping language, but having that freedom of choice would be cool.


QUOTE (Kryptos @ Oct 3 2004, 18:50)
I've been thinking about coding a makeshift SCM interpreter that reads Perl-based syntax. It would then merely use spooky/sheep's method of directly using the interpreter to input the script at runtime. It's a pretty lame idea, but at least it would give people a way of directly debugging missions at runtime and using a nicer syntax, in my opinion anyway.

That'd be cool, it's not a lame idea. If you can do it, go for it smile.gif

SoLo_
  • SoLo_

    Rat

  • Members
  • Joined: 27 Apr 2004

#22

Posted 04 October 2004 - 09:22 PM

The edges of the speedo aren't smoothed out for me like that, I'vr tried changing my res already. I have no idea, I might need to change some settings on my vid card?

rallysiim
  • rallysiim

    Ghost.

  • Members
  • Joined: 09 Jan 2004

#23

Posted 05 October 2004 - 12:59 PM

I've noticed a little bug, when you change the radio station, the speedo disappears and comes back when the game has tuned in on the next station happy.gif

vALKYR
  • vALKYR

    The Object of Affection.

  • Andolini Mafia Family
  • Joined: 01 Jun 2004

#24

Posted 05 October 2004 - 07:47 PM

Stop making so damn good mods, I don't want to stand up and get my VC CD to install it again sigh.gif

Nice mate !

kyeman
  • kyeman

    Snitch

  • Members
  • Joined: 13 Sep 2004

#25

Posted 06 October 2004 - 04:58 AM

QUOTE (Spooky)
Great idea! Although I've had no experience making ActiveX objects myself, so are you offering?


I haven't had much time for any recreational coding lately, but I'll make a start on it when I do though, if nobody else does first. It's relatively simple if you make use of the msscript control. Implementing all the scms as methods will be the hardest part.

Un3462
  • Un3462

    Mack Pimp

  • Members
  • Joined: 27 Jun 2002

#26

Posted 06 October 2004 - 01:12 PM

wouldn't it be better/easier to see which functions the scm opcodes actually call, and instead document those, combine them with known memory addresses, and try to reconstruct the real vehicle/actor/scriptthread objects from that (in which case the ps2 gta3 exe with embedded function names would help a lot)?

Squiddy
  • Squiddy

    Back!

  • The Connection
  • Joined: 06 Oct 2004

#27

Posted 06 October 2004 - 05:47 PM

Hi.
I know this may not be the right thread for this, but i don't want to open a new one. At first my respect to this wonderful project!

I talked to Spookie yesterday on IRC, he helped me a lot. But one problem couldn't be solved yet. I'm trying to get the actual car health.

First attemp: Using a scm opcode

CODE
const SCRIPT_COMMAND get_car_health    = { 0x0227, "vv" };
...
float fCurrentHealth = 0.0;
if (!ScriptCommand(&car_stopped, &dwPlayerCar))    // Is the car moving?...
{
ScriptCommand(&get_car_health, &dwPlayerCar, &fCurrentHealth); // Get the car health
}

It returns 0. confused.gif

Second attemp: Getting the value from memory
CODE
float* fCarHealth=(float*)GameGetVehicle(dwPlayerCar);
fCarHealth += 516;

float fCarHealth2=*fCarHealth;

Same here. blink.gif


What's wrong? Please help me, i can't find a mistake.
Greets Squiddy

LT_McGyver
  • LT_McGyver

    King

  • Members
  • Joined: 30 Jan 2004

#28

Posted 06 October 2004 - 06:43 PM

This is the best speedometer i've seen. And the only one i use smile.gif . BUT...there seems to be a problem... I don't know is it just my pc or something, but this speedometer doesn't work with GTA: LC confused.gif . I've tried reinstalling the game...and gtalc... Anyone have the same problem?

PatrickW
  • PatrickW

    GTA Juggernaut

  • The Connection
  • Joined: 07 Jan 2004
  • Netherlands
  • Best Script 2013 [DYOM]
    Best Script 2012 [DYOM]

#29

Posted 06 October 2004 - 07:25 PM

QUOTE (_Squiddy_ @ Oct 6 2004, 19:47)

I talked to Spookie yesterday on IRC, he helped me a lot. But one problem couldn't be solved yet. I'm trying to get the actual car health.

First attemp: Using a scm opcode

CODE
const SCRIPT_COMMAND get_car_health    = { 0x0227, "vv" };
...
float fCurrentHealth = 0.0;
if (!ScriptCommand(&car_stopped, &dwPlayerCar))    // Is the car moving?...
{
ScriptCommand(&get_car_health, &dwPlayerCar, &fCurrentHealth); // Get the car health
}


I'm not sure it solves the problem, but I think car health is returned as an integer value, and you're putting it in a float variable. Thus interpreting the returned bit-pattern as a float, while is was meant to be a Integer.

Just my $0.021

Vice Master
  • Vice Master

    Vrooom!

  • Members
  • Joined: 07 Feb 2004

#30

Posted 06 October 2004 - 07:44 PM

Well, whenever I copy the thing into the root directory the game crashes. is there anything else I should be doing? blink.gif




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users