From 58a5565b6d840af2e6a0e74c1554018f75d689b0 Mon Sep 17 00:00:00 2001 From: Moritz Eberl Date: Mon, 11 Apr 2016 16:53:05 +0200 Subject: Added a mechanism to load c++ extensions dynamically. (bzr r14761.1.1) --- src/extension/loader.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/extension/loader.cpp (limited to 'src/extension/loader.cpp') diff --git a/src/extension/loader.cpp b/src/extension/loader.cpp new file mode 100644 index 000000000..df193a051 --- /dev/null +++ b/src/extension/loader.cpp @@ -0,0 +1,103 @@ +/* + * Loader for external plug-ins. + * + * Authors: + * Moritz Eberl + * + * Copyright (C) 2016 Authors + * + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include "loader.h" +#include "system.h" +#include +#include +#include "dependency.h" + +namespace Inkscape { +namespace Extension { + +typedef Implementation::Implementation *(*_getImplementation)(void); + +bool Loader::LoadDependency(Dependency *dep) +{ + GModule *module = NULL; + module = g_module_open(dep->getName(), (GModuleFlags)0); + if (module == NULL) { + return false; + } + return true; +} + +/** + * @brief Load the actual implementation of a plugin supplied by the plugin. + * @param doc The xml representation of the INX extension configuration. + * @return The implementation of the extension loaded from the plugin. + */ +Implementation::Implementation *Loader::LoadImplementation(Inkscape::XML::Document *doc) +{ + try { + Inkscape::XML::Node *repr = doc->root(); + Inkscape::XML::Node *child_repr = repr->firstChild(); + while (child_repr != NULL) { + char const *chname = child_repr->name(); + if (!strncmp(chname, INKSCAPE_EXTENSION_NS_NC, strlen(INKSCAPE_EXTENSION_NS_NC))) { + chname += strlen(INKSCAPE_EXTENSION_NS); + } + + if (!strcmp(chname, "dependency")) { + Dependency dep = Dependency(child_repr); + bool success = LoadDependency(&dep); + if( !success ){ + const char *res = g_module_error(); + g_warning("Unable to load dependency %s of plugin %s.\nDetails: %s\n", dep.getName(), res); + return NULL; + } + } + + if (!strcmp(chname, "plugin")) { + if (const gchar *name = child_repr->attribute("name")) { + GModule *module = NULL; + _getImplementation GetImplementation = NULL; + gchar *path = g_build_filename(_baseDirectory, name, (char *) NULL); + module = g_module_open(path, G_MODULE_BIND_LOCAL); + g_free(path); + if (module == NULL) { + const char *res = g_module_error(); + g_warning("Unable to load extension %s.\nDetails: %s\n", name, res); + return NULL; + } + + if (g_module_symbol(module, "GetImplementation", (gpointer *) &GetImplementation) == FALSE) { + const char *res = g_module_error(); + g_warning("Unable to load extension %s.\nDetails: %s\n", name, res); + return NULL; + } + + Implementation::Implementation *i = GetImplementation(); + return i; + } + } + + child_repr = child_repr->next(); + } + } catch (std::exception &e) { + g_warning("Unable to load extension."); + } + return NULL; +} + +} // namespace Extension +} // namespace Inkscape + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace .0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99: \ No newline at end of file -- cgit v1.2.3 From d2a9d82cd79c61f44e2224dee38847d4113c7eda Mon Sep 17 00:00:00 2001 From: Moritz Eberl Date: Tue, 12 Apr 2016 11:43:56 +0200 Subject: fixed naming of methods. External extensions can now be other module types. (bzr r14761.1.2) --- src/extension/loader.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/extension/loader.cpp') diff --git a/src/extension/loader.cpp b/src/extension/loader.cpp index df193a051..8893f3f1d 100644 --- a/src/extension/loader.cpp +++ b/src/extension/loader.cpp @@ -20,10 +20,10 @@ namespace Extension { typedef Implementation::Implementation *(*_getImplementation)(void); -bool Loader::LoadDependency(Dependency *dep) +bool Loader::load_dependency(Dependency *dep) { GModule *module = NULL; - module = g_module_open(dep->getName(), (GModuleFlags)0); + module = g_module_open(dep->get_name(), (GModuleFlags)0); if (module == NULL) { return false; } @@ -35,7 +35,7 @@ bool Loader::LoadDependency(Dependency *dep) * @param doc The xml representation of the INX extension configuration. * @return The implementation of the extension loaded from the plugin. */ -Implementation::Implementation *Loader::LoadImplementation(Inkscape::XML::Document *doc) +Implementation::Implementation *Loader::load_implementation(Inkscape::XML::Document *doc) { try { Inkscape::XML::Node *repr = doc->root(); @@ -48,10 +48,10 @@ Implementation::Implementation *Loader::LoadImplementation(Inkscape::XML::Docume if (!strcmp(chname, "dependency")) { Dependency dep = Dependency(child_repr); - bool success = LoadDependency(&dep); + bool success = load_dependency(&dep); if( !success ){ const char *res = g_module_error(); - g_warning("Unable to load dependency %s of plugin %s.\nDetails: %s\n", dep.getName(), res); + g_warning("Unable to load dependency %s of plugin %s.\nDetails: %s\n", dep.get_name(), res); return NULL; } } -- cgit v1.2.3 From fd70a7878e170d18f91e06a28b48e1a63daf216b Mon Sep 17 00:00:00 2001 From: Moritz Eberl Date: Tue, 12 Apr 2016 17:23:50 +0200 Subject: Modified the windows build to integrate gmodule-2.0 and loader.cpp/.h Added method to check inscape version used to build plugins. (bzr r14761.1.3) --- src/extension/loader.cpp | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/extension/loader.cpp') diff --git a/src/extension/loader.cpp b/src/extension/loader.cpp index 8893f3f1d..32ac4b3b4 100644 --- a/src/extension/loader.cpp +++ b/src/extension/loader.cpp @@ -14,11 +14,13 @@ #include #include #include "dependency.h" +#include "inkscape-version.h" namespace Inkscape { namespace Extension { typedef Implementation::Implementation *(*_getImplementation)(void); +typedef const gchar *(*_getInkscapeVersion)(void); bool Loader::load_dependency(Dependency *dep) { @@ -38,43 +40,75 @@ bool Loader::load_dependency(Dependency *dep) Implementation::Implementation *Loader::load_implementation(Inkscape::XML::Document *doc) { try { + Inkscape::XML::Node *repr = doc->root(); Inkscape::XML::Node *child_repr = repr->firstChild(); + + // Iterate over the xml content while (child_repr != NULL) { char const *chname = child_repr->name(); if (!strncmp(chname, INKSCAPE_EXTENSION_NS_NC, strlen(INKSCAPE_EXTENSION_NS_NC))) { chname += strlen(INKSCAPE_EXTENSION_NS); } - + + // Deal with dependencies if we have them if (!strcmp(chname, "dependency")) { Dependency dep = Dependency(child_repr); + // try to load it bool success = load_dependency(&dep); if( !success ){ + // Could not load dependency, we abort const char *res = g_module_error(); g_warning("Unable to load dependency %s of plugin %s.\nDetails: %s\n", dep.get_name(), res); return NULL; } } + // Found a plugin to load if (!strcmp(chname, "plugin")) { + + // The name of the plugin is actually the library file we want to load if (const gchar *name = child_repr->attribute("name")) { GModule *module = NULL; _getImplementation GetImplementation = NULL; + _getInkscapeVersion GetInkscapeVersion = NULL; + + // build the path where to look for the plugin gchar *path = g_build_filename(_baseDirectory, name, (char *) NULL); module = g_module_open(path, G_MODULE_BIND_LOCAL); g_free(path); + if (module == NULL) { + // we were not able to load the plugin, write warning and abort const char *res = g_module_error(); g_warning("Unable to load extension %s.\nDetails: %s\n", name, res); return NULL; } + // Get a handle to the version function of the module + if (g_module_symbol(module, "GetInkscapeVersion", (gpointer *) &GetInkscapeVersion) == FALSE) { + // This didn't work, write warning and abort + const char *res = g_module_error(); + g_warning("Unable to load extension %s.\nDetails: %s\n", name, res); + return NULL; + } + + // Get a handle to the function that delivers the implementation if (g_module_symbol(module, "GetImplementation", (gpointer *) &GetImplementation) == FALSE) { + // This didn't work, write warning and abort const char *res = g_module_error(); g_warning("Unable to load extension %s.\nDetails: %s\n", name, res); return NULL; } - + + // Get version and test against this version + const gchar* version = GetInkscapeVersion(); + if( strcmp(version, version_string) != 0) { + // The versions are different, display warning. + g_warning("Plugin was built against Inkscape version %s, this is %s. The plugin might not be compatible.", version, version_string); + } + + Implementation::Implementation *i = GetImplementation(); return i; } -- cgit v1.2.3 From e71c798f2fd8610baaddbb58d103b2ef44d65ccb Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Wed, 20 Apr 2016 23:14:10 -0700 Subject: minor warning cleanup. (bzr r14861) --- src/extension/loader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/extension/loader.cpp') diff --git a/src/extension/loader.cpp b/src/extension/loader.cpp index 32ac4b3b4..110df3ae3 100644 --- a/src/extension/loader.cpp +++ b/src/extension/loader.cpp @@ -59,7 +59,7 @@ Implementation::Implementation *Loader::load_implementation(Inkscape::XML::Docum if( !success ){ // Could not load dependency, we abort const char *res = g_module_error(); - g_warning("Unable to load dependency %s of plugin %s.\nDetails: %s\n", dep.get_name(), res); + g_warning("Unable to load dependency %s of plugin %s.\nDetails: %s\n", dep.get_name(), "", res); return NULL; } } @@ -134,4 +134,4 @@ Implementation::Implementation *Loader::load_implementation(Inkscape::XML::Docum fill-column:99 End: */ -// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99: \ No newline at end of file +// vim:filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99: -- cgit v1.2.3 From b8136b3cdbb75028e9eb815e69b735695453d70f Mon Sep 17 00:00:00 2001 From: Moritz Eberl Date: Wed, 27 Apr 2016 09:29:21 +0200 Subject: Fixed path resolution in plugin loader. (bzr r14862.1.1) --- src/extension/loader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/extension/loader.cpp') diff --git a/src/extension/loader.cpp b/src/extension/loader.cpp index 110df3ae3..863a176ca 100644 --- a/src/extension/loader.cpp +++ b/src/extension/loader.cpp @@ -74,7 +74,7 @@ Implementation::Implementation *Loader::load_implementation(Inkscape::XML::Docum _getInkscapeVersion GetInkscapeVersion = NULL; // build the path where to look for the plugin - gchar *path = g_build_filename(_baseDirectory, name, (char *) NULL); + gchar *path = g_build_filename(_baseDirectory.c_str(), name, (char *) NULL); module = g_module_open(path, G_MODULE_BIND_LOCAL); g_free(path); -- cgit v1.2.3