FileSystemUtil.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Text;
  5. using JetBrains.Annotations;
  6. using UnityEngine;
  7. namespace Packages.Rider.Editor.Util
  8. {
  9. public static class FileSystemUtil
  10. {
  11. [NotNull]
  12. public static string GetFinalPathName([NotNull] string path)
  13. {
  14. if (path == null) throw new ArgumentNullException("path");
  15. // up to MAX_PATH. MAX_PATH on Linux currently 4096, on Mac OS X 1024
  16. // doc: http://man7.org/linux/man-pages/man3/realpath.3.html
  17. var sb = new StringBuilder(8192);
  18. var result = LibcNativeInterop.realpath(path, sb);
  19. if (result == IntPtr.Zero)
  20. {
  21. throw new Win32Exception($"{path} was not resolved.");
  22. }
  23. return new FileInfo(sb.ToString()).FullName;
  24. }
  25. public static string FileNameWithoutExtension(string path)
  26. {
  27. if (string.IsNullOrEmpty(path))
  28. {
  29. return "";
  30. }
  31. var indexOfDot = -1;
  32. var indexOfSlash = 0;
  33. for (var i = path.Length - 1; i >= 0; i--)
  34. {
  35. if (indexOfDot == -1 && path[i] == '.')
  36. {
  37. indexOfDot = i;
  38. }
  39. if (indexOfSlash == 0 && path[i] == '/' || path[i] == '\\')
  40. {
  41. indexOfSlash = i + 1;
  42. break;
  43. }
  44. }
  45. if (indexOfDot == -1)
  46. {
  47. indexOfDot = path.Length;
  48. }
  49. return path.Substring(indexOfSlash, indexOfDot - indexOfSlash);
  50. }
  51. public static bool EditorPathExists(string editorPath)
  52. {
  53. return SystemInfo.operatingSystemFamily == OperatingSystemFamily.MacOSX && new DirectoryInfo(editorPath).Exists
  54. || SystemInfo.operatingSystemFamily != OperatingSystemFamily.MacOSX && new FileInfo(editorPath).Exists;
  55. }
  56. }
  57. }