summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/VRCSDK/Dependencies/VRChat/Scripts/Validation/ShaderValidation.cs
blob: 0a69a1af3f48e7cdf6d604c86b45f451b72dec16 (plain)
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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace VRC.SDKBase.Validation
{
    public static class ShaderValidation
    {
        public static IEnumerable<Shader> FindIllegalShaders(GameObject target, string[] whitelist)
        {
            List<Shader> illegalShaders = new List<Shader>();
            IEnumerator seeker = FindIllegalShadersEnumerator(target, whitelist, (c) => illegalShaders.Add(c));
            while(seeker.MoveNext())
            {
            }

            return illegalShaders;
        }

        private static IEnumerator FindIllegalShadersEnumerator(GameObject target, string[] whitelist, System.Action<Shader> onFound, bool useWatch = false)
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            if(useWatch)
            {
                watch.Start();
            }

            List<Material> materialCache = new List<Material>();
            Queue<GameObject> children = new Queue<GameObject>();
            children.Enqueue(target.gameObject);
            while(children.Count > 0)
            {
                GameObject child = children.Dequeue();
                if(child == null)
                {
                    continue;
                }

                for(int idx = 0; idx < child.transform.childCount; ++idx)
                {
                    children.Enqueue(child.transform.GetChild(idx).gameObject);
                }

                foreach(Renderer childRenderers in child.transform.GetComponents<Renderer>())
                {
                    if(childRenderers == null)
                    {
                        continue;
                    }

                    foreach(Material sharedMaterial in childRenderers.sharedMaterials)
                    {
                        if(materialCache.Any(cacheMtl => sharedMaterial == cacheMtl)) // did we already look at this one?
                        {
                            continue;
                        }

                        // Skip empty material slots, or materials without shaders.
                        // Both will end up using the magenta error shader.
                        if(sharedMaterial == null || sharedMaterial.shader == null)
                        {
                            continue;
                        }

                        if(whitelist.All(okayShaderName => sharedMaterial.shader.name != okayShaderName))
                        {
                            onFound(sharedMaterial.shader);
                            yield return null;
                        }

                        materialCache.Add(sharedMaterial);
                    }

                    if(!useWatch || watch.ElapsedMilliseconds <= 1)
                    {
                        continue;
                    }

                    yield return null;
                    watch.Reset();
                }
            }
        }
    }
}