Example #1
0
        public static T ItemFromXmlString <T>(string xml, bool resolveCrossRefs = true) where T : new()
        {
            if (resolveCrossRefs && DirectXmlCrossRefLoader.LoadingInProgress)
            {
                Log.Error("Cannot call ItemFromXmlFile with resolveCrossRefs=true while loading is already in progress.", false);
            }
            if (xml.NullOrEmpty())
            {
                return(Activator.CreateInstance <T>());
            }
            T result;

            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                // Trim should theoretically also get rid of BOMs
                xmlDocument.LoadXml(xml.Trim());
                T t = DirectXmlToObject.ObjectFromXml <T>(xmlDocument.DocumentElement, false);
                if (resolveCrossRefs)
                {
                    DirectXmlCrossRefLoader.ResolveAllWantedCrossReferences(FailMode.LogErrors);
                }
                result = t;
            }
            catch (Exception ex)
            {
                Log.Error($"Exception loading item from string. Loading defaults instead. \nXML: {xml}\n\nException: {ex}");
                result = Activator.CreateInstance <T>();
            }
            return(result);
        }
            public void LoadDataFromXmlCustom(XmlNode xmlRoot)
            {
                foreach (XmlNode node in xmlRoot.ChildNodes)
                {
                    if (node is XmlComment)
                    {
                        continue;
                    }

                    string nodeName = node.Name;

                    switch (nodeName)
                    {
                    case "update":
                        update = DirectXmlToObject.ObjectFromXml <List <string> >(node, false);
                        break;

                    case "replaceOpposing":
                        replaceOpposing = new Dictionary <string, string>();
                        ProcessKeyValues(node.ChildNodes, replaceOpposing);
                        break;

                    case "replaceMissing":
                        replaceMissing = new Dictionary <string, string>();
                        ProcessKeyValues(node.ChildNodes, replaceMissing);
                        break;

                    default:
                        Log.Error($"Invalid node name {nodeName}");
                        break;
                    }
                }
            }
Example #3
0
 public static GraphicExtraData Extract(GraphicRequest req,
                                        out GraphicRequest outReq,
                                        bool removeExtraFromReq = false)
 {
     outReq = CopyGraphicRequest(req);
     if (req.graphicData.texPath[0] == '[')
     {
         GraphicExtraData extraData = null;
         try {
             var helperDoc = new System.Xml.XmlDocument();
             helperDoc.LoadXml(req.graphicData.texPath.Replace('[', '<').Replace(']', '>'));
             extraData = DirectXmlToObject.ObjectFromXml <GraphicExtraData>(
                 helperDoc.DocumentElement, false);
         } catch (Exception e) {
             Log.Error("GraphicExtraData was unable to extract XML from \"" + req.graphicData.texPath +
                       "\"; Exception: " + e);
             return(null);
         }
         extraData.inputString = req.graphicData.texPath;
         if (removeExtraFromReq)
         {
             outReq.graphicData.texPath = extraData.texPath;
             outReq.path = extraData.texPath;
         }
         Debug.Message(Debug.Flag.ConveyorGraphics, "Graphic Extra Data extracted: " + extraData);
         return(extraData);
     }
     #if DEBUG
     else
     {
         Debug.Message(Debug.Flag.ConveyorGraphics, "Graphic Extra Data empty: " + req.graphicData.texPath);
     }
     #endif
     return(null);
 }
        private static void LoadSaveGamePatchesFor(ModContentPack mod)          //Taken from Verse.ModContentPack.LoadPatches()
        {
            List <LoadableXmlAsset> list = DirectXmlLoader.XmlAssetsInModFolder(mod, "SaveGamePatches/").ToList <LoadableXmlAsset> ();

            for (int i = 0; i < list.Count; i++)
            {
                XmlElement documentElement = list [i].xmlDoc.DocumentElement;
                if (documentElement.Name != "Patch")
                {
                    Log.Error(string.Format("Unexpected document element in patch XML; got {0}, expected 'Patch'", documentElement.Name));
                }
                else
                {
                    for (int j = 0; j < documentElement.ChildNodes.Count; j++)
                    {
                        XmlNode xmlNode = documentElement.ChildNodes [j];
                        if (xmlNode.NodeType == XmlNodeType.Element)
                        {
                            if (xmlNode.Name != "Operation")
                            {
                                Log.Error(string.Format("Unexpected element in patch XML; got {0}, expected 'Operation'", documentElement.ChildNodes [j].Name));
                            }
                            else
                            {
                                PatchOperation patchOperation = DirectXmlToObject.ObjectFromXml <PatchOperation> (xmlNode, false);
                                patchOperation.sourceFile = list [i].FullFilePath;
                                SaveGamePatches.patches.Add(patchOperation);
                            }
                        }
                    }
                }
            }
        }
