-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathtest_generate_UnityToon.cs
More file actions
265 lines (222 loc) · 8.71 KB
/
test_generate_UnityToon.cs
File metadata and controls
265 lines (222 loc) · 8.71 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
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
try
{
// Test reading the common properties shader
string commonPropertiesPath = "com.unity.toonshader/Runtime/Shaders/Common/Parts/CommonProperties.shaderblock";
string commonPropertiesShader = File.ReadAllText(commonPropertiesPath);
if (string.IsNullOrEmpty(commonPropertiesShader))
{
Console.WriteLine("ERROR: Common properties shader file is empty or could not be read");
return;
}
string commonProperties = ExtractProperties(commonPropertiesShader);
if (string.IsNullOrEmpty(commonProperties))
{
Console.WriteLine("ERROR: Failed to extract properties from CommonProperties.shaderblock");
return;
}
Console.WriteLine($"Successfully extracted common properties. Length: {commonProperties.Length} characters");
// Test reading the tessellation properties shader
string tessellationPropertiesPath = "com.unity.toonshader/Runtime/Shaders/Common/Parts/TessellationProperties.shaderblock";
string tessellationPropertiesShader = File.ReadAllText(tessellationPropertiesPath);
if (string.IsNullOrEmpty(tessellationPropertiesShader))
{
Console.WriteLine("ERROR: Tessellation properties shader file is empty or could not be read");
return;
}
string tessellationProperties = ExtractProperties(tessellationPropertiesShader);
if (string.IsNullOrEmpty(tessellationProperties))
{
Console.WriteLine("ERROR: Failed to extract properties from TessellationProperties.shaderblock");
return;
}
Console.WriteLine($"Successfully extracted tessellation properties. Length: {tessellationProperties.Length} characters");
// Test reading the original shader files
string unityToonPath = "com.unity.toonshader/Runtime/Integrated/Shaders/UnityToon.shader";
string unityToonContent = File.ReadAllText(unityToonPath);
if (string.IsNullOrEmpty(unityToonContent))
{
Console.WriteLine("ERROR: UnityToon.shader file is empty or could not be read");
return;
}
Console.WriteLine($"Successfully read UnityToon.shader. Length: {unityToonContent.Length} characters");
// Test the Properties block replacement
string propertiesPattern = @"Properties\s*\{";
Match startMatch = Regex.Match(unityToonContent, propertiesPattern);
if (!startMatch.Success)
{
Console.WriteLine("ERROR: Could not find Properties block start in shader file");
return;
}
Console.WriteLine($"Found Properties block start at position {startMatch.Index}");
// Find the matching closing brace
int startIndex = startMatch.Index;
int braceCount = 0;
int endIndex = startIndex;
bool foundStart = false;
for (int i = startIndex; i < unityToonContent.Length; i++)
{
if (unityToonContent[i] == '{')
{
braceCount++;
foundStart = true;
}
else if (unityToonContent[i] == '}')
{
braceCount--;
if (foundStart && braceCount == 0)
{
endIndex = i;
break;
}
}
}
if (braceCount != 0)
{
Console.WriteLine("ERROR: Could not find matching closing brace for Properties block");
return;
}
Console.WriteLine($"Found Properties block end at position {endIndex}");
// Build new Properties block
StringBuilder newProperties = new StringBuilder();
newProperties.AppendLine(" Properties {");
// Add common properties
string[] commonLines = commonProperties.Split('\n');
int propertyCount = 0;
foreach (string line in commonLines)
{
if (!string.IsNullOrWhiteSpace(line) && !line.TrimStart().StartsWith("//"))
{
newProperties.AppendLine($" {line.Trim()}");
propertyCount++;
}
}
newProperties.AppendLine(" }");
Console.WriteLine($"Generated new Properties block with {propertyCount} properties. Length: {newProperties.Length} characters");
// Test the replacement
string newContent = unityToonContent.Substring(0, startIndex) + newProperties.ToString() + unityToonContent.Substring(endIndex + 1);
newContent = ApplyAutoGeneratedComment(newContent, "//Auto-generated (test output)");
Console.WriteLine($"Generated new shader content. Original length: {unityToonContent.Length}, New length: {newContent.Length}");
// Write test file
string testPath = "UnityToon_Generated_Test.shader";
File.WriteAllText(testPath, newContent);
Console.WriteLine($"Test shader written to {testPath}");
Console.WriteLine("Shader generation test completed successfully!");
try
{
File.Delete(testPath);
}
catch (IOException)
{
}
catch (UnauthorizedAccessException)
{
}
}
catch (Exception e)
{
Console.WriteLine($"ERROR: {e.Message}");
Console.WriteLine(e.StackTrace);
}
}
private static string ExtractProperties(string shaderContent)
{
if (string.IsNullOrEmpty(shaderContent))
{
return null;
}
string propertiesPattern = @"Properties\s*\{";
Match startMatch = Regex.Match(shaderContent, propertiesPattern);
if (!startMatch.Success)
{
return null;
}
int openBraceIndex = shaderContent.IndexOf('{', startMatch.Index);
if (openBraceIndex == -1)
{
return null;
}
int braceCount = 1;
int closeBraceIndex = -1;
for (int i = openBraceIndex + 1; i < shaderContent.Length; i++)
{
char c = shaderContent[i];
if (c == '{')
{
braceCount++;
}
else if (c == '}')
{
braceCount--;
if (braceCount == 0)
{
closeBraceIndex = i;
break;
}
}
}
if (closeBraceIndex == -1)
{
return null;
}
string block = shaderContent.Substring(openBraceIndex + 1, closeBraceIndex - openBraceIndex - 1);
string[] rawLines = block.Split(new[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);
for (int i = 0; i < rawLines.Length; i++)
{
rawLines[i] = rawLines[i].Trim();
}
int start = 0;
int end = rawLines.Length - 1;
while (start <= end && string.IsNullOrEmpty(rawLines[start]))
{
start++;
}
while (end >= start && string.IsNullOrEmpty(rawLines[end]))
{
end--;
}
if (start > end)
{
return string.Empty;
}
StringBuilder result = new StringBuilder();
for (int i = start; i <= end; i++)
{
result.Append(rawLines[i]);
if (i < end)
{
result.Append('\n');
}
}
return result.ToString();
}
private static string ApplyAutoGeneratedComment(string content, string commentLine)
{
const string autoPrefix = "//Auto-generated on ";
string[] lines = content.Split(new[] { "\n" }, StringSplitOptions.None);
if (lines.Length > 0 && lines[0].StartsWith(autoPrefix, StringComparison.Ordinal))
{
lines[0] = commentLine;
}
else
{
string[] updatedLines = new string[lines.Length + 1];
updatedLines[0] = commentLine;
Array.Copy(lines, 0, updatedLines, 1, lines.Length);
lines = updatedLines;
}
string result = string.Join("\n", lines);
if (!result.EndsWith("\n", StringComparison.Ordinal))
{
result += "\n";
}
return result;
}
}