From 8b0db338aef65babf0da2ff496da91d0acaecda4 Mon Sep 17 00:00:00 2001 From: Sergej Geringer Date: Thu, 27 Jan 2022 23:49:20 +0100 Subject: [PATCH] Lua Service: add mmProject() and lua project getters --- .../Lua_Service_Wrapper.cpp | 62 ++++++++++++++++++- .../project_loader/ProjectLoader_Service.cpp | 2 + 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/frontend/services/lua_service_wrapper/Lua_Service_Wrapper.cpp b/frontend/services/lua_service_wrapper/Lua_Service_Wrapper.cpp index c9ccf487bb..4516779ee6 100644 --- a/frontend/services/lua_service_wrapper/Lua_Service_Wrapper.cpp +++ b/frontend/services/lua_service_wrapper/Lua_Service_Wrapper.cpp @@ -16,6 +16,7 @@ #include "FrameStatistics.h" #include "GUIState.h" #include "GlobalValueStore.h" +#include "MegaMolProject.h" #include "RuntimeConfig.h" #include "Screenshots.h" #include "WindowManipulation.h" @@ -119,8 +120,9 @@ bool Lua_Service_Wrapper::init(const Config& config) { frontend_resources::CommandRegistry_Req_Name, "optional", "RuntimeConfig", + "MegaMolProject", #ifdef PROFILING -#define PROFILING_RESOURCE_SLOT 11 +#define PROFILING_RESOURCE_SLOT 12 frontend_resources::PerformanceManager_Req_Name, #endif }; //= {"ZMQ_Context"}; @@ -676,6 +678,64 @@ void Lua_Service_Wrapper::fill_graph_manipulation_callbacks(void* callbacks_coll return StringResult{answer.str().c_str()}; }}); + + callbacks.add("mmProject", + "()\n\tSets the current script file as active project, clearing all previous graph state", + {[&]() -> VoidResult { + // clear graph + auto& graph = const_cast( + m_requestedResourceReferences[5].getResource()); + + graph.Clear(); + + // set current script file as project + auto& project = const_cast( + m_requestedResourceReferences[11].getResource()); + + auto scriptpath = luaAPI.GetScriptPath(); + + if (scriptpath.empty()) + return Error{"mmProject() error: no script path set. can not use it as project path."}; + + project.setProjectFile(scriptpath); + + return VoidResult{}; + }}); + + callbacks.add("mmGetProjectFile", + "()\n\tReturns the current project file, i.e. the script file. If no script file is set, an error is returned.", + {[&]() -> StringResult { + auto& project = m_requestedResourceReferences[11].getResource(); + + if (!project.attributes.has_value()) + return Error{"mmGetProjectFile() error: no project file set."}; + + return StringResult{project.attributes.value().project_file.string()}; + }}); + + callbacks.add("mmGetProjectDirectory", + "()\n\tReturns the current project directory. If no script file is set, an error is returned.", + {[&]() -> StringResult { + auto& project = m_requestedResourceReferences[11].getResource(); + + if (!project.attributes.has_value()) + return Error{"mmGetProjectDirectory() error: no project directory set."}; + + // has trailing '/' + return StringResult{project.attributes.value().project_directory.string()}; + }}); + + callbacks.add("mmGetProjectName", + "()\n\tReturns the name of the current project directory. If no script file is set, an error is returned.", + {[&]() -> StringResult { + auto& project = m_requestedResourceReferences[11].getResource(); + + if (!project.attributes.has_value()) + return Error{"mmGetProjectName() error: no project name set."}; + + return StringResult{project.attributes.value().project_name}; + }}); + #ifdef PROFILING callbacks.add("mmListModuleTimers", "(string name)\n\tList the registered timers of a module.", {[&](std::string name) -> StringResult { diff --git a/frontend/services/project_loader/ProjectLoader_Service.cpp b/frontend/services/project_loader/ProjectLoader_Service.cpp index cd43bf62e9..ca244f444d 100644 --- a/frontend/services/project_loader/ProjectLoader_Service.cpp +++ b/frontend/services/project_loader/ProjectLoader_Service.cpp @@ -127,6 +127,8 @@ bool ProjectLoader_Service::load_file(std::filesystem::path const& filename) con } log("loaded file " + filename.generic_u8string() + ((script_error.size()) ? "\n\t" + script_error : "")); + if (m_current_project.attributes.has_value()) + log("file is project"); return true; }