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 


Access Lua variable from within AA, from within Lua?

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

Joined: 23 Feb 2013
Posts: 73

PostPosted: Tue Mar 06, 2018 12:16 am    Post subject: Access Lua variable from within AA, from within Lua? Reply with quote

I have the following Auto Assembler code:
Code:
[ENABLE]
//code from here to '[DISABLE]' will be used to enable the cheat
alloc(newmem,2048,"WHGame.DLL"+1B915C)
label(returnhere)
label(originalcode)
label(exit)

newmem: //this is allocated memory, you have read,write,execute access
mov [rcx+38],000001F4
test edx,edx

originalcode:
add [rcx+38],edx
test edx,edx

exit:
jmp returnhere

"WHGame.DLL"+1B915C:
jmp newmem
returnhere:

[DISABLE]
//code from here till the end of the code will be used to disable the cheat
dealloc(newmem)

"WHGame.DLL"+1B915C:
add [rcx+38],edx
test edx,edx
//Alt: db 01 51 38 85 D2

While this works pretty well for my own purposes, I decided to take it a step further...
I'd like to replace (Line #9) mov [rcx+38],000001F4 with mov [rcx+38],[SOME_LUA_VARIABLE], so that it's not just some fixed value... but rather, a value that the user can enter into a form's edit box.

So I made a form, added an edit box and a toggle box, then moved the AA code into my main Lua script (as a multiline string variable).
It gets enabled/disabled via the toggle box, which calls autoAssemble(). And after a quick test, it does indeed toggle the AA script, so...
So far, so good!

Now all I need is a way to access the value of the edit box from within the AA code, or some means of passing data from Lua to AA, when the AA code itself is being utilized from within a Lua script.
Is this possible?
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: Tue Mar 06, 2018 1:45 am    Post subject: Reply with quote

allocate data in the AA script and use registersymbol on it

then use lua writeInteger when the values update and just let the AA script read that memory

_________________
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
Rectangle
Advanced Cheater
Reputation: 1

Joined: 23 Feb 2013
Posts: 73

PostPosted: Tue Mar 06, 2018 8:57 pm    Post subject: Reply with quote

Dark Byte wrote:
allocate data in the AA script and use registersymbol on it

then use lua writeInteger when the values update and just let the AA script read that memory

You know, Dark Byte... You don't have to answer over 50% of every question ever created on your site, lol jk =P
Anyway, that worked. Thanks!

Here's what I came up with (for Kingdom Come: Deliverance v1.2 CODEX):
Code:
--[[ GLOBAL VARIABLES --]]
itemsamount = 0x00000032 -- 50 in decimal
diInfiniteItems = nil
bEnabledInfiniteItems = false

--[[ FUNCTION DECLARATIONS --]]
local updateItemsAmount = nil
local updateInfiniteItems = nil
local toggleInfiniteItems = nil

--[[ LOCAL SCRIPTS --]]
local scrInfiniteItems = [[
[ENABLE]
alloc(newmem,2048,"WHGame.DLL"+1B915C)
label(returnhere)
label(originalcode)
label(exit)
label(itemsamount)
registersymbol(itemsamount)

newmem:
mov edx,dword ptr [itemsamount]
mov [rcx+38],edx
test edx,edx

originalcode:
add [rcx+38],edx
test edx,edx

exit:
jmp returnhere

itemsamount:
dd 00000032

"WHGame.DLL"+1B915C:
jmp newmem
nop

returnhere:
[DISABLE]
unregistersymbol(itemsamount)
dealloc(newmem)

"WHGame.DLL"+1B915C:
add [rcx+38],edx
test edx,edx
]]

--[[ FUNCTION IMPLEMENTATIONS --]]
updateItemsAmount = function()
  -- Groschen is passed to EDX at 10 times the actual desired value, for whatever reason.
  --   Also for whatever reason, this equates to 5 times the actual desired value for Groschen,
  --   and half the value for anything else (AFAIK) when passing from Lua to AA. No idea why.

  -- Grab the current value from the text box
  infiniteItemsAmount = tonumber(frmMain.txtItemsAmount.Text)
  if frmMain.chkGroschen.State == cbChecked then
    -- Update the value for Groschen
    itemsamount = tonumber(infiniteItemsAmount * 5)
  else
    -- Update the value for other items
    itemsamount = tonumber(infiniteItemsAmount * 0.5)
  end
  if writeInteger("itemsamount", itemsamount) then print("[Y]") else print("[N]") end
end

updateInfiniteItems = function()
  local bSuccess = false
  if bEnabledInfiniteItems then
    bSuccess, diInfiniteItems = autoAssemble(scrInfiniteItems)
    if bSuccess then
      print("[SUCCESS]: Infinite Items Enabled")
    else
      print("[FAILURE]: Infinite Items Enabled")
    end
    bEnabledInfiniteItems = bSuccess
  else
    if diInfiniteItems ~= nil then
      bSuccess = autoAssemble(scrInfiniteItems, diInfiniteItems)
      if bSuccess then
        print("[SUCCESS]: Infinite Items Disabled")
      else
        print("[FAILURE]: Infinite Items Disabled")
      end
    end
    bEnabledInfiniteItems = false
    diInfiniteItems = nil
  end

  updateItemsAmount()

  if bEnabledInfiniteItems then
    frmMain.tbItems.setCaption("Disable Infinite Items")
  else
    frmMain.tbItems.setCaption("Enable Infinite Items")
  end
end

toggleInfiniteItems = function()
  bEnabledInfiniteItems = not bEnabledInfiniteItems
  updateInfiniteItems()
end

--[[ FORM/CONTROL EVENTS --]]
function tbItemsChange(sender)
  -- Toggle Box > OnChange()
  toggleInfiniteItems()
end

function txtItemsAmountKeyPress(sender, key)
  -- Edit Box > OnKeyPress()
  -- Makes sure only valid numeric values are allowed
  if key == string.format("\x08") then -- backspace key
    return key
  elseif tonumber(key) ~= nil then -- numeric key
    return key
  end
  return nil
end

function chkGroschenChange(sender)
  -- Check Box > OnChange()
  updateInfiniteItems()
end

function FormClose(sender)
  -- Form > OnClose()
  -- Here, we make sure to revert any changes to the game we've made,
  --   disabling any active hacks to reinstate original ASM code.
  if bEnabledInfiniteItems then
    bEnabledInfiniteItems = false
    updateInfiniteItems()
  end
  return caFree --Possible options: caHide, caFree, caMinimize, caNone
end

--[[ MAIN SCRIPT --]]
openProcess("KingdomCome.exe")
frmMain.show()


Still some minor kinks to work out, but if I load the game, hit the toggle box, set a value in the text box, then drop any item in the game... sure enough, it drops that full amount but still keeps X amount in your inventory.
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