Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileUtilities.cs 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.IO;
  3. using UnityEditor.ShaderGraph.Serialization;
  4. using Debug = UnityEngine.Debug;
  5. using UnityEditor.VersionControl;
  6. using System.Text;
  7. namespace UnityEditor.ShaderGraph
  8. {
  9. static class FileUtilities
  10. {
  11. // if successfully written to disk, returns the serialized file contents as a string
  12. // on failure, returns null
  13. public static string WriteShaderGraphToDisk(string path, GraphData data)
  14. {
  15. if (data == null)
  16. {
  17. // Returning false may be better than throwing this exception, in terms of preserving data.
  18. // But if GraphData is null, it's likely we don't have any data to preserve anyways.
  19. // So this exception seems fine for now.
  20. throw new ArgumentNullException(nameof(data));
  21. }
  22. var text = MultiJson.Serialize(data);
  23. if (WriteToDisk(path, text))
  24. return text;
  25. else
  26. return null;
  27. }
  28. // returns true if successfully written to disk
  29. public static bool WriteToDisk(string path, string text)
  30. {
  31. CheckoutIfValid(path);
  32. while (true)
  33. {
  34. try
  35. {
  36. File.WriteAllText(path, text);
  37. }
  38. catch (Exception e)
  39. {
  40. if (e.GetBaseException() is UnauthorizedAccessException &&
  41. (File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
  42. {
  43. if (EditorUtility.DisplayDialog("File is Read-Only", path, "Make Writeable", "Cancel Save"))
  44. {
  45. // make writeable
  46. FileInfo fileInfo = new FileInfo(path);
  47. fileInfo.IsReadOnly = false;
  48. continue; // retry save
  49. }
  50. else
  51. return false;
  52. }
  53. Debug.LogException(e);
  54. if (EditorUtility.DisplayDialog("Exception While Saving", e.ToString(), "Retry", "Cancel"))
  55. continue; // retry save
  56. else
  57. return false;
  58. }
  59. break; // no exception, file save success!
  60. }
  61. return true;
  62. }
  63. // returns contents of the asset file as a string, or null if any error or exception occurred
  64. public static string SafeReadAllText(string assetPath)
  65. {
  66. string result = null;
  67. try
  68. {
  69. result = File.ReadAllText(assetPath, Encoding.UTF8);
  70. }
  71. catch
  72. {
  73. result = null;
  74. }
  75. return result;
  76. }
  77. static void CheckoutIfValid(string path)
  78. {
  79. if (VersionControl.Provider.enabled && VersionControl.Provider.isActive)
  80. {
  81. var asset = VersionControl.Provider.GetAssetByPath(path);
  82. if (asset != null)
  83. {
  84. if (!VersionControl.Provider.IsOpenForEdit(asset))
  85. {
  86. var task = VersionControl.Provider.Checkout(asset, VersionControl.CheckoutMode.Asset);
  87. task.Wait();
  88. if (!task.success)
  89. Debug.Log(task.text + " " + task.resultCode);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }