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
|
// !! AI ARTIFACT !!
// This code was originally generated by Claude 3.5 Sonnet.
// I wanted to write this tooling like I want a fucking hole in the head so I
// kindly asked Claude to write it for me. It's shitty and poorly designed, but
// it works well enough for my purposes.
// It has been slightly tweaked by me, and validated on *this* codebase. It is
// provided with no warranty.
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
public class ShaderInlinerV2 : EditorWindow
{
private string inputShaderPath;
private string outputShaderPath;
private int maxOutputLines = 10000000; // 10 million lines limit
private int currentLineCount = 0;
[MenuItem("Tools/yum_food/Shader Inliner (v2)")]
public static void ShowWindow()
{
GetWindow<ShaderInlinerV2>("Shader Inliner");
}
private void OnGUI()
{
GUILayout.Label("Shader Inliner", EditorStyles.boldLabel);
inputShaderPath = EditorGUILayout.TextField("Input Shader Path", inputShaderPath);
if (GUILayout.Button("Select Input Shader"))
{
inputShaderPath = EditorUtility.OpenFilePanel("Select Shader", "", "shader");
}
maxOutputLines = EditorGUILayout.IntField("Max Output Lines", maxOutputLines);
if (GUILayout.Button("Inline Shader"))
{
if (string.IsNullOrEmpty(inputShaderPath))
{
EditorUtility.DisplayDialog("Error", "Please select an input shader.", "OK");
return;
}
InlineShader();
}
}
private void InlineShader()
{
currentLineCount = 0;
string shaderContent = File.ReadAllText(inputShaderPath);
string inlinedShader = ProcessShader(shaderContent, Path.GetDirectoryName(inputShaderPath));
string fileName = Path.GetFileNameWithoutExtension(inputShaderPath);
outputShaderPath = Path.Combine(Path.GetDirectoryName(inputShaderPath), $"{fileName}_inlined.shader");
File.WriteAllText(outputShaderPath, inlinedShader);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("Success",
$"Inlined shader saved to:\n{outputShaderPath}\nTotal lines: {currentLineCount}", "OK");
}
private string ProcessShader(string content, string basePath)
{
// Update shader name
content = Regex.Replace(content, @"Shader\s+""(.+?)""", match =>
{
string shaderName = match.Groups[1].Value;
return $"Shader \"{shaderName}_inlined\"";
});
// Count initial lines
currentLineCount += content.Split('\n').Length;
if (currentLineCount > maxOutputLines)
{
Debug.LogError($"Maximum line count exceeded: {currentLineCount}");
return content;
}
// Process all includes, regardless of whether they're in CGPROGRAM blocks
content = ProcessIncludes(content, basePath);
// Check for mismatched preprocessor macros in the entire shader
CheckMismatchedMacros(content);
return content;
}
private string ProcessIncludes(string content, string basePath)
{
string pattern = @"#include\s+""(.+?)""";
return Regex.Replace(content, pattern, match =>
{
string includePath = match.Groups[1].Value;
string fullPath = Path.Combine(basePath, includePath);
if (File.Exists(fullPath))
{
string includeContent = File.ReadAllText(fullPath);
// Count the lines in the included file
int includeLines = includeContent.Split('\n').Length;
currentLineCount += includeLines;
// Check if we've exceeded the line limit
if (currentLineCount > maxOutputLines)
{
Debug.LogError($"Maximum line count exceeded ({currentLineCount} lines) while including: {includePath}");
return $"// ERROR: Maximum line count exceeded while including: {includePath}";
}
// Process includes recursively
return ProcessIncludes(includeContent, Path.GetDirectoryName(fullPath));
}
else
{
Debug.LogWarning($"Include file not found: {fullPath}");
return match.Value;
}
});
}
private void CheckMismatchedMacros(string content)
{
var stack = new Stack<string>();
var lines = content.Split('\n');
var macroPattern = @"^\s*#(if|ifdef|ifndef|elif|else|endif|if\s+defined)";
for (int i = 0; i < lines.Length; i++)
{
var line = lines[i].Trim();
var match = Regex.Match(line, macroPattern);
if (match.Success)
{
var directive = match.Groups[1].Value;
switch (directive)
{
case "if":
case "ifdef":
case "ifndef":
case "if defined":
stack.Push(directive);
break;
case "elif":
if (stack.Count == 0 || (stack.Peek() != "if" && stack.Peek() != "elif"))
{
Debug.LogError($"Mismatched #elif at line {i + 1}");
}
else
{
stack.Pop();
stack.Push("elif");
}
break;
case "else":
if (stack.Count == 0 || (stack.Peek() != "if" && stack.Peek() != "elif"))
{
Debug.LogError($"Mismatched #else at line {i + 1}");
}
else
{
stack.Pop();
stack.Push("else");
}
break;
case "endif":
if (stack.Count == 0)
{
Debug.LogError($"Mismatched #endif at line {i + 1}");
}
else
{
stack.Pop();
}
break;
}
}
}
if (stack.Count > 0)
{
Debug.LogError($"Unclosed preprocessor directives: {string.Join(", ", stack)}");
}
}
}
|