-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathAssetModificationProcessor.cs
More file actions
609 lines (515 loc) · 25.6 KB
/
AssetModificationProcessor.cs
File metadata and controls
609 lines (515 loc) · 25.6 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// 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 UnityEngine;
using UnityEngine.Scripting;
using UnityEditor.VersionControl;
using UnityEditorInternal;
using UnityEditorInternal.VersionControl;
using System.Linq;
using System.Reflection;
using UnityEditor.Profiling;
using UnityEngine.Scripting.APIUpdating;
namespace UnityEditor
{
[MovedFrom("")]
public class AssetModificationProcessor
{
}
internal class AssetModificationProcessorInternal
{
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>());
assetModificationProcessors = processors.ToArray();
}
return assetModificationProcessors;
}
}
#pragma warning restore 0618
[RequiredByNativeCode]
internal static void OnWillCreateAsset(string path)
{
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
const string methodName = "OnWillCreateAsset";
MethodInfo method = assetModificationProcessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { path };
if (!CheckArguments(args, method))
continue;
using (new EditorPerformanceMarker($"{assetModificationProcessorClass.Name}.{methodName}", assetModificationProcessorClass).Auto())
method.Invoke(null, args);
}
}
}
// ReSharper disable once UnusedMember.Local - invoked from native code
static void FileModeChanged(string[] assets, FileMode mode)
{
AssetModificationHook.FileModeChanged(assets, mode);
object[] args = { assets, mode };
foreach (var type in AssetModificationProcessors)
{
const string methodName = "FileModeChanged";
var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method == null)
continue;
if (!CheckArgumentsAndReturnType(args, method, typeof(void)))
continue;
using (new EditorPerformanceMarker($"{type.Name}.{methodName}", type).Auto())
method.Invoke(null, args);
}
}
// Postprocess on all assets once an automatic import has completed
// ReSharper disable once UnusedMember.Local - invoked from native code
[RequiredByNativeCode]
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)
{
const string methodName = "OnWillSaveAssets";
MethodInfo method = assetModificationProcessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { assetsThatShouldBeSaved };
if (!CheckArguments(args, method))
continue;
string[] result;
using (new EditorPerformanceMarker($"{assetModificationProcessorClass.Name}.{methodName}", assetModificationProcessorClass).Auto())
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)
{
const string methodName = "OnWillMoveAsset";
MethodInfo method = assetModificationProcessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { fromPath, toPath };
if (!CheckArgumentsAndReturnType(args, method, finalResult.GetType()))
continue;
using (new EditorPerformanceMarker($"{assetModificationProcessorClass.Name}.{methodName}", assetModificationProcessorClass).Auto())
finalResult |= (AssetMoveResult)method.Invoke(null, args);
}
}
return finalResult;
}
static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions options)
{
AssetDeleteResult finalResult = AssetDeleteResult.DidNotDelete;
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
const string methodName = "OnWillDeleteAsset";
MethodInfo method = assetModificationProcessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = { assetPath, options };
if (!CheckArgumentsAndReturnType(args, method, finalResult.GetType()))
continue;
using (new EditorPerformanceMarker($"{assetModificationProcessorClass.Name}.{methodName}", assetModificationProcessorClass).Auto())
finalResult |= (AssetDeleteResult)method.Invoke(null, args);
}
}
if (finalResult != AssetDeleteResult.DidNotDelete)
return finalResult;
finalResult = AssetModificationHook.OnWillDeleteAsset(assetPath, options);
return finalResult;
}
static void OnWillDeleteAssets(string[] assetPaths, AssetDeleteResult[] outPathDeletionResults, RemoveAssetOptions options)
{
for (int i = 0; i < outPathDeletionResults.Length; i++)
outPathDeletionResults[i] = (int)AssetDeleteResult.DidNotDelete;
List<string> nonDeletedPaths = new List<string>();
List<int> nonDeletedPathIndices = new List<int>();
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
const string methodName = "OnWillDeleteAsset";
MethodInfo method = assetModificationProcessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method == null)
continue;
for (int i = 0; i < assetPaths.Length; i++)
{
object[] args = { assetPaths[i], options };
if (!CheckArgumentsAndReturnType(args, method, typeof(AssetDeleteResult)))
continue;
using (new EditorPerformanceMarker($"{assetModificationProcessorClass.Name}.{methodName}", assetModificationProcessorClass).Auto())
{
AssetDeleteResult callbackResult = (AssetDeleteResult)method.Invoke(null, args);
outPathDeletionResults[i] |= callbackResult;
}
}
}
for (int i = 0; i < assetPaths.Length; i++)
{
if (outPathDeletionResults[i] == (int)AssetDeleteResult.DidNotDelete)
{
nonDeletedPaths.Add(assetPaths[i]);
nonDeletedPathIndices.Add(i);
}
}
if (nonDeletedPaths.Count > 0)
{
if (!Provider.enabled || EditorUserSettings.WorkOffline)
return;
for (int i = 0; i < nonDeletedPaths.Count; i++)
{
if (!Provider.PathIsVersioned(nonDeletedPaths[i]))
{
nonDeletedPaths.RemoveAt(i);
nonDeletedPathIndices.RemoveAt(i);
i--;
}
}
var nonDeletedPathDeletionResults = new AssetDeleteResult[nonDeletedPaths.Count];
AssetModificationHook.OnWillDeleteAssets(nonDeletedPaths.ToArray(), nonDeletedPathDeletionResults, options);
for (int i = 0; i < nonDeletedPathIndices.Count; i++)
{
outPathDeletionResults[nonDeletedPathIndices[i]] = nonDeletedPathDeletionResults[i];
}
}
}
static MethodInfo[] s_CanOpenForEditMethods;
static MethodInfo[] s_LegacyCanOpenForEditMethods;
static MethodInfo[] s_IsOpenForEditMethods;
static MethodInfo[] s_LegacyIsOpenForEditMethods;
static void GetOpenForEditMethods(bool canOpenForEditVariant, out MethodInfo[] methods, out MethodInfo[] legacyMethods)
{
if (canOpenForEditVariant)
{
if (s_CanOpenForEditMethods == null)
GetOpenForEditMethods("CanOpenForEdit", out s_CanOpenForEditMethods, out s_LegacyCanOpenForEditMethods);
methods = s_CanOpenForEditMethods;
legacyMethods = s_LegacyCanOpenForEditMethods;
}
else
{
if (s_IsOpenForEditMethods == null)
GetOpenForEditMethods("IsOpenForEdit", out s_IsOpenForEditMethods, out s_LegacyIsOpenForEditMethods);
methods = s_IsOpenForEditMethods;
legacyMethods = s_LegacyIsOpenForEditMethods;
}
}
static void GetOpenForEditMethods(string name, out MethodInfo[] methods, out MethodInfo[] legacyMethods)
{
var methodList = new List<MethodInfo>();
var legacyMethodList = new List<MethodInfo>();
Type[] types = { typeof(string[]), typeof(List<string>), typeof(StatusQueryOptions) };
Type[] legacyTypes = { typeof(string), typeof(string).MakeByRefType() };
foreach (var type in AssetModificationProcessors)
{
// First look for a method with a signature that accepts multiple paths "bool (string[] assetOrMetaFilePaths, List<string> outNotEditablePaths, StatusQueryOptions statusQueryOptions)".
MethodInfo method;
try
{
method = type.GetMethod(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, types, null);
}
catch (AmbiguousMatchException)
{
Debug.LogWarning($"Ambiguous {name} methods in {type.Name}.");
continue;
}
if (method?.ReturnType == typeof(bool))
{
methodList.Add(method);
continue;
}
// Then look for a legacy method that accepts single path only "bool (string assetOrMetaFilePath, out string message)".
MethodInfo legacyMethod;
try
{
legacyMethod = type.GetMethod(name, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
}
catch (AmbiguousMatchException)
{
Debug.LogWarning($"Ambiguous {name} methods in {type.Name}.");
continue;
}
if (legacyMethod != null && CheckArgumentTypesAndReturnType(legacyTypes, legacyMethod, typeof(bool)))
legacyMethodList.Add(legacyMethod);
}
methods = methodList.ToArray();
legacyMethods = legacyMethodList.ToArray();
}
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.TryGetAssetFolderInfo(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 (!VersionControlUtils.IsPathVersioned(assetPath))
return Editability.Always;
return Editability.Maybe;
}
static bool GetOpenForEditViaScriptCallbacks(bool canOpenForEditVariant, string[] paths, List<string> notEditablePaths, out string message, StatusQueryOptions options)
{
message = string.Empty;
GetOpenForEditMethods(canOpenForEditVariant, out var methods, out var legacyMethods);
if (methods.Length != 0)
{
object[] args = { paths, notEditablePaths, options };
foreach (var method in methods)
{
if (!(bool)method.Invoke(null, args))
return false;
}
}
if (legacyMethods.Length != 0)
{
foreach (var legacyMethod in legacyMethods)
{
var result = true;
foreach (var path in paths)
{
object[] legacyArgs = { path, null };
if (!(bool)legacyMethod.Invoke(null, legacyArgs))
{
result = false;
notEditablePaths.Add(path);
message = legacyArgs[1] as string;
}
}
if (!result)
return false;
}
}
return true;
}
internal static bool CanOpenForEdit(string assetPath, out string message, StatusQueryOptions statusOptions)
{
if (IsOpenForEdit(assetPath, out message, statusOptions))
return true;
// Status has just been updated so there's no need to force update again.
if (statusOptions == StatusQueryOptions.ForceUpdate)
statusOptions = StatusQueryOptions.UseCachedIfPossible;
return GetOpenForEdit(true, assetPath, out message, statusOptions);
}
internal static bool IsOpenForEdit(string assetPath, out string message, StatusQueryOptions statusOptions)
{
return GetOpenForEdit(false, assetPath, out message, statusOptions);
}
static bool GetOpenForEdit(bool canOpenForEditVariant, 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.GetOpenForEdit(canOpenForEditVariant, assetPath, out message, statusOptions))
return false;
return GetOpenForEditViaScriptCallbacks(canOpenForEditVariant, new[] { assetPath }, new List<string>(), out message, statusOptions);
}
internal static bool CanOpenForEdit(string[] assetOrMetaFilePaths, List<string> outNotEditablePaths, StatusQueryOptions statusQueryOptions)
{
outNotEditablePaths.Clear();
if (assetOrMetaFilePaths == null || assetOrMetaFilePaths.Length == 0)
return true;
var queryList = GetQueryList(assetOrMetaFilePaths, outNotEditablePaths);
if (queryList.Count == 0)
return outNotEditablePaths.Count == 0;
// Get a list of paths that are not open for edit.
var notOpenForEditPaths = new List<string>();
AssetModificationHook.GetOpenForEdit(false, queryList, notOpenForEditPaths, statusQueryOptions);
GetOpenForEditViaScriptCallbacks(false, queryList.ToArray(), notOpenForEditPaths, out var message, statusQueryOptions);
if (notOpenForEditPaths.Count == 0)
return outNotEditablePaths.Count == 0;
// Status has just been updated so there's no need to force update again.
if (statusQueryOptions == StatusQueryOptions.ForceUpdate)
statusQueryOptions = StatusQueryOptions.UseCachedIfPossible;
// Check paths that are not open for edit.
if (!AssetModificationHook.GetOpenForEdit(true, notOpenForEditPaths, outNotEditablePaths, statusQueryOptions))
return false;
return GetOpenForEditViaScriptCallbacks(true, notOpenForEditPaths.ToArray(), outNotEditablePaths, out message, statusQueryOptions);
}
internal static bool IsOpenForEdit(string[] assetOrMetaFilePaths, List<string> outNotEditablePaths, StatusQueryOptions statusQueryOptions)
{
outNotEditablePaths.Clear();
if (assetOrMetaFilePaths == null || assetOrMetaFilePaths.Length == 0)
return true;
var queryList = GetQueryList(assetOrMetaFilePaths, outNotEditablePaths);
if (queryList.Count == 0)
return outNotEditablePaths.Count == 0;
// check with VCS
if (!AssetModificationHook.GetOpenForEdit(false, queryList, outNotEditablePaths, statusQueryOptions))
return false;
// check with possible script callbacks
return GetOpenForEditViaScriptCallbacks(false, queryList.ToArray(), outNotEditablePaths, out var message, statusQueryOptions);
}
static List<string> GetQueryList(string[] paths, List<string> outNotEditablePaths)
{
var result = new List<string>(paths.Length);
foreach (var path in paths)
{
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;
}
result.Add(path);
}
return result;
}
internal static void OnStatusUpdated()
{
WindowPending.OnStatusUpdated();
foreach (var assetModificationProcessorClass in AssetModificationProcessors)
{
const string methodName = "OnStatusUpdated";
MethodInfo method = assetModificationProcessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
object[] args = {};
if (!CheckArgumentsAndReturnType(args, method, typeof(void)))
continue;
using (new EditorPerformanceMarker($"{assetModificationProcessorClass.Name}.{methodName}", assetModificationProcessorClass).Auto())
method.Invoke(null, args);
}
}
}
static MethodInfo[] s_MakeEditableMethods;
static MethodInfo[] GetMakeEditableMethods()
{
if (s_MakeEditableMethods == null)
{
var methods = new List<MethodInfo>();
Type[] types = { typeof(string[]), typeof(string), typeof(List<string>) };
foreach (var type in AssetModificationProcessors)
{
MethodInfo method;
try
{
method = type.GetMethod("MakeEditable", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
}
catch (AmbiguousMatchException)
{
Debug.LogWarning($"Ambiguous MakeEditable methods in {type.Name}.");
continue;
}
if (method != null && CheckArgumentTypesAndReturnType(types, method, typeof(bool)))
methods.Add(method);
}
s_MakeEditableMethods = methods.ToArray();
}
return s_MakeEditableMethods;
}
internal static bool MakeEditable(string[] paths, string prompt, List<string> outNotEditablePaths)
{
var methods = GetMakeEditableMethods();
if (methods == null || methods.Length == 0)
return true;
object[] args = { paths, prompt ?? string.Empty, outNotEditablePaths ?? new List<string>() };
foreach (var method in methods)
{
if (!(bool)method.Invoke(null, args))
return false;
}
return true;
}
}
}