Also, add the function mp.get_script_directory() to let scripts know if they're loaded as a directory and where.
Unified · Split
| @@ -28,6 +28,8 @@ Interface changes · 27 unmodified lines | |||
| 28 | - directories in ~/.mpv/scripts/ (or equivalent) now have special semantics | 28 | - directories in ~/.mpv/scripts/ (or equivalent) now have special semantics |
| 29 | (see mpv Lua scripting docs) | 29 | (see mpv Lua scripting docs) |
| 30 | - names starting with "." in ~/.mpv/scripts/ (or equivalent) are now ignored | 30 | - names starting with "." in ~/.mpv/scripts/ (or equivalent) are now ignored |
| 31 | - js modules: ~~/scripts/modules.js/ is no longer used, global paths can be | ||
| 32 | set with custom init (see docs), dir-scripts first look at <dir>/modules/ | ||
| 31 | --- mpv 0.32.0 --- | 33 | --- mpv 0.32.0 --- |
| 32 | - change behavior when using legacy option syntax with options that start | 34 | - change behavior when using legacy option syntax with options that start |
| 33 | with two dashes (``--`` instead of a ``-``). Now, using the recommended | 35 | with two dashes (``--`` instead of a ``-``). Now, using the recommended |
| @@ -139,6 +139,8 @@ success, ``fn`` is called always a-sync, ``error`` is empty string on success. · 138 unmodified lines | |||
| 139 | 139 | ||
| 140 | ``mp.get_script_name()`` | 140 | ``mp.get_script_name()`` |
| 141 | 141 | ||
| 142 | ``mp.get_script_directory()`` | ||
| 143 | |||
| 142 | ``mp.osd_message(text [,duration])`` | 144 | ``mp.osd_message(text [,duration])`` |
| 143 | 145 | ||
| 144 | ``mp.get_wakeup_pipe()`` | 146 | ``mp.get_wakeup_pipe()`` |
| @@ -309,7 +311,8 @@ or ``~/x``. Otherwise it's considered a global module id and searched according · 164 unmodified lines | |||
| 309 | load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js`` | 311 | load ``x.js`` at one of the array paths, and id ``foo/x`` tries to load ``x.js`` |
| 310 | inside dir ``foo`` at one of the paths. | 312 | inside dir ``foo`` at one of the paths. |
| 311 | 313 | ||
| 312 | The ``mp.module_paths`` array is empty by default. | 314 | The ``mp.module_paths`` array is empty by default except for scripts which are |
| 315 | loaded as a directory where it contains one item - ``<directory>/modules/``. | ||
| 313 | ``mp.module_paths`` may be updated from a script (preferably via custom init - | 316 | ``mp.module_paths`` may be updated from a script (preferably via custom init - |
| 314 | see below) which will affect future calls to ``require`` for global module id's | 317 | see below) which will affect future calls to ``require`` for global module id's |
| 315 | which are not already loaded/cached. | 318 | which are not already loaded/cached. |
| @@ -59,6 +59,7 @@ static const char *const builtin_files[][3] = { · 58 unmodified lines | |||
| 59 | // Represents a loaded script. Each has its own js state. | 59 | // Represents a loaded script. Each has its own js state. |
| 60 | struct script_ctx { | 60 | struct script_ctx { |
| 61 | const char *filename; | 61 | const char *filename; |
| 62 | const char *path; // NULL if single file | ||
| 62 | struct mpv_handle *client; | 63 | struct mpv_handle *client; |
| 63 | struct MPContext *mpctx; | 64 | struct MPContext *mpctx; |
| 64 | struct mp_log *log; | 65 | struct mp_log *log; |
| @@ -477,6 +478,7 @@ static int s_load_javascript(struct mp_script_args *args) · 412 unmodified lines | |||
| 477 | .log = args->log, | 478 | .log = args->log, |
| 478 | .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"), | 479 | .last_error_str = talloc_strdup(ctx, "Cannot initialize JavaScript"), |
| 479 | .filename = args->filename, | 480 | .filename = args->filename, |
| 481 | .path = args->path, | ||
| 480 | }; | 482 | }; |
| 481 | 483 | ||
| 482 | int r = -1; | 484 | int r = -1; |
| @@ -1281,6 +1283,11 @@ static void add_functions(js_State *J, struct script_ctx *ctx) · 798 unmodified lines | |||
| 1281 | js_pushstring(J, ctx->filename); | 1283 | js_pushstring(J, ctx->filename); |
| 1282 | js_setproperty(J, -2, "script_file"); | 1284 | js_setproperty(J, -2, "script_file"); |
| 1283 | 1285 | ||
| 1286 | if (ctx->path) { | ||
| 1287 | js_pushstring(J, ctx->path); | ||
| 1288 | js_setproperty(J, -2, "script_path"); | ||
| 1289 | } | ||
| 1290 | |||
| 1284 | js_pop(J, 2); // leave the stack as we got it | 1291 | js_pop(J, 2); // leave the stack as we got it |
| 1285 | } | 1292 | } |
| 1286 | 1293 | ||
| @@ -463,6 +463,8 @@ function process_timers() { · 462 unmodified lines | |||
| 463 | *********************************************************************/ | 463 | *********************************************************************/ |
| 464 | 464 | ||
| 465 | mp.module_paths = []; // global modules search paths | 465 | mp.module_paths = []; // global modules search paths |
| 466 | if (mp.script_path !== undefined) // loaded as a directory | ||
| 467 | mp.module_paths.push(mp.utils.join_path(mp.script_path, "modules")); | ||
| 466 | 468 | ||
| 467 | // Internal meta top-dirs. Users should not rely on these names. | 469 | // Internal meta top-dirs. Users should not rely on these names. |
| 468 | var MODULES_META = "~~modules", | 470 | var MODULES_META = "~~modules", |
| @@ -645,6 +647,7 @@ mp.options = { read_options: read_options }; · 176 unmodified lines | |||
| 645 | g.print = mp.msg.info; // convenient alias | 647 | g.print = mp.msg.info; // convenient alias |
| 646 | mp.get_script_name = function() { return mp.script_name }; | 648 | mp.get_script_name = function() { return mp.script_name }; |
| 647 | mp.get_script_file = function() { return mp.script_file }; | 649 | mp.get_script_file = function() { return mp.script_file }; |
| 650 | mp.get_script_directory = function() { return mp.script_path }; | ||
| 648 | mp.get_time = function() { return mp.get_time_ms() / 1000 }; | 651 | mp.get_time = function() { return mp.get_time_ms() / 1000 }; |
| 649 | mp.utils.getcwd = function() { return mp.get_property("working-directory") }; | 652 | mp.utils.getcwd = function() { return mp.get_property("working-directory") }; |
| 650 | mp.dispatch_event = dispatch_event; | 653 | mp.dispatch_event = dispatch_event; |