2014-05-23 11:14:17 +00:00
|
|
|
local msg = require 'mp.msg'
|
|
|
|
|
|
|
|
local function val2str(val)
|
|
|
|
if type(val) == "boolean" then
|
|
|
|
if val then val = "yes" else val = "no" end
|
|
|
|
end
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
|
|
|
-- converts val to type of desttypeval
|
|
|
|
local function typeconv(desttypeval, val)
|
|
|
|
if type(desttypeval) == "boolean" then
|
|
|
|
if val == "yes" then
|
|
|
|
val = true
|
|
|
|
elseif val == "no" then
|
|
|
|
val = false
|
|
|
|
else
|
|
|
|
msg.error("Error: Can't convert " .. val .. " to boolean!")
|
|
|
|
val = nil
|
|
|
|
end
|
|
|
|
elseif type(desttypeval) == "number" then
|
|
|
|
if not (tonumber(val) == nil) then
|
|
|
|
val = tonumber(val)
|
|
|
|
else
|
|
|
|
msg.error("Error: Can't convert " .. val .. " to number!")
|
|
|
|
val = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-05-29 13:50:09 +00:00
|
|
|
local function read_options(options, identifier)
|
2014-05-23 11:14:17 +00:00
|
|
|
if identifier == nil then
|
|
|
|
identifier = mp.get_script_name()
|
|
|
|
end
|
2014-09-16 15:19:22 +00:00
|
|
|
msg.debug("reading options for " .. identifier)
|
2014-05-23 11:14:17 +00:00
|
|
|
|
|
|
|
-- read config file
|
2017-12-13 17:37:42 +00:00
|
|
|
local conffilename = "script-opts/" .. identifier .. ".conf"
|
2014-05-23 11:14:17 +00:00
|
|
|
local conffile = mp.find_config_file(conffilename)
|
2017-12-13 17:37:42 +00:00
|
|
|
if conffile == nil then
|
2019-11-01 00:52:05 +00:00
|
|
|
msg.debug(conffilename .. " not found.")
|
2017-12-13 17:37:42 +00:00
|
|
|
conffilename = "lua-settings/" .. identifier .. ".conf"
|
|
|
|
conffile = mp.find_config_file(conffilename)
|
|
|
|
if conffile then
|
|
|
|
msg.warn("lua-settings/ is deprecated, use directory script-opts/")
|
|
|
|
end
|
|
|
|
end
|
2014-05-23 11:14:17 +00:00
|
|
|
local f = conffile and io.open(conffile,"r")
|
|
|
|
if f == nil then
|
|
|
|
-- config not found
|
2019-11-01 00:52:05 +00:00
|
|
|
msg.debug(conffilename .. " not found.")
|
2014-05-23 11:14:17 +00:00
|
|
|
else
|
|
|
|
-- config exists, read values
|
2019-11-01 00:52:05 +00:00
|
|
|
msg.verbose("Opened config file " .. conffilename .. ".")
|
2014-05-23 11:14:17 +00:00
|
|
|
local linecounter = 1
|
|
|
|
for line in f:lines() do
|
|
|
|
if string.find(line, "#") == 1 then
|
|
|
|
|
|
|
|
else
|
|
|
|
local eqpos = string.find(line, "=")
|
|
|
|
if eqpos == nil then
|
|
|
|
|
|
|
|
else
|
|
|
|
local key = string.sub(line, 1, eqpos-1)
|
|
|
|
local val = string.sub(line, eqpos+1)
|
|
|
|
|
|
|
|
-- match found values with defaults
|
|
|
|
if options[key] == nil then
|
|
|
|
msg.warn(conffilename..":"..linecounter..
|
|
|
|
" unknown key " .. key .. ", ignoring")
|
|
|
|
else
|
|
|
|
local convval = typeconv(options[key], val)
|
|
|
|
if convval == nil then
|
|
|
|
msg.error(conffilename..":"..linecounter..
|
|
|
|
" error converting value '" .. val ..
|
|
|
|
"' for key '" .. key .. "'")
|
|
|
|
else
|
|
|
|
options[key] = convval
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
linecounter = linecounter + 1
|
|
|
|
end
|
|
|
|
io.close(f)
|
|
|
|
end
|
|
|
|
|
2014-05-23 11:24:27 +00:00
|
|
|
--parse command-line options
|
2014-12-14 23:31:30 +00:00
|
|
|
for key, val in pairs(mp.get_property_native("options/script-opts")) do
|
2014-05-23 11:14:17 +00:00
|
|
|
local prefix = identifier.."-"
|
|
|
|
if not (string.find(key, prefix, 1, true) == nil) then
|
|
|
|
key = string.sub(key, string.len(prefix)+1)
|
|
|
|
|
|
|
|
-- match found values with defaults
|
|
|
|
if options[key] == nil then
|
2014-12-14 23:31:30 +00:00
|
|
|
msg.warn("script-opts: unknown key " .. key .. ", ignoring")
|
2014-05-23 11:14:17 +00:00
|
|
|
else
|
|
|
|
local convval = typeconv(options[key], val)
|
|
|
|
if convval == nil then
|
2014-12-14 23:31:30 +00:00
|
|
|
msg.error("script-opts: error converting value '" .. val ..
|
2014-05-23 11:14:17 +00:00
|
|
|
"' for key '" .. key .. "'")
|
|
|
|
else
|
|
|
|
options[key] = convval
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-05-29 13:50:09 +00:00
|
|
|
-- backwards compatibility with broken read_options export
|
|
|
|
_G.read_options = read_options
|
2014-05-23 11:14:17 +00:00
|
|
|
|
2015-05-29 13:50:09 +00:00
|
|
|
return {
|
|
|
|
read_options = read_options,
|
|
|
|
}
|