Migrations.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using SRF;
  5. using UnityEngine;
  6. using UnityEditor;
  7. #pragma warning disable 162
  8. namespace SRDebugger.Editor
  9. {
  10. [InitializeOnLoad]
  11. static class Migrations
  12. {
  13. static Migrations()
  14. {
  15. RunMigrations();
  16. }
  17. private const bool EnableLog = false;
  18. public class Migration
  19. {
  20. public readonly string Id;
  21. public readonly string[] ObsoleteFiles;
  22. public Migration(string id, string[] obsoleteFiles)
  23. {
  24. Id = id;
  25. ObsoleteFiles = obsoleteFiles;
  26. }
  27. }
  28. public static List<Migration> AvailableMigrations = new List<Migration>()
  29. {
  30. new Migration("DeleteOldEditorResources", new[]
  31. {
  32. "Editor/Resources/SRDebugger/BG_Dark.png",
  33. "Editor/Resources/SRDebugger/BG_Light.png",
  34. "Editor/Resources/SRDebugger/DemoSprite.png",
  35. "Editor/Resources/SRDebugger/Logo_DarkBG.png",
  36. "Editor/Resources/SRDebugger/Logo_LightBG.png",
  37. "Editor/Resources/SRDebugger/WelcomeLogo_DarkBG.png",
  38. "Editor/Resources/SRDebugger/WelcomeLogo_LightBG.png",
  39. "Editor/Resources/SRDebugger/Icons/Dark/console-25.png",
  40. "Editor/Resources/SRDebugger/Icons/Dark/options-25.png",
  41. "Editor/Resources/SRDebugger/Icons/Dark/profiler-25.png",
  42. "Editor/Resources/SRDebugger/Icons/Light/console-25.png",
  43. "Editor/Resources/SRDebugger/Icons/Light/options-25.png",
  44. "Editor/Resources/SRDebugger/Icons/Light/profiler-25.png",
  45. })
  46. };
  47. public static void RunMigrations(bool forceRun = false)
  48. {
  49. if(EnableLog)
  50. Debug.Log("[SRDebugger] Running Migrations...");
  51. foreach (var m in AvailableMigrations)
  52. {
  53. var key = GetProjectPrefsKey(m.Id);
  54. if (!forceRun && EditorPrefs.GetBool(key, false))
  55. {
  56. continue;
  57. }
  58. EditorPrefs.SetBool(key, true);
  59. RunMigration(m);
  60. }
  61. }
  62. public static void RunMigration(Migration migration)
  63. {
  64. if (EnableLog)
  65. Debug.Log("Running Migration: " + migration.Id);
  66. var assetPaths = AssetDatabase.GetAllAssetPaths();
  67. var root = new DirectoryInfo(SRInternalEditorUtil.GetRootPath());
  68. if(EnableLog)
  69. Debug.Log("Using Root Path: " + root.FullName);
  70. var obsoleteAssets = migration.ObsoleteFiles.Select(p => root + "/" + p).ToList();
  71. var deleteQueue = assetPaths.Where(assetPath => obsoleteAssets.Contains(assetPath)).ToList();
  72. if (deleteQueue.Count == 0)
  73. return;
  74. var message = "The following files used by a previous version of SRDebugger are obsolete and can be safely deleted: \n\n" +
  75. deleteQueue.Aggregate((s1, s2) => s1 + "\n" + s2);
  76. Debug.Log(message);
  77. message += "\n\nIt is recommended to delete these files.";
  78. if (EditorUtility.DisplayDialog("SRDebugger Migration Assistant",
  79. message, "Delete Now", "Ignore"))
  80. {
  81. foreach (var s in deleteQueue)
  82. {
  83. Debug.Log("[SRDebugger] Deleting Asset {0}".Fmt(s));
  84. if (!AssetDatabase.DeleteAsset(s))
  85. {
  86. Debug.LogWarning("[SRDebugger] Error deleting asset {0}".Fmt(s));
  87. }
  88. }
  89. Debug.Log("[SRDebugger] Migration Complete");
  90. }
  91. else
  92. {
  93. EditorUtility.DisplayDialog("SRDebugger Migration Assitant",
  94. "You can run this migration check again via the \"Run Migrations\" button in the advanced tab of the SRDebugger settings window.", "OK");
  95. }
  96. }
  97. private static string GetProjectPrefsKey(string key)
  98. {
  99. return "SRDebugger_Migration_" + Application.dataPath + "_" + key;
  100. }
  101. }
  102. }