RuntimeEnvironment.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace FF8
  4. {
  5. public static class RuntimeEnvironment
  6. {
  7. public static RuntimePlatform Platform { get; } = Init();
  8. private static RuntimePlatform Init()
  9. {
  10. PlatformID platform = Environment.OSVersion.Platform;
  11. switch (platform)
  12. {
  13. case PlatformID.Win32S:
  14. case PlatformID.Win32Windows:
  15. case PlatformID.Win32NT:
  16. case PlatformID.WinCE:
  17. return RuntimePlatform.Windows;
  18. case PlatformID.Unix:
  19. return GetUnixPlatform();
  20. case PlatformID.MacOSX:
  21. return RuntimePlatform.MacOSX;
  22. default:
  23. throw new NotSupportedException($"Environment.OSVersion.Platform = {platform}");
  24. }
  25. }
  26. private static RuntimePlatform GetUnixPlatform()
  27. {
  28. IntPtr buffer = IntPtr.Zero;
  29. try
  30. {
  31. buffer = Marshal.AllocHGlobal(8192);
  32. if (uname(buffer) == 0)
  33. {
  34. if (Marshal.PtrToStringAnsi(buffer) == "Darwin")
  35. return RuntimePlatform.MacOSX;
  36. }
  37. }
  38. catch
  39. {
  40. // Nothing
  41. }
  42. finally
  43. {
  44. if (buffer != IntPtr.Zero)
  45. Marshal.FreeHGlobal(buffer);
  46. }
  47. return RuntimePlatform.Linux;
  48. }
  49. [DllImport("libc")]
  50. private static extern Int32 uname(IntPtr buf);
  51. }
  52. }