corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using System.Text; |
||
5 | using System.Text.RegularExpressions; |
||
6 | using System.Xml; |
||
7 | |||
8 | namespace VisualParamGenerator |
||
9 | { |
||
10 | class VisualParamGenerator |
||
11 | { |
||
12 | public static readonly System.Globalization.CultureInfo EnUsCulture = |
||
13 | new System.Globalization.CultureInfo("en-us"); |
||
14 | |||
15 | static void Main(string[] args) |
||
16 | { |
||
17 | if (args.Length < 2) |
||
18 | { |
||
19 | Console.WriteLine("Usage: VisualParamGenerator.exe [template.cs] [_VisualParams_.cs]"); |
||
20 | return; |
||
21 | } |
||
22 | else if (!File.Exists(args[0])) |
||
23 | { |
||
24 | Console.WriteLine("Couldn't find file " + args[0]); |
||
25 | return; |
||
26 | } |
||
27 | |||
28 | XmlNodeList list; |
||
29 | TextWriter writer; |
||
30 | |||
31 | try |
||
32 | { |
||
33 | writer = new StreamWriter(args[1]); |
||
34 | } |
||
35 | catch (Exception) |
||
36 | { |
||
37 | Console.WriteLine("Couldn't open " + args[1] + " for writing"); |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | try |
||
42 | { |
||
43 | // Read in the template.cs file and write it to our output |
||
44 | TextReader reader = new StreamReader(args[0]); |
||
45 | writer.WriteLine(reader.ReadToEnd()); |
||
46 | reader.Close(); |
||
47 | } |
||
48 | catch (Exception) |
||
49 | { |
||
50 | Console.WriteLine("Couldn't read from file " + args[0]); |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | try |
||
55 | { |
||
56 | // Read in avatar_lad.xml |
||
57 | Stream stream = OpenMetaverse.Helpers.GetResourceStream("avatar_lad.xml"); |
||
58 | |||
59 | if (stream != null) |
||
60 | { |
||
61 | StreamReader reader = new StreamReader(stream); |
||
62 | XmlDocument doc = new XmlDocument(); |
||
63 | doc.LoadXml(reader.ReadToEnd()); |
||
64 | list = doc.GetElementsByTagName("param"); |
||
65 | } |
||
66 | else |
||
67 | { |
||
68 | Console.WriteLine("Failed to load resource avatar_lad.xml. Are you missing a data folder?"); |
||
69 | return; |
||
70 | } |
||
71 | } |
||
72 | catch (Exception e) |
||
73 | { |
||
74 | Console.WriteLine(e.ToString()); |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | SortedList<int, string> IDs = new SortedList<int, string>(); |
||
79 | Dictionary<int, string> Alphas = new Dictionary<int, string>(); |
||
80 | Dictionary<int, string> Colors = new Dictionary<int, string>(); |
||
81 | |||
82 | StringWriter output = new StringWriter(); |
||
83 | |||
84 | // Make sure we end up with 251 Group-0 VisualParams as a sanity check |
||
85 | int count = 0; |
||
86 | |||
87 | foreach (XmlNode node in list) |
||
88 | { |
||
89 | if (node.Attributes["group"] == null) |
||
90 | { |
||
91 | // Sanity check that a group is assigned |
||
92 | Console.WriteLine("Encountered a param with no group set!"); |
||
93 | continue; |
||
94 | } |
||
95 | if ((node.Attributes["shared"] != null) && (node.Attributes["shared"].Value.Equals("1"))) |
||
96 | { |
||
97 | // This param will have been already been defined |
||
98 | continue; |
||
99 | } |
||
100 | if ((node.Attributes["edit_group"] == null)) |
||
101 | { |
||
102 | // This param is calculated by the client based on other params |
||
103 | continue; |
||
104 | } |
||
105 | |||
106 | // Confirm this is a valid VisualParam |
||
107 | if (node.Attributes["id"] != null && |
||
108 | node.Attributes["name"] != null) |
||
109 | { |
||
110 | try |
||
111 | { |
||
112 | int id = Int32.Parse(node.Attributes["id"].Value); |
||
113 | |||
114 | string bumpAttrib = "false"; |
||
115 | bool skipColor = false; |
||
116 | |||
117 | if (node.ParentNode.Name == "layer") |
||
118 | { |
||
119 | if (node.ParentNode.Attributes["render_pass"] != null && node.ParentNode.Attributes["render_pass"].Value == "bump") |
||
120 | { |
||
121 | bumpAttrib = "true"; |
||
122 | } |
||
123 | |||
124 | for (int nodeNr = 0; nodeNr < node.ParentNode.ChildNodes.Count; nodeNr++) |
||
125 | { |
||
126 | XmlNode lnode = node.ParentNode.ChildNodes[nodeNr]; |
||
127 | |||
128 | if (lnode.Name == "texture") |
||
129 | { |
||
130 | if (lnode.Attributes["local_texture_alpha_only"] != null && lnode.Attributes["local_texture_alpha_only"].Value.ToLower() == "true") |
||
131 | { |
||
132 | skipColor = true; |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | if (node.HasChildNodes) |
||
140 | { |
||
141 | for (int nodeNr = 0; nodeNr < node.ChildNodes.Count; nodeNr++) |
||
142 | { |
||
143 | #region Alpha mask and bumps |
||
144 | if (node.ChildNodes[nodeNr].Name == "param_alpha") |
||
145 | { |
||
146 | XmlNode anode = node.ChildNodes[nodeNr]; |
||
147 | string tga_file = "string.Empty"; |
||
148 | string skip_if_zero = "false"; |
||
149 | string multiply_blend = "false"; |
||
150 | string domain = "0"; |
||
151 | |||
152 | if (anode.Attributes["domain"] != null) |
||
153 | domain = anode.Attributes["domain"].Value; |
||
154 | |||
155 | if (anode.Attributes["tga_file"] != null) |
||
156 | tga_file = string.Format("\"{0}\"", anode.Attributes["tga_file"].Value); ; |
||
157 | |||
158 | if (anode.Attributes["skip_if_zero"] != null && anode.Attributes["skip_if_zero"].Value.ToLower() == "true") |
||
159 | skip_if_zero = "true"; |
||
160 | |||
161 | if (anode.Attributes["multiply_blend"] != null && anode.Attributes["multiply_blend"].Value.ToLower() == "true") |
||
162 | multiply_blend = "true"; |
||
163 | |||
164 | Alphas.Add(id, string.Format("new VisualAlphaParam({0}f, {1}, {2}, {3})", domain, tga_file, skip_if_zero, multiply_blend)); |
||
165 | } |
||
166 | #endregion |
||
167 | #region Colors |
||
168 | else if (node.ChildNodes[nodeNr].Name == "param_color" && node.ChildNodes[nodeNr].HasChildNodes) |
||
169 | { |
||
170 | XmlNode cnode = node.ChildNodes[nodeNr]; |
||
171 | string operation = "VisualColorOperation.Add"; |
||
172 | List<string> colors = new List<string>(); |
||
173 | |||
174 | if (cnode.Attributes["operation"] != null) |
||
175 | { |
||
176 | |||
177 | switch (cnode.Attributes["operation"].Value) |
||
178 | { |
||
179 | case "blend": |
||
180 | operation = "VisualColorOperation.Blend"; |
||
181 | break; |
||
182 | |||
183 | case "multiply": |
||
184 | operation = "VisualColorOperation.Blend"; |
||
185 | break; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | foreach (XmlNode cvalue in cnode.ChildNodes) |
||
190 | { |
||
191 | if (cvalue.Name == "value" && cvalue.Attributes["color"] != null) |
||
192 | { |
||
193 | Match m = Regex.Match(cvalue.Attributes["color"].Value, @"((?<val>\d+)(?:, *)?){4}"); |
||
194 | if (!m.Success) |
||
195 | { |
||
196 | continue; |
||
197 | } |
||
198 | CaptureCollection val = m.Groups["val"].Captures; |
||
199 | colors.Add(string.Format("new Color4({0}, {1}, {2}, {3})", val[0], val[1], val[2], val[3])); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | if (colors.Count > 0 && !skipColor) |
||
204 | { |
||
205 | string colorsStr = string.Join(", ", colors.ToArray()); |
||
206 | Colors.Add(id, string.Format("new VisualColorParam({0}, new Color4[] {{ {1} }})", operation, colorsStr)); |
||
207 | } |
||
208 | } |
||
209 | #endregion |
||
210 | |||
211 | } |
||
212 | } |
||
213 | |||
214 | // Check for duplicates |
||
215 | if (IDs.ContainsKey(id)) |
||
216 | continue; |
||
217 | |||
218 | string name = node.Attributes["name"].Value; |
||
219 | int group = Int32.Parse(node.Attributes["group"].Value); |
||
220 | |||
221 | string wearable = "null"; |
||
222 | if (node.Attributes["wearable"] != null) |
||
223 | wearable = "\"" + node.Attributes["wearable"].Value + "\""; |
||
224 | |||
225 | string label = "String.Empty"; |
||
226 | if (node.Attributes["label"] != null) |
||
227 | label = "\"" + node.Attributes["label"].Value + "\""; |
||
228 | |||
229 | string label_min = "String.Empty"; |
||
230 | if (node.Attributes["label_min"] != null) |
||
231 | label_min = "\"" + node.Attributes["label_min"].Value + "\""; |
||
232 | |||
233 | string label_max = "String.Empty"; |
||
234 | if (node.Attributes["label_max"] != null) |
||
235 | label_max = "\"" + node.Attributes["label_max"].Value + "\""; |
||
236 | |||
237 | float min = Single.Parse(node.Attributes["value_min"].Value, |
||
238 | System.Globalization.NumberStyles.Float, EnUsCulture.NumberFormat); |
||
239 | float max = Single.Parse(node.Attributes["value_max"].Value, |
||
240 | System.Globalization.NumberStyles.Float, EnUsCulture.NumberFormat); |
||
241 | |||
242 | float def; |
||
243 | if (node.Attributes["value_default"] != null) |
||
244 | def = Single.Parse(node.Attributes["value_default"].Value, |
||
245 | System.Globalization.NumberStyles.Float, EnUsCulture.NumberFormat); |
||
246 | else |
||
247 | def = min; |
||
248 | |||
249 | string drivers = "null"; |
||
250 | if (node.HasChildNodes) |
||
251 | { |
||
252 | for (int nodeNr = 0; nodeNr < node.ChildNodes.Count; nodeNr++) |
||
253 | { |
||
254 | XmlNode cnode = node.ChildNodes[nodeNr]; |
||
255 | |||
256 | if (cnode.Name == "param_driver" && cnode.HasChildNodes) |
||
257 | { |
||
258 | List<string> driverIDs = new List<string>(); |
||
259 | foreach (XmlNode dnode in cnode.ChildNodes) |
||
260 | { |
||
261 | if (dnode.Name == "driven" && dnode.Attributes["id"] != null) |
||
262 | { |
||
263 | driverIDs.Add(dnode.Attributes["id"].Value); |
||
264 | } |
||
265 | } |
||
266 | |||
267 | if (driverIDs.Count > 0) |
||
268 | { |
||
269 | drivers = string.Format("new int[] {{ {0} }}", string.Join(", ", driverIDs.ToArray())); |
||
270 | } |
||
271 | |||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | IDs.Add(id, |
||
277 | String.Format(" Params[{0}] = new VisualParam({0}, \"{1}\", {2}, {3}, {4}, {5}, {6}, {7}f, {8}f, {9}f, {10}, {11}, ", |
||
278 | id, name, group, wearable, label, label_min, label_max, def, min, max, bumpAttrib, drivers)); |
||
279 | |||
280 | if (group == 0) |
||
281 | ++count; |
||
282 | } |
||
283 | catch (Exception e) |
||
284 | { |
||
285 | Console.WriteLine(e.ToString()); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | if (count != 251) |
||
291 | { |
||
292 | Console.WriteLine("Ended up with the wrong number of Group-0 VisualParams! Exiting..."); |
||
293 | return; |
||
294 | } |
||
295 | |||
296 | // Now that we've collected all the entries and sorted them, add them to our output buffer |
||
297 | foreach (KeyValuePair<int, string> line in IDs) |
||
298 | { |
||
299 | output.Write(line.Value); |
||
300 | |||
301 | if (Alphas.ContainsKey(line.Key)) |
||
302 | { |
||
303 | output.Write(Alphas[line.Key] + ", "); |
||
304 | } |
||
305 | else |
||
306 | { |
||
307 | output.Write("null, "); |
||
308 | } |
||
309 | |||
310 | if (Colors.ContainsKey(line.Key)) |
||
311 | { |
||
312 | output.Write(Colors[line.Key]); |
||
313 | } |
||
314 | else |
||
315 | { |
||
316 | output.Write("null"); |
||
317 | } |
||
318 | |||
319 | |||
320 | output.WriteLine(");"); |
||
321 | } |
||
322 | |||
323 | output.Write(" }" + Environment.NewLine); |
||
324 | output.Write(" }" + Environment.NewLine); |
||
325 | output.Write("}" + Environment.NewLine); |
||
326 | |||
327 | try |
||
328 | { |
||
329 | writer.Write(output.ToString()); |
||
330 | writer.Close(); |
||
331 | } |
||
332 | catch (Exception e) |
||
333 | { |
||
334 | Console.WriteLine(e.ToString()); |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 | } |