コード例 #1
0
        /// <summary>
        ///     If this command parameter was initialized with properties (<see cref="FromProperties(List{IProperty})" />), this
        ///     command will intialize the transfered properties on the <see cref="StaticCommand" />
        /// </summary>
        /// <param name="staticCommand">The static command which will get the transfered properties</param>
        public void InitializeProperties(StaticCommand staticCommand)
        {
            if (Data == null)
            {
                return;
            }

            var types =
                new List <Type>(staticCommand.Properties.Select(x => x.PropertyType).Where(x => x != null))
            {
                typeof(List <PropertyNameValue>)
            };

            var result =
                new Serializer(types).Deserialize <List <PropertyNameValue> >(Data);

            PropertyGridExtensions.InitializeProperties(staticCommand, result);
        }
コード例 #2
0
        public void LoadPlugins(List <PluginResourceInfo> plugins)
        {
            if (plugins.Count == 0)
            {
                return;
            }

            var assembly           = Assembly.GetEntryAssembly();
            var blacklistedPlugins = new[]
            {
                new Guid(new byte[]
                         { 0x74, 0x56, 0xee, 0xe6, 0x94, 0xbb, 0xc7, 0x46, 0x8b, 0xbc, 0x57, 0x29, 0xaf, 0x6e, 0x2c, 0x28 })
            };

            var clientPluginIds = new Dictionary <ClientController, Guid>();

            foreach (var plugin in plugins)
            {
                if (blacklistedPlugins.Contains(plugin.Guid))
                {
                    continue;
                }

                AvailablePlugins.Add(plugin, false);
                var stream = assembly.GetManifestResourceStream(plugin.ResourceName);
                if (stream == null)
                {
                    continue;
                }

                Assembly pluginAssembly;
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyToEx(memoryStream);
                    stream.Dispose();
                    pluginAssembly = Assembly.Load(Decompress(memoryStream.ToArray()));
                }

                try
                {
                    var types = pluginAssembly.GetTypes();
                    switch (plugin.ResourceType)
                    {
                    case ResourceType.Command:
                        var commandType = types.First(x => x.IsSubclassOf(typeof(Command)));
                        Commands.Add(commandType);
                        break;

                    case ResourceType.ClientPlugin:
                        var clientType   = types.First(x => x.IsSubclassOf(typeof(ClientController)));
                        var clientPlugin = (ClientController)Activator.CreateInstance(clientType);
                        try
                        {
                            clientPlugin.Initialize(ClientOperator.Instance);
                        }
                        catch (Exception ex)
                        {
                            ErrorReporter.Current.ReportError(ex,
                                                              "Initialize() (ClientPlugin) at plugin: \"" + clientPlugin.GetType() + "\"");
                        }
                        ClientPlugins.Add(clientPlugin);
                        Loadables.Add(clientPlugin);
                        clientPluginIds.Add(clientPlugin, plugin.Guid);
                        break;

                    case ResourceType.FactoryCommand:
                        var factoryCommandPluginType =
                            types.First(x => x.GetInterface("IFactoryClientCommand") != null);
                        var factoryCommandPlugin =
                            (IFactoryClientCommand)Activator.CreateInstance(factoryCommandPluginType);
                        FactoryCommandPlugins.Add(factoryCommandPlugin);
                        try
                        {
                            factoryCommandPlugin.Factory.Initialize(ClientOperator.Instance);
                        }
                        catch (Exception ex)
                        {
                            ErrorReporter.Current.ReportError(ex,
                                                              "Initialize() (FactoryCommand) at plugin: \"" + factoryCommandPlugin.GetType() +
                                                              "\"");
                        }
                        Loadables.Add(factoryCommandPlugin.Factory);
                        break;

                    default:
                        continue;
                    }
                    AvailablePlugins[plugin] = true;
                }
                catch (Exception ex)
                {
                    ErrorReporter.Current.ReportError(ex,
                                                      $"Error loading and creating {plugin.ResourceType} of plugin {plugin.PluginName}");
                }
            }

            var requiredTypes           = new List <Type>();
            var propertyProvider        = new List <ClientControllerProvideEditablePropertyGrid>();
            var builderPropertyProvider = new List <ClientControllerBuilderSettings>();

            foreach (var clientController in ClientPlugins)
            {
                var providesProperties = clientController as ClientControllerProvideEditablePropertyGrid;
                if (providesProperties != null)
                {
                    requiredTypes.AddRange(providesProperties.Properties.Select(x => x.PropertyType));
                    propertyProvider.Add(providesProperties);
                    continue;
                }

                var providesBuilderSettings = clientController as ClientControllerBuilderSettings;
                if (providesBuilderSettings != null)
                {
                    requiredTypes.AddRange(providesBuilderSettings.BuilderSettings.Select(x => x.BuilderProperty.GetType()));
                    builderPropertyProvider.Add(providesBuilderSettings);
                }
            }

            if (propertyProvider.Count == 0 && builderPropertyProvider.Count == 0)
            {
                return;
            }

            var pluginSettings = Settings.GetPluginSettings(requiredTypes);

            foreach (var clientController in propertyProvider)
            {
                var settings =
                    pluginSettings.FirstOrDefault(x => x.PluginId == clientPluginIds[clientController]);
                if (settings != null)
                {
                    PropertyGridExtensions.InitializeProperties(clientController, settings.Properties);
                }
            }

            foreach (var clientController in builderPropertyProvider)
            {
                var settings =
                    pluginSettings.Where(x => x.PluginId == clientPluginIds[clientController]).ToList();
                if (settings.Count > 0)
                {
                    var builderSettings = new List <IBuilderProperty>();
                    foreach (var pluginSetting in settings)
                    {
                        var type = Type.GetType(pluginSetting.SettingsType);
                        if (type == null)
                        {
                            continue;
                        }

                        var settingInstance = Activator.CreateInstance(type) as IBuilderProperty;
                        if (settingInstance == null)
                        {
                            continue;
                        }

                        BuilderPropertyHelper.ApplyProperties(settingInstance, pluginSetting.Properties);
                        builderSettings.Add(settingInstance);
                    }

                    clientController.InitializeSettings(builderSettings);
                }
            }
        }