mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
refactor Core.Plugin.PluginsLoader (continued)
This commit is contained in:
parent
35cf51f82f
commit
0d9cfd65a2
1 changed files with 19 additions and 36 deletions
|
|
@ -21,7 +21,7 @@ namespace Flow.Launcher.Core.Plugin
|
||||||
|
|
||||||
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
|
public static List<PluginPair> Plugins(List<PluginMetadata> metadatas, PluginsSettings settings)
|
||||||
{
|
{
|
||||||
var dotnetPlugins = DotNetPlugins(metadatas).ToList();
|
var dotnetPlugins = DotNetPlugins(metadatas);
|
||||||
var pythonPlugins = PythonPlugins(metadatas, settings.PythonDirectory);
|
var pythonPlugins = PythonPlugins(metadatas, settings.PythonDirectory);
|
||||||
var executablePlugins = ExecutablePlugins(metadatas);
|
var executablePlugins = ExecutablePlugins(metadatas);
|
||||||
var plugins = dotnetPlugins.Concat(pythonPlugins).Concat(executablePlugins).ToList();
|
var plugins = dotnetPlugins.Concat(pythonPlugins).Concat(executablePlugins).ToList();
|
||||||
|
|
@ -46,75 +46,58 @@ namespace Flow.Launcher.Core.Plugin
|
||||||
var type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
var type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
||||||
var plugin = (IPlugin)Activator.CreateInstance(type);
|
var plugin = (IPlugin)Activator.CreateInstance(type);
|
||||||
#else
|
#else
|
||||||
Assembly assembly;
|
Assembly assembly = null;
|
||||||
|
IPlugin plugin = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
|
assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(metadata.ExecuteFilePath);
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
erroredPlugins.Add(metadata.Name);
|
|
||||||
|
|
||||||
Log.Exception($"|PluginsLoader.DotNetPlugins|Couldn't load assembly for the plugin: {metadata.Name}", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Type type;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var types = assembly.GetTypes();
|
var types = assembly.GetTypes();
|
||||||
|
var type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
||||||
type = types.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IPlugin)));
|
|
||||||
|
plugin = (IPlugin)Activator.CreateInstance(type);
|
||||||
|
}
|
||||||
|
catch (Exception e) when (assembly == null)
|
||||||
|
{
|
||||||
|
Log.Exception($"|PluginsLoader.DotNetPlugins|Couldn't load assembly for the plugin: {metadata.Name}", e);
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException e)
|
catch (InvalidOperationException e)
|
||||||
{
|
{
|
||||||
erroredPlugins.Add(metadata.Name);
|
|
||||||
|
|
||||||
Log.Exception($"|PluginsLoader.DotNetPlugins|Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
|
Log.Exception($"|PluginsLoader.DotNetPlugins|Can't find the required IPlugin interface for the plugin: <{metadata.Name}>", e);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
catch (ReflectionTypeLoadException e)
|
catch (ReflectionTypeLoadException e)
|
||||||
{
|
{
|
||||||
erroredPlugins.Add(metadata.Name);
|
|
||||||
|
|
||||||
Log.Exception($"|PluginsLoader.DotNetPlugins|The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
|
Log.Exception($"|PluginsLoader.DotNetPlugins|The GetTypes method was unable to load assembly types for the plugin: <{metadata.Name}>", e);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
IPlugin plugin;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
plugin = (IPlugin)Activator.CreateInstance(type);
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
erroredPlugins.Add(metadata.Name);
|
|
||||||
|
|
||||||
Log.Exception($"|PluginsLoader.DotNetPlugins|The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
|
Log.Exception($"|PluginsLoader.DotNetPlugins|The following plugin has errored and can not be loaded: <{metadata.Name}>", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
erroredPlugins.Add(metadata.Name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
PluginPair pair = new PluginPair
|
plugins.Add(new PluginPair
|
||||||
{
|
{
|
||||||
Plugin = plugin,
|
Plugin = plugin,
|
||||||
Metadata = metadata
|
Metadata = metadata
|
||||||
};
|
});
|
||||||
plugins.Add(pair);
|
|
||||||
});
|
});
|
||||||
metadata.InitTime += milliseconds;
|
metadata.InitTime += milliseconds;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (erroredPlugins.Count > 0)
|
if (erroredPlugins.Count > 0)
|
||||||
{
|
{
|
||||||
var errorPluginString = "";
|
var errorPluginString = String.Join(Environment.NewLine, erroredPlugins);
|
||||||
|
|
||||||
var errorMessage = "The following "
|
var errorMessage = "The following "
|
||||||
+ (erroredPlugins.Count > 1 ? "plugins have " : "plugin has ")
|
+ (erroredPlugins.Count > 1 ? "plugins have " : "plugin has ")
|
||||||
+ "errored and cannot be loaded:";
|
+ "errored and cannot be loaded:";
|
||||||
|
|
||||||
erroredPlugins.ForEach(x => errorPluginString += x + Environment.NewLine);
|
|
||||||
|
|
||||||
Task.Run(() =>
|
Task.Run(() =>
|
||||||
{
|
{
|
||||||
MessageBox.Show($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
|
MessageBox.Show($"{errorMessage}{Environment.NewLine}{Environment.NewLine}" +
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue