1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using GitHub . Logging ;
4
+ using UnityEditor ;
5
+
6
+ namespace GitHub . Unity
7
+ {
8
+ class LfsLocksModificationProcessor : UnityEditor . AssetModificationProcessor
9
+ {
10
+ private static ILogging logger ;
11
+ private static ILogging Logger { get { return logger = logger ?? LogHelper . GetLogger < LfsLocksModificationProcessor > ( ) ; } }
12
+
13
+ private static IRepository repository ;
14
+
15
+ private static List < GitLock > locks = new List < GitLock > ( ) ;
16
+
17
+ private static CacheUpdateEvent lastLocksChangedEvent ;
18
+
19
+ public static void Initialize ( IRepository repo )
20
+ {
21
+ Logger . Trace ( "Initialize HasRepository:{0}" , repo != null ) ;
22
+
23
+ repository = repo ;
24
+
25
+ if ( repository != null )
26
+ {
27
+ repository . LocksChanged += RepositoryOnLocksChanged ;
28
+ repository . CheckLocksChangedEvent ( lastLocksChangedEvent ) ;
29
+ }
30
+ }
31
+
32
+ private static void RepositoryOnLocksChanged ( CacheUpdateEvent cacheUpdateEvent )
33
+ {
34
+ if ( ! lastLocksChangedEvent . Equals ( cacheUpdateEvent ) )
35
+ {
36
+ lastLocksChangedEvent = cacheUpdateEvent ;
37
+ locks = repository . CurrentLocks ;
38
+ }
39
+ }
40
+
41
+ public static string [ ] OnWillSaveAssets ( string [ ] paths )
42
+ {
43
+ Logger . Trace ( "OnWillSaveAssets: [{0}]" , string . Join ( ", " , paths ) ) ;
44
+ return paths ;
45
+ }
46
+
47
+ public static AssetMoveResult OnWillMoveAsset ( string oldPath , string newPath )
48
+ {
49
+ Logger . Trace ( "OnWillMoveAsset:{0}->{1}" , oldPath , newPath ) ;
50
+
51
+ var result = AssetMoveResult . DidNotMove ;
52
+ if ( IsLocked ( oldPath ) )
53
+ {
54
+ result = AssetMoveResult . FailedMove ;
55
+ }
56
+ else if ( IsLocked ( newPath ) )
57
+ {
58
+ result = AssetMoveResult . FailedMove ;
59
+ }
60
+ return result ;
61
+ }
62
+
63
+ public static AssetDeleteResult OnWillDeleteAsset ( string assetPath , RemoveAssetOptions option )
64
+ {
65
+ Logger . Trace ( "OnWillDeleteAsset:{0}" , assetPath ) ;
66
+
67
+ if ( IsLocked ( assetPath ) )
68
+ {
69
+ return AssetDeleteResult . FailedDelete ;
70
+ }
71
+ return AssetDeleteResult . DidNotDelete ;
72
+ }
73
+
74
+ public static bool IsOpenForEdit ( string assetPath , out string message )
75
+ {
76
+ Logger . Trace ( "IsOpenForEdit:{0}" , assetPath ) ;
77
+
78
+ if ( IsLocked ( assetPath ) )
79
+ {
80
+ message = "File is locked for editing!" ;
81
+ return false ;
82
+ }
83
+ else
84
+ {
85
+ message = null ;
86
+ return true ;
87
+ }
88
+ }
89
+
90
+ private static bool IsLocked ( string assetPath )
91
+ {
92
+ if ( repository != null )
93
+ {
94
+ var repositoryPath = EntryPoint . Environment . GetRepositoryPath ( assetPath . ToNPath ( ) ) ;
95
+ var gitLock = locks . FirstOrDefault ( @lock => @lock . Path == repositoryPath ) ;
96
+ if ( ! gitLock . Equals ( GitLock . Default ) )
97
+ {
98
+ Logger . Trace ( "Lock found on: {0}" , assetPath ) ;
99
+
100
+ //TODO: Check user and return true
101
+ }
102
+ }
103
+
104
+ return false ;
105
+ }
106
+ }
107
+ }
0 commit comments