Skip to content

Commit

Permalink
Don't repeatedly access Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Jan 8, 2024
1 parent 4bb4f5f commit 6fae01a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions BeatSaberMarkupLanguage/BSMLParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void HandleNode(XmlNode node, GameObject parent, BSMLParserParams parserP

private void HandleTagNode(XmlNode node, GameObject parent, BSMLParserParams parserParams, out IEnumerable<ComponentTypeWithData> componentInfo)
{
if (!tags.TryGetValue(node.Name, out BSMLTag currentTag))
if (!this.tags.TryGetValue(node.Name, out BSMLTag currentTag))
{
throw new TagNotFoundException(node.Name);
}
Expand Down Expand Up @@ -322,25 +322,27 @@ private void HandleTagNode(XmlNode node, GameObject parent, BSMLParserParams par
}

object host = parserParams.host;
if (host != null && node.Attributes["id"] != null)
XmlAttribute id = node.Attributes["id"];
if (host != null && id != null)
{
foreach (FieldInfo fieldInfo in host.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
if (fieldInfo.GetCustomAttribute<UIComponent>(true)?.id == node.Attributes["id"].Value)
if (fieldInfo.GetCustomAttribute<UIComponent>(true)?.id == id.Value)
{
fieldInfo.SetValue(host, GetExternalComponent(currentNode, fieldInfo.FieldType));
}

if (fieldInfo.GetCustomAttribute<UIObject>(true)?.id == node.Attributes["id"].Value)
if (fieldInfo.GetCustomAttribute<UIObject>(true)?.id == id.Value)
{
fieldInfo.SetValue(host, currentNode);
}
}
}

if (node.Attributes["tags"] != null)
XmlAttribute tags = node.Attributes["tags"];
if (tags != null)
{
parserParams.AddObjectTags(currentNode, node.Attributes["tags"].Value.Split(','));
parserParams.AddObjectTags(currentNode, tags.Value.Split(','));
}

IEnumerable<ComponentTypeWithData> childrenComponents = Enumerable.Empty<ComponentTypeWithData>();
Expand Down Expand Up @@ -410,9 +412,10 @@ private Dictionary<string, string> GetParameters(XmlNode node, Dictionary<string

foreach (string alias in aliasList)
{
if (node.Attributes[alias] != null)
XmlAttribute attribute = node.Attributes[alias];
if (attribute != null)
{
string value = node.Attributes[alias].Value;
string value = attribute.Value;
if (value.StartsWith(RetrieveValuePrefix, StringComparison.Ordinal))
{
try
Expand Down

0 comments on commit 6fae01a

Please sign in to comment.