Example #1
0
        public void Test_SectionConfigTarget_IsAlwaysLowerCase(string target, string expected)
        {
            var configSection = new ConfigSections {
                Target = target
            };

            Assert.AreEqual(expected, configSection.Target);
        }
Example #2
0
        public void AddConfigSection(IConfigSection configSection)
        {
            if (FindConfigSection(configSection) != null)
            {
                throw new ArgumentException("Can not add a section that already exists: " + configSection.SectionName);
            }

            ConfigSections.Add(configSection);
        }
Example #3
0
        public IConfigSection FindOrCreateConfigSection(string name)
        {
            var result = FindConfigSection(name);
            if (result == null)
            {
                result = new ConfigSection(name, true);
                ConfigSections.Add(result);
            }

            return result;
        }
Example #4
0
        public void RemoveConfigSection(string configSectionName)
        {
            var configSection = FindConfigSection(configSectionName);

            if (configSection == null)
            {
                return;
            }

            ConfigSections.Remove(configSection);
        }
        public static void Startup(string startupParameter)
        {
            CurrentConfigPath = startupParameter;

            // If no custom configuration path is specified
            if (string.IsNullOrWhiteSpace(CurrentConfigPath))
            {
                // Set current configuration directory/path as default
                CurrentConfigDirectory = DefaultConfigDirectory;
                CurrentConfigPath      = DefaultConfigPath;
            }
            else
            {
                // Set current configuration directory based on given path
                CurrentConfigDirectory = Path.GetDirectoryName(CurrentConfigPath);
            }

            // Clear config sections
            ConfigSections.Clear();

            // Get all types in the executing assembly (Configuration Tool)
            Type[] allTypes = Assembly.GetExecutingAssembly().GetTypes();

            foreach (Type type in allTypes)
            {
                // Filter to parent namespace "Configuration_Tool.Configuration.Sections"
                if (type.Namespace.StartsWith("Configuration_Tool.Configuration.Sections"))
                {
                    // Filter to types with child interface IConfig
                    if (type.GetInterfaces().Contains(typeof(IConfig)))
                    {
                        // Create instance
                        IConfig instance = Activator.CreateInstance(type) as IConfig;

                        // Add to list
                        ConfigSections.Add(instance);
                    }
                }
            }

            // Sort in order of integer value of enumerator 'Type'
            ConfigSections.Sort((x, y) => (x.Type.CompareTo(y.Type)));

            // Cache default configuration
            CacheDefaults();

            // Load configuration
            loadConfig();
        }
        // Returns true if an error occured
        private static bool loadSections(BinaryReader reader, MainWindow mainWindow)
        {
            bool error = false;

            // Get number of config sections
            int count = reader.ReadInt32();

            // For every config section
            for (int i = 0; i < count; i++)
            {
                // Get config section type
                EConfigType type = (EConfigType)reader.ReadInt32();

                // Get length of data for specific section
                int bufferLength = reader.ReadInt32();

                // Read buffer for section to isolate from others
                byte[] buffer = reader.ReadBytes(bufferLength);

                // Find section for loading
                IConfig config = ConfigSections.FirstOrDefault(x => x.Type == type);

                // Possibly removed section, skip it
                if (config == null)
                {
                    continue;
                }

                // Create isolated binary reader for section loading
                using (MemoryStream bufferStream = new MemoryStream(buffer))
                    using (BinaryReader sectionReader = new BinaryReader(bufferStream))
                    {
                        // Set main window
                        config.MainWindow = mainWindow;

                        try
                        {
                            // Load config
                            config.Load(sectionReader);
                        }
                        catch { error = true; }
                    }
            }

            return(error);
        }
        static ConfigurationManager()
        {
            var streamInfo = Application.GetResourceStream(new Uri("Silverlight.config", UriKind.RelativeOrAbsolute));

            if (streamInfo == null || streamInfo.Stream == null)
            {
                return;
            }

            Sections  = new Dictionary <string, object>();
            XDocument = XDocument.Load(streamInfo.Stream);
            if (XDocument.Root == null || XDocument.Root.Name != "configuration")
            {
                throw new ConfigurationErrorsException(
                          "The configuration file must contain a root element called <configuration>.");
            }

            if (XDocument.Root.FirstNode == null)
            {
                throw new ConfigurationErrorsException(
                          "The <configuration> node must contain a <configSections> node as its first child.");
            }

            XElement configSections = (XElement)XDocument.Root.FirstNode;

            if (configSections.Name != "configSections")
            {
                throw new ConfigurationErrorsException(
                          "The first element under <configuration> must be <configSections>.");
            }

            XmlSerializer serializer = new XmlSerializer(typeof(ConfigSections));

            ConfigSections = (ConfigSections)serializer.Deserialize(configSections.CreateReader());

            HasConfiguration = true;
        }
Example #8
0
 public void RemoveConfigSections(string configSectionName)
 {
     var toRemove = GetConfigSections(configSectionName).ToArray();
     toRemove.ForEach(section => ConfigSections.Remove(section));
 }
Example #9
0
 public IEnumerable<IConfigSection> GetConfigSections(string sectionName)
 {
     return ConfigSections.Where(section => section.SectionName.Equals(sectionName, StringComparison.OrdinalIgnoreCase));
 }
