Exemple #1
0
        public bool Parse(out XmlErrorCollection errorCollection, out SchemeGenerator schemeGenerator)
        {
            if (_xDoc == null)
            {
                throw new ArgumentNullException("XML document is not valid.");
            }

            //Store a reference to a new scheme generator and error collector for ease of use later on.
            //This object is not responsible for their lifetime once Parse() has finished!
            _schemeGenerator = new SchemeGenerator();
            _errorCollection = new XmlErrorCollection();

            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = _xDoc.Elements();

            foreach (XElement element in elements)
            {
                //Handle scheme element.
                if (element.Name.LocalName == ElementTypes.Metascheme.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Metascheme))
                    {
                        foundElements.Add(ElementTypes.Metascheme, element);
                        ParseMetaschemeElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }

            //If we didn't get the scheme element, we didn't do anything just now!
            if (!foundElements.Contains(ElementTypes.Metascheme))
            {
                _errorCollection.AddElementNotFound(ElementTypes.Metascheme.ToString());
            }

            //Set the out variables and lose our stored references to them.
            schemeGenerator  = _schemeGenerator;
            _schemeGenerator = null;

            errorCollection  = _errorCollection;
            _errorCollection = null;

            return(errorCollection.Errors.Count == 0);
        }
Exemple #2
0
        void ParseExtendedOptionsElement(XElement extendedOptionsElement)
        {
            //Get version.
            XAttribute versionAttribute = extendedOptionsElement.Attribute("version");

            if (versionAttribute != null)
            {
                if (TryParseIntegerAttribute(extendedOptionsElement, versionAttribute, 0, SchemeTypes.MaximumExtendedOptionDataVersion, out int value))
                {
                    _schemeGenerator.ExtendedOptionsDataVersion = value;
                }
                else
                {
                    _errorCollection.AddAttributeValueNonNumber(extendedOptionsElement, versionAttribute);
                }
            }
            else
            {
                _errorCollection.AddAttributeNotFound("version", extendedOptionsElement);
            }

            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = extendedOptionsElement.Elements();

            foreach (XElement element in elements)
            {
                //Handle setting element.
                ExtendedOptionTypes extendedOptionType;
                if (Enum.TryParse <ExtendedOptionTypes>(element.Name.LocalName, out extendedOptionType))
                {
                    if (extendedOptionType != ExtendedOptionTypes.DataVersion && extendedOptionType != ExtendedOptionTypes.Count)
                    {
                        if (!foundElements.Contains(extendedOptionType))
                        {
                            foundElements.Add(extendedOptionType, element);

                            ParseExtendedOptionElement(element, extendedOptionType);
                        }
                        else
                        {
                            _errorCollection.AddRepeatedElement(element, foundElements.Get(extendedOptionType));
                        }
                    }
                    else
                    {
                        _errorCollection.AddInvalidElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }
        }
Exemple #3
0
        void ParseWeaponElement(XElement weaponElement, WeaponTypes weaponType)
        {
            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = weaponElement.Elements();

            foreach (XElement element in elements)
            {
                //Handle weapon setting element.
                WeaponSettings weaponSetting;

                if (Enum.TryParse <WeaponSettings>(element.Name.LocalName, out weaponSetting))
                {
                    if (weaponSetting != WeaponSettings.Count)
                    {
                        if (!foundElements.Contains(weaponSetting))
                        {
                            foundElements.Add(weaponSetting, element);

                            if (SchemeTypes.CanApplyWeaponSetting(weaponType, weaponSetting))
                            {
                                ParseWeaponSettingElement(element, weaponType, weaponSetting);
                            }
                            else
                            {
                                _errorCollection.AddSettingNotApplicableToWeapon(element, weaponType, weaponSetting);
                            }
                        }
                        else
                        {
                            _errorCollection.AddRepeatedElement(element, foundElements.Get(weaponSetting));
                        }
                    }
                    else
                    {
                        _errorCollection.AddInvalidElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }
        }
Exemple #4
0
        void ParseWeaponsElement(XElement weaponsElement)
        {
            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = weaponsElement.Elements();

            foreach (XElement element in elements)
            {
                //Handle weapon element.
                WeaponTypes weapon;

                if (Enum.TryParse <WeaponTypes>(element.Name.LocalName, out weapon))
                {
                    if (weapon != WeaponTypes.Count)
                    {
                        if (!foundElements.Contains(weapon))
                        {
                            foundElements.Add(weapon, element);

                            ParseWeaponElement(element, weapon);
                        }
                        else
                        {
                            _errorCollection.AddRepeatedElement(element, foundElements.Get(weapon));
                        }
                    }
                    else
                    {
                        _errorCollection.AddInvalidElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }
        }
Exemple #5
0
        void ParseSettingsElement(XElement settingsElement)
        {
            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = settingsElement.Elements();

            foreach (XElement element in elements)
            {
                //Handle setting element.
                SettingTypes settingType;

                if (Enum.TryParse <SettingTypes>(element.Name.LocalName, out settingType))
                {
                    if (settingType != SettingTypes.Version && settingType != SettingTypes.BountyMode && settingType != SettingTypes.Count)
                    {
                        if (!foundElements.Contains(settingType))
                        {
                            foundElements.Add(settingType, element);

                            ParseSettingElement(element, settingType);
                        }
                        else
                        {
                            _errorCollection.AddRepeatedElement(element, foundElements.Get(settingType));
                        }
                    }
                    else
                    {
                        _errorCollection.AddInvalidElement(element);
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }
        }
Exemple #6
0
        void ParseMetaschemeElement(XElement metaschemeElement)
        {
            FoundElements foundElements = new FoundElements();

            //Iterate through all elements.
            IEnumerable <XElement> elements = metaschemeElement.Elements();

            foreach (XElement element in elements)
            {
                //TODO: Can I generalise these calls?
                //Handle settings element.
                if (element.Name.LocalName == ElementTypes.Information.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Information))
                    {
                        foundElements.Add(ElementTypes.Information, element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.Information));
                    }
                }
                //Handle settings element.
                else if (element.Name.LocalName == ElementTypes.Settings.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Settings))
                    {
                        foundElements.Add(ElementTypes.Settings, element);
                        ParseSettingsElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.Settings));
                    }
                }
                //Handle weapons element.
                else if (element.Name.LocalName == ElementTypes.Weapons.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Weapons))
                    {
                        foundElements.Add(ElementTypes.Weapons, element);
                        ParseWeaponsElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.Weapons));
                    }
                }
                //Handle extended options element.
                else if (element.Name.LocalName == ElementTypes.ExtendedOptions.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.ExtendedOptions))
                    {
                        foundElements.Add(ElementTypes.ExtendedOptions, element);
                        ParseExtendedOptionsElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.ExtendedOptions));
                    }
                }
                //Handle guarantees element.
                else if (element.Name.LocalName == ElementTypes.Guarantees.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.Guarantees))
                    {
                        foundElements.Add(ElementTypes.Guarantees, element);
                        ParseGuaranteesElement(element);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.Guarantees));
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }
        }
