-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathOptions.js
195 lines (184 loc) · 7.37 KB
/
Options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* OPTIONS.JS (MODULE)
*
* Description: JavaScript implementation of mpv's Lua API's config file system,
* via "mp.options.read_options()". See official Lua docs for help.
* https://github.com/mpv-player/mpv/blob/master/DOCS/man/lua.rst#mpoptions-functions
* Version: 2.1.0
* Author: VideoPlayerCode
* URL: https://github.com/VideoPlayerCode/mpv-tools
* License: Apache License, Version 2.0
*/
/* jshint -W097 */
/* global mp, exports, require */
'use strict';
var ScriptConfig = function(options, identifier)
{
if (!options)
throw 'Options table parameter is missing.';
this.options = options;
this.scriptName = typeof identifier === 'string' ? identifier : mp.get_script_name();
this.configFile = null;
// Converts string "val" to same primitive type as "destTypeVal".
var typeConv = function(destTypeVal, val)
{
switch (typeof destTypeVal) {
case 'object':
if (!Array.isArray(destTypeVal))
val = undefined; // Unknown "object" target variable.
else if (typeof val !== 'string')
val = String(val); // Target is array, so use string values.
break;
case 'string':
if (typeof val !== 'string')
val = String(val);
break;
case 'boolean':
if (val === 'yes')
val = true;
else if (val === 'no')
val = false;
else {
mp.msg.error('Error: Can\'t convert '+JSON.stringify(val)+' to boolean!');
val = undefined;
}
break;
case 'number':
var num = parseFloat(val);
if (!isNaN(num))
val = num;
else {
mp.msg.error('Error: Can\'t convert '+JSON.stringify(val)+' to number!');
val = undefined;
}
break;
default:
val = undefined;
}
return val;
};
// Find config file.
if (this.scriptName && this.scriptName.length) {
mp.msg.debug('Reading options for '+this.scriptName+'.');
this.configFile = mp.find_config_file('script-settings/'+this.scriptName+'.conf');
if (!this.configFile) // Try legacy settings location as fallback.
this.configFile = mp.find_config_file('lua-settings/'+this.scriptName+'.conf');
}
// Read and parse configuration if found.
var i, len, pos, key, val, isArrayVal, convVal;
if (this.configFile && this.configFile.length) {
try {
var line, configLines = mp.utils.read_file(this.configFile).split(/[\r\n]+/);
for (i = 0, len = configLines.length; i < len; ++i) {
line = configLines[i].replace(/^\s+/, '');
if (!line.length || line.charAt(0) === '#')
continue;
pos = line.indexOf('=');
if (pos < 0) {
mp.msg.warn('"'+this.configFile+'": Ignoring malformatted config line "'+line.replace(/\s+$/, '')+'".');
continue;
}
key = line.substring(0, pos);
val = line.substring(pos + 1);
isArrayVal = false;
if ('[]' === line.substring(pos - 2, pos)) {
key = key.substring(0, key.length - 2);
isArrayVal = true;
}
if (this.options.hasOwnProperty(key)) {
convVal = typeConv(this.options[key], val);
if (typeof convVal !== 'undefined') {
if (Array.isArray(this.options[key])) {
if (isArrayVal)
this.options[key].push(convVal);
else
mp.msg.error('"'+this.configFile+'": Ignoring non-array value for array-based option key "'+key+'".');
}
else
this.options[key] = convVal;
}
else
mp.msg.error('"'+this.configFile+'": Unable to convert value "'+val+'" for key "'+key+'".');
}
else
mp.msg.warn('"'+this.configFile+'": Ignoring unknown key "'+key+'".');
}
} catch (e) {
mp.msg.error('Unable to read configuration file "'+this.configFile+'".');
}
}
else
mp.msg.verbose('Unable to find configuration file for '+this.scriptName+'.');
// Parse command-line options.
if (this.scriptName && this.scriptName.length) {
var cmdOpts = mp.get_property_native('options/script-opts'), rawOpt,
prefix = this.scriptName+'-', keyLen;
len = prefix.length;
for (rawOpt in cmdOpts) {
if (!cmdOpts.hasOwnProperty(rawOpt))
continue;
pos = rawOpt.indexOf(prefix);
if (pos !== 0)
continue;
key = rawOpt.substring(len);
keyLen = key.length;
isArrayVal = false;
if ('[]' === key.substring(keyLen - 2)) {
key = key.substring(0, keyLen - 2);
isArrayVal = true;
}
if (key.length && this.options.hasOwnProperty(key)) {
val = cmdOpts[rawOpt];
convVal = typeConv(this.options[key], val);
if (typeof convVal !== 'undefined') {
if (Array.isArray(this.options[key])) {
if (isArrayVal)
this.options[key].push(convVal);
else
mp.msg.error('script-opts: Ignoring non-array value for array-based option key "'+key+'".');
}
else
this.options[key] = convVal;
}
else
mp.msg.error('script-opts: Unable to convert value "'+val+'" for key "'+key+'".');
}
else
mp.msg.warn('script-opts: Ignoring unknown key "'+key+'".');
}
}
};
ScriptConfig.prototype.getValue = function(key)
{
if (!this.options.hasOwnProperty(key))
throw 'Invalid option "'+key+'"';
return this.options[key];
};
ScriptConfig.prototype.getMultiValue = function(key)
{
// Multi-value format: `{one}+{two}+{three}`.
var i, len,
val = this.getValue(key), // Throws.
result = [];
if (typeof val !== 'string')
throw 'Invalid non-string value in multi-value option "'+key+'"';
len = val.length;
if (len) {
if (val.charAt(0) !== '{' || val.charAt(len - 1) !== '}')
throw 'Missing surrounding "{}" brackets in multi-value option "'+key+'"';
val = val.substring(1, len - 1).split('}+{');
len = val.length;
for (i = 0; i < len; ++i) {
result.push(val[i]);
}
}
return result;
};
// Class `advanced_options()`: Offers extended features such as multi-values.
exports.advanced_options = ScriptConfig;
// Function `read_options()`: Behaves like Lua API (returns plain list of opts).
exports.read_options = function(table, identifier) {
// NOTE: "table" will be modified by reference, just as the Lua version.
var config = new ScriptConfig(table, identifier);
return config.options; // This is the same object as "table".
};