From ef96ce874abd8023a293ebfc8e1295dfceb490f9 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 9 Sep 2021 10:25:26 +0200
Subject: [PATCH] SCRIPT: Add a way to attach a VivyDocument to a LuaContext

---
 src/Lib/Script/LuaContext.cc  |  9 +++++++++
 src/Lib/Script/ScriptStore.cc | 14 +++++++++++++-
 src/Lib/Script/ScriptStore.hh |  2 ++
 src/VivyApplication.cc        | 27 +++++++++++++++++++++++++--
 src/VivyApplication.hh        |  4 +++-
 5 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc
index 2998a1eb..52c00dd7 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 14ee4bc2..44b8e5d1 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 636da75c..0a1968a6 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 f511fe9d..d474fca4 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 71c8344b..654989b1 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);
-- 
GitLab