diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc index 2998a1eb3f197a23e9e669a7a42389127515da18..52c00dd7d2ded0365192014bbc7feea9c9fd518c 100644 --- a/src/Lib/Script/LuaContext.cc +++ b/src/Lib/Script/LuaContext.cc @@ -49,6 +49,15 @@ LuaContext::getContext(const lua_State *const state) noexcept return contextList.contains(state) ? contextList.at(state) : nullptr; } +LuaContext::Code +LuaContext::loadDocument(std::shared_ptr<Vivy::VivyDocument> doc) noexcept +{ + if (attachedVivyDocument != nullptr) + return Error; + attachedVivyDocument = doc; + return Success; +} + LuaContext::Code LuaContext::loadDocument(std::shared_ptr<ScriptDocument> doc) noexcept { diff --git a/src/Lib/Script/ScriptStore.cc b/src/Lib/Script/ScriptStore.cc index 14ee4bc27d8a7e94c854e99c1df62be71c8fa929..44b8e5d16652b9ac658debbdd7ec40c7ea1bfeba 100644 --- a/src/Lib/Script/ScriptStore.cc +++ b/src/Lib/Script/ScriptStore.cc @@ -47,10 +47,22 @@ ScriptStore::resetLoadedScripts() noexcept bool ScriptStore::executeScript(Uuid id) noexcept { - const auto script = documents.at(id); + const auto &script = documents.at(id); return luaContext->loadDocument(script) == Code::Success; } +bool +ScriptStore::executeScript(Uuid id, std::shared_ptr<Vivy::VivyDocument> doc) noexcept +{ + if (doc == nullptr) + return false; + if (luaContext->loadDocument(doc) != Script::LuaContext::Success) { + luaContext->setFailedWith("A VivyDocument is already loaded, can't attach a new one"); + return false; + } + return executeScript(id); +} + const std::vector<std::string_view> ScriptStore::getLoadedModules() const noexcept { diff --git a/src/Lib/Script/ScriptStore.hh b/src/Lib/Script/ScriptStore.hh index 636da75c11c3eec908e695e5d3529f062999b101..0a1968a6d3b2579843e248605191eca7e534690c 100644 --- a/src/Lib/Script/ScriptStore.hh +++ b/src/Lib/Script/ScriptStore.hh @@ -1,6 +1,7 @@ #pragma once #include "../CRTPStore.hh" +#include "../Document/VivyDocument.hh" #include "ScriptDocument.hh" #include "LuaContext.hh" @@ -27,6 +28,7 @@ public: void loadScriptFolder(const QString &folderPath); std::vector<Item> getLoadedScripts() const noexcept; bool executeScript(Uuid) noexcept; + bool executeScript(Uuid, std::shared_ptr<Vivy::VivyDocument>) noexcept; QString getLastLuaError() noexcept; diff --git a/src/VivyApplication.cc b/src/VivyApplication.cc index f511fe9d3f34a840508a268651b7a4658639c04f..d474fca4c21e01152380f3f4b5ff196d013d1cfa 100644 --- a/src/VivyApplication.cc +++ b/src/VivyApplication.cc @@ -2,7 +2,9 @@ #include "UI/MainWindow.hh" #include "UI/DockWidgetTitleBar.hh" #include "Lib/Document/VivyDocumentStore.hh" +#include "Lib/Document/VivyDocument.hh" #include "Lib/Script/ScriptStore.hh" +#include "Lib/Script/ScriptDocument.hh" using namespace Vivy; @@ -14,7 +16,25 @@ VivyApplication::VivyApplication(int &argc, char **argv) VIVY_LOG_CTOR() << "Construction is OK"; if (argc >= 2) { - selectedDoc = scriptStore->loadDocument(QString::fromUtf8(argv[1])); + for (int i = 1; i < argc; ++i) { + const QString passedFileName(QString::fromUtf8(argv[1])); + const QFileInfo passedArgumentFile(passedFileName); + Utils::DocumentType passedFileType; + if (Utils::detectDocumentType(passedArgumentFile, &passedFileType)) { + if (passedFileType == Utils::DocumentType::Vivy) { + selectedVivyDoc = documentStore->loadDocument(passedFileName); + logInfo() << "Select vivy document " << passedFileName; + } else if (passedFileType == Utils::DocumentType::VivyScript) { + selectedScriptDoc = scriptStore->loadDocument(passedFileName); + logInfo() << "Select script document " << passedFileName; + } else { + logError() << "Not handled file type for file " << passedFileName; + } + } else { + logError() << "Unrecognized file type for file " << passedFileName; + } + } + selectedType = ApplicationType::CLI; VIVY_LOG_CTOR() << "Select the ApplicationType::CLI"; } @@ -70,9 +90,12 @@ VivyApplication::exec() noexcept switch (selectedType) { case ApplicationType::CLI: { - if ((selectedDoc == nullptr) || (!scriptStore->executeScript(selectedDoc->getUuid()))) + if ((selectedVivyDoc == nullptr) || (selectedScriptDoc == nullptr)) return 1; + if (!scriptStore->executeScript(selectedScriptDoc->getUuid(), selectedVivyDoc)) + return 2; + for (const auto &str : scriptStore->getLoadedModules()) { std::cout << "Module " << str << " was loaded!\n"; [[maybe_unused]] const auto *mod = scriptStore->getModule(str); diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh index 71c8344bf90801e35252e329cd8b6e6f833aa717..654989b1805c41220045f297bfeca642a51f3632 100644 --- a/src/VivyApplication.hh +++ b/src/VivyApplication.hh @@ -49,6 +49,7 @@ class ScriptStore; class VivyDocumentStore; class AbstractDocument; class ScriptDocument; +class VivyDocument; // Vivy application class class VivyApplication : public QApplication { @@ -87,7 +88,8 @@ private: ApplicationType selectedType{ ApplicationType::GUI }; // ApplicationType::CLI Only! - std::shared_ptr<ScriptDocument> selectedDoc{ nullptr }; + std::shared_ptr<ScriptDocument> selectedScriptDoc{ nullptr }; + std::shared_ptr<VivyDocument> selectedVivyDoc{ nullptr }; public: VivyApplication(int &argc, char **argv);