Exemple #7
0
        WeaponSettingGuarantee CreateWeaponSettingGuarantee(XElement weaponSettingGuaranteeElement)
        {
            //Get setting
            XAttribute settingAttribute = weaponSettingGuaranteeElement.Attribute("setting");

            if (settingAttribute == null)
            {
                _errorCollection.AddAttributeNotFound("setting", weaponSettingGuaranteeElement);
                return(null);
            }

            WeaponSettings weaponSetting = WeaponSettings.Count;

            Enum.TryParse <WeaponSettings>(settingAttribute.Value, out weaponSetting);
            if (weaponSetting == WeaponSettings.Count)
            {
                _errorCollection.AddAttributeValueInvalidValue(weaponSettingGuaranteeElement, settingAttribute);
                return(null);
            }

            WeaponSettingGuarantee weaponSettingGuarantee    = new WeaponSettingGuarantee(weaponSetting);
            SettingLimits          weaponCountSettingLimits  = new SettingLimits(0, 64);
            SettingLimits          settingValueSettingLimits = new SettingLimits(0, 255);

            //Iterate through all elements.
            FoundElements foundElements = new FoundElements();

            IEnumerable <XElement> elements = weaponSettingGuaranteeElement.Elements();

            foreach (XElement element in elements)
            {
                //Handle minimum weapons element.
                if (element.Name.LocalName == ElementTypes.MinimumWeaponCount.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.MinimumWeaponCount))
                    {
                        foundElements.Add(ElementTypes.MinimumWeaponCount, element);

                        weaponSettingGuarantee.MinimumWeaponCount = CreateValueGenerator(element, weaponCountSettingLimits);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MinimumWeaponCount));
                    }
                }
                //Handle maximum weapons element.
                else if (element.Name.LocalName == ElementTypes.MaximumWeaponCount.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.MaximumWeaponCount))
                    {
                        foundElements.Add(ElementTypes.MaximumWeaponCount, element);

                        ParseMaximumWeaponCountElement(element, weaponSettingGuarantee, weaponCountSettingLimits);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MaximumWeaponCount));
                    }
                }
                //Handle minimum value element.
                else if (element.Name.LocalName == ElementTypes.MinimumSettingValue.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.MinimumSettingValue))
                    {
                        foundElements.Add(ElementTypes.MinimumSettingValue, element);

                        weaponSettingGuarantee.MinimumSettingValue = CreateValueGenerator(element, settingValueSettingLimits);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MinimumSettingValue));
                    }
                }
                //Handle maximum weapons element.
                else if (element.Name.LocalName == ElementTypes.MaximumSettingValue.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.MaximumSettingValue))
                    {
                        foundElements.Add(ElementTypes.MaximumSettingValue, element);

                        weaponSettingGuarantee.MaximumSettingValue = CreateValueGenerator(element, settingValueSettingLimits);
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.MaximumSettingValue));
                    }
                }
                //Handle inclusion filter element.
                else if (element.Name.LocalName == ElementTypes.InclusionFilter.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.InclusionFilter))
                    {
                        foundElements.Add(ElementTypes.InclusionFilter, element);

                        ParseWeaponFilterElement(element, ref weaponSettingGuarantee.GetCategoryInclusionFilterRef(), ref weaponSettingGuarantee.GetInclusionMatchTypeRef());
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.InclusionFilter));
                    }
                }
                //Handle inclusion filter element.
                else if (element.Name.LocalName == ElementTypes.ExclusionFilter.ToString())
                {
                    if (!foundElements.Contains(ElementTypes.ExclusionFilter))
                    {
                        foundElements.Add(ElementTypes.ExclusionFilter, element);

                        ParseWeaponFilterElement(element, ref weaponSettingGuarantee.GetCategoryExclusionFilterRef(), ref weaponSettingGuarantee.GetExclusionMatchTypeRef());
                    }
                    else
                    {
                        _errorCollection.AddRepeatedElement(element, foundElements.Get(ElementTypes.ExclusionFilter));
                    }
                }
                //Invalid element.
                else
                {
                    _errorCollection.AddInvalidElement(element);
                }
            }

            return(weaponSettingGuarantee);
        }