forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetModificationProcessor.cs
385 lines (324 loc) · 15 KB
/
AssetModificationProcessor.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Globalization;
using UnityEngine;
using UnityEditor.VersionControl;
using UnityEditorInternal;
using UnityEditorInternal.VersionControl;
using System.Linq;
using System.Reflection;
[Obsolete("Use UnityEditor.AssetModificationProcessor")]
public class AssetModificationProcessor
{
}
namespace UnityEditor
{
public class AssetModificationProcessor
{
}
internal class AssetModificationProcessorInternal
{
enum FileMode
{
Binary,
Text
}
static bool CheckArgumentTypes(Type[] types, MethodInfo method)
{
ParameterInfo[] parameters = method.GetParameters();
if (types.Length != parameters.Length)
{
Debug.LogWarning("Parameter count did not match. Expected: " + types.Length + " Got: " + parameters.Length + " in " + method.DeclaringType + "." + method.Name);
return false;
}
int i = 0;
foreach (Type type in types)
{
ParameterInfo pInfo = parameters[i];
if (type != pInfo.ParameterType)
{
Debug.LogWarning("Parameter type mismatch at parameter " + i + ". Expected: " + type + " Got: " + pInfo.ParameterType + " in " + method.DeclaringType + "." + method.Name);
return false;
}
++i;
}
return true;
}
static bool CheckArgumentTypesAndReturnType(Type[] types, MethodInfo method, System.Type returnType)
{
if (returnType != method.ReturnType)
{
Debug.LogWarning("Return type mismatch. Expected: " + returnType + " Got: " + method.ReturnType + " in " + method.DeclaringType + "." + method.Name);
return false;
}
return CheckArgumentTypes(types, method);
}
static bool CheckArguments(object[] args, MethodInfo method)
{
Type[] types = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
types[i] = args[i].GetType();
return CheckArgumentTypes(types, method);
}
static bool CheckArgumentsAndReturnType(object[] args, MethodInfo method, System.Type returnType)
{
Type[] types = new Type[args.Length];
for (int i = 0; i < args.Length; i++)
types[i] = args[i].GetType();
return CheckArgumentTypesAndReturnType(types, method, returnType);
}
#pragma warning disable 0618
static System.Collections.Generic.IEnumerable<System.Type> assetModificationProcessors = null;
static System.Collections.Generic.IEnumerable<System.Type> AssetModificationProcessors
{
get
{
if (assetModificationProcessors == null)
{
List<Type> processors = new List<Type>();
processors.AddRange(TypeCache.GetTypesDerivedFrom<AssetModificationProcessor>());
processors.AddRange(TypeCache.GetTypesDerivedFrom<global::AssetModificationProcessor>());
assetModificationProcessors = processors.ToArray();
}
return assetModificationProcessors;
}
}
#pragma warning restore 0618
static void OnWillCreateAsset(string path)
{
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
MethodInfo method = assetModificationProcessorClass.GetMethod("OnWillCreateAsset", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { path };
if (!CheckArguments(args, method))
continue;
method.Invoke(null, args);
}
}
}
// ReSharper disable once UnusedMember.Local - invoked from native code
static void FileModeChanged(string[] assets, UnityEditor.VersionControl.FileMode mode)
{
if (!Provider.enabled)
return;
// if we happen to be disconnected or work offline, there's not much we can do;
// just ignore the file mode and hope that VCS client/project is setup to handle
// appropriate file types correctly
if (!Provider.isActive)
return;
// we'll want to re-serialize these assets in different (text vs binary) mode;
// make sure they are editable first
AssetDatabase.MakeEditable(assets);
Provider.SetFileMode(assets, mode);
}
// Postprocess on all assets once an automatic import has completed
// ReSharper disable once UnusedMember.Local - invoked from native code
static void OnWillSaveAssets(string[] assets, out string[] assetsThatShouldBeSaved, out string[] assetsThatShouldBeReverted, bool explicitlySaveAsset)
{
assetsThatShouldBeReverted = new string[0];
assetsThatShouldBeSaved = assets;
bool showSaveDialog = assets.Length > 0 && EditorPrefs.GetBool("VerifySavingAssets", false) && InternalEditorUtility.isHumanControllingUs;
// If we are only saving a single scene or prefab and the user explicitly said we should, skip the dialog. We don't need
// to verify this twice.
if (explicitlySaveAsset && assets.Length == 1 && (assets[0].EndsWith(".unity") || assets[0].EndsWith(".prefab")))
showSaveDialog = false;
if (showSaveDialog)
AssetSaveDialog.ShowWindow(assets, out assetsThatShouldBeSaved);
else
assetsThatShouldBeSaved = assets;
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
MethodInfo method = assetModificationProcessorClass.GetMethod("OnWillSaveAssets", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { assetsThatShouldBeSaved };
if (!CheckArguments(args, method))
continue;
string[] result = (string[])method.Invoke(null, args);
if (result != null)
assetsThatShouldBeSaved = result;
}
}
if (assetsThatShouldBeSaved == null)
{
return;
}
var assetsNotOpened = new List<string>();
AssetDatabase.IsOpenForEdit(assetsThatShouldBeSaved, assetsNotOpened, StatusQueryOptions.ForceUpdate);
assets = assetsNotOpened.ToArray();
// Try to checkout if needed
var notEditableAssets = new List<string>();
if (assets.Length != 0 && !AssetDatabase.MakeEditable(assets, null, notEditableAssets))
{
// only save assets that can be made editable (not locked by someone else, etc.),
// unless we are in the behavior mode that just overwrites everything anyway
if (!EditorUserSettings.overwriteFailedCheckoutAssets)
{
assetsThatShouldBeReverted = notEditableAssets.ToArray();
assetsThatShouldBeSaved = assetsThatShouldBeSaved.Except(assetsThatShouldBeReverted).ToArray();
}
}
}
static AssetMoveResult OnWillMoveAsset(string fromPath, string toPath, string[] newPaths, string[] NewMetaPaths)
{
AssetMoveResult finalResult = AssetMoveResult.DidNotMove;
finalResult = AssetModificationHook.OnWillMoveAsset(fromPath, toPath);
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
MethodInfo method = assetModificationProcessorClass.GetMethod("OnWillMoveAsset", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { fromPath, toPath };
if (!CheckArgumentsAndReturnType(args, method, finalResult.GetType()))
continue;
finalResult |= (AssetMoveResult)method.Invoke(null, args);
}
}
return finalResult;
}
static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
{
AssetDeleteResult finalResult = AssetDeleteResult.DidNotDelete;
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
MethodInfo method = assetModificationProcessorClass.GetMethod("OnWillDeleteAsset", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { assetPath, options };
if (!CheckArgumentsAndReturnType(args, method, finalResult.GetType()))
continue;
finalResult |= (AssetDeleteResult)method.Invoke(null, args);
}
}
if (finalResult != AssetDeleteResult.DidNotDelete)
return finalResult;
finalResult = AssetModificationHook.OnWillDeleteAsset(assetPath, options);
return finalResult;
}
static MethodInfo[] isOpenForEditMethods = null;
static MethodInfo[] GetIsOpenForEditMethods()
{
if (isOpenForEditMethods == null)
{
List<MethodInfo> mArray = new List<MethodInfo>();
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
MethodInfo method = assetModificationProcessorClass.GetMethod("IsOpenForEdit", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
string dummy = "";
bool bool_dummy = false;
Type[] types = { dummy.GetType(), dummy.GetType().MakeByRefType() };
if (!CheckArgumentTypesAndReturnType(types, method, bool_dummy.GetType()))
continue;
mArray.Add(method);
}
}
isOpenForEditMethods = mArray.ToArray();
}
return isOpenForEditMethods;
}
enum Editability
{
Never,
Always,
Maybe
}
static Editability GetPathEditability(string assetPath)
{
// read-only asset locations (e.g. shared packages) are considered not editable
bool rootFolder, readOnly;
bool validPath = AssetDatabase.GetAssetFolderInfo(assetPath, out rootFolder, out readOnly);
if (validPath && readOnly)
return Editability.Never;
// other paths that are not know to asset database, and not versioned, are considered always editable
if (!Provider.PathIsVersioned(assetPath))
return Editability.Always;
return Editability.Maybe;
}
static bool IsOpenForEditViaScriptCallbacks(string assetPath, ref string message)
{
foreach (var method in GetIsOpenForEditMethods())
{
object[] args = {assetPath, message};
if (!(bool)method.Invoke(null, args))
{
message = args[1] as string;
return false;
}
}
return true;
}
internal static bool IsOpenForEdit(string assetPath, out string message, StatusQueryOptions statusOptions)
{
message = string.Empty;
if (string.IsNullOrEmpty(assetPath))
return true; // treat empty/null paths as editable (might be under Library folders etc.)
var editability = GetPathEditability(assetPath);
if (editability == Editability.Always)
return true;
if (editability == Editability.Never)
return false;
if (!AssetModificationHook.IsOpenForEdit(assetPath, out message, statusOptions))
return false;
if (!IsOpenForEditViaScriptCallbacks(assetPath, ref message))
return false;
return true;
}
internal static void IsOpenForEdit(string[] assetOrMetaFilePaths, List<string> outNotEditablePaths, StatusQueryOptions statusQueryOptions = StatusQueryOptions.UseCachedIfPossible)
{
outNotEditablePaths.Clear();
if (assetOrMetaFilePaths == null || assetOrMetaFilePaths.Length == 0)
return;
var queryList = new List<string>();
foreach (var path in assetOrMetaFilePaths)
{
if (string.IsNullOrEmpty(path))
continue; // treat empty/null paths as editable (might be under Library folders etc.)
var editability = GetPathEditability(path);
if (editability == Editability.Always)
continue;
if (editability == Editability.Never)
{
outNotEditablePaths.Add(path);
continue;
}
queryList.Add(path);
}
// check with VCS
AssetModificationHook.IsOpenForEdit(queryList, outNotEditablePaths, statusQueryOptions);
// check with possible script callbacks
var scriptCallbacks = GetIsOpenForEditMethods();
if (scriptCallbacks != null && scriptCallbacks.Length > 0)
{
var stillEditable = assetOrMetaFilePaths.Except(outNotEditablePaths).Where(f => !string.IsNullOrEmpty(f));
var message = string.Empty;
foreach (var path in stillEditable)
{
if (!IsOpenForEditViaScriptCallbacks(path, ref message))
outNotEditablePaths.Add(path);
}
}
}
internal static void OnStatusUpdated()
{
WindowPending.OnStatusUpdated();
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
MethodInfo method = assetModificationProcessorClass.GetMethod("OnStatusUpdated", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = {};
if (!CheckArgumentsAndReturnType(args, method, typeof(void)))
continue;
method.Invoke(null, args);
}
}
}
}
}