RealWorldTerrainClearCacheWindow.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace InfinityCode.RealWorldTerrain.Windows
  7. {
  8. public class RealWorldTerrainClearCacheWindow : EditorWindow
  9. {
  10. private bool clearOSM = false;
  11. private bool clearSRTM = false;
  12. private bool clearTexture = false;
  13. private bool clearTextureErrorOnly = false;
  14. private bool clearHistory = false;
  15. private bool clearSettings = false;
  16. private long osmSize;
  17. private long srtmSize;
  18. private long textureSize;
  19. private void Clear()
  20. {
  21. if (clearSRTM) RealWorldTerrainUtils.SafeDeleteDirectory(RealWorldTerrainEditorUtils.heightmapCacheFolder);
  22. if (clearOSM) RealWorldTerrainUtils.SafeDeleteDirectory(RealWorldTerrainEditorUtils.osmCacheFolder);
  23. if (clearTexture)
  24. {
  25. if (!clearTextureErrorOnly)
  26. RealWorldTerrainUtils.SafeDeleteDirectory(RealWorldTerrainEditorUtils.textureCacheFolder);
  27. else
  28. {
  29. string[] files = Directory.GetFiles(RealWorldTerrainEditorUtils.textureCacheFolder, "*.err",
  30. SearchOption.AllDirectories);
  31. foreach (string file in files) RealWorldTerrainUtils.SafeDeleteFile(file);
  32. }
  33. }
  34. if (clearHistory)
  35. {
  36. RealWorldTerrainUtils.SafeDeleteDirectory(RealWorldTerrainEditorUtils.historyCacheFolder);
  37. RealWorldTerrainHistoryWindow.Load();
  38. }
  39. if (clearSettings)
  40. {
  41. if (File.Exists(RealWorldTerrainPrefs.prefsFilename)) File.Delete(RealWorldTerrainPrefs.prefsFilename);
  42. RealWorldTerrainSettingsWindow.ClearSettings();
  43. RealWorldTerrainEditorUtils.ClearFoldersCache();
  44. }
  45. EditorUtility.DisplayDialog("Complete", "Clear cache complete.", "OK");
  46. Close();
  47. }
  48. private void OnEnable()
  49. {
  50. osmSize = RealWorldTerrainUtils.GetDirectorySize(RealWorldTerrainEditorUtils.osmCacheFolder);
  51. srtmSize = RealWorldTerrainUtils.GetDirectorySize(RealWorldTerrainEditorUtils.heightmapCacheFolder);
  52. textureSize = RealWorldTerrainUtils.GetDirectorySize(RealWorldTerrainEditorUtils.textureCacheFolder);
  53. }
  54. public static string FormatSize(long size)
  55. {
  56. if (size > 10485760) return size / 1048576 + " MB";
  57. if (size > 1024) return (size / 1048576f).ToString("0.000") + " MB";
  58. return size + " B";
  59. }
  60. private void OnGUI()
  61. {
  62. clearSRTM = GUILayout.Toggle(clearSRTM, "Clear elevation cache (" + FormatSize(srtmSize) + ")");
  63. clearTexture = GUILayout.Toggle(clearTexture, "Clear texture cache (" + FormatSize(textureSize) + ")");
  64. if (clearTexture)
  65. {
  66. GUILayout.BeginHorizontal();
  67. GUILayout.Space(20);
  68. clearTextureErrorOnly = GUILayout.Toggle(clearTextureErrorOnly, "Errors only");
  69. GUILayout.EndHorizontal();
  70. }
  71. clearOSM = GUILayout.Toggle(clearOSM, "Clear OSM cache (" + FormatSize(osmSize) + ")");
  72. clearHistory = GUILayout.Toggle(clearHistory, "Clear History");
  73. clearSettings = GUILayout.Toggle(clearSettings, "Clear Settings");
  74. if (GUILayout.Button("Open Cache Folder")) EditorUtility.RevealInFinder(RealWorldTerrainEditorUtils.cacheFolder);
  75. if (GUILayout.Button("Clear")) Clear();
  76. }
  77. public static void OpenWindow()
  78. {
  79. RealWorldTerrainClearCacheWindow wnd = GetWindow<RealWorldTerrainClearCacheWindow>(true, "Clear cache");
  80. DontDestroyOnLoad(wnd);
  81. }
  82. }
  83. }