.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   Half-Life 2 SDK (http://forums.bots-united.com/forumdisplay.php?f=62)
-   -   Hooking into HL2 DLL ? (http://forums.bots-united.com/showthread.php?t=3400)

stefanhendriks 12-01-2005 21:04

Hooking into HL2 DLL ?
 
I got to this link via the hlcoding mailing list,

http://www.sourcemod.net/forums/viewtopic.php?t=441

i don't understand it yet. But basicly its telling how to HOOK (aka HL1 style!?) into HL2 and such... if possible, this could be more interesting...

Cpl. Shrike 12-01-2005 21:20

Re: Hooking into HL2 DLL ?
 
Reading..... brain hurts o_O
Keeps reading

Pierre-Marie Baty 12-01-2005 21:42

Re: Hooking into HL2 DLL ?
 
that guy had a clever idea. However, if I understand well, as soon as the interface changes a bit, he's left to rearrange all his function pointers in the vtable. I'm not sure it's very practical. It doesn't seem to bother the AMX guys though, as they are used to get/set info where they shouldn't, such as in HL1 edict's private data...

sfx1999 12-01-2005 23:23

Re: Hooking into HL2 DLL ?
 
You could probably code it so that if there is a new interface, it bypasses the hook.

Pierre-Marie Baty 13-01-2005 02:33

Re: Hooking into HL2 DLL ?
 
huh?
what would be the point ?

sfx1999 13-01-2005 04:59

Re: Hooking into HL2 DLL ?
 
Well, you can hook whatever functions you need, and have the ones you don't bypass it. You can also make it so that if new functions come around, they could also bypass it.

Pierre-Marie Baty 13-01-2005 15:42

Re: Hooking into HL2 DLL ?
 
No, you don't get it. The order in which the functions are may be arranged everytime a new interface version is defined. Contrarily to HL1. That's the purpose of this "interface factory" to ensure consistency between DLLs using different versions of the interface. Unless he cares about the version of the interface he hooks onto (but I don't see him doing that), he'll be in big trouble at any minor revision of the interface.

dub 17-01-2005 01:18

Re: Hooking into HL2 DLL ?
 
i thought about an easier way using the plugin interface. Technically if we chain the plugin, Is it not possible too dump any information between the server -> plugin interface like weapon bit id`s (really needed at the moment).

botman 17-01-2005 01:38

Re: Hooking into HL2 DLL ?
 
What do you mean by "chain the plugin"?

Plugins in Half-Life2 are implemented similar to the way they are in Metamod. The plugin does not sit between the engine and the game DLL.

In Half-Life2, the engine can call plugin functions when (after) game DLL functions are called. It's more of a branch off from the main engine/gameDLL connection than it is a connection between them.

For example, the engine calls the game DLL GameFrame() function, then when the game DLL returns back to the engine, the engine calls it for plugin A, then plugin B, then plugin C, like this...

engine->GameDLL::GameFrame()
<- GameDLL::GameFrame() returns back to the engine
engine->PluginA::GameFrame()
<- PluginA::GameFrame() returns back to then engine
engine->PluginB::GameFrame()
<- PluginB::GameFrame() returns back to the engine

...it's NOT like this...

engine->PluginA::GameFrame()->PluginB::GameFrame()->GameDLL::GameFrame()

..that would be more like the way the HPB bot hooks the game DLL. The Half-Life2 plugin system does not work that way.

botman

dub 17-01-2005 01:46

Re: Hooking into HL2 DLL ?
 
botman what i mean is engine call`s the hook plugin, then hook plugin calls the real plugin.
Have a hook dll that sit`s in between dumping various information on engine functions passed to the real plugin.

edit
found an intresting hooking library for injecting your own code into a remote process windows only here

Lazy 17-01-2005 03:06

Re: Hooking into HL2 DLL ?
 
Maybe you could do something like that if you managed to get in-between the engine and the gamedll.
Injection would require a loader but you could also look into proxying a dll that the server uses like wsock32.dll and hooking LoadLibrary from there which you'd return a handle to your dll rather than the server's.
You wouldn't even need a loader and since it would be loaded while windows sets up the exe to be run you'd have no problems with hooking something too late.

The only problem I noticed was with HL1 where I got something like CCodeErrorException after a while.
I did manage to see some libraries being loaded before it crashed.

( Note: Above is an example to add a clock into the client while running in fullscreen, nothing else )

dub 17-01-2005 10:07

Re: Hooking into HL2 DLL ?
 
There`s a method of hooking the directx 9 dll in gta:vc and creating a speedometer overlay on the screen done by a guy named spooky here.
This method could intresting for hooking into the game but in xp sp2 has this new memory protection, so im not sure how easy it would be (basically all it does is unables anything to write into a remote process other than it`s own, microsoft attempt at stopping buffer overrun`s). One idea i thought about is to change the hWnd, hThread of the inject process to the same as the process you want to hook into then inject quickly and exit (hopefully confuse windows). im not sure if LoadLibraryA method would work with xp sp2`s new memory protection.

Lazy 17-01-2005 16:10

Re: Hooking into HL2 DLL ?
 
If I remember correctly you can also get your dll injected by using SetWindowsHookEx, the only problem may be hooking too late.

dub 17-01-2005 16:25

Re: Hooking into HL2 DLL ?
 
send idHook = WH_GETMESSAGE then break in on WM_CREATE to inject. Surely that wouldn`t break in too late ?
edit..
create a thread to check for the executable with above normal priority

Lazy 17-01-2005 16:29

Re: Hooking into HL2 DLL ?
 
I think you need a valid hWnd to set a windows hook though :(. I just wonder if its possible to make an exe loader which runs it under our program's address space which would allow changing of imports without injecting a dll.
( Note: Not thought out very well )

dub 17-01-2005 16:33

Re: Hooking into HL2 DLL ?
 
you could use CreateProcessA or CreateToolHelp32Snapshot & Module32First, Module32Next
ex. here using CreateToolHelp32Snapshot, Module32First & Module32Next to get the hWnd

Pierre-Marie Baty 17-01-2005 19:58

Re: Hooking into HL2 DLL ?
 
these are ugly hacks, I definitely wouldn't want to use them :(

anyway, let me correct one thing:
Quote:

For example, the engine calls the game DLL GameFrame() function, then when the game DLL returns back to the engine, the engine calls it for plugin A, then plugin B, then plugin C, like this...

engine->GameDLL::GameFrame()
<- GameDLL::GameFrame() returns back to the engine
engine->PluginA::GameFrame()
<- PluginA::GameFrame() returns back to then engine
engine->PluginB::GameFrame()
<- PluginB::GameFrame() returns back to the engine
Technically speaking, it's the other way around in metamod : the game DLL functions are called AFTER all the plugins hooks (which enables us to set a MRES_SUPERCEDE to prevent the real function from being called). Same goes for engine functions. For hooking a function AFTER the actual call, we use metamod's *_Post function tables.

This is actually what we would need in HL2.

dub 17-01-2005 21:50

Re: Hooking into HL2 DLL ?
 
Quote:

these are ugly hacks, I definitely wouldn't want to use them :(
yeah ugly hacks, just exchanging various ways to go about hooking into hl2 :D. Pierre never know these methods could come in handy someday.

botman 18-01-2005 03:41

Re: Hooking into HL2 DLL ?
 
ugly, and won't work on Linux.

botman

stefanhendriks 18-01-2005 09:11

Re: Hooking into HL2 DLL ?
 
I just read the bots here, using the runplayermove botman had 'invented' stopped working. Perhaps its time for more ugly methods? I dunno.

dub 18-01-2005 14:18

Re: Hooking into HL2 DLL ?
 
Quote:

ugly, and won't work on Linux.
yeah, i need some linux/unix coding practice, never coded in linux/unix enviroment before.

found in CBasePlayer, might help.
Code:

// Run a user command. The default implementation calls ::PlayerRunCommand. In TF, this controls a vehicle if
        // the player is in one.
 virtual void                        PlayerRunCommand (CUserCmd *ucmd, IMoveHelper *moveHelper);

only problem is, i think this is for vehicles only. I tried with passing NULL on the movehelper and hl crashed :'(.

Cheeseh 18-01-2005 15:37

Re: Hooking into HL2 DLL ?
 
botmans method DOES still work. It's the CBasePlayer->ProcessUsercmds() that doesn't work anymore (as of latest cs:s update) I found the cbaseplayer method much tider though than the buffer writing and stuff, but hell it looks like it's gonna be the only way to do it. :)

dub 18-01-2005 18:58

Re: Hooking into HL2 DLL ?
 
cheeseh i thought botmans method was using the ProcessUsercmds, to get the bots to process movement.

SteveC 18-01-2005 19:51

Re: Hooking into HL2 DLL ?
 
The CBasePlayer->ProcessUserCommands() trick (which I started) doesn't work anymore as has been said, but the CBasePlayer->PlayerRunCommand() doesn't work either, even if you do pass the correct movehelper pointer. The debug error is that the function isn't returning as it expected so it sounds like Valve changed the CBasePlayer class, and therefore changed the virtual function table. However the SDK hasn't been updated (?), so currently we can't use the CBasePlayer class.
I don't know if Valve changed the CBasePlayer to stop us using it or if it was to fix some bugs. I think it's just something they had to do to get the bots working.

PS. Botmans way will always work, because it is based on building a network message. (until they re-write the netcode)

SteveC 18-01-2005 20:10

Re: Hooking into HL2 DLL ?
 
PS! They have changed stuff, because you can now add bots in HL2: DM !

dub 18-01-2005 20:20

Re: Hooking into HL2 DLL ?
 
maybe they chaged it to give us a way to access CBasePlayer & CBaseEntity without using any hacks too.

SteveC 18-01-2005 20:24

Re: Hooking into HL2 DLL ?
 
Hopefully. But we'll just have to wait.

Cheeseh 18-01-2005 20:30

Re: Hooking into HL2 DLL ?
 
botmans method is this ...

PHP Code:


void CBot 
:: runPlayerMove()
{
    static 
CUserCmd cmd;
    static 
bf_write write_buf;
    static 
bf_read read_buf;
    static 
unsigned char buffer[CMD_BUFFER_SIZE];  // shared buffer between write_buf and read_buf (EVIL!!!)

    //////////////////////////////////
    
memset(&cmd0sizeof(cmd));
    
//////////////////////////////////
    
cmd.forwardmove m_fForwardSpeed;
    
cmd.sidemove m_fSideSpeed;
    
cmd.upmove m_fUpSpeed;
    
cmd.buttons m_iButtons;
    
cmd.impulse m_iImpulse;
    
cmd.viewangles m_vViewAngles;

    
// SteveC:
    // this changes the fixangle from absolute (which isn't coded for) to none
    // note: has to be run here because it doesn't work in the addbot routine?!?!?
    
m_pBaseEdict->PlayerData()->fixangle FIXANGLE_NONE;
    
    
//////////////////////////////////
    
write_buf.StartWritingbufferCMD_BUFFER_SIZE );
    
WriteUsercmd( &write_buf, &cmd );  // by magic the same data appears in the read_buf!!!
    
read_buf.StartReadingbufferCMD_BUFFER_SIZE );  // this is truly EVIL!!!
    
gameclients->ProcessUsercmds(m_pEdict, &read_buf110falsefalse);
    
//////////////////////////////////


it still works, but as said before (again :P) SteveC's CBasePlayer->ProcessUserCmds doesn't work anymore

Cheeseh 18-01-2005 23:39

Re: Hooking into HL2 DLL ?
 
Quote:

Originally Posted by SteveC
PS! They have changed stuff, because you can now add bots in HL2: DM !

your own bots? when I try it still crashes in createFakeclient()

SteveC 19-01-2005 01:01

Re: Hooking into HL2 DLL ?
 
Yeah I had my own bot in HL2: DM. I hadn't converted my movement code so it didn't do anything, just floated there. But it was in the game, in a team, and alive. It died too with a high speed barrel :D !

I haven't done anything special, just refreshed the SDK content which might have changed stuff.

botman 19-01-2005 01:56

Re: Hooking into HL2 DLL ?
 
"PS. Botmans way will always work, because it is based on building a network message. (until they re-write the netcode)"

Heh-heh! :)

botman

Pierre-Marie Baty 19-01-2005 02:49

Re: Hooking into HL2 DLL ?
 
Just tossing this...
Maybe the hooking method isn't impossible, what about implementing the functionality of the interface factory in the hook DLL ? We would "just" need to know about a particular version of the interface and stick to it (which involves of course the need to know where to read and write for this particular version). It sounds like it would work OK for one side of the interface, but I'm wondering about the other...

sfx1999 19-01-2005 03:43

Re: Hooking into HL2 DLL ?
 
You know it might work if the netcode was hooked, but that would probably be more evil than hooking the interfaces.

Pierre-Marie Baty 19-01-2005 04:45

Re: Hooking into HL2 DLL ?
 
huh ? I don't get it again. I wasn't talking about netcode, but about the virtual function tables... ???:(

stefanhendriks 19-01-2005 09:39

Re: Hooking into HL2 DLL ?
 
Quote:

Originally Posted by SteveC
The CBasePlayer->ProcessUserCommands() trick (which I started) doesn't work anymore as has been said, but the CBasePlayer->PlayerRunCommand() doesn't work either, even if you do pass the correct movehelper pointer. The debug error is that the function isn't returning as it expected so it sounds like Valve changed the CBasePlayer class, and therefore changed the virtual function table. However the SDK hasn't been updated (?), so currently we can't use the CBasePlayer class.
I don't know if Valve changed the CBasePlayer to stop us using it or if it was to fix some bugs. I think it's just something they had to do to get the bots working.

PS. Botmans way will always work, because it is based on building a network message. (until they re-write the netcode)

rofl well i only converted code that i considered nescesary to do it directly through cBasePlayer, so in my case my bots should still work fine ;)

stefanhendriks 19-01-2005 09:41

Re: Hooking into HL2 DLL ?
 
Also, if any SDK release comes soon, we can hope for a fully basic bot plugin from Valve, which i find very neat from them! :)

Cheeseh 19-01-2005 18:47

Re: Hooking into HL2 DLL ?
 
Quote:

Originally Posted by SteveC
Yeah I had my own bot in HL2: DM. I hadn't converted my movement code so it didn't do anything, just floated there. But it was in the game, in a team, and alive. It died too with a high speed barrel :D !

I haven't done anything special, just refreshed the SDK content which might have changed stuff.

I have no joy here, I've refreshed game content, still no dice ! same thing happens (engine->createFakeclient() crashes.)

SteveC 19-01-2005 18:49

Re: Hooking into HL2 DLL ?
 
Quote:

Originally Posted by stefanhendriks
rofl well i only converted code that i considered nescesary to do it directly through cBasePlayer, so in my case my bots should still work fine ;)

Yeah, I dived in a bit quick with using lots of useful stuff within the CBasePlayer, like choosing and changing weapons, detecting damage etc. So I'm gonna wait a week until I start undoing it all. :(

dub 19-01-2005 19:21

Re: Hooking into HL2 DLL ?
 
My stupidity getting mixed up with CBasePlayer & IServerGameClients Processusercmds (i was messing with the ubframe and not steve`s template) and wasn`t using my brain :).

sfx1999 20-01-2005 03:23

Re: Hooking into HL2 DLL ?
 
Quote:

Originally Posted by Pierre-Marie Baty
huh ? I don't get it again. I wasn't talking about netcode, but about the virtual function tables... ???:(

I am saying there is always the brute force method of simulating an actual client connecting. You could pass the server data to the bots through a plugin.


All times are GMT +2. The time now is 01:00.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.