From e3c71210155e0a44cc26054a29fbc51906dd8826 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 9 Sep 2021 11:14:34 +0200
Subject: [PATCH] SCRIPT: Add getters to get some VivyDocument properties from
 the script API

---
 src/Lib/Script/CRTPLuaScriptObject.hh         | 46 +++++++++++++++++--
 .../CRTPLuaScriptObject/FreeFunctions.cc      | 36 ++++++++-------
 2 files changed, 62 insertions(+), 20 deletions(-)

diff --git a/src/Lib/Script/CRTPLuaScriptObject.hh b/src/Lib/Script/CRTPLuaScriptObject.hh
index 311acb65..6772bb5a 100644
--- a/src/Lib/Script/CRTPLuaScriptObject.hh
+++ b/src/Lib/Script/CRTPLuaScriptObject.hh
@@ -17,10 +17,15 @@ namespace Vivy::Script
 #define LUA_RETURN_NOTHING(L)                       { lua_settop(L, 0); return 0; }
 // clang-format on
 
+#define method_list static constexpr inline auto
 #define script_class(theClassName) \
     class theClassName final : public CRTPLuaScriptObject<theClassName>
 
-#define method_list static constexpr inline auto
+#define LUA_DECL_GETTER(type, luaName, value)             \
+    static int luaName(lua_State *const L) noexcept       \
+    {                                                     \
+        return luaPushValue<type>(L, CHECK(L, 1)->value); \
+    }
 
 #define LUA_SCRIPTABLE_CLASS_MANUAL_CTOR(theClassName) \
     VIVY_UNMOVABLE_OBJECT(theClassName)                \
@@ -62,6 +67,30 @@ protected:
         exit(EXIT_FAILURE); // lua_error should not return anything
     }
 
+    // Push something onto the stack, must be specialized
+    template <typename T> static int luaPushValue(lua_State *const L, const T &thing) noexcept;
+    using std_str  = std::string;
+    using std_strv = std::string_view;
+
+    template <> static int luaPushValue<int>(lua_State *const L, const int &thing) noexcept
+    {
+        lua_pushinteger(L, thing);
+        return 1;
+    }
+
+    template <>
+    static int luaPushValue<std_strv>(lua_State *const L, const std_strv &thing) noexcept
+    {
+        lua_pushlstring(L, thing.data(), thing.size());
+        return 1;
+    }
+
+    template <> static int luaPushValue<std_str>(lua_State *const L, const std_str &thing) noexcept
+    {
+        lua_pushstring(L, thing.c_str());
+        return 1;
+    }
+
     static bool PushFunctionFromRegistry(lua_State *const L, const int key) noexcept
     {
         if (key >= 0) {
@@ -335,13 +364,22 @@ script_class (FreeFunctions) {
 
     static int print(lua_State *const) noexcept;
     static int getModule(lua_State *const) noexcept;
-    static int start(lua_State *const) noexcept;
-    static int finish(lua_State *const) noexcept;
+
+    LUA_DECL_GETTER(int, start, audioStart)
+    LUA_DECL_GETTER(int, finish, audioFinish)
+    LUA_DECL_GETTER(int, width, videoWidth)
+    LUA_DECL_GETTER(int, height, videoHeight)
 
     method_list metaMethods = { luaRegDefaultGC };
     method_list methods     = { LUA_DECL_METHOD(FreeFunctions, getModule),
                             LUA_DECL_METHOD(FreeFunctions, start),
                             LUA_DECL_METHOD(FreeFunctions, finish),
-                            LUA_DECL_METHOD(FreeFunctions, print), LUA_DECL_CREATE(FreeFunctions) };
+                            LUA_DECL_METHOD(FreeFunctions, width),
+                            LUA_DECL_METHOD(FreeFunctions, height),
+                            LUA_DECL_METHOD(FreeFunctions, print),
+                            LUA_DECL_CREATE(FreeFunctions) };
+
+    int videoWidth{ 0 }, videoHeight{ 0 };
+    int audioStart{ 0 }, audioFinish{ 0 };
 };
 }
diff --git a/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc b/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc
index 9192f1d1..36c9cee7 100644
--- a/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc
+++ b/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc
@@ -1,5 +1,9 @@
 #include "../CRTPLuaScriptObject.hh"
 #include "../LuaContext.hh"
+#include "../../Document/CRTPSubDocument.hh"
+#include "../../Document/VivyDocument.hh"
+#include "../../Audio.hh"
+#include "../../Video.hh"
 #include "../../../VivyApplication.hh"
 
 using namespace Vivy::Script;
@@ -11,6 +15,22 @@ FreeFunctions::FreeFunctions(lua_State *const L) noexcept
 
     std::shared_ptr<VivyDocument> vivyDoc = context->getAttachedVivyDocument();
     if (vivyDoc) {
+        if (!(vivyDoc->checkDocumentCapabilities(VivyDocument::AudioAble) &&
+              vivyDoc->checkDocumentCapabilities(VivyDocument::AssAble) &&
+              vivyDoc->checkDocumentCapabilities(VivyDocument::AudioAble))) {
+            err(context) << "The VivyDocument is not a complete document, "
+                         << "can't load it for scripting";
+            luaGlobalError(L, "Attached VivyDocument is not complete");
+        }
+
+        out(context) << "Initializing the FreeFunctions lua object with the VivyDocument";
+        std::shared_ptr<VideoStream> video = vivyDoc->getVideoSubDocument()->getDefaultStream();
+        [[maybe_unused]] std::shared_ptr<AudioStream> audio =
+            vivyDoc->getAudioSubDocument()->getDefaultStream();
+
+        videoWidth  = video->getWidth();
+        videoHeight = video->getWidth();
+        TODO(Set getters values for audio)
     }
 
     else {
@@ -34,19 +54,3 @@ FreeFunctions::getModule(lua_State *const L) noexcept
     [[maybe_unused]] const ModuleDeclaration *const mod = context->getModule(modName);
     return 1;
 }
-
-int
-FreeFunctions::start(lua_State *const L) noexcept
-{
-    lua_settop(L, 0);
-    lua_pushinteger(L, 0);
-    return 1;
-}
-
-int
-FreeFunctions::finish(lua_State *const L) noexcept
-{
-    lua_settop(L, 0);
-    lua_pushinteger(L, 0);
-    return 1;
-}
-- 
GitLab