Showing the changes to player/javascript (1 of 2 files). Show all
Unified · Split
| @@ -419,6 +419,58 @@ function new_require(base_id) { · 418 unmodified lines | ||
| 419 | 419 | |
| 420 | 420 | g.require = new_require(SCRIPTDIR_META + "/" + main_script[1]); |
| 421 | 421 | |
| 422 | +/********************************************************************** | |
| 423 | + * mp.options | |
| 424 | + *********************************************************************/ | |
| 425 | +function read_options(opts, id) { | |
| 426 | + id = String(typeof id != "undefined" ? id : mp.get_script_name()); | |
| 427 | + mp.msg.debug("reading options for " + id); | |
| 428 | + | |
| 429 | + var conf, fname = "~~/script-opts/" + id + ".conf"; | |
| 430 | + try { | |
| 431 | + conf = mp.utils.read_file(fname); | |
| 432 | + } catch (e) { | |
| 433 | + mp.msg.verbose(fname + " not found."); | |
| 434 | + } | |
| 435 | + | |
| 436 | + // data as config file lines array, or empty array | |
| 437 | + var data = conf ? conf.replace(/\r\n/g, "\n").split("\n") : [], | |
| 438 | + conf_len = data.length; // before we append script-opts below | |
| 439 | + | |
| 440 | + // Append relevant script-opts as <key-sans-id>=<value> to data | |
| 441 | + var sopts = mp.get_property_native("options/script-opts"), | |
| 442 | + prefix = id + "-"; | |
| 443 | + for (var key in sopts) { | |
| 444 | + if (key.indexOf(prefix) == 0) | |
| 445 | + data.push(key.substring(prefix.length) + "=" + sopts[key]); | |
| 446 | + } | |
| 447 | + | |
| 448 | + // Update opts from data | |
| 449 | + data.forEach(function(line, i) { | |
| 450 | + if (line[0] == "#" || line.trim() == "") | |
| 451 | + return; | |
| 452 | + | |
| 453 | + var key = line.substring(0, line.indexOf("=")), | |
| 454 | + val = line.substring(line.indexOf("=") + 1), | |
| 455 | + type = typeof opts[key], | |
| 456 | + info = i < conf_len ? fname + ":" + (i + 1) // 1-based line number | |
| 457 | + : "script-opts:" + prefix + key; | |
| 458 | + | |
| 459 | + if (!opts.hasOwnProperty(key)) | |
| 460 | + mp.msg.warn(info, "Ignoring unknown key '" + key + "'"); | |
| 461 | + else if (type == "string") | |
| 462 | + opts[key] = val; | |
| 463 | + else if (type == "boolean" && (val == "yes" || val == "no")) | |
| 464 | + opts[key] = (val == "yes"); | |
| 465 | + else if (type == "number" && val.trim() != "" && !isNaN(val)) | |
| 466 | + opts[key] = Number(val); | |
| 467 | + else | |
| 468 | + mp.msg.error(info, "Error: can't convert '" + val + "' to " + type); | |
| 469 | + }); | |
| 470 | +} | |
| 471 | + | |
| 472 | +mp.options = { read_options: read_options }; | |
| 473 | + | |
| 422 | 474 | /********************************************************************** |
| 423 | 475 | * various |
| 424 | 476 | *********************************************************************/ |