Jump to content

Examples of Lua scripts


Enyby
 Share

Recommended Posts

  • Administrators

Saving input between script restarts

local configFile = gg.getFile()..'.cfg'
local data = loadfile(configFile)
if data ~= nil then data = data() end
local input = gg.prompt({'Please input something'}, data)
if input == nil then os.exit() end
gg.saveVariable(input, configFile)

 

Link to comment
Share on other sites

  • Administrators

Performing an action by clicking on the GG icon

function doAction()
	local ret = gg.alert('Here some action', 'OK', 'Cancel', 'Exit')
	if ret == 3 then os.exit() end -- exit from the script
end

gg.setVisible(false)
while true do
	if gg.isVisible() then
		gg.setVisible(false)
		doAction()
	end
	gg.sleep(100)
end

 

Link to comment
Share on other sites

  • Administrators

Performing multiple actions

with multiChoice

local t = gg.multiChoice({'A', 'B', 'C', 'D'})
if t == nil then
    gg.alert('Canceled')
else
    if t[1] then
        gg.alert('do A')
    end
    if t[2] then
        gg.alert('do B')
    end
    if t[3] then
        gg.alert('do C')
    end
    if t[4] then
        gg.alert('do D')
    end
end

with prompt

local t = gg.prompt({'A', 'B', 'C', 'D'}, nil, {'checkbox', 'checkbox', 'checkbox', 'checkbox'})
if t == nil then
    gg.alert('Canceled')
else
    if t[1] then
        gg.alert('do A')
    end
    if t[2] then
        gg.alert('do B')
    end
    if t[3] then
        gg.alert('do C')
    end
    if t[4] then
        gg.alert('do D')
    end
end

 

Link to comment
Share on other sites

  • 2 months later...
  • Administrators

Search and replace text

Up to 32 bytes.

Replace must be same length.

gg.clearResults()

function searchText(text)
	local bytes = gg.bytes(text, 'UTF-8')
	local ret = ''
	for i, b in ipairs(bytes) do
		ret = ret .. ';' .. b
	end
	ret = ret:sub(2)..'::'..#bytes
	return gg.searchNumber(ret, gg.TYPE_BYTE)
end

searchText('rfde')

function replaceText(text)
	local bytes = gg.bytes(text, 'UTF-8')
	local all = gg.getResults(100000)
	local len = #bytes
	for i, t in ipairs(all) do
		t.value = bytes[((i - 1) % len) + 1]
	end
	return gg.setValues(all)
end

replaceText('gold')

If you want UTF-16, replace 'UTF-8' in code to 'UTF-16LE'.

Link to comment
Share on other sites

  • 8 months later...
  • Administrators

Search and replace text

Up to 4096 bytes.

Good if replace be same length in bytes.

-- UTF-8: search 'rfde', replace to 'gold'
gg.require('80.0', 15060)
gg.clearResults()
gg.searchNumber(':rfde')
gg.getResults(100000)
gg.editAll(':gold', gg.TYPE_BYTE)

-- UTF-16LE: search 'dust', replace to 'gold'
gg.require('80.0', 15060)
gg.clearResults()
gg.searchNumber(';dust')
gg.getResults(100000)
gg.editAll(';gold', gg.TYPE_WORD)

 

Link to comment
Share on other sites

  • 1 month later...
  • Administrators

Avoid use global variables

Global variables is slow.

Also if you put

local gg = gg

At top of your script it can speed up it. Just one line.

Now see tests:

local n = 1000000

local t = os.clock()
for i = 1, n do
	gg.isVisible()
end
t = os.clock() - t
print('use global gg: '..t..' seconds')

local gg = gg
local t = os.clock()
for i = 1, n do
	gg.isVisible()
end
t = os.clock() - t
print('use local gg: '..t..' seconds')

a, b, c = 1, 2, 3
local t = os.clock()
for i = 1, n do
	c = a + b
end
t = os.clock() - t
print('use global vars: '..t..' seconds')

local a, b, c = 1, 2, 3
local t = os.clock()
for i = 1, n do
	c = a + b
end
t = os.clock() - t
print('use local vars: '..t..' seconds')

Results:

use global gg: 2.138 seconds
use local gg: 1.6 seconds
use global vars: 2.068 seconds
use local vars: 0.727 seconds

It is not big difference, because I run it on powerful emulator. On real device it can be more slow.

You can see disassembled code - for global vars need more Lua instructions, so it more slow in any case.

Upvalue too slow, Because of that better define local copy of var in places where you need optimization. For example huge math.

Link to comment
Share on other sites

  • 1 month later...
  • Administrators

Prompt with 'remember' checkbox for store data in the config.

local info = {}
local config = gg.getFile()..'.cfg'
local data = loadfile(config)
if data ~= nil then
	info = data()
	data = nil
end

info = gg.prompt({'Login', 'Password', 'Remember'}, info, {'text', 'text', 'checkbox'})
if info == nil then os.exit() end
if info[3] then
	gg.saveVariable(info, config)
else
	os.remove(config)
end

-- here work with 'info' content
print(info)

 

Link to comment
Share on other sites

  • Administrators

Prompt file with specified extension

local ext = '.txt'
local p = {gg.EXT_STORAGE}
while true do
	p = gg.prompt({'Select "'..ext..'" file:'}, p, {'file'})
	if p == nil then os.exit() end
	if p[1]:sub(-#ext) == ext then break end
	gg.alert('You select "'..p[1]..'".\n\nIt is not end with "'..ext..'".\n\nPlease select file with "'..ext..'" extension.')
end
print(p[1]) -- do something

 

Link to comment
Share on other sites

  • Administrators

Checking the password against the list of allowed passwords

local pass = gg.prompt({"Input password:"}, nil, {"text"})
if pass == nil then os.exit() end
pass = pass[1]..' '
local allowed = false
local function allow_password(password)
	if password..' ' == pass then 
		allowed = true
	end
end

allow_password('myPassword')
allow_password('anotherPassword')
allow_password('This_is_too_Password')
allow_password('12345')
allow_password('sex')
allow_password('god')

if not allowed then
	os.exit(gg.alert('Wrong password'))
else 
	gg.toast("Password correct")
end

 

Link to comment
Share on other sites

  • 2 weeks later...
  • Administrators

Count used memory by target process with group by region type

local t = gg.getRangesList()
local out = {}
for i, v in ipairs(t) do
  local prev = out[v.state]
  if prev == nil then prev = 0 end
  out[v.state] = prev + (v['end'] - v.start)
end
print(out)

 

Link to comment
Share on other sites

  • 3 weeks later...
  • Administrators

Track value changes in the background

Run Tap Counter. Find tap count. It must be first value in search results.

Run script.

Increase or decrease tap for see toast. For end script open GG UI.

local v = gg.getResults(1)

gg.setVisible(false)
while not gg.isVisible() do
	local old = v[1].value
	v = gg.getValues(v)
    if old ~= v[1].value then
		gg.toast('changed: '..old..' -> '..v[1].value)
	end
	gg.sleep(100)
end

Video:

Track value changes in the background - GameGuardian (#6x95p9tk)

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.