Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


proxValues.lua (find values that are close together)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions
View previous topic :: View next topic  
Author Message
crabshank
How do I cheat?
Reputation: 0

Joined: 28 Sep 2022
Posts: 3

PostPosted: Wed Nov 23, 2022 9:03 pm    Post subject: proxValues.lua (find values that are close together) Reply with quote

Readme on my Github ('/crabshank/Cheat-engine-tools')

Code:
local ress={}
local boundedResParams={-1,0,false}
local boundedRes={}
local narrow_err=true

local function resetAllResults()
   ress={}
   boundedResParams={-1,0,false}
   boundedRes={}
   narrow_err=true
end

local function addMemScan()
      local ms=getCurrentMemscan()
      local fl=ms.FoundList
      table.insert(ress,{})
      local currRes=#ress
      local base = ms.isHexadecimal and 16 or nil
      local len_fl=fl.Count
      for i = 0, len_fl-1 do
         local addr=fl.getAddress(i)
         local val=fl.getValue(i)
         local addrConv=tonumber(addr,16)
         ress[currRes][i+1]={Address=addr, Value=val, addressConv=addrConv, ix=i+1}
      end

   table.sort( ress[currRes], function(a, b) return a.addressConv < b.addressConv end ) -- Converted results array now sorted by address (ascending); "ix" all jumbled
   print('Set of results #'..currRes .. ' added!')
end

local function removeResult(i) --Remove i-th element from results table
   table.remove(ress,i)
   narrow_err=true
end

local function printFiltered(m,n)      
   if #boundedRes>=1 then
      local brl=#boundedRes
      local ags=2
      if m==nil then
         ags=0
      elseif n==nil then
         ags=1
      end
      
      for i = 1, brl do --iterate over boundedRes
         local t = {}
         local bri=boundedRes[i]
         local d=bri.data
         local r=bri.range
         local dle=#d
         if (ags==0) or (ags==1 and r<=m) or (ags==2 and r>=m and r<=n) then
               t[1]=d[1].Address .. ' (' .. d[1].Value .. ')'
               if dle >= 2 then
                  for k = 2, dle do --iterate over boundedRes.data
                     t[k]= ' || ' .. d[k].Address .. ' (' .. d[k].Value .. ')'
                  end
                  t[dle+1]=' 〈Range: ' .. boundedRes[i].range .. ' bytes〉'
               end

               local a = table.concat(t)
               print(a)
         end
      end
   else
      print('No matching results')
   end
end

local function sortBoundedRes()
   table.sort( boundedRes, function(a, b)
      return a.range < b.range
   end)
end

local function filterByRange(n)
   local brt={}
   local brl=#boundedRes
   for i=1, brl do
      local bri=boundedRes[i]
      if bri.range<=n then
         table.insert(brt,boundedRes[i])
      else
         i=brl --Early terminate, as sorted by range
      end
   end
end