Example #10
0
        public void ParseData()
        {
            string       lineIterator;
            StreamReader Reader    = new StreamReader(this.FullPath);
            Exception    exception = new Exception("Неизвестное количество параметров.");

            while (!Reader.EndOfStream)
            {
                lineIterator = Reader.ReadLine();
                List <string> paramsBuf;

                if (ExcludedSynbols.IndexOf(lineIterator) >= 0 || lineIterator.IndexOf('#') > -1)
                {
                    continue;
                }

                if (SectionNames.IndexOf(lineIterator) >= 0)
                {
                    ConfigSections.Add(new ConfigSection(lineIterator));
                    continue;
                }

                paramsBuf = new List <string>(lineIterator.Split(','));

                if (paramsBuf.Count < 2)
                {
                    continue;
                }

                paramsBuf = CleanParams(paramsBuf);
                var configRows = ConfigSections.Last().ConfigRows;

                switch (ConfigSections.Last().Name)
                {
                case PATH.SectionName:
                    lineIterator = Reader.ReadLine();

                    List <PATHNode> parsedNodes    = new List <PATHNode>();
                    List <string>   nodesParamsBuf = new List <string>(lineIterator.Split(','));
                    nodesParamsBuf = CleanParams(nodesParamsBuf);

                    switch (paramsBuf.Count)
                    {
                    case 3:
                        while (nodesParamsBuf.Count == 9)
                        {
                            parsedNodes.Add(new PATHNode(
                                                Int32.Parse(nodesParamsBuf[0]),
                                                Int32.Parse(nodesParamsBuf[1]),
                                                Int32.Parse(nodesParamsBuf[2]),
                                                double.Parse(nodesParamsBuf[3]),
                                                double.Parse(nodesParamsBuf[4]),
                                                double.Parse(nodesParamsBuf[5]),
                                                double.Parse(nodesParamsBuf[6]),
                                                Int32.Parse(nodesParamsBuf[7]),
                                                Int32.Parse(nodesParamsBuf[8])
                                                ));

                            if (parsedNodes.Count == 12)
                            {
                                break;
                            }

                            lineIterator   = Reader.ReadLine();
                            nodesParamsBuf = new List <string>(lineIterator.Split(','));
                        }

                        ConfigSections.Last().ConfigRows.Add(new PATHType1(
                                                                 paramsBuf[0],
                                                                 Int32.Parse(paramsBuf[1]),
                                                                 paramsBuf[2],
                                                                 parsedNodes.ToArray()
                                                                 ));
                        break;

                    case 2:
                        while (nodesParamsBuf.Count == 12)
                        {
                            parsedNodes.Add(new PATHNode(
                                                Int32.Parse(nodesParamsBuf[0]),
                                                Int32.Parse(nodesParamsBuf[1]),
                                                Int32.Parse(nodesParamsBuf[2]),
                                                double.Parse(nodesParamsBuf[3]),
                                                double.Parse(nodesParamsBuf[4]),
                                                double.Parse(nodesParamsBuf[5]),
                                                Int32.Parse(nodesParamsBuf[7]),
                                                Int32.Parse(nodesParamsBuf[8]),
                                                double.Parse(nodesParamsBuf[6])
                                                ));

                            if (parsedNodes.Count == 12)
                            {
                                break;
                            }

                            lineIterator   = Reader.ReadLine();
                            nodesParamsBuf = new List <string>(lineIterator.Split(','));
                        }

                        ConfigSections.Last().ConfigRows.Add(new PATHType2(
                                                                 paramsBuf[0],
                                                                 Int32.Parse(paramsBuf[1]),
                                                                 parsedNodes.ToArray()
                                                                 ));
                        break;

                    default:
                        throw exception;
                    }
                    break;

                case INST.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 11:
                        configRows.Add(new INSTType3().Parse(paramsBuf));
                        break;

                    case 12:
                        configRows.Add(new INSTType1().Parse(paramsBuf));
                        break;

                    case 13:
                        configRows.Add(new INSTType2().Parse(paramsBuf));
                        break;
                    }
                    break;

                case AUZO.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 7:
                        configRows.Add(new AUZOType2().Parse(paramsBuf));
                        break;

                    case 9:
                        configRows.Add(new AUZOType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case CARS.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 12:
                        configRows.Add(new CARSType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case CULL.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 11:
                        if (Int32.TryParse(paramsBuf[3], out int _))
                        {
                            configRows.Add(new CULLType2().Parse(paramsBuf));
                        }
                        else
                        {
                            configRows.Add(new CULLType1().Parse(paramsBuf));
                        }
                        break;

                    case 13:
                        configRows.Add(new CULLType3().Parse(paramsBuf));
                        break;
                    }
                    break;

                case ENEX.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 18:
                        configRows.Add(new ENEXType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case GRGE.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 11:
                        configRows.Add(new GRGEType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case JUMP.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 16:
                        configRows.Add(new JUMPType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case OCCL.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 7:
                        configRows.Add(new OCCLType1().Parse(paramsBuf));
                        break;

                    case 9:
                        configRows.Add(new OCCLType2().Parse(paramsBuf));
                        break;
                    }
                    break;

                case PICK.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 4:
                        configRows.Add(new PICKType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case TCYC.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 11:
                        configRows.Add(new TCYCType1().Parse(paramsBuf));
                        break;
                    }
                    break;

                case ZONE.SectionName:
                    switch (paramsBuf.Count)
                    {
                    case 10:
                        configRows.Add(new ZONEType1().Parse(paramsBuf));
                        break;
                    }
                    break;
                }
            }

            Reader.Close();
        }