public static void ParseAndProcessXML(XmlDocument xmlDoc, Dictionary <XmlNode, LoadableXmlAsset> assetlookup) { XmlNodeList childNodes = xmlDoc.DocumentElement.ChildNodes; for (int i = 0; i < childNodes.Count; i++) { if (childNodes[i].NodeType == XmlNodeType.Element) { LoadableXmlAsset loadableXmlAsset = assetlookup.TryGetValue(childNodes[i], null); XmlInheritance.TryRegister(childNodes[i], (loadableXmlAsset == null) ? null : loadableXmlAsset.mod); } } XmlInheritance.Resolve(); DefPackage defPackage = new DefPackage("Unknown", string.Empty); ModContentPack modContentPack = LoadedModManager.runningMods.FirstOrDefault <ModContentPack>(); modContentPack.AddDefPackage(defPackage); foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes) { LoadableXmlAsset loadableXmlAsset2 = assetlookup.TryGetValue(xmlNode, null); DefPackage defPackage2 = (loadableXmlAsset2 == null) ? defPackage : loadableXmlAsset2.defPackage; Def def = DirectXmlLoader.DefFromNode(xmlNode, loadableXmlAsset2); if (def != null) { def.modContentPack = ((loadableXmlAsset2 == null) ? modContentPack : loadableXmlAsset2.mod); defPackage2.AddDef(def); } } }
private static void CheckForDuplicateNodes(XmlNode node, XmlNode root) { XmlInheritance.tempUsedNodeNames.Clear(); foreach (XmlNode xmlNode in node.ChildNodes) { if (xmlNode.NodeType == XmlNodeType.Element && !(xmlNode.Name == "li")) { if (XmlInheritance.tempUsedNodeNames.Contains(xmlNode.Name)) { Log.Error(string.Concat(new string[] { "XML error: Duplicate XML node name ", xmlNode.Name, " in this XML block: ", node.OuterXml, (node == root) ? string.Empty : ("\n\nRoot node: " + root.OuterXml) }), false); } else { XmlInheritance.tempUsedNodeNames.Add(xmlNode.Name); } } } XmlInheritance.tempUsedNodeNames.Clear(); foreach (XmlNode xmlNode2 in node.ChildNodes) { if (xmlNode2.NodeType == XmlNodeType.Element) { XmlInheritance.CheckForDuplicateNodes(xmlNode2, root); } } }
public static IEnumerable <T> LoadXmlDataInResourcesFolder <T>(string folderPath) where T : new() { XmlInheritance.Clear(); List <LoadableXmlAsset> assets = new List <LoadableXmlAsset>(); object[] textObjects = Resources.LoadAll <TextAsset>(folderPath); object[] array = textObjects; for (int j = 0; j < array.Length; j++) { TextAsset textAsset = (TextAsset)array[j]; LoadableXmlAsset loadableXmlAsset = new LoadableXmlAsset(textAsset.name, string.Empty, textAsset.text); XmlInheritance.TryRegisterAllFrom(loadableXmlAsset, null); assets.Add(loadableXmlAsset); } XmlInheritance.Resolve(); for (int i = 0; i < assets.Count; i++) { using (IEnumerator <T> enumerator = DirectXmlLoader.AllGameItemsFromAsset <T>(assets[i]).GetEnumerator()) { if (enumerator.MoveNext()) { T item = enumerator.Current; yield return(item); /*Error: Unable to find new state assignment for yield return*/; } } } XmlInheritance.Clear(); yield break; IL_0195: /*Error near IL_0196: Unexpected return in MoveNext()*/; }
private static void RecursiveNodeCopyOverwriteElements(XmlNode source, XmlNode target) { if (source.ChildNodes.Count == 0) { while (target.HasChildNodes) { target.RemoveChild(target.FirstChild); } } if (source.ChildNodes.Count == 1 && source.FirstChild.NodeType == XmlNodeType.Text) { while (target.HasChildNodes) { target.RemoveChild(target.FirstChild); } XmlNode newChild = target.OwnerDocument.ImportNode(source.FirstChild, true); target.AppendChild(newChild); } else { IEnumerator enumerator = source.GetEnumerator(); try { while (enumerator.MoveNext()) { XmlNode xmlNode = (XmlNode)enumerator.Current; if (xmlNode.Name == "li") { XmlNode newChild2 = target.OwnerDocument.ImportNode(xmlNode, true); target.AppendChild(newChild2); } else { XmlElement xmlElement = target[xmlNode.Name]; if (xmlElement != null) { XmlInheritance.RecursiveNodeCopyOverwriteElements(xmlNode, xmlElement); } else { XmlNode newChild3 = target.OwnerDocument.ImportNode(xmlNode, true); target.AppendChild(newChild3); } } } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } } }
public static void LoadAllActiveMods() { XmlInheritance.Clear(); int num = 0; foreach (ModMetaData item2 in ModsConfig.ActiveModsInLoadOrder.ToList()) { DeepProfiler.Start("Initializing " + item2); if (!item2.RootDir.Exists) { ModsConfig.SetActive(item2.Identifier, false); Log.Warning("Failed to find active mod " + item2.Name + "(" + item2.Identifier + ") at " + item2.RootDir); DeepProfiler.End(); } else { ModContentPack item = new ModContentPack(item2.RootDir, num, item2.Name); num++; LoadedModManager.runningMods.Add(item); DeepProfiler.End(); } } for (int i = 0; i < LoadedModManager.runningMods.Count; i++) { ModContentPack modContentPack = LoadedModManager.runningMods[i]; DeepProfiler.Start("Loading " + modContentPack + " content"); modContentPack.ReloadContent(); DeepProfiler.End(); } foreach (Type item3 in typeof(Mod).InstantiableDescendantsAndSelf()) { if (!LoadedModManager.runningModClasses.ContainsKey(item3)) { ModContentPack modContentPack2 = (from modpack in LoadedModManager.runningMods where modpack.assemblies.loadedAssemblies.Contains(item3.Assembly) select modpack).FirstOrDefault(); LoadedModManager.runningModClasses[item3] = (Mod)Activator.CreateInstance(item3, modContentPack2); } } for (int j = 0; j < LoadedModManager.runningMods.Count; j++) { ModContentPack modContentPack3 = LoadedModManager.runningMods[j]; DeepProfiler.Start("Loading " + modContentPack3); modContentPack3.LoadDefs(LoadedModManager.runningMods.SelectMany((ModContentPack rm) => rm.Patches)); DeepProfiler.End(); } foreach (ModContentPack runningMod in LoadedModManager.runningMods) { foreach (PatchOperation patch in runningMod.Patches) { patch.Complete(runningMod.Name); } runningMod.ClearPatchesCache(); } XmlInheritance.Clear(); }
private static void ResolveXmlNodesRecursively(XmlInheritance.XmlInheritanceNode node) { if (node.resolvedXmlNode != null) { Log.Error("XML error: Cyclic inheritance hierarchy detected for node \"" + node.xmlNode.Name + "\". Full node: " + node.xmlNode.OuterXml); return; } XmlInheritance.ResolveXmlNodeFor(node); for (int i = 0; i < node.children.Count; i++) { XmlInheritance.ResolveXmlNodesRecursively(node.children[i]); } }
public static void TryRegisterAllFrom(LoadableXmlAsset xmlAsset, ModContentPack mod) { if (xmlAsset.xmlDoc != null) { XmlNodeList childNodes = xmlAsset.xmlDoc.DocumentElement.ChildNodes; for (int i = 0; i < childNodes.Count; i++) { if (childNodes[i].NodeType == XmlNodeType.Element) { XmlInheritance.TryRegister(childNodes[i], mod); } } } }
private static void ResolveParentsAndChildNodesLinks() { for (int i = 0; i < XmlInheritance.unresolvedNodes.Count; i++) { XmlAttribute xmlAttribute = XmlInheritance.unresolvedNodes[i].xmlNode.Attributes[XmlInheritance.ParentNameAttributeName]; if (xmlAttribute != null) { XmlInheritance.unresolvedNodes[i].parent = XmlInheritance.GetBestParentFor(XmlInheritance.unresolvedNodes[i], xmlAttribute.Value); if (XmlInheritance.unresolvedNodes[i].parent != null) { XmlInheritance.unresolvedNodes[i].parent.children.Add(XmlInheritance.unresolvedNodes[i]); } } } }
public static void LoadAllActiveMods() { XmlInheritance.Clear(); LoadedModManager.InitializeMods(); LoadedModManager.LoadModContent(); LoadedModManager.CreateModClasses(); List <LoadableXmlAsset> xmls = LoadedModManager.LoadModXML(); Dictionary <XmlNode, LoadableXmlAsset> assetlookup = new Dictionary <XmlNode, LoadableXmlAsset>(); XmlDocument xmlDoc = LoadedModManager.CombineIntoUnifiedXML(xmls, assetlookup); LoadedModManager.ApplyPatches(xmlDoc, assetlookup); LoadedModManager.ParseAndProcessXML(xmlDoc, assetlookup); LoadedModManager.ClearCachedPatches(); XmlInheritance.Clear(); }
public static void ParseAndProcessXML(XmlDocument xmlDoc, Dictionary <XmlNode, LoadableXmlAsset> assetlookup) { XmlNodeList childNodes = xmlDoc.DocumentElement.ChildNodes; for (int i = 0; i < childNodes.Count; i++) { if (childNodes[i].NodeType == XmlNodeType.Element) { LoadableXmlAsset loadableXmlAsset = assetlookup.TryGetValue(childNodes[i], null); XmlInheritance.TryRegister(childNodes[i], (loadableXmlAsset == null) ? null : loadableXmlAsset.mod); } } XmlInheritance.Resolve(); DefPackage defPackage = new DefPackage("(unknown)", "(unknown)"); ModContentPack modContentPack = LoadedModManager.runningMods.FirstOrDefault <ModContentPack>(); modContentPack.AddDefPackage(defPackage); IEnumerator enumerator = xmlDoc.DocumentElement.ChildNodes.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; XmlNode xmlNode = (XmlNode)obj; LoadableXmlAsset loadableXmlAsset2 = assetlookup.TryGetValue(xmlNode, null); DefPackage defPackage2 = (loadableXmlAsset2 == null) ? defPackage : loadableXmlAsset2.defPackage; Def def = DirectXmlLoader.DefFromNode(xmlNode, loadableXmlAsset2); if (def != null) { def.modContentPack = ((loadableXmlAsset2 == null) ? modContentPack : loadableXmlAsset2.mod); defPackage2.AddDef(def); } } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } }
private static void ResolveXmlNodeFor(XmlInheritance.XmlInheritanceNode node) { if (node.parent == null) { node.resolvedXmlNode = node.xmlNode; return; } if (node.parent.resolvedXmlNode == null) { Log.Error("XML error: Internal error. Tried to resolve node whose parent has not been resolved yet. This means that this method was called in incorrect order."); node.resolvedXmlNode = node.xmlNode; return; } XmlInheritance.CheckForDuplicateNodes(node.xmlNode); XmlNode xmlNode = node.parent.resolvedXmlNode.CloneNode(true); xmlNode.Attributes.RemoveAll(); XmlAttributeCollection attributes = node.xmlNode.Attributes; for (int i = 0; i < attributes.Count; i++) { if (!(attributes[i].Name == XmlInheritance.NameAttributeName) && !(attributes[i].Name == XmlInheritance.ParentNameAttributeName)) { XmlAttribute node2 = (XmlAttribute)xmlNode.OwnerDocument.ImportNode(attributes[i], true); xmlNode.Attributes.Append(node2); } } XmlAttributeCollection attributes2 = node.parent.resolvedXmlNode.Attributes; for (int j = 0; j < attributes2.Count; j++) { if (!(attributes2[j].Name == XmlInheritance.NameAttributeName) && !(attributes2[j].Name == XmlInheritance.ParentNameAttributeName)) { if (xmlNode.Attributes[attributes2[j].Name] == null) { XmlAttribute node3 = (XmlAttribute)xmlNode.OwnerDocument.ImportNode(attributes2[j], true); xmlNode.Attributes.Append(node3); } } } XmlInheritance.RecursiveNodeCopyOverwriteElements(node.xmlNode, xmlNode); node.resolvedXmlNode = xmlNode; }
private static void RecursiveNodeCopyOverwriteElements(XmlNode source, XmlNode target) { if (source.ChildNodes.Count == 0) { while (target.HasChildNodes) { target.RemoveChild(target.FirstChild); } } if (source.ChildNodes.Count == 1 && source.FirstChild.NodeType == XmlNodeType.Text) { while (target.HasChildNodes) { target.RemoveChild(target.FirstChild); } XmlNode newChild = target.OwnerDocument.ImportNode(source.FirstChild, true); target.AppendChild(newChild); } else { foreach (XmlNode xmlNode in source) { if (xmlNode.Name == "li") { XmlNode newChild2 = target.OwnerDocument.ImportNode(xmlNode, true); target.AppendChild(newChild2); } else { XmlElement xmlElement = target[xmlNode.Name]; if (xmlElement != null) { XmlInheritance.RecursiveNodeCopyOverwriteElements(xmlNode, xmlElement); } else { XmlNode newChild3 = target.OwnerDocument.ImportNode(xmlNode, true); target.AppendChild(newChild3); } } } } }
private static void ResolveXmlNodeFor(XmlInheritance.XmlInheritanceNode node) { if (node.parent == null) { node.resolvedXmlNode = node.xmlNode; return; } if (node.parent.resolvedXmlNode == null) { Log.Error("XML error: Internal error. Tried to resolve node whose parent has not been resolved yet. This means that this method was called in incorrect order.", false); node.resolvedXmlNode = node.xmlNode; return; } XmlInheritance.CheckForDuplicateNodes(node.xmlNode, node.xmlNode); XmlNode xmlNode = node.parent.resolvedXmlNode.CloneNode(true); XmlInheritance.RecursiveNodeCopyOverwriteElements(node.xmlNode, xmlNode); node.resolvedXmlNode = xmlNode; }
public static IEnumerable <T> LoadXmlDataInResourcesFolder <T>(string folderPath) where T : new() { XmlInheritance.Clear(); List <LoadableXmlAsset> assets = new List <LoadableXmlAsset>(); object[] array = Resources.LoadAll <TextAsset>(folderPath); for (int j = 0; j < array.Length; j++) { TextAsset textAsset = (TextAsset)array[j]; LoadableXmlAsset loadableXmlAsset = new LoadableXmlAsset(textAsset.name, "", textAsset.text); XmlInheritance.TryRegisterAllFrom(loadableXmlAsset, null); assets.Add(loadableXmlAsset); } XmlInheritance.Resolve(); for (int i = 0; i < assets.Count; i++) { foreach (T item in AllGameItemsFromAsset <T>(assets[i])) { yield return(item); } } XmlInheritance.Clear(); }
private static void ResolveXmlNodes() { List <XmlInheritance.XmlInheritanceNode> list = (from x in XmlInheritance.unresolvedNodes where x.parent == null || x.parent.resolvedXmlNode != null select x).ToList <XmlInheritance.XmlInheritanceNode>(); for (int i = 0; i < list.Count; i++) { XmlInheritance.ResolveXmlNodesRecursively(list[i]); } for (int j = 0; j < XmlInheritance.unresolvedNodes.Count; j++) { if (XmlInheritance.unresolvedNodes[j].resolvedXmlNode == null) { Log.Error("XML error: Cyclic inheritance hierarchy detected for node \"" + XmlInheritance.unresolvedNodes[j].xmlNode.Name + "\". Full node: " + XmlInheritance.unresolvedNodes[j].xmlNode.OuterXml); } else { XmlInheritance.resolvedNodes.Add(XmlInheritance.unresolvedNodes[j].xmlNode, XmlInheritance.unresolvedNodes[j]); } } XmlInheritance.unresolvedNodes.Clear(); }
public static IEnumerable <T> LoadXmlDataInResourcesFolder <T>(string folderPath) where T : new() { XmlInheritance.Clear(); List <LoadableXmlAsset> assets = new List <LoadableXmlAsset>(); object[] textObjects = Resources.LoadAll <TextAsset>(folderPath); foreach (TextAsset textAsset in textObjects) { LoadableXmlAsset loadableXmlAsset = new LoadableXmlAsset(textAsset.name, string.Empty, textAsset.text); XmlInheritance.TryRegisterAllFrom(loadableXmlAsset, null); assets.Add(loadableXmlAsset); } XmlInheritance.Resolve(); for (int i = 0; i < assets.Count; i++) { foreach (T item in DirectXmlLoader.AllGameItemsFromAsset <T>(assets[i])) { yield return(item); } } XmlInheritance.Clear(); yield break; }
public void LoadDefs(IEnumerable <PatchOperation> patches) { DeepProfiler.Start("Loading all defs"); List <LoadableXmlAsset> list = DirectXmlLoader.XmlAssetsInModFolder(this, "Defs/").ToList <LoadableXmlAsset>(); foreach (LoadableXmlAsset current in list) { foreach (PatchOperation current2 in patches) { current2.Apply(current.xmlDoc); } } for (int i = 0; i < list.Count; i++) { if (list[i] == null || list[i].xmlDoc == null || list[i].xmlDoc.DocumentElement == null) { Log.Error(string.Format("{0}: unknown parse failure", list[i].fullFolderPath + "/" + list[i].name)); } else if (list[i].xmlDoc.DocumentElement.Name != "Defs") { Log.Error(string.Format("{0}: root element named {1}; should be named Defs", list[i].fullFolderPath + "/" + list[i].name, list[i].xmlDoc.DocumentElement.Name)); } XmlInheritance.TryRegisterAllFrom(list[i], this); } XmlInheritance.Resolve(); for (int j = 0; j < list.Count; j++) { string relFolder = GenFilePaths.FolderPathRelativeToDefsFolder(list[j].fullFolderPath, this); DefPackage defPackage = new DefPackage(list[j].name, relFolder); foreach (Def current3 in DirectXmlLoader.AllDefsFromAsset(list[j])) { defPackage.defs.Add(current3); } this.defPackages.Add(defPackage); } DeepProfiler.End(); }
public static T ObjectFromXml <T>(XmlNode xmlRoot, bool doPostLoad) where T : new() { MethodInfo methodInfo = DirectXmlToObject.CustomDataLoadMethodOf(typeof(T)); T result; if (methodInfo != null) { xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); DirectXmlToObject.currentlyInstantiatingObjectOfType.Push(type); T t; try { t = (T)((object)Activator.CreateInstance(type)); } finally { DirectXmlToObject.currentlyInstantiatingObjectOfType.Pop(); } try { methodInfo.Invoke(t, new object[] { xmlRoot }); } catch (Exception ex) { Log.Error(string.Concat(new object[] { "Exception in custom XML loader for ", typeof(T), ". Node is:\n ", xmlRoot.OuterXml, "\n\nException is:\n ", ex.ToString() }), false); t = default(T); } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(t); } result = t; } else if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.CDATA) { if (typeof(T) != typeof(string)) { Log.Error("CDATA can only be used for strings. Bad xml: " + xmlRoot.OuterXml, false); result = default(T); } else { result = (T)((object)xmlRoot.FirstChild.Value); } } else if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.Text) { try { return((T)((object)ParseHelper.FromString(xmlRoot.InnerText, typeof(T)))); } catch (Exception ex2) { Log.Error(string.Concat(new object[] { "Exception parsing ", xmlRoot.OuterXml, " to type ", typeof(T), ": ", ex2 }), false); } result = default(T); } else if (Attribute.IsDefined(typeof(T), typeof(FlagsAttribute))) { List <T> list = DirectXmlToObject.ListFromXml <T>(xmlRoot); int num = 0; foreach (T t2 in list) { int num2 = (int)((object)t2); num |= num2; } result = (T)((object)num); } else if (typeof(T).HasGenericDefinition(typeof(List <>))) { MethodInfo method = typeof(DirectXmlToObject).GetMethod("ListFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments = typeof(T).GetGenericArguments(); MethodInfo methodInfo2 = method.MakeGenericMethod(genericArguments); object[] parameters = new object[] { xmlRoot }; object obj = methodInfo2.Invoke(null, parameters); result = (T)((object)obj); } else if (typeof(T).HasGenericDefinition(typeof(Dictionary <, >))) { MethodInfo method2 = typeof(DirectXmlToObject).GetMethod("DictionaryFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments2 = typeof(T).GetGenericArguments(); MethodInfo methodInfo3 = method2.MakeGenericMethod(genericArguments2); object[] parameters2 = new object[] { xmlRoot }; object obj2 = methodInfo3.Invoke(null, parameters2); result = (T)((object)obj2); } else { if (!xmlRoot.HasChildNodes) { if (typeof(T) == typeof(string)) { return((T)((object)"")); } XmlAttribute xmlAttribute = xmlRoot.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } if (typeof(T).IsGenericType) { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(List <>) || genericTypeDefinition == typeof(HashSet <>) || genericTypeDefinition == typeof(Dictionary <, >)) { return(Activator.CreateInstance <T>()); } } } xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type2 = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); Type type3 = Nullable.GetUnderlyingType(type2) ?? type2; DirectXmlToObject.currentlyInstantiatingObjectOfType.Push(type3); T t3; try { t3 = (T)((object)Activator.CreateInstance(type3)); } finally { DirectXmlToObject.currentlyInstantiatingObjectOfType.Pop(); } List <string> list2 = null; if (xmlRoot.ChildNodes.Count > 1) { list2 = new List <string>(); } for (int i = 0; i < xmlRoot.ChildNodes.Count; i++) { XmlNode xmlNode = xmlRoot.ChildNodes[i]; if (!(xmlNode is XmlComment)) { if (xmlRoot.ChildNodes.Count > 1) { if (list2.Contains(xmlNode.Name)) { Log.Error(string.Concat(new object[] { "XML ", typeof(T), " defines the same field twice: ", xmlNode.Name, ".\n\nField contents: ", xmlNode.InnerText, ".\n\nWhole XML:\n\n", xmlRoot.OuterXml }), false); } else { list2.Add(xmlNode.Name); } } FieldInfo fieldInfo = DirectXmlToObject.GetFieldInfoForType(t3.GetType(), xmlNode.Name, xmlRoot); if (fieldInfo == null) { foreach (FieldInfo fieldInfo2 in t3.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { foreach (object obj3 in fieldInfo2.GetCustomAttributes(typeof(LoadAliasAttribute), true)) { string alias = ((LoadAliasAttribute)obj3).alias; if (alias.EqualsIgnoreCase(xmlNode.Name)) { fieldInfo = fieldInfo2; break; } } if (fieldInfo != null) { break; } } } if (fieldInfo == null) { bool flag = false; foreach (object obj4 in t3.GetType().GetCustomAttributes(typeof(IgnoreSavedElementAttribute), true)) { string elementToIgnore = ((IgnoreSavedElementAttribute)obj4).elementToIgnore; if (string.Equals(elementToIgnore, xmlNode.Name, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } if (!flag) { Log.Error(string.Concat(new string[] { "XML error: ", xmlNode.OuterXml, " doesn't correspond to any field in type ", t3.GetType().Name, ". Context: ", xmlRoot.OuterXml }), false); } } else if (typeof(Def).IsAssignableFrom(fieldInfo.FieldType)) { if (xmlNode.InnerText.NullOrEmpty()) { fieldInfo.SetValue(t3, null); } else { DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(t3, fieldInfo, xmlNode.InnerText); } } else { object value = null; try { MethodInfo method3 = typeof(DirectXmlToObject).GetMethod("ObjectFromXml", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo methodInfo4 = method3.MakeGenericMethod(new Type[] { fieldInfo.FieldType }); value = methodInfo4.Invoke(null, new object[] { xmlNode, doPostLoad }); } catch (Exception ex3) { Log.Error("Exception loading from " + xmlNode.ToString() + ": " + ex3.ToString(), false); goto IL_863; } if (!typeof(T).IsValueType) { fieldInfo.SetValue(t3, value); } else { object obj5 = t3; fieldInfo.SetValue(obj5, value); t3 = (T)((object)obj5); } } } IL_863 :; } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(t3); } result = t3; } return(result); }
private static void RecursiveNodeCopyOverwriteElements(XmlNode child, XmlNode current) { XmlAttribute xmlAttribute = child.Attributes["Inherit"]; if (xmlAttribute != null && xmlAttribute.Value.ToLower() == "false") { while (current.HasChildNodes) { current.RemoveChild(current.FirstChild); } foreach (XmlNode node in child) { XmlNode newChild = current.OwnerDocument.ImportNode(node, true); current.AppendChild(newChild); } return; } current.Attributes.RemoveAll(); XmlAttributeCollection attributes = child.Attributes; for (int i = 0; i < attributes.Count; i++) { XmlAttribute node2 = (XmlAttribute)current.OwnerDocument.ImportNode(attributes[i], true); current.Attributes.Append(node2); } List <XmlElement> list = new List <XmlElement>(); XmlNode xmlNode = null; foreach (XmlNode xmlNode2 in child) { if (xmlNode2.NodeType == XmlNodeType.Text) { xmlNode = xmlNode2; } else if (xmlNode2.NodeType == XmlNodeType.Element) { list.Add((XmlElement)xmlNode2); } } if (xmlNode != null) { for (int j = current.ChildNodes.Count - 1; j >= 0; j--) { XmlNode xmlNode3 = current.ChildNodes[j]; if (xmlNode3.NodeType != XmlNodeType.Attribute) { current.RemoveChild(xmlNode3); } } XmlNode newChild2 = current.OwnerDocument.ImportNode(xmlNode, true); current.AppendChild(newChild2); } else if (!list.Any <XmlElement>()) { bool flag = false; foreach (XmlNode xmlNode4 in current.ChildNodes) { if (xmlNode4.NodeType == XmlNodeType.Element) { flag = true; break; } } if (!flag) { foreach (XmlNode xmlNode5 in current.ChildNodes) { if (xmlNode5.NodeType != XmlNodeType.Attribute) { current.RemoveChild(xmlNode5); } } } } else { for (int k = 0; k < list.Count; k++) { XmlElement xmlElement = list[k]; if (xmlElement.Name == "li") { XmlNode newChild3 = current.OwnerDocument.ImportNode(xmlElement, true); current.AppendChild(newChild3); } else { XmlElement xmlElement2 = current[xmlElement.Name]; if (xmlElement2 != null) { XmlInheritance.RecursiveNodeCopyOverwriteElements(xmlElement, xmlElement2); } else { XmlNode newChild4 = current.OwnerDocument.ImportNode(xmlElement, true); current.AppendChild(newChild4); } } } } }
public static void LoadAllActiveMods() { DeepProfiler.Start("XmlInheritance.Clear()"); try { XmlInheritance.Clear(); } finally { DeepProfiler.End(); } DeepProfiler.Start("InitializeMods()"); try { InitializeMods(); } finally { DeepProfiler.End(); } DeepProfiler.Start("LoadModContent()"); try { LoadModContent(); } finally { DeepProfiler.End(); } DeepProfiler.Start("CreateModClasses()"); try { CreateModClasses(); } finally { DeepProfiler.End(); } List <LoadableXmlAsset> xmls = null; DeepProfiler.Start("LoadModXML()"); try { xmls = LoadModXML(); } finally { DeepProfiler.End(); } Dictionary <XmlNode, LoadableXmlAsset> assetlookup = new Dictionary <XmlNode, LoadableXmlAsset>(); XmlDocument xmlDocument = null; DeepProfiler.Start("CombineIntoUnifiedXML()"); try { xmlDocument = CombineIntoUnifiedXML(xmls, assetlookup); } finally { DeepProfiler.End(); } TKeySystem.Clear(); DeepProfiler.Start("TKeySystem.Parse()"); try { TKeySystem.Parse(xmlDocument); } finally { DeepProfiler.End(); } DeepProfiler.Start("ApplyPatches()"); try { ApplyPatches(xmlDocument, assetlookup); } finally { DeepProfiler.End(); } DeepProfiler.Start("ParseAndProcessXML()"); try { ParseAndProcessXML(xmlDocument, assetlookup); } finally { DeepProfiler.End(); } DeepProfiler.Start("ClearCachedPatches()"); try { ClearCachedPatches(); } finally { DeepProfiler.End(); } DeepProfiler.Start("XmlInheritance.Clear()"); try { XmlInheritance.Clear(); } finally { DeepProfiler.End(); } }
public static void Resolve() { XmlInheritance.ResolveParentsAndChildNodesLinks(); XmlInheritance.ResolveXmlNodes(); }
public static void ParseAndProcessXML(XmlDocument xmlDoc, Dictionary <XmlNode, LoadableXmlAsset> assetlookup) { XmlNodeList childNodes = xmlDoc.DocumentElement.ChildNodes; List <XmlNode> list = new List <XmlNode>(); foreach (object item in childNodes) { list.Add(item as XmlNode); } DeepProfiler.Start("Loading asset nodes " + list.Count); try { for (int i = 0; i < list.Count; i++) { if (list[i].NodeType == XmlNodeType.Element) { LoadableXmlAsset value = null; DeepProfiler.Start("assetlookup.TryGetValue"); try { assetlookup.TryGetValue(list[i], out value); } finally { DeepProfiler.End(); } DeepProfiler.Start("XmlInheritance.TryRegister"); try { XmlInheritance.TryRegister(list[i], value?.mod); } finally { DeepProfiler.End(); } } } } finally { DeepProfiler.End(); } DeepProfiler.Start("XmlInheritance.Resolve()"); try { XmlInheritance.Resolve(); } finally { DeepProfiler.End(); } runningMods.FirstOrDefault(); DeepProfiler.Start("Loading defs for " + list.Count + " nodes"); try { foreach (XmlNode item2 in list) { LoadableXmlAsset loadableXmlAsset = assetlookup.TryGetValue(item2); Def def = DirectXmlLoader.DefFromNode(item2, loadableXmlAsset); if (def != null) { ModContentPack modContentPack = loadableXmlAsset?.mod; if (modContentPack != null) { modContentPack.AddDef(def, loadableXmlAsset.name); } else { patchedDefs.Add(def); } } } } finally { DeepProfiler.End(); } }
public static T ObjectFromXml <T>(XmlNode xmlRoot, bool doPostLoad) { XmlAttribute xmlAttribute = xmlRoot.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } MethodInfo methodInfo = CustomDataLoadMethodOf(typeof(T)); if (methodInfo != null) { xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type = ClassTypeOf <T>(xmlRoot); currentlyInstantiatingObjectOfType.Push(type); T val; try { val = (T)Activator.CreateInstance(type); } finally { currentlyInstantiatingObjectOfType.Pop(); } try { methodInfo.Invoke(val, new object[1] { xmlRoot }); } catch (Exception ex) { Log.Error(string.Concat("Exception in custom XML loader for ", typeof(T), ". Node is:\n ", xmlRoot.OuterXml, "\n\nException is:\n ", ex.ToString())); val = default(T); } if (doPostLoad) { TryDoPostLoad(val); } return(val); } if (typeof(ISlateRef).IsAssignableFrom(typeof(T))) { try { return(ParseHelper.FromString <T>(InnerTextWithReplacedNewlinesOrXML(xmlRoot))); } catch (Exception ex2) { Log.Error(string.Concat("Exception parsing ", xmlRoot.OuterXml, " to type ", typeof(T), ": ", ex2)); } return(default(T)); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.CDATA) { if (typeof(T) != typeof(string)) { Log.Error("CDATA can only be used for strings. Bad xml: " + xmlRoot.OuterXml); return(default(T)); } return((T)(object)xmlRoot.FirstChild.Value); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.Text) { try { return(ParseHelper.FromString <T>(xmlRoot.InnerText)); } catch (Exception ex3) { Log.Error(string.Concat("Exception parsing ", xmlRoot.OuterXml, " to type ", typeof(T), ": ", ex3)); } return(default(T)); } if (Attribute.IsDefined(typeof(T), typeof(FlagsAttribute))) { List <T> list = ListFromXml <T>(xmlRoot); int num = 0; foreach (T item in list) { int num2 = (int)(object)item; num |= num2; } return((T)(object)num); } if (typeof(T).HasGenericDefinition(typeof(List <>))) { Func <XmlNode, object> value = null; if (!listFromXmlMethods.TryGetValue(typeof(T), out value)) { MethodInfo method = typeof(DirectXmlToObject).GetMethod("ListFromXmlReflection", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments = typeof(T).GetGenericArguments(); value = (Func <XmlNode, object>)Delegate.CreateDelegate(typeof(Func <XmlNode, object>), method.MakeGenericMethod(genericArguments)); listFromXmlMethods.Add(typeof(T), value); } return((T)value(xmlRoot)); } if (typeof(T).HasGenericDefinition(typeof(Dictionary <, >))) { Func <XmlNode, object> value2 = null; if (!dictionaryFromXmlMethods.TryGetValue(typeof(T), out value2)) { MethodInfo method2 = typeof(DirectXmlToObject).GetMethod("DictionaryFromXmlReflection", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments2 = typeof(T).GetGenericArguments(); value2 = (Func <XmlNode, object>)Delegate.CreateDelegate(typeof(Func <XmlNode, object>), method2.MakeGenericMethod(genericArguments2)); dictionaryFromXmlMethods.Add(typeof(T), value2); } return((T)value2(xmlRoot)); } if (!xmlRoot.HasChildNodes) { if (typeof(T) == typeof(string)) { return((T)(object)""); } XmlAttribute xmlAttribute2 = xmlRoot.Attributes["IsNull"]; if (xmlAttribute2 != null && xmlAttribute2.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } if (typeof(T).IsGenericType) { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(List <>) || genericTypeDefinition == typeof(HashSet <>) || genericTypeDefinition == typeof(Dictionary <, >)) { return(Activator.CreateInstance <T>()); } } } xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type2 = ClassTypeOf <T>(xmlRoot); Type type3 = Nullable.GetUnderlyingType(type2) ?? type2; currentlyInstantiatingObjectOfType.Push(type3); T val2; try { val2 = (T)Activator.CreateInstance(type3); } finally { currentlyInstantiatingObjectOfType.Pop(); } HashSet <string> hashSet = null; if (xmlRoot.ChildNodes.Count > 1) { hashSet = new HashSet <string>(); } for (int i = 0; i < xmlRoot.ChildNodes.Count; i++) { XmlNode xmlNode = xmlRoot.ChildNodes[i]; if (xmlNode is XmlComment) { continue; } if (xmlRoot.ChildNodes.Count > 1) { if (hashSet.Contains(xmlNode.Name)) { Log.Error(string.Concat("XML ", typeof(T), " defines the same field twice: ", xmlNode.Name, ".\n\nField contents: ", xmlNode.InnerText, ".\n\nWhole XML:\n\n", xmlRoot.OuterXml)); } else { hashSet.Add(xmlNode.Name); } } FieldInfo value3 = null; DeepProfiler.Start("GetFieldInfoForType"); try { value3 = GetFieldInfoForType(val2.GetType(), xmlNode.Name, xmlRoot); } finally { DeepProfiler.End(); } if (value3 == null) { DeepProfiler.Start("Field search"); try { FieldAliasCache key = new FieldAliasCache(val2.GetType(), xmlNode.Name); if (!fieldAliases.TryGetValue(key, out value3)) { FieldInfo[] fields = val2.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fields) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(LoadAliasAttribute), inherit: true); for (int k = 0; k < customAttributes.Length; k++) { if (((LoadAliasAttribute)customAttributes[k]).alias.EqualsIgnoreCase(xmlNode.Name)) { value3 = fieldInfo; break; } } if (value3 != null) { break; } } fieldAliases.Add(key, value3); } } finally { DeepProfiler.End(); } } if (value3 != null && value3.TryGetAttribute <UnsavedAttribute>() != null && !value3.TryGetAttribute <UnsavedAttribute>().allowLoading) { Log.Error("XML error: " + xmlNode.OuterXml + " corresponds to a field in type " + val2.GetType().Name + " which has an Unsaved attribute. Context: " + xmlRoot.OuterXml); } else if (value3 == null) { DeepProfiler.Start("Field search 2"); try { bool flag = false; XmlAttribute xmlAttribute3 = xmlNode.Attributes?["IgnoreIfNoMatchingField"]; if (xmlAttribute3 != null && xmlAttribute3.Value.ToUpperInvariant() == "TRUE") { flag = true; } else { object[] customAttributes = val2.GetType().GetCustomAttributes(typeof(IgnoreSavedElementAttribute), inherit: true); for (int j = 0; j < customAttributes.Length; j++) { if (string.Equals(((IgnoreSavedElementAttribute)customAttributes[j]).elementToIgnore, xmlNode.Name, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } } if (!flag) { Log.Error("XML error: " + xmlNode.OuterXml + " doesn't correspond to any field in type " + val2.GetType().Name + ". Context: " + xmlRoot.OuterXml); } } finally { DeepProfiler.End(); } } else if (typeof(Def).IsAssignableFrom(value3.FieldType)) { if (xmlNode.InnerText.NullOrEmpty()) { value3.SetValue(val2, null); continue; } XmlAttribute xmlAttribute4 = xmlNode.Attributes["MayRequire"]; DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(val2, value3, xmlNode.InnerText, xmlAttribute4?.Value.ToLower()); } else { object obj = null; try { obj = GetObjectFromXmlMethod(value3.FieldType)(xmlNode, doPostLoad); } catch (Exception ex4) { Log.Error("Exception loading from " + xmlNode.ToString() + ": " + ex4.ToString()); continue; } if (!typeof(T).IsValueType) { value3.SetValue(val2, obj); continue; } object obj2 = val2; value3.SetValue(obj2, obj); val2 = (T)obj2; } } if (doPostLoad) { TryDoPostLoad(val2); } return(val2); }
private static void CheckForDuplicateNodes(XmlNode node, XmlNode root) { XmlInheritance.tempUsedNodeNames.Clear(); IEnumerator enumerator = node.ChildNodes.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; XmlNode xmlNode = (XmlNode)obj; if (xmlNode.NodeType == XmlNodeType.Element && !(xmlNode.Name == "li")) { if (XmlInheritance.tempUsedNodeNames.Contains(xmlNode.Name)) { Log.Error(string.Concat(new string[] { "XML error: Duplicate XML node name ", xmlNode.Name, " in this XML block: ", node.OuterXml, (node == root) ? string.Empty : ("\n\nRoot node: " + root.OuterXml) }), false); } else { XmlInheritance.tempUsedNodeNames.Add(xmlNode.Name); } } } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } XmlInheritance.tempUsedNodeNames.Clear(); IEnumerator enumerator2 = node.ChildNodes.GetEnumerator(); try { while (enumerator2.MoveNext()) { object obj2 = enumerator2.Current; XmlNode xmlNode2 = (XmlNode)obj2; if (xmlNode2.NodeType == XmlNodeType.Element) { XmlInheritance.CheckForDuplicateNodes(xmlNode2, root); } } } finally { IDisposable disposable2; if ((disposable2 = (enumerator2 as IDisposable)) != null) { disposable2.Dispose(); } } }
private static void RecursiveNodeCopyOverwriteElements(XmlNode child, XmlNode current) { XmlAttribute xmlAttribute = child.Attributes["Inherit"]; if (xmlAttribute != null && xmlAttribute.Value.ToLower() == "false") { while (current.HasChildNodes) { current.RemoveChild(current.FirstChild); } IEnumerator enumerator = child.GetEnumerator(); try { while (enumerator.MoveNext()) { object obj = enumerator.Current; XmlNode node = (XmlNode)obj; XmlNode newChild = current.OwnerDocument.ImportNode(node, true); current.AppendChild(newChild); } } finally { IDisposable disposable; if ((disposable = (enumerator as IDisposable)) != null) { disposable.Dispose(); } } return; } current.Attributes.RemoveAll(); XmlAttributeCollection attributes = child.Attributes; for (int i = 0; i < attributes.Count; i++) { XmlAttribute node2 = (XmlAttribute)current.OwnerDocument.ImportNode(attributes[i], true); current.Attributes.Append(node2); } List <XmlElement> list = new List <XmlElement>(); XmlNode xmlNode = null; IEnumerator enumerator2 = child.GetEnumerator(); try { while (enumerator2.MoveNext()) { object obj2 = enumerator2.Current; XmlNode xmlNode2 = (XmlNode)obj2; if (xmlNode2.NodeType == XmlNodeType.Text) { xmlNode = xmlNode2; } else if (xmlNode2.NodeType == XmlNodeType.Element) { list.Add((XmlElement)xmlNode2); } } } finally { IDisposable disposable2; if ((disposable2 = (enumerator2 as IDisposable)) != null) { disposable2.Dispose(); } } if (xmlNode != null) { for (int j = current.ChildNodes.Count - 1; j >= 0; j--) { XmlNode xmlNode3 = current.ChildNodes[j]; if (xmlNode3.NodeType != XmlNodeType.Attribute) { current.RemoveChild(xmlNode3); } } XmlNode newChild2 = current.OwnerDocument.ImportNode(xmlNode, true); current.AppendChild(newChild2); } else if (!list.Any <XmlElement>()) { bool flag = false; IEnumerator enumerator3 = current.ChildNodes.GetEnumerator(); try { while (enumerator3.MoveNext()) { object obj3 = enumerator3.Current; XmlNode xmlNode4 = (XmlNode)obj3; if (xmlNode4.NodeType == XmlNodeType.Element) { flag = true; break; } } } finally { IDisposable disposable3; if ((disposable3 = (enumerator3 as IDisposable)) != null) { disposable3.Dispose(); } } if (!flag) { IEnumerator enumerator4 = current.ChildNodes.GetEnumerator(); try { while (enumerator4.MoveNext()) { object obj4 = enumerator4.Current; XmlNode xmlNode5 = (XmlNode)obj4; if (xmlNode5.NodeType != XmlNodeType.Attribute) { current.RemoveChild(xmlNode5); } } } finally { IDisposable disposable4; if ((disposable4 = (enumerator4 as IDisposable)) != null) { disposable4.Dispose(); } } } } else { for (int k = 0; k < list.Count; k++) { XmlElement xmlElement = list[k]; if (xmlElement.Name == "li") { XmlNode newChild3 = current.OwnerDocument.ImportNode(xmlElement, true); current.AppendChild(newChild3); } else { XmlElement xmlElement2 = current[xmlElement.Name]; if (xmlElement2 != null) { XmlInheritance.RecursiveNodeCopyOverwriteElements(xmlElement, xmlElement2); } else { XmlNode newChild4 = current.OwnerDocument.ImportNode(xmlElement, true); current.AppendChild(newChild4); } } } } }
public static T ObjectFromXml <T>(XmlNode xmlRoot, bool doPostLoad) where T : new() { MethodInfo methodInfo = DirectXmlToObject.CustomDataLoadMethodOf(typeof(T)); if (methodInfo != null) { xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); T val = (T)Activator.CreateInstance(type); try { methodInfo.Invoke(val, new object[1] { xmlRoot }); } catch (Exception ex) { Log.Error("Exception in custom XML loader for " + typeof(T) + ". Node is:\n " + xmlRoot.OuterXml + "\n\nException is:\n " + ex.ToString()); val = default(T); } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(val); } return(val); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.CDATA) { if (typeof(T) != typeof(string)) { Log.Error("CDATA can only be used for strings. Bad xml: " + xmlRoot.OuterXml); return(default(T)); } return((T)(object)xmlRoot.FirstChild.Value); } if (xmlRoot.ChildNodes.Count == 1 && xmlRoot.FirstChild.NodeType == XmlNodeType.Text) { try { return((T)ParseHelper.FromString(xmlRoot.InnerText, typeof(T))); } catch (Exception ex2) { Log.Error("Exception parsing " + xmlRoot.OuterXml + " to type " + typeof(T) + ": " + ex2); } return(default(T)); } if (Attribute.IsDefined(typeof(T), typeof(FlagsAttribute))) { List <T> list = DirectXmlToObject.ListFromXml <T>(xmlRoot); int num = 0; foreach (T item in list) { int num2 = (int)(object)item; num |= num2; } return((T)(object)num); } if (typeof(T).HasGenericDefinition(typeof(List <>))) { MethodInfo method = typeof(DirectXmlToObject).GetMethod("ListFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments = typeof(T).GetGenericArguments(); MethodInfo methodInfo2 = method.MakeGenericMethod(genericArguments); object[] parameters = new object[1] { xmlRoot }; object obj = methodInfo2.Invoke(null, parameters); return((T)obj); } if (typeof(T).HasGenericDefinition(typeof(Dictionary <, >))) { MethodInfo method2 = typeof(DirectXmlToObject).GetMethod("DictionaryFromXml", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); Type[] genericArguments2 = typeof(T).GetGenericArguments(); MethodInfo methodInfo3 = method2.MakeGenericMethod(genericArguments2); object[] parameters2 = new object[1] { xmlRoot }; object obj2 = methodInfo3.Invoke(null, parameters2); return((T)obj2); } if (!xmlRoot.HasChildNodes) { if (typeof(T) == typeof(string)) { return((T)(object)string.Empty); } XmlAttribute xmlAttribute = xmlRoot.Attributes["IsNull"]; if (xmlAttribute != null && xmlAttribute.Value.ToUpperInvariant() == "TRUE") { return(default(T)); } if (typeof(T).IsGenericType) { Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition(); if (genericTypeDefinition != typeof(List <>) && genericTypeDefinition != typeof(HashSet <>) && genericTypeDefinition != typeof(Dictionary <, >)) { goto IL_03ed; } return(new T()); } } goto IL_03ed; IL_03ed: xmlRoot = XmlInheritance.GetResolvedNodeFor(xmlRoot); Type type2 = DirectXmlToObject.ClassTypeOf <T>(xmlRoot); T val2 = (T)Activator.CreateInstance(type2); List <string> list2 = null; if (xmlRoot.ChildNodes.Count > 1) { list2 = new List <string>(); } for (int i = 0; i < xmlRoot.ChildNodes.Count; i++) { XmlNode xmlNode = xmlRoot.ChildNodes[i]; if (!(xmlNode is XmlComment)) { if (xmlRoot.ChildNodes.Count > 1) { if (list2.Contains(xmlNode.Name)) { Log.Error("XML " + typeof(T) + " defines the same field twice: " + xmlNode.Name + ".\n\nField contents: " + xmlNode.InnerText + ".\n\nWhole XML:\n\n" + xmlRoot.OuterXml); } else { list2.Add(xmlNode.Name); } } FieldInfo fieldInfo = DirectXmlToObject.GetFieldInfoForType(val2.GetType(), xmlNode.Name, xmlRoot); if (fieldInfo == null) { FieldInfo[] fields = val2.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); int num3 = 0; while (num3 < fields.Length) { FieldInfo fieldInfo2 = fields[num3]; object[] customAttributes = fieldInfo2.GetCustomAttributes(typeof(LoadAliasAttribute), true); foreach (object obj3 in customAttributes) { string alias = ((LoadAliasAttribute)obj3).alias; if (alias.EqualsIgnoreCase(xmlNode.Name)) { fieldInfo = fieldInfo2; break; } } if (fieldInfo == null) { num3++; continue; } break; } } if (fieldInfo == null) { bool flag = false; object[] customAttributes2 = val2.GetType().GetCustomAttributes(typeof(IgnoreSavedElementAttribute), true); foreach (object obj4 in customAttributes2) { string elementToIgnore = ((IgnoreSavedElementAttribute)obj4).elementToIgnore; if (string.Equals(elementToIgnore, xmlNode.Name, StringComparison.OrdinalIgnoreCase)) { flag = true; break; } } if (!flag) { Log.Error("XML error: " + xmlNode.OuterXml + " doesn't correspond to any field in type " + val2.GetType().Name + "."); } } else if (typeof(Def).IsAssignableFrom(fieldInfo.FieldType)) { if (xmlNode.InnerText.NullOrEmpty()) { fieldInfo.SetValue(val2, null); } else { DirectXmlCrossRefLoader.RegisterObjectWantsCrossRef(val2, fieldInfo, xmlNode.InnerText); } } else { object obj5 = null; try { MethodInfo method3 = typeof(DirectXmlToObject).GetMethod("ObjectFromXml", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); MethodInfo methodInfo4 = method3.MakeGenericMethod(fieldInfo.FieldType); obj5 = methodInfo4.Invoke(null, new object[2] { xmlNode, doPostLoad }); } catch (Exception ex3) { Log.Error("Exception loading from " + xmlNode.ToString() + ": " + ex3.ToString()); continue; } if (!typeof(T).IsValueType) { fieldInfo.SetValue(val2, obj5); } else { object obj6 = val2; fieldInfo.SetValue(obj6, obj5); val2 = (T)obj6; } } } } if (doPostLoad) { DirectXmlToObject.TryDoPostLoad(val2); } return(val2); }
public static void LoadAllActiveMods() { XmlInheritance.Clear(); int num = 0; foreach (ModMetaData current in ModsConfig.ActiveModsInLoadOrder.ToList <ModMetaData>()) { DeepProfiler.Start("Initializing " + current); if (!current.RootDir.Exists) { ModsConfig.SetActive(current.Identifier, false); Log.Warning(string.Concat(new object[] { "Failed to find active mod ", current.Name, "(", current.Identifier, ") at ", current.RootDir })); DeepProfiler.End(); } else { ModContentPack item = new ModContentPack(current.RootDir, num, current.Name); num++; LoadedModManager.runningMods.Add(item); DeepProfiler.End(); } } for (int i = 0; i < LoadedModManager.runningMods.Count; i++) { ModContentPack modContentPack = LoadedModManager.runningMods[i]; DeepProfiler.Start("Loading " + modContentPack + " content"); modContentPack.ReloadContent(); DeepProfiler.End(); } foreach (Type type in typeof(Mod).InstantiableDescendantsAndSelf()) { if (!LoadedModManager.runningModClasses.ContainsKey(type)) { ModContentPack modContentPack2 = (from modpack in LoadedModManager.runningMods where modpack.assemblies.loadedAssemblies.Contains(type.Assembly) select modpack).FirstOrDefault <ModContentPack>(); LoadedModManager.runningModClasses[type] = (Mod)Activator.CreateInstance(type, new object[] { modContentPack2 }); } } for (int j = 0; j < LoadedModManager.runningMods.Count; j++) { ModContentPack modContentPack3 = LoadedModManager.runningMods[j]; DeepProfiler.Start("Loading " + modContentPack3); modContentPack3.LoadDefs(LoadedModManager.runningMods.SelectMany((ModContentPack rm) => rm.Patches)); DeepProfiler.End(); } foreach (ModContentPack current2 in LoadedModManager.runningMods) { foreach (PatchOperation current3 in current2.Patches) { current3.Complete(current2.Name); } current2.ClearPatchesCache(); } XmlInheritance.Clear(); }