ConflictWatcher.cs 2.3 KB

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