|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Linq; |
| 6 | +using System.Xml.Linq; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | +using Microsoft.Toolkit.Diagnostics; |
| 9 | + |
| 10 | +namespace Barotrauma.LuaCs.Data; |
| 11 | + |
| 12 | +public class SettingList<T> : SettingEntry<T>, ISettingList<T> where T : IEquatable<T>, IConvertible |
| 13 | +{ |
| 14 | + public class LFactory : ISettingBase.IFactory<ISettingList<T>> |
| 15 | + { |
| 16 | + public ISettingList<T> CreateInstance(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate) |
| 17 | + { |
| 18 | + Guard.IsNotNull(configInfo, nameof(configInfo)); |
| 19 | + return new SettingList<T>(configInfo, valueChangePredicate); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public SettingList(IConfigInfo configInfo, Func<OneOf<string, XElement, object>, bool> valueChangePredicate) : base(configInfo, valueChangePredicate) |
| 24 | + { |
| 25 | + if (!( |
| 26 | + typeof(T).IsEnum || |
| 27 | + typeof(T).IsPrimitive || |
| 28 | + typeof(T) == typeof(string))) |
| 29 | + { |
| 30 | + ThrowHelper.ThrowArgumentException($"{nameof(ISettingBase<T>)}: The type of {nameof(T)} is not an allowed type."); |
| 31 | + } |
| 32 | + ValueChangePredicate = valueChangePredicate; |
| 33 | + |
| 34 | + var valuesElements = ConfigInfo.Element.GetChildElement("Values")?.GetChildElements("Value")?.ToImmutableArray(); |
| 35 | + |
| 36 | + Guard.IsNotNull(valuesElements, this.InternalName); |
| 37 | + if (valuesElements.Value.IsEmpty) |
| 38 | + { |
| 39 | + ThrowHelper.ThrowArgumentNullException($"{this.InternalName}: Could not find any values in list!"); |
| 40 | + } |
| 41 | + |
| 42 | + foreach (var element in valuesElements.Value) |
| 43 | + { |
| 44 | + if (!TryConvert(element, out var v1)) |
| 45 | + { |
| 46 | + ThrowHelper.ThrowArgumentException($"{this.InternalName}: Error while parsing list values"); |
| 47 | + } |
| 48 | + _valuesList.Add(v1); |
| 49 | + } |
| 50 | + |
| 51 | + if (TryConvert(ConfigInfo.Element, out var v) && _valuesList.Contains(v)) |
| 52 | + { |
| 53 | + Value = v; |
| 54 | + DefaultValue = v; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + Value = _valuesList[0]; |
| 59 | + DefaultValue = _valuesList[0]; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + bool TryConvert(XElement element, out T value) |
| 64 | + { |
| 65 | + try |
| 66 | + { |
| 67 | + value = (T)Convert.ChangeType(element.GetAttributeString("Value", null), typeof(T)); |
| 68 | + return true; |
| 69 | + } |
| 70 | + catch (Exception e) when (e is InvalidCastException or ArgumentNullException) |
| 71 | + { |
| 72 | + value = default(T); |
| 73 | + return false; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private readonly List<T> _valuesList = new(); |
| 79 | + |
| 80 | + public override bool TrySetValue(T value) |
| 81 | + { |
| 82 | + if (!_valuesList.Contains(value)) |
| 83 | + { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + return base.TrySetValue(value); |
| 88 | + } |
| 89 | + |
| 90 | + public bool TrySetValueByIndex(int index) |
| 91 | + { |
| 92 | + throw new NotImplementedException(); |
| 93 | + } |
| 94 | + |
| 95 | + public IReadOnlyList<T> Options => _valuesList.AsReadOnly(); |
| 96 | + |
| 97 | + public IReadOnlyList<string> StringOptions => _valuesList.Select(e => e.ToString()).ToImmutableArray(); |
| 98 | +} |
0 commit comments