local function narrowDown(n) --m is the same as the first round, unless argument specified
   local alrdyProc=boundedResParams[2]
   local rl=#ress
   if rl <= alrdyProc then
      print("Must have more results than already processed")
      return
   end
   if narrow_err==true then
      print("Do a full scan!")
      return
   end
   
   local m=boundedResParams[1]
   local unlim=boundedResParams[3]
   if n~=nil then
   if (unlim==false) and (n>=m) then
         print('Argument must be <'..m)
         return
      end
      if (n < #ress-1) then
      print("Argument must be >= number of results compared (>=" .. (#ress-1) .. ")")
        return
      end
         unlim=false
         m=n
         filterByRange(n)
   end
   
   boundedResParams={m, rl,unlim}
   local bndOut={}
   
         for i=alrdyProc+1, rl do --process ones not already done
            local cir = ress[i]
            for j=1, #cir do
               for k=1, #boundedRes do
                  local cj,bk=cir[j],boundedRes[k]
                  local mn,mx,cjac,ib,ob=bk.min,bk.max,cj.addressConv,false,false
                  local new_min=bk.max-m
                  local new_max=bk.min+m
                  if cjac>=bk.min and cjac<=bk.max then
                     ib=true
                  end
                  if ib==false then
                     if cjac<bk.min and (cjac>=new_min or unlim==true) then
                        mn=cjac
                        ob=true
                     else if cjac>bk.max and (cjac<=new_max or unlim==true) then
                        mx=cjac
                        ob=true
                     end
                  end
               if ib==true or ob==true then
                  local nd=bk.data
                  table.insert(nd,cj)
                  local rnge=mx-mn
                  local nb ={min=mn, max=mx, range=rnge, data=nd}
                  table.insert(bndOut,nb)
               end
               if (cjac > bk.max+m) and (unlim==false) then
                  k=#boundedRes --EARLY TERMINATE
               end
            end
         end
      end
   end
      boundedRes=bndOut
      sortBoundedRes()
      printFiltered()
end

local function firstScan(m,narrowDwn,unlim)
   local rl=#ress
   if (m < 1 or m==nil) and (unlim==false) then
      print("Argument must be a positive integer >=1")
      return
   end
   if (m < rl-1) and (unlim==false) then
      print("Argument must be >= number of results compared (>=" .. (rl1) .. ")")
      return
   end
   if boundedResParams[1]==m and boundedResParams[2]==rl and boundedResParams[3]==unlim then
      print("Already printed results!")
      return
   end
   if rl < 2 then
      print("Must have added at least 2 memscan results!")
      return
   end
   if narrow_err==true then
      narrow_err=false
   end
   
   boundedResParams={m,2,unlim}
   tempBndRes={}
   
   table.sort( ress, function(a, b) return #a < #b end ) --sort ress asc. order of their lengths
   
   local firstResEl=ress[1]
   
   --First round
   for i=1, #firstResEl do --each result from this list
      local ci=firstResEl[i]
      local currResEl=ress[2]
      for j=1, #currResEl do --each result from this list
               local cj=currResEl[j]
               local cad={ci.addressConv, cj.addressConv}
               local df=cad[2]-cad[1]
               local mn,mx=cad[1],cad[2]
               local dfa=math.abs(df)

               if (dfa<=m) or (unlim==true) then
                  if cad[1]>cad[2] then
                           mn=cad[2]
                           mx=cad[1]
                  end
                     local b={min=mn, max=mx, range=dfa,data={ci,cj}}
                     table.insert(tempBndRes,b)
                  else if (df>m) and (unlim==false) then
                     j=#currResEl
                  end
               end

      end
   end
   boundedRes=tempBndRes
-- End of first round
   if narrowDwn==true then
      narrowDown()
   end
end

local function fullScan(m) -- m is limit (Absolute value)
   local b=true
   local unlim=false
   if #ress==2 then b=false end
   if m==nil then
      m=0
      unlim=true
   end
   firstScan(m,b,unlim)
   if b==false then -- if no narrow down
      sortBoundedRes()
      printFiltered()
   end
end

proxValues={
   resetAllResults=resetAllResults,
   addMemScan=addMemScan,
   removeResult=removeResult,
   printFiltered=printFiltered,
   narrowDown=narrowDown,
   fullScan=fullScan
}


Last edited by crabshank on Wed Nov 30, 2022 10:17 am; edited 1 time in total
Back to top
View user's profile Send private message
crabshank
How do I cheat?
Reputation: 0

Joined: 28 Sep 2022
Posts: 3

PostPosted: Sun Nov 27, 2022 3:31 pm    Post subject: Update Reply with quote

Changelog:

* Added support for searching an unlimited number of bytes (empty argument to 'proxValues.fullScan()').
* Shows memory area sizes that contains the results when printing them, and prints them in the order of smallest to largest size.
* printFiltered and narrowDown functions now take optional arguments.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Extensions All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites