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 


Question concerning formerly working code

 
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: Fri Jan 10, 2014 10:14 am    Post subject: Question concerning formerly working code Reply with quote

When moving my cheat tables from one lap top to another, I detected an error in calculation. Here is the setup using version 6.2:

for x = 0, addresslist_getCount(addresslist)-1 do
memrec4 = addresslist_getMemoryRecordByID(addresslist, x);
if byteoffset ~= orrinaddress then
print("Table record num ", x + 1, "is ", memrec4);
--remove the correction for testing
--memoryrecord_setAddress(memrec4, memoryrecord_getAddress(memrec4) + byteoffset - orrinaddress);

The output from the seems normal from record 1 to 154. The ouput is 00000000 from 155 to 3985. Normal output from 3986 to 4949 and then reverted to 00000000 from 4950 to 5408.

1. Why are there zero address outputs from the table?
2. For refreshers the memoryrecord_setAddress statement has an addition function. What should the format be for this addition? Numbers? Hex? Hex (without 0x)?
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: Fri Jan 10, 2014 11:33 am    Post subject: Reply with quote

Using addresslist_getMemoryRecordByID

Example, you created five memory records. (click "add address manually" five times)
Their IDs will be 0,1,2,3,4. Delete second(ID=1) and third(ID=2) memory record.
OK, create another five memory records.

You will have 8 memory records. Their IDs will be:
0,3,4,5,6,7,8,9


Now, look at your loop:
for x = 0, addresslist_getCount(addresslist)-1 do

X will start at 0, and end at 7. (so, 0,1,2,3,4,5,6,7)

Look at this:
memrec4 = addresslist_getMemoryRecordByID(addresslist, x)

As you see, addresslist_getMemoryRecordByID will return nil for X=1,2 , and you are missing memrec with ID=8 and 9


How to iterate on all memrec from addresslist?
Again, their IDs are:
0,3,4,5,6,7,8,9

memrec have another useful property. Indexes:
0,1,2,3,4,5,6,7


Instead of
addresslist_getMemoryRecordByID(addresslist, ID)

better use:
addresslist_getMemoryRecord(addresslist, index)



memoryRecord properties types, when reading (getting):
ID            number
Index       number

Address    string/decimal number   -  When setting new value you can use string hex. And CE will automatically convert "decimal number" address to "string hex" address.

Value        string       -   decimal or hex (depends on "show as hex" and "show as signed" settings)
Type         number    -   open defines.lua file and find vtByte, ..., vtCustom variables



memoryRecord property, Address, type is string or number, it depends on what CE version you are using, and what Lua instruction you are using to get it.

For now, CE6.3 :
memoryrecord_getAddress - will return address as decimal number, and optionally will return Lua Table (integers) with pointer offsets ( if memoryrecord is a Pointer)

memoryrecord.getAddress() - will return address as hex string, and optionally will return Lua Table (integers) with pointer offsets ( if memoryrecord is a Pointer)

memoryrecord.Address - you can set or get address/offsets


So, change this
memoryrecord_setAddress(memrec4, memoryrecord_getAddress(memrec4) + byteoffset - orrinaddress)

into this:
memoryrecord_setAddress(memrec4, tonumber(memoryrecord_getAddress(memrec4), 16) + byteoffset - orrinaddress);



memoryrecord_getAddress(memrec4) - will get address in "string hex"(without leading '0x') or "decimal number" format

tonumber( , 16) - will convert string hex to decimal number (integer). If it is already a number, it will return that number.

"+ byteoffset - orrinaddress" - your correction

memoryrecord_setAddress - will set address, you can use "string hex" (without leading '0x') or "decimal number"



About your script.
I can not help you more. For that I have to see full script.
About, "I have over 1000 memory records". Maybe it would be better to keep addresses inside Lua table?

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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sat Jan 11, 2014 12:15 am    Post subject: Reply with quote

