mirror of
https://github.com/greenshot/greenshot
synced 2025-07-30 19:50:11 -07:00
Introduced a very simple "singleton" service-locator, which allowed for a removal of specific implementations which were very limited. With this it's easier to access dependencies.
This commit is contained in:
parent
3ebdf3d2fe
commit
80d8f51fc5
53 changed files with 744 additions and 1230 deletions
|
@ -31,108 +31,35 @@ namespace Greenshot.Helpers {
|
|||
/// </summary>
|
||||
public static class ProcessorHelper {
|
||||
private static readonly ILog LOG = LogManager.GetLogger(typeof(ProcessorHelper));
|
||||
private static readonly Dictionary<string, IProcessor> RegisteredProcessors = new Dictionary<string, IProcessor>();
|
||||
|
||||
/// Initialize the Processors
|
||||
static ProcessorHelper() {
|
||||
foreach(Type ProcessorType in InterfaceUtils.GetSubclassesOf(typeof(IProcessor),true)) {
|
||||
/// <summary>
|
||||
/// Register the internal processors
|
||||
/// </summary>
|
||||
public static void RegisterInternalProcessors() {
|
||||
foreach(Type processorType in InterfaceUtils.GetSubclassesOf(typeof(IProcessor),true)) {
|
||||
// Only take our own
|
||||
if (!"Greenshot.Processors".Equals(ProcessorType.Namespace)) {
|
||||
if (!"Greenshot.Processors".Equals(processorType.Namespace)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (!ProcessorType.IsAbstract) {
|
||||
IProcessor Processor;
|
||||
if (!processorType.IsAbstract) {
|
||||
IProcessor processor;
|
||||
try {
|
||||
Processor = (IProcessor)Activator.CreateInstance(ProcessorType);
|
||||
processor = (IProcessor)Activator.CreateInstance(processorType);
|
||||
} catch (Exception e) {
|
||||
LOG.ErrorFormat("Can't create instance of {0}", ProcessorType);
|
||||
LOG.ErrorFormat("Can't create instance of {0}", processorType);
|
||||
LOG.Error(e);
|
||||
continue;
|
||||
}
|
||||
if (Processor.isActive) {
|
||||
LOG.DebugFormat("Found Processor {0} with designation {1}", ProcessorType.Name, Processor.Designation);
|
||||
RegisterProcessor(Processor);
|
||||
if (processor.isActive) {
|
||||
LOG.DebugFormat("Found Processor {0} with designation {1}", processorType.Name, processor.Designation);
|
||||
SimpleServiceProvider.Current.AddService(processor);
|
||||
} else {
|
||||
LOG.DebugFormat("Ignoring Processor {0} with designation {1}", ProcessorType.Name, Processor.Designation);
|
||||
LOG.DebugFormat("Ignoring Processor {0} with designation {1}", processorType.Name, processor.Designation);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.ErrorFormat("Error loading processor {0}, message: ", ProcessorType.FullName, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register your Processor here, if it doesn't come from a plugin and needs to be available
|
||||
/// </summary>
|
||||
/// <param name="Processor"></param>
|
||||
public static void RegisterProcessor(IProcessor Processor) {
|
||||
// don't test the key, an exception should happen wenn it's not unique
|
||||
RegisteredProcessors.Add(Processor.Designation, Processor);
|
||||
}
|
||||
|
||||
private static List<IProcessor> GetPluginsProcessors() {
|
||||
List<IProcessor> processors = new List<IProcessor>();
|
||||
foreach (PluginAttribute pluginAttribute in PluginHelper.Instance.Plugins.Keys) {
|
||||
IGreenshotPlugin plugin = PluginHelper.Instance.Plugins[pluginAttribute];
|
||||
try {
|
||||
var procs = plugin.Processors();
|
||||
if (procs != null) {
|
||||
processors.AddRange(procs);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
LOG.ErrorFormat("Couldn't get processors from the plugin {0}", pluginAttribute.Name);
|
||||
LOG.Error(ex);
|
||||
}
|
||||
}
|
||||
processors.Sort();
|
||||
return processors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a list of all Processors, registered or supplied by a plugin
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<IProcessor> GetAllProcessors() {
|
||||
List<IProcessor> processors = new List<IProcessor>();
|
||||
processors.AddRange(RegisteredProcessors.Values);
|
||||
processors.AddRange(GetPluginsProcessors());
|
||||
processors.Sort();
|
||||
return processors;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a Processor by a designation
|
||||
/// </summary>
|
||||
/// <param name="designation">Designation of the Processor</param>
|
||||
/// <returns>IProcessor or null</returns>
|
||||
public static IProcessor GetProcessor(string designation) {
|
||||
if (designation == null) {
|
||||
return null;
|
||||
}
|
||||
if (RegisteredProcessors.ContainsKey(designation)) {
|
||||
return RegisteredProcessors[designation];
|
||||
}
|
||||
foreach (IProcessor processor in GetPluginsProcessors()) {
|
||||
if (designation.Equals(processor.Designation)) {
|
||||
return processor;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A simple helper method which will call ProcessCapture for the Processor with the specified designation
|
||||
/// </summary>
|
||||
/// <param name="designation"></param>
|
||||
/// <param name="surface"></param>
|
||||
/// <param name="captureDetails"></param>
|
||||
public static void ProcessCapture(string designation, ISurface surface, ICaptureDetails captureDetails) {
|
||||
if (RegisteredProcessors.ContainsKey(designation)) {
|
||||
IProcessor Processor = RegisteredProcessors[designation];
|
||||
if (Processor.isActive) {
|
||||
Processor.ProcessCapture(surface, captureDetails);
|
||||
LOG.ErrorFormat("Error loading processor {0}, message: ", processorType.FullName, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue