Tanoda
VariantEnabler.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.IO;
11using System.Linq;
12using System.Text.RegularExpressions;
13using System.Collections.Generic;
14using UnityEngine;
15using UnityEditor;
16
17public static class VariantEnabler {
18 public const string EARLY_OUT_KEYWORD = "#pragma";
19
20 public static Regex isSurfaceShaderRegex = new Regex(@"#pragma\s+surface\s+surf");
21 public static Regex disabledVariantRegex = new Regex(@"/{2,}\s*#pragma\s+shader_feature\s+_\s+GRAPHIC_RENDERER");
22 public static Regex enabledVariantRegex = new Regex(@"^\s*#pragma\s+shader_feature\s+_\s+GRAPHIC_RENDERER");
23
24 public static bool IsSurfaceShader(Shader shader) {
25 ShaderInfo info;
26 if (tryGetShaderInfo(shader, out info)) {
27 return info.isSurfaceShader;
28 } else {
29 return false;
30 }
31 }
32
33 public static bool DoesShaderHaveVariantsDisabled(Shader shader) {
34 ShaderInfo info;
35 if (tryGetShaderInfo(shader, out info)) {
36 return info.doesHaveShaderVariantsDisabled;
37 } else {
38 return false;
39 }
40 }
41
42 public static void SetShaderVariantsEnabled(Shader shader, bool enable) {
43 string path = AssetDatabase.GetAssetPath(shader);
44 if (string.IsNullOrEmpty(path)) {
45 return;
46 }
47
48 _infoCache.Remove(path);
49
50 string[] lines = File.ReadAllLines(path);
51 using (var writer = File.CreateText(path)) {
52 foreach (var line in lines) {
53 if (enable && disabledVariantRegex.IsMatch(line)) {
54 writer.WriteLine(line.Replace("/", " "));
55 } else if (!enable && enabledVariantRegex.IsMatch(line)) {
56 var startEnum = line.TakeWhile(c => char.IsWhiteSpace(c));
57 int count = Mathf.Max(0, startEnum.Count() - 2);
58 var start = new string(startEnum.Take(count).ToArray());
59 writer.WriteLine(start + "//" + line.TrimStart());
60 } else {
61 writer.WriteLine(line);
62 }
63 }
64 }
65 }
66
67 private static Dictionary<string, ShaderInfo> _infoCache = new Dictionary<string, ShaderInfo>();
68 private static bool tryGetShaderInfo(Shader shader, out ShaderInfo info) {
69 string path = AssetDatabase.GetAssetPath(shader);
70 if (string.IsNullOrEmpty(path)) {
71 info = default(ShaderInfo);
72 return false;
73 }
74
75 DateTime modifiedTime = File.GetLastWriteTime(path);
76
77 if (_infoCache.TryGetValue(path, out info)) {
78 //If the check time is newer than the modified time, return cached results
79 if (modifiedTime < info.checkTime) {
80 return true;
81 }
82 }
83
84 info.isSurfaceShader = false;
85 info.doesHaveShaderVariantsDisabled = false;
86 info.checkTime = modifiedTime;
87
88 string[] lines = File.ReadAllLines(path);
89 foreach (var line in lines) {
90 if (!line.Contains(EARLY_OUT_KEYWORD)) {
91 continue;
92 }
93
94 if (disabledVariantRegex.IsMatch(line)) {
95 info.doesHaveShaderVariantsDisabled = true;
96 }
97
98 if (isSurfaceShaderRegex.IsMatch(line)) {
99 info.isSurfaceShader = true;
100 }
101 }
102
103 _infoCache[path] = info;
104 return true;
105 }
106
107 private struct ShaderInfo {
108 public bool doesHaveShaderVariantsDisabled;
109 public bool isSurfaceShader;
110 public DateTime checkTime;
111 }
112}