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 


Changing String (LENGTH)!

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

Joined: 17 Aug 2020
Posts: 170
Location: Milkey Way

PostPosted: Thu Sep 24, 2020 1:16 am    Post subject: Changing String (LENGTH)! Reply with quote

if i have a string (unicode)
Code:
str = 'CheatEngine'


i can print it like and its length
Code:
print(str)
print(#str)


What I want is to change the length of the string using LUA so that it sets itself to
Quote:
Cheat
or something like this, WITHOUT actually making
Code:
str = 'Cheat'


I tried using
Code:
#str = 5 --and
string.len(str) =  5

and obviously it doesnt work
Back to top
View user's profile Send private message
n1vX
Advanced Cheater
Reputation: 0

Joined: 27 May 2007
Posts: 61

PostPosted: Thu Sep 24, 2020 1:40 am    Post subject: Reply with quote

Have you tried this :

Code:
string.sub(str,1,5)
Back to top
View user's profile Send private message
MMM-304
Expert Cheater
Reputation: 0

Joined: 17 Aug 2020
Posts: 170
Location: Milkey Way

PostPosted: Thu Sep 24, 2020 1:54 am    Post subject: Reply with quote

yes thats not what i meant to ask
what i meant was that it would make
Quote:
str = 'Cheat'

like if u do like this
Code:
print(str)--= CheatEngine
--do Changes in length of string then
print(str)--Cheat

also not just that, I also want to be able to increase its actual length. although I dont think i can insert Char(s) in string can I?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25806
Location: The netherlands

PostPosted: Thu Sep 24, 2020 2:47 am    Post subject: Reply with quote

you can't from within lua (unless you use writeInteger on string specifiers inside lua itself, and I really do not recommend that)
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Sep 24, 2020 2:50 am    Post subject: Reply with quote

Code:

str = "CheatEngine";
str = str:sub(1,5); -- copy only first 5 characters
print(str);

function subString(s,length)
     return tostring(s):sub(1,length or -1);
end
str = "Test! variable";
print(str); -- Test! variable
print(subString(str)); -- Test!

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
MMM-304
Expert Cheater
Reputation: 0

Joined: 17 Aug 2020
Posts: 170
Location: Milkey Way

PostPosted: Thu Sep 24, 2020 4:26 am    Post subject: Reply with quote

Actually I was helping someone with this:

Code:
function setString(fdlist)
    getCurrentMemscan().waitTillDone()
    fdCount = fdlist.Items.Count
    for i=0,fdCount-1 do
        writeString(fdlist.Items.Item[i].Caption,"HELLO")
    end
end
function searchString(stringToSearch,mf)
    mf.VarType.ItemIndex = 7
    mf.scanvalue.Text = stringToSearch
    if mf.btnNewScan.Caption == 'First Scan' then
      mf.btnNewScan.doClick()
    end
    wt = getCurrentMemscan()
    wt.waitTillDone()
end
--------------------
theMF = MainForm
getfdlist = theMF.Foundlist3
searchString('My String',theMF)
wait = 0
while wait < 2 do
    wait = wait + 1
    sleep(500)
end
setString(getfdlist)


as u can see i was trying to overwrite the string My String with HELLO inside foundlist3 and the result was
Quote:
HELLOring


Similarly if i was to write a string whose length is bigger than My String, a fraction appeared!
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Sep 24, 2020 5:17 am    Post subject: Reply with quote

Code:
function writeString2(address,str)
   if (type(str)=='string') then
      local bytesTable = {}
      for char in str:gmatch('.') do
         bytesTable[#bytesTable+1] = char:byte();
      end
      bytesTable[#bytesTable+1] = 0 -- \0
      writeBytes(address,bytesTable)
   end
end

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Sep 24, 2020 5:24 am    Post subject: Reply with quote

Or (from old topic somewhere)

Code:
function replaceString(string_in, string_out, ignore_length)
   -- ignore_length = true or false
   if (not ignore_length) then
      if (not(string_in and string_out and #string_in >= #string_out)) then
         return print("Not recommended to override shorter string with a longer string");
      end
   end

   local bytes_in = {}
   local bytes_out = {}

   for i=1,(#string_in >= #string_out and #string_in or #string_out) do
      if (i <= #string_in) then
         table.insert(bytes_in,string.format("%x", tonumber(string.byte(string.sub(string_in,i,i)))));
      end

      if (i <= #string_out) then
         table.insert(bytes_out,tonumber(string.byte(string.sub(string_out,i,i))));
      end
   end

   local object = AOBScan(table.concat(bytes_in," "))

   if object then
      for entry = 0, object.Count -1 do
         writeBytes(object.getString(entry), unpack(bytes_out));
      end
      object.destroy();
      return true
   end

   return false

end

replaceString("HELLO", "Whatever", true)

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
MMM-304
Expert Cheater
Reputation: 0

Joined: 17 Aug 2020
Posts: 170
Location: Milkey Way

PostPosted: Thu Sep 24, 2020 5:37 am    Post subject: Reply with quote

wait does that means we can covert string into bytes than overwrite those bytes?
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1526

PostPosted: Thu Sep 24, 2020 5:56 am    Post subject: Reply with quote

Lua will give you usable examples.
Here are some of them;

Code:
local chr = "Cheat Engine V7.1";

function subString(s, start, length)
s=string.sub(s, start, length)
print("subString: "..start.." - "..length.." = "..s);
end

function CharacterResult1(str)
i, j = string.find(str, str)
print("Result1: find, (str) Start, Finish =  "..i, j)
end

function CharacterResult2(str, str1)
print("Result2: '"..str1.."' Start =  "..string.find(str, str1))
end

function CharacterResult3(str, str1)
s, f = string.find(str, str1)
print("Result3: '"..str1.."' Start, Finish =  "..s, f)
subString(str, s, f)
end

CharacterResult1(chr)
CharacterResult2(chr, "Engine")
CharacterResult3(chr, "Engine")
subString(chr, 6, 15)

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past


Last edited by AylinCE on Thu Sep 24, 2020 6:06 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Sep 24, 2020 6:01 am    Post subject: Reply with quote

MMM-304 wrote:
wait does that means we can covert string into bytes than overwrite those bytes?


Yes. Memory consists of address and bytes. String, float, double, etc just the types of value.

If want to replace, insert, etc in a string/text then take a look for lua string library or lua string manipulation, example has given by DaSpammer, Aylin.
But if want to manipulate string inside the memory then use byte conversion is more easy.

Correct me if I am wrong.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
daspamer
Grandmaster Cheater Supreme
Reputation: 54

Joined: 13 Sep 2011
Posts: 1588

PostPosted: Thu Sep 24, 2020 6:12 am    Post subject: This post has 1 review(s) Reply with quote

Corroder wrote:
MMM-304 wrote:
wait does that means we can covert string into bytes than overwrite those bytes?


Yes. Memory consists of address and bytes. String, float, double, etc just the types of value.

If want to replace, insert, etc in a string/text then take a look for lua string library or lua string manipulation, example has given by DaSpammer, Aylin.
But if want to manipulate string inside the memory then use byte conversion is more easy.

Correct me if I am wrong.

That's exactly what I was doing,
The reason why he couldn't overwrite 'My String' with 'HELLO' because 'HELLO' is missing null terminator

The example I sent, converts characters to bytes and adds null terminator character at the end.

I don't recommend overwriting a string with longer one, as it might override other essential data, would be better to allocate memory and replace the string pointer.

_________________
I'm rusty and getting older, help me re-learn lua.
Back to top
View user's profile Send private message Visit poster's website
MMM-304
Expert Cheater
Reputation: 0

Joined: 17 Aug 2020
Posts: 170
Location: Milkey Way

PostPosted: Thu Sep 24, 2020 6:48 am    Post subject: Reply with quote

hmm.. that makes sense. And u r right, overwriting a string with longer bytes than it already has, without allocating additional memory will most likely corrupt the data

Thanks guys. Appreciate ur help!
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1526

PostPosted: Thu Sep 24, 2020 6:51 am    Post subject: Reply with quote

If you want to change a order from FoundList without downloading it to AdressList,
It's complicated for me Smile
But when it comes to Lua Script, below is an example code for searching and exchanging.
You only need to specify the characters (text, as strings) to search and replace.


Code:
function TextReplacer(search, change)
local searchTable = {}
for i=1,search:len() do
searchTable[i]=string.format('%X',search: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,change)
end
object_destroy(aobs2);
aobs2=nil
end
beep()
print("done")
return
end

--Put the activation key below into the code you're calling.
--Note: make sure you put the above code at the top.
TextReplacer("you search", "new change") -- or new change="HELLO"

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website 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