ConflictWatcher.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace GitMerge
  7. {
  8. [InitializeOnLoad]
  9. public class ConflictWatcher
  10. {
  11. private static FileSystemWatcher watcher;
  12. private static FileSystemEventArgs e;
  13. public ConflictWatcher()
  14. {
  15. string assetPath = Application.dataPath;
  16. watcher = new FileSystemWatcher(assetPath);
  17. watcher.IncludeSubdirectories = true;
  18. watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
  19. watcher.Changed += OnFileChanged;
  20. watcher.EnableRaisingEvents = true;
  21. }
  22. public event Action<string> OnConflict;
  23. private void OnFileChanged(object sender, FileSystemEventArgs _e)
  24. {
  25. e = _e;
  26. EditorApplication.update += CheckWindow;
  27. }
  28. private void CheckWindow()
  29. {
  30. AssetDatabase.Refresh();
  31. EditorApplication.update -= CheckWindow;
  32. var path = e.FullPath;
  33. var relativePath = "Assets" + path.Substring(Application.dataPath.Length);
  34. if (!path.EndsWith(".unity") && !path.EndsWith(".prefab")) return;
  35. // Found it
  36. UnityEngine.Object asset;
  37. try
  38. {
  39. asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(relativePath);
  40. }
  41. catch (Exception exception)
  42. {
  43. Debug.Log(exception);
  44. throw;
  45. }
  46. Debug.Log("asset" + asset);
  47. if (asset != null)
  48. {
  49. Debug.Log("Found asset" + asset.GetType());
  50. bool isBroken = asset is BrokenPrefabAsset;
  51. // Open merge conflict window
  52. if (isBroken)
  53. {
  54. OnConflict?.Invoke(relativePath);
  55. }
  56. }
  57. }
  58. static bool IsOpenForEdit(string[] paths, List<string> outNotEditablePaths, StatusQueryOptions statusQueryOptions)
  59. {
  60. Debug.Log("IsOpenForEdit:");
  61. // TODO: Do locking/unlocking here
  62. foreach (var path in paths)
  63. {
  64. // Check for lock
  65. Debug.Log(path);
  66. }
  67. return true;
  68. }
  69. }
  70. }