using Island.StandardLib.Storage; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text; using System.Threading; namespace Island.StandardLib { public static class CodeExtension { /// /// 获取这个byte[]的16字节特征值 /// public static byte[] Hash16(this byte[] bytes) { MD5 md5 = new MD5CryptoServiceProvider(); return md5.ComputeHash(bytes); } /// /// 用序列化方法深拷贝对象 /// /// 对象类型 /// 对象 /// 对象的深拷贝 public static T MemoryCopy(this T obj) where T : IStorable, new() { DataStorage ds = new DataStorage(); obj.WriteToData(ds); T t = new T(); t.ReadFromData(ds); return t; } public static void Clear(this byte[] bytes) { for (int i = 0; i < bytes.Length; i++) bytes[i] = 0; } public static bool ByteEquals(this byte[] data, byte[] bytes) { if (data.Length != bytes.Length) return false; for (int i = 0; i < data.Length; i++) if (data[i] != bytes[i]) return false; return true; } public static string ToStringEx(this long data) { string ret = ""; if (data >= 100000000) ret = System.Math.Round(data / 100000000d, 2) + "亿"; else if (data >= 10000) ret = System.Math.Round(data / 10000d, 2) + "万"; else ret = data.ToString(); return ret; } public static bool Contain(this T[] tlist, T finding) { for (int i = 0; i < tlist.Length; i++) if (tlist[i].Equals(finding)) return true; return false; } public static string Join(this string[] strs, string join = " ") { StringBuilder sb = new StringBuilder(); for (int i = 0; i < strs.Length; i++) { sb.Append(strs[i]); if (i != strs.Length - 1) { sb.Append(join); } } return sb.ToString(); } public static string ToStringEx(this float data) => data.ToString("P"); public static MultData ToMultData(this byte[] data) => new MultData(data); public static MultData ToMultData(this int data) => new MultData(data); public static MultData ToMultData(this char data) => new MultData(data); public static MultData ToMultData(this bool data) => new MultData(data); public static MultData ToMultData(this float data) => new MultData(data); public static MultData ToMultData(this string data) => new MultData(data); public static MultData ToMultData(this T data) where T : IStorable => new MultData(data); public static T[] Combine(this T value, T[] array) { T[] newT = new T[array.Length + 1]; newT[0] = value; for (int i = 0; i < array.Length; i++) newT[i + 1] = array[i]; return newT; } public static ConnectCommand CommandWithArgs(this int command, params object[] args) { MultData[] datas = new MultData[args.Length]; for (int i = 0; i < args.Length; i++) { if (args[i] is MultData) datas[i] = (MultData)args[i]; else if (args[i] is byte[]) datas[i] = new MultData((byte[])args[i]); else if (args[i] is int) datas[i] = new MultData((int)args[i]); else if (args[i] is char) datas[i] = new MultData((char)args[i]); else if (args[i] is bool) datas[i] = new MultData((bool)args[i]); else if (args[i] is float) datas[i] = new MultData((float)args[i]); else if (args[i] is string) datas[i] = new MultData((string)args[i]); else if (args[i] is long) datas[i] = new MultData((long)args[i]); else if (args[i] is uint) datas[i] = new MultData((uint)args[i]); else if (args[i] is ulong) datas[i] = new MultData((ulong)args[i]); else if (args[i] is double) datas[i] = new MultData((double)args[i]); else if (args[i] is IStorable) datas[i] = new MultData((IStorable)args[i]); else throw new InvalidCastException(); } return new ConnectCommand(command, datas); } public static void Stop(this Thread thread) { try { thread?.Abort(); } catch { } } public static void Do(this CollectionType[] collection, Action func) { for (int i = 0; i < collection.Length; i++) func(collection[i]); } public static void Do(this List collection, Action func) { for (int i = 0; i < collection.Count; i++) func(collection[i]); } public static void DoWithTryAndLock(this List collection, Action func, LogLevel logLevel = LogLevel.Warning, string logTitle = "") { lock (collection) { for (int i = 0; i < collection.Count; i++) { try { func(collection[i]); } catch (Exception e) { Logger.Log(logLevel, logTitle + "\n" + e.ToString()); } } } } public static void Do(this CollectionType[] collection, Action func) { for (int i = 0; i < collection.Length; i++) func(collection[i], i); } public static RetType[] Do(this CollectionType[] collection, Func func) { RetType[] rets = new RetType[collection.Length]; for (int i = 0; i < collection.Length; i++) rets[i] = func(collection[i]); return rets; } public static T[] Sub(this T[] array, int begin, int end) { T[] newT = new T[end - begin]; for (int i = begin; i < end; i++) newT[i - begin] = array[i]; return newT; } public static T[] Add(this T[] array, T[] arr) { T[] newT = new T[array.Length + arr.Length]; for (int i = 0; i < array.Length; i++) newT[i] = array[i]; for (int i = 0; i < arr.Length; i++) newT[i + array.Length] = arr[i]; return newT; } public static void Set(this Dictionary dict, TKey name, TValue val) { if (dict.ContainsKey(name)) dict[name] = val; else dict.Add(name, val); } public static TValue Get(this Dictionary dict, TKey name, TValue defval = default) { if (dict.ContainsKey(name)) return dict[name]; return defval; } public static string PushRandom(this Dictionary dict, TValue objToPush) { Random rd = new Random(); string val = ""; while (true) { val = (char)rd.Next(char.MinValue, char.MaxValue) + "" + (char)rd.Next(char.MinValue, char.MaxValue); if (!dict.ContainsKey(val)) break; } dict[val] = objToPush; return val; } } }