Initial changes needed for building Mixbus (with MSVC) as version 5
[ardour.git] / libs / ardour / luascripting.cc
index 8e9157afe8e00c8c0029f0b4e96c821e44ff73c0..1193e081301834acd875ebe41440c1d32e93bd83 100644 (file)
@@ -56,6 +56,8 @@ LuaScripting::LuaScripting ()
        , _sl_hook (0)
        , _sl_action (0)
        , _sl_snippet (0)
+       , _sl_setup (0)
+       , _sl_tracks (0)
 {
        ;
 }
@@ -69,6 +71,8 @@ LuaScripting::~LuaScripting ()
                delete _sl_hook;
                delete _sl_action;
                delete _sl_snippet;
+               delete _sl_setup;
+               delete _sl_tracks;
        }
 }
 
@@ -83,12 +87,16 @@ LuaScripting::refresh (bool run_scan)
        delete _sl_hook;
        delete _sl_action;
        delete _sl_snippet;
+       delete _sl_setup;
+       delete _sl_tracks;
 
        _sl_dsp = 0;
        _sl_session = 0;
        _sl_hook = 0;
        _sl_action = 0;
        _sl_snippet = 0;
+       _sl_setup = 0;
+       _sl_tracks = 0;
 
        if (run_scan) {
                lm.release ();
@@ -120,6 +128,8 @@ LuaScripting::scan ()
        CLEAR_OR_NEW (_sl_hook)
        CLEAR_OR_NEW (_sl_action)
        CLEAR_OR_NEW (_sl_snippet)
+       CLEAR_OR_NEW (_sl_setup)
+       CLEAR_OR_NEW (_sl_tracks)
 
 #undef CLEAR_OR_NEW
 
@@ -148,6 +158,9 @@ LuaScripting::scan ()
                        case LuaScriptInfo::Snippet:
                                _sl_snippet->push_back(lsi);
                                break;
+                       case LuaScriptInfo::SessionInit:
+                               _sl_setup->push_back(lsi);
+                               break;
                        default:
                                break;
                }
@@ -158,6 +171,8 @@ LuaScripting::scan ()
        std::sort (_sl_hook->begin(), _sl_hook->end(), ScriptSorter());
        std::sort (_sl_action->begin(), _sl_action->end(), ScriptSorter());
        std::sort (_sl_snippet->begin(), _sl_snippet->end(), ScriptSorter());
+       std::sort (_sl_setup->begin(), _sl_setup->end(), ScriptSorter());
+       std::sort (_sl_tracks->begin(), _sl_tracks->end(), ScriptSorter());
 
        scripts_changed (); /* EMIT SIGNAL */
 }
@@ -179,8 +194,7 @@ LuaScripting::scan_script (const std::string &fn, const std::string &sc)
 
        lua_State* L = lua.getState();
        lua.Print.connect (&LuaScripting::lua_print);
-
-       lua.do_command ("io = nil;");
+       lua.sandbox (true);
 
        lua.do_command (
                        "ardourluainfo = {}"
@@ -269,13 +283,33 @@ LuaScripting::scan_script (const std::string &fn, const std::string &sc)
                if (key == "description") { lsi->description = val; }
                if (key == "category") { lsi->category = val; }
        }
