Example #1
0
        public static IEnumerable<PluginDescriptor> Load()
        {
            var installedPluginSystemNames = PluginFileParser.ParseInstalledPluginsFile(GetInstalledPluginsFilePath());

            var plugins = new List<PluginDescriptor>();

            if (PluginFolder == null)
                throw new ArgumentNullException("pluginFolder");

            foreach (var pluginFolder in PluginFolder.GetDirectories())
            {
                if (installedPluginSystemNames.Count > 0 && installedPluginSystemNames.Contains(pluginFolder.Name) == false)
                {
                    continue;
                }

                var descriptionFilepath = Path.Combine(pluginFolder.FullName, "Description.txt");

                if (File.Exists(descriptionFilepath))
                {
                    var pluginDescriptor = PluginFileParser.ParsePluginDescriptionFile(descriptionFilepath);
                    pluginDescriptor.Name = pluginFolder.Name;
                    pluginDescriptor.PluginPath = Path.GetDirectoryName(descriptionFilepath);
                    plugins.Add(pluginDescriptor);
                }
                else
                {
                    var pluginDescriptor = new PluginDescriptor();
                    pluginDescriptor.Name = pluginFolder.Name;
                    plugins.Add(pluginDescriptor);
                }
            }
            plugins.Sort((firstPair, nextPair) => firstPair.DisplayOrder.CompareTo(nextPair.DisplayOrder));
            CopyToTempPluginFolderDirectory(plugins);
            IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies();

            var pluginList = TempPluginFolder.GetFiles("*.dll", SearchOption.AllDirectories).Select(x => Assembly.LoadFile(x.FullName)).ToList().FindAll(p => assemblies.Contains(p) == false);

            foreach (var plugin in pluginList)
            {
                var requiredAssemblies = plugin.GetReferencedAssemblies();
                var existingAssemblyNames = assemblies.Select(ass => ass.GetName().Name);
                var uniqueAssemblies = requiredAssemblies.Where(ass => requiredAssemblies.Select(ra => ra.Name).Except(existingAssemblyNames).Contains(ass.Name));

                var descriptor = plugins.FirstOrDefault(p => p.Name == plugin.GetName().Name);

                if (descriptor != null)
                {
                    foreach (var uniqueAssembly in uniqueAssemblies)
                    {
                        var dllPath = Path.Combine(descriptor.PluginPath, uniqueAssembly.Name) + ".dll";
                        descriptor.DependentAssemblys.Add(Assembly.LoadFile(dllPath));
                    }
                }

            }

            InitPlugins(pluginList, plugins);

            return plugins.Where(p => p.Plugin != null);
        }
        public static PluginDescriptor ParsePluginDescriptionFile(string filePath)
        {
            var descriptor = new PluginDescriptor();
            var text = File.ReadAllText(filePath);
            if (String.IsNullOrEmpty(text))
                return descriptor;

            var settings = new List<string>();
            using (var reader = new StringReader(text))
            {
                string str;
                while ((str = reader.ReadLine()) != null)
                {
                    if (String.IsNullOrWhiteSpace(str))
                        continue;
                    settings.Add(str.Trim());
                }
            }

            foreach (string setting in settings)
            {
                var separatorIndex = setting.IndexOf(':');
                if (separatorIndex == -1)
                {
                    continue;
                }
                var key = setting.Substring(0, separatorIndex).Trim();
                var value = setting.Substring(separatorIndex + 1).Trim();

                switch (key)
                {
                    case "Group":
                        descriptor.Group = value;
                        break;
                    case "FriendlyName":
                        descriptor.FriendlyName = value;
                        break;
                    case "Version":
                        descriptor.Version = value;
                        break;
                    case "SupportedVersions":
                    {
                        descriptor.SupportedVersions = value.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
                            .Select(x => x.Trim())
                            .ToList();
                    }
                        break;
                    case "Author":
                        descriptor.Author = value;
                        break;
                    case "DisplayOrder":
                    {
                        int displayOrder;
                        int.TryParse(value, out displayOrder);
                        descriptor.DisplayOrder = displayOrder;
                    }
                        break;
                    case "FileName":
                        descriptor.PluginFileName = value;
                        break;
                    case "LimitedToStores":
                    {
                        foreach (string str1 in value.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries)
                            .Select(x => x.Trim()))
                        {
                            int storeId;
                            if (int.TryParse(str1, out storeId))
                            {
                                descriptor.LimitedToStores.Add(storeId);
                            }
                        }
                    }
                        break;
                    default:
                        break;
                }
            }

            if (descriptor.SupportedVersions.Count == 0)
                descriptor.SupportedVersions.Add("2.00");

            return descriptor;
        }
        public static void SavePluginDescriptionFile(PluginDescriptor plugin)
        {
            if (plugin == null)
                throw new ArgumentException("plugin");

            if (plugin.OriginalAssemblyFile == null)
                throw new Exception(string.Format("Cannot load original assembly path for {0} plugin.", plugin.Name));
            string filePath = Path.Combine(plugin.OriginalAssemblyFile.Directory.FullName, "Description.txt");
            if (!File.Exists(filePath))
                throw new Exception(string.Format("Description file for {0} plugin does not exist. {1}", plugin.Name,
                    filePath));

            var keyValues = new List<KeyValuePair<string, string>>
            {
                new KeyValuePair<string, string>("Group", plugin.Group),
                new KeyValuePair<string, string>("FriendlyName", plugin.FriendlyName),
                new KeyValuePair<string, string>("Version", plugin.Version),
                new KeyValuePair<string, string>("SupportedVersions",
                    string.Join(",", plugin.SupportedVersions)),
                new KeyValuePair<string, string>("Author", plugin.Author),
                new KeyValuePair<string, string>("DisplayOrder", plugin.DisplayOrder.ToString()),
                new KeyValuePair<string, string>("FileName", plugin.PluginFileName)
            };
            if (plugin.LimitedToStores.Count > 0)
            {
                string storeList = "";
                for (int i = 0; i < plugin.LimitedToStores.Count; i++)
                {
                    storeList += plugin.LimitedToStores[i];
                    if (i != plugin.LimitedToStores.Count - 1)
                        storeList += ",";
                }
                keyValues.Add(new KeyValuePair<string, string>("LimitedToStores", storeList));
            }

            var sb = new StringBuilder();
            for (int i = 0; i < keyValues.Count; i++)
            {
                string key = keyValues[i].Key;
                string value = keyValues[i].Value;
                sb.AppendFormat("{0}: {1}", key, value);
                if (i != keyValues.Count - 1)
                    sb.Append(Environment.NewLine);
            }

            File.WriteAllText(filePath, sb.ToString());
        }
Example #4
0
        private static void Initialize(PluginDescriptor pluginDescriptor)
        {
            string key = pluginDescriptor.Plugin.Name;

            if (_plugins.ContainsKey(key)) return;

            pluginDescriptor.Plugin.Initialize();
            _plugins.Add(key, pluginDescriptor);
        }
Example #5
0
        public static void Unload(PluginDescriptor pluginDescriptor)
        {
            pluginDescriptor.Plugin.Unload();

            _plugins.Remove(pluginDescriptor.Plugin.ToString());
        }
        public int CompareTo(PluginDescriptor other)
        {
            if (DisplayOrder != other.DisplayOrder)
                return DisplayOrder.CompareTo(other.DisplayOrder);

            return FriendlyName.CompareTo(other.FriendlyName);
        }