CodeExtension.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using Island.StandardLib.Storage;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Runtime.CompilerServices;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading;
  10. namespace Island.StandardLib
  11. {
  12. public static class CodeExtension
  13. {
  14. /// <summary>
  15. /// 获取这个byte[]的16字节特征值
  16. /// </summary>
  17. public static byte[] Hash16(this byte[] bytes)
  18. {
  19. MD5 md5 = new MD5CryptoServiceProvider();
  20. return md5.ComputeHash(bytes);
  21. }
  22. /// <summary>
  23. /// 用序列化方法深拷贝对象
  24. /// </summary>
  25. /// <typeparam name="T">对象类型</typeparam>
  26. /// <param name="obj">对象</param>
  27. /// <returns>对象的深拷贝</returns>
  28. public static T MemoryCopy<T>(this T obj) where T : IStorable, new()
  29. {
  30. DataStorage ds = new DataStorage();
  31. obj.WriteToData(ds);
  32. T t = new T();
  33. t.ReadFromData(ds);
  34. return t;
  35. }
  36. public static void Clear(this byte[] bytes)
  37. {
  38. for (int i = 0; i < bytes.Length; i++)
  39. bytes[i] = 0;
  40. }
  41. public static bool ByteEquals(this byte[] data, byte[] bytes)
  42. {
  43. if (data.Length != bytes.Length) return false;
  44. for (int i = 0; i < data.Length; i++)
  45. if (data[i] != bytes[i]) return false;
  46. return true;
  47. }
  48. public static string ToStringEx(this long data)
  49. {
  50. string ret = "";
  51. if (data >= 100000000) ret = System.Math.Round(data / 100000000d, 2) + "亿";
  52. else if (data >= 10000) ret = System.Math.Round(data / 10000d, 2) + "万";
  53. else ret = data.ToString();
  54. return ret;
  55. }
  56. public static bool Contain<T>(this T[] tlist, T finding)
  57. {
  58. for (int i = 0; i < tlist.Length; i++)
  59. if (tlist[i].Equals(finding))
  60. return true;
  61. return false;
  62. }
  63. public static string Join(this string[] strs, string join = " ")
  64. {
  65. StringBuilder sb = new StringBuilder();
  66. for (int i = 0; i < strs.Length; i++)
  67. {
  68. sb.Append(strs[i]);
  69. if (i != strs.Length - 1)
  70. {
  71. sb.Append(join);
  72. }
  73. }
  74. return sb.ToString();
  75. }
  76. public static string ToStringEx(this float data) => data.ToString("P");
  77. public static MultData ToMultData(this byte[] data) => new MultData(data);
  78. public static MultData ToMultData(this int data) => new MultData(data);
  79. public static MultData ToMultData(this char data) => new MultData(data);
  80. public static MultData ToMultData(this bool data) => new MultData(data);
  81. public static MultData ToMultData(this float data) => new MultData(data);
  82. public static MultData ToMultData(this string data) => new MultData(data);
  83. public static MultData ToMultData<T>(this T data) where T : IStorable => new MultData(data);
  84. public static T[] Combine<T>(this T value, T[] array)
  85. {
  86. T[] newT = new T[array.Length + 1];
  87. newT[0] = value;
  88. for (int i = 0; i < array.Length; i++)
  89. newT[i + 1] = array[i];
  90. return newT;
  91. }
  92. public static ConnectCommand CommandWithArgs(this int command, params object[] args)
  93. {
  94. MultData[] datas = new MultData[args.Length];
  95. for (int i = 0; i < args.Length; i++)
  96. {
  97. if (args[i] is MultData)
  98. datas[i] = (MultData)args[i];
  99. else if (args[i] is byte[])
  100. datas[i] = new MultData((byte[])args[i]);
  101. else if (args[i] is int)
  102. datas[i] = new MultData((int)args[i]);
  103. else if (args[i] is char)
  104. datas[i] = new MultData((char)args[i]);
  105. else if (args[i] is bool)
  106. datas[i] = new MultData((bool)args[i]);
  107. else if (args[i] is float)
  108. datas[i] = new MultData((float)args[i]);
  109. else if (args[i] is string)
  110. datas[i] = new MultData((string)args[i]);
  111. else if (args[i] is long)
  112. datas[i] = new MultData((long)args[i]);
  113. else if (args[i] is uint)
  114. datas[i] = new MultData((uint)args[i]);
  115. else if (args[i] is ulong)
  116. datas[i] = new MultData((ulong)args[i]);
  117. else if (args[i] is double)
  118. datas[i] = new MultData((double)args[i]);
  119. else if (args[i] is IStorable)
  120. datas[i] = new MultData((IStorable)args[i]);
  121. else throw new InvalidCastException();
  122. }
  123. return new ConnectCommand(command, datas);
  124. }
  125. public static void Stop(this Thread thread)
  126. {
  127. try
  128. {
  129. thread?.Abort();
  130. }
  131. catch { }
  132. }
  133. public static void Do<CollectionType>(this CollectionType[] collection, Action<CollectionType> func)
  134. {
  135. for (int i = 0; i < collection.Length; i++)
  136. func(collection[i]);
  137. }
  138. public static void Do<CollectionType>(this List<CollectionType> collection, Action<CollectionType> func)
  139. {
  140. for (int i = 0; i < collection.Count; i++)
  141. func(collection[i]);
  142. }
  143. public static void DoWithTryAndLock<CollectionType>(this List<CollectionType> collection, Action<CollectionType> func, LogLevel logLevel = LogLevel.Warning, string logTitle = "")
  144. {
  145. lock (collection)
  146. {
  147. for (int i = 0; i < collection.Count; i++)
  148. {
  149. try
  150. {
  151. func(collection[i]);
  152. }
  153. catch (Exception e)
  154. {
  155. Logger.Log(logLevel, logTitle + "\n" + e.ToString());
  156. }
  157. }
  158. }
  159. }
  160. public static void Do<CollectionType>(this CollectionType[] collection, Action<CollectionType, int> func)
  161. {
  162. for (int i = 0; i < collection.Length; i++)
  163. func(collection[i], i);
  164. }
  165. public static RetType[] Do<CollectionType, RetType>(this CollectionType[] collection, Func<CollectionType, RetType> func)
  166. {
  167. RetType[] rets = new RetType[collection.Length];
  168. for (int i = 0; i < collection.Length; i++)
  169. rets[i] = func(collection[i]);
  170. return rets;
  171. }
  172. public static T[] Sub<T>(this T[] array, int begin, int end)
  173. {
  174. T[] newT = new T[end - begin];
  175. for (int i = begin; i < end; i++)
  176. newT[i - begin] = array[i];
  177. return newT;
  178. }
  179. public static T[] Add<T>(this T[] array, T[] arr)
  180. {
  181. T[] newT = new T[array.Length + arr.Length];
  182. for (int i = 0; i < array.Length; i++)
  183. newT[i] = array[i];
  184. for (int i = 0; i < arr.Length; i++)
  185. newT[i + array.Length] = arr[i];
  186. return newT;
  187. }
  188. public static void Set<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey name, TValue val)
  189. {
  190. if (dict.ContainsKey(name)) dict[name] = val;
  191. else dict.Add(name, val);
  192. }
  193. public static TValue Get<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey name, TValue defval = default)
  194. {
  195. if (dict.ContainsKey(name)) return dict[name];
  196. return defval;
  197. }
  198. public static string PushRandom<TValue>(this Dictionary<string, TValue> dict, TValue objToPush)
  199. {
  200. Random rd = new Random();
  201. string val = "";
  202. while (true)
  203. {
  204. val = (char)rd.Next(char.MinValue, char.MaxValue) + "" + (char)rd.Next(char.MinValue, char.MaxValue);
  205. if (!dict.ContainsKey(val))
  206. break;
  207. }
  208. dict[val] = objToPush;
  209. return val;
  210. }
  211. }
  212. }