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 


How to get index of caret position in Lua script?

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

Joined: 17 Jan 2018
Posts: 202

PostPosted: Sat Nov 25, 2023 7:55 am    Post subject: How to get index of caret position in Lua script? Reply with quote

How to get an index of a vertical caret position in the Lua script? Or, how to get the line index on which the caret is located in the Lua script?
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 30

Joined: 16 Feb 2017
Posts: 1210

PostPosted: Sat Nov 25, 2023 10:26 am    Post subject: Reply with quote

Let me know if you started the last project we mentioned.

You can import rows in the Lua script, but the current row in the script will not give you a valid height.

Code:
aa1 = MainForm.frmAutoInject.Assemblescreen.Font.Size
aa2 = MainForm.frmAutoInject.Assemblescreen.Lines.Count
--print(aa1,aa2)


If I understand correctly, the ideal display location would be as follows;

Code:
lf1 = tonumber(MainForm.frmAutoInject.Left) + tonumber(MainForm.frmAutoInject.Assemblescreen.Width)
tp1 = tonumber(MainForm.frmAutoInject.Top) + tonumber(MainForm.frmAutoInject.Assemblescreen.Height)

-- ex form name UDF1

UDF1.PopupMode = 1
UDF1.ShowInTaskBar = 2

lf2 = tonumber(lf1) - tonumber(UDF1.Width)
tp2 = tonumber(tp1) - tonumber(UDF1.Height) + 70

UDF1.Left = lf2
UDF1.Top = tp2
UDF1.Show()

MainForm.frmAutoInject.Assemblescreen.OnClick=function()
 UDF1.Hide()
end


Here is the reading code;

https://forum.cheatengine.org/viewtopic.php?p=5772590#5772590

_________________
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
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 202

PostPosted: Sat Nov 25, 2023 4:00 pm    Post subject: Reply with quote

AylinCE wrote:
Let me know if you started the last project we mentioned.

Did not do anything.
I want to find out the number of the selected line in the Lua script. In CEMemo we can find out this by: print(UDF1.CEMemo1.CaretPos.y) , but do not know how to do the same in the Lua script.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

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

PostPosted: Sun Nov 26, 2023 10:37 am    Post subject: Reply with quote

not to discourage you, but try pressing shift+alt and press the cursor keys Wink

but there's currently no exposed code to access the caretx or carety , but there is selstart which will be just as useful

_________________
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
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 202

PostPosted: Sun Nov 26, 2023 9:43 pm    Post subject: Reply with quote

Dark Byte wrote:
try pressing shift+alt and press the cursor keys

Useful function, thanks.
I want to use the following Strings Class method: insert(index, string): Inserts a string at a specific spot moving the items after it.
Code:
MainForm.frmAutoInject.Assemblescreen.Lines.insert(5, '111')
But instead of the number 5, I want to click on any line in the middle of the Lua script and insert a line with the text into this place. This is for 'Custom Lua Script Templates' extension. And then it would be possible to insert the code template into any line where we can click. Because if you have a script of 3000-10000 lines of the code, you need to insert a code template somewhere in the middle of the script.
Quote:
but there is selstart

How 'selstart' can be used to insert a string of text?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

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

PostPosted: Mon Nov 27, 2023 1:11 am    Post subject: Reply with quote

selstart is the character index of where the caret is
you could of course count the characters inside the lines object to calculate the position and then go from there, but you can also use seltext to change the string at the cursor.

i'm not 100% sure it appends or replaces the selection or if it works without selection

_________________
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


Last edited by Dark Byte on Mon Nov 27, 2023 3:30 am; edited 1 time in total
Back to top
View user's profile Send private message MSN Messenger
AylinCE
Grandmaster Cheater Supreme
Reputation: 30

Joined: 16 Feb 2017
Posts: 1210

PostPosted: Mon Nov 27, 2023 2:27 am    Post subject: Reply with quote

@DarkByte, is it possible to recalculate the "x" and "y" memory parameters of this code?

Last time it was working on 7.2. But I'm not sure how it will be calculated now.

I created a code that reads all lines with SelStart and finds the current (clicked) line by matching the selStart value. I did not find the code safe because it scrolled some lines.

