Ejemplo n.º 1
0
        /// <summary>
        /// OnLoad handler for the form. Init the UI and populate
        /// the datagridview
        /// </summary>
        /// <param name="sender">event sender</param>
        /// <param name="eventArgs">eventargs</param>
        private void OnLoad(object sender, EventArgs eventArgs)
        {
            if (Actuator == null)
            {
                MessageBox.Show("Error.  Actuator to configure is null");
                Close();
            }

            float currentAspectRatio = (float)ClientSize.Height / ClientSize.Width;

            if (_designTimeAspectRatio != 0.0f && currentAspectRatio != _designTimeAspectRatio)
            {
                ClientSize = new System.Drawing.Size(ClientSize.Width, (int)(_designTimeAspectRatio * ClientSize.Width));
            }

            TopMost = false;
            TopMost = true;

            if (!String.IsNullOrEmpty(Title))
            {
                Text = Title;
            }
            else
            {
                Text = Text + " - " + Actuator.Name;
            }

            initializeUI();

            _actuatorConfig = ActuatorConfig.Load();

            _actuatorSetting = _actuatorConfig.Find(Actuator.Descriptor.Id);

            refreshDataGridView();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads actuator settigns from the settings file.
        /// Walks through the extensions dirs, looks for actuators in there
        /// and caches the Types of the actuators.
        /// Configures the actuators with the settings from the settings file.
        /// Also if any acutators were discovered that are not in the settings file,
        /// adds them to the settings file and saves it.
        /// </summary>
        /// <param name="extensionDirs">directories to walk through</param>
        /// <param name="configFile">name of the actuators settings file</param>
        /// <param name="loadAll">whether to load even the disabled actuators</param>
        /// <returns>true on success</returns>
        public bool Load(IEnumerable <String> extensionDirs, String configFile, bool loadAll = false)
        {
            addKeyboardActuatorToCache();

            foreach (string dir in extensionDirs)
            {
                String extensionDir = dir + "\\" + ActuatorManager.ActuatorsRootDir;
                loadActuatorTypesIntoCache(extensionDir);
            }

            if (!File.Exists(configFile))
            {
                return(false);
            }

            ActuatorConfig.ActuatorSettingsFileName = configFile;
            Config = ActuatorConfig.Load();

            // walk through the settings file create and configure
            // actuators
            foreach (var actuatorSetting in Config.ActuatorSettings)
            {
                try
                {
                    bool enabled = (loadAll) || actuatorSetting.Enabled;
                    if (enabled && (actuatorSetting.Id != Guid.Empty))
                    {
                        if (!_actuatorsTypeCache.ContainsKey(actuatorSetting.Id))
                        {
                            continue;
                        }

                        var type = _actuatorsTypeCache[actuatorSetting.Id];
                        if (type != null)
                        {
                            var assembly = Assembly.LoadFrom(type.Assembly.Location);
                            var actuator = (IActuator)assembly.CreateInstance(type.FullName);
                            if (actuator != null)
                            {
                                actuator.OnRegisterSwitches();
                                actuator.Load(actuatorSetting.SwitchSettings);
                                actuator.Enabled = actuatorSetting.Enabled;
                                var actuatorEx = new ActuatorEx(actuator);
                                _actuatorsEx.Add(actuatorEx);
                                _actuators.Add(actuator);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Exception(ex);
                }
            }

            // now go through all the actuators that are not in the
            // settings file and add them to the settings file

            bool isDirty = false;

            foreach (var actuatorType in _actuatorsTypeCache.Values)
            {
                var attr = DescriptorAttribute.GetDescriptor(actuatorType);
                if (attr != null && attr.Id != Guid.Empty)
                {
                    var actuatorSetting = Config.Find(attr.Id);
                    if (actuatorSetting != null)
                    {
                        continue;
                    }

                    try
                    {
                        var assembly = Assembly.LoadFrom(actuatorType.Assembly.Location);
                        var actuator = (IActuator)assembly.CreateInstance(actuatorType.FullName);
                        if (actuator != null)
                        {
                            var actuatorEx = new ActuatorEx(actuator);
                            _actuatorsEx.Add(actuatorEx);
                            _actuators.Add(actuator);

                            actuatorSetting = new ActuatorSetting(attr.Name, attr.Id);
                            Config.ActuatorSettings.Add(actuatorSetting);

                            actuator.OnRegisterSwitches();
                            actuator.Load(actuatorSetting.SwitchSettings);

                            isDirty = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                }
            }

            if (isDirty)
            {
                Config.Save();
            }

            return(true);
        }