public void Proccess(XmlElement element, AmmunitionLibraryEntry target)
            {
                var attribute = element.Attributes[Attribute];

                if (attribute == null)
                {
                    if (!Optional)
                    {
                        throw new InvalidOperationException($"The element {element.Name} does not have the attribute {Attribute}");
                    }
                    return;
                }

                if (TryParse != null && mTargetProperty != null)
                {
                    string text = attribute.Value, text1 = null;
                    if (AdditionalAttribute != null)
                    {
                        var additionalAttribute = element.Attributes[AdditionalAttribute];
                        if (additionalAttribute != null)
                        {
                            text1 = additionalAttribute.Value;
                        }
                    }

                    object value = TryParse(text, text1);
                    if (value == null)
                    {
                        throw new InvalidOperationException($"The value {text} of the attribute {Attribute} could not be parsed");
                    }

                    object targetObject = mTargetLeftSide.Invoke(target);
                    mTargetProperty.SetValue(targetObject, value);
                }
            }
        public void RoundTrip_AmmuntionLibraryEntry()
        {
            AmmunitionLibraryEntry entry = new AmmunitionLibraryEntry()
            {
                Name           = "Entry name",
                Source         = "Entry source",
                Caliber        = "Entry caliber",
                AmmunitionType = "BTHP",
                BarrelLength   = new Measurement <DistanceUnit>(410, DistanceUnit.Millimeter),
                Ammunition     = new Ammunition()
                {
                    BallisticCoefficient = new BallisticCoefficient(0.295, DragTableId.G1),
                    Weight         = new Measurement <WeightUnit>(9.1, WeightUnit.Gram),
                    MuzzleVelocity = new Measurement <VelocityUnit>(2956.5, VelocityUnit.FeetPerSecond),
                    BulletDiameter = new Measurement <DistanceUnit>(0.224, DistanceUnit.Inch),
                    BulletLength   = new Measurement <DistanceUnit>(0.98, DistanceUnit.Inch)
                }
            };

            SerializerRoundtrip serializer = new SerializerRoundtrip();

            var node = serializer.Serialize(entry);

            node.Should().NotBeNull();

            var entry1 = serializer.Deserialize <AmmunitionLibraryEntry>(node);

            entry1.Should().NotBeNull();

            entry1.Name.Should().Be(entry.Name);
            entry1.Source.Should().Be(entry.Source);
            entry1.Caliber.Should().Be(entry.Caliber);
            entry1.AmmunitionType.Should().Be(entry.AmmunitionType);
            entry1.BarrelLength.Should().Be(entry.BarrelLength);

            entry1.Ammunition.BallisticCoefficient.Should().Be(entry.Ammunition.BallisticCoefficient);
            entry1.Ammunition.Weight.Should().Be(entry.Ammunition.Weight);
            entry1.Ammunition.MuzzleVelocity.Should().Be(entry.Ammunition.MuzzleVelocity);
            entry1.Ammunition.BulletDiameter.Should().Be(entry.Ammunition.BulletDiameter);
            entry1.Ammunition.BulletLength.Should().Be(entry.Ammunition.BulletLength);
        }
예제 #3
0
        /// <summary>
        /// Reads legacy ammunition info from the XML node
        /// </summary>
        /// <param name="legacyEntry"></param>
        /// <returns></returns>
        public static AmmunitionLibraryEntry ReadLegacyAmmunitionLibraryEntry(XmlElement legacyEntry)
        {
            if (legacyEntry == null)
            {
                throw new ArgumentNullException(nameof(legacyEntry));
            }

            var entry = new AmmunitionLibraryEntry()
            {
                Ammunition = new Ammunition()
            };

            if (legacyEntry.Attributes["table"] == null)
            {
                throw new ArgumentException("The element must have table attribute", nameof(legacyEntry));
            }

            if (!Enum.TryParse <DragTableId>(legacyEntry.Attributes["table"].Value, out DragTableId table))
            {
                throw new ArgumentException("Unknown table identifier", nameof(legacyEntry));
            }

            if (legacyEntry.Attributes["bc"] == null)
            {
                throw new ArgumentException("The element must have bc attribute", nameof(legacyEntry));
            }

            if (!double.TryParse(legacyEntry.Attributes["bc"].Value, NumberStyles.Float, CultureInfo.InvariantCulture, out double bc))
            {
                throw new ArgumentException("Ballistic coefficient is not a number", nameof(legacyEntry));
            }

            entry.Ammunition.BallisticCoefficient = new BallisticCoefficient(bc, table);

            if (legacyEntry.Attributes["bullet-weight"] == null)
            {
                throw new ArgumentException("The element must have bullet-weight attribute", nameof(legacyEntry));
            }

            if (!Measurement <WeightUnit> .TryParse(CultureInfo.InvariantCulture, legacyEntry.Attributes["bullet-weight"].Value, out Measurement <WeightUnit> weight))
            {
                throw new ArgumentException("Can't parse bullet weight", nameof(legacyEntry));
            }

            entry.Ammunition.Weight = weight;

            if (legacyEntry.Attributes["muzzle-velocity"] == null)
            {
                throw new ArgumentException("The element must have muzzle-velocity attribute", nameof(legacyEntry));
            }

            if (!Measurement <VelocityUnit> .TryParse(CultureInfo.InvariantCulture, legacyEntry.Attributes["muzzle-velocity"].Value, out Measurement <VelocityUnit> muzzleVelocity))
            {
                throw new ArgumentException("Can't parse muzzle velocity", nameof(legacyEntry));
            }

            entry.Ammunition.MuzzleVelocity = muzzleVelocity;

            if (legacyEntry.Attributes["bullet-length"] != null)
            {
                if (Measurement <DistanceUnit> .TryParse(CultureInfo.InvariantCulture, legacyEntry.Attributes["bullet-length"].Value, out Measurement <DistanceUnit> bulletLength))
                {
                    entry.Ammunition.BulletLength = bulletLength;
                }
            }

            if (legacyEntry.Attributes["bullet-diameter"] != null)
            {
                if (Measurement <DistanceUnit> .TryParse(CultureInfo.InvariantCulture, legacyEntry.Attributes["bullet-diameter"].Value, out Measurement <DistanceUnit> bulletDiameter))
                {
                    entry.Ammunition.BulletDiameter = bulletDiameter;
                }
            }

            if (legacyEntry.Attributes["name"] == null)
            {
                throw new ArgumentException("The element must have name attribute", nameof(legacyEntry));
            }

            entry.Name = legacyEntry.Attributes["name"].Value;

            if (legacyEntry.Attributes["barrel-length"] != null)
            {
                if (Measurement <DistanceUnit> .TryParse(CultureInfo.InvariantCulture, legacyEntry.Attributes["barrel-length"].Value, out Measurement <DistanceUnit> bulletLength))
                {
                    entry.BarrelLength = bulletLength;
                }
            }

            if (legacyEntry.Attributes["source"] != null)
            {
                entry.Source = legacyEntry.Attributes["source"].Value;
            }

            if (legacyEntry.Attributes["caliber"] != null)
            {
                entry.Caliber = legacyEntry.Attributes["caliber"].Value;
            }

            if (legacyEntry.Attributes["bullet-type"] != null)
            {
                entry.AmmunitionType = legacyEntry.Attributes["bullet-type"].Value;
            }

            return(entry);
        }