modulemanager.cpp 5.75 KB
Newer Older
Leander Schulten's avatar
Leander Schulten committed
1
2
3
#include "modulemanager.h"
#include <QLibrary>
#include "module.h"
Leander Schulten's avatar
Leander Schulten committed
4
#include <QDebug>
5
#include "settings.h"
Leander Schulten's avatar
Leander Schulten committed
6
7
#include <QJsonObject>
#include <QJsonArray>
Leander Schulten's avatar
Leander Schulten committed
8
9
10

namespace Modules {

Leander Schulten's avatar
Leander Schulten committed
11
12
13
namespace detail {

    PropertyInformation::PropertyInformation(const QJsonObject &o):name(o["name"].toString("no name")),
14
15
        description(o["description"].toString()),type(static_cast<Type>(o["type"].toInt())),max(o["maxValue"].toInt()),
        min(o["minValue"].toInt()),defaultValue(o["defaultValue"].toInt()){
Leander Schulten's avatar
Leander Schulten committed
16
17
18
19
20
21
22

    }

    void PropertyInformation::writeJsonObject(QJsonObject &o)const{
            o["type"] = type;
            o["name"] = name;
            o["description"] = description;
23
24
25
            o["minValue"] = min;
            o["maxValue"] = max;
            o["defaultValue"] = defaultValue;
Leander Schulten's avatar
Leander Schulten committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    }

}

Module::Module(const QJsonObject &o):name(o["name"].toString("no name")),
    description(o["description"].toString()),code(o["code"].toString()),
    valueType(static_cast<ValueType>(o["valueType"].toInt())),
    type(static_cast<Type>(o["type"].toInt())){
    QJsonArray a = o["properties"].toArray();
    for(const auto i : a){
        QJsonObject ob = i.toObject();
        properties.push_back(new detail::PropertyInformation(ob));
    }
}

void Module::writeJsonObject(QJsonObject &o)const{
    o["name"] = name;
    o["description"] = description;
    o["code"] = code;
    o["valueType"] = valueType;
    o["type"] = type;
    QJsonArray a;
    for(const auto & p : properties){
        QJsonObject prop;
        p->writeJsonObject(prop);
        a.append(prop);
    }
    o["properties"] = a;
}

Leander Schulten's avatar
Leander Schulten committed
56
57
58
59
60
61
typedef  bool (*Have_Func)(MODUL_TYPE t);


typedef unsigned int (*GetNumberOfProgramms)();
typedef std::string (*GetNameOfProgramm)(unsigned int index);
typedef std::string (*GetDescriptionOfProgramm)(unsigned int index);
Leander Schulten's avatar
Leander Schulten committed
62
typedef Modules::Program* (*CreateProgramm)(unsigned int index);
Leander Schulten's avatar
Leander Schulten committed
63
64
65
66
67
68

    ModuleManager::ModuleManager()
    {

    }

Leander Schulten's avatar
Leander Schulten committed
69
70
71
72
73
74
75
76
    void ModuleManager::loadModules(const QJsonObject & o){
        QJsonArray a = o["modules"].toArray();
        for(const auto _o : a){
            QJsonObject ob = _o.toObject();
            modules.push_back(new Module(ob));
        }
    }

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    bool ModuleManager::unloadLibrary(QString filePath){
        for(auto e = loadedLibraryMap.begin();e!=loadedLibraryMap.end();++e){
            qDebug()<<e->first;
            if(e->first == filePath){
                filter.erase(std::remove_if(filter.begin(),filter.end(),[&](const auto &f){
                    return f.libraryIdentifier == e->second;
                }),filter.cend());
                programms.erase(std::remove_if(programms.begin(),programms.end(),[&](const auto &f){
                    return f.libraryIdentifier == e->second;
                }),programms.cend());
                consumer.erase(std::remove_if(consumer.begin(),consumer.end(),[&](const auto &f){
                    return f.libraryIdentifier == e->second;
                }),consumer.cend());
                QLibrary lib(filePath);
                bool suc = lib.unload();
                if(suc){
                   loadedLibraryMap.erase(e);
                   QFile f(filePath);
                   if(!f.remove()){
                       return f.rename(f.fileName()+".old");
                   }
                   return true;
                }
                int counter = 0;
                while (QFile::exists(filePath+ QString::number(counter)+".old")) {
                    ++counter;
                }
                suc = QFile(filePath).rename(filePath+ QString::number(counter)+".old");
                if(suc){
                    loadedLibraryMap.erase(e);
                }
                return suc;
            }
        }
        return true;
    }

Leander Schulten's avatar
Leander Schulten committed
114
115
116
117
118
119
120
121
122
123
    void ModuleManager::writeJsonObject(QJsonObject &o){
        QJsonArray a ;
        for(const auto &m : modules){
            QJsonObject ob;
            m->writeJsonObject(ob);
            a.append(ob);
        }
        o["modules"] = a;
    }

Leander Schulten's avatar
Leander Schulten committed
124
    void ModuleManager::loadModule(QString name){
Leander Schulten's avatar
Leander Schulten committed
125
        qDebug()<<"load lib  : " << name;
Leander Schulten's avatar
Leander Schulten committed
126
127
128
        QLibrary lib(name);
        if(lib.load()){
            Have_Func f = reinterpret_cast<Have_Func>(lib.resolve("have"));
Leander Schulten's avatar
Leander Schulten committed
129
130
            if(!f){
                qDebug()<<"have funktion is missing";
Leander Schulten's avatar
Leander Schulten committed
131
                return;
Leander Schulten's avatar
Leander Schulten committed
132
            }
133
134
            lastLibraryIdentifier++;
            loadedLibraryMap.emplace_back(lib.fileName(),lastLibraryIdentifier);
Leander Schulten's avatar
Leander Schulten committed
135
            if(f(MODUL_TYPE::Program)){
136
                loadType(lib,programms,"Programm",lastLibraryIdentifier);
Leander Schulten's avatar
Leander Schulten committed
137
            }if(f(MODUL_TYPE::LoopProgram)){
138
                //loadType(lib,programms,"LoopProgramm",lastLibraryIdentifier);
Leander Schulten's avatar
Leander Schulten committed
139
            }if(f(MODUL_TYPE::Filter)){
140
                loadType(lib,filter,"Filter",lastLibraryIdentifier);
Leander Schulten's avatar
Leander Schulten committed
141
            }if(f(MODUL_TYPE::Consumer)){
Leander Schulten's avatar
Leander Schulten committed
142
                qDebug()<< "Loading Consumer";
143
                loadType(lib,consumer,"Consumer",lastLibraryIdentifier);
Leander Schulten's avatar
Leander Schulten committed
144
            }
Leander Schulten's avatar
Leander Schulten committed
145
        }else{
146
            qDebug()<<"Cant load lib :" << name<< " because : " << lib.errorString() ;
Leander Schulten's avatar
Leander Schulten committed
147
148
149
150
        }
    }

    void ModuleManager::loadAllModulesInDir(QDir dir){
Leander Schulten's avatar
Leander Schulten committed
151
152
         qDebug() << dir;
        for(auto s : dir.entryInfoList(QDir::Files)){
153
#ifdef Q_OS_WIN
154
155
156
            if(s.suffix() == "old"){
                QFile::remove(s.filePath());
            }
157
            if(s.suffix() == "dll")
158
#endif
159
            loadModule(s.absoluteFilePath());
160
            //qDebug()<<"found : " << s;
Leander Schulten's avatar
Leander Schulten committed
161
        }
162
        for(auto s : dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)){
163
164
            loadAllModulesInDir(s);
        }
Leander Schulten's avatar
Leander Schulten committed
165
        for(const auto & m : getProgrammModules())
166
            qDebug() << m.name().c_str();
Leander Schulten's avatar
Leander Schulten committed
167
        for(const auto & m : getFilterModules())
168
            qDebug() << m.name().c_str();
Leander Schulten's avatar
Leander Schulten committed
169
        for(const auto & m : getConsumerModules())
170
171
            qDebug() << m.name().c_str();

Leander Schulten's avatar
Leander Schulten committed
172
173
174
    }

}