From 6d29beb91b019e1995cdb7c4aaf7a043de4bf053 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Fri, 20 Sep 2024 15:13:43 -0400 Subject: Add ability to pass command-line arguments to a Lua script. Introduce `ScriptCommand` class that parses a command line into a script name and optional args, using bash-like quoting and escaping. `ScriptCommand` searches for a file with that script name on a passed list of directories; the directories may be specified relative to a particular base directory. `ScriptCommand` supports the special case of a script name containing unescaped spaces. It guarantees that either the returned script file exists, or its `error()` string is non-empty. Replace `LLLeap::create()` logic, from which `ScriptCommand` was partly derived, with a `ScriptCommand` instance. Make `LLLUAmanager::runScriptFile()` use a `ScriptCommand` instance to parse the passed command line. Subsume `LLAppViewer::init()` script-path-searching logic for `--luafile` into `ScriptCommand`. In fact that lambda now simply calls `LLLUAmanager::runScriptFile()`. Make `lluau::dostring()` accept an optional vector of script argument strings. Following PUC-Rio Lua convention, pass these arguments into a Lua script as the predefined global `arg`, and also as the script's `...` argument. `LuaState::expr()` also accepts and passes through script argument strings. Change the log tag for the Lua script interruption message: if we want it, we can still enable it, but we don't necessarily want it along with all other "Lua" DEBUG messages. Remove `LuaState::script_finished_fn`, which isn't used any more. Also remove the corresponding `LLLUAmanager::script_finished_fn`. This allows us to simplify `~LuaState()` slightly, as well as the parameter signatures for `LLLUAmanager::runScriptFile()` and `runScriptLine()`. --- indra/newview/llappviewer.cpp | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'indra/newview/llappviewer.cpp') diff --git a/indra/newview/llappviewer.cpp b/indra/newview/llappviewer.cpp index 789aaab70d..a9ac48ac88 100644 --- a/indra/newview/llappviewer.cpp +++ b/indra/newview/llappviewer.cpp @@ -1234,23 +1234,8 @@ bool LLAppViewer::init() "--luafile", "LuaScript", [](const LLSD& script) { - LLSD paths(gSavedSettings.getLLSD("LuaCommandPath")); - LL_DEBUGS("Lua") << "LuaCommandPath = " << paths << LL_ENDL; - for (const auto& path : llsd::inArray(paths)) - { - // if script path is already absolute, operator/() preserves it - auto abspath(fsyspath(gDirUtilp->getAppRODataDir()) / path.asString()); - auto absscript{ (abspath / script.asString()) }; - std::error_code ec; - if (std::filesystem::exists(absscript, ec)) - { - // no completion callback: we don't need to know - LLLUAmanager::runScriptFile(absscript.u8string()); - return; // from lambda - } - } - LL_WARNS("Lua") << "--luafile " << std::quoted(script.asString()) - << " not found on " << paths << LL_ENDL; + // no completion callback: we don't need to know + LLLUAmanager::runScriptFile(script); }); processComposeSwitch( "LuaAutorunPath", "LuaAutorunPath", @@ -1259,15 +1244,16 @@ bool LLAppViewer::init() // each directory can be relative to the viewer's install // directory -- if directory is already absolute, operator/() // preserves it - auto abspath(fsyspath(gDirUtilp->getAppRODataDir()) / directory.asString()); - std::string absdir(abspath.string()); + fsyspath abspath(fsyspath(gDirUtilp->getAppRODataDir()) / + fsyspath(directory.asString())); + std::string absdir(fsyspath(abspath).string()); LL_DEBUGS("InitInfo") << "LuaAutorunPath: " << absdir << LL_ENDL; LLDirIterator scripts(absdir, "*.lua"); std::string script; while (scripts.next(script)) { LL_DEBUGS("InitInfo") << "LuaAutorunPath: " << absdir << ": " << script << LL_ENDL; - LLLUAmanager::runScriptFile((abspath / script).string(), true); + LLLUAmanager::runScriptFile(fsyspath(abspath / fsyspath(script)).string(), true); } }); -- cgit v1.2.3