diff options
author | Avi Halachmi (:avih) <avihpit@yahoo.com> | 2017-12-14 21:22:56 +0200 |
---|---|---|
committer | Kevin Mitchell <kevmitch@gmail.com> | 2018-04-07 16:02:19 -0700 |
commit | b04f0cad43eb627fb4e970367062329e3331d9fe (patch) | |
tree | 1efa6536ec401f7c7c10577db0f300f10b7bda62 /player/javascript | |
parent | 9eadc068fa13b8d83ad049e83960883d06d3c125 (diff) |
js: implement mp.options.read_options
Diffstat (limited to 'player/javascript')
-rw-r--r-- | player/javascript/defaults.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/player/javascript/defaults.js b/player/javascript/defaults.js index 56c83f8abb..c5c1f35161 100644 --- a/player/javascript/defaults.js +++ b/player/javascript/defaults.js @@ -420,6 +420,58 @@ function new_require(base_id) { g.require = new_require(SCRIPTDIR_META + "/" + main_script[1]); /********************************************************************** + * mp.options + *********************************************************************/ +function read_options(opts, id) { + id = String(typeof id != "undefined" ? id : mp.get_script_name()); + mp.msg.debug("reading options for " + id); + + var conf, fname = "~~/script-opts/" + id + ".conf"; + try { + conf = mp.utils.read_file(fname); + } catch (e) { + mp.msg.verbose(fname + " not found."); + } + + // data as config file lines array, or empty array + var data = conf ? conf.replace(/\r\n/g, "\n").split("\n") : [], + conf_len = data.length; // before we append script-opts below + + // Append relevant script-opts as <key-sans-id>=<value> to data + var sopts = mp.get_property_native("options/script-opts"), + prefix = id + "-"; + for (var key in sopts) { + if (key.indexOf(prefix) == 0) + data.push(key.substring(prefix.length) + "=" + sopts[key]); + } + + // Update opts from data + data.forEach(function(line, i) { + if (line[0] == "#" || line.trim() == "") + return; + + var key = line.substring(0, line.indexOf("=")), + val = line.substring(line.indexOf("=") + 1), + type = typeof opts[key], + info = i < conf_len ? fname + ":" + (i + 1) // 1-based line number + : "script-opts:" + prefix + key; + + if (!opts.hasOwnProperty(key)) + mp.msg.warn(info, "Ignoring unknown key '" + key + "'"); + else if (type == "string") + opts[key] = val; + else if (type == "boolean" && (val == "yes" || val == "no")) + opts[key] = (val == "yes"); + else if (type == "number" && val.trim() != "" && !isNaN(val)) + opts[key] = Number(val); + else + mp.msg.error(info, "Error: can't convert '" + val + "' to " + type); + }); +} + +mp.options = { read_options: read_options }; + +/********************************************************************** * various *********************************************************************/ g.print = mp.msg.info; // convenient alias |