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.
 
 
 

167 lines
4.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using UnityEngine;
  5. namespace SUISS.Core
  6. {
  7. public static class IDictionaryExtensions
  8. {
  9. public static TValue Get<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
  10. {
  11. TValue result;
  12. if (dictionary.TryGetValue(key, out result))
  13. {
  14. return result;
  15. }
  16. return defaultValue;
  17. }
  18. public static string GetString<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, string defaultValue)
  19. {
  20. TValue tvalue;
  21. if (dictionary.TryGetValue(key, out tvalue))
  22. {
  23. string result = defaultValue;
  24. try
  25. {
  26. result = Convert.ToString(tvalue, CultureInfo.InvariantCulture);
  27. }
  28. catch (Exception exception)
  29. {
  30. UnityEngine.Debug.LogException(exception);
  31. }
  32. return result;
  33. }
  34. return defaultValue;
  35. }
  36. public static int GetInt<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, int defaultValue)
  37. {
  38. TValue tvalue;
  39. if (dictionary.TryGetValue(key, out tvalue))
  40. {
  41. int result = defaultValue;
  42. try
  43. {
  44. result = Convert.ToInt32(tvalue, CultureInfo.InvariantCulture);
  45. }
  46. catch (Exception exception)
  47. {
  48. UnityEngine.Debug.LogException(exception);
  49. }
  50. return result;
  51. }
  52. return defaultValue;
  53. }
  54. public static long GetLong<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, long defaultValue)
  55. {
  56. TValue tvalue;
  57. if (dictionary.TryGetValue(key, out tvalue))
  58. {
  59. long result = defaultValue;
  60. try
  61. {
  62. result = Convert.ToInt64(tvalue, CultureInfo.InvariantCulture);
  63. }
  64. catch (Exception exception)
  65. {
  66. UnityEngine.Debug.LogException(exception);
  67. }
  68. return result;
  69. }
  70. return defaultValue;
  71. }
  72. public static float GetFloat<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, float defaultValue)
  73. {
  74. TValue tvalue;
  75. if (dictionary.TryGetValue(key, out tvalue))
  76. {
  77. try
  78. {
  79. return Convert.ToSingle(tvalue, CultureInfo.InvariantCulture);
  80. }
  81. catch (Exception exception)
  82. {
  83. UnityEngine.Debug.LogException(exception);
  84. }
  85. return defaultValue;
  86. }
  87. return defaultValue;
  88. }
  89. public static decimal GetDecimal<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, decimal defaultValue)
  90. {
  91. TValue tvalue;
  92. if (dictionary.TryGetValue(key, out tvalue))
  93. {
  94. try
  95. {
  96. return Convert.ToDecimal(tvalue, CultureInfo.InvariantCulture);
  97. }
  98. catch (Exception exception)
  99. {
  100. UnityEngine.Debug.LogException(exception);
  101. }
  102. return defaultValue;
  103. }
  104. return defaultValue;
  105. }
  106. public static double GetDouble<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, double defaultValue)
  107. {
  108. TValue tvalue;
  109. if (dictionary.TryGetValue(key, out tvalue))
  110. {
  111. try
  112. {
  113. return Convert.ToDouble(tvalue, CultureInfo.InvariantCulture);
  114. }
  115. catch (Exception exception)
  116. {
  117. UnityEngine.Debug.LogException(exception);
  118. }
  119. return defaultValue;
  120. }
  121. return defaultValue;
  122. }
  123. public static bool GetBoolean<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, bool defaultValue)
  124. {
  125. TValue tvalue;
  126. if (dictionary.TryGetValue(key, out tvalue))
  127. {
  128. bool result = defaultValue;
  129. try
  130. {
  131. result = Convert.ToBoolean(tvalue, CultureInfo.InvariantCulture);
  132. }
  133. catch (Exception exception)
  134. {
  135. UnityEngine.Debug.LogException(exception);
  136. }
  137. return result;
  138. }
  139. return defaultValue;
  140. }
  141. public static void RemoveAll<Key, Value>(this IDictionary<Key, Value> dict, Func<KeyValuePair<Key, Value>, bool> predicate)
  142. {
  143. List<Key> list = new List<Key>();
  144. foreach (KeyValuePair<Key, Value> arg in dict)
  145. {
  146. if (predicate(arg))
  147. {
  148. list.Add(arg.Key);
  149. }
  150. }
  151. foreach (Key key in list)
  152. {
  153. dict.Remove(key);
  154. }
  155. }
  156. }
  157. }