The code below is more precise and could find the clicked word, line.

It would be useful if x and y and "TSynEditPtr" memory values were recalculated.

Code:
MainForm.frmAutoInject.Assemblescreen.OnClick = function(o) -- Lua Script TSynEdit ...
    local TSynEditPtr = userDataToInteger(o)
    local is64Bit,x,y = cheatEngineIs64Bit()
    if (is64Bit) then
        x = readIntegerLocal(readIntegerLocal(TSynEditPtr+0x5F8)+0x28)
        y = readIntegerLocal(readIntegerLocal(TSynEditPtr+0x5F8)+0x24)
    else
        x = readIntegerLocal(readIntegerLocal(readIntegerLocal(TSynEditPtr+0x18)+0x20)+0x78)
        y = readIntegerLocal(readIntegerLocal(readIntegerLocal(TSynEditPtr+0x18)+0x20)+0x74)
    end
    local o_CaretPos = { x = x, -- caret position in line
                         y = y - 1} -- current caret line adjusted,
    local wordPattern = "[%w%p]+"; -- what kind of word are we looking for? letters,numbers and punctuation symbols
    local caretPos,line = o_CaretPos.x-1,o.Lines[o_CaretPos.y] --x - caret position, y - line number

    if (line:sub(caretPos,caretPos+1):match(wordPattern)) then -- alphanumeric+punctuation, we care only about our pattern
        -- let's find the end of this word...
        local _,y = line:find(wordPattern,caretPos) -- finds the first occurence of our pattern which is the word we click on (second paramter is start position)
                                                    --returns x,y... but we dont care about x. , accepts 3rd parameter for plain search
        if (y) then
            -- aight, so we can cut the string off here
            local trimmedLine = line:sub(1,y);
            -- now we can use the same wordPattern to match the very last word of our trimmed line
            local word = trimmedLine:match(("(%s)$"):format(wordPattern));
            if (word) then
                print(("You have press on \"%s\""):format(word))
            end
        end
    end
end

_________________
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
panraven
Grandmaster Cheater
Reputation: 53

Joined: 01 Oct 2008
Posts: 935

PostPosted: Mon Nov 27, 2023 9:40 am    Post subject: This post has 1 review(s) Reply with quote

May try this.
Since I'm not sure mac's CE use same line-break as windows,
the first call is to setup, with windows usage as default,
it then return a function when call with a synedit/memo like ui,
it return a table {x=x_postion, y=y_position} with extra infos.
Code:

--
function emuCaret( LinesName, LF ) -- LF ~ crlf etc
  LinesName, LF = LinesName or 'Lines', LF or '\r\n'
  local reLF, txt = '()'..LF..'()'
  local function pos2xy(Pos)
    local y, x= 1, Pos
    for pre, post in txt:gmatch(reLF)do
      if Pos > pre then -- more line, y++
        y, x = y + 1, Pos - post + 1
      elseif Pos < post then -- pass Pos, done
        break
      end
    end
    return x, y, Pos
  end
  return function(ceo) -- input: a ce object
    local errmsg = type(ceo)=='userdata'
      and (not ceo.Name and '' or ceo.Name..'/') ..
          (ceo.ClassName or tostring(ceo))
      or'not userdata: '..tostring(ceo)
    txt = ceo[LinesName]
    txt = txt and txt.Text
    if not txt then return nil,'no text lines, '..errmsg end
    local r = {}
    r.x,  r.y,  r.Start = pos2xy(ceo.SelStart)
    r.xe, r.ye, r.End   = pos2xy(ceo.SelEnd)
    r.Selected = r.Start < r.End or false
    return r
  end
end

-- test , with [ Lua script:Cheat Table ] form
-- look up title bar of LuaEngine, ie. the print console like form
---
local function getCheatTableScriptScreen()
  local tx, frm = 'luascript:cheattable'
  for i=1,GetFormCount()do
    local f = GetForm(i-1)
    local tt = f.Caption
    local x = tt:gsub('%s+',''):lower()
    if x:find(tx) and f.Assemblescreen then
      frm = f
      break
    end
  end
  if not frm then
    return nil,'not found'
  else
    return frm.Assemblescreen, frm, frm.Assemblescreen.Lines.Text:len()
  end
