A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.Collections.ObjectModel;
3using System.Linq;
4
5using UnityEditor;
6
7using Codice.LogWrapper;
8using Unity.PlasticSCM.Editor.AssetsOverlays.Cache;
9using Unity.PlasticSCM.Editor.UI;
10
11using AssetOverlays = Unity.PlasticSCM.Editor.AssetsOverlays;
12
13namespace Unity.PlasticSCM.Editor.AssetUtils.Processor
14{
15 class AssetModificationProcessor : UnityEditor.AssetModificationProcessor
16 {
17 internal static bool IsManualCheckoutEnabled { get; private set; }
18
19 static AssetModificationProcessor()
20 {
21 IsManualCheckoutEnabled = EditorPrefs.GetBool(
22 UnityConstants.FORCE_CHECKOUT_KEY_NAME);
23 }
24
25 internal static void Enable(
26 string wkPath,
27 IAssetStatusCache assetStatusCache)
28 {
29 mLog.Debug("Enable");
30
31 mWkPath = wkPath;
32 mAssetStatusCache = assetStatusCache;
33
34 mIsEnabled = true;
35 }
36
37 internal static void Disable()
38 {
39 mLog.Debug("Disable");
40
41 mIsEnabled = false;
42
43 ModifiedAssets.Clear();
44
45 mWkPath = null;
46 mAssetStatusCache = null;
47 }
48
49 internal static void SetManualCheckoutPreference(bool isEnabled)
50 {
51 if (IsManualCheckoutEnabled == isEnabled)
52 return;
53
54 IsManualCheckoutEnabled = isEnabled;
55
56 EditorPrefs.SetBool(
57 UnityConstants.FORCE_CHECKOUT_KEY_NAME,
58 isEnabled);
59 }
60
61 internal static ReadOnlyCollection<string> GetModifiedAssetsToProcess()
62 {
63 return ModifiedAssets.ToList().AsReadOnly();
64 }
65
66 internal static string[] ExtractModifiedAssetsToProcess()
67 {
68 string[] result = ModifiedAssets.ToArray();
69
70 ModifiedAssets.Clear();
71
72 return result;
73 }
74
75 static string[] OnWillSaveAssets(string[] paths)
76 {
77 if (!mIsEnabled)
78 return paths;
79
80 foreach (string path in paths)
81 {
82 ModifiedAssets.Add(path);
83 }
84
85 return paths;
86 }
87
88 // If IsOpenForEdit returns false, the preCheckoutCallback is invoked
89 // to perform the checkout operation and make the asset editable.
90 static bool IsOpenForEdit(string assetPath, out string message)
91 {
92 message = string.Empty;
93
94 if (!mIsEnabled)
95 return true;
96
97 if (!IsManualCheckoutEnabled)
98 return true;
99
100 if (assetPath.StartsWith("ProjectSettings/"))
101 return true;
102
103 string assetFullPath = AssetsPath.GetFullPathUnderWorkspace.
104 ForAsset(mWkPath, assetPath);
105
106 if (assetFullPath == null)
107 return true;
108
109 if (MetaPath.IsMetaPath(assetFullPath))
110 assetFullPath = MetaPath.GetPathFromMetaPath(assetFullPath);
111
112 AssetOverlays.AssetStatus status = mAssetStatusCache.
113 GetStatus(assetFullPath);
114
115 if (AssetOverlays.ClassifyAssetStatus.IsAdded(status) ||
116 AssetOverlays.ClassifyAssetStatus.IsCheckedOut(status))
117 return true;
118
119 return !AssetOverlays.ClassifyAssetStatus.IsControlled(status);
120 }
121
122 // We need to process the modified assets to perform their check-out.
123 // To do this, we must verify their content, date, and size to determine if they
124 // have actually changed. This requires the changes to be written to disk first.
125 // To ensure this, we store the modified files in this array and process them
126 // when they are reloaded in AssetPostprocessor.OnPostprocessAllAssets.
127 static readonly HashSet<string> ModifiedAssets = new HashSet<string>();
128
129 static IAssetStatusCache mAssetStatusCache;
130 static string mWkPath;
131 static bool mIsEnabled;
132
133 static readonly ILog mLog = PlasticApp.GetLogger("AssetModificationProcessor");
134 }
135}