This repository was archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathLfsLocksModificationProcessor.cs
137 lines (119 loc) · 4.95 KB
/
LfsLocksModificationProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections.Generic;
using System.Linq;
using GitHub.Logging;
using UnityEditor;
using UnityEngine;
namespace GitHub.Unity
{
class LfsLocksModificationProcessor : UnityEditor.AssetModificationProcessor
{
private static ILogging Logger = LogHelper.GetLogger<LfsLocksModificationProcessor>();
private static IRepository repository;
private static IPlatform platform;
private static IEnvironment environment;
private static Dictionary<NPath, GitLock> locks = new Dictionary<NPath, GitLock>();
private static CacheUpdateEvent lastLocksChangedEvent;
private static string loggedInUser;
public static void Initialize(IEnvironment env, IPlatform plat)
{
environment = env;
platform = plat;
platform.Keychain.ConnectionsChanged += UserMayHaveChanged;
// we need to do this to get the initial user information up front
UserMayHaveChanged();
repository = environment.Repository;
UnityShim.Editor_finishedDefaultHeaderGUI += InspectorHeaderFinished;
if (repository != null)
{
repository.LocksChanged += RepositoryOnLocksChanged;
repository.CheckAndRaiseEventsIfCacheNewer(CacheType.GitLocks, lastLocksChangedEvent);
}
}
public static string[] OnWillSaveAssets(string[] paths)
{
return paths;
}
public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath)
{
return IsLockedBySomeoneElse(oldPath) || IsLockedBySomeoneElse(newPath) ? AssetMoveResult.FailedMove : AssetMoveResult.DidNotMove;
}
public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
{
return IsLockedBySomeoneElse(assetPath) ? AssetDeleteResult.FailedDelete : AssetDeleteResult.DidNotDelete;
}
// Returns true if this file can be edited by this user
public static bool IsOpenForEdit(string assetPath, out string message)
{
var lck = GetLock(assetPath);
bool canEdit = true;
if (assetPath.EndsWith(".meta"))
{
canEdit &= !IsLockedBySomeoneElse(lck);
assetPath = assetPath.TrimEnd(".meta");
}
canEdit &= !IsLockedBySomeoneElse(lck);
message = !canEdit ? string.Format("File is locked for editing by {0}", lck.Value.Owner.Name) : null;
return canEdit;
}
private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
{
if (!lastLocksChangedEvent.Equals(cacheUpdateEvent))
{
lastLocksChangedEvent = cacheUpdateEvent;
locks = repository.CurrentLocks.ToDictionary(gitLock => gitLock.Path);
}
}
private static void UserMayHaveChanged()
{
loggedInUser = platform.Keychain.Connections.Select(x => x.Username).FirstOrDefault();
}
private static bool IsLockedBySomeoneElse(GitLock? lck)
{
return lck.HasValue && !lck.Value.Owner.Name.Equals(loggedInUser);
}
private static bool IsLockedBySomeoneElse(string assetPath)
{
return IsLockedBySomeoneElse(GetLock(assetPath));
}
private static GitLock? GetLock(string assetPath)
{
if (repository == null)
return null;
GitLock lck;
var repositoryPath = environment.GetRepositoryPath(assetPath.ToNPath());
if (locks.TryGetValue(repositoryPath, out lck))
return lck;
return null;
}
private static void InspectorHeaderFinished(Editor editor)
{
string message = "";
if (!IsOpenForEdit(AssetDatabase.GetAssetPath(editor.target), out message))
{
var enabled = GUI.enabled;
GUI.enabled = true;
GUILayout.BeginVertical();
{
GUILayout.Space(9);
GUILayout.BeginHorizontal();
{
GUILayout.BeginVertical(GUILayout.Width(32));
{
GUILayout.Label(Utility.GetIcon("big-logo.png", "[email protected]", Utility.IsDarkTheme), GUILayout.Width(32), GUILayout.Height(32));
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
{
GUILayout.Space(9);
GUILayout.Label(message, Styles.HeaderBranchLabelStyle);
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
GUI.enabled = enabled;
}
}
}
}