<commit_before>﻿#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Tabster.Core.Plugins;

#endregion

namespace Tabster
{
    public class TabsterPlugin
    {
        public Assembly Assembly { get; private set; }
        public ITabsterPlugin Interface { get; private set; }

        private Guid _guid;

        public Guid GUID
        {
            get
            {
                if (_guid == Guid.Empty)
                {
                    var attributes = Assembly.GetCustomAttributes(typeof(GuidAttribute), false);

                    if (attributes.Length > 0)
                    {
                        _guid = new Guid(((GuidAttribute)attributes[0]).Value);
                       
                    }
                }

                return _guid;
            }
        }

        public TabsterPlugin(Assembly assembly, ITabsterPlugin pluginInterface)
        {
            Interface = pluginInterface;
            Assembly = assembly;
        }

        public IEnumerable<T> GetClassInstances<T>()
        {
            var instances = new List<T>();

            var cType = typeof (T);

            foreach (var type in Interface.Types)
            {
                if (cType.IsAssignableFrom(type))
                {
                    var instance = (T)Activator.CreateInstance(type);
                    instances.Add(instance);
                }
            }

            return instances;
        }
    }

    public class PluginController : IEnumerable<TabsterPlugin>
    {
        private readonly string _pluginsDirectory;
        private readonly List<TabsterPlugin> _plugins = new List<TabsterPlugin>();
        private List<Guid> _disabledPlugins = new List<Guid>();

        public PluginController(string pluginsDirectory)
        {
            _pluginsDirectory = pluginsDirectory;
        }

        public void LoadPlugins()
        {
            if (!Directory.Exists(_pluginsDirectory))
            {
                try
                {
                    Directory.CreateDirectory(_pluginsDirectory);
                }

                catch
                {
                    //unhandled
                }
            }

            if (Directory.Exists(_pluginsDirectory))
            {
                var pluginFiles = Directory.GetFiles(_pluginsDirectory, "*.dll");

                foreach (var pluginPath in pluginFiles)
                {
                    LoadPluginFromDisk(pluginPath);
                }
            }
        }

        public IEnumerable<T> GetClassInstances<T>()
        {
            var instances = new List<T>();

            //todo cache instances?
            foreach(var plugin in _plugins)
            {
                if (IsEnabled(plugin.GUID))
                instances.AddRange(plugin.GetClassInstances<T>());
            }

            return instances;
        }

        private void LoadPluginFromDisk(string path)
        {
            try
            {
                var assembly = Assembly.LoadFrom(path);

                if (assembly != null)
                {
                    Type pluginType = null;

                    foreach (var objType in assembly.GetTypes())
                    {
                        if (typeof(ITabsterPlugin).IsAssignableFrom(objType))
                        {
                            pluginType = objType;
                            break;
                        }
                    }

                    if (pluginType != null)
                    {
                        var plguinInterface = (ITabsterPlugin) Activator.CreateInstance(pluginType);

                        var plugin = new TabsterPlugin(assembly, plguinInterface);<commit_msg>Make _disabledPlugins readonly.<commit_after>﻿#region

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Tabster.Core.Plugins;

#endregion

namespace Tabster
{
    public class TabsterPlugin
    {
        public Assembly Assembly { get; private set; }
        public ITabsterPlugin Interface { get; private set; }

        private Guid _guid;

        public Guid GUID
        {
            get
            {
                if (_guid == Guid.Empty)
                {
                    var attributes = Assembly.GetCustomAttributes(typeof(GuidAttribute), false);

                    if (attributes.Length > 0)
                    {
                        _guid = new Guid(((GuidAttribute)attributes[0]).Value);
                       
                    }
                }

                return _guid;
            }
        }

        public TabsterPlugin(Assembly assembly, ITabsterPlugin pluginInterface)
        {
            Interface = pluginInterface;
            Assembly = assembly;
        }

        public IEnumerable<T> GetClassInstances<T>()
        {
            var instances = new List<T>();

            var cType = typeof (T);

            foreach (var type in Interface.Types)
            {
                if (cType.IsAssignableFrom(type))
                {
                    var instance = (T)Activator.CreateInstance(type);
                    instances.Add(instance);
                }
            }

            return instances;
        }
    }

    public class PluginController : IEnumerable<TabsterPlugin>
    {
        private readonly string _pluginsDirectory;
        private readonly List<TabsterPlugin> _plugins = new List<TabsterPlugin>();
        private readonly List<Guid> _disabledPlugins = new List<Guid>();

        public PluginController(string pluginsDirectory)
        {
            _pluginsDirectory = pluginsDirectory;
        }

        public void LoadPlugins()
        {
            if (!Directory.Exists(_pluginsDirectory))
            {
                try
                {
                    Directory.CreateDirectory(_pluginsDirectory);
                }

                catch
                {
                    //unhandled
                }
            }

            if (Directory.Exists(_pluginsDirectory))
            {
                var pluginFiles = Directory.GetFiles(_pluginsDirectory, "*.dll");

                foreach (var pluginPath in pluginFiles)
                {
                    LoadPluginFromDisk(pluginPath);
                }
            }
        }

        public IEnumerable<T> GetClassInstances<T>()
        {
            var instances = new List<T>();

            //todo cache instances?
            foreach(var plugin in _plugins)
            {
                if (IsEnabled(plugin.GUID))
                instances.AddRange(plugin.GetClassInstances<T>());
            }

            return instances;
        }

        public void LoadPluginFromDisk(string path)
        {
            try
            {
                var assembly = Assembly.LoadFrom(path);

                if (assembly != null)
                {
                    Type pluginType = null;

                    foreach (var objType in assembly.GetTypes())
                    {
                        if (typeof(ITabsterPlugin).IsAssignableFrom(objType))
                        {
                            pluginType = objType;
                            break;
                        }
                    }

                    if (pluginType != null)
                    {
                        var plguinInterface = (ITabsterPlugin) Activator.CreateInstance(pluginType);

                        var plugin = new TabsterPlugin(assembly, plguinInterface);
{'fraction_non_alphanumeric': 0.07392438070404173, 'fraction_numerical': 0.0005215123859191656, 'mean_word_length': 1.2087532392744025, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '939027', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1662, 'n_words': 378}