public static TrapConfig FromXmlElement(XmlElement xmlData) {
      TrapConfig resultingTrapConfig = new TrapConfig();
      resultingTrapConfig.ProjectileType = int.Parse(xmlData["ProjectileType"].InnerText);
      resultingTrapConfig.ProjectileDamage = int.Parse(xmlData["ProjectileDamage"].InnerText);
      resultingTrapConfig.ProjectileSpeed = float.Parse(xmlData["ProjectileSpeed"].InnerText);
      resultingTrapConfig.ProjectileAngle = float.Parse(xmlData["ProjectileAngle"].InnerText);
      resultingTrapConfig.ProjectileAngleVariance = float.Parse(xmlData["ProjectileAngleVariance"].InnerText);
      resultingTrapConfig.ProjectileOffset = float.Parse(xmlData["ProjectileOffset"].InnerText);
      resultingTrapConfig.ProjectileLifeTime = int.Parse(xmlData["ProjectileLifeTime"].InnerText);
      resultingTrapConfig.ProjectileKnockback = float.Parse(xmlData["ProjectileKnockback"].InnerText);
      resultingTrapConfig.Cooldown = int.Parse(xmlData["Cooldown"].InnerText);
      if (xmlData["WirePermission"] != null)
        resultingTrapConfig.WirePermission = xmlData["WirePermission"].InnerText;
      if (xmlData["TriggerPermission"] != null)
        resultingTrapConfig.TriggerPermission = xmlData["TriggerPermission"].InnerText;

      return resultingTrapConfig;
    }
