Permalink
Browse files

Begin of plugin manager in c++

  • Loading branch information...
feragon committed Jun 14, 2017
1 parent 04ba806 commit dad86e62725f9c3ad0ac1442b216e4fdf08245f5
View
@@ -1,7 +1,8 @@
#include "luainterface.h"
LuaInterface::LuaInterface() {
_L = LuaIntf::LuaState::newState();
LuaInterface::LuaInterface() :
_L(LuaIntf::LuaState::newState()),
_pluginManager("gui") {
}
LuaInterface::~LuaInterface() {
@@ -24,6 +25,8 @@ void LuaInterface::initLua() {
std::cout << lua_tostring(_L, -1) << std::endl;
lua_pop(_L, 1);
}
_pluginManager.loadPlugins();
}
bool LuaInterface::luaConnect(
View
@@ -17,6 +17,7 @@ extern "C"
#include <QTextStream>
#include <QtUiTools/QUiLoader>
#include <QCoreApplication>
#include <managers/pluginmanager.h>
#include "lua/luaqobject.h"
#include "lua-intf/LuaIntf/LuaIntf.h"
@@ -97,4 +98,5 @@ class LuaInterface {
private:
LuaIntf::LuaState _L;
std::vector<LuaQObject_SPtr> _luaQObjects;
lc::PluginManager _pluginManager;
};
@@ -35,7 +35,7 @@ function createSandboxEnv()
return env
end
function load_plugins()
function load_plugins() -- TODO: remove
local plugins = luaInterface:pluginList(plugin_path)
for k, v in pairs(plugins) do
plugin, err = loadfile(plugin_path .. "/" .. v .. "/plugin.lua", "t", createSandboxEnv())
@@ -0,0 +1 @@
print(LC_interface)
View
@@ -6,5 +6,4 @@ require 'actions.operations'
require 'ui.operations'
require 'kernel.pluginmanager'
create_main_window()
load_plugins()
create_main_window()
@@ -11,6 +11,7 @@ message("***** LibreCAD lcadluascript *****")
set(lcluascript_srcs
lcadluascript.cpp
cad/lualibrecadbridge.cpp
managers/pluginmanager.cpp
)
# HEADER FILES
@@ -20,6 +21,7 @@ set(lcluascript_hdrs
lcadluascript.h
cad/lualibrecadbridge.h
cad/timer.h
managers/pluginmanager.h
)
@@ -35,6 +37,11 @@ else ()
include_directories( ${EIGEN3_INCLUDE_DIR})
endif ()
#Lua
find_package(Lua 5.2 REQUIRED)
include_directories(${LUA_INCLUDE_DIR})
link_directories(${LUA_LIBRARIES})
set(SEPARATE_BUILD OFF)
if (SEPARATE_BUILD STREQUAL "ON")
@@ -0,0 +1,66 @@
#include "pluginmanager.h"
#include <dirent.h>
#include <iostream>
#include <cad/lualibrecadbridge.h>
static const luaL_Reg loadedlibs[] = {
{"_G", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_STRLIBNAME, luaopen_string},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_UTF8LIBNAME, luaopen_utf8},
{NULL, NULL}
};
lc::PluginManager::PluginManager(const char* interface) :
_interface(interface) {
}
void lc::PluginManager::loadPlugins() {
const char* path = "../lcUILua/plugins/"; //TODO: get path
DIR* dir;
struct dirent* ent;
if((dir = opendir(path)) != NULL) {
while((ent = readdir(dir)) != NULL) {
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
loadPlugin((std::string(path) + ent->d_name + "/plugin.lua").c_str());
}
closedir(dir);
}
else {
perror("Error opening dir");
}
}
void lc::PluginManager::loadPlugin(const char* file) {
auto state = LuaIntf::LuaState::newState();
addLuaLibs(state);
lua_openlckernel(state);
LuaIntf::Lua::setGlobal(state, "LC_interface", _interface);
int s = state.doFile(file);
if (s != 0) {
std::cout << lua_tostring(state, -1) << std::endl;
lua_pop(state, 1);
}
}
void lc::PluginManager::addLuaLibs(lua_State* L) {
const luaL_Reg *lib;
for (lib = loadedlibs; lib->func; lib++) {
luaL_requiref(L, lib->name, lib->func, 1);
lua_pop(L, 1);
}
}
@@ -0,0 +1,22 @@
#pragma once
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "lua-intf/LuaIntf/LuaIntf.h"
namespace lc {
class PluginManager {
public:
PluginManager(const char* interface);
void loadPlugins();
private:
void loadPlugin(const char* file);
void addLuaLibs(lua_State* L);
const char* _interface;
};
}
View
@@ -13,6 +13,8 @@
#include <curl/curl.h>
#include <boost/program_options.hpp>
#include <boost/filesystem.hpp>
#include <managers/pluginmanager.h>
namespace po = boost::program_options;
using LcPainter = LCViewer::LcPainter;
@@ -162,6 +164,9 @@ int main(int argc, char** argv) {
[&](LcPainter & lcPainter) {});
// Render Lua Code
lc::PluginManager pluginManager("cli");
pluginManager.loadPlugins();
LCadLuaScript luaScript(_document, false);
std::string luaCode = loadFile(fIn);

0 comments on commit dad86e6

Please sign in to comment.