I am trying to emulate the memory address scanning by array of bytes
Cheat Engine, what I did works exactly the same as Cheat Engine with only one problem... It is slow, Cheat Engine shows more than 4 million results in less than 2 seconds, while the mio can take minutes to scan all the memory blocks to get the results, what seems really strange to me is how Cheat Engine manages to get the addresses so quickly.
buffer = pattern.Split(' ').Select(by => (byte)Convert.ToInt32(by.ToUpper(), 16)).ToArray();
for (int i = 3; i < MemoryRegion.Count; i++)
{
int baseAddress = (int)MemoryRegion[i].BaseAddress;
int regionSize = (int)IntPtr.Add((IntPtr)baseAddress,
(int)MemoryRegion[i].RegionSize);
for (int mapAddress = baseAddress; mapAddress < regionSize; mapAddress++)
{
if (ReadMemory(process, mapAddress, buffer.Length).SequenceEqual(buffer))
Console.WriteLine("Result: " + mapAddress.ToString("x8").ToUpper());
}
}
That works like a charm, first I get the base addresses for each memory block and their lengths, then I scan address by address on each memory block which makes it take a long time. I would like to understand and learn how Cheat Engine scans so quickly, what method it uses, what I am doing wrong or what I can improve.
I solved the question myself, it was very obvious... At night after a long afternoon of stress not finding the answer, I just tried to sleep and I couldn't so I got up, I made coffee and started to go over everything on my blackboard . To my surprise, the answer why it was so slow was more than obvious, it was scanning millions of addresses one by one, instead of obtaining the complete memory block and making a function that identifies the array within this block and thus obtain the address in just milli-seconds.
If someone had the same problem and does not understand what I explain 100%, you are free to ask me for source code and a more detailed explanation, I will gladly give it to you!