Utility.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.IO;
  2. using UnityEngine;
  3. namespace VEngine
  4. {
  5. public static class Utility
  6. {
  7. public static string buildPath = "Bundles";
  8. public const string unsupportedPlatform = "Unsupported";
  9. private static readonly double[] byteUnits =
  10. {
  11. 1073741824.0, 1048576.0, 1024.0, 1
  12. };
  13. private static readonly string[] byteUnitsNames =
  14. {
  15. "GB", "MB", "KB", "B"
  16. };
  17. public static string GetPlatformName()
  18. {
  19. if (Application.platform == RuntimePlatform.Android) return "Android";
  20. if (Application.platform == RuntimePlatform.WindowsPlayer) return "Windows";
  21. if (Application.platform == RuntimePlatform.IPhonePlayer) return "iOS";
  22. return Application.platform == RuntimePlatform.WebGLPlayer ? "WebGL" : unsupportedPlatform;
  23. }
  24. public static string FormatBytes(long bytes)
  25. {
  26. var size = "0 B";
  27. if (bytes == 0) return size;
  28. for (var index = 0; index < byteUnits.Length; index++)
  29. {
  30. var unit = byteUnits[index];
  31. if (bytes >= unit)
  32. {
  33. size = $"{bytes / unit:##.##} {byteUnitsNames[index]}";
  34. break;
  35. }
  36. }
  37. return size;
  38. }
  39. public static uint ComputeCRC32(Stream stream)
  40. {
  41. var crc32 = new CRC32();
  42. return crc32.Compute(stream);
  43. }
  44. public static uint ComputeCRC32(string filename)
  45. {
  46. if (!File.Exists(filename)) return 0;
  47. using (var stream = File.OpenRead(filename))
  48. {
  49. return ComputeCRC32(stream);
  50. }
  51. }
  52. }
  53. }