Seven is the number.
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.

199 lines
7.1 KiB

4 years ago
  1. // ----------------------------------------------------------------------------
  2. // <copyright file="Extensions.cs" company="Exit Games GmbH">
  3. // Photon Extensions - Copyright (C) 2018 Exit Games GmbH
  4. // </copyright>
  5. // <summary>
  6. // Provides some helpful methods and extensions for Hashtables, etc.
  7. // </summary>
  8. // <author>developer@photonengine.com</author>
  9. // ----------------------------------------------------------------------------
  10. #if UNITY_4_7 || UNITY_5 || UNITY_5_3_OR_NEWER
  11. #define SUPPORTED_UNITY
  12. #endif
  13. namespace Photon.Realtime
  14. {
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using ExitGames.Client.Photon;
  18. #if SUPPORTED_UNITY
  19. using UnityEngine;
  20. using Debug = UnityEngine.Debug;
  21. #endif
  22. #if SUPPORTED_UNITY || NETFX_CORE
  23. using Hashtable = ExitGames.Client.Photon.Hashtable;
  24. using SupportClass = ExitGames.Client.Photon.SupportClass;
  25. #endif
  26. /// <summary>
  27. /// This static class defines some useful extension methods for several existing classes (e.g. Vector3, float and others).
  28. /// </summary>
  29. public static class Extensions
  30. {
  31. /// <summary>
  32. /// Merges all keys from addHash into the target. Adds new keys and updates the values of existing keys in target.
  33. /// </summary>
  34. /// <param name="target">The IDictionary to update.</param>
  35. /// <param name="addHash">The IDictionary containing data to merge into target.</param>
  36. public static void Merge(this IDictionary target, IDictionary addHash)
  37. {
  38. if (addHash == null || target.Equals(addHash))
  39. {
  40. return;
  41. }
  42. foreach (object key in addHash.Keys)
  43. {
  44. target[key] = addHash[key];
  45. }
  46. }
  47. /// <summary>
  48. /// Merges keys of type string to target Hashtable.
  49. /// </summary>
  50. /// <remarks>
  51. /// Does not remove keys from target (so non-string keys CAN be in target if they were before).
  52. /// </remarks>
  53. /// <param name="target">The target IDicitionary passed in plus all string-typed keys from the addHash.</param>
  54. /// <param name="addHash">A IDictionary that should be merged partly into target to update it.</param>
  55. public static void MergeStringKeys(this IDictionary target, IDictionary addHash)
  56. {
  57. if (addHash == null || target.Equals(addHash))
  58. {
  59. return;
  60. }
  61. foreach (object key in addHash.Keys)
  62. {
  63. // only merge keys of type string
  64. if (key is string)
  65. {
  66. target[key] = addHash[key];
  67. }
  68. }
  69. }
  70. /// <summary>Helper method for debugging of IDictionary content, inlcuding type-information. Using this is not performant.</summary>
  71. /// <remarks>Should only be used for debugging as necessary.</remarks>
  72. /// <param name="origin">Some Dictionary or Hashtable.</param>
  73. /// <returns>String of the content of the IDictionary.</returns>
  74. public static string ToStringFull(this IDictionary origin)
  75. {
  76. return SupportClass.DictionaryToString(origin, false);
  77. }
  78. /// <summary>Helper method for debugging of List<T> content. Using this is not performant.</summary>
  79. /// <remarks>Should only be used for debugging as necessary.</remarks>
  80. /// <param name="data">Any List<T> where T implements .ToString().</param>
  81. /// <returns>A comma-separated string containing each value's ToString().</returns>
  82. public static string ToStringFull<T>(this List<T> data)
  83. {
  84. if (data == null) return "null";
  85. string[] sb = new string[data.Count];
  86. for (int i = 0; i < data.Count; i++)
  87. {
  88. object o = data[i];
  89. sb[i] = (o != null) ? o.ToString() : "null";
  90. }
  91. return string.Join(", ", sb);
  92. }
  93. /// <summary>Helper method for debugging of object[] content. Using this is not performant.</summary>
  94. /// <remarks>Should only be used for debugging as necessary.</remarks>
  95. /// <param name="data">Any object[].</param>
  96. /// <returns>A comma-separated string containing each value's ToString().</returns>
  97. public static string ToStringFull(this object[] data)
  98. {
  99. if (data == null) return "null";
  100. string[] sb = new string[data.Length];
  101. for (int i = 0; i < data.Length; i++)
  102. {
  103. object o = data[i];
  104. sb[i] = (o != null) ? o.ToString() : "null";
  105. }
  106. return string.Join(", ", sb);
  107. }
  108. /// <summary>
  109. /// This method copies all string-typed keys of the original into a new Hashtable.
  110. /// </summary>
  111. /// <remarks>
  112. /// Does not recurse (!) into hashes that might be values in the root-hash.
  113. /// This does not modify the original.
  114. /// </remarks>
  115. /// <param name="original">The original IDictonary to get string-typed keys from.</param>
  116. /// <returns>New Hashtable containing only string-typed keys of the original.</returns>
  117. public static Hashtable StripToStringKeys(this IDictionary original)
  118. {
  119. Hashtable target = new Hashtable();
  120. if (original != null)
  121. {
  122. foreach (object key in original.Keys)
  123. {
  124. if (key is string)
  125. {
  126. target[key] = original[key];
  127. }
  128. }
  129. }
  130. return target;
  131. }
  132. /// <summary>
  133. /// This removes all key-value pairs that have a null-reference as value.
  134. /// Photon properties are removed by setting their value to null.
  135. /// Changes the original passed IDictionary!
  136. /// </summary>
  137. /// <param name="original">The IDictionary to strip of keys with null-values.</param>
  138. public static void StripKeysWithNullValues(this IDictionary original)
  139. {
  140. object[] keys = new object[original.Count];
  141. original.Keys.CopyTo(keys, 0);
  142. for (int index = 0; index < keys.Length; index++)
  143. {
  144. var key = keys[index];
  145. if (original[key] == null)
  146. {
  147. original.Remove(key);
  148. }
  149. }
  150. }
  151. /// <summary>
  152. /// Checks if a particular integer value is in an int-array.
  153. /// </summary>
  154. /// <remarks>This might be useful to look up if a particular actorNumber is in the list of players of a room.</remarks>
  155. /// <param name="target">The array of ints to check.</param>
  156. /// <param name="nr">The number to lookup in target.</param>
  157. /// <returns>True if nr was found in target.</returns>
  158. public static bool Contains(this int[] target, int nr)
  159. {
  160. if (target == null)
  161. {
  162. return false;
  163. }
  164. for (int index = 0; index < target.Length; index++)
  165. {
  166. if (target[index] == nr)
  167. {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. }
  174. }