| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| using System; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Xml; |
| using UnityEngine; |
|
|
| namespace Mujoco { |
|
|
| public class MjXmlModifiers { |
| private XmlDocument _root; |
|
|
| public MjXmlModifiers(XmlDocument root) { |
| _root = root; |
| } |
|
|
| public void ApplyModifiersToElement(XmlElement element, string elementName=null) { |
| |
| if (elementName == null) { |
| elementName = element.Name; |
| } |
|
|
| |
| |
| var aggregateDefaults = _root.CreateElement("aggregate"); |
| |
| var rootDefaultLeaf = _root.SelectSingleNode($"/mujoco/default/{elementName}") as XmlElement; |
| if (rootDefaultLeaf != null) { |
| CopyAttributes(rootDefaultLeaf, aggregateDefaults); |
| } |
| |
| var classes = GetApplicableDefaultClasses(element).Reverse(); |
| foreach (var className in classes) { |
| var defaultClassElement = |
| _root.SelectSingleNode($"descendant::default[@class='{className}']") as XmlElement; |
| |
| var ancestors = GetDefaultAncestry(defaultClassElement, elementName).Reverse(); |
| foreach (var defaultAncestor in ancestors) { |
| CopyAttributesOverwriteExisting(defaultAncestor, aggregateDefaults); |
| } |
| } |
| |
| CopyAttributes(aggregateDefaults, element); |
| } |
|
|
| private IEnumerable<XmlElement> GetDefaultAncestry(XmlElement classElement, string nodeType) { |
| var defaultElement = classElement; |
| var top = _root.SelectSingleNode("/mujoco/default"); |
| while (defaultElement != top) { |
| foreach (var element in defaultElement.ChildNodes) { |
| if (((XmlElement)element).Name == nodeType) { |
| yield return (XmlElement)element; |
| } |
| } |
| defaultElement = defaultElement.ParentNode as XmlElement; |
| } |
| } |
|
|
| |
| |
| public static IEnumerable<string> GetApplicableDefaultClasses(XmlElement modifiedElement) { |
| var nodeType = modifiedElement.Name; |
|
|
| |
| |
| |
| var inheritance = new List<string>(); |
| var element = modifiedElement; |
| while (element != null) { |
| var className = element.GetStringAttribute("childclass", string.Empty); |
| if (element == modifiedElement) { |
| |
| |
| className = element.GetStringAttribute("class", className); |
| } |
| if (!string.IsNullOrEmpty(className) && !inheritance.Contains(className)) { |
| inheritance.Add(className); |
| } |
| element = element.ParentNode as XmlElement; |
| } |
| return inheritance; |
| } |
|
|
| private static string GetElementClass(XmlElement element) { |
| var elementClass = element.GetStringAttribute("class", string.Empty); |
| if (string.IsNullOrEmpty(elementClass)) { |
| elementClass = element.GetStringAttribute("childclass", string.Empty); |
| } |
| return elementClass; |
| } |
|
|
| public static void CopyAttributes(XmlElement from, XmlElement to) { |
| foreach (XmlAttribute attribute in from.Attributes) { |
| if (to.HasAttribute(attribute.Name) == false) { |
| to.SetAttribute(attribute.Name, attribute.Value); |
| } |
| } |
| } |
|
|
| public static void CopyAttributesOverwriteExisting(XmlElement from, XmlElement to) { |
| foreach (XmlAttribute attribute in from.Attributes) { |
| to.SetAttribute(attribute.Name, attribute.Value); |
| } |
| } |
| } |
| } |
|
|