diff --git a/Source/Pd/Library.cpp b/Source/Pd/Library.cpp index ef2114d5d..693fd7b86 100644 --- a/Source/Pd/Library.cpp +++ b/Source/Pd/Library.cpp @@ -336,6 +336,11 @@ void Library::filesystemChanged() } File Library::findPatch(String const& patchToFind) +{ + return findFile(patchToFind + ".pd"); +} + +File Library::findFile(String const& fileToFind) { auto pathTree = SettingsFile::getInstance()->getValueTree().getChildWithName("Paths"); for (auto path : pathTree) { @@ -343,7 +348,7 @@ File Library::findPatch(String const& patchToFind) if (!searchPath.exists() || !searchPath.isDirectory()) continue; - auto childFile = searchPath.getChildFile(patchToFind + ".pd"); + auto childFile = searchPath.getChildFile(fileToFind); if (childFile.existsAsFile()) return childFile; } diff --git a/Source/Pd/Library.h b/Source/Pd/Library.h index 0e9b59682..ef393e1b3 100644 --- a/Source/Pd/Library.h +++ b/Source/Pd/Library.h @@ -35,6 +35,7 @@ class Library : public FileSystemWatcher::Listener StringArray searchObjectDocumentation(String const& query); static File findPatch(String const& patchToFind); + static File findFile(String const& fileToFind); static StackArray parseIoletTooltips(ValueTree const& iolets, String const& name, int numIn, int numOut); diff --git a/Source/Sidebar/CommandInput.h b/Source/Sidebar/CommandInput.h index 26e972ee2..22e2ab2ae 100644 --- a/Source/Sidebar/CommandInput.h +++ b/Source/Sidebar/CommandInput.h @@ -61,10 +61,31 @@ class LuaExpressionParser { } } + void executeScript(const juce::String& filePath) { + // Load the script without executing it + if (luaL_loadfile(L, filePath.toRawUTF8()) == LUA_OK) { + // Set the environment of the loaded chunk to _G to make everything global + lua_pushglobaltable(L); // Push _G onto the stack + lua_setupvalue(L, -2, 1); // Set _G as the environment of the chunk + + // Execute the chunk, which will register all functions globally + if (!lua_pcall(L, 0, 0, 0) == LUA_OK) { + const char* error = lua_tostring(L, -1); + pd->logError("Error executing Lua script: " + juce::String::fromUTF8(error)); + lua_pop(L, 1); // Remove error message from stack + } + } else { + const char* error = lua_tostring(L, -1); + pd->logError("Error loading Lua script: " + juce::String::fromUTF8(error)); + lua_pop(L, 1); // Remove error message from stack + } + } + + // Function to execute an expression and return result as LuaResult (either double or string) LuaResult executeExpression(String const& expression, bool hasReturnValue) { - String luaCode = "local __eval = function()"; + String luaCode = "local __eval = function()\n"; if(hasReturnValue) luaCode += "\nreturn "; luaCode += expression.trim(); // Append the expression without altering it luaCode += R"( @@ -526,6 +547,14 @@ class CommandInput final cnv->patch.deselectAll(); } } + case hash("script"): + { + auto script = pd::Library::findFile(tokens[1] + ".lua"); + if(script.existsAsFile()) { + lua->executeScript(script.getFullPathName()); + } + break; + } case hash("?"): case hash("help"): {