Ejemplo n.º 1
0
        /// <summary>
        ///     Reads a config file
        /// </summary>
        /// <param name="filePath">The config file path</param>
        /// <param name="configData">
        ///     The config data to match.
        ///     This is a dictionary containing groupnames and configItems
        /// </param>
        /// <param name="readAll">If true this even reads excessive data which do not exist within configGroups</param>
        /// <returns></returns>
        private Dictionary <string, Dictionary <string, ConfigParseItem> > ReadConfig(string filePath, Dictionary <string, Dictionary <string, ConfigParseItem> > configData, bool readAll = false)
        {
            StreamReader config = new StreamReader(filePath);

            ConfigGroup currentGroup = null;

            string line;

            while ((line = config.ReadLine()) != null)
            {
                line = line.Trim();

                //Identifies what type of line we're dealing with. ConfigGroup or ConfigItem
                ConfigLine configLine = IdentifyLine(line);

                if (configLine == null)
                {
                    continue;
                }

                if (configLine.GroupName != null)
                {
                    currentGroup = new ConfigGroup(configLine.GroupName, filePath);
                    continue;
                }

                //Check if this is a new value and if it should be saved to the given configData
                if (
                    currentGroup != null &&
                    configLine.ItemName != null &&
                    configLine.ItemValue != null &&
                    configData.ContainsKey(currentGroup.GroupName) &&
                    configData[currentGroup.GroupName].ContainsKey(configLine.ItemName) &&
                    configData[currentGroup.GroupName][configLine.ItemName].ConfigDefault != configLine.ItemValue
                    )
                {
                    configData[currentGroup.GroupName][configLine.ItemName].NewValue = configLine.ItemValue;
                }
                else if (readAll)
                {
                    if (currentGroup != null && configData.ContainsKey(currentGroup.GroupName))
                    {
                        if (!configData[currentGroup.GroupName].ContainsKey(configLine.ItemName))
                        {
                            configData[currentGroup.GroupName].Add(configLine.ItemName, new ConfigParseItem
                            {
                                ConfigName = configLine.ItemName,
                                NewValue   = configLine.ItemValue
                            });
                        }
                    }
                    else
                    {
                        if (currentGroup != null)
                        {
                            configData.Add(currentGroup.GroupName, new Dictionary <string, ConfigParseItem>
                            {
                                {
                                    configLine.ItemName, new ConfigParseItem
                                    {
                                        ConfigName = configLine.ItemName,
                                        NewValue   = configLine.ItemValue
                                    }
                                }
                            });
                        }
                    }
                }
            }

            config.Close();

            return(configData);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Parses all config classes for file conversion
        /// </summary>
        /// <param name="assembly">The assembly to parse from</param>
        private Dictionary <string, Dictionary <string, Dictionary <string, ConfigParseItem> > > ParseConfig(Assembly assembly = null)
        {
            Dictionary <string, Dictionary <string, Dictionary <string, ConfigParseItem> > > parseDict = new Dictionary <string, Dictionary <string, Dictionary <string, ConfigParseItem> > >();

            foreach (Type type in GenericHelpers.GetTypesWithAttribute(typeof(ConfigGroup), assembly))
            {
                if (!type.IsAbstract && !type.IsSealed)
                {
                    throw new ForgeCritical(Language.ForgeCriticalInvalidConfigGroupClassType + type.Name);
                }

                ConfigGroup config = type.GetCustomAttributes(typeof(ConfigGroup), true).FirstOrDefault() as ConfigGroup;

                foreach (PropertyInfo propertyInfo in type.GetProperties())
                {
                    //Turn all config items into ParseItems
                    foreach (ConfigItem configItem in propertyInfo.GetCustomAttributes(typeof(ConfigItem), true))
                    {
                        ConfigParseItem parseItem = new ConfigParseItem
                        {
                            ConfigDefault = configItem.DefaultValue,
                            ConfigName    = configItem.Name,
                            Property      = propertyInfo,
                        };

                        if (parseItem.ConfigName == null)
                        {
                            parseItem.ConfigName = parseItem.Property.Name;
                        }

                        if (config != null && parseDict.ContainsKey(config.FileName))
                        {
                            if (parseDict[config.FileName].ContainsKey(config.GroupName))
                            {
                                parseDict[config.FileName][config.GroupName].Add(parseItem.ConfigName, parseItem);
                            }
                            else
                            {
                                parseDict[config.FileName].Add(config.GroupName, new Dictionary <string, ConfigParseItem> {
                                    { parseItem.ConfigName, parseItem }
                                });
                            }
                        }
                        else
                        {
                            if (config != null)
                            {
                                parseDict.Add(
                                    config.FileName,
                                    new Dictionary <string, Dictionary <string, ConfigParseItem> >
                                {
                                    {
                                        config.GroupName,
                                        new Dictionary <string, ConfigParseItem> {
                                            { parseItem.ConfigName, parseItem }
                                        }
                                    }
                                }
                                    );
                            }
                        }
                    }
                }
            }

            return(parseDict);
        }