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 


Checking values using readBytes

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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sat Feb 28, 2015 10:35 pm    Post subject: Checking values using readBytes Reply with quote

Lets say the I have a foundlist from a memory scan. What I am attempting is to compare a 4 byte long address that is 0x18 forward from the foundlist with a known value. When I used the readBytes I got decimal numbers instead of hex numbers(as I expected).

Code:

   for i = 0, foundlist_getCount(FL1)-1  do
   foundAddr_hex = foundlist_getAddress(FL1, i)
   testAddr_dec = tonumber(foundAddr_hex, 16) + 0x18
   fv = readBytes(testAddr_dec,4)
   ...
   end


The first few outputs of fv are:
132 28 1 0
75 4 80 139
36 16 85 139
255 208 141 68

So what code lines are used to convert these results into a decimal numbers that then could be compared to a n value?
Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sat Feb 28, 2015 11:02 pm    Post subject: Reply with quote

Unless you're dealing with strings, all numbers are stored the same way.

For example, if you type print(0x18), it prints 24.

You could use: fv = readInteger(testAddr_dec)
Then compare it against your expected value, such as 0x07ABCDEF

If the value at the address equals 128699887 (0x07ABCDEF), then it's a match.
Back to top
View user's profile Send private message
bknight2602
Grandmaster Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sat Feb 28, 2015 11:25 pm    Post subject: Reply with quote

I tried integer, but the none of the values (there were 5299 in the scan) equated to the one I was looking for. In this case it was 20000. And unless the code is totally botched nothing turned up as a match.

Now if you use a print statement like:
print(132 28 1 0), you receive an error that is "looking for a ")" near 28.
Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Mar 01, 2015 12:22 am    Post subject: Reply with quote

That's because 132 28 1 0 are 4 separate integer values.

In hex, those numbers are 0x84, 0x1C, 0x01, and 0x00.
They are stored in memory in that order.
When read as an integer, the 4-bytes are reversed to 0x00011C84 (decimal 72836).

20,000 is equivalent to hex 0x00004E20.
If you use readInteger() you can compare the result directly to 20000.

If you use readBytes(), you're going to get 4 values back: 32, 78, 0, 0
That is equivalent to 0x20, 0x4E, 0x00, 0x00.

edit: I think what you might be trying to do is:

Code:
for i = 0, foundlist_getCount(FL1)-1  do
   foundAddr_hex = foundlist_getAddress(FL1, i)
   testAddr_dec = getAddress(foundAddr_hex) + 0x18
   fv = readInteger(testAddr_dec)
   if (fv == 20000) then
   ...
end
Back to top
View user's profile Send private message
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Sun Mar 01, 2015 5:28 am    Post subject: Reply with quote

Code:
for i = 0, FL1.Count-1  do
   address = FL1.Address[i]
   fv = readInteger(address..'+18')
   if (fv == 20000) then
     print(   'address found:', address..'+18' )
   end
end



Or
Code:
for i = 0, FL1.Count-1  do
   address = FL1.Address[i]
   fv = readInteger(address..'+18')
   if (fv == 0x20000) then
     print(   'address found:', address..'+18' )
   end
end



Depends on what value you are looking for. Is it 20000 or 0x20000 ?

_________________
Back to top
View user's profile Send private message MSN Messenger
bknight2602
Grandmaster Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sun Mar 01, 2015 7:51 am    Post subject: Reply with quote

mgr.inz.Player
It is dec 20000.
As I stated I tried readInteger and it did not find a match, however, since both of you guys think it will work, I'll give it another try. I believe adding 18 may not work as summations result in decimal numbers. I'll test that out this morning.

Zanzer wrote:
...
In hex, those numbers are 0x84, 0x1C, 0x01, and 0x00.
They are stored in memory in that order.
When read as an integer, the 4-bytes are reversed to 0x00011C84 (decimal 72836).
...

Exactly, but what lines should be used to find that, I'll try integer again.

Thanks for both of the replies.

EDIT 1:
Well I tried readInteger and that did not find any matches, just like yesterday, but I added a print statement and by visual inspection there should have been a match.
Ore addressess 10 0287C320
Forward list addressess 10 287c338 and has the value 528355 (new gold value as game progressed from yesterday)
The if statement
Code:
if fv == gold_value then

never had equality so the statements that followed were not executed, just like yesterday.
Now what is necessary to make the if statement work?
I'll post the whole code.
Code:

