Skip to content

Commit

Permalink
Added script loading feature
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Nov 13, 2024
1 parent b5e97ff commit 4762295
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Source/Pd/Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,19 @@ 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) {
auto searchPath = File(path.getProperty("Path").toString());
if (!searchPath.exists() || !searchPath.isDirectory())
continue;

auto childFile = searchPath.getChildFile(patchToFind + ".pd");
auto childFile = searchPath.getChildFile(fileToFind);
if (childFile.existsAsFile())
return childFile;
}
Expand Down
1 change: 1 addition & 0 deletions Source/Pd/Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<StringArray, 2> parseIoletTooltips(ValueTree const& iolets, String const& name, int numIn, int numOut);

Expand Down
31 changes: 30 additions & 1 deletion Source/Sidebar/CommandInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand Down Expand Up @@ -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"):
{
Expand Down

0 comments on commit 4762295

Please sign in to comment.