GObject-Plugin/src/loader/PluginLoader.vala

212 lines
5.1 KiB
Vala
Raw Normal View History

2014-09-19 11:04:31 +04:00
/**
* GObject Models.
*/
namespace GObject {
/**
* Modules/Plugins.
*/
namespace Plugins {
/**
* Module.
*/
public class Module : TypeModule {
[CCode (has_target = false)]
private delegate Type PluginInitFunc (TypeModule module);
private GLib.Module module = null;
private string path = null;
private Type type;
/**
* Creates a new Module by specific path.
*
* @param path path to the module.
*/
public Module (string path) {
this.path = path;
}
/**
* Loads the module.
*/
public override bool load () {
2020-04-29 08:50:21 +03:00
module = GLib.Module.open (path, GLib.ModuleFlags.LAZY);
2014-09-19 11:04:31 +04:00
if (null == module) {
2018-06-21 15:31:39 +03:00
stderr.printf("Cannot load module %s\n", path);
2014-09-19 11:04:31 +04:00
return false;
}
void * plugin_init = null;
if (! module.symbol ("plugin_init", out plugin_init)) {
2018-06-21 15:31:39 +03:00
stderr.printf("No such symbol: plugin_init in %s\n", path);
2014-09-19 11:04:31 +04:00
return false;
}
type = ((PluginInitFunc) plugin_init) (this);
return true;
}
/**
* Unloads the module.
*/
public override void unload () {
module = null;
}
/**
* Gets Plugin Type.
*/
public Type get_plugin_type () {
return type;
}
/**
* Creates Plugin instance from the module.
*/
2014-10-16 16:11:28 +04:00
public Plugin create_instance (IHost ihost) {
var p = Object.new (type) as Plugin;
p.host = ihost;
return p;
2014-09-19 11:04:31 +04:00
}
}
void sort_modules (Gee.ArrayList<Module> modules) {
modules.sort ((a, b) => {
var a_name = a.get_plugin_type ().name ();
var b_name = b.get_plugin_type ().name ();
if (a_name < b_name) return -1;
if (a_name > b_name) return 1;
return 0;
});
}
/**
* Loads modules in the specific directory.
*
* @param dir_path path to the directory.
* @param modules where to save list of modules.
*
* @return are the modules loaded correctly or not.
*/
public bool load_modules (string dir_path, ref Gee.ArrayList<Module>? modules) {
modules = new Gee.ArrayList<Module> ();
#if (WINDOWS)
var paths = new Gee.HashSet<string> ();
#endif
try {
var libPath = File.new_for_path (dir_path);
#if (WINDOWS)
for (var i = 0; i < 32; ++i) {
var saved_length = modules.size;
#endif
var lib_enumerator = libPath.enumerate_children (FileAttribute.STANDARD_NAME, 0, null);
FileInfo file_info = null;
while ((file_info = lib_enumerator.next_file (null)) != null) {
if (Regex.match_simple ("^.*\\.(so|dll)$", file_info.get_name ())) {
var path = GLib.Module.build_path (dir_path, file_info.get_name ());
#if (WINDOWS)
if (paths.contains(path)) continue;
#endif
var module = new Module (path);
if (module.load ()) {
modules.add (module);
#if (WINDOWS)
paths.add(path);
#endif
}
}
}
#if (WINDOWS)
if (modules.size == saved_length) break;
}
#endif
} catch (Error e) {
message (e.message);
return false;
}
sort_modules (modules);
return true;
}
2014-09-19 11:04:31 +04:00
/**
* Loads modules in the 2-depth directory tree path.
*
* @param dir_path path to the 2-depth directory tree.
* @param modules where to save list of modules.
*
* @return are the modules loaded correctly or not.
*/
public bool load_modules_depth2 (string dir_path, ref Gee.ArrayList<Module>? modules) {
2014-09-19 11:04:31 +04:00
modules = new Gee.ArrayList<Module> ();
#if (WINDOWS)
var paths = new Gee.HashSet<string> ();
#endif
2014-09-19 11:04:31 +04:00
try {
var libPath = File.new_for_path (dir_path);
#if (WINDOWS)
for (var i = 0; i < 32; ++i) {
var saved_length = modules.size;
#endif
var dir_enumerator = libPath.enumerate_children ("standard::*",
FileQueryInfoFlags.NOFOLLOW_SYMLINKS, null);
FileInfo dir_info = null;
while ((dir_info = dir_enumerator.next_file (null)) != null ) {
if (dir_info.get_file_type () == FileType.DIRECTORY) {
File subdir = libPath.resolve_relative_path (dir_info.get_name ());
var lib_enumerator = subdir.enumerate_children (FileAttribute.STANDARD_NAME, 0, null);
FileInfo file_info = null;
while ((file_info = lib_enumerator.next_file (null)) != null) {
if (Regex.match_simple ("^.*\\.(so|dll)$", file_info.get_name ())) {
var path = Path.build_path (Path.DIR_SEPARATOR_S, dir_path, dir_info.get_name ());
path = GLib.Module.build_path (path, file_info.get_name ());
#if (WINDOWS)
if (paths.contains(path)) continue;
#endif
var module = new Module (path);
if (module.load ()) {
modules.add (module);
#if (WINDOWS)
paths.add(path);
#endif
}
}
2014-09-19 11:04:31 +04:00
}
}
}
#if (WINDOWS)
if (modules.size == saved_length) break;
2014-09-19 11:04:31 +04:00
}
#endif
2014-09-19 11:04:31 +04:00
} catch (Error e) {
message (e.message);
return false;
}
sort_modules (modules);
2014-09-19 11:04:31 +04:00
return true;
}
/**
* Unloads modules.
*
* @param modules list of modules.
*/
public void unload_modules (Gee.ArrayList<Module> modules) {
modules.foreach ((a) => {a.unload (); return true;});
}
}
}