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 


LuaServer, how 'func call by name'?

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

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Mar 14, 2018 2:38 am    Post subject: LuaServer, how 'func call by name'? Reply with quote

I try to call luaserver with function call by name (cmd = 3 ), by reading lua_server.pas and examine the assembler from server ce (64bit).
I come up with following test script, but it not work.
What am I missing? Thank you.
Code:

if pipe then pipe.Destroy()end
pipe = connectToPipe("Main_Test",500)

local function readAll()
  local rb={}
  local r = pipe.readByte()
  while r do rb[1+#rb], r = string.format('%02X',r), pipe.readByte()end
  return #rb==0 and '<timeout>' or table.concat(rb,' ')
end

local function execScript(code)
  local cmd = 1
  pipe.writeByte(cmd)
  pipe.writeDword(#code) -- script size
  pipe.writeString(code) -- script string
  pipe.writeQword(0) -- eof?
  return readAll()
end


local function execFunc(refOrName)
  local cmd = 3 -- exec a defined function, by ref or by name, here only by name
  pipe.writeByte(cmd)
  pipe.writeByte(0) -- async ?

  if type(refOrName)=='number' then

    pipe.writeDword(refOrName) -- function ref
  elseif type(refOrName)=='string' then
   pipe.writeDword(0) -- function ref? but 0 for 'by name'
   pipe.writeWord(#refOrName)  -- name size
   pipe.writeString(refOrName) -- name string
  else
    return nil,'oops'
  end
  pipe.writeByte(0) -- input parameter count, here 0 for test
  pipe.writeByte(1) -- expected return count, here 1 for test

  return readAll()
end


local code = [[
  function retStr()
    print'in retStr, expected to return 1 string'
    return 'Hello'
  end
  print("----test start")
  print(retStr())
  print("----test end")
  print('should "retStr" defined ? ',tostring(retStr),tostring(_G.retStr))
  if _REF then _REF=nil,destroyRef(_REF)end
  _REF = createRef(retStr)
  print(_REF)
  return _REF
]]

print('script:\n',execScript(code))
print('func:\n',execFunc'retStr')


client output
Code:

script:
 12 00 00 00 00 00 00 00
func:
 <timeout>

server output
Code:

----test start
in retStr, expected to return 1 string
Hello
----test end
should "retStr" defined ?  function: 000000000FED9920 function: 000000000FED9920
18

_________________
- Retarded.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

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

PostPosted: Wed Mar 14, 2018 3:51 am    Post subject: Reply with quote

is your "Main_Test" in the same process as this lua script you're running?
If so, then calling pipe_readByte will will block the call to synchronize() which will then wait until readByte stops reading (timeout)

you can try an async script (so it won't synchronize with the main thread, but keep in mind that async threads NEED to synchronize for GUI commands) or run execFunc in a thread instead (same thing about GUI stuff)
Or run a 2nd CE and connect to 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
panraven
Grandmaster Cheater
Reputation: 54

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Mar 14, 2018 5:39 am    Post subject: Reply with quote

Thank you the response.

They are in different CE instance, both 64bit version.

I've try both async or not, unless my sizes of data sent in function execFunc is not right.

I use execScript to define function retStr in server ce,
and seems verified it do define the function and execute once over the server side as testing.

--

The intention of the testing is to return string (or may be multiple retunr values) from server side to the client.
It seems beside command 3, command 1,2,4 all can only return integers.

_________________
- Retarded.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 457

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

PostPosted: Wed Mar 14, 2018 6:32 am    Post subject: Reply with quote

I think your readAll function breaks the pipe
if readByte() times out, the connection is destroyed, therefore retStr() never gets called after execScript(code)

So only read the bytes you are expecting, not more

Also, while the other commands only return 1 parameter, that should not be an issue as parameters and return values are address size values. This means that the caller can allocate as much memory as it wants into the target process, and then pass on that memory address to the function. The function can then use read/writeBytesLocal (and related functions) to read and fill in the return data.

And when the caller is done with the data, it can free it as well
Code:

allocateMemory(size, BaseAddress OPTIONAL, Protection OPTIONAL): Allocates some memory into the target process
deAlloc(address, size OPTIONAL): Frees allocated memory


Sure, it's not as 'easy' as using default lua functions and not having to write a call wrapper for the functions

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

Joined: 01 Oct 2008
Posts: 941

PostPosted: Wed Mar 14, 2018 7:02 am    Post subject: Reply with quote

Oh, it work!!

In another test, the function should return 5123,'Hello' (2 values).
By replacing return readAll() with
Code:

  return pipe.readByte(), -- return cnt
         pipe.readByte(),pipe.readQword(), -- type int, int
         pipe.readByte(),pipe.readString(pipe.readWord()) -- type string, string(word len)

it return
Code:

func:
 2 2 5123 5 Hello


Thank you!

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