From 7c2023803ebdc83393d6ea56287222a223febc3d Mon Sep 17 00:00:00 2001 From: Marvin W Date: Tue, 29 Aug 2017 21:47:53 +0200 Subject: libdino: try to load all plugins found in the respective folders --- libdino/src/plugin/loader.vala | 75 ++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 36 deletions(-) (limited to 'libdino/src/plugin') diff --git a/libdino/src/plugin/loader.vala b/libdino/src/plugin/loader.vala index d38a0ec5..43876b92 100644 --- a/libdino/src/plugin/loader.vala +++ b/libdino/src/plugin/loader.vala @@ -1,7 +1,6 @@ -namespace Dino.Plugins { +using Gee; -private extern const string SYSTEM_LIBDIR_NAME; -private extern const string SYSTEM_PLUGIN_DIR; +namespace Dino.Plugins { private class Info : Object { public Module module; @@ -15,72 +14,76 @@ private class Info : Object { public class Loader : Object { [CCode (has_target = false)] - private delegate Type RegisterPluginFunction (Module module); + private delegate Type RegisterPluginFunction(Module module); - private string[] search_paths = new string[0]; + private Application app; + private string[] search_paths; private RootInterface[] plugins = new RootInterface[0]; private Info[] infos = new Info[0]; - public Loader(string? exec_str = null) { - if (Environment.get_variable("DINO_PLUGIN_DIR") != null) { - search_paths += Environment.get_variable("DINO_PLUGIN_DIR"); + public Loader(Application app) { + this.app = app; + this.search_paths = app.search_path_generator.get_plugin_paths(); + } + + public void loadAll() { + if (Module.supported() == false) { + throw new Error(-1, 0, "Plugins are not supported"); } - search_paths += Path.build_filename(Environment.get_home_dir(), ".local", "lib", "dino", "plugins"); - string? exec_path = exec_str; - if (exec_path != null) { - if (!exec_path.contains(Path.DIR_SEPARATOR_S)) { - exec_path = Environment.find_program_in_path(exec_str); - } - if (Path.get_dirname(exec_path).contains("dino") || Path.get_dirname(exec_path) == "." || Path.get_dirname(exec_path).contains("build")) { - search_paths += Path.build_filename(Path.get_dirname(exec_path), "plugins"); - } - if (Path.get_basename(Path.get_dirname(exec_path)) == "bin") { - search_paths += Path.build_filename(Path.get_dirname(Path.get_dirname(exec_path)), SYSTEM_LIBDIR_NAME, "dino", "plugins"); + + HashSet plugin_names = new HashSet(); + + foreach (string path in search_paths) { + try { + Dir dir = Dir.open(path, 0); + string? file = null; + while ((file = dir.read_name()) != null) { + if (file.has_suffix(Module.SUFFIX)) plugin_names.add(file); + } + } catch (Error e) { + // Ignore this folder } } - search_paths += SYSTEM_PLUGIN_DIR; - } - public void print_search_paths() { - foreach (string prefix in search_paths) { - print(@"$prefix\n"); + foreach (string plugin in plugin_names) { + load(plugin); } } - public RootInterface load(string name, Dino.Application app) throws Error { - if (Module.supported () == false) { - throw new Error (-1, 0, "Plugins are not supported"); + public RootInterface load(string name) throws Error { + if (Module.supported() == false) { + throw new Error(-1, 0, "Plugins are not supported"); } Module module = null; string path = ""; foreach (string prefix in search_paths) { path = Path.build_filename(prefix, name); - module = Module.open (path, ModuleFlags.BIND_LAZY); + module = Module.open(path, ModuleFlags.BIND_LAZY); if (module != null) break; } if (module == null) { - throw new Error (-1, 1, "%s", Module.error ().replace(path, name)); + throw new Error(-1, 1, "%s", Module.error().replace(path, name)); } void* function; - module.symbol ("register_plugin", out function); + module.symbol("register_plugin", out function); if (function == null) { - throw new Error (-1, 2, "register_plugin () not found"); + throw new Error(-1, 2, "register_plugin () not found"); } RegisterPluginFunction register_plugin = (RegisterPluginFunction) function; - Type type = register_plugin (module); - if (type.is_a (typeof (RootInterface)) == false) { - throw new Error (-1, 3, "Unexpected type"); + Type type = register_plugin(module); + if (type.is_a(typeof(RootInterface)) == false) { + throw new Error(-1, 3, "Unexpected type"); } - Info info = new Plugins.Info (type, (owned) module); + Info info = new Plugins.Info(type, (owned) module); infos += info; RootInterface plugin = (RootInterface) Object.new (type); plugins += plugin; - plugin.registered (app); + plugin.registered(app); return plugin; } -- cgit v1.2.3-54-g00ecf