Jump to content
Enyby

Examples of Lua scripts

Recommended Posts

The topic for various examples of Lua scripts

Share this post


Link to post
Share on other sites

Frozen values from the search

Search for the number 10.
The first 7 results are frozen with a value of 8.

 

gg.searchNumber('10', gg.TYPE_DWORD)
local t = gg.getResults(7)
for i, v in ipairs(t) do
	t[i].value = '8'
	t[i].freeze = true
end
gg.addListItems(t)

 

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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'.

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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)

 

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...