AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Module Coding (https://forums.alliedmods.net/forumdisplay.php?f=9)
-   -   some questions ( CString, CVector, Ham ) (https://forums.alliedmods.net/showthread.php?t=99108)

AntiBots 08-01-2009 16:21

some questions ( CString, CVector, Ham )
 
1# I have to use CString.h or string.h

2# I have to use CVector.h or vector.h

3# How work Ham with Metamod. Because I see that RegisterHam work diferent than Fakemeta or Engine.

Metamod dont exec func in Ham ( Well the function like Player Pre Think ).


Plis, sorry my english.

jim_yang 08-01-2009 21:40

Re: some questions ( CString, CVector, Ham )
 
about 1#,2#, if you want your plugins compiled only in windows, you can use stl, if you want them to be compiled in both win and linux, use the library amxx supplied.
about #3, you mean why ham don't hook engine or dll function?

AntiBots 08-02-2009 17:03

Re: some questions ( CString, CVector, Ham )
 
Quote:

Originally Posted by jim_yang (Post 887999)
about 1#,2#, if you want your plugins compiled only in windows, you can use stl, if you want them to be compiled in both win and linux, use the library amxx supplied.

Thanks :D

Quote:

Originally Posted by jim_yang (Post 887999)
about #3, you mean why ham don't hook engine or dll function?

Well. If I see moduleconfig that dont define any HL Func
PHP Code:

// #define FN_PlayerPreThink PlayerPreThink /* pfnPlayerPreThink() */ 

But the module use PreThink.

How Ham Hook HL or Metamod events?

Else: I have to use define of ARRAYSIZE of winnt or eiface?

jim_yang 08-02-2009 22:59

Re: some questions ( CString, CVector, Ham )
 
Hamsandwish hooks the virtual functions(vfuncs) of a class, to explain clearly, I'll give an example.
Here is the Class Hierachy:
Code:

CBaseEntity
    CBaseDelay
        CBaseToggle
            CBaseItem
            CBaseMonster
                CBaseCycler
                CBasePlayer
                CBaseGroup

You can see that CBaseEntity is the base class of all other entity classes.
Here is part of vfuncs from CBasePlayer class
Code:

0    CBasePlayer::Spawn(void)
1    CBasePlayer::Precache(void)
2    CBaseEntity::Restart(void)
3    CBaseMonster::KeyValue(KeyValueData_s *)
4    CBasePlayer::Save(CSave &)
5    CBasePlayer::Restore(CRestore &)
6    CBasePlayer::ObjectCaps(void)
7    CBaseEntity::Activate(void)
8    CBaseEntity::SetObjectCollisionBox(void)
9    CBasePlayer::Classify(void)
10  CBaseEntity::DeathNotice(entvars_s *)
11  CBasePlayer::TraceAttack(entvars_s *,float,Vector,TraceResult *,int)
12  CBasePlayer::TakeDamage(entvars_s *,entvars_s *,float,int)

You can see that some functions from CBasePlayer override the original function from CBaseEntity
Code:

RegisterHam(Ham_TakeDamage, "player", "my_hook_func", 0 or 1) //pre or post
To hook the function CBasePlayer::TakeDamage, ham first spawn an entity with given classname, because different classes have different TakeDamage function, so we should specify which class we want to hook.
Virtual table(vtbl) stores in each object of a class, it stores the virtual function's address(pointer). When an obj call its virtual function, it will look up the vtbl to find its real address in memory.
Code:

CBasePlayer *pPlayer;
pPlayer->Spawn();

Numver 0 - 12 above is the index of these vfuncs in vtbl. You can find these numbers in hamdata.ini from configs folder
Ham does the followings to hook CBasePlayer::TakeDamage function:
(1)spawn a "player" entity
(2)get its vtbl address
(3)get its vtbl index from hamdata(actually this have been done when ham init)
(4)get the vfunc address
(5)generate a trampoline function
(6)changed the original vfunc address in vtbl to the trampoline function address(hooking started)
about the trampoline function, it looks like this:
Code:

Tramp()
{
    //prehook
    return_value = my_hook_func(original parameters)
    if(return_value == HAM_SUPERSEDE)
        return
   
    //call the original function
    this->TakeDamage(modified parameters from our prehook function)
   
    //posthook
    my_hook_func()
}

I'm not good at explaining things because of my poor English, so I didn't explain the c++ part, such as class, hierachy, virtual function..., you can find these in any c++ book.
===================
if you want to use ARRAYSIZE, use the hlsdk version

AntiBots 09-08-2009 21:25

Re: some questions ( CString, CVector, Ham )
 
@jim: I can use Trampoline.h from Ham, Or I have to make a new?

Is you have time, only show how to hook Ham_Spawn, Example.
Only the code, without explain.
I was traing but sometimes work and others no. :D

Thanks!!!

jim_yang 09-09-2009 04:15

Re: some questions ( CString, CVector, Ham )
 
Code:

#include "amxxmodule.h"
#include <windows.h>
#define HOOK_Spawn 0
void *pOriginalFunction = NULL;
void Hook_Spawn()
{
    //save this pointer
    void *pthis;
    __asm mov pthis, ecx;
    //pre hook
    SERVER_PRINT("::Spawn(void) prehook!\n");
    //call original function
    __asm mov ecx, pthis;
    __asm call [pOriginalFunction];
    //post hook
    SERVER_PRINT("::Spawn(void) posthook!\n");
}
void Hook()
{
    //get classname of the entity you want to hook
    const char *classname = CMD_ARGV(1);
    edict_t *pEdict = CREATE_ENTITY();
    CALL_GAME_ENTITY(PLID, classname, &pEdict->v);
    if(pEdict->pvPrivateData == NULL)
    {
        REMOVE_ENTITY(pEdict);
        return;
    }
    //get this pointer
    void *pthis = pEdict->pvPrivateData;
    //get vtable
    void **vtbl = *(void ***)pthis;
    REMOVE_ENTITY(pEdict);
    if(vtbl == NULL)
        return;
   
    int **ivtbl = (int **)vtbl;
    //get the original function
    pOriginalFunction = (void *)ivtbl[HOOK_Spawn];
   
    //patch original function to our function
    DWORD oldflags;
    if(VirtualProtect(&ivtbl[HOOK_Spawn], sizeof(int *), PAGE_READWRITE, &oldflags))
    {
        ivtbl[HOOK_Spawn] = (int *)Hook_Spawn;
    }
}
void OnMetaAttach()
{
    REG_SVR_COMMAND("hook", Hook);
}

ecx store this pointer, you need to do some calculate to get the edict_t *
in windows this calc is
Code:

mov eax, [ecx + 4];  get this->pev
mov eax, [eax + 520]; get pev->pContainingEntity
mov pEdict, eax


AntiBots 09-10-2009 12:45

Re: some questions ( CString, CVector, Ham )
 
Wow Jim, Work perfect. Also the edict_t :D

Now I am Traing to Transport Trampoline from Ham to my Module. First Test Work :D

pRoxxxDD 07-31-2011 08:16

Re: some questions ( CString, CVector, Ham )
 
Quote:

Originally Posted by jim_yang (Post 927573)
[code]
Code:

mov eax, [ecx + 4];  get this->pev
mov eax, [eax + 520]; get pev->pContainingEntity
mov pEdict, eax


Can you explain it ?

jim_yang 07-31-2011 08:41

Re: some questions ( CString, CVector, Ham )
 
if you know how a struct store in memory, you will know the asm part above
Code:

0        string_t        classname;
1        string_t        globalname;

2        vec3_t                origin;
5        vec3_t                oldorigin;
8        vec3_t                velocity;
11        vec3_t                basevelocity;
14        vec3_t              clbasevelocity;

17        vec3_t                movedir;

20        vec3_t                angles;
23        vec3_t                avelocity;
26        vec3_t                punchangle;
29        vec3_t                v_angle;

32        vec3_t                endpos;
35        vec3_t                startpos;
38        float                impacttime;
39        float                starttime;

40        int                fixangle;
41        float                idealpitch;
42        float                pitch_speed;
43        float                ideal_yaw;
44        float                yaw_speed;

45        int                modelindex;
46        string_t        model;

47        int                viewmodel;
48        int                weaponmodel;
       
49        vec3_t                absmin;
52        vec3_t                absmax;
55        vec3_t                mins;
58        vec3_t                maxs;
61        vec3_t                size;

64        float                ltime;
65        float                nextthink;

66        int                movetype;
67        int                solid;

68        int                skin;
69        int                body;
70        int                effects;
       
71        float                gravity;
72        float                friction;
       
73        int                light_level;

74        int                sequence;
75        int                gaitsequence;
76        float                frame;
77        float                animtime;
78        float                framerate;
79        byte                controller[4];
80        byte                blending[2];

81        float                scale;

82        int                rendermode;
83        float                renderamt;
84        vec3_t                rendercolor;
87        int                renderfx;

88        float                health;
89        float                frags;
90        int                weapons;
91        float                takedamage;

92        int                deadflag;
93        vec3_t                view_ofs;

96        int                button;
97        int                impulse;

98        edict_t                *chain;
99        edict_t                *dmg_inflictor;
100        edict_t                *enemy;
101        edict_t                *aiment;
102        edict_t                *owner;
103        edict_t                *groundentity;

104        int                spawnflags;
105        int                flags;
       
106        int                colormap;
107        int                team;

108        float                max_health;
109        float                teleport_time;
110        float                armortype;
111        float                armorvalue;
112        int                waterlevel;
113        int                watertype;
       
114        string_t        target;
115        string_t        targetname;
116        string_t        netname;
117        string_t        message;

118        float                dmg_take;
119        float                dmg_save;
120        float                dmg;
121        float                dmgtime;
       
122        string_t        noise;
123        string_t        noise1;
124        string_t        noise2;
125        string_t        noise3;
       
126        float                speed;
127        float                air_finished;
128        float                pain_finished;
129        float                radsuit_finished;
       
130        edict_t                *pContainingEntity;

131        int                playerclass;
132        float                maxspeed;

133        float                fov;
134        int                weaponanim;

135        int                pushmsec;

136        int                bInDuck;
137        int                flTimeStepSound;
138        int                flSwimTime;
139        int                flDuckTime;
140        int                iStepLeft;
141        float                flFallVelocity;

142        int                gamestate;

143        int                oldbuttons;

144        int                groupinfo;

        // For mods
145        int                iuser1;
146        int                iuser2;
147        int                iuser3;
148        int                iuser4;
149        float                fuser1;
150        float                fuser2;
151        float                fuser3;
152        float                fuser4;
153        vec3_t                vuser1;
156        vec3_t                vuser2;
159        vec3_t                vuser3;
162        vec3_t                vuser4;
165        edict_t                *euser1;
166        edict_t                *euser2;
167        edict_t                *euser3;
168        edict_t                *euser4;

the pev struct in windows, its offset is 1 from "this" pointer, and pev->pContainingEntity is 130 in the pev struct, each take 4 byte means 130 * 4 = 520,
so this->pev->pContainingEntity, you will get the entity itself from its class object.

Shadows In Rain 07-31-2011 09:17

Re: some questions ( CString, CVector, Ham )
 
Quote:

Originally Posted by jim_yang (Post 887999)
if you want your plugins compiled only in windows, you can use stl, if you want them to be compiled in both win and linux, use the library amxx supplied.

LOLWUT? STL is part of C++ standart library. It comes with almost compilers.

http://en.wikipedia.org/wiki/Standard_Template_Library
http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library
http://en.wikipedia.org/wiki/Core_language

Also, files with extension ".h" are deprecated, use files without ".h" (read links):
#include <string>
#include <vector>


All times are GMT -4. The time now is 15:06.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.