/// <summary> /// Construct a new ParentedEXmlObject from an existing Base. /// </summary> /// <param name="baseObject">The existing base.</param> protected ParentedEXmlObject(EXmlBase baseObject) { Name = baseObject.Name; Elements = baseObject.Elements; Children = new List <ParentedEXmlObject>(); EXmlData data = baseObject as EXmlData; EXmlMeta meta = baseObject as EXmlMeta; EXmlProperty prop = baseObject as EXmlProperty; if (data != null) { Attribute = new EXmlAttribute { AttributeName = "template", Value = data.Template }; } else if (meta != null) { Attribute = new EXmlAttribute { AttributeName = "comment", Value = meta.Comment, }; } else if (prop != null) { Attribute = new EXmlAttribute { AttributeName = "value", Value = prop.Value }; } else { Attribute = null; } }
/// <summary> /// Converts an EXmlBase and all of its child elements into an XXMLBase. Do not implicitly or explicitly cast EXmlBase into XXMLObject as it will not populate the parent data. /// </summary> /// <param name="obj">The EXmlBase to convert.</param> /// <param name="parent">The parent object of the XXMLObject. If you are converting an EXmlBase manually, it is advised that you do not specify this.</param> public static ParentedEXmlObject TransformEntireElementTree(EXmlBase obj, ParentedEXmlObject parent = null) { ParentedEXmlObject root = new ParentedEXmlObject(obj); root.Parent = parent; foreach (EXmlBase exml in obj.Elements) { root.Children.Add(TransformEntireElementTree(exml, root)); } return(root); }
/// <summary> /// Construct a new parallel MBIN file. /// </summary> /// <param name="sourceFile">The vanilla version of this MBIN file.</param> /// <param name="filesToMerge">A list of modified MBIN files that should have their contents merged.</param> /// <param name="vanillaPath">The path to the vanilla MBIN file.</param> /// <param name="mergePaths">The paths to all of the files being merged.</param> public MultiMBINEnumerator(MBINFile sourceFile, IEnumerable <MBINFile> filesToMerge, string vanillaPath, IEnumerable <string> mergePaths) { VanillaEXML = sourceFile.GetData().SerializeEXml(false); VanillaEXMLParented = ParentedEXmlObject.TransformEntireElementTree(VanillaEXML); VanillaPath = vanillaPath; ModEXMLs = new List <EXmlBase>(filesToMerge.Count()); ModEXMLsParented = new List <ParentedEXmlObject>(filesToMerge.Count()); ModPaths = mergePaths.ToList(); foreach (MBINFile file in filesToMerge) { EXmlBase data = file.GetData().SerializeEXml(false); ModEXMLs.Add(data); ModEXMLsParented.Add(ParentedEXmlObject.TransformEntireElementTree(data)); } }
/// <summary> /// Converts this custom object back into an EXmlBase so that it can be serialized back into an MBIN. /// </summary> /// <returns></returns> public EXmlBase ConvertToSerializableEXML(EXmlBase parent = null, ParentedEXmlObject parentAsNative = null) { EXmlBase root; if (parent == null) { // Attribute is gonna be template. If it's not, there's a problem. if (Attribute.AttributeName != "template") { throw new Exception("Top level attribute is not a template!"); } EXmlData newData = new EXmlData(); newData.Name = Name; newData.Template = Attribute.Value; root = newData; } else { root = parent; } //EXmlBase root = parent ?? this; parentAsNative = parentAsNative ?? this; root.Elements = new List <EXmlBase>(); foreach (ParentedEXmlObject child in parentAsNative.Children) { EXmlBase childAsBase = child; if (child.Attribute?.AttributeName == "template") { EXmlData childAsTyped = new EXmlData(); childAsTyped.Name = childAsBase.Name; childAsTyped.Elements = childAsBase.Elements; childAsTyped.Template = child.Attribute.Value; root.Elements.Add(childAsTyped); childAsBase = childAsTyped; } else if (child.Attribute?.AttributeName == "value") { EXmlProperty childAsTyped = new EXmlProperty(); childAsTyped.Name = childAsBase.Name; childAsTyped.Elements = childAsBase.Elements; childAsTyped.Value = child.Attribute.Value; root.Elements.Add(childAsTyped); childAsBase = childAsTyped; } else if (child.Attribute?.AttributeName == "comment") { EXmlMeta childAsTyped = new EXmlMeta(); childAsTyped.Name = childAsBase.Name; childAsTyped.Elements = childAsBase.Elements; childAsTyped.Comment = child.Attribute.Value; root.Elements.Add(childAsTyped); childAsBase = childAsTyped; } else { root.Elements.Add(childAsBase); } ConvertToSerializableEXML(childAsBase, child); } return(root); }