0
\$\begingroup\$

I'm using MemorySharp to calculate the memory address from a pointer address and then inject values in memory.

The code works perfectly, but I was wondering if this would be the right way. I found a little dirty the way I'm calculating the memory address. Maybe there's a easier way that I'm not able to see.

Any advice, memory related, or clean code, etc.. is welcome.

class Amongus {

   private const string nameGame = "Among Us";
   private const int addrM = 0x01D4358C;
   private MemorySharp mSharp = new MemorySharp(Process.GetProcessesByName(nameGame).FirstOrDefault());

   // Static Address
   readonly List<int> moveSpeedOffset = new List<int> { 0x4, 0x74 };

   public Amongus()  { }

   // Calculate Address.
   private IntPtr getAddress(int baseM, List<int> offsets)
   {
      try
      {
         IntPtr Base_ptr = mSharp["GameAssembly.dll"].BaseAddress + baseM;
         IntPtr Base = mSharp.Read<IntPtr>(Base_ptr, false);

         int count = 0;
         foreach (int ptr in offsets)
         {
            if (count != offsets.Count - 1)
            {
               Base += ptr;
               Base = mSharp.Read<IntPtr>(Base, false);
               count++;
            }
            else { Base += ptr; }
          }

          return Base;
      }
      catch (Exception)
      {
         return new IntPtr(0);
      } 
  }

public void changeMoveSpeed2(float speed) => mSharp[getAddress(addrM, moveSpeedOffset), false].Write(speed);

}
```
\$\endgroup\$
3
  • 2
    \$\begingroup\$ In 64 bit app the address is long not int, check IntPtr.Size. \$\endgroup\$
    – aepot
    Jun 14 at 9:58
  • \$\begingroup\$ @aepot Hello once again! I tried changing addrM type to long type, the thing is that I can't sum the IntPtr that returns the BaseAddress with a long type. I saw that Microsoft docs has LongPtr type (here), but when I try to declare it, it doesn't recognize it as a type \$\endgroup\$
    – Sharki
    Jun 14 at 16:42
  • 1
    \$\begingroup\$ I'm not about IntPtr, it's always ok on any platform. You misunderstood me. \$\endgroup\$
    – aepot
    Jun 14 at 17:18
2
\$\begingroup\$

Clean code advice. You need to calculate Base in a loop with two expressions:

Base += ptr;
Base = mSharp.Read<IntPtr>(Base, false);

The second should not be calculated for the last iteration, but before the loop you have

IntPtr Base = mSharp.Read<IntPtr>(Base_ptr, false);

So, mSharp.Read should be executed before adding ptr, right? Let's do so:

IntPtr Base_ptr = mSharp["GameAssembly.dll"].BaseAddress + baseM;
IntPtr Base = Base_ptr;

foreach (int ptr in offsets)
{
    Base = mSharp.Read<IntPtr>(Base, false);
    Base += ptr;
}

Now we don't need count variable. The Base_ptr is excessive, and we can calculate loop in one expression:

IntPtr Base = mSharp["GameAssembly.dll"].BaseAddress + baseM;
foreach (int ptr in offsets)
    Base = mSharp.Read<IntPtr>(Base, false) + ptr;

which makes a perfect sense to me.

And you probably want to return IntPtr.Zero, not new IntPtr(0), in the case of exception.

\$\endgroup\$
1
  • \$\begingroup\$ It makes sense, I don't know how I didn't think in that. Thanks a lot for that clean code advice! \$\endgroup\$
    – Sharki
    Jun 14 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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