Tools.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using MessagePack;
  2. using MessagePack.Resolvers;
  3. using Shaker.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.SqlTypes;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Shaker.Tools
  12. {
  13. public static class Tools
  14. {
  15. private static MessagePackSerializerOptions options = TypelessContractlessStandardResolver.Options.WithOmitAssemblyVersion(true);
  16. public static byte[] GetBytes<T>(this T value)
  17. {
  18. return MessagePack.MessagePackSerializer.Serialize(value, options);
  19. }
  20. public static T GetValue<T>(this byte[] data)
  21. {
  22. return MessagePackSerializer.Deserialize<T>(data, options);
  23. }
  24. /// <summary>
  25. /// 压缩数据包
  26. /// </summary>
  27. /// <param name="arrays"></param>
  28. /// <returns></returns>
  29. public static byte[] CompressionBytes(byte[] arrays)
  30. {
  31. if (arrays == null || arrays.Length == 0) return new byte[0];
  32. using (MemoryStream ms = new MemoryStream())
  33. {
  34. using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionLevel.SmallestSize, true))
  35. {
  36. ds.Write(arrays, 0, arrays.Length);
  37. }
  38. return ms.ToArray();
  39. }
  40. }
  41. /// <summary>
  42. /// 解压数据包
  43. /// </summary>
  44. /// <param name="arrays"></param>
  45. /// <returns></returns>
  46. public static byte[] DecompressionBytes(byte[] arrays)
  47. {
  48. if (arrays == null || arrays.Length == 0) return new byte[0];
  49. using (MemoryStream ms = new MemoryStream(arrays))
  50. {
  51. using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress, true))
  52. {
  53. using (MemoryStream memoryStream = new MemoryStream())
  54. {
  55. ds.CopyTo(memoryStream);
  56. return memoryStream.ToArray();
  57. }
  58. }
  59. }
  60. }
  61. public static string GetUnit<T>(this T e) where T: Enum
  62. {
  63. if (e == null || !e.GetType().IsEnum) return "";
  64. return (e.GetType()?
  65. .GetField(e.ToString())?
  66. .GetCustomAttributes(typeof(ResultChannelUnitAttribute), false)
  67. .FirstOrDefault() as ResultChannelUnitAttribute)?.Unit ?? "";
  68. }
  69. public static T[] FixedArray<T>(this T[] value,int lenght)
  70. {
  71. if (lenght == 0) return Array.Empty<T>();
  72. if (value == null) return [.. Enumerable.Range(0, lenght).Select(x => Activator.CreateInstance<T>())];
  73. if (value.Length > lenght) return value[0..lenght];
  74. return [.. value,..Enumerable.Range(0, lenght - value.Length).Select(x=>Activator.CreateInstance<T>())];
  75. }
  76. public static IEnumerable<T> FixedArray<T>(this IEnumerable<T> value, int lenght)
  77. {
  78. if (lenght == 0) return Array.Empty<T>();
  79. if (value == null) return [.. Enumerable.Range(0, lenght).Select(x => Activator.CreateInstance<T>())];
  80. if (value.Count() > lenght) return value.Take(lenght);
  81. return [.. value, .. Enumerable.Range(0, lenght - value.Count()).Select(x => Activator.CreateInstance<T>())];
  82. }
  83. }
  84. }