local b2974 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
local r2974 = function(i, f)
return function()
local c = f()
b2974[i] = function() return c end
return c
end
end
local math = ((function() local x, y = pcall(require, 'math'); return x and y end)()) or _G.math
local function m2974()
local version = b2974[1]('source_version')
local loadcore = b2974[2]('source_shared_engine_loadcore')
local loadgame = b2974[3]('source_shared_engine_loadgame')
local engine_encoder = b2974[4]('source_engine_api_data_encoder')
local engine_game = b2974[5]('source_engine_api_system_app')
local engine_hash = b2974[6]('source_engine_api_data_hash')
local engine_http = b2974[7]('source_engine_api_io_http')
local engine_i18n = b2974[8]('source_engine_api_data_i18n')
local engine_key = b2974[9]('source_engine_api_system_key')
local engine_env = b2974[10]('source_engine_api_system_getenv')
local engine_log = b2974[11]('source_engine_api_debug_log')
local engine_math = b2974[12]('source_engine_api_math_basic')
local engine_math_clib = b2974[13]('source_engine_api_math_clib')
local engine_math_random = b2974[14]('source_engine_api_math_random')
local engine_array = b2974[15]('source_engine_api_data_array')
local engine_media = b2974[16]('source_engine_api_io_media')
local engine_api_draw_fps = b2974[17]('source_engine_api_draw_fps')
local engine_api_draw_text = b2974[18]('source_engine_api_draw_text')
local engine_api_draw_poly = b2974[19]('source_engine_api_draw_poly')
local engine_raw_memory = b2974[20]('source_engine_api_raw_memory')
local callback_http = b2974[21]('source_engine_protocol_http_callback')
local application_default = b2974[22]('source_shared_var_object_root')
local color = b2974[23]('source_engine_api_system_color')
local std = b2974[24]('source_shared_var_object_std')
local application = application_default
local engine = {
keyboard = function(a, b, c, d) end,
current = application_default,
root = application_default
}
local cfg_system = {
exit = native_system_exit,
reset = native_system_reset,
title = native_system_title,
get_fps = native_system_get_fps,
get_secret = native_system_get_secret,
get_language = native_system_get_language
}
local cfg_media = {
bootstrap=native_media_bootstrap,
position=native_media_position,
resize=native_media_resize,
resume=native_media_resume,
source=native_media_source,
pause=native_media_pause,
play=native_media_play,
stop=native_media_stop
}
local cfg_poly = {
repeats = {
native_cfg_poly_repeat_0 or false,
native_cfg_poly_repeat_1 or false,
native_cfg_poly_repeat_2 or false
},
triangle = native_draw_triangle,
poly2 = native_draw_poly2,
poly = native_draw_poly,
line = native_draw_line
}
local cfg_text = {
is_tui = native_text_is_tui,
font_previous = native_text_font_previous
}
local cfg_http = {
install = native_http_install,
handler = native_http_handler,
has_ssl = native_http_has_ssl,
has_callback = native_http_has_callback,
force = native_http_force_protocol
}
local cfg_log = {
fatal = native_log_fatal,
error = native_log_error,
warn = native_log_warn,
info = native_log_info,
debug = native_log_debug
}
local cfg_base64 = {
decode = native_base64_decode,
encode = native_base64_encode
}
local cfg_json = {
decode = native_json_decode,
encode = native_json_encode
}
local cfg_xml = {
decode = native_xml_decode,
encode = native_xml_encode
}
local cfg_env = {
get_env = native_system_get_env
}
function native_callback_loop(dt)
std.milis = std.milis + dt
std.delta = dt
application.callbacks.loop(application.data, std)
end
function native_callback_draw()
native_draw_start()
application.callbacks.draw(application.data, std)
native_draw_flush()
end
function native_callback_resize(width, height)
application.data.width = width
application.data.height = height
std.app.width = width
std.app.height = height
end
function native_callback_keyboard(key, value)
engine.keyboard(std, engine, key, value)
end
function native_callback_http(id, key, data)
return callback_http.func(engine['http'][id], key, data, std)
end
function native_callback_init(width, height, game_lua)
application = loadgame.script(game_lua, application_default)
if application then
application.data.width = width
application.data.height = height
std.app.width = width
std.app.height = height
end
std.bus = {
emit=function() end,
emit_next=function() end,
listen=function() end,
listen_std_engine=function() end
}
std.draw.color=native_draw_color
std.draw.font=native_draw_font
std.draw.rect2=native_draw_rect2 or native_draw_rect
std.draw.rect=native_draw_rect
std.draw.line=native_draw_line
std.image.load=native_image_load
std.image.draw=native_image_draw
std.image.mensure=native_image_mensure
std.text.print=native_text_print
std.text.mensure=native_text_mensure
std.text.font_size=native_text_font_size
std.text.font_name=native_text_font_name
std.text.font_default=native_text_font_default
std.draw.clear=function(tint)
native_draw_clear(tint, 0, 0, application.data.width, application.data.height)
end
engine.root = application
loadcore.setup(std, application, engine)
:package('@memory', engine_raw_memory)
:package('@game', engine_game, cfg_system)
:package('@math', engine_math)
:package('@array', engine_array)
:package('@key', engine_key, {})
:package('@draw.fps', engine_api_draw_fps)
:package('@draw.text', engine_api_draw_text, cfg_text)
:package('@draw.poly', engine_api_draw_poly, cfg_poly)
:package('@color', color)
:package('@log', engine_log, cfg_log)
:package('@env', engine_env, cfg_env)
:package('math', engine_math_clib)
:package('math.random', engine_math_random)
:package('http', engine_http, cfg_http)
:package('base64', engine_encoder, cfg_base64)
:package('json', engine_encoder, cfg_json)
:package('xml', engine_encoder, cfg_xml)
:package('i18n', engine_i18n, cfg_system)
:package('hash', engine_hash, cfg_system)
:package('media.video', engine_media, cfg_media)
:package('mock.video', engine_media)
:package('mock.audio', engine_media)
:run()
application.data.width, std.app.width = width, width
application.data.height, std.app.height = height, height
std.app.title(application.meta.title..' - '..application.meta.version)
engine.current = application
application.callbacks.init(application.data, std)
end
local P = {
meta={
title='gly-engine-lite',
author='RodrigoDornelles',
description='native lite',
version=version
}
}
return P
end
b2974[1] = r2974(1, function()
return '0.3.13'
end)
b2974[2] = r2974(2, function()
local zeebo_pipeline = b2974[25]('source_shared_functional_pipeline')
local requires = b2974[26]('source_shared_string_dsl_requires')
local function step_install_libsys(self, lib_name, library, custom, is_system)
if not is_system then return end
local ok, msg = pcall(function()
library.install(self.std, self.engine, custom, lib_name)
end)
if ok then
self.libsys[lib_name] = true
else
self.error('sys', lib_name, msg)
end
end
local function step_check_libsys(self, lib_name, library, custom, is_system)
if not is_system then return end
if not self.libsys[lib_name] then
self.error('sys', lib_name, 'is missing!')
end
end
local function step_install_libusr(self, lib_name, library, custom, is_system)
if is_system then return end
if self.libusr[lib_name] then return end
if not requires.should_import(self.spec, lib_name) then return end
self.libusr[lib_name] = pcall(function()
library.install(self.std, self.engine, custom, lib_name)
end)
end
local function step_check_libsys_all(self)
local missing = requires.missing(self.spec, self.libusr)
if #missing > 0 then
self.error('usr', '*', 'missing libs: '..table.concat(missing, ' '))
end
end
local function package(self, lib_name, library, custom)
self.pipeline[#self.pipeline + 1] = function()
local is_system = lib_name:sub(1, 1) == '@'
local name = is_system and lib_name:sub(2) or lib_name
self:step(name, library, custom, is_system)
end
return self
end
local function setup(std, application, engine)
if not application then
error('game not found!')
end
local spec = requires.encode((application.config or application).require or '')
local self = {
std = std,
spec = spec,
errmsg = '',
engine = engine,
package = package,
libusr = {},
libsys = {},
pipeline = {},
pipe = zeebo_pipeline.pipe
}
self.error = function (prefix, lib_name, message) 
self.errmsg = self.errmsg..'['..prefix..':'..lib_name..'] '..message..'\n'
end
self.run = function()
self.step = step_install_libsys
zeebo_pipeline.reset(self)
zeebo_pipeline.run(self)
self.step = step_check_libsys
zeebo_pipeline.reset(self)
zeebo_pipeline.run(self)
self.step = step_install_libusr
zeebo_pipeline.reset(self)
zeebo_pipeline.run(self)
step_check_libsys_all(self)
if #self.errmsg > 0 then
error(self.errmsg, 0)
end
end
return self
end
local P = {
setup = setup
}
return P
end)
b2974[3] = r2974(3, function()
local eval_file = b2974[27]('source_shared_string_eval_file')
local eval_code = b2974[28]('source_shared_string_eval_code')
local has_io_open = io and io.open
local function normalize(app, base)
if not app then return nil end
if not app.callbacks then
local old_app = app
app = {meta={},config={},callbacks={}, data={}, envs={}}
for key, value in pairs(old_app) do
local is_function = type(value) == 'function'
if base.meta and base.meta[key] and not is_function then
app.meta[key] = value
elseif base.config and base.config[key] and not is_function then
app.config[key] = value
elseif is_function then
app.callbacks[key] = value
elseif app[key] then
app[key] = value
else
app.data[key] = value
end
end
end
local function defaults(a, b, key)
if type(a[key]) ~= "table" then a[key] = {} end
for k, v in pairs(b[key]) do
if a[key][k] == nil then
a[key][k] = b[key][k]
end
end
end
for field in pairs(base) do
defaults(app, base, field)
end
return app
end
local function script(src, base)
if not src and package and package.jspath then
src = {}
end
if type(src) == 'table' or type(src) == 'userdata' then
return normalize(src, base)
end
local application = type(src) == 'function' and src
if not application then
if type(src) ~= 'string' or #src == 0 then
src = 'game'
end
if src:find('\n') then
local ok, app = eval_code.script(src)
application = ok and app
else
local ok, app = eval_file.script(src)
application = ok and app
end
if not application and has_io_open then
local app_file = io.open(src)
if app_file then
local app_src = app_file:read('*a')
local ok, app = eval_code.script(app_src)
application = ok and app
app_file:close()
end
end
end     
while type(application) == 'function' do
application = application()
end
return normalize(application, base)
end
local P = {
script = script
}
return P
end)
b2974[4] = r2974(4, function()
local function install(std, engine, library, name)
std = std or {}
std[name] = {
encode=library.encode,
decode=library.decode
}
return {[name]=std[name]}
end
local P = {
install=install
}
return P
end)
b2974[5] = r2974(5, function()
local function reset(std, engine)
if std.node then
return function()
std.bus.emit('exit')
std.bus.emit('init')
end
end
return function()
engine.root.callbacks.exit(engine.root.data, std)
engine.root.callbacks.init(engine.root.data, std)
end
end
local function exit(std)
return function()
std.bus.emit('exit')
std.bus.emit('quit')
end
end
local function title(func)
return function(window_name)
if func then
func(window_name)
end
end
end
local function get_info(my, info)
return function()
return my.root.meta[info]
end
end
local function install(std, engine, config)
std = std or {}
config = config or {}
std.app = std.app or {}
std.bus.listen('post_quit', config.quit or config.exit or function() end)
std.app.title = title(config.set_title)
std.app.exit = exit(std)
std.app.reset = reset(std, engine)
std.app.get_name = get_info(engine, 'title')
std.app.get_version = get_info(engine, 'version')
std.app.get_fps = config.get_fps
return std.app
end
local P = {
install=install
}
return P
end)
b2974[6] = r2974(6, function()
local function djb2(digest)
local index = 1
local hash = 5381
while index <= #digest do
local char = string.byte(digest, index)
hash = (hash * 33 + char) % 4294967296
index = index + 1
end
return hash
end
local function install(std, engine, cfg_system)
local id = djb2(cfg_system.get_secret())
std = std or {}
std.hash = std.hash or {}
std.hash.djb2 = djb2
std.hash.fingerprint = function() return id end
end
local P = {
install = install
}
return P
end)
b2974[7] = r2974(7, function()
local zeebo_pipeline = b2974[25]('source_shared_functional_pipeline')
local function json(self)
self.options['json'] = true
return self
end
local function noforce(self)
self.options['noforce'] = true
return self
end
local function fast(self)
self.speed = '_fast'
return self
end
local function param(self, name, value)
local index = #self.param_list + 1
self.param_list[index] = tostring(name)
self.param_dict[name] = tostring(value)
return self
end
local function header(self, name, value)
local index = #self.header_list + 1
self.header_list[index] = tostring(name)
self.header_dict[name] = tostring(value)
return self
end
local function body(self, content, json_encode)
if type(content) == 'table' then
header(self, 'Content-Type', 'application/json')
content = json_encode(content)
end
self.body_content=content
return self
end
local function success(self, handler_func)
self.success_handler = handler_func
return self
end
local function failed(self, handler_func)
self.failed_handler = handler_func
return self
end
local function http_error(self, handler_func)
self.error_handler = handler_func
return self
end
local function request(method, std, engine, protocol)
local callback_handler = function()
if std.node then
std.node.emit(engine.current, 'http')
elseif engine.current.callbacks.http then
engine.current.callbacks.http(std, engine.current.data)
end
end
return function (url)
local json_encode = std.json and std.json.encode
local json_decode = std.json and std.json.decode
local http_body = function(self, content) return body(self, content, json_encode) end
local game = engine.current.data
local self = {
url = url,
speed = '',
options = {},
method = method,
body_content = '',
header_list = {},
header_dict = {},
param_list = {},
param_dict = {},
success_handler = function (std, game) end,
failed_handler = function (std, game) end,
error_handler = function (std, game) end,
fast = fast,
json = json,
noforce = noforce,
body = http_body,
param = param,
header = header,
success = success,
failed = failed,
error = http_error,
run = zeebo_pipeline.run,
}
self.promise = function()
zeebo_pipeline.stop(self)
end
self.resolve = function()
zeebo_pipeline.resume(self)
end
self.set = function (key, value)
std.http[key] = value
end
self.pipeline = {
function()
self.id = tonumber(tostring({}):gsub('0x', ''):match('^table: (%w+)$'), 16)
engine.http[self.id] = self
if protocol.force and not self.options['noforce'] then
self.url = url:gsub("^[^:]+://", protocol.force.."://")
end
end,
function()
protocol.handler(self, self.id)
end,
function()
if self.options['json'] and json_decode and std.http.body then
local ok, err = pcall(function()
local new_body = json_decode(std.http.body)
std.http.body = new_body
end)
if not ok then
self.set('ok', false)
self.set('error', err)
end
end
local lower_header = {}
for k, v in pairs(std.http.headers or {}) do
lower_header[string.lower(k)] = v
end
std.http.headers = lower_header
end,
function()
callback_handler(std, game)
if std.http.ok then
self.success_handler(std, game)
elseif std.http.error then
self.error_handler(std, game)
elseif not std.http.status then
self.set('error', 'missing protocol response')
self.error_handler(std, game)
else
self.failed_handler(std, game)
end
end,
function ()
std.http.ok = nil
std.http.body = nil
std.http.error = nil
std.http.status = nil
std.http.body_is_table = nil
end,
function()
engine.http[self.id] = nil
zeebo_pipeline.reset(self)
end
}
return self
end
end
local function install(std, engine, protocol)
assert(protocol and protocol.handler, 'missing protocol handler')
engine.http = {}
std.http = std.http or {}
std.http.get=request('GET', std, engine, protocol)
std.http.head=request('HEAD', std, engine, protocol)
std.http.post=request('POST', std, engine, protocol)
std.http.put=request('PUT', std, engine, protocol)
std.http.delete=request('DELETE', std, engine, protocol)
std.http.patch=request('PATCH', std, engine, protocol)
if protocol.install then
protocol.install(std, engine)
end
end
local P = {
install=install
}
return P
end)
b2974[8] = r2974(8, function()
local language = 'en-US'
local language_default = 'en-US'
local language_list = {}
local language_inverse_list = {}
local translate = {}
local function update_languages(texts)
local index = 1
translate = texts
language_list = {language_default}
language_inverse_list = {[language_default]=1}
repeat
local lang = next(texts)
if lang then
index = index + 1
language_inverse_list[lang] = index
language_list[#language_list + 1] = lang
end
until lang
end
local function get_text(old_text)
local new_text = translate[language] and translate[language][old_text]
return new_text or old_text
end
local function get_language()
return language
end
local function set_language(l)
if language_inverse_list[l] then
language = l
else 
language = language_default
end
end
local function next_language(to)
local index = language_inverse_list[language]
local incr = to or 1
if index then
index = index + incr
if index > #language_list then
index = 1
end
if index <= 0 then
index = #language_list
end
index = index == 0 and 1 or index
set_language(language_list[index])
end
end
local function back_language()
next_language(-1)
end
local function decorator_draw_text(func)
return function (x, y, text, a, b, c)
return func(x, y, get_text(text), a, b, c)
end
end
local function install(std, engine, cfg)
if not (std and std.text and std.text.print) then
error('missing draw text')
end
local old_put = std.text.put
local old_print = std.text.print
local old_print_ex = std.text.print_ex
local callback_lang = function(result)
update_languages(result)
if cfg and cfg.get_language then
set_language(cfg.get_language())
end
end
if not std.node and engine.root.callbacks.i18n then
callback_lang(engine.root.callbacks.i18n())
else
std.bus.listen('ret_i18n', callback_lang)
std.bus.emit_next('i18n')
end
std.text.put = decorator_draw_text(old_put)
std.text.print = decorator_draw_text(old_print)
std.text.print_ex = decorator_draw_text(old_print_ex)
std.i18n = {}
std.i18n.get_text = get_text
std.i18n.get_language = get_language
std.i18n.set_language = set_language
std.i18n.back = back_language
std.i18n.next = next_language
end
local P = {
install=install
}
return P
end)
b2974[9] = r2974(9, function()
local function real_key(std, engine, rkey, rvalue)
local value = (rvalue == 1 or rvalue == true) or false
local key = engine.key_bindings[rkey] or (std.key.axis[rkey] and rkey)
local key_media = std.key.media and std.key.media[rkey] ~= nil and rkey
if key_media then
std.key.media[key_media] = value
std.bus.emit('key_media')
end
if key then
std.key.axis[key] = value and 1 or 0
std.key.press[key] = value
if key == 'right' or key == 'left' then
std.key.axis.x = std.key.axis.right - std.key.axis.left
end
if key == 'down' or key == 'up' then
std.key.axis.y = std.key.axis.down - std.key.axis.up
end
std.bus.emit('key')
end
local a = std.key.axis
std.key.press.any = (a.left + a.right + a.down + a.up + a.a + a.b + a.c + a.d + a.menu) > 0
end
local function real_keydown(std, engine, key)
real_key(std, engine, key, 1)
end
local function real_keyup(std, engine, key)
real_key(std, engine, key, 0)
end
local function install(std, engine, config)
config = config or {}
engine.key_bindings = config.bindings or {}
engine.keyboard = real_key
if config.has_media then
std.key.media = {
ch_up = false,
ch_down = false,
vol_up = false,
vol_down = false
}
end
std.bus.listen_std_engine('rkey', real_key)
std.bus.listen_std_engine('rkey1', real_keydown)
std.bus.listen_std_engine('rkey0', real_keyup)
end
return {
install = install
}
end)
b2974[10] = r2974(10, function()
local function setenv(engine)
return function(varname, value)
if engine.root ~= engine.current then
error('unauthorized set environment', 0)
end
if varname then
engine.overrides_envs[varname] = tostring(value)
end
end
end
local function getenv(engine, get_env)
return function(varname) 
local game_envs = engine.root and engine.root.envs
local core_envs = engine.envs
if not (varname or #varname > 0) then
return nil
end
if engine.overrides_envs[varname] then
return engine.overrides_envs[varname]
end
if game_envs and game_envs[varname] then
return game_envs[varname]
end
if core_envs and core_envs[varname] then
return core_envs[varname]
end    
if get_env then
return get_env(varname)
end
return nil
end
end
local function install(std, engine, cfg)
engine.overrides_envs = {}
std.getenv = getenv(engine, cfg.get_env)
std.setenv = setenv(engine)
end
local P = {
install = install
}
return P
end)
b2974[11] = r2974(11, function()
local yaml = b2974[29]('source_shared_string_encode_yaml')
local levels = { none = 0, fatal = 1, error = 2, warn = 3, info = 4, debug = 5, trace = 6}
local function printer(engine, printers, func_a, func_b)
local fn_a = func_a and printers[func_a]
local fn_b = func_b and printers[func_b]
local func = fn_a or fn_b or function() end
local level = levels[func_a] or 7
return function (...)
local msgs = {...}
local count = #msgs
if level <= engine.loglevel then
local content = ''
for i = 1, count do
local v = msgs[i]
local t = type(v)
if t == 'table' then
content = content..'\n'..yaml.encode(v)..'\n'
elseif t ~= 'function' then
local add_space = i ~= count and i > 1 and content:sub(-1) ~= '\n'
local prefix = add_space and ' ' or ''
content = content..prefix..tostring(v)
end
end
func(content)
end
end
end
local function level(engine)
return function(n)
local lv = levels[n] or (type(n) == 'number' and n) or -1
if lv < 0 or 6 < lv then
error('logging level not exist: '..tostring(level)) 
end
engine.loglevel = lv
end
end
local function init(std, engine)
return function(printers)
std.log.fatal = printer(engine, printers, 'fatal', 'error')
std.log.error = printer(engine, printers, 'error')
std.log.warn = printer(engine, printers, 'warn')
std.log.info = printer(engine, printers, 'info')
std.log.debug = printer(engine, printers, 'debug', 'info')
std.log.trace = printer(engine, printers, 'trace', 'info')
end    
end
local function install(std, engine, printers)
std.log = std.log or {}
std.log.init = init(std, engine)
std.log.level = level(engine)
std.log.init(printers)
std.log.level('debug')
end
local P = {
install = install
}
return P
end)
b2974[12] = r2974(12, function()
local function abs(value)
if value < 0 then
return -value
end
return value
end
local function clamp(value, value_min, value_max)
if value < value_min then
return value_min
elseif value > value_max then
return value_max
else
return value
end
end
local function clamp2(value, value_min, value_max)
return (value - value_min) % (value_max - value_min + 1) + value_min
end
local function dir(value, alpha)
alpha = alpha or 0
if value < -alpha then
return -1
elseif value > alpha then
return 1
else
return 0
end
end
local function dis(x1,y1,x2,y2)
local sqr = 1/2
return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ (sqr ~= 0 and sqr or 1)
end
local function dis2(x1,y1,x2,y2)
return (x2 - x1) ^ 2 + (y2 - y1) ^ 2
end
local function dis3(x1,y1,x2,y2)
return abs(x1 - x2) + abs(x2 - y2)
end
local function lerp(a, b, alpha)
return a + alpha * ( b - a )
end 
local function map(value, in_min, in_max, out_min, out_max)
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
end
local function max(...)
local args = {...}
local index = 1
local value = nil
local max_value = nil
if #args == 1 then
args = args[1]
end
while index <= #args do
value = args[index]
if max_value == nil or value > max_value then
max_value = value
end
index = index + 1
end
return max_value
end
local function min(...)
local args = {...}
local index = 1
local value = nil
local min_value = nil
if #args == 1 then
args = args[1]
end
while index <= #args do
value = args[index]
if min_value == nil or value < min_value then
min_value = value
end
index = index + 1
end
return min_value
end
local function install(std)
std.math = std.math or {}
std.math.abs=abs
std.math.clamp=clamp
std.math.clamp2=clamp2
std.math.dir=dir
std.math.dis=dis
std.math.dis2=dis2
std.math.dis3=dis3
std.math.lerp=lerp
std.math.map=map
std.math.max=max
std.math.min=min
end
local P = {
install = install
}
return P
end)
b2974[13] = r2974(13, function()
local function install(std)
assert(math and (1/2 ~= 0))
std.math = std.math or {}
std.math.acos=math.acos
std.math.asin=math.asin
std.math.atan=math.atan
std.math.atan2=math.atan2
std.math.ceil=math.ceil
std.math.cos=math.cos
std.math.cosh=math.cosh
std.math.deg=math.deg
std.math.exp=math.exp
std.math.floor=math.floor
std.math.fmod=math.fmod
std.math.frexp=math.frexp
std.math.huge=math.huge
std.math.ldexp=math.ldexp
std.math.log=math.log
std.math.log10=math.log10
std.math.modf=math.modf
std.math.pi=math.pi
std.math.pow=math.pow
std.math.rad=math.rad
std.math.sin=math.sin
std.math.sinh=math.sinh
std.math.sqrt=math.sqrt
std.math.tan=math.tan
std.math.tanh=math.tanh
end
local P = {
install = install
}
return P
end)
b2974[14] = r2974(14, function()
local function install(std)
assert(math and (1/2 ~= 0))
std.math = std.math or {}
std.math.random = function(a, b)
a = a and math.floor(a)
b = b and math.floor(b)
if a > b then a, b = b, a end
return math.random(a, b)
end
end
local P = {
install = install
}
return P
end)
b2974[15] = r2974(15, function()
local util_decorator = b2974[31]('source_shared_functional_decorator')
local function array_map(array, func)
local res = {}
local index = 1
local length = #array
while index <= length do
res[#res + 1] = func(array[index], index)
index = index + 1
end
return res
end
local function array_filter(array, func)
func = func or (function(v) return v and v ~= 0 end)
local res = {}
local index = 1
local length = #array
while index <= length do
local value = array[index]
if func(value, index) then
res[#res + 1] = value
end
index = index + 1
end
return res
end
local function array_unique(array)
local res = {}
local index = 1
local length = #array
local setmap = {}
while index <= length do
local value = array[index]
if not setmap[value] then
res[#res + 1] = value
end
setmap[value] = true
index = index + 1
end
return res
end
local function array_foreach(array, func)
local index = 1
local length = #array
while index <= length do
func(array[index], index)
index = index + 1
end
end
local function array_reducer(array, func, value)
local index = value and 1 or 2
local length = #array
value = value or array[1]
while index <= length do
value = func(value, array[index], index)
index = index + 1
end
return value
end
local function array_index(array, func, reverse)
func = func or function() return true end
local index, inc, final = 1, 1, #array
if reverse then
index, inc, final = #array, -1, 1
end
repeat
if func(array[index], index) then
return index
end
index = index + inc
until (reverse and index < final) or (not reverse and index > final)
return nil
end
local function array_first(array, func)
local index = array_index(array, func)
if index then
return array[index]
end
return nil
end
local function array_last(array, func)
local index = array_index(array, func, true)
if index then
return array[index]
end
return nil
end
local function array_some(array, func, reverse)
local index, inc, final = 1, 1, #array
if reverse then
index, inc, final = #array, -1, 1
end
repeat
if func(array[index], index) then
return true
end
index = index + inc
until (reverse and index < final) or (not reverse and index > final)
return false
end
local function array_every(array, func)
local index = 1
local length = #array
while index <= length do
if not func(array[index], index) then
return false
end
index = index + 1
end
return true
end
local function array_pipeline(std, array)
local decorator_iterator = function(func) 
return function(self, func2, extra)
self.array = func(self.array, func2, extra)
return self
end
end
local decorator_reduce = function(func, return_self)
return function(self, func2, extra)
local res = func(self.array, func2, extra)
return (return_self and self) or res
end
end
local self = {
array = array,
map = decorator_iterator(array_map),
filter = decorator_iterator(array_filter),
unique = decorator_iterator(array_unique),
each = decorator_reduce(array_foreach, true),
reducer = decorator_reduce(array_reducer),
index = decorator_reduce(array_index),
first = decorator_reduce(array_first),
last = decorator_reduce(array_last),
some = decorator_reduce(array_some),
every = decorator_reduce(array_every),
table = function(self) return self.array end,
json = function(self) return std.json.encode(self.array) end
}
return self
end
local function install(std, engine, library, name)
local lib = std[name] or {}
lib.map = array_map
lib.filter = array_filter
lib.unique = array_unique
lib.each = array_foreach
lib.reducer = array_reducer
lib.index = array_index
lib.first = array_first
lib.last = array_last
lib.some = array_some
lib.every = array_every
lib.from = util_decorator.prefix1(std, array_pipeline)
std[name] = lib
end
local P = {
install = install
}
return P
end)
b2974[16] = r2974(16, function()
local function media_create(node, channels, handler)
local decorator = function(func)
func = func or function() end
return function(self, a, b, c, d, e, f)
func(0, a, b, c, d, e, f)
return self
end
end
local self = { 
src = decorator(handler.source),
play = decorator(handler.play),
prepare = decorator(handler.prepare),
pause = decorator(handler.pause),
resume = decorator(handler.resume),
stop = decorator(handler.stop),
position = decorator(handler.position),
in_mutex = handler.mutex or function() return false end,
get_error = handler.error or function() return nil end,
node = node,
apply = function() end
}
return function()
return self
end
end
local function install(std, engine, handler, name)
std.media = std.media or {}
local mediatype = name:match('%w+%.(%w+)')
if handler.install then
handler.install(std, engine, mediatype, name)
end
if not std.media[mediatype] then
local channels = handler.bootstrap and handler.bootstrap(mediatype)
if (not channels or channels == 0) and handler.bootstrap then
error('media '..mediatype..' is not supported!')
end
local node = std.node and std.node.load({})
std.media[mediatype] = media_create(node, channels, handler)
end
end
local P = {
install=install
}
return P
end)
b2974[17] = r2974(17, function()
local function draw_fps(std, engine, show, pos_x, pos_y)
if show < 1 then return end
local x = engine.current.config.offset_x + pos_x
local y = engine.current.config.offset_y + pos_y
local s = 4
std.draw.color(0xFFFF00FF)
if show >= 1 then
std.draw.rect(0, x, y, 40, 24)
end
if show >= 2 then
std.draw.rect(0, x + 48, y, 40, 24)
end
if show >= 3 then
std.draw.rect(0, x + 96, y, 40, 24)
end
std.draw.color(0x000000FF)
std.text.font_size(16)
if show >= 3 then
local floor = std.math.floor or math.floor or function() return 'XX' end
local fps =  floor((1/std.delta) * 1000)
std.text.print(x + s, y, fps)
s = s + 46
end
if show >= 1 then
std.text.print(x + s, y, engine.fps)
s = s + 46
end
if show >= 2 then
std.text.print(x + s, y, engine.root.config.fps_max)
s = s + 46
end
end
local function install(std, engine)
std.app = std.app or {}
std.app.fps_show = function(show)
engine.root.config.fps_show = show
end
std.bus.listen('post_draw', function()
engine.current = engine.root
draw_fps(std, engine, engine.root.config.fps_show, 8, 8)
end)
end
local P = {
install=install
}
return P
end)
b2974[18] = r2974(18, function()
local function text_put(std, engine, font_previous)
return function(pos_x, pos_y, text, size)
size = size or 2
local hem = engine.current.data.width / 80
local vem = engine.current.data.height / 24
local font_size = hem * size
std.text.font_default(0)
std.text.font_size(font_size)
std.text.print(pos_x * hem, pos_y * vem, text)
font_previous()
end
end
local function text_print_ex(std, engine)
return function(x, y, text, align_x, align_y)
local w, h = std.text.mensure(text)
local aligns_x, aligns_y = {w, w/2, 0}, {h, h/2, 0}
std.text.print(x - aligns_x[(align_x or 1) + 2], y - aligns_y[(align_y or 1) + 2], text)
return w, h
end
end
local function install(std, engine, config)
std.text.font_previous = config.font_previous
std.text.is_tui = config.is_tui or function() return false end
std.text.print_ex = text_print_ex(std, engine)
std.text.put = text_put(std, engine, config.font_previous)
end
local P = {
install=install
}
return P
end)
b2974[19] = r2974(19, function()
local function decorator_poo(object, func)
if not object or not func then return func end
return function(a, b, c, d)
return func(object, a, b, c, d)
end
end
local function decorator_line(func_draw_line)
return function(mode, verts)
local index = 4
while index <= #verts do
func_draw_line(verts[index - 3], verts[index - 2], verts[index - 1], verts[index])
index = index + 2
end
end
end
local function decorator_triangle(func_draw_poly, std, func_draw_triangle)
if not func_draw_triangle then
return func_draw_poly
end
local point = function(x, y, px, py, scale, angle, ox, oy)
local xx = x + ((ox - px) * -scale * std.math.cos(angle)) - ((ox - py) * -scale * std.math.sin(angle))
local yy = y + ((oy - px) * -scale * std.math.sin(angle)) + ((oy - py) * -scale * std.math.cos(angle))
return xx, yy
end
return function(engine_mode, verts, x, y, scale, angle, ox, oy)
if #verts ~= 6 then
return func_draw_poly(engine_mode, verts, x, y, scale, angle, ox, oy)
end
ox = ox or 0
oy = oy or ox or 0
local x1, y1 = point(x, y, verts[1], verts[2], scale, angle, ox, oy)
local x2, y2 = point(x, y, verts[3], verts[4], scale, angle, ox, oy)
local x3, y3 = point(x, y, verts[5], verts[6], scale, angle, ox, oy)
return func_draw_triangle(engine_mode, x1, y1, x2, y2, x3, y3)
end
end
local function decorator_poly(func_draw_poly, std, modes, repeats)
local func_repeat = function(verts, mode)
if repeats and repeats[mode + 1] then
verts[#verts + 1] = verts[1]
verts[#verts + 1] = verts[2]
end
end
return function (engine_mode, verts, x, y, scale, angle, ox, oy)
if #verts < 6 or #verts % 2 ~= 0 then return end
local mode = modes and modes[engine_mode + 1] or engine_mode
local rotated = std.math.cos and angle and angle ~= 0
ox = ox or 0
oy = oy or ox or 0
if x and y and not rotated then
local index = 1
local verts2 = {}
scale = scale or 1
while index <= #verts do
if index % 2 ~= 0 then
verts2[index] = x + (verts[index] * scale)
else
verts2[index] = y + (verts[index] * scale)
end
index = index + 1
end
func_repeat(verts2, engine_mode)
func_draw_poly(mode, verts2)
elseif x and y then
local index = 1
local verts2 = {}
while index < #verts do
local px = verts[index]
local py = verts[index + 1]
local xx = x + ((ox - px) * -scale * std.math.cos(angle)) - ((ox - py) * -scale * std.math.sin(angle))
local yy = y + ((oy - px) * -scale * std.math.sin(angle)) + ((oy - py) * -scale * std.math.cos(angle))
verts2[index] = xx
verts2[index + 1] = yy
index = index + 2
end
func_repeat(verts2, engine_mode)
func_draw_poly(mode, verts2)
else
func_draw_poly(mode, verts)
end
end
end
local function decorator_position(engine, func)
return function(mode, verts, pos_x, pos_y, scale, angle, ox, oy)
local x = engine.current.config.offset_x + (pos_x or 0)
local y = engine.current.config.offset_y + (pos_y or 0)
ox = ox or 0
oy = ox or oy or 0
scale = scale or 1
angle = angle or 0
return func(mode, verts, x, y, scale, angle, ox, oy)
end
end
local function install(std, engine, config)
local draw_line = decorator_poo(config.object, config.line)
local draw_poly = decorator_poo(config.object, config.poly) or decorator_line(draw_line)
local draw_poly2 = config.poly2 or decorator_poly(draw_poly, std, config.modes, config.repeats)
local draw_verts = decorator_triangle(draw_poly2, std, config.triangle)
std.draw.poly = decorator_position(engine, draw_verts)
end
local P = {
install=install
}
return P
end)
b2974[20] = r2974(20, function()
local memory_dict_unload = {}
local memory_dict = {}
local memory_list = {}
local function cache_get(key)
return memory_dict[key]
end
local function cache_set(key, load_func, unload_func)
local value = load_func()
memory_list[#memory_list + 1] = key
memory_dict_unload[key] = unload_func
memory_dict[key] = value
end
local function cache(key, load_func, unload_func)
local value = cache_get(key)
if value == nil then
cache_set(key, load_func, unload_func)
value = cache_get(key)
end    
return value
end
local function unset(key)
if memory_dict_unload[key] then
memory_dict_unload[key](memory_dict[key])
end
memory_dict[key] = nil
end
local function gc_clear_all()
local index = 1
local items = #memory_list
while index <= items do
unset(memory_list[index])
index = index + 1
end
memory_list = {}
return items
end
local function install(std)
std = std or {}
std.mem = std.mem or {}
std.mem.cache = cache
std.mem.cache_get = cache_get
std.mem.cache_set = cache_set
std.mem.unset = unset
std.mem.gc_clear_all = gc_clear_all
return {
mem=std.mem
}
end
local P = {
install=install
}
return P
end)
b2974[21] = r2974(21, function()
local str_http = b2974[32]('source_shared_string_encode_http')
local str_url = b2974[33]('source_shared_string_encode_url')
local callbacks = {
['async-promise'] = function(self)
return self:promise()
end,
['async-resolve'] = function(self)
return self:resolve()
end,
['get-url'] = function(self)
return self.url
end,
['get-fullurl'] = function(self)
return self.url..str_url.search_param(self.param_list, self.param_dict)
end,
['get-method'] = function(self)
return self.method
end,
['get-body'] = function(self)
return self.body_content
end,
['get-param-count'] = function(self)
return #self.param_list
end,
['get-param-name'] = function(self, data)
return self.param_list[self.data]
end,
['get-param-data'] = function(self, data)
return self.param_dict[self.data] or self.param_dict[self.param_list[self.data]]
end,
['get-header-count'] = function(self)
return #self.header_list
end,
['get-header-name'] = function(self, data)
return self.header_list[self.data]
end,
['get-header-data'] = function(self, data)
return self.heeader_dict[self.data] or self.heeader_dict[self.header_list[self.data]]
end,
['set-status'] = function(self, data)
self.set('status', data)
self.set('ok', str_http.is_ok(data))
end,
['set-error'] = function(self, data)
self.set('error', data)
end,
['set-ok'] = function(self, data)
self.set('ok', data)
end,
['set-body'] = function(self, data)
self.set('body', data)
end,
['set-headers'] = function(self, data)
self.set('headers', data or {})
end,
['add-body-data'] = function(self, data, std)
self.set('body', (std.http.body or '')..data)
end    
}
local function native_http_callback(self, evt, data, std)
if not callbacks[evt] then
error('http evt '..evt..' not exist!')
end
return callbacks[evt](self, data, std)
end
local P = {
func = native_http_callback
}
return P
end)
b2974[22] = r2974(22, function()
local P = {
data={
width=1280,
height=720
},
meta={
id='',
title='',
author='',
company='',
description='',
tizen_package='',
version=''
},
config = {
offset_x = 0,
offset_y = 0,
require = '',
fps_max = 60,
fps_show = 0,
fps_drop = 5,
fps_time = 5
},
callbacks={
}
}
return P;
end)
b2974[23] = r2974(23, function()
local function install(std)
std.color = std.color or {}
std.color.white = 0xFFFFFFFF
std.color.lightgray = 0xC8CCCCFF
std.color.gray = 0x828282FF
std.color.darkgray = 0x505050FF
std.color.yellow = 0xFDF900FF
std.color.gold = 0xFFCB00FF
std.color.orange = 0xFFA100FF
std.color.pink = 0xFF6DC2FF
std.color.red = 0xE62937FF
std.color.maroon = 0xBE2137FF
std.color.green = 0x00E430FF
std.color.lime = 0x009E2FFF
std.color.darkgreen = 0x00752CFF
std.color.skyblue = 0x66BFFFFF
std.color.blue = 0x0079F1FF
std.color.darkblue = 0x0052ACFF
std.color.purple = 0xC87AFFFF
std.color.violet = 0x873CBEFF
std.color.darkpurple = 0x701F7EFF
std.color.beige = 0xD3B083FF
std.color.brown = 0x7F6A4FFF
std.color.darkbrown = 0x4C3F2FFF
std.color.black = 0x000000FF
std.color.blank = 0x00000000
std.color.magenta = 0xFF00FFFF
end
local P = {
install = install
}
return P
end)
b2974[24] = r2974(24, function()
local P = {
milis = 0,
delta = 0,
math = {
},
media = {
},
draw = {
image = function() end,
clear = function () end,
color = function () end,
rect = function () end,
line = function () end,
poly = function () end,
tui_text = function() end
},
text = {
put = function() end,
print = function() end,
mensure = function() end,
font_size = function() end,
font_name = function() end,
font_default = function() end
},
image = {
load = function() end,
draw = function() end
},
app = {
width = 1280,
height = 720,
title = function() end,
reset = function () end,
load = function() end,
exit = function () end
},
key = {
axis = {
x = 0,
y = 0,
menu=0,
up=0,
down=0,
left=0,
right=0,
a = 0,
b = 0, 
c = 0,
d = 0
},
press = {
menu=false,
up=false,
down=false,
left=false,
right=false,
a=false,
b=false,
c=false,
d=false,
any=false
}
}
}
return P;
end)
b2974[25] = r2974(25, function()
local function pipe(self)
return function()
self:run()
end
end
local function stop(self)
if self.pipeline and not self.pipeline2 then
self.pipeline2 = self.pipeline
self.pipeline = nil
end
end
local function resume(self)
if not self.pipeline and self.pipeline2 then
self.pipeline = self.pipeline2
self.pipeline2 = nil
self:run()
end
end
local function run(self)
self.pipeline_current = self.pipeline_current or 1
while self.pipeline and self.pipeline_current and self.pipeline_current <= #self.pipeline do
self.pipeline[self.pipeline_current]()
if self.pipeline_current then
self.pipeline_current = self.pipeline_current + 1
end
end
return self
end
local function reset(self)
self.pipeline = self.pipeline or self.pipeline2
self.pipeline2 = nil
self.pipeline_current = nil
end
local function clear(self)
self.pipeline_current = nil
self.pipeline2 = nil
self.pipeline = nil
end
local P = {
reset=reset,
clear=clear,
pipe=pipe,
stop=stop,
resume=resume,
run=run
}
return P
end)
b2974[26] = r2974(26, function()
local function encode(dsl_string)
local spec = {
list = {},
required = {},
all = false
}
for entry in (dsl_string or ''):gmatch("[^%s]+") do
if entry == "*" then
spec.all = true
else
local is_optional = entry:sub(-1) == "?"
local name = is_optional and entry:sub(1, -2) or entry
spec.list[#spec.list + 1] = name
spec.required[#spec.required + 1] = not is_optional
end
end
return spec
end
local function missing(spec, imported)
local result = {}
do
local index = 1
while spec.list[index] do
local name = spec.list[index]
if spec.required[index] and not imported[name] then
result[#result + 1] = name
end
index = index + 1
end
end
return result
end
local function should_import(spec, libname)
local index = 1
while spec.list[index] do
if spec.list[index] == libname then return true end
index = index + 1
end
return spec.all
end
local P = {
encode = encode,
missing = missing,
should_import = should_import
}
return P
end)
b2974[27] = r2974(27, function()
local function script(src)
local ok, app = false, nil
if require then
ok, app = pcall(require, src:gsub('%.lua$', ''))
end
if not ok and dofile then
ok, app =  pcall(dofile, src)
end
if not ok and loadfile then
ok, app = pcall(loadfile, src)
end
if type(app) == 'function' then
ok, app = pcall(app)
end
if not ok then
return false, 'failed to eval file'
end
return ok, app
end
local P = {
script = script,
}
return P
end)
b2974[28] = r2974(28, function()
local function script(src)
local loader = loadstring or load
if not loader then
error('eval not allowed')
end
local ok, chunk = pcall(loader, src)
if not ok then
return false, chunk
end
if type(chunk) ~= 'function' then
return false, 'failed to eval code'
end
return pcall(chunk)
end
local P = {
script = script,
}
return P
end)
b2974[29] = r2974(29, function()
local function get_max_width(indent)
if indent <= 40 then
return 80
else
return nil
end
end
local function wrap_text(text, indent)
local max_width = get_max_width(indent)
local prefix = string.rep("  ", indent)
if not max_width then
local lines = {}
for line in text:gmatch("[^\n]+") do
table.insert(lines, prefix .. line)
end
return table.concat(lines, "\n")
end
local words = {}
for word in text:gmatch("%S+") do table.insert(words, word) end
local lines = {}
local line = ""
for _, word in ipairs(words) do
if #line + #word + 1 > max_width - indent * 2 then
table.insert(lines, prefix .. line)
line = word
else
if line == "" then
line = word
else
line = line .. " " .. word
end
end
end
if line ~= "" then table.insert(lines, prefix .. line) end
return table.concat(lines, "\n")
end
local function sanitize_key(key)
if type(key) ~= "string" then
key = tostring(key)
end
key = key:gsub("[^%w_-]", "_")
return key
end
local function should_quote(s)
if type(s) ~= "string" then return false end
if s:match("^[^%a_]") or s:match("^%d+$") then
return true
end
if s == "true" or s == "false" or s == "null" then
return false
end
return false
end
local function format_string(s, indent)
if type(s) ~= "string" then
return tostring(s)
end
local max_width = get_max_width(indent)
if s:find("\n") then
local lines = {}
for line in s:gmatch("[^\n]+") do
if max_width then
line = wrap_text(line, indent + 1)
else
line = string.rep("  ", indent + 1) .. line
end
table.insert(lines, line)
end
return "|-\n" .. table.concat(lines, "\n")
elseif max_width and #s > max_width - indent * 2 then
return ">-\n" .. wrap_text(s, indent + 1)
elseif should_quote(s) then
return string.format("%q", s)
else
return s
end
end
local function to_yaml(tbl, indent)
indent = indent or 0
local yaml = ""
local prefix = string.rep("  ", indent)
local nums = {}
local keys = {}
for k, v in pairs(tbl) do
if type(v) ~= "function" then
if type(k) == "number" then
table.insert(nums, k)
else
table.insert(keys, k)
end
end
end
table.sort(keys)
for _, k in ipairs(keys) do
local v = tbl[k]
local safe_key = sanitize_key(k)
if type(v) == "table" then
yaml = yaml .. prefix .. safe_key .. ":\n" .. to_yaml(v, indent + 1)
else
yaml = yaml .. prefix .. safe_key .. ": " .. format_string(v, indent) .. "\n"
end
end
table.sort(nums)
for _, k in ipairs(nums) do
local v = tbl[k]
if type(v) == "table" then
yaml = yaml .. prefix .. "-\n" .. to_yaml(v, indent + 1)
else
yaml = yaml .. prefix .. "- " .. format_string(v, indent) .. "\n"
end
end
return yaml
end
return {
encode = to_yaml
}
end)
b2974[31] = r2974(31, function()
local function decorator_prefix3(zig, zag, zom, func)
return function (a, b, c, d, e, f)
return func(zig, zag, zom, a, b, c, d, e, f)
end
end
local function decorator_prefix2(zig, zag, func)
return function (a, b, c, d, e, f)
return func(zig, zag, a, b, c, d, e, f)
end
end
local function decorator_prefix1(zig, func)
return function (a, b, c, d, e, f)
return func(zig, a, b, c, d, e, f)
end
end
local function decorator_offset_xy2(object, func)
return function(a, b, c, d, e, f)
local x = object.offset_x + (b or 0)
local y = object.offset_y + (c or 0)
return func(a, x, y, d, e, f)
end
end
local function decorator_offset_xyxy1(object, func)
return function(a, b, c, d, e, f)
local x1 = object.offset_x + a
local y1 = object.offset_y + b
local x2 = object.offset_x + c
local y2 = object.offset_y + d
return func(x1, y1, x2, y2, e, f)
end
end
local function decorator_offset_xy1(object, func)
return function(a, b, c, d, e, f)
local x = object.offset_x + a
local y = object.offset_y + b
return func(x, y, c, d, e, f)
end
end
local function table_prefix1(prefix, fn_table)
local new_table = {}
for name, fn in pairs(fn_table) do
new_table[name] = decorator_prefix1(prefix, fn)
end
return new_table
end
local P = {
offset_xy1 = decorator_offset_xy1,
offset_xy2 = decorator_offset_xy2,
offset_xyxy1 = decorator_offset_xyxy1,
prefix3 = decorator_prefix3,
prefix2 = decorator_prefix2,
prefix1 = decorator_prefix1,
prefix1_t = table_prefix1
}
return P
end)
b2974[32] = r2974(32, function()
local function is_ok(status)
return (status and 200 <= status and status < 300) or false
end
local function is_ok_header(header)
local status = tonumber(header:match('HTTP/%d.%d (%d%d%d)'))
local ok = status and is_ok(status) or false
return ok, status
end
local function is_redirect(status)
return (status and 300 <= status and status < 400) or false
end
local function get_content(response)
local header, body = response:match("^(.-\r\n\r\n)(.*)")
if not header or not body then return nil end
local content_length = tonumber(header:match("Content%-Length:%s*(%d+)"))
if not content_length then return nil end
if #body < content_length then return nil end
local content = body:sub(1, content_length)
return content
end
local function get_user_agent()
return 'Ginga (GlyOS;SmartTv/Linux)'
end
local function create_request(method, uri)
local self = {
body_content = '',
header_list = {},
header_dict = {},
header_imutable = {},
print_http_status = true
}
self.add_body_content = function (body)
self.body_content = self.body_content..(body or '')
return self
end
self.add_imutable_header = function (header, value, cond)
if cond == false then return self end
if self.header_imutable[header] == nil then
self.header_list[#self.header_list + 1] = header
self.header_dict[header] = value
elseif self.header_imutable[header] == false then
self.header_dict[header] = value
end
self.header_imutable[header] = true
return self
end
self.add_mutable_header = function (header, value, cond)
if cond == false then return self end
if self.header_imutable[header] == nil then
self.header_list[#self.header_list + 1] = header
self.header_imutable[header] = false
self.header_dict[header] = value
end
return self
end
self.add_custom_headers = function(header_list, header_dict)
local index = 1
while header_list and #header_list >= index do
local header = header_list[index]
local value = header_dict[header]
if self.header_imutable[header] == nil then
self.header_list[#self.header_list + 1] = header
self.header_imutable[header] = false
self.header_dict[header] = value
elseif self.header_imutable[header] == false then
self.header_dict[header] = value
end
index = index + 1
end
return self
end
self.not_status = function()
self.print_http_status = false
return self
end
self.to_http_protocol = function ()
local index = 1
local request = method..' '..uri..' HTTP/1'..'.1\r\n'
while index <= #self.header_list do
local header = self.header_list[index]
local value = self.header_dict[header]
request = request..header..': '..value..'\r\n'
index = index + 1
end
request = request..'\r\n'
if method ~= 'GET' and method ~= 'HEAD' and #self.body_content > 0 then
request = request..self.body_content..'\r\n\r\n'
end
return request, function() end
end
self.to_curl_cmd = function ()
local index = 1
local request = 'curl -L -'..'-silent -'..'-insecure '
if self.print_http_status then
request = request..'-w "\n%{http_code}" '
end
if method == 'HEAD' then
request = request..'-'..'-HEAD '
else
request = request..'-X '..method..' '
end
while index <= #self.header_list do
local header = self.header_list[index]
local value = self.header_dict[header]
request = request..'-H "'..header..': '..value..'" '
index = index + 1
end
if method ~= 'GET' and method ~= 'HEAD' and #self.body_content > 0 then
request = request..'-d \''..self.body_content..'\' '
end
request = request..uri
return request, function() end
end
self.to_wget_cmd = function ()
local request = 'wget -'..'-quiet -'..'-output-document=-'
if method == 'HEAD' then
request = request..' -'..'-method=HEAD'
elseif method ~= 'GET' then
request = request..' -'..'-method='..method
end
for index, header in ipairs(self.header_list) do
local value = self.header_dict[header]
if value then
local escaped_value = value:gsub('"', '\\"')
request = request..' -'..'-header="'..header..': '..escaped_value..'"'
end
end
if method ~= 'GET' and method ~= 'HEAD' and #self.body_content > 0 then
local escaped_body = self.body_content:gsub('"', '\\"')
request = request..' -'..'-body-data="'..escaped_body..'"'
end
request = request..' '..uri
return request, function() end
end
return self
end
return {
is_ok=is_ok,
is_ok_header=is_ok_header,
is_redirect=is_redirect,
get_content=get_content,
get_user_agent=get_user_agent,
create_request=create_request
}
end)
b2974[33] = r2974(33, function()
local function percent_encode(str)
return (str:gsub('[^A-Za-z0-9%-_%.~]', function(c)
return string.format('%%%02X', string.byte(c))
end))
end
local function search_param(param_list, param_dict)
local index, params = 1, ''
while param_list and param_dict and index <= #param_list do
local param = param_list[index]
local value = param_dict[param]
if #params == 0 then
params = params..'?'
else
params = params..'&'
end
params = params..percent_encode(param)..'='..percent_encode(value or '')
index = index + 1
end
return params
end
local P = {
search_param = search_param
}
return P
end)
return m2974()