Example #5
0
 public void LoadInfo()
 {
     using (var zip = ZipFile)
     {
         var doc = ScribeUtil.LoadDocument(zip["info"].GetBytes());
         info = DirectXmlToObject.ObjectFromXml <ReplayInfo>(doc.DocumentElement, true);
     }
 }
Example #6
0
 public static List <ISettingRow> LoadDataFromXml(XmlNode xmlRoot)
 {
     return(xmlRoot.ChildNodes.Cast <XmlNode>()
            .Where(n => n.NodeType == XmlNodeType.Element)
            .Select(n => n as XmlElement)
            .Where(e => e != null)
            .Where(e => e.Name == "Row")
            .Select(e => DirectXmlToObject.ObjectFromXml <ISettingRow>(e, false))
            .ToList());
 }
 public void LoadDataFromXmlCustom(XmlNode xmlRoot)
 {
     if (xmlRoot.ChildNodes.Count != 1)
     {
         Log.Error("Misconfigured PrefixCapturedVar: " + xmlRoot.OuterXml);
         return;
     }
     name  = xmlRoot.Name;
     value = new SlateRef <object>(DirectXmlToObject.InnerTextWithReplacedNewlinesOrXML(xmlRoot));
     TKeySystem.MarkTreatAsList(xmlRoot.ParentNode);
 }
 // Completely override default RW read from XML:
 //   We do this to allow pretty XML
 public void LoadDataFromXmlCustom(XmlNode xmlRoot)
 {
     if (xmlRoot.Attributes["Chance"] != null)
     {
         float.TryParse(xmlRoot.Attributes["Chance"].Value, out this.chance);
     }
     this.bonuses = xmlRoot.ChildNodes.Cast <XmlNode>().Where(n => n.NodeType == XmlNodeType.Element)
                    .Select(n => n as XmlElement)
                    .Where(e => e != null)
                    .Select(e => DirectXmlToObject.ObjectFromXml <BonusYield>(e, false))
                    .ToList();
 }
 public void LoadDataFromXmlCustom(XmlNode xmlRoot)
 {
     this.name    = xmlRoot.Name;
     this.noClass = xmlRoot.Attributes["Class"] == null;
     if (this.noClass)
     {
         this.value = xmlRoot.InnerText;
     }
     else
     {
         this.value = DirectXmlToObject.ObjectFromXml <object>(xmlRoot, false);
     }
 }
Example #10
0
        public bool LoadInfo()
        {
            using (var zip = ZipFile)
            {
                var infoFile = zip["info"];
                if (infoFile == null)
                {
                    return(false);
                }

                var doc = ScribeUtil.LoadDocument(infoFile.GetBytes());
                info = DirectXmlToObject.ObjectFromXml <ReplayInfo>(doc.DocumentElement, true);
            }

            return(true);
        }
