ConflictWatcher.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. Debug.Log(relativePath);
  35. if (!path.EndsWith(".unity") && !path.EndsWith(".prefab")) return;
  36. // Found it
  37. UnityEngine.Object asset;
  38. try
  39. {
  40. asset = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(relativePath);
  41. }
  42. catch (Exception exception)
  43. {
  44. Debug.Log(exception);
  45. throw;
  46. }
  47. Debug.Log("asset" + asset);
  48. if (asset != null)
  49. {
  50. Debug.Log("Found asset" + asset.GetType());
  51. bool isBroken = asset is BrokenPrefabAsset;
  52. // Open merge conflict window
  53. if (isBroken)
  54. {
  55. OnConflict?.Invoke(relativePath);
  56. }
  57. }
  58. }
  59. static bool IsOpenForEdit(string[] paths, List<string> outNotEditablePaths, StatusQueryOptions statusQueryOptions)
  60. {
  61. Debug.Log("IsOpenForEdit:");
  62. // TODO: Do locking/unlocking here
  63. foreach (var path in paths)
  64. {
  65. // Check for lock
  66. Debug.Log(path);
  67. }
  68. return true;
  69. }
  70. }
  71. }