Update plugin interface

now is notified during a file load event (can handle itself)
This commit is contained in:
Kurt
2018-05-14 17:30:56 -07:00
parent 663ec5c536
commit 78c4dbd69c
3 changed files with 40 additions and 5 deletions

View File

@@ -2,9 +2,32 @@
{
public interface IPlugin
{
/// <summary>
/// Plugin Name
/// </summary>
string Name { get; }
/// <summary>
/// Entrypoint for the parent to initialize the plugin with provided arguments.
/// </summary>
/// <param name="args">Arguments containing objects useful for initializing the plugin.</param>
void Initialize(params object[] args);
/// <summary>
/// Notifies the plugin that a save file was just loaded.
/// </summary>
void NotifySaveLoaded();
/// <summary>
/// Attempts to load a file using the plugin.
/// </summary>
/// <param name="filePath">Path to file to be loaded.</param>
/// <returns></returns>
bool TryLoadFile(string filePath);
/// <summary>
/// Retrieves the <see cref="ISaveFileProvider"/> object which can provide a <see cref="SaveFile"/>.
/// </summary>
ISaveFileProvider SaveFileEditor { get; }
}
}

View File

@@ -456,6 +456,9 @@ private void OpenQuick(string path, bool force = false)
SystemSounds.Asterisk.Play();
return;
}
if (Plugins.Any(p => p.TryLoadFile(path)))
return; // handled by plugin
// detect if it is a folder (load into boxes or not)
if (Directory.Exists(path))
{ C_SAV.LoadBoxes(out string _, path); return; }

View File

@@ -26,13 +26,22 @@ private static IEnumerable<Assembly> GetAssemblies(IEnumerable<string> dllFileNa
private static IEnumerable<Type> GetPluginsOfType<T>(IEnumerable<Assembly> assemblies)
{
var pluginType = typeof(T);
foreach (var type in assemblies.Where(z => z != null).SelectMany(a => a.GetTypes()))
foreach (var z in assemblies.Where(z => z != null))
{
if (type.IsInterface || type.IsAbstract)
Type[] types; try { types = z.GetTypes(); }
catch (Exception ex)
{
Console.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message);
continue;
if (type.GetInterface(pluginType.FullName) == null)
continue;
yield return type;
}
foreach (Type type in types)
{
if (type.IsInterface || type.IsAbstract)
continue;
if (type.GetInterface(pluginType.FullName) == null)
continue;
yield return type;
}
}
}
}