1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using MessagePack;
- using MessagePack.Resolvers;
- using Shaker.Model;
- using System;
- using System.Collections.Generic;
- using System.Data.SqlTypes;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace Shaker.Tools
- {
- public static class Tools
- {
- private static MessagePackSerializerOptions options = TypelessContractlessStandardResolver.Options.WithOmitAssemblyVersion(true);
- public static byte[] GetBytes<T>(this T value)
- {
- return MessagePack.MessagePackSerializer.Serialize(value, options);
- }
- public static T GetValue<T>(this byte[] data)
- {
- return MessagePackSerializer.Deserialize<T>(data, options);
- }
- /// <summary>
- /// 压缩数据包
- /// </summary>
- /// <param name="arrays"></param>
- /// <returns></returns>
- public static byte[] CompressionBytes(byte[] arrays)
- {
- if (arrays == null || arrays.Length == 0) return new byte[0];
- using (MemoryStream ms = new MemoryStream())
- {
- using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionLevel.SmallestSize, true))
- {
- ds.Write(arrays, 0, arrays.Length);
- }
- return ms.ToArray();
- }
- }
- /// <summary>
- /// 解压数据包
- /// </summary>
- /// <param name="arrays"></param>
- /// <returns></returns>
- public static byte[] DecompressionBytes(byte[] arrays)
- {
- if (arrays == null || arrays.Length == 0) return new byte[0];
- using (MemoryStream ms = new MemoryStream(arrays))
- {
- using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress, true))
- {
- using (MemoryStream memoryStream = new MemoryStream())
- {
- ds.CopyTo(memoryStream);
- return memoryStream.ToArray();
- }
- }
- }
- }
- public static string GetUnit<T>(this T e) where T: Enum
- {
- if (e == null || !e.GetType().IsEnum) return "";
- return (e.GetType()?
- .GetField(e.ToString())?
- .GetCustomAttributes(typeof(ResultChannelUnitAttribute), false)
- .FirstOrDefault() as ResultChannelUnitAttribute)?.Unit ?? "";
- }
- public static T[] FixedArray<T>(this T[] value,int lenght)
- {
- if (lenght == 0) return Array.Empty<T>();
- if (value == null) return [.. Enumerable.Range(0, lenght).Select(x => Activator.CreateInstance<T>())];
- if (value.Length > lenght) return value[0..lenght];
- return [.. value,..Enumerable.Range(0, lenght - value.Length).Select(x=>Activator.CreateInstance<T>())];
- }
- public static IEnumerable<T> FixedArray<T>(this IEnumerable<T> value, int lenght)
- {
- if (lenght == 0) return Array.Empty<T>();
- if (value == null) return [.. Enumerable.Range(0, lenght).Select(x => Activator.CreateInstance<T>())];
- if (value.Count() > lenght) return value.Take(lenght);
- return [.. value, .. Enumerable.Range(0, lenght - value.Count()).Select(x => Activator.CreateInstance<T>())];
- }
- }
- }
|