diff --git a/Gameloop.Vdf/Linq/VObject.cs b/Gameloop.Vdf/Linq/VObject.cs index 051ff69..c80db1e 100644 --- a/Gameloop.Vdf/Linq/VObject.cs +++ b/Gameloop.Vdf/Linq/VObject.cs @@ -5,9 +5,9 @@ using System.Linq; using System.Linq.Expressions; -namespace Gameloop.Vdf +namespace Gameloop.Vdf.Linq { - public class VObject : VToken + public class VObject : VToken, IDictionary { private readonly List _children; @@ -44,21 +44,26 @@ public VToken this[string key] { get { - if (!TryGetValue(key, out VProperty result)) + if (!TryGetValue(key, out VToken result)) throw new KeyNotFoundException("The given key was not present."); - return result.Value; + return result; } set { - if (TryGetValue(key, out VProperty result)) - result.Value = value; + VProperty prop = _children.FirstOrDefault(x => x.Key == key); + if (prop != null) + prop.Value = value; else Add(key, value); } } + ICollection IDictionary.Keys => _children.Select(x => x.Key).ToList(); + + ICollection IDictionary.Values => throw new NotImplementedException(); + public override IEnumerable Children() { return _children; @@ -102,10 +107,10 @@ public bool Remove(string key) return _children.RemoveAll(x => x.Key == key) != 0; } - public bool TryGetValue(string key, out VProperty value) + public bool TryGetValue(string key, out VToken value) { - value = _children.FirstOrDefault(x => x.Key == key); - return value != null; + value = _children.FirstOrDefault(x => x.Key == key)?.Value; + return (value != null); } public void RemoveAt(string key) @@ -123,6 +128,61 @@ public override void WriteTo(VdfWriter writer) writer.WriteObjectEnd(); } + #region ICollection> Members + + public IEnumerator> GetEnumerator() + { + foreach (VProperty property in _children) + yield return new KeyValuePair(property.Key, property.Value); + } + + void ICollection>.Add(KeyValuePair item) + { + Add(new VProperty(item.Key, item.Value)); + } + + void ICollection>.Clear() + { + _children.Clear(); + } + + bool ICollection>.Contains(KeyValuePair item) + { + VProperty property = _children.FirstOrDefault(x => x.Key == item.Key); + if (property == null) + return false; + + return (property.Value == item.Value); + } + + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) + { + if (array == null) + throw new ArgumentNullException(nameof(array)); + if (arrayIndex < 0) + throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0."); + if (arrayIndex >= array.Length && arrayIndex != 0) + throw new ArgumentException("arrayIndex is equal to or greater than the length of array."); + if (Count > array.Length - arrayIndex) + throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array."); + + for (int index = 0; index < _children.Count; index++) + array[arrayIndex + index] = new KeyValuePair(_children[index].Key, _children[index].Value); + } + + bool ICollection>.IsReadOnly => false; + + bool ICollection>.Remove(KeyValuePair item) + { + if (!((ICollection>) this).Contains(item)) + return false; + + ((IDictionary) this).Remove(item.Key); + return true; + } + + #endregion + protected override DynamicMetaObject GetMetaObject(Expression parameter) { return new DynamicProxyMetaObject(parameter, this, new VObjectDynamicProxy()); diff --git a/Gameloop.Vdf/Linq/VProperty.cs b/Gameloop.Vdf/Linq/VProperty.cs index 6307ab7..938fc80 100644 --- a/Gameloop.Vdf/Linq/VProperty.cs +++ b/Gameloop.Vdf/Linq/VProperty.cs @@ -1,6 +1,6 @@ using System; -namespace Gameloop.Vdf +namespace Gameloop.Vdf.Linq { public class VProperty : VToken { diff --git a/Gameloop.Vdf/Linq/VToken.cs b/Gameloop.Vdf/Linq/VToken.cs index 70ccb96..eb91de5 100644 --- a/Gameloop.Vdf/Linq/VToken.cs +++ b/Gameloop.Vdf/Linq/VToken.cs @@ -1,5 +1,4 @@ -using Gameloop.Vdf.Linq; -using Gameloop.Vdf.Utilities; +using Gameloop.Vdf.Utilities; using System; using System.Collections; using System.Collections.Generic; @@ -9,7 +8,7 @@ using System.Linq; using System.Linq.Expressions; -namespace Gameloop.Vdf +namespace Gameloop.Vdf.Linq { public abstract class VToken : IVEnumerable, IDynamicMetaObjectProvider { diff --git a/Gameloop.Vdf/Linq/VValue.cs b/Gameloop.Vdf/Linq/VValue.cs index 30648bf..e37ba1d 100644 --- a/Gameloop.Vdf/Linq/VValue.cs +++ b/Gameloop.Vdf/Linq/VValue.cs @@ -1,6 +1,6 @@ using System; -namespace Gameloop.Vdf +namespace Gameloop.Vdf.Linq { public class VValue : VToken { diff --git a/Gameloop.Vdf/VdfConvert.cs b/Gameloop.Vdf/VdfConvert.cs index 91f89c7..86f711c 100644 --- a/Gameloop.Vdf/VdfConvert.cs +++ b/Gameloop.Vdf/VdfConvert.cs @@ -1,4 +1,5 @@ -using System; +using Gameloop.Vdf.Linq; +using System; using System.Globalization; using System.IO; using System.Text; diff --git a/Gameloop.Vdf/VdfSerializer.cs b/Gameloop.Vdf/VdfSerializer.cs index 3e61f80..7b37394 100644 --- a/Gameloop.Vdf/VdfSerializer.cs +++ b/Gameloop.Vdf/VdfSerializer.cs @@ -1,4 +1,5 @@ -using System.IO; +using Gameloop.Vdf.Linq; +using System.IO; namespace Gameloop.Vdf { diff --git a/Gameloop.Vdf/VdfTextWriter.cs b/Gameloop.Vdf/VdfTextWriter.cs index 1bba1ea..85ac6e4 100644 --- a/Gameloop.Vdf/VdfTextWriter.cs +++ b/Gameloop.Vdf/VdfTextWriter.cs @@ -1,6 +1,6 @@ -using System; +using Gameloop.Vdf.Linq; +using System; using System.IO; -using System.Linq; namespace Gameloop.Vdf { diff --git a/Gameloop.Vdf/VdfWriter.cs b/Gameloop.Vdf/VdfWriter.cs index b4d53fe..8ebb8ac 100644 --- a/Gameloop.Vdf/VdfWriter.cs +++ b/Gameloop.Vdf/VdfWriter.cs @@ -1,4 +1,5 @@ -using System; +using Gameloop.Vdf.Linq; +using System; namespace Gameloop.Vdf {