techiew / EldenRingMods

A collection of mods I've made for Elden Ring.
MIT License
170 stars 23 forks source link

There are some errors in the AobScan function #19

Closed wxhwz closed 9 months ago

wxhwz commented 9 months ago

There are some errors in the AobScan function:

  1. The isMemoryReadable does not include the case where protection == PAGE_EXECUTE_READ.
  2. There is an issue with the scanning process. For example, if the memory bytes are aa aa cc bb and the pattern is aa ?? bb, the currentAddress will skip the second aa, pointing to cc, causing the search to fail.
  3. During scanning, it is recommended to use std::vector AobRaw in conjunction with std::vector AobMask instead of repeatedly using std::stoul(aobTokens[i], nullptr, 16). This can significantly improve efficiency.
static bool aobstring_convert_aobraw(std::string aob, std::vector<BYTE>& aob_raw, std::vector<bool>& aob_mask)
{
    std::vector<std::string>aobTokens = TokenifyAobString(aob);
    std::string whitelist = "0123456789ABCDEF";
    for (std::string& byte : aobTokens)
    {
        if (byte == muAobMask)
        {
            aob_raw.push_back(0);
            aob_mask.push_back(true);
            continue;
        }
        if (byte.length() != 2)
        {
            goto label;
        }
        for (char& c : byte) {
            c = std::toupper(c);
        }
        if (byte.find_first_not_of(whitelist) != std::string::npos)
        {
            goto label;
        }
        else
        {
            aob_raw.push_back((BYTE)std::stoul(byte, nullptr, 16));
            aob_mask.push_back(false);
        }
    }
    return true;
label:
    ShowErrorPopup("AOB is invalid! (" + aob + ")");
    return false;
}
if (is_MemoryReadable)
{
   //Log("Checking region: ", NumberToHexString(regionStart));
   currentAddress = regionStart;

   while (currentAddress < regionEnd - aob_raw_size)
   {
       tmp_address = currentAddress;
       for (size_t i = 0; i < aob_raw_size; i++)
       {
           if (!aob_mask[i] && (*(BYTE*)tmp_address != aob_raw[i]))
           {
               tmp_address++;
               break;
           }
           else if (i == aob_raw_size - 1)
           {
               signature = tmp_address - aob_raw_size + 1;
               result.emplace_back(signature);
               /*Log("Found signature at ", NumberToHexString(signature));*/
               if (result.size() == result_count)
               {
                   return result;
               }
           }
           tmp_address++;
       }
       currentAddress++;
   }
}