A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.8KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace TechbloxModdingAPI.Utility
  6. {
  7. public class WeakDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TValue : class
  8. {
  9. private readonly Dictionary<TKey, WeakReference<TValue>> _dictionary = new();
  10. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  11. {
  12. using var enumerator = _dictionary.GetEnumerator();
  13. while (enumerator.MoveNext())
  14. {
  15. if (enumerator.Current.Value.TryGetTarget(out var value))
  16. yield return new KeyValuePair<TKey, TValue>(enumerator.Current.Key, value);
  17. }
  18. }
  19. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  20. public void Add(KeyValuePair<TKey, TValue> item) => Add(item.Key, item.Value);
  21. public void Clear() => _dictionary.Clear();
  22. public bool Contains(KeyValuePair<TKey, TValue> item) =>
  23. TryGetValue(item.Key, out var value) && item.Value == value;
  24. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) =>
  25. throw new System.NotImplementedException();
  26. public bool Remove(KeyValuePair<TKey, TValue> item) => Contains(item) && Remove(item.Key);
  27. public int Count => _dictionary.Count;
  28. public bool IsReadOnly => false;
  29. public bool ContainsKey(TKey key) => TryGetValue(key, out _);
  30. public void Add(TKey key, TValue value) => _dictionary.Add(key, new WeakReference<TValue>(value));
  31. public bool Remove(TKey key) => _dictionary.Remove(key);
  32. public bool TryGetValue(TKey key, out TValue value)
  33. {
  34. value = null;
  35. bool ret = _dictionary.TryGetValue(key, out var reference) && reference.TryGetTarget(out value);
  36. if (!ret) _dictionary.Remove(key);
  37. return ret;
  38. }
  39. public TValue this[TKey key]
  40. {
  41. get => TryGetValue(key, out var value)
  42. ? value
  43. : throw new KeyNotFoundException($"Key {key} not found in WeakDictionary.");
  44. set => _dictionary[key] = new WeakReference<TValue>(value);
  45. }
  46. public ICollection<TKey> Keys => new KeyCollection(this);
  47. public ICollection<TValue> Values => new ValueCollection(this);
  48. public class KeyCollection : ICollection<TKey>, IReadOnlyCollection<TKey>
  49. {
  50. private readonly WeakDictionary<TKey, TValue> _dictionary;
  51. internal KeyCollection(WeakDictionary<TKey, TValue> dictionary)
  52. {
  53. _dictionary = dictionary;
  54. }
  55. public IEnumerator<TKey> GetEnumerator()
  56. {
  57. using var enumerator = _dictionary.GetEnumerator();
  58. while (enumerator.MoveNext())
  59. yield return enumerator.Current.Key;
  60. }
  61. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  62. public void Add(TKey item) => throw new NotSupportedException("The key collection is read only.");
  63. public void Clear() => throw new NotSupportedException("The key collection is read only.");
  64. public bool Contains(TKey item) => _dictionary.ContainsKey(item);
  65. public void CopyTo(TKey[] array, int arrayIndex) => throw new NotImplementedException();
  66. public bool Remove(TKey item) => throw new NotSupportedException("The key collection is read only.");
  67. public int Count => _dictionary.Count;
  68. public bool IsReadOnly => true;
  69. }
  70. public class ValueCollection : ICollection<TValue>, IReadOnlyCollection<TValue>
  71. {
  72. private readonly WeakDictionary<TKey, TValue> _dictionary;
  73. internal ValueCollection(WeakDictionary<TKey, TValue> dictionary)
  74. {
  75. _dictionary = dictionary;
  76. }
  77. public IEnumerator<TValue> GetEnumerator()
  78. {
  79. using var enumerator = _dictionary.GetEnumerator();
  80. while (enumerator.MoveNext())
  81. yield return enumerator.Current.Value;
  82. }
  83. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  84. public void Add(TValue item) => throw new NotSupportedException("The value collection is read only.");
  85. public void Clear() => throw new NotSupportedException("The value collection is read only.");
  86. public bool Contains(TValue item) => _dictionary.Any(kv => kv.Value == item);
  87. public void CopyTo(TValue[] array, int arrayIndex) => throw new NotImplementedException();
  88. public bool Remove(TValue item) => throw new NotSupportedException("The value collection is read only.");
  89. public int Count => _dictionary.Count;
  90. public bool IsReadOnly => true;
  91. }
  92. }
  93. }