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 


Function to Find and Replace 4 bytes only if address starts

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

Joined: 09 Aug 2018
Posts: 5

PostPosted: Thu Aug 09, 2018 3:52 pm    Post subject: Function to Find and Replace 4 bytes only if address starts Reply with quote

Hi, I've got two addresses and there is always two with random address, with values of 250000. However there are around 9 more with the same value. The main two addresses that must be changed always start with 2AB. On my current instance the addresses are 2AB5FFA4 & 2AB6D434. I'm trying to make a function that accepts three arguments being; Value-to-hunt, Value-to-Replace, Address-Prefix

If I wanted to change 2AB5FFA4 & 2AB6D434 values from 250000 to 1337 I'd format like

Code:
findandReplace(250000, 1337, "2AB")


And if it's possible if I leave third argument blank it would change any/all addresses with value of argument 1 (25000)?
I am completely stumped and exhausted.


EDIT - I've made a function and it is working as I please. If anyone wants this function it is here.
Code:
function FindAndReplaceInteger(find, replace, start, finish)
  ms=createMemScan()
  ms.firstScan(soExactValue, vtDword, rtTruncated, find,find,start,finish,'+W*X-C', fsmNotAligned,'4', false, false, false, false)
  fl=createFoundList(ms)
  ms.waitTillDone()
  fl.initialize()
  if fl.Count>0 then
    local i
    for i=0,fl.Count-1 do
      if readInteger(fl.Address[i])==find then
        writeInteger(fl.Address[i],replace)
        print("Replaced " & fl.Address)
      end
    end
  end

  fl.destroy()
  ms.destroy()
end

FindAndReplaceInteger(250000,1337,'2AB00000','2AC00000')


Last edited by zw443 on Thu Aug 09, 2018 5:55 pm; edited 1 time in total
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Aug 09, 2018 4:36 pm    Post subject: Reply with quote

When you do a first scan set the start and stop address to 2AB00000 and 2AC00000 (so append something like ((targetIs64Bit() and 16 or Cool-#prefix) '0's then do the same for tonumber(prefix,16)+1 for the end address)? If they're in a named module you're probably better off using that to determine the range to scan. To scan all memory if it's not given just check for nil or an empty string and give 0 and -1 (or 'FFFFFFFFFFFFFFFF') /shrug

Presumably you're using the memscan class since it gives you all the options the gui has (such as variable type eg. 4 byte / float etc.).
Code:
firstScan(scanoption, vartype, roundingtype, input1, input2 ,startAddress ,stopAddress ,protectionflags ,alignmenttype ,"alignmentparam" ,isHexadecimalInput ,isNotABinaryString, isunicodescan, iscasesensitive)

See celua.txt or the wiki for more information

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
zw443
How do I cheat?
Reputation: 0

Joined: 09 Aug 2018
Posts: 5

PostPosted: Thu Aug 09, 2018 4:44 pm    Post subject: Reply with quote

When I use that function it dose not replace 250000 to 1337.
is input1 FInd and input2 Replace?
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Aug 09, 2018 4:52 pm    Post subject: Reply with quote

no, it's just like the gui, it's not always necessary but eg. for the find between scan you need two inputs the minimum and the maximum.

You wait for the scan to finish then create a foundlist based on it and loop over the result, google can help you find examples eg. https://forum.cheatengine.org/viewtopic.php?p=5726953#5726953

_________________
https://github.com/FreeER/ has a few CE related repos
Back to top
View user's profile Send private message
zw443
How do I cheat?
Reputation: 0

Joined: 09 Aug 2018
Posts: 5

PostPosted: Thu Aug 09, 2018 5:15 pm    Post subject: Reply with quote

I'm looking at making a function for a trainer. I click a button, and it uses a function that contains three arguments being; Value-To-Search-For, Value-To-REPlace, Address-Prefix. That is my question. How do I make a function similar to
Code:
function FindAndReplaceDouble(find, replace)
  ms=createMemScan()
  ms.firstScan(soValueBetween, vtDouble, rtTruncated, find,find,0,0xffffffffffffffff,'+W*X-C', fsmAligned,'4', false, false, false, false)
  fl=createFoundList(ms)
  ms.waitTillDone()
  fl.initialize()
  if fl.Count>0 then
    local i
    for i=0,fl.Count-1 do
      if readDouble(fl.Address[i])==find then
        writeDouble(fl.Address[i],replace)
      end
    end
  end

  fl.destroy()
  ms.destroy()
end

FindAndReplaceDouble(60, 100)
Which I found in another thread answered Dark Byte

I just can't wrap my head around it.
Back to top
View user's profile Send private message
FreeER
Grandmaster Cheater Supreme
Reputation: 53

Joined: 09 Aug 2013
Posts: 1091

PostPosted: Thu Aug 09, 2018 6:09 pm    Post subject: Reply with quote

Pretty much like that but you add an argument to the function and change the arguments to firstscan to match what you want, eg. if you're not scanning for doubles you're going to change vtDouble (eg. to vtDword for 4 bytes) and read/write Double to read/write Integer (for 4 byte values).

And if you only want addresses starting with 2AB then you replace 0 and 0xFF... with 0x2AB00000 and 0x2AC00000, if you want the user to be able to give you a 3 hex digit prefix then you use prefix .. '00000' and tostring(tonumber(prefix,16)+1) .. '00000', if you want any number of hex digits then you use prefix .. ('0'):rep(8 - #prefix) and tostring(tonumber(prefix,16)+1) .. ('0'):rep(8 - #prefix), if you want to allow the same code to work for both 32 bit and 64 bit you might check if the target is 64 bit and use 16 instead of 8 (16 hex digits to represent 64 binary digits), if you want to allow them to not give a prefix and so scan all memory then check

if type(prefix) ~= 'string' or #prefix == 0 then startaddr, endaddr = 0,0xffffffffffffffff
else ... to calculate startaddr and endaddr as detailed above end
.

Assuming firstscan can take string addresses like the vast majority of CE functions, if not then use tonumber(..., 16) to convert the string to a number when passing it to the firstscan function.

zw443 wrote:
That is my question
If you're tired from working all day or whatever then take a break and look into it when you're not exhausted.
Unless you explicitly ask for someone to do all the work for you, don't expect them to spend their time doing all the work for you. There's all the information you need there to do it if you know the basics of lua and read a bit of documentation or find a few examples.

_________________
https://github.com/FreeER/ has a few CE related repos
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