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 (T1, T2) GetAttributeTuple<T1, T2>(string key, in (T1, T2) def) => Element.GetAttributeTuple(key, def);
97  public (T1, T2)[] GetAttributeTupleArray<T1, T2>(string key, in (T1, T2)[] def) => Element.GetAttributeTupleArray(key, def);
98  public Range<int> GetAttributeRange(string key, in Range<int> def) => Element.GetAttributeRange(key, def);
99 
100  public Identifier VariantOf() => Element.VariantOf();
101 
102  public bool DoesAttributeReferenceFileNameAlone(string key) => Element.DoesAttributeReferenceFileNameAlone(key);
103 
104  public string ParseContentPathFromUri() => Element.ParseContentPathFromUri();
105 
106  public void SetAttributeValue(string key, string val) => Element.SetAttributeValue(key, val);
107 
108  public void Add(ContentXElement elem)
109  {
110  Element.Add(elem.Element);
112  #warning TODO: update %ModDir% instances in case the content package changes
113  }
114 
115  public void AddFirst(ContentXElement elem)
116  {
117  Element.AddFirst(elem.Element);
119  #warning TODO: update %ModDir% instances in case the content package changes
120  }
121 
122  public void AddAfterSelf(ContentXElement elem)
123  {
124  Element.AddAfterSelf(elem.Element);
126  #warning TODO: update %ModDir% instances in case the content package changes
127  }
128 
129  public void Remove() => Element.Remove();
130 
131  public override bool Equals(object? obj)
132  {
133  return obj is ContentXElement element && this == element;
134  }
135 
136  public override int GetHashCode()
137  {
138  return HashCode.Combine(ContentPackage, Element);
139  }
140 
141  public static bool operator ==(in ContentXElement? a, in ContentXElement? b)
142  {
143  return a?.ContentPackage == b?.ContentPackage && a?.Element == b?.Element;
144  }
145 
146  public static bool operator !=(in ContentXElement? a, in ContentXElement? b) =>
147  !(a == b);
148  }
149 
150  public static class ContentXElementExtensions
151  {
152  public static ContentXElement FromPackage(this XElement element, ContentPackage? contentPackage)
153  => new ContentXElement(contentPackage, element);
154 
155  public static IEnumerable<ContentXElement> Elements(this IEnumerable<ContentXElement> elements)
156  => elements.SelectMany(e => e.Elements());
157  }
158 }
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)
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)