-4
\$\begingroup\$

I'm writing an independent code which I'll import into an exe, Here is an self-contained example:

This code is a PCSTATE handler - basically when lara starts walking - this code will be executed. It checks for button presses and specific states (i.e. when an enemy is nearby and the action button is pressed - l(char, 0x128) == 2).

enum LARA_ANIM //lara animation IDs
{
    LARA_RUN_TURN180, //here given only
    LARA_RUNJUMP //2 for obvious reasons
}; //code will be too long

#define playIsSourceAnim ((int (*)(char *,enum LARA_ANIM))0x005530AC) //an address that is a function in the host executable

#define printf ((int (*)(char const* const, ...))0x005D4280)

#define playWalkingCallback ((int (*)(char *, unsigned int))0x00553618)

#define playStandingCallback ((int (*)(char *, unsigned int))0x005535F0)

#define playSwimCheckStatus_ForSurface ((int (*)(char *, float *,float))0x000578020)

#define flt0 (*(float*)0x07FA4D8) //unusued

#define fxTriggerRipple1 ((int (*)(float (*)[4], float, float))0x004CF3E4)
#define fxRandf ((float (*)(float))0x004B56C4)
#define fxGetWaterHeight ((float (*)(float (*)[4]))0x004D4D08)
#define WadeRippleCounter (*(int *)0x07B5904)
#define playCheckStepUp ((int (*)(char*,int))0x00552C40)
#define playHandleStandardJumpRequest ((int (*volatile)(char*))0x0056CC1C)
#define playStartRoll ((int (*)(char*))0x005719E8)
#define playHandleRunJumpRequest ((int (*)(char*))0x056CB38)    
#define playRunningCallback ((int (*)(char*, unsigned int))0x553650)
#define playBlendAnimationCB ((int (*)(char*,enum LARA_ANIM,int,int (*)(char *, unsigned int),enum PCSTATE))0x005532C4)


#define cast(type, p) ((type) (p)) //code sugar
#define casta(type, p) *((type *) (p)) //code sugar 1

#define l(type, p) (*(*(cast(type  **, Sp) + 0x150 / 4) + (p) / sizeof(type))) //OK so at 0x150 is a structure, Sp is our entry function first argument and the first argument of most of our calls

#define m60 l(float, 0x60) //m60 is the lara rotation
//change the above field to make her rotate
#define playSetAnimRateByAnalogStick \
cast(int(*)(char *, float, float, float, float), 0x56DFD0) //here
//we are using the syntax sugar

//#define float_const(p, )

#define gAnimTargetMS cast(float *,0x0926C08)

#define gplayCollisionInfo cast(char *,0x007F9480)

_forceinline float vfl_partl(unsigned int j) { return *cast(float*, &j); }

#define vfl_part(a,b, c, d, r) f##a##c##b = vfl_partl(r)//fapb=floathexvalue

#define vfl(a,b, f) vfl_part(a,b,p,.,f)//here we are using a helper macro
//to pass all the needed part of the variable name - p and f . is left unused
#define playStepOffLedge cast(int (*)(char*),0x00567268)

#define _playPlayerShouldFall cast(int (*)(char*,char*,float),0x0056DBF4)

#define _playPlayerRequestFall cast(int (*)(char *,int),0x0056DEF0)

#define _playHandleRunJumpRequest cast(int(*)(char*),0x0056C9C0)
#define _playHandleStandardJumpRequest cast(int(*)(char*),0x0056CAA4)
#define _playHandToHandStart cast(int(*)(char*),0x005603B8)
#define _playStartRoll cast(int(*)(char*),0x00571764)
#define _playHandleStealthRequest cast(int(*)(char*),0x00575F50)

int playHandlerWalk(char *Sp)
{
    float  vfl(1, 5, 0x3fc00000), vfl(1, 0, 0x3f800000), vfl(0, 25, 0x3e800000
    ), vfl(0, 1, 0x3dcccccd),vfl(0,75, 0x3f400000);//Here we are defining the float constants - I use web browser to calculate the hex values

    if (!l(char, 0x150))//Set some kind of rotation
        m60 *= f1p0 - casta(float, 0x007F9370) * f0p25;

    playSetAnimRateByAnalogStick(Sp, *cast(float *, 0x967CC8) * *gAnimTargetMS,
        *gAnimTargetMS, f0p1, l(char, 0xc) ? f1p0 : f0p75);

    if (l(char, 0x128) == 8) playStepOffLedge(Sp);

    if (_playPlayerShouldFall(Sp, gplayCollisionInfo, 0))
        _playPlayerRequestFall(Sp, 1);

    if (l(enum LARA_ANIM, 0x14) == LARA_RUN_TURN180 ||
        l(enum LARA_ANIM, 0x14) == LARA_RUNJUMP)
        return m60 = 0; //Obviously if we are turning around or jumping we don't need to rotate

    if (casta(unsigned int, 0x007F9430) & 0x20)//If the jump button is pressed
        if (casta(unsigned int, 0x007F9880) & 0xE007)
            _playHandleStandardJumpRequest(Sp);
        else
            _playHandleRunJumpRequest(Sp);

    if (l(char, 0x128) == 2)//If HtH should begin
        _playHandToHandStart(Sp);

    if (casta(unsigned int, 0x007F9430) & 0x80)//If the roll button is pressed
        return _playStartRoll(Sp);
    if (l(char, 0xc) & 4)_playHandleStealthRequest(Sp); //If the stealth button is pressed
}   

_fltused()
{}

Compile like this:

cl x.c /Ox /GS- /link /NODEFAULTLIB /DYNAMICBASE:NO /BASE:0x40000 /SUBSYSTEM:native /entry:playHandlerWalk

I'm obviously using the Microsoft C++ Compiler (community edition latest version).

Look at the way I'm defining floats - this is because it's the only way they will be embedded into my code (I'm using online float to hex converter) - otherwise the compiler will use global constants which will not be present in the context for which this code is targeted.

I swear the above code compiles if you use the command line parameters provided above and a modern Microsoft C compiler.

\$\endgroup\$
  • \$\begingroup\$ @SᴀᴍOnᴇᴌᴀ I changed the title. \$\endgroup\$ – FISOCPP Sep 17 '18 at 19:42
  • 2
    \$\begingroup\$ Thanks for editing. I'm looking at Rev 5 now, and it looks like it's obfuscated, non-portable code. Could you explain what this code accomplishes, and why you chose to write it this way? (Note that we can't review obfuscated code, since it is off-topic for Code Review — see the the help center.) \$\endgroup\$ – 200_success Sep 17 '18 at 20:09
  • \$\begingroup\$ @200_success It's not obfuscated - consider the defines as a header file. The reason why I'm defining the floating numbers by a hex constant is because this is the only way they'll be included as part of the code (part of a mov immediate instruction) - otherwise the compiler will create data that would not be part of the host executable in which this code will be copied. \$\endgroup\$ – FISOCPP Sep 17 '18 at 21:29
  • \$\begingroup\$ @200_success At least my target is not to make an obfuscated code - everything in it have certain purpose. \$\endgroup\$ – FISOCPP Sep 17 '18 at 21:40
  • 1
    \$\begingroup\$ Then can you do a better job of explaining what this code is for? \$\endgroup\$ – 200_success Sep 17 '18 at 21:43
1
\$\begingroup\$
#define cast(p, type) ((type) p)

I suggest you take p from macro's expansion in parentheses as well:

#define cast(p, type) ((type) (p))

Consider cast(3.6 + 3.6, int).

Or even better,

char *buf_start = buf;
int *third_int_in_buf = cast(buf_start + 8, int *);

<_<

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.