WPF code cave looping with timers

142 Views Asked by At

I run into a problem with a code cave code trying to do a loop using timers instead of "while". It's just writing one time a value then writes only 0 value. What i want is to write to an address every second based on timers and after reading some other value. Like a refresh of that read value. I have another code that is working just fine but this code cave is not:

using System.Timers;

public static System.Timers.Timer aTimer;
public MainWindow()
{
    InitializeComponent();
    aTimer = new System.Timers.Timer(1000);
    aTimer.Elapsed += new ElapsedEventHandler(Timer);
    aTimer.AutoReset = true;
    GC.KeepAlive(aTimer);
}
public static void Start()
{
    OpenProcess(); Allocate(out vMemory, 0x1024);
}
public static void Timer(object source, ElapsedEventArgs e)
{
    aTimer.Enabled = true;

    int[] wepOffsets = { 0x70, 0x10, 0x20, 0x368, 0x36c };
    byte[] readMemory = PointerRead((IntPtr)weapBase, 4, wepOffsets, out bytesToRead);
    int final = BitConverter.ToInt32(readMemory, 0);
    //MessageBox.Show("value :  " + final);

    if (final == 5) // at first start it is writing this value then 0
    {
        byte[] swByte = { 0xC7,0x83,0x22,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x8B,0x74,0x24,0x40,
            0x48,0x8B,0x6C,0x24,0x48,0x48,0x8B,0x5C,0x24,0x50,0x48,0x83,0xC4,0x58,0xC3 };

        for (int i = 6; i < 10; ++i) { swByte[i] = (byte)(swSpd & 0xFF); swSpd = swSpd >> 8; }

        write(vMemory + 8, swByte, 30);
        write(vMemory, BitConverter.GetBytes((vMemory + 8)), 8);
        write(gameBase, new byte[] { 0xFF, 0x24, 0x25 }, 3);
        write(gameBase + 3, BitConverter.GetBytes((vMemory)), 4);
    }

    if (final == 14) // doesn't reach this part
    {
        byte[] gsByte = { 0xC7,0x83,0x22,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x8B,0x74,0x24,0x40,
            0x48,0x8B,0x6C,0x24,0x48,0x48,0x8B,0x5C,0x24,0x50,0x48,0x83,0xC4,0x58,0xC3 };

        for (int i = 6; i < 10; ++i) { gsByte[i] = (byte)(gsSpd & 0xFF); gsSpd = gsSpd >> 8; }

        write(vMemory + 8, gsByte, 30);
        write(vMemory, BitConverter.GetBytes((vMemory + 8)), 8);
        write(gameBase, new byte[] { 0xFF, 0x24, 0x25 }, 3);
        write(gameBase + 3, BitConverter.GetBytes((vMemory)), 4);
    }
}

Well, i need 2 values to be written every second based on what the read address is...if that has changed then to write the new value instantly. And how can i reduce this code...i think there can be made some modifications :).

Thank you very much for any support,idea.

0

There are 0 best solutions below