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 


Substitute writeBytes 0x.. with a function

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sun Jun 21, 2015 7:03 am    Post subject: Substitute writeBytes 0x.. with a function Reply with quote

Say I have AOB code :

aob_s = d0 d1 46 1c 22 01
aob_r = d0 d0 46 7c 22 01

below is a process to scan and replace (all) aob code found

Code:

     resultList = AOBScan("d0 d1 46 1c 22 01", "+W*X-C")
   --- or resultList = AOBScan(tostring(aob_s), "+W*X-C")
 if (resultList) then
   lngt = resultList.getCount()
   for x=0, lngt-1, 1 do
      writeBytes(resultList[x], 0xd0, 0xd0, 0x46, 0x7c, 0x22, 0x01)
  end
   resultList.Destroy()
   resultList = nil


Does somebody have a function to substitute this "0x.." in writeBytes so its automatic add 0x.. in front of byte for aob_r ?.

Thank you and regards
Back to top
View user's profile Send private message
Alamer99
Expert Cheater
Reputation: 1

Joined: 04 Jan 2015
Posts: 136

PostPosted: Sun Jun 21, 2015 11:08 am    Post subject: Reply with quote

You Can Merge Auto Assemble With LUA

Code:
resultList = AOBScan("d0 d1 46 1c 22 01", "+W*X-C") --Scan Your AoB
if (resultList) then --Check For Results
--Start Of Auto Assemble Code
autoAssemble[[aobscan(Scan0,d0 d1 46 1c 22 01)
Scan0:
db d0 d0 46 7c 22 01]]
--End Of AutAssemble Code
end --End Of IF
resultList.Destroy()
resultList = nil


P.S: If Anyone Can Post a Simpler Way I'd Be Interested In Reading Smile

Edit:
just realized that you want to change ALL the results... Here is Even A Simpler Way
Code:
repeat
until
not autoAssemble[[aobscan(Scan0,d0 d1 46 1c 22 01)
Scan0:
db d0 d0 46 7c 22 01]]
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Sun Jun 21, 2015 2:17 pm    Post subject: Reply with quote

AA aobscan is efficiency for batch scan multiple distinct aobs with only one expected single result each,
it will scan the memory one pass and stop (and success) when each aob get one result.

Breaking AA script with one aob string expecting multiple result inside lua inverse this efficiency.

Try use string concate or string.format for more complicated script:

Code:

...
for x=0, lngt-1, 1 do
      autoAssemble(resultList[x]..[[:
db d0 d0 46 7c 22 01
]])
end
...
for x=0, lngt-1, 1 do
     local addy = resultList[x]
      autoAssemble(string.format([[
%s+01:
db d0 d0 46 7c 22 01
%s+1a:
db d0 d0 46 7c 22 01
]],addy,addy))
end
...


There are also string interpolation method that may save some typo:
Code:
function sinterp(binop)
  getmetatable("")['__'..binop] = function (s, tab)
    if type(tab) ~= 'table' then tab = {tab} end
    return s:gsub([[%%%(([_0-9a-zA-Z]+)([ :]-)([-0-9%.]*[cdeEfgGiouxXsq]-)%)]],
      function(k,sep, fmt)
        local n,ofmt = tonumber(k),fmt
        if fmt == '' then fmt = 's' end
        return n ~=nil and tab[n]~=nil and ("%"..fmt):format(tab[n]) or
               tab[k]~=nil and ("%"..fmt):format(tab[k]) or
               '%('..k..sep..ofmt..')'
      end)
  end
end
sinterp('mod')


print([[
%(addy1 X): // match by key-name in supplied table
db 01 02 03
%(addy2):  // use default format %s, but for number tostring, it is in decimal base instead of hexadecimal, unfortunate
db 01 02 03
%(1 X):   // match key name by position (key is numeric)
db 01 02 03
%(3 s):
db 01 02 03
%(2 s):
db 01 02 03
%(1 X):    // main point of string interpolation is to match by key-name so no repeated string format parameters as above example needed (it is a source of bug)
db 01 02 03
]] % {0x1111000, '2222000', '3333000'; addy1=0x789a00,addy2=0xFEEDBEEF})


The interpolation function is for generalize string format, probably can be optimize for autoAssemble.
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Sun Jun 21, 2015 11:47 pm    Post subject: Reply with quote

Thanks for reply and hint both Alamer99 and Panraven.
That very interesting scripts and i have try it.
Anyhow i found another function :

Code:

--- lend a function has posted by DaSpammer
local scan = 'd0 d1 46 1c 22 01';
local replace = 'd0 d0 46 7c 22 01';
local replace_table = {};
for byte in string.gfind(replace, "[^%s]+") do
   table.insert(replace_table, tonumber('0x'..byte));
end
print(unpack(replace_table));
local data = AOBScan(scan);
if (data) then
   local count = data.getCount();
   for i=0, count-1 do
      local address = data.getString(i);
      writeBytes(address, replace_table);
   end
end


Maybe its possible there are another ways to do this...

Regards
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Mon Jun 22, 2015 4:23 am    Post subject: Reply with quote

