ConflictWatcher.cs 2.1 KB

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