Example #11
0
        // XXX: This is mostly just for maxWordClasses, but you can't define a LoadDataFromXmlCustom for a
        // Dictionary, sadly.
        public void LoadDataFromXmlCustom(XmlNode xmlRoot)
        {
            foreach (XmlNode xmlMain in xmlRoot.ChildNodes)
            {
                if (xmlMain.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                switch (xmlMain.Name)
                {
                case "defName":
                    defName = ParseHelper.FromString <string>(xmlMain.FirstChild.Value);
                    break;

                case "rulePack":
                    RulePack rulePackFromXml = DirectXmlToObject.ObjectFromXml <RulePack>(xmlMain, false);

                    // [Reflection] this.rulePack = rulePackFromXml;
                    FieldInfo rulePackField = AccessTools.Field(typeof(RulePackDef), "rulePack");
                    rulePackField.SetValue(this, rulePackFromXml);
                    break;

                case "disallowedAffixCombos":
                    disallowedAffixCombos = DirectXmlToObject.ObjectFromXml <List <string> >(xmlMain, false);
                    break;

                case "maxWordClasses":
                    foreach (XmlNode node in xmlMain.ChildNodes)
                    {
                        string key = ParseHelper.FromString <string>(node.Name);
                        int    val = ParseHelper.FromString <int>   (node.FirstChild.Value);
                        maxWordClasses.Add(key, val);
                    }
                    break;

                default:
                    Log.Error("XML error: " + xmlMain.Name + " doesn't correspond to any field in type " + this.GetType().Name + ". Context: " + xmlMain.OuterXml);
                    break;
                }
            }
        }
Example #12
0
        public void LoadModCheckPatches()
        {
            DeepProfiler.Start("Loading all ModCheck patches");
            this.patches = new List <PatchOperation>();
            int modIndex = -1;

            foreach (ModContentPack mod in LoadedModManager.RunningMods)
            {
                ++modIndex;
                List <LoadableXmlAsset> list = DirectXmlLoader.XmlAssetsInModFolder(mod, "ModCheckPatches/").ToList <LoadableXmlAsset>();
                for (int i = 0; i < list.Count; i++)
                {
                    XmlElement documentElement = list[i].xmlDoc.DocumentElement;
                    if (documentElement.Name != "Patch")
                    {
                        Log.Error(string.Format("Unexpected document element in patch XML; got {0}, expected 'Patch'", documentElement.Name), false);
                    }
                    else
                    {
                        for (int j = 0; j < documentElement.ChildNodes.Count; j++)
                        {
                            XmlNode xmlNode = documentElement.ChildNodes[j];
                            if (xmlNode.NodeType == XmlNodeType.Element)
                            {
                                if (xmlNode.Name != "Operation")
                                {
                                    Log.Error(string.Format("Unexpected element in patch XML; got {0}, expected 'Operation'", documentElement.ChildNodes[j].Name), false);
                                }
                                else
                                {
                                    PatchOperation patchOperation = DirectXmlToObject.ObjectFromXml <PatchOperation>(xmlNode, false);
                                    patchOperation.sourceFile = list[i].FullFilePath;
                                    this.patches.Add(patchOperation);
                                    PatchMemory.Add(new PatchMemoryModule(modIndex, mod.Name, patchOperation, true));
                                }
                            }
                        }
                    }
                }
            }
            DeepProfiler.End();
        }
Example #13
0
        static bool Prefix(Verse.ModContentPack __instance)
        {
            DeepProfiler.Start("Loading all patches");
            List <PatchOperation> lst = new List <PatchOperation>();

            typeof(ModContentPack).GetField("patches", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
            .SetValue(__instance, lst);
            List <LoadableXmlAsset> list = DirectXmlLoader.XmlAssetsInModFolder(__instance, "Patches/").ToList <LoadableXmlAsset>();

            for (int i = 0; i < list.Count; i++)
            {
                XmlElement documentElement = list[i].xmlDoc.DocumentElement;
                if (documentElement.Name != "Patch")
                {
                    Log.Error(string.Format("Unexpected document element in patch XML; got {0}, expected 'Patch'", documentElement.Name), false);
                }
                else
                {
                    foreach (XmlNode xmlNode in documentElement.ChildNodes)
                    {
                        if (xmlNode.NodeType == XmlNodeType.Element)
                        {
                            if (xmlNode.Name != "Operation")
                            {
                                Log.Error(string.Format("Unexpected element in patch XML; got {0}, expected 'Operation'", xmlNode.Name), false);
                            }
                            else
                            {
                                PatchOperation patchOperation = DirectXmlToObject.ObjectFromXml <PatchOperation>(xmlNode, false);
                                patchOperation.sourceFile = list[i].FullFilePath;
                                lst.Add(patchOperation);
                            }
                        }
                    }
                }
            }
            DeepProfiler.End();

            return(false);
        }
Example #14
0
            public void LoadDataFromXmlCustom(XmlNode xmlRoot)
            {
                XmlAttribute mayRequire = xmlRoot.Attributes[name : "MayRequire"];
                int          index      = mayRequire != null?xmlRoot.Name.LastIndexOf(value : '\"') + 1 : 0;

                DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(this, nameof(this.hediff), xmlRoot.Name.Substring(index, xmlRoot.Name.Length - index), mayRequire?.Value.ToLower());

                this.path = xmlRoot.FirstChild.Value?.Trim();

                Traverse traverse = Traverse.Create(this);

                foreach (XmlNode xmlRootChildNode in xmlRoot.ChildNodes)
                {
                    Traverse field = traverse.Field(xmlRootChildNode.Name);
                    if (field.FieldExists())
                    {
                        field.SetValue(field.GetValueType().IsGenericType ?
                                       DirectXmlToObject.GetObjectFromXmlMethod(field.GetValueType())(xmlRootChildNode, arg2: false) :
                                       xmlRootChildNode.InnerXml.Trim());
                    }
                }
            }
        public void LoadDataFromXmlCustom(XmlNode xmlRoot)
        {
            foreach (XmlNode node in xmlRoot.ChildNodes)
            {
                if (node is XmlComment)
                {
                    continue;
                }

                string nodeName = node.Name;

                switch (nodeName)
                {
                case "defName":
                    defName = node.FirstChild.Value;
                    break;

                case "modID":
                    modID = node.FirstChild.Value;
                    break;

                case "backstory":
                    backstory = DirectXmlToObject.ObjectFromXml <Backstory>(node, false);
                    break;

                default:
                    Type type = GenTypes.GetTypeInAnyAssembly(nodeName);

                    if (type == null)
                    {
                        Log.Error($"Invalid type {nodeName}");
                        continue;
                    }

                    ProcessDefReplacers(node.ChildNodes, type);
                    break;
                }
            }
        }
Example #16
0
 // Load from XML file Settings/SpecialSculpture.xml; called from ModComponent
 static public List <SpecialSculpture> LoadAvailableSpecialSculptures(ModContentPack content)
 {
     try {
         var xml = DirectXmlLoader.XmlAssetsInModFolder(content, "Settings")?.
                   Where(x => x.name == "SpecialSculpture.xml")?.FirstOrDefault();
         if (xml == null || xml.xmlDoc == null)
         {
             Log.Warning("PRF could not load special sculpture data");
             return(null);
         }
         List <SpecialSculpture> list = DirectXmlToObject
                                        .ObjectFromXml <List <SpecialSculpture> >(xml.xmlDoc.DocumentElement, false);
         #if DEBUG
         Log.Message("PRF: loaded " + ((list == null) ? "zero" : (list.Count.ToString())) +
                     " special Sculptures");
         #endif
         return(list);
     } catch (Exception e) {
         Log.Error("PRF was unable to extract Special Sculpture data from XML; \n" +
                   "  Exception: " + e);
         return(null);
     }
 }
Example #17
0
 public void LoadDataFromXmlCustom(XmlNode xmlRoot)
 {
     this.name        = xmlRoot.Name;
     this.graphicData = DirectXmlToObject.ObjectFromXml <GraphicData>(xmlRoot.FirstChild, false);
 }