Client LuaCsForBarotrauma
ContentXElement.cs
1 #nullable enable
2 using Microsoft.Xna.Framework;
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Immutable;
6 using System.Diagnostics.CodeAnalysis;
7 using System.Linq;
8 using System.Xml.Linq;
9 
10 namespace Barotrauma
11 {
12  public sealed class ContentXElement
13  {
14  public ContentPackage? ContentPackage { get; private set; }
15  public readonly XElement Element;
16 
17  public ContentXElement(ContentPackage? contentPackage, XElement element)
18  {
19  ContentPackage = contentPackage;
20  Element = element;
21  }
22 
23  [return: NotNullIfNotNull("cxe")]
24  public static implicit operator XElement?(ContentXElement? cxe) => cxe?.Element;
25  //public static implicit operator ContentXElement?(XElement? xe) => xe is null ? null : new ContentXElement(null, xe);
26 
27  public XName Name => Element.Name;
28  public Identifier NameAsIdentifier() => Element.NameAsIdentifier();
29 
30  public string BaseUri => Element.BaseUri;
31 
32  public XDocument? Document => Element.Document;
33 
34  public ContentXElement? FirstElement() => Elements().FirstOrDefault();
35 
36  public ContentXElement? Parent => Element.Parent is null ? null : new ContentXElement(ContentPackage, Element.Parent);
37  public bool HasElements => Element.HasElements;
38 
39  public bool IsOverride() => Element.IsOverride();
40 
41  public bool ComesAfter(ContentXElement other) => Element.ComesAfter(other.Element);
42 
43  public ContentXElement? GetChildElement(string name)
44  => Element.GetChildElement(name) is { } elem ? new ContentXElement(ContentPackage, elem) : null;
45 
46  public IEnumerable<ContentXElement> Elements()
47  => Element.Elements().Select(e => new ContentXElement(ContentPackage, e));
48 
49  public IEnumerable<ContentXElement> ElementsBeforeSelf()
50  => Element.ElementsBeforeSelf().Select(e => new ContentXElement(ContentPackage, e));
51 
52  public IEnumerable<ContentXElement> Descendants()
53  => Element.Descendants().Select(e => new ContentXElement(ContentPackage, e));
54 
55  public IEnumerable<ContentXElement> GetChildElements(string name)
56  => Elements().Where(e => string.Equals(name, e.Name.LocalName, StringComparison.OrdinalIgnoreCase));
57 
58  public XAttribute? GetAttribute(string name) => Element.GetAttribute(name);
59 
60  public IEnumerable<XAttribute> Attributes() => Element.Attributes();
61  public IEnumerable<XAttribute> Attributes(string name) => Element.Attributes(name);
62 
63  public string ElementInnerText() => Element.ElementInnerText();
64 
65  public Identifier GetAttributeIdentifier(string key, string def) => Element.GetAttributeIdentifier(key, def);
66  public Identifier GetAttributeIdentifier(string key, Identifier def) => Element.GetAttributeIdentifier(key, def);
67 
68  [return: NotNullIfNotNull("def")]
69  public Identifier[] GetAttributeIdentifierArray(Identifier[] def, params string[] keys) => Element.GetAttributeIdentifierArray(def, keys);
70  [return: NotNullIfNotNull("def")]
71  public Identifier[] GetAttributeIdentifierArray(string key, Identifier[] def, bool trim = true) => Element.GetAttributeIdentifierArray(key, def, trim);
72  [return: NotNullIfNotNull("def")]
73  public ImmutableHashSet<Identifier> GetAttributeIdentifierImmutableHashSet(string key, ImmutableHashSet<Identifier>? def, bool trim = true) => Element.GetAttributeIdentifierImmutableHashSet(key, def, trim);
74  [return: NotNullIfNotNull(parameterName: "def")]
75  public string? GetAttributeString(string key, string? def) => Element.GetAttributeString(key, def);
76  public string GetAttributeStringUnrestricted(string key, string def) => Element.GetAttributeStringUnrestricted(key, def);
77  public string[]? GetAttributeStringArray(string key, string[]? def, bool convertToLowerInvariant = false) => Element.GetAttributeStringArray(key, def, convertToLowerInvariant);
78  public ContentPath? GetAttributeContentPath(string key) => Element.GetAttributeContentPath(key, ContentPackage);
79  public int GetAttributeInt(string key, int def) => Element.GetAttributeInt(key, def);
80  public ushort GetAttributeUInt16(string key, ushort def) => Element.GetAttributeUInt16(key, def);
81  public int[]? GetAttributeIntArray(string key, int[]? def) => Element.GetAttributeIntArray(key, def);
82  public ushort[]? GetAttributeUshortArray(string key, ushort[]? def) => Element.GetAttributeUshortArray(key, def);
83  public float GetAttributeFloat(string key, float def) => Element.GetAttributeFloat(key, def);
84  public float[]? GetAttributeFloatArray(string key, float[]? def) => Element.GetAttributeFloatArray(key, def);
85  public float GetAttributeFloat(float def, params string[] keys) => Element.GetAttributeFloat(def, keys);
86  public bool GetAttributeBool(string key, bool def) => Element.GetAttributeBool(key, def);
87  public Point GetAttributePoint(string key, in Point def) => Element.GetAttributePoint(key, def);
88  public Vector2 GetAttributeVector2(string key, in Vector2 def) => Element.GetAttributeVector2(key, def);
89  public Vector4 GetAttributeVector4(string key, in Vector4 def) => Element.GetAttributeVector4(key, def);
90  public Color GetAttributeColor(string key, in Color def) => Element.GetAttributeColor(key, def);
91  public Color? GetAttributeColor(string key) => Element.GetAttributeColor(key);
92  public Color[]? GetAttributeColorArray(string key, Color[]? def) => Element.GetAttributeColorArray(key, def);
93  public Rectangle GetAttributeRect(string key, in Rectangle def) => Element.GetAttributeRect(key, def);
94  public Version GetAttributeVersion(string key, Version def) => Element.GetAttributeVersion(key, def);
95  public T GetAttributeEnum<T>(string key, in T def) where T : struct, Enum => Element.GetAttributeEnum(key, def);
96  public T[] GetAttributeEnumArray<T>(string key, T[] def) where T : struct, Enum => Element.GetAttributeEnumArray(key, def);
97  public (T1, T2) GetAttributeTuple<T1, T2>(string key, in (T1, T2) def) => Element.GetAttributeTuple(key, def);
98  public (T1, T2)[] GetAttributeTupleArray<T1, T2>(string key, in (T1, T2)[] def) => Element.GetAttributeTupleArray(key, def);
99  public Range<int> GetAttributeRange(string key, in Range<int> def) => Element.GetAttributeRange(key, def);
100 
101  public Identifier VariantOf() => Element.VariantOf();
102 
103  public bool DoesAttributeReferenceFileNameAlone(string key) => Element.DoesAttributeReferenceFileNameAlone(key);
104 
105  public string ParseContentPathFromUri() => Element.ParseContentPathFromUri();
106 
107  public void SetAttributeValue(string key, string val) => Element.SetAttributeValue(key, val);
108 
109  public void Add(ContentXElement elem)
110  {
111  Element.Add(elem.Element);
113  #warning TODO: update %ModDir% instances in case the content package changes
114  }
115 
116  public void AddFirst(ContentXElement elem)
117  {
118  Element.AddFirst(elem.Element);
120  #warning TODO: update %ModDir% instances in case the content package changes
121  }
122 
123  public void AddAfterSelf(ContentXElement elem)
124  {
125  Element.AddAfterSelf(elem.Element);
127  #warning TODO: update %ModDir% instances in case the content package changes
128  }
129 
130  public void Remove() => Element.Remove();
131 
132  public override bool Equals(object? obj)
133  {
134  return obj is ContentXElement element && this == element;
135  }
136 
137  public override int GetHashCode()
138  {
139  return HashCode.Combine(ContentPackage, Element);
140  }
141 
142  public static bool operator ==(in ContentXElement? a, in ContentXElement? b)
143  {
144  return a?.ContentPackage == b?.ContentPackage && a?.Element == b?.Element;
145  }
146 
147  public static bool operator !=(in ContentXElement? a, in ContentXElement? b) =>
148  !(a == b);
149  }
150 
151  public static class ContentXElementExtensions
152  {
153  public static ContentXElement FromPackage(this XElement element, ContentPackage? contentPackage)
154  => new ContentXElement(contentPackage, element);
155 
156  public static IEnumerable<ContentXElement> Elements(this IEnumerable<ContentXElement> elements)
157  => elements.SelectMany(e => e.Elements());
158  }
159 }
void Add(ContentXElement elem)
override bool Equals(object? obj)
bool ComesAfter(ContentXElement other)
string? GetAttributeString(string key, string? def)
Identifier[] GetAttributeIdentifierArray(Identifier[] def, params string[] keys)
Range< int > GetAttributeRange(string key, in Range< int > def)
Point GetAttributePoint(string key, in Point def)
int?[] GetAttributeIntArray(string key, int[]? def)
IEnumerable< XAttribute > Attributes()
void AddAfterSelf(ContentXElement elem)
T GetAttributeEnum< T >(string key, in T def)
T[] GetAttributeEnumArray< T >(string key, T[] def)
void AddFirst(ContentXElement elem)
Vector4 GetAttributeVector4(string key, in Vector4 def)
IEnumerable< ContentXElement > Descendants()
Identifier[] GetAttributeIdentifierArray(string key, Identifier[] def, bool trim=true)
Color GetAttributeColor(string key, in Color def)
float GetAttributeFloat(string key, float def)
ContentPackage? ContentPackage
IEnumerable< ContentXElement > GetChildElements(string name)
static bool operator!=(in ContentXElement? a, in ContentXElement? b)
float?[] GetAttributeFloatArray(string key, float[]? def)
IEnumerable< ContentXElement > ElementsBeforeSelf()
string GetAttributeStringUnrestricted(string key, string def)
Identifier NameAsIdentifier()
IEnumerable< ContentXElement > Elements()
bool DoesAttributeReferenceFileNameAlone(string key)
readonly XElement Element
Vector2 GetAttributeVector2(string key, in Vector2 def)
ContentPath? GetAttributeContentPath(string key)
ushort GetAttributeUInt16(string key, ushort def)
IEnumerable< XAttribute > Attributes(string name)
ContentXElement? FirstElement()
static bool operator==(in ContentXElement? a, in ContentXElement? b)
ushort?[] GetAttributeUshortArray(string key, ushort[]? def)
Version GetAttributeVersion(string key, Version def)
Color?[] GetAttributeColorArray(string key, Color[]? def)
ContentXElement? GetChildElement(string name)
Color? GetAttributeColor(string key)
ContentXElement(ContentPackage? contentPackage, XElement element)
bool GetAttributeBool(string key, bool def)
float GetAttributeFloat(float def, params string[] keys)
int GetAttributeInt(string key, int def)
Rectangle GetAttributeRect(string key, in Rectangle def)
string?[] GetAttributeStringArray(string key, string[]? def, bool convertToLowerInvariant=false)
Identifier GetAttributeIdentifier(string key, Identifier def)
XAttribute? GetAttribute(string name)
void SetAttributeValue(string key, string val)
ImmutableHashSet< Identifier > GetAttributeIdentifierImmutableHashSet(string key, ImmutableHashSet< Identifier >? def, bool trim=true)
Identifier GetAttributeIdentifier(string key, string def)