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
418 lines (372 loc) · 16.5 KB
/
Copy pathAssetPostprocessor.cs
File metadata and controls
418 lines (372 loc) · 16.5 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
// 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.Scripting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace UnityEditor
{
internal class AssetPostprocessingInternal
{
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)
{
object[] args = { importedAssets, deletedAssets, movedAssets, movedFromPathAssets };
foreach (var assetPostprocessorClass in EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)))
{
MethodInfo method = assetPostprocessorClass.GetMethod("OnPostprocessAllAssets", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
method.Invoke(null, args);
}
///@TODO: we need addedAssets for SyncVS. Make this into a proper API and write tests
SyncVS.PostprocessSyncProject(importedAssets, addedAssets, deletedAssets, movedAssets, movedFromPathAssets);
}
[RequiredByNativeCode]
static void PreprocessAssembly(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPreprocessAssembly", new[] { pathName });
}
}
//This is undocumented, and a "safeguard" for when visualstudio gets a new release that is incompatible with ours, so that users can postprocess our csproj to fix it.
//(or just completely replace them). Hopefully we'll never need this.
static internal void CallOnGeneratedCSProjectFiles()
{
object[] args = {};
foreach (var method in AllPostProcessorMethodsNamed("OnGeneratedCSProjectFiles"))
{
method.Invoke(null, args);
}
}
//This callback is used by UnityVS to take over project generation from unity
static internal bool OnPreGeneratingCSProjectFiles()
{
object[] args = {};
bool result = false;
foreach (var method in AllPostProcessorMethodsNamed("OnPreGeneratingCSProjectFiles"))
{
object returnValue = method.Invoke(null, args);
if (method.ReturnType == typeof(bool))
result = result | (bool)returnValue;
}
return result;
}
private static IEnumerable<MethodInfo> AllPostProcessorMethodsNamed(string callbackName)
{
return EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)).Select(assetPostprocessorClass => assetPostprocessorClass.GetMethod(callbackName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)).Where(method => method != null);
}
internal class CompareAssetImportPriority : IComparer
{
int IComparer.Compare(System.Object xo, System.Object yo)
{
int x = ((AssetPostprocessor)xo).GetPostprocessOrder();
int y = ((AssetPostprocessor)yo).GetPostprocessOrder();
return x.CompareTo(y);
}
}
internal class PostprocessStack
{
internal ArrayList m_ImportProcessors = null;
}
static ArrayList m_PostprocessStack = null;
static ArrayList m_ImportProcessors = null;
static ArrayList m_PostprocessorClasses = null;
static ArrayList GetCachedAssetPostprocessorClasses()
{
if (m_PostprocessorClasses == null)
{
m_PostprocessorClasses = new ArrayList();
foreach (var type in EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)))
{
m_PostprocessorClasses.Add(type);
}
}
return m_PostprocessorClasses;
}
[RequiredByNativeCode]
static void InitPostprocessors(string pathName)
{
m_ImportProcessors = new ArrayList();
// @TODO: This is just a temporary workaround for the import settings.
// We should add importers to the asset, persist them and show an inspector for them.
foreach (Type assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
try
{
var assetPostprocessor = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
assetPostprocessor.assetPath = pathName;
m_ImportProcessors.Add(assetPostprocessor);
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
m_ImportProcessors.Sort(new CompareAssetImportPriority());
// Setup postprocessing stack to support rentrancy (Import asset immediate)
PostprocessStack postStack = new PostprocessStack();
postStack.m_ImportProcessors = m_ImportProcessors;
if (m_PostprocessStack == null)
m_PostprocessStack = new ArrayList();
m_PostprocessStack.Add(postStack);
}
[RequiredByNativeCode]
static void CleanupPostprocessors()
{
if (m_PostprocessStack != null)
{
m_PostprocessStack.RemoveAt(m_PostprocessStack.Count - 1);
if (m_PostprocessStack.Count != 0)
{
PostprocessStack postStack = (PostprocessStack)m_PostprocessStack[m_PostprocessStack.Count - 1];
m_ImportProcessors = postStack.m_ImportProcessors;
}
}
}
[RequiredByNativeCode]
static uint[] GetMeshProcessorVersions()
{
List<uint> versions = new List<uint>();
foreach (var assetPostprocessorClass in EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)))
{
try
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
var type = inst.GetType();
bool hasPreProcessMethod = type.GetMethod("OnPreprocessModel", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
bool hasProcessMeshAssignMethod = type.GetMethod("OnProcessMeshAssingModel", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
bool hasPostProcessMethod = type.GetMethod("OnPostprocessModel", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
uint version = inst.GetVersion();
if (version != 0 && (hasPreProcessMethod || hasProcessMeshAssignMethod || hasPostProcessMethod))
{
versions.Add(version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return versions.ToArray();
}
[RequiredByNativeCode]
static void PreprocessMesh(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPreprocessModel", null);
}
}
[RequiredByNativeCode]
static void PreprocessSpeedTree(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPreprocessSpeedTree", null);
}
}
[RequiredByNativeCode]
static void PreprocessAnimation(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPreprocessAnimation", null);
}
}
[RequiredByNativeCode]
static void PostprocessAnimation(GameObject root, AnimationClip clip)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { root, clip };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessAnimation", args);
}
}
[RequiredByNativeCode]
static Material ProcessMeshAssignMaterial(Renderer renderer, Material material)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { material, renderer };
object assignedMaterial = AttributeHelper.InvokeMemberIfAvailable(inst, "OnAssignMaterialModel", args);
if (assignedMaterial as Material)
return assignedMaterial as Material;
}
return null;
}
[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;
}
static void PostprocessMesh(GameObject gameObject)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { gameObject };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessModel", args);
}
}
static void PostprocessSpeedTree(GameObject gameObject)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { gameObject };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessSpeedTree", args);
}
}
[RequiredByNativeCode]
static void PostprocessGameObjectWithUserProperties(GameObject go, string[] prop_names, object[] prop_values)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { go, prop_names, prop_values };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessGameObjectWithUserProperties", args);
}
}
[RequiredByNativeCode]
static EditorCurveBinding[] PostprocessGameObjectWithAnimatedUserProperties(GameObject go, EditorCurveBinding[] bindings)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { go, bindings };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessGameObjectWithAnimatedUserProperties", args);
}
return bindings;
}
[RequiredByNativeCode]
static uint[] GetTextureProcessorVersions()
{
List<uint> versions = new List<uint>();
foreach (var assetPostprocessorClass in EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)))
{
try
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
var type = inst.GetType();
bool hasPreProcessMethod = type.GetMethod("OnPreprocessTexture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
bool hasPostProcessMethod = type.GetMethod("OnPostprocessTexture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
uint version = inst.GetVersion();
if (version != 0 && (hasPreProcessMethod || hasPostProcessMethod))
{
versions.Add(version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return versions.ToArray();
}
[RequiredByNativeCode]
static void PreprocessTexture(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPreprocessTexture", null);
}
}
[RequiredByNativeCode]
static void PostprocessTexture(Texture2D tex, string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessTexture", args);
}
}
[RequiredByNativeCode]
static void PostprocessSprites(Texture2D tex, string pathName, Sprite[] sprites)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex, sprites };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessSprites", args);
}
}
[RequiredByNativeCode]
static uint[] GetAudioProcessorVersions()
{
List<uint> versions = new List<uint>();
foreach (var assetPostprocessorClass in EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)))
{
try
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
var type = inst.GetType();
bool hasPreProcessMethod = type.GetMethod("OnPreprocessAudio", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
bool hasPostProcessMethod = type.GetMethod("OnPostprocessAudio", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
uint version = inst.GetVersion();
if (version != 0 && (hasPreProcessMethod || hasPostProcessMethod))
{
versions.Add(version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
return versions.ToArray();
}
[RequiredByNativeCode]
static void PreprocessAudio(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPreprocessAudio", null);
}
}
[RequiredByNativeCode]
static void PostprocessAudio(AudioClip tex, string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex };
AttributeHelper.InvokeMemberIfAvailable(inst, "OnPostprocessAudio", args);
}
}
[RequiredByNativeCode]
static void PostprocessAssetbundleNameChanged(string assetPAth, string prevoiusAssetBundleName, string newAssetBundleName)
{
object[] args = { assetPAth, prevoiusAssetBundleName, newAssetBundleName };
foreach (var assetPostprocessorClass in EditorAssemblies.SubclassesOf(typeof(AssetPostprocessor)))
{
var assetPostprocessor = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
AttributeHelper.InvokeMemberIfAvailable(assetPostprocessor, "OnPostprocessAssetbundleNameChanged", args);
}
}
}
}