Esempio n. 2
0
        public static TrapConfig FromXmlElement(XmlElement xmlData)
        {
            TrapConfig resultingTrapConfig = new TrapConfig();

            resultingTrapConfig.ProjectileType          = int.Parse(xmlData["ProjectileType"].InnerText);
            resultingTrapConfig.ProjectileDamage        = int.Parse(xmlData["ProjectileDamage"].InnerText);
            resultingTrapConfig.ProjectileSpeed         = float.Parse(xmlData["ProjectileSpeed"].InnerText);
            resultingTrapConfig.ProjectileAngle         = float.Parse(xmlData["ProjectileAngle"].InnerText);
            resultingTrapConfig.ProjectileAngleVariance = float.Parse(xmlData["ProjectileAngleVariance"].InnerText);
            resultingTrapConfig.ProjectileOffset        = float.Parse(xmlData["ProjectileOffset"].InnerText);
            resultingTrapConfig.ProjectileLifeTime      = int.Parse(xmlData["ProjectileLifeTime"].InnerText);
            resultingTrapConfig.ProjectileKnockback     = float.Parse(xmlData["ProjectileKnockback"].InnerText);
            resultingTrapConfig.Cooldown = int.Parse(xmlData["Cooldown"].InnerText);
            if (xmlData["WirePermission"] != null)
            {
                resultingTrapConfig.WirePermission = xmlData["WirePermission"].InnerText;
            }
            if (xmlData["TriggerPermission"] != null)
            {
                resultingTrapConfig.TriggerPermission = xmlData["TriggerPermission"].InnerText;
            }

            return(resultingTrapConfig);
        }
        public static Configuration Read(string filePath)
        {
            XmlReaderSettings configReaderSettings = new XmlReaderSettings {
                ValidationType  = ValidationType.Schema,
                ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings
            };

            string configSchemaPath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + ".xsd");

            configReaderSettings.Schemas.Add(null, configSchemaPath);

            XmlDocument document = new XmlDocument();

            using (XmlReader configReader = XmlReader.Create(filePath, configReaderSettings))
                document.Load(configReader);

            // Before validating using the schema, first check if the configuration file's version matches with the supported version.
            XmlElement rootElement = document.DocumentElement;
            string     fileVersionRaw;

            if (rootElement.HasAttribute("Version"))
            {
                fileVersionRaw = rootElement.GetAttribute("Version");
            }
            else
            {
                fileVersionRaw = "1.0";
            }

            if (fileVersionRaw != Configuration.CurrentVersion)
            {
                throw new FormatException(string.Format(
                                              "The configuration file is either outdated or too new. Expected version was: {0}. File version is: {1}",
                                              Configuration.CurrentVersion, fileVersionRaw
                                              ));
            }

            Configuration resultingConfig = new Configuration(false);

            resultingConfig.OverrideVanillaCircuits = BoolEx.ParseEx(rootElement["OverrideVanillaCircuits"].InnerXml);
            resultingConfig.AdvancedCircuitsEnabled = BoolEx.ParseEx(rootElement["AdvancedCircuitsEnabled"].InnerText);
            resultingConfig.MaxTrapsPerCircuit      = int.Parse(rootElement["MaxTrapsPerCircuit"].InnerText);
            resultingConfig.MaxStatuesPerCircuit    = int.Parse(rootElement["MaxStatuesPerCircuit"].InnerText);
            resultingConfig.MaxPumpsPerCircuit      = int.Parse(rootElement["MaxPumpsPerCircuit"].InnerText);
            resultingConfig.MaxCircuitLength        = int.Parse(rootElement["MaxCircuitLength"].InnerText);
            if (string.IsNullOrWhiteSpace(rootElement["MaxTimerActivityTime"].InnerText))
            {
                resultingConfig.MaxTimerActivityTime = TimeSpan.Zero;
            }
            else
            {
                TimeSpan maxTimerActivityTime;
                if (TimeSpanEx.TryParseShort(rootElement["MaxTimerActivityTime"].InnerText, out maxTimerActivityTime))
                {
                    resultingConfig.MaxTimerActivityTime = maxTimerActivityTime;
                }
            }
            resultingConfig.SignConfig           = SignConfig.FromXmlElement(rootElement["SignConfig"]);
            resultingConfig.BlockActivatorConfig = BlockActivatorConfig.FromXmlElement(rootElement["BlockActivatorConfig"]);

            XmlElement pumpConfigsNode = rootElement["PumpConfigs"];

            foreach (XmlNode pumpConfigNode in pumpConfigsNode.ChildNodes)
            {
                XmlElement pumpConfigElement = (pumpConfigNode as XmlElement);
                if (pumpConfigElement == null)
                {
                    continue;
                }

                PaintColor paintColor = (PaintColor)Enum.Parse(typeof(PaintColor), pumpConfigElement.Attributes["Paint"].Value);
                resultingConfig.PumpConfigs.Add(paintColor, PumpConfig.FromXmlElement(pumpConfigElement));
            }

            XmlElement trapConfigsNode = rootElement["TrapConfigs"];

            foreach (XmlNode trapConfigNode in trapConfigsNode.ChildNodes)
            {
                XmlElement trapConfigElement = (trapConfigNode as XmlElement);
                if (trapConfigElement == null)
                {
                    continue;
                }

                TrapStyle  trapStyle  = (TrapStyle)Enum.Parse(typeof(TrapStyle), trapConfigElement.Attributes["TrapType"].Value);
                PaintColor paintColor = (PaintColor)Enum.Parse(typeof(PaintColor), trapConfigElement.Attributes["Paint"].Value);
                resultingConfig.TrapConfigs.Add(new TrapConfigKey(trapStyle, paintColor), TrapConfig.FromXmlElement(trapConfigElement));
            }

            /*XmlElement explosivesConfigsNode = rootElement["ExplosivesConfigs"];
             * foreach (XmlNode explosivesConfigNode in explosivesConfigsNode.ChildNodes) {
             * XmlElement explosivesConfigElement = (explosivesConfigNode as XmlElement);
             * if (explosivesConfigElement == null)
             *  continue;
             *
             * ComponentConfigProfile componentConfigProfile = (ComponentConfigProfile)Enum.Parse(typeof(ComponentConfigProfile), explosivesConfigElement.Attributes["Profile"].Value);
             * resultingConfig.explosivesConfigs.Add(componentConfigProfile, ExplosivesConfig.FromXmlElement(explosivesConfigElement));
             * }*/

            XmlElement wirelessTransmitterConfigsNode = rootElement["WirelessTransmitterConfigs"];

            foreach (XmlNode wirelessTransmitterConfigNode in wirelessTransmitterConfigsNode.ChildNodes)
            {
                XmlElement wirelessTransmitterConfigElement = (wirelessTransmitterConfigNode as XmlElement);
                if (wirelessTransmitterConfigElement == null)
                {
                    continue;
                }

                PaintColor paintColor = (PaintColor)Enum.Parse(typeof(PaintColor), wirelessTransmitterConfigElement.Attributes["Paint"].Value);
                resultingConfig.WirelessTransmitterConfigs.Add(paintColor, WirelessTransmitterConfig.FromXmlElement(wirelessTransmitterConfigElement));
            }

            XmlElement statueConfigsNode = rootElement["StatueConfigs"];

            foreach (XmlNode statueConfigNode in statueConfigsNode.ChildNodes)
            {
                XmlElement statueConfigElement = (statueConfigNode as XmlElement);
                if (statueConfigElement == null)
                {
                    continue;
                }

                StatueStyle statueStyle = (StatueStyle)Enum.Parse(typeof(StatueStyle), statueConfigElement.Attributes["StatueType"].Value);
                resultingConfig.StatueConfigs.Add(statueStyle, StatueConfig.FromXmlElement(statueConfigElement));
            }

            return(resultingConfig);
        }