using System; using System.Collections.Generic; using System.Globalization; using UnityEngine; namespace SUISS.Core { public static class IDictionaryExtensions { public static TValue Get(this IDictionary dictionary, TKey key, TValue defaultValue) { TValue result; if (dictionary.TryGetValue(key, out result)) { return result; } return defaultValue; } public static string GetString(this IDictionary dictionary, TKey key, string defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { string result = defaultValue; try { result = Convert.ToString(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static int GetInt(this IDictionary dictionary, TKey key, int defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { int result = defaultValue; try { result = Convert.ToInt32(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static long GetLong(this IDictionary dictionary, TKey key, long defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { long result = defaultValue; try { result = Convert.ToInt64(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static float GetFloat(this IDictionary dictionary, TKey key, float defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { try { return Convert.ToSingle(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return defaultValue; } return defaultValue; } public static decimal GetDecimal(this IDictionary dictionary, TKey key, decimal defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { try { return Convert.ToDecimal(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return defaultValue; } return defaultValue; } public static double GetDouble(this IDictionary dictionary, TKey key, double defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { try { return Convert.ToDouble(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return defaultValue; } return defaultValue; } public static bool GetBoolean(this IDictionary dictionary, TKey key, bool defaultValue) { TValue tvalue; if (dictionary.TryGetValue(key, out tvalue)) { bool result = defaultValue; try { result = Convert.ToBoolean(tvalue, CultureInfo.InvariantCulture); } catch (Exception exception) { UnityEngine.Debug.LogException(exception); } return result; } return defaultValue; } public static void RemoveAll(this IDictionary dict, Func, bool> predicate) { List list = new List(); foreach (KeyValuePair arg in dict) { if (predicate(arg)) { list.Add(arg.Key); } } foreach (Key key in list) { dict.Remove(key); } } } }