There are 5408 entries (I didn't count them but the number seems about correct) in the table so the output is correct for the number. All of them have a description, value, type and address, so that is why I asked the question.

I'm not sure what you mean by "Maybe it would be better to keep addresses inside Lua table"? They all are there. I will post the table and you can look at the whole code, minus some print statements as I am on a different machine.

I'll try the code changes tomorrow.

EDIT AFTER CHANGING CODE:

Code:

addresslist = getAddressList();
memrec3 = addresslist_getMemoryRecordByDescription(addresslist, "Orrin");
orrinaddress = memoryrecord_getAddress(memrec3);--Item in num format
print("Orrin table address in num format", orrinaddress);
for x = 0, addresslist_getCount(addresslist)-1 do
--memrec4 = addresslist_getMemoryRecordByID(addresslist, x);--Original code
Suggested change, memrec4 = addresslist_getMemoryRecord(addresslist, x);
Code:

memrec4 = addresslist_getMemoryRecord(addresslist, x);
--print("The description for record num ", x + 1, "is", memoryrecord_getDescription(memrec4));
--print("The table address for record num ", x + 1, "is", memoryrecord_getAddress(memrec4));
--print("The table address for record num ", x + 1, "is", string.format('%x', memoryrecord_getAddress(memrec4)));
--print("The adjusted table address for record num ", x + 1, "is", string.format('%x',(memoryrecord_getAddress(memrec4) + byteoffset - orrinaddress)));if byteoffset ~= orrinaddress then
--memoryrecord_setAddress(memrec4, itemtableaddress + byteoffset - orrinaddress);--Original code
Suggested change, memoryrecord_setAddress(memrec4, tonumber(memoryrecord_getAddress(memrec4), 16) + byteoffset - orrinaddress);
Code:

--memoryrecord_setAddress(memrec4, tonumber(memoryrecord_getAddress(memrec4), 16) + byteoffset - orrinaddress);
memoryrecord_setAddress(memrec4, string.format('%x',(memoryrecord_getAddress(memrec4) + byteoffset - orrinaddress)));

Change in code required because summation of memrec4, byteoffset and orrinaddress adjusted table as if summation were hex not num
Code:


end;
end;


The change from the record ID was very informative, thanks. I'll have to adjust all my codes to this. I guess my other tables didn't have a lot of deleted records as this code has worked for other games.



HEROES3_62.ct
 Description:

Download
 Filename:  HEROES3_62.ct
 Filesize:  1.2 MB
 Downloaded:  1278 Time(s)

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: Sat Jan 11, 2014 12:23 pm    Post subject: Reply with quote

Here is my solution:

Code:
function CEButton1Click(sender)
  local AL = getAddressList()
  local MS = createMemScan()
  local FL = createFoundList(MS)

  --scanoption, vartype, roundingtype, input1, input2,
  --startAddress ,stopAddress ,protectionflags ,alignmenttype ,"alignmentparam",
  --isHexadecimalInput ,isNotABinaryString, isunicodescan, iscasesensitive
  memscan_firstScan(MS,
    soExactValue, vtString, rtRounded, "Orrin", "",
    0, 0xffffffffffffffff, "*X*C*W", fsmNotAligned, "",
    false, false, false, true)

  memscan_waitTillDone(MS)
  foundlist_initialize(FL)
  print("Num of Orrin found ", foundlist_getCount(FL))

  for i = 0, foundlist_getCount(FL)-1  do
   local orinAddr_hex = foundlist_getAddress(FL, i) -- foundlist_getAddress always returns in hex without 0x
   local orinAddr_dec = tonumber(orinAddr_hex,16)   -- convert to decimal number
   local tivaaddress_dec = orinAddr_dec + 0x22c9e   -- distance between orrin and tiva should be 0x22c9e

   print("Orrin record num ", i + 1, ", address "..orinAddr_hex)
   print("Testing for Tiva, address ", string.format('%X', tivaaddress_dec))

   if readString(tivaaddress_dec, 5) == "Tiva" then
     print('')
     print("Found Tiva which is 0x22c9e after Orrin")
     print("Found correct Orrin, address is ", orinAddr_hex)

     -- DO MAGIC (correct "address" property for all memory records)
     local oldOrrinAddr_hex = memoryrecord_getAddress( addresslist_getMemoryRecordByDescription(AL, "Orrin") )
     local oldOrrinAddr_dec = tonumber(oldOrrinAddr_hex,16)
     local correction = orinAddr_dec - oldOrrinAddr_dec

     for j=0, addresslist_getCount(AL)-1 do
       local mr = addresslist_getMemoryRecord(AL, j)
       local newAddress = memoryrecord_getAddress(mr) + correction
       memoryrecord_setAddress(mr,newAddress)
     end -- loop 'for j' end

     break -- break loop 'for i'
   end

  end -- loop 'for i' end

  sleep(100)
  object_destroy(FL)
  object_destroy(MS)
end -- function end

form_show(CheatPanel)





EDIT:


I checked again, all methods we can get Address property. I tested CE6.3+ (from SVN), CE6.3 official, CE6.2 official.
Memory record address was set to 400500.


CE6.3+
Code:
mr = getAddressList().getMemoryRecord(0)

print( type( memoryrecord_getAddress(mr) ), memoryrecord_getAddress(mr) )
print( type( mr.getAddress() ), mr.getAddress() )
print( type(mr.Address), mr.Address )


output:
Code:
number 4195584
string 400500
string 400500



CE6.3 official, output is the same
Code:
mr = getAddressList().getMemoryRecord(0)

print( type( memoryrecord_getAddress(mr) ), memoryrecord_getAddress(mr) )
print( type( mr.getAddress() ), mr.getAddress() )
print( type(mr.Address), mr.Address )


output:
Code:
number 4195584
string 400500
string 400500


So,
memoryrecord_getAddress - returns number
mr.getAddress() - returns string, a hex string
mr.Address - also returns string, a hex string




CE6.2 official
Code:
al = getAddressList()
mr = addresslist_getMemoryRecord(al,0)

print( type( memoryrecord_getAddress(mr) ), memoryrecord_getAddress(mr) )


output:
Code:
number 4195584


memoryrecord_getAddress - returns number



I think, no one is using older CE versions (6.1, 6.0). So it is safe to assume that

memoryrecord retrieved address will be ALWAYS
memoryrecord_getAddress(mr) - number (CE 6.2,6.3,6.3+)
mr.getAddress() - string, a hex string (CE 6.3,6.3+)
mr.Address - string, a hex string (CE 6.3,6.3+)


And
foundlist_getAddress(fl,index) - string, a hex string (CE 6.2,6.3,6.3+)
fl.getAddress(index) - string, a hex string (CE 6.3,6.3+)
fl.Address[index] - string, a hex string (CE 6.3,6.3+)



HEROES3_62.ct
 Description:
Created with CE6.3+ (I used CE6.2 Lua functions, should work)

Download
 Filename:  HEROES3_62.ct
 Filesize:  1.55 MB
 Downloaded:  1021 Time(s)


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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sat Jan 11, 2014 2:39 pm    Post subject: Reply with quote

Thanks for the revised code, much more compact than mine.

However there are two errors in the code. It may be the way 6.2 displays numbers and strings or in the original settings for the scan.

local oldOrrinAddr_hex = memoryrecord_getAddress( addresslist_getMemoryRecordByDescription(AL, "Orrin") )

The statement retrieves numbers not hex format. Changed to:

local oldOrrinAddr_dec = memoryrecord_getAddress( addresslist_getMemoryRecordByDescription(AL, "Orrin") )
--local oldOrrinAddr_dec = tonumber(oldOrrinAddr_hex,16)

memoryrecord_setAddress(mr,newAddress)

This statement, just as my attempts this morning, adjusts the table as if newAddress is a hex. The calculation is a number, so changed to:

memoryrecord_setAddress(mr,string.format('%x', newAddress))

The code adjusted the table correctly.

Again thanks for the streamlined code and your time and effort.
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: Sat Jan 11, 2014 3:18 pm    Post subject: Reply with quote

This is not an error:
oldOrrinAddr_hex is just a variable name (I forgot to change to something else)


This:
tonumber(hexstring,16)

is equal to:
tonumber(tonumber(hexstring,16))

and equal to:
tonumber(tonumber(tonumber(tonumber(tonumber(hexstring,16)))))
Because if argument is already a number, it returns that number
http://www.lua.org/manual/5.1/manual.html#pdf-tonumber


And by doing
Code:
step1 = memoryrecord_getAddress(mr)  -- a number
or
step1 = mr.getAddress() -- a hexstring
or
step1 = mr.Address -- a hexstring


and then
Code:
address_dec = tonumber(step1,16)

You will be perfectly sure that address_dec variable is just a decimal number.




About memoryrecord_setAddress, yes, it was bugged if I remember correctly (CE6.1 maybe). I forgot to check this. Give me a while (I will check CE6.2 and CE6.3).

Btw. Why don't you use CE6.3? You can get more streamlined Lua script.

EDIT:
Wait, CE6.2, CE6.3 and CE6.3+ version still have this bugged memoryrecord_setAddress


This code deceived me:
http://code.google.com/p/cheat-engine/source/browse/trunk/Cheat+Engine/LuaMemoryRecord.pas?%72=2210#239

It should convert decimal to hex automatically (line 241 to 245).

Conslusion:
we have to take into account this:
memoryrecord_setAddress(mr, newAddress) -- newAddress can be a hexstring or decimal number, but, strangely decimal doesn't work as should

mr.setAddress(newAddress) -- newAddress can be only a hexstring

mr.Address = newAddress -- newAddress can be only a hexstring

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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Sat Jan 11, 2014 4:28 pm    Post subject: Reply with quote

This table was first built, without code, in maybe ver 5.1. For a long time I recalculated all the tables manually, but became increasingly annoyed with the process and wanted to automate the process. Many comments from other members indicated I should use pointers. Pointers do not work with the games I play, but those comments kept coming. Pointers MAY work with Heroes3 as it is played as a stand alone whereas the vast majority of the other games are played through a console where pointers are useless.

With a lot of help from DB I developed this code in maybe ver 6.1, the first one of many written. Lots of different schemes to get the tables recalculated, and all had a great deal of help from DB and DaSpamer.

Then I encountered a problem with one of the game codes, DB suggested I use the command errorOnLookupFailure(false), but that was only available in ver 6.2.

Downloaded and installed. I have two laps one using WinXP and the other Win 7. It seemed that code structure was handled differently between the two OS and/or 6.1 and 6.2. I had to re-do codes developed in the XP system. I'm not complaining as it must be a very difficult job to code a application like CE. But I decided not to update further until/unless it becomes necessary to run my old games.

Long winded but any explanation would require history to understand current choices.

Thanks for the link I have been using http://cheat-engine.googlecode.com/svn/trunk/Cheat%20Engine/bin/main.lua?p=1487 along with http://www.lua.org/manual/5.1/ DB told me there was a newer link but I didn't add it to favorites.
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: Sat Jan 11, 2014 4:34 pm    Post subject: Reply with quote

Probably because newer game version (executable) is using ASLR (shipped with win7 and newer)


It's not that troublesome to create ASLR protected binary. It's easy, using this {$SetPEOptFlags $100} you instruct compiler to set the ASLR flag.

Link I posted points to LuaMemoryRecord.pas, to explain why I thought memoryrecord_setAddress accepts decimal number. (it doesn't work as should)



About link to Lua docs (main.lua file), your link:
Code:
http://cheat-engine.googlecode.com/svn/trunk/Cheat%20Engine/bin/main.lua?p=1487

Did you notice that ?p=1487 thing? Remove it and you will have link to the newest main.lua:
http://cheat-engine.googlecode.com/svn/trunk/Cheat%20Engine/bin/main.lua

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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Tue Apr 01, 2014 6:19 pm    Post subject: mgr.inz.Player Look here please Reply with quote

Using your condensed code in another game, I have stumbled yet again badly.
Code:
function RecalculateAddresses(sender)
 goldvalue = 74500
 local AL = getAddressList()
 local MS = createMemScan()
 local FL = createFoundList(MS)
 memscan_firstScan(MS,
   soExactValue, vtQword, rtRounded, goldvalue, "",
   0, 0xffffffffffffffff, "*X*C*W", fsmNotAligned, "",
   false, false, false, true)

 memscan_waitTillDone(MS)
 foundlist_initialize(FL)
 print("Num of Gold found ", foundlist_getCount(FL))

 for i = 0, foundlist_getCount(FL)-1  do
 local goldAddr_hex = foundlist_getAddress(FL, i) -- foundlist_getAddress always returns in hex without 0x
 print("The address for Gold is ", goldAddr_hex)
 local goldAddr_dec = tonumber(goldAddr_hex,16)   -- convert to decimal number
 print("The num conversion for Gold address is ", goldAddr_dec)
 end
 end


Nothing ever prints so the memscan setup is not correct, tried a few changes but still no print outs.
The goldvalue will eventually be entered into an edit box that will be use in the scan. I set the value manually and a normal manual scan accesses 6 values.

What do I need to change in the code?
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: Wed Apr 09, 2014 1:14 pm    Post subject: Reply with quote

bknight2602 wrote:
Sorry, I hit the send button when I wanted to hit the preview.
Seondly I'm not sure how to post a link in a PM or thread.
Anyway, thanks for responding the thread was under Codes formerly not working. This is the one you provided a better way to recalculate addresses. What I was asking basically was the code doesn't work, no debug prints on the number of items found with the scan, so I've done something wrong with the memscan using a 4 byte number.
Again thanks

Oh, this topic. Yes, I didn't see the new post. ( I didn't add this topic to "Watched topics" list ).


EDIT:
for me, function you posted above works:
Code:
RecalculateAddresses()

Num of Gold found  7
The address for Gold is  00400500
The num conversion for Gold address is  4195584
The address for Gold is  00400510
The num conversion for Gold address is  4195600
The address for Gold is  00400600
The num conversion for Gold address is  4195840
The address for Gold is  03960E70
The num conversion for Gold address is  60165744
The address for Gold is  03993E70
The num conversion for Gold address is  60374640
The address for Gold is  047C87B0
The num conversion for Gold address is  75270064
The address for Gold is  047C8940
The num conversion for Gold address is  75270464

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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Wed Apr 09, 2014 2:10 pm    Post subject: Reply with quote

OK, but pasting the following code into the debug window and executing,

I get no search results and of course no print out of the addresses.

The only change made was to the gold amount set.

However, pasting the code into the table and then executing I did get (in this particular case) two hits. The thread entry was trying to execute the code in the debug window and I didn't at that day try it in the table.

So um why didn't the code work in the debug window?
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: Wed Apr 09, 2014 2:27 pm    Post subject: Reply with quote

To be clear. For you "debug window" is the same as "Lua Engine window" ?
_________________
Back to top
View user's profile Send private message MSN Messenger
bknight2602
Grandmaster Cheater
Reputation: 0

Joined: 08 Oct 2012
Posts: 577

PostPosted: Wed Apr 09, 2014 2:40 pm    Post subject: Reply with quote

Yes, my term
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: Wed Apr 09, 2014 2:46 pm    Post subject: Reply with quote

Well, I just attached to "tutorial" process, changed few addresses values (address: 400500, 400510 and 400600, value: 74500 )
pasted above code, executed it
then I typed: RecalculateAddresses() and again I clicked "execute"

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

Joined: 08 Oct 2012
Posts: 577

PostPosted: Wed Apr 09, 2014 3:23 pm    Post subject: Reply with quote

I didn't paste in the last line, as it seemed to me that the Execute button would execute what was in the box, but I stand corrected. Thanks
Back to top
View user's profile Send private message Yahoo 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