跳转至

搜索字符串

下一个 Lua 脚本扫描游戏内存中的文本字符串。它的工作方式与使用字符串数值类型的 Cheat Engine 内存扫描器非常相似。

BASEADDRESS = getAddress("Game.exe")
 function findString(str)
  local len = string.len(str)
  local chunkSize = 4096
  local chunkStep = chunkSize - len
  print("Found '" .. str .. "' at:")
  for address = BASEADDRESS, (BASEADDRESS + 
0x2ffffff), chunkStep do
    local chunk = readBytes(address, chunkSize, 
true)
    if (not chunk) then break end
    for c = 0, chunkSize-len do
      checkForString(address , chunk, c, str, 
len)
    end
  end
end
function checkForString(address, chunk, start, 
str, len)
  for i = 1, len do
    if (chunk[start+i] ~= string.byte(str, i)) then
      return false
    end
  end
  print(string.format("\t0x%x", address + start))
end
 findString("hello")
 findString("world")

获得基地址后,定义 findString() 函数①,它接受一个字符串参数 str。这个函数以 4096 字节的大块④遍历游戏内存②。各块按顺序扫描,每块都在前一块结束前 len(str 的长度)字节处开始③,以防止错过一个块中开始、另一个块中结束的字符串。

当 findString() 读取每个块时,它遍历块中直到重叠点的每个字节⑤,把每个子块传给 checkForString() 函数⑥。如果 checkForString() 把子块与 str 匹配,它就把该子块的地址打印到控制台⑦。

最后,为了找到引用字符串 “hello” 和 “world” 的地址,调用 findString("hello")⑧ 和 findString("world")⑨。通过用这段代码搜索嵌入的调试字符串,并配合前一段定位函数头的代码,我能在几秒钟内找到游戏中的大量内部函数。