forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetPostprocessor.cs
More file actions
1134 lines (984 loc) · 47.7 KB
/
Copy pathAssetPostprocessor.cs
File metadata and controls
1134 lines (984 loc) · 47.7 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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEngine.Internal;
using UnityEngine.Scripting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor.AssetImporters;
using Object = UnityEngine.Object;
using UnityEditor.Profiling;
using UnityEditor.Callbacks;
namespace UnityEditor
{
// AssetPostprocessor lets you hook into the import pipeline and run scripts prior or after importing assets.
public partial class AssetPostprocessor
{
internal struct PostprocessorInfo
{
public Type Type { get; }
public string[] Methods { get; }
public uint Version { get; }
public int Priority { get; }
/// <summary>
/// StaticDependency is true if any method in the postprocessor is not part of the NonAutomaticDependencyMethods list.
/// This is used to know which PostprocessorInfo should be used for the static importer dependency hash.
/// </summary>
public bool StaticDependency { get; }
public PostprocessorInfo(Type assetPostprocessorType, int importerPriority)
{
Type = assetPostprocessorType;
Methods = null;
Version = 0;
StaticDependency = false;
Priority = importerPriority;
}
public PostprocessorInfo(Type assetPostprocessorType, string[] implementedMethods)
{
Type = assetPostprocessorType;
Methods = implementedMethods;
StaticDependency = Methods.Intersect(AssetPostprocessingInternal.k_NonAutomaticDependencyMethods).Count() != Methods.Length;
var inst = (AssetPostprocessor)Activator.CreateInstance(assetPostprocessorType);
Version = inst.GetVersion();
Priority = inst.GetPostprocessOrder();
}
}
private string m_PathName;
private AssetImportContext m_Context;
// The path name of the asset being imported.
public string assetPath { get { return m_PathName; } set { m_PathName = value; } }
// The context of the import, used to specify dependencies
public AssetImportContext context { get { return m_Context; } internal set { m_Context = value; } }
// Logs an import warning to the console.
[ExcludeFromDocs]
[Obsolete("Use context.LogImportWarning(string) instead.")]
public void LogWarning(string warning)
{
Object context = null;
LogWarning(warning, context);
}
[Obsolete("Use context.LogImportWarning(string, Object) instead.")]
public void LogWarning(string warning, [DefaultValue("null")] Object context)
{
if (m_Context != null)
m_Context.LogImportWarning(warning, context);
else
Debug.LogWarning(warning, context);
}
// Logs an import error message to the console.
[ExcludeFromDocs]
[Obsolete("Use context.LogImportError(string) instead.")]
public void LogError(string warning)
{
Object context = null;
LogError(warning, context);
}
[Obsolete("Use context.LogImportError(string, Object) instead.")]
public void LogError(string warning, [DefaultValue("null")] Object context)
{
if (m_Context != null)
m_Context.LogImportError(warning, context);
else
Debug.LogError(warning, context);
}
// Returns the version of the asset postprocessor.
public virtual uint GetVersion() { return 0; }
// Reference to the asset importer
public AssetImporter assetImporter { get { return AssetImporter.GetAtPath(assetPath); } }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[Obsolete("To set or get the preview, call EditorUtility.SetAssetPreview or AssetPreview.GetAssetPreview instead", true)]
public Texture2D preview { get { return null; } set {} }
// Override the order in which importers are processed.
public virtual int GetPostprocessOrder() { return 0; }
}
class OnPostprocessAllAssetsCallbackCollection : OrderedCallbackCollection
{
public class MethodInfoCallback : Callback
{
public MethodInfo Method { get; }
public override Type classType => Method.DeclaringType;
public bool MethodDomainReload { get; }
public override string name => classType.FullName;
public MethodInfoCallback(MethodInfo method, bool methodDomainReload)
{
Method = method;
MethodDomainReload = methodDomainReload;
}
public override IEnumerable<T> GetCustomAttributes<T>() => Method.GetCustomAttributes<T>();
}
public override string name => "OnPostprocessAllAssets";
public override List<Callback> GetCallbacks()
{
var methodArgTypes = new Type[] { typeof(string).MakeArrayType(), typeof(string).MakeArrayType(), typeof(string).MakeArrayType(), typeof(string).MakeArrayType() };
var methodDomainReloadParamArgTypes = new Type[] { methodArgTypes[0], methodArgTypes[1], methodArgTypes[2], methodArgTypes[3], typeof(bool) };
const BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static;
var callbacks = new List<Callback>();
foreach (var assetPostprocessorClass in TypeCache.GetTypesDerivedFrom<AssetPostprocessor>())
{
var method = assetPostprocessorClass.GetMethod("OnPostprocessAllAssets", flags, null, methodArgTypes, null);
if (method != null)
{
callbacks.Add(new MethodInfoCallback(method, false));
}
else
{
// OnPostprocessAllAssets with didDomainReload parameter
method = assetPostprocessorClass.GetMethod("OnPostprocessAllAssets", flags, null, methodDomainReloadParamArgTypes, null);
if (method != null)
{
callbacks.Add(new MethodInfoCallback(method, true));
}
}
}
return callbacks;
}
}
internal class AssetPostprocessingInternal
{
// What is it:
// Static postprocessor methods always called for each importer that are part of importer static dependency.
// No new postprocessors should be added to these lists. Please reach out to #devs-import-workflow to talk about new additions.
internal static readonly string[] k_NonAutomaticDependencyMethods =
{
"OnPreprocessAsset",
};
static readonly string[] k_ModelImporterPostprocessors =
{
"OnPreprocessModel",
"OnPostprocessMeshHierarchy",
"OnPostprocessModel",
"OnPreprocessAnimation",
"OnPostprocessAnimation",
"OnPostprocessGameObjectWithAnimatedUserProperties",
"OnPostprocessGameObjectWithUserProperties",
"OnPostprocessMaterial",
"OnAssignMaterialModel",
"OnPreprocessMaterialDescription",
};
static readonly string[] k_DynamicModelImporterPostprocessors =
{
"OnPreprocessCameraDescription",
"OnPreprocessLightDescription"
};
static readonly string[] k_TextureImporterPostprocessors =
{
"OnPreprocessTexture",
"OnPostprocessTexture",
"OnPostprocessCubemap",
"OnPostprocessSprites",
"OnPostprocessTexture3D",
"OnPostprocessTexture2DArray"
};
static readonly string[] k_IHVImporterPostprocessors =
{
"OnPostprocessTexture",
};
static readonly string[] k_AudioImporterPostprocessors =
{
"OnPreprocessAudio",
"OnPostprocessAudio",
};
static readonly string[] k_SpeedTreeImporterPostprocessors =
{
"OnPreprocessSpeedTree",
"OnPostprocessSpeedTree",
};
static readonly string[] k_PrefabImporterPostprocessors =
{
"OnPostprocessPrefab",
};
static readonly string[] k_CameraPostprocessors =
{
"OnPreprocessCameraDescription",
};
static readonly string[] k_LightPostprocessors =
{
"OnPreprocessLightDescription",
};
static readonly string[] k_TexturePreprocessors =
{
"OnPreprocessTexture",
};
static readonly string[] k_Texture2DPostprocessors =
{
"OnPostprocessTexture",
};
static readonly string[] k_Texture2DArrayPostprocessors =
{
"OnPostprocessTexture2DArray"
};
static readonly string[] k_Texture3DPostprocessors =
{
"OnPostprocessTexture3D"
};
static readonly string[] k_TextureCubePostprocessors =
{
"OnPostprocessCubemap"
};
static readonly string[] k_SpritePostprocessors =
{
"OnPostprocessSprites",
};
static Dictionary<string, string[]> s_PostprocessorMethodsByDependencyKey;
static Dictionary<Type, string[]> s_StaticPostprocessorMethodsByImporterType;
static Dictionary<Type, string[]> s_DynamicPostprocessorMethodsByImporterType;
// Internal for debugging purposes. We can generate dependency graphs to help understand issues.
internal static OnPostprocessAllAssetsCallbackCollection s_OnPostprocessAllAssetsCallbacks = new OnPostprocessAllAssetsCallbackCollection();
static AssetPostprocessingInternal()
{
s_StaticPostprocessorMethodsByImporterType = new Dictionary<Type, string[]>();
s_StaticPostprocessorMethodsByImporterType.Add(typeof(ModelImporter), k_ModelImporterPostprocessors);
s_StaticPostprocessorMethodsByImporterType.Add(typeof(IHVImageFormatImporter), k_IHVImporterPostprocessors);
s_StaticPostprocessorMethodsByImporterType.Add(typeof(SpeedTreeImporter), k_SpeedTreeImporterPostprocessors);
s_StaticPostprocessorMethodsByImporterType.Add(typeof(AudioImporter), k_AudioImporterPostprocessors);
s_StaticPostprocessorMethodsByImporterType.Add(typeof(PrefabImporter), k_PrefabImporterPostprocessors);
s_DynamicPostprocessorMethodsByImporterType = new Dictionary<Type, string[]>();
s_DynamicPostprocessorMethodsByImporterType.Add(typeof(ModelImporter), k_DynamicModelImporterPostprocessors);
s_DynamicPostprocessorMethodsByImporterType.Add(typeof(TextureImporter), k_TextureImporterPostprocessors);
s_PostprocessorMethodsByDependencyKey = new Dictionary<string, string[]>();
s_PostprocessorMethodsByDependencyKey.Add(kCameraPostprocessorDependencyName, k_CameraPostprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kLightPostprocessorDependencyName, k_LightPostprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kTexturePreprocessorDependencyName, k_TexturePreprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kTexture2DPostprocessorDependencyName, k_Texture2DPostprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kTexture2DArrayPostprocessorDependencyName, k_Texture2DArrayPostprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kTexture3DPostprocessorDependencyName, k_Texture3DPostprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kTextureCubePostprocessorDependencyName, k_TextureCubePostprocessors);
s_PostprocessorMethodsByDependencyKey.Add(kTextureSpritePostprocessorDependencyName, k_SpritePostprocessors);
}
[Serializable]
class AssetPostProcessorAnalyticsData
{
public string importActionId;
public List<AssetPostProcessorMethodCallAnalyticsData> postProcessorCalls = new List<AssetPostProcessorMethodCallAnalyticsData>();
}
[Serializable]
struct AssetPostProcessorMethodCallAnalyticsData
{
public string methodName;
public float duration_sec;
public int invocationCount;
}
static void LogPostProcessorMissingDefaultConstructor(Type type)
{
Debug.LogErrorFormat("{0} requires a default constructor to be used as an asset post processor", type);
}
[RequiredByNativeCode]
// Postprocess on all assets once an automatic import has completed
static void PostprocessAllAssets(string[] importedAssets, string[] addedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPathAssets, bool didDomainReload)
{
object[] args = { importedAssets, deletedAssets, movedAssets, movedFromPathAssets };
object[] argsWithDidDomainReload = { importedAssets, deletedAssets, movedAssets, movedFromPathAssets, didDomainReload};
var containsNoAssets = importedAssets.Length == 0 && addedAssets.Length == 0 && deletedAssets.Length == 0 && movedAssets.Length == 0 && movedFromPathAssets.Length == 0;
foreach (OnPostprocessAllAssetsCallbackCollection.MethodInfoCallback assetPostProcessor in s_OnPostprocessAllAssetsCallbacks.sortedCallbacks)
{
try
{
if (assetPostProcessor.MethodDomainReload)
{
using (new EditorPerformanceMarker($"{assetPostProcessor.classType.Name}.OnPostprocessAllAssets", assetPostProcessor.classType).Auto())
InvokeMethod(assetPostProcessor.Method, argsWithDidDomainReload);
}
else
{
if (containsNoAssets)
continue;
using (new EditorPerformanceMarker($"{assetPostProcessor.classType.Name}.OnPostprocessAllAssets", assetPostProcessor.classType).Auto())
InvokeMethod(assetPostProcessor.Method, args);
}
}
catch (Exception e)
{
Debug.LogException(e);
}
}
using (new EditorPerformanceMarker("SyncVS.PostprocessSyncProject").Auto())
CodeEditorProjectSync.PostprocessSyncProject(importedAssets, addedAssets, deletedAssets, movedAssets, movedFromPathAssets);
}
internal class CompareAssetImportPriority : IComparer<AssetPostprocessor.PostprocessorInfo>, IComparer<AssetPostprocessor>
{
public int Compare(AssetPostprocessor.PostprocessorInfo x, AssetPostprocessor.PostprocessorInfo y)
{
int xo = x.Priority;
int yo = y.Priority;
var compare = xo.CompareTo(yo);
if (compare == 0)
{
compare = x.Type.FullName.CompareTo(y.Type.FullName);
if (compare == 0)
compare = x.Type.AssemblyQualifiedName.CompareTo(y.Type.AssemblyQualifiedName);
}
return compare;
}
public int Compare(AssetPostprocessor x, AssetPostprocessor y)
{
var xi = new AssetPostprocessor.PostprocessorInfo(x.GetType(), x.GetPostprocessOrder());
var yi = new AssetPostprocessor.PostprocessorInfo(y.GetType(), y.GetPostprocessOrder());
return Compare(xi, yi);
}
}
private static string BuildStaticDependencyHashString(SortedSet<AssetPostprocessor.PostprocessorInfo> list)
{
var hashStr = "";
foreach (var info in list)
{
if (info.StaticDependency)
{
hashStr += info.Type.AssemblyQualifiedName;
hashStr += '.';
hashStr += info.Version;
hashStr += '|';
}
}
return hashStr;
}
private static string BuildHashString(SortedList<string, uint> list)
{
var hashStr = "";
foreach (var pair in list)
{
hashStr += pair.Key;
hashStr += '.';
hashStr += pair.Value;
hashStr += '|';
}
return hashStr;
}
internal const string kCameraPostprocessorDependencyName = "postprocessor/camera";
internal const string kLightPostprocessorDependencyName = "postprocessor/light";
internal const string kTexture2DPostprocessorDependencyName = "postprocessor/texture2D";
internal const string kTextureCubePostprocessorDependencyName = "postprocessor/textureCube";
internal const string kTexture3DPostprocessorDependencyName = "postprocessor/texture3D";
internal const string kTexture2DArrayPostprocessorDependencyName = "postprocessor/texture2DArray";
internal const string kTextureSpritePostprocessorDependencyName = "postprocessor/textureSprite";
internal const string kTexturePreprocessorDependencyName = "postprocessor/texturePreprocessor";
static Stack<SortedSet<AssetPostprocessor>> m_PostprocessStack = null;
static SortedSet<AssetPostprocessor> m_ImportProcessors = null;
static Type[] m_PostprocessorClasses = null;
static string m_MeshProcessorsHashString = null;
static string m_AudioProcessorsHashString = null;
static string m_SpeedTreeProcessorsHashString = null;
static string m_PrefabProcessorsHashString = null;
static string m_CameraProcessorsHashString = null;
static string m_LightProcessorsHashString = null;
static string m_Texture2DProcessorsHashString = null;
static string m_TextureCubeProcessorsHashString = null;
static string m_Texture3DPostprocessorDependencyName = null;
static string m_Texture2DArrayDependencyName = null;
static string m_TextureSpriteDependencyName = null;
static string m_TexturePreprocessorDependencyName = null;
static Dictionary<Type, SortedSet<AssetPostprocessor.PostprocessorInfo>> s_StaticPostprocessorsPerImporterType = new Dictionary<Type, SortedSet<AssetPostprocessor.PostprocessorInfo>>();
static Dictionary<Type, SortedSet<AssetPostprocessor.PostprocessorInfo>> s_DynamicPostprocessorsPerImporterType = new Dictionary<Type, SortedSet<AssetPostprocessor.PostprocessorInfo>>();
static Stack<AssetPostProcessorAnalyticsData> s_AnalyticsEventsStack = new Stack<AssetPostProcessorAnalyticsData>();
static Type[] GetCachedAssetPostprocessorClasses()
{
if (m_PostprocessorClasses == null)
m_PostprocessorClasses = TypeCache.GetTypesDerivedFrom<AssetPostprocessor>().ToArray();
return m_PostprocessorClasses;
}
[RequiredByNativeCode]
static void InitPostprocessorsForTextureGenerator(string pathName)
{
var analyticsEvent = new AssetPostProcessorAnalyticsData();
analyticsEvent.importActionId = "None";
s_AnalyticsEventsStack.Push(analyticsEvent);
m_ImportProcessors = new SortedSet<AssetPostprocessor>(new CompareAssetImportPriority());
foreach (var postprocessorInfo in GetSortedStaticPostprocessorTypes(typeof(TextureImporter)))
{
var assetPostprocessor = (AssetPostprocessor)Activator.CreateInstance(postprocessorInfo.Type);
assetPostprocessor.assetPath = pathName;
assetPostprocessor.context = null;
m_ImportProcessors.Add(assetPostprocessor);
}
foreach (var postprocessorInfo in GetSortedDynamicPostprocessorTypes(typeof(TextureImporter)))
{
var assetPostprocessor = (AssetPostprocessor)Activator.CreateInstance(postprocessorInfo.Type);
assetPostprocessor.assetPath = pathName;
assetPostprocessor.context = null;
m_ImportProcessors.Add(assetPostprocessor);
}
// Setup postprocessing stack to support reentrancy (Import asset immediate)
if (m_PostprocessStack == null)
m_PostprocessStack = new Stack<SortedSet<AssetPostprocessor>>();
m_PostprocessStack.Push(m_ImportProcessors);
}
[RequiredByNativeCode]
static void InitPostprocessors(AssetImportContext context, string pathName, Type importerType, double importStartTime)
{
var analyticsEvent = new AssetPostProcessorAnalyticsData();
analyticsEvent.importActionId = ((int)Math.Floor(importStartTime * 1000)).ToString();
s_AnalyticsEventsStack.Push(analyticsEvent);
m_ImportProcessors = new SortedSet<AssetPostprocessor>(new CompareAssetImportPriority());
foreach (var postprocessorInfo in GetSortedStaticPostprocessorTypes(importerType))
{
var assetPostprocessor = (AssetPostprocessor)Activator.CreateInstance(postprocessorInfo.Type);
assetPostprocessor.assetPath = pathName;
assetPostprocessor.context = context;
m_ImportProcessors.Add(assetPostprocessor);
}
foreach (var postprocessorInfo in GetSortedDynamicPostprocessorTypes(importerType))
{
var assetPostprocessor = (AssetPostprocessor)Activator.CreateInstance(postprocessorInfo.Type);
assetPostprocessor.assetPath = pathName;
assetPostprocessor.context = context;
m_ImportProcessors.Add(assetPostprocessor);
}
// Setup postprocessing stack to support reentrancy (Import asset immediate)
if (m_PostprocessStack == null)
m_PostprocessStack = new Stack<SortedSet<AssetPostprocessor>>();
m_PostprocessStack.Push(m_ImportProcessors);
}
[RequiredByNativeCode]
static void CleanupPostprocessors()
{
if (m_PostprocessStack != null)
{
m_PostprocessStack.Pop();
m_ImportProcessors = m_PostprocessStack.Count > 0 ? m_PostprocessStack.Peek() : null;
}
if (s_AnalyticsEventsStack.Count > 0)
{
var lastEvent = s_AnalyticsEventsStack.Pop();
if (lastEvent.postProcessorCalls.Count > 0)
EditorAnalytics.SendAssetPostprocessorsUsage(lastEvent);
}
}
static bool ImplementsAnyOfTheses(Type type, IEnumerable<string> methods, out List<string> usedMethods)
{
usedMethods = new List<string>(methods.Where(method => type.GetMethod(method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null));
return usedMethods.Count > 0;
}
/*
* Returns the list of actual dynamic postprocessor methods for a particular asset.
* Note: That where the asset is not yet imported, this list will be empty.
*/
internal static SortedSet<AssetPostprocessor.PostprocessorInfo> GetSortedDynamicPostprocessorsForAsset(string path)
{
var list = new SortedSet<AssetPostprocessor.PostprocessorInfo>(new CompareAssetImportPriority());
var guid = AssetDatabase.GUIDFromAssetPath(path);
if (guid.Empty())
return list;
//Artifact Infos may contains multiple artifacts, associated with different version of the object (E.g. Main, Preview etc.)
var artifactInfos = AssetDatabase.GetArtifactInfos(guid);
var allMethodsNames = new List<string>();
foreach (var info in artifactInfos)
{
if (!info.isCurrentArtifact)
continue;
foreach (var kvp in info.dependencies)
{
if (kvp.Value.type == ArtifactInfoDependencyType.Dynamic)
{
//Try to retrieve Postprocessor Methods associated with the supplied Dependency keys
string dependencyName = kvp.Key.Replace(ArtifactDifferenceReporter.kEnvironment_CustomDependency + "/", "");
if (s_PostprocessorMethodsByDependencyKey.TryGetValue(dependencyName, out var methodNames))
allMethodsNames.AddRange(methodNames);
}
}
}
if (allMethodsNames.Count == 0)
return list;
/*
* The asset has dynamic dependencies to an Asset Postprocessor, so let's find any Postprocessors which
* implements those methods.
*/
var distinctMethodNames = allMethodsNames.Distinct();
foreach (Type assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
if (ImplementsAnyOfTheses(assetPostprocessorClass, distinctMethodNames, out var methods))
{
if (assetPostprocessorClass.GetConstructors().Any(t => t.GetParameters().Count() == 0))
list.Add(new AssetPostprocessor.PostprocessorInfo(assetPostprocessorClass, methods.ToArray()));
else
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
}
return list;
}
internal static SortedSet<AssetPostprocessor.PostprocessorInfo> GetSortedStaticPostprocessorTypes(Type importer)
{
var defaultMethods = new string[]
{
"OnPreprocessAsset"
};
return GetSortedPostprocessorTypes(importer, s_StaticPostprocessorMethodsByImporterType, defaultMethods,
s_StaticPostprocessorsPerImporterType);
}
/*
* Returns the list of *possible* dynamic postprocessor methods associated with a particular importer type.
* See also: GetSortedDynamicPostprocessorsForAsset, to get the actual postprocessor methods for a given asset.
*/
internal static SortedSet<AssetPostprocessor.PostprocessorInfo> GetSortedDynamicPostprocessorTypes(Type importer)
{
return GetSortedPostprocessorTypes(importer, s_DynamicPostprocessorMethodsByImporterType, new string[0],
s_DynamicPostprocessorsPerImporterType);
}
static SortedSet<AssetPostprocessor.PostprocessorInfo> GetSortedPostprocessorTypes(Type importer, Dictionary<Type, string[]> postprocessorMethodsByImporterType, string[] defaultMethods, Dictionary<Type, SortedSet<AssetPostprocessor.PostprocessorInfo>> cache)
{
if (cache.TryGetValue(importer, out var cachedPostprocessors))
return cachedPostprocessors;
var list = new SortedSet<AssetPostprocessor.PostprocessorInfo>(new CompareAssetImportPriority());
var allMethodsNames = defaultMethods.ToList();
var methodsType = importer;
while (methodsType != null && methodsType != typeof(AssetImporter))
{
if (postprocessorMethodsByImporterType.TryGetValue(methodsType, out var methodNames))
allMethodsNames.AddRange(methodNames);
methodsType = methodsType.BaseType;
}
foreach (Type assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
if (ImplementsAnyOfTheses(assetPostprocessorClass, allMethodsNames, out var methods))
{
if (assetPostprocessorClass.GetConstructors().Any(t => t.GetParameters().Count() == 0))
list.Add(new AssetPostprocessor.PostprocessorInfo(assetPostprocessorClass, methods.ToArray()));
else
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
}
cache.Add(importer, list);
return list;
}
[RequiredByNativeCode]
static string GetMeshProcessorsHashString()
{
if (m_MeshProcessorsHashString != null)
return m_MeshProcessorsHashString;
m_MeshProcessorsHashString = BuildStaticDependencyHashString(GetSortedStaticPostprocessorTypes(typeof(ModelImporter)));
return m_MeshProcessorsHashString;
}
[RequiredByNativeCode]
static void PreprocessAsset()
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessAsset", null);
}
}
[RequiredByNativeCode]
static void PreprocessModel(string pathName)
{
CallPostProcessMethods("OnPreprocessModel", null);
}
[RequiredByNativeCode]
static void PreprocessSpeedTree(string pathName)
{
CallPostProcessMethods("OnPreprocessSpeedTree", null);
}
[RequiredByNativeCode]
static void PreprocessAnimation(string pathName)
{
CallPostProcessMethods("OnPreprocessAnimation", null);
}
[RequiredByNativeCode]
static void PostprocessAnimation(GameObject root, AnimationClip clip)
{
object[] args = { root, clip };
CallPostProcessMethods("OnPostprocessAnimation", args);
}
[RequiredByNativeCode]
static Material ProcessMeshAssignMaterial(Renderer renderer, Material material)
{
object[] args = { material, renderer };
Material assignedMaterial;
CallPostProcessMethodsUntilReturnedObjectIsValid("OnAssignMaterialModel", args, out assignedMaterial);
return assignedMaterial;
}
[RequiredByNativeCode]
static bool ProcessMeshHasAssignMaterial()
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
if (inst.GetType().GetMethod("OnAssignMaterialModel", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null)
return true;
}
return false;
}
[RequiredByNativeCode]
static void PostprocessMeshHierarchy(GameObject root)
{
object[] args = { root };
CallPostProcessMethods("OnPostprocessMeshHierarchy", args);
}
[RequiredByNativeCode]
static void PostprocessMesh(GameObject gameObject)
{
object[] args = { gameObject };
CallPostProcessMethods("OnPostprocessModel", args);
}
[RequiredByNativeCode]
static void PostprocessSpeedTree(GameObject gameObject)
{
object[] args = { gameObject };
CallPostProcessMethods("OnPostprocessSpeedTree", args);
}
[RequiredByNativeCode]
static void PostprocessMaterial(Material material)
{
object[] args = { material };
CallPostProcessMethods("OnPostprocessMaterial", args);
}
[RequiredByNativeCode]
static void PreprocessCameraDescription(AssetImportContext assetImportContext, CameraDescription description, Camera camera, AnimationClip[] animations)
{
assetImportContext.DependsOnCustomDependency(kCameraPostprocessorDependencyName);
object[] args = { description, camera, animations };
CallPostProcessMethods("OnPreprocessCameraDescription", args);
}
[RequiredByNativeCode]
static void PreprocessLightDescription(AssetImportContext assetImportContext, LightDescription description, Light light, AnimationClip[] animations)
{
assetImportContext.DependsOnCustomDependency(kLightPostprocessorDependencyName);
object[] args = { description, light, animations };
CallPostProcessMethods("OnPreprocessLightDescription", args);
}
[RequiredByNativeCode]
static void PreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] animations)
{
object[] args = { description, material, animations };
CallPostProcessMethods("OnPreprocessMaterialDescription", args);
}
[RequiredByNativeCode]
static void PostprocessGameObjectWithUserProperties(GameObject go, string[] prop_names, object[] prop_values)
{
object[] args = { go, prop_names, prop_values };
CallPostProcessMethods("OnPostprocessGameObjectWithUserProperties", args);
}
[RequiredByNativeCode]
static EditorCurveBinding[] PostprocessGameObjectWithAnimatedUserProperties(GameObject go, EditorCurveBinding[] bindings)
{
object[] args = { go, bindings };
CallPostProcessMethods("OnPostprocessGameObjectWithAnimatedUserProperties", args);
return bindings;
}
[RequiredByNativeCode]
static void PreprocessTexture(string pathName, AssetImportContext context)
{
if (context != null)
{
context.DependsOnCustomDependency(kTexturePreprocessorDependencyName);
}
CallPostProcessMethods("OnPreprocessTexture", null);
}
[RequiredByNativeCode]
static void PreprocessTextureFromScript(string pathName)
{
CallPostProcessMethods("OnPreprocessTexture", null);
}
[RequiredByNativeCode]
static void PostprocessTexture(Texture2D tex, string pathName, AssetImportContext context)
{
if (context != null)
{
context.DependsOnCustomDependency(kTexture2DPostprocessorDependencyName);
}
object[] args = { tex };
CallPostProcessMethods("OnPostprocessTexture", args);
}
[RequiredByNativeCode]
static void PostprocessTextureFromScript(Texture2D tex, string pathName)
{
object[] args = { tex };
CallPostProcessMethods("OnPostprocessTexture", args);
}
[RequiredByNativeCode]
static void PostprocessCubemap(Cubemap tex, string pathName, AssetImportContext context)
{
if (context != null)
{
context.DependsOnCustomDependency(kTextureCubePostprocessorDependencyName);
}
object[] args = { tex };
CallPostProcessMethods("OnPostprocessCubemap", args);
}
[RequiredByNativeCode]
static void PostprocessCubemapFromScript(Cubemap tex, string pathName)
{
object[] args = { tex };
CallPostProcessMethods("OnPostprocessCubemap", args);
}
[RequiredByNativeCode]
static void PostprocessTexture3D(Texture3D tex, string pathName, AssetImportContext context)
{
if (context != null)
{
context.DependsOnCustomDependency(kTexture3DPostprocessorDependencyName);
}
object[] args = { tex };
CallPostProcessMethods("OnPostprocessTexture3D", args);
}
[RequiredByNativeCode]
static void PostprocessTexture3DFromScript(Texture3D tex, string pathName)
{
object[] args = { tex };
CallPostProcessMethods("OnPostprocessTexture3D", args);
}
[RequiredByNativeCode]
static void PostprocessTexture2DArray(Texture2DArray tex, string pathName, AssetImportContext context)
{
if (context != null)
{
context.DependsOnCustomDependency(kTexture2DArrayPostprocessorDependencyName);
}
object[] args = { tex };
CallPostProcessMethods("OnPostprocessTexture2DArray", args);
}
[RequiredByNativeCode]
static void PostprocessTexture2DArrayFromScript(Texture2DArray tex, string pathName)
{
object[] args = { tex };
CallPostProcessMethods("OnPostprocessTexture2DArray", args);
}
[RequiredByNativeCode]
static void PostprocessSprites(Texture2D tex, string pathName, Sprite[] sprites, AssetImportContext context)
{
if (context != null)
{
context.DependsOnCustomDependency(kTextureSpritePostprocessorDependencyName);
}
object[] args = { tex, sprites };
CallPostProcessMethods("OnPostprocessSprites", args);
}
[RequiredByNativeCode]
static void PostprocessSpritesFromScript(Texture2D tex, string pathName, Sprite[] sprites)
{
object[] args = { tex, sprites };
CallPostProcessMethods("OnPostprocessSprites", args);
}
[RequiredByNativeCode]
static string GetAudioProcessorsHashString()
{
if (m_AudioProcessorsHashString != null)
return m_AudioProcessorsHashString;
m_AudioProcessorsHashString = BuildStaticDependencyHashString(GetSortedStaticPostprocessorTypes(typeof(AudioImporter)));
return m_AudioProcessorsHashString;
}
[RequiredByNativeCode]
static void PreprocessAudio(string pathName)
{
CallPostProcessMethods("OnPreprocessAudio", null);
}
[RequiredByNativeCode]
static void PostprocessAudio(AudioClip clip, string pathName)
{
object[] args = { clip };
CallPostProcessMethods("OnPostprocessAudio", args);
}
[RequiredByNativeCode]
static string GetPrefabProcessorsHashString()
{
if (m_PrefabProcessorsHashString != null)
return m_PrefabProcessorsHashString;
m_PrefabProcessorsHashString = BuildStaticDependencyHashString(GetSortedStaticPostprocessorTypes(typeof(PrefabImporter)));
return m_PrefabProcessorsHashString;
}
[RequiredByNativeCode]
static void PostprocessPrefab(GameObject prefabAssetRoot)
{
object[] args = { prefabAssetRoot };
CallPostProcessMethods("OnPostprocessPrefab", args);
}
[RequiredByNativeCode]
static void PostprocessAssetbundleNameChanged(string assetPath, string previousAssetBundleName, string newAssetBundleName)
{
object[] args = { assetPath, previousAssetBundleName, newAssetBundleName };
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
var assetPostprocessor = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
InvokeMethodIfAvailable(assetPostprocessor, "OnPostprocessAssetbundleNameChanged", args);
}
}
[RequiredByNativeCode]
static string GetSpeedTreeProcessorsHashString()
{
if (m_SpeedTreeProcessorsHashString != null)
return m_SpeedTreeProcessorsHashString;
m_SpeedTreeProcessorsHashString = BuildStaticDependencyHashString(GetSortedStaticPostprocessorTypes(typeof(SpeedTreeImporter)));
return m_SpeedTreeProcessorsHashString;
}
[InitializeOnLoadMethod]
static void RefreshCustomDependencies()
{
AssetDatabase.RegisterCustomDependency(kCameraPostprocessorDependencyName, Hash128.Compute(GetCameraProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kLightPostprocessorDependencyName, Hash128.Compute(GetLightProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kTexture2DPostprocessorDependencyName, Hash128.Compute(GetTexture2DProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kTextureCubePostprocessorDependencyName, Hash128.Compute(GetTextureCubeProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kTexture3DPostprocessorDependencyName, Hash128.Compute(GetTexture3DProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kTexture2DArrayPostprocessorDependencyName, Hash128.Compute(GetTexture2DArrayProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kTextureSpritePostprocessorDependencyName, Hash128.Compute(GetTextureSpriteProcessorsHashString()));
AssetDatabase.RegisterCustomDependency(kTexturePreprocessorDependencyName, Hash128.Compute(GetTexturePreProcessorsHashString()));
}
static void GetProcessorHashString(string methodName, ref string hashString)
{
if (hashString != null)
return;
var versionsByType = new SortedList<string, uint>();
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
try
{
if (assetPostprocessorClass.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null)
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
uint version = inst.GetVersion();
versionsByType.Add(assetPostprocessorClass.FullName, version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
hashString = BuildHashString(versionsByType);
}
[RequiredByNativeCode]
static string GetCameraProcessorsHashString()
{
GetProcessorHashString("OnPreprocessCameraDescription", ref m_CameraProcessorsHashString);
return m_CameraProcessorsHashString;
}
[RequiredByNativeCode]
static string GetLightProcessorsHashString()
{
GetProcessorHashString("OnPreprocessLightDescription", ref m_LightProcessorsHashString);
return m_LightProcessorsHashString;
}
[RequiredByNativeCode]
static string GetTexturePreProcessorsHashString()
{
GetProcessorHashString("OnPreprocessTexture", ref m_TexturePreprocessorDependencyName);
return m_TexturePreprocessorDependencyName;
}
[RequiredByNativeCode]
static string GetTexture2DProcessorsHashString()
{
GetProcessorHashString("OnPostprocessTexture", ref m_Texture2DProcessorsHashString);
return m_Texture2DProcessorsHashString;
}
[RequiredByNativeCode]
static string GetTextureCubeProcessorsHashString()
{