📚 [Lib] - LuaLib for Ankulua scripts
May 21, 2017 2:28:06 GMT
chamchicaycuoc, MD. Mamun, and 1 more like this
Post by Mercobots on May 21, 2017 2:28:06 GMT
hello folks
Past some time coding on ankulua i have created a small lib to help me with my scripts.
Most of the functions code come from intenet as lua-users.org/wiki/ , www.lua.org, others are created by myself and are based on php names
My goal on sharing this is help people and be helped, so i open a repository LuaLib - repository and will be nice if everyone contribute with more functions.
Feel free to request functions or report an issue
or load directly from github
Variable handling Functions
gettype(var)
return the var type , adapted to ankulua to return location,region....
result
is_string(var)
Find whether the type of a variable is string , return true or false
result
is_bool(var)
Finds out whether a variable is a boolean, return true or false
is_numeric(var)
Finds whether a variable is a number or a numeric string , return true or false
is_float(var)
Finds whether the type of a variable is float , return true or false
is_table(var)
Finds whether a variable is an array/table , return true or false
is_float(var)
Finds whether the type of a variable is float
print_r(table, name string, indent string)
print hole table and sub-tables values
result
dprint()
dprint_r ()
dprint()
developer prints , set DEVELOPER = true to enable prints
String Functions
trim(string)
Strip whitespace from the beginning and end of a string , return string
trim(string)
Strip whitespace from the beginning of a string , return string
ltrim(string)
Strip whitespace from the end of a string , return string
explode(string,delimiter string)
Split a string by string , return table
default delimiter - space
result
str_replace(search string, replace string, subject string, count int)
Replace all occurrences of the search string with the replacement string
result
Table/array Functions
count(table)
Count all elements in an array/table , return int
in_table(table, value string)
Finds whether a variable is an array, return true or false
result
table_reverse(table)
Return an index table with elements in reverse order
table_key_exists(table,key)
Checks if the given key or index exists in the table
validate_table(file or table,rules)
can load table from files and validate the structure, also you can set what should be vlid or not
rules = {string,boolean,funtion ,table}
on load a table from file, this disable prints
Filesystem Functions
basename(path string)
Returns trailing name component of path
dirname(path string)
Returns a parent directory's path
file_extension(file string)
Retrieves the file extension from file , return string
mkdir(path string)
Makes directory , return true or false
rmdir(path string)
Removes directory , return true or false
copy(path source string, path destination string)
copy a directory or file, return true or false
is_dir(path string)
Tells whether the filename is a directory, return true or false
pathinfo(path string ,option string)
get information about a file path, return table
result
scandir(dir to scan string, temp file string)
List files and directories inside the specified path
default temp file - "/sdcard/__temp/_scandir_"
Android Functions
btn_home()
simulate home botton
btn_back(loops int, wait time int)
simulate back botton
btn_switch(loops int, wait time int)
simulate SWITCH botton
lua essential function
clone_table(table)
Clone all table and sub tables to avoid the __pairs metamethod. return table
Ankulua extends
location_to_string(Location)
convert a Location to string , return string
region_to_string(Region)
convert a Region to string , return string
table_to_string(table, space string)
convert a table and sub tables to string , return string
is_location(var)
Finds out whether a variable is a Location , return true or false
is_region(var)
Finds out whether a variable is a Region, return true or false
is_match(var)
Finds out whether a variable is a Match, return true or false
is_pattern(var)
Finds out whether a variable is a Pattern, return true and the file name or false
debug_r(title, var, time)
Auto highlight any value table, img,region,match or location
this function has a global var DEBUG_R , set to false will turn off all debug_r
if you wanna turn off all the highlight without comment all debug_r lines
img_r(S,time)
highlight an image string given and return the same type (string)
- supports DEBUG_R global var
this function was created to help on scripting, will help you save some lines code and hide all highlight , once your release is ready
Before
Using img_r
get_values(MRLT)
Return all the values of an region,location,match or table
- match obj also return getTarget(),getScore(),getCenter()
result
is_timeout(obj Timer(), int time_out)
returns true or false if the var timeout
obj timer()
get_clock(int seconds)
converts seconds to clock time
returns 2 parameters
1º - string
2º - table
result
preferencePutData(string,RLT)
preferenceGetData(string,RLT)
Extend ankulua preferencePut* to save regions locations and tables
Others functions
odd(int)
check odd , return true or false
random_string(length int)
creates a random string , return string
Alias functions
is_double()
This function is an alias of is_float()
is_array()
This function is an alias of is_table()
clone_array()
This function is an alias of clone_table()
array_to_string()
This function is an alias of table_to_string()
in_array()
This function is an alias of in_table()
array_reverse()
This function is an alias of table_reverse()
validate_array ()
This function is an alias of validate_table()
preferencePutRegion()
preferencePutLocation()
preferencePutTable()
preferencePutArray()
This function is an alias of preferencePutData()
preferenceGetRegion()
preferenceGetLocation ()
preferenceGetTable()
preferenceGetArray()
This function is an alias of preferenceGetData()
Past some time coding on ankulua i have created a small lib to help me with my scripts.
Most of the functions code come from intenet as lua-users.org/wiki/ , www.lua.org, others are created by myself and are based on php names
My goal on sharing this is help people and be helped, so i open a repository LuaLib - repository and will be nice if everyone contribute with more functions.
Feel free to request functions or report an issue
or load directly from github
luaLib = loadstring(httpGet("https://raw.githubusercontent.com/mercobots/luaLib/master/luaLib.lua"))
luaLib()
Variable handling Functions
gettype(var)
return the var type , adapted to ankulua to return location,region....
loc = Location(100,200)
reg = Region(100,100,100,100)
print("loc type -> " .. gettype(loc))
print("reg type -> " .. gettype(reg))
result
loc type -> Location
reg type -> Region
is_string(var)
Find whether the type of a variable is string , return true or false
s = "text"
if is_string(s) then print("s is a string") end
result
s is a string
is_bool(var)
Finds out whether a variable is a boolean, return true or false
is_numeric(var)
Finds whether a variable is a number or a numeric string , return true or false
is_float(var)
Finds whether the type of a variable is float , return true or false
is_table(var)
Finds whether a variable is an array/table , return true or false
is_float(var)
Finds whether the type of a variable is float
print_r(table, name string, indent string)
print hole table and sub-tables values
t = {"1r","2r","3r"}
print_r(t,"My Table","->")
result
-> My Tabe = {
-> [1] = "1r";
-> [2] = "2r";
-> [3] = "3r";
-> }
dprint()
dprint_r ()
dprint()
developer prints , set DEVELOPER = true to enable prints
String Functions
trim(string)
Strip whitespace from the beginning and end of a string , return string
trim(string)
Strip whitespace from the beginning of a string , return string
ltrim(string)
Strip whitespace from the end of a string , return string
explode(string,delimiter string)
Split a string by string , return table
default delimiter - space
list = "banana|orange|car|bus"
list = explode(list,"|")
print_r(list,"list")
result
list = {
[1] = "banana";
[2] = "orange";
[3] = "car";
[4] = "bus";
}
str_replace(search string, replace string, subject string, count int)
Replace all occurrences of the search string with the replacement string
s = "My Bot"
s = str_replace(s,"bot","auto bot")
print(s)
result
My auto bot
Table/array Functions
count(table)
Count all elements in an array/table , return int
in_table(table, value string)
Finds whether a variable is an array, return true or false
items = { "apple", "orange", "pear", "banana" }
print("pear exists? -> " .. in_table(items,"pear")
print("car exists? -> " .. in_table(items,"car")
result
pear exists? -> true
car exists? -> false
table_reverse(table)
Return an index table with elements in reverse order
table_key_exists(table,key)
Checks if the given key or index exists in the table
validate_table(file or table,rules)
can load table from files and validate the structure, also you can set what should be vlid or not
rules = {string,boolean,funtion ,table}
on load a table from file, this disable prints
Filesystem Functions
basename(path string)
Returns trailing name component of path
dirname(path string)
Returns a parent directory's path
file_extension(file string)
Retrieves the file extension from file , return string
mkdir(path string)
Makes directory , return true or false
rmdir(path string)
Removes directory , return true or false
copy(path source string, path destination string)
copy a directory or file, return true or false
is_dir(path string)
Tells whether the filename is a directory, return true or false
pathinfo(path string ,option string)
get information about a file path, return table
path = "root/folder1/folder2/file.lua"
print_r(pathinfo(path))
result
table{
["basename"] = file.lua,
["dirname"] = root/folder1/folder2/,
["extension"] = lua,
["filename"] = file
}
scandir(dir to scan string, temp file string)
List files and directories inside the specified path
default temp file - "/sdcard/__temp/_scandir_"
Android Functions
btn_home()
simulate home botton
btn_back(loops int, wait time int)
simulate back botton
btn_switch(loops int, wait time int)
simulate SWITCH botton
lua essential function
clone_table(table)
Clone all table and sub tables to avoid the __pairs metamethod. return table
Ankulua extends
location_to_string(Location)
convert a Location to string , return string
region_to_string(Region)
convert a Region to string , return string
table_to_string(table, space string)
convert a table and sub tables to string , return string
is_location(var)
Finds out whether a variable is a Location , return true or false
is_region(var)
Finds out whether a variable is a Region, return true or false
is_match(var)
Finds out whether a variable is a Match, return true or false
is_pattern(var)
Finds out whether a variable is a Pattern, return true and the file name or false
debug_r(title, var, time)
Auto highlight any value table, img,region,match or location
this function has a global var DEBUG_R , set to false will turn off all debug_r
-- using a table of values
tab1 = {100,100,100,100} -- as a region
tab2 = {100,100} -- as a location
-- using a Location
loc = Location(100,100)
-- using a Region
reg = Region(100,100,100,100)
-- using a Pattern
p = Pattern("myimg.png")
-- using a String
s = "myimg.png"
-- using a String
exists("myimg.png")
m = getLastMatch()
debug_r("Table 1",tab1)
debug_r("Table 2",tab2)
debug_r("Location",loc)
debug_r("Region",reg)
debug_r("Pattern",p)
bebug_r("String",s)
debug_r("Match",m)
if you wanna turn off all the highlight without comment all debug_r lines
DEBUG_R = false
debug_r("Table 1",tab1)
debug_r("Table 2",tab2)
debug_r("Location",loc)
debug_r("Region",reg)
debug_r("Pattern",p)
debug_r("String",s)
debug_r("Match",m)
img_r(S,time)
highlight an image string given and return the same type (string)
- supports DEBUG_R global var
this function was created to help on scripting, will help you save some lines code and hide all highlight , once your release is ready
Before
if exists("myImg.png") then
getLastMatch():highlight(2)
end
if exists("myImg2.png") then
getLastMatch():highlight(2)
end
-- in this case if you want hide all highlight you will need to comment 1 by 1
Using img_r
if exists(img_r("myImg")) then end
if exists(img_r("myImg2")) then end
-- to hide the highlight's only add the DEBUG_R == FALSE before these functions
get_values(MRLT)
Return all the values of an region,location,match or table
- match obj also return getTarget(),getScore(),getCenter()
reg = Region(100,200,300,400)
print_r(get_values(reg))
result
table{
["x"] = 100,
["y"] = 200,
["w"] = 300,
["h"] = 400
}
is_timeout(obj Timer(), int time_out)
returns true or false if the var timeout
obj timer()
get_clock(int seconds)
converts seconds to clock time
returns 2 parameters
1º - string
2º - table
s,t = get_clock(1000)
print(s)
print_r(t)
result
00:16:40
table{
h = 00,
m = 16,
s = 40,
}
preferencePutData(string,RLT)
preferenceGetData(string,RLT)
Extend ankulua preferencePut* to save regions locations and tables
Others functions
odd(int)
check odd , return true or false
random_string(length int)
creates a random string , return string
Alias functions
is_double()
This function is an alias of is_float()
is_array()
This function is an alias of is_table()
clone_array()
This function is an alias of clone_table()
array_to_string()
This function is an alias of table_to_string()
in_array()
This function is an alias of in_table()
array_reverse()
This function is an alias of table_reverse()
validate_array ()
This function is an alias of validate_table()
preferencePutRegion()
preferencePutLocation()
preferencePutTable()
preferencePutArray()
This function is an alias of preferencePutData()
preferenceGetRegion()
preferenceGetLocation ()
preferenceGetTable()
preferenceGetArray()
This function is an alias of preferenceGetData()