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 


Lua scripts to scan/replace strings can't find UTF-16 result

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking
View previous topic :: View next topic  
Author Message
Autem
Expert Cheater
Reputation: 1

Joined: 30 Jan 2023
Posts: 155

PostPosted: Tue Jun 04, 2024 3:29 pm    Post subject: Lua scripts to scan/replace strings can't find UTF-16 result Reply with quote

After doing some searching on this I have been testing 3 different "replace strings" scripts and the first 2 seem to work identically in that they're always missing UTF-16 results. How can I get either of these first 2 to also include UTF-16?

There is also a 3rd script I am pasting below that DOES find UTF-16 results but won't work for strings/text. It only works for numbers replacement.

If any of these three can be made to search text strings and also include utf-16, I am all set. Would very much appreciate some help with this, thanks!

First script (doesn't find UTF-16):
Code:
[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
{$lua}
if syntaxcheck then return end

function Textswap()
  local txt2scn = 'OLDTEXT'
  local txt2chg = 'NewText'
   --convert to hex string
  local searchTable = {}
  for i=1,txt2scn:len() do
    searchTable[i]=string.format('%X',txt2scn:byte(i))
  end
  local searchHexString = table.concat(searchTable)
  local aobs2 = AOBScan(searchHexString)
 if(aobs2 ~= nil) then
    for i=0,stringlist_getCount(aobs2)-1 do
       local address=stringlist_getString(aobs2,i)
       writeString('0x'..address,change2)
    end
    object_destroy(aobs2);
    aobs2=nil
  end
  beep()
  print('Done')
end

[DISABLE]




Second script (doesn't find UTF-16):
Code:
[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
{$lua}
if syntaxcheck then return end

function byteTableToAobString(t)
  for k,v in ipairs(t) do
    t[k] = ('%02X'):format(v)
  end
  return table.concat(t, ' ')
end

function TextToAobs(text)
  newvalue = (text) --:lower()
  --print(newvalue)
  if not newvalue then return end
newvalue = stringToByteTable(newvalue)
  newvalue = byteTableToAobString(newvalue)
  print(newvalue)
return newvalue
end

print(TextToAobs("Zar"))

function multiReplace(from,to)
  local good,aob = 0,AOBScan(tostring(from),"+W*XC")

  if (aob == nil) then
  showMessage("Code not found..")
  else
 aobCnt=strings_getCount(aob) - 1
    for i=0, aobCnt do
      if autoAssemble (aob[i]..":\ndb "..tostring(to)) then good=good+1
      end
    end
  end
    aob.Destroy()
    showMessage("Replace code: " .. good)
  return good
end

multiReplace(TextToAobs("OLDTEXT"),TextToAobs("NewText"))


[DISABLE]




Third script DOES find utf-16 results but it will not work unless the search and replace values are just numbers. Is there a way to get this script to also support searching and replacing text?
Code:
[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
{$lua}
if syntaxcheck then return end

function ChangeValues(v, v2)
local MemScan = createMemScan()

MemScan.firstScan(soExactValue, vtString, nil, v, nil, '0', '7fffffffffff', '+W*C*X', fsmNotAligned, '0', false, false, false, false)
MemScan.waitTillDone()
local Results = createFoundList(MemScan)
Results.initialize()
for i=0,Results.Count-1 do
   print('replacing', Results[i], v)
   writeString(Results[i],v2,false)
end
MemScan.destroy()
Results.destroy()

--Next we make sure utf-16 widechar is also found below
local MemScan = createMemScan()
MemScan.firstScan(soExactValue, vtString, nil, v, nil, '0', '7fffffffffff', '+W*C*X', fsmNotAligned, '0', false, false, true, false)
MemScan.waitTillDone()
local Results = createFoundList(MemScan)
Results.initialize()
for i=0,Results.Count-1 do
   print('replacing', Results[i], v)
   writeString(Results[i],v2,true)
end
MemScan.destroy()
Results.destroy()
end

ChangeValues(OLDTEXT, NewText)   --Doesn't work as is for text strings, but will work for number values

[DISABLE]
Back to top
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4711

PostPosted: Tue Jun 04, 2024 4:28 pm    Post subject: Reply with quote

Each character in CE's standard string type is represented as 1-byte values. In CE's UTF-16 string type, they're 2-byte values. You can't really search for both 1-byte and 2-byte values in the same scan. That's why your first and second scripts don't work.

The third script works fine for me. I don't know what you mean by "it will not work unless the search and replace values are just numbers." The input to the scan gets converted to a string regardless of its actual type.

_________________
I don't know where I'm going, but I'll figure it out when I get there.
Back to top
View user's profile Send private message
Autem
Expert Cheater
Reputation: 1

Joined: 30 Jan 2023
Posts: 155

PostPosted: Tue Jun 04, 2024 4:45 pm    Post subject: Reply with quote

ParkourPenguin wrote:
The third script works fine for me. I don't know what you mean by "it will not work unless the search and replace values are just numbers." The input to the scan gets converted to a string regardless of its actual type.


I got it working but needed to add quotation marks I was forgetting. Thanks for encouraging me to give that one a closer look!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General Gamehacking 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