SRDebugEditor.Resources.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace SRDebugger.Editor
  7. {
  8. public partial class SRDebugEditor
  9. {
  10. internal const string DisabledDirectoryPostfix = "_DISABLED~";
  11. // Paths to enable/disable (relative to SRDebugger root directory)
  12. private static readonly string[] _resourcePaths = new[]
  13. {
  14. "Resources",
  15. "usr",
  16. "UI/Prefabs"
  17. };
  18. static void SetResourcesEnabled(bool enable)
  19. {
  20. AssetDatabase.StartAssetEditing();
  21. foreach (ResourceDirectory d in GetResourcePaths())
  22. {
  23. d.SetDirectoryEnabled(enable);
  24. }
  25. AssetDatabase.StopAssetEditing();
  26. AssetDatabase.Refresh();
  27. AssetDatabase.ImportAsset(SRInternalEditorUtil.GetRootPath(
  28. ), ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceUpdate);
  29. }
  30. internal static IEnumerable<ResourceDirectory> GetResourcePaths()
  31. {
  32. foreach (string resourcePath in _resourcePaths)
  33. {
  34. string enabledPath = Path.Combine(SRInternalEditorUtil.GetRootPath(), resourcePath);
  35. string disabledPath = Path.Combine(SRInternalEditorUtil.GetRootPath(), resourcePath) + DisabledDirectoryPostfix;
  36. yield return new ResourceDirectory(enabledPath, disabledPath);
  37. }
  38. }
  39. internal class ResourceDirectory
  40. {
  41. public readonly string EnabledPath;
  42. public readonly string DisabledPath;
  43. public readonly string EnabledPathMetaFile;
  44. public readonly string DisabledPathMetaFile;
  45. public readonly string DisabledPathBackupMetaFile;
  46. public bool IsEnabled
  47. {
  48. get { return Directory.Exists(EnabledPath); }
  49. }
  50. public bool IsDisabled
  51. {
  52. get { return Directory.Exists(DisabledPath); }
  53. }
  54. public ResourceDirectory(string enabledPath, string disabledPath)
  55. {
  56. EnabledPath = enabledPath;
  57. DisabledPath = disabledPath;
  58. EnabledPathMetaFile = enabledPath + ".meta";
  59. DisabledPathMetaFile = disabledPath + ".meta";
  60. DisabledPathBackupMetaFile = disabledPath + ".meta.bak~";
  61. }
  62. public void SetDirectoryEnabled(bool enable)
  63. {
  64. if (IsEnabled && enable)
  65. {
  66. return;
  67. }
  68. if (IsDisabled && !enable)
  69. {
  70. return;
  71. }
  72. if (IsEnabled && IsDisabled)
  73. {
  74. // TODO
  75. throw new Exception();
  76. }
  77. string title = string.Format("SRDebugger - {0} Resources", enable ? "Enable" : "Disable");
  78. string oldPath = enable ? DisabledPath : EnabledPath;
  79. string newPath = enable ? EnabledPath : DisabledPath;
  80. bool useAssetDatabase = !enable;
  81. string error = null;
  82. if (useAssetDatabase)
  83. {
  84. error = AssetDatabase.MoveAsset(oldPath, newPath);
  85. if (!string.IsNullOrEmpty(error))
  86. {
  87. if (EditorUtility.DisplayDialog(title, GetErrorMessage(enable, error), "Force Move", "Abort"))
  88. {
  89. useAssetDatabase = false;
  90. }
  91. }
  92. }
  93. if (!useAssetDatabase)
  94. {
  95. try
  96. {
  97. Directory.Move(oldPath, newPath);
  98. }
  99. catch (Exception e)
  100. {
  101. Debug.LogError("Error moving directory");
  102. Debug.LogException(e);
  103. error = "Exception occurred, see console for details.";
  104. }
  105. }
  106. if (!string.IsNullOrEmpty(error))
  107. {
  108. string message = string.Format(
  109. "An error occurred while attempting to {3} SRDebugger resource directory.\n\n Old Path: {0}\n New Path: {1}\n\n Error: \n{2}",
  110. EnabledPath, DisabledPath, error, enable ? "enable" : "disable");
  111. EditorUtility.DisplayDialog(title, message, "Continue");
  112. return;
  113. }
  114. if (!enable)
  115. {
  116. // Disable meta files
  117. if (File.Exists(DisabledPathMetaFile))
  118. {
  119. if (File.Exists(DisabledPathBackupMetaFile))
  120. {
  121. File.Delete(DisabledPathBackupMetaFile);
  122. }
  123. File.Move(DisabledPathMetaFile, DisabledPathBackupMetaFile);
  124. }
  125. }
  126. else
  127. {
  128. // Enable backed up meta files
  129. if (File.Exists(DisabledPathBackupMetaFile))
  130. {
  131. if (File.Exists(EnabledPathMetaFile))
  132. {
  133. File.Delete(EnabledPathMetaFile);
  134. }
  135. File.Move(DisabledPathBackupMetaFile, EnabledPathMetaFile);
  136. }
  137. }
  138. }
  139. private string GetErrorMessage(bool enable, string error)
  140. {
  141. return string.Format(
  142. "An error occurred while attempting to {3} SRDebugger resources. \n\n Old Path: {0}\n New Path: {1}\n\n Error: \n{2}",
  143. EnabledPath, DisabledPath, error, enable ? "enable" : "disable");
  144. }
  145. }
  146. }
  147. }