summaryrefslogtreecommitdiff
path: root/core/modules.cpp
blob: 09ee63be574f03d585db7126a8075a052fc848c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
#include "commonheaders.h"

extern std::list<plugin*> plugins;

extern PLUGINLINK pluglink;

void load_modules()
{
    wxStandardPaths *pth = new wxStandardPaths;
    wxString path = wxPathOnly(pth->GetExecutablePath());
    delete pth;
#ifdef _WIN32
    path.append((wxChar*)_T("\\plugins"));
#else
    path.append((wxChar*)_T("/plugins"));
#endif
    wxDir dir(path);
//    wxMessageBox(path ,_("path"),  wxOK | wxICON_INFORMATION);

    if(!dir.IsOpened())
    {
        wxMessageBox(_T("Plugins directory does not exists") ,_T("Error"),  wxOK | wxICON_ERROR);
        wxLogDebug(_T("Plugins directory does not exists\n"));
        return;
    }
    wxString filename;
#ifdef _WIN32
    if(!dir.GetFirst(&filename, _T("*.dll"), wxDIR_FILES | wxDIR_HIDDEN))
    {
        wxMessageBox(_T("Plugins directory does not contain plugins") ,_T("Error"),  wxOK | wxICON_ERROR);
        wxLogDebug((wxChar*)_T("Plugins directory does not contain plugins\n"));
        return;
    }

#else
    if(!dir.GetFirst(&filename, _T("*.so"), wxDIR_FILES | wxDIR_HIDDEN))
    {
        wxMessageBox(_T("Plugins directory does not contain plugins") ,_T("Error"),  wxOK | wxICON_ERROR);
        wxLogDebug(_T("Plugins directory does not contain plugins\n"));
        return;
    }

#endif
    do
    {
        wxString  lib_path = path;
#ifdef _WIN32
        lib_path += _T("\\");
#else
        lib_path += _T("/");
#endif
        lib_path += filename;
        wxDynamicLibrary *plug = new wxDynamicLibrary(lib_path);
        if(!plug->IsLoaded())
        {
            wxMessageBox(_T("Failed to load plugin") ,_T("Error"),  wxOK | wxICON_ERROR);
            wxLogDebug(_T("Failed to load plugin\n"));
        }
        bool is_plugin = true;
        plugin::exported_functions_s *funcs = new plugin::exported_functions_s;
        if((funcs->Load = (load)plug->GetSymbol(_T("load"))) == NULL)
            is_plugin = false;
        if((funcs->OnModulesLoaded = (on_modules_loaded)plug->GetSymbol(_T("on_modules_loaded"))) == NULL)
            is_plugin = false;
        if((funcs->Unload = (unload)plug->GetSymbol(_T("unload"))) == NULL)
            is_plugin = false;
        if((funcs->SetPluginInfo = (set_plugin_info)plug->GetSymbol(_T("set_plugin_info"))) == NULL)
            is_plugin = false;
        if(!is_plugin)
        {
            delete plug;
            delete funcs;
            continue;
        }
        PLUGININFO *info = funcs->SetPluginInfo();
        plugins.push_back(new plugin(plug, info, funcs));
        lib_path.clear();
    }
    while(dir.GetNext(&filename));

}
void run_plugins()
{ //now for testing only
    if(!plugins.empty())
    {
        std::list<plugin*>::iterator end = plugins.end();
        for(std::list<plugin*>::iterator i = plugins.begin(); i != end; ++i)
        {
            (*i)->get_exported_functions()->Load(&pluglink);
            (*i)->get_exported_functions()->OnModulesLoaded();
        }
    }
}

plugin::plugin(wxDynamicLibrary *lib, PLUGININFO *info, exported_functions_s *funcs)
{
    if(lib)
        plug = lib;
    if(info)
        plugininfo = info;
    if(funcs)
        exported_funcs = funcs;
}
const plugin::exported_functions_s* plugin::get_exported_functions()
{
    return exported_funcs;
}