Note that the gfind doesn't handle wildcard bytes.
In a previous experience, memscan with vtByteArray can treat an aob byte table with number -1 as wildcard.
Oh, another form of lua AOBScan should handle wildcard...
Code:
AOBScan(unpack(aobByteTable))
in (doc/main.lua)
AOBScan(x,x,x,x,...):
scans the currently opened process and returns a StringList object containing all the results. don't forget to free this list when done
Bytevalue of higher than 255 or anything not an integer will be seen as a wildcard
AOBScan(aobstring): see above but here you just input one string


And also in AA Script of new beta ce, the db (equivalent to writeBytes in lua) support wildcard too

SomeAddress:
db 11 22 ?? 44 55

The byte at address SomeAddress + 02 where the ?? locate will skipped for written anything.
Back to top
View user's profile Send private message
Alamer99
Expert Cheater
Reputation: 1

Joined: 04 Jan 2015
Posts: 136

PostPosted: Mon Jun 22, 2015 5:56 am    Post subject: Reply with quote

Let Me Just Jump Into This

What You Want To Do Is Scan For AoB "d0 d1 46 1c 22 01"
And Replace All The Results With "d0 d0 46 7c 22 01" Right?

If Thats The Case Then The Answer is Simple And I Write It Before
Code:
repeat
until
not autoAssemble[[aobscan(AoB,d0 d1 46 1c 22 01)
AoB:
db d0 d0 46 7c 22 01]]


panraven wrote:
AA aobscan is efficiency for batch scan multiple distinct aobs with only one expected single result each,
it will scan the memory one pass and stop (and success) when each aob get one result.

That is Totally Right And Nothing is Wrong in It BUT you missed The Idea
Code:
repeat
until
not

This Code Will Repeat The autoAssemble Script (Or Whatever You Assign To It) Over And Over Until There Are No More Results Thus Making It The Simplest Script For Replacing All The Results
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Tue Jun 23, 2015 6:24 am    Post subject: Reply with quote

Thanks again to you both Alamer99 and Panraven.

My point to a question, as I asked at the beginning of the topic , is to implementation encoded AOB code into the script . Yours both method is absolutely right, mainly to replace all codes found while doing scan.

Anyhow, i have finish and get good result (for this time) for my script.
I am use a aob swap function and put encoded aob codes in button click sender. And everything running properly.



Code:

function DEC_HEX(IN)
    local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
   if IN<1 then
      OUT=0
      return OUT
   end
    while IN>0 do
        I=I+1
        IN,D=math.floor(IN/B),math.mod(IN,B)+1
        OUT=string.sub(K,D,D)..OUT
    end
    return OUT
end
---
function aobswap(search, change)
   aobs = AOBScan(search)
   if(aobs ~= nil) then
      j = stringlist_getCount(aobs)
       for i = 1, j do
         address=stringlist_getString(aobs,i-1)
                for i = 1, string.len(change), 3 do
               z = string.sub(change, i, i+2)
                    x, y = string.find(z, "%?+")
                    if (x == nil) then
                  script=[[
                  ]]..address.."+"..(DEC_HEX((i-1)/3))..[[:
                  db ]]..z..[[
                  ]]
                  autoAssemble(script)
                  end
            end
      end
      object_destroy(aobs);
      aobs=nil
   end
end


and in a button function (in a trainer)

Code:

function CEButton1Click(sender)
scn02=dec("ZDMgOTYgMTIgMTcgMDAgMDAgOTY=")
rpl02=dec("ZDEgOTYgMTIgMTcgMDAgMDAgOTY=") 
 --- decoding AOB code with a function decode(data)  put in script
cdfound = AOBScan(tostring(scn02), "+W*X-C")
if (cdfound) then
goswp = aobswap(scn02,rpl02)
scsMssgs()  -- function if success
cdfound.Destroy()
cdfound = nil
else
failMssgsl()  -- function if fail
end
end


Also i try to separate AOB code by some part and combine it all again as one part with function concatenate.

Code:

local wb,md,gt,sf=memory.writebyte,memory.readdwordunsigned,gui.text,string.format
local rd,rw,rb=memory.readdwordunsigned,memory.readwordunsigned,memory.readbyteunsigned
mb=function(x) return rb(x)                      end
mw=function(x) return rb(x)+rb(x+1)*0x100                end
md=function(x) return rb(x)+rb(x+1)*0x100+rb(x+2)*0x10000+rb(x+3)*0x1000000    end
local i,j,x,t=1,1,1,{}

   scstart=md(0x02000024)+0x1267C4
   game=2

function main()

--List all portions of or script
s1="2E 00 FFFF"
s2=""
s3=""
s4="02 00"

--Concatenate all strings in order
s=s1..s2..s3..s4

--Remove spaces from eventscript so that we can add the script in
s = string.gsub (s, " ", "")

--Break up eventscript string into a table
while i<=string.len(s) do
   t[j] ="0x"..string.sub(s,i,i+1)
   j=j+1
   i=i+2
end

--Write script per byte via values in the table.
while x<j do
   wb(scstart+x-1,t[x])
   --Graphical display of text
   gt((x-1)%10*15,math.floor((x-1)/0xA)*10,sf("%02X",t[x]))
   x=x+1
end

x=1
end
gui.register(main)


Just another method to confusing leecher who try break our codes and steal it.

Note :
Panraven function "Making Forest" very useful to generating junk codes.
I just add :

Code:

getLuaEngine().cbShowOnPrint.Checked=false
getLuaEngine().hide()


to hide lua engine pop up.

Regards
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 Lua Scripting 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