+
+
+       if (type == LuaScriptInfo::EditorAction) {
+
+               luabridge::LuaRef lua_rs = luabridge::getGlobal (L, "route_setup");
+               if (lua_rs.isFunction ()) {
+                       lsi->subtype |= LuaScriptInfo::RouteSetup;
+               }
+
+               luabridge::LuaRef lua_ss = luabridge::getGlobal (L, "session_setup");
+               if (lua_ss.isFunction ()) {
+                       try {
+                               if (lua_ss () == true) {
+                                       lsi->subtype |= LuaScriptInfo::SessionSetup;
+                               }
+                       } catch (...) { }
+               }
+
+       }
+
        return lsi;
 }
 
 LuaScriptList &
 LuaScripting::scripts (LuaScriptInfo::ScriptType type) {
 
-       if (!_sl_dsp || !_sl_session || !_sl_hook || !_sl_action || !_sl_snippet) {
+       if (!_sl_dsp || !_sl_session || !_sl_hook || !_sl_action || !_sl_snippet || ! _sl_setup || ! _sl_tracks) {
                scan ();
        }
 
@@ -295,6 +329,9 @@ LuaScripting::scripts (LuaScriptInfo::ScriptType type) {
                case LuaScriptInfo::Snippet:
                        return *_sl_snippet;
                        break;
+               case LuaScriptInfo::SessionInit:
+                       return *_sl_setup;
+                       break;
                default:
                        break;
        }
@@ -310,6 +347,7 @@ LuaScriptInfo::type2str (const ScriptType t) {
                case LuaScriptInfo::EditorHook: return "EditorHook";
                case LuaScriptInfo::EditorAction: return "EditorAction";
                case LuaScriptInfo::Snippet: return "Snippet";
+               case LuaScriptInfo::SessionInit: return "SessionInit";
                default: return "Invalid";
        }
 }
@@ -322,6 +360,7 @@ LuaScriptInfo::str2type (const std::string& str) {
        if (!strcasecmp (type, "EditorHook")) {return LuaScriptInfo::EditorHook;}
        if (!strcasecmp (type, "EditorAction")) {return LuaScriptInfo::EditorAction;}
        if (!strcasecmp (type, "Snippet")) {return LuaScriptInfo::Snippet;}
+       if (!strcasecmp (type, "SessionInit")) {return LuaScriptInfo::SessionInit;}
        return LuaScriptInfo::Invalid;
 }
 
@@ -334,12 +373,18 @@ LuaScriptParams::script_params (const LuaScriptInfoPtr& lsi, const std::string &
 
 LuaScriptParamList
 LuaScriptParams::script_params (const std::string& s, const std::string &pname, bool file)
+{
+       LuaState lua;
+       return LuaScriptParams::script_params (lua, s, pname, file);
+}
+
+LuaScriptParamList
+LuaScriptParams::script_params (LuaState& lua, const std::string& s, const std::string &pname, bool file)
 {
        LuaScriptParamList rv;
 
-       LuaState lua;
        lua_State* L = lua.getState();
-       lua.do_command ("io = nil;");
+       lua.sandbox (true);
        lua.do_command ("function ardour () end");
 
        try {
@@ -348,7 +393,7 @@ LuaScriptParams::script_params (const std::string& s, const std::string &pname,
                } else {
                        lua.do_command (s);
                }
-       } catch (luabridge::LuaException const& e) {
+       } catch (...) {
                return rv;
        }
 
@@ -365,6 +410,7 @@ LuaScriptParams::script_params (const std::string& s, const std::string &pname,
                                std::string title = i.value ()["title"].cast<std::string> ();
                                std::string dflt;
                                bool optional = false;
+                               bool preseeded = false;
 
                                if (i.value ()["default"].isString ()) {
                                        dflt = i.value ()["default"].cast<std::string> ();
@@ -372,7 +418,10 @@ LuaScriptParams::script_params (const std::string& s, const std::string &pname,
                                if (i.value ()["optional"].isBoolean ()) {
                                        optional = i.value ()["optional"].cast<bool> ();
                                }
-                               LuaScriptParamPtr lsspp (new LuaScriptParam(name, title, dflt, optional));
+                               if (i.value ()["preseeded"].isBoolean ()) {
+                                       preseeded = i.value ()["preseeded"].cast<bool> ();
+                               }
+                               LuaScriptParamPtr lsspp (new LuaScriptParam(name, title, dflt, optional, preseeded));
                                rv.push_back (lsspp);
                        }
                }
@@ -416,6 +465,7 @@ LuaScripting::try_compile (const std::string& script, const LuaScriptParamList&
        }
        LuaState l;
        l.Print.connect (&LuaScripting::lua_print);
+       l.sandbox (true);
        lua_State* L = l.getState();
 
        l.do_command (""
@@ -425,7 +475,7 @@ LuaScripting::try_compile (const std::string& script, const LuaScriptParamList&
                        "  assert(type(f) == 'string', 'Assigned ByteCode must be string')"
                        "  local factory = load(f)"
                        "  assert(type(factory) == 'function', 'Factory is a not a function')"
-                       "  local env = _ENV;  env.f = nil env.debug = nil os.exit = nil"
+                       "  local env = _ENV; env.f = nil env.os = nil env.io = nil"
                        "  load (string.dump(factory, true), nil, nil, env)(a)"
                        " end"
                        );
@@ -444,7 +494,7 @@ LuaScripting::try_compile (const std::string& script, const LuaScriptParamList&
                cerr << e.what() << "\n";
 #endif
                lua_print (e.what());
-       }
+       } catch (...) { }
 
        return false;
 }
@@ -454,6 +504,7 @@ LuaScripting::get_factory_bytecode (const std::string& script, const std::string
 {
        LuaState l;
        l.Print.connect (&LuaScripting::lua_print);
+       l.sandbox (true);
        lua_State* L = l.getState();
 
        l.do_command (
@@ -474,7 +525,7 @@ LuaScripting::get_factory_bytecode (const std::string& script, const std::string
                if (lua_factory.isFunction()) {
                        return (lua_dump(lua_factory)).cast<std::string> ();
                }
-       } catch (luabridge::LuaException const& e) { }
+       } catch (...) { }
        return "";
 }