function RecalculateAddresses(sender)
   local AL = getAddressList()
   local MS1 = createMemScan()
   local FL1 = createFoundList(MS1)
   correction = nil

   memscan_firstScan(MS1,
     soExactValue, vtDword, rtRounded, ore_value, "",
     0, 0xffffffffffffffff, "*X*C*W", fsmNotAligned, "",
     false, false, false, true)

   memscan_waitTillDone(MS1)
   foundlist_initialize(FL1)
   print("Num of ore amounts found ", foundlist_getCount(FL1))

   for i = 0, foundlist_getCount(FL1)-1  do
    local oreAddr_hex = foundlist_getAddress(FL1, i) -- foundlist_getAddress always returns in hex without 0x
    print("Ore addressess " .. i + 1 .. " " .. oreAddr_hex)
    local goldAddr_dec = tonumber(oreAddr_hex, 16) + 0x18  -- convert to decimal number amd adds 0x18, the distance from ore to gold
    goldAddr_hex = string.format("%x", goldAddr_dec)
    local fv = readInteger(goldAddr_hex)
    print("Forward list addressess " .. i + 1 .. " " .. goldAddr_hex .. " and has the value " .. fv)
    if fv == gold_value then
      print("Forward list addressess " .. i + 1 .. " " .. goldAddr_hex .. " and has the value " .. fv)
      print("Found gold which is 0x18 after ore")
      print("Found correct ore address is ", oreAddr_hex)

      -- DO MAGIC (correct "address" property for all memory records)
      --local oldOrrinAddr_hex = memoryrecord_getAddress( addresslist_getMemoryRecordByDescription(AL, "Orrin") )
      local oldOreAddr_dec = memoryrecord_getAddress( addresslist_getMemoryRecordByDescription(AL, playercolor .. "Ore") )
      --print("The old table num address for ore is ", oldOreAddr_dec)
      --print("The old table hex address is ", string.format('%x', oldOreAddr_dec))
      --local oldOreAddr_dec = tonumber(oldOreAddr_dec, 16)
      --print("The num conversion for ore old table address is ", ooldOreAddr_dec)
      local correction = oreAddr_dec - oldOreAddr_dec
      --print("The num correction to the table is ", correction)

      for j=0, addresslist_getCount(AL)-1 do
        local mr = addresslist_getMemoryRecord(AL, j)
        --print("The old address for rec num ", j + 1, "is", string.format('%x', memoryrecord_getAddress(mr)))
        local newAddress = memoryrecord_getAddress(mr) + correction
        --print("The new address for rec num ", j + 1, "is", string.format('%x', newAddress))
        memoryrecord_setAddress(mr,string.format('%x', newAddress))
      end -- loop 'for j' end
      break -- break loop 'for i'
    end --if fv == gold_value then
      print("Correct ore not found and the table has not been recalculated")
   end -- loop 'for i' end

Used a template that mgr.inz.Player provided a while back

EDIT 2:
I found my mistake, in another piece of code that had the values as strings and thus the equality was not met. Those strings were changed to numbers and everything worked.
Thanks for the hints from both.


Last edited by bknight2602 on Sun Mar 01, 2015 11:16 am; edited 2 times in total
Back to top
View user's profile Send private message Yahoo Messenger
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Sun Mar 01, 2015 11:09 am    Post subject: Reply with quote

Where are you setting the gold_value variable?

Have you checked the print out statements? It prints an address that does contains the expected gold value?

Would further lead me to believe that gold_value isn't being set correctly.

Are you sure you're not setting it as a string? print(type(gold_value))

Are you sure gold is found 0x18 away from ore?

Code:
--ORE--- | 00000000 | 00000000 | 00000000
00000000 | 00000000 | --GOLD-- | 00000000


I'm assuming all that tonumber and string.format code is behaving correctly.

Not relevant, but could it not all be simplified to:

Code:
local oreAddr = foundlist_getAddress(FL1, i)
local goldAddr = getAddress(oreAddr) + 0x18
local fv = readInteger(goldAddr)
Back to top
View user's profile Send private message
bknight2602
Grandmaster Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sun Mar 01, 2015 11:15 am    Post subject: Reply with quote

Zanzer wrote:
Where are you setting the gold_value variable?
...


See my EDIT 2
Back to top
View user's profile Send private message Yahoo Messenger
mgr.inz.Player
I post too much
Reputation: 218

Joined: 07 Nov 2008
Posts: 4438
Location: W kraju nad Wisla. UTC+01:00

PostPosted: Mon Mar 02, 2015 10:03 am    Post subject: Reply with quote

bknight2602 wrote:
As I stated I tried readInteger and it did not find a match, however, since both of you guys think it will work, I'll give it another try.

EDIT 2:
I found my mistake, in another piece of code that had the values as strings and thus the equality was not met. Those strings were changed to numbers and everything worked.
Thanks for the hints from both.

It would be better if you had given the whole script. Attaching CT file is the best option for beginners. And much easier for us to test code. (many 'bugs' are also related with forms created in "Form Designer").

I'm glad you found your mistake.


PS: You can use print for debugging your code.
Place this code at the begging of your code:
Code:
debug=1

function printdebug(...)
  if debug==1 then print(...) end
end



Then, in your code use this:
Code:
printdebug("gold_value keeps", gold_value)
printdebug("type of gold_value keeps is", type(gold_value))

for i = 0, foundlist_getCount(FL1)-1  do
   ...
   ...
   ...
end


When you finished debugging, just change debug=1 to debug=0.
Just don't use printdebug inside loops with 9....999 iterations.





Zanzer wrote:

From my experience, we can skip using getAddress() function entirely. I made many CT files and trainers, where 95% of code is Lua, and 5% is ASM.

_________________
Back to top
View user's profile Send private message MSN Messenger
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