end


FF = FF or getCheatTableScriptScreen()

local caret = emuCaret()

_TMR = _TMR or createTimer()
local t = _TMR
t.Interval, t.Enabled, t.OnTimer = 1000, true, function(tmr)
  local c = FF and caret(FF)
  if c then
    getLuaEngine().Caption = string.format('(%s,%s)~(%s,%s): <%s~%s> %s',
      c.x,c.y, c.xe, c.ye, c.Start, c.End, c.Selected and '<SELECTED>' or '<no select>'
    )
  end
end

---


_________________
- Retarded.
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 202

PostPosted: Mon Nov 27, 2023 10:09 am    Post subject: Reply with quote

Dark Byte wrote:
but you can also use seltext to change the string at the cursor.

Yes, it works, thanks. It inserts text at caret position. (It seems that it works like SynEdit1.InsertTextAtCaret('text'); in Lazarus.)
Code:
MainForm.frmAutoInject.Assemblescreen.SelText=[[123
45]]
MainForm.frmAutoInject.Assemblescreen.SelText='111\
12'

If the text is selected it replaces the selection. And it works without selection. Can be multiline.
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 30

Joined: 16 Feb 2017
Posts: 1210

PostPosted: Sat Dec 02, 2023 8:51 am    Post subject: Reply with quote

Thanks.
This code does not need a "data" update.

And the code below successfully prints the clicked word and *line in Lua Script.

panraven wrote:
...




Code:
function emuCaret( LinesName, LF ) -- LF ~ crlf etc
  LinesName, LF = LinesName or 'Lines', LF or '\r\n'
  local reLF, txt = '()'..LF..'()'
  local function pos2xy(Pos)
    local y, x= 1, Pos
    for pre, post in txt:gmatch(reLF)do
      if Pos > pre then -- more line, y++
        y, x = y + 1, Pos - post + 1
      elseif Pos < post then -- pass Pos, done
        break
      end
    end
    return x, y, Pos
  end
  return function(ceo) -- input: a ce object
    local errmsg = type(ceo)=='userdata'
      and (not ceo.Name and '' or ceo.Name..'/') ..
          (ceo.ClassName or tostring(ceo))
      or'not userdata: '..tostring(ceo)
    txt = ceo[LinesName]
    txt = txt and txt.Text
    if not txt then return nil,'no text lines, '..errmsg end
    local r = {}
    r.x,  r.y,  r.Start = pos2xy(ceo.SelStart)
    r.xe, r.ye, r.End   = pos2xy(ceo.SelEnd)
    r.Selected = r.Start < r.End or false
    return r
  end
end

local function getCheatTableScriptScreen()
  local tx, frm = 'luascript:cheattable'
  for i=1,GetFormCount()do
    local f = GetForm(i-1)
    local tt = f.Caption
    local x = tt:gsub('%s+',''):lower()
    if x:find(tx) and f.Assemblescreen then
      frm = f
      break
    end
  end
  if not frm then
    return nil,'not found'
  else
    return frm.Assemblescreen, frm, frm.Assemblescreen.Lines.Text:len()
  end
end

FF = FF or getCheatTableScriptScreen()

local caret = emuCaret()
local cpt = ""


-- test click , with [ Lua script:Cheat Table ] form

MainForm.frmAutoInject.Assemblescreen.OnClick = function(o)
local c = FF and caret(FF)
    local o_CaretPos = { x = c.x,
                         y = c.y - 1}
    local wordPattern = "[%w%p]+";
    local caretPos,line = o_CaretPos.x-1,o.Lines[o_CaretPos.y]

    if (line:sub(caretPos,caretPos+1):match(wordPattern)) then
        local _,y = line:find(wordPattern,caretPos)

        if (y) then

            local trimmedLine = line:sub(1,y);

            local word = trimmedLine:match(("(%s)$"):format(wordPattern));
            if word~="" or word~=" " then
                cpt = ("\"%s\""):format(word)
                lns = o_CaretPos.y + 1
                print("Lines: " .. lns .. " -> "..cpt)
            end
        end
        else
                lns = o_CaretPos.y + 1
                print("Lines: " .. lns)
